]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Honor dynamic thresholds for greylisting module
authorVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 5 Apr 2024 16:56:15 +0000 (17:56 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 5 Apr 2024 16:56:15 +0000 (17:56 +0100)
src/lua/lua_task.c
src/plugins/lua/greylist.lua

index e37c6838d4f390ddda95b6cf3b929dd9244f83ac..edbe5b89acf60a54427e5d1f8d8c1c94d7894ba3 100644 (file)
@@ -872,6 +872,14 @@ LUA_FUNCTION_DEF(task, get_metric_result);
  * @return {number,number} 2 numbers containing the current score and required score of the metric
  */
 LUA_FUNCTION_DEF(task, get_metric_score);
+
+/***
+ * @method task:get_metric_threshold(action)
+ * Get the current threshold of the action `action` for the default metric. Should be used after settings have been applied.
+ * @param {string} action name of a action
+ * @return {number} the current threshold of the action
+ */
+LUA_FUNCTION_DEF(task, get_metric_threshold);
 /***
  * @method task:get_metric_action(name)
  * Get the current action of metric `name` (must be nil or 'default'). Should be used in idempotent filters only.
@@ -1307,6 +1315,7 @@ static const struct luaL_reg tasklib_m[] = {
        LUA_INTERFACE_DEF(task, get_metric_result),
        LUA_INTERFACE_DEF(task, get_metric_score),
        LUA_INTERFACE_DEF(task, get_metric_action),
+       LUA_INTERFACE_DEF(task, get_metric_threshold),
        LUA_INTERFACE_DEF(task, set_metric_score),
        LUA_INTERFACE_DEF(task, set_metric_subject),
        LUA_INTERFACE_DEF(task, learn),
@@ -6351,6 +6360,38 @@ lua_task_get_metric_action(lua_State *L)
        return 1;
 }
 
+static int
+lua_task_get_metric_threshold(lua_State *L)
+{
+       LUA_TRACE_POINT;
+       struct rspamd_task *task = lua_check_task(L, 1);
+       const char *act_name = luaL_checkstring(L, 2);
+
+       if (task && act_name && task->result) {
+               struct rspamd_action *action = rspamd_config_get_action(task->cfg, act_name);
+
+               if (action == NULL) {
+                       lua_pushnil(L);
+               }
+               else {
+                       struct rspamd_action_config *act_config =
+                               rspamd_find_action_config_for_action(task->result, action);
+
+                       if (act_config) {
+                               lua_pushnumber(L, act_config->cur_limit);
+                       }
+                       else {
+                               lua_pushnil(L);
+                       }
+               }
+       }
+       else {
+               return luaL_error(L, "invalid arguments");
+       }
+
+       return 1;
+}
+
 static int
 lua_task_set_metric_score(lua_State *L)
 {
index 540b92fe6efef1651fa681d0059eec2214993d00..e4a633233c52e8a04c7321c3bee6b1054df5d65f 100644 (file)
@@ -330,7 +330,7 @@ local function greylist_set(task)
   end
 
   -- We need to update this on each scan, as it can vary per settings or be redefined dynamically
-  local greylist_min_score = settings.greylist_min_score or rspamd_config:get_metric_action('greylist')
+  local greylist_min_score = settings.greylist_min_score or task:get_metric_threshold('greylist')
   if greylist_min_score then
     local score = task:get_metric_score()[1]
     if score < greylist_min_score then