diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-03-31 14:31:41 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-03-31 14:31:41 +0100 |
commit | 9edaaecc87bcef858af637ac27859117dd34e2b9 (patch) | |
tree | 30c5b1279514e6a88097f847ab887c27087f1b88 /src/lua/lua_expression.c | |
parent | 0db46326589192d66babcd781e627eaa2b99ae4d (diff) | |
download | rspamd-9edaaecc87bcef858af637ac27859117dd34e2b9.tar.gz rspamd-9edaaecc87bcef858af637ac27859117dd34e2b9.zip |
[Feature] Add expression:process_traced method
Diffstat (limited to 'src/lua/lua_expression.c')
-rw-r--r-- | src/lua/lua_expression.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/lua/lua_expression.c b/src/lua/lua_expression.c index fe52e929b..ac5933993 100644 --- a/src/lua/lua_expression.c +++ b/src/lua/lua_expression.c @@ -68,6 +68,14 @@ LUA_FUNCTION_DEF (expr, to_string); LUA_FUNCTION_DEF (expr, process); /*** + * @method rspamd_expression:process_traced(input) + * Executes the expression and pass input to process atom callbacks. This function also saves the full trace + * @param {any} input input data for processing callbacks + * @return {number, table of matched atoms} result of the expression evaluation + */ +LUA_FUNCTION_DEF (expr, process_traced); + +/*** * @method rspamd_expression:atoms() * Extract all atoms from the expression as table of strings * @return {table/strings} list of all atoms in the expression @@ -78,6 +86,7 @@ static const struct luaL_reg exprlib_m[] = { LUA_INTERFACE_DEF (expr, to_string), LUA_INTERFACE_DEF (expr, atoms), LUA_INTERFACE_DEF (expr, process), + LUA_INTERFACE_DEF (expr, process_traced), {"__tostring", lua_expr_to_string}, {NULL, NULL} }; @@ -197,6 +206,40 @@ lua_expr_process (lua_State *L) } static gint +lua_expr_process_traced (lua_State *L) +{ + struct lua_expression *e = rspamd_lua_expression (L, 1); + rspamd_expression_atom_t *atom; + gint res; + guint i; + gint flags = 0; + GPtrArray *trace; + + if (lua_gettop (L) >= 3) { + flags = lua_tonumber (L, 3); + } + + trace = g_ptr_array_sized_new (32); + res = rspamd_process_expression_track (e->expr, flags, GINT_TO_POINTER (2), + trace); + + lua_pushnumber (L, res); + + lua_createtable (L, trace->len, 0); + + for (i = 0; i < trace->len; i ++) { + atom = g_ptr_array_index (trace, i); + + lua_pushlstring (L, atom->str, atom->len); + lua_rawseti (L, -2, i + 1); + } + + g_ptr_array_free (trace, TRUE); + + return 2; +} + +static gint lua_expr_create (lua_State *L) { struct lua_expression *e, **pe; |