From dd1f4534b3261e9cb1b3b3cfcceedb5e1f175e91 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 9 Nov 2023 15:16:33 +0000 Subject: [PATCH] [Minor] Allow to list storages configured from Lua --- src/plugins/fuzzy_check.c | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c index 6e59c45b8..96c283c5d 100644 --- a/src/plugins/fuzzy_check.c +++ b/src/plugins/fuzzy_check.c @@ -195,6 +195,7 @@ static gint fuzzy_lua_learn_handler(lua_State *L); static gint fuzzy_lua_unlearn_handler(lua_State *L); static gint fuzzy_lua_gen_hashes_handler(lua_State *L); static gint fuzzy_lua_hex_hashes_handler(lua_State *L); +static gint fuzzy_lua_list_storages(lua_State *L); module_t fuzzy_check_module = { "fuzzy_check", @@ -1223,6 +1224,9 @@ gint fuzzy_check_module_config(struct rspamd_config *cfg, bool validate) lua_pushstring(L, "hex_hashes"); lua_pushcfunction(L, fuzzy_lua_hex_hashes_handler); lua_settable(L, -3); + lua_pushstring(L, "list_storages"); + lua_pushcfunction(L, fuzzy_lua_list_storages); + lua_settable(L, -3); /* Finish fuzzy_check key */ lua_settable(L, -3); } @@ -4241,3 +4245,61 @@ fuzzy_attach_controller(struct module_ctx *ctx, GHashTable *commands) return 0; } + +static void +lua_upstream_str_inserter(struct upstream *up, guint idx, void *ud) +{ + lua_State *L = (lua_State *) ud; + + lua_pushstring(L, rspamd_upstream_name(up)); + lua_rawseti(L, -2, idx + 1); +} + +static gint +fuzzy_lua_list_storages(lua_State *L) +{ + struct rspamd_config *cfg = lua_check_config(L, 1); + + if (cfg == NULL) { + return luaL_error(L, "invalid arguments"); + } + + struct fuzzy_ctx *fuzzy_module_ctx = fuzzy_get_context(cfg); + struct fuzzy_rule *rule; + guint i; + + lua_createtable(L, 0, fuzzy_module_ctx->fuzzy_rules->len); + PTR_ARRAY_FOREACH(fuzzy_module_ctx->fuzzy_rules, i, rule) + { + lua_newtable(L); + + lua_pushboolean(L, rule->read_only); + lua_setfield(L, -2, "read_only"); + + /* Push servers */ + lua_createtable(L, rspamd_upstreams_count(rule->servers), 0); + rspamd_upstreams_foreach(rule->servers, lua_upstream_str_inserter, L); + lua_setfield(L, -2, "servers"); + + /* Push flags */ + GHashTableIter it; + + lua_createtable(L, 0, g_hash_table_size(rule->mappings)); + gpointer k, v; + struct fuzzy_mapping *map; + + g_hash_table_iter_init(&it, rule->mappings); + while (g_hash_table_iter_next(&it, &k, &v)) { + map = v; + + lua_pushinteger(L, map->fuzzy_flag); + lua_setfield(L, -2, map->symbol); + } + lua_setfield(L, -2, "flags"); + + /* Final table */ + lua_setfield(L, -2, rule->name); + } + + return 1; +} \ No newline at end of file -- 2.39.5