From 7e1e71a5459a0729326f15ee30e28ccb17e597f9 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 8 Feb 2011 21:32:19 +0300 Subject: [PATCH] Call lua functions correctly as well. --- src/expressions.c | 2 +- src/lua/lua_common.c | 34 ++++++++++++++++++++++++++++++---- src/lua/lua_common.h | 2 +- src/plugins/regexp.c | 2 +- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/expressions.c b/src/expressions.c index 24540b44c..c3b45ef7c 100644 --- a/src/expressions.c +++ b/src/expressions.c @@ -750,7 +750,7 @@ call_expression_function (struct expression_function * func, struct worker_task if (selected == NULL) { /* Try to check lua function */ #ifdef RSPAMD_MAIN - if (! lua_call_expression_func (func->name, task, func->args, &res)) { + if (! lua_call_expression_func (NULL, func->name, task, func->args, &res)) { msg_warn ("call to undefined function %s", key.name); return FALSE; } diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index 9c4cef113..bc3748f4c 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -356,15 +356,40 @@ lua_call_chain_filter (const gchar *function, struct worker_task *task, gint *ma /* Call custom lua function in rspamd expression */ gboolean -lua_call_expression_func (const gchar *function, struct worker_task *task, GList *args, gboolean *res) +lua_call_expression_func (const gchar *module, const gchar *function, + struct worker_task *task, GList *args, gboolean *res) { lua_State *L = task->cfg->lua_state; struct worker_task **ptask; GList *cur; struct expression_argument *arg; - gint nargs = 0; - - lua_getglobal (L, function); + int nargs = 0; + + /* Call specified function and expect result of given expected_type */ + /* First check function in config table */ + lua_getglobal (L, "config"); + if (module != NULL && lua_istable (L, -1)) { + lua_pushstring (L, module); + lua_gettable (L, -2); + if (lua_isnil (L, -1)) { + /* Try to get global variable */ + lua_getglobal (L, function); + } + else { + /* Call local function in table */ + lua_pushstring (L, function); + lua_gettable (L, -2); + } + } + else { + /* Try to get global variable */ + lua_getglobal (L, function); + } + if (lua_isnil (L, -1)) { + msg_err ("function with name %s is not defined", function); + return FALSE; + } + /* Now we got function in top of stack */ ptask = lua_newuserdata (L, sizeof (struct worker_task *)); lua_setclass (L, "rspamd{task}", -1); *ptask = task; @@ -404,6 +429,7 @@ lua_call_expression_func (const gchar *function, struct worker_task *task, GList return TRUE; } + /* * LUA custom consolidation function */ diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index d70501034..4851dc1a6 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -42,7 +42,7 @@ gboolean init_lua_filters (struct config_file *cfg); gint lua_call_filter (const gchar *function, struct worker_task *task); gint lua_call_chain_filter (const gchar *function, struct worker_task *task, gint *marks, guint number); double lua_consolidation_func (struct worker_task *task, const gchar *metric_name, const gchar *function_name); -gboolean lua_call_expression_func (const gchar *function, struct worker_task *task, GList *args, gboolean *res); +gboolean lua_call_expression_func (const gchar *module, const gchar *symbol, struct worker_task *task, GList *args, gboolean *res); void lua_call_post_filters (struct worker_task *task); void add_luabuf (const gchar *line); diff --git a/src/plugins/regexp.c b/src/plugins/regexp.c index ce6599e86..4ae056bee 100644 --- a/src/plugins/regexp.c +++ b/src/plugins/regexp.c @@ -1017,7 +1017,7 @@ process_regexp_item (struct worker_task *task, void *user_data) if (item->lua_function) { /* Just call function */ - if (lua_call_expression_func (item->lua_function, task, NULL, &res) && res) { + if (lua_call_expression_func ("regexp", item->lua_function, task, NULL, &res) && res) { insert_result (task, item->symbol, 1, NULL); } } -- 2.39.5