diff --git a/conf/vanilla/autoload_configs/lua.conf.xml b/conf/vanilla/autoload_configs/lua.conf.xml index 1eb594f0b5..f50307ea89 100644 --- a/conf/vanilla/autoload_configs/lua.conf.xml +++ b/conf/vanilla/autoload_configs/lua.conf.xml @@ -26,5 +26,7 @@ --> + + diff --git a/src/mod/languages/mod_lua/mod_lua.cpp b/src/mod/languages/mod_lua/mod_lua.cpp index 642ad3fbf7..3d79dd8d0e 100644 --- a/src/mod/languages/mod_lua/mod_lua.cpp +++ b/src/mod/languages/mod_lua/mod_lua.cpp @@ -32,6 +32,7 @@ #include +#include SWITCH_BEGIN_EXTERN_C #include "lua.h" #include @@ -44,6 +45,7 @@ SWITCH_MODULE_DEFINITION_EX(mod_lua, mod_lua_load, mod_lua_shutdown, NULL, SMODF static struct { switch_memory_pool_t *pool; char *xml_handler; + switch_event_node_t *node; } globals; int luaopen_freeswitch(lua_State * L); @@ -287,10 +289,12 @@ static switch_xml_t lua_fetch(const char *section, } +static void lua_event_handler(switch_event_t *event); + static switch_status_t do_config(void) { const char *cf = "lua.conf"; - switch_xml_t cfg, xml, settings, param; + switch_xml_t cfg, xml, settings, param, hook; switch_stream_handle_t path_stream = {0}; switch_stream_handle_t cpath_stream = {0}; @@ -327,6 +331,31 @@ static switch_status_t do_config(void) path_stream.write_function(&path_stream, "%s", val); } } + + for (hook = switch_xml_child(settings, "hook"); hook; hook = hook->next) { + char *event = (char *) switch_xml_attr_soft(hook, "event"); + char *subclass = (char *) switch_xml_attr_soft(hook, "subclass"); + //char *script = strdup( (char *) switch_xml_attr_soft(hook, "script")); + char *script = (char *) switch_xml_attr_soft(hook, "script"); + switch_event_types_t evtype; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "hook params: '%s' | '%s' | '%s'\n", event, subclass, script); + + if (switch_name_event(event,&evtype) == SWITCH_STATUS_SUCCESS) { + if (!zstr(script)) { + if (switch_event_bind_removable(modname, evtype, !zstr(subclass) ? subclass : SWITCH_EVENT_SUBCLASS_ANY, + lua_event_handler, script, &globals.node) == SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "event handler for '%s' set to '%s'\n", switch_event_name(evtype), script); + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "cannot set event handler: unsuccessful bind\n"); + } + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "cannot set event handler: no script name\n", event); + } + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "cannot set event handler: unknown event type '%s'\n", event); + } + } } if (cpath_stream.data_len) { @@ -405,6 +434,23 @@ int lua_thread(const char *text) return 0; } +static void lua_event_handler(switch_event_t *event) +{ + lua_State *L = lua_init(); + char *script = NULL; + + if (event->bind_user_data) { + script = strdup((char *)event->bind_user_data); + } + + mod_lua_conjure_event(L, event, "event", 1); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "lua event hook: execute '%s'\n", (char *)script); + lua_parse_and_execute(L, (char *)script); + lua_uninit(L); + + switch_safe_free(script); +} + SWITCH_STANDARD_APP(lua_function) { lua_State *L = lua_init(); @@ -645,6 +691,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_lua_load) SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_lua_shutdown) { + switch_event_unbind(&globals.node); + return SWITCH_STATUS_SUCCESS; }