]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Add function to load redis server config in C using lua_redis
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 23 Nov 2018 13:18:34 +0000 (13:18 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 23 Nov 2018 16:10:28 +0000 (16:10 +0000)
src/lua/lua_common.c
src/lua/lua_common.h

index ab344e4849e7a5a2e753a11faf7349fdf27775c5..54c1dcad6e0223930980df2faf69a08cb1fbee95 100644 (file)
@@ -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
index fccbf51150538d328437245b4ff8c91196fa5c86..2dee888eeb11d6ab2bf7b5896220b299706bf416 100644 (file)
@@ -423,6 +423,16 @@ 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);
 
+/**
+ * Tries to load redis server definition from ucl object specified
+ * @param L
+ * @param obj
+ * @param cfg
+ * @return
+ */
+gboolean rspamd_lua_try_load_redis (lua_State *L, const ucl_object_t *obj,
+               struct rspamd_config *cfg, gint *ref_id);
+
 /* Paths defs */
 #define RSPAMD_CONFDIR_INDEX "CONFDIR"
 #define RSPAMD_LOCAL_CONFDIR_INDEX "LOCAL_CONFDIR"