summaryrefslogtreecommitdiffstats
path: root/lualib
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2021-07-29 15:54:06 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2021-07-29 15:54:06 +0100
commit293fe598e44b4e9d620a3c49a40d0a693a56e826 (patch)
treed3c592de6beaf05a8ca3859e3805cb57594fd788 /lualib
parent27d54da3f6c7801edea15cdf9d00f9956f7f87bf (diff)
downloadrspamd-293fe598e44b4e9d620a3c49a40d0a693a56e826.tar.gz
rspamd-293fe598e44b4e9d620a3c49a40d0a693a56e826.zip
[Feature] Add lua_maps.fill_config_maps function
Diffstat (limited to 'lualib')
-rw-r--r--lualib/lua_maps.lua37
1 files changed, 37 insertions, 0 deletions
diff --git a/lualib/lua_maps.lua b/lualib/lua_maps.lua
index a3dc88053..f4b64173d 100644
--- a/lualib/lua_maps.lua
+++ b/lualib/lua_maps.lua
@@ -349,6 +349,43 @@ end
exports.rspamd_maybe_check_map = rspamd_maybe_check_map
+--[[[
+-- @function lua_maps.fill_config_maps(mname, options, defs)
+-- Fill maps that could be defined in defs, from the config in the options
+-- Defs is a table indexed by a map's parameter name and defining it's config,
+-- for example:
+defs = {
+ my_map = {
+ type = 'map',
+ description = 'my cool map',
+ optional = true,
+ }
+}
+-- Then this function will look for opts.my_map parameter and try to replace it's with
+-- a map with the specific type, description but not failing if it was empty.
+-- It will also set options.my_map_orig to the original value defined in the map
+--]]
+exports.fill_config_maps = function(mname, opts, map_defs)
+ assert(type(opts) == 'table')
+ assert(type(map_defs) == 'table')
+ for k, v in pairs(map_defs) do
+ if opts[k] then
+ local map = rspamd_map_add_from_ucl(opts[k], v.type or 'map', v.description)
+ if not map then
+ rspamd_logger.errx(rspamd_config, 'map add error %s for module %s', k, mname)
+ return false
+ end
+ opts[k..'_orig'] = opts[k]
+ opts[k] = map
+ elseif not v.optional then
+ rspamd_logger.errx(rspamd_config, 'cannot find non optional map %s for module %s', k, mname)
+ return false
+ end
+ end
+
+ return true
+end
+
exports.map_schema = ts.one_of{
ts.string, -- 'http://some_map'
ts.array_of(ts.string), -- ['foo', 'bar']