]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Implement dependencies for maps
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 28 Jul 2016 16:20:04 +0000 (17:20 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 28 Jul 2016 16:20:50 +0000 (17:20 +0100)
Issue: #765

src/plugins/lua/multimap.lua
test/functional/cases/102_maps.robot
test/functional/configs/maps.conf

index 5b24c2f77b4a1faff39e34eaa0ab8a227e538f01..b197402319e7f449a0e188940959475f4a3969e9 100644 (file)
@@ -21,6 +21,7 @@ local rspamd_logger = require "rspamd_logger"
 local cdb = require "rspamd_cdb"
 local util = require "rspamd_util"
 local regexp = require "rspamd_regexp"
+local rspamd_expression = require "rspamd_expression"
 require "fun" ()
 
 local urls = {}
@@ -347,6 +348,17 @@ local function multimap_callback(task, rule)
     end
   end
 
+  if rule['expression'] then
+    local res,trace = rule['expression']:process_traced(task)
+
+    if not res then
+      rspamd_logger.debugx(task, 'condition is false for %s', rule['symbol'])
+    else
+      rspamd_logger.debugx(task, 'condition is true for %s: %s', rule['symbol'],
+        trace)
+    end
+  end
+
   local rt = rule['type']
   if rt == 'ip' or rt == 'dnsbl' then
     local ip = task:get_from_ip()
@@ -519,6 +531,37 @@ local function add_multimap_rule(key, newrule)
   end
 
   if ret then
+    if newrule['require_symbols'] and not newrule['prefilter'] then
+      local atoms = {}
+
+      local function parse_atom(str)
+        local atom = table.concat(totable(take_while(function(c)
+          if string.find(', \t()><+!|&\n', c) then
+            return false
+          end
+          return true
+        end, iter(str))), '')
+        table.insert(atoms, atom)
+
+        return atom
+      end
+
+      local function process_atom(atom, task)
+        if task:has_symbol(atom) then
+          return 1
+        end
+
+        return 0
+      end
+
+      local expression = rspamd_expression.create(newrule['require_symbols'],
+        {parse_atom, process_atom}, rspamd_config:get_mempool())
+      if expression then
+        newrule['expression'] = expression
+        each(function(v) rspamd_config:register_dependency(newrule['symbol'], v) end,
+          atoms)
+      end
+    end
     return newrule
   end
 
index 0f89a6ff82e32e99d84b33f848d1175f75147682..0a83283f68557a174745e045d7efbb00af4bc87f 100644 (file)
@@ -59,4 +59,8 @@ MAP - FROM REGEXP
 
 MAP - FROM REGEXP MISS
   ${result} =  Scan Message With Rspamc  ${MESSAGE}  --from  user@other.org
-  Check Rspamc  ${result}  REGEXP_MAP  inverse=1  rc_nocheck=1
\ No newline at end of file
+  Check Rspamc  ${result}  REGEXP_MAP  inverse=1  rc_nocheck=1
+
+MAP - DEPENDS HIT
+  ${result} =  Scan Message With Rspamc  ${MESSAGE}  -i  147.243.1.47  --from  user123@microsoft.com
+  Check Rspamc  ${result}  DEPS_MAP
\ No newline at end of file
index df568c22dbf54fe1ffb64394d5be90f8ad94a69a..d90440384f8c1d684c58a6dc31b1e7a8f8392a72 100644 (file)
@@ -48,4 +48,11 @@ multimap {
     regexp = true;
     map = "${TESTDIR}/configs/maps/regexp.list";
   }
+  DEPS_MAP {
+    type = "from";
+    filter = "email:addr";
+    regexp = true;
+    map = "${TESTDIR}/configs/maps/regexp.list";
+    require_symbols = "R_SPF_ALLOW & DNSBL_MAP & !FROM_MAP";
+  }
 }