From 05ccc9c195c44da6fd2be6087fbca674f0df51b3 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 9 Oct 2017 20:53:52 +0100 Subject: [PATCH] [Minor] Allow to add custom maps from ucl in Lua --- src/lua/lua_config.c | 7 ++++++ src/lua/lua_map.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ src/lua/lua_map.h | 1 + 3 files changed, 61 insertions(+) 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 @@ -110,6 +110,12 @@ local function foo(task) return false 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. @@ -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); -- 2.39.5