struct metric_action actions[METRIC_ACTION_MAX]; /**< all actions of the metric */
};
+struct rspamd_config_post_load_script {
+ gint cbref;
+ struct rspamd_config_post_load_script *prev, *next;
+};
+
/**
* Structure that stores all config data
*/
GHashTable *trusted_keys; /**< list of trusted public keys */
+ struct rspamd_config_post_load_script *on_load; /**< list of scripts executed on config load */
+
ref_entry_t ref; /**< reference counter */
};
struct timespec ts;
#endif
struct metric *def_metric;
+ struct rspamd_config_post_load_script *sc;
+ struct rspamd_config **pcfg;
#ifdef HAVE_CLOCK_GETTIME
#ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID
/* Config other libraries */
rspamd_config_libs (cfg->libs_ctx, cfg);
+ /* Execute post load scripts */
+ LL_FOREACH (cfg->on_load, sc) {
+ lua_rawgeti (cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref);
+ pcfg = lua_newuserdata (cfg->lua_state, sizeof (*pcfg));
+ *pcfg = cfg;
+ rspamd_lua_setclass (cfg->lua_state, "rspamd{config}", -1);
+
+ if (lua_pcall (cfg->lua_state, 1, 0, 0) != 0) {
+ msg_err_config ("error executing post load code: %s",
+ lua_tostring (cfg->lua_state, -1));
+ lua_pop (cfg->lua_state, 1);
+
+ return FALSE;
+ }
+ }
+
/* Validate cache */
if (validate_cache) {
return rspamd_symbols_cache_validate (cfg->cache, cfg, FALSE);
* of script function depends on worker type
* @param {string} worker_type worker type (e.g. "normal")
* @param {function} script script for a worker
+ * @return {boolean} `true` if a script has been registered
*/
LUA_FUNCTION_DEF (config, register_worker_script);
+/***
+ * @method rspamd_config:add_on_load(function(cfg) ... end)
+ * Registers the following script to be executed when configuration is completely loaded
+ * @param {function} script function to be executed
+ */
+LUA_FUNCTION_DEF (config, add_on_load);
+
static const struct luaL_reg configlib_m[] = {
LUA_INTERFACE_DEF (config, get_module_opt),
LUA_INTERFACE_DEF (config, get_mempool),
LUA_INTERFACE_DEF (config, register_regexp),
LUA_INTERFACE_DEF (config, replace_regexp),
LUA_INTERFACE_DEF (config, register_worker_script),
+ LUA_INTERFACE_DEF (config, add_on_load),
{"__tostring", rspamd_lua_class_tostring},
{"__newindex", lua_config_newindex},
{NULL, NULL}
return 1;
}
+static gint
+lua_config_add_on_load (lua_State *L)
+{
+ struct rspamd_config *cfg = lua_check_config (L, 1);
+ struct rspamd_config_post_load_script *sc;
+
+ if (cfg == NULL || lua_type (L, 2) != LUA_TFUNCTION) {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ sc = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*sc));
+ lua_pushvalue (L, 2);
+ sc->cbref = luaL_ref (L, LUA_REGISTRYINDEX);
+ DL_APPEND (cfg->on_load, sc);
+
+ return 0;
+}
+
void
luaopen_config (lua_State * L)
{