]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Fix lua symbols scores registration when config does not define scores
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 12 Mar 2020 12:27:40 +0000 (12:27 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 12 Mar 2020 12:28:19 +0000 (12:28 +0000)
src/libserver/cfg_file.h
src/libserver/cfg_utils.c
src/lua/lua_config.c

index b368464a0a493b1156fa8f92caffc71df50cd094..702325f627411f181f463173fdaac6fd8dad5348 100644 (file)
@@ -130,6 +130,7 @@ enum rspamd_symbol_flags {
        RSPAMD_SYMBOL_FLAG_ONEPARAM = (1 << 2),
        RSPAMD_SYMBOL_FLAG_UNGROUPPED = (1 << 3),
        RSPAMD_SYMBOL_FLAG_DISABLED = (1 << 4),
+       RSPAMD_SYMBOL_FLAG_UNSCORED = (1 << 5),
 };
 
 /**
index 9a308a8a4bfaf9306213080692d1a42ccd9832f6..8486a029fbcfc0235fc18ef293f47095f583b55d 100644 (file)
@@ -1625,6 +1625,7 @@ rspamd_config_new_symbol (struct rspamd_config *cfg, const gchar *symbol,
                score = 0.0;
                /* Also set priority to 0 to allow override by anything */
                sym_def->priority = 0;
+               flags |= RSPAMD_SYMBOL_FLAG_UNSCORED;
        }
        else {
                sym_def->priority = priority;
@@ -1725,7 +1726,8 @@ rspamd_config_add_symbol (struct rspamd_config *cfg,
                        }
                }
 
-               if (sym_def->priority > priority) {
+               if (sym_def->priority > priority &&
+                       (isnan(score) || !(sym_def->flags & RSPAMD_SYMBOL_FLAG_UNSCORED))) {
                        msg_debug_config ("symbol %s has been already registered with "
                                        "priority %ud, do not override (new priority: %ud)",
                                        symbol,
@@ -1759,6 +1761,7 @@ rspamd_config_add_symbol (struct rspamd_config *cfg,
                                *sym_def->weight_ptr = score;
                                sym_def->score = score;
                                sym_def->priority = priority;
+                               sym_def->flags &= ~RSPAMD_SYMBOL_FLAG_UNSCORED;
                        }
 
                        sym_def->flags = flags;
index 8afe8fbfea5a1e2c9b145f788310474e2ac0d777..fbaadd8bb98d03f5f0b7cfddd280ade20ef82b11 100644 (file)
@@ -2049,7 +2049,7 @@ lua_config_register_symbol (lua_State * L)
                        }
                        else {
                                rspamd_config_add_symbol (cfg, name,
-                                               0.0, description, group, flags,
+                                               NAN, description, group, flags,
                                                0, nshots);
                        }
 
@@ -2314,7 +2314,7 @@ lua_config_set_metric_symbol (lua_State * L)
        struct rspamd_config *cfg = lua_check_config (L, 1);
        const gchar *description = NULL,
                        *group = NULL, *name = NULL, *flags_str = NULL;
-       double weight;
+       double score;
        gboolean one_shot = FALSE, one_param = FALSE;
        GError *err = NULL;
        gdouble priority = 0.0;
@@ -2329,7 +2329,7 @@ lua_config_set_metric_symbol (lua_State * L)
                                        "*name=S;score=N;description=S;"
                                        "group=S;one_shot=B;one_param=B;priority=N;flags=S;"
                                        "nshots=I",
-                                       &name, &weight, &description,
+                                       &name, &score, &description,
                                        &group, &one_shot, &one_param,
                                        &priority, &flags_str, &nshots)) {
                                msg_err_config ("bad arguments: %e", err);
@@ -2340,7 +2340,7 @@ lua_config_set_metric_symbol (lua_State * L)
                }
                else {
                        name = luaL_checkstring (L, 2);
-                       weight = luaL_checknumber (L, 3);
+                       score = luaL_checknumber (L, 3);
 
                        if (lua_gettop (L) > 3 && lua_type (L, 4) == LUA_TSTRING) {
                                description = luaL_checkstring (L, 4);
@@ -2380,7 +2380,7 @@ lua_config_set_metric_symbol (lua_State * L)
                }
 
                rspamd_config_add_symbol (cfg, name,
-                               weight, description, group, flags, (guint) priority, nshots);
+                               score, description, group, flags, (guint) priority, nshots);
 
 
                if (lua_type (L, 2) == LUA_TTABLE) {
@@ -2786,7 +2786,8 @@ lua_config_newindex (lua_State *L)
                         * Now check if a symbol has not been registered in any metric and
                         * insert default value if applicable
                         */
-                       if (g_hash_table_lookup (cfg->symbols, name) == NULL) {
+                       struct rspamd_symbol *sym = g_hash_table_lookup (cfg->symbols, name);
+                       if (sym == NULL || (sym->flags & RSPAMD_SYMBOL_FLAG_UNSCORED)) {
                                nshots = cfg->default_max_shots;
 
                                lua_pushstring (L, "score");
@@ -2794,6 +2795,10 @@ lua_config_newindex (lua_State *L)
                                if (lua_type (L, -1) == LUA_TNUMBER) {
                                        score = lua_tonumber (L, -1);
 
+                                       if (sym) {
+                                               /* Reset unscored flag */
+                                               sym->flags &= ~RSPAMD_SYMBOL_FLAG_UNSCORED;
+                                       }
                                }
                                lua_pop (L, 1);
 
@@ -2843,7 +2848,7 @@ lua_config_newindex (lua_State *L)
                                        }
                                        else if (group) {
                                                /* Add with zero score */
-                                               rspamd_config_add_symbol (cfg, name, 0.0,
+                                               rspamd_config_add_symbol (cfg, name, NAN,
                                                                description, group, flags, 0, nshots);
                                        }