aboutsummaryrefslogtreecommitdiffstats
path: root/lualib/lua_util.lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2019-05-31 16:16:16 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2019-05-31 16:16:16 +0100
commit0b00f71a3fd29f6fb48e21eccf49345b26be8e83 (patch)
tree1c1bf6a3c9edbc0b6fb9c093b4391581227d08fb /lualib/lua_util.lua
parentcc7f49b369afad5a061603f73ce1445cafec07fa (diff)
downloadrspamd-0b00f71a3fd29f6fb48e21eccf49345b26be8e83.tar.gz
rspamd-0b00f71a3fd29f6fb48e21eccf49345b26be8e83.zip
[Minor] Lua_util: Add callback_from_string routine
Diffstat (limited to 'lualib/lua_util.lua')
-rw-r--r--lualib/lua_util.lua20
1 files changed, 20 insertions, 0 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua
index 3e443bce3..3d1a2e732 100644
--- a/lualib/lua_util.lua
+++ b/lualib/lua_util.lua
@@ -968,4 +968,24 @@ exports.maybe_obfuscate_string = function(subject, settings, prefix)
return subject
end
+---[[[
+-- @function lua_util.callback_from_string(str)
+-- Converts a string like `return function(...) end` to lua function or emits error using
+-- `rspamd_config` superglobal
+-- @return function object or nil
+--]]]
+exports.callback_from_string = function(str)
+ local loadstring = loadstring or load
+ local ret, res_or_err = pcall(loadstring(str))
+
+ if not ret or type(res_or_err) ~= 'function' then
+ rspamd_logger.errx(rspamd_config, 'invalid callback (%s) - must be a function',
+ res_or_err)
+
+ return nil
+ end
+
+ return res_or_err
+end
+
return exports