]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Add worker scripts functionality
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 5 Apr 2016 12:03:25 +0000 (13:03 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 5 Apr 2016 12:03:25 +0000 (13:03 +0100)
src/libserver/cfg_file.h
src/lua/lua_config.c

index 8efdce092a3fe42b13ad02a3f7f355c2581f4f14..2dd6addcb48f9a18c901c999cb57c8eadc75413d 100644 (file)
@@ -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 {
index ea53c53ae2a449241944afe41b693a6e8cf85330..6e96c900cb7a28cbb2ed0ffeeea848a4cee2fe7f 100644 (file)
@@ -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)
 {