]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Add per-domain emails normalisation rules
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 29 Jun 2017 18:13:49 +0000 (19:13 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 29 Jun 2017 18:13:49 +0000 (19:13 +0100)
lualib/lua_util.lua

index bdb0045d536ee6f2eb7185cf17bb6b521c2d7d05..457c2f7b2d99b2aa87f03cac9d6540f5a4892f48 100644 (file)
@@ -41,19 +41,37 @@ exports.template = function(tmpl, keys)
 end
 
 exports.remove_email_aliases = function(addr)
+  local function check_gmail_user(addr)
+    -- Remove all points
+    local no_dots_user = string.gsub(addr.user, '.', '')
+    local cap, pluses = string.match(no_dots_user, '^([^%+][^%+]*)(%+.*)$')
+    if cap then
+      return cap, rspamd_str_split(pluses, '+'), nil
+    elseif no_dots_user ~= addr.user then
+      return no_dots_user
+    end
+
+    return nil
+  end
+
   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, '+')
+        return cap, rspamd_str_split(pluses, '+'), nil
       end
     end
 
     return nil
   end
 
-  local function set_addr(addr, new_user)
-    addr.user = new_user
+  local function set_addr(addr, new_user, new_domain)
+    if new_user then
+      addr.user = new_user
+    end
+    if new_domain then
+      addr.domain = new_domain
+    end
 
     if addr.domain then
       addr.addr = string.format('%s@%s', addr.user, addr.domain)
@@ -68,12 +86,47 @@ exports.remove_email_aliases = function(addr)
     end
   end
 
-  if addr then
-    local nu, tags = check_address(addr)
+  local function check_gmail(addr)
+    local nu, tags, nd = check_gmail_user(addr)
+
+    if nu then
+      return nu, tags, nd
+    end
+
+    return nil
+  end
+
+  local function check_gmail(addr)
+    local nd = 'gmail.com'
+    local nu, tags = check_gmail_user(addr)
+
     if nu then
-      set_addr(addr, nu)
+      return nu, tags, nd
+    end
+
+    return nil, nil, nd
+  end
+
+  local specific_domains = {
+    ['gmail.com'] = check_gmail,
+    ['googlemail.com'] = check_googlemail,
+  }
 
-      return nu, tags
+  if addr then
+    if addr.domain and specific_domains[addr.domain] then
+      local nu, tags, nd = specific_domains[addr.domain](addr)
+      if nu or nd then
+        set_addr(addr, nu, nd)
+
+        return nu, tags
+      end
+    else
+      local nu, tags, nd = check_address(addr)
+      if nu or nd then
+        set_addr(addr, nu, nd)
+
+        return nu, tags
+      end
     end
 
     return nil