int adsi_get_cpeid(struct ast_channel *chan, unsigned char *cpeid, int voice) { char buf[256]; int bytes = 0; int res; bytes += adsi_data_mode(buf); adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DISPLAY); bytes = 0; bytes += adsi_query_cpeid(buf); adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DISPLAY); /* Get response */ memset(buf, 0, sizeof(buf)); res = adsi_read_encoded_dtmf(chan, cpeid, 4); if (res != 4) { ast_log(LOG_WARNING, "Got %d bytes back of encoded DTMF, expecting 4\n", res); res = 0; } else { res = 1; } if (voice) { bytes = 0; bytes += adsi_voice_mode(buf, 0); adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DISPLAY); /* Ignore the resulting DTMF B announcing it's in voice mode */ ast_waitfordigit(chan, 1000); } return res; }
static int _ast_adsi_get_cpeinfo(struct ast_channel *chan, int *width, int *height, int *buttons, int voice) { unsigned char buf[256] = ""; int bytes = 0, res; bytes += ast_adsi_data_mode(buf); ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0); bytes = 0; bytes += ast_adsi_query_cpeinfo(buf); ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0); /* Get width */ if ((res = ast_readstring(chan, (char *)buf, 2, 1000, 500, "")) < 0) return res; if (strlen((char *)buf) != 2) { ast_log(LOG_WARNING, "Got %d bytes of width, expecting 2\n", res); res = 0; } else { res = 1; } if (width) *width = atoi((char *)buf); /* Get height */ memset(buf, 0, sizeof(buf)); if (res) { if ((res = ast_readstring(chan, (char *)buf, 2, 1000, 500, "")) < 0) return res; if (strlen((char *)buf) != 2) { ast_log(LOG_WARNING, "Got %d bytes of height, expecting 2\n", res); res = 0; } else { res = 1; } if (height) *height= atoi((char *)buf); } /* Get buttons */ memset(buf, 0, sizeof(buf)); if (res) { if ((res = ast_readstring(chan, (char *)buf, 1, 1000, 500, "")) < 0) return res; if (strlen((char *)buf) != 1) { ast_log(LOG_WARNING, "Got %d bytes of buttons, expecting 1\n", res); res = 0; } else { res = 1; } if (buttons) *buttons = atoi((char *)buf); } if (voice) { bytes = 0; bytes += ast_adsi_voice_mode(buf, 0); ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0); /* Ignore the resulting DTMF B announcing it's in voice mode */ ast_waitfordigit(chan, 1000); } return res; }
static int cpeid_exec(struct ast_channel *chan, const char *idata) { int res=0; unsigned char cpeid[4]; int gotgeometry = 0; int gotcpeid = 0; int width, height, buttons; char *data[4]; unsigned int x; for (x = 0; x < 4; x++) data[x] = alloca(80); strcpy(data[0], "** CPE Info **"); strcpy(data[1], "Identifying CPE..."); strcpy(data[2], "Please wait..."); res = ast_adsi_load_session(chan, NULL, 0, 1); if (res > 0) { cpeid_setstatus(chan, data, 0); res = ast_adsi_get_cpeid(chan, cpeid, 0); if (res > 0) { gotcpeid = 1; ast_verb(3, "Got CPEID of '%02x:%02x:%02x:%02x' on '%s'\n", cpeid[0], cpeid[1], cpeid[2], cpeid[3], chan->name); } if (res > -1) { strcpy(data[1], "Measuring CPE..."); strcpy(data[2], "Please wait..."); cpeid_setstatus(chan, data, 0); res = ast_adsi_get_cpeinfo(chan, &width, &height, &buttons, 0); if (res > -1) { ast_verb(3, "CPE has %d lines, %d columns, and %d buttons on '%s'\n", height, width, buttons, chan->name); gotgeometry = 1; } } if (res > -1) { if (gotcpeid) snprintf(data[1], 80, "CPEID: %02x:%02x:%02x:%02x", cpeid[0], cpeid[1], cpeid[2], cpeid[3]); else strcpy(data[1], "CPEID Unknown"); if (gotgeometry) snprintf(data[2], 80, "Geom: %dx%d, %d buttons", width, height, buttons); else strcpy(data[2], "Geometry unknown"); strcpy(data[3], "Press # to exit"); cpeid_setstatus(chan, data, 1); for(;;) { res = ast_waitfordigit(chan, 1000); if (res < 0) break; if (res == '#') { res = 0; break; } } ast_adsi_unload_session(chan); } } return res; }
int adsi_read_encoded_dtmf(struct ast_channel *chan, unsigned char *buf, int maxlen) { int bytes = 0; int res; unsigned char current = 0; int gotstar = 0; int pos = 0; memset(buf, 0, sizeof(buf)); while(bytes <= maxlen) { /* Wait up to a second for a digit */ res = ast_waitfordigit(chan, 1000); if (!res) break; if (res == '*') { gotstar = 1; continue; } /* Ignore anything other than a digit */ if ((res < '0') || (res > '9')) continue; res -= '0'; if (gotstar) res += 9; if (pos) { pos = 0; buf[bytes++] = (res << 4) | current; } else { pos = 1; current = res; } gotstar = 0; } return bytes; }
static int sendnoise(struct ast_channel *chan, int ms) { int res; res = ast_tonepair_start(chan, 1537, 2195, ms, 8192); if (!res) { res = ast_waitfordigit(chan, ms); ast_tonepair_stop(chan); } return res; }
static int _ast_adsi_print(struct ast_channel *chan, char **lines, int *aligns, int voice) { unsigned char buf[4096]; int bytes = 0, res, x; for(x = 0; lines[x]; x++) bytes += ast_adsi_display(buf + bytes, ADSI_INFO_PAGE, x+1, aligns[x], 0, lines[x], ""); bytes += ast_adsi_set_line(buf + bytes, ADSI_INFO_PAGE, 1); if (voice) bytes += ast_adsi_voice_mode(buf + bytes, 0); res = ast_adsi_transmit_message_full(chan, buf, bytes, ADSI_MSG_DISPLAY, 0); if (voice) /* Ignore the resulting DTMF B announcing it's in voice mode */ ast_waitfordigit(chan, 1000); return res; }
static char *listen_for_dtmf(struct ast_channel *chan, int timeout, int max_digits) { char *dtmf_conversion = (char *) malloc(100); char cnv[2]; int dtmf = 0, i = 0; memset(dtmf_conversion, 0, 100); memset(cnv, 0, 2); while (i < max_digits) { dtmf = ast_waitfordigit(chan, timeout); if (!dtmf) { break; } sprintf(cnv, "%c", dtmf_conv(dtmf)); strcat(dtmf_conversion, cnv); i += 1; } return strdup(dtmf_conversion); }
int AST_OPTIONAL_API_NAME(ast_adsi_read_encoded_dtmf)(struct ast_channel *chan, unsigned char *buf, int maxlen) { int bytes = 0, res, gotstar = 0, pos = 0; unsigned char current = 0; memset(buf, 0, maxlen); while (bytes <= maxlen) { /* Wait up to a second for a digit */ if (!(res = ast_waitfordigit(chan, 1000))) { break; } if (res == '*') { gotstar = 1; continue; } /* Ignore anything other than a digit */ if ((res < '0') || (res > '9')) { continue; } res -= '0'; if (gotstar) { res += 9; } if (pos) { pos = 0; buf[bytes++] = (res << 4) | current; } else { pos = 1; current = res; } gotstar = 0; } return bytes; }
int AST_OPTIONAL_API_NAME(ast_adsi_transmit_message_full)(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype, int dowait) { unsigned char *msgs[5] = { NULL, NULL, NULL, NULL, NULL }; int msglens[5], msgtypes[5], newdatamode = (ast_channel_adsicpe(chan) & ADSI_FLAG_DATAMODE), res, x, waitforswitch = 0; struct ast_format writeformat; struct ast_format readformat; ast_format_copy(&writeformat, ast_channel_writeformat(chan)); ast_format_copy(&readformat, ast_channel_readformat(chan)); for (x = 0; x < msglen; x += (msg[x+1]+2)) { if (msg[x] == ADSI_SWITCH_TO_DATA) { ast_debug(1, "Switch to data is sent!\n"); waitforswitch++; newdatamode = ADSI_FLAG_DATAMODE; } if (msg[x] == ADSI_SWITCH_TO_VOICE) { ast_debug(1, "Switch to voice is sent!\n"); waitforswitch++; newdatamode = 0; } } msgs[0] = msg; msglens[0] = msglen; msgtypes[0] = msgtype; if (msglen > 253) { ast_log(LOG_WARNING, "Can't send ADSI message of %d bytes, too large\n", msglen); return -1; } ast_stopstream(chan); if (ast_set_write_format_by_id(chan, AST_FORMAT_ULAW)) { ast_log(LOG_WARNING, "Unable to set write format to ULAW\n"); return -1; } if (ast_set_read_format_by_id(chan, AST_FORMAT_ULAW)) { ast_log(LOG_WARNING, "Unable to set read format to ULAW\n"); if (writeformat.id) { if (ast_set_write_format(chan, &writeformat)) { ast_log(LOG_WARNING, "Unable to restore write format to %s\n", ast_getformatname(&writeformat)); } } return -1; } res = __adsi_transmit_messages(chan, msgs, msglens, msgtypes); if (dowait) { ast_debug(1, "Wait for switch is '%d'\n", waitforswitch); while (waitforswitch-- && ((res = ast_waitfordigit(chan, 1000)) > 0)) { res = 0; ast_debug(1, "Waiting for 'B'...\n"); } } if (!res) { ast_channel_adsicpe_set(chan, (ast_channel_adsicpe(chan) & ~ADSI_FLAG_DATAMODE) | newdatamode); } if (writeformat.id) { ast_set_write_format(chan, &writeformat); } if (readformat.id) { ast_set_read_format(chan, &readformat); } if (!res) { res = ast_safe_sleep(chan, 100 ); } return res; }
static int _while_exec(struct ast_channel *chan, void *data, int end) { int res=0; const char *while_pri = NULL; char *my_name = NULL; const char *condition = NULL, *label = NULL; char varname[VAR_SIZE], end_varname[VAR_SIZE]; const char *prefix = "WHILE"; size_t size=0; int used_index_i = -1, x=0; char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0"; if (!chan) { /* huh ? */ return -1; } #if 0 /* dont want run away loops if the chan isn't even up this is up for debate since it slows things down a tad ...... Debate is over... this prevents While/EndWhile from working within the "h" extension. Not good. */ if (ast_waitfordigit(chan,1) < 0) return -1; #endif for (x=0;;x++) { if (get_index(chan, prefix, x)) { used_index_i = x; } else break; } snprintf(used_index, VAR_SIZE, "%d", used_index_i); snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1); if (!end) condition = ast_strdupa(data); size = strlen(chan->context) + strlen(chan->exten) + 32; my_name = alloca(size); memset(my_name, 0, size); snprintf(my_name, size, "%s_%s_%d", chan->context, chan->exten, chan->priority); ast_channel_lock(chan); if (end) { label = used_index; } else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) { label = new_index; pbx_builtin_setvar_helper(chan, my_name, label); } snprintf(varname, VAR_SIZE, "%s_%s", prefix, label); if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) { while_pri = ast_strdupa(while_pri); snprintf(end_varname,VAR_SIZE,"END_%s",varname); } ast_channel_unlock(chan); if ((!end && !pbx_checkcondition(condition)) || (end == 2)) { /* Condition Met (clean up helper vars) */ const char *goto_str; pbx_builtin_setvar_helper(chan, varname, NULL); pbx_builtin_setvar_helper(chan, my_name, NULL); snprintf(end_varname,VAR_SIZE,"END_%s",varname); ast_channel_lock(chan); if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) { ast_parseable_goto(chan, goto_str); pbx_builtin_setvar_helper(chan, end_varname, NULL); } else { int pri = find_matching_endwhile(chan); if (pri > 0) { ast_verb(3, "Jumping to priority %d\n", pri); chan->priority = pri; } else { ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan->context, chan->exten, chan->priority); } } ast_channel_unlock(chan); return res; } if (!end && !while_pri) { char *goto_str; size = strlen(chan->context) + strlen(chan->exten) + 32; goto_str = alloca(size); memset(goto_str, 0, size); snprintf(goto_str, size, "%s,%s,%d", chan->context, chan->exten, chan->priority); pbx_builtin_setvar_helper(chan, varname, goto_str); } else if (end && while_pri) { /* END of loop */ snprintf(end_varname, VAR_SIZE, "END_%s", varname); if (! pbx_builtin_getvar_helper(chan, end_varname)) { char *goto_str; size = strlen(chan->context) + strlen(chan->exten) + 32; goto_str = alloca(size); memset(goto_str, 0, size); snprintf(goto_str, size, "%s,%s,%d", chan->context, chan->exten, chan->priority+1); pbx_builtin_setvar_helper(chan, end_varname, goto_str); } ast_parseable_goto(chan, while_pri); } return res; }
/* play name of mailbox owner. * returns: -1 for bad or missing extension * '1' for selected entry from directory * '*' for skipped entry from directory */ static int play_mailbox_owner(struct ast_channel *chan, char *context, char *dialcontext, char *ext, char *name, int readext, int fromappvm) { int res = 0; int loop; char fn[256]; /* Check for the VoiceMail2 greeting first */ snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/greet", ast_config_AST_SPOOL_DIR, context, ext); #ifdef ODBC_STORAGE retrieve_file(fn); #endif if (ast_fileexists(fn, NULL, chan->language) <= 0) { /* no file, check for an old-style Voicemail greeting */ snprintf(fn, sizeof(fn), "%s/vm/%s/greet", ast_config_AST_SPOOL_DIR, ext); } #ifdef ODBC_STORAGE retrieve_file(fn); #endif if (ast_fileexists(fn, NULL, chan->language) > 0) { res = ast_stream_and_wait(chan, fn, chan->language, AST_DIGIT_ANY); ast_stopstream(chan); /* If Option 'e' was specified, also read the extension number with the name */ if (readext) { ast_stream_and_wait(chan, "vm-extension", chan->language, AST_DIGIT_ANY); res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, chan->language); } } else { res = ast_say_character_str(chan, S_OR(name, ext), AST_DIGIT_ANY, chan->language); if (!ast_strlen_zero(name) && readext) { ast_stream_and_wait(chan, "vm-extension", chan->language, AST_DIGIT_ANY); res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, chan->language); } } #ifdef ODBC_STORAGE ast_filedelete(fn, NULL); #endif for (loop = 3 ; loop > 0; loop--) { if (!res) res = ast_stream_and_wait(chan, "dir-instr", chan->language, AST_DIGIT_ANY); if (!res) res = ast_waitfordigit(chan, 3000); ast_stopstream(chan); if (res < 0) /* User hungup, so jump out now */ break; if (res == '1') { /* Name selected */ if (fromappvm) { /* We still want to set the exten though */ ast_copy_string(chan->exten, ext, sizeof(chan->exten)); } else { if (ast_goto_if_exists(chan, dialcontext, ext, 1)) { ast_log(LOG_WARNING, "Can't find extension '%s' in context '%s'. " "Did you pass the wrong context to Directory?\n", ext, dialcontext); res = -1; } } break; } if (res == '*') /* Skip to next match in list */ break; /* Not '1', or '*', so decrement number of tries */ res = 0; } return(res); }
static int chanspy_exec(struct ast_channel *chan, void *data) { struct localuser *u; struct ast_channel *peer=NULL, *prev=NULL; char name[AST_NAME_STRLEN], peer_name[AST_NAME_STRLEN + 5], *args, *ptr = NULL, *options = NULL, *spec = NULL, *argv[5], *mygroup = NULL, *recbase = NULL; int res = -1, volfactor = 0, silent = 0, argc = 0, bronly = 0, chosen = 0, count=0, waitms = 100, num = 0, oldrf = 0, oldwf = 0, fd = 0; struct ast_flags flags; signed char zero_volume = 0; if (!(args = ast_strdupa((char *)data))) { ast_log(LOG_ERROR, "Out of memory!\n"); return -1; } LOCAL_USER_ADD(u); oldrf = chan->readformat; oldwf = chan->writeformat; if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) { ast_log(LOG_ERROR, "Could Not Set Read Format.\n"); LOCAL_USER_REMOVE(u); return -1; } if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) { ast_log(LOG_ERROR, "Could Not Set Write Format.\n"); LOCAL_USER_REMOVE(u); return -1; } ast_answer(chan); ast_set_flag(chan, AST_FLAG_SPYING); /* so nobody can spy on us while we are spying */ if ((argc = ast_app_separate_args(args, '|', argv, sizeof(argv) / sizeof(argv[0])))) { spec = argv[0]; if ( argc > 1) { options = argv[1]; } if (ast_strlen_zero(spec) || !strcmp(spec, "all")) { spec = NULL; } } if (options) { char *opts[OPT_ARG_ARRAY_SIZE]; ast_app_parse_options(chanspy_opts, &flags, opts, options); if (ast_test_flag(&flags, OPTION_GROUP)) { mygroup = opts[OPT_ARG_GROUP]; } if (ast_test_flag(&flags, OPTION_RECORD)) { if (!(recbase = opts[OPT_ARG_RECORD])) { recbase = "chanspy"; } } silent = ast_test_flag(&flags, OPTION_QUIET); bronly = ast_test_flag(&flags, OPTION_BRIDGED); if (ast_test_flag(&flags, OPTION_VOLUME) && opts[OPT_ARG_VOLUME]) { int vol; if ((sscanf(opts[OPT_ARG_VOLUME], "%d", &vol) != 1) || (vol > 4) || (vol < -4)) ast_log(LOG_NOTICE, "Volume factor must be a number between -4 and 4\n"); else volfactor = vol; } } else ast_clear_flag(&flags, AST_FLAGS_ALL); if (recbase) { char filename[512]; snprintf(filename,sizeof(filename),"%s/%s.%d.raw",ast_config_AST_MONITOR_DIR, recbase, (int)time(NULL)); if ((fd = open(filename, O_CREAT | O_WRONLY, O_TRUNC, 0644)) <= 0) { ast_log(LOG_WARNING, "Cannot open %s for recording\n", filename); fd = 0; } } for(;;) { if (!silent) { res = ast_streamfile(chan, "beep", chan->language); if (!res) res = ast_waitstream(chan, ""); if (res < 0) { ast_clear_flag(chan, AST_FLAG_SPYING); break; } } count = 0; res = ast_waitfordigit(chan, waitms); if (res < 0) { ast_clear_flag(chan, AST_FLAG_SPYING); break; } peer = local_channel_walk(NULL); prev=NULL; while(peer) { if (peer != chan) { char *group = NULL; int igrp = 1; if (peer == prev && !chosen) { break; } chosen = 0; group = pbx_builtin_getvar_helper(peer, "SPYGROUP"); if (mygroup) { if (!group || strcmp(mygroup, group)) { igrp = 0; } } if (igrp && (!spec || ((strlen(spec) <= strlen(peer->name) && !strncasecmp(peer->name, spec, strlen(spec)))))) { if (peer && (!bronly || ast_bridged_channel(peer)) && !ast_check_hangup(peer) && !ast_test_flag(peer, AST_FLAG_SPYING)) { int x = 0; strncpy(peer_name, "spy-", 5); strncpy(peer_name + strlen(peer_name), peer->name, AST_NAME_STRLEN); ptr = strchr(peer_name, '/'); *ptr = '\0'; ptr++; for (x = 0 ; x < strlen(peer_name) ; x++) { if (peer_name[x] == '/') { break; } peer_name[x] = tolower(peer_name[x]); } if (!silent) { if (ast_fileexists(peer_name, NULL, NULL) != -1) { res = ast_streamfile(chan, peer_name, chan->language); if (!res) res = ast_waitstream(chan, ""); if (res) break; } else res = ast_say_character_str(chan, peer_name, "", chan->language); if ((num=atoi(ptr))) ast_say_digits(chan, atoi(ptr), "", chan->language); } count++; prev = peer; res = channel_spy(chan, peer, &volfactor, fd); if (res == -1) { break; } else if (res > 1 && spec) { snprintf(name, AST_NAME_STRLEN, "%s/%d", spec, res); if ((peer = local_get_channel_begin_name(name))) { chosen = 1; } continue; } } } } if ((peer = local_channel_walk(peer)) == NULL) { break; } } waitms = count ? 100 : 5000; } if (fd > 0) { close(fd); } if (oldrf && ast_set_read_format(chan, oldrf) < 0) { ast_log(LOG_ERROR, "Could Not Set Read Format.\n"); } if (oldwf && ast_set_write_format(chan, oldwf) < 0) { ast_log(LOG_ERROR, "Could Not Set Write Format.\n"); } ast_clear_flag(chan, AST_FLAG_SPYING); ast_channel_setoption(chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0); ALL_DONE(u, res); }
int ast_control_streamfile(struct ast_channel *chan, char *file, char *fwd, char *rev, char *stop, char *pause, int skipms) { struct timeval started, ended; long elapsed = 0,last_elapsed =0; char *breaks=NULL; char *end=NULL; int blen=2; int res=0; if (stop) blen += strlen(stop); if (pause) blen += strlen(pause); if (blen > 2) { breaks = alloca(blen + 1); breaks[0] = '\0'; strcat(breaks, stop); strcat(breaks, pause); } if (chan->_state != AST_STATE_UP) res = ast_answer(chan); if (chan) ast_stopstream(chan); if (file) { if ((end = strchr(file,':'))) { if (!strcasecmp(end, ":end")) { *end = '\0'; end++; } } } for (;;) { gettimeofday(&started,NULL); if (chan) ast_stopstream(chan); res = ast_streamfile(chan, file, chan->language); if (!res) { if (end) { ast_seekstream(chan->stream, 0, SEEK_END); end=NULL; } res = 1; if (elapsed) { ast_stream_fastforward(chan->stream, elapsed); last_elapsed = elapsed - 200; } if (res) res = ast_waitstream_fr(chan, breaks, fwd, rev, skipms); else break; } if (res < 1) break; if (pause != NULL && strchr(pause, res)) { gettimeofday(&ended, NULL); elapsed = (((ended.tv_sec * 1000) + ended.tv_usec / 1000) - ((started.tv_sec * 1000) + started.tv_usec / 1000) + last_elapsed); for(;;) { if (chan) ast_stopstream(chan); res = ast_waitfordigit(chan, 1000); if (res == 0) continue; else if (res == -1 || strchr(pause, res) || (stop && strchr(stop, res))) break; } if (res == *pause) { res = 0; continue; } } if (res == -1) break; /* if we get one of our stop chars, return it to the calling function */ if (stop && strchr(stop, res)) { /* res = 0; */ break; } } if (chan) ast_stopstream(chan); return res; }
static int directory_exec(struct ast_channel *chan, void *data) { int res = 0; struct localuser *u; struct ast_config *cfg; int last = 1; int fromappvm = 0; char *context, *dialcontext, *dirintro, *options; if (ast_strlen_zero(data)) { ast_log(LOG_WARNING, "Directory requires an argument (context[,dialcontext])\n"); return -1; } LOCAL_USER_ADD(u); context = ast_strdupa(data); dialcontext = strchr(context, '|'); if (dialcontext) { *dialcontext = '\0'; dialcontext++; options = strchr(dialcontext, '|'); if (options) { *options = '\0'; options++; if (strchr(options, 'f')) last = 0; if (strchr(options, 'v')) fromappvm = 1; } } else dialcontext = context; cfg = realtime_directory(context); if (!cfg) { LOCAL_USER_REMOVE(u); return -1; } dirintro = ast_variable_retrieve(cfg, context, "directoryintro"); if (ast_strlen_zero(dirintro)) dirintro = ast_variable_retrieve(cfg, "general", "directoryintro"); if (ast_strlen_zero(dirintro)) { if (last) dirintro = "dir-intro"; else dirintro = "dir-intro-fn"; } if (chan->_state != AST_STATE_UP) res = ast_answer(chan); for (;;) { if (!res) res = ast_streamfile(chan, dirintro, chan->language); if (!res) res = ast_waitstream(chan, AST_DIGIT_ANY); ast_stopstream(chan); if (!res) res = ast_waitfordigit(chan, 5000); if (res > 0) { res = do_directory(chan, cfg, context, dialcontext, res, last, fromappvm); if (res > 0) { res = ast_waitstream(chan, AST_DIGIT_ANY); ast_stopstream(chan); if (res >= 0) { continue; } } } break; } ast_config_destroy(cfg); LOCAL_USER_REMOVE(u); return res; }
/* play name of mailbox owner. * returns: -1 for bad or missing extension * '1' for selected entry from directory * '*' for skipped entry from directory */ static int play_mailbox_owner(struct ast_channel *chan, char *context, char *dialcontext, char *ext, char *name, int fromappvm) { int res = 0; int loop = 3; char fn[256]; char fn2[256]; /* Check for the VoiceMail2 greeting first */ snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/greet", (char *)ast_config_AST_SPOOL_DIR, context, ext); #ifdef USE_ODBC_STORAGE retrieve_file(fn); #endif /* Otherwise, check for an old-style Voicemail greeting */ snprintf(fn2, sizeof(fn2), "%s/vm/%s/greet", (char *)ast_config_AST_SPOOL_DIR, ext); #ifdef USE_ODBC_STORAGE retrieve_file(fn2); #endif if (ast_fileexists(fn, NULL, chan->language) > 0) { res = ast_streamfile(chan, fn, chan->language); if (!res) { res = ast_waitstream(chan, AST_DIGIT_ANY); } ast_stopstream(chan); } else if (ast_fileexists(fn2, NULL, chan->language) > 0) { res = ast_streamfile(chan, fn2, chan->language); if (!res) { res = ast_waitstream(chan, AST_DIGIT_ANY); } ast_stopstream(chan); } else { res = ast_say_character_str(chan, !ast_strlen_zero(name) ? name : ext, AST_DIGIT_ANY, chan->language); } #ifdef USE_ODBC_STORAGE ast_filedelete(fn, NULL); ast_filedelete(fn2, NULL); #endif while (loop) { if (!res) { res = ast_streamfile(chan, "dir-instr", chan->language); } if (!res) { res = ast_waitstream(chan, AST_DIGIT_ANY); } if (!res) { res = ast_waitfordigit(chan, 3000); } ast_stopstream(chan); if (res > -1) { switch (res) { case '1': /* Name selected */ loop = 0; if (fromappvm) { /* We still want to set the exten */ ast_copy_string(chan->exten, ext, sizeof(chan->exten)); } else { if (ast_goto_if_exists(chan, dialcontext, ext, 1)) { ast_log(LOG_WARNING, "Can't find extension '%s' in context '%s'. " "Did you pass the wrong context to Directory?\n", ext, dialcontext); res = -1; } } break; case '*': /* Skip to next match in list */ loop = 0; break; default: /* Not '1', or '*', so decrement number of tries */ res = 0; loop--; break; } /* end switch */ } /* end if */ else { /* User hungup, so jump out now */ loop = 0; } } /* end while */ return(res); }
static int common_exec(struct ast_channel *chan, const struct ast_flags *flags, int volfactor, const int fd, const char *mygroup, const char *myenforced, const char *spec, const char *exten, const char *context) { char nameprefix[AST_NAME_STRLEN]; char peer_name[AST_NAME_STRLEN + 5]; char exitcontext[AST_MAX_CONTEXT] = ""; signed char zero_volume = 0; int waitms; int res; char *ptr; int num; int num_spyed_upon = 1; struct chanspy_ds chanspy_ds; if (ast_test_flag(flags, OPTION_EXIT)) { const char *c; if ((c = pbx_builtin_getvar_helper(chan, "SPY_EXIT_CONTEXT"))) ast_copy_string(exitcontext, c, sizeof(exitcontext)); else if (!ast_strlen_zero(chan->macrocontext)) ast_copy_string(exitcontext, chan->macrocontext, sizeof(exitcontext)); else ast_copy_string(exitcontext, chan->context, sizeof(exitcontext)); } ast_mutex_init(&chanspy_ds.lock); snprintf(chanspy_ds.unique_id, sizeof(chanspy_ds.unique_id), "%d", ast_atomic_fetchadd_int(&next_unique_id_to_use, +1)); if (chan->_state != AST_STATE_UP) ast_answer(chan); ast_set_flag(chan, AST_FLAG_SPYING); /* so nobody can spy on us while we are spying */ waitms = 100; for (;;) { struct chanspy_ds *peer_chanspy_ds = NULL, *next_chanspy_ds = NULL; struct ast_channel *prev = NULL, *peer = NULL; if (!ast_test_flag(flags, OPTION_QUIET) && num_spyed_upon) { res = ast_streamfile(chan, "beep", chan->language); if (!res) res = ast_waitstream(chan, ""); else if (res < 0) { ast_clear_flag(chan, AST_FLAG_SPYING); break; } if (!ast_strlen_zero(exitcontext)) { char tmp[2]; tmp[0] = res; tmp[1] = '\0'; if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) goto exit; else ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext); } } res = ast_waitfordigit(chan, waitms); if (res < 0) { ast_clear_flag(chan, AST_FLAG_SPYING); break; } if (!ast_strlen_zero(exitcontext)) { char tmp[2]; tmp[0] = res; tmp[1] = '\0'; if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) goto exit; else ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext); } /* reset for the next loop around, unless overridden later */ waitms = 100; num_spyed_upon = 0; for (peer_chanspy_ds = next_channel(chan, prev, spec, exten, context, &chanspy_ds); peer_chanspy_ds; chanspy_ds_free(peer_chanspy_ds), prev = peer, peer_chanspy_ds = next_chanspy_ds ? next_chanspy_ds : next_channel(chan, prev, spec, exten, context, &chanspy_ds), next_chanspy_ds = NULL) { const char *group; int igrp = !mygroup; char *groups[25]; int num_groups = 0; char dup_group[512]; int x; char *s; char *buffer; char *end; char *ext; char *form_enforced; int ienf = !myenforced; peer = peer_chanspy_ds->chan; ast_mutex_unlock(&peer_chanspy_ds->lock); if (peer == prev) { ast_channel_unlock(peer); chanspy_ds_free(peer_chanspy_ds); break; } if (ast_check_hangup(chan)) { ast_channel_unlock(peer); chanspy_ds_free(peer_chanspy_ds); break; } if (ast_test_flag(flags, OPTION_BRIDGED) && !ast_bridged_channel(peer)) { ast_channel_unlock(peer); continue; } if (ast_check_hangup(peer) || ast_test_flag(peer, AST_FLAG_SPYING)) { ast_channel_unlock(peer); continue; } if (mygroup) { if ((group = pbx_builtin_getvar_helper(peer, "SPYGROUP"))) { ast_copy_string(dup_group, group, sizeof(dup_group)); num_groups = ast_app_separate_args(dup_group, ':', groups, ARRAY_LEN(groups)); } for (x = 0; x < num_groups; x++) { if (!strcmp(mygroup, groups[x])) { igrp = 1; break; } } } if (!igrp) { ast_channel_unlock(peer); continue; } if (myenforced) { /* We don't need to allocate more space than just the length of (peer->name) for ext as we will cut the channel name's ending before copying into ext */ ext = alloca(strlen(peer->name)); form_enforced = alloca(strlen(myenforced) + 3); strcpy(form_enforced, ":"); strcat(form_enforced, myenforced); strcat(form_enforced, ":"); buffer = ast_strdupa(peer->name); if ((end = strchr(buffer, '-'))) { *end++ = ':'; *end = '\0'; } strcpy(ext, ":"); strcat(ext, buffer); if (strcasestr(form_enforced, ext)) ienf = 1; } if (!ienf) continue; strcpy(peer_name, "spy-"); strncat(peer_name, peer->name, AST_NAME_STRLEN - 4 - 1); ptr = strchr(peer_name, '/'); *ptr++ = '\0'; for (s = peer_name; s < ptr; s++) *s = tolower(*s); /* We have to unlock the peer channel here to avoid a deadlock. * So, when we need to dereference it again, we have to lock the * datastore and get the pointer from there to see if the channel * is still valid. */ ast_channel_unlock(peer); if (!ast_test_flag(flags, OPTION_QUIET)) { if (ast_fileexists(peer_name, NULL, NULL) != -1) { res = ast_streamfile(chan, peer_name, chan->language); if (!res) res = ast_waitstream(chan, ""); if (res) { chanspy_ds_free(peer_chanspy_ds); break; } } else res = ast_say_character_str(chan, peer_name, "", chan->language); if ((num = atoi(ptr))) ast_say_digits(chan, atoi(ptr), "", chan->language); } res = channel_spy(chan, peer_chanspy_ds, &volfactor, fd, flags, exitcontext); num_spyed_upon++; if (res == -1) { chanspy_ds_free(peer_chanspy_ds); goto exit; } else if (res == -2) { res = 0; chanspy_ds_free(peer_chanspy_ds); goto exit; } else if (res > 1 && spec) { struct ast_channel *next; snprintf(nameprefix, AST_NAME_STRLEN, "%s/%d", spec, res); if ((next = ast_get_channel_by_name_prefix_locked(nameprefix, strlen(nameprefix)))) { peer_chanspy_ds = chanspy_ds_free(peer_chanspy_ds); next_chanspy_ds = setup_chanspy_ds(next, &chanspy_ds); } else { /* stay on this channel, if it is still valid */ ast_mutex_lock(&peer_chanspy_ds->lock); if (peer_chanspy_ds->chan) { ast_channel_lock(peer_chanspy_ds->chan); next_chanspy_ds = peer_chanspy_ds; peer_chanspy_ds = NULL; } else { /* the channel is gone */ ast_mutex_unlock(&peer_chanspy_ds->lock); next_chanspy_ds = NULL; } } peer = NULL; } } if (res == -1 || ast_check_hangup(chan)) break; } exit: ast_clear_flag(chan, AST_FLAG_SPYING); ast_channel_setoption(chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0); ast_mutex_lock(&chanspy_ds.lock); ast_mutex_unlock(&chanspy_ds.lock); ast_mutex_destroy(&chanspy_ds.lock); return res; }
static int testserver_exec(struct ast_channel *chan, const char *data) { int res = 0; char testid[80]=""; char fn[80]; FILE *f; if (ast_channel_state(chan) != AST_STATE_UP) res = ast_answer(chan); /* Read version */ ast_debug(1, "Read client version\n"); if (!res) res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0); if (res > 0) res = 0; ast_debug(1, "client version: %s\n", testid); ast_debug(1, "Transmit server version\n"); res = ast_safe_sleep(chan, 1000); if (!res) res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0); if (res > 0) res = 0; if (!res) res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0); ast_debug(1, "read test identifier: %s\n", testid); /* Check for sneakyness */ if (strchr(testid, '/')) res = -1; if ((res >=0) && (!ast_strlen_zero(testid))) { /* Got a Test ID! Whoo hoo! */ /* Make the directory to hold the test results in case it's not there */ snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR); ast_mkdir(fn, 0777); snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid); if ((f = fopen(fn, "w+"))) { setlinebuf(f); fprintf(f, "SERVERCHAN: %s\n", ast_channel_name(chan)); fprintf(f, "SERVERTEST ID: %s\n", testid); fprintf(f, "ANSWER: PASS\n"); ast_debug(1, "Processing Test ID '%s'\n", testid); res = ast_safe_sleep(chan, 1000); if (!res) { /* Step 1: Send "1" */ ast_debug(1, "TestServer: 1. Send DTMF 1\n"); res = ast_dtmf_stream(chan, NULL, "1", 0,0 ); fprintf(f, "SEND DTMF 1: %s\n", (res < 0) ? "FAIL" : "PASS"); if (res > 0) res = 0; } if (!res) { /* Step 2: Wait for "2" */ ast_debug(1, "TestServer: 2. Wait DTMF 2\n"); res = ast_waitfordigit(chan, 3000); fprintf(f, "WAIT DTMF 2: %s\n", (res != '2') ? "FAIL" : "PASS"); if (res == '2') res = 0; else res = -1; } if (!res) { /* Step 3: Measure noise */ ast_debug(1, "TestServer: 3. Measure noise\n"); res = measurenoise(chan, 6000, "TestServer"); fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res); if (res > 0) res = 0; } if (!res) { /* Step 4: Send "4" */ ast_debug(1, "TestServer: 4. Send DTMF 4\n"); res = ast_dtmf_stream(chan, NULL, "4", 0, 0); fprintf(f, "SEND DTMF 4: %s\n", (res < 0) ? "FAIL" : "PASS"); if (res > 0) res = 0; } if (!res) { /* Step 5: Wait one second */ ast_debug(1, "TestServer: 5. Wait one second\n"); res = ast_safe_sleep(chan, 1000); fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS"); if (res > 0) res = 0; } if (!res) { /* Step 6: Measure noise */ ast_debug(1, "TestServer: 6. Measure tone\n"); res = measurenoise(chan, 4000, "TestServer"); fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res); if (res > 0) res = 0; } if (!res) { /* Step 7: Send "5" */ ast_debug(1, "TestServer: 7. Send DTMF 5\n"); res = ast_dtmf_stream(chan, NULL, "5", 0, 0); fprintf(f, "SEND DTMF 5: %s\n", (res < 0) ? "FAIL" : "PASS"); if (res > 0) res = 0; } if (!res) { /* Step 8: Transmit tone noise */ ast_debug(1, "TestServer: 8. Transmit tone\n"); res = sendnoise(chan, 6000); fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS"); } if (!res || (res == '7')) { /* Step 9: Wait for "7" */ ast_debug(1, "TestServer: 9. Wait DTMF 7\n"); if (!res) res = ast_waitfordigit(chan, 3000); fprintf(f, "WAIT DTMF 7: %s\n", (res != '7') ? "FAIL" : "PASS"); if (res == '7') res = 0; else res = -1; } if (!res) { res = ast_safe_sleep(chan, 1000); } if (!res) { /* Step 10: Send "8" */ ast_debug(1, "TestServer: 10. Send DTMF 8\n"); res = ast_dtmf_stream(chan, NULL, "8", 0, 0); fprintf(f, "SEND DTMF 8: %s\n", (res < 0) ? "FAIL" : "PASS"); if (res > 0) res = 0; } if (!res) { /* Step 11: Wait for hangup to arrive! */ ast_debug(1, "TestServer: 11. Waiting for hangup\n"); res = ast_safe_sleep(chan, 10000); fprintf(f, "WAIT HANGUP: %s\n", (res < 0) ? "PASS" : "FAIL"); } ast_log(LOG_NOTICE, "-- TEST COMPLETE--\n"); fprintf(f, "-- END TEST--\n"); fclose(f); res = -1; } else res = -1; } else { ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", ast_channel_name(chan)); res = -1; } return res; }
int adsi_transmit_message_full(struct ast_channel *chan, unsigned char *msg, int msglen, int msgtype, int dowait) { unsigned char *msgs[5] = { NULL, NULL, NULL, NULL, NULL }; int msglens[5]; int msgtypes[5]; int newdatamode; int res; int x; int writeformat, readformat; int waitforswitch = 0; writeformat = chan->writeformat; readformat = chan->readformat; newdatamode = chan->adsicpe & ADSI_FLAG_DATAMODE; for (x=0;x<msglen;x+=(msg[x+1]+2)) { if (msg[x] == ADSI_SWITCH_TO_DATA) { ast_log(LOG_DEBUG, "Switch to data is sent!\n"); waitforswitch++; newdatamode = ADSI_FLAG_DATAMODE; } if (msg[x] == ADSI_SWITCH_TO_VOICE) { ast_log(LOG_DEBUG, "Switch to voice is sent!\n"); waitforswitch++; newdatamode = 0; } } msgs[0] = msg; msglens[0] = msglen; msgtypes[0] = msgtype; if (msglen > 253) { ast_log(LOG_WARNING, "Can't send ADSI message of %d bytes, too large\n", msglen); return -1; } ast_stopstream(chan); if (ast_set_write_format(chan, AST_FORMAT_ULAW)) { ast_log(LOG_WARNING, "Unable to set write format to ULAW\n"); return -1; } if (ast_set_read_format(chan, AST_FORMAT_ULAW)) { ast_log(LOG_WARNING, "Unable to set read format to ULAW\n"); if (writeformat) { if (ast_set_write_format(chan, writeformat)) ast_log(LOG_WARNING, "Unable to restore write format to %d\n", writeformat); } return -1; } res = __adsi_transmit_messages(chan, msgs, msglens, msgtypes); if (dowait) { ast_log(LOG_DEBUG, "Wait for switch is '%d'\n", waitforswitch); while(waitforswitch-- && ((res = ast_waitfordigit(chan, 1000)) > 0)) { res = 0; ast_log(LOG_DEBUG, "Waiting for 'B'...\n"); } } if (!res) chan->adsicpe = (chan->adsicpe & ~ADSI_FLAG_DATAMODE) | newdatamode; if (writeformat) ast_set_write_format(chan, writeformat); if (readformat) ast_set_read_format(chan, readformat); if (!res) res = ast_safe_sleep(chan, 100 ); return res; }
static int common_exec(struct ast_channel *chan, const struct ast_flags *flags, int volfactor, const int fd, const char *mygroup, const char *spec, const char *exten, const char *context) { char nameprefix[AST_NAME_STRLEN]; char peer_name[AST_NAME_STRLEN + 5]; signed char zero_volume = 0; int waitms; int res; char *ptr; int num; int num_spyed_upon = 1; struct chanspy_ds chanspy_ds = { 0, }; ast_mutex_init(&chanspy_ds.lock); snprintf(chanspy_ds.unique_id, sizeof(chanspy_ds.unique_id), "%d", ast_atomic_fetchadd_int(&next_unique_id_to_use, +1)); if (chan->_state != AST_STATE_UP) ast_answer(chan); ast_set_flag(chan, AST_FLAG_SPYING); /* so nobody can spy on us while we are spying */ waitms = 100; for (;;) { struct chanspy_ds *peer_chanspy_ds = NULL, *next_chanspy_ds = NULL; struct ast_channel *prev = NULL, *peer = NULL; if (!ast_test_flag(flags, OPTION_QUIET) && num_spyed_upon) { res = ast_streamfile(chan, "beep", chan->language); if (!res) res = ast_waitstream(chan, ""); else if (res < 0) { ast_clear_flag(chan, AST_FLAG_SPYING); break; } } res = ast_waitfordigit(chan, waitms); if (res < 0) { ast_clear_flag(chan, AST_FLAG_SPYING); break; } /* reset for the next loop around, unless overridden later */ waitms = 100; num_spyed_upon = 0; for (peer_chanspy_ds = next_channel(chan, prev, spec, exten, context, &chanspy_ds); peer_chanspy_ds; chanspy_ds_free(peer_chanspy_ds), prev = peer, peer_chanspy_ds = next_chanspy_ds ? next_chanspy_ds : next_channel(chan, prev, spec, exten, context, &chanspy_ds), next_chanspy_ds = NULL) { const char *group; int igrp = !mygroup; char *groups[25]; int num_groups = 0; char dup_group[512]; int x; char *s; peer = peer_chanspy_ds->chan; ast_mutex_unlock(&peer_chanspy_ds->lock); if (peer == prev) { ast_channel_unlock(peer); chanspy_ds_free(peer_chanspy_ds); break; } if (ast_check_hangup(chan)) { ast_channel_unlock(peer); chanspy_ds_free(peer_chanspy_ds); break; } if (ast_test_flag(flags, OPTION_BRIDGED) && !ast_bridged_channel(peer)) { ast_channel_unlock(peer); continue; } if (ast_check_hangup(peer) || ast_test_flag(peer, AST_FLAG_SPYING)) { ast_channel_unlock(peer); continue; } if (mygroup) { if ((group = pbx_builtin_getvar_helper(peer, "SPYGROUP"))) { ast_copy_string(dup_group, group, sizeof(dup_group)); num_groups = ast_app_separate_args(dup_group, ':', groups, sizeof(groups) / sizeof(groups[0])); } for (x = 0; x < num_groups; x++) { if (!strcmp(mygroup, groups[x])) { igrp = 1; break; } } } if (!igrp) { ast_channel_unlock(peer); continue; } strcpy(peer_name, "spy-"); strncat(peer_name, peer->name, AST_NAME_STRLEN - 4 - 1); ptr = strchr(peer_name, '/'); *ptr++ = '\0'; for (s = peer_name; s < ptr; s++) *s = tolower(*s); /* We have to unlock the peer channel here to avoid a deadlock. * So, when we need to dereference it again, we have to lock the * datastore and get the pointer from there to see if the channel * is still valid. */ ast_channel_unlock(peer); if (!ast_test_flag(flags, OPTION_QUIET)) { if (ast_fileexists(peer_name, NULL, NULL) != -1) { res = ast_streamfile(chan, peer_name, chan->language); if (!res) res = ast_waitstream(chan, ""); if (res) { chanspy_ds_free(peer_chanspy_ds); break; } } else res = ast_say_character_str(chan, peer_name, "", chan->language); if ((num = atoi(ptr))) ast_say_digits(chan, atoi(ptr), "", chan->language); } res = channel_spy(chan, peer_chanspy_ds, &volfactor, fd, flags); num_spyed_upon++; if (res == -1) { chanspy_ds_free(peer_chanspy_ds); break; } else if (res > 1 && spec) { struct ast_channel *next; snprintf(nameprefix, AST_NAME_STRLEN, "%s/%d", spec, res); if ((next = ast_get_channel_by_name_prefix_locked(nameprefix, strlen(nameprefix)))) { peer_chanspy_ds = chanspy_ds_free(peer_chanspy_ds); next_chanspy_ds = setup_chanspy_ds(next, &chanspy_ds); } else { /* stay on this channel, if it is still valid */ ast_mutex_lock(&peer_chanspy_ds->lock); if (peer_chanspy_ds->chan) { ast_channel_lock(peer_chanspy_ds->chan); next_chanspy_ds = peer_chanspy_ds; peer_chanspy_ds = NULL; } else { /* the channel is gone */ ast_mutex_unlock(&peer_chanspy_ds->lock); next_chanspy_ds = NULL; } } peer = NULL; } } if (res == -1 || ast_check_hangup(chan)) break; } ast_clear_flag(chan, AST_FLAG_SPYING); ast_channel_setoption(chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0); ast_mutex_lock(&chanspy_ds.lock); ast_mutex_unlock(&chanspy_ds.lock); ast_mutex_destroy(&chanspy_ds.lock); return res; }
static int testclient_exec(struct ast_channel *chan, const char *data) { int res = 0; const char *testid=data; char fn[80]; char serverver[80]; FILE *f; /* Check for test id */ if (ast_strlen_zero(testid)) { ast_log(LOG_WARNING, "TestClient requires an argument - the test id\n"); return -1; } if (ast_channel_state(chan) != AST_STATE_UP) res = ast_answer(chan); /* Wait a few just to be sure things get started */ res = ast_safe_sleep(chan, 3000); /* Transmit client version */ if (!res) res = ast_dtmf_stream(chan, NULL, "8378*1#", 0, 0); ast_debug(1, "Transmit client version\n"); /* Read server version */ ast_debug(1, "Read server version\n"); if (!res) res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0); if (res > 0) res = 0; ast_debug(1, "server version: %s\n", serverver); if (res > 0) res = 0; if (!res) res = ast_safe_sleep(chan, 1000); /* Send test id */ if (!res) res = ast_dtmf_stream(chan, NULL, testid, 0, 0); if (!res) res = ast_dtmf_stream(chan, NULL, "#", 0, 0); ast_debug(1, "send test identifier: %s\n", testid); if ((res >=0) && (!ast_strlen_zero(testid))) { /* Make the directory to hold the test results in case it's not there */ snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR); ast_mkdir(fn, 0777); snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid); if ((f = fopen(fn, "w+"))) { setlinebuf(f); fprintf(f, "CLIENTCHAN: %s\n", ast_channel_name(chan)); fprintf(f, "CLIENTTEST ID: %s\n", testid); fprintf(f, "ANSWER: PASS\n"); res = 0; if (!res) { /* Step 1: Wait for "1" */ ast_debug(1, "TestClient: 2. Wait DTMF 1\n"); res = ast_waitfordigit(chan, 3000); fprintf(f, "WAIT DTMF 1: %s\n", (res != '1') ? "FAIL" : "PASS"); if (res == '1') res = 0; else res = -1; } if (!res) { res = ast_safe_sleep(chan, 1000); } if (!res) { /* Step 2: Send "2" */ ast_debug(1, "TestClient: 2. Send DTMF 2\n"); res = ast_dtmf_stream(chan, NULL, "2", 0, 0); fprintf(f, "SEND DTMF 2: %s\n", (res < 0) ? "FAIL" : "PASS"); if (res > 0) res = 0; } if (!res) { /* Step 3: Wait one second */ ast_debug(1, "TestClient: 3. Wait one second\n"); res = ast_safe_sleep(chan, 1000); fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS"); if (res > 0) res = 0; } if (!res) { /* Step 4: Measure noise */ ast_debug(1, "TestClient: 4. Measure noise\n"); res = measurenoise(chan, 5000, "TestClient"); fprintf(f, "MEASURENOISE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res); if (res > 0) res = 0; } if (!res) { /* Step 5: Wait for "4" */ ast_debug(1, "TestClient: 5. Wait DTMF 4\n"); res = ast_waitfordigit(chan, 3000); fprintf(f, "WAIT DTMF 4: %s\n", (res != '4') ? "FAIL" : "PASS"); if (res == '4') res = 0; else res = -1; } if (!res) { /* Step 6: Transmit tone noise */ ast_debug(1, "TestClient: 6. Transmit tone\n"); res = sendnoise(chan, 6000); fprintf(f, "SENDTONE: %s\n", (res < 0) ? "FAIL" : "PASS"); } if (!res || (res == '5')) { /* Step 7: Wait for "5" */ ast_debug(1, "TestClient: 7. Wait DTMF 5\n"); if (!res) res = ast_waitfordigit(chan, 3000); fprintf(f, "WAIT DTMF 5: %s\n", (res != '5') ? "FAIL" : "PASS"); if (res == '5') res = 0; else res = -1; } if (!res) { /* Step 8: Wait one second */ ast_debug(1, "TestClient: 8. Wait one second\n"); res = ast_safe_sleep(chan, 1000); fprintf(f, "WAIT 1 SEC: %s\n", (res < 0) ? "FAIL" : "PASS"); if (res > 0) res = 0; } if (!res) { /* Step 9: Measure noise */ ast_debug(1, "TestClient: 9. Measure tone\n"); res = measurenoise(chan, 4000, "TestClient"); fprintf(f, "MEASURETONE: %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res); if (res > 0) res = 0; } if (!res) { /* Step 10: Send "7" */ ast_debug(1, "TestClient: 10. Send DTMF 7\n"); res = ast_dtmf_stream(chan, NULL, "7", 0, 0); fprintf(f, "SEND DTMF 7: %s\n", (res < 0) ? "FAIL" : "PASS"); if (res > 0) res =0; } if (!res) { /* Step 11: Wait for "8" */ ast_debug(1, "TestClient: 11. Wait DTMF 8\n"); res = ast_waitfordigit(chan, 3000); fprintf(f, "WAIT DTMF 8: %s\n", (res != '8') ? "FAIL" : "PASS"); if (res == '8') res = 0; else res = -1; } if (!res) { res = ast_safe_sleep(chan, 1000); } if (!res) { /* Step 12: Hangup! */ ast_debug(1, "TestClient: 12. Hangup\n"); } ast_debug(1, "-- TEST COMPLETE--\n"); fprintf(f, "-- END TEST--\n"); fclose(f); res = -1; } else res = -1; } else { ast_log(LOG_NOTICE, "Did not read a test ID on '%s'\n", ast_channel_name(chan)); res = -1; } return res; }
static int cpeid_exec(struct ast_channel *chan, void *idata) { int res=0; struct localuser *u; unsigned char cpeid[4]; int gotgeometry = 0; int gotcpeid = 0; int width, height, buttons; char data[4][80]; char *stuff[4]; LOCAL_USER_ADD(u); stuff[0] = data[0]; stuff[1] = data[1]; stuff[2] = data[2]; stuff[3] = data[3]; memset(data, 0, sizeof(data)); strncpy(stuff[0], "** CPE Info **", sizeof(data[0]) - 1); strncpy(stuff[1], "Identifying CPE...", sizeof(data[1]) - 1); strncpy(stuff[2], "Please wait...", sizeof(data[2]) - 1); res = adsi_load_session(chan, NULL, 0, 1); if (res > 0) { cpeid_setstatus(chan, stuff, 0); res = adsi_get_cpeid(chan, cpeid, 0); if (res > 0) { gotcpeid = 1; if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "Got CPEID of '%02x:%02x:%02x:%02x' on '%s'\n", cpeid[0], cpeid[1], cpeid[2], cpeid[3], chan->name); } if (res > -1) { strncpy(stuff[1], "Measuring CPE...", sizeof(data[1]) - 1); strncpy(stuff[2], "Please wait...", sizeof(data[2]) - 1); cpeid_setstatus(chan, stuff, 0); res = adsi_get_cpeinfo(chan, &width, &height, &buttons, 0); if (res > -1) { if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "CPE has %d lines, %d columns, and %d buttons on '%s'\n", height, width, buttons, chan->name); gotgeometry = 1; } } if (res > -1) { if (gotcpeid) snprintf(stuff[1], sizeof(data[1]), "CPEID: %02x:%02x:%02x:%02x", cpeid[0], cpeid[1], cpeid[2], cpeid[3]); else strncpy(stuff[1], "CPEID Unknown", sizeof(data[1]) - 1); if (gotgeometry) snprintf(stuff[2], sizeof(data[2]), "Geom: %dx%d, %d buttons", width, height, buttons); else strncpy(stuff[2], "Geometry unknown", sizeof(data[2]) - 1); strncpy(stuff[3], "Press # to exit", sizeof(data[3]) - 1); cpeid_setstatus(chan, stuff, 1); for(;;) { res = ast_waitfordigit(chan, 1000); if (res < 0) break; if (res == '#') { res = 0; break; } } adsi_unload_session(chan); } } LOCAL_USER_REMOVE(u); return res; }
static int common_exec(struct ast_channel *chan, struct ast_flags *flags, int volfactor, const int fd, struct spy_dtmf_options *user_options, const char *mygroup, const char *myenforced, const char *spec, const char *exten, const char *context, const char *mailbox, const char *name_context) { char nameprefix[AST_NAME_STRLEN]; char peer_name[AST_NAME_STRLEN + 5]; char exitcontext[AST_MAX_CONTEXT] = ""; signed char zero_volume = 0; int waitms; int res; char *ptr; int num; int num_spyed_upon = 1; struct ast_channel_iterator *iter = NULL; if (ast_test_flag(flags, OPTION_EXIT)) { const char *c; ast_channel_lock(chan); if ((c = pbx_builtin_getvar_helper(chan, "SPY_EXIT_CONTEXT"))) { ast_copy_string(exitcontext, c, sizeof(exitcontext)); } else if (!ast_strlen_zero(chan->macrocontext)) { ast_copy_string(exitcontext, chan->macrocontext, sizeof(exitcontext)); } else { ast_copy_string(exitcontext, chan->context, sizeof(exitcontext)); } ast_channel_unlock(chan); } if (chan->_state != AST_STATE_UP) ast_answer(chan); ast_set_flag(chan, AST_FLAG_SPYING); /* so nobody can spy on us while we are spying */ waitms = 100; for (;;) { struct ast_autochan *autochan = NULL, *next_autochan = NULL; struct ast_channel *prev = NULL; if (!ast_test_flag(flags, OPTION_QUIET) && num_spyed_upon) { res = ast_streamfile(chan, "beep", chan->language); if (!res) res = ast_waitstream(chan, ""); else if (res < 0) { ast_clear_flag(chan, AST_FLAG_SPYING); break; } if (!ast_strlen_zero(exitcontext)) { char tmp[2]; tmp[0] = res; tmp[1] = '\0'; if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) goto exit; else ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext); } } /* Set up the iterator we'll be using during this call */ if (!ast_strlen_zero(spec)) { iter = ast_channel_iterator_by_name_new(spec, strlen(spec)); } else if (!ast_strlen_zero(exten)) { iter = ast_channel_iterator_by_exten_new(exten, context); } else { iter = ast_channel_iterator_all_new(); } if (!iter) { return -1; } res = ast_waitfordigit(chan, waitms); if (res < 0) { ast_clear_flag(chan, AST_FLAG_SPYING); break; } if (!ast_strlen_zero(exitcontext)) { char tmp[2]; tmp[0] = res; tmp[1] = '\0'; if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) goto exit; else ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext); } /* reset for the next loop around, unless overridden later */ waitms = 100; num_spyed_upon = 0; for (autochan = next_channel(iter, autochan, chan); autochan; prev = autochan->chan, ast_autochan_destroy(autochan), autochan = next_autochan ? next_autochan : next_channel(iter, autochan, chan), next_autochan = NULL) { int igrp = !mygroup; int ienf = !myenforced; char *s; if (autochan->chan == prev) { ast_autochan_destroy(autochan); break; } if (ast_check_hangup(chan)) { ast_autochan_destroy(autochan); break; } if (ast_test_flag(flags, OPTION_BRIDGED) && !ast_bridged_channel(autochan->chan)) { continue; } if (ast_check_hangup(autochan->chan) || ast_test_flag(autochan->chan, AST_FLAG_SPYING)) { continue; } if (mygroup) { int num_groups = 0; int num_mygroups = 0; char dup_group[512]; char dup_mygroup[512]; char *groups[NUM_SPYGROUPS]; char *mygroups[NUM_SPYGROUPS]; const char *group = NULL; int x; int y; ast_copy_string(dup_mygroup, mygroup, sizeof(dup_mygroup)); num_mygroups = ast_app_separate_args(dup_mygroup, ':', mygroups, ARRAY_LEN(mygroups)); /* Before dahdi scan was part of chanspy, it would use the "GROUP" variable * rather than "SPYGROUP", this check is done to preserve expected behavior */ if (ast_test_flag(flags, OPTION_DAHDI_SCAN)) { group = pbx_builtin_getvar_helper(autochan->chan, "GROUP"); } else { group = pbx_builtin_getvar_helper(autochan->chan, "SPYGROUP"); } if (!ast_strlen_zero(group)) { ast_copy_string(dup_group, group, sizeof(dup_group)); num_groups = ast_app_separate_args(dup_group, ':', groups, ARRAY_LEN(groups)); } for (y = 0; y < num_mygroups; y++) { for (x = 0; x < num_groups; x++) { if (!strcmp(mygroups[y], groups[x])) { igrp = 1; break; } } } } if (!igrp) { continue; } if (myenforced) { char ext[AST_CHANNEL_NAME + 3]; char buffer[512]; char *end; snprintf(buffer, sizeof(buffer) - 1, ":%s:", myenforced); ast_copy_string(ext + 1, autochan->chan->name, sizeof(ext) - 1); if ((end = strchr(ext, '-'))) { *end++ = ':'; *end = '\0'; } ext[0] = ':'; if (strcasestr(buffer, ext)) { ienf = 1; } } if (!ienf) { continue; } strcpy(peer_name, "spy-"); strncat(peer_name, autochan->chan->name, AST_NAME_STRLEN - 4 - 1); ptr = strchr(peer_name, '/'); *ptr++ = '\0'; ptr = strsep(&ptr, "-"); for (s = peer_name; s < ptr; s++) *s = tolower(*s); if (!ast_test_flag(flags, OPTION_QUIET)) { if (ast_test_flag(flags, OPTION_NAME)) { const char *local_context = S_OR(name_context, "default"); const char *local_mailbox = S_OR(mailbox, ptr); res = ast_app_sayname(chan, local_mailbox, local_context); } if (!ast_test_flag(flags, OPTION_NAME) || res < 0) { if (!ast_test_flag(flags, OPTION_NOTECH)) { if (ast_fileexists(peer_name, NULL, NULL) > 0) { res = ast_streamfile(chan, peer_name, chan->language); if (!res) { res = ast_waitstream(chan, ""); } if (res) { ast_autochan_destroy(autochan); break; } } else { res = ast_say_character_str(chan, peer_name, "", chan->language); } } if ((num = atoi(ptr))) ast_say_digits(chan, atoi(ptr), "", chan->language); } } res = channel_spy(chan, autochan, &volfactor, fd, user_options, flags, exitcontext); num_spyed_upon++; if (res == -1) { ast_autochan_destroy(autochan); goto exit; } else if (res == -2) { res = 0; ast_autochan_destroy(autochan); goto exit; } else if (res > 1 && spec) { struct ast_channel *next; snprintf(nameprefix, AST_NAME_STRLEN, "%s/%d", spec, res); if ((next = ast_channel_get_by_name_prefix(nameprefix, strlen(nameprefix)))) { next_autochan = ast_autochan_setup(next); next = ast_channel_unref(next); } else { /* stay on this channel, if it is still valid */ if (!ast_check_hangup(autochan->chan)) { next_autochan = ast_autochan_setup(autochan->chan); } else { /* the channel is gone */ next_autochan = NULL; } } } else if (res == 0 && ast_test_flag(flags, OPTION_EXITONHANGUP)) { goto exit; } } iter = ast_channel_iterator_destroy(iter); if (res == -1 || ast_check_hangup(chan)) break; if (ast_test_flag(flags, OPTION_STOP) && !next_autochan) { break; } } exit: ast_clear_flag(chan, AST_FLAG_SPYING); ast_channel_setoption(chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0); return res; }
static int _while_exec(struct ast_channel *chan, void *data, int end) { int res=0; struct localuser *u; char *while_pri = NULL; char *goto_str = NULL, *my_name = NULL; char *condition = NULL, *label = NULL; char varname[VAR_SIZE], end_varname[VAR_SIZE]; const char *prefix = "WHILE"; size_t size=0; int used_index_i = -1, x=0; char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0"; if (!chan) { /* huh ? */ return -1; } LOCAL_USER_ADD(u); /* dont want run away loops if the chan isn't even up this is up for debate since it slows things down a tad ...... */ if (ast_waitfordigit(chan,1) < 0) ALL_DONE(u,-1); for (x=0;;x++) { if (get_index(chan, prefix, x)) { used_index_i = x; } else break; } snprintf(used_index, VAR_SIZE, "%d", used_index_i); snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1); if (!end) { condition = ast_strdupa((char *) data); } size = strlen(chan->context) + strlen(chan->exten) + 32; my_name = alloca(size); memset(my_name, 0, size); snprintf(my_name, size, "%s_%s_%d", chan->context, chan->exten, chan->priority); if (ast_strlen_zero(label)) { if (end) label = used_index; else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) { label = new_index; pbx_builtin_setvar_helper(chan, my_name, label); } } snprintf(varname, VAR_SIZE, "%s_%s", prefix, label); while_pri = pbx_builtin_getvar_helper(chan, varname); if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) { snprintf(end_varname,VAR_SIZE,"END_%s",varname); } if (!end && !pbx_checkcondition(condition)) { /* Condition Met (clean up helper vars) */ pbx_builtin_setvar_helper(chan, varname, NULL); pbx_builtin_setvar_helper(chan, my_name, NULL); snprintf(end_varname,VAR_SIZE,"END_%s",varname); if ((goto_str=pbx_builtin_getvar_helper(chan, end_varname))) { ast_parseable_goto(chan, goto_str); pbx_builtin_setvar_helper(chan, end_varname, NULL); } else { int pri = find_matching_endwhile(chan); if (pri > 0) { if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "Jumping to priority %d\n", pri); chan->priority = pri; } else { ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan->context, chan->exten, chan->priority); } } ALL_DONE(u,res); } if (!end && !while_pri) { size = strlen(chan->context) + strlen(chan->exten) + 32; goto_str = alloca(size); memset(goto_str, 0, size); snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority); pbx_builtin_setvar_helper(chan, varname, goto_str); } else if (end && while_pri) { /* END of loop */ snprintf(end_varname, VAR_SIZE, "END_%s", varname); if (! pbx_builtin_getvar_helper(chan, end_varname)) { size = strlen(chan->context) + strlen(chan->exten) + 32; goto_str = alloca(size); memset(goto_str, 0, size); snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority+1); pbx_builtin_setvar_helper(chan, end_varname, goto_str); } ast_parseable_goto(chan, while_pri); } ALL_DONE(u, res); }