]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Allow to add custom maps from ucl in Lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 9 Oct 2017 19:53:52 +0000 (20:53 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 9 Oct 2017 19:53:52 +0000 (20:53 +0100)
src/lua/lua_config.c
src/lua/lua_map.c
src/lua/lua_map.h

index f12b7504eb2f090d169690074a1361bcc2ced688..17eeeaf295321eb4a0a7872c37ad6d2c5bf66ed7 100644 (file)
@@ -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),
index 6300216de03a4dd7aba15d6bd4650ae1a15b8a7a..e01c1308d43c2d4bf73a38ab69c4174d63121aa1 100644 (file)
@@ -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)
 {
index 01a7a639f4895cc24801582fd66f63aef169be69..bee698e081e8c971c7600de45efd8b7dc5603515 100644 (file)
@@ -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);