commit ad9f078: [Project] Add get_elts method

Vsevolod Stakhov vsevolod at highsecure.ru
Sat Nov 30 10:21:07 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-11-30 10:10:50 +0000
URL: https://github.com/rspamd/rspamd/commit/ad9f078c81a1de0bf54895b38b2b3cfbb568438f (HEAD -> master)

[Project] Add get_elts method

---
 src/lua/lua_spf.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

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


More information about the Commits mailing list