]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Add get_elts method
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 30 Nov 2019 10:10:50 +0000 (10:10 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 30 Nov 2019 10:10:50 +0000 (10:10 +0000)
src/lua/lua_spf.c

index 8a01b0ef78aee9fffcdf57a2e52be3b1dfa0aa93..a65ea00516a1ad7a1a02a70410d98d2f132fdbb6 100644 (file)
@@ -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