2008-05-12 00:24:38 +00:00
|
|
|
|
2008-04-30 19:42:26 +00:00
|
|
|
#include <switch.h>
|
2008-04-25 22:16:16 +00:00
|
|
|
#include "freeswitch_lua.h"
|
2008-05-12 00:24:38 +00:00
|
|
|
using namespace LUA;
|
2008-04-25 22:16:16 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
Session::Session():CoreSession()
|
2008-04-25 22:16:16 +00:00
|
|
|
{
|
2008-05-08 23:58:45 +00:00
|
|
|
cb_function = cb_arg = hangup_func_str = hangup_func_arg = NULL;
|
2008-04-30 19:42:26 +00:00
|
|
|
hh = mark = 0;
|
2008-04-25 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
2008-07-16 21:06:14 +00:00
|
|
|
Session::Session(char *nuuid, CoreSession *a_leg):CoreSession(nuuid, a_leg)
|
2008-04-25 22:16:16 +00:00
|
|
|
{
|
2008-05-08 23:58:45 +00:00
|
|
|
cb_function = cb_arg = hangup_func_str = hangup_func_arg = NULL;
|
2008-04-30 19:42:26 +00:00
|
|
|
hh = mark = 0;
|
2008-04-25 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
Session::Session(switch_core_session_t *new_session):CoreSession(new_session)
|
2008-04-25 22:16:16 +00:00
|
|
|
{
|
2008-05-08 23:58:45 +00:00
|
|
|
cb_function = cb_arg = hangup_func_str = hangup_func_arg = NULL;
|
2008-04-30 19:42:26 +00:00
|
|
|
hh = mark = 0;
|
2008-04-25 22:16:16 +00:00
|
|
|
}
|
2008-04-29 23:34:26 +00:00
|
|
|
static switch_status_t lua_hanguphook(switch_core_session_t *session_hungup);
|
|
|
|
Session::~Session()
|
2008-04-25 22:16:16 +00:00
|
|
|
{
|
2008-05-03 16:36:12 +00:00
|
|
|
|
2009-03-22 18:35:55 +00:00
|
|
|
if (channel) {
|
|
|
|
switch_channel_set_private(channel, "CoreSession", NULL);
|
|
|
|
}
|
|
|
|
|
2008-05-03 16:36:12 +00:00
|
|
|
if (hangup_func_str) {
|
|
|
|
if (session) {
|
|
|
|
switch_core_event_hook_remove_state_change(session, lua_hanguphook);
|
|
|
|
}
|
2009-03-22 21:32:08 +00:00
|
|
|
switch_safe_free(hangup_func_str);
|
2008-05-03 16:36:12 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-08 23:58:45 +00:00
|
|
|
switch_safe_free(hangup_func_arg);
|
2008-04-29 23:34:26 +00:00
|
|
|
switch_safe_free(cb_function);
|
|
|
|
switch_safe_free(cb_arg);
|
2008-04-25 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
bool Session::begin_allow_threads()
|
2008-04-25 22:16:16 +00:00
|
|
|
{
|
2008-04-30 19:42:26 +00:00
|
|
|
do_hangup_hook();
|
2008-04-25 22:16:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
bool Session::end_allow_threads()
|
2008-04-25 22:16:16 +00:00
|
|
|
{
|
2008-04-30 19:42:26 +00:00
|
|
|
do_hangup_hook();
|
2008-04-25 22:16:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
void Session::setLUA(lua_State * state)
|
2008-04-30 19:42:26 +00:00
|
|
|
{
|
2008-07-16 20:19:11 +00:00
|
|
|
L = state;
|
2008-08-11 18:47:30 +00:00
|
|
|
|
|
|
|
if (session && allocated && uuid) {
|
2008-07-16 22:04:31 +00:00
|
|
|
lua_setglobal(L, uuid);
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, uuid);
|
|
|
|
}
|
2008-07-16 20:19:11 +00:00
|
|
|
|
2008-04-30 19:42:26 +00:00
|
|
|
}
|
|
|
|
|
2008-08-14 17:35:46 +00:00
|
|
|
int Session::originate(CoreSession *a_leg_session, char *dest, int timeout)
|
2008-08-11 18:47:30 +00:00
|
|
|
{
|
2008-08-14 17:35:46 +00:00
|
|
|
int x = CoreSession::originate(a_leg_session, dest, timeout);
|
|
|
|
|
|
|
|
if (x) {
|
|
|
|
setLUA(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
2008-08-11 18:47:30 +00:00
|
|
|
}
|
|
|
|
|
2008-04-30 19:42:26 +00:00
|
|
|
lua_State *Session::getLUA()
|
|
|
|
{
|
|
|
|
if (!L) {
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Doh!\n");
|
2008-04-30 19:42:26 +00:00
|
|
|
}
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
bool Session::ready()
|
|
|
|
{
|
2008-04-30 19:42:26 +00:00
|
|
|
bool r;
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
sanity_check(false);
|
2008-04-30 19:42:26 +00:00
|
|
|
r = switch_channel_ready(channel) != 0;
|
|
|
|
do_hangup_hook();
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
void Session::check_hangup_hook()
|
2008-04-25 22:16:16 +00:00
|
|
|
{
|
2008-05-05 15:30:55 +00:00
|
|
|
if (hangup_func_str && (hook_state == CS_HANGUP || hook_state == CS_ROUTING)) {
|
2008-04-30 19:42:26 +00:00
|
|
|
hh++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
void Session::do_hangup_hook()
|
2008-04-30 19:42:26 +00:00
|
|
|
{
|
|
|
|
if (hh && !mark) {
|
|
|
|
const char *err = NULL;
|
2008-07-14 15:38:44 +00:00
|
|
|
int arg_count = 2;
|
2008-04-30 19:42:26 +00:00
|
|
|
mark++;
|
2008-04-29 23:34:26 +00:00
|
|
|
|
2008-04-30 19:42:26 +00:00
|
|
|
if (!getLUA()) {
|
2008-04-29 23:34:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-04-30 19:42:26 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (char *) hangup_func_str);
|
2008-07-14 15:38:44 +00:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, uuid);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-29 23:34:26 +00:00
|
|
|
lua_pushstring(L, hook_state == CS_HANGUP ? "hangup" : "transfer");
|
2008-05-08 23:58:45 +00:00
|
|
|
|
|
|
|
if (hangup_func_arg) {
|
2008-05-27 04:54:52 +00:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (char *) hangup_func_arg);
|
2008-05-12 15:38:51 +00:00
|
|
|
arg_count++;
|
2008-05-08 23:58:45 +00:00
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-12 15:38:51 +00:00
|
|
|
lua_call(L, arg_count, 1);
|
2008-04-30 19:42:26 +00:00
|
|
|
err = lua_tostring(L, -1);
|
|
|
|
|
|
|
|
if (!switch_strlen_zero(err)) {
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s\n", err);
|
2008-04-30 19:42:26 +00:00
|
|
|
}
|
2009-03-22 21:32:08 +00:00
|
|
|
|
|
|
|
if (channel) {
|
|
|
|
switch_channel_set_private(channel, "CoreSession", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session) {
|
|
|
|
switch_core_event_hook_remove_state_change(session, lua_hanguphook);
|
|
|
|
}
|
|
|
|
switch_safe_free(hangup_func_str);
|
|
|
|
|
2008-04-29 23:34:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
static switch_status_t lua_hanguphook(switch_core_session_t *session_hungup)
|
2008-04-29 23:34:26 +00:00
|
|
|
{
|
|
|
|
switch_channel_t *channel = switch_core_session_get_channel(session_hungup);
|
2009-03-22 21:19:25 +00:00
|
|
|
Session *coresession = NULL;
|
2008-04-29 23:34:26 +00:00
|
|
|
switch_channel_state_t state = switch_channel_get_state(channel);
|
|
|
|
|
2009-03-22 18:23:24 +00:00
|
|
|
if (session_hungup) {
|
|
|
|
|
|
|
|
channel = switch_core_session_get_channel(session_hungup);
|
|
|
|
|
|
|
|
if (channel) {
|
2009-03-22 21:32:08 +00:00
|
|
|
void *vs = switch_channel_get_private(channel, "CoreSession");
|
|
|
|
if (vs) {
|
|
|
|
coresession = (Session *) vs;
|
|
|
|
}
|
2009-03-22 18:23:24 +00:00
|
|
|
}
|
|
|
|
|
2009-03-22 18:35:55 +00:00
|
|
|
if (!(coresession && coresession->hook_state)) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2009-03-22 18:23:24 +00:00
|
|
|
if (coresession && coresession->allocated && (state == CS_HANGUP || state == CS_ROUTING) && coresession->hook_state != state) {
|
2008-04-29 23:34:26 +00:00
|
|
|
coresession->hook_state = state;
|
|
|
|
coresession->check_hangup_hook();
|
2009-03-22 18:23:24 +00:00
|
|
|
switch_core_event_hook_remove_state_change(session_hungup, lua_hanguphook);
|
2008-04-29 23:34:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
void Session::setHangupHook(char *func, char *arg)
|
|
|
|
{
|
2008-04-29 23:34:26 +00:00
|
|
|
|
|
|
|
sanity_check_noreturn;
|
|
|
|
|
|
|
|
switch_safe_free(hangup_func_str);
|
2008-05-08 23:58:45 +00:00
|
|
|
switch_safe_free(hangup_func_arg);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-29 23:34:26 +00:00
|
|
|
if (func) {
|
|
|
|
hangup_func_str = strdup(func);
|
2008-05-09 16:24:38 +00:00
|
|
|
if (!switch_strlen_zero(arg)) {
|
2008-05-09 09:08:32 +00:00
|
|
|
hangup_func_arg = strdup(arg);
|
|
|
|
}
|
2008-04-29 23:34:26 +00:00
|
|
|
switch_channel_set_private(channel, "CoreSession", this);
|
|
|
|
hook_state = switch_channel_get_state(channel);
|
|
|
|
switch_core_event_hook_add_state_change(session, lua_hanguphook);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-14 17:20:34 +00:00
|
|
|
void Session::unsetInputCallback(void)
|
|
|
|
{
|
|
|
|
sanity_check_noreturn;
|
|
|
|
switch_safe_free(cb_function);
|
|
|
|
switch_safe_free(cb_arg);
|
|
|
|
args.input_callback = NULL;
|
|
|
|
switch_channel_set_private(channel, "CoreSession", NULL);
|
|
|
|
ap = NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
void Session::setInputCallback(char *cbfunc, char *funcargs)
|
|
|
|
{
|
2008-04-29 23:34:26 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-29 23:34:26 +00:00
|
|
|
args.buf = this;
|
|
|
|
switch_channel_set_private(channel, "CoreSession", this);
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
args.input_callback = dtmf_callback;
|
2008-04-29 23:34:26 +00:00
|
|
|
ap = &args;
|
2008-04-25 22:16:16 +00:00
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_status_t Session::run_dtmf_callback(void *input, switch_input_type_t itype)
|
2008-04-25 22:16:16 +00:00
|
|
|
{
|
2008-04-29 23:34:26 +00:00
|
|
|
const char *ret;
|
2008-04-30 19:42:26 +00:00
|
|
|
|
|
|
|
if (!getLUA()) {
|
|
|
|
return SWITCH_STATUS_FALSE;;
|
2008-04-29 23:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (itype) {
|
2008-05-27 04:54:52 +00:00
|
|
|
case SWITCH_INPUT_TYPE_DTMF:
|
2008-04-29 23:34:26 +00:00
|
|
|
{
|
|
|
|
switch_dtmf_t *dtmf = (switch_dtmf_t *) input;
|
|
|
|
char str[2] = "";
|
2008-07-12 18:44:05 +00:00
|
|
|
int arg_count = 3;
|
2008-04-29 23:34:26 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (char *) cb_function);
|
2008-07-12 18:44:05 +00:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, uuid);
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
lua_pushstring(L, "dtmf");
|
|
|
|
|
|
|
|
lua_newtable(L);
|
2008-04-29 23:34:26 +00:00
|
|
|
lua_pushstring(L, "digit");
|
|
|
|
str[0] = dtmf->digit;
|
|
|
|
lua_pushstring(L, str);
|
|
|
|
lua_rawset(L, -3);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-29 23:34:26 +00:00
|
|
|
lua_pushstring(L, "duration");
|
|
|
|
lua_pushnumber(L, dtmf->duration);
|
|
|
|
lua_rawset(L, -3);
|
|
|
|
|
2008-08-16 15:56:29 +00:00
|
|
|
if (!switch_strlen_zero(cb_arg)) {
|
2008-05-27 04:54:52 +00:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (char *) cb_arg);
|
2008-04-29 23:34:26 +00:00
|
|
|
arg_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_call(L, arg_count, 1);
|
|
|
|
ret = lua_tostring(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
return process_callback_result((char *) ret);
|
2008-04-29 23:34:26 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-05-27 04:54:52 +00:00
|
|
|
case SWITCH_INPUT_TYPE_EVENT:
|
2008-04-29 23:34:26 +00:00
|
|
|
{
|
|
|
|
switch_event_t *event = (switch_event_t *) input;
|
2008-07-12 18:44:05 +00:00
|
|
|
int arg_count = 3;
|
|
|
|
|
2008-04-29 23:34:26 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (char *) cb_function);
|
2008-07-12 18:44:05 +00:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, uuid);
|
2008-04-29 23:34:26 +00:00
|
|
|
lua_pushstring(L, "event");
|
|
|
|
mod_lua_conjure_event(L, event, "__Input_Event__", 1);
|
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, "__Input_Event__");
|
|
|
|
|
2008-08-16 15:56:29 +00:00
|
|
|
if (!switch_strlen_zero(cb_arg)) {
|
2008-05-27 04:54:52 +00:00
|
|
|
lua_getfield(L, LUA_GLOBALSINDEX, (char *) cb_arg);
|
2008-04-29 23:34:26 +00:00
|
|
|
arg_count++;
|
|
|
|
}
|
|
|
|
|
2008-07-12 18:44:05 +00:00
|
|
|
lua_call(L, arg_count, 1);
|
2008-05-27 04:54:52 +00:00
|
|
|
ret = lua_tostring(L, -1);
|
2008-04-29 23:34:26 +00:00
|
|
|
lua_pop(L, 1);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
|
|
|
return process_callback_result((char *) ret);
|
2008-04-29 23:34:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2008-04-25 22:16:16 +00:00
|
|
|
}
|