From 0b00f71a3fd29f6fb48e21eccf49345b26be8e83 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 31 May 2019 16:16:16 +0100 Subject: [PATCH] [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 -- 2.39.5