mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 10:45:11 +00:00
[ADD/WIP] Start introducing whitelist command
The whitelist system will also be managable from XMPP and Matrix, and will be for MUC/room management.
This commit is contained in:
parent
3ceae7b053
commit
af2d08a431
1 changed files with 114 additions and 0 deletions
114
tools/whitelist.c
Normal file
114
tools/whitelist.c
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
/* whitelist.c - Manages a Parsee MUC/user whitelist manually
|
||||
* ============================================================
|
||||
* Example of usage:
|
||||
* parsee-whitelist [CONFIG] [add|del|remove|list] <user>
|
||||
* 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 <Cytoplasm/Str.h>
|
||||
|
||||
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] <user>",
|
||||
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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue