From: Vsevolod Stakhov Date: Wed, 12 Nov 2014 16:32:58 +0000 (+0000) Subject: Rework calling of lua functions from regexp module. X-Git-Tag: 0.7.5~12 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=55abc1e4fd53a5eb9bfcb89661ba2ae4308deb5a;p=rspamd.git Rework calling of lua functions from regexp module. --- diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index a4ae97590..8df878585 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -476,61 +476,6 @@ rspamd_lua_call_chain_filter (const gchar *function, return result; } -/* Call custom lua function in rspamd expression */ -gboolean -rspamd_lua_call_expression_func (gpointer lua_data, - struct rspamd_task *task, GList *args, gboolean *res) -{ - lua_State *L = task->cfg->lua_state; - struct rspamd_task **ptask; - GList *cur; - struct expression_argument *arg; - int nargs = 1, pop = 0; - - lua_rawgeti (L, LUA_REGISTRYINDEX, GPOINTER_TO_INT (lua_data)); - /* Now we got function in top of stack */ - ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); - rspamd_lua_setclass (L, "rspamd{task}", -1); - *ptask = task; - - /* Now push all arguments */ - cur = args; - while (cur) { - arg = get_function_arg (cur->data, task, FALSE); - if (arg) { - switch (arg->type) { - case EXPRESSION_ARGUMENT_NORMAL: - lua_pushstring (L, (const gchar *)arg->data); - break; - case EXPRESSION_ARGUMENT_BOOL: - lua_pushboolean (L, (gboolean) GPOINTER_TO_SIZE (arg->data)); - break; - default: - msg_err ("cannot pass custom params to lua function"); - return FALSE; - } - } - nargs++; - cur = g_list_next (cur); - } - - if (lua_pcall (L, nargs, 1, 0) != 0) { - msg_info ("call to lua function failed: %s", lua_tostring (L, -1)); - return FALSE; - } - pop++; - - if (!lua_isboolean (L, -1)) { - lua_pop (L, pop); - msg_info ("lua function must return a boolean"); - return FALSE; - } - *res = lua_toboolean (L, -1); - lua_pop (L, pop); - - return TRUE; -} - /* * LUA custom consolidation function diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index afcfcdcdb..cbcdf5b68 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -182,10 +182,6 @@ gint rspamd_lua_call_chain_filter (const gchar *function, double rspamd_lua_consolidation_func (struct rspamd_task *task, const gchar *metric_name, const gchar *function_name); -gboolean rspamd_lua_call_expression_func (gpointer lua_data, - struct rspamd_task *task, - GList *args, - gboolean *res); void rspamd_lua_call_post_filters (struct rspamd_task *task); void rspamd_lua_call_pre_filters (struct rspamd_task *task); void rspamd_lua_dostring (const gchar *line); diff --git a/src/plugins/regexp.c b/src/plugins/regexp.c index d1f456a9f..ea0dc8781 100644 --- a/src/plugins/regexp.c +++ b/src/plugins/regexp.c @@ -40,7 +40,7 @@ struct regexp_module_item { struct expression *expr; const gchar *symbol; guint32 avg_time; - gpointer lua_function; + struct ucl_lua_funcdata *lua_function; }; struct regexp_ctx { @@ -341,7 +341,7 @@ regexp_module_config (struct rspamd_config *cfg) cur_item = rspamd_mempool_alloc0 (regexp_module_ctx->regexp_pool, sizeof (struct regexp_module_item)); cur_item->symbol = ucl_object_key (value); - cur_item->lua_function = value->value.ud; + cur_item->lua_function = ucl_object_toclosure (value); register_symbol (&cfg->cache, cur_item->symbol, 1, @@ -1001,6 +1001,61 @@ process_regexp_expression (struct expression *expr, return FALSE; } +/* Call custom lua function in rspamd expression */ +static gboolean +rspamd_lua_call_expression_func (struct ucl_lua_funcdata *lua_data, + struct rspamd_task *task, GList *args, gboolean *res) +{ + lua_State *L = lua_data->L; + struct rspamd_task **ptask; + GList *cur; + struct expression_argument *arg; + int nargs = 1, pop = 0; + + lua_rawgeti (L, LUA_REGISTRYINDEX, lua_data->idx); + /* Now we got function in top of stack */ + ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); + rspamd_lua_setclass (L, "rspamd{task}", -1); + *ptask = task; + + /* Now push all arguments */ + cur = args; + while (cur) { + arg = get_function_arg (cur->data, task, FALSE); + if (arg) { + switch (arg->type) { + case EXPRESSION_ARGUMENT_NORMAL: + lua_pushstring (L, (const gchar *)arg->data); + break; + case EXPRESSION_ARGUMENT_BOOL: + lua_pushboolean (L, (gboolean) GPOINTER_TO_SIZE (arg->data)); + break; + default: + msg_err ("cannot pass custom params to lua function"); + return FALSE; + } + } + nargs++; + cur = g_list_next (cur); + } + + if (lua_pcall (L, nargs, 1, 0) != 0) { + msg_info ("call to lua function failed: %s", lua_tostring (L, -1)); + return FALSE; + } + pop++; + + if (!lua_isboolean (L, -1)) { + lua_pop (L, pop); + msg_info ("lua function must return a boolean"); + return FALSE; + } + *res = lua_toboolean (L, -1); + lua_pop (L, pop); + + return TRUE; +} + struct regexp_threaded_ud { struct regexp_module_item *item; struct rspamd_task *task;