aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_common.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-01-25 11:23:35 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-01-25 11:23:35 +0000
commitbe47c345d50b355e890400fccf4437ac82ceccb8 (patch)
tree32ea8ab14c901db77a1aa743b97ae616441c8cc8 /src/lua/lua_common.c
parent10287f4f6fca92b3e8bba803abec24bac175629f (diff)
downloadrspamd-be47c345d50b355e890400fccf4437ac82ceccb8.tar.gz
rspamd-be47c345d50b355e890400fccf4437ac82ceccb8.zip
Move internet address pushing to common routines
Diffstat (limited to 'src/lua/lua_common.c')
-rw-r--r--src/lua/lua_common.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c
index e475ace83..2c4e913f6 100644
--- a/src/lua/lua_common.c
+++ b/src/lua/lua_common.c
@@ -963,3 +963,99 @@ rspamd_lua_table_size (lua_State *L, gint tbl_pos)
return tbl_size;
}
+
+gboolean
+lua_push_internet_address (lua_State *L, InternetAddress *ia)
+{
+ const char *addr, *at;
+
+#ifndef GMIME24
+ if (internet_address_get_type (ia) == INTERNET_ADDRESS_NAME) {
+ lua_newtable (L);
+ addr = internet_address_get_addr (ia);
+ rspamd_lua_table_set (L, "name", internet_address_get_name (ia));
+ rspamd_lua_table_set (L, "addr", addr);
+
+ if (addr) {
+ at = strchr (addr, '@');
+ if (at != NULL) {
+ lua_pushstring(L, "user");
+ lua_pushlstring(L, addr, at - addr);
+ lua_settable (L, -3);
+ lua_pushstring (L, "domain");
+ lua_pushstring (L, at + 1);
+ lua_settable (L, -3);
+ }
+ }
+
+ return TRUE;
+ }
+ return FALSE;
+#else
+ InternetAddressMailbox *iamb;
+
+ if (ia && INTERNET_ADDRESS_IS_MAILBOX (ia)) {
+ lua_newtable (L);
+ iamb = INTERNET_ADDRESS_MAILBOX (ia);
+ addr = internet_address_mailbox_get_addr (iamb);
+
+ if (addr) {
+ rspamd_lua_table_set (L, "name", internet_address_get_name (ia));
+ rspamd_lua_table_set (L, "addr", addr);
+ /* Set optional fields */
+
+ at = strchr (addr, '@');
+ if (at != NULL) {
+ lua_pushstring(L, "user");
+ lua_pushlstring(L, addr, at - addr);
+ lua_settable (L, -3);
+ lua_pushstring (L, "domain");
+ lua_pushstring (L, at + 1);
+ lua_settable (L, -3);
+ }
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+#endif
+}
+
+/*
+ * Push internet addresses to lua as a table
+ */
+void
+lua_push_internet_address_list (lua_State *L, InternetAddressList *addrs)
+{
+ InternetAddress *ia;
+ gint idx = 1;
+
+#ifndef GMIME24
+ /* Gmime 2.2 version */
+ InternetAddressList *cur;
+
+ lua_newtable (L);
+ cur = addrs;
+ while (cur) {
+ ia = internet_address_list_get_address (cur);
+ if (lua_push_internet_address (L, ia)) {
+ lua_rawseti (L, -2, idx++);
+ }
+ cur = internet_address_list_next (cur);
+ }
+#else
+ /* Gmime 2.4 version */
+ gsize len, i;
+
+ lua_newtable (L);
+ if (addrs != NULL) {
+ len = internet_address_list_length (addrs);
+ for (i = 0; i < len; i++) {
+ ia = internet_address_list_get_address (addrs, i);
+ if (lua_push_internet_address (L, ia)) {
+ lua_rawseti (L, -2, idx++);
+ }
+ }
+ }
+#endif
+}