commit 79e6941: [Feature] Use maps logic from lua_maps for multimap

Vsevolod Stakhov vsevolod at highsecure.ru
Tue May 21 16:49:04 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-05-21 17:30:27 +0100
URL: https://github.com/rspamd/rspamd/commit/79e69412d0ca7cf644a946409b9673f8d5073023

[Feature] Use maps logic from lua_maps for multimap

---
 lualib/lua_maps.lua          |   3 +
 src/plugins/lua/multimap.lua | 166 ++++++++++++++-----------------------------
 2 files changed, 55 insertions(+), 114 deletions(-)

diff --git a/lualib/lua_maps.lua b/lualib/lua_maps.lua
index 7cbd9ae0d..6b1a37590 100644
--- a/lualib/lua_maps.lua
+++ b/lualib/lua_maps.lua
@@ -57,7 +57,10 @@ local function maybe_adjust_type(data,mtype)
   local known_types = {
     {'regexp;', 'regexp'},
     {'re;', 'regexp'},
+    {'regexp_multi;', 'regexp_multi'},
+    {'re_multi;', 'regexp_multi'},
     {'glob;', 'glob'},
+    {'glob_multi;', 'glob_multi'},
     {'radix;', 'radix'},
     {'ipnet;', 'radix'},
     {'set;', 'set'},
diff --git a/src/plugins/lua/multimap.lua b/src/plugins/lua/multimap.lua
index 9ee8f59c2..68f254c1c 100644
--- a/src/plugins/lua/multimap.lua
+++ b/src/plugins/lua/multimap.lua
@@ -29,12 +29,11 @@ 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 lua_maps = require "lua_maps"
 local redis_params
 local fun = require "fun"
 local N = 'multimap'
 
-local urls = {}
-
 local value_types = {
   ip = {
     get_value = function(ip) return ip:to_string() end,
@@ -918,38 +917,23 @@ local function add_multimap_rule(key, newrule)
   local function multimap_load_hash(rule)
     if rule['regexp'] then
       if rule['multi'] then
-        rule['hash'] = rspamd_config:add_map ({
-          url = rule['map'],
-          description = rule['description'],
-          type = 'regexp_multi'
-        })
+        rule.hash = lua_maps.map_add_from_ucl(rule.map, 'regexp_multi',
+            rule.description)
       else
-        rule['hash'] = rspamd_config:add_map ({
-          url = newrule['map'],
-          description = newrule['description'],
-          type = 'regexp'
-        })
+        rule.hash = lua_maps.map_add_from_ucl(rule.map, 'regexp',
+            rule.description)
       end
     elseif rule['glob'] then
       if rule['multi'] then
-        rule['hash'] = rspamd_config:add_map ({
-          url = rule['map'],
-          description = rule['description'],
-          type = 'glob_multi'
-        })
+        rule.hash = lua_maps.map_add_from_ucl(rule.map, 'glob_multi',
+            rule.description)
       else
-        rule['hash'] = rspamd_config:add_map ({
-          url = rule['map'],
-          description = rule['description'],
-          type = 'glob'
-        })
+        rule.hash = lua_maps.map_add_from_ucl(rule.map, 'glob',
+            rule.description)
       end
     else
-      rule['hash'] = rspamd_config:add_map ({
-        url = rule['map'],
-        description = rule['description'],
-        type = 'hash'
-      })
+      rule.hash = lua_maps.map_add_from_ucl(rule.map, 'hash',
+          rule.description)
     end
   end
 
@@ -1045,106 +1029,60 @@ local function add_multimap_rule(key, newrule)
       ret = true
     end
   else
-    if type(newrule['map']) == 'string' then
-      local map = urls[newrule['map']]
-      if map and map['regexp'] == newrule['regexp'] and
-          map['glob'] == newrule['glob'] then
-        if newrule['type'] == 'ip' or newrule['filter'] == 'ip_addr' then
-          newrule['radix'] = map['map']
-        else
-          newrule['hash'] = map['map']
-        end
-        rspamd_logger.infox(rspamd_config, 'reuse url for %s: "%s"',
-            newrule['symbol'], newrule['map'])
+    if newrule['type'] == 'ip' then
+      newrule['radix'] = lua_maps.map_add_from_ucl(newrule.map, 'radix',
+          newrule.description)
+      if newrule['radix'] then
         ret = true
+      else
+        rspamd_logger.warnx(rspamd_config, 'Cannot add rule: map doesn\'t exists: %1',
+            newrule['map'])
       end
-    end
-    if not ret then
-      if newrule['type'] == 'ip' then
-        newrule['radix'] = rspamd_config:add_map ({
-          url = newrule['map'],
-          description = newrule['description'],
-          type = 'radix'
-        })
+    elseif newrule['type'] == 'received' then
+      if type(newrule['flags']) == 'table' and newrule['flags'][1] then
+        newrule['flags'] = newrule['flags']
+      elseif type(newrule['flags']) == 'string' then
+        newrule['flags'] = {newrule['flags']}
+      end
+      if type(newrule['nflags']) == 'table' and newrule['nflags'][1] then
+        newrule['nflags'] = newrule['nflags']
+      elseif type(newrule['nflags']) == 'string' then
+        newrule['nflags'] = {newrule['nflags']}
+      end
+      local filter = newrule['filter'] or 'real_ip'
+      if filter == 'real_ip' or filter == 'from_ip' then
+        newrule['radix'] = lua_maps.map_add_from_ucl(newrule.map, 'radix',
+            newrule.description)
         if newrule['radix'] then
           ret = true
-          if type(newrule['map']) == 'string' then
-            urls[newrule['map']] = {
-              type = 'ip',
-              map = newrule['radix'],
-              regexp = false
-            }
-          end
-        else
-          rspamd_logger.warnx(rspamd_config, 'Cannot add rule: map doesn\'t exists: %1',
-              newrule['map'])
-        end
-      elseif newrule['type'] == 'received' then
-        if type(newrule['flags']) == 'table' and newrule['flags'][1] then
-          newrule['flags'] = newrule['flags']
-        elseif type(newrule['flags']) == 'string' then
-          newrule['flags'] = {newrule['flags']}
-        end
-        if type(newrule['nflags']) == 'table' and newrule['nflags'][1] then
-          newrule['nflags'] = newrule['nflags']
-        elseif type(newrule['nflags']) == 'string' then
-          newrule['nflags'] = {newrule['nflags']}
-        end
-        local filter = newrule['filter'] or 'real_ip'
-        if filter == 'real_ip' or filter == 'from_ip' then
-          newrule['radix'] = rspamd_config:add_map ({
-            url = newrule['map'],
-            description = newrule['description'],
-            type = 'radix'
-          })
-          if newrule['radix'] then
-            ret = true
-          end
-        else
-          multimap_load_hash(newrule)
-
-          if newrule['hash'] then
-            ret = true
-            if type(newrule['map']) == 'string' then
-              urls[newrule['map']] = {
-                type = newrule['type'],
-                map = newrule['hash'],
-                regexp = newrule['regexp']
-              }
-            end
-          else
-            rspamd_logger.warnx(rspamd_config, 'Cannot add rule: map doesn\'t exists: %1',
-                newrule['map'])
-          end
-        end
-      elseif known_generic_types[newrule.type] then
-
-        if newrule.filter == 'ip_addr' then
-          newrule['radix'] = rspamd_config:add_map ({
-            url = newrule['map'],
-            description = newrule['description'],
-            type = 'radix'
-          })
-        elseif not newrule.combined then
-          multimap_load_hash(newrule)
         end
+      else
+        multimap_load_hash(newrule)
 
-        if newrule.hash or newrule.radix then
+        if newrule['hash'] then
           ret = true
-          if type(newrule['map']) == 'string' then
-            urls[newrule['map']] = {
-              type = newrule['type'],
-              map = newrule.hash or newrule.radix,
-              regexp = newrule['regexp']
-            }
-          end
         else
           rspamd_logger.warnx(rspamd_config, 'Cannot add rule: map doesn\'t exists: %1',
               newrule['map'])
         end
-      elseif newrule['type'] == 'dnsbl' then
+      end
+    elseif known_generic_types[newrule.type] then
+
+      if newrule.filter == 'ip_addr' then
+        newrule['radix'] = lua_maps.map_add_from_ucl(newrule.map, 'radix',
+            newrule.description)
+      elseif not newrule.combined then
+        multimap_load_hash(newrule)
+      end
+
+      if newrule.hash or newrule.radix then
         ret = true
+      else
+        rspamd_logger.warnx(rspamd_config, 'Cannot add rule: map doesn\'t exists: %1',
+            newrule['map'])
       end
+    elseif newrule['type'] == 'dnsbl' then
+      ret = true
     end
   end
 


More information about the Commits mailing list