diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2023-03-15 00:16:02 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2023-03-15 00:16:02 +0300 |
commit | 90807b2d9e0565629a913d3b28b09c5fc9d746e6 (patch) | |
tree | 80c56bc83a926d2e0573f58a5786cd964b91775f /xmpp | |
parent | 7bd165a738d6c39d2f9e530c67118f8c639ffb0b (diff) |
Convert XEP-0461 replies to Telegram
Diffstat (limited to 'xmpp')
-rw-r--r-- | xmpp/extensions/extensions.go | 12 | ||||
-rw-r--r-- | xmpp/handlers.go | 41 |
2 files changed, 46 insertions, 7 deletions
diff --git a/xmpp/extensions/extensions.go b/xmpp/extensions/extensions.go index 7e44bae..c982581 100644 --- a/xmpp/extensions/extensions.go +++ b/xmpp/extensions/extensions.go @@ -121,22 +121,22 @@ type Reply struct { // Fallback is from XEP-0428 type Fallback struct { - XMLName xml.Name `xml:"urn:xmpp:fallback:0 fallback"` - For string `xml:"for,attr"` - Body []FallbackBody - Subject []FallbackSubject + XMLName xml.Name `xml:"urn:xmpp:fallback:0 fallback"` + For string `xml:"for,attr"` + Body []FallbackBody `xml:"urn:xmpp:fallback:0 body"` + Subject []FallbackSubject `xml:"urn:xmpp:fallback:0 subject"` } // FallbackBody is from XEP-0428 type FallbackBody struct { - XMLName xml.Name `xml:"body"` + XMLName xml.Name `xml:"urn:xmpp:fallback:0 body"` Start string `xml:"start,attr"` End string `xml:"end,attr"` } // FallbackSubject is from XEP-0428 type FallbackSubject struct { - XMLName xml.Name `xml:"subject"` + XMLName xml.Name `xml:"urn:xmpp:fallback:0 subject"` Start string `xml:"start,attr"` End string `xml:"end,attr"` } diff --git a/xmpp/handlers.go b/xmpp/handlers.go index b023bc5..acf62a2 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -99,7 +99,46 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) { toID, ok := toToID(msg.To) if ok { - session.ProcessOutgoingMessage(toID, msg.Body, msg.From) + var reply extensions.Reply + var fallback extensions.Fallback + msg.Get(&reply) + msg.Get(&fallback) + log.Debugf("reply: %#v", reply) + log.Debugf("fallback: %#v", fallback) + + var replyId int64 + var err error + text := msg.Body + if len(reply.Id) > 0 { + id := reply.Id + if id[0] == 'e' { + id = id[1:] + } + replyId, err = strconv.ParseInt(id, 10, 64) + if err != nil { + log.Warn(errors.Wrap(err, "Failed to parse message ID!")) + } + + if replyId != 0 && fallback.For == "urn:xmpp:reply:0" && len(fallback.Body) > 0 { + body := fallback.Body[0] + var start, end int64 + start, err = strconv.ParseInt(body.Start, 10, 64) + if err != nil { + log.WithFields(log.Fields{ + "start": body.Start, + }).Warn(errors.Wrap(err, "Failed to parse fallback start!")) + } + end, err = strconv.ParseInt(body.End, 10, 64) + if err != nil { + log.WithFields(log.Fields{ + "end": body.End, + }).Warn(errors.Wrap(err, "Failed to parse fallback end!")) + } + text = text[:start] + text[end:] + } + } + + session.ProcessOutgoingMessage(toID, text, msg.From, replyId) return } else { toJid, err := stanza.NewJid(msg.To) |