]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Improve URL tags
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 12 Jan 2017 19:10:28 +0000 (19:10 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 12 Jan 2017 19:10:28 +0000 (19:10 +0000)
src/libserver/url.c
src/libserver/url.h
src/lua/lua_url.c

index 7df4cb98af88bd783c8dc52cb34ff1fa6571239e..4252d5ac1b80f4e7959a9dacfcb7e7de12bb89b5 100644 (file)
@@ -48,6 +48,7 @@
 #include "http.h"
 #include "multipattern.h"
 #include "http_parser.h"
+#include "contrib/uthash/utlist.h"
 
 typedef struct url_match_s {
        const gchar *m_begin;
@@ -2543,15 +2544,28 @@ rspamd_url_task_callback (struct rspamd_url *url, gsize start_offset,
 
 void
 rspamd_url_add_tag (struct rspamd_url *url, const gchar *tag,
+               const gchar *value,
                rspamd_mempool_t *pool)
 {
-       g_assert (url != NULL && tag != NULL);
+       struct rspamd_url_tag *found, *ntag;
+
+       g_assert (url != NULL && tag != NULL && value != 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);
+               url->tags = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
+               rspamd_mempool_add_destructor (pool,
+                               (rspamd_mempool_destruct_t)g_hash_table_unref, url->tags);
        }
 
-       g_ptr_array_add (url->tags, rspamd_mempool_strdup (pool, tag));
+       found = g_hash_table_lookup (url->tags, tag);
+
+       ntag = rspamd_mempool_alloc0 (pool, sizeof (*ntag));
+       ntag->data = rspamd_mempool_strdup (pool, value);
+
+       if (found == NULL) {
+               g_hash_table_insert (url->tags, rspamd_mempool_strdup (pool, tag),
+                               ntag);
+       }
 
+       DL_APPEND (found, ntag);
 }
index eb70a9672e91d653abd404557d1682258aaf6aa4..dbe3eb00b21a45350ef662ce8f52a9896cc0bf30 100644 (file)
@@ -16,6 +16,11 @@ enum rspamd_url_flags {
        RSPAMD_URL_FLAG_REDIRECTED = 1 << 3,
 };
 
+struct rspamd_url_tag {
+       const gchar *data;
+       struct rspamd_url_tag *prev, *next;
+};
+
 struct rspamd_url {
        gchar *string;
        gint protocol;
@@ -42,7 +47,7 @@ struct rspamd_url {
        guint urllen;
 
        enum rspamd_url_flags flags;
-       GPtrArray *tags;
+       GHashTable *tags;
 };
 
 enum uri_errno {
@@ -169,6 +174,7 @@ void rspamd_url_task_callback (struct rspamd_url *url, gsize start_offset,
  * @param pool
  */
 void rspamd_url_add_tag (struct rspamd_url *url, const gchar *tag,
+               const gchar *value,
                rspamd_mempool_t *pool);
 
 #endif
index eddef32d696f4301893f2283fd03494bc89cbfdc..cdc74e8d8b6fea845177b550945bbb5e51986c5d 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 #include "lua_common.h"
+#include "contrib/uthash/utlist.h"
 
 /***
  * @module rspamd_url
@@ -52,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, get_phished);
+LUA_FUNCTION_DEF (url, get_tag);
 LUA_FUNCTION_DEF (url, get_tags);
 LUA_FUNCTION_DEF (url, add_tag);
 LUA_FUNCTION_DEF (url, create);
@@ -73,6 +75,7 @@ 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_tag),
        LUA_INTERFACE_DEF (url, get_tags),
        LUA_INTERFACE_DEF (url, add_tag),
        {"get_redirected", lua_url_get_phished},
@@ -313,6 +316,53 @@ lua_url_is_obscured (lua_State *L)
        return 1;
 }
 
+/***
+ * @method url:get_tag(tag)
+ * Returns list of string for a specific tagname for an url
+ * @return {table/strings} list of tags for an url
+ */
+static gint
+lua_url_get_tag (lua_State *L)
+{
+       struct rspamd_lua_url *url = lua_check_url (L, 1);
+       guint i;
+       const gchar *tag = luaL_checkstring (L, 2);
+       GHashTableIter it;
+       struct rspamd_url_tag *tval, *cur;
+       gpointer k, v;
+
+       if (url != NULL && tag != NULL) {
+
+               if (url->url->tags == NULL) {
+                       lua_createtable (L, 0, 0);
+               }
+               else {
+                       tval = g_hash_table_lookup (url->url->tags, tag);
+
+                       if (tval) {
+                               lua_newtable (L);
+                               i = 1;
+
+                               DL_FOREACH (tval, cur) {
+                                       lua_pushstring (L, cur->data);
+                                       lua_rawseti (L, -2, i ++);
+                               }
+
+                               lua_settable (L, -3);
+                       }
+                       else {
+                               lua_createtable (L, 0, 0);
+                       }
+               }
+       }
+       else {
+               lua_pushnil (L);
+       }
+
+       return 1;
+}
+
+
 /***
  * @method url:get_tags()
  * Returns list of string tags for an url
@@ -324,17 +374,30 @@ lua_url_get_tags (lua_State *L)
        struct rspamd_lua_url *url = lua_check_url (L, 1);
        guint i;
        const gchar *tag;
+       GHashTableIter it;
+       struct rspamd_url_tag *tval, *cur;
+       gpointer k, v;
 
        if (url != NULL) {
                if (url->url->tags == NULL) {
                        lua_createtable (L, 0, 0);
                }
                else {
-                       lua_createtable (L, url->url->tags->len, 0);
+                       lua_createtable (L, 0, g_hash_table_size (url->url->tags));
+                       g_hash_table_iter_init (&it, url->url->tags);
+
+                       while (g_hash_table_iter_next (&it, &k, &v)) {
+                               tval = v;
+                               lua_pushstring (L, (const gchar *)k);
+                               lua_newtable (L);
+                               i = 1;
+
+                               DL_FOREACH (tval, cur) {
+                                       lua_pushstring (L, cur->data);
+                                       lua_rawseti (L, -2, i ++);
+                               }
 
-                       PTR_ARRAY_FOREACH (url->url->tags, i, tag) {
-                               lua_pushstring (L, tag);
-                               lua_rawseti (L, -2, i + 1);
+                               lua_settable (L, -3);
                        }
                }
        }
@@ -357,9 +420,17 @@ 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);
+       const gchar *value;
+
+       if (lua_type (L, 3) == LUA_TSTRING) {
+               value = lua_tostring (L, 3);
+       }
+       else {
+               value = "1"; /* Some stupid placeholder */
+       }
 
        if (url != NULL && mempool != NULL && tag != NULL) {
-               rspamd_url_add_tag (url->url, tag, mempool);
+               rspamd_url_add_tag (url->url, tag, value, mempool);
        }
        else {
                return luaL_error (L, "invalid arguments");