]> source.dussan.org Git - rspamd.git/commitdiff
* Add right way to pass config params with common names to lua scripts
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Wed, 11 Nov 2009 15:48:34 +0000 (18:48 +0300)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Wed, 11 Nov 2009 15:48:34 +0000 (18:48 +0300)
src/lua/lua_config.c
src/plugins/lua/once_received.lua
src/plugins/lua/received_rbl.lua

index defc9508f9d4b4a9ab3f98eec087a71d14d8afb4..377d72be5e4c6a254f9e06d920e151110e3e8374 100644 (file)
@@ -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;
                }
index 3840eb0b0c4bbd8d68e1e97c2146f13f73960ecb..bf8e31462bf028eb6be71e190d64886e19ab16f3 100644 (file)
@@ -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
index 1e0f7d4d692a7a0f8f0dd244c710baf52474bd06..045b56e7f29e6bd4e8d7dece99855cc656719656 100644 (file)
@@ -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')