]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Allow to specify `selector_alias` in the maps definition
authorVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 12 Dec 2022 21:33:58 +0000 (21:33 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 12 Dec 2022 21:33:58 +0000 (21:33 +0000)
lualib/lua_maps.lua
lualib/lua_selectors/init.lua

index c7c5a983d3203e86fd07de2f3d605fa34874cfba..a436a60f0b78404bf2c1eeac64ce70cb24b3f8d0 100644 (file)
@@ -247,6 +247,13 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
     return nil
   end
 
+  local function maybe_register_selector()
+    if opt.selector_alias then
+      local lua_selectors = require "lua_selectors"
+      lua_selectors.add_map(opt.selector_alias, ret)
+    end
+  end
+
   if type(opt) == 'string' then
     opt,mtype = maybe_adjust_type(opt, mtype)
     local cache_key = map_hash_key(opt, mtype)
@@ -303,6 +310,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             ret.__data = map
             setmetatable(ret, ret_mt)
             maps_cache[cache_key] = ret
+            maybe_register_selector()
+
             return ret
           end
         else
@@ -316,6 +325,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             ret.__data = map
             setmetatable(ret, ret_mt)
             maps_cache[cache_key] = ret
+            maybe_register_selector()
+
             return ret
           end
         end
@@ -331,6 +342,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             ret.__data = map
             setmetatable(ret, ret_mt)
             maps_cache[cache_key] = ret
+            maybe_register_selector()
+
             return ret
           end
         else
@@ -346,6 +359,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             ret.__data = map
             setmetatable(ret, ret_mt)
             maps_cache[cache_key] = ret
+            maybe_register_selector()
+
             return ret
           end
         end
@@ -361,6 +376,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             ret.__data = map
             setmetatable(ret, ret_mt)
             maps_cache[cache_key] = ret
+            maybe_register_selector()
+
             return ret
           end
         else
@@ -399,6 +416,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             end
 
             maps_cache[cache_key] = ret
+            maybe_register_selector()
+
             return ret
           else
             -- Empty map, huh?
@@ -418,6 +437,7 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             ret.__data = opt
             ret.__external = true
             setmetatable(ret, ret_mt)
+            maybe_register_selector()
 
             return ret
           else
@@ -439,6 +459,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
           ret.__data = map
           setmetatable(ret, ret_mt)
           maps_cache[cache_key] = ret
+          maybe_register_selector()
+
           return ret
         end
       end
@@ -539,6 +561,7 @@ end
 local direct_map_schema = ts.shape{ -- complex object
   name = ts.string:is_optional(),
   description = ts.string:is_optional(),
+  selector_alias = ts.string:is_optional(), -- an optional alias for the selectos framework
   timeout = ts.number,
   data = ts.array_of(ts.string):is_optional(),
   -- Tableshape has no options support for something like key1 or key2?
index f85b9a48773a5090e5df1e81aa64918b831d0f66..39cf654f3d91d16a9b6a9bf8ae719e664d329f75 100644 (file)
@@ -616,4 +616,12 @@ exports.list_transforms = function()
   return display_selectors(transform_function)
 end
 
+exports.add_map = function(name, map)
+  if not exports.maps[name] then
+    exports.maps[name] = map
+  else
+    logger.errx(rspamd_config, "duplicate map redefinition for the selectors: %s", name)
+  end
+end
+
 return exports