#include "http.h"
#include "multipattern.h"
#include "http_parser.h"
+#include "contrib/uthash/utlist.h"
typedef struct url_match_s {
const gchar *m_begin;
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);
}
* limitations under the License.
*/
#include "lua_common.h"
+#include "contrib/uthash/utlist.h"
/***
* @module rspamd_url
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);
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},
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
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);
}
}
}
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");