From be47c345d50b355e890400fccf4437ac82ceccb8 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 25 Jan 2016 11:23:35 +0000 Subject: [PATCH] Move internet address pushing to common routines --- src/lua/lua_common.c | 96 ++++++++++++++++++++++++++++++++++++++++++++ src/lua/lua_common.h | 15 +++++++ src/lua/lua_task.c | 79 ------------------------------------ 3 files changed, 111 insertions(+), 79 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 +} diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index fd207134a..62ac54f3d 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -296,5 +296,20 @@ gint rspamd_lua_traceback (lua_State *L); * Returns size of table at position `tbl_pos` */ guint rspamd_lua_table_size (lua_State *L, gint tbl_pos); + +/** + * Pushes a single InternetAddress as lua table + * @param L + * @param addrs + */ +void lua_push_internet_address_list (lua_State *L, InternetAddressList *addrs); + +/** + * Pushes internet address list to Lua as table of tables + * @param L + * @param ia + * @return + */ +gboolean lua_push_internet_address (lua_State *L, InternetAddress *ia); #endif /* WITH_LUA */ #endif /* RSPAMD_LUA_H */ diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 2980aba9b..0dbf8dc0d 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -1250,85 +1250,6 @@ lua_task_inc_dns_req (lua_State *L) return 0; } -static gboolean -lua_push_internet_address (lua_State *L, InternetAddress *ia) -{ -#ifndef GMIME24 - if (internet_address_get_type (ia) == INTERNET_ADDRESS_NAME) { - lua_newtable (L); - rspamd_lua_table_set (L, "name", internet_address_get_name (ia)); - rspamd_lua_table_set (L, "addr", internet_address_get_addr (ia)); - return TRUE; - } - return FALSE; -#else - InternetAddressMailbox *iamb; - const char *addr, *at; - 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 - */ -static 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 -} - /* * Convert element at the specified position to the type * for get_from/get_recipients -- 2.39.5