diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-10-26 17:35:59 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-10-26 17:35:59 +0000 |
commit | 3a293ef64cc1c088f36c6be297ab05c9c3e9d70a (patch) | |
tree | a222c0f325cd65bd809abe87351ea0354c1763d2 /src/lua/lua_expression.c | |
parent | e74409e4f5f504d4647af72098ddfa1aa2228e53 (diff) | |
download | rspamd-3a293ef64cc1c088f36c6be297ab05c9c3e9d70a.tar.gz rspamd-3a293ef64cc1c088f36c6be297ab05c9c3e9d70a.zip |
Add method to lua expression to extract all atoms
Diffstat (limited to 'src/lua/lua_expression.c')
-rw-r--r-- | src/lua/lua_expression.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/lua/lua_expression.c b/src/lua/lua_expression.c index d8bd0409e..bb65b561e 100644 --- a/src/lua/lua_expression.c +++ b/src/lua/lua_expression.c @@ -76,8 +76,16 @@ LUA_FUNCTION_DEF (expr, to_string); */ LUA_FUNCTION_DEF (expr, process); +/*** + * @method rspamd_expression:atoms() + * Extract all atoms from the expression as table of strings + * @return {table/strings} list of all atoms in the expression + */ +LUA_FUNCTION_DEF (expr, atoms); + static const struct luaL_reg exprlib_m[] = { LUA_INTERFACE_DEF (expr, to_string), + LUA_INTERFACE_DEF (expr, atoms), LUA_INTERFACE_DEF (expr, process), {"__tostring", lua_expr_to_string}, {NULL, NULL} @@ -297,6 +305,39 @@ lua_expr_to_string (lua_State *L) return 1; } +struct lua_expr_atoms_cbdata { + lua_State *L; + gint idx; +}; + +static void +lua_exr_atom_cb (const rspamd_ftok_t *tok, gpointer ud) +{ + struct lua_expr_atoms_cbdata *cbdata = ud; + + lua_pushlstring (cbdata->L, tok->begin, tok->len); + lua_rawseti (cbdata->L, -2, cbdata->idx ++); +} + +static gint +lua_expr_atoms (lua_State *L) +{ + struct lua_expression *e = rspamd_lua_expression (L, 1); + struct lua_expr_atoms_cbdata cbdata; + + if (e != NULL && e->expr != NULL) { + lua_newtable (L); + cbdata.L = L; + cbdata.idx = 1; + rspamd_expression_atom_foreach (e->expr, lua_exr_atom_cb, &cbdata); + } + else { + lua_pushnil (L); + } + + return 1; +} + static gint lua_load_expression (lua_State * L) { |