]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Rework logic of actions setting
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 22 Mar 2018 10:59:31 +0000 (10:59 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 22 Mar 2018 10:59:31 +0000 (10:59 +0000)
src/libmime/filter.c
src/libmime/filter.h
src/libmime/message.c
src/libserver/composites.c
src/libserver/task.c
src/lua/lua_task.c
src/plugins/lua/greylist.lua
src/plugins/lua/metadata_exporter.lua

index 5fd202f2a467a742a7f200e88c65d55ad0ec2df7..1efe898ce873f67a4bd1c29aa9df071de98a4ec4 100644 (file)
@@ -50,14 +50,11 @@ rspamd_create_metric_result (struct rspamd_task *task)
                        metric_res->sym_groups);
        metric_res->grow_factor = 0;
        metric_res->score = 0;
-       metric_res->changes = 0;
 
        for (i = 0; i < METRIC_ACTION_MAX; i++) {
                metric_res->actions_limits[i] = task->cfg->actions[i].score;
        }
 
-       metric_res->action = METRIC_ACTION_MAX;
-
        return metric_res;
 }
 
@@ -244,10 +241,9 @@ insert_metric_result (struct rspamd_task *task,
        }
 
        msg_debug_task ("symbol %s, score %.2f, factor: %f",
-               symbol,
-               s->score,
-               w);
-       metric_res->changes ++;
+                       symbol,
+                       s->score,
+                       w);
 
        return s;
 }
@@ -355,14 +351,6 @@ rspamd_check_action_metric (struct rspamd_task *task, struct rspamd_metric_resul
        int i;
        gboolean set_action = FALSE;
 
-       if (task->processed_stages | (RSPAMD_TASK_STAGE_DONE|RSPAMD_TASK_STAGE_IDEMPOTENT)) {
-               if (mres->action != METRIC_ACTION_MAX) {
-                       return mres->action;
-               }
-
-               set_action = TRUE;
-       }
-
        /* We are not certain about the results during processing */
        if (task->pre_result.action == METRIC_ACTION_MAX) {
                for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) {
@@ -413,10 +401,6 @@ rspamd_check_action_metric (struct rspamd_task *task, struct rspamd_metric_resul
        }
 
        if (selected_action) {
-               if (set_action) {
-                       mres->action = selected_action->action;
-               }
-
                return selected_action->action;
        }
 
index 53d47a7695a608bb34ff4fe56a1cbef2e3da41f3..3914413924b5f4644b8add37eeee6cc4ce0b6a1e 100644 (file)
@@ -40,8 +40,6 @@ struct rspamd_metric_result {
        GHashTable *symbols;                            /**< symbols of metric                                          */
        GHashTable *sym_groups;                                                 /**< groups of symbols                                          */
        gdouble actions_limits[METRIC_ACTION_MAX];              /**< set of actions for this metric                     */
-       guint changes;
-       enum rspamd_action_type action;               /**< the current action                                           */
 };
 
 /**
index 5e1118fc230038e67be7fd73dd6abe29dac32912..756cd9b478aa6ebcd82a918e4d4955be07aa5145 100644 (file)
@@ -801,8 +801,6 @@ rspamd_message_process_text_part (struct rspamd_task *task,
                        else {
                                mres->score = mres->actions_limits[act];
                        }
-
-                       mres->action = act;
                }
 
                task->result = mres;
index 6b6776f47b9780f469392645812fa059a705fe37..a90b5c032c7298179bdc8904c533798c0d9d8846 100644 (file)
@@ -433,8 +433,6 @@ composites_remove_symbols (gpointer key, gpointer value, gpointer data)
                        cd->metric_res->score -= rd->ms->score;
                        rd->ms->score = 0.0;
                }
-
-               cd->metric_res->changes ++;
        }
 }
 
index ce21523e7d70828f8f14f2c9b1007fa37a5c7592..71e38ed4c721c2f1497f8077a0056a449f299d36 100644 (file)
@@ -1033,10 +1033,11 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
        rspamd_fstring_t *symbuf;
        struct rspamd_symbol_result *sym;
        GPtrArray *sorted_symbols;
+       enum rspamd_action_type act;
        guint i, j;
 
        mres = task->result;
-       rspamd_check_action_metric (task, mres);
+       act = rspamd_check_action_metric (task, mres);
 
        if (mres != NULL) {
                switch (lf->type) {
@@ -1044,7 +1045,7 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
                        if (RSPAMD_TASK_IS_SKIPPED (task)) {
                                res.begin = "S";
                        }
-                       else if (mres->action == METRIC_ACTION_REJECT) {
+                       else if (act == METRIC_ACTION_REJECT) {
                                res.begin = "T";
                        }
                        else {
@@ -1054,7 +1055,7 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
                        res.len = 1;
                        break;
                case RSPAMD_LOG_ACTION:
-                       res.begin = rspamd_action_to_str (mres->action);
+                       res.begin = rspamd_action_to_str (act);
                        res.len = strlen (res.begin);
                        break;
                case RSPAMD_LOG_SCORES:
index b0c7c8fd50c6d13c862bc77ddfa5d89558649eaf..64373708663120ab0ca24b2f2976423a13193bc1 100644 (file)
@@ -598,13 +598,6 @@ LUA_FUNCTION_DEF (task, get_metric_action);
  * @param {number} score the current score of the metric
  */
 LUA_FUNCTION_DEF (task, set_metric_score);
-/***
- * @method task:set_metric_action(name, action)
- * Set the current action of metric `name`. Should be used in post-filters only.
- * @param {string} name name of a metric
- * @param {string} action name to set
- */
-LUA_FUNCTION_DEF (task, set_metric_action);
 /***
  * @method task:set_metric_subject(subject)
  * Set the subject in the default metric
@@ -841,6 +834,15 @@ LUA_FUNCTION_DEF (task, get_protocol_reply);
  */
 LUA_FUNCTION_DEF (task, headers_foreach);
 
+/***
+ * @method task:disable_action(action)
+ * Disables some action for this task (e.g. 'greylist')
+ *
+ * @param {string} action action to disable
+ * @return {boolean} true if an action was enabled and is disabled after the method call
+ */
+LUA_FUNCTION_DEF (task, disable_action);
+
 static const struct luaL_reg tasklib_f[] = {
        {NULL, NULL}
 };
@@ -914,7 +916,6 @@ static const struct luaL_reg tasklib_m[] = {
        LUA_INTERFACE_DEF (task, get_metric_score),
        LUA_INTERFACE_DEF (task, get_metric_action),
        LUA_INTERFACE_DEF (task, set_metric_score),
-       LUA_INTERFACE_DEF (task, set_metric_action),
        LUA_INTERFACE_DEF (task, set_metric_subject),
        LUA_INTERFACE_DEF (task, learn),
        LUA_INTERFACE_DEF (task, set_settings),
@@ -934,6 +935,7 @@ static const struct luaL_reg tasklib_m[] = {
        LUA_INTERFACE_DEF (task, store_in_file),
        LUA_INTERFACE_DEF (task, get_protocol_reply),
        LUA_INTERFACE_DEF (task, headers_foreach),
+       LUA_INTERFACE_DEF (task, disable_action),
        {"__tostring", rspamd_lua_class_tostring},
        {NULL, NULL}
 };
@@ -1344,7 +1346,6 @@ lua_task_set_pre_result (lua_State * L)
 {
        struct rspamd_task *task = lua_check_task (L, 1);
        struct rspamd_config *cfg;
-       struct rspamd_metric_result *mres;
        gchar *action_str;
        gint action = METRIC_ACTION_MAX;
 
@@ -1361,14 +1362,7 @@ lua_task_set_pre_result (lua_State * L)
                if (action < METRIC_ACTION_MAX && action >= METRIC_ACTION_REJECT) {
                        /* We also need to set the default metric to that result */
                        if (!task->result) {
-                               mres = rspamd_create_metric_result (task);
-                               if (mres != NULL) {
-                                       mres->score = cfg->actions[action].score;
-                                       mres->action = action;
-                               }
-                       }
-                       else {
-                               task->result->action = action;
+                               task->result = rspamd_create_metric_result (task);
                        }
 
                        task->pre_result.action = action;
@@ -4227,35 +4221,26 @@ lua_task_set_metric_score (lua_State *L)
 }
 
 static gint
-lua_task_set_metric_action (lua_State *L)
+lua_task_disable_action (lua_State *L)
 {
        struct rspamd_task *task = lua_check_task (L, 1);
-       const gchar *metric_name, *action_name;
+       const gchar *action_name;
        struct rspamd_metric_result *metric_res;
        gint action;
 
-       metric_name = luaL_checkstring (L, 2);
-
-       if (metric_name == NULL) {
-               metric_name = DEFAULT_METRIC;
-       }
-
-       action_name = luaL_checkstring (L, 3);
-
-       if (task && metric_name && action_name) {
-               if ((metric_res = task->result) != NULL) {
+       action_name = luaL_checkstring (L, 2);
 
-                       if (rspamd_action_from_str (action_name, &action)) {
-                               metric_res->action = action;
-                               lua_pushboolean (L, true);
-                       }
-                       else {
-                               lua_pushboolean (L, false);
-                       }
+       if (task && action_name && rspamd_action_from_str (action_name, &action)) {
+               if (!task->result) {
+                       task->result = rspamd_create_metric_result (task);
                }
-               else {
+               if (isnan (task->result->actions_limits[action])) {
                        lua_pushboolean (L, false);
                }
+               else {
+                       task->result->actions_limits[action] = NAN;
+                       lua_pushboolean (L, true);
+               }
        }
        else {
                return luaL_error (L, "invalid arguments");
index a555fcfc8a527ee2bb82b7afb70083ba9d269913..8de4f69c80f42ba27175f5a6c56c77f7f67d8d67 100644 (file)
@@ -256,7 +256,7 @@ local function greylist_set(task)
       if action == 'greylist' then
         -- We are going to accept message
         rspamd_logger.infox(task, 'Downgrading metric action from "greylist" to "no action"')
-        task:set_metric_action('default', 'no action')
+        task:disable_action('greylist')
       end
       return
     end
@@ -266,7 +266,7 @@ local function greylist_set(task)
     if action == 'greylist' then
       -- We are going to accept message
       rspamd_logger.infox(task, 'Downgrading metric action from "greylist" to "no action"')
-      task:set_metric_action('default', 'no action')
+      task:disable_action('greylist')
     end
     return
   end
@@ -276,7 +276,7 @@ local function greylist_set(task)
       if action == 'greylist' then
         -- We are going to accept message
         rspamd_logger.infox(task, 'Downgrading metric action from "greylist" to "no action"')
-        task:set_metric_action('default', 'no action')
+        task:disable_action('greylist')
       end
       return
     end
@@ -320,7 +320,7 @@ local function greylist_set(task)
     if action == 'greylist' then
       -- We are going to accept message
       rspamd_logger.infox(task, 'Downgrading metric action from "greylist" to "no action"')
-      task:set_metric_action('default', 'no action')
+      task:disable_action('greylist')
     end
 
     task:insert_result(settings['symbol'], 0.0, 'pass', is_whitelisted)
index 3569650abec9c416ec9809532afcb1e632e246f5..523b4251c1f762d8e31071be8d780c47abe02309 100644 (file)
@@ -229,7 +229,7 @@ local selectors = {
 local function maybe_defer(task, rule)
   if rule.defer then
     rspamd_logger.warnx(task, 'deferring message')
-    task:set_metric_action('default', 'soft reject')
+    task:set_pre_result('soft reject', 'deferred')
   end
 end