From 236b023bbdfecfd5875c8f829a646043e3675b58 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 28 May 2015 08:44:40 +0100 Subject: [PATCH] Registering symbols now returns their id. --- src/libserver/symbols_cache.c | 22 +++++++++-------- src/libserver/symbols_cache.h | 10 ++++---- src/lua/lua_config.c | 45 +++++++++++++++++++++++------------ 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/libserver/symbols_cache.c b/src/libserver/symbols_cache.c index be7ef98d97..f7e4e5d7b5 100644 --- a/src/libserver/symbols_cache.c +++ b/src/libserver/symbols_cache.c @@ -305,7 +305,7 @@ rspamd_symbols_cache_save_items (struct symbols_cache *cache, const gchar *name) return ret; } -void +gint rspamd_symbols_cache_add_symbol (struct symbols_cache *cache, const gchar *name, double weight, @@ -320,7 +320,7 @@ rspamd_symbols_cache_add_symbol (struct symbols_cache *cache, if (g_hash_table_lookup (cache->items_by_symbol, name) != NULL) { msg_err ("skip duplicate symbol registration for %s", name); - return; + return -1; } item = rspamd_mempool_alloc0_shared (cache->static_pool, @@ -349,13 +349,15 @@ rspamd_symbols_cache_add_symbol (struct symbols_cache *cache, rspamd_set_counter (item, 0); g_hash_table_insert (cache->items_by_symbol, item->symbol, item); g_ptr_array_add (cache->items_by_order, item); + + return item->id; } -void +gint rspamd_symbols_cache_add_symbol_normal (struct symbols_cache *cache, const gchar *name, double weight, symbol_func_t func, gpointer user_data) { - rspamd_symbols_cache_add_symbol (cache, + return rspamd_symbols_cache_add_symbol (cache, name, weight, 0, @@ -364,12 +366,12 @@ rspamd_symbols_cache_add_symbol_normal (struct symbols_cache *cache, const gchar SYMBOL_TYPE_NORMAL); } -void +gint rspamd_symbols_cache_add_symbol_virtual (struct symbols_cache *cache, const gchar *name, double weight) { - rspamd_symbols_cache_add_symbol (cache, + return rspamd_symbols_cache_add_symbol (cache, name, weight, 0, @@ -378,14 +380,14 @@ rspamd_symbols_cache_add_symbol_virtual (struct symbols_cache *cache, SYMBOL_TYPE_VIRTUAL); } -void +gint rspamd_symbols_cache_add_symbol_callback (struct symbols_cache *cache, const gchar *name, double weight, symbol_func_t func, gpointer user_data) { - rspamd_symbols_cache_add_symbol (cache, + return rspamd_symbols_cache_add_symbol (cache, name, weight, 0, @@ -394,7 +396,7 @@ rspamd_symbols_cache_add_symbol_callback (struct symbols_cache *cache, SYMBOL_TYPE_CALLBACK); } -void +gint rspamd_symbols_cache_add_symbol_callback_prio (struct symbols_cache *cache, const gchar *name, double weight, @@ -402,7 +404,7 @@ rspamd_symbols_cache_add_symbol_callback_prio (struct symbols_cache *cache, symbol_func_t func, gpointer user_data) { - rspamd_symbols_cache_add_symbol (cache, + return rspamd_symbols_cache_add_symbol (cache, name, weight, priority, diff --git a/src/libserver/symbols_cache.h b/src/libserver/symbols_cache.h index c0b43ee6b6..b3ebcc5597 100644 --- a/src/libserver/symbols_cache.h +++ b/src/libserver/symbols_cache.h @@ -68,7 +68,7 @@ gboolean rspamd_symbols_cache_init (struct symbols_cache* cache, * @param func pointer to handler * @param user_data pointer to user_data */ -void rspamd_symbols_cache_add_symbol_normal (struct symbols_cache *cache, +gint rspamd_symbols_cache_add_symbol_normal (struct symbols_cache *cache, const gchar *name, double weight, symbol_func_t func, @@ -79,7 +79,7 @@ void rspamd_symbols_cache_add_symbol_normal (struct symbols_cache *cache, * Register virtual symbol * @param name name of symbol */ -void rspamd_symbols_cache_add_symbol_virtual (struct symbols_cache *cache, +gint rspamd_symbols_cache_add_symbol_virtual (struct symbols_cache *cache, const gchar *name, double weight); @@ -89,7 +89,7 @@ void rspamd_symbols_cache_add_symbol_virtual (struct symbols_cache *cache, * @param func pointer to handler * @param user_data pointer to user_data */ -void rspamd_symbols_cache_add_symbol_callback (struct symbols_cache *cache, +gint rspamd_symbols_cache_add_symbol_callback (struct symbols_cache *cache, const gchar *name, double weight, symbol_func_t func, @@ -101,7 +101,7 @@ void rspamd_symbols_cache_add_symbol_callback (struct symbols_cache *cache, * @param func pointer to handler * @param user_data pointer to user_data */ -void rspamd_symbols_cache_add_symbol_callback_prio (struct symbols_cache *cache, +gint rspamd_symbols_cache_add_symbol_callback_prio (struct symbols_cache *cache, const gchar *name, double weight, gint priority, @@ -118,7 +118,7 @@ void rspamd_symbols_cache_add_symbol_callback_prio (struct symbols_cache *cache, * @param user_data * @param type */ -void rspamd_symbols_cache_add_symbol (struct symbols_cache *cache, +gint rspamd_symbols_cache_add_symbol (struct symbols_cache *cache, const gchar *name, double weight, gint priority, diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index 247805f984..845c3740df 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -848,7 +848,7 @@ lua_metric_symbol_callback (struct rspamd_task *task, gpointer ud) } } -static void +static gint rspamd_register_symbol_fromlua (lua_State *L, struct rspamd_config *cfg, const gchar *name, @@ -858,6 +858,7 @@ rspamd_register_symbol_fromlua (lua_State *L, enum rspamd_symbol_type type) { struct lua_callback_data *cd; + gint ret = -1; if (name) { cd = rspamd_mempool_alloc0 (cfg->cfg_pool, @@ -867,7 +868,7 @@ rspamd_register_symbol_fromlua (lua_State *L, cd->L = L; cd->symbol = rspamd_mempool_strdup (cfg->cfg_pool, name); - rspamd_symbols_cache_add_symbol (cfg->cache, + ret = rspamd_symbols_cache_add_symbol (cfg->cache, name, weight, priority, @@ -879,7 +880,7 @@ rspamd_register_symbol_fromlua (lua_State *L, cd); } - + return ret; } static gint @@ -888,6 +889,7 @@ lua_config_register_symbol (lua_State * L) struct rspamd_config *cfg = lua_check_config (L, 1); gchar *name; double weight; + gint ret = -1; if (cfg) { name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2)); @@ -900,7 +902,7 @@ lua_config_register_symbol (lua_State * L) lua_pushvalue (L, 4); } if (name) { - rspamd_register_symbol_fromlua (L, + ret = rspamd_register_symbol_fromlua (L, cfg, name, luaL_ref (L, LUA_REGISTRYINDEX), @@ -910,14 +912,16 @@ lua_config_register_symbol (lua_State * L) } } - return 0; + lua_pushnumber (L, ret); + + return 1; } static gint lua_config_register_symbols (lua_State *L) { struct rspamd_config *cfg = lua_check_config (L, 1); - gint i, top, idx; + gint i, top, idx, ret = -1; gchar *sym; gdouble weight = 1.0; @@ -942,7 +946,7 @@ lua_config_register_symbols (lua_State *L) top = 3; } sym = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, top ++)); - rspamd_register_symbol_fromlua (L, + ret = rspamd_register_symbol_fromlua (L, cfg, sym, idx, @@ -970,7 +974,9 @@ lua_config_register_symbols (lua_State *L) } } - return 0; + lua_pushnumber (L, ret); + + return 1; } static gint @@ -979,15 +985,19 @@ lua_config_register_virtual_symbol (lua_State * L) struct rspamd_config *cfg = lua_check_config (L, 1); gchar *name; double weight; + gint ret = -1; if (cfg) { name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2)); weight = luaL_checknumber (L, 3); if (name) { - rspamd_symbols_cache_add_symbol_virtual (cfg->cache, name, weight); + ret = rspamd_symbols_cache_add_symbol_virtual (cfg->cache, name, weight); } } - return 0; + + lua_pushnumber (L, ret); + + return 1; } static gint @@ -996,6 +1006,7 @@ lua_config_register_callback_symbol (lua_State * L) struct rspamd_config *cfg = lua_check_config (L, 1); gchar *name; double weight; + gint ret = -1; if (cfg) { name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2)); @@ -1008,7 +1019,7 @@ lua_config_register_callback_symbol (lua_State * L) lua_pushvalue (L, 4); } if (name) { - rspamd_register_symbol_fromlua (L, + ret = rspamd_register_symbol_fromlua (L, cfg, name, luaL_ref (L, LUA_REGISTRYINDEX), @@ -1018,7 +1029,9 @@ lua_config_register_callback_symbol (lua_State * L) } } - return 0; + lua_pushnumber (L, ret); + + return 1; } static gint @@ -1027,7 +1040,7 @@ lua_config_register_callback_symbol_priority (lua_State * L) struct rspamd_config *cfg = lua_check_config (L, 1); gchar *name; double weight; - gint priority; + gint priority, ret = -1; if (cfg) { name = rspamd_mempool_strdup (cfg->cfg_pool, luaL_checkstring (L, 2)); @@ -1041,7 +1054,7 @@ lua_config_register_callback_symbol_priority (lua_State * L) lua_pushvalue (L, 5); } if (name) { - rspamd_register_symbol_fromlua (L, + ret = rspamd_register_symbol_fromlua (L, cfg, name, luaL_ref (L, LUA_REGISTRYINDEX), @@ -1051,7 +1064,9 @@ lua_config_register_callback_symbol_priority (lua_State * L) } } - return 0; + lua_pushnumber (L, ret); + + return 1; } static gint -- 2.47.3