From af2d08a4311ca1bd211b088cf825e811194b39e5 Mon Sep 17 00:00:00 2001 From: LDA Date: Sat, 28 Sep 2024 13:24:38 +0200 Subject: [PATCH] [ADD/WIP] Start introducing whitelist command The whitelist system will also be managable from XMPP and Matrix, and will be for MUC/room management. --- tools/whitelist.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 tools/whitelist.c diff --git a/tools/whitelist.c b/tools/whitelist.c new file mode 100644 index 0000000..e1348d5 --- /dev/null +++ b/tools/whitelist.c @@ -0,0 +1,114 @@ +/* whitelist.c - Manages a Parsee MUC/user whitelist manually + * ============================================================ + * Example of usage: + * parsee-whitelist [CONFIG] [add|del|remove|list] + * Under CC0, as its a rather useful example of a Parsee tool. + * See LICENSE for more information about Parsee's licensing. */ + +#include "common.h" + +#include + +int +Main(Array *args, HashMap *env) +{ + int ret = EXIT_SUCCESS; + char *verb; + Db *db; + if (ArraySize(args) <= 2) + { + Log(LOG_ERR, + "Usage: %s [CONFIG] [add|del|clear|list] ", + ArrayGet(args, 0) + ); + return EXIT_FAILURE; + } + + if (!(db = GetDB(ArrayGet(args, 1)))) + { + Log(LOG_ERR, "Couldn't load database"); + ret = EXIT_FAILURE; + goto end; + } + + verb = ArrayGet(args, 2); + if (StrEquals(verb, "add")) + { + char *user = ArrayGet(args, 3); + DbRef *ref = !user ? NULL : DbLock( + db, 1, "whitelist" + ); + if (!ref && user) + { + ref = DbCreate( + db, 1, "whitelist" + ); + } + + if (!user) + { + ret = EXIT_FAILURE; + goto end; + } + JsonValueFree(HashMapSet( + DbJson(ref), + user, JsonValueObject(HashMapCreate()) + )); + DbUnlock(db, ref); + } + else if (StrEquals(verb, "del")) + { + char *user = ArrayGet(args, 3); + DbRef *ref = !user ? NULL : DbLock( + db, 1, "whitelist" + ); + if (!ref && user) + { + ref = DbCreate( + db, 1, "whitelist" + ); + } + + if (!user) + { + ret = EXIT_FAILURE; + goto end; + } + JsonValueFree(HashMapDelete(DbJson(ref), user)); + DbUnlock(db, ref); + } + else if (StrEquals(verb, "clear")) + { + DbDelete(db, 1, "whitelist"); + } + else if (StrEquals(verb, "list")) + { + DbRef *ref = DbLockIntent( + db, DB_HINT_READONLY, + 1, "whitelist" + ); + Array *keys = HashMapKeys(DbJson(ref)); + size_t i; + + for (i = 0; i < ArraySize(keys); i++) + { + char *key = ArrayGet(keys, i); + + Log(LOG_INFO, "- %s", key); + } + + + ArrayFree(keys); + DbUnlock(db, ref); + } + else + { + ret = EXIT_FAILURE; + goto end; + } + +end: + (void) env; + DbClose(db); + return ret; +}