From a167d5d7245833f25a853828de685cbd2f80f473 Mon Sep 17 00:00:00 2001 From: LDA Date: Sun, 7 Jul 2024 21:52:38 +0200 Subject: [PATCH] [ADD/WIP] Make basic XMPP->Matrix redactions I'll need to use the actual redaction endpoint eventually, hence the WIP status. --- src/XMPP/Stanza.c | 28 ++++++++++++++++++++++++++++ src/XMPPThread.c | 20 ++++++++++++++++++-- src/include/XMPP.h | 1 + 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/XMPP/Stanza.c b/src/XMPP/Stanza.c index be70c67..1902a5f 100644 --- a/src/XMPP/Stanza.c +++ b/src/XMPP/Stanza.c @@ -290,6 +290,34 @@ XMPPGetReplacedID(XMLElement *stanza) return replace ? HashMapGet(replace->attrs, "id") : NULL; } char * +XMPPGetRetractedID(XMLElement *stanza) +{ + XMLElement *retract = XMLookForTKV( + stanza, "retract", + "xmlns", "urn:xmpp:message-retract:1" + ); + char *id = retract ? HashMapGet(retract->attrs, "id") : NULL; + if (!id) + { + /* Pretend its fastened */ + XMLElement *fasten = XMLookForTKV( + stanza, "apply-to", + "xmlns", "urn:xmpp:fasten:0" + ); + retract = XMLookForTKV( + fasten, "retract", + "xmlns", "urn:xmpp:message-retract:0" + ); + + if (retract) + { + id = HashMapGet(fasten->attrs, "id"); + } + } + + return id; +} +char * XMPPGetReply(XMLElement *elem) { XMLElement *rep = XMLookForTKV(elem, "reply", "xmlns", "urn:xmpp:reply:0"); diff --git a/src/XMPPThread.c b/src/XMPPThread.c index 2bdb04a..0ed5421 100644 --- a/src/XMPPThread.c +++ b/src/XMPPThread.c @@ -527,6 +527,7 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr) char *encoded = ParseeEncodeJID(args->config, decode_from, false); char *event_id = NULL; char *replaced = XMPPGetReplacedID(stanza); + char *retracted = XMPPGetRetractedID(stanza); char *reply_to = XMPPGetReply(stanza); bool chat = false; @@ -618,11 +619,26 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr) Free(event_id); event_id = NULL; } + else if (retracted) + { + /* TODO: Use an actual redact. Not doing it now because it's not + * fun to do at the moment... */ + event_id = ParseeGetEventFromID(args, stanza, retracted); + Free(ASSend( + args->config, mroom_id, encoded, "m.room.message", + MatrixCreateReplace(event_id, "[Retracted]") + )); + ParseePushAllStanza(args, stanza, event_id); + pthread_mutex_unlock(&thr->info->chk_lock); + + Free(event_id); + event_id = NULL; + } else { /* TODO: Use HTML-formatted bodies, and respect the fallback - * trims the stanza provides us if possible. Element does - * not like raw bodies on replies too. Go figure. */ + * trims the stanza provides us if possible. Element does not + * like raw bodies on replies too. Go figure. */ size_t off = reply_to ? ParseeFindDatastart(data->data) : 0; HashMap *ev = MatrixCreateMessage(data->data + off); diff --git a/src/include/XMPP.h b/src/include/XMPP.h index 13e6c8e..7206978 100644 --- a/src/include/XMPP.h +++ b/src/include/XMPP.h @@ -67,6 +67,7 @@ extern char * XMPPGetOriginID(XMLElement *); /* Returns the origin ID of the replaced stanza, if the current one * is a replacement notice */ extern char * XMPPGetReplacedID(XMLElement *); +extern char * XMPPGetRetractedID(XMLElement *); /* Get the replied-to stanza ID, if existent. */ extern char * XMPPGetReply(XMLElement *elem);