aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lualib/lua_selectors/extractors.lua41
-rw-r--r--lualib/lua_selectors/transforms.lua21
-rw-r--r--src/plugins/lua/force_actions.lua28
3 files changed, 87 insertions, 3 deletions
diff --git a/lualib/lua_selectors/extractors.lua b/lualib/lua_selectors/extractors.lua
index 625af435c..695371e79 100644
--- a/lualib/lua_selectors/extractors.lua
+++ b/lualib/lua_selectors/extractors.lua
@@ -388,6 +388,45 @@ The first argument must be header name.]],
]],
['args_schema'] = { ts.one_of { 'stem', 'raw', 'norm', 'full' }:is_optional()},
},
+ -- Get queue ID
+ ['queueid'] = {
+ ['get_value'] = function(task)
+ local queueid = task:get_queue_id()
+ if queueid then return queueid,'string' end
+ return nil
+ end,
+ ['description'] = [[Get queue ID]],
+ },
+ -- Get ID of the task being processed
+ ['uid'] = {
+ ['get_value'] = function(task)
+ local uid = task:get_uid()
+ if uid then return uid,'string' end
+ return nil
+ end,
+ ['description'] = [[Get ID of the task being processed]],
+ },
+ -- Get message ID of the task being processed
+ ['messageid'] = {
+ ['get_value'] = function(task)
+ local mid = task:get_message_id()
+ if mid then return mid,'string' end
+ return nil
+ end,
+ ['description'] = [[Get message ID]],
+ },
+ -- Get specific symbol
+ ['symbol'] = {
+ ['get_value'] = function(task, args)
+ local symbol = task:get_symbol(args[1])
+ if symbol then
+ return symbol[1],'table'
+ end
+ end,
+ ['description'] = [[Get specific symbol. The first argument must be the symbol name. Returns the symbol table. See task:get_symbol()]],
+ ['args_schema'] = {ts.string}
+ },
+
}
-return extractors \ No newline at end of file
+return extractors
diff --git a/lualib/lua_selectors/transforms.lua b/lualib/lua_selectors/transforms.lua
index 5f1a4dca0..4cfff3654 100644
--- a/lualib/lua_selectors/transforms.lua
+++ b/lualib/lua_selectors/transforms.lua
@@ -417,8 +417,27 @@ Empty string comes the first argument or 'true', non-empty string comes nil]],
['args_schema'] = {(ts.number + ts.string / tonumber),
(ts.number + ts.string / tonumber):is_optional()}
},
+ -- Returns the string(s) with all non ascii chars replaced
+ ['to_ascii'] = {
+ ['types'] = {
+ ['string'] = true,
+ ['list'] = true,
+ },
+ ['map_type'] = 'string',
+ ['process'] = function(inp, _, args)
+ if type(inp) == 'table' then
+ return fun.map( function(s) return string.gsub(tostring(s), '[\128-\255]', args[1] or '?') end , inp), 'string_list'
+ else
+ return string.gsub(tostring(inp), '[\128-\255]', '?'), 'string'
+ end
+
+ end,
+ ['description'] = 'Returns the string with all non-ascii bytes replaced with the character given as second argument or `?`',
+ ['args_schema'] = {ts.string:is_optional()}
+ },
+
}
transform_function.match = transform_function.regexp
-return transform_function \ No newline at end of file
+return transform_function
diff --git a/src/plugins/lua/force_actions.lua b/src/plugins/lua/force_actions.lua
index 108c0b76e..eda87ead0 100644
--- a/src/plugins/lua/force_actions.lua
+++ b/src/plugins/lua/force_actions.lua
@@ -23,12 +23,14 @@ end
local E = {}
local N = 'force_actions'
+local selector_cache = {}
local fun = require "fun"
local lua_util = require "lua_util"
local rspamd_cryptobox_hash = require "rspamd_cryptobox_hash"
local rspamd_expression = require "rspamd_expression"
local rspamd_logger = require "rspamd_logger"
+local lua_selectors = require "lua_selectors"
local function gen_cb(expr, act, pool, message, subject, raction, honor, limit, least)
@@ -63,6 +65,29 @@ local function gen_cb(expr, act, pool, message, subject, raction, honor, limit,
return function(task)
+ local function process_message_selectors(repl, selector_expr)
+ -- create/reuse selector to extract value for this placeholder
+ local selector = selector_cache[selector_expr]
+ if not selector then
+ selector_cache[selector_expr] = lua_selectors.create_selector_closure(rspamd_config, selector_expr, '', true)
+ selector = selector_cache[selector_expr]
+ if not selector then
+ rspamd_logger.errx(task, 'could not create selector [%1]', selector_expr)
+ return "((could not create selector))"
+ end
+ end
+ local extracted = selector(task)
+ if extracted then
+ if type(extracted) == 'table' then
+ extracted = table.concat(extracted, ',')
+ end
+ else
+ rspamd_logger.errx(task, 'could not extract value with selector [%1]', selector_expr)
+ extracted = '((error extracting value))'
+ end
+ return extracted
+ end
+
local cact = task:get_metric_action('default')
if cact == act then
return false
@@ -83,8 +108,9 @@ local function gen_cb(expr, act, pool, message, subject, raction, honor, limit,
if least then flags = "least" end
if type(message) == 'string' then
+ -- process selector expressions in the message
+ message = string.gsub(message, '(${(.-)})', process_message_selectors)
task:set_pre_result(act, message, N, nil, nil, flags)
-
else
task:set_pre_result(act, nil, N, nil, nil, flags)
end