From 5b3582fb4e1fdac5df3219938eb2e825c175770c Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 21 Jul 2018 11:55:01 +0100 Subject: [PATCH] [Feature] Allow to split symbols in reputation plugin --- src/plugins/lua/reputation.lua | 42 ++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/plugins/lua/reputation.lua b/src/plugins/lua/reputation.lua index 3f21b8030..f64fd6c6c 100644 --- a/src/plugins/lua/reputation.lua +++ b/src/plugins/lua/reputation.lua @@ -1,5 +1,5 @@ --[[ -Copyright (c) 2017, Vsevolod Stakhov +Copyright (c) 2017-2018, Vsevolod Stakhov Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -55,6 +55,21 @@ local function generic_reputation_calc(token, rule, mult) return score end +local function add_symbol_score(task, rule, mult, params) + if not params then params = {tostring(mult)}; + + end + if rule.cfg.split_symbols then + if mult >= 0 then + task:insert_result(rule.symbol .. '_SPAM', mult, params) + else + task:insert_result(rule.symbol .. '_HAM', mult, params) + end + else + task:insert_result(rule.symbol, mult, params) + end +end + -- DKIM Selector functions local gr local function gen_dkim_queries(task, rule) @@ -116,9 +131,9 @@ local function dkim_reputation_filter(task, rule) -- Set local reputation symbol if rep_accepted > 0 or rep_rejected > 0 then if rep_accepted > rep_rejected then - task:insert_result(rule.symbol, -(rep_accepted - rep_rejected)) + add_symbol_score(task, rule, -(rep_accepted - rep_rejected)) else - task:insert_result(rule.symbol, (rep_rejected - rep_accepted)) + add_symbol_score(task, rule, (rep_rejected - rep_accepted)) end -- Store results for future DKIM results adjustments @@ -271,7 +286,7 @@ local function url_reputation_filter(task, rule) if math.abs(score) > 1e-3 then -- TODO: add description - task:insert_result(rule.symbol, score) + add_symbol_score(task, rule, score) end end end @@ -404,7 +419,7 @@ local function ip_reputation_filter(task, rule) end if math.abs(score) > 0.001 then - task:insert_result(rule.symbol, score, table.concat(description_t, ', ')) + add_symbol_score(task, rule, score, table.concat(description_t, ', ')) end end @@ -565,7 +580,7 @@ local function spf_reputation_filter(task, rule) if math.abs(score) > 1e-3 then -- TODO: add description - task:insert_result(rule.symbol, score) + add_symbol_score(task, rule, score) end end end @@ -1101,12 +1116,25 @@ local function parse_rule(name, tbl) end) -- We now generate symbol for checking - rspamd_config:register_symbol{ + local id = rspamd_config:register_symbol{ name = symbol, type = 'normal', callback = callback_gen(reputation_filter_cb, rule), } + if rule.config.split_symbols then + rspamd_config:register_symbol{ + name = symbol .. '_HAM', + type = 'virtual', + parent = id, + } + rspamd_config:register_symbol{ + name = symbol .. '_SPAM', + type = 'virtual', + parent = id, + } + end + if rule.selector.dependencies then fun.each(function(d) rspamd_config:register_dependency(symbol, d) -- 2.39.5