diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-11-23 13:18:34 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-11-23 16:10:28 +0000 |
commit | ee8a7f23d9b5b932a0acba5cf9ce319d222c3052 (patch) | |
tree | 3e2d7d957d5317b3fbb2bbaab0bad19741461d37 /src/lua/lua_common.c | |
parent | eacb870321833191b7fc876c05c45fb6b2eae711 (diff) | |
download | rspamd-ee8a7f23d9b5b932a0acba5cf9ce319d222c3052.tar.gz rspamd-ee8a7f23d9b5b932a0acba5cf9ce319d222c3052.zip |
[Minor] Add function to load redis server config in C using lua_redis
Diffstat (limited to 'src/lua/lua_common.c')
-rw-r--r-- | src/lua/lua_common.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index ab344e484..54c1dcad6 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -2343,3 +2343,64 @@ rspamd_lua_require_function (lua_State *L, const gchar *modname, return FALSE; } + + +gboolean +rspamd_lua_try_load_redis (lua_State *L, const ucl_object_t *obj, + struct rspamd_config *cfg, gint *ref_id) +{ + gint res_pos, err_idx; + struct rspamd_config **pcfg; + + /* Create results table */ + lua_createtable (L, 0, 0); + res_pos = lua_gettop (L); + lua_pushcfunction (L, &rspamd_lua_traceback); + err_idx = lua_gettop (L); + + /* Obtain function */ + if (!rspamd_lua_require_function (L, "lua_redis", "try_load_redis_servers")) { + msg_err_config ("cannot require lua_redis"); + lua_pop (L, 2); + + return FALSE; + } + + /* Function arguments */ + ucl_object_push_lua (L, obj, true); + pcfg = lua_newuserdata (L, sizeof (*pcfg)); + rspamd_lua_setclass (L, "rspamd{config}", -1); + *pcfg = cfg; + lua_pushvalue (L, res_pos); + + if (lua_pcall (L, 0, 1, err_idx) != 0) { + GString *tb; + + tb = lua_touserdata (L, -1); + msg_err_config ("cannot call lua try_load_redis_servers script: %s", tb->str); + g_string_free (tb, TRUE); + lua_settop (L, 0); + + return FALSE; + } + + if (lua_toboolean (L, -1)) { + if (ref_id) { + /* Ref table */ + lua_pushvalue (L, res_pos); + *ref_id = luaL_ref (L, LUA_REGISTRYINDEX); + lua_settop (L, 0); + } + else { + /* Leave it on the stack */ + lua_settop (L, res_pos); + } + + return TRUE; + } + else { + lua_settop (L, 0); + } + + return FALSE; +}
\ No newline at end of file |