From 4174974c5cf7f1ba10667f858d96f9ba5795db09 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 28 Mar 2017 14:25:56 +0100 Subject: [PATCH] [Feature] Distinguish URLs found in Subject --- src/libmime/message.c | 10 +++------- src/libserver/url.c | 4 ++-- src/libserver/url.h | 4 +++- src/lua/lua_url.c | 22 ++++++++++++++++++++++ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/libmime/message.c b/src/libmime/message.c index d445362b2b..91e32be332 100644 --- a/src/libmime/message.c +++ b/src/libmime/message.c @@ -632,8 +632,6 @@ rspamd_message_from_data (struct rspamd_task *task, const guchar *start, gboolean rspamd_message_parse (struct rspamd_task *task) { - GPtrArray *hdrs; - struct rspamd_mime_header *rh; struct rspamd_mime_text_part *p1, *p2; struct received_header *recv, *trecv; const gchar *p; @@ -824,13 +822,11 @@ rspamd_message_parse (struct rspamd_task *task) } /* Parse urls inside Subject header */ - hdrs = rspamd_message_get_header_array (task, "Subject", FALSE); - - PTR_ARRAY_FOREACH (hdrs, i, rh) { - p = rh->decoded; + if (task->subject) { + p = task->subject; len = strlen (p); rspamd_url_find_multiple (task->task_pool, p, len, FALSE, NULL, - rspamd_url_task_callback, task); + rspamd_url_task_subject_callback, task); } /* Calculate distance for 2-parts messages */ diff --git a/src/libserver/url.c b/src/libserver/url.c index 0a1b26bb30..1efae6019d 100644 --- a/src/libserver/url.c +++ b/src/libserver/url.c @@ -2486,7 +2486,7 @@ rspamd_url_find_single (rspamd_mempool_t *pool, const gchar *in, void -rspamd_url_task_callback (struct rspamd_url *url, gsize start_offset, +rspamd_url_task_subject_callback (struct rspamd_url *url, gsize start_offset, gsize end_offset, gpointer ud) { struct rspamd_task *task = ud; @@ -2495,7 +2495,7 @@ rspamd_url_task_callback (struct rspamd_url *url, gsize start_offset, gint rc; /* It is just a displayed URL, we should not check it for certain things */ - url->flags |= RSPAMD_URL_FLAG_HTML_DISPLAYED; + url->flags |= RSPAMD_URL_FLAG_HTML_DISPLAYED|RSPAMD_URL_FLAG_SUBJECT; if (url->protocol == PROTOCOL_MAILTO) { if (url->userlen > 0) { diff --git a/src/libserver/url.h b/src/libserver/url.h index 9a9522c50c..14c0c5b696 100644 --- a/src/libserver/url.h +++ b/src/libserver/url.h @@ -16,6 +16,7 @@ enum rspamd_url_flags { RSPAMD_URL_FLAG_REDIRECTED = 1 << 3, RSPAMD_URL_FLAG_HTML_DISPLAYED = 1 << 4, RSPAMD_URL_FLAG_FROM_TEXT = 1 << 5, + RSPAMD_URL_FLAG_SUBJECT = 1 << 6, }; struct rspamd_url_tag { @@ -163,7 +164,8 @@ void rspamd_url_find_single (rspamd_mempool_t *pool, const gchar *in, * @param end_offset * @param ud */ -void rspamd_url_task_callback (struct rspamd_url *url, gsize start_offset, +void rspamd_url_task_subject_callback (struct rspamd_url *url, + gsize start_offset, gsize end_offset, gpointer ud); /** diff --git a/src/lua/lua_url.c b/src/lua/lua_url.c index 9b69267a44..8826bb41ca 100644 --- a/src/lua/lua_url.c +++ b/src/lua/lua_url.c @@ -53,6 +53,7 @@ LUA_FUNCTION_DEF (url, is_phished); LUA_FUNCTION_DEF (url, is_redirected); LUA_FUNCTION_DEF (url, is_obscured); LUA_FUNCTION_DEF (url, is_html_displayed); +LUA_FUNCTION_DEF (url, is_subject); LUA_FUNCTION_DEF (url, get_phished); LUA_FUNCTION_DEF (url, get_tag); LUA_FUNCTION_DEF (url, get_tags); @@ -76,6 +77,7 @@ static const struct luaL_reg urllib_m[] = { LUA_INTERFACE_DEF (url, is_redirected), LUA_INTERFACE_DEF (url, is_obscured), LUA_INTERFACE_DEF (url, is_html_displayed), + LUA_INTERFACE_DEF (url, is_subject), LUA_INTERFACE_DEF (url, get_phished), LUA_INTERFACE_DEF (url, get_tag), LUA_INTERFACE_DEF (url, get_tags), @@ -339,6 +341,26 @@ lua_url_is_html_displayed (lua_State *L) return 1; } +/*** + * @method url:is_subject() + * Check whether URL is found in subject + * @return {boolean} `true` if URL is found in subject + */ +static gint +lua_url_is_subject (lua_State *L) +{ + struct rspamd_lua_url *url = lua_check_url (L, 1); + + if (url != NULL) { + lua_pushboolean (L, url->url->flags & RSPAMD_URL_FLAG_SUBJECT); + } + else { + lua_pushnil (L); + } + + return 1; +} + /*** * @method url:get_tag(tag) * Returns list of string for a specific tagname for an url -- 2.47.3