diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-07-23 12:45:28 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-07-23 12:45:28 +0100 |
commit | e0483657ff6cf1adc828ccce457814d61fe90a0d (patch) | |
tree | 5183e4163f40b81b3e7d5f51488d360883782154 /src/lua/lua_common.c | |
parent | 7962087e808fb824aa3af6d41d02abc92916ba1e (diff) | |
download | rspamd-e0483657ff6cf1adc828ccce457814d61fe90a0d.tar.gz rspamd-e0483657ff6cf1adc828ccce457814d61fe90a0d.zip |
Unify code style.
Diffstat (limited to 'src/lua/lua_common.c')
-rw-r--r-- | src/lua/lua_common.c | 284 |
1 files changed, 164 insertions, 120 deletions
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index ea1e43010..cb2730231 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -22,13 +22,13 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "lua_common.h" #include "expressions.h" +#include "lua_common.h" /* Lua module init function */ #define MODULE_INIT_FUNC "module_init" -const luaL_reg null_reg[] = { +const luaL_reg null_reg[] = { {"__tostring", lua_class_tostring}, {NULL, NULL} }; @@ -39,7 +39,7 @@ LUA_FUNCTION_DEF (logger, warn); LUA_FUNCTION_DEF (logger, info); LUA_FUNCTION_DEF (logger, debug); -static const struct luaL_reg loggerlib_f[] = { +static const struct luaL_reg loggerlib_f[] = { LUA_INTERFACE_DEF (logger, err), LUA_INTERFACE_DEF (logger, warn), LUA_INTERFACE_DEF (logger, info), @@ -57,16 +57,18 @@ static const struct luaL_reg loggerlib_f[] = { * @param func table of class methods */ void -lua_newclass (lua_State * L, const gchar *classname, const struct luaL_reg *methods) +lua_newclass (lua_State * L, + const gchar *classname, + const struct luaL_reg *methods) { - luaL_newmetatable (L, classname); /* mt */ + luaL_newmetatable (L, classname); /* mt */ lua_pushstring (L, "__index"); - lua_pushvalue (L, -2); /* pushes the metatable */ - lua_settable (L, -3); /* metatable.__index = metatable */ + lua_pushvalue (L, -2); /* pushes the metatable */ + lua_settable (L, -3); /* metatable.__index = metatable */ - lua_pushstring (L, "class"); /* mt,"__index",it,"class" */ - lua_pushstring (L, classname); /* mt,"__index",it,"class",classname */ - lua_rawset (L, -3); /* mt,"__index",it */ + lua_pushstring (L, "class"); /* mt,"__index",it,"class" */ + lua_pushstring (L, classname); /* mt,"__index",it,"class",classname */ + lua_rawset (L, -3); /* mt,"__index",it */ luaL_register (L, NULL, methods); } @@ -74,7 +76,11 @@ lua_newclass (lua_State * L, const gchar *classname, const struct luaL_reg *meth * Create and register new class with static methods and store metatable on top of the stack */ void -lua_newclass_full (lua_State *L, const gchar *classname, const gchar *static_name, const struct luaL_reg *methods, const struct luaL_reg *func) +lua_newclass_full (lua_State *L, + const gchar *classname, + const gchar *static_name, + const struct luaL_reg *methods, + const struct luaL_reg *func) { lua_newclass (L, classname, methods); luaL_register (L, static_name, func); @@ -83,7 +89,7 @@ lua_newclass_full (lua_State *L, const gchar *classname, const gchar *static_nam gint lua_class_tostring (lua_State * L) { - gchar buf[32]; + gchar buf[32]; if (!lua_getmetatable (L, 1)) { goto error; @@ -107,7 +113,7 @@ lua_class_tostring (lua_State * L) return 1; - error: +error: lua_pushstring (L, "invalid object passed to 'lua_common.c:__tostring'"); lua_error (L); return 1; @@ -157,31 +163,48 @@ lua_get_table_index_str (lua_State *L, const gchar *index) static void lua_common_log (GLogLevelFlags level, lua_State *L, const gchar *msg) { - lua_Debug d; - gchar func_buf[128], *p; + lua_Debug d; + gchar func_buf[128], *p; if (lua_getstack (L, 1, &d) == 1) { - (void)lua_getinfo(L, "Sl", &d); + (void)lua_getinfo (L, "Sl", &d); if ((p = strrchr (d.short_src, '/')) == NULL) { p = d.short_src; } else { - p ++; + p++; } - rspamd_snprintf (func_buf, sizeof (func_buf), "%s:%d", p, d.currentline); + rspamd_snprintf (func_buf, sizeof (func_buf), "%s:%d", p, + d.currentline); if (level == G_LOG_LEVEL_DEBUG) { - rspamd_conditional_debug (rspamd_main->logger, NULL, func_buf, "%s", msg); + rspamd_conditional_debug (rspamd_main->logger, + NULL, + func_buf, + "%s", + msg); } else { - rspamd_common_log_function (rspamd_main->logger, level, func_buf, "%s", msg); + rspamd_common_log_function (rspamd_main->logger, + level, + func_buf, + "%s", + msg); } } else { if (level == G_LOG_LEVEL_DEBUG) { - rspamd_conditional_debug (rspamd_main->logger, NULL, __FUNCTION__, "%s", msg); + rspamd_conditional_debug (rspamd_main->logger, + NULL, + __FUNCTION__, + "%s", + msg); } else { - rspamd_common_log_function (rspamd_main->logger, level, __FUNCTION__, "%s", msg); + rspamd_common_log_function (rspamd_main->logger, + level, + __FUNCTION__, + "%s", + msg); } } } @@ -190,7 +213,7 @@ lua_common_log (GLogLevelFlags level, lua_State *L, const gchar *msg) static gint lua_logger_err (lua_State * L) { - const gchar *msg; + const gchar *msg; msg = luaL_checkstring (L, 1); lua_common_log (G_LOG_LEVEL_CRITICAL, L, msg); return 1; @@ -199,7 +222,7 @@ lua_logger_err (lua_State * L) static gint lua_logger_warn (lua_State * L) { - const gchar *msg; + const gchar *msg; msg = luaL_checkstring (L, 1); lua_common_log (G_LOG_LEVEL_WARNING, L, msg); return 1; @@ -208,7 +231,7 @@ lua_logger_warn (lua_State * L) static gint lua_logger_info (lua_State * L) { - const gchar *msg; + const gchar *msg; msg = luaL_checkstring (L, 1); lua_common_log (G_LOG_LEVEL_INFO, L, msg); return 1; @@ -217,7 +240,7 @@ lua_logger_info (lua_State * L) static gint lua_logger_debug (lua_State * L) { - const gchar *msg; + const gchar *msg; msg = luaL_checkstring (L, 1); lua_common_log (G_LOG_LEVEL_DEBUG, L, msg); return 1; @@ -250,11 +273,11 @@ luaopen_logger (lua_State * L) static void lua_add_actions_global (lua_State *L) { - gint i; + gint i; lua_newtable (L); - for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i ++) { + for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { lua_pushstring (L, str_action_metric (i)); lua_pushinteger (L, i); lua_settable (L, -3); @@ -266,7 +289,7 @@ lua_add_actions_global (lua_State *L) lua_State * init_lua (struct rspamd_config *cfg) { - lua_State *L; + lua_State *L; L = luaL_newstate (); luaL_openlibs (L); @@ -306,10 +329,10 @@ init_lua (struct rspamd_config *cfg) /** * Initialize new locked lua_State structure */ -struct lua_locked_state* +struct lua_locked_state * init_lua_locked (struct rspamd_config *cfg) { - struct lua_locked_state *new; + struct lua_locked_state *new; new = g_slice_alloc (sizeof (struct lua_locked_state)); new->L = init_lua (cfg); @@ -337,18 +360,19 @@ free_lua_locked (struct lua_locked_state *st) gboolean init_lua_filters (struct rspamd_config *cfg) { - struct rspamd_config **pcfg; - GList *cur, *tmp; - struct script_module *module; - struct rspamd_statfile_config *st; - lua_State *L = cfg->lua_state; + struct rspamd_config **pcfg; + GList *cur, *tmp; + struct script_module *module; + struct rspamd_statfile_config *st; + lua_State *L = cfg->lua_state; cur = g_list_first (cfg->script_modules); while (cur) { module = cur->data; if (module->path) { if (luaL_loadfile (L, module->path) != 0) { - msg_info ("load of %s failed: %s", module->path, lua_tostring (L, -1)); + msg_info ("load of %s failed: %s", module->path, + lua_tostring (L, -1)); cur = g_list_next (cur); return FALSE; } @@ -361,12 +385,15 @@ init_lua_filters (struct rspamd_config *cfg) /* do the call (0 arguments, N result) */ if (lua_pcall (L, 0, LUA_MULTRET, 0) != 0) { - msg_info ("init of %s failed: %s", module->path, lua_tostring (L, -1)); + msg_info ("init of %s failed: %s", module->path, + lua_tostring (L, -1)); return FALSE; } if (lua_gettop (L) != 0) { if (lua_tonumber (L, -1) == -1) { - msg_info ("%s returned -1 that indicates configuration error", module->path); + msg_info ( + "%s returned -1 that indicates configuration error", + module->path); return FALSE; } lua_pop (L, lua_gettop (L)); @@ -374,24 +401,24 @@ init_lua_filters (struct rspamd_config *cfg) } cur = g_list_next (cur); } - /* Init statfiles normalizers */ - cur = g_list_first (cfg->statfiles); - while (cur) { - st = cur->data; - if (st->normalizer == lua_normalizer_func) { - tmp = st->normalizer_data; - if (tmp && (tmp = g_list_next (tmp))) { - if (tmp->data) { - /* Code must be loaded from data */ - if (luaL_loadstring (L, tmp->data) != 0) { - msg_info ("cannot load normalizer code %s", tmp->data); - return FALSE; - } - } - } - } - cur = g_list_next (cur); - } + /* Init statfiles normalizers */ + cur = g_list_first (cfg->statfiles); + while (cur) { + st = cur->data; + if (st->normalizer == lua_normalizer_func) { + tmp = st->normalizer_data; + if (tmp && (tmp = g_list_next (tmp))) { + if (tmp->data) { + /* Code must be loaded from data */ + if (luaL_loadstring (L, tmp->data) != 0) { + msg_info ("cannot load normalizer code %s", tmp->data); + return FALSE; + } + } + } + } + cur = g_list_next (cur); + } /* Assign state */ cfg->lua_state = L; @@ -403,9 +430,9 @@ init_lua_filters (struct rspamd_config *cfg) gint lua_call_filter (const gchar *function, struct rspamd_task *task) { - gint result; - struct rspamd_task **ptask; - lua_State *L = task->cfg->lua_state; + gint result; + struct rspamd_task **ptask; + lua_State *L = task->cfg->lua_state; lua_getglobal (L, function); ptask = lua_newuserdata (L, sizeof (struct rspamd_task *)); @@ -421,17 +448,20 @@ lua_call_filter (const gchar *function, struct rspamd_task *task) msg_info ("function %s must return a number", function); } result = lua_tonumber (L, -1); - lua_pop (L, 1); /* pop returned value */ + lua_pop (L, 1); /* pop returned value */ return result; } gint -lua_call_chain_filter (const gchar *function, struct rspamd_task *task, gint *marks, guint number) +lua_call_chain_filter (const gchar *function, + struct rspamd_task *task, + gint *marks, + guint number) { - gint result; - guint i; - lua_State *L = task->cfg->lua_state; + gint result; + guint i; + lua_State *L = task->cfg->lua_state; lua_getglobal (L, function); @@ -447,46 +477,46 @@ lua_call_chain_filter (const gchar *function, struct rspamd_task *task, gint *ma msg_info ("function %s must return a number", function); } result = lua_tonumber (L, -1); - lua_pop (L, 1); /* pop returned value */ + lua_pop (L, 1); /* pop returned value */ return result; } /* Call custom lua function in rspamd expression */ -gboolean +gboolean lua_call_expression_func (gpointer lua_data, - struct rspamd_task *task, GList *args, gboolean *res) + 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_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 *)); 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; + 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 ++; + nargs++; cur = g_list_next (cur); } @@ -494,7 +524,7 @@ lua_call_expression_func (gpointer lua_data, msg_info ("call to lua function failed: %s", lua_tostring (L, -1)); return FALSE; } - pop ++; + pop++; if (!lua_isboolean (L, -1)) { lua_pop (L, pop); @@ -512,18 +542,19 @@ lua_call_expression_func (gpointer lua_data, * LUA custom consolidation function */ struct consolidation_callback_data { - struct rspamd_task *task; - double score; - const gchar *func; + struct rspamd_task *task; + double score; + const gchar *func; }; static void lua_consolidation_callback (gpointer key, gpointer value, gpointer arg) { - double res; - struct symbol *s = (struct symbol *)value; - struct consolidation_callback_data *data = (struct consolidation_callback_data *)arg; - lua_State *L = data->task->cfg->lua_state; + double res; + struct symbol *s = (struct symbol *)value; + struct consolidation_callback_data *data = + (struct consolidation_callback_data *)arg; + lua_State *L = data->task->cfg->lua_state; lua_getglobal (L, data->func); @@ -538,15 +569,17 @@ lua_consolidation_callback (gpointer key, gpointer value, gpointer arg) msg_info ("function %s must return a number", data->func); } res = lua_tonumber (L, -1); - lua_pop (L, 1); /* pop returned value */ + lua_pop (L, 1); /* pop returned value */ data->score += res; } double -lua_consolidation_func (struct rspamd_task *task, const gchar *metric_name, const gchar *function_name) +lua_consolidation_func (struct rspamd_task *task, + const gchar *metric_name, + const gchar *function_name) { - struct metric_result *metric_res; - double res = 0.; + struct metric_result *metric_res; + double res = 0.; struct consolidation_callback_data data = { task, 0, function_name }; if (function_name == NULL) { @@ -558,28 +591,29 @@ lua_consolidation_func (struct rspamd_task *task, const gchar *metric_name, cons return res; } - g_hash_table_foreach (metric_res->symbols, lua_consolidation_callback, &data); + g_hash_table_foreach (metric_res->symbols, lua_consolidation_callback, + &data); return data.score; } -double +double lua_normalizer_func (struct rspamd_config *cfg, long double score, void *params) { - GList *p = params; - long double res = score; - lua_State *L = cfg->lua_state; - - /* Call specified function and put input score on stack */ - if (!p->data) { - msg_info ("bad function name while calling normalizer"); - return score; - } + GList *p = params; + long double res = score; + lua_State *L = cfg->lua_state; + + /* Call specified function and put input score on stack */ + if (!p->data) { + msg_info ("bad function name while calling normalizer"); + return score; + } - lua_getglobal (L, p->data); - lua_pushnumber (L, score); + lua_getglobal (L, p->data); + lua_pushnumber (L, score); - if (lua_pcall (L, 1, 1, 0) != 0) { + if (lua_pcall (L, 1, 1, 0) != 0) { msg_info ("call to %s failed", p->data); } @@ -590,16 +624,16 @@ lua_normalizer_func (struct rspamd_config *cfg, long double score, void *params) res = lua_tonumber (L, -1); lua_pop (L, 1); - return res; + return res; } void lua_dumpstack (lua_State *L) { - gint i, t, r = 0; - gint top = lua_gettop (L); - gchar buf[BUFSIZ]; + gint i, t, r = 0; + gint top = lua_gettop (L); + gchar buf[BUFSIZ]; r += rspamd_snprintf (buf + r, sizeof (buf) - r, "lua stack: "); for (i = 1; i <= top; i++) { /* repeat for each level */ @@ -607,19 +641,29 @@ lua_dumpstack (lua_State *L) switch (t) { case LUA_TSTRING: /* strings */ - r += rspamd_snprintf (buf + r, sizeof (buf) - r, "str: %s", lua_tostring(L, i)); + r += rspamd_snprintf (buf + r, + sizeof (buf) - r, + "str: %s", + lua_tostring (L, i)); break; case LUA_TBOOLEAN: /* booleans */ - r += rspamd_snprintf (buf + r, sizeof (buf) - r,lua_toboolean (L, i) ? "bool: true" : "bool: false"); + r += rspamd_snprintf (buf + r, sizeof (buf) - r,lua_toboolean (L, + i) ? "bool: true" : "bool: false"); break; case LUA_TNUMBER: /* numbers */ - r += rspamd_snprintf (buf + r, sizeof (buf) - r, "number: %.2f", lua_tonumber (L, i)); + r += rspamd_snprintf (buf + r, + sizeof (buf) - r, + "number: %.2f", + lua_tonumber (L, i)); break; default: /* other values */ - r += rspamd_snprintf (buf + r, sizeof (buf) - r, "type: %s", lua_typename (L, t)); + r += rspamd_snprintf (buf + r, + sizeof (buf) - r, + "type: %s", + lua_typename (L, t)); break; } @@ -633,7 +677,7 @@ lua_dumpstack (lua_State *L) gpointer lua_check_class (lua_State *L, gint index, const gchar *name) { - gpointer p; + gpointer p; if (lua_type (L, index) == LUA_TUSERDATA) { p = lua_touserdata (L, index); @@ -655,6 +699,6 @@ int rspamd_lua_typerror (lua_State *L, int narg, const char *tname) { const char *msg = lua_pushfstring (L, "%s expected, got %s", tname, - luaL_typename(L, narg)); + luaL_typename (L, narg)); return luaL_argerror (L, narg, msg); } |