summaryrefslogtreecommitdiffstats
path: root/src/lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-11-24 11:34:42 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-11-24 11:35:08 +0000
commitadce4f38a87bf1db5af13c3ad301429f2483c956 (patch)
tree386eafe4dce6e500af82f7f6e87440842b40918d /src/lua
parent30d8bbafc550e8f57a0d220afa7cfb220d310adb (diff)
downloadrspamd-adce4f38a87bf1db5af13c3ad301429f2483c956.tar.gz
rspamd-adce4f38a87bf1db5af13c3ad301429f2483c956.zip
[Feature] Add generic tool to add universal maps for lua modules
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/global_functions.lua79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/lua/global_functions.lua b/src/lua/global_functions.lua
index 31ee6c8c4..f335caf40 100644
--- a/src/lua/global_functions.lua
+++ b/src/lua/global_functions.lua
@@ -386,3 +386,82 @@ function rspamd_count_metatokens()
return total
end
+
+function rspamd_map_add(mname, optname, mtype, description)
+ local ret = {
+ get_key = function(t, k)
+ if t.__data then
+ return t.__data:get_key(k)
+ end
+
+ return nil
+ end
+ }
+ local ret_mt = {
+ __index = function(t, k)
+ if t.__data then
+ return t.get_key(k)
+ end
+
+ return nil
+ end
+ }
+ local opt = rspamd_config:get_module_opt(mname, optname)
+
+ if not opt then
+ return nil
+ end
+
+ if type(opt) == 'string' then
+ -- We have a single string, so we treat it as a map
+ local map = rspamd_config:add_map{
+ type = mtype,
+ description = description,
+ url = opt,
+ }
+
+ if map then
+ ret.__data = map
+ setmetatable(ret, ret_mt)
+ return ret
+ end
+ elseif type(opt) == 'table' then
+ -- it might be plain map or map of plain elements
+ if opt[1] then
+ if mtype == 'radix' then
+ local map = rspamd_config:radix_from_config(mname, optname)
+
+ if map then
+ ret.__data = map
+ setmetatable(ret, ret_mt)
+ return ret
+ end
+ else
+ local data = {}
+ for _,elt in ipairs(opt) do
+ if type(elt) == 'string' then
+ table.insert(data, tostring(elt))
+ end
+ end
+
+ ret.__data = data
+ ret.get_key = function(t, k)
+ return t.__data[k]
+ end
+ end
+ else
+ local map = rspamd_config:add_map{
+ type = mtype,
+ description = description,
+ url = opt,
+ }
+ if map then
+ ret.__data = map
+ setmetatable(ret, ret_mt)
+ return ret
+ end
+ end
+ end
+
+ return nil
+end