summaryrefslogtreecommitdiffstats
path: root/src/lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-08-28 15:32:11 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-08-28 15:32:11 +0100
commita490aeb16da110d340a56a80434e5483550f23bd (patch)
tree6b57dcb2a1645c3538c05b7e22da5dfc4dbaf6a5 /src/lua
parent42138e70dfde9b74df304332a7473788a4089da1 (diff)
downloadrspamd-a490aeb16da110d340a56a80434e5483550f23bd.tar.gz
rspamd-a490aeb16da110d340a56a80434e5483550f23bd.zip
[Feature] Add ability to lookup settings by key
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/lua_task.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index 0adb5432a..a0f1a1a11 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -532,6 +532,14 @@ LUA_FUNCTION_DEF (task, set_settings);
LUA_FUNCTION_DEF (task, get_settings);
/***
+ * @method task:lookup_settings(key)
+ * Gets users settings object with the specified key for a task.
+ * @param {string} key key to lookup
+ * @return {lua object} lua object generated from UCL
+ */
+LUA_FUNCTION_DEF (task, lookup_settings);
+
+/***
* @method task:get_settings_id()
* Get numeric hash of settings id if specified for this task. 0 is returned otherwise.
* @return {number} settings-id hash
@@ -728,6 +736,7 @@ static const struct luaL_reg tasklib_m[] = {
LUA_INTERFACE_DEF (task, learn),
LUA_INTERFACE_DEF (task, set_settings),
LUA_INTERFACE_DEF (task, get_settings),
+ LUA_INTERFACE_DEF (task, lookup_settings),
LUA_INTERFACE_DEF (task, get_settings_id),
LUA_INTERFACE_DEF (task, cache_get),
LUA_INTERFACE_DEF (task, cache_set),
@@ -2915,6 +2924,45 @@ lua_task_get_settings (lua_State *L)
}
static gint
+lua_task_lookup_settings (lua_State *L)
+{
+ struct rspamd_task *task = lua_check_task (L, 1);
+ const gchar *key = NULL;
+ const ucl_object_t *elt;
+
+ if (task != NULL) {
+
+ if (lua_isstring (L, 2)) {
+ key = lua_tostring (L, 2);
+ }
+
+ if (task->settings) {
+ if (key == NULL) {
+ return ucl_object_push_lua (L, task->settings, true);
+ }
+ else {
+ elt = ucl_object_lookup (task->settings, key);
+
+ if (elt) {
+ return ucl_object_push_lua (L, elt, true);
+ }
+ else {
+ lua_pushnil (L);
+ }
+ }
+ }
+ 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);