|
|
@@ -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"); |