From: Vsevolod Stakhov Date: Tue, 5 Apr 2016 12:03:25 +0000 (+0100) Subject: [Feature] Add worker scripts functionality X-Git-Tag: 1.2.3~57 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=61a4994df8e7983682aec6f71e5f181653e7f448;p=rspamd.git [Feature] Add worker scripts functionality --- diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h index 8efdce092..2dd6addcb 100644 --- a/src/libserver/cfg_file.h +++ b/src/libserver/cfg_file.h @@ -164,6 +164,11 @@ struct rspamd_worker_bind_conf { struct rspamd_worker_bind_conf *next; }; +struct rspamd_worker_lua_script { + gint cbref; + struct rspamd_worker_lua_script *prev, *next; +}; + /** * Config params for rspamd worker */ @@ -179,7 +184,8 @@ struct rspamd_worker_conf { GQueue *active_workers; /**< linked list of spawned workers */ gboolean has_socket; /**< whether we should make listening socket in main process */ gpointer *ctx; /**< worker's context */ - ucl_object_t *options; /**< other worker's options */ + ucl_object_t *options; /**< other worker's options */ + struct rspamd_worker_lua_script *scripts; /**< registered lua scripts */ }; enum rspamd_log_format_type { diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index ea53c53ae..6e96c900c 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -387,6 +387,15 @@ LUA_FUNCTION_DEF (config, register_regexp); */ LUA_FUNCTION_DEF (config, replace_regexp); +/*** + * @method rspamd_config:register_worker_script(worker_type, script) + * Registers the following script for workers of a specified type. The exact type + * of script function depends on worker type + * @param {string} worker_type worker type (e.g. "normal") + * @param {function} script script for a worker + */ +LUA_FUNCTION_DEF (config, register_worker_script); + static const struct luaL_reg configlib_m[] = { LUA_INTERFACE_DEF (config, get_module_opt), LUA_INTERFACE_DEF (config, get_mempool), @@ -414,6 +423,7 @@ static const struct luaL_reg configlib_m[] = { LUA_INTERFACE_DEF (config, add_condition), LUA_INTERFACE_DEF (config, register_regexp), LUA_INTERFACE_DEF (config, replace_regexp), + LUA_INTERFACE_DEF (config, register_worker_script), {"__tostring", rspamd_lua_class_tostring}, {"__newindex", lua_config_newindex}, {NULL, NULL} @@ -1632,6 +1642,39 @@ lua_config_replace_regexp (lua_State *L) return 0; } +static gint +lua_config_register_worker_script (lua_State *L) +{ + struct rspamd_config *cfg = lua_check_config (L, 1); + const gchar *worker_type = luaL_checkstring (L, 2), *wtype; + struct rspamd_worker_conf *cf; + GList *cur; + struct rspamd_worker_lua_script *sc; + gboolean found = FALSE; + + if (cfg == NULL || worker_type == NULL || lua_type (L, 3) != LUA_TFUNCTION) { + return luaL_error (L, "invalid arguments"); + } + + for (cur = g_list_first (cfg->workers); cur != NULL; cur = g_list_next (cur)) { + cf = cur->data; + + wtype = g_quark_to_string (cf->type); + + if (g_ascii_strcasecmp (wtype, worker_type) == 0) { + sc = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*sc)); + lua_pushvalue (L, 3); + sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX); + DL_APPEND (cf->scripts, sc); + found = TRUE; + } + } + + lua_pushboolean (L, found); + + return 1; +} + void luaopen_config (lua_State * L) {