aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2017-10-09 20:53:52 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2017-10-09 20:53:52 +0100
commit05ccc9c195c44da6fd2be6087fbca674f0df51b3 (patch)
treeb1e763193bb30275119be3bc926de16a347297e3 /src
parent2e2c66f6be03c03de7696918cc222047c70e9747 (diff)
downloadrspamd-05ccc9c195c44da6fd2be6087fbca674f0df51b3.tar.gz
rspamd-05ccc9c195c44da6fd2be6087fbca674f0df51b3.zip
[Minor] Allow to add custom maps from ucl in Lua
Diffstat (limited to 'src')
-rw-r--r--src/lua/lua_config.c7
-rw-r--r--src/lua/lua_map.c53
-rw-r--r--src/lua/lua_map.h1
3 files changed, 61 insertions, 0 deletions
diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c
index f12b7504e..17eeeaf29 100644
--- a/src/lua/lua_config.c
+++ b/src/lua/lua_config.c
@@ -111,6 +111,12 @@ local function foo(task)
end
*/
/***
+* @method rspamd_config:radix_from_ucl(obj)
+* Creates new embedded map of IP/mask addresses from object.
+* @param {ucl} obj object
+* @return {map} radix tree object
+*/
+/***
* @method rspamd_config:add_hash_map(mapline[, description])
* Creates new dynamic map string objects.
* @param {string} mapline URL for a map
@@ -655,6 +661,7 @@ static const struct luaL_reg configlib_m[] = {
LUA_INTERFACE_DEF (config, get_ucl),
LUA_INTERFACE_DEF (config, add_radix_map),
LUA_INTERFACE_DEF (config, radix_from_config),
+ LUA_INTERFACE_DEF (config, radix_from_ucl),
LUA_INTERFACE_DEF (config, add_hash_map),
LUA_INTERFACE_DEF (config, add_kv_map),
LUA_INTERFACE_DEF (config, add_map),
diff --git a/src/lua/lua_map.c b/src/lua/lua_map.c
index 6300216de..e01c1308d 100644
--- a/src/lua/lua_map.c
+++ b/src/lua/lua_map.c
@@ -17,6 +17,7 @@
#include "lua_common.h"
#include "libutil/map.h"
#include "libutil/map_private.h"
+#include "contrib/libucl/lua_ucl.h"
/***
* This module is used to manage rspamd maps and map like objects
@@ -210,6 +211,58 @@ lua_config_radix_from_config (lua_State *L)
return 1;
}
+
+gint
+lua_config_radix_from_ucl (lua_State *L)
+{
+ struct rspamd_config *cfg = lua_check_config (L, 1);
+ ucl_object_t *obj;
+ struct rspamd_lua_map *map, **pmap;
+ ucl_object_t *fake_obj;
+ struct rspamd_map *m;
+
+ if (!cfg) {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ obj = ucl_object_lua_import (L, 2);
+
+ if (obj) {
+ map = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*map));
+ map->data.radix = NULL;
+ map->type = RSPAMD_LUA_MAP_RADIX;
+
+ fake_obj = ucl_object_typed_new (UCL_OBJECT);
+ ucl_object_insert_key (fake_obj, ucl_object_ref (obj),
+ "data", 0, false);
+ ucl_object_insert_key (fake_obj, ucl_object_fromstring ("static"),
+ "url", 0, false);
+
+ if ((m = rspamd_map_add_from_ucl (cfg, fake_obj, "static radix map",
+ rspamd_radix_read,
+ rspamd_radix_fin,
+ (void **)&map->data.radix)) == NULL) {
+ msg_err_config ("invalid radix map static");
+ lua_pushnil (L);
+ ucl_object_unref (fake_obj);
+
+ return 1;
+ }
+
+ ucl_object_unref (fake_obj);
+ pmap = lua_newuserdata (L, sizeof (void *));
+ map->map = m;
+ *pmap = map;
+ rspamd_lua_setclass (L, "rspamd{map}", -1);
+
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ return 1;
+}
+
gint
lua_config_add_hash_map (lua_State *L)
{
diff --git a/src/lua/lua_map.h b/src/lua/lua_map.h
index 01a7a639f..bee698e08 100644
--- a/src/lua/lua_map.h
+++ b/src/lua/lua_map.h
@@ -20,6 +20,7 @@
LUA_PUBLIC_FUNCTION_DEF (config, add_radix_map);
LUA_PUBLIC_FUNCTION_DEF (config, radix_from_config);
+LUA_PUBLIC_FUNCTION_DEF (config, radix_from_ucl);
LUA_PUBLIC_FUNCTION_DEF (config, add_map);
LUA_PUBLIC_FUNCTION_DEF (config, add_hash_map);
LUA_PUBLIC_FUNCTION_DEF (config, add_kv_map);