aboutsummaryrefslogtreecommitdiff
path: root/xmpp
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2023-06-05 11:22:13 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2023-06-05 11:22:13 +0300
commit7215d11d7973b9896c6223938649c75165fa3ae7 (patch)
tree0aa0bf028cc899a283eb55654ee7618eba167681 /xmpp
parenta5c90340ad2da16a68ddcbfa3d9573f0664067ca (diff)
XEP-0308 message editing
Diffstat (limited to 'xmpp')
-rw-r--r--xmpp/extensions/extensions.go17
-rw-r--r--xmpp/handlers.go43
2 files changed, 59 insertions, 1 deletions
diff --git a/xmpp/extensions/extensions.go b/xmpp/extensions/extensions.go
index 78de47d..2d547af 100644
--- a/xmpp/extensions/extensions.go
+++ b/xmpp/extensions/extensions.go
@@ -180,6 +180,12 @@ type ClientMessage struct {
Extensions []stanza.MsgExtension `xml:",omitempty"`
}
+// Replace is from XEP-0308
+type Replace struct {
+ XMLName xml.Name `xml:"urn:xmpp:message-correct:0 replace"`
+ Id string `xml:"id,attr"`
+}
+
// Namespace is a namespace!
func (c PresenceNickExtension) Namespace() string {
return c.XMLName.Space
@@ -225,6 +231,11 @@ func (c ComponentPrivilege) Namespace() string {
return c.XMLName.Space
}
+// Namespace is a namespace!
+func (c Replace) Namespace() string {
+ return c.XMLName.Space
+}
+
// Name is a packet name
func (ClientMessage) Name() string {
return "message"
@@ -291,4 +302,10 @@ func init() {
"urn:xmpp:privilege:1",
"privilege",
}, ComponentPrivilege{})
+
+ // message edit
+ stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{
+ "urn:xmpp:message-correct:0",
+ "replace",
+ }, Replace{})
}
diff --git a/xmpp/handlers.go b/xmpp/handlers.go
index 5178acd..51fd831 100644
--- a/xmpp/handlers.go
+++ b/xmpp/handlers.go
@@ -102,10 +102,13 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
if ok {
var reply extensions.Reply
var fallback extensions.Fallback
+ var replace extensions.Replace
msg.Get(&reply)
msg.Get(&fallback)
+ msg.Get(&replace)
log.Debugf("reply: %#v", reply)
log.Debugf("fallback: %#v", fallback)
+ log.Debugf("replace: %#v", replace)
var replyId int64
var err error
@@ -138,8 +141,46 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
text = text[:start] + text[end:]
}
}
+ var replaceId int64
+ if replace.Id != "" {
+ chatId, msgId, err := gateway.IdsDB.GetByXmppId(session.Session.Login, bare, replace.Id)
+ if err == nil {
+ if chatId != toID {
+ gateway.SendTextMessage(msg.From, strconv.FormatInt(toID, 10), "<ERROR: Chat mismatch>", component)
+ return
+ }
+ replaceId = msgId
+ log.Debugf("replace tg: %#v %#v", chatId, msgId)
+ } else {
+ gateway.SendTextMessage(msg.From, strconv.FormatInt(toID, 10), "<ERROR: Could not find matching message to edit>", component)
+ return
+ }
+ }
- session.ProcessOutgoingMessage(toID, text, msg.From, msg.Id, replyId)
+ tgMessageId := session.ProcessOutgoingMessage(toID, text, msg.From, replyId, replaceId)
+ if tgMessageId != 0 {
+ if replaceId != 0 {
+ // not needed (is it persistent among clients though?)
+ /* err = gateway.IdsDB.ReplaceIdPair(session.Session.Login, bare, replace.Id, msg.Id, tgMessageId)
+ if err != nil {
+ log.Errorf("Failed to replace id %v with %v %v", replace.Id, msg.Id, tgMessageId)
+ } */
+ } else {
+ err = gateway.IdsDB.Set(session.Session.Login, bare, toID, tgMessageId, msg.Id)
+ if err != nil {
+ log.Errorf("Failed to save ids %v/%v %v", toID, tgMessageId, msg.Id)
+ }
+ }
+ } else {
+ /*
+ // if a message failed to edit on Telegram side, match new XMPP ID with old Telegram ID anyway
+ if replaceId != 0 {
+ err = gateway.IdsDB.ReplaceXmppId(session.Session.Login, bare, replace.Id, msg.Id)
+ if err != nil {
+ log.Errorf("Failed to replace id %v with %v", replace.Id, msg.Id)
+ }
+ } */
+ }
return
} else {
toJid, err := stanza.NewJid(msg.To)