Support urn:xmpp:privilege:2

This commit is contained in:
Bohdan Horbeshko 2023-08-02 17:08:06 -04:00
parent 608f675512
commit c03ccfdfb7
7 changed files with 58 additions and 21 deletions

View file

@ -2,7 +2,7 @@
COMMIT := $(shell git rev-parse --short HEAD) COMMIT := $(shell git rev-parse --short HEAD)
TD_COMMIT := "8517026415e75a8eec567774072cbbbbb52376c1" TD_COMMIT := "8517026415e75a8eec567774072cbbbbb52376c1"
VERSION := "v1.7.3" VERSION := "v1.7.4"
MAKEOPTS := "-j4" MAKEOPTS := "-j4"
all: all:

View file

@ -15,7 +15,7 @@ import (
goxmpp "gosrc.io/xmpp" goxmpp "gosrc.io/xmpp"
) )
var version string = "1.7.3" var version string = "1.7.4"
var commit string var commit string
var sm *goxmpp.StreamManager var sm *goxmpp.StreamManager

View file

@ -384,7 +384,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
} }
case "config": case "config":
if len(args) > 1 { if len(args) > 1 {
if !gateway.MessageOutgoingPermission && args[0] == "carbons" && args[1] == "true" { if gateway.MessageOutgoingPermissionVersion == 0 && args[0] == "carbons" && args[1] == "true" {
return "The server did not allow to enable carbons" return "The server did not allow to enable carbons"
} }

View file

@ -842,7 +842,7 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string,
prefix := []string{} prefix := []string{}
// message direction // message direction
var directionChar string var directionChar string
if !isPM || !gateway.MessageOutgoingPermission || !c.Session.Carbons { if !isPM || gateway.MessageOutgoingPermissionVersion == 0 || !c.Session.Carbons {
if c.Session.AsciiArrows { if c.Session.AsciiArrows {
if message.IsOutgoing { if message.IsOutgoing {
directionChar = "> " directionChar = "> "
@ -914,7 +914,7 @@ func (c *Client) ensureDownloadFile(file *client.File) *client.File {
func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) { func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
var isPM bool var isPM bool
var err error var err error
if gateway.MessageOutgoingPermission && c.Session.Carbons { if gateway.MessageOutgoingPermissionVersion > 0 && c.Session.Carbons {
isPM, err = c.IsPM(chatId) isPM, err = c.IsPM(chatId)
if err != nil { if err != nil {
log.Errorf("Could not determine if chat is PM: %v", err) log.Errorf("Could not determine if chat is PM: %v", err)

View file

@ -154,12 +154,19 @@ type CarbonSent struct {
} }
// ComponentPrivilege is from XEP-0356 // ComponentPrivilege is from XEP-0356
type ComponentPrivilege struct { type ComponentPrivilege1 struct {
XMLName xml.Name `xml:"urn:xmpp:privilege:1 privilege"` XMLName xml.Name `xml:"urn:xmpp:privilege:1 privilege"`
Perms []ComponentPerm `xml:"perm"` Perms []ComponentPerm `xml:"perm"`
Forwarded stanza.Forwarded `xml:"urn:xmpp:forward:0 forwarded"` Forwarded stanza.Forwarded `xml:"urn:xmpp:forward:0 forwarded"`
} }
// ComponentPrivilege is from XEP-0356
type ComponentPrivilege2 struct {
XMLName xml.Name `xml:"urn:xmpp:privilege:2 privilege"`
Perms []ComponentPerm `xml:"perm"`
Forwarded stanza.Forwarded `xml:"urn:xmpp:forward:0 forwarded"`
}
// ComponentPerm is from XEP-0356 // ComponentPerm is from XEP-0356
type ComponentPerm struct { type ComponentPerm struct {
XMLName xml.Name `xml:"perm"` XMLName xml.Name `xml:"perm"`
@ -227,7 +234,12 @@ func (c CarbonSent) Namespace() string {
} }
// Namespace is a namespace! // Namespace is a namespace!
func (c ComponentPrivilege) Namespace() string { func (c ComponentPrivilege1) Namespace() string {
return c.XMLName.Space
}
// Namespace is a namespace!
func (c ComponentPrivilege2) Namespace() string {
return c.XMLName.Space return c.XMLName.Space
} }
@ -297,11 +309,17 @@ func init() {
"sent", "sent",
}, CarbonSent{}) }, CarbonSent{})
// component privilege // component privilege v1
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{ stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
"urn:xmpp:privilege:1", "urn:xmpp:privilege:1",
"privilege", "privilege",
}, ComponentPrivilege{}) }, ComponentPrivilege1{})
// component privilege v2
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
"urn:xmpp:privilege:2",
"privilege",
}, ComponentPrivilege2{})
// message edit // message edit
stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{ stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{

View file

@ -38,8 +38,8 @@ var IdsDB badger.IdsDB
// were changed and need to be re-flushed to the YamlDB // were changed and need to be re-flushed to the YamlDB
var DirtySessions = false var DirtySessions = false
// MessageOutgoingPermission allows to fake outgoing messages by foreign JIDs // MessageOutgoingPermissionVersion contains a XEP-0356 version to fake outgoing messages by foreign JIDs
var MessageOutgoingPermission = false var MessageOutgoingPermissionVersion = 0
// SendMessage creates and sends a message stanza // SendMessage creates and sends a message stanza
func SendMessage(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, isCarbon bool) { func SendMessage(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, isCarbon bool) {
@ -142,11 +142,19 @@ func sendMessageWrapper(to string, from string, body string, id string, componen
To: toJid.Domain, To: toJid.Domain,
}, },
} }
privilegeMessage.Extensions = append(privilegeMessage.Extensions, extensions.ComponentPrivilege{ if MessageOutgoingPermissionVersion == 2 {
privilegeMessage.Extensions = append(privilegeMessage.Extensions, extensions.ComponentPrivilege2{
Forwarded: stanza.Forwarded{ Forwarded: stanza.Forwarded{
Stanza: carbonMessage, Stanza: carbonMessage,
}, },
}) })
} else {
privilegeMessage.Extensions = append(privilegeMessage.Extensions, extensions.ComponentPrivilege1{
Forwarded: stanza.Forwarded{
Stanza: carbonMessage,
},
})
}
sendMessage(&privilegeMessage, component) sendMessage(&privilegeMessage, component)
} else { } else {
sendMessage(&message, component) sendMessage(&message, component)

View file

@ -209,14 +209,25 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
} }
if msg.Body == "" { if msg.Body == "" {
var privilege extensions.ComponentPrivilege var privilege1 extensions.ComponentPrivilege1
if ok := msg.Get(&privilege); ok { if ok := msg.Get(&privilege1); ok {
log.Debugf("privilege: %#v", privilege) log.Debugf("privilege1: %#v", privilege1)
} }
for _, perm := range privilege.Perms { for _, perm := range privilege1.Perms {
if perm.Access == "message" && perm.Type == "outgoing" { if perm.Access == "message" && perm.Type == "outgoing" {
gateway.MessageOutgoingPermission = true gateway.MessageOutgoingPermissionVersion = 1
}
}
var privilege2 extensions.ComponentPrivilege2
if ok := msg.Get(&privilege2); ok {
log.Debugf("privilege2: %#v", privilege2)
}
for _, perm := range privilege2.Perms {
if perm.Access == "message" && perm.Type == "outgoing" {
gateway.MessageOutgoingPermissionVersion = 2
} }
} }
} }