diff --git a/src/Events.c b/src/Events.c index e0d56a4..48ad604 100644 --- a/src/Events.c +++ b/src/Events.c @@ -192,3 +192,14 @@ MatrixCreateReact(char *event, char *body) return map; } +void +MatrixSetReply(HashMap *map, char *event) +{ + if (!map || !event) + { + return; + } + JsonValueFree(JsonSet(map, JsonValueString(event), + 3, "m.relates_to", "m.in_reply_to", "event_id" + )); +} diff --git a/src/XMPP/Stanza.c b/src/XMPP/Stanza.c index a98e71d..e3a73d3 100644 --- a/src/XMPP/Stanza.c +++ b/src/XMPP/Stanza.c @@ -268,3 +268,14 @@ XMPPGetReplacedID(XMLElement *stanza) ); return replace ? HashMapGet(replace->attrs, "id") : NULL; } +char * +XMPPGetReply(XMLElement *elem) +{ + XMLElement *rep = XMLookForTKV(elem, "reply", "xmlns", "urn:xmpp:reply:0"); + if (!rep) + { + return NULL; + } + + return HashMapGet(rep->attrs, "id"); +} diff --git a/src/XMPPThread.c b/src/XMPPThread.c index 065441e..a542a23 100644 --- a/src/XMPPThread.c +++ b/src/XMPPThread.c @@ -90,6 +90,7 @@ MessageStanza(ParseeData *args, XMLElement *stanza) char *id_str = HashMapGet(stanza->attrs, "id"); char *event_id = NULL; char *replaced = XMPPGetReplacedID(stanza); + char *reply_to = XMPPGetReply(stanza); if (ParseeVerifyStanza(args, chat_id, s_id_str) && !replaced) { @@ -145,9 +146,23 @@ MessageStanza(ParseeData *args, XMLElement *stanza) } 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. Go figure. */ + size_t off = + reply_to ? ParseeFindDatastart(data->data) : 0; + HashMap *ev = MatrixCreateMessage(data->data + off); + if (reply_to) + { + char *reply_id = ParseeEventFromSID( + args, chat_id, reply_to + ); + MatrixSetReply(ev, reply_id); + Free(reply_id); + } event_id = ASSend( args->config, mroom_id, encoded, - "m.room.message", MatrixCreateMessage(data->data) + "m.room.message", ev ); } ParseePushStanza(args, chat_id, s_id_str, id_str, event_id, from); diff --git a/src/include/Matrix.h b/src/include/Matrix.h index 9073352..3d86abd 100644 --- a/src/include/Matrix.h +++ b/src/include/Matrix.h @@ -26,6 +26,9 @@ extern HashMap * MatrixCreateNameState(char *name); /* Creates a join membership with a specific nickname */ extern HashMap * MatrixCreateNickChange(char *nick); +/* Modifies a hashmap to create a reply event */ +extern void MatrixSetReply(HashMap *e, char *event); + /* Get the event ID of the reply if existent */ extern char * MatrixGetReply(HashMap *event); extern char * MatrixGetEdit(HashMap *event); diff --git a/src/include/XMPP.h b/src/include/XMPP.h index 0171b3d..d5537b8 100644 --- a/src/include/XMPP.h +++ b/src/include/XMPP.h @@ -70,4 +70,7 @@ extern char * XMPPGetOriginID(XMLElement *); * is a replacement notice */ extern char * XMPPGetReplacedID(XMLElement *); +/* Get the replied-to stanza ID, if existent. */ +extern char * XMPPGetReply(XMLElement *elem); + #endif