[ADD/WIP] Matrix<->XMPP DMs.

There is still this fun issue with the blocking XML parser leaking
memory on pthread_cancel, I'll try to deal with that later, maybe with a
test stanza.

I also apologise for swearing in the last commit. I promise it won't
happen again, Prosody.
This commit is contained in:
LDA 2024-06-18 20:59:35 +02:00
commit 2cc4b0ed17
11 changed files with 325 additions and 63 deletions

75
src/XMPPThread.c Normal file
View file

@ -0,0 +1,75 @@
#include <Parsee.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Str.h>
#include <Matrix.h>
#include <XMPP.h>
#include <XML.h>
#include <AS.h>
void *
ParseeXMPPThread(void *argp)
{
ParseeData *args = argp;
XMPPComponent *jabber = args->jabber;
XMLElement *stanza = NULL;
while (true)
{
/* TODO: pthread cancelation points */
XMLElement *body = NULL;
XMLElement *data = NULL;
char *to, *room, *from, *from_matrix;
/* Decoding XML is blocking, which will cause memory issues when we
* want to murder this thread.
* We could ping the server, and die on a "response" stanza, however.
*/
stanza = XMLDecode(jabber->stream, false);
if (!stanza)
{
ASSend(
args->config, "!jdHnoKgHCm51ujbw:ari.lt", "_parsee_bridge",
"m.room.message", MatrixCreateNotice("Hi.")
);
continue;
}
if (StrEquals(stanza->name, "message"))
{
size_t i;
for (i = 0; i < ArraySize(stanza->children); i++)
{
XMLElement *child = ArrayGet(stanza->children, i);
if (StrEquals(child->name, "body"))
{
body = child;
break;
}
}
if (!body)
{
XMLFreeElement(stanza);
continue;
}
to = ParseeDecodeMXID(HashMapGet(stanza->attrs, "to"));
from = HashMapGet(stanza->attrs, "from");
from_matrix = ParseeEncodeJID(args->config, from);
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 */
ASSend(
args->config, room, from_matrix,
"m.room.message", MatrixCreateNotice(data->data)
);
Free(from_matrix);
Free(room);
Free(to);
}
XMLFreeElement(stanza);
}
return NULL;
}