]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Rework groups scores definitions
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 18 Feb 2019 15:42:43 +0000 (15:42 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 18 Feb 2019 15:42:43 +0000 (15:42 +0000)
src/libserver/cfg_file.h
src/libserver/cfg_rcl.c
src/libserver/cfg_utils.c

index c7475c327033016515f6589e50ed8bb8d8d92934..c102ef570ce3a4663ce4c8f1b98f5d9eb2f6a874 100644 (file)
@@ -98,6 +98,13 @@ enum lua_var_type {
        LUA_VAR_UNKNOWN
 };
 
+enum rspamd_symbol_group_flags {
+       RSPAMD_SYMBOL_GROUP_NORMAL = 0,
+       RSPAMD_SYMBOL_GROUP_DISABLED = (1 << 0),
+       RSPAMD_SYMBOL_GROUP_ONE_SHOT = (1 << 1),
+       RSPAMD_SYMBOL_GROUP_UNGROUPED = (1 << 2),
+};
+
 /**
  * Symbols group
  */
@@ -106,13 +113,15 @@ struct rspamd_symbols_group {
        gchar *name;
        GHashTable *symbols;
        gdouble max_score;
-       gboolean disabled;
-       gboolean one_shot;
+       enum rspamd_symbol_group_flags flags;
 };
 
-#define RSPAMD_SYMBOL_FLAG_IGNORE (1 << 1)
-#define RSPAMD_SYMBOL_FLAG_ONEPARAM (1 << 2)
-#define RSPAMD_SYMBOL_FLAG_UNGROUPPED (1 << 3)
+enum rspamd_symbol_flags {
+       RSPAMD_SYMBOL_FLAG_NORMAL = 0,
+       RSPAMD_SYMBOL_FLAG_IGNORE = (1 << 1),
+       RSPAMD_SYMBOL_FLAG_ONEPARAM = (1 << 2),
+       RSPAMD_SYMBOL_FLAG_UNGROUPPED = (1 << 3),
+};
 
 /**
  * Symbol config definition
@@ -125,7 +134,7 @@ struct rspamd_symbol {
        guint priority;
        struct rspamd_symbols_group *gr; /* Main group */
        GPtrArray *groups; /* Other groups */
-       guint flags;
+       enum rspamd_symbol_flags flags;
        gint nshots;
 };
 
