From 14fd0067a4b13ec87be7ddb37ee4e1939850b783 Mon Sep 17 00:00:00 2001 From: Andrew Lewis Date: Wed, 8 Feb 2017 11:14:58 +0200 Subject: [PATCH] [Minor] Multimap: use table lookup to determine processing --- src/plugins/lua/multimap.lua | 178 +++++++++++++++++++---------------- 1 file changed, 99 insertions(+), 79 deletions(-) diff --git a/src/plugins/lua/multimap.lua b/src/plugins/lua/multimap.lua index c36945f9d..3df9e0a0c 100644 --- a/src/plugins/lua/multimap.lua +++ b/src/plugins/lua/multimap.lua @@ -558,95 +558,115 @@ local function multimap_callback(task, rule) end local rt = rule['type'] - if rt == 'ip' or rt == 'dnsbl' then - local ip = task:get_from_ip() - if ip:is_valid() then - if rt == 'ip' then - match_rule(rule, ip) - else - local cb = function (_, to_resolve, results, err) - task:inc_dns_req() - if err and (err ~= 'requested record is not found' and err ~= 'no records with this name') then - rspamd_logger.errx(task, 'error looking up %s: %s', to_resolve, err) - end - if results then - task:insert_result(rule['symbol'], 1, rule['map']) + local process_rule_funcs = { + dnsbl = function() + local ip = task:get_from_ip() + if ip:is_valid() then + if rt == 'ip' then + match_rule(rule, ip) + else + local cb = function (_, to_resolve, results, err) + task:inc_dns_req() + if err and (err ~= 'requested record is not found' and err ~= 'no records with this name') then + rspamd_logger.errx(task, 'error looking up %s: %s', to_resolve, err) + end + if results then + task:insert_result(rule['symbol'], 1, rule['map']) - if pre_filter then - task:set_pre_result(rule['action'], 'Matched map: ' .. rule['symbol']) + if pre_filter then + task:set_pre_result(rule['action'], 'Matched map: ' .. rule['symbol']) + end end end - end - task:get_resolver():resolve_a({task = task, - name = ip_to_rbl(ip, rule['map']), - callback = cb, - }) + task:get_resolver():resolve_a({task = task, + name = ip_to_rbl(ip, rule['map']), + callback = cb, + }) + end end - end - elseif rt == 'header' then - local hv = task:get_header_full(rule['header']) - match_list(rule, hv, {'decoded'}) - elseif rt == 'rcpt' then - if task:has_recipients('smtp') then - local rcpts = task:get_recipients('smtp') - match_addr(rule, rcpts) - end - elseif rt == 'from' then - if task:has_from('smtp') then - local from = task:get_from('smtp') - match_addr(rule, from) - end - elseif rt == 'helo' then - local helo = task:get_helo() - if helo then - match_hostname(rule, helo) - end - elseif rt == 'url' then - if task:has_urls() then - local msg_urls = task:get_urls() - for _,url in ipairs(msg_urls) do - match_url(rule, url) + end, + header = function() + local hv = task:get_header_full(rule['header']) + match_list(rule, hv, {'decoded'}) + end, + rcpt = function() + if task:has_recipients('smtp') then + local rcpts = task:get_recipients('smtp') + match_addr(rule, rcpts) + end + end, + from = function() + if task:has_from('smtp') then + local from = task:get_from('smtp') + match_addr(rule, from) + end + end, + helo = function() + local helo = task:get_helo() + if helo then + match_hostname(rule, helo) + end + end, + url = function() + if task:has_urls() then + local msg_urls = task:get_urls() + for _,url in ipairs(msg_urls) do + match_url(rule, url) + end end - end - elseif rt == 'filename' then - local parts = task:get_parts() - for _,p in ipairs(parts) do - if p:is_archive() then - local fnames = p:get_archive():get_files() + end, + filename = function() + local parts = task:get_parts() + for _,p in ipairs(parts) do + if p:is_archive() then + local fnames = p:get_archive():get_files() - for _,fn in ipairs(fnames) do - match_filename(rule, fn) + for _,fn in ipairs(fnames) do + match_filename(rule, fn) + end end - end - local fn = p:get_filename() - if fn then - match_filename(rule, fn) + local fn = p:get_filename() + if fn then + match_filename(rule, fn) + end end - end - elseif rt == 'content' then - match_content(rule) - elseif rt == 'hostname' then - local hostname = task:get_hostname() - if hostname and hostname ~= 'unknown' then - match_hostname(rule, hostname) - end - elseif rt == 'asn' then - local asn = task:get_mempool():get_variable('asn') - if asn then - match_rule(rule, asn) - end - elseif rt == 'country' then - local country = task:get_mempool():get_variable('country') - if country then - match_rule(rule, country) - end - elseif rt == 'mempool' then - local var = task:get_mempool():get_variable(rule['variable']) - if var then - match_rule(rule, var) - end + end, + content = function() + match_content(rule) + end, + hostname = function() + local hostname = task:get_hostname() + if hostname and hostname ~= 'unknown' then + match_hostname(rule, hostname) + end + end, + asn = function() + local asn = task:get_mempool():get_variable('asn') + if asn then + match_rule(rule, asn) + end + end, + country = function() + local country = task:get_mempool():get_variable('country') + if country then + match_rule(rule, country) + end + end, + mempool = function() + local var = task:get_mempool():get_variable(rule['variable']) + if var then + match_rule(rule, var) + end + end, + } + process_rule_funcs.ip = process_rule_funcs.dnsbl + local f = process_rule_funcs[rt] + if f then + f() + else + rspamd_logger.errx(task, 'Unrecognised rule type: %s', rt) end end -- 2.39.5