]> source.dussan.org Git - rspamd.git/commitdiff
[Rework] Isolate disable/enable logic for the configuration ucl objects
authorVsevolod Stakhov <vsevolod@rspamd.com>
Wed, 30 Mar 2022 19:31:21 +0000 (20:31 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Wed, 30 Mar 2022 19:31:21 +0000 (20:31 +0100)
src/libserver/cfg_file.h
src/libserver/cfg_utils.c

index 61999d54793c60ef20638f9ac6460affadc41382..6f88c6b636c318d235dc34a9c606402fc50355bb 100644 (file)
@@ -718,6 +718,14 @@ gboolean rspamd_config_maybe_disable_action (struct rspamd_config *cfg,
 gboolean rspamd_config_is_module_enabled (struct rspamd_config *cfg,
                                                                                  const gchar *module_name);
 
+/**
+ * Verifies enabled/disabled combination in the specified object
+ * @param obj
+ * @return TRUE if there is no explicit disable in the object found
+ */
+gboolean rspamd_config_is_enabled_from_ucl (struct rspamd_config *cfg,
+               const ucl_object_t *obj);
+
 /*
  * Get action from a string
  */
index 3a27978059f57333d52f6b38acd4ba250c01515c..49653c22c55ff93a3ec1d6b16ab224228e2f4599 100644 (file)
@@ -1778,13 +1778,74 @@ rspamd_config_add_symbol_group (struct rspamd_config *cfg,
        return FALSE;
 }
 
+gboolean
+rspamd_config_is_enabled_from_ucl (struct rspamd_config *cfg,
+               const ucl_object_t *obj)
+{
+       {
+               const ucl_object_t *enabled;
+
+               enabled = ucl_object_lookup(obj, "enabled");
+
+               if (enabled) {
+                       if (ucl_object_type(enabled) == UCL_BOOLEAN) {
+                               return ucl_object_toboolean(enabled);
+                       }
+                       else if (ucl_object_type(enabled) == UCL_STRING) {
+                               gchar ret;
+
+                               ret = rspamd_config_parse_flag(ucl_object_tostring(enabled), 0);
+
+                               if (ret == 0) {
+                                       return FALSE;
+                               }
+                               else if (ret == -1) {
+
+                                       msg_info_config ("wrong value for the `enabled` key");
+                                       return FALSE;
+                               }
+                               /* Default return is TRUE here */
+                       }
+               }
+       }
+
+       {
+               const ucl_object_t *disabled;
+
+               disabled = ucl_object_lookup(obj, "disabled");
+
+               if (disabled) {
+                       if (ucl_object_type(disabled) == UCL_BOOLEAN) {
+                               return !ucl_object_toboolean(disabled);
+                       }
+                       else if (ucl_object_type(disabled) == UCL_STRING) {
+                               gchar ret;
+
+                               ret = rspamd_config_parse_flag(ucl_object_tostring(disabled), 0);
+
+                               if (ret == 0) {
+                                       return TRUE;
+                               }
+                               else if (ret == -1) {
+
+                                       msg_info_config ("wrong value for the `disabled` key");
+                                       return FALSE;
+                               }
+
+                               return FALSE;
+                       }
+               }
+       }
+
+       return TRUE;
+}
 
 gboolean
 rspamd_config_is_module_enabled (struct rspamd_config *cfg,
                const gchar *module_name)
 {
-       gboolean is_c = FALSE;
-       const ucl_object_t *conf, *enabled;
+       gboolean is_c = FALSE, enabled;
+       const ucl_object_t *conf;
        GList *cur;
        struct rspamd_symbols_group *gr;
        lua_State *L = cfg->lua_state;
@@ -1843,45 +1904,16 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg,
                }
        }
        else {
-               enabled = ucl_object_lookup (conf, "enabled");
-
-               if (enabled) {
-                       if (ucl_object_type (enabled) == UCL_BOOLEAN) {
-                               if (!ucl_object_toboolean (enabled)) {
-                                       rspamd_plugins_table_push_elt (L,
-                                                       "disabled_explicitly", module_name);
-
-                                       msg_info_config (
-                                                       "%s module %s is disabled in the configuration",
-                                                       is_c ? "internal" : "lua", module_name);
-                                       return FALSE;
-                               }
-                       }
-                       else if (ucl_object_type (enabled) == UCL_STRING) {
-                               gint ret;
-
-                               ret = rspamd_config_parse_flag (ucl_object_tostring (enabled), 0);
-
-                               if (ret == 0) {
-                                       rspamd_plugins_table_push_elt (L,
-                                                       "disabled_explicitly", module_name);
+               enabled = rspamd_config_is_enabled_from_ucl (cfg, conf);
 
-                                       msg_info_config (
-                                                       "%s module %s is disabled in the configuration",
-                                                       is_c ? "internal" : "lua", module_name);
-                                       return FALSE;
-                               }
-                               else if (ret == -1) {
-                                       rspamd_plugins_table_push_elt (L,
-                                                       "disabled_failed", module_name);
+               if (!enabled) {
+                       rspamd_plugins_table_push_elt (L,
+                                       "disabled_explicitly", module_name);
 
-                                       msg_info_config (
-                                                       "%s module %s has wrong enabled flag (%s) in the configuration",
-                                                       is_c ? "internal" : "lua", module_name,
-                                                       ucl_object_tostring (enabled));
-                                       return FALSE;
-                               }
-                       }
+                       msg_info_config (
+                                       "%s module %s is disabled in the configuration",
+                                       is_c ? "internal" : "lua", module_name);
+                       return FALSE;
                }
        }