]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Simplify actions application by the settings
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 29 Mar 2016 10:31:59 +0000 (11:31 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 29 Mar 2016 10:31:59 +0000 (11:31 +0100)
src/libmime/filter.c
src/libmime/filter.h
src/libserver/protocol.c
src/libserver/roll_history.c
src/libstat/stat_process.c
src/lua/lua_task.c

index f5c8198214152c50ae761f68ddf1814aafe0fbe4..7ed6ea01080b7bfdf1334ab86e882e5e30caa71d 100644 (file)
@@ -344,62 +344,27 @@ rspamd_action_to_str (enum rspamd_metric_action action)
        return "unknown action";
 }
 
-static double
-get_specific_action_score (struct rspamd_task *task,
-               const ucl_object_t *metric,
-               struct metric_action *action)
-{
-       const ucl_object_t *act, *sact;
-       const gchar *act_name;
-       double score;
-
-       if (metric) {
-               act = ucl_object_lookup (metric, "actions");
-               if (act) {
-                       act_name = rspamd_action_to_str (action->action);
-                       sact = ucl_object_lookup (act, act_name);
-                       if (sact != NULL && ucl_object_todouble_safe (sact, &score)) {
-                               msg_debug_task ("found override score %.2f for action %s in settings",
-                                               score, act_name);
-                               return score;
-                       }
-               }
-       }
-
-       return action->score;
-}
-
-gint
-rspamd_check_action_metric (struct rspamd_task *task,
-               double score, double *rscore, struct metric *metric)
+enum rspamd_metric_action
+rspamd_check_action_metric (struct rspamd_task *task, struct metric_result *mres)
 {
        struct metric_action *action, *selected_action = NULL;
        double max_score = 0;
-       const ucl_object_t *ms = NULL;
        int i;
 
-       if (task->settings) {
-               ms = ucl_object_lookup (task->settings, metric->name);
-       }
-
        for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) {
                double sc;
 
-               action = &metric->actions[i];
-               sc = get_specific_action_score (task, ms, action);
+               action = &mres->metric->actions[i];
+               sc = mres->actions_limits[i];
 
                if (isnan (sc)) {
                        continue;
                }
 
-               if (score >= sc && sc > max_score) {
+               if (mres->score >= sc && sc > max_score) {
                        selected_action = action;
                        max_score = sc;
                }
-
-               if (rscore != NULL && i == METRIC_ACTION_REJECT) {
-                       *rscore = sc;
-               }
        }
 
        if (selected_action) {
index 6bb0ef4e98a41524c5a3ee6ce70d94020eb8ef33..280c54879300d1b06e009b242b4a3bf693def63d 100644 (file)
@@ -97,9 +97,7 @@ const gchar * rspamd_action_to_str (enum rspamd_metric_action action);
 /*
  * Get action for specific metric
  */
-gint rspamd_check_action_metric (struct rspamd_task *task,
-       double score,
-       double *rscore,
-       struct metric *metric);
+enum rspamd_metric_action rspamd_check_action_metric (struct rspamd_task *task,
+       struct metric_result *mres);
 
 #endif
index f949ca0a50e10c1d91854aac1771876b745d15cd..609f7995bf9b680442b0640bc663e4e28e4ec7cc 100644 (file)
@@ -801,8 +801,7 @@ rspamd_metric_result_ucl (struct rspamd_task *task,
        const gchar *subject;
 
        m = mres->metric;
-       mres->action = rspamd_check_action_metric (task, mres->score,
-                                       &mres->actions_limits[METRIC_ACTION_REJECT], m);
+       mres->action = rspamd_check_action_metric (task, mres);
 
        action = mres->action;
        is_spam = (action < METRIC_ACTION_GREYLIST);
@@ -978,7 +977,6 @@ rspamd_protocol_http_reply (struct rspamd_http_message *msg,
        const struct rspamd_re_cache_stat *restat;
        gpointer h, v;
        ucl_object_t *top = NULL;
-       gdouble required_score;
        gint action;
 
        /* Write custom headers */
@@ -1032,8 +1030,7 @@ rspamd_protocol_http_reply (struct rspamd_http_message *msg,
                /* Update stat for default metric */
                metric_res = g_hash_table_lookup (task->results, DEFAULT_METRIC);
                if (metric_res != NULL) {
-                       action = rspamd_check_action_metric (task, metric_res->score, &required_score,
-                                       metric_res->metric);
+                       action = rspamd_check_action_metric (task, metric_res);
                        if (action <= METRIC_ACTION_NOACTION) {
 #ifndef HAVE_ATOMIC_BUILTINS
                                task->worker->srv->stat->actions_stat[action]++;
index ca577317d3803624fb429669fa50999e4794dd6f..b8029b306ba34c35c075ef08afeb187fe1b84cd1 100644 (file)
@@ -124,9 +124,7 @@ rspamd_roll_history_update (struct roll_history *history,
        }
        else {
                row->score = metric_res->score;
-               row->action = rspamd_check_action_metric (task, metric_res->score,
-                               &row->required_score,
-                               metric_res->metric);
+               row->action = rspamd_check_action_metric (task, metric_res);
                cbdata.pos = row->symbols;
                cbdata.remain = sizeof (row->symbols);
                g_hash_table_foreach (metric_res->symbols,
index 2a40ca26c9ccf1abdc25b871253378cef49ccbd0..e6d34e406072a15ca66011a0917a706d5841516d 100644 (file)
@@ -733,10 +733,7 @@ rspamd_stat_check_autolearn (struct rspamd_task *task)
                                        mres = g_hash_table_lookup (task->results, DEFAULT_METRIC);
 
                                        if (mres) {
-                                               mres->action = rspamd_check_action_metric (task,
-                                                               mres->score,
-                                                               &mres->actions_limits[METRIC_ACTION_REJECT],
-                                                               mres->metric);
+                                               mres->action = rspamd_check_action_metric (task, mres);
 
                                                if (mres->action == METRIC_ACTION_REJECT) {
                                                        task->flags |= RSPAMD_TASK_FLAG_LEARN_SPAM;
index fa60184eef438247638f52324f41e5f9d56b2b5f..82ac792885feb35efac1284458523047eafc3dd2 100644 (file)
@@ -2358,10 +2358,39 @@ lua_task_set_settings (lua_State *L)
 {
        struct rspamd_task *task = lua_check_task (L, 1);
        ucl_object_t *settings;
+       const ucl_object_t *act, *elt, *metric_elt;
+       struct metric_result *mres;
+       guint i;
 
        settings = ucl_object_lua_import (L, 2);
+
        if (settings != NULL && task != NULL) {
                task->settings = settings;
+
+               metric_elt = ucl_object_lookup (settings, DEFAULT_METRIC);
+
+               if (metric_elt) {
+                       act = ucl_object_lookup (metric_elt, "actions");
+
+                       if (act) {
+                               /* Adjust desired actions */
+                               mres = g_hash_table_lookup (task->results, DEFAULT_METRIC);
+
+                               if (mres == NULL) {
+                                       mres = rspamd_create_metric_result (task, DEFAULT_METRIC);
+                               }
+
+                               for (i = 0; i < METRIC_ACTION_MAX; i++) {
+                                       elt = ucl_object_lookup (act, rspamd_action_to_str (i));
+
+                                       if (elt) {
+                                               mres->actions_limits[i] = ucl_object_todouble (elt);
+                                               msg_debug_task ("adjusted action %s to %.2f",
+                                                               ucl_object_key (elt), mres->actions_limits[i]);
+                                       }
+                               }
+                       }
+               }
        }
        else {
                return luaL_error (L, "invalid arguments");
@@ -2506,9 +2535,7 @@ lua_task_get_metric_action (lua_State *L)
        if (task && metric_name) {
                if ((metric_res =
                        g_hash_table_lookup (task->results, metric_name)) != NULL) {
-                       action = rspamd_check_action_metric (task, metric_res->score,
-                                       NULL,
-                                       metric_res->metric);
+                       action = rspamd_check_action_metric (task, metric_res);
                        lua_pushstring (L, rspamd_action_to_str (action));
                }
                else {