aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rspamd.com>2023-05-27 15:02:34 +0100
committerVsevolod Stakhov <vsevolod@rspamd.com>2023-05-27 15:02:34 +0100
commit02c28f369b72d97d75cded4000675b98411a0fcb (patch)
tree30a3aaa2025d55b691f89445d6ee6189ec2152a0 /src/lua
parent92f695cc48a3611a2aaf14944d641343c52e504a (diff)
downloadrspamd-02c28f369b72d97d75cded4000675b98411a0fcb.tar.gz
rspamd-02c28f369b72d97d75cded4000675b98411a0fcb.zip
[Feature] Maps: Add on_load support
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/lua_map.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/lua/lua_map.c b/src/lua/lua_map.c
index fe01c3031..29e2053f2 100644
--- a/src/lua/lua_map.c
+++ b/src/lua/lua_map.c
@@ -136,6 +136,13 @@ LUA_FUNCTION_DEF (map, get_stats);
LUA_FUNCTION_DEF (map, foreach);
/***
+ * @method map:on_load(callback)
+ * Sets a callback for a map that is called when map is loaded
+ * @param {function} callback callback function, that accepts no arguments (pass maps in a closure if needed)
+ */
+LUA_FUNCTION_DEF (map, on_load);
+
+/***
* @method map:get_data_digest()
* Get data digest for specific map
* @return {string} 64 bit number represented as string (due to Lua limitations)
@@ -159,6 +166,7 @@ static const struct luaL_reg maplib_m[] = {
LUA_INTERFACE_DEF (map, get_uri),
LUA_INTERFACE_DEF (map, get_stats),
LUA_INTERFACE_DEF (map, foreach),
+ LUA_INTERFACE_DEF (map, on_load),
LUA_INTERFACE_DEF (map, get_data_digest),
LUA_INTERFACE_DEF (map, get_nelts),
{"__tostring", rspamd_lua_class_tostring},
@@ -1131,6 +1139,7 @@ lua_map_foreach (lua_State * L)
cbdata.L = L;
lua_pushvalue (L, 2); /* func */
cbdata.cbref = lua_gettop (L);
+ cbdata.use_text = use_text;
if (map->map->traverse_function) {
rspamd_map_traverse (map->map, lua_map_foreach_cb, &cbdata, FALSE);
@@ -1364,6 +1373,58 @@ lua_map_get_uri (lua_State *L)
return map->map->backends->len;
}
+struct lua_map_on_load_cbdata {
+ lua_State *L;
+ gint ref;
+};
+
+static void
+lua_map_on_load_dtor (gpointer p)
+{
+ struct lua_map_on_load_cbdata *cbd = p;
+
+ luaL_unref (cbd->L, LUA_REGISTRYINDEX, cbd->ref);
+ g_free (cbd);
+}
+
+static void
+lua_map_on_load_handler (struct rspamd_map *map, gpointer ud)
+{
+ struct lua_map_on_load_cbdata *cbd = ud;
+ lua_State *L = cbd->L;
+
+ lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->ref);
+
+ if (lua_pcall(L, 0, 0, 0) != 0) {
+ msg_err_map ("call to on_load function failed: %s", lua_tostring (L, -1));
+ }
+}
+
+static gint
+lua_map_on_load (lua_State *L)
+{
+ LUA_TRACE_POINT;
+ struct rspamd_lua_map *map = lua_check_map (L, 1);
+
+ if (map == NULL) {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ if (lua_type (L, 2) == LUA_TFUNCTION) {
+ lua_pushvalue (L, 2);
+ struct lua_map_on_load_cbdata *cbd = g_malloc (sizeof (struct lua_map_on_load_cbdata));
+ cbd->L = L;
+ cbd->ref = luaL_ref (L, LUA_REGISTRYINDEX);
+
+ rspamd_map_set_on_load_function(map->map, lua_map_on_load_handler, cbd, lua_map_on_load_dtor);
+ }
+ else {
+ return luaL_error (L, "invalid callback");
+ }
+
+ return 0;
+}
+
void
luaopen_map (lua_State * L)
{