aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2017-01-05 15:36:43 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2017-01-05 15:36:43 +0000
commitd26c0a18a7c5670e97bc23c902b609e17979122f (patch)
treeda5cf48e64bdd08363b3a7af0f04c70e3087a076 /src
parentf56cb9d4eca864b9943d33c1f83fcff49ec58a67 (diff)
downloadrspamd-d26c0a18a7c5670e97bc23c902b609e17979122f.tar.gz
rspamd-d26c0a18a7c5670e97bc23c902b609e17979122f.zip
[Feature] Implement url tags concept
Diffstat (limited to 'src')
-rw-r--r--src/libserver/html.c2
-rw-r--r--src/libserver/url.c15
-rw-r--r--src/libserver/url.h10
-rw-r--r--src/lua/lua_url.c59
-rw-r--r--src/plugins/surbl.c2
5 files changed, 86 insertions, 2 deletions
diff --git a/src/libserver/html.c b/src/libserver/html.c
index 3fb24c22e..1231650a3 100644
--- a/src/libserver/html.c
+++ b/src/libserver/html.c
@@ -1297,7 +1297,7 @@ rspamd_html_process_url (rspamd_mempool_t *pool, const gchar *start, guint len,
*d = '\0';
- url = rspamd_mempool_alloc (pool, sizeof (*url));
+ url = rspamd_mempool_alloc0 (pool, sizeof (*url));
rc = rspamd_url_parse (url, decoded, d - decoded, pool);
if (rc == URI_ERRNO_OK) {
diff --git a/src/libserver/url.c b/src/libserver/url.c
index 507913413..6bd42daea 100644
--- a/src/libserver/url.c
+++ b/src/libserver/url.c
@@ -2537,3 +2537,18 @@ rspamd_url_task_callback (struct rspamd_url *url, gsize start_offset,
}
}
}
+
+void
+rspamd_url_add_tag (struct rspamd_url *url, const gchar *tag,
+ rspamd_mempool_t *pool)
+{
+ g_assert (url != NULL && tag != NULL);
+
+ if (url->tags == NULL) {
+ url->tags = g_ptr_array_sized_new (2);
+ rspamd_mempool_add_destructor (pool, rspamd_ptr_array_free_hard, url->tags);
+ }
+
+ g_ptr_array_add (url->tags, rspamd_mempool_strdup (pool, tag));
+
+}
diff --git a/src/libserver/url.h b/src/libserver/url.h
index 2e1862090..eb70a9672 100644
--- a/src/libserver/url.h
+++ b/src/libserver/url.h
@@ -42,6 +42,7 @@ struct rspamd_url {
guint urllen;
enum rspamd_url_flags flags;
+ GPtrArray *tags;
};
enum uri_errno {
@@ -161,4 +162,13 @@ void rspamd_url_find_single (rspamd_mempool_t *pool, const gchar *in,
void rspamd_url_task_callback (struct rspamd_url *url, gsize start_offset,
gsize end_offset, gpointer ud);
+/**
+ * Adds a tag for url
+ * @param url
+ * @param tag
+ * @param pool
+ */
+void rspamd_url_add_tag (struct rspamd_url *url, const gchar *tag,
+ rspamd_mempool_t *pool);
+
#endif
diff --git a/src/lua/lua_url.c b/src/lua/lua_url.c
index b51487a88..eddef32d6 100644
--- a/src/lua/lua_url.c
+++ b/src/lua/lua_url.c
@@ -52,6 +52,8 @@ LUA_FUNCTION_DEF (url, is_phished);
LUA_FUNCTION_DEF (url, is_redirected);
LUA_FUNCTION_DEF (url, is_obscured);
LUA_FUNCTION_DEF (url, get_phished);
+LUA_FUNCTION_DEF (url, get_tags);
+LUA_FUNCTION_DEF (url, add_tag);
LUA_FUNCTION_DEF (url, create);
LUA_FUNCTION_DEF (url, init);
LUA_FUNCTION_DEF (url, all);
@@ -71,6 +73,8 @@ static const struct luaL_reg urllib_m[] = {
LUA_INTERFACE_DEF (url, is_redirected),
LUA_INTERFACE_DEF (url, is_obscured),
LUA_INTERFACE_DEF (url, get_phished),
+ LUA_INTERFACE_DEF (url, get_tags),
+ LUA_INTERFACE_DEF (url, add_tag),
{"get_redirected", lua_url_get_phished},
{"__tostring", lua_url_get_text},
{NULL, NULL}
@@ -310,6 +314,61 @@ lua_url_is_obscured (lua_State *L)
}
/***
+ * @method url:get_tags()
+ * Returns list of string tags for an url
+ * @return {table/strings} list of tags for an url
+ */
+static gint
+lua_url_get_tags (lua_State *L)
+{
+ struct rspamd_lua_url *url = lua_check_url (L, 1);
+ guint i;
+ const gchar *tag;
+
+ if (url != NULL) {
+ if (url->url->tags == NULL) {
+ lua_createtable (L, 0, 0);
+ }
+ else {
+ lua_createtable (L, url->url->tags->len, 0);
+
+ PTR_ARRAY_FOREACH (url->url->tags, i, tag) {
+ lua_pushstring (L, tag);
+ lua_rawseti (L, -2, i + 1);
+ }
+ }
+ }
+ else {
+ lua_pushnil (L);
+ }
+
+ return 1;
+}
+
+/***
+ * @method url:add_tag(tag, mempool)
+ * Adds a new tag for url
+ * @param {string} tag new tag to add
+ * @param {mempool} mempool memory pool (e.g. `task:get_pool()`)
+ */
+static gint
+lua_url_add_tag (lua_State *L)
+{
+ struct rspamd_lua_url *url = lua_check_url (L, 1);
+ rspamd_mempool_t *mempool = rspamd_lua_check_mempool (L, 3);
+ const gchar *tag = luaL_checkstring (L, 2);
+
+ if (url != NULL && mempool != NULL && tag != NULL) {
+ rspamd_url_add_tag (url->url, tag, mempool);
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ return 0;
+}
+
+/***
* @method url:get_phished()
* Get another URL that pretends to be this URL (e.g. used in phishing)
* @return {url} phished URL
diff --git a/src/plugins/surbl.c b/src/plugins/surbl.c
index 32c002d96..92741da5e 100644
--- a/src/plugins/surbl.c
+++ b/src/plugins/surbl.c
@@ -1308,7 +1308,7 @@ surbl_redirector_finish (struct rspamd_http_connection *conn,
urllen = hdr->len;
urlstr = rspamd_mempool_alloc (task->task_pool,
urllen + 1);
- redirected_url = rspamd_mempool_alloc (task->task_pool,
+ redirected_url = rspamd_mempool_alloc0 (task->task_pool,
sizeof (*redirected_url));
rspamd_strlcpy (urlstr, hdr->begin, urllen + 1);
r = rspamd_url_parse (redirected_url, urlstr, urllen,