From 9d99bef1243389742c3071801192e16fb90d8995 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 16 Dec 2017 15:02:28 +0000 Subject: [PATCH] [Minor] Allow to get all upstreams from the list in Lua --- src/lua/lua_upstream.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/lua/lua_upstream.c b/src/lua/lua_upstream.c index 84d112ef1..066183187 100644 --- a/src/lua/lua_upstream.c +++ b/src/lua/lua_upstream.c @@ -52,6 +52,7 @@ end /* Upstream list functions */ LUA_FUNCTION_DEF (upstream_list, create); LUA_FUNCTION_DEF (upstream_list, destroy); +LUA_FUNCTION_DEF (upstream_list, all_upstreams); LUA_FUNCTION_DEF (upstream_list, get_upstream_by_hash); LUA_FUNCTION_DEF (upstream_list, get_upstream_round_robin); LUA_FUNCTION_DEF (upstream_list, get_upstream_master_slave); @@ -61,6 +62,7 @@ static const struct luaL_reg upstream_list_m[] = { LUA_INTERFACE_DEF (upstream_list, get_upstream_by_hash), LUA_INTERFACE_DEF (upstream_list, get_upstream_round_robin), LUA_INTERFACE_DEF (upstream_list, get_upstream_master_slave), + LUA_INTERFACE_DEF (upstream_list, all_upstreams), {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_upstream_list_destroy}, {NULL, NULL} @@ -317,6 +319,39 @@ lua_upstream_list_get_upstream_master_slave (lua_State *L) return 1; } +static void lua_upstream_inserter (struct upstream *up, guint idx, void *ud) +{ + struct upstream **pup; + lua_State *L = (lua_State *)ud; + + pup = lua_newuserdata (L, sizeof (struct upstream *)); + rspamd_lua_setclass (L, "rspamd{upstream}", -1); + *pup = up; + + lua_rawseti (L, -2, idx + 1); +} +/*** + * @method upstream_list:all_upstreams() + * Returns all upstreams for this list + * @return {table|upstream} all upstreams defined + */ +static gint +lua_upstream_list_all_upstreams (lua_State *L) +{ + struct upstream_list *upl; + + upl = lua_check_upstream_list (L); + if (upl) { + lua_createtable (L, rspamd_upstreams_count (upl), 0); + rspamd_upstreams_foreach (upl, lua_upstream_inserter, L); + } + else { + lua_pushnil (L); + } + + return 1; +} + static gint lua_load_upstream_list (lua_State * L) { -- 2.39.5