diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-03-08 15:49:41 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-03-08 15:49:41 +0000 |
commit | b42f0dd192db5b34aac06f373c9eecf8ce7b90c2 (patch) | |
tree | 73ba8748046f5470bd74d27938b542cfbbff1d95 | |
parent | 7cfee52f79040695515710b1bb4b8d0c6faf7c50 (diff) | |
download | rspamd-b42f0dd192db5b34aac06f373c9eecf8ce7b90c2.tar.gz rspamd-b42f0dd192db5b34aac06f373c9eecf8ce7b90c2.zip |
[Feature] Allow optional symbols registration
-rw-r--r-- | src/libserver/symbols_cache.c | 3 | ||||
-rw-r--r-- | src/lua/lua_config.c | 44 |
2 files changed, 39 insertions, 8 deletions
diff --git a/src/libserver/symbols_cache.c b/src/libserver/symbols_cache.c index daeadcfe6..bd92ee782 100644 --- a/src/libserver/symbols_cache.c +++ b/src/libserver/symbols_cache.c @@ -684,7 +684,8 @@ rspamd_symbols_cache_add_condition (struct symbols_cache *cache, gint id, return TRUE; } -gboolean rspamd_symbols_cache_add_condition_delayed (struct symbols_cache *cache, +gboolean +rspamd_symbols_cache_add_condition_delayed (struct symbols_cache *cache, const gchar *sym, lua_State *L, gint cbref) { gint id; diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index 9f97902bb..4237ff97a 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -834,7 +834,8 @@ rspamd_register_symbol_fromlua (lua_State *L, gdouble weight, gint priority, enum rspamd_symbol_type type, - gint parent) + gint parent, + gboolean optional) { struct lua_callback_data *cd; gint ret = -1; @@ -850,6 +851,19 @@ rspamd_register_symbol_fromlua (lua_State *L, priority = 1; } + if ((ret = rspamd_symbols_cache_find_symbol (cfg->cache, name)) != -1) { + if (optional) { + msg_debug_config ("duplicate symbol: %s, skip registering", name); + + return ret; + } + else { + msg_err_config ("duplicate symbol: %s, skip registering", name); + + return -1; + } + } + ret = rspamd_symbols_cache_add_symbol (cfg->cache, name, priority, @@ -890,7 +904,8 @@ lua_config_register_symbol (lua_State * L) weight, 0, SYMBOL_TYPE_NORMAL, - -1); + -1, + FALSE); } } @@ -940,7 +955,9 @@ lua_config_register_symbols (lua_State *L) weight, 0, SYMBOL_TYPE_CALLBACK, - -1); + -1, + FALSE); + for (i = top; i <= lua_gettop (L); i++) { if (lua_type (L, i) == LUA_TTABLE) { lua_pushvalue (L, i); @@ -1027,7 +1044,8 @@ lua_config_register_callback_symbol (lua_State * L) weight, 0, SYMBOL_TYPE_CALLBACK, - -1); + -1, + FALSE); } lua_pushnumber (L, ret); @@ -1067,7 +1085,8 @@ lua_config_register_callback_symbol_priority (lua_State * L) weight, priority, SYMBOL_TYPE_CALLBACK, - -1); + -1, + FALSE); } lua_pushnumber (L, ret); @@ -1292,6 +1311,7 @@ lua_config_newindex (lua_State *L) struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *name; gint id; + gboolean optional = FALSE; name = luaL_checkstring (L, 2); @@ -1306,7 +1326,8 @@ lua_config_newindex (lua_State *L) 1.0, 0, SYMBOL_TYPE_NORMAL, - -1); + -1, + FALSE); } else if (lua_type (L, 3) == LUA_TTABLE) { gint type = SYMBOL_TYPE_NORMAL, priority = 0, idx; @@ -1355,6 +1376,14 @@ lua_config_newindex (lua_State *L) } lua_pop (L, 1); + lua_pushstring (L, "optional"); + lua_gettable (L, -2); + + if (lua_type (L, -1) == LUA_TBOOLEAN) { + optional = lua_toboolean (L, -1); + } + lua_pop (L, 1); + lua_pushstring (L, "type"); lua_gettable (L, -2); @@ -1383,7 +1412,8 @@ lua_config_newindex (lua_State *L) weight, priority, type, - -1); + -1, + optional); if (id != -1) { /* Check for condition */ |