mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 21:35:10 +00:00
[MOD] Die on loopback stanza
Dirty hack to a memory problem.
This commit is contained in:
parent
b408cbf224
commit
14ebef53ce
7 changed files with 77 additions and 16 deletions
|
|
@ -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
|
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.
|
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.~~
|
~~Also dependency bad.~~
|
||||||
|
|
||||||
## BUILDING
|
## BUILDING
|
||||||
|
|
|
||||||
16
src/Events.c
16
src/Events.c
|
|
@ -15,3 +15,19 @@ MatrixCreateNotice(char *body)
|
||||||
|
|
||||||
return map;
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
34
src/Main.c
34
src/Main.c
|
|
@ -1,5 +1,7 @@
|
||||||
#include <Cytoplasm/HttpServer.h>
|
#include <Cytoplasm/HttpServer.h>
|
||||||
|
#include <Cytoplasm/Memory.h>
|
||||||
#include <Cytoplasm/Log.h>
|
#include <Cytoplasm/Log.h>
|
||||||
|
#include <Cytoplasm/Str.h>
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
@ -12,10 +14,14 @@
|
||||||
|
|
||||||
static HttpServer *server = NULL;
|
static HttpServer *server = NULL;
|
||||||
static pthread_t xmpp_thr;
|
static pthread_t xmpp_thr;
|
||||||
|
static XMPPComponent *jabber = NULL;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
SignalHandler(int signal)
|
SignalHandler(int signal)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
XMLElement *message, *body, *data;
|
||||||
|
char *from;
|
||||||
|
|
||||||
switch (signal)
|
switch (signal)
|
||||||
{
|
{
|
||||||
|
|
@ -27,8 +33,22 @@ SignalHandler(int signal)
|
||||||
{
|
{
|
||||||
return;
|
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);
|
HttpServerStop(server);
|
||||||
pthread_cancel(xmpp_thr);
|
//pthread_cancel(xmpp_thr);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -37,10 +57,9 @@ int
|
||||||
Main(void)
|
Main(void)
|
||||||
{
|
{
|
||||||
HttpServerConfig conf;
|
HttpServerConfig conf;
|
||||||
ParseeData *data;
|
ParseeData *data = NULL;
|
||||||
const ParseeConfig *parsee_conf;
|
const ParseeConfig *parsee_conf;
|
||||||
Stream *yaml;
|
Stream *yaml;
|
||||||
XMPPComponent *jabber;
|
|
||||||
struct sigaction sigAction;
|
struct sigaction sigAction;
|
||||||
|
|
||||||
Log(LOG_INFO, "%s - v%s", NAME, VERSION);
|
Log(LOG_INFO, "%s - v%s", NAME, VERSION);
|
||||||
|
|
@ -59,10 +78,14 @@ Main(void)
|
||||||
parsee_conf->component_host,
|
parsee_conf->component_host,
|
||||||
parsee_conf->component_port
|
parsee_conf->component_port
|
||||||
);
|
);
|
||||||
XMPPAuthenticateCompStream(
|
if (!XMPPAuthenticateCompStream(
|
||||||
jabber,
|
jabber,
|
||||||
parsee_conf->shared_comp_secret
|
parsee_conf->shared_comp_secret
|
||||||
);
|
))
|
||||||
|
{
|
||||||
|
Log(LOG_ERR, "Could not connect to XMPP...");
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Log(LOG_NOTICE, "Setting up local Matrix user...");
|
Log(LOG_NOTICE, "Setting up local Matrix user...");
|
||||||
|
|
@ -107,6 +130,7 @@ Main(void)
|
||||||
HttpServerStart(server);
|
HttpServerStart(server);
|
||||||
HttpServerJoin(server);
|
HttpServerJoin(server);
|
||||||
|
|
||||||
|
end:
|
||||||
HttpServerFree(server);
|
HttpServerFree(server);
|
||||||
ParseeConfigFree();
|
ParseeConfigFree();
|
||||||
ParseeFreeData(conf.handlerArgs);
|
ParseeFreeData(conf.handlerArgs);
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ XMPPAuthenticateCompStream(XMPPComponent *comp, char *shared)
|
||||||
if (ev->type != XML_LEXER_STARTELEM ||
|
if (ev->type != XML_LEXER_STARTELEM ||
|
||||||
!StrEquals(ev->element, "stream:stream"))
|
!StrEquals(ev->element, "stream:stream"))
|
||||||
{
|
{
|
||||||
Log(LOG_ERR, "Excepted strea:stream element.");
|
Log(LOG_ERR, "Excepted stream:stream element.");
|
||||||
XMLFreeEvent(ev);
|
XMLFreeEvent(ev);
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,16 +34,23 @@ XMPPSendPlain(XMPPComponent *comp, char *fr, char *to, char *msg, char *type)
|
||||||
void
|
void
|
||||||
XMPPSendPresence(XMPPComponent *comp, char *fr, char *to)
|
XMPPSendPresence(XMPPComponent *comp, char *fr, char *to)
|
||||||
{
|
{
|
||||||
|
XMLElement *presence, *x;
|
||||||
|
char *from;
|
||||||
if (!comp || !fr || !to)
|
if (!comp || !fr || !to)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
StreamPrintf(comp->stream,
|
|
||||||
"<presence from='%s@%s' to='%s'>"
|
presence = XMLCreateTag("presence");
|
||||||
"<x xmlns='http://jabber.org/protocol/muc'/>"
|
x = XMLCreateTag("x");
|
||||||
"</presence>",
|
XMLAddAttr(presence, "from", (from = StrConcat(3, fr, "@", comp->host)));
|
||||||
fr, comp->host,
|
XMLAddAttr(presence, "to", to);
|
||||||
to
|
XMLAddAttr(x, "xmlns", "http://jabber.org/protocol/muc");
|
||||||
);
|
|
||||||
|
XMLAddChild(presence, x);
|
||||||
|
|
||||||
|
XMLEncode(comp->stream, presence);
|
||||||
StreamFlush(comp->stream);
|
StreamFlush(comp->stream);
|
||||||
|
XMLFreeElement(presence);
|
||||||
|
Free(from);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,14 @@ ParseeXMPPThread(void *argp)
|
||||||
if (StrEquals(stanza->name, "message"))
|
if (StrEquals(stanza->name, "message"))
|
||||||
{
|
{
|
||||||
size_t i;
|
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++)
|
for (i = 0; i < ArraySize(stanza->children); i++)
|
||||||
{
|
{
|
||||||
XMLElement *child = ArrayGet(stanza->children, i);
|
XMLElement *child = ArrayGet(stanza->children, i);
|
||||||
|
|
@ -55,11 +63,10 @@ ParseeXMPPThread(void *argp)
|
||||||
room = ParseeFindDMRoom(args, to, from);
|
room = ParseeFindDMRoom(args, to, from);
|
||||||
data = ArrayGet(body->children, 0);
|
data = ArrayGet(body->children, 0);
|
||||||
|
|
||||||
/* TODO: Manage that out. $[to] corresponds to a Matrix
|
/* TODO: Consider having rich messages */
|
||||||
* user, which we need to find the */
|
|
||||||
ASSend(
|
ASSend(
|
||||||
args->config, room, from_matrix,
|
args->config, room, from_matrix,
|
||||||
"m.room.message", MatrixCreateNotice(data->data)
|
"m.room.message", MatrixCreateMessage(data->data)
|
||||||
);
|
);
|
||||||
Free(from_matrix);
|
Free(from_matrix);
|
||||||
Free(room);
|
Free(room);
|
||||||
|
|
|
||||||
|
|
@ -8,4 +8,7 @@ extern HashMap * MatrixCreateError(char *err, char *msg);
|
||||||
|
|
||||||
/* Creates the content for a notice message. */
|
/* Creates the content for a notice message. */
|
||||||
extern HashMap * MatrixCreateNotice(char *body);
|
extern HashMap * MatrixCreateNotice(char *body);
|
||||||
|
|
||||||
|
/* Creates the content for a normal message. */
|
||||||
|
extern HashMap * MatrixCreateMessage(char *body);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue