From: Vsevolod Stakhov Date: Thu, 27 Aug 2015 15:27:19 +0000 (+0100) Subject: Add method to register symbols conditions X-Git-Tag: 1.0.0~158 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=641af77bc5cd5119af57efb67ef16449a9999f38;p=rspamd.git Add method to register symbols conditions --- diff --git a/src/libserver/symbols_cache.c b/src/libserver/symbols_cache.c index bf363b499..30b146a4e 100644 --- a/src/libserver/symbols_cache.c +++ b/src/libserver/symbols_cache.c @@ -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; +} diff --git a/src/libserver/symbols_cache.h b/src/libserver/symbols_cache.h index bc3386eba..b014709c8 100644 --- a/src/libserver/symbols_cache.h +++ b/src/libserver/symbols_cache.h @@ -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