Revision
15316
Author
ledr
Date
2009-11-02 05:54:12 -0600 (Mon, 02 Nov 2009)

Log Message

See if I can call a LUA script directly as dialplan

Added Paths

Diff

Added: freeswitch/trunk/contrib/ledr/lua/lua_dialplan.diff (0 => 15316)


--- freeswitch/trunk/contrib/ledr/lua/lua_dialplan.diff	                        (rev 0)
+++ freeswitch/trunk/contrib/ledr/lua/lua_dialplan.diff	2009-11-02 11:54:12 UTC (rev 15316)
@@ -0,0 +1,68 @@
+Index: mod_lua.cpp
+===================================================================
+--- mod_lua.cpp	(revision 15305)
++++ mod_lua.cpp	(working copy)
+@@ -461,10 +461,55 @@
+ 	return SWITCH_STATUS_SUCCESS;
+ }
+ 
++SWITCH_STANDARD_DIALPLAN(lua_dialplan_hunt)
++{
++	lua_State *L = lua_init();
++	switch_caller_extension_t *extension = NULL;
++	switch_channel_t *channel = switch_core_session_get_channel(session);
++	char *cmd;
++	switch_stream_handle_t stream = { 0 };
++	SWITCH_STANDARD_STREAM(stream);
++
++	if (!caller_profile) {
++		if (!(caller_profile = switch_channel_get_caller_profile(channel))) {
++			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error Obtaining Profile!\n");
++			goto done;
++		}
++	}
++
++	if (!caller_profile->context) {
++		caller_profile->context = "lua/dialplan.lua";
++	}
++
++	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Processing %s->%s in script %s\n",
++	          caller_profile->caller_id_name, caller_profile->destination_number, caller_profile->context);
++
++	if ((extension = switch_caller_extension_new(session, "_anon_", caller_profile->destination_number)) == 0) {
++		switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Memory Error!\n");
++		goto done;
++	}
++
++	cmd = strdup(caller_profile->context);
++	switch_assert(cmd);
++
++	mod_lua_conjure_session(L, session, "session", 1);
++	mod_lua_conjure_stream(L, &stream, "stream", 1);
++
++	lua_parse_and_execute(L, cmd); // TODO is it possible to call switch_caller_extension_add_application(session, *extension, application, app_data) from the script ?
++	lua_uninit(L);
++	free(cmd);
++
++	// loop through everything returned by stream:write and add it to 'switch_caller_extension_add_application'
++
++ done:
++	return extension;
++}
++
+ SWITCH_MODULE_LOAD_FUNCTION(mod_lua_load)
+ {
+ 	switch_api_interface_t *api_interface;
+ 	switch_application_interface_t *app_interface;
++	switch_dialplan_interface_t *dp_interface;
+ 
+ 	/* connect my internal structure to the blank pointer passed to me */
+ 	*module_interface = switch_loadable_module_create_module_interface(pool, modname);
+@@ -472,6 +517,7 @@
+ 	SWITCH_ADD_API(api_interface, "luarun", "run a script", luarun_api_function, "<script>");
+ 	SWITCH_ADD_API(api_interface, "lua", "run a script as an api function", lua_api_function, "<script>");
+ 	SWITCH_ADD_APP(app_interface, "lua", "Launch LUA ivr", "Run a lua ivr on a channel", lua_function, "<script>", SAF_SUPPORT_NOMEDIA);
++	SWITCH_ADD_DIALPLAN(dp_interface, "LUA", lua_dialplan_hunt);
+ 
+ 
+