From: Vsevolod Stakhov Date: Mon, 28 Sep 2015 14:53:20 +0000 (+0100) Subject: Add lua API for conditions registering. X-Git-Tag: 1.0.3~3 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5f00b5dd858cc602eca347c20e0b4ad828c6e819;p=rspamd.git Add lua API for conditions registering. --- diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index 07e10916a..ae7111f4a 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -283,6 +283,22 @@ elseif type(set_section) == "table" then end */ LUA_FUNCTION_DEF (config, get_key); + +/*** + * @method rspamd_config:add_condition(symbol, condition) + * Adds condition callback for specified symbol + * @param {string} symbol symbol's name + * @param {function} condition condition callback + * @return {boolean} true if condition has been added + * @example + +rspamd_config:add_condition('FUZZY_DENIED', function(task) + if some_map:find_key(task:get_from()) then return false end + return true +end) + */ +LUA_FUNCTION_DEF (config, add_condition); + /*** * @method rspamd_config:__newindex(name, callback) * This metamethod is called if new indicies are added to the `rspamd_config` object. @@ -357,6 +373,7 @@ static const struct luaL_reg configlib_m[] = { LUA_INTERFACE_DEF (config, register_post_filter), LUA_INTERFACE_DEF (config, get_api_version), LUA_INTERFACE_DEF (config, get_key), + LUA_INTERFACE_DEF (config, add_condition), {"__tostring", rspamd_lua_class_tostring}, {"__newindex", lua_config_newindex}, {NULL, NULL} @@ -1490,6 +1507,30 @@ lua_config_newindex (lua_State *L) return 0; } +static gint +lua_config_add_condition (lua_State *L) +{ + struct rspamd_config *cfg = lua_check_config (L, 1); + const gchar *sym = luaL_checkstring (L, 2); + gboolean ret = FALSE; + gint condref; + + if (cfg && sym && lua_type (L, 3) == LUA_TFUNCTION) { + lua_pushvalue (L, 3); + condref = luaL_ref (L, LUA_REGISTRYINDEX); + + ret = rspamd_symbols_cache_add_condition_delayed (cfg->cache, sym, L, + condref); + + if (!ret) { + luaL_unref (L, LUA_REGISTRYINDEX, condref); + } + } + + lua_pushboolean (L, ret); + return 1; +} + struct lua_map_callback_data { lua_State *L; gint ref;