From: Vsevolod Stakhov Date: Tue, 7 Apr 2015 14:30:28 +0000 (+0100) Subject: Rework lua trie API, allow raw message search. X-Git-Tag: 0.9.0~308 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=125f1805175b7760930f404578642066d0981eb5;p=rspamd.git Rework lua trie API, allow raw message search. --- diff --git a/src/lua/lua_trie.c b/src/lua/lua_trie.c index 7c7021bd1..3a07fa3b1 100644 --- a/src/lua/lua_trie.c +++ b/src/lua/lua_trie.c @@ -29,13 +29,15 @@ /* Suffix trie */ LUA_FUNCTION_DEF (trie, create); -LUA_FUNCTION_DEF (trie, search_text); -LUA_FUNCTION_DEF (trie, search_task); +LUA_FUNCTION_DEF (trie, match); +LUA_FUNCTION_DEF (trie, search_mime); +LUA_FUNCTION_DEF (trie, search_rawmsg); LUA_FUNCTION_DEF (trie, destroy); static const struct luaL_reg trielib_m[] = { - LUA_INTERFACE_DEF (trie, search_text), - LUA_INTERFACE_DEF (trie, search_task), + LUA_INTERFACE_DEF (trie, match), + LUA_INTERFACE_DEF (trie, search_mime), + LUA_INTERFACE_DEF (trie, search_rawmsg), {"__tostring", rspamd_lua_class_tostring}, {"__gc", lua_trie_destroy}, {NULL, NULL} @@ -171,7 +173,7 @@ lua_trie_search_str (lua_State *L, ac_trie_t *trie, const gchar *str, gsize len, } static gint -lua_trie_search_text (lua_State *L) +lua_trie_match (lua_State *L) { ac_trie_t *trie = lua_check_trie (L, 1); const gchar *text; @@ -211,7 +213,7 @@ lua_trie_search_text (lua_State *L) } static gint -lua_trie_search_task (lua_State *L) +lua_trie_search_mime (lua_State *L) { ac_trie_t *trie = lua_check_trie (L, 1); struct rspamd_task *task = lua_check_task (L, 2); @@ -245,6 +247,29 @@ lua_trie_search_task (lua_State *L) return 1; } +static gint +lua_trie_search_rawmsg (lua_State *L) +{ + ac_trie_t *trie = lua_check_trie (L, 1); + struct rspamd_task *task = lua_check_task (L, 2); + const gchar *text; + gint state = 0; + gsize len; + gboolean found = FALSE; + + if (trie) { + text = task->msg.start; + len = task->msg.len; + + if (lua_trie_search_str (L, trie, text, len, &state) != 0) { + found = TRUE; + } + } + + lua_pushboolean (L, found); + return 1; +} + static gint lua_load_trie (lua_State *L) { diff --git a/test/lua/unit/trie.lua b/test/lua/unit/trie.lua index cff933d61..6fa7573f9 100644 --- a/test/lua/unit/trie.lua +++ b/test/lua/unit/trie.lua @@ -43,7 +43,7 @@ context("Trie search functions", function() return 0 end - ret = trie:search_text(c[1], cb) + ret = trie:match(c[1], cb) assert_equal(c[2], ret, tostring(c[2]) .. ' while matching ' .. c[1])