]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Further reworks of rescore tool
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 30 May 2018 13:07:59 +0000 (14:07 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 30 May 2018 13:07:59 +0000 (14:07 +0100)
lualib/rescore_utility.lua
src/libserver/symbols_cache.c
src/lua/lua_config.c

index 268e814d858d03a109efd863fad9ee7eae0a4f6d..39ad63365d4305454805ac11f7cd48e815d41670 100644 (file)
@@ -78,13 +78,13 @@ function utility.get_all_logs(dirs)
 end
 
 function utility.get_all_symbol_scores(conf, ignore_symbols)
-  local counters = conf:get_symbols_counters()
+  local symbols = conf:get_symbols_scores()
 
-  return fun.tomap(fun.map(function(elt)
-    return elt['symbol'],elt['weight']
-  end, fun.filter(function(elt)
-    return not ignore_symbols[elt['symbol']]
-  end, counters)))
+  return fun.tomap(fun.map(function(name, elt)
+    return name,elt['score']
+  end, fun.filter(function(name, elt)
+    return not ignore_symbols[name]
+  end, symbols)))
 end
 
 function utility.generate_statistics_from_logs(logs, threshold)
index abed26fc346b99bf68185329583c7c05b7604b54..23ad15ed14ed7bc6975ac6f2ec834e76d1659c81 100644 (file)
@@ -2037,7 +2037,7 @@ rspamd_symbols_cache_counters (struct symbols_cache * cache)
        top = ucl_object_typed_new (UCL_ARRAY);
        cbd.top = top;
        cbd.cache = cache;
-       g_ptr_array_foreach (cache->items_by_order->d,
+       g_ptr_array_foreach (cache->items_by_id,
                        rspamd_symbols_cache_counters_cb, &cbd);
 
        return top;
index 73724fa5157a9d92baaabe003ef82e28bf15d61a..f54411e8871cd62a93a7e7d896f10c0bae82c438 100644 (file)
@@ -545,6 +545,13 @@ LUA_FUNCTION_DEF (config, get_symbols_cksum);
  */
 LUA_FUNCTION_DEF (config, get_symbols_counters);
 
+/***
+ * @method rspamd_config:get_symbols_scores()
+ * Returns table of all scores defined in config
+ * @return {table|tables} all symbols indexed by name
+ */
+LUA_FUNCTION_DEF (config, get_symbols_scores);
+
 /***
  * @method rspamd_config:get_symbol_callback(name)
  * Returns callback function for the specified symbol if it is a lua registered callback
@@ -742,6 +749,7 @@ static const struct luaL_reg configlib_m[] = {
        LUA_INTERFACE_DEF (config, get_symbols_count),
        LUA_INTERFACE_DEF (config, get_symbols_cksum),
        LUA_INTERFACE_DEF (config, get_symbols_counters),
+       LUA_INTERFACE_DEF (config, get_symbols_scores),
        LUA_INTERFACE_DEF (config, get_symbol_callback),
        LUA_INTERFACE_DEF (config, set_symbol_callback),
        LUA_INTERFACE_DEF (config, get_symbol_stat),
@@ -2882,6 +2890,44 @@ lua_config_get_symbols_counters (lua_State *L)
 
        return 1;
 }
+static void
+lua_metric_symbol_inserter (gpointer k, gpointer v, gpointer ud)
+{
+       lua_State *L = (lua_State *) ud;
+       const gchar *sym = k;
+       struct rspamd_symbol *s = (struct rspamd_symbol *) v;
+
+       lua_pushstring (L, sym);
+
+       lua_createtable (L, 0, 3); /* TODO: add more if needed */
+       lua_pushstring (L, "score");
+       lua_pushnumber (L, s->score);
+       lua_settable (L, -3);
+       lua_pushstring (L, "description");
+       lua_pushstring (L, s->description);
+       lua_settable (L, -3);
+
+       lua_settable (L, -3);
+}
+
+static gint
+lua_config_get_symbols_scores (lua_State *L)
+{
+       struct rspamd_config *cfg = lua_check_config (L, 1);
+
+       if (cfg != NULL) {
+               lua_createtable (L, 0, g_hash_table_size (cfg->symbols));
+               g_hash_table_foreach (cfg->symbols,
+                               lua_metric_symbol_inserter,
+                               L);
+       }
+       else {
+               return luaL_error (L, "invalid arguments");
+       }
+
+       return 1;
+}
+
 
 static gint
 lua_config_get_symbol_callback (lua_State *L)
@@ -3334,7 +3380,6 @@ lua_config_parse_rcl (lua_State *L)
                return luaL_error (L, "invalid arguments");
        }
 
-       rspamd_config_post_load (cfg, RSPAMD_CONFIG_INIT_SYMCACHE);
        g_hash_table_unref (excluded);
        rspamd_rcl_section_free (top);
        lua_pushboolean (L, true);