]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-5524 --resolve
authorKen Rice <krice@freeswitch.org>
Wed, 10 Jul 2013 16:26:41 +0000 (11:26 -0500)
committerKen Rice <krice@freeswitch.org>
Wed, 10 Jul 2013 16:49:59 +0000 (11:49 -0500)
conf/vanilla/autoload_configs/lua.conf.xml
src/mod/languages/mod_lua/mod_lua.cpp

index 1eb594f0b5d5774f7fb9b412a11285b12a3593a9..f50307ea89ba6469d0d08bf2d8b9803e1214882b 100644 (file)
@@ -26,5 +26,7 @@
     -->
     <!--<param name="startup-script" value="startup_script_1.lua"/>-->
     <!--<param name="startup-script" value="startup_script_2.lua"/>-->
+    
+    <!--<hook event="CUSTOM" subclass="conference::maintenance" script="catch-event.lua"/>-->
   </settings>
 </configuration>
index 642ad3fbf79aff21a9e1c02dd1509ce857a7c7e2..3d79dd8d0ea2a4956436bdc0b180fa6cf204b4d3 100644 (file)
@@ -32,6 +32,7 @@
 
 
 #include <switch.h>
+#include <switch_event.h>
 SWITCH_BEGIN_EXTERN_C
 #include "lua.h"
 #include <lauxlib.h>
@@ -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;
 }