[ADD/WIP] Parsee IDs, -v flag

This commit is contained in:
LDA 2024-08-16 16:38:21 +02:00
commit aa9b68e02d
7 changed files with 56 additions and 13 deletions

View file

@ -29,14 +29,14 @@ ParseeRequest(HttpServerContext *ctx, void *argp)
arg.stream = stream; arg.stream = stream;
Log(LOG_NOTICE, "%s %s", Log(LOG_DEBUG, "%s %s",
HttpRequestMethodToString(HttpRequestMethodGet(ctx)), HttpRequestMethodToString(HttpRequestMethodGet(ctx)),
path path
); );
if (!HttpRouterRoute(data->router, path, &arg, (void **) &response)) if (!HttpRouterRoute(data->router, path, &arg, (void **) &response))
{ {
Log(LOG_NOTICE, "Couldn't route %s", path); Log(LOG_DEBUG, "Couldn't route %s", path);
HttpResponseStatus(ctx, HTTP_NOT_FOUND); HttpResponseStatus(ctx, HTTP_NOT_FOUND);
JsonFree(response); JsonFree(response);
@ -56,8 +56,11 @@ ParseeRequest(HttpServerContext *ctx, void *argp)
HttpSendHeaders(ctx); HttpSendHeaders(ctx);
JsonEncode(response, stream, JSON_DEFAULT); JsonEncode(response, stream, JSON_DEFAULT);
JsonFree(response); JsonFree(response);
return;
} }
Log(LOG_DEBUG, "%s %s (%d)",
HttpRequestMethodToString(HttpRequestMethodGet(ctx)),
path, HttpResponseStatusGet(ctx)
);
} }
HttpClientContext * HttpClientContext *

View file

@ -56,7 +56,7 @@ Main(Array *args, HashMap *env)
int http = 8; int http = 8;
ArgParseStateInit(&state); ArgParseStateInit(&state);
while ((flag = ArgParse(&state, args, "gH:J:")) != -1) while ((flag = ArgParse(&state, args, "vgH:J:")) != -1)
{ {
switch (flag) switch (flag)
{ {
@ -73,6 +73,9 @@ Main(Array *args, HashMap *env)
ParseeExportConfigYAML(yaml); ParseeExportConfigYAML(yaml);
StreamClose(yaml); StreamClose(yaml);
goto end; goto end;
case 'v':
LogConfigLevelSet(LogConfigGlobal(), LOG_DEBUG);
break;
} }
} }
ParseeSetThreads(xmpp, http); ParseeSetThreads(xmpp, http);

View file

@ -247,6 +247,7 @@ ParseeMessageHandler(ParseeData *data, HashMap *event)
char *sender = NULL; char *sender = NULL;
char *unedited_id = MatrixGetEdit(event); char *unedited_id = MatrixGetEdit(event);
char *url = GrabString(event, 2, "content", "url"); char *url = GrabString(event, 2, "content", "url");
char *encoded_from = NULL;
bool direct = false; bool direct = false;
@ -284,9 +285,13 @@ ParseeMessageHandler(ParseeData *data, HashMap *event)
type = direct ? "chat" : "groupchat"; type = direct ? "chat" : "groupchat";
user = GrabString(json, 1, "xmpp_user"); user = GrabString(json, 1, "xmpp_user");
unauth = ParseeToUnauth(data, url); unauth = ParseeToUnauth(data, url);
encoded_from = ParseeEncodeMXID(m_sender);
xmppified_user = StrConcat(3,
encoded_from, "@", jabber->host
);
if (direct) if (direct)
{ {
xmppified_user = ParseeEncodeMXID(m_sender);
to = StrDuplicate(user); to = StrDuplicate(user);
Free(chat_id); Free(chat_id);
@ -300,7 +305,6 @@ ParseeMessageHandler(ParseeData *data, HashMap *event)
{ {
goto end; goto end;
} }
xmppified_user = ParseeEncodeMXID(m_sender);
/* TODO: Check the name's validity. /* TODO: Check the name's validity.
* Is there a good way to check for that that isn't * Is there a good way to check for that that isn't
@ -308,7 +312,7 @@ ParseeMessageHandler(ParseeData *data, HashMap *event)
name = ASGetName(data->config, id, m_sender); name = ASGetName(data->config, id, m_sender);
rev = StrConcat(4, muc_id, "/", name, "[p]"); rev = StrConcat(4, muc_id, "/", name, "[p]");
XMPPJoinMUC(jabber, xmppified_user, rev); XMPPJoinMUC(jabber, encoded_from, rev);
to = muc_id; to = muc_id;
@ -342,7 +346,7 @@ ParseeMessageHandler(ParseeData *data, HashMap *event)
char *xmpp_ident = StrRandom(32); char *xmpp_ident = StrRandom(32);
builder = CreateStanzaBuilder(xmppified_user, to, xmpp_ident); builder = CreateStanzaBuilder(xmppified_user, to, xmpp_ident);
SetStanzaType(builder, type); SetStanzaType(builder, type);
SetStanzaBody(builder, xepd ? xepd : body); SetStanzaBody(builder, unauth ? unauth : (xepd ? xepd : body));
SetStanzaReply(builder, stanza, sender); SetStanzaReply(builder, stanza, sender);
SetStanzaLink(builder, unauth); SetStanzaLink(builder, unauth);
SetStanzaEdit(builder, origin_id); SetStanzaEdit(builder, origin_id);
@ -372,6 +376,7 @@ end:
Free(sender); Free(sender);
Free(unauth); Free(unauth);
Free(unedited_id); Free(unedited_id);
Free(encoded_from);
DbUnlock(data->db, ref); DbUnlock(data->db, ref);
ref = NULL; ref = NULL;

View file

@ -17,6 +17,7 @@ ParseeData *
ParseeInitData(XMPPComponent *comp) ParseeInitData(XMPPComponent *comp)
{ {
ParseeData *data; ParseeData *data;
DbRef *ref;
if (!ParseeConfigGet()) if (!ParseeConfigGet())
{ {
return NULL; return NULL;
@ -39,6 +40,18 @@ ParseeInitData(XMPPComponent *comp)
data->db = DbOpen(data->config->db_path, 0); data->db = DbOpen(data->config->db_path, 0);
} }
if (!(ref = DbLock(data->db, 1, "info")))
{
char *id = StrRandom(8);
ref = DbCreate(data->db, 1, "info");
HashMapSet(DbJson(ref), "identifier", JsonValueString(id));
Free(id);
}
data->id = StrDuplicate(GrabString(DbJson(ref), 1, "identifier"));
Log(LOG_DEBUG, "- Parsee ID: %s", data->id);
DbUnlock(data->db, ref);
#define X_ROUTE(path, func) do {\ #define X_ROUTE(path, func) do {\
if (!HttpRouterAdd(data->router, path, func))\ if (!HttpRouterAdd(data->router, path, func))\
{\ {\
@ -61,6 +74,7 @@ ParseeFreeData(ParseeData *data)
return; return;
} }
Free(data->id);
XMPPEndCompStream(data->jabber); XMPPEndCompStream(data->jabber);
DbClose(data->db); DbClose(data->db);
HttpRouterFree(data->router); HttpRouterFree(data->router);
@ -76,8 +90,9 @@ ParseeCleanup(void *datp)
char *chat; char *chat;
size_t i; size_t i;
uint64_t ts = UtilTsMillis(); uint64_t ts = UtilTsMillis();
size_t entries = 0;
Log(LOG_NOTICE, "Cleaning up..."); Log(LOG_DEBUG, "Cleaning up...");
chats = DbList(data->db, 1, "chats"); chats = DbList(data->db, 1, "chats");
@ -121,6 +136,7 @@ ParseeCleanup(void *datp)
if (cleaned > threshold) \ if (cleaned > threshold) \
{ \ { \
DbDelete(data->db, 4, "chats", chat, #field"s", field); \ DbDelete(data->db, 4, "chats", chat, #field"s", field); \
entries++; \
} \ } \
Free(field); \ Free(field); \
} \ } \
@ -174,6 +190,7 @@ ParseeCleanup(void *datp)
if (cleaned > threshold) \ if (cleaned > threshold) \
{ \ { \
JsonValueFree(HashMapDelete(field##s, field)); \ JsonValueFree(HashMapDelete(field##s, field)); \
entries++; \
} \ } \
Free(field); \ Free(field); \
} \ } \
@ -188,6 +205,7 @@ ParseeCleanup(void *datp)
DbUnlock(data->db, ref); DbUnlock(data->db, ref);
} }
DbListFree(chats); DbListFree(chats);
Log(LOG_DEBUG, "Cleant up %d entries...", entries);
} }
int int
ParseeFindDatastart(char *data) ParseeFindDatastart(char *data)

View file

@ -140,7 +140,7 @@ XMPPAuthenticateCompStream(XMPPComponent *comp, char *shared)
stream_id = StrDuplicate(HashMapGet(ev->attrs, "id")); stream_id = StrDuplicate(HashMapGet(ev->attrs, "id"));
handshake = ComputeHandshake(shared, stream_id); handshake = ComputeHandshake(shared, stream_id);
Log(LOG_NOTICE, "- sID='%s'", stream_id); Log(LOG_DEBUG, "- sID='%s'", stream_id);
StreamPrintf(stream, "<handshake>%s</handshake>", handshake); StreamPrintf(stream, "<handshake>%s</handshake>", handshake);
StreamFlush(stream); StreamFlush(stream);
XMLFreeEvent(ev); XMLFreeEvent(ev);

View file

@ -170,7 +170,10 @@ ParseeXMPPThread(void *argp)
pthread_t *thr = &info.dispatchers[i].thr; pthread_t *thr = &info.dispatchers[i].thr;
info.dispatchers[i].info = &info; info.dispatchers[i].info = &info;
pthread_create(thr, NULL, XMPPDispatcher, &info.dispatchers[i]); if (pthread_create(thr, NULL, XMPPDispatcher, &info.dispatchers[i]))
{
Achievement("COULDNT INITIALISE XMPP DISPATCHERS", true);
}
} }
while (true) while (true)
@ -213,15 +216,25 @@ ParseeXMPPThread(void *argp)
} }
info.running = false; info.running = false;
Log(LOG_INFO, "Joining subthreads...");
for (i = 0; i < info.available_dispatchers; i++) for (i = 0; i < info.available_dispatchers; i++)
{ {
pthread_t thr = info.dispatchers[i].thr; pthread_t thr = info.dispatchers[i].thr;
pthread_join(thr, NULL); if (pthread_join(thr, NULL))
{
Achievement("COULDNT JOIN XMPP DISPATCHER", true);
}
Log(LOG_DEBUG, "- joined %d/%d...",
i + 1, info.available_dispatchers
);
} }
Log(LOG_INFO, "Joined subthreads..."); Log(LOG_INFO, "Joined subthreads...");
Free(info.dispatchers); Free(info.dispatchers);
if (ArraySize(info.stanzas))
{
Log(LOG_DEBUG, "(%d unprocessed stanzas)", ArraySize(info.stanzas));
}
for (i = 0; i < ArraySize(info.stanzas); i++) for (i = 0; i < ArraySize(info.stanzas); i++)
{ {
XMLFreeElement(ArrayGet(info.stanzas, i)); XMLFreeElement(ArrayGet(info.stanzas, i));

View file

@ -55,6 +55,7 @@ typedef struct ParseeData {
XMPPComponent *jabber; XMPPComponent *jabber;
Db *db; Db *db;
char *id;
} ParseeData; } ParseeData;
#define GrabString(obj, ...) JsonValueAsString(JsonGet(obj, __VA_ARGS__)) #define GrabString(obj, ...) JsonValueAsString(JsonGet(obj, __VA_ARGS__))