]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Rework initialization order to handle C modules
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 3 Dec 2017 12:48:04 +0000 (12:48 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 3 Dec 2017 16:55:30 +0000 (16:55 +0000)
src/libserver/cfg_utils.c
src/lua/lua_common.c
src/lua/lua_common.h

index 597d661ae799d03c80a6595bb82e99470f1734fd..25ef943b9fcedae18924f15ee809c00958ab8ffc 100644 (file)
@@ -1522,7 +1522,7 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg,
        const ucl_object_t *conf, *enabled;
        GList *cur;
        struct rspamd_symbols_group *gr;
-
+       lua_State *L = cfg->lua_state;
 
        if (g_hash_table_lookup (cfg->c_modules, module_name)) {
                is_c = TRUE;
@@ -1530,6 +1530,10 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg,
 
        if (g_hash_table_lookup (cfg->explicit_modules, module_name) != NULL) {
                /* Always load module */
+               rspamd_table_push_global_elt (L,
+                               rspamd_modules_state_global,
+                               "enabled", module_name);
+
                return TRUE;
        }
 
@@ -1550,6 +1554,9 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg,
                if (!found) {
                        msg_info_config ("internal module %s is disable in `filters` line",
                                        module_name);
+                       rspamd_table_push_global_elt (L,
+                                       rspamd_modules_state_global,
+                                       "disabled_explicitly", module_name);
 
                        return FALSE;
                }
@@ -1558,6 +1565,10 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg,
        conf = ucl_object_lookup (cfg->rcl_obj, module_name);
 
        if (conf == NULL) {
+               rspamd_table_push_global_elt (L,
+                               rspamd_modules_state_global,
+                               "disabled_unconfigured", module_name);
+
                msg_info_config ("%s module %s is enabled but has not been configured",
                                is_c ? "internal" : "lua", module_name);
 
@@ -1571,6 +1582,10 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg,
 
                if (enabled && ucl_object_type (enabled) == UCL_BOOLEAN) {
                        if (!ucl_object_toboolean (enabled)) {
+                               rspamd_table_push_global_elt (L,
+                                               rspamd_modules_state_global,
+                                               "disabled_explicitly", module_name);
+
                                msg_info_config ("%s module %s is disabled in the configuration",
                                                is_c ? "internal" : "lua", module_name);
                                return FALSE;
@@ -1583,9 +1598,13 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg,
 
        if (gr) {
                if (gr->disabled) {
+                       rspamd_table_push_global_elt (L,
+                                       rspamd_modules_state_global,
+                                       "disabled_explicitly", module_name);
                        msg_info_config ("%s module %s is disabled in the configuration as "
                                        "its group has been disabled",
                                        is_c ? "internal" : "lua", module_name);
+
                        return FALSE;
                }
        }
index c3263bd7225d7aad1400d71c0f22b0837adc90d6..f6fc36aa698f7ea1defb2f521550a420886c8667 100644 (file)
@@ -49,8 +49,6 @@ const luaL_reg worker_reg[] = {
        {NULL, NULL}
 };
 
-static const char rspamd_modules_state_global[] = "rspamd_plugins_state";
-
 static GQuark
 lua_error_quark (void)
 {
@@ -410,6 +408,32 @@ rspamd_lua_init ()
        lua_pcall (L, 1, 0, 0);
        lua_pop (L, 1); /* math table */
 
+       /* Modules state */
+       lua_newtable (L);
+       /*
+        * rspamd_plugins_state = {
+        *   enabled = {},
+        *   disabled_unconfigured = {},
+        *   disabled_redis = {},
+        *   disabled_explicitly = {},
+        *   disabled_failed = {},
+        * }
+        */
+#define ADD_TABLE(name) do { \
+       lua_pushstring (L, #name); \
+       lua_newtable (L); \
+       lua_settable (L, -3); \
+} while (0)
+
+       ADD_TABLE (enabled);
+       ADD_TABLE (disabled_unconfigured);
+       ADD_TABLE (disabled_redis);
+       ADD_TABLE (disabled_explicitly);
+       ADD_TABLE (disabled_failed);
+
+#undef ADD_TABLE
+       lua_setglobal (L, rspamd_modules_state_global);
+
        return L;
 }
 
@@ -428,7 +452,6 @@ rspamd_init_lua_locked (struct rspamd_config *cfg)
        return new;
 }
 
-
 /**
  * Free locked state structure
  */
@@ -444,7 +467,7 @@ rspamd_free_lua_locked (struct lua_locked_state *st)
        g_free (st);
 }
 
-static void
+void
 rspamd_table_push_global_elt (lua_State *L, const gchar *global_name,
                const gchar *field_name, const gchar *new_elt)
 {
@@ -469,31 +492,6 @@ rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load)
        GString *tb;
        gint err_idx;
 
-       lua_newtable (L);
-       /*
-        * rspamd_plugins_state = {
-        *   enabled = {},
-        *   disabled_unconfigured = {},
-        *   disabled_redis = {},
-        *   disabled_explicitly = {},
-        *   disabled_failed = {},
-        * }
-        */
-#define ADD_TABLE(name) do { \
-       lua_pushstring (L, #name); \
-       lua_newtable (L); \
-       lua_settable (L, -3); \
-} while (0)
-
-       ADD_TABLE (enabled);
-       ADD_TABLE (disabled_unconfigured);
-       ADD_TABLE (disabled_redis);
-       ADD_TABLE (disabled_explicitly);
-       ADD_TABLE (disabled_failed);
-
-#undef ADD_TABLE
-       lua_setglobal (L, rspamd_modules_state_global);
-
        cur = g_list_first (cfg->script_modules);
 
        while (cur) {
@@ -502,9 +500,6 @@ rspamd_init_lua_filters (struct rspamd_config *cfg, gboolean force_load)
                if (module->path) {
                        if (!force_load) {
                                if (!rspamd_config_is_module_enabled (cfg, module->name)) {
-                                       rspamd_table_push_global_elt (L,
-                                                       rspamd_modules_state_global,
-                                                       "disabled_explicitly", module->name);
                                        cur = g_list_next (cur);
                                        continue;
                                }
index 9dbd8782bdb875c822403581defea8fa552fcd21..1bc1e0bdf37f1566eb02ca43bc572fccbe8b8144 100644 (file)
@@ -48,6 +48,7 @@ luaL_register (lua_State *L, const gchar *name, const struct luaL_reg *methods)
 #define LUA_INTERFACE_DEF(class, name) { # name, lua_ ## class ## _ ## name }
 
 extern const luaL_reg null_reg[];
+static const char rspamd_modules_state_global[] = "rspamd_plugins_state";
 
 #define RSPAMD_LUA_API_VERSION 12
 
@@ -159,6 +160,17 @@ gpointer rspamd_lua_check_class (lua_State *L, gint index, const gchar *name);
  */
 lua_State *rspamd_lua_init (void);
 
+
+/**
+ * Sets field in a global variable
+ * @param L
+ * @param global_name
+ * @param field_name
+ * @param new_elt
+ */
+void
+rspamd_table_push_global_elt (lua_State *L, const gchar *global_name,
+               const gchar *field_name, const gchar *new_elt);
 /**
  * Load and initialize lua plugins
  */