From: Vsevolod Stakhov Date: Mon, 8 Feb 2016 00:10:04 +0000 (+0000) Subject: Add fast path for checking symbols X-Git-Tag: 1.2.0~291 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9cb4f26bd048aaa3a292d7a1ac8161288cbf7bf9;p=thirdparty%2Frspamd.git Add fast path for checking symbols --- diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 6e644b0da4..3f2b0723d9 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -372,6 +372,13 @@ LUA_FUNCTION_DEF (task, get_images); * @return {list of tables} list of tables or nil if symbol was not found in any metric */ LUA_FUNCTION_DEF (task, get_symbol); +/*** + * @method task:has_symbol(name) + * Fast path to check if a specified symbol is in the task's results + * @param {string} name symbol's name + * @return {boolean} `true` if symbol has been found + */ +LUA_FUNCTION_DEF (task, has_symbol); /*** * @method task:get_date(type[, gmt]) * Returns timestamp for a connection or for a MIME message. This function can be called with a @@ -570,6 +577,7 @@ static const struct luaL_reg tasklib_m[] = { LUA_INTERFACE_DEF (task, set_hostname), LUA_INTERFACE_DEF (task, get_images), LUA_INTERFACE_DEF (task, get_symbol), + LUA_INTERFACE_DEF (task, has_symbol), LUA_INTERFACE_DEF (task, get_date), LUA_INTERFACE_DEF (task, get_message_id), LUA_INTERFACE_DEF (task, get_timeval), @@ -1785,6 +1793,29 @@ lua_task_get_symbol (lua_State *L) return 1; } +static gint +lua_task_has_symbol (lua_State *L) +{ + struct rspamd_task *task = lua_check_task (L, 1); + const gchar *symbol; + struct metric_result *mres; + gboolean found = FALSE; + + symbol = luaL_checkstring (L, 2); + + if (task && symbol) { + mres = g_hash_table_lookup (task->results, DEFAULT_METRIC); + + if (mres) { + found = g_hash_table_lookup (mres->symbols, symbol) != NULL; + } + } + + lua_pushboolean (L, found); + + return 1; +} + enum lua_date_type { DATE_CONNECT = 0, DATE_MESSAGE,