[MOD] Die on loopback stanza

Dirty hack to a memory problem.
This commit is contained in:
LDA 2024-06-19 13:45:26 +02:00
commit 14ebef53ce
7 changed files with 77 additions and 16 deletions

View file

@ -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

View file

@ -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;
}

View file

@ -1,5 +1,7 @@
#include <Cytoplasm/HttpServer.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Str.h>
#include <pthread.h>
#include <string.h>
@ -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);

View file

@ -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;
}

View file

@ -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,
"<presence from='%s@%s' to='%s'>"
"<x xmlns='http://jabber.org/protocol/muc'/>"
"</presence>",
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);
}

View file

@ -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);

View file

@ -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