]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Integrate selectors framework to multimap
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 17 Aug 2018 16:27:57 +0000 (17:27 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 17 Aug 2018 16:27:57 +0000 (17:27 +0100)
src/plugins/lua/multimap.lua

index 2898bfe4a21b868b1eee972169840a1140572988..8ba0a164d29c12c7bde557d3544677405c01f318 100644 (file)
@@ -28,6 +28,7 @@ local regexp = require "rspamd_regexp"
 local rspamd_expression = require "rspamd_expression"
 local rspamd_ip = require "rspamd_ip"
 local lua_util = require "lua_util"
+local lua_selectors = require "lua_selectors"
 local redis_params
 local fun = require "fun"
 local N = 'multimap'
@@ -80,6 +81,9 @@ local value_types = {
   mempool = {
     get_value = function(val) return val end,
   },
+  selector = {
+    get_value = function(val) return val end,
+  },
   symbol_options = {
     get_value = function(val) return val end,
   },
@@ -367,6 +371,7 @@ local multimap_filters = {
   url = apply_url_filter,
   filename = apply_filename_filter,
   mempool = apply_regexp_filter,
+  selector = apply_regexp_filter,
   hostname = apply_hostname_filter,
   --content = apply_content_filter, -- Content filters are special :(
 }
@@ -831,6 +836,12 @@ local function multimap_callback(task, rule)
         end
       end
     end,
+    selector = function()
+      local elts = lua_selectors.process_selectors(task, rule.selector) or {}
+      for _,elt in ipairs(elts) do
+        match_rule(rule, elt)
+      end
+    end,
   }
   process_rule_funcs.ip = process_rule_funcs.dnsbl
   local f = process_rule_funcs[rt]
@@ -873,6 +884,19 @@ local function add_multimap_rule(key, newrule)
     rspamd_logger.errx(rspamd_config, 'mempool map requires variable')
     return nil
   end
+  if newrule['type'] == 'selector' and not newrule['selector'] then
+    rspamd_logger.errx(rspamd_config, 'selector map requires selector definition')
+    return nil
+  else
+    local selector = lua_selectors.parse_selector(rspamd_config, newrule['selector'])
+
+    if not selector then
+      rspamd_logger.errx(rspamd_config, 'selector map has invalid selector')
+      return nil
+    end
+
+    newrule.selector = selector
+  end
   -- Check cdb flag
   if type(newrule['map']) == 'string' and string.find(newrule['map'], '^cdb://.*$') then
     newrule['cdb'] = cdb.create(newrule['map'])
@@ -985,17 +1009,18 @@ local function add_multimap_rule(key, newrule)
           end
         end
       elseif newrule['type'] == 'header'
-        or newrule['type'] == 'rcpt'
-        or newrule['type'] == 'from'
-        or newrule['type'] == 'helo'
-        or newrule['type'] == 'symbol_options'
-        or newrule['type'] == 'filename'
-        or newrule['type'] == 'url'
-        or newrule['type'] == 'content'
-        or newrule['type'] == 'hostname'
-        or newrule['type'] == 'asn'
-        or newrule['type'] == 'country'
-        or newrule['type'] == 'mempool' then
+          or newrule['type'] == 'rcpt'
+          or newrule['type'] == 'from'
+          or newrule['type'] == 'helo'
+          or newrule['type'] == 'symbol_options'
+          or newrule['type'] == 'filename'
+          or newrule['type'] == 'url'
+          or newrule['type'] == 'content'
+          or newrule['type'] == 'hostname'
+          or newrule['type'] == 'asn'
+          or newrule['type'] == 'country'
+          or newrule['type'] == 'mempool'
+          or newrule['type'] == 'selector'then
         if newrule['regexp'] then
           newrule['hash'] = rspamd_config:add_map ({
             url = newrule['map'],