Skip to content

Commit

Permalink
[Minor] Allow to list storages configured from Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Nov 9, 2023
1 parent 619f90f commit dd1f453
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/plugins/fuzzy_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

0 comments on commit dd1f453

Please sign in to comment.