index 21a89c06e76cef5f7df8351c7e33887f37687c0c..59656392d93013ac7bde44499509de2bd791b282 100644 (file)
@@ -326,7 +326,7 @@ rspamd_rcl_group_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
 {
        struct rspamd_config *cfg = ud;
        struct rspamd_symbols_group *gr;
-       const ucl_object_t *val;
+       const ucl_object_t *val, *elt;
        struct rspamd_rcl_section *subsection;
        struct rspamd_rcl_symbol_data sd;
 
@@ -343,6 +343,51 @@ rspamd_rcl_group_handler (rspamd_mempool_t *pool, const ucl_object_t *obj,
                return FALSE;
        }
 
+       if ((elt = ucl_object_lookup (obj, "one_shot")) != NULL) {
+               if (ucl_object_type (elt) != UCL_BOOLEAN) {
+                       g_set_error (err,
+                                       CFG_RCL_ERROR,
+                                       EINVAL,
+                                       "one_shot attribute is not boolean for symbol: '%s'",
+                                       key);
+
+                       return FALSE;
+               }
+               if (ucl_object_toboolean (elt)) {
+                       gr->flags |= RSPAMD_SYMBOL_GROUP_ONE_SHOT;
+               }
+       }
+
+       if ((elt = ucl_object_lookup (obj, "disabled")) != NULL) {
+               if (ucl_object_type (elt) != UCL_BOOLEAN) {
+                       g_set_error (err,
+                                       CFG_RCL_ERROR,
+                                       EINVAL,
+                                       "disabled attribute is not boolean for symbol: '%s'",
+                                       key);
+
+                       return FALSE;
+               }
+               if (ucl_object_toboolean (elt)) {
+                       gr->flags |= RSPAMD_SYMBOL_GROUP_DISABLED;
+               }
+       }
+
+       if ((elt = ucl_object_lookup (obj, "enabled")) != NULL) {
+               if (ucl_object_type (elt) != UCL_BOOLEAN) {
+                       g_set_error (err,
+                                       CFG_RCL_ERROR,
+                                       EINVAL,
+                                       "enabled attribute is not boolean for symbol: '%s'",
+                                       key);
+
+                       return FALSE;
+               }
+               if (!ucl_object_toboolean (elt)) {
+                       gr->flags |= RSPAMD_SYMBOL_GROUP_DISABLED;
+               }
+       }
+
        sd.gr = gr;
        sd.cfg = cfg;
 
@@ -1316,7 +1361,7 @@ rspamd_rcl_composite_handler (rspamd_mempool_t *pool,
                rspamd_config_add_symbol (cfg, composite_name, score,
                                description, group,
                                0,
-                               ucl_object_get_priority (obj) + 1,
+                               ucl_object_get_priority (obj), /* No +1 as it is default... */
                                1);
 
                elt = ucl_object_lookup (obj, "groups");
@@ -2193,18 +2238,6 @@ rspamd_rcl_config_init (struct rspamd_config *cfg, GHashTable *skip_sections)
                                "Symbols configuration");
 
                /* Group part */
-               rspamd_rcl_add_default_handler (sub,
-                               "disabled",
-                               rspamd_rcl_parse_struct_boolean,
-                               G_STRUCT_OFFSET (struct rspamd_symbols_group, disabled),
-                               0,
-                               "Disable symbols group");
-               rspamd_rcl_add_default_handler (sub,
-                               "enabled",
-                               rspamd_rcl_parse_struct_boolean,
-                               G_STRUCT_OFFSET (struct rspamd_symbols_group, disabled),
-                               RSPAMD_CL_FLAG_BOOLEAN_INVERSE,
-                               "Enable or disable symbols group");
                rspamd_rcl_add_default_handler (sub,
                                "max_score",
                                rspamd_rcl_parse_struct_double,
index cc07c4007780c837d762e66ccccb45cd87b30c47..5c6fc6672ce81b6c7aed0412f8776b81ed66c9ac 100644 (file)
@@ -1039,6 +1039,10 @@ rspamd_config_new_group (struct rspamd_config *cfg, const gchar *name)
                        (rspamd_mempool_destruct_t)g_hash_table_unref, gr->symbols);
        gr->name = rspamd_mempool_strdup (cfg->cfg_pool, name);
 
+       if (strcmp (gr->name, "ungrouped") == 0) {
+               gr->flags |= RSPAMD_SYMBOL_GROUP_UNGROUPED;
+       }
+
        g_hash_table_insert (cfg->groups, gr->name, gr);
 
        return gr;
@@ -1684,10 +1688,7 @@ rspamd_config_add_symbol (struct rspamd_config *cfg,
                        /* We also check group information in this case */
                        if (group != NULL && sym_def->gr != NULL &&
                                        strcmp (group, sym_def->gr->name) != 0) {
-                               msg_debug_config ("move symbol %s from group %s to %s",
-                                               sym_def->gr->name, group);
 
-                               g_hash_table_remove (sym_def->gr->symbols, sym_def->name);
                                sym_group = g_hash_table_lookup (cfg->groups, group);
 
                                if (sym_group == NULL) {
@@ -1695,8 +1696,13 @@ rspamd_config_add_symbol (struct rspamd_config *cfg,
                                        sym_group = rspamd_config_new_group (cfg, group);
                                }
 
-                               sym_def->gr = sym_group;
-                               g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def);
+                               if (!(sym_group->flags & RSPAMD_SYMBOL_GROUP_UNGROUPED)) {
+                                       msg_debug_config ("move symbol %s from group %s to %s",
+                                                       sym_def->gr->name, group);
+                                       g_hash_table_remove (sym_def->gr->symbols, sym_def->name);
+                                       sym_def->gr = sym_group;
+                                       g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def);
+                               }
                        }
 
                        return TRUE;
@@ -1870,7 +1876,7 @@ rspamd_config_is_module_enabled (struct rspamd_config *cfg,
        gr = g_hash_table_lookup (cfg->groups, module_name);
 
        if (gr) {
-               if (gr->disabled) {
+               if (gr->flags & RSPAMD_SYMBOL_GROUP_DISABLED) {
                        rspamd_plugins_table_push_elt (L,
                                        "disabled_explicitly", module_name);
                        msg_info_config ("%s module %s is disabled in the configuration as "