/* 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; }