]> source.dussan.org Git - rspamd.git/commitdiff
Add util.parse_addr function.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 25 Aug 2015 11:38:38 +0000 (12:38 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 25 Aug 2015 11:38:38 +0000 (12:38 +0100)
src/lua/lua_util.c

index 57b93d12f8f3e5953bf22ca698f3fd74afc013df..9fc71d86a2e195a8562c38935f3d1f47fab89cd1 100644 (file)
@@ -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)
 {