From 5f52812464d4209b93c1665c242f2cd2c3576f60 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 29 Jun 2017 08:05:54 +0100 Subject: [PATCH] [Minor] Move emails normalization to the lua library --- lualib/lua_util.lua | 40 ++++++++++++++++++++++++++++++++++++++++ rules/misc.lua | 34 +++------------------------------- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index 1f53d51ed..bdb0045d5 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -40,4 +40,44 @@ exports.template = function(tmpl, keys) return lpeg.match(template_grammar, tmpl) end +exports.remove_email_aliases = function(addr) + local function check_address(addr) + if addr.user then + local cap, pluses = string.match(addr.user, '^([^%+][^%+]*)(%+.*)$') + if cap then + return cap, rspamd_str_split(pluses, '+') + end + end + + return nil + end + + local function set_addr(addr, new_user) + addr.user = new_user + + if addr.domain then + addr.addr = string.format('%s@%s', addr.user, addr.domain) + else + addr.addr = string.format('%s@', addr.user) + end + + if addr.name and #addr.name > 0 then + addr.raw = string.format('"%s" <%s>', addr.name, addr.addr) + else + addr.raw = string.format('<%s>', addr.addr) + end + end + + if addr then + local nu, tags = check_address(addr) + if nu then + set_addr(addr, nu) + + return nu, tags + end + + return nil + end +end + return exports diff --git a/rules/misc.lua b/rules/misc.lua index 1814be8ab..bc9b044eb 100644 --- a/rules/misc.lua +++ b/rules/misc.lua @@ -20,6 +20,7 @@ local E = {} local fun = require "fun" local util = require "rspamd_util" local rspamd_regexp = require "rspamd_regexp" +local rspamd_lua_utils = require "lua_util" -- Different text parts rspamd_config.R_PARTS_DIFFER = { @@ -409,39 +410,11 @@ local aliases_id = rspamd_config:register_symbol{ type = 'prefilter', name = 'EMAIL_PLUS_ALIASES', callback = function(task) - local function check_address(addr) - if addr.user then - local cap, pluses = string.match(addr.user, '^([^%+][^%+]*)(%+.*)$') - if cap then - return cap, rspamd_str_split(pluses, '+') - end - end - - return nil - end - - local function set_addr(addr, new_user) - addr.user = new_user - - if addr.domain then - addr.addr = string.format('%s@%s', addr.user, addr.domain) - else - addr.addr = string.format('%s@', addr.user) - end - - if addr.name and #addr.name > 0 then - addr.raw = string.format('"%s" <%s>', addr.name, addr.addr) - else - addr.raw = string.format('<%s>', addr.addr) - end - end - local function check_from(type) if task:has_from(type) then local addr = task:get_from(type)[1] - local na,tags = check_address(addr) + local na,tags = rspamd_lua_utils.remove_email_aliases(addr) if na then - set_addr(addr, na) task:set_from(type, addr) task:insert_result('TAGGED_FROM', 1.0, fun.totable( fun.filter(function(t) return t and #t > 0 end, tags))) @@ -459,9 +432,8 @@ local aliases_id = rspamd_config:register_symbol{ local addrs = task:get_recipients(type) for _, addr in ipairs(addrs) do - local na,tags = check_address(addr) + local na,tags = rspamd_lua_utils.remove_email_aliases(addr) if na then - set_addr(addr, na) modified = true fun.each(function(t) table.insert(all_tags, t) end, fun.filter(function(t) return t and #t > 0 end, tags)) -- 2.39.5