From e80aae67f2e95519c8dd5259f57b8dc32b97487d Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 5 May 2015 15:57:47 +0100 Subject: [PATCH] `get_all_opt` now flattens the content of objects So now in a configuration with multiple section with the same name, for example: module { param1 = value1; param2 = value2; } module { param2 = value2_1; param3 = value3; } this function will return a single lua table that looks as following: param1 = value1, param2 = value2_1, param3 = value3 --- src/lua/lua_config.c | 29 +++++++++++++++++++++++++---- src/plugins/lua/settings.lua | 2 +- src/plugins/lua/spamassassin.lua | 2 +- src/plugins/lua/trie.lua | 2 +- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index ad980e57e..938c0fa62 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -28,6 +28,7 @@ #include "message.h" #include "radix.h" #include "expression.h" +#include "utlist.h" /*** * This module is used to configure rspamd and is normally available as global @@ -57,7 +58,8 @@ local opts = rspamd_config:get_key('options') -- get content of the specified ke LUA_FUNCTION_DEF (config, get_module_opt); /*** * @method rspamd_config:get_all_opt(mname) - * Returns value of all options for a module `mname`, + * Returns value of all options for a module `mname`, flattening values into a single table consisting + * of all sections with such a name. * @param {string} mname name of module * @return {table} table of all options for `mname` or `nil` if a module's configuration is not found */ @@ -407,19 +409,38 @@ lua_config_get_all_opt (lua_State * L) { struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *mname; - const ucl_object_t *obj; + const ucl_object_t *obj, *cur, *cur_elt; + ucl_object_iter_t it = NULL; if (cfg) { mname = luaL_checkstring (L, 2); if (mname) { obj = ucl_obj_get_key (cfg->rcl_obj, mname); - if (obj != NULL) { - return ucl_object_push_lua (L, obj, TRUE); + /* Flatten object */ + if (obj != NULL && ucl_object_type (obj) == UCL_OBJECT) { + + lua_newtable (L); + it = ucl_object_iterate_new (obj); + + LL_FOREACH (obj, cur) { + it = ucl_object_iterate_reset (it, cur); + + while ((cur_elt = ucl_object_iterate_safe (it, true))) { + lua_pushstring (L, ucl_object_key (cur_elt)); + ucl_object_push_lua (L, cur_elt, true); + lua_settable (L, -3); + } + } + + ucl_object_iterate_free (it); + + return 1; } } } lua_pushnil (L); + return 1; } diff --git a/src/plugins/lua/settings.lua b/src/plugins/lua/settings.lua index 4b984a07e..5d2181e00 100644 --- a/src/plugins/lua/settings.lua +++ b/src/plugins/lua/settings.lua @@ -28,7 +28,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -- Settings documentation can be found here: -- https://rspamd.com/doc/configuration/settings.html -local set_section = rspamd_config:get_key("settings") +local set_section = rspamd_config:get_all_opt("settings") local settings = {} local settings_initialized = false local max_pri = 0 diff --git a/src/plugins/lua/spamassassin.lua b/src/plugins/lua/spamassassin.lua index 8a5fc79b2..5960c1221 100644 --- a/src/plugins/lua/spamassassin.lua +++ b/src/plugins/lua/spamassassin.lua @@ -56,7 +56,7 @@ local replace = { post = {}, rules = {} } -local section = rspamd_config:get_key("spamassassin") +local section = rspamd_config:get_all_opt("spamassassin") -- Minimum score to treat symbols as meta local meta_score_alpha = 0.5 diff --git a/src/plugins/lua/trie.lua b/src/plugins/lua/trie.lua index 81f20bfc8..279fc2d67 100644 --- a/src/plugins/lua/trie.lua +++ b/src/plugins/lua/trie.lua @@ -132,7 +132,7 @@ local function process_trie_conf(symbol, cf) rspamd_config:register_virtual_symbol(symbol, 1.0) end -local opts = rspamd_config:get_key("trie") +local opts = rspamd_config:get_all_opt("trie") if opts then for sym, opt in pairs(opts) do process_trie_conf(sym, opt) -- 2.39.5