]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Add fast path for checking symbols
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 8 Feb 2016 00:10:04 +0000 (00:10 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 8 Feb 2016 00:22:38 +0000 (00:22 +0000)
src/lua/lua_task.c

index 6e644b0da4c04de16ff35cfcf95e77f9387b9755..3f2b0723d9aebe2b789933a153350de1fa30a424 100644 (file)
@@ -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,