From 90807b2d9e0565629a913d3b28b09c5fc9d746e6 Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Tue, 14 Mar 2023 17:16:02 -0400 Subject: Convert XEP-0461 replies to Telegram --- xmpp/handlers.go | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'xmpp/handlers.go') 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) -- cgit v1.2.3