2008-05-01 22:56:14 +00:00
|
|
|
#include <switch.h>
|
2008-04-25 22:12:01 +00:00
|
|
|
#include "freeswitch_perl.h"
|
2008-05-01 22:56:14 +00:00
|
|
|
#include "mod_perl_extra.h"
|
|
|
|
|
|
|
|
static STRLEN n_a;
|
2008-04-25 22:12:01 +00:00
|
|
|
|
2008-05-08 23:58:45 +00:00
|
|
|
#define init_me() cb_function = hangup_func_str = NULL; hangup_func_arg = NULL; hh = mark = 0; my_perl = NULL; cb_arg = NULL
|
2008-05-02 13:58:44 +00:00
|
|
|
|
2008-05-12 13:37:36 +00:00
|
|
|
using namespace PERL;
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
Session::Session():CoreSession()
|
2008-04-25 22:12:01 +00:00
|
|
|
{
|
2008-05-02 13:58:44 +00:00
|
|
|
init_me();
|
2008-04-25 22:12:01 +00:00
|
|
|
}
|
|
|
|
|
2008-07-16 21:06:14 +00:00
|
|
|
Session::Session(char *uuid, CoreSession *a_leg):CoreSession(uuid, a_leg)
|
2008-04-25 22:12:01 +00:00
|
|
|
{
|
2008-05-02 13:58:44 +00:00
|
|
|
init_me();
|
2008-07-16 20:19:11 +00:00
|
|
|
if (session && allocated) {
|
2008-07-16 06:23:35 +00:00
|
|
|
suuid = switch_core_session_sprintf(session, "main::uuid_%s\n", switch_core_session_get_uuid(session));
|
|
|
|
for (char *p = suuid; p && *p; p++) {
|
|
|
|
if (*p == '-') {
|
|
|
|
*p = '_';
|
|
|
|
}
|
|
|
|
if (*p == '\n') {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2008-07-14 20:37:36 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-25 22:12:01 +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:12:01 +00:00
|
|
|
{
|
2008-05-02 13:58:44 +00:00
|
|
|
init_me();
|
2008-07-16 06:23:35 +00:00
|
|
|
if (session) {
|
|
|
|
suuid = switch_core_session_sprintf(session, "main::uuid_%s\n", switch_core_session_get_uuid(session));
|
|
|
|
for (char *p = suuid; p && *p; p++) {
|
|
|
|
if (*p == '-') {
|
|
|
|
*p = '_';
|
|
|
|
}
|
2008-07-14 20:37:36 +00:00
|
|
|
}
|
2008-07-16 06:23:35 +00:00
|
|
|
} else {
|
|
|
|
//handle failure
|
2008-07-14 20:37:36 +00:00
|
|
|
}
|
2008-04-25 22:12:01 +00:00
|
|
|
}
|
2008-05-01 22:56:14 +00:00
|
|
|
static switch_status_t perl_hanguphook(switch_core_session_t *session_hungup);
|
|
|
|
Session::~Session()
|
2008-04-25 22:12:01 +00:00
|
|
|
{
|
2008-05-01 22:56:14 +00:00
|
|
|
switch_safe_free(cb_function);
|
|
|
|
switch_safe_free(cb_arg);
|
2008-05-12 16:04:34 +00:00
|
|
|
|
|
|
|
if (session && hangup_func_str) {
|
|
|
|
switch_core_event_hook_remove_state_change(session, perl_hanguphook);
|
|
|
|
}
|
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
switch_safe_free(hangup_func_str);
|
2008-05-08 23:58:45 +00:00
|
|
|
switch_safe_free(hangup_func_arg);
|
2008-05-12 16:04:34 +00:00
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
bool Session::begin_allow_threads()
|
2008-04-25 22:12:01 +00:00
|
|
|
{
|
2008-05-01 22:56:14 +00:00
|
|
|
do_hangup_hook();
|
2008-04-25 22:12:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
bool Session::end_allow_threads()
|
2008-04-25 22:12:01 +00:00
|
|
|
{
|
2008-05-01 22:56:14 +00:00
|
|
|
do_hangup_hook();
|
2008-04-25 22:12:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
void Session::setPERL(PerlInterpreter * pi)
|
2008-05-01 22:56:14 +00:00
|
|
|
{
|
2008-08-11 18:47:30 +00:00
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
my_perl = pi;
|
|
|
|
}
|
|
|
|
|
2008-07-14 20:37:36 +00:00
|
|
|
void Session::setME(SV *p)
|
|
|
|
{
|
2008-07-16 20:19:11 +00:00
|
|
|
sanity_check_noreturn;
|
|
|
|
|
2008-07-14 20:37:36 +00:00
|
|
|
me = p;
|
|
|
|
}
|
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
PerlInterpreter *Session::getPERL()
|
|
|
|
{
|
|
|
|
if (!my_perl) {
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Doh!\n");
|
2008-05-01 22:56:14 +00:00
|
|
|
}
|
|
|
|
return my_perl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
bool Session::ready()
|
|
|
|
{
|
2008-05-01 22:56:14 +00:00
|
|
|
bool r;
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
sanity_check(false);
|
2008-05-01 22:56:14 +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:12:01 +00:00
|
|
|
{
|
2008-05-05 15:30:55 +00:00
|
|
|
if (hangup_func_str && (hook_state == CS_HANGUP || hook_state == CS_ROUTING)) {
|
2008-05-01 22:56:14 +00:00
|
|
|
hh++;
|
|
|
|
}
|
2008-04-25 22:12:01 +00:00
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
void Session::do_hangup_hook()
|
2008-04-25 22:12:01 +00:00
|
|
|
{
|
2008-05-01 22:56:14 +00:00
|
|
|
if (hh && !mark) {
|
|
|
|
const char *err = NULL;
|
|
|
|
mark++;
|
|
|
|
char *code;
|
|
|
|
if (!getPERL()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-08 23:58:45 +00:00
|
|
|
if (hangup_func_arg) {
|
2008-07-14 20:37:36 +00:00
|
|
|
code = switch_mprintf("%s($%s,\"%s\",%s)", hangup_func_str, suuid, hook_state == CS_HANGUP ? "hangup" : "transfer", hangup_func_arg);
|
|
|
|
} else {
|
|
|
|
code = switch_mprintf("%s($%s,\"%s\")", hangup_func_str, suuid, hook_state == CS_HANGUP ? "hangup" : "transfer");
|
|
|
|
}
|
2008-05-08 23:58:45 +00:00
|
|
|
|
2008-07-14 20:37:36 +00:00
|
|
|
Perl_eval_pv(my_perl, code, TRUE);
|
|
|
|
free(code);
|
2008-05-01 22:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
static switch_status_t perl_hanguphook(switch_core_session_t *session_hungup)
|
2008-05-01 22:56:14 +00:00
|
|
|
{
|
|
|
|
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;
|
2008-04-25 22:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
void Session::setHangupHook(char *func, char *arg)
|
|
|
|
{
|
2008-05-01 22:56:14 +00:00
|
|
|
|
|
|
|
sanity_check_noreturn;
|
|
|
|
|
|
|
|
switch_safe_free(hangup_func_str);
|
|
|
|
|
|
|
|
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, perl_hanguphook);
|
2008-05-08 23:58:45 +00:00
|
|
|
if (arg) {
|
|
|
|
hangup_func_arg = strdup(func);
|
|
|
|
}
|
2008-05-01 22:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
switch_channel_set_private(channel, "CoreSession", NULL);
|
|
|
|
args.input_callback = NULL;
|
|
|
|
ap = NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
void Session::setInputCallback(char *cbfunc, char *funcargs)
|
|
|
|
{
|
2008-05-01 22:56:14 +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-05-01 22:56:14 +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-05-01 22:56:14 +00:00
|
|
|
ap = &args;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_status_t Session::run_dtmf_callback(void *input, switch_input_type_t itype)
|
2008-05-01 22:56:14 +00:00
|
|
|
{
|
|
|
|
if (!getPERL()) {
|
|
|
|
return SWITCH_STATUS_FALSE;;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (itype) {
|
2008-05-27 04:54:52 +00:00
|
|
|
case SWITCH_INPUT_TYPE_DTMF:
|
2008-05-01 22:56:14 +00:00
|
|
|
{
|
|
|
|
switch_dtmf_t *dtmf = (switch_dtmf_t *) input;
|
|
|
|
char str[32] = "";
|
|
|
|
int arg_count = 2;
|
|
|
|
HV *hash;
|
|
|
|
SV *this_sv;
|
|
|
|
char *code;
|
|
|
|
|
|
|
|
if (!(hash = get_hv("__dtmf", TRUE))) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
str[0] = dtmf->digit;
|
2008-05-27 04:54:52 +00:00
|
|
|
this_sv = newSV(strlen(str) + 1);
|
2008-05-01 22:56:14 +00:00
|
|
|
sv_setpv(this_sv, str);
|
|
|
|
hv_store(hash, "digit", 5, this_sv, 0);
|
|
|
|
|
|
|
|
switch_snprintf(str, sizeof(str), "%d", dtmf->duration);
|
2008-05-27 04:54:52 +00:00
|
|
|
this_sv = newSV(strlen(str) + 1);
|
2008-05-01 22:56:14 +00:00
|
|
|
sv_setpv(this_sv, str);
|
2008-05-27 04:54:52 +00:00
|
|
|
hv_store(hash, "duration", 8, this_sv, 0);
|
2008-05-01 22:56:14 +00:00
|
|
|
|
2008-07-14 20:37:36 +00:00
|
|
|
code = switch_mprintf("$__RV = %s($%s, 'dtmf', \\%%__dtmf, %s);", cb_function, suuid, switch_str_nil(cb_arg));
|
2008-05-02 13:58:44 +00:00
|
|
|
Perl_eval_pv(my_perl, code, FALSE);
|
2008-05-01 22:56:14 +00:00
|
|
|
free(code);
|
|
|
|
|
2008-05-02 13:58:44 +00:00
|
|
|
return process_callback_result(SvPV(get_sv("__RV", TRUE), n_a));
|
2008-05-01 22:56:14 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-05-27 04:54:52 +00:00
|
|
|
case SWITCH_INPUT_TYPE_EVENT:
|
2008-05-01 22:56:14 +00:00
|
|
|
{
|
|
|
|
switch_event_t *event = (switch_event_t *) input;
|
|
|
|
int arg_count = 2;
|
|
|
|
char *code;
|
|
|
|
|
|
|
|
mod_perl_conjure_event(my_perl, event, "__Input_Event__");
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-07-14 20:37:36 +00:00
|
|
|
code = switch_mprintf("$__RV = %s($%s, 'event', $__Input_Event__, %s);", cb_function, suuid, switch_str_nil(cb_arg));
|
2008-05-01 22:56:14 +00:00
|
|
|
Perl_eval_pv(my_perl, code, TRUE);
|
|
|
|
free(code);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-02 13:58:44 +00:00
|
|
|
return process_callback_result(SvPV(get_sv("__RV", TRUE), n_a));
|
2008-05-01 22:56:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|