From 1bd6eb011a50b0d57781850db9b8fd7150300133 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 28 Jul 2016 17:20:04 +0100 Subject: [PATCH] [Feature] Implement dependencies for maps Issue: #765 --- src/plugins/lua/multimap.lua | 43 ++++++++++++++++++++++++++++ test/functional/cases/102_maps.robot | 6 +++- test/functional/configs/maps.conf | 7 +++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/plugins/lua/multimap.lua b/src/plugins/lua/multimap.lua index 5b24c2f77..b19740231 100644 --- a/src/plugins/lua/multimap.lua +++ b/src/plugins/lua/multimap.lua @@ -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 diff --git a/test/functional/cases/102_maps.robot b/test/functional/cases/102_maps.robot index 0f89a6ff8..0a83283f6 100644 --- a/test/functional/cases/102_maps.robot +++ b/test/functional/cases/102_maps.robot @@ -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 diff --git a/test/functional/configs/maps.conf b/test/functional/configs/maps.conf index df568c22d..d90440384 100644 --- a/test/functional/configs/maps.conf +++ b/test/functional/configs/maps.conf @@ -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"; + } } -- 2.39.5