mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-10-21 10:44:16 +00:00
more ivr stuff (not done)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@693 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
1 parent
5938cee550
commit
bdd2223542
13 files changed
+362
-91
No files matched your search
@@ -53,6 +53,7 @@
|
||||
|
||||
static const char modname[] = "mod_spidermonkey";
|
||||
|
||||
static int eval_some_js(char *code, JSContext *cx, JSObject *obj, jsval *rval);
|
||||
|
||||
static struct {
|
||||
size_t gStackChunkSize;
|
||||
@@ -74,8 +75,20 @@ static JSClass global_class = {
|
||||
};
|
||||
|
||||
|
||||
struct jchan {
|
||||
struct dtmf_callback_state {
|
||||
struct js_session *session_state;
|
||||
char code_buffer[1024];
|
||||
int code_buffer_len;
|
||||
char ret_buffer[1024];
|
||||
int ret_buffer_len;
|
||||
int digit_count;
|
||||
void *extra;
|
||||
};
|
||||
|
||||
struct js_session {
|
||||
switch_core_session *session;
|
||||
JSContext *cx;
|
||||
JSObject *obj;
|
||||
unsigned int flags;
|
||||
};
|
||||
|
||||
@@ -110,33 +123,177 @@ static switch_status init_js(void)
|
||||
|
||||
}
|
||||
|
||||
static JSBool chan_streamfile(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
static switch_status js_dtmf_callback(switch_core_session *session, char *dtmf, void *buf, unsigned int buflen)
|
||||
{
|
||||
struct jchan *jc = JS_GetPrivate(cx, obj);
|
||||
JSString *str = NULL;
|
||||
char *filename = NULL;
|
||||
char buf[25] = "";
|
||||
char code[2048];
|
||||
struct dtmf_callback_state *cb_state = buf;
|
||||
struct js_session *jss = cb_state->session_state;
|
||||
switch_file_handle *fh = cb_state->extra;
|
||||
jsval rval;
|
||||
char *ret;
|
||||
|
||||
if(!jss) {
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
if (cb_state->digit_count || (cb_state->code_buffer[0] > 47 && cb_state->code_buffer[0] < 58)) {
|
||||
char *d;
|
||||
if (!cb_state->digit_count) {
|
||||
cb_state->digit_count = atoi(cb_state->code_buffer);
|
||||
}
|
||||
|
||||
if (argc > 0) {
|
||||
if ((str = JS_ValueToString(cx, argv[0])) && (filename = JS_GetStringBytes(str))) {
|
||||
switch_ivr_play_file(jc->session, filename, NULL, NULL, buf, sizeof(buf));
|
||||
*rval = STRING_TO_JSVAL ( JS_NewStringCopyZ(cx, buf) );
|
||||
for(d = dtmf; *d; d++) {
|
||||
cb_state->ret_buffer[cb_state->ret_buffer_len++] = *d;
|
||||
if ((cb_state->ret_buffer_len > cb_state->digit_count)||
|
||||
(cb_state->ret_buffer_len > sizeof(cb_state->ret_buffer))||
|
||||
(cb_state->ret_buffer_len >= cb_state->digit_count)
|
||||
) {
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
}
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
} else {
|
||||
snprintf(code, sizeof(code), "~%s(\"%s\")", cb_state->code_buffer, dtmf);
|
||||
eval_some_js(code, jss->cx, jss->obj, &rval);
|
||||
ret = JS_GetStringBytes(JS_ValueToString(jss->cx, rval));
|
||||
|
||||
if (*ret == 'F') {
|
||||
int step;
|
||||
ret++;
|
||||
|
||||
step = *ret ? atoi(ret) : 1;
|
||||
fh->speed += step ? step : 1;
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (*ret == 'S') {
|
||||
int step;
|
||||
ret++;
|
||||
|
||||
step = *ret ? atoi(ret) : 1;
|
||||
fh->speed -= step ? step : 1;
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (*ret == 'N') {
|
||||
fh->speed = 0;
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (*ret == 'P') {
|
||||
if (switch_test_flag(fh, SWITCH_FILE_PAUSE)) {
|
||||
printf("unpause\n");
|
||||
switch_clear_flag(fh, SWITCH_FILE_PAUSE);
|
||||
} else {
|
||||
printf("pause\n");
|
||||
switch_set_flag(fh, SWITCH_FILE_PAUSE);
|
||||
fh->speed = 0;
|
||||
}
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (*ret == 'R') {
|
||||
unsigned int pos = 0;
|
||||
fh->speed = 0;
|
||||
switch_core_file_seek(fh, &pos, 0, SEEK_SET);
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (*ret == '+' || *ret == '-') {
|
||||
switch_codec *codec;
|
||||
codec = switch_core_session_get_read_codec(jss->session);
|
||||
unsigned int samps = 0;
|
||||
unsigned int pos = 0;
|
||||
if (*ret == '+') {
|
||||
ret++;
|
||||
samps = atoi(ret) * (codec->implementation->samples_per_second / 1000);
|
||||
switch_core_file_seek(fh, &pos, samps, SEEK_CUR);
|
||||
} else {
|
||||
samps = atoi(ret) * (codec->implementation->samples_per_second / 1000);
|
||||
switch_core_file_seek(fh, &pos, fh->pos - samps, SEEK_SET);
|
||||
}
|
||||
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (!strcmp(ret, "true") || !strcmp(ret, "undefined")) {
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
switch_copy_string(cb_state->ret_buffer, ret, sizeof(cb_state->ret_buffer));
|
||||
}
|
||||
}
|
||||
return JS_FALSE;
|
||||
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
static JSBool chan_streamfile(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
{
|
||||
struct js_session *jss = JS_GetPrivate(cx, obj);
|
||||
switch_channel *channel;
|
||||
char *file_name = NULL;
|
||||
char *timer_name = NULL;
|
||||
char *dtmf_callback = NULL;
|
||||
void *bp = NULL;
|
||||
int len = 0;
|
||||
switch_dtmf_callback_function dtmf_func = NULL;
|
||||
struct dtmf_callback_state cb_state = {0};
|
||||
switch_file_handle fh;
|
||||
|
||||
channel = switch_core_session_get_channel(jss->session);
|
||||
assert(channel != NULL);
|
||||
|
||||
if (argc > 0) {
|
||||
file_name = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
|
||||
if (switch_strlen_zero(file_name)) {
|
||||
return JS_FALSE;
|
||||
}
|
||||
}
|
||||
if (argc > 1) {
|
||||
timer_name = JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
|
||||
if (switch_strlen_zero(timer_name)) {
|
||||
timer_name = NULL;
|
||||
}
|
||||
}
|
||||
if (argc > 2) {
|
||||
dtmf_callback = JS_GetStringBytes(JS_ValueToString(cx, argv[2]));
|
||||
if (switch_strlen_zero(dtmf_callback)) {
|
||||
dtmf_callback = NULL;
|
||||
} else {
|
||||
memset(&cb_state, 0, sizeof(cb_state));
|
||||
switch_copy_string(cb_state.code_buffer, dtmf_callback, sizeof(cb_state.code_buffer));
|
||||
cb_state.code_buffer_len = strlen(cb_state.code_buffer);
|
||||
cb_state.session_state = jss;
|
||||
dtmf_func = js_dtmf_callback;
|
||||
bp = &cb_state;
|
||||
len = sizeof(cb_state);
|
||||
}
|
||||
}
|
||||
|
||||
memset(&fh, 0, sizeof(fh));
|
||||
cb_state.extra = &fh;
|
||||
|
||||
switch_ivr_play_file(jss->session, &fh, file_name, timer_name, dtmf_func, bp, len);
|
||||
*rval = STRING_TO_JSVAL (JS_NewStringCopyZ(cx, cb_state.ret_buffer));
|
||||
|
||||
return (switch_channel_get_state(channel) == CS_EXECUTE) ? JS_TRUE : JS_FALSE;
|
||||
}
|
||||
|
||||
static JSBool chan_speak(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
{
|
||||
struct jchan *jc = JS_GetPrivate(cx, obj);
|
||||
struct js_session *jss = JS_GetPrivate(cx, obj);
|
||||
switch_channel *channel;
|
||||
char *tts_name = NULL;
|
||||
char *voice_name = NULL;
|
||||
char *text = NULL;
|
||||
switch_codec *codec;
|
||||
char buf[10] = "";
|
||||
void *bp = NULL;
|
||||
int len = 0;
|
||||
//switch_dtmf_callback_function dtmf_func = NULL;
|
||||
|
||||
channel = switch_core_session_get_channel(jc->session);
|
||||
channel = switch_core_session_get_channel(jss->session);
|
||||
assert(channel != NULL);
|
||||
|
||||
if (argc > 0) {
|
||||
@@ -148,30 +305,35 @@ static JSBool chan_speak(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
|
||||
if (argc > 2) {
|
||||
text = JS_GetStringBytes(JS_ValueToString(cx, argv[2]));
|
||||
}
|
||||
if (argc > 3) {
|
||||
bp = buf;
|
||||
len = sizeof(buf);
|
||||
}
|
||||
|
||||
if (!tts_name && text) {
|
||||
return JS_FALSE;
|
||||
}
|
||||
|
||||
codec = switch_core_session_get_read_codec(jc->session);
|
||||
switch_ivr_speak_text(jc->session,
|
||||
codec = switch_core_session_get_read_codec(jss->session);
|
||||
switch_ivr_speak_text(jss->session,
|
||||
tts_name,
|
||||
voice_name && strlen(voice_name) ? voice_name : NULL,
|
||||
NULL,
|
||||
codec->implementation->samples_per_second,
|
||||
NULL,
|
||||
text,
|
||||
buf,
|
||||
sizeof(buf));
|
||||
*rval = STRING_TO_JSVAL ( JS_NewStringCopyZ(cx, buf) );
|
||||
|
||||
bp,
|
||||
len);
|
||||
if(len) {
|
||||
*rval = STRING_TO_JSVAL ( JS_NewStringCopyZ(cx, buf) );
|
||||
}
|
||||
return JS_TRUE;
|
||||
|
||||
}
|
||||
|
||||
static JSBool chan_get_digits(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
{
|
||||
struct jchan *jc = JS_GetPrivate(cx, obj);
|
||||
struct js_session *jss = JS_GetPrivate(cx, obj);
|
||||
char *terminators = NULL;
|
||||
char *buf;
|
||||
int digits;
|
||||
@@ -182,8 +344,8 @@ static JSBool chan_get_digits(JSContext *cx, JSObject *obj, uintN argc, jsval *a
|
||||
if (argc > 1) {
|
||||
terminators = JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
|
||||
}
|
||||
buf = switch_core_session_alloc(jc->session, digits);
|
||||
switch_ivr_collect_digits_count(jc->session, buf, digits, digits, terminators, &term);
|
||||
buf = switch_core_session_alloc(jss->session, digits);
|
||||
switch_ivr_collect_digits_count(jss->session, buf, digits, digits, terminators, &term);
|
||||
*rval = STRING_TO_JSVAL ( JS_NewStringCopyZ(cx, buf) );
|
||||
return JS_TRUE;
|
||||
}
|
||||
@@ -193,10 +355,10 @@ static JSBool chan_get_digits(JSContext *cx, JSObject *obj, uintN argc, jsval *a
|
||||
|
||||
static JSBool chan_answer(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
||||
{
|
||||
struct jchan *jc = JS_GetPrivate(cx, obj);
|
||||
struct js_session *jss = JS_GetPrivate(cx, obj);
|
||||
switch_channel *channel;
|
||||
|
||||
channel = switch_core_session_get_channel(jc->session);
|
||||
channel = switch_core_session_get_channel(jss->session);
|
||||
assert(channel != NULL);
|
||||
|
||||
switch_channel_answer(channel);
|
||||
@@ -208,10 +370,10 @@ enum its_tinyid {
|
||||
};
|
||||
|
||||
static JSFunctionSpec chan_methods[] = {
|
||||
{"StreamFile", chan_streamfile, 1},
|
||||
{"Speak", chan_speak, 1},
|
||||
{"GetDigits", chan_get_digits, 1},
|
||||
{"Answer", chan_answer, 0},
|
||||
{"streamFile", chan_streamfile, 1},
|
||||
{"speak", chan_speak, 1},
|
||||
{"getDigits", chan_get_digits, 1},
|
||||
{"answer", chan_answer, 0},
|
||||
{0}
|
||||
};
|
||||
|
||||
@@ -224,13 +386,13 @@ static JSPropertySpec chan_props[] = {
|
||||
|
||||
static JSBool chan_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
|
||||
{
|
||||
struct jchan *jc = JS_GetPrivate(cx, obj);
|
||||
struct js_session *jss = JS_GetPrivate(cx, obj);
|
||||
int param = 0;
|
||||
JSBool res = JS_TRUE;
|
||||
switch_channel *channel;
|
||||
char *name;
|
||||
|
||||
channel = switch_core_session_get_channel(jc->session);
|
||||
channel = switch_core_session_get_channel(jss->session);
|
||||
assert(channel != NULL);
|
||||
|
||||
name = JS_GetStringBytes(JS_ValueToString(cx, id));
|
||||
@@ -258,8 +420,8 @@ static JSBool chan_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp
|
||||
}
|
||||
|
||||
|
||||
JSClass chan_class = {
|
||||
"Chan", JSCLASS_HAS_PRIVATE,
|
||||
JSClass session_class = {
|
||||
"Session", JSCLASS_HAS_PRIVATE,
|
||||
JS_PropertyStub, JS_PropertyStub, chan_getProperty, JS_PropertyStub,
|
||||
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
|
||||
};
|
||||
@@ -279,28 +441,32 @@ static JSBool js_log(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsva
|
||||
|
||||
|
||||
static JSFunctionSpec fs_functions[] = {
|
||||
{"Log", js_log, 2},
|
||||
{"console_log", js_log, 2},
|
||||
{0}
|
||||
};
|
||||
|
||||
|
||||
static JSObject *new_jchan(JSContext *cx, JSObject *obj, switch_core_session *session, struct jchan *jc, int flags) {
|
||||
JSObject *Chan;
|
||||
if ((Chan = JS_DefineObject(cx, obj, "Chan", &chan_class, NULL, 0))) {
|
||||
memset(jc, 0, sizeof(struct jchan));
|
||||
jc->session = session;
|
||||
jc->flags = flags;
|
||||
if ((JS_SetPrivate(cx, Chan, jc) &&
|
||||
JS_DefineProperties(cx, Chan, chan_props) &&
|
||||
JS_DefineFunctions(cx, Chan, chan_methods))) {
|
||||
return Chan;
|
||||
static JSObject *new_js_session(JSContext *cx, JSObject *obj, switch_core_session *session, struct js_session *jss, char *name, int flags)
|
||||
{
|
||||
JSObject *session_obj;
|
||||
if ((session_obj = JS_DefineObject(cx, obj, name, &session_class, NULL, 0))) {
|
||||
memset(jss, 0, sizeof(struct js_session));
|
||||
jss->session = session;
|
||||
jss->flags = flags;
|
||||
jss->cx = cx;
|
||||
jss->obj = obj;
|
||||
if ((JS_SetPrivate(cx, session_obj, jss) &&
|
||||
JS_DefineProperties(cx, session_obj, chan_props) &&
|
||||
JS_DefineFunctions(cx, session_obj, chan_methods))) {
|
||||
return session_obj;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int eval_some_js(char *code, JSContext *cx, JSObject *obj, jsval *rval) {
|
||||
static int eval_some_js(char *code, JSContext *cx, JSObject *obj, jsval *rval)
|
||||
{
|
||||
JSScript *script;
|
||||
char *cptr;
|
||||
char path[512];
|
||||
@@ -321,7 +487,7 @@ static int eval_some_js(char *code, JSContext *cx, JSObject *obj, jsval *rval) {
|
||||
}
|
||||
|
||||
if (script) {
|
||||
res = JS_ExecuteScript(cx, obj, script, rval) == JS_TRUE ? 0 : -1;
|
||||
res = JS_ExecuteScript(cx, obj, script, rval) == JS_TRUE ? 1 : 0;
|
||||
JS_DestroyScript(cx, script);
|
||||
}
|
||||
|
||||
@@ -334,8 +500,8 @@ static void js_exec(switch_core_session *session, char *data)
|
||||
int res=-1;
|
||||
jsval rval;
|
||||
JSContext *cx;
|
||||
JSObject *glob, *Chan;
|
||||
struct jchan jc;
|
||||
JSObject *javascript_global_object, *session_obj;
|
||||
struct js_session jss;
|
||||
int x = 0, y = 0;
|
||||
char buf[512];
|
||||
int flags = 0;
|
||||
@@ -366,12 +532,12 @@ static void js_exec(switch_core_session *session, char *data)
|
||||
|
||||
if ((cx = JS_NewContext(globals.rt, globals.gStackChunkSize))) {
|
||||
JS_SetErrorReporter(cx, js_error);
|
||||
if ((glob = JS_NewObject(cx, &global_class, NULL, NULL)) &&
|
||||
JS_DefineFunctions(cx, glob, fs_functions) &&
|
||||
JS_InitStandardClasses(cx, glob) &&
|
||||
(Chan = new_jchan(cx, glob, session, &jc, flags))) {
|
||||
JS_SetGlobalObject(cx, glob);
|
||||
JS_SetPrivate(cx, glob, session);
|
||||
if ((javascript_global_object = JS_NewObject(cx, &global_class, NULL, NULL)) &&
|
||||
JS_DefineFunctions(cx, javascript_global_object, fs_functions) &&
|
||||
JS_InitStandardClasses(cx, javascript_global_object) &&
|
||||
(session_obj = new_js_session(cx, javascript_global_object, session, &jss, "session", flags))) {
|
||||
JS_SetGlobalObject(cx, javascript_global_object);
|
||||
JS_SetPrivate(cx, javascript_global_object, session);
|
||||
res = 0;
|
||||
|
||||
do {
|
||||
@@ -386,22 +552,22 @@ static void js_exec(switch_core_session *session, char *data)
|
||||
*arg = '\0';
|
||||
arg++;
|
||||
snprintf(buf, sizeof(buf), "~var Argv = new Array(%d);", y);
|
||||
eval_some_js(buf, cx, glob, &rval);
|
||||
eval_some_js(buf, cx, javascript_global_object, &rval);
|
||||
snprintf(buf, sizeof(buf), "~var argc = %d", y);
|
||||
eval_some_js(buf, cx, glob, &rval);
|
||||
eval_some_js(buf, cx, javascript_global_object, &rval);
|
||||
do {
|
||||
if ((nextarg = strchr(arg, ':'))) {
|
||||
*nextarg = '\0';
|
||||
nextarg++;
|
||||
}
|
||||
snprintf(buf, sizeof(buf), "~Argv[%d] = \"%s\";", x++, arg);
|
||||
eval_some_js(buf, cx, glob, &rval);
|
||||
eval_some_js(buf, cx, javascript_global_object, &rval);
|
||||
arg = nextarg;
|
||||
} while (arg);
|
||||
}
|
||||
if ((res=eval_some_js(code, cx, glob, &rval)) < 0) {
|
||||
if (!(res=eval_some_js(code, cx, javascript_global_object, &rval))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
code = next;
|
||||
} while (code);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user