]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add method to get all maps configured
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 26 Apr 2018 13:51:50 +0000 (14:51 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 26 Apr 2018 13:51:50 +0000 (14:51 +0100)
src/lua/lua_common.h
src/lua/lua_map.c

index 55070acf23501d98e0a935d8816c320145a6004a..457f470e96a1f73d10300cfd92289b3a19a068aa 100644 (file)
@@ -95,7 +95,8 @@ enum rspamd_lua_map_type {
        RSPAMD_LUA_MAP_HASH,
        RSPAMD_LUA_MAP_REGEXP,
        RSPAMD_LUA_MAP_REGEXP_MULTIPLE,
-       RSPAMD_LUA_MAP_CALLBACK
+       RSPAMD_LUA_MAP_CALLBACK,
+       RSPAMD_LUA_MAP_UNKNOWN,
 };
 
 struct rspamd_lua_map {
index 27e79ac2b1ffe3e3ca99b4dd4f9960a40e289656..7f5195d8e73a66ac314a714dbda5ecaa5b2f0c9c 100644 (file)
@@ -85,6 +85,15 @@ LUA_FUNCTION_DEF (map, set_callback);
  */
 LUA_FUNCTION_DEF (map, get_uri);
 
+/***
+ * @method map:get_stats(reset)
+ * Get statistics for specific map. It returns table in form:
+ *  [key] => [nhits]
+ * @param {boolean} reset reset stats if true
+ * @return {table} map's stat
+ */
+LUA_FUNCTION_DEF (map, get_stats);
+
 static const struct luaL_reg maplib_m[] = {
        LUA_INTERFACE_DEF (map, get_key),
        LUA_INTERFACE_DEF (map, is_signed),
@@ -93,6 +102,7 @@ static const struct luaL_reg maplib_m[] = {
        LUA_INTERFACE_DEF (map, set_sign_key),
        LUA_INTERFACE_DEF (map, set_callback),
        LUA_INTERFACE_DEF (map, get_uri),
+       LUA_INTERFACE_DEF (map, get_stats),
        {"__tostring", rspamd_lua_class_tostring},
        {NULL, NULL}
 };
@@ -596,6 +606,61 @@ lua_config_add_map (lua_State *L)
        return 1;
 }
 
+gint
+lua_config_get_maps (lua_State*L)
+{
+       struct rspamd_config *cfg = lua_check_config (L, 1);
+       struct rspamd_lua_map *map, **pmap;
+       struct rspamd_map *m;
+       gint i = 1;
+       GList *cur;
+
+       if (cfg) {
+               lua_newtable (L);
+               cur = g_list_first (cfg->maps);
+
+               while (cur) {
+                       m = cur->data;
+
+                       if (m->lua_map) {
+                               map = m->lua_map;
+                       }
+                       else {
+                               /* Implement heuristic */
+                               map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
+
+                               if (m->read_callback == rspamd_radix_read) {
+                                       map->type = RSPAMD_LUA_MAP_RADIX;
+                                       map->data.radix = *m->user_data;
+                               }
+                               else if (m->read_callback == rspamd_kv_list_read) {
+                                       map->type = RSPAMD_LUA_MAP_HASH;
+                                       map->data.hash = *m->user_data;
+                               }
+                               else {
+                                       map->type = RSPAMD_LUA_MAP_UNKNOWN;
+                               }
+
+                               map->map = m;
+                               m->lua_map = map;
+                       }
+
+                       pmap = lua_newuserdata (L, sizeof (*pmap));
+                       *pmap = map;
+                       rspamd_lua_setclass (L, "rspamd{map}", -1);
+                       lua_rawseti (L, -2, i);
+
+                       cur = g_list_next (cur);
+                       i ++;
+               }
+       }
+       else {
+               return luaL_error (L, "invalid arguments");
+       }
+
+       return 1;
+}
+
 static const gchar *
 lua_map_process_string_key (lua_State *L, gint pos, gsize *len)
 {