From b820a441edd29817555516ba8190b67dd8f22277 Mon Sep 17 00:00:00 2001 From: LDA Date: Sat, 22 Jun 2024 22:43:58 +0200 Subject: [PATCH] [MOD] Add x-parsee tag with metadata, global nicks Still need to grab it on the room-level from the room state if possible, otherwise just rollback to the MXID. I also need to consider profile pictures and bridging media, and check if the nick was present XMPP-side first, as that can and will cause issues, especially around semi-anonymous MUCs. --- Makefile | 3 ++- src/AS.c | 41 ++++++++++++++++++++++++++++++++++++++++ src/Main.c | 5 ++++- src/MatrixEventHandler.c | 5 ++++- src/XML/SAX.c | 6 ++++++ src/XMPP/Stanza.c | 37 +++++++++++++++++++++++++++++++++++- src/XMPPThread.c | 4 ++-- src/include/AS.h | 4 ++++ src/include/XMPP.h | 3 +++ 9 files changed, 102 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 0a324b2..b70d8cc 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ # =========================== Parsee Flags ============================= NAME=Parsee VERSION=0.0.0 +REPOSITORY=$(shell git remote get-url origin) # =========================== Compilation Flags ============================ CYTO_INC=/usr/local/include/ # Where Cytoplasm's include path is @@ -18,7 +19,7 @@ SOURCE=src OBJECT=build INCLUDES=src/include CC=cc -CFLAGS=-I$(INCLUDES) -I$(CYTO_INC) -DNAME="\"$(NAME)\"" -DVERSION="\"$(VERSION)\"" -g -ggdb +CFLAGS=-I$(INCLUDES) -I$(CYTO_INC) -DNAME="\"$(NAME)\"" -DVERSION="\"$(VERSION)\"" -DREPOSITORY=\"$(REPOSITORY)\" -g -ggdb LDFLAGS=-L $(CYTO_LIB) -lCytoplasm -Wl,--export-dynamic BINARY=parsee # ============================ Compilation ================================= diff --git a/src/AS.c b/src/AS.c index 1e05dad..f2b1302 100644 --- a/src/AS.c +++ b/src/AS.c @@ -280,3 +280,44 @@ ASSetName(const ParseeConfig *conf, char *user, char *name) JsonFree(json); Free(user); } +char * +ASGetName(const ParseeConfig *c, char *room, char *user) +{ + HttpClientContext *ctx; + HashMap *reply; + char *path, *ret; + if (!c || !user) + { + return NULL; + } + + if (!room) + { + user = HttpUrlEncode(user); + path = StrConcat(3, + "/_matrix/client/v3/profile/", user, "/displayname" + ); + ctx = ParseeCreateRequest(c, HTTP_GET, path); + Free(user); + ASAuthenticateRequest(c, ctx); + HttpRequestSendHeaders(ctx); + HttpRequestSend(ctx); + + reply = JsonDecode(HttpClientStream(ctx)); + + JsonFree(reply); + ret = StrDuplicate( + JsonValueAsString(HashMapGet(reply, "displayname")) + ); + HttpClientContextFree(ctx); + Free(path); + + if (!ret) + { + ret = StrDuplicate(user); + } + return ret; + } + + return NULL; +} diff --git a/src/Main.c b/src/Main.c index 5bb50b4..b967560 100644 --- a/src/Main.c +++ b/src/Main.c @@ -29,6 +29,9 @@ SignalHandler(int signal) { case SIGPIPE: return; + case SIGUSR1: + /* TODO: Soft-restart everything */ + return; case SIGTERM: case SIGINT: if (!server) @@ -127,7 +130,7 @@ Main(void) SIGACTION(SIGINT, &sigAction, NULL); SIGACTION(SIGTERM, &sigAction, NULL); SIGACTION(SIGPIPE, &sigAction, NULL); - SIGACTION(SIGUSR1, &sigAction, NULL); + SIGACTION(SIGUSR1, &sigAction, NULL); /* Make USR1 do a softrestart */ #undef SIGACTION server = HttpServerCreate(&conf); diff --git a/src/MatrixEventHandler.c b/src/MatrixEventHandler.c index bc634ed..9a0e17f 100644 --- a/src/MatrixEventHandler.c +++ b/src/MatrixEventHandler.c @@ -104,10 +104,13 @@ ParseeMessageHandler(ParseeData *data, HashMap *event) } jid = ParseeEncodeMXID(sender); { - char *rev = StrConcat(3, muc_id, "/", jid); + /* TODO: Check the name's validity */ + char *name = ASGetName(data->config, NULL, sender); + char *rev = StrConcat(3, muc_id, "/", name); XMPPJoinMUC(jabber, jid, rev); XMPPSendPlain(jabber, jid, muc_id, body, "groupchat"); Free(rev); + Free(name); } Free(chat_id); Free(muc_id); diff --git a/src/XML/SAX.c b/src/XML/SAX.c index 7fe73f6..6730e61 100644 --- a/src/XML/SAX.c +++ b/src/XML/SAX.c @@ -613,6 +613,11 @@ XMLDecodeString(char *s) cs[0] = '\''; s += 6; } + else if (!strncmp(s, """, 6)) + { + cs[0] = '"'; + s += 6; + } else if (!strncmp(s, "<", 4)) { cs[0] = '<'; @@ -632,6 +637,7 @@ XMLDecodeString(char *s) { s++; } + /* TODO: Support hexcodes */ tmp = ret; ret = StrConcat(2, ret, cs); diff --git a/src/XMPP/Stanza.c b/src/XMPP/Stanza.c index 80edee8..b0d6cb2 100644 --- a/src/XMPP/Stanza.c +++ b/src/XMPP/Stanza.c @@ -9,7 +9,7 @@ void XMPPSendPlain(XMPPComponent *comp, char *fr, char *to, char *msg, char *type) { - XMLElement *message, *body, *data; + XMLElement *message, *body, *data, *parsee; char *from; if (!comp || !fr || !to || !msg) { @@ -24,7 +24,32 @@ XMPPSendPlain(XMPPComponent *comp, char *fr, char *to, char *msg, char *type) body = XMLCreateTag("body"); data = XMLCreateText(msg); + /* TODO: Add Parsee specific fields here */ + parsee = XMLCreateTag("x-parsee"); + { + XMLElement *parsee_version, *ver_elem; + XMLElement *parsee_link, *link_elem; + XMLElement *parsee_text, *text_elem; + + parsee_version = XMLCreateTag("version"); + ver_elem = XMLCreateText(VERSION); + XMLAddChild(parsee_version, ver_elem); + XMLAddChild(parsee, parsee_version); + + parsee_link = XMLCreateTag("repository"); + link_elem = XMLCreateText(REPOSITORY); + XMLAddChild(parsee_link, link_elem); + XMLAddChild(parsee, parsee_link); + + parsee_text = XMLCreateTag("zayds-note"); + text_elem = XMLCreateText("\"LDA HANG YOURSELF\" - Zayd"); + XMLAddChild(parsee_text, text_elem); + XMLAddChild(parsee, parsee_text); + /* TODO: Add custom fields depending on the caller's wishes */ + } + XMLAddChild(message, body); + XMLAddChild(message, parsee); XMLAddChild(body, data); XMLEncode(comp->stream, message); @@ -122,3 +147,13 @@ XMPPIsKiller(XMLElement *stanza) return StrEquals( HashMapGet(stanza->attrs, "from"), HashMapGet(stanza->attrs, "to")); } +bool +XMPPIsParseeStanza(XMLElement *stanza) +{ + if (!stanza) + { + return false; + } + + return !!XMLookForUnique(stanza, "x-parsee"); +} diff --git a/src/XMPPThread.c b/src/XMPPThread.c index 7432f7a..146262a 100644 --- a/src/XMPPThread.c +++ b/src/XMPPThread.c @@ -102,7 +102,7 @@ ParseeXMPPThread(void *argp) { ParseeDMHandler(room, from_matrix, data, args->config); } - if (mroom_id && !ParseeIsJabberPuppet(args->config, from)) + if (mroom_id && !XMPPIsParseeStanza(stanza)) { char *res = ParseeGetResource(from); char *encoded = ParseeEncodeJID(args->config, from, false); @@ -113,8 +113,8 @@ ParseeXMPPThread(void *argp) if (ParseeVerifyStanza(args, chat_id, s_id_str)) { ASRegisterUser(args->config, encoded); - ASJoin(args->config, mroom_id, encoded); ASSetName(args->config, encoded, res); + ASJoin(args->config, mroom_id, encoded); ASSend( args->config, mroom_id, encoded, "m.room.message", MatrixCreateMessage(data->data) diff --git a/src/include/AS.h b/src/include/AS.h index a9aef2f..f5b083b 100644 --- a/src/include/AS.h +++ b/src/include/AS.h @@ -39,4 +39,8 @@ extern char * ASCreateRoom(const ParseeConfig *c, char *by, char *alias); /* Sets a user's displayname */ extern void ASSetName(const ParseeConfig *c, char *user, char *name); + +/* Returns the user's name in a room, or a copy of the MXID itself, to be + * Free'd. */ +extern char * ASGetName(const ParseeConfig *c, char *room, char *user); #endif diff --git a/src/include/XMPP.h b/src/include/XMPP.h index f55223f..8ef17f3 100644 --- a/src/include/XMPP.h +++ b/src/include/XMPP.h @@ -52,4 +52,7 @@ extern bool XMPPQueryMUC(XMPPComponent *jabber, char *muc, MUCInfo *out); extern char * XMPPGetMUCName(MUCInfo info); extern void XMPPFreeMUCInfo(MUCInfo info); + +/* Checks if a stanza has an x-parsee element */ +extern bool XMPPIsParseeStanza(XMLElement *); #endif