]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Add C method to process ANN tokens vector
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 6 Jul 2019 11:29:25 +0000 (12:29 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 6 Jul 2019 11:29:25 +0000 (12:29 +0100)
src/libserver/rspamd_symcache.c
src/libserver/rspamd_symcache.h
src/lua/lua_task.c

index d6fa726c6b7799ff40ee452c89f88eb0ee970d64..3de339654f13ab010832e973c770dd28ff694931 100644 (file)
@@ -3390,4 +3390,14 @@ rspamd_symcache_process_settings_elt (struct rspamd_symcache *cache,
                        }
                }
        }
+}
+
+enum rspamd_symbol_type
+rspamd_symcache_item_flags (struct rspamd_symcache_item *item)
+{
+       if (item) {
+               return item->type;
+       }
+
+       return 0;
 }
\ No newline at end of file
index 7bfb1d09328880c6500556a30d87c06b3f55e076..640a1c89851a2c2e6835ffb8cad2a344518a808a 100644 (file)
@@ -493,4 +493,10 @@ gboolean rspamd_symcache_is_item_allowed (struct rspamd_task *task,
                                                                                  struct rspamd_symcache_item *item,
                                                                                  gboolean exec_only);
 
+/**
+ * Returns symbcache item flags
+ * @param item
+ * @return
+ */
+enum rspamd_symbol_type rspamd_symcache_item_flags (struct rspamd_symcache_item *item);
 #endif
index f9033eef832c4c5af4f0ed1888c4dbfd610739fe..99faa4b3e4952af38d8984dd313a50ccee490258 100644 (file)
@@ -640,6 +640,16 @@ LUA_FUNCTION_DEF (task, get_symbols_numeric);
  */
 LUA_FUNCTION_DEF (task, get_symbols_tokens);
 
+/***
+ * @method task:process_ann_tokens(symbols, ann_tokens, offset)
+ * Processes ann tokens
+ * @param {table|string} symbols list of symbols in this profile
+ * @param {table|number} ann_tokens list of tokens (including metatokens)
+ * @param {integer} offset offset for symbols token (#metatokens)
+ * @return nothing
+ */
+LUA_FUNCTION_DEF (task, process_ann_tokens);
+
 /***
  * @method task:has_symbol(name)
  * Fast path to check if a specified symbol is in the task's results
@@ -1118,6 +1128,7 @@ static const struct luaL_reg tasklib_m[] = {
        LUA_INTERFACE_DEF (task, get_symbols_all),
        LUA_INTERFACE_DEF (task, get_symbols_numeric),
        LUA_INTERFACE_DEF (task, get_symbols_tokens),
+       LUA_INTERFACE_DEF (task, process_ann_tokens),
        LUA_INTERFACE_DEF (task, has_symbol),
        LUA_INTERFACE_DEF (task, enable_symbol),
        LUA_INTERFACE_DEF (task, disable_symbol),
@@ -4236,6 +4247,45 @@ lua_task_get_symbols_tokens (lua_State *L)
        return 1;
 }
 
+static gint
+lua_task_process_ann_tokens (lua_State *L)
+{
+       LUA_TRACE_POINT;
+       struct rspamd_task *task = lua_check_task (L, 1);
+       gint offset = luaL_checkinteger (L, 4);
+
+       if (task && lua_istable (L, 2) && lua_istable (L, 3)) {
+               guint symlen = rspamd_lua_table_size (L, 2);
+
+               for (guint i = 1; i <= symlen; i ++, offset ++) {
+                       const gchar *sym;
+                       struct rspamd_symbol_result *sres;
+
+                       lua_rawgeti (L, 2, i);
+                       sym = lua_tostring (L, -1);
+
+                       sres = rspamd_task_find_symbol_result (task, sym);
+
+                       if (sres && !(sres->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) {
+                               if (!isnan (sres->score) &&
+                                       !(rspamd_symcache_item_flags (sres->sym->cache_item) &
+                                         SYMBOL_TYPE_NOSTAT)) {
+
+                                       lua_pushnumber (L, tanh (sres->score));
+                                       lua_rawseti (L, 3, offset + 1);
+                               }
+                       }
+
+                       lua_pop (L, 1); /* Symbol name */
+               }
+       }
+       else {
+               return luaL_error (L, "invalid arguments");
+       }
+
+       return 0;
+}
+
 enum lua_date_type {
        DATE_CONNECT = 0,
        DATE_MESSAGE,