git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8214 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale
2008-04-29 23:34:26 +00:00
parent f2994a09f8
commit 5952667987
14 changed files with 2999 additions and 586 deletions
+7 -3
View File
@@ -3,12 +3,12 @@ LOCAL_CFLAGS=-Ilua
LIBLUA_A=lua/liblua.a
LOCAL_LIBADD=$(LIBLUA_A)
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
include $(BASE)/build/modmake.rules
glua.o: mod_lua_wrap.cpp
local_depend: $(LOCAL_OBJS)
$(LIBLUA_A):
cd lua && $(MAKE) CFLAGS="$(ALL_CFLAGS) -DLUA_USE_LINUX -w" liblua.a
@@ -16,14 +16,18 @@ $(LIBLUA_A):
luaclean:
cd lua && $(MAKE) clean
freeswitch_lua.o: freeswitch_lua.h
allclean: clean luaclean
rm -f mod_lua_wrap.* freeswitch_lua.$(DYNAMIC_LIB_EXTEN)
swigclean:
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
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)
$(LINK) $(SOLINK) -o freeswitch_lua.$(DYNAMIC_LIB_EXTEN) $(LOCAL_OBJS) $(LOCAL_LIBADD) $(LDFLAGS)
+152 -7
View File
@@ -1,23 +1,34 @@
#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()
{
cb_function = cb_arg = hangup_func_str = NULL;
}
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)
{
cb_function = cb_arg = hangup_func_str = NULL;
}
Session::~Session()
static switch_status_t lua_hanguphook(switch_core_session_t *session_hungup);
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()
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)
{
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;
}
+7 -54
View File
@@ -1,5 +1,5 @@
#ifndef FREESWITCH_PYTHON_H
#define FREESWITCH_PYTHON_H
#ifndef FREESWITCH_LUA_H
#define FREESWITCH_LUA_H
#include <switch_cpp.h>
@@ -10,7 +10,6 @@ void api_reply_delete(char *reply);
class Session : public CoreSession {
private:
public:
Session();
Session(char *uuid);
@@ -21,58 +20,12 @@ class Session : public CoreSession {
virtual bool end_allow_threads();
virtual void check_hangup_hook();
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;
switch_channel_t *channel;
unsigned int flags;
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
char *cb_function;
char *cb_arg;
char *hangup_func_str;
};
#endif
+29
View File
@@ -0,0 +1,29 @@
--- 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;
File diff suppressed because it is too large Load Diff
+131 -6
View File
@@ -33,16 +33,27 @@
#include "lua.h"
#include <lauxlib.h>
#include <lualib.h>
#include "mod_lua_extra.h"
SWITCH_MODULE_LOAD_FUNCTION(mod_lua_load);
SWITCH_MODULE_DEFINITION(mod_lua, mod_lua_load, NULL, NULL);
static struct {
switch_memory_pool_t *pool;
char *xml_handler;
} globals;
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)
{
lua_State *L = lua_open();
@@ -51,6 +62,7 @@ static lua_State *lua_init(void)
luaL_openlibs(L);
luaopen_freeswitch(L);
lua_gc(L, LUA_GCRESTART, 0);
lua_atpanic(L, panic);
}
return L;
}
@@ -144,6 +156,11 @@ static void *SWITCH_THREAD_FUNC lua_thread_run(switch_thread_t *thread, void *ob
char *input_code = (char *) obj;
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);
if (input_code) {
@@ -155,6 +172,96 @@ static void *SWITCH_THREAD_FUNC lua_thread_run(switch_thread_t *thread, void *ob
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)
{
switch_thread_t *thread;
@@ -172,20 +279,36 @@ SWITCH_STANDARD_APP(lua_function)
{
lua_State *L = lua_init();
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, (char *) data);
snprintf(code, sizeof(code), "~session:delete()");
lua_parse_and_execute(L, code);
lua_uninit(L);
}
SWITCH_STANDARD_API(lua_api_function) {
SWITCH_STANDARD_API(luarun_api_function) {
lua_thread(cmd);
stream->write_function(stream, "+OK\n");
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_api_interface_t *api_interface;
@@ -194,12 +317,14 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_lua_load)
/* connect my internal structure to the blank pointer passed to me */
*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_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Hello World!\n");
globals.pool = pool;
do_config();
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS;
+21
View File
@@ -0,0 +1,21 @@
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
@@ -0,0 +1,9 @@
#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
File diff suppressed because it is too large Load Diff