From: Vsevolod Stakhov Date: Wed, 27 Apr 2016 12:56:21 +0000 (+0100) Subject: [Feature] Allow to get settings and settings id hash from lua_task X-Git-Tag: 1.3.0~618 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=da867bccbf72f3063222a7fe56596a17f0ba5797;p=thirdparty%2Frspamd.git [Feature] Allow to get settings and settings id hash from lua_task --- diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index ca51dc8b58..49fda36561 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -478,6 +478,21 @@ LUA_FUNCTION_DEF (task, learn); */ LUA_FUNCTION_DEF (task, set_settings); +/*** + * @method task:get_settings() + * Gets users settings object for a task. The format of this object is described + * [here](https://rspamd.com/doc/configuration/settings.html). + * @return {lua object} lua object generated from UCL + */ +LUA_FUNCTION_DEF (task, get_settings); + +/*** + * @method task:get_settings_id() + * Get numeric hash of settings id if specified for this task. 0 is returned otherwise. + * @param {any} obj any lua object that corresponds to the settings format + */ +LUA_FUNCTION_DEF (task, get_settings_id); + /*** * @method task:process_re(params) * Processes the specified regexp and returns number of captures (cached or new) @@ -623,6 +638,8 @@ static const struct luaL_reg tasklib_m[] = { LUA_INTERFACE_DEF (task, get_metric_action), LUA_INTERFACE_DEF (task, learn), LUA_INTERFACE_DEF (task, set_settings), + LUA_INTERFACE_DEF (task, get_settings), + LUA_INTERFACE_DEF (task, get_settings_id), LUA_INTERFACE_DEF (task, cache_get), LUA_INTERFACE_DEF (task, cache_set), LUA_INTERFACE_DEF (task, process_regexp), @@ -2634,6 +2651,50 @@ lua_task_set_settings (lua_State *L) return 0; } +static gint +lua_task_get_settings (lua_State *L) +{ + struct rspamd_task *task = lua_check_task (L, 1); + + if (task != NULL) { + + if (task->settings) { + return ucl_object_push_lua (L, task->settings, true); + } + else { + lua_pushnil (L); + } + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + +static gint +lua_task_get_settings_id (lua_State *L) +{ + struct rspamd_task *task = lua_check_task (L, 1); + guint32 *hp; + + if (task != NULL) { + hp = rspamd_mempool_get_variable (task->task_pool, "settings_hash"); + + if (hp) { + lua_pushnumber (L, *hp); + } + else { + lua_pushnil (L); + } + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + static gint lua_task_cache_get (lua_State *L) {