summaryrefslogtreecommitdiffstats
path: root/src/lua/lua_config.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2017-05-18 15:47:16 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2017-05-18 16:40:48 +0100
commitde8eb1d9a3b915fc932cc9f97581505ecefa47df (patch)
treee36ea9bbcb815481c28440fc2a4b89cfcde43e4c /src/lua/lua_config.c
parent96a56bb5e598fd8846c5eff20c1c78f4fbf7c51a (diff)
downloadrspamd-de8eb1d9a3b915fc932cc9f97581505ecefa47df.tar.gz
rspamd-de8eb1d9a3b915fc932cc9f97581505ecefa47df.zip
[Minor] Add method to get the whole configuration in Lua
Diffstat (limited to 'src/lua/lua_config.c')
-rw-r--r--src/lua/lua_config.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c
index 0eea492ca..cba81c2a6 100644
--- a/src/lua/lua_config.c
+++ b/src/lua/lua_config.c
@@ -55,6 +55,14 @@ LUA_FUNCTION_DEF (config, get_module_opt);
* @return {table} table of all options for `mname` or `nil` if a module's configuration is not found
*/
LUA_FUNCTION_DEF (config, get_all_opt);
+
+/***
+ * @method rspamd_config:get_ucl()
+ * Returns full configuration as a native Lua object (ucl to lua conversion).
+ * This method uses caching if possible.
+ * @return {table} table of all options in the configuration
+ */
+LUA_FUNCTION_DEF (config, get_ucl);
/***
* @method rspamd_config:get_mempool()
* Returns static configuration memory pool.
@@ -613,6 +621,7 @@ static const struct luaL_reg configlib_m[] = {
LUA_INTERFACE_DEF (config, get_mempool),
LUA_INTERFACE_DEF (config, get_resolver),
LUA_INTERFACE_DEF (config, get_all_opt),
+ LUA_INTERFACE_DEF (config, get_ucl),
LUA_INTERFACE_DEF (config, add_radix_map),
LUA_INTERFACE_DEF (config, radix_from_config),
LUA_INTERFACE_DEF (config, add_hash_map),
@@ -810,6 +819,48 @@ lua_config_get_all_opt (lua_State * L)
return 1;
}
+struct rspamd_lua_cached_config {
+ lua_State *L;
+ gint ref;
+};
+
+static void
+lua_config_ucl_dtor (gpointer p)
+{
+ struct rspamd_lua_cached_config *cached = p;
+
+ luaL_unref (cached->L, LUA_REGISTRYINDEX, cached->ref);
+}
+
+static gint
+lua_config_get_ucl (lua_State * L)
+{
+ struct rspamd_config *cfg = lua_check_config (L, 1);
+ struct rspamd_lua_cached_config *cached;
+
+ if (cfg) {
+ cached = rspamd_mempool_get_variable (cfg->cfg_pool, "ucl_cached");
+
+ if (cached) {
+ lua_rawgeti (L, LUA_REGISTRYINDEX, cached->ref);
+ }
+ else {
+ ucl_object_push_lua (L, cfg->rcl_obj, true);
+ lua_pushvalue (L, -1);
+ cached = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (*cached));
+ cached->L = L;
+ cached->ref = luaL_ref (L, LUA_REGISTRYINDEX);
+ rspamd_mempool_set_variable (cfg->cfg_pool, "ucl_cached",
+ cached, lua_config_ucl_dtor);
+ }
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ return 1;
+}
+
static gint
lua_config_get_classifier (lua_State * L)