提交 59526679 authored 作者: Anthony Minessale's avatar Anthony Minessale

update

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8214 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 f2994a09
...@@ -11,6 +11,11 @@ extern "C" { ...@@ -11,6 +11,11 @@ extern "C" {
#include <switch.h> #include <switch.h>
#define sanity_check(x) do { if (!(session && allocated)) { switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "session is not initalized\n"); return x;}} while(0)
#define sanity_check_noreturn do { if (!(session && allocated)) { switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "session is not initalized\n"); return;}} while(0)
#define init_vars() do { session = NULL; channel = NULL; uuid = NULL; tts_name = NULL; voice_name = NULL; memset(&args, 0, sizeof(args)); ap = NULL; caller_profile.source = "mod_unknown"; caller_profile.dialplan = ""; caller_profile.context = ""; caller_profile.caller_id_name = ""; caller_profile.caller_id_number = ""; caller_profile.network_addr = ""; caller_profile.ani = ""; caller_profile.aniii = ""; caller_profile.rdnis = ""; caller_profile.username = ""; on_hangup = NULL; memset(&cb_state, 0, sizeof(cb_state)); hook_state = CS_NEW; } while(0)
// //
// C++ Interface: switch_to_cpp_mempool // C++ Interface: switch_to_cpp_mempool
// //
...@@ -62,19 +67,6 @@ char *api_execute(char *cmd, char *arg); ...@@ -62,19 +67,6 @@ char *api_execute(char *cmd, char *arg);
void api_reply_delete(char *reply); void api_reply_delete(char *reply);
/**
* \brief - process language specific callback results
*
* First the actual callback is called, (in the case of python,
* PythonDTMFCallback), it then calls the language specific callback defined
* by the user (eg, some python method), and then _this_ function is called
* with the results of the language specific callback.
*/
switch_status_t process_callback_result(char *raw_result,
struct input_callback_state *cb_state,
switch_core_session_t *session);
typedef struct input_callback_state { typedef struct input_callback_state {
void *function; // pointer to the language specific callback function void *function; // pointer to the language specific callback function
// eg, PyObject *pyfunc // eg, PyObject *pyfunc
...@@ -105,10 +97,15 @@ class Stream { ...@@ -105,10 +97,15 @@ class Stream {
class Event { class Event {
protected: protected:
switch_event_t *event;
public: public:
switch_event_t *event;
char *serialized_string;
int mine;
Event(const char *type, const char *subclass_name = NULL); Event(const char *type, const char *subclass_name = NULL);
Event(switch_event_t *wrap_me, int free_me=0);
virtual ~Event(); virtual ~Event();
const char *serialize(const char *format=NULL);
bool set_priority(switch_priority_t priority = SWITCH_PRIORITY_NORMAL); bool set_priority(switch_priority_t priority = SWITCH_PRIORITY_NORMAL);
char *get_header(char *header_name); char *get_header(char *header_name);
char *get_body(void); char *get_body(void);
...@@ -131,8 +128,9 @@ class CoreSession { ...@@ -131,8 +128,9 @@ class CoreSession {
char *voice_name; char *voice_name;
void store_file_handle(switch_file_handle_t *fh); void store_file_handle(switch_file_handle_t *fh);
void *on_hangup; // language specific callback function, cast as void * void *on_hangup; // language specific callback function, cast as void *
switch_file_handle_t local_fh;
switch_file_handle_t *fhp;
switch_status_t process_callback_result(char *ret);
public: public:
CoreSession(); CoreSession();
CoreSession(char *uuid); CoreSession(char *uuid);
...@@ -150,8 +148,10 @@ class CoreSession { ...@@ -150,8 +148,10 @@ class CoreSession {
int preAnswer(); int preAnswer();
virtual void hangup(char *cause = "normal_clearing"); virtual void hangup(char *cause = "normal_clearing");
void setVariable(char *var, char *val); void setVariable(char *var, char *val);
void setPrivate(char *var, void *val);
void *getPrivate(char *var);
const char *getVariable(char *var); const char *getVariable(char *var);
/** \brief Record to a file /** \brief Record to a file
* \param filename * \param filename
...@@ -193,7 +193,6 @@ class CoreSession { ...@@ -193,7 +193,6 @@ class CoreSession {
*/ */
void setDTMFCallback(void *cbfunc, char *funcargs); void setDTMFCallback(void *cbfunc, char *funcargs);
int speak(char *text); int speak(char *text);
void set_tts_parms(char *tts_name, char *voice_name); void set_tts_parms(char *tts_name, char *voice_name);
...@@ -265,6 +264,9 @@ class CoreSession { ...@@ -265,6 +264,9 @@ class CoreSession {
bool ready(); bool ready();
void execute(char *app, char *data); void execute(char *app, char *data);
void sendEvent(Event *sendME);
virtual bool begin_allow_threads() = 0; virtual bool begin_allow_threads() = 0;
virtual bool end_allow_threads() = 0; virtual bool end_allow_threads() = 0;
......
...@@ -3,12 +3,12 @@ LOCAL_CFLAGS=-Ilua ...@@ -3,12 +3,12 @@ LOCAL_CFLAGS=-Ilua
LIBLUA_A=lua/liblua.a LIBLUA_A=lua/liblua.a
LOCAL_LIBADD=$(LIBLUA_A) LOCAL_LIBADD=$(LIBLUA_A)
LOCAL_LDFLAGS=-lm LOCAL_LDFLAGS=-lm
LOCAL_OBJS=freeswitch_lua.o mod_lua_wrap.o #glua.o LOCAL_OBJS=freeswitch_lua.o mod_lua_wrap.o
VERBOSE=1 VERBOSE=1
include $(BASE)/build/modmake.rules include $(BASE)/build/modmake.rules
glua.o: mod_lua_wrap.cpp local_depend: $(LOCAL_OBJS)
$(LIBLUA_A): $(LIBLUA_A):
cd lua && $(MAKE) CFLAGS="$(ALL_CFLAGS) -DLUA_USE_LINUX -w" liblua.a cd lua && $(MAKE) CFLAGS="$(ALL_CFLAGS) -DLUA_USE_LINUX -w" liblua.a
...@@ -16,14 +16,18 @@ $(LIBLUA_A): ...@@ -16,14 +16,18 @@ $(LIBLUA_A):
luaclean: luaclean:
cd lua && $(MAKE) clean cd lua && $(MAKE) clean
freeswitch_lua.o: freeswitch_lua.h
allclean: clean luaclean allclean: clean luaclean
rm -f mod_lua_wrap.* freeswitch_lua.$(DYNAMIC_LIB_EXTEN) rm -f mod_lua_wrap.* freeswitch_lua.$(DYNAMIC_LIB_EXTEN)
swigclean: swigclean:
rm mod_lua_wrap.* rm mod_lua_wrap.*
mod_lua_wrap.cpp: mod_lua_wrap.cpp: mod_lua_extra.c
swig -lua -c++ -I../../../../src/include -oh mod_lua_wrap.h -o mod_lua_wrap.cpp freeswitch.i swig -lua -c++ -I../../../../src/include -oh mod_lua_wrap.h -o mod_lua_wrap.cpp freeswitch.i
echo "#include \"mod_lua_extra.c\"" >> mod_lua_wrap.cpp
patch -s -p0 -i hack.diff
freeswitch_lua.$(DYNAMIC_LIB_EXTEN): $(LOCAL_OBJS) $(LOCAL_LIBADD) freeswitch_lua.$(DYNAMIC_LIB_EXTEN): $(LOCAL_OBJS) $(LOCAL_LIBADD)
$(LINK) $(SOLINK) -o freeswitch_lua.$(DYNAMIC_LIB_EXTEN) $(LOCAL_OBJS) $(LOCAL_LIBADD) $(LDFLAGS) $(LINK) $(SOLINK) -o freeswitch_lua.$(DYNAMIC_LIB_EXTEN) $(LOCAL_OBJS) $(LOCAL_LIBADD) $(LDFLAGS)
#include "freeswitch_lua.h" #include "freeswitch_lua.h"
SWITCH_BEGIN_EXTERN_C
#include "lua.h"
#include <lauxlib.h>
#include <lualib.h>
#include "mod_lua_extra.h"
SWITCH_END_EXTERN_C
Session::Session() : CoreSession() Session::Session() : CoreSession()
{ {
cb_function = cb_arg = hangup_func_str = NULL;
} }
Session::Session(char *uuid) : CoreSession(uuid) Session::Session(char *uuid) : CoreSession(uuid)
{ {
cb_function = cb_arg = hangup_func_str = NULL;
} }
Session::Session(switch_core_session_t *new_session) : CoreSession(new_session) Session::Session(switch_core_session_t *new_session) : CoreSession(new_session)
{ {
cb_function = cb_arg = hangup_func_str = NULL;
} }
static switch_status_t lua_hanguphook(switch_core_session_t *session_hungup);
Session::~Session() Session::~Session()
{ {
switch_safe_free(cb_function);
switch_safe_free(cb_arg);
switch_safe_free(hangup_func_str);
switch_core_event_hook_remove_state_change(session, lua_hanguphook);
} }
...@@ -33,11 +44,145 @@ bool Session::end_allow_threads() ...@@ -33,11 +44,145 @@ bool Session::end_allow_threads()
void Session::check_hangup_hook() void Session::check_hangup_hook()
{ {
if (hangup_func_str && (hook_state == CS_HANGUP || hook_state == CS_RING)) {
lua_State *L;
L = (lua_State *) getPrivate("__L");
if (!L) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Doh!\n");
return;
}
lua_getfield(L, LUA_GLOBALSINDEX, (char *)hangup_func_str);
lua_pushstring(L, hook_state == CS_HANGUP ? "hangup" : "transfer");
lua_call(L, 1, 0);
}
}
static switch_status_t lua_hanguphook(switch_core_session_t *session_hungup)
{
switch_channel_t *channel = switch_core_session_get_channel(session_hungup);
CoreSession *coresession = NULL;
switch_channel_state_t state = switch_channel_get_state(channel);
if ((coresession = (CoreSession *) switch_channel_get_private(channel, "CoreSession"))) {
if (coresession->hook_state != state) {
coresession->hook_state = state;
coresession->check_hangup_hook();
}
}
return SWITCH_STATUS_SUCCESS;
}
void Session::setHangupHook(char *func) {
sanity_check_noreturn;
switch_safe_free(hangup_func_str);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Not Currently Available\n");
func = NULL;
if (func) {
hangup_func_str = strdup(func);
switch_channel_set_private(channel, "CoreSession", this);
hook_state = switch_channel_get_state(channel);
switch_core_event_hook_add_state_change(session, lua_hanguphook);
}
}
void Session::setInputCallback(char *cbfunc, char *funcargs) {
sanity_check_noreturn;
switch_safe_free(cb_function);
if (cbfunc) {
cb_function = strdup(cbfunc);
}
switch_safe_free(cb_arg);
if (funcargs) {
cb_arg = strdup(funcargs);
}
args.buf = this;
switch_channel_set_private(channel, "CoreSession", this);
args.input_callback = dtmf_callback;
ap = &args;
} }
switch_status_t Session::run_dtmf_callback(void *input, switch_input_type_t itype) switch_status_t Session::run_dtmf_callback(void *input, switch_input_type_t itype)
{ {
return SWITCH_STATUS_FALSE; lua_State *L;
const char *ret;
L = (lua_State *) getPrivate("__L");
if (!L) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Doh!\n");
return SWITCH_STATUS_FALSE;
}
switch (itype) {
case SWITCH_INPUT_TYPE_DTMF:
{
switch_dtmf_t *dtmf = (switch_dtmf_t *) input;
char str[2] = "";
int arg_count = 2;
lua_getfield(L, LUA_GLOBALSINDEX, (char *)cb_function);
lua_pushstring(L, "dtmf");
lua_newtable(L);
lua_pushstring(L, "digit");
str[0] = dtmf->digit;
lua_pushstring(L, str);
lua_rawset(L, -3);
lua_pushstring(L, "duration");
lua_pushnumber(L, dtmf->duration);
lua_rawset(L, -3);
if (cb_arg) {
lua_getfield(L, LUA_GLOBALSINDEX, (char *)cb_arg);
arg_count++;
}
lua_call(L, arg_count, 1);
ret = lua_tostring(L, -1);
lua_pop(L, 1);
return process_callback_result((char *)ret);
}
break;
case SWITCH_INPUT_TYPE_EVENT:
{
switch_event_t *event = (switch_event_t *) input;
int arg_count = 2;
lua_getfield(L, LUA_GLOBALSINDEX, (char *)cb_function);
lua_pushstring(L, "event");
mod_lua_conjure_event(L, event, "__Input_Event__", 1);
lua_getfield(L, LUA_GLOBALSINDEX, "__Input_Event__");
if (cb_arg) {
lua_getfield(L, LUA_GLOBALSINDEX, (char *)cb_arg);
arg_count++;
}
lua_call(L, 2, 1);
ret = lua_tostring(L, -1);
lua_pop(L, 1);
return process_callback_result((char *)ret);
}
break;
}
return SWITCH_STATUS_SUCCESS;
} }
......
#ifndef FREESWITCH_PYTHON_H #ifndef FREESWITCH_LUA_H
#define FREESWITCH_PYTHON_H #define FREESWITCH_LUA_H
#include <switch_cpp.h> #include <switch_cpp.h>
...@@ -10,7 +10,6 @@ void api_reply_delete(char *reply); ...@@ -10,7 +10,6 @@ void api_reply_delete(char *reply);
class Session : public CoreSession { class Session : public CoreSession {
private:
public: public:
Session(); Session();
Session(char *uuid); Session(char *uuid);
...@@ -21,58 +20,12 @@ class Session : public CoreSession { ...@@ -21,58 +20,12 @@ class Session : public CoreSession {
virtual bool end_allow_threads(); virtual bool end_allow_threads();
virtual void check_hangup_hook(); virtual void check_hangup_hook();
virtual switch_status_t run_dtmf_callback(void *input, switch_input_type_t itype); virtual switch_status_t run_dtmf_callback(void *input, switch_input_type_t itype);
void setInputCallback(char *cbfunc, char *funcargs);
void setHangupHook(char *func);
switch_core_session_t *session; char *cb_function;
switch_channel_t *channel; char *cb_arg;
unsigned int flags; char *hangup_func_str;
int allocated;
input_callback_state cb_state; // callback state, always pointed to by the buf
// field in this->args
switch_channel_state_t hook_state; // store hookstate for on_hangup callback
#if 0
int answer();
int preAnswer();
virtual void hangup(char *cause);
void setVariable(char *var, char *val);
const char *getVariable(char *var);
int recordFile(char *file_name, int max_len=0, int silence_threshold=0, int silence_secs=0);
void setCallerData(char *var, char *val);
int originate(CoreSession *a_leg_session, char *dest, int timeout=60);
void setDTMFCallback(void *cbfunc, char *funcargs);
int speak(char *text);
void set_tts_parms(char *tts_name, char *voice_name);
int collectDigits(int timeout);
int getDigits(char *dtmf_buf,
switch_size_t buflen,
switch_size_t maxdigits,
char *terminators,
char *terminator,
int timeout);
int transfer(char *extensions, char *dialplan, char *context);
int playAndGetDigits(int min_digits,
int max_digits,
int max_tries,
int timeout,
char *terminators,
char *audio_files,
char *bad_input_audio_files,
char *dtmf_buf,
char *digits_regex);
int streamFile(char *file, int starting_sample_count=0);
int flushEvents();
int flushDigits();
int setAutoHangup(bool val);
void setHangupHook(void *hangup_func);
bool ready();
void execute(char *app, char *data);
char* get_uuid();
const switch_input_args_t& get_cb_args();
#endif
}; };
#endif #endif
--- hack.cpp 2008-04-29 13:49:22.000000000 -0400
+++ mod_lua_wrap.cpp 2008-04-29 13:49:10.000000000 -0400
@@ -4028,7 +4028,7 @@
SWIG_check_num_args("Session",0,0)
result = (Session *)new Session();
SWIG_arg=0;
- SWIG_NewPointerObj(L,result,SWIGTYPE_p_Session,1); SWIG_arg++;
+ SWIG_NewPointerObj(L,result,SWIGTYPE_p_Session,1); SWIG_arg++; result->setPrivate("__L", L);
return SWIG_arg;
if(0) SWIG_fail;
@@ -4049,7 +4049,7 @@
arg1 = (char *)lua_tostring(L, 1);
result = (Session *)new Session(arg1);
SWIG_arg=0;
- SWIG_NewPointerObj(L,result,SWIGTYPE_p_Session,1); SWIG_arg++;
+ SWIG_NewPointerObj(L,result,SWIGTYPE_p_Session,1); SWIG_arg++; result->setPrivate("__L", L);
return SWIG_arg;
if(0) SWIG_fail;
@@ -4074,7 +4074,7 @@
result = (Session *)new Session(arg1);
SWIG_arg=0;
- SWIG_NewPointerObj(L,result,SWIGTYPE_p_Session,1); SWIG_arg++;
+ SWIG_NewPointerObj(L,result,SWIGTYPE_p_Session,1); SWIG_arg++; result->setPrivate("__L", L);
return SWIG_arg;
if(0) SWIG_fail;
差异被折叠。
...@@ -33,16 +33,27 @@ ...@@ -33,16 +33,27 @@
#include "lua.h" #include "lua.h"
#include <lauxlib.h> #include <lauxlib.h>
#include <lualib.h> #include <lualib.h>
#include "mod_lua_extra.h"
SWITCH_MODULE_LOAD_FUNCTION(mod_lua_load); SWITCH_MODULE_LOAD_FUNCTION(mod_lua_load);
SWITCH_MODULE_DEFINITION(mod_lua, mod_lua_load, NULL, NULL); SWITCH_MODULE_DEFINITION(mod_lua, mod_lua_load, NULL, NULL);
static struct { static struct {
switch_memory_pool_t *pool; switch_memory_pool_t *pool;
char *xml_handler;
} globals; } globals;
int luaopen_freeswitch(lua_State* L); int luaopen_freeswitch(lua_State* L);
static int panic (lua_State *L)
{
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "unprotected error in call to Lua API (%s)\n",
lua_tostring(L, -1));
return 0;
}
static lua_State *lua_init(void) static lua_State *lua_init(void)
{ {
lua_State *L = lua_open(); lua_State *L = lua_open();
...@@ -51,6 +62,7 @@ static lua_State *lua_init(void) ...@@ -51,6 +62,7 @@ static lua_State *lua_init(void)
luaL_openlibs(L); luaL_openlibs(L);
luaopen_freeswitch(L); luaopen_freeswitch(L);
lua_gc(L, LUA_GCRESTART, 0); lua_gc(L, LUA_GCRESTART, 0);
lua_atpanic(L, panic);
} }
return L; return L;
} }
...@@ -144,6 +156,11 @@ static void *SWITCH_THREAD_FUNC lua_thread_run(switch_thread_t *thread, void *ob ...@@ -144,6 +156,11 @@ static void *SWITCH_THREAD_FUNC lua_thread_run(switch_thread_t *thread, void *ob
char *input_code = (char *) obj; char *input_code = (char *) obj;
lua_State *L = lua_init(); /* opens Lua */ lua_State *L = lua_init(); /* opens Lua */
//switch_event_t *event;
//switch_event_create(&event, SWITCH_EVENT_MESSAGE);
//switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "testing", "1234");
//mod_lua_conjure_event(L, event, "blah");
lua_parse_and_execute(L, input_code); lua_parse_and_execute(L, input_code);
if (input_code) { if (input_code) {
...@@ -155,6 +172,96 @@ static void *SWITCH_THREAD_FUNC lua_thread_run(switch_thread_t *thread, void *ob ...@@ -155,6 +172,96 @@ static void *SWITCH_THREAD_FUNC lua_thread_run(switch_thread_t *thread, void *ob
return NULL; return NULL;
} }
static switch_xml_t lua_fetch(const char *section,
const char *tag_name,
const char *key_name,
const char *key_value,
switch_event_t *params,
void *user_data)
{
switch_xml_t xml = NULL;
if (!switch_strlen_zero(globals.xml_handler)) {
lua_State *L = lua_init();
char *mycmd = strdup(globals.xml_handler);
const char *str;
switch_assert(mycmd);
lua_newtable(L);
lua_pushstring(L, "section");
lua_pushstring(L, switch_str_nil(section));
lua_rawset(L, -3);
lua_pushstring(L, "tag_name");
lua_pushstring(L, switch_str_nil(tag_name));
lua_rawset(L, -3);
lua_pushstring(L, "key_name");
lua_pushstring(L, switch_str_nil(key_name));
lua_rawset(L, -3);
lua_pushstring(L, "key_value");
lua_pushstring(L, switch_str_nil(key_value));
lua_rawset(L, -3);
lua_setglobal(L, "XML_REQUEST");
if (params) {
mod_lua_conjure_event(L, params, "params", 1);
}
lua_parse_and_execute(L, mycmd);
lua_getfield(L, LUA_GLOBALSINDEX, "XML_STRING");
str = lua_tostring(L, 1);
if (str) {
if (switch_strlen_zero(str)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No Result\n");
} else if (!(xml = switch_xml_parse_str((char *)str, strlen(str)))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing XML Result!\n");
}
}
lua_uninit(L);
free(mycmd);
}
return xml;
}
static switch_status_t do_config(void)
{
char *cf = "lua.conf";
switch_xml_t cfg, xml, settings, param;
if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
return SWITCH_STATUS_TERM;
}
if ((settings = switch_xml_child(cfg, "settings"))) {
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
char *var = (char *) switch_xml_attr_soft(param, "name");
char *val = (char *) switch_xml_attr_soft(param, "value");
if (!strcmp(var, "xml-handler-script")) {
globals.xml_handler = switch_core_strdup(globals.pool, val);
} else if (!strcmp(var, "xml-handler-bindings")) {
if (!switch_strlen_zero(globals.xml_handler)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "binding '%s' to '%s'\n", globals.xml_handler, val);
switch_xml_bind_search_function(lua_fetch, switch_xml_parse_section_string(val), NULL);
}
}
}
}
switch_xml_free(xml);
return SWITCH_STATUS_SUCCESS;
}
int lua_thread(const char *text) int lua_thread(const char *text)
{ {
switch_thread_t *thread; switch_thread_t *thread;
...@@ -172,20 +279,36 @@ SWITCH_STANDARD_APP(lua_function) ...@@ -172,20 +279,36 @@ SWITCH_STANDARD_APP(lua_function)
{ {
lua_State *L = lua_init(); lua_State *L = lua_init();
char code[1024] = ""; char code[1024] = "";
snprintf(code, sizeof(code), "~session = LUASession:new(\"%s\");", switch_core_session_get_uuid(session)); snprintf(code, sizeof(code), "~session = freeswitch.Session(\"%s\");", switch_core_session_get_uuid(session));
lua_parse_and_execute(L, code); lua_parse_and_execute(L, code);
lua_parse_and_execute(L, (char *) data); lua_parse_and_execute(L, (char *) data);
snprintf(code, sizeof(code), "~session:delete()");
lua_parse_and_execute(L, code);
lua_uninit(L); lua_uninit(L);
} }
SWITCH_STANDARD_API(lua_api_function) { SWITCH_STANDARD_API(luarun_api_function) {
lua_thread(cmd); lua_thread(cmd);
stream->write_function(stream, "+OK\n"); stream->write_function(stream, "+OK\n");
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
SWITCH_STANDARD_API(lua_api_function) {
lua_State *L = lua_init();
char *mycmd = strdup(cmd);
switch_assert(mycmd);
mod_lua_conjure_stream(L, stream, "stream", 1);
if (stream->event) {
mod_lua_conjure_event(L, stream->event, "env", 1);
}
lua_parse_and_execute(L, mycmd);
lua_uninit(L);
free(mycmd);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_LOAD_FUNCTION(mod_lua_load) SWITCH_MODULE_LOAD_FUNCTION(mod_lua_load)
{ {
switch_api_interface_t *api_interface; switch_api_interface_t *api_interface;
...@@ -194,12 +317,14 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_lua_load) ...@@ -194,12 +317,14 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_lua_load)
/* connect my internal structure to the blank pointer passed to me */ /* connect my internal structure to the blank pointer passed to me */
*module_interface = switch_loadable_module_create_module_interface(pool, modname); *module_interface = switch_loadable_module_create_module_interface(pool, modname);
SWITCH_ADD_API(api_interface, "luarun", "run a script", lua_api_function, "<script>"); 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_APP(app_interface, "lua", "Launch LUA ivr", "Run a lua ivr on a channel", lua_function, "<script>", SAF_SUPPORT_NOMEDIA);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Hello World!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Hello World!\n");
globals.pool = pool; globals.pool = pool;
do_config();
/* indicate that the module should continue to be loaded */ /* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
......
SWITCH_BEGIN_EXTERN_C
int mod_lua_conjure_event(lua_State *L, switch_event_t *event, const char *name, int destroy_me)
{
Event *result = new Event(event);
SWIG_NewPointerObj(L,result,SWIGTYPE_p_Event, destroy_me);
lua_setglobal(L, name);
}
int mod_lua_conjure_stream(lua_State *L, switch_stream_handle_t *stream, const char *name, int destroy_me)
{
Stream *result = new Stream(stream);
SWIG_NewPointerObj(L,result,SWIGTYPE_p_Stream, destroy_me);
lua_setglobal(L, name);
}
SWITCH_END_EXTERN_C
#ifndef MOD_LUA_EXTRA
#define MOD_LUA_EXTRA
SWITCH_BEGIN_EXTERN_C
int mod_lua_conjure_event(lua_State *L, switch_event_t *event, const char *name, int destroy_me);
int mod_lua_conjure_stream(lua_State *L, switch_stream_handle_t *stream, const char *name, int destroy_me);
SWITCH_END_EXTERN_C
#endif
...@@ -52,7 +52,6 @@ package freeswitch; ...@@ -52,7 +52,6 @@ package freeswitch;
*console_clean_log = *freeswitchc::console_clean_log; *console_clean_log = *freeswitchc::console_clean_log;
*api_execute = *freeswitchc::api_execute; *api_execute = *freeswitchc::api_execute;
*api_reply_delete = *freeswitchc::api_reply_delete; *api_reply_delete = *freeswitchc::api_reply_delete;
*process_callback_result = *freeswitchc::process_callback_result;
*bridge = *freeswitchc::bridge; *bridge = *freeswitchc::bridge;
*hanguphook = *freeswitchc::hanguphook; *hanguphook = *freeswitchc::hanguphook;
*dtmf_callback = *freeswitchc::dtmf_callback; *dtmf_callback = *freeswitchc::dtmf_callback;
...@@ -148,6 +147,12 @@ use vars qw(@ISA %OWNER %ITERATORS %BLESSEDMEMBERS); ...@@ -148,6 +147,12 @@ use vars qw(@ISA %OWNER %ITERATORS %BLESSEDMEMBERS);
@ISA = qw( freeswitch ); @ISA = qw( freeswitch );
%OWNER = (); %OWNER = ();
%ITERATORS = (); %ITERATORS = ();
*swig_event_get = *freeswitchc::Event_event_get;
*swig_event_set = *freeswitchc::Event_event_set;
*swig_serialized_string_get = *freeswitchc::Event_serialized_string_get;
*swig_serialized_string_set = *freeswitchc::Event_serialized_string_set;
*swig_mine_get = *freeswitchc::Event_mine_get;
*swig_mine_set = *freeswitchc::Event_mine_set;
sub new { sub new {
my $pkg = shift; my $pkg = shift;
my $self = freeswitchc::new_Event(@_); my $self = freeswitchc::new_Event(@_);
...@@ -165,6 +170,7 @@ sub DESTROY { ...@@ -165,6 +170,7 @@ sub DESTROY {
} }
} }
*serialize = *freeswitchc::Event_serialize;
*set_priority = *freeswitchc::Event_set_priority; *set_priority = *freeswitchc::Event_set_priority;
*get_header = *freeswitchc::Event_get_header; *get_header = *freeswitchc::Event_get_header;
*get_body = *freeswitchc::Event_get_body; *get_body = *freeswitchc::Event_get_body;
...@@ -219,6 +225,8 @@ sub DESTROY { ...@@ -219,6 +225,8 @@ sub DESTROY {
*preAnswer = *freeswitchc::CoreSession_preAnswer; *preAnswer = *freeswitchc::CoreSession_preAnswer;
*hangup = *freeswitchc::CoreSession_hangup; *hangup = *freeswitchc::CoreSession_hangup;
*setVariable = *freeswitchc::CoreSession_setVariable; *setVariable = *freeswitchc::CoreSession_setVariable;
*setPrivate = *freeswitchc::CoreSession_setPrivate;
*getPrivate = *freeswitchc::CoreSession_getPrivate;
*getVariable = *freeswitchc::CoreSession_getVariable; *getVariable = *freeswitchc::CoreSession_getVariable;
*recordFile = *freeswitchc::CoreSession_recordFile; *recordFile = *freeswitchc::CoreSession_recordFile;
*setCallerData = *freeswitchc::CoreSession_setCallerData; *setCallerData = *freeswitchc::CoreSession_setCallerData;
...@@ -237,6 +245,7 @@ sub DESTROY { ...@@ -237,6 +245,7 @@ sub DESTROY {
*setHangupHook = *freeswitchc::CoreSession_setHangupHook; *setHangupHook = *freeswitchc::CoreSession_setHangupHook;
*ready = *freeswitchc::CoreSession_ready; *ready = *freeswitchc::CoreSession_ready;
*execute = *freeswitchc::CoreSession_execute; *execute = *freeswitchc::CoreSession_execute;
*sendEvent = *freeswitchc::CoreSession_sendEvent;
*begin_allow_threads = *freeswitchc::CoreSession_begin_allow_threads; *begin_allow_threads = *freeswitchc::CoreSession_begin_allow_threads;
*end_allow_threads = *freeswitchc::CoreSession_end_allow_threads; *end_allow_threads = *freeswitchc::CoreSession_end_allow_threads;
*get_uuid = *freeswitchc::CoreSession_get_uuid; *get_uuid = *freeswitchc::CoreSession_get_uuid;
......
...@@ -37,30 +37,83 @@ ...@@ -37,30 +37,83 @@
#pragma warning(disable:4127 4003) #pragma warning(disable:4127 4003)
#endif #endif
#define sanity_check(x) do { if (!(session && allocated)) { switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "session is not initalized\n"); return x;}} while(0)
#define sanity_check_noreturn do { if (!(session && allocated)) { switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "session is not initalized\n"); return;}} while(0)
#define init_vars() do { session = NULL; channel = NULL; uuid = NULL; tts_name = NULL; voice_name = NULL; memset(&args, 0, sizeof(args)); ap = NULL; caller_profile.source = "mod_unknown"; caller_profile.dialplan = ""; caller_profile.context = ""; caller_profile.caller_id_name = ""; caller_profile.caller_id_number = ""; caller_profile.network_addr = ""; caller_profile.ani = ""; caller_profile.aniii = ""; caller_profile.rdnis = ""; caller_profile.username = ""; on_hangup = NULL; cb_state.function = NULL; } while(0)
Event::Event(const char *type, const char *subclass_name) Event::Event(const char *type, const char *subclass_name)
{ {
switch_event_types_t event_id; switch_event_types_t event_id;
if (switch_name_event(type, &event_id) != SWITCH_STATUS_SUCCESS) { if (switch_name_event(type, &event_id) != SWITCH_STATUS_SUCCESS) {
event_id = SWITCH_EVENT_MESSAGE; event_id = SWITCH_EVENT_MESSAGE;
} }
switch_event_create_subclass(&event, event_id, subclass_name); switch_event_create_subclass(&event, event_id, subclass_name);
serialized_string = NULL;
mine = 1;
}
Event::Event(switch_event_t *wrap_me, int free_me)
{
event = wrap_me;
mine = free_me;
serialized_string = NULL;
} }
Event::~Event() Event::~Event()
{ {
if (event) {
if (serialized_string) {
free(serialized_string);
}
if (event && mine) {
switch_event_destroy(&event); switch_event_destroy(&event);
} }
} }
const char *Event::serialize(const char *format)
{
int isxml = 0;
if (serialized_string) {
free(serialized_string);
}
if (!event) {
return "";
}
if (format && !strcasecmp(format, "xml")) {
isxml++;
}
if (isxml) {
switch_xml_t xml;
char *xmlstr;
if ((xml = switch_event_xmlize(event, SWITCH_VA_NONE))) {
serialized_string = switch_xml_toxml(xml, SWITCH_FALSE);
switch_xml_free(xml);
return serialized_string;
} else {
return "";
}
} else {
if (switch_event_serialize(event, &serialized_string, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
return serialized_string;
}
}
return "";
}
bool Event::fire(void) bool Event::fire(void)
{ {
if (!mine) {
switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Not My event!\n");
return false;
}
if (event) { if (event) {
switch_event_fire(&event); switch_event_fire(&event);
return true; return true;
...@@ -184,7 +237,7 @@ CoreSession::CoreSession(char *nuuid) ...@@ -184,7 +237,7 @@ CoreSession::CoreSession(char *nuuid)
{ {
memset(&caller_profile, 0, sizeof(caller_profile)); memset(&caller_profile, 0, sizeof(caller_profile));
init_vars(); init_vars();
if (session = switch_core_session_locate(nuuid)) { if (!strchr(nuuid, '/') && (session = switch_core_session_locate(nuuid))) {
uuid = strdup(nuuid); uuid = strdup(nuuid);
channel = switch_core_session_get_channel(session); channel = switch_core_session_get_channel(session);
allocated = 1; allocated = 1;
...@@ -254,6 +307,18 @@ void CoreSession::hangup(char *cause) ...@@ -254,6 +307,18 @@ void CoreSession::hangup(char *cause)
switch_channel_hangup(channel, switch_channel_str2cause(cause)); switch_channel_hangup(channel, switch_channel_str2cause(cause));
} }
void CoreSession::setPrivate(char *var, void *val)
{
sanity_check_noreturn;
switch_channel_set_private(channel, var, val);
}
void *CoreSession::getPrivate(char *var)
{
sanity_check(NULL);
return switch_channel_get_private(channel, var);
}
void CoreSession::setVariable(char *var, char *val) void CoreSession::setVariable(char *var, char *val)
{ {
sanity_check_noreturn; sanity_check_noreturn;
...@@ -301,6 +366,15 @@ void CoreSession::setDTMFCallback(void *cbfunc, char *funcargs) { ...@@ -301,6 +366,15 @@ void CoreSession::setDTMFCallback(void *cbfunc, char *funcargs) {
} }
void CoreSession::sendEvent(Event *sendME)
{
if (sendME->mine) {
switch_core_session_receive_event(session, &sendME->event);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Not My event!\n");
}
}
int CoreSession::speak(char *text) int CoreSession::speak(char *text)
{ {
switch_status_t status; switch_status_t status;
...@@ -417,24 +491,30 @@ int CoreSession::playAndGetDigits(int min_digits, ...@@ -417,24 +491,30 @@ int CoreSession::playAndGetDigits(int min_digits,
int CoreSession::streamFile(char *file, int starting_sample_count) { int CoreSession::streamFile(char *file, int starting_sample_count) {
switch_status_t status; switch_status_t status;
switch_file_handle_t fh = { 0 }; //switch_file_handle_t fh = { 0 };
const char *prebuf; const char *prebuf;
sanity_check(-1); sanity_check(-1);
fh.samples = starting_sample_count;
store_file_handle(&fh); memset(&local_fh, 0, sizeof(local_fh));
fhp = &local_fh;
local_fh.samples = starting_sample_count;
begin_allow_threads();
status = switch_ivr_play_file(session, &fh, file, ap);
end_allow_threads();
if ((prebuf = switch_channel_get_variable(this->channel, "stream_prebuffer"))) { if ((prebuf = switch_channel_get_variable(this->channel, "stream_prebuffer"))) {
int maybe = atoi(prebuf); int maybe = atoi(prebuf);
if (maybe > 0) { if (maybe > 0) {
fh.prebuf = maybe; local_fh.prebuf = maybe;
} }
} }
store_file_handle(&local_fh);
begin_allow_threads();
status = switch_ivr_play_file(session, fhp, file, ap);
end_allow_threads();
return status == SWITCH_STATUS_SUCCESS ? 1 : 0; return status == SWITCH_STATUS_SUCCESS ? 1 : 0;
} }
...@@ -676,7 +756,7 @@ switch_status_t hanguphook(switch_core_session_t *session_hungup) ...@@ -676,7 +756,7 @@ switch_status_t hanguphook(switch_core_session_t *session_hungup)
switch_channel_state_t state = switch_channel_get_state(channel); switch_channel_state_t state = switch_channel_get_state(channel);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "hangup_hook called\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "hangup_hook called\n");
fflush(stdout);
if ((coresession = (CoreSession *) switch_channel_get_private(channel, "CoreSession"))) { if ((coresession = (CoreSession *) switch_channel_get_private(channel, "CoreSession"))) {
if (coresession->hook_state != state) { if (coresession->hook_state != state) {
...@@ -700,9 +780,11 @@ switch_status_t dtmf_callback(switch_core_session_t *session_cb, ...@@ -700,9 +780,11 @@ switch_status_t dtmf_callback(switch_core_session_t *session_cb,
switch_status_t result; switch_status_t result;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "dtmf_callback called\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "dtmf_callback called\n");
fflush(stdout);
//coresession = (CoreSession *) buf;
coresession = (CoreSession *) switch_channel_get_private(channel, "CoreSession"); coresession = (CoreSession *) switch_channel_get_private(channel, "CoreSession");
if (!coresession) { if (!coresession) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid CoreSession\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid CoreSession\n");
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
...@@ -717,24 +799,24 @@ switch_status_t dtmf_callback(switch_core_session_t *session_cb, ...@@ -717,24 +799,24 @@ switch_status_t dtmf_callback(switch_core_session_t *session_cb,
} }
switch_status_t process_callback_result(char *ret,
struct input_callback_state *cb_state,
switch_core_session_t *session) switch_status_t CoreSession::process_callback_result(char *ret)
{ {
switch_file_handle_t *fh = NULL; switch_file_handle_t *fh = NULL;
if (!cb_state) { if (fhp) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Process callback result aborted because cb_state is null\n"); fh = fhp;
return SWITCH_STATUS_FALSE; } else {
} if (!cb_state.extra) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Process callback result aborted because cb_state.extra is null\n");
if (!cb_state->extra) { return SWITCH_STATUS_FALSE;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Process callback result aborted because cb_state->extra is null\n"); }
return SWITCH_STATUS_FALSE;
} fh = (switch_file_handle_t *) cb_state.extra;
}
fh = (switch_file_handle_t *) cb_state->extra;
if (!fh) { if (!fh) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Process callback result aborted because fh is null\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Process callback result aborted because fh is null\n");
...@@ -747,8 +829,7 @@ switch_status_t process_callback_result(char *ret, ...@@ -747,8 +829,7 @@ switch_status_t process_callback_result(char *ret,
} }
if (!ret) { if (!ret) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Process callback result aborted because ret is null\n"); return SWITCH_STATUS_SUCCESS;
return SWITCH_STATUS_FALSE;
} }
if (!session) { if (!session) {
......
...@@ -871,7 +871,7 @@ SWITCH_DECLARE(switch_xml_t) switch_event_xmlize(switch_event_t *event, const ch ...@@ -871,7 +871,7 @@ SWITCH_DECLARE(switch_xml_t) switch_event_xmlize(switch_event_t *event, const ch
add_xml_header(xml, hp->name, hp->value, off++); add_xml_header(xml, hp->name, hp->value, off++);
} }
if (data) { if (!switch_strlen_zero(data)) {
body = data; body = data;
} else if (event->body) { } else if (event->body) {
body = event->body; body = event->body;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论