From c2ea3807ec50b8392e293e1236f452364f93a9dd Mon Sep 17 00:00:00 2001 From: LDA Date: Sat, 21 Sep 2024 18:26:35 +0200 Subject: [PATCH] [MOD/FIX] Licensewerk, start counting by codepoint XEP-0426 came out of the blue, and it *hit* me! --- LICENSE | 2 +- src/Parsee/Utils/String.c | 18 +++++++++++++ src/StanzaBuilder.c | 2 +- src/Unistr.c | 45 ++++++++++++++++++++++++++++++++ src/XMPPThread/Stanzas/Message.c | 25 +++++++++++++----- src/include/Parsee.h | 4 +++ src/include/Unistring.h | 12 +++++++++ 7 files changed, 100 insertions(+), 8 deletions(-) diff --git a/LICENSE b/LICENSE index 1e1282b..ce865e6 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -For the files src/Parsee/HMAC.c, src/XML/*, tools/*, src/include/XML.h, etc/*, and Makefile, +For the files src/include/Unistring.h, src/Unistr.h rc/Parsee/HMAC.c, src/XML/*, tools/*, src/include/XML.h, etc/*, and Makefile, see COPYING.CC0. For any other file in src/, see COPYING.AGPL as the primary license. diff --git a/src/Parsee/Utils/String.c b/src/Parsee/Utils/String.c index 89d6f4a..c054d13 100644 --- a/src/Parsee/Utils/String.c +++ b/src/Parsee/Utils/String.c @@ -3,6 +3,8 @@ #include #include +#include + #include #include @@ -37,6 +39,22 @@ ParseeFindDatastart(char *data) return (int) (startline - data); } +int +ParseeFindDatastartU(char *data) +{ + Unistr *str; + size_t ret; + if (!data) + { + return 0; + } + + str = UnistrCreate(data); + ret = UnistrGetOffset(str, (uint32_t) '>'); + UnistrFree(str); + + return (int) ret; +} char * ParseeStringifyDate(uint64_t millis) diff --git a/src/StanzaBuilder.c b/src/StanzaBuilder.c index 0a97102..811c6d3 100644 --- a/src/StanzaBuilder.c +++ b/src/StanzaBuilder.c @@ -185,7 +185,7 @@ ExportStanza(StanzaBuilder *builder) builder->replying_to_sender && builder->body) { - int off = ParseeFindDatastart(builder->body); + int off = ParseeFindDatastartU(builder->body); char *ostr = StrInt(off); XMLElement *reply = XMLCreateTag("reply"); XMLElement *fallback = XMLCreateTag("fallback"); diff --git a/src/Unistr.c b/src/Unistr.c index 3db7441..5eaae43 100644 --- a/src/Unistr.c +++ b/src/Unistr.c @@ -3,8 +3,10 @@ #include #include #include +#include #include +#include struct Unistr { size_t length; @@ -222,3 +224,46 @@ UnistrFilter(Unistr *str, UnistrFilterFunc filter) return unistr; } + +Unistr * +UnistrConcat(size_t n, ...) +{ + va_list list; + size_t i; + Unistr *ret = UnistrCreate(""); + + va_start(list, n); + for (i = 0; i < n; i++) + { + Unistr *to_concat = va_arg(list, Unistr *); + size_t j; + for (j = 0; j < UnistrSize(to_concat); j++) + { + UnistrAddch(ret, UnistrGetch(to_concat, j)); + } + } + + va_end(list); + return ret; +} +size_t +UnistrGetOffset(Unistr *str, uint32_t sep) +{ + size_t i; + uint32_t prev = 0x0A; + if (!str || !sep) + { + return 0; + } + + for (i = 0; i < str->length; i++) + { + uint32_t curr = str->codepoints[i]; + if (prev == 0x0A && curr != sep) + { + return i; + } + prev = curr; + } + return 0; +} diff --git a/src/XMPPThread/Stanzas/Message.c b/src/XMPPThread/Stanzas/Message.c index 9237aec..1b79b77 100644 --- a/src/XMPPThread/Stanzas/Message.c +++ b/src/XMPPThread/Stanzas/Message.c @@ -9,6 +9,20 @@ #include +static void +LazyRegister(ParseeData *data, char *mxid, char *name) +{ + if (!DbExists(data->db, 2, "users", mxid)) + { + ASRegisterUser(data->config, mxid); + DbUnlock(data->db, DbCreate(data->db, 2, "users", mxid)); + } + if (name) + { + ASSetName(data->config, mxid, name); + } +} + static void ProcessChatstates(ParseeData *args, XMLElement *stanza) { @@ -211,7 +225,7 @@ end_error: HashMap *room_json; char *from = HashMapGet(stanza->attrs, "from"); - ASRegisterUser(args->config, from_matrix); + LazyRegister(args, from_matrix, NULL); room = ASCreateDM(args->config, from_matrix, to); mroom_id = StrDuplicate(room); Log(LOG_INFO, "Creating a DM to '%s'(%s)...", to, mroom_id); @@ -273,11 +287,10 @@ end_error: XMLElement *oob, *oob_data; pthread_mutex_unlock(&thr->info->chk_lock); - ASRegisterUser(args->config, encoded); - if (!chat) - { - ASSetName(args->config, encoded, res); - } + + LazyRegister(args, encoded, !chat ? res : NULL); + /* TODO: I don't think we can quite remove that. Maybe + * the user was kicked and the bridge is unaware? */ ASInvite(args->config, mroom_id, encoded); Free(ASJoin(args->config, mroom_id, encoded)); diff --git a/src/include/Parsee.h b/src/include/Parsee.h index 34077cd..efa59eb 100644 --- a/src/include/Parsee.h +++ b/src/include/Parsee.h @@ -286,6 +286,10 @@ extern void ParseeCleanup(void *data); /* Finds the offset of the first line without a '>' at its start. */ extern int ParseeFindDatastart(char *data); +/* Finds the offset of the first line without a '>' at its start(as + * Unicode codepoints). */ +extern int ParseeFindDatastartU(char *data); + /* XMPP-ifies a message event to XEP-0393 if possible. */ extern char * ParseeXMPPify(HashMap *event); diff --git a/src/include/Unistring.h b/src/include/Unistring.h index f1c9aee..6fbc296 100644 --- a/src/include/Unistring.h +++ b/src/include/Unistring.h @@ -39,6 +39,13 @@ extern uint32_t UnistrGetch(Unistr *unistr, size_t i); * Modifies: unistr */ extern void UnistrAddch(Unistr *unistr, uint32_t u); +/** Concats N unistrings into a new, separate unistring. + * --------------------- + * Returns: a new unistring[HEAP] + * Modifies: NOTHING + * Thrasher: UnistrFree */ +extern Unistr * UnistrConcat(size_t n, ...); + /** Encodes a unistring into a C UTF-8 string * -------------- * Returns: a valid NULL-terminated string[HEAP] | NULL @@ -64,4 +71,9 @@ typedef bool (*UnistrFilterFunc)(uint32_t u); * Returns: a new unistring with filtered characters removed */ extern Unistr * UnistrFilter(Unistr *str, UnistrFilterFunc filter); +/** Finds the offset of the first line not starting with a specific + * characters in terms of Unicode codepoints. + * -------- + * Returns: an offset of the first line to not start by {c} | 0 */ +extern size_t UnistrGetOffset(Unistr *str, uint32_t sep); #endif