From 1a67a8f93ea662fde33e7e14493ca81f554891ae Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 25 Aug 2015 12:38:38 +0100 Subject: [PATCH] Add util.parse_addr function. --- src/lua/lua_util.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 57b93d12f..9fc71d86a 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -95,6 +95,18 @@ LUA_FUNCTION_DEF (util, parse_html); */ LUA_FUNCTION_DEF (util, levenshtein_distance); +/*** + * @function util.parse_addr(str) + * Parse rfc822 address to components. Returns a table of components: + * + * - `name`: name of address (e.g. Some User) + * - `addr`: address part (e.g. user@example.com) + * + * @param {string} str input string + * @return {table} resulting table of components + */ +LUA_FUNCTION_DEF (util, parse_addr); + static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, create_event_base), LUA_INTERFACE_DEF (util, load_rspamd_config), @@ -106,6 +118,7 @@ static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, tanh), LUA_INTERFACE_DEF (util, parse_html), LUA_INTERFACE_DEF (util, levenshtein_distance), + LUA_INTERFACE_DEF (util, parse_addr), {NULL, NULL} }; @@ -529,6 +542,50 @@ lua_util_levenshtein_distance (lua_State *L) return 1; } +static gint +lua_util_parse_addr (lua_State *L) +{ + InternetAddressList *ia; + InternetAddress *addr; + const gchar *str = luaL_checkstring (L, 1); + int i, cnt; + + if (str) { + ia = internet_address_list_parse_string (str); + + if (ia == NULL) { + lua_pushnil (L); + } + else { + cnt = internet_address_list_length (ia); + lua_newtable (L); + + for (i = 0; i < cnt; i ++) { + addr = internet_address_list_get_address (ia, i); + + lua_newtable (L); + lua_pushstring (L, "name"); + lua_pushstring (L, internet_address_get_name (addr)); + lua_settable (L, -3); + + if (INTERNET_ADDRESS_IS_MAILBOX (addr)) { + lua_pushstring (L, "addr"); + lua_pushstring (L, internet_address_mailbox_get_addr ( + INTERNET_ADDRESS_MAILBOX (addr))); + lua_settable (L, -3); + } + + lua_rawseti (L, -2, (i + 1)); + } + } + } + else { + lua_pushnil (L); + } + + return 1; +} + static gint lua_load_util (lua_State * L) { -- 2.39.5