From 06496123199f364065fa1e75bd4cd9d89304070d Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 11 Nov 2009 18:48:34 +0300 Subject: [PATCH] * Add right way to pass config params with common names to lua scripts --- src/lua/lua_config.c | 62 ++++++++++++++++++++++++++++--- src/plugins/lua/once_received.lua | 4 +- src/plugins/lua/received_rbl.lua | 15 ++++---- 3 files changed, 66 insertions(+), 15 deletions(-) diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index defc9508f..377d72be5 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -129,13 +129,23 @@ lua_config_get_module_opt (lua_State * L) return 1; } +static int +opt_compare (gconstpointer a, gconstpointer b) +{ + const struct module_opt *o1 = a, + *o2 = b; + + return g_ascii_strcasecmp (o1->param, o2->param); +} + static int lua_config_get_all_opt (lua_State * L) { struct config_file *cfg = lua_check_config (L); const char *mname; - GList *cur_opt; - struct module_opt *cur; + GList *cur_opt, *next_opt; + struct module_opt *opt, *tmp; + int i; if (cfg) { mname = luaL_checkstring (L, 2); @@ -146,12 +156,54 @@ lua_config_get_all_opt (lua_State * L) lua_pushnil (L); return 1; } + /* Sort options in alphabet order by param name */ + cur_opt = g_list_sort (cur_opt, opt_compare); + g_hash_table_insert (cfg->modules_opts, (gpointer)mname, cur_opt); lua_newtable (L); while (cur_opt) { - cur = cur_opt->data; - lua_set_table_index (L, cur->param, cur->value); - cur_opt = g_list_next (cur_opt); + opt = cur_opt->data; + next_opt = g_list_next (cur_opt); + if (next_opt) { + tmp = next_opt->data; + if (g_ascii_strcasecmp (tmp->param, opt->param) == 0) { + /* We have some common values */ + lua_pushstring (L, opt->param); + lua_newtable (L); + /* Now stack looks like: + * table - parent associated table of options + * key - string key of this option + * table - array of values, beginig from 1 + */ + + for (i = 1; ; i++) { + lua_pushinteger (L, i); + lua_pushstring (L, opt->value); + lua_settable (L, -3); + + cur_opt = g_list_next (cur_opt); + if (!cur_opt) { + break; + } + tmp = cur_opt->data; + if (g_ascii_strcasecmp (tmp->param, opt->param) != 0) { + break; + } + opt = tmp; + } + /* Now set index in parent table */ + lua_settable (L, -3); + /* Now continue in outter cycle */ + continue; + } + else { + lua_set_table_index (L, opt->param, opt->value); + } + } + else { + lua_set_table_index (L, opt->param, opt->value); + } + cur_opt = next_opt; } return 1; } diff --git a/src/plugins/lua/once_received.lua b/src/plugins/lua/once_received.lua index 3840eb0b0..bf8e31462 100644 --- a/src/plugins/lua/once_received.lua +++ b/src/plugins/lua/once_received.lua @@ -51,9 +51,9 @@ if opts then if n == 'symbol_strict' then symbol_strict = v elseif n == 'bad_host' then - table.insert(bad_hosts, string.lower(v)) + bad_hosts = v elseif n == 'good_host' then - table.insert(good_hosts, string.lower(v)) + good_hosts = v elseif n == 'metric' then metric = v end diff --git a/src/plugins/lua/received_rbl.lua b/src/plugins/lua/received_rbl.lua index 1e0f7d4d6..045b56e7f 100644 --- a/src/plugins/lua/received_rbl.lua +++ b/src/plugins/lua/received_rbl.lua @@ -38,14 +38,13 @@ local opts = rspamd_config:get_all_opt('received_rbl') if opts then if opts['symbol'] then symbol = opts['symbol'] - - for n,v in pairs(opts) do - if n == 'rbl' then - table.insert(rbls, v) - elseif n == 'metric' then - metric = v - end - end + + if opts['metric'] then + metric = opts['metric'] + end + if opts['rbl'] then + rbls = opts['rbl'] + end -- Register symbol's callback local m = rspamd_config:get_metric(metric) m:register_symbol(symbol, 1.0, 'received_cb') -- 2.39.5