if (periodic->need_modify || periodic->cbdata.errored) {
/* Need to notify the real data structure */
periodic->map->fin_callback (&periodic->cbdata, periodic->map->user_data);
+
+ if (map->on_load_function) {
+ map->on_load_function(map, map->on_load_ud);
+ }
}
else {
/* Not modified */
if (succeed) {
map->fin_callback (&fake_cbd.cbdata, map->user_data);
+
+ if (map->on_load_function) {
+ map->on_load_function(map, map->on_load_ud);
+ }
}
else {
msg_info_map ("preload of %s failed", map->name);
*map->user_data = NULL;
}
+ if (map->on_load_ud_dtor) {
+ map->on_load_ud_dtor(map->on_load_ud);
+ }
+
for (i = 0; i < map->backends->len; i ++) {
bk = g_ptr_array_index (map->backends, i);
map->traverse_function (*map->user_data, cb, cbdata, reset_hits);
}
}
+
+void
+rspamd_map_set_on_load_function (struct rspamd_map *map, rspamd_map_on_load_function cb,
+ gpointer cbdata, GDestroyNotify dtor)
+{
+ if (map) {
+ map->on_load_function = cb;
+ map->on_load_ud = cbdata;
+ map->on_load_ud_dtor = dtor;
+ }
+}
struct map_cb_data;
struct rspamd_worker;
+/**
+ * Common map object
+ */
+struct rspamd_config;
+struct rspamd_map;
+
/**
* Callback types
*/
typedef void (*rspamd_map_traverse_function) (void *data,
rspamd_map_traverse_cb cb,
gpointer cbdata, gboolean reset_hits);
-
-/**
- * Common map object
- */
-struct rspamd_config;
-struct rspamd_map;
+typedef void (*rspamd_map_on_load_function) (struct rspamd_map *map, gpointer ud);
/**
* Callback data for async load
void rspamd_map_traverse (struct rspamd_map *map, rspamd_map_traverse_cb cb,
gpointer cbdata, gboolean reset_hits);
+/**
+ * Set map on load callback
+ * @param map
+ * @param cb
+ * @param cbdata
+ */
+void rspamd_map_set_on_load_function (struct rspamd_map *map, rspamd_map_on_load_function cb,
+ gpointer cbdata, GDestroyNotify dtor);
+
#ifdef __cplusplus
}
#endif
*/
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
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},
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);
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)
{