summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-04-08 11:24:12 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-04-08 11:24:12 +0100
commita4f8e31bff5546c25aee8bbddb5093c25afa762f (patch)
tree91e1cb33787d4d4640a426afc9602d183d3208df /src
parent257d49a97ca96348754e5e9f939f7943171393e2 (diff)
downloadrspamd-a4f8e31bff5546c25aee8bbddb5093c25afa762f.tar.gz
rspamd-a4f8e31bff5546c25aee8bbddb5093c25afa762f.zip
[Feature] Add config:set_symbol_callback function
Diffstat (limited to 'src')
-rw-r--r--src/lua/lua_config.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c
index 2317a0b87..6f994da53 100644
--- a/src/lua/lua_config.c
+++ b/src/lua/lua_config.c
@@ -418,6 +418,13 @@ LUA_FUNCTION_DEF (config, get_symbols_count);
*/
LUA_FUNCTION_DEF (config, get_symbol_callback);
+/***
+ * @method rspamd_config:set_symbol_callback(name, callback)
+ * Sets callback for the specified symbol
+ * @return {boolean} true if function has been replaced
+ */
+LUA_FUNCTION_DEF (config, set_symbol_callback);
+
static const struct luaL_reg configlib_m[] = {
LUA_INTERFACE_DEF (config, get_module_opt),
LUA_INTERFACE_DEF (config, get_mempool),
@@ -449,6 +456,7 @@ static const struct luaL_reg configlib_m[] = {
LUA_INTERFACE_DEF (config, add_on_load),
LUA_INTERFACE_DEF (config, get_symbols_count),
LUA_INTERFACE_DEF (config, get_symbol_callback),
+ LUA_INTERFACE_DEF (config, set_symbol_callback),
{"__tostring", rspamd_lua_class_tostring},
{"__newindex", lua_config_newindex},
{NULL, NULL}
@@ -1778,6 +1786,42 @@ lua_config_get_symbol_callback (lua_State *L)
return 1;
}
+static gint
+lua_config_set_symbol_callback (lua_State *L)
+{
+ struct rspamd_config *cfg = lua_check_config (L, 1);
+ const gchar *sym = luaL_checkstring (L, 2);
+ struct rspamd_abstract_callback_data *abs_cbdata;
+ struct lua_callback_data *cbd;
+
+ if (cfg != NULL && sym != NULL && lua_type (L, 3) == LUA_TFUNCTION) {
+ abs_cbdata = rspamd_symbols_cache_get_cbdata (cfg->cache, sym);
+
+ if (abs_cbdata == NULL || abs_cbdata->magic != rspamd_lua_callback_magic) {
+ lua_pushboolean (L, FALSE);
+ }
+ else {
+ cbd = (struct lua_callback_data *)abs_cbdata;
+
+ if (cbd->cb_is_ref) {
+ luaL_unref (L, LUA_REGISTRYINDEX, cbd->callback.ref);
+ }
+ else {
+ cbd->cb_is_ref = TRUE;
+ }
+
+ lua_pushvalue (L, 3);
+ cbd->callback.ref = luaL_ref (L, LUA_REGISTRYINDEX);
+ lua_pushboolean (L, TRUE);
+ }
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ return 1;
+}
+
void
luaopen_config (lua_State * L)
{