From 14ebef53ced32caaf6d42c8170ec0ce836c5f508 Mon Sep 17 00:00:00 2001 From: LDA Date: Wed, 19 Jun 2024 13:45:26 +0200 Subject: [PATCH] [MOD] Die on loopback stanza Dirty hack to a memory problem. --- README.MD | 4 ++++ src/Events.c | 16 ++++++++++++++++ src/Main.c | 34 +++++++++++++++++++++++++++++----- src/XMPP/Component.c | 2 +- src/XMPP/Stanza.c | 21 ++++++++++++++------- src/XMPPThread.c | 13 ++++++++++--- src/include/Matrix.h | 3 +++ 7 files changed, 77 insertions(+), 16 deletions(-) diff --git a/README.MD b/README.MD index 32f476b..09172e9 100644 --- a/README.MD +++ b/README.MD @@ -12,6 +12,10 @@ I hate Bifrost. I also wanted to dip my toes in XMPP, XML, and bridges a bit. Al this means that I can integrate Parsee with KappaChat however I wish it to be, which allows me to mess around with a codebase I'm already familiar with. +### "Why not just use Matrix lol" +### "Why not just use XMPP lol" +These two having the same answer should be enough information. Also can I *just* have fun? + ~~Also dependency bad.~~ ## BUILDING diff --git a/src/Events.c b/src/Events.c index 9e4a620..1bd099a 100644 --- a/src/Events.c +++ b/src/Events.c @@ -15,3 +15,19 @@ MatrixCreateNotice(char *body) return map; } + +HashMap * +MatrixCreateMessage(char *body) +{ + HashMap *map; + if (!body) + { + return NULL; + } + + map = HashMapCreate(); + HashMapSet(map, "msgtype", JsonValueString("m.text")); + HashMapSet(map, "body", JsonValueString(body)); + + return map; +} diff --git a/src/Main.c b/src/Main.c index aaf6144..7538431 100644 --- a/src/Main.c +++ b/src/Main.c @@ -1,5 +1,7 @@ #include +#include #include +#include #include #include @@ -12,10 +14,14 @@ static HttpServer *server = NULL; static pthread_t xmpp_thr; +static XMPPComponent *jabber = NULL; + static void SignalHandler(int signal) { size_t i; + XMLElement *message, *body, *data; + char *from; switch (signal) { @@ -27,8 +33,22 @@ SignalHandler(int signal) { return; } + /* Create a loopback stanza, forcing the thread to die */ + from = StrConcat(2, "jabber_die@", jabber->host); + message = XMLCreateTag("message"); + XMLAddAttr(message, "from", from); + XMLAddAttr(message, "to", from); + XMLAddAttr(message, "type", "kill_parsee"); + body = XMLCreateTag("body"); + XMLAddChild(message, body); + + XMLEncode(jabber->stream, message); + StreamFlush(jabber->stream); + XMLFreeElement(message); + Free(from); + HttpServerStop(server); - pthread_cancel(xmpp_thr); + //pthread_cancel(xmpp_thr); break; } } @@ -37,10 +57,9 @@ int Main(void) { HttpServerConfig conf; - ParseeData *data; + ParseeData *data = NULL; const ParseeConfig *parsee_conf; Stream *yaml; - XMPPComponent *jabber; struct sigaction sigAction; Log(LOG_INFO, "%s - v%s", NAME, VERSION); @@ -59,10 +78,14 @@ Main(void) parsee_conf->component_host, parsee_conf->component_port ); - XMPPAuthenticateCompStream( + if (!XMPPAuthenticateCompStream( jabber, parsee_conf->shared_comp_secret - ); + )) + { + Log(LOG_ERR, "Could not connect to XMPP..."); + goto end; + } } Log(LOG_NOTICE, "Setting up local Matrix user..."); @@ -107,6 +130,7 @@ Main(void) HttpServerStart(server); HttpServerJoin(server); +end: HttpServerFree(server); ParseeConfigFree(); ParseeFreeData(conf.handlerArgs); diff --git a/src/XMPP/Component.c b/src/XMPP/Component.c index b101c77..f134d4d 100644 --- a/src/XMPP/Component.c +++ b/src/XMPP/Component.c @@ -136,7 +136,7 @@ XMPPAuthenticateCompStream(XMPPComponent *comp, char *shared) if (ev->type != XML_LEXER_STARTELEM || !StrEquals(ev->element, "stream:stream")) { - Log(LOG_ERR, "Excepted strea:stream element."); + Log(LOG_ERR, "Excepted stream:stream element."); XMLFreeEvent(ev); goto end; } diff --git a/src/XMPP/Stanza.c b/src/XMPP/Stanza.c index 1a1afee..793890e 100644 --- a/src/XMPP/Stanza.c +++ b/src/XMPP/Stanza.c @@ -34,16 +34,23 @@ XMPPSendPlain(XMPPComponent *comp, char *fr, char *to, char *msg, char *type) void XMPPSendPresence(XMPPComponent *comp, char *fr, char *to) { + XMLElement *presence, *x; + char *from; if (!comp || !fr || !to) { return; } - StreamPrintf(comp->stream, - "" - "" - "", - fr, comp->host, - to - ); + + presence = XMLCreateTag("presence"); + x = XMLCreateTag("x"); + XMLAddAttr(presence, "from", (from = StrConcat(3, fr, "@", comp->host))); + XMLAddAttr(presence, "to", to); + XMLAddAttr(x, "xmlns", "http://jabber.org/protocol/muc"); + + XMLAddChild(presence, x); + + XMLEncode(comp->stream, presence); StreamFlush(comp->stream); + XMLFreeElement(presence); + Free(from); } diff --git a/src/XMPPThread.c b/src/XMPPThread.c index 0b4b969..f381574 100644 --- a/src/XMPPThread.c +++ b/src/XMPPThread.c @@ -35,6 +35,14 @@ ParseeXMPPThread(void *argp) if (StrEquals(stanza->name, "message")) { size_t i; + if (StrEquals(HashMapGet(stanza->attrs, "from"), + HashMapGet(stanza->attrs, "to"))) + { + /* There's not a lot of scenarios where we loopback */ + Log(LOG_INFO, "Dropping thread..."); + XMLFreeElement(stanza); + break; + } for (i = 0; i < ArraySize(stanza->children); i++) { XMLElement *child = ArrayGet(stanza->children, i); @@ -55,11 +63,10 @@ ParseeXMPPThread(void *argp) room = ParseeFindDMRoom(args, to, from); data = ArrayGet(body->children, 0); - /* TODO: Manage that out. $[to] corresponds to a Matrix - * user, which we need to find the */ + /* TODO: Consider having rich messages */ ASSend( args->config, room, from_matrix, - "m.room.message", MatrixCreateNotice(data->data) + "m.room.message", MatrixCreateMessage(data->data) ); Free(from_matrix); Free(room); diff --git a/src/include/Matrix.h b/src/include/Matrix.h index 47943b8..c37e67a 100644 --- a/src/include/Matrix.h +++ b/src/include/Matrix.h @@ -8,4 +8,7 @@ extern HashMap * MatrixCreateError(char *err, char *msg); /* Creates the content for a notice message. */ extern HashMap * MatrixCreateNotice(char *body); + +/* Creates the content for a normal message. */ +extern HashMap * MatrixCreateMessage(char *body); #endif