]> source.dussan.org Git - rspamd.git/commitdiff
Fix calling of lua from rspamadm
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 15 Dec 2015 00:19:34 +0000 (00:19 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 15 Dec 2015 00:19:34 +0000 (00:19 +0000)
src/rspamadm/fuzzy_stat.lua [new file with mode: 0644]
src/rspamadm/rspamadm.c

diff --git a/src/rspamadm/fuzzy_stat.lua b/src/rspamadm/fuzzy_stat.lua
new file mode 100644 (file)
index 0000000..5bc49d1
--- /dev/null
@@ -0,0 +1,75 @@
+--Res here is the table of the following args:
+--workers: {
+-- pid: {
+--  data: {
+--    key_id: {
+--      matched:
+--      scanned:
+--      added:
+--      removed:
+--      errors:
+--      last_ips: {
+--        ip: {
+--          matched:
+--          scanned
+--          added:
+--          removed:
+--        }
+--      }
+--    }
+--  }
+-- }
+--}
+
+local function add_data(target, src)
+  for k,v in pairs(src) do
+    if k ~= 'ips' then
+      if target[k] then
+        target[k] = target[k] + v
+      else
+        target[k] = v
+      end
+    else
+      if target['ips'] then
+        add_data(target['ips'], v)
+      else
+        target['ips'] = {}
+        add_data(target['ips'], v)
+      end
+    end
+  end
+end
+
+return function(args, res)
+  local res_keys = {}
+  local res_ips = {}
+
+  local wrk = res['workers']
+
+  if wrk then
+    for i,pr in pairs(wrk) do
+      -- processes cycle
+      if pr['data'] then
+        for k,elts in pairs(pr['data']) do
+          -- keys cycle
+          if not res_keys[k] then
+            res_keys[k] = {}
+          end
+          add_data(res_keys[k], elts)
+
+          if elts['ips'] then
+            for ip,v in pairs(elts['ips']) do
+              if not res_ips[ip] then
+                res_ips[ip] = {}
+              end
+              add_data(res_ips[ip], v)
+            end
+          end
+        end
+      end
+    end
+  end
+
+
+end
+
index ac12359e1fa2e05fa481b4ce3cb89b823063a4b6..735f1299db8fb277f913471bd196779487af7501 100644 (file)
@@ -226,6 +226,9 @@ rspamadm_execute_lua_ucl_subr (gpointer pL, gint argc, gchar **argv,
        lua_pushcfunction (L, &rspamd_lua_traceback);
        err_idx = lua_gettop (L);
 
+       /* Push function */
+       lua_rawgeti (L, LUA_REGISTRYINDEX, cb_idx);
+
        /* Push argv */
        lua_newtable (L);
 
@@ -237,9 +240,9 @@ rspamadm_execute_lua_ucl_subr (gpointer pL, gint argc, gchar **argv,
        /* Push results */
        ucl_object_push_lua (L, res, TRUE);
 
-       if (lua_pcall (L, 1, 0, err_idx) != 0) {
+       if (lua_pcall (L, 2, 0, err_idx) != 0) {
                tb = lua_touserdata (L, -1);
-               msg_err ("call to user extraction script failed: %v", tb);
+               msg_err ("call to adm lua script failed: %v", tb);
                g_string_free (tb, TRUE);
                lua_pop (L, 1);
 
@@ -249,6 +252,8 @@ rspamadm_execute_lua_ucl_subr (gpointer pL, gint argc, gchar **argv,
        /* error function */
        lua_pop (L, 1);
 
+       luaL_unref (L, LUA_REGISTRYINDEX, cb_idx);
+
        return TRUE;
 }