From ad9f078c81a1de0bf54895b38b2b3cfbb568438f Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 30 Nov 2019 10:10:50 +0000 Subject: [Project] Add get_elts method --- src/lua/lua_spf.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'src/lua/lua_spf.c') diff --git a/src/lua/lua_spf.c b/src/lua/lua_spf.c index 8a01b0ef7..a65ea0051 100644 --- a/src/lua/lua_spf.c +++ b/src/lua/lua_spf.c @@ -452,6 +452,64 @@ lua_spf_record_get_digest (lua_State *L) return 1; } +/*** + * @method rspamd_spf_record:get_elts() + * Returns a list of all elements in an SPF record. Each element is a table with the + * following fields: + * + * - result - mech flag from rspamd_spf.results + * - flags - all flags + * - addr - address and mask as a string + * - str - string representation (if available) +*/ +static gint +lua_spf_record_get_elts (lua_State *L) +{ + struct spf_resolved *record = + *(struct spf_resolved **) rspamd_lua_check_udata (L, 1, + SPF_RECORD_CLASS); + + if (record) { + guint i; + struct spf_addr *addr; + + lua_createtable (L, record->elts->len, 0); + + for (i = 0; i < record->elts->len; i ++) { + gchar *addr_mask; + + addr = (struct spf_addr *)&g_array_index (record->elts, + struct spf_addr, i); + lua_createtable (L, 0, 4); + + lua_pushinteger (L, addr->mech); + lua_setfield (L, -2, "result"); + lua_pushinteger (L, addr->flags); + lua_setfield (L, -2, "flags"); + + if (addr->spf_string) { + lua_pushstring (L, addr->spf_string); + lua_setfield (L, -2, "str"); + } + + addr_mask = spf_addr_mask_to_string (addr); + + if (addr_mask) { + lua_pushstring (L, addr_mask); + lua_setfield (L, -2, "addr"); + g_free (addr_mask); + } + + lua_rawseti (L, -2, i + 1); + } + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + /*** * @function rspamd_spf.config(object) * Configures SPF library according to the UCL config -- cgit v1.2.3