From 9775bd8d4e1188cd9887f82fa7de34b3369659b2 Mon Sep 17 00:00:00 2001 From: LDA Date: Sun, 8 Sep 2024 16:12:27 +0200 Subject: [PATCH] [MOD] Read the config file for tool DB access --- etc/man/man1/parsee-adminify.1 | 7 +- src/XMPPThread/Stanzas/Message.c | 55 +++++++------- tools/adminify.c | 11 +-- tools/common.h | 118 +++++++++++++++++++++++++++++++ tools/config.c | 68 +----------------- tools/noavatars.c | 11 +-- 6 files changed, 156 insertions(+), 114 deletions(-) create mode 100644 tools/common.h diff --git a/etc/man/man1/parsee-adminify.1 b/etc/man/man1/parsee-adminify.1 index c3dde09..fd4a582 100644 --- a/etc/man/man1/parsee-adminify.1 +++ b/etc/man/man1/parsee-adminify.1 @@ -17,8 +17,8 @@ allows you to see/add admins to it, using simplified globrules. .SH OPTIONS .TP -.B [DB path] -The DB directory this tool will use to manage administrators. +.B [config] +The configuration file used by the Parsee daemon. .TP .B [user] If set, adds the glob @@ -28,7 +28,8 @@ the Parsee instance. .SH BUGS .PP -Currently not available with flat-based backends. +None as I know of. If anyone notices any issues with this, please +let me know. .SH LICENSE Unlike Parsee, diff --git a/src/XMPPThread/Stanzas/Message.c b/src/XMPPThread/Stanzas/Message.c index 187382a..75722e5 100644 --- a/src/XMPPThread/Stanzas/Message.c +++ b/src/XMPPThread/Stanzas/Message.c @@ -230,34 +230,31 @@ end_error: char *res = ParseeGetResource(from); char *encoded = ParseeGetBridgedUser(args, stanza); char *event_id = NULL; + char *parsee = ParseeJID(args); + char *trim = ParseeTrimJID(from); + XMLElement *ps; - { - char *parsee = ParseeJID(args); - char *trim = ParseeTrimJID(from); - XMLElement *ps; + /* Subscribe to the sender's metadata node. */ + ps = CreatePubsubRequest( + parsee, decode_from, "urn:xmpp:avatar:metadata" + ); + pthread_mutex_lock(&jabber->write_lock); + XMLEncode(jabber->stream, ps); + StreamFlush(jabber->stream); + pthread_mutex_unlock(&jabber->write_lock); + XMLFreeElement(ps); - /* Subscribe to the sender's metadata node. */ - ps = CreatePubsubRequest( - parsee, decode_from, "urn:xmpp:avatar:metadata" - ); - pthread_mutex_lock(&jabber->write_lock); - XMLEncode(jabber->stream, ps); - StreamFlush(jabber->stream); - pthread_mutex_unlock(&jabber->write_lock); - XMLFreeElement(ps); + ps = CreatePubsubRequest( + parsee, trim, "urn:xmpp:avatar:metadata" + ); + pthread_mutex_lock(&jabber->write_lock); + XMLEncode(jabber->stream, ps); + StreamFlush(jabber->stream); + pthread_mutex_unlock(&jabber->write_lock); + XMLFreeElement(ps); - ps = CreatePubsubRequest( - parsee, trim, "urn:xmpp:avatar:metadata" - ); - pthread_mutex_lock(&jabber->write_lock); - XMLEncode(jabber->stream, ps); - StreamFlush(jabber->stream); - pthread_mutex_unlock(&jabber->write_lock); - XMLFreeElement(ps); - - Free(parsee); - Free(trim); - } + Free(parsee); + Free(trim); pthread_mutex_lock(&thr->info->chk_lock); if (ParseeVerifyAllStanza(args, stanza) && !replaced) @@ -317,6 +314,7 @@ end_error: size_t reacts = ArraySize(react_child); event_id = ParseeGetReactedEvent(args, stanza); + /* Redact the last few reactions. */ to_redact = ASGetRelations( args->config, 30, mroom_id, event_id, "m.reaction" @@ -335,6 +333,7 @@ end_error: } ASFreeRelations(to_redact); + /* ... and push out the new ones. */ for (i = 0; i < reacts; i++) { XMLElement *reaction, *react_data; @@ -365,9 +364,9 @@ end_error: } else if (data) { - /* TODO: Use HTML-formatted bodies, and respect the fallback - * trims the stanza provides us if possible. Element does not - * like raw bodies on replies too. Go figure. */ + /* TODO: Respect the fallback trims the stanza provides us + * if possible. Element does not like raw bodies on replies + * too. Go figure. */ size_t off = reply_to ? ParseeFindDatastart(data->data) : 0; HashMap *ev = MatrixCreateMessage(data->data + off); diff --git a/tools/adminify.c b/tools/adminify.c index 1c04b74..26afb92 100644 --- a/tools/adminify.c +++ b/tools/adminify.c @@ -13,12 +13,7 @@ * Under CC0, as its a rather useful example of a Parsee tool. * See LICENSE for more information about Parsee's licensing. */ -#include -#include -#include -#include - -#include +#include "common.h" static void AddAdmin(Db *parsee, char *glob) @@ -86,14 +81,14 @@ Main(Array *args, HashMap *env) if (ArraySize(args) < 2) { - Log(LOG_ERR, "Usage: %s [DB path] [glob]", exec); + Log(LOG_ERR, "Usage: %s [config] ", exec); return EXIT_FAILURE; } db_path = ArrayGet(args, 1); glob = ArrayGet(args, 2); - parsee = DbOpenLMDB(db_path, 8 * 1024 * 1024); + parsee = GetDB(db_path); if (parsee) { diff --git a/tools/common.h b/tools/common.h new file mode 100644 index 0000000..eee4793 --- /dev/null +++ b/tools/common.h @@ -0,0 +1,118 @@ +/* Single-header tools for Parsee tools. */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define GrabString(obj, ...) JsonValueAsString(JsonGet(obj, __VA_ARGS__)) +#define GrabInteger(obj, ...) JsonValueAsInteger(JsonGet(obj, __VA_ARGS__)) +#define GrabBoolean(obj, ...) JsonValueAsBoolean(JsonGet(obj, __VA_ARGS__)) +#define GrabObject(obj, ...) JsonValueAsObject(JsonGet(obj, __VA_ARGS__)) +#define GrabArray(obj, ...) JsonValueAsArray(JsonGet(obj, __VA_ARGS__)) + +Uri * +DelegateServer(char *raw) +{ + HttpClientContext *ctx = NULL; + Stream *stream = NULL; + HashMap *reply = NULL; + Uri *ret = NULL; + + if (!raw) + { + return NULL; + } + + ctx = HttpRequest( + HTTP_GET, + HTTP_FLAG_TLS, 443, + raw, "/.well-known/matrix/client" + ); + HttpRequestSendHeaders(ctx); + + switch (HttpRequestSend(ctx)) + { + case HTTP_OK: + stream = HttpClientStream(ctx); + reply = JsonDecode(stream); + if (!reply) + { + goto end; + } + + ret = UriParse(JsonValueAsString(JsonGet( + reply, 2, + "m.homeserver", "base_url" + ))); + break; + default: + /* TODO */ + break; + } + + if (ret && !ret->port && StrEquals(ret->proto, "https")) + { + ret->port = 443; + } + if (ret && !ret->port) + { + ret->port = 80; + } + +end: + HttpClientContextFree(ctx); + JsonFree(reply); + + return ret; +} + +Db * +GetDB(char *config) +{ + size_t lmdb_size; + Db *ret = NULL; + char *db_path; + HashMap *json; + Stream *file; + if (!config) + { + return NULL; + } + + file = StreamOpen(config, "r"); + json = JsonDecode(file); + StreamClose(file); + if (!json) + { + return NULL; + } + + lmdb_size = GrabInteger(json, 1, "db_size"); + db_path = GrabString(json, 1, "db"); + + if (!db_path) + { + JsonFree(json); + return NULL; + } + + if (lmdb_size) + { + ret = DbOpenLMDB(db_path, lmdb_size); + } + if (!ret) + { + ret = DbOpen(db_path, 0); + } + + JsonFree(json); + return ret; +} diff --git a/tools/config.c b/tools/config.c index ace1a76..c6ab515 100644 --- a/tools/config.c +++ b/tools/config.c @@ -10,73 +10,7 @@ * Under CC0, as its a rather useful example of a Parsee tool. * See LICENSE for more information about Parsee's licensing. */ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -static Uri * -DelegateServer(char *raw) -{ - HttpClientContext *ctx = NULL; - Stream *stream = NULL; - HashMap *reply = NULL; - Uri *ret = NULL; - - if (!raw) - { - return NULL; - } - - ctx = HttpRequest( - HTTP_GET, - HTTP_FLAG_TLS, 443, - raw, "/.well-known/matrix/client" - ); - HttpRequestSendHeaders(ctx); - - switch (HttpRequestSend(ctx)) - { - case HTTP_OK: - stream = HttpClientStream(ctx); - reply = JsonDecode(stream); - if (!reply) - { - goto end; - } - - ret = UriParse(JsonValueAsString(JsonGet( - reply, 2, - "m.homeserver", "base_url" - ))); - break; - default: - /* TODO */ - break; - } - - if (ret && !ret->port && StrEquals(ret->proto, "https")) - { - ret->port = 443; - } - if (ret && !ret->port) - { - ret->port = 80; - } - -end: - HttpClientContextFree(ctx); - JsonFree(reply); - - return ret; -} +#include "common.h" int Main(Array *args, HashMap *env) diff --git a/tools/noavatars.c b/tools/noavatars.c index e8ef0f4..0c5526c 100644 --- a/tools/noavatars.c +++ b/tools/noavatars.c @@ -3,12 +3,7 @@ * Under CC0, as its a rather useful example of a Parsee tool. * See LICENSE for more information about Parsee's licensing. */ -#include -#include -#include -#include - -#include +#include "common.h" int Main(Array *args, HashMap *env) @@ -20,13 +15,13 @@ Main(Array *args, HashMap *env) if (ArraySize(args) < 2) { - Log(LOG_ERR, "Usage: %s [DB path]", exec); + Log(LOG_ERR, "Usage: %s [config]", exec); return EXIT_FAILURE; } db_path = ArrayGet(args, 1); - parsee = DbOpenLMDB(db_path, 8 * 1024 * 1024); + parsee = GetDB(db_path); if (parsee) { DbDelete(parsee, 1, "avatars");