]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Allow setting scores and actions from lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 3 May 2016 11:23:18 +0000 (12:23 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 3 May 2016 11:23:18 +0000 (12:23 +0100)
src/libmime/filter.c
src/libserver/protocol.c
src/libstat/stat_process.c
src/lua/lua_task.c

index b0f93ec4b8ac30bf6649f42ea4c9e53c8fa2deea..3a3203ed9162d3bb8e849d0e87b775bd046585d4 100644 (file)
@@ -57,14 +57,14 @@ rspamd_create_metric_result (struct rspamd_task *task, const gchar *name)
        metric_res->metric = metric;
        metric_res->grow_factor = 0;
        metric_res->score = 0;
-       g_hash_table_insert (task->results, (gpointer) metric->name,
-                       metric_res);
 
        for (i = 0; i < METRIC_ACTION_MAX; i++) {
                metric_res->actions_limits[i] = metric->actions[i].score;
        }
 
        metric_res->action = METRIC_ACTION_MAX;
+       g_hash_table_insert (task->results, (gpointer) metric->name,
+                       metric_res);
 
        return metric_res;
 }
index 3571e2e4198bc6fa7d410f557967a8d6cf5f54fd..a394ddaff0473402e4f9bc4f1543befa545727d2 100644 (file)
@@ -835,7 +835,10 @@ rspamd_metric_result_ucl (struct rspamd_task *task,
        const gchar *subject;
 
        m = mres->metric;
-       mres->action = rspamd_check_action_metric (task, mres);
+
+       if (mres->action == METRIC_ACTION_MAX) {
+               mres->action = rspamd_check_action_metric (task, mres);
+       }
 
        action = mres->action;
        is_spam = (action < METRIC_ACTION_GREYLIST);
@@ -1064,7 +1067,14 @@ 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);
+
+                       if (metric_res->action != METRIC_ACTION_MAX) {
+                               action = metric_res->action;
+                       }
+                       else {
+                               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 1c8fbf070a818eb5578187b92402de6125b9d423..78166906fb637bb16a282edcc07057f117950c99 100644 (file)
@@ -793,7 +793,10 @@ 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);
+
+                                               if (mres->action == METRIC_ACTION_MAX) {
+                                                       mres->action = rspamd_check_action_metric (task, mres);
+                                               }
 
                                                if (mres->action == METRIC_ACTION_REJECT) {
                                                        task->flags |= RSPAMD_TASK_FLAG_LEARN_SPAM;
index 1f038376401406ff27f285268ed4a1a30b25b484..1348769404b300b48c0e873b98de31a2c7904e79 100644 (file)
@@ -468,6 +468,21 @@ LUA_FUNCTION_DEF (task, get_metric_score);
  * @return {string} the current action of the metric as a string
  */
 LUA_FUNCTION_DEF (task, get_metric_action);
+/***
+ * @method task:set_metric_score(name, score)
+ * Set the current score of metric `name`. Should be used in post-filters only.
+ * @param {string} name name of a metric
+ * @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:learn(is_spam[, classifier)
  * Learn classifier `classifier` with the task. If `is_spam` is true then message
@@ -645,6 +660,8 @@ static const struct luaL_reg tasklib_m[] = {
        LUA_INTERFACE_DEF (task, get_timeval),
        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, learn),
        LUA_INTERFACE_DEF (task, set_settings),
        LUA_INTERFACE_DEF (task, get_settings),
@@ -2883,6 +2900,77 @@ lua_task_get_metric_action (lua_State *L)
        return 1;
 }
 
+static gint
+lua_task_set_metric_score (lua_State *L)
+{
+       struct rspamd_task *task = lua_check_task (L, 1);
+       const gchar *metric_name;
+       struct metric_result *metric_res;
+       gdouble nscore;
+
+       metric_name = luaL_checkstring (L, 2);
+       nscore = luaL_checknumber (L, 3);
+
+       if (metric_name == NULL) {
+               metric_name = DEFAULT_METRIC;
+       }
+
+       if (task && metric_name) {
+               if ((metric_res =
+                       g_hash_table_lookup (task->results, metric_name)) != NULL) {
+                       metric_res->score = nscore;
+                       lua_pushboolean (L, true);
+               }
+               else {
+                       lua_pushboolean (L, false);
+               }
+       }
+       else {
+               return luaL_error (L, "invalid arguments");
+       }
+
+       return 1;
+}
+
+static gint
+lua_task_set_metric_action (lua_State *L)
+{
+       struct rspamd_task *task = lua_check_task (L, 1);
+       const gchar *metric_name, *action_name;
+       struct 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 =
+                       g_hash_table_lookup (task->results, metric_name)) != NULL) {
+
+                       if (rspamd_action_from_str (action_name, &action)) {
+                               metric_res->action = action;
+                               lua_pushboolean (L, true);
+                       }
+                       else {
+                               lua_pushboolean (L, false);
+                       }
+               }
+               else {
+                       lua_pushboolean (L, false);
+               }
+       }
+       else {
+               return luaL_error (L, "invalid arguments");
+       }
+
+       return 1;
+}
+
 /* Image functions */
 static gint
 lua_image_get_width (lua_State *L)