commit 0b00f71: [Minor] Lua_util: Add callback_from_string routine

Vsevolod Stakhov vsevolod at highsecure.ru
Fri May 31 15:42:03 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-05-31 16:16:16 +0100
URL: https://github.com/rspamd/rspamd/commit/0b00f71a3fd29f6fb48e21eccf49345b26be8e83

[Minor] Lua_util: Add callback_from_string routine

---
 lualib/lua_util.lua | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

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


More information about the Commits mailing list