diff options
Diffstat (limited to 'src/plugins/lua/force_actions.lua')
-rw-r--r-- | src/plugins/lua/force_actions.lua | 71 |
1 files changed, 17 insertions, 54 deletions
diff --git a/src/plugins/lua/force_actions.lua b/src/plugins/lua/force_actions.lua index 8e645290e..caa03da64 100644 --- a/src/plugins/lua/force_actions.lua +++ b/src/plugins/lua/force_actions.lua @@ -64,66 +64,29 @@ local function gen_cb(expr, act, pool, message, subject, raction, honor, limit, return function(task) - local function fill_vars(repl, var_class, var_payload) - -- fill template vars prefixed with - -- 'selector::' => fill with value extracted by a selector - -- 'symbol::' => fill with matching symbol option string(s) - -- 'task::' => fill with matching task attribute - -- (only queue_id and uid allowed) - - if var_class == "selector" then - local selector = lua_selectors.create_selector_closure(rspamd_config, var_payload, '', true) + 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(rspamd_config, 'could not create selector [%1]', var_payload) + rspamd_logger.errx(task, 'could not create selector [%1]', selector_expr) return "((could not create selector))" end - local extracted = selector(task) - if extracted then - -- replace non ascii chars - if type(extracted) == 'table' then - extracted = table.concat(extracted, ',') - end - extracted = string.gsub(extracted, '[\128-\255]', '?') - else - rspamd_logger.errx(rspamd_config, 'could not extract value with selector [%1]', var_payload) - extracted = '((error extracting value))' - end - return extracted end - - if var_class == "symbol" then - local symb_opts - local symb_strs = {} - if task:has_symbol(var_payload) then - local symb_tbl = task:get_symbol(var_payload) - for _,symb in ipairs(symb_tbl) do - local symb_opts = symb.options - if symb_opts then - -- replace non ascii chars - symb_strs[#symb_strs+1] = string.gsub(table.concat(symb_opts, ','), '[\128-\255]', '?') - end - end - symb_str = table.concat(symb_strs, ',') - else - symb_str = '((symbol not found))' + local extracted = selector(task) + if extracted then + if type(extracted) == 'table' then + extracted = table.concat(extracted, ',') end - return symb_str - end - - -- NOTE-TO-VSTAKHOV: would it make sense to export task:get_queue_id and task:get_uid as selector data definition? - if var_class == "task" then - local attr_val = '((unknown task attribute))' - if var_payload == 'queue_id' then - attr_val = task:get_queue_id() - elseif var_payload == 'uid' then - attr_val = task:get_uid() - end - return attr_val + 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 @@ -144,8 +107,8 @@ local function gen_cb(expr, act, pool, message, subject, raction, honor, limit, if least then flags = "least" end if type(message) == 'string' then - -- fill vars in return message - message = string.gsub(message, '(${(.-)::(.-)})', fill_vars) + -- process selector expressions in the message + message = string.gsub(message, '(${(.-)})', process_message_selectors) task:set_pre_result(act, message, N, nil, nil, flags) else |