Navigation Menu

Skip to content

Commit

Permalink
[Minor] Milter_headers: Refactor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Feb 11, 2021
1 parent 9157ad8 commit 414fdd4
Showing 1 changed file with 47 additions and 37 deletions.
84 changes: 47 additions & 37 deletions src/plugins/lua/milter_headers.lua
Expand Up @@ -52,7 +52,8 @@ local settings = {
['x-spamd-result'] = {
header = 'X-Spamd-Result',
remove = 0,
stop_chars = ' '
stop_chars = ' ',
sort_by = 'name',
},
['x-rspamd-server'] = {
header = 'X-Rspamd-Server',
Expand Down Expand Up @@ -225,6 +226,7 @@ local function milter_headers(task)
end

routines['x-spamd-result'] = function()
local local_mod = settings.routines['x-spamd-result']
if skip_wanted('x-spamd-result') then return end
if not common.symbols then
common.symbols = task:get_symbols_all()
Expand All @@ -235,19 +237,22 @@ local function milter_headers(task)
if not common['metric_action'] then
common['metric_action'] = task:get_metric_action('default')
end
if settings.routines['x-spamd-result'].remove then
remove[settings.routines['x-spamd-result'].header] = settings.routines['x-spamd-result'].remove
if local_mod.remove then
remove[local_mod.header] = local_mod.remove
end

local buf = {}
table.insert(buf, table.concat({
'default: ', (common['metric_action'] == 'reject') and 'True' or 'False', ' [',
string.format('%.2f', common['metric_score'][1]), ' / ', string.format('%.2f', common['metric_score'][2]), ']'
}))
local verdict = string.format('default: %s [%.2f / %.2f]',
--TODO: (common.metric_action == 'no action') and 'False' or 'True',
(common.metric_action == 'reject') and 'True' or 'False',
common.metric_score[1], common.metric_score[2])
table.insert(buf, verdict)

-- Deal with symbols
for _, s in ipairs(common.symbols) do
if not s.options then s.options = {} end
table.insert(buf, table.concat({
' ', s.name, '(', string.format('%.2f', s.score), ')[', table.concat(s.options, ','), ']',

This comment has been minimized.

Copy link
@chkpnt

chkpnt Oct 15, 2021

Contributor

Nice to see the leading space is gone :-)

}))
local sym_str = string.format('%s(%.2f)[%s]',
s.name, s.score, table.concat(s.options or {}, ','))
table.insert(buf, sym_str)
end
add_header('x-spamd-result', table.concat(buf, '; '), ';')
end
Expand Down Expand Up @@ -291,37 +296,40 @@ local function milter_headers(task)
end

routines['x-rspamd-server'] = function()
local local_mod = settings.routines['x-rspamd-server']
if skip_wanted('x-rspamd-server') then return end
if settings.routines['x-rspamd-server'].remove then
remove[settings.routines['x-rspamd-server'].header] = settings.routines['x-rspamd-server'].remove
if local_mod.remove then
remove[local_mod.header] = local_mod.remove
end
local hostname = settings.routines['x-rspamd-server'].hostname
add[settings.routines['x-rspamd-server'].header] = hostname and hostname or HOSTNAME
local hostname = local_mod.hostname
add[local_mod.header] = hostname and hostname or HOSTNAME
end

routines['x-spamd-bar'] = function()
local local_mod = settings.routines['x-spamd-bar']
if skip_wanted('x-rspamd-bar') then return end
if not common['metric_score'] then
common['metric_score'] = task:get_metric_score('default')
end
local score = common['metric_score'][1]
local spambar
if score <= -1 then
spambar = string.rep(settings.routines['x-spamd-bar'].negative, score*-1)
spambar = string.rep(local_mod.negative, score * (-1))
elseif score >= 1 then
spambar = string.rep(settings.routines['x-spamd-bar'].positive, score)
spambar = string.rep(local_mod.positive, score)
else
spambar = settings.routines['x-spamd-bar'].neutral
spambar = local_mod.neutral
end
if settings.routines['x-spamd-bar'].remove then
remove[settings.routines['x-spamd-bar'].header] = settings.routines['x-spamd-bar'].remove
if local_mod.remove then
remove[local_mod.header] = local_mod.remove
end
if spambar ~= '' then
add[settings.routines['x-spamd-bar'].header] = spambar
add[local_mod.header] = spambar
end
end

routines['x-spam-level'] = function()
local local_mod = settings.routines['x-spam-level']
if skip_wanted('x-spam-level') then return end
if not common['metric_score'] then
common['metric_score'] = task:get_metric_score('default')
Expand All @@ -330,10 +338,10 @@ local function milter_headers(task)
if score < 1 then
return nil, {}, {}
end
if settings.routines['x-spam-level'].remove then
remove[settings.routines['x-spam-level'].header] = settings.routines['x-spam-level'].remove
if local_mod.remove then
remove[local_mod.header] = local_mod.remove
end
add[settings.routines['x-spam-level'].header] = string.rep(settings.routines['x-spam-level'].char, score)
add[local_mod.header] = string.rep(local_mod.char, score)
end

local function spam_header (class, name, value, remove_v)
Expand Down Expand Up @@ -362,6 +370,7 @@ local function milter_headers(task)
end

routines['x-virus'] = function()
local local_mod = settings.routines['x-virus']
if skip_wanted('x-virus') then return end
if not common.symbols_hash then
if not common.symbols then
Expand All @@ -373,11 +382,11 @@ local function milter_headers(task)
end
common.symbols_hash = h
end
if settings.routines['x-virus'].remove then
remove[settings.routines['x-virus'].header] = settings.routines['x-virus'].remove
if local_mod.remove then
remove[local_mod.header] = local_mod.remove
end
local virii = {}
for _, sym in ipairs(settings.routines['x-virus'].symbols) do
for _, sym in ipairs(local_mod.symbols) do
local s = common.symbols_hash[sym]
if s then
if (s.options or E)[1] then
Expand All @@ -389,14 +398,14 @@ local function milter_headers(task)
end
if #virii > 0 then
local virusstatus = table.concat(virii, ',')
if settings.routines['x-virus'].status_infected then
virusstatus = settings.routines['x-virus'].status_infected .. ', ' .. virusstatus
if local_mod.status_infected then
virusstatus = local_mod,status_infected .. ', ' .. virusstatus
end
add_header('x-virus', virusstatus)
else
local failed = false
local fail_reason = 'unknown'
for _, sym in ipairs(settings.routines['x-virus'].symbols_fail) do
for _, sym in ipairs(local_mod.symbols_fail) do
local s = common.symbols_hash[sym]
if s then
failed = true
Expand All @@ -406,20 +415,21 @@ local function milter_headers(task)
end
end
if not failed then
if settings.routines['x-virus'].status_clean then
add_header('x-virus', settings.routines['x-virus'].status_clean)
if local_mod.status_clean then
add_header('x-virus', local_mod.status_clean)
end
else
if settings.routines['x-virus'].status_clean then
if local_mod.status_clean then
add_header('x-virus', string.format('%s(%s)',
settings.routines['x-virus'].status_fail, fail_reason))
local_mod.status_fail, fail_reason))
end
end
end
end

routines['x-os-fingerprint'] = function()
if skip_wanted('x-os-fingerprint') then return end
local local_mod = settings.routines['x-os-fingerprint\'']

local os_string, link_type, uptime_min, distance =
task:get_mempool():get_variable('os_fingerprint',
Expand All @@ -430,9 +440,8 @@ local function milter_headers(task)
local value = string.format('%s, (up: %i min), (distance %i, link: %s)',
os_string, uptime_min, distance, link_type)

if settings.routines['x-os-fingerprint'].remove then
remove[settings.routines['x-os-fingerprint'].header]
= settings.routines['x-os-fingerprint'].remove
if local_mod.remove then
remove[local_mod.header] = local_mod.remove
end

add_header('x-os-fingerprint', value)
Expand All @@ -456,6 +465,7 @@ local function milter_headers(task)
is_spam = 'No'
end
spamstatus = is_spam .. ', score=' .. string.format('%.2f', score)

if settings.routines['x-spam-status'].remove then
remove[settings.routines['x-spam-status'].header] = settings.routines['x-spam-status'].remove
end
Expand Down

0 comments on commit 414fdd4

Please sign in to comment.