From: Vsevolod Stakhov Date: Wed, 30 May 2018 08:58:14 +0000 (+0100) Subject: [Minor] Add method to load UCL into `rspamd_config` in Lua X-Git-Tag: 1.7.6~84 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=34786cedaa72545a372d47be695c432db442cf98;p=rspamd.git [Minor] Add method to load UCL into `rspamd_config` in Lua --- diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index 230c64b78..323957086 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -375,17 +375,6 @@ rspamd_lua_set_path (lua_State *L, const ucl_object_t *cfg_obj, GHashTable *vars lua_pop (L, 1); } -#define RSPAMD_CONFDIR_INDEX "CONFDIR" -#define RSPAMD_RUNDIR_INDEX "RUNDIR" -#define RSPAMD_DBDIR_INDEX "DBDIR" -#define RSPAMD_LOGDIR_INDEX "LOGDIR" -#define RSPAMD_PLUGINSDIR_INDEX "PLUGINSDIR" -#define RSPAMD_RULESDIR_INDEX "RULESDIR" -#define RSPAMD_LUALIBDIR_INDEX "LUALIBDIR" -#define RSPAMD_WWWDIR_INDEX "WWWDIR" -#define RSPAMD_PREFIX_INDEX "PREFIX" -#define RSPAMD_VERSION_INDEX "VERSION" - static gint rspamd_lua_cmp_version_components (const gchar *comp1, const gchar *comp2) { diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index b4a241412..6c64c76d4 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -404,5 +404,17 @@ void rspamd_lua_add_ref_dtor (lua_State *L, rspamd_mempool_t *pool, gboolean rspamd_lua_require_function (lua_State *L, const gchar *modname, const gchar *funcname); +/* Paths defs */ +#define RSPAMD_CONFDIR_INDEX "CONFDIR" +#define RSPAMD_RUNDIR_INDEX "RUNDIR" +#define RSPAMD_DBDIR_INDEX "DBDIR" +#define RSPAMD_LOGDIR_INDEX "LOGDIR" +#define RSPAMD_PLUGINSDIR_INDEX "PLUGINSDIR" +#define RSPAMD_RULESDIR_INDEX "RULESDIR" +#define RSPAMD_LUALIBDIR_INDEX "LUALIBDIR" +#define RSPAMD_WWWDIR_INDEX "WWWDIR" +#define RSPAMD_PREFIX_INDEX "PREFIX" +#define RSPAMD_VERSION_INDEX "VERSION" + #endif /* WITH_LUA */ #endif /* RSPAMD_LUA_H */ diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index 2802ab6a4..4ebb73d8b 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -680,6 +680,14 @@ LUA_FUNCTION_DEF (config, has_torch); */ LUA_FUNCTION_DEF (config, experimental_enabled); +/*** + * @method rspamd_config:load_ucl(filename) + * Loads config from the UCL file (but does not perform parsing using rcl) + * @param {string} filename file to load + * @return true or false + error message + */ +LUA_FUNCTION_DEF (config, load_ucl); + static const struct luaL_reg configlib_m[] = { LUA_INTERFACE_DEF (config, get_module_opt), LUA_INTERFACE_DEF (config, get_mempool), @@ -737,6 +745,7 @@ static const struct luaL_reg configlib_m[] = { LUA_INTERFACE_DEF (config, get_cpu_flags), LUA_INTERFACE_DEF (config, has_torch), LUA_INTERFACE_DEF (config, experimental_enabled), + LUA_INTERFACE_DEF (config, load_ucl), {"__tostring", rspamd_lua_class_tostring}, {"__newindex", lua_config_newindex}, {NULL, NULL} @@ -3212,6 +3221,72 @@ lua_config_experimental_enabled (lua_State *L) return 1; } +#define IDX_TO_HASH(idx) do { \ + lua_pushstring (L, (idx)); \ + lua_gettable (L, -2); \ + if (lua_isstring (L, -1)) { \ + g_hash_table_insert (paths, (idx), g_strdup (lua_tostring (L, -1))); \ + } \ + lua_pop (L, 1); \ +} while(0) + +static gint +lua_config_load_ucl (lua_State *L) +{ + struct rspamd_config *cfg = lua_check_config (L, 1); + const gchar *filename; + GHashTable *paths = g_hash_table_new_full (rspamd_str_hash, rspamd_str_equal, + NULL, g_free); + GError *err = NULL; + + if (cfg) { + if (lua_isstring (L, 2)) { + filename = lua_tostring (L, 2); + } + else { + filename = RSPAMD_CONFDIR "/rspamd.conf"; + } + + /* Convert rspamd_paths */ + lua_getglobal (L, "rspamd_paths"); + + if (lua_istable (L, -1)) { + IDX_TO_HASH(RSPAMD_CONFDIR_INDEX); + IDX_TO_HASH(RSPAMD_RUNDIR_INDEX); + IDX_TO_HASH(RSPAMD_DBDIR_INDEX); + IDX_TO_HASH(RSPAMD_LOGDIR_INDEX); + IDX_TO_HASH(RSPAMD_WWWDIR_INDEX); + IDX_TO_HASH(RSPAMD_PLUGINSDIR_INDEX); + IDX_TO_HASH(RSPAMD_RULESDIR_INDEX); + IDX_TO_HASH(RSPAMD_LUALIBDIR_INDEX); + IDX_TO_HASH(RSPAMD_PREFIX_INDEX); + } + + lua_pop (L, 1); + + if (!rspamd_config_parse_ucl (cfg, filename, paths, &err)) { + lua_pushboolean (L, false); + lua_pushfstring (L, "failed to load config: %s", err->message); + g_error_free (err); + g_hash_table_unref (paths); + + return 2; + } + + rspamd_rcl_maybe_apply_lua_transform (cfg); + rspamd_config_calculate_cksum (cfg); + } + else { + return luaL_error (L, "invalid arguments"); + } + + g_hash_table_unref (paths); + lua_pushboolean (L, true); + + return 1; +} + +#undef IDX_TO_HASH static gint lua_monitored_alive (lua_State *L)