commit 414fdd4: [Minor] Milter_headers: Refactor stuff

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Feb 11 16:14:06 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-02-11 15:45:37 +0000
URL: https://github.com/rspamd/rspamd/commit/414fdd46d75e716a2e5e6064ad5c7e580bfc0739

[Minor] Milter_headers: Refactor stuff

---
 src/plugins/lua/milter_headers.lua | 84 +++++++++++++++++++++-----------------
 1 file changed, 47 insertions(+), 37 deletions(-)

diff --git a/src/plugins/lua/milter_headers.lua b/src/plugins/lua/milter_headers.lua
index 7dd590faa..7761b632f 100644
--- a/src/plugins/lua/milter_headers.lua
+++ b/src/plugins/lua/milter_headers.lua
@@ -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',
@@ -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()
@@ -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, ','), ']',
-      }))
+      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
@@ -291,15 +296,17 @@ 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')
@@ -307,21 +314,22 @@ local function milter_headers(task)
     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')
@@ -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)
@@ -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
@@ -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
@@ -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
@@ -406,13 +415,13 @@ 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
@@ -420,6 +429,7 @@ local function milter_headers(task)
 
   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',
@@ -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)
@@ -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


More information about the Commits mailing list