From fb6e9f73f4b26fff517784e820d4896020f49c40 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 29 Mar 2016 11:31:59 +0100 Subject: [PATCH] [Feature] Simplify actions application by the settings --- src/libmime/filter.c | 45 ++++-------------------------------- src/libmime/filter.h | 6 ++--- src/libserver/protocol.c | 7 ++---- src/libserver/roll_history.c | 4 +--- src/libstat/stat_process.c | 5 +--- src/lua/lua_task.c | 33 +++++++++++++++++++++++--- 6 files changed, 41 insertions(+), 59 deletions(-) diff --git a/src/libmime/filter.c b/src/libmime/filter.c index f5c819821..7ed6ea010 100644 --- a/src/libmime/filter.c +++ b/src/libmime/filter.c @@ -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) { diff --git a/src/libmime/filter.h b/src/libmime/filter.h index 6bb0ef4e9..280c54879 100644 --- a/src/libmime/filter.h +++ b/src/libmime/filter.h @@ -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 diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c index f949ca0a5..609f7995b 100644 --- a/src/libserver/protocol.c +++ b/src/libserver/protocol.c @@ -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]++; diff --git a/src/libserver/roll_history.c b/src/libserver/roll_history.c index ca577317d..b8029b306 100644 --- a/src/libserver/roll_history.c +++ b/src/libserver/roll_history.c @@ -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, diff --git a/src/libstat/stat_process.c b/src/libstat/stat_process.c index 2a40ca26c..e6d34e406 100644 --- a/src/libstat/stat_process.c +++ b/src/libstat/stat_process.c @@ -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; diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index fa60184ee..82ac79288 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -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 { -- 2.39.5