From 3737999bbc0f7c5d1db1060abfa788c95c17e99b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 27 Mar 2024 16:04:29 +0000 Subject: [PATCH] [Feature] Cloudmark: Add scores_symbols setting --- lualib/lua_scanners/cloudmark.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lualib/lua_scanners/cloudmark.lua b/lualib/lua_scanners/cloudmark.lua index 13ebc157d..082596688 100644 --- a/lualib/lua_scanners/cloudmark.lua +++ b/lualib/lua_scanners/cloudmark.lua @@ -111,6 +111,7 @@ local function cloudmark_config(opts) symbol_spam = 'CLOUDMARK_SPAM', add_score_header = false, -- Add X-CMAE-Score header add_headers = false, -- allow addition of the headers from Cloudmark + scores_symbols = nil, -- a table with match { [score_threshold] = symbol, ... } } cloudmark_conf = lua_util.override_defaults(cloudmark_conf, opts) @@ -201,6 +202,28 @@ local function table_to_multipart_body(tbl, boundary) return out end +local function get_specific_symbol(scores_symbols, score) + local selected = nil + local sel_thr = 0 + + for threshold, sym in pairs(scores_symbols) do + if sel_thr < threshold and threshold <= score then + selected = sym + sel_thr = threshold + end + end + + return selected +end + +assert(get_specific_symbol({ [90] = 'CLOUDMARK_SPAM' }, 100) == 'CLOUDMARK_SPAM') +assert(get_specific_symbol({ [90] = 'CLOUDMARK_SPAM' }, 80) == nil) +assert(get_specific_symbol({ [90] = 'CLOUDMARK_SPAM', [80] = 'CLOUDMARK_SPAM2' }, 100) == 'CLOUDMARK_SPAM') +assert(get_specific_symbol({ [90] = 'CLOUDMARK_SPAM', [80] = 'CLOUDMARK_SPAM2' }, 80) == 'CLOUDMARK_SPAM2') +assert(get_specific_symbol({ [90] = 'CLOUDMARK_SPAM', [80] = 'CLOUDMARK_SPAM2' }, 70) == nil) +assert(get_specific_symbol({ [90] = 'CLOUDMARK_SPAM', [80] = 'CLOUDMARK_SPAM2' }, 90) == 'CLOUDMARK_SPAM') +assert(get_specific_symbol({ }, 80) == nil) + local function parse_cloudmark_reply(task, rule, body) local parser = ucl.parser() local ret, err = parser:parse_string(body) @@ -250,6 +273,13 @@ local function parse_cloudmark_reply(task, rule, body) }) end + if type(rule.scores_symbols) == 'table' then + local sym = get_specific_symbol(rule.scores_symbols, score) + if sym then + task:insert_result(sym, 1.0, tostring(score)) + end + end + end local function cloudmark_check(task, content, digest, rule, maybe_part) -- 2.39.5