]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Support filters for hostnames in multimap 775/head
authorAndrew Lewis <nerf@judo.za.org>
Sun, 31 Jul 2016 20:52:32 +0000 (22:52 +0200)
committerAndrew Lewis <nerf@judo.za.org>
Sun, 31 Jul 2016 20:52:32 +0000 (22:52 +0200)
src/plugins/lua/multimap.lua

index 56ea68214f6643d227bc0b2702cfef9e304bd0b7..506fd44557366b6635f998baa4d8fc5daec1ed49 100644 (file)
@@ -221,6 +221,33 @@ local function multimap_callback(task, rule)
     return ret
   end
 
+  local function apply_hostname_filter(filter, hostname, r)
+    if filter == 'tld' then
+      local tld = util.get_tld(hostname)
+      return tld
+    else
+      if not r['re_filter'] then
+        local pat = string.match(filter, 'tld:regexp:(.+)')
+        if not pat then
+          rspamd_logger.errx(task, 'bad search filter: %s', filter)
+          return
+        end
+        r['re_filter'] = regexp.create(pat)
+        if not r['re_filter'] then
+          rspamd_logger.errx(task, 'couldnt create regex: %s', pat)
+          return
+        end
+      end
+      local tld = util.get_tld(hostname)
+      local res = r['re_filter']:search(tld)
+      if res then
+        return res[1]
+      else
+        return nil
+      end
+    end
+  end
+
   local function apply_url_filter(filter, url, r)
     if filter == 'tld' then
       return url:get_tld()
@@ -312,6 +339,27 @@ local function multimap_callback(task, rule)
     end
   end
 
+  local function match_hostname(r, hostname)
+    local value
+    local ret = false
+
+    if r['filter'] then
+      value = apply_hostname_filter(r['filter'], hostname, r)
+    else
+      value = hostname
+    end
+
+    ret = match_element(r, value)
+
+    if ret then
+      task:insert_result(r['symbol'], 1)
+
+      if pre_filter then
+        task:set_pre_result(r['action'], 'Matched map: ' .. r['symbol'])
+      end
+    end
+  end
+
   local function apply_filename_filter(filter, fn, r)
     if filter == 'extension' or filter == 'ext' then
       return string.match(fn, '%.([^.]+)$')
@@ -488,7 +536,9 @@ local function multimap_callback(task, rule)
     match_content(rule)
   elseif rt == 'hostname' then
     local hostname = task:get_hostname()
-    match_rule(rule, hostname)
+    if hostname ~= 'unknown' then
+      match_hostname(rule, hostname)
+    end
   end
 end