Skip to content

Commit

Permalink
[Project] Add C method to process ANN tokens vector
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Jul 6, 2019
1 parent ff02466 commit cb40d91
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/libserver/rspamd_symcache.c
Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions src/libserver/rspamd_symcache.h
Expand Up @@ -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
50 changes: 50 additions & 0 deletions src/lua/lua_task.c
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit cb40d91

Please sign in to comment.