]> source.dussan.org Git - rspamd.git/commitdiff
Add method to register symbols conditions
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 27 Aug 2015 15:27:19 +0000 (16:27 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 27 Aug 2015 16:36:29 +0000 (17:36 +0100)
src/libserver/symbols_cache.c
src/libserver/symbols_cache.h

index bf363b499c3cf2590672ad0707717936946c8b23..30b146a4e06394511d8efa4972d39ff830584e1b 100644 (file)
@@ -481,6 +481,7 @@ rspamd_symbols_cache_add_symbol (struct symbols_cache *cache,
 
        item = rspamd_mempool_alloc0_shared (cache->static_pool,
                        sizeof (struct cache_item));
+       item->condition_cb = -1;
        /*
         * We do not share cd to skip locking, instead we'll just calculate it on
         * save or accumulate
@@ -523,6 +524,29 @@ rspamd_symbols_cache_add_symbol (struct symbols_cache *cache,
        return item->id;
 }
 
+gboolean
+rspamd_symbols_cache_add_condition (struct symbols_cache *cache, gint id, lua_State *L, gint cbref)
+{
+       struct cache_item *item;
+
+       g_assert (cache != NULL);
+
+       if (id < 0 || id >= cache->items_by_id->len) {
+               return FALSE;
+       }
+
+       item = g_ptr_array_index (cache->items_by_id, id);
+
+       if (item->condition_cb != -1) {
+               /* We already have a condition, so we need to remove old cbref first */
+               msg_warn ("rewriting condition for symbol %s", item->symbol);
+               luaL_unref (L, LUA_REGISTRYINDEX, item->condition_cb);
+       }
+
+       item->condition_cb = cbref;
+
+       return TRUE;
+}
 
 void
 rspamd_symbols_cache_destroy (struct symbols_cache *cache)
@@ -1262,3 +1286,23 @@ rspamd_symbols_cache_add_delayed_dependency (struct symbols_cache *cache,
 
        cache->delayed_deps = g_list_prepend (cache->delayed_deps, ddep);
 }
+
+gint
+rspamd_symbols_cache_find_symbol (struct symbols_cache *cache, const gchar *name)
+{
+       struct cache_item *item;
+
+       g_assert (cache != NULL);
+
+       if (name == NULL) {
+               return -1;
+       }
+
+       item = g_hash_table_lookup (cache->items_by_symbol, name);
+
+       if (item != NULL) {
+               return item->id;
+       }
+
+       return -1;
+}
index bc3386eba7809a891127731612c85516125824c8..b014709c89bf30c6e1c9f15789e43c3ce5ef1e09 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "config.h"
 #include "ucl.h"
+#include "lua/lua_common.h"
 
 #define MAX_SYMBOL 128
 
@@ -83,6 +84,24 @@ gint rspamd_symbols_cache_add_symbol (struct symbols_cache *cache,
        enum rspamd_symbol_type type,
        gint parent);
 
+/**
+ * Add condition to the specific symbol in cache
+ * @param cache
+ * @param id id of symbol
+ * @param L lua state pointer
+ * @param cbref callback reference (returned by luaL_ref)
+ * @return TRUE if condition has been added
+ */
+gboolean rspamd_symbols_cache_add_condition (struct symbols_cache *cache, gint id, lua_State *L, gint cbref);
+
+/**
+ * Find symbol in cache by id and returns its id
+ * @param cache
+ * @param name
+ * @return id of symbol or (-1) if a symbol has not been found
+ */
+gint rspamd_symbols_cache_find_symbol (struct symbols_cache *cache, const gchar *name);
+
 /**
  * Call function for cached symbol using saved callback
  * @param task task object