]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Implement url tags concept
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 5 Jan 2017 15:36:43 +0000 (15:36 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 5 Jan 2017 15:36:43 +0000 (15:36 +0000)
src/libserver/html.c
src/libserver/url.c
src/libserver/url.h
src/lua/lua_url.c
src/plugins/surbl.c

index 3fb24c22e08c31f5576ade8d21062f431eacebc9..1231650a3107783e9615a2074aa7eb03fb233859 100644 (file)
@@ -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) {
index 50791341350e944bce825a43ef444e2559b89277..6bd42daea64701027bad45ba7ea5aacd680de1e8 100644 (file)
@@ -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));
+
+}
index 2e186209047720132cccb19fc1eb70db4751d850..eb70a9672e91d653abd404557d1682258aaf6aa4 100644 (file)
@@ -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
index b51487a885f9e3171f0608d18772ebeff421d0c1..eddef32d696f4301893f2283fd03494bc89cbfdc 100644 (file)
@@ -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}
@@ -309,6 +313,61 @@ lua_url_is_obscured (lua_State *L)
        return 1;
 }
 
+/***
+ * @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)
index 32c002d963c151f63788dfb7ba03c3cd409d5ca0..92741da5e35e2dc8b2f333035fe852aa1cf99479 100644 (file)
@@ -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,