aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-01-25 11:31:34 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-01-25 11:31:34 +0000
commit550bc44b90f33e476d85e2df922d7b05ed1733ad (patch)
tree9194f2b250e776f068b6fad7bf6f49fbe5c33657 /src/lua
parentbe47c345d50b355e890400fccf4437ac82ceccb8 (diff)
downloadrspamd-550bc44b90f33e476d85e2df922d7b05ed1733ad.tar.gz
rspamd-550bc44b90f33e476d85e2df922d7b05ed1733ad.zip
Add util.parse_mail_address function to LUA API
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/lua_util.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c
index eda1ede2b..cbd54e6cf 100644
--- a/src/lua/lua_util.c
+++ b/src/lua/lua_util.c
@@ -162,6 +162,20 @@ LUA_FUNCTION_DEF (util, get_tld);
*/
LUA_FUNCTION_DEF (util, glob);
+/**
+ * @function util.parse_mail_address(str)
+ * Parses email address and returns a table of tables in the following format:
+ *
+ * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
+ * - `addr` - address part of the address
+ * - `user` - user part (if present) of the address, e.g. `blah`
+ * - `domain` - domain part (if present), e.g. `foo.com`
+ *
+ * @param {string} str input string
+ * @return {table/tables} parsed list of mail addresses
+ */
+LUA_FUNCTION_DEF (util, parse_mail_address);
+
static const struct luaL_reg utillib_f[] = {
LUA_INTERFACE_DEF (util, create_event_base),
LUA_INTERFACE_DEF (util, load_rspamd_config),
@@ -179,6 +193,7 @@ static const struct luaL_reg utillib_f[] = {
LUA_INTERFACE_DEF (util, humanize_number),
LUA_INTERFACE_DEF (util, get_tld),
LUA_INTERFACE_DEF (util, glob),
+ LUA_INTERFACE_DEF (util, parse_mail_address),
{NULL, NULL}
};
@@ -776,6 +791,35 @@ lua_util_glob (lua_State *L)
}
static gint
+lua_util_parse_mail_address (lua_State *L)
+{
+ InternetAddressList *ia;
+ const gchar *str = luaL_checkstring (L, 1);
+ gboolean ret = FALSE;
+
+ if (str) {
+ ia = internet_address_list_parse_string (str);
+
+ if (ia != NULL) {
+ ret = TRUE;
+
+ lua_push_internet_address_list (L, ia);
+#ifdef GMIME24
+ g_object_unref (ia);
+#else
+ internet_address_list_destroy (ia);
+#endif
+ }
+ }
+
+ if (!ret) {
+ lua_pushnil (L);
+ }
+
+ return 1;
+}
+
+static gint
lua_load_util (lua_State * L)
{
lua_newtable (L);