00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00029
00030 #include <sys/types.h>
00031 #include <netdb.h>
00032 #include <sys/socket.h>
00033 #include <netinet/in.h>
00034 #include <netinet/tcp.h>
00035 #include <arpa/inet.h>
00036 #include <math.h>
00037 #include <stdlib.h>
00038 #include <unistd.h>
00039 #include <string.h>
00040 #include <stdlib.h>
00041 #include <signal.h>
00042 #include <sys/time.h>
00043 #include <stdio.h>
00044 #include <fcntl.h>
00045 #include <errno.h>
00046 #include <sys/stat.h>
00047 #include <sys/wait.h>
00048
00049 #include "asterisk/file.h"
00050 #include "asterisk/logger.h"
00051 #include "asterisk/channel.h"
00052 #include "asterisk/pbx.h"
00053 #include "asterisk/module.h"
00054 #include "asterisk/astdb.h"
00055 #include "asterisk/callerid.h"
00056 #include "asterisk/cli.h"
00057 #include "asterisk/logger.h"
00058 #include "asterisk/options.h"
00059 #include "asterisk/image.h"
00060 #include "asterisk/say.h"
00061 #include "asterisk/app.h"
00062 #include "asterisk/dsp.h"
00063 #include "asterisk/musiconhold.h"
00064 #include "asterisk/utils.h"
00065 #include "asterisk/lock.h"
00066 #include "asterisk/strings.h"
00067 #include "asterisk/agi.h"
00068 #include "asterisk/features.h"
00069
00070 #define MAX_ARGS 128
00071 #define MAX_COMMANDS 128
00072 #define AGI_NANDFS_RETRY 3
00073 #define AGI_BUF_LEN 2048
00074
00075
00076 #define fdprintf agi_debug_cli
00077
00078 static char *app = "AGI";
00079
00080 static char *eapp = "EAGI";
00081
00082 static char *deadapp = "DeadAGI";
00083
00084 static char *synopsis = "Executes an AGI compliant application";
00085 static char *esynopsis = "Executes an EAGI compliant application";
00086 static char *deadsynopsis = "Executes AGI on a hungup channel";
00087
00088 static char *descrip =
00089 " [E|Dead]AGI(command|args): Executes an Asterisk Gateway Interface compliant\n"
00090 "program on a channel. AGI allows Asterisk to launch external programs\n"
00091 "written in any language to control a telephony channel, play audio,\n"
00092 "read DTMF digits, etc. by communicating with the AGI protocol on stdin\n"
00093 "and stdout.\n"
00094 " This channel will stop dialplan execution on hangup inside of this\n"
00095 "application, except when using DeadAGI. Otherwise, dialplan execution\n"
00096 "will continue normally.\n"
00097 " A locally executed AGI script will receive SIGHUP on hangup from the channel\n"
00098 "except when using DeadAGI. This can be disabled by setting the AGISIGHUP channel\n"
00099 "variable to \"no\" before executing the AGI application.\n"
00100 " Using 'EAGI' provides enhanced AGI, with incoming audio available out of band\n"
00101 "on file descriptor 3\n\n"
00102 " Use the CLI command 'agi show' to list available agi commands\n"
00103 " This application sets the following channel variable upon completion:\n"
00104 " AGISTATUS The status of the attempt to the run the AGI script\n"
00105 " text string, one of SUCCESS | FAILURE | HANGUP\n";
00106
00107 static int agidebug = 0;
00108
00109 #define TONE_BLOCK_SIZE 200
00110
00111
00112 #define MAX_AGI_CONNECT 2000
00113
00114 #define AGI_PORT 4573
00115
00116 enum agi_result {
00117 AGI_RESULT_FAILURE = -1,
00118 AGI_RESULT_SUCCESS,
00119 AGI_RESULT_SUCCESS_FAST,
00120 AGI_RESULT_HANGUP
00121 };
00122
00123 static int __attribute__((format(printf, 2, 3))) agi_debug_cli(int fd, char *fmt, ...)
00124 {
00125 char *stuff;
00126 int res = 0;
00127
00128 va_list ap;
00129 va_start(ap, fmt);
00130 res = vasprintf(&stuff, fmt, ap);
00131 va_end(ap);
00132 if (res == -1) {
00133 ast_log(LOG_ERROR, "Out of memory\n");
00134 } else {
00135 if (agidebug)
00136 ast_verbose("AGI Tx >> %s", stuff);
00137 res = ast_carefulwrite(fd, stuff, strlen(stuff), 100);
00138 free(stuff);
00139 }
00140
00141 return res;
00142 }
00143
00144
00145
00146 static enum agi_result launch_netscript(char *agiurl, char *argv[], int *fds, int *efd, int *opid)
00147 {
00148 int s;
00149 int flags;
00150 struct pollfd pfds[1];
00151 char *host;
00152 char *c; int port = AGI_PORT;
00153 char *script="";
00154 struct sockaddr_in sin;
00155 struct hostent *hp;
00156 struct ast_hostent ahp;
00157 int res;
00158
00159
00160 host = ast_strdupa(agiurl + 6);
00161
00162 if ((c = strchr(host, '/'))) {
00163 *c = '\0';
00164 c++;
00165 script = c;
00166 }
00167 if ((c = strchr(host, ':'))) {
00168 *c = '\0';
00169 c++;
00170 port = atoi(c);
00171 }
00172 if (efd) {
00173 ast_log(LOG_WARNING, "AGI URI's don't support Enhanced AGI yet\n");
00174 return -1;
00175 }
00176 hp = ast_gethostbyname(host, &ahp);
00177 if (!hp) {
00178 ast_log(LOG_WARNING, "Unable to locate host '%s'\n", host);
00179 return -1;
00180 }
00181 s = socket(AF_INET, SOCK_STREAM, 0);
00182 if (s < 0) {
00183 ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
00184 return -1;
00185 }
00186 flags = fcntl(s, F_GETFL);
00187 if (flags < 0) {
00188 ast_log(LOG_WARNING, "Fcntl(F_GETFL) failed: %s\n", strerror(errno));
00189 close(s);
00190 return -1;
00191 }
00192 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) {
00193 ast_log(LOG_WARNING, "Fnctl(F_SETFL) failed: %s\n", strerror(errno));
00194 close(s);
00195 return -1;
00196 }
00197 memset(&sin, 0, sizeof(sin));
00198 sin.sin_family = AF_INET;
00199 sin.sin_port = htons(port);
00200 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
00201 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) && (errno != EINPROGRESS)) {
00202 ast_log(LOG_WARNING, "Connect failed with unexpected error: %s\n", strerror(errno));
00203 close(s);
00204 return AGI_RESULT_FAILURE;
00205 }
00206
00207 pfds[0].fd = s;
00208 pfds[0].events = POLLOUT;
00209 while ((res = poll(pfds, 1, MAX_AGI_CONNECT)) != 1) {
00210 if (errno != EINTR) {
00211 if (!res) {
00212 ast_log(LOG_WARNING, "FastAGI connection to '%s' timed out after MAX_AGI_CONNECT (%d) milliseconds.\n",
00213 agiurl, MAX_AGI_CONNECT);
00214 } else
00215 ast_log(LOG_WARNING, "Connect to '%s' failed: %s\n", agiurl, strerror(errno));
00216 close(s);
00217 return AGI_RESULT_FAILURE;
00218 }
00219 }
00220
00221 if (fdprintf(s, "agi_network: yes\n") < 0) {
00222 if (errno != EINTR) {
00223 ast_log(LOG_WARNING, "Connect to '%s' failed: %s\n", agiurl, strerror(errno));
00224 close(s);
00225 return AGI_RESULT_FAILURE;
00226 }
00227 }
00228
00229
00230
00231 if (!ast_strlen_zero(script))
00232 fdprintf(s, "agi_network_script: %s\n", script);
00233
00234 if (option_debug > 3)
00235 ast_log(LOG_DEBUG, "Wow, connected!\n");
00236 fds[0] = s;
00237 fds[1] = s;
00238 *opid = -1;
00239 return AGI_RESULT_SUCCESS_FAST;
00240 }
00241
00242 static enum agi_result launch_script(char *script, char *argv[], int *fds, int *efd, int *opid)
00243 {
00244 char tmp[256];
00245 int pid;
00246 int toast[2];
00247 int fromast[2];
00248 int audio[2];
00249 int x;
00250 int res;
00251 sigset_t signal_set, old_set;
00252 struct stat statbuf;
00253
00254 if (!strncasecmp(script, "agi://", 6))
00255 return launch_netscript(script, argv, fds, efd, opid);
00256
00257 if (script[0] != '/') {
00258 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_AGI_DIR, script);
00259 script = tmp;
00260 }
00261 if (stat(script, &statbuf) < 0) {
00262 ast_log(LOG_WARNING, "Unable to execute %s: File does not exist\n", script);
00263 return -1;
00264 }
00265 if (!(statbuf.st_mode & S_IXUSR)) {
00266 ast_log(LOG_WARNING, "Unable to execute %s: File is not executable\n", script);
00267 return -1;
00268 }
00269 if (pipe(toast)) {
00270 ast_log(LOG_WARNING, "Unable to create toast pipe: %s\n",strerror(errno));
00271 return AGI_RESULT_FAILURE;
00272 }
00273 if (pipe(fromast)) {
00274 ast_log(LOG_WARNING, "unable to create fromast pipe: %s\n", strerror(errno));
00275 close(toast[0]);
00276 close(toast[1]);
00277 return AGI_RESULT_FAILURE;
00278 }
00279 if (efd) {
00280 if (pipe(audio)) {
00281 ast_log(LOG_WARNING, "unable to create audio pipe: %s\n", strerror(errno));
00282 close(fromast[0]);
00283 close(fromast[1]);
00284 close(toast[0]);
00285 close(toast[1]);
00286 return AGI_RESULT_FAILURE;
00287 }
00288 res = fcntl(audio[1], F_GETFL);
00289 if (res > -1)
00290 res = fcntl(audio[1], F_SETFL, res | O_NONBLOCK);
00291 if (res < 0) {
00292 ast_log(LOG_WARNING, "unable to set audio pipe parameters: %s\n", strerror(errno));
00293 close(fromast[0]);
00294 close(fromast[1]);
00295 close(toast[0]);
00296 close(toast[1]);
00297 close(audio[0]);
00298 close(audio[1]);
00299 return AGI_RESULT_FAILURE;
00300 }
00301 }
00302
00303
00304 sigfillset(&signal_set);
00305 pthread_sigmask(SIG_BLOCK, &signal_set, &old_set);
00306 pid = fork();
00307 if (pid < 0) {
00308 ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno));
00309 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
00310 return AGI_RESULT_FAILURE;
00311 }
00312 if (!pid) {
00313
00314 setenv("AST_CONFIG_DIR", ast_config_AST_CONFIG_DIR, 1);
00315 setenv("AST_CONFIG_FILE", ast_config_AST_CONFIG_FILE, 1);
00316 setenv("AST_MODULE_DIR", ast_config_AST_MODULE_DIR, 1);
00317 setenv("AST_SPOOL_DIR", ast_config_AST_SPOOL_DIR, 1);
00318 setenv("AST_MONITOR_DIR", ast_config_AST_MONITOR_DIR, 1);
00319 setenv("AST_VAR_DIR", ast_config_AST_VAR_DIR, 1);
00320 setenv("AST_DATA_DIR", ast_config_AST_DATA_DIR, 1);
00321 setenv("AST_LOG_DIR", ast_config_AST_LOG_DIR, 1);
00322 setenv("AST_AGI_DIR", ast_config_AST_AGI_DIR, 1);
00323 setenv("AST_KEY_DIR", ast_config_AST_KEY_DIR, 1);
00324 setenv("AST_RUN_DIR", ast_config_AST_RUN_DIR, 1);
00325
00326
00327 ast_set_priority(0);
00328
00329
00330 dup2(fromast[0], STDIN_FILENO);
00331 dup2(toast[1], STDOUT_FILENO);
00332 if (efd) {
00333 dup2(audio[0], STDERR_FILENO + 1);
00334 } else {
00335 close(STDERR_FILENO + 1);
00336 }
00337
00338
00339 signal(SIGHUP, SIG_DFL);
00340 signal(SIGCHLD, SIG_DFL);
00341 signal(SIGINT, SIG_DFL);
00342 signal(SIGURG, SIG_DFL);
00343 signal(SIGTERM, SIG_DFL);
00344 signal(SIGPIPE, SIG_DFL);
00345 signal(SIGXFSZ, SIG_DFL);
00346
00347
00348 if (pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL)) {
00349 ast_log(LOG_WARNING, "unable to unblock signals for AGI script: %s\n", strerror(errno));
00350 _exit(1);
00351 }
00352
00353
00354 for (x=STDERR_FILENO + 2;x<1024;x++)
00355 close(x);
00356
00357
00358
00359 execv(script, argv);
00360
00361 fprintf(stdout, "verbose \"Failed to execute '%s': %s\" 2\n", script, strerror(errno));
00362
00363 fprintf(stdout, "failure\n");
00364 fflush(stdout);
00365 _exit(1);
00366 }
00367 pthread_sigmask(SIG_SETMASK, &old_set, NULL);
00368 if (option_verbose > 2)
00369 ast_verbose(VERBOSE_PREFIX_3 "Launched AGI Script %s\n", script);
00370 fds[0] = toast[0];
00371 fds[1] = fromast[1];
00372 if (efd) {
00373 *efd = audio[1];
00374 }
00375
00376 close(toast[1]);
00377 close(fromast[0]);
00378
00379 if (efd)
00380 close(audio[0]);
00381
00382 *opid = pid;
00383 return AGI_RESULT_SUCCESS;
00384 }
00385
00386 static void setup_env(struct ast_channel *chan, char *request, int fd, int enhanced, int argc, char *argv[])
00387 {
00388 int count;
00389
00390
00391
00392 fdprintf(fd, "agi_request: %s\n", request);
00393 fdprintf(fd, "agi_channel: %s\n", chan->name);
00394 fdprintf(fd, "agi_language: %s\n", chan->language);
00395 fdprintf(fd, "agi_type: %s\n", chan->tech->type);
00396 fdprintf(fd, "agi_uniqueid: %s\n", chan->uniqueid);
00397
00398
00399 fdprintf(fd, "agi_callerid: %s\n", S_OR(chan->cid.cid_num, "unknown"));
00400 fdprintf(fd, "agi_calleridname: %s\n", S_OR(chan->cid.cid_name, "unknown"));
00401 fdprintf(fd, "agi_callingpres: %d\n", chan->cid.cid_pres);
00402 fdprintf(fd, "agi_callingani2: %d\n", chan->cid.cid_ani2);
00403 fdprintf(fd, "agi_callington: %d\n", chan->cid.cid_ton);
00404 fdprintf(fd, "agi_callingtns: %d\n", chan->cid.cid_tns);
00405 fdprintf(fd, "agi_dnid: %s\n", S_OR(chan->cid.cid_dnid, "unknown"));
00406 fdprintf(fd, "agi_rdnis: %s\n", S_OR(chan->cid.cid_rdnis, "unknown"));
00407
00408
00409 fdprintf(fd, "agi_context: %s\n", chan->context);
00410 fdprintf(fd, "agi_extension: %s\n", chan->exten);
00411 fdprintf(fd, "agi_priority: %d\n", chan->priority);
00412 fdprintf(fd, "agi_enhanced: %s\n", enhanced ? "1.0" : "0.0");
00413
00414
00415 fdprintf(fd, "agi_accountcode: %s\n", chan->accountcode ? chan->accountcode : "");
00416
00417
00418
00419 for(count = 1; count < argc; count++)
00420 fdprintf(fd, "agi_arg_%d: %s\n", count, argv[count]);
00421
00422
00423 fdprintf(fd, "\n");
00424 }
00425
00426 static int handle_answer(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00427 {
00428 int res;
00429 res = 0;
00430 if (chan->_state != AST_STATE_UP) {
00431
00432 res = ast_answer(chan);
00433 }
00434 fdprintf(agi->fd, "200 result=%d\n", res);
00435 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00436 }
00437
00438 static int handle_waitfordigit(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00439 {
00440 int res;
00441 int to;
00442 if (argc != 4)
00443 return RESULT_SHOWUSAGE;
00444 if (sscanf(argv[3], "%d", &to) != 1)
00445 return RESULT_SHOWUSAGE;
00446 res = ast_waitfordigit_full(chan, to, agi->audio, agi->ctrl);
00447 fdprintf(agi->fd, "200 result=%d\n", res);
00448 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00449 }
00450
00451 static int handle_sendtext(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00452 {
00453 int res;
00454 if (argc != 3)
00455 return RESULT_SHOWUSAGE;
00456
00457
00458
00459
00460
00461
00462
00463 res = ast_sendtext(chan, argv[2]);
00464 fdprintf(agi->fd, "200 result=%d\n", res);
00465 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00466 }
00467
00468 static int handle_recvchar(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00469 {
00470 int res;
00471 if (argc != 3)
00472 return RESULT_SHOWUSAGE;
00473 res = ast_recvchar(chan,atoi(argv[2]));
00474 if (res == 0) {
00475 fdprintf(agi->fd, "200 result=%d (timeout)\n", res);
00476 return RESULT_SUCCESS;
00477 }
00478 if (res > 0) {
00479 fdprintf(agi->fd, "200 result=%d\n", res);
00480 return RESULT_SUCCESS;
00481 }
00482 else {
00483 fdprintf(agi->fd, "200 result=%d (hangup)\n", res);
00484 return RESULT_FAILURE;
00485 }
00486 }
00487
00488 static int handle_recvtext(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00489 {
00490 char *buf;
00491
00492 if (argc != 3)
00493 return RESULT_SHOWUSAGE;
00494 buf = ast_recvtext(chan,atoi(argv[2]));
00495 if (buf) {
00496 fdprintf(agi->fd, "200 result=1 (%s)\n", buf);
00497 free(buf);
00498 } else {
00499 fdprintf(agi->fd, "200 result=-1\n");
00500 }
00501 return RESULT_SUCCESS;
00502 }
00503
00504 static int handle_tddmode(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00505 {
00506 int res,x;
00507 if (argc != 3)
00508 return RESULT_SHOWUSAGE;
00509 if (!strncasecmp(argv[2],"on",2))
00510 x = 1;
00511 else
00512 x = 0;
00513 if (!strncasecmp(argv[2],"mate",4))
00514 x = 2;
00515 if (!strncasecmp(argv[2],"tdd",3))
00516 x = 1;
00517 res = ast_channel_setoption(chan, AST_OPTION_TDD, &x, sizeof(char), 0);
00518 if (res != RESULT_SUCCESS)
00519 fdprintf(agi->fd, "200 result=0\n");
00520 else
00521 fdprintf(agi->fd, "200 result=1\n");
00522 return RESULT_SUCCESS;
00523 }
00524
00525 static int handle_sendimage(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00526 {
00527 int res;
00528 if (argc != 3)
00529 return RESULT_SHOWUSAGE;
00530 res = ast_send_image(chan, argv[2]);
00531 if (!ast_check_hangup(chan))
00532 res = 0;
00533 fdprintf(agi->fd, "200 result=%d\n", res);
00534 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00535 }
00536
00537 static int handle_controlstreamfile(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00538 {
00539 int res = 0;
00540 int skipms = 3000;
00541 char *fwd = NULL;
00542 char *rev = NULL;
00543 char *pause = NULL;
00544 char *stop = NULL;
00545
00546 if (argc < 5 || argc > 9)
00547 return RESULT_SHOWUSAGE;
00548
00549 if (!ast_strlen_zero(argv[4]))
00550 stop = argv[4];
00551 else
00552 stop = NULL;
00553
00554 if ((argc > 5) && (sscanf(argv[5], "%d", &skipms) != 1))
00555 return RESULT_SHOWUSAGE;
00556
00557 if (argc > 6 && !ast_strlen_zero(argv[6]))
00558 fwd = argv[6];
00559 else
00560 fwd = "#";
00561
00562 if (argc > 7 && !ast_strlen_zero(argv[7]))
00563 rev = argv[7];
00564 else
00565 rev = "*";
00566
00567 if (argc > 8 && !ast_strlen_zero(argv[8]))
00568 pause = argv[8];
00569 else
00570 pause = NULL;
00571
00572 res = ast_control_streamfile(chan, argv[3], fwd, rev, stop, pause, NULL, skipms);
00573
00574 fdprintf(agi->fd, "200 result=%d\n", res);
00575
00576 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00577 }
00578
00579 static int handle_streamfile(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00580 {
00581 int res;
00582 int vres;
00583 struct ast_filestream *fs;
00584 struct ast_filestream *vfs;
00585 long sample_offset = 0;
00586 long max_length;
00587 char *edigits = "";
00588
00589 if (argc < 4 || argc > 5)
00590 return RESULT_SHOWUSAGE;
00591
00592 if (argv[3])
00593 edigits = argv[3];
00594
00595 if ((argc > 4) && (sscanf(argv[4], "%ld", &sample_offset) != 1))
00596 return RESULT_SHOWUSAGE;
00597
00598 fs = ast_openstream(chan, argv[2], chan->language);
00599
00600 if (!fs) {
00601 fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
00602 return RESULT_SUCCESS;
00603 }
00604 vfs = ast_openvstream(chan, argv[2], chan->language);
00605 if (vfs)
00606 ast_log(LOG_DEBUG, "Ooh, found a video stream, too\n");
00607
00608 if (option_verbose > 2)
00609 ast_verbose(VERBOSE_PREFIX_3 "Playing '%s' (escape_digits=%s) (sample_offset %ld)\n", argv[2], edigits, sample_offset);
00610
00611 ast_seekstream(fs, 0, SEEK_END);
00612 max_length = ast_tellstream(fs);
00613 ast_seekstream(fs, sample_offset, SEEK_SET);
00614 res = ast_applystream(chan, fs);
00615 if (vfs)
00616 vres = ast_applystream(chan, vfs);
00617 ast_playstream(fs);
00618 if (vfs)
00619 ast_playstream(vfs);
00620
00621 res = ast_waitstream_full(chan, argv[3], agi->audio, agi->ctrl);
00622
00623
00624 sample_offset = (chan->stream) ? ast_tellstream(fs) : max_length;
00625 ast_stopstream(chan);
00626 if (res == 1) {
00627
00628 return RESULT_SUCCESS;
00629 }
00630 fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
00631 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00632 }
00633
00634
00635 static int handle_getoption(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00636 {
00637 int res;
00638 int vres;
00639 struct ast_filestream *fs;
00640 struct ast_filestream *vfs;
00641 long sample_offset = 0;
00642 long max_length;
00643 int timeout = 0;
00644 char *edigits = "";
00645
00646 if ( argc < 4 || argc > 5 )
00647 return RESULT_SHOWUSAGE;
00648
00649 if ( argv[3] )
00650 edigits = argv[3];
00651
00652 if ( argc == 5 )
00653 timeout = atoi(argv[4]);
00654 else if (chan->pbx->dtimeout) {
00655
00656 timeout = chan->pbx->dtimeout * 1000;
00657 }
00658
00659 fs = ast_openstream(chan, argv[2], chan->language);
00660 if (!fs) {
00661 fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
00662 ast_log(LOG_WARNING, "Unable to open %s\n", argv[2]);
00663 return RESULT_SUCCESS;
00664 }
00665 vfs = ast_openvstream(chan, argv[2], chan->language);
00666 if (vfs)
00667 ast_log(LOG_DEBUG, "Ooh, found a video stream, too\n");
00668
00669 if (option_verbose > 2)
00670 ast_verbose(VERBOSE_PREFIX_3 "Playing '%s' (escape_digits=%s) (timeout %d)\n", argv[2], edigits, timeout);
00671
00672 ast_seekstream(fs, 0, SEEK_END);
00673 max_length = ast_tellstream(fs);
00674 ast_seekstream(fs, sample_offset, SEEK_SET);
00675 res = ast_applystream(chan, fs);
00676 if (vfs)
00677 vres = ast_applystream(chan, vfs);
00678 ast_playstream(fs);
00679 if (vfs)
00680 ast_playstream(vfs);
00681
00682 res = ast_waitstream_full(chan, argv[3], agi->audio, agi->ctrl);
00683
00684
00685 sample_offset = (chan->stream)?ast_tellstream(fs):max_length;
00686 ast_stopstream(chan);
00687 if (res == 1) {
00688
00689 return RESULT_SUCCESS;
00690 }
00691
00692
00693 if (res == 0 ) {
00694 res = ast_waitfordigit_full(chan, timeout, agi->audio, agi->ctrl);
00695
00696 if ( !strchr(edigits,res) )
00697 res=0;
00698 }
00699
00700 fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
00701 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00702 }
00703
00704
00705
00706
00707
00708
00709
00710 static int handle_saynumber(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00711 {
00712 int res;
00713 int num;
00714 if (argc != 4)
00715 return RESULT_SHOWUSAGE;
00716 if (sscanf(argv[2], "%d", &num) != 1)
00717 return RESULT_SHOWUSAGE;
00718 res = ast_say_number_full(chan, num, argv[3], chan->language, (char *) NULL, agi->audio, agi->ctrl);
00719 if (res == 1)
00720 return RESULT_SUCCESS;
00721 fdprintf(agi->fd, "200 result=%d\n", res);
00722 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00723 }
00724
00725 static int handle_saydigits(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00726 {
00727 int res;
00728 int num;
00729
00730 if (argc != 4)
00731 return RESULT_SHOWUSAGE;
00732 if (sscanf(argv[2], "%d", &num) != 1)
00733 return RESULT_SHOWUSAGE;
00734
00735 res = ast_say_digit_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
00736 if (res == 1)
00737 return RESULT_SUCCESS;
00738 fdprintf(agi->fd, "200 result=%d\n", res);
00739 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00740 }
00741
00742 static int handle_sayalpha(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00743 {
00744 int res;
00745
00746 if (argc != 4)
00747 return RESULT_SHOWUSAGE;
00748
00749 res = ast_say_character_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
00750 if (res == 1)
00751 return RESULT_SUCCESS;
00752 fdprintf(agi->fd, "200 result=%d\n", res);
00753 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00754 }
00755
00756 static int handle_saydate(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00757 {
00758 int res;
00759 int num;
00760 if (argc != 4)
00761 return RESULT_SHOWUSAGE;
00762 if (sscanf(argv[2], "%d", &num) != 1)
00763 return RESULT_SHOWUSAGE;
00764 res = ast_say_date(chan, num, argv[3], chan->language);
00765 if (res == 1)
00766 return RESULT_SUCCESS;
00767 fdprintf(agi->fd, "200 result=%d\n", res);
00768 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00769 }
00770
00771 static int handle_saytime(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00772 {
00773 int res;
00774 int num;
00775 if (argc != 4)
00776 return RESULT_SHOWUSAGE;
00777 if (sscanf(argv[2], "%d", &num) != 1)
00778 return RESULT_SHOWUSAGE;
00779 res = ast_say_time(chan, num, argv[3], chan->language);
00780 if (res == 1)
00781 return RESULT_SUCCESS;
00782 fdprintf(agi->fd, "200 result=%d\n", res);
00783 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00784 }
00785
00786 static int handle_saydatetime(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00787 {
00788 int res=0;
00789 time_t unixtime;
00790 char *format, *zone=NULL;
00791
00792 if (argc < 4)
00793 return RESULT_SHOWUSAGE;
00794
00795 if (argc > 4) {
00796 format = argv[4];
00797 } else {
00798
00799 if (!strcasecmp(chan->language, "de")) {
00800 format = "A dBY HMS";
00801 } else {
00802 format = "ABdY 'digits/at' IMp";
00803 }
00804 }
00805
00806 if (argc > 5 && !ast_strlen_zero(argv[5]))
00807 zone = argv[5];
00808
00809 if (ast_get_time_t(argv[2], &unixtime, 0, NULL))
00810 return RESULT_SHOWUSAGE;
00811
00812 res = ast_say_date_with_format(chan, unixtime, argv[3], chan->language, format, zone);
00813 if (res == 1)
00814 return RESULT_SUCCESS;
00815
00816 fdprintf(agi->fd, "200 result=%d\n", res);
00817 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00818 }
00819
00820 static int handle_sayphonetic(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00821 {
00822 int res;
00823
00824 if (argc != 4)
00825 return RESULT_SHOWUSAGE;
00826
00827 res = ast_say_phonetic_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
00828 if (res == 1)
00829 return RESULT_SUCCESS;
00830 fdprintf(agi->fd, "200 result=%d\n", res);
00831 return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
00832 }
00833
00834 static int handle_getdata(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00835 {
00836 int res;
00837 char data[1024];
00838 int max;
00839 int timeout;
00840
00841 if (argc < 3)
00842 return RESULT_SHOWUSAGE;
00843 if (argc >= 4)
00844 timeout = atoi(argv[3]);
00845 else
00846 timeout = 0;
00847 if (argc >= 5)
00848 max = atoi(argv[4]);
00849 else
00850 max = 1024;
00851 res = ast_app_getdata_full(chan, argv[2], data, max, timeout, agi->audio, agi->ctrl);
00852 if (res == 2)
00853 return RESULT_SUCCESS;
00854 else if (res == 1)
00855 fdprintf(agi->fd, "200 result=%s (timeout)\n", data);
00856 else if (res < 0 )
00857 fdprintf(agi->fd, "200 result=-1\n");
00858 else
00859 fdprintf(agi->fd, "200 result=%s\n", data);
00860 return RESULT_SUCCESS;
00861 }
00862
00863 static int handle_setcontext(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00864 {
00865
00866 if (argc != 3)
00867 return RESULT_SHOWUSAGE;
00868 ast_copy_string(chan->context, argv[2], sizeof(chan->context));
00869 fdprintf(agi->fd, "200 result=0\n");
00870 return RESULT_SUCCESS;
00871 }
00872
00873 static int handle_setextension(struct ast_channel *chan, AGI *agi, int argc, char **argv)
00874 {
00875 if (argc != 3)
00876 return RESULT_SHOWUSAGE;
00877 ast_copy_string(chan->exten, argv[2], sizeof(chan->exten));
00878 fdprintf(agi->fd, "200 result=0\n");
00879 return RESULT_SUCCESS;
00880 }
00881
00882 static int handle_setpriority(struct ast_channel *chan, AGI *agi, int argc, char **argv)
00883 {
00884 int pri;
00885 if (argc != 3)
00886 return RESULT_SHOWUSAGE;
00887
00888 if (sscanf(argv[2], "%d", &pri) != 1) {
00889 if ((pri = ast_findlabel_extension(chan, chan->context, chan->exten, argv[2], chan->cid.cid_num)) < 1)
00890 return RESULT_SHOWUSAGE;
00891 }
00892
00893 ast_explicit_goto(chan, NULL, NULL, pri);
00894 fdprintf(agi->fd, "200 result=0\n");
00895 return RESULT_SUCCESS;
00896 }
00897
00898 static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
00899 {
00900 struct ast_filestream *fs;
00901 struct ast_frame *f;
00902 struct timeval start;
00903 long sample_offset = 0;
00904 int res = 0;
00905 int ms;
00906
00907 struct ast_dsp *sildet=NULL;
00908 int totalsilence = 0;
00909 int dspsilence = 0;
00910 int silence = 0;
00911 int gotsilence = 0;
00912 char *silencestr=NULL;
00913 int rfmt=0;
00914
00915
00916
00917
00918 if (argc < 6)
00919 return RESULT_SHOWUSAGE;
00920 if (sscanf(argv[5], "%d", &ms) != 1)
00921 return RESULT_SHOWUSAGE;
00922
00923 if (argc > 6)
00924 silencestr = strchr(argv[6],'s');
00925 if ((argc > 7) && (!silencestr))
00926 silencestr = strchr(argv[7],'s');
00927 if ((argc > 8) && (!silencestr))
00928 silencestr = strchr(argv[8],'s');
00929
00930 if (silencestr) {
00931 if (strlen(silencestr) > 2) {
00932 if ((silencestr[0] == 's') && (silencestr[1] == '=')) {
00933 silencestr++;
00934 silencestr++;
00935 if (silencestr)
00936 silence = atoi(silencestr);
00937 if (silence > 0)
00938 silence *= 1000;
00939 }
00940 }
00941 }
00942
00943 if (silence > 0) {
00944 rfmt = chan->readformat;
00945 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
00946 if (res < 0) {
00947 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
00948 return -1;
00949 }
00950 sildet = ast_dsp_new();
00951 if (!sildet) {
00952 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
00953 return -1;
00954 }
00955 ast_dsp_set_threshold(sildet, 256);
00956 }
00957
00958
00959
00960
00961 if ((argc >6) && (sscanf(argv[6], "%ld", &sample_offset) != 1) && (!strchr(argv[6], '=')))
00962 res = ast_streamfile(chan, "beep", chan->language);
00963
00964 if ((argc > 7) && (!strchr(argv[7], '=')))
00965 res = ast_streamfile(chan, "beep", chan->language);
00966
00967 if (!res)
00968 res = ast_waitstream(chan, argv[4]);
00969 if (res) {
00970 fdprintf(agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset);
00971 } else {
00972 fs = ast_writefile(argv[2], argv[3], NULL, O_CREAT | O_WRONLY | (sample_offset ? O_APPEND : 0), 0, 0644);
00973 if (!fs) {
00974 res = -1;
00975 fdprintf(agi->fd, "200 result=%d (writefile)\n", res);
00976 if (sildet)
00977 ast_dsp_free(sildet);
00978 return RESULT_FAILURE;
00979 }
00980
00981
00982 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
00983
00984 chan->stream = fs;
00985 ast_applystream(chan,fs);
00986
00987 ast_seekstream(fs, sample_offset, SEEK_SET);
00988 ast_truncstream(fs);
00989
00990 start = ast_tvnow();
00991 while ((ms < 0) || ast_tvdiff_ms(ast_tvnow(), start) < ms) {
00992 res = ast_waitfor(chan, ms - ast_tvdiff_ms(ast_tvnow(), start));
00993 if (res < 0) {
00994 ast_closestream(fs);
00995 fdprintf(agi->fd, "200 result=%d (waitfor) endpos=%ld\n", res,sample_offset);
00996 if (sildet)
00997 ast_dsp_free(sildet);
00998 return RESULT_FAILURE;
00999 }
01000 f = ast_read(chan);
01001 if (!f) {
01002 fdprintf(agi->fd, "200 result=%d (hangup) endpos=%ld\n", -1, sample_offset);
01003 ast_closestream(fs);
01004 if (sildet)
01005 ast_dsp_free(sildet);
01006 return RESULT_FAILURE;
01007 }
01008 switch(f->frametype) {
01009 case AST_FRAME_DTMF:
01010 if (strchr(argv[4], f->subclass)) {
01011
01012
01013
01014 ast_stream_rewind(fs, 200);
01015 ast_truncstream(fs);
01016 sample_offset = ast_tellstream(fs);
01017 fdprintf(agi->fd, "200 result=%d (dtmf) endpos=%ld\n", f->subclass, sample_offset);
01018 ast_closestream(fs);
01019 ast_frfree(f);
01020 if (sildet)
01021 ast_dsp_free(sildet);
01022 return RESULT_SUCCESS;
01023 }
01024 break;
01025 case AST_FRAME_VOICE:
01026 ast_writestream(fs, f);
01027
01028
01029
01030 sample_offset = ast_tellstream(fs);
01031 if (silence > 0) {
01032 dspsilence = 0;
01033 ast_dsp_silence(sildet, f, &dspsilence);
01034 if (dspsilence) {
01035 totalsilence = dspsilence;
01036 } else {
01037 totalsilence = 0;
01038 }
01039 if (totalsilence > silence) {
01040
01041 gotsilence = 1;
01042 break;
01043 }
01044 }
01045 break;
01046 case AST_FRAME_VIDEO:
01047 ast_writestream(fs, f);
01048 default:
01049
01050 break;
01051 }
01052 ast_frfree(f);
01053 if (gotsilence)
01054 break;
01055 }
01056
01057 if (gotsilence) {
01058 ast_stream_rewind(fs, silence-1000);
01059 ast_truncstream(fs);
01060 sample_offset = ast_tellstream(fs);
01061 }
01062 fdprintf(agi->fd, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset);
01063 ast_closestream(fs);
01064 }
01065
01066 if (silence > 0) {
01067 res = ast_set_read_format(chan, rfmt);
01068 if (res)
01069 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
01070 ast_dsp_free(sildet);
01071 }
01072 return RESULT_SUCCESS;
01073 }
01074
01075 static int handle_autohangup(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
01076 {
01077 int timeout;
01078
01079 if (argc != 3)
01080 return RESULT_SHOWUSAGE;
01081 if (sscanf(argv[2], "%d", &timeout) != 1)
01082 return RESULT_SHOWUSAGE;
01083 if (timeout < 0)
01084 timeout = 0;
01085 if (timeout)
01086 chan->whentohangup = time(NULL) + timeout;
01087 else
01088 chan->whentohangup = 0;
01089 fdprintf(agi->fd, "200 result=0\n");
01090 return RESULT_SUCCESS;
01091 }
01092
01093 static int handle_hangup(struct ast_channel *chan, AGI *agi, int argc, char **argv)
01094 {
01095 struct ast_channel *c;
01096 if (argc == 1) {
01097
01098 ast_softhangup(chan,AST_SOFTHANGUP_EXPLICIT);
01099 fdprintf(agi->fd, "200 result=1\n");
01100 return RESULT_SUCCESS;
01101 } else if (argc == 2) {
01102
01103 c = ast_get_channel_by_name_locked(argv[1]);
01104 if (c) {
01105
01106 ast_softhangup(c,AST_SOFTHANGUP_EXPLICIT);
01107 fdprintf(agi->fd, "200 result=1\n");
01108 ast_channel_unlock(c);
01109 return RESULT_SUCCESS;
01110 }
01111
01112 fdprintf(agi->fd, "200 result=-1\n");
01113 return RESULT_SUCCESS;
01114 } else {
01115 return RESULT_SHOWUSAGE;
01116 }
01117 }
01118
01119 static int handle_exec(struct ast_channel *chan, AGI *agi, int argc, char **argv)
01120 {
01121 int res;
01122 struct ast_app *app;
01123
01124 if (argc < 2)
01125 return RESULT_SHOWUSAGE;
01126
01127 if (option_verbose > 2)
01128 ast_verbose(VERBOSE_PREFIX_3 "AGI Script Executing Application: (%s) Options: (%s)\n", argv[1], argv[2]);
01129
01130 app = pbx_findapp(argv[1]);
01131
01132 if (app) {
01133 if(!strcasecmp(argv[1], PARK_APP_NAME)) {
01134 ast_masq_park_call(chan, NULL, 0, NULL);
01135 }
01136 res = pbx_exec(chan, app, argv[2]);
01137 } else {
01138 ast_log(LOG_WARNING, "Could not find application (%s)\n", argv[1]);
01139 res = -2;
01140 }
01141 fdprintf(agi->fd, "200 result=%d\n", res);
01142
01143
01144 return res;
01145 }
01146
01147 static int handle_setcallerid(struct ast_channel *chan, AGI *agi, int argc, char **argv)
01148 {
01149 char tmp[256]="";
01150 char *l = NULL, *n = NULL;
01151
01152 if (argv[2]) {
01153 ast_copy_string(tmp, argv[2], sizeof(tmp));
01154 ast_callerid_parse(tmp, &n, &l);
01155 if (l)
01156 ast_shrink_phone_number(l);
01157 else
01158 l = "";
01159 if (!n)
01160 n = "";
01161 ast_set_callerid(chan, l, n, NULL);
01162 }
01163
01164 fdprintf(agi->fd, "200 result=1\n");
01165 return RESULT_SUCCESS;
01166 }
01167
01168 static int handle_channelstatus(struct ast_channel *chan, AGI *agi, int argc, char **argv)
01169 {
01170 struct ast_channel *c;
01171 if (argc == 2) {
01172
01173 fdprintf(agi->fd, "200 result=%d\n", chan->_state);
01174 return RESULT_SUCCESS;
01175 } else if (argc == 3) {
01176
01177 c = ast_get_channel_by_name_locked(argv[2]);
01178 if (c) {
01179 fdprintf(agi->fd, "200 result=%d\n", c->_state);
01180 ast_channel_unlock(c);
01181 return RESULT_SUCCESS;
01182 }
01183
01184 fdprintf(agi->fd, "200 result=-1\n");
01185 return RESULT_SUCCESS;
01186 } else {
01187 return RESULT_SHOWUSAGE;
01188 }
01189 }
01190
01191 static int handle_setvariable(struct ast_channel *chan, AGI *agi, int argc, char **argv)
01192 {
01193 if (argv[3])
01194 pbx_builtin_setvar_helper(chan, argv[2], argv[3]);
01195
01196 fdprintf(agi->fd, "200 result=1\n");
01197 return RESULT_SUCCESS;
01198 }
01199
01200 static int handle_getvariable(struct ast_channel *chan, AGI *agi, int argc, char **argv)
01201 {
01202 char *ret;
01203 char tempstr[1024];
01204
01205 if (argc != 3)
01206 return RESULT_SHOWUSAGE;
01207
01208
01209 if (!ast_strlen_zero(argv[2]) && (argv[2][strlen(argv[2]) - 1] == ')')) {
01210 ret = ast_func_read(chan, argv[2], tempstr, sizeof(tempstr)) ? NULL : tempstr;
01211 } else {
01212 pbx_retrieve_variable(chan, argv[2], &ret, tempstr, sizeof(tempstr), NULL);
01213 }
01214
01215 if (ret)
01216 fdprintf(agi->fd, "200 result=1 (%s)\n", ret);
01217 else
01218 fdprintf(agi->fd, "200 result=0\n");
01219
01220 return RESULT_SUCCESS;
01221 }
01222
01223 static int handle_getvariablefull(struct ast_channel *chan, AGI *agi, int argc, char **argv)
01224 {
01225 char tmp[4096] = "";
01226 struct ast_channel *chan2=NULL;
01227
01228 if ((argc != 4) && (argc != 5))
01229 return RESULT_SHOWUSAGE;
01230 if (argc == 5) {
01231 chan2 = ast_get_channel_by_name_locked(argv[4]);
01232 } else {
01233 chan2 = chan;
01234 }
01235 if (chan2) {
01236 pbx_substitute_variables_helper(chan2, argv[3], tmp, sizeof(tmp) - 1);
01237 fdprintf(agi->fd, "200 result=1 (%s)\n", tmp);
01238 } else {
01239 fdprintf(agi->fd, "200 result=0\n");
01240 }
01241 if (chan2 && (chan2 != chan))
01242 ast_channel_unlock(chan2);
01243 return RESULT_SUCCESS;
01244 }
01245
01246 static int handle_verbose(struct ast_channel *chan, AGI *agi, int argc, char **argv)
01247 {
01248 int level = 0;
01249 char *prefix;
01250
01251 if (argc < 2)
01252 return RESULT_SHOWUSAGE;
01253
01254 if (argv[2])
01255 sscanf(argv[2], "%d", &level);
01256
01257 switch (level) {
01258 case 4:
01259 prefix = VERBOSE_PREFIX_4;
01260 break;
01261 case 3:
01262 prefix = VERBOSE_PREFIX_3;
01263 break;
01264 case 2:
01265 prefix = VERBOSE_PREFIX_2;
01266 break;
01267 case 1:
01268 default:
01269 prefix = VERBOSE_PREFIX_1;
01270 break;
01271 }
01272
01273 if (level <= option_verbose)
01274 ast_verbose("%s %s: %s\n", prefix, chan->data, argv[1]);
01275
01276 fdprintf(agi->fd, "200 result=1\n");
01277
01278 return RESULT_SUCCESS;
01279 }
01280
01281 static int handle_dbget(struct ast_channel *chan, AGI *agi, int argc, char **argv)
01282 {
01283 int res;
01284 char tmp[256];
01285
01286 if (argc != 4)
01287 return RESULT_SHOWUSAGE;
01288 res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp));
01289 if (res)
01290 fdprintf(agi->fd, "200 result=0\n");
01291 else
01292 fdprintf(agi->fd, "200 result=1 (%s)\n", tmp);
01293
01294 return RESULT_SUCCESS;
01295 }
01296
01297 static int handle_dbput(struct ast_channel *chan, AGI *agi, int argc, char **argv)
01298 {
01299 int res;
01300
01301 if (argc != 5)
01302 return RESULT_SHOWUSAGE;
01303 res = ast_db_put(argv[2], argv[3], argv[4]);
01304 fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1');
01305 return RESULT_SUCCESS;
01306 }
01307
01308 static int handle_dbdel(struct ast_channel *chan, AGI *agi, int argc, char **argv)
01309 {
01310 int res;
01311
01312 if (argc != 4)
01313 return RESULT_SHOWUSAGE;
01314 res = ast_db_del(argv[2], argv[3]);
01315 fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1');
01316 return RESULT_SUCCESS;
01317 }
01318
01319 static int handle_dbdeltree(struct ast_channel *chan, AGI *agi, int argc, char **argv)
01320 {
01321 int res;
01322 if ((argc < 3) || (argc > 4))
01323 return RESULT_SHOWUSAGE;
01324 if (argc == 4)
01325 res = ast_db_deltree(argv[2], argv[3]);
01326 else
01327 res = ast_db_deltree(argv[2], NULL);
01328
01329 fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1');
01330 return RESULT_SUCCESS;
01331 }
01332
01333 static char debug_usage[] =
01334 "Usage: agi debug\n"
01335 " Enables dumping of AGI transactions for debugging purposes\n";
01336
01337 static char no_debug_usage[] =
01338 "Usage: agi debug off\n"
01339 " Disables dumping of AGI transactions for debugging purposes\n";
01340
01341 static int agi_do_debug(int fd, int argc, char *argv[])
01342 {
01343 if (argc != 2)
01344 return RESULT_SHOWUSAGE;
01345 agidebug = 1;
01346 ast_cli(fd, "AGI Debugging Enabled\n");
01347 return RESULT_SUCCESS;
01348 }
01349
01350 static int agi_no_debug_deprecated(int fd, int argc, char *argv[])
01351 {
01352 if (argc != 3)
01353 return RESULT_SHOWUSAGE;
01354 agidebug = 0;
01355 ast_cli(fd, "AGI Debugging Disabled\n");
01356 return RESULT_SUCCESS;
01357 }
01358
01359 static int agi_no_debug(int fd, int argc, char *argv[])
01360 {
01361 if (argc != 3)
01362 return RESULT_SHOWUSAGE;
01363 agidebug = 0;
01364 ast_cli(fd, "AGI Debugging Disabled\n");
01365 return RESULT_SUCCESS;
01366 }
01367
01368 static int handle_noop(struct ast_channel *chan, AGI *agi, int arg, char *argv[])
01369 {
01370 fdprintf(agi->fd, "200 result=0\n");
01371 return RESULT_SUCCESS;
01372 }
01373
01374 static int handle_setmusic(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
01375 {
01376 if (!strncasecmp(argv[2], "on", 2))
01377 ast_moh_start(chan, argc > 3 ? argv[3] : NULL, NULL);
01378 else if (!strncasecmp(argv[2], "off", 3))
01379 ast_moh_stop(chan);
01380 fdprintf(agi->fd, "200 result=0\n");
01381 return RESULT_SUCCESS;
01382 }
01383
01384 static char usage_setmusic[] =
01385 " Usage: SET MUSIC ON <on|off> <class>\n"
01386 " Enables/Disables the music on hold generator. If <class> is\n"
01387 " not specified, then the default music on hold class will be used.\n"
01388 " Always returns 0.\n";
01389
01390 static char usage_dbput[] =
01391 " Usage: DATABASE PUT <family> <key> <value>\n"
01392 " Adds or updates an entry in the Asterisk database for a\n"
01393 " given family, key, and value.\n"
01394 " Returns 1 if successful, 0 otherwise.\n";
01395
01396 static char usage_dbget[] =
01397 " Usage: DATABASE GET <family> <key>\n"
01398 " Retrieves an entry in the Asterisk database for a\n"
01399 " given family and key.\n"
01400 " Returns 0 if <key> is not set. Returns 1 if <key>\n"
01401 " is set and returns the variable in parentheses.\n"
01402 " Example return code: 200 result=1 (testvariable)\n";
01403
01404 static char usage_dbdel[] =
01405 " Usage: DATABASE DEL <family> <key>\n"
01406 " Deletes an entry in the Asterisk database for a\n"
01407 " given family and key.\n"
01408 " Returns 1 if successful, 0 otherwise.\n";
01409
01410 static char usage_dbdeltree[] =
01411 " Usage: DATABASE DELTREE <family> [keytree]\n"
01412 " Deletes a family or specific keytree within a family\n"
01413 " in the Asterisk database.\n"
01414 " Returns 1 if successful, 0 otherwise.\n";
01415
01416 static char usage_verbose[] =
01417 " Usage: VERBOSE <message> <level>\n"
01418 " Sends <message> to the console via verbose message system.\n"
01419 " <level> is the the verbose level (1-4)\n"
01420 " Always returns 1.\n";
01421
01422 static char usage_getvariable[] =
01423 " Usage: GET VARIABLE <variablename>\n"
01424 " Returns 0 if <variablename> is not set. Returns 1 if <variablename>\n"
01425 " is set and returns the variable in parentheses.\n"
01426 " example return code: 200 result=1 (testvariable)\n";
01427
01428 static char usage_getvariablefull[] =
01429 " Usage: GET FULL VARIABLE <variablename> [<channel name>]\n"
01430 " Returns 0 if <variablename> is not set or channel does not exist. Returns 1\n"
01431 "if <variablename> is set and returns the variable in parenthesis. Understands\n"
01432 "complex variable names and builtin variables, unlike GET VARIABLE.\n"
01433 " example return code: 200 result=1 (testvariable)\n";
01434
01435 static char usage_setvariable[] =
01436 " Usage: SET VARIABLE <variablename> <value>\n";
01437
01438 static char usage_channelstatus[] =
01439 " Usage: CHANNEL STATUS [<channelname>]\n"
01440 " Returns the status of the specified channel.\n"
01441 " If no channel name is given the returns the status of the\n"
01442 " current channel. Return values:\n"
01443 " 0 Channel is down and available\n"
01444 " 1 Channel is down, but reserved\n"
01445 " 2 Channel is off hook\n"
01446 " 3 Digits (or equivalent) have been dialed\n"
01447 " 4 Line is ringing\n"
01448 " 5 Remote end is ringing\n"
01449 " 6 Line is up\n"
01450 " 7 Line is busy\n";
01451
01452 static char usage_setcallerid[] =
01453 " Usage: SET CALLERID <number>\n"
01454 " Changes the callerid of the current channel.\n";
01455
01456 static char usage_exec[] =
01457 " Usage: EXEC <application> <options>\n"
01458 " Executes <application> with given <options>.\n"
01459 " Returns whatever the application returns, or -2 on failure to find application\n";
01460
01461 static char usage_hangup[] =
01462 " Usage: HANGUP [<channelname>]\n"
01463 " Hangs up the specified channel.\n"
01464 " If no channel name is given, hangs up the current channel\n";
01465
01466 static char usage_answer[] =
01467 " Usage: ANSWER\n"
01468 " Answers channel if not already in answer state. Returns -1 on\n"
01469 " channel failure, or 0 if successful.\n";
01470
01471 static char usage_waitfordigit[] =
01472 " Usage: WAIT FOR DIGIT <timeout>\n"
01473 " Waits up to 'timeout' milliseconds for channel to receive a DTMF digit.\n"
01474 " Returns -1 on channel failure, 0 if no digit is received in the timeout, or\n"
01475 " the numerical value of the ascii of the digit if one is received. Use -1\n"
01476 " for the timeout value if you desire the call to block indefinitely.\n";
01477
01478 static char usage_sendtext[] =
01479 " Usage: SEND TEXT \"<text to send>\"\n"
01480 " Sends the given text on a channel. Most channels do not support the\n"
01481 " transmission of text. Returns 0 if text is sent, or if the channel does not\n"
01482 " support text transmission. Returns -1 only on error/hangup. Text\n"
01483 " consisting of greater than one word should be placed in quotes since the\n"
01484 " command only accepts a single argument.\n";
01485
01486 static char usage_recvchar[] =
01487 " Usage: RECEIVE CHAR <timeout>\n"
01488 " Receives a character of text on a channel. Specify timeout to be the\n"
01489 " maximum time to wait for input in milliseconds, or 0 for infinite. Most channels\n"
01490 " do not support the reception of text. Returns the decimal value of the character\n"
01491 " if one is received, or 0 if the channel does not support text reception. Returns\n"
01492 " -1 only on error/hangup.\n";
01493
01494 static char usage_recvtext[] =
01495 " Usage: RECEIVE TEXT <timeout>\n"
01496 " Receives a string of text on a channel. Specify timeout to be the\n"
01497 " maximum time to wait for input in milliseconds, or 0 for infinite. Most channels\n"
01498 " do not support the reception of text. Returns -1 for failure or 1 for success, and the string in parentheses.\n";
01499
01500 static char usage_tddmode[] =
01501 " Usage: TDD MODE <on|off>\n"
01502 " Enable/Disable TDD transmission/reception on a channel. Returns 1 if\n"
01503 " successful, or 0 if channel is not TDD-capable.\n";
01504
01505 static char usage_sendimage[] =
01506 " Usage: SEND IMAGE <image>\n"
01507 " Sends the given image on a channel. Most channels do not support the\n"
01508 " transmission of images. Returns 0 if image is sent, or if the channel does not\n"
01509 " support image transmission. Returns -1 only on error/hangup. Image names\n"
01510 " should not include extensions.\n";
01511
01512 static char usage_streamfile[] =
01513 " Usage: STREAM FILE <filename> <escape digits> [sample offset]\n"
01514 " Send the given file, allowing playback to be interrupted by the given\n"
01515 " digits, if any. Use double quotes for the digits if you wish none to be\n"
01516 " permitted. If sample offset is provided then the audio will seek to sample\n"
01517 " offset before play starts. Returns 0 if playback completes without a digit\n"
01518 " being pressed, or the ASCII numerical value of the digit if one was pressed,\n"
01519 " or -1 on error or if the channel was disconnected. Remember, the file\n"
01520 " extension must not be included in the filename.\n";
01521
01522 static char usage_controlstreamfile[] =
01523 " Usage: CONTROL STREAM FILE <filename> <escape digits> [skipms] [ffchar] [rewchr] [pausechr]\n"
01524 " Send the given file, allowing playback to be controled by the given\n"
01525 " digits, if any. Use double quotes for the digits if you wish none to be\n"
01526 " permitted. Returns 0 if playback completes without a digit\n"
01527 " being pressed, or the ASCII numerical value of the digit if one was pressed,\n"
01528 " or -1 on error or if the channel was disconnected. Remember, the file\n"
01529 " extension must not be included in the filename.\n\n"
01530 " Note: ffchar and rewchar default to * and # respectively.\n";
01531
01532 static char usage_getoption[] =
01533 " Usage: GET OPTION <filename> <escape_digits> [timeout]\n"
01534 " Behaves similar to STREAM FILE but used with a timeout option.\n";
01535
01536 static char usage_saynumber[] =
01537 " Usage: SAY NUMBER <number> <escape digits>\n"
01538 " Say a given number, returning early if any of the given DTMF digits\n"
01539 " are received on the channel. Returns 0 if playback completes without a digit\n"
01540 " being pressed, or the ASCII numerical value of the digit if one was pressed or\n"
01541 " -1 on error/hangup.\n";
01542
01543 static char usage_saydigits[] =
01544 " Usage: SAY DIGITS <number> <escape digits>\n"
01545 " Say a given digit string, returning early if any of the given DTMF digits\n"
01546 " are received on the channel. Returns 0 if playback completes without a digit\n"
01547 " being pressed, or the ASCII numerical value of the digit if one was pressed or\n"
01548 " -1 on error/hangup.\n";
01549
01550 static char usage_sayalpha[] =
01551 " Usage: SAY ALPHA <number> <escape digits>\n"
01552 " Say a given character string, returning early if any of the given DTMF digits\n"
01553 " are received on the channel. Returns 0 if playback completes without a digit\n"
01554 " being pressed, or the ASCII numerical value of the digit if one was pressed or\n"
01555 " -1 on error/hangup.\n";
01556
01557 static char usage_saydate[] =
01558 " Usage: SAY DATE <date> <escape digits>\n"
01559 " Say a given date, returning early if any of the given DTMF digits are\n"
01560 " received on the channel. <date> is number of seconds elapsed since 00:00:00\n"
01561 " on January 1, 1970, Coordinated Universal Time (UTC). Returns 0 if playback\n"
01562 " completes without a digit being pressed, or the ASCII numerical value of the\n"
01563 " digit if one was pressed or -1 on error/hangup.\n";
01564
01565 static char usage_saytime[] =
01566 " Usage: SAY TIME <time> <escape digits>\n"
01567 " Say a given time, returning early if any of the given DTMF digits are\n"
01568 " received on the channel. <time> is number of seconds elapsed since 00:00:00\n"
01569 " on January 1, 1970, Coordinated Universal Time (UTC). Returns 0 if playback\n"
01570 " completes without a digit being pressed, or the ASCII numerical value of the\n"
01571 " digit if one was pressed or -1 on error/hangup.\n";
01572
01573 static char usage_saydatetime[] =
01574 " Usage: SAY DATETIME <time> <escape digits> [format] [timezone]\n"
01575 " Say a given time, returning early if any of the given DTMF digits are\n"
01576 " received on the channel. <time> is number of seconds elapsed since 00:00:00\n"
01577 " on January 1, 1970, Coordinated Universal Time (UTC). [format] is the format\n"
01578 " the time should be said in. See voicemail.conf (defaults to \"ABdY\n"
01579 " 'digits/at' IMp\"). Acceptable values for [timezone] can be found in\n"
01580 " /usr/share/zoneinfo. Defaults to machine default. Returns 0 if playback\n"
01581 " completes without a digit being pressed, or the ASCII numerical value of the\n"
01582 " digit if one was pressed or -1 on error/hangup.\n";
01583
01584 static char usage_sayphonetic[] =
01585 " Usage: SAY PHONETIC <string> <escape digits>\n"
01586 " Say a given character string with phonetics, returning early if any of the\n"
01587 " given DTMF digits are received on the channel. Returns 0 if playback\n"
01588 " completes without a digit pressed, the ASCII numerical value of the digit\n"
01589 " if one was pressed, or -1 on error/hangup.\n";
01590
01591 static char usage_getdata[] =
01592 " Usage: GET DATA <file to be streamed> [timeout] [max digits]\n"
01593 " Stream the given file, and recieve DTMF data. Returns the digits received\n"
01594 "from the channel at the other end.\n";
01595
01596 static char usage_setcontext[] =
01597 " Usage: SET CONTEXT <desired context>\n"
01598 " Sets the context for continuation upon exiting the application.\n";
01599
01600 static char usage_setextension[] =
01601 " Usage: SET EXTENSION <new extension>\n"
01602 " Changes the extension for continuation upon exiting the application.\n";
01603
01604 static char usage_setpriority[] =
01605 " Usage: SET PRIORITY <priority>\n"
01606 " Changes the priority for continuation upon exiting the application.\n"
01607 " The priority must be a valid priority or label.\n";
01608
01609 static char usage_recordfile[] =
01610 " Usage: RECORD FILE <filename> <format> <escape digits> <timeout> \\\n"
01611 " [offset samples] [BEEP] [s=silence]\n"
01612 " Record to a file until a given dtmf digit in the sequence is received\n"
01613 " Returns -1 on hangup or error. The format will specify what kind of file\n"
01614 " will be recorded. The timeout is the maximum record time in milliseconds, or\n"
01615 " -1 for no timeout. \"Offset samples\" is optional, and, if provided, will seek\n"
01616 " to the offset without exceeding the end of the file. \"silence\" is the number\n"
01617 " of seconds of silence allowed before the function returns despite the\n"
01618 " lack of dtmf digits or reaching timeout. Silence value must be\n"
01619 " preceeded by \"s=\" and is also optional.\n";
01620
01621 static char usage_autohangup[] =
01622 " Usage: SET AUTOHANGUP <time>\n"
01623 " Cause the channel to automatically hangup at <time> seconds in the\n"
01624 " future. Of course it can be hungup before then as well. Setting to 0 will\n"
01625 " cause the autohangup feature to be disabled on this channel.\n";
01626
01627 static char usage_noop[] =
01628 " Usage: NoOp\n"
01629 " Does nothing.\n";
01630
01631 static agi_command commands[MAX_COMMANDS] = {
01632 { { "answer", NULL }, handle_answer, "Answer channel", usage_answer },
01633 { { "channel", "status", NULL }, handle_channelstatus, "Returns status of the connected channel", usage_channelstatus },
01634 { { "database", "del", NULL }, handle_dbdel, "Removes database key/value", usage_dbdel },
01635 { { "database", "deltree", NULL }, handle_dbdeltree, "Removes database keytree/value", usage_dbdeltree },
01636 { { "database", "get", NULL }, handle_dbget, "Gets database value", usage_dbget },
01637 { { "database", "put", NULL }, handle_dbput, "Adds/updates database value", usage_dbput },
01638 { { "exec", NULL }, handle_exec, "Executes a given Application", usage_exec },
01639 { { "get", "data", NULL }, handle_getdata, "Prompts for DTMF on a channel", usage_getdata },
01640 { { "get", "full", "variable", NULL }, handle_getvariablefull, "Evaluates a channel expression", usage_getvariablefull },
01641 { { "get", "option", NULL }, handle_getoption, "Stream file, prompt for DTMF, with timeout", usage_getoption },
01642 { { "get", "variable", NULL }, handle_getvariable, "Gets a channel variable", usage_getvariable },
01643 { { "hangup", NULL }, handle_hangup, "Hangup the current channel", usage_hangup },
01644 { { "noop", NULL }, handle_noop, "Does nothing", usage_noop },
01645 { { "receive", "char", NULL }, handle_recvchar, "Receives one character from channels supporting it", usage_recvchar },
01646 { { "receive", "text", NULL }, handle_recvtext, "Receives text from channels supporting it", usage_recvtext },
01647 { { "record", "file", NULL }, handle_recordfile, "Records to a given file", usage_recordfile },
01648 { { "say", "alpha", NULL }, handle_sayalpha, "Says a given character string", usage_sayalpha },
01649 { { "say", "digits", NULL }, handle_saydigits, "Says a given digit string", usage_saydigits },
01650 { { "say", "number", NULL }, handle_saynumber, "Says a given number", usage_saynumber },
01651 { { "say", "phonetic", NULL }, handle_sayphonetic, "Says a given character string with phonetics", usage_sayphonetic },
01652 { { "say", "date", NULL }, handle_saydate, "Says a given date", usage_saydate },
01653 { { "say", "time", NULL }, handle_saytime, "Says a given time", usage_saytime },
01654 { { "say", "datetime", NULL }, handle_saydatetime, "Says a given time as specfied by the format given", usage_saydatetime },
01655 { { "send", "image", NULL }, handle_sendimage, "Sends images to channels supporting it", usage_sendimage },
01656 { { "send", "text", NULL }, handle_sendtext, "Sends text to channels supporting it", usage_sendtext },
01657 { { "set", "autohangup", NULL }, handle_autohangup, "Autohangup channel in some time", usage_autohangup },
01658 { { "set", "callerid", NULL }, handle_setcallerid, "Sets callerid for the current channel", usage_setcallerid },
01659 { { "set", "context", NULL }, handle_setcontext, "Sets channel context", usage_setcontext },
01660 { { "set", "extension", NULL }, handle_setextension, "Changes channel extension", usage_setextension },
01661 { { "set", "music", NULL }, handle_setmusic, "Enable/Disable Music on hold generator", usage_setmusic },
01662 { { "set", "priority", NULL }, handle_setpriority, "Set channel dialplan priority", usage_setpriority },
01663 { { "set", "variable", NULL }, handle_setvariable, "Sets a channel variable", usage_setvariable },
01664 { { "stream", "file", NULL }, handle_streamfile, "Sends audio file on channel", usage_streamfile },
01665 { { "control", "stream", "file", NULL }, handle_controlstreamfile, "Sends audio file on channel and allows the listner to control the stream", usage_controlstreamfile },
01666 { { "tdd", "mode", NULL }, handle_tddmode, "Toggles TDD mode (for the deaf)", usage_tddmode },
01667 { { "verbose", NULL }, handle_verbose, "Logs a message to the asterisk verbose log", usage_verbose },
01668 { { "wait", "for", "digit", NULL }, handle_waitfordigit, "Waits for a digit to be pressed", usage_waitfordigit },
01669 };
01670
01671 static int help_workhorse(int fd, char *match[])
01672 {
01673 char fullcmd[80];
01674 char matchstr[80];
01675 int x;
01676 struct agi_command *e;
01677 if (match)
01678 ast_join(matchstr, sizeof(matchstr), match);
01679 for (x=0;x<sizeof(commands)/sizeof(commands[0]);x++) {
01680 e = &commands[x];
01681 if (!e->cmda[0])
01682 break;
01683
01684 if ((e->cmda[0])[0] == '_')
01685 continue;
01686 ast_join(fullcmd, sizeof(fullcmd), e->cmda);
01687 if (match && strncasecmp(matchstr, fullcmd, strlen(matchstr)))
01688 continue;
01689 ast_cli(fd, "%20.20s %s\n", fullcmd, e->summary);
01690 }
01691 return 0;
01692 }
01693
01694 int ast_agi_register(agi_command *agi)
01695 {
01696 int x;
01697 for (x=0; x<MAX_COMMANDS - 1; x++) {
01698 if (commands[x].cmda[0] == agi->cmda[0]) {
01699 ast_log(LOG_WARNING, "Command already registered!\n");
01700 return -1;
01701 }
01702 }
01703 for (x=0; x<MAX_COMMANDS - 1; x++) {
01704 if (!commands[x].cmda[0]) {
01705 commands[x] = *agi;
01706 return 0;
01707 }
01708 }
01709 ast_log(LOG_WARNING, "No more room for new commands!\n");
01710 return -1;
01711 }
01712
01713 void ast_agi_unregister(agi_command *agi)
01714 {
01715 int x;
01716 for (x=0; x<MAX_COMMANDS - 1; x++) {
01717 if (commands[x].cmda[0] == agi->cmda[0]) {
01718 memset(&commands[x], 0, sizeof(agi_command));
01719 }
01720 }
01721 }
01722
01723 static agi_command *find_command(char *cmds[], int exact)
01724 {
01725 int x;
01726 int y;
01727 int match;
01728
01729 for (x=0; x < sizeof(commands) / sizeof(commands[0]); x++) {
01730 if (!commands[x].cmda[0])
01731 break;
01732
01733 match = 1;
01734 for (y=0; match && cmds[y]; y++) {
01735
01736
01737
01738 if (!commands[x].cmda[y] && !exact)
01739 break;
01740
01741 if (!commands[x].cmda[y])
01742 return NULL;
01743 if (strcasecmp(commands[x].cmda[y], cmds[y]))
01744 match = 0;
01745 }
01746
01747
01748 if ((exact > -1) && commands[x].cmda[y])
01749 match = 0;
01750 if (match)
01751 return &commands[x];
01752 }
01753 return NULL;
01754 }
01755
01756
01757 static int parse_args(char *s, int *max, char *argv[])
01758 {
01759 int x=0;
01760 int quoted=0;
01761 int escaped=0;
01762 int whitespace=1;
01763 char *cur;
01764
01765 cur = s;
01766 while(*s) {
01767 switch(*s) {
01768 case '"':
01769
01770 if (escaped)
01771 goto normal;
01772 else
01773 quoted = !quoted;
01774 if (quoted && whitespace) {
01775
01776 argv[x++] = cur;
01777 whitespace=0;
01778 }
01779 escaped = 0;
01780 break;
01781 case ' ':
01782 case '\t':
01783 if (!quoted && !escaped) {
01784
01785
01786 whitespace = 1;
01787 *(cur++) = '\0';
01788 } else
01789
01790 goto normal;
01791 break;
01792 case '\\':
01793
01794 if (escaped) {
01795 goto normal;
01796 } else {
01797 escaped=1;
01798 }
01799 break;
01800 default:
01801 normal:
01802 if (whitespace) {
01803 if (x >= MAX_ARGS -1) {
01804 ast_log(LOG_WARNING, "Too many arguments, truncating\n");
01805 break;
01806 }
01807
01808 argv[x++] = cur;
01809 whitespace=0;
01810 }
01811 *(cur++) = *s;
01812 escaped=0;
01813 }
01814 s++;
01815 }
01816
01817 *(cur++) = '\0';
01818 argv[x] = NULL;
01819 *max = x;
01820 return 0;
01821 }
01822
01823 static int agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf)
01824 {
01825 char *argv[MAX_ARGS];
01826 int argc = MAX_ARGS;
01827 int res;
01828 agi_command *c;
01829
01830 parse_args(buf, &argc, argv);
01831 c = find_command(argv, 0);
01832 if (c) {
01833 res = c->handler(chan, agi, argc, argv);
01834 switch(res) {
01835 case RESULT_SHOWUSAGE:
01836 fdprintf(agi->fd, "520-Invalid command syntax. Proper usage follows:\n");
01837 fdprintf(agi->fd, "%s", c->usage);
01838 fdprintf(agi->fd, "520 End of proper usage.\n");
01839 break;
01840 case AST_PBX_KEEPALIVE:
01841
01842 return AST_PBX_KEEPALIVE;
01843 break;
01844 case RESULT_FAILURE:
01845
01846
01847 return -1;
01848 }
01849 } else {
01850 fdprintf(agi->fd, "510 Invalid or unknown command\n");
01851 }
01852 return 0;
01853 }
01854
01855 #define RETRY 3
01856 static enum agi_result run_agi(struct ast_channel *chan, char *request, AGI *agi, int pid, int *status, int dead, int argc, char *argv[])
01857 {
01858 struct ast_channel *c;
01859 int outfd;
01860 int ms;
01861 enum agi_result returnstatus = AGI_RESULT_SUCCESS;
01862 struct ast_frame *f;
01863 char buf[AGI_BUF_LEN];
01864 char *res = NULL;
01865 FILE *readf;
01866
01867
01868 int retry = AGI_NANDFS_RETRY;
01869
01870 if (!(readf = fdopen(agi->ctrl, "r"))) {
01871 ast_log(LOG_WARNING, "Unable to fdopen file descriptor\n");
01872 if (pid > -1)
01873 kill(pid, SIGHUP);
01874 close(agi->ctrl);
01875 return AGI_RESULT_FAILURE;
01876 }
01877 setlinebuf(readf);
01878 setup_env(chan, request, agi->fd, (agi->audio > -1), argc, argv);
01879 for (;;) {
01880 ms = -1;
01881 c = ast_waitfor_nandfds(&chan, dead ? 0 : 1, &agi->ctrl, 1, NULL, &outfd, &ms);
01882 if (c) {
01883 retry = AGI_NANDFS_RETRY;
01884
01885 f = ast_read(c);
01886 if (!f) {
01887 ast_log(LOG_DEBUG, "%s hungup\n", chan->name);
01888 returnstatus = AGI_RESULT_HANGUP;
01889 break;
01890 } else {
01891
01892 if ((agi->audio > -1) && (f->frametype == AST_FRAME_VOICE)) {
01893
01894 if (write(agi->audio, f->data, f->datalen) < 0) {
01895 }
01896 }
01897 ast_frfree(f);
01898 }
01899 } else if (outfd > -1) {
01900 size_t len = sizeof(buf);
01901 size_t buflen = 0;
01902
01903 retry = AGI_NANDFS_RETRY;
01904 buf[0] = '\0';
01905
01906 while (buflen < (len - 1)) {
01907 res = fgets(buf + buflen, len, readf);
01908 if (feof(readf))
01909 break;
01910 if (ferror(readf) && ((errno != EINTR) && (errno != EAGAIN)))
01911 break;
01912 if (res != NULL && !agi->fast)
01913 break;
01914 buflen = strlen(buf);
01915 if (buflen && buf[buflen - 1] == '\n')
01916 break;
01917 len -= buflen;
01918 if (agidebug)
01919 ast_verbose( "AGI Rx << temp buffer %s - errno %s\n", buf, strerror(errno));
01920 }
01921
01922 if (!buf[0]) {
01923
01924 if (returnstatus)
01925 returnstatus = -1;
01926 if (option_verbose > 2)
01927 ast_verbose(VERBOSE_PREFIX_3 "AGI Script %s completed, returning %d\n", request, returnstatus);
01928 if (pid > 0)
01929 waitpid(pid, status, 0);
01930
01931 pid = -1;
01932 break;
01933 }
01934
01935
01936 if (*buf && strncasecmp(buf, "failure", 7) == 0) {
01937 returnstatus = AGI_RESULT_FAILURE;
01938 break;
01939 }
01940
01941
01942 if (*buf && buf[strlen(buf) - 1] == '\n')
01943 buf[strlen(buf) - 1] = 0;
01944 if (agidebug)
01945 ast_verbose("AGI Rx << %s\n", buf);
01946 returnstatus |= agi_handle_command(chan, agi, buf);
01947
01948 if ((returnstatus < 0) || (returnstatus == AST_PBX_KEEPALIVE)) {
01949 break;
01950 }
01951 } else {
01952 if (--retry <= 0) {
01953 ast_log(LOG_WARNING, "No channel, no fd?\n");
01954 returnstatus = AGI_RESULT_FAILURE;
01955 break;
01956 }
01957 }
01958 }
01959
01960 if (pid > -1) {
01961 const char *sighup = pbx_builtin_getvar_helper(chan, "AGISIGHUP");
01962 if (ast_strlen_zero(sighup) || !ast_false(sighup)) {
01963 if (kill(pid, SIGHUP)) {
01964 ast_log(LOG_WARNING, "unable to send SIGHUP to AGI process %d: %s\n", pid, strerror(errno));
01965 } else {
01966 usleep(1);
01967 }
01968 }
01969 waitpid(pid, status, WNOHANG);
01970 }
01971 fclose(readf);
01972 return returnstatus;
01973 }
01974
01975 static int handle_showagi(int fd, int argc, char *argv[])
01976 {
01977 struct agi_command *e;
01978 char fullcmd[80];
01979 if ((argc < 2))
01980 return RESULT_SHOWUSAGE;
01981 if (argc > 2) {
01982 e = find_command(argv + 2, 1);
01983 if (e)
01984 ast_cli(fd, "%s", e->usage);
01985 else {
01986 if (find_command(argv + 2, -1)) {
01987 return help_workhorse(fd, argv + 1);
01988 } else {
01989 ast_join(fullcmd, sizeof(fullcmd), argv+1);
01990 ast_cli(fd, "No such command '%s'.\n", fullcmd);
01991 }
01992 }
01993 } else {
01994 return help_workhorse(fd, NULL);
01995 }
01996 return RESULT_SUCCESS;
01997 }
01998
01999 static int handle_agidumphtml(int fd, int argc, char *argv[])
02000 {
02001 struct agi_command *e;
02002 char fullcmd[80];
02003 int x;
02004 FILE *htmlfile;
02005
02006 if ((argc < 3))
02007 return RESULT_SHOWUSAGE;
02008
02009 if (!(htmlfile = fopen(argv[2], "wt"))) {
02010 ast_cli(fd, "Could not create file '%s'\n", argv[2]);
02011 return RESULT_SHOWUSAGE;
02012 }
02013
02014 fprintf(htmlfile, "<HTML>\n<HEAD>\n<TITLE>AGI Commands</TITLE>\n</HEAD>\n");
02015 fprintf(htmlfile, "<BODY>\n<CENTER><B><H1>AGI Commands</H1></B></CENTER>\n\n");
02016
02017
02018 fprintf(htmlfile, "<TABLE BORDER=\"0\" CELLSPACING=\"10\">\n");
02019
02020 for (x=0;x<sizeof(commands)/sizeof(commands[0]);x++) {
02021 char *stringp, *tempstr;
02022
02023 e = &commands[x];
02024 if (!e->cmda[0])
02025 break;
02026
02027 if ((e->cmda[0])[0] == '_')
02028 continue;
02029 ast_join(fullcmd, sizeof(fullcmd), e->cmda);
02030
02031 fprintf(htmlfile, "<TR><TD><TABLE BORDER=\"1\" CELLPADDING=\"5\" WIDTH=\"100%%\">\n");
02032 fprintf(htmlfile, "<TR><TH ALIGN=\"CENTER\"><B>%s - %s</B></TH></TR>\n", fullcmd,e->summary);
02033
02034 stringp=e->usage;
02035 tempstr = strsep(&stringp, "\n");
02036
02037 fprintf(htmlfile, "<TR><TD ALIGN=\"CENTER\">%s</TD></TR>\n", tempstr);
02038
02039 fprintf(htmlfile, "<TR><TD ALIGN=\"CENTER\">\n");
02040 while ((tempstr = strsep(&stringp, "\n")) != NULL)
02041 fprintf(htmlfile, "%s<BR>\n",tempstr);
02042 fprintf(htmlfile, "</TD></TR>\n");
02043 fprintf(htmlfile, "</TABLE></TD></TR>\n\n");
02044
02045 }
02046
02047 fprintf(htmlfile, "</TABLE>\n</BODY>\n</HTML>\n");
02048 fclose(htmlfile);
02049 ast_cli(fd, "AGI HTML Commands Dumped to: %s\n", argv[2]);
02050 return RESULT_SUCCESS;
02051 }
02052
02053 static int agi_exec_full(struct ast_channel *chan, void *data, int enhanced, int dead)
02054 {
02055 enum agi_result res;
02056 struct ast_module_user *u;
02057 char *argv[MAX_ARGS];
02058 char buf[AGI_BUF_LEN] = "";
02059 char *tmp = (char *)buf;
02060 int argc = 0;
02061 int fds[2];
02062 int efd = -1;
02063 int pid;
02064 char *stringp;
02065 AGI agi;
02066
02067 if (ast_strlen_zero(data)) {
02068 ast_log(LOG_WARNING, "AGI requires an argument (script)\n");
02069 return -1;
02070 }
02071 ast_copy_string(buf, data, sizeof(buf));
02072
02073 memset(&agi, 0, sizeof(agi));
02074 while ((stringp = strsep(&tmp, "|")) && argc < MAX_ARGS-1)
02075 argv[argc++] = stringp;
02076 argv[argc] = NULL;
02077
02078 u = ast_module_user_add(chan);
02079 #if 0
02080
02081 if (chan->_state != AST_STATE_UP) {
02082 if (ast_answer(chan)) {
02083 LOCAL_USER_REMOVE(u);
02084 return -1;
02085 }
02086 }
02087 #endif
02088 ast_replace_sigchld();
02089 res = launch_script(argv[0], argv, fds, enhanced ? &efd : NULL, &pid);
02090 if (res == AGI_RESULT_SUCCESS || res == AGI_RESULT_SUCCESS_FAST) {
02091 int status = 0;
02092 agi.fd = fds[1];
02093 agi.ctrl = fds[0];
02094 agi.audio = efd;
02095 agi.fast = (res == AGI_RESULT_SUCCESS_FAST) ? 1 : 0;
02096 res = run_agi(chan, argv[0], &agi, pid, &status, dead, argc, argv);
02097
02098 if ((res == AGI_RESULT_SUCCESS || res == AGI_RESULT_SUCCESS_FAST) && status)
02099 res = AGI_RESULT_FAILURE;
02100 if (fds[1] != fds[0])
02101 close(fds[1]);
02102 if (efd > -1)
02103 close(efd);
02104 }
02105 ast_unreplace_sigchld();
02106 ast_module_user_remove(u);
02107
02108 switch (res) {
02109 case AGI_RESULT_SUCCESS:
02110 case AGI_RESULT_SUCCESS_FAST:
02111 pbx_builtin_setvar_helper(chan, "AGISTATUS", "SUCCESS");
02112 break;
02113 case AGI_RESULT_FAILURE:
02114 pbx_builtin_setvar_helper(chan, "AGISTATUS", "FAILURE");
02115 break;
02116 case AGI_RESULT_HANGUP:
02117 pbx_builtin_setvar_helper(chan, "AGISTATUS", "HANGUP");
02118 return -1;
02119 }
02120
02121 return 0;
02122 }
02123
02124 static int agi_exec(struct ast_channel *chan, void *data)
02125 {
02126 if (chan->_softhangup)
02127 ast_log(LOG_WARNING, "If you want to run AGI on hungup channels you should use DeadAGI!\n");
02128 return agi_exec_full(chan, data, 0, 0);
02129 }
02130
02131 static int eagi_exec(struct ast_channel *chan, void *data)
02132 {
02133 int readformat;
02134 int res;
02135
02136 if (chan->_softhangup)
02137 ast_log(LOG_WARNING, "If you want to run AGI on hungup channels you should use DeadAGI!\n");
02138 readformat = chan->readformat;
02139 if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
02140 ast_log(LOG_WARNING, "Unable to set channel '%s' to linear mode\n", chan->name);
02141 return -1;
02142 }
02143 res = agi_exec_full(chan, data, 1, 0);
02144 if (!res) {
02145 if (ast_set_read_format(chan, readformat)) {
02146 ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %s\n", chan->name, ast_getformatname(readformat));
02147 }
02148 }
02149 return res;
02150 }
02151
02152 static int deadagi_exec(struct ast_channel *chan, void *data)
02153 {
02154 if (!ast_check_hangup(chan))
02155 ast_log(LOG_WARNING,"Running DeadAGI on a live channel will cause problems, please use AGI\n");
02156 return agi_exec_full(chan, data, 0, 1);
02157 }
02158
02159 static char showagi_help[] =
02160 "Usage: agi show [topic]\n"
02161 " When called with a topic as an argument, displays usage\n"
02162 " information on the given command. If called without a\n"
02163 " topic, it provides a list of AGI commands.\n";
02164
02165
02166 static char dumpagihtml_help[] =
02167 "Usage: agi dumphtml <filename>\n"
02168 " Dumps the agi command list in html format to given filename\n";
02169
02170 static struct ast_cli_entry cli_show_agi_deprecated = {
02171 { "show", "agi", NULL },
02172 handle_showagi, NULL,
02173 NULL };
02174
02175 static struct ast_cli_entry cli_dump_agihtml_deprecated = {
02176 { "dump", "agihtml", NULL },
02177 handle_agidumphtml, NULL,
02178 NULL };
02179
02180 static struct ast_cli_entry cli_agi_no_debug_deprecated = {
02181 { "agi", "no", "debug", NULL },
02182 agi_no_debug_deprecated, NULL,
02183 NULL };
02184
02185 static struct ast_cli_entry cli_agi[] = {
02186 { { "agi", "debug", NULL },
02187 agi_do_debug, "Enable AGI debugging",
02188 debug_usage },
02189
02190 { { "agi", "debug", "off", NULL },
02191 agi_no_debug, "Disable AGI debugging",
02192 no_debug_usage, NULL, &cli_agi_no_debug_deprecated },
02193
02194 { { "agi", "show", NULL },
02195 handle_showagi, "List AGI commands or specific help",
02196 showagi_help, NULL, &cli_show_agi_deprecated },
02197
02198 { { "agi", "dumphtml", NULL },
02199 handle_agidumphtml, "Dumps a list of agi commands in html format",
02200 dumpagihtml_help, NULL, &cli_dump_agihtml_deprecated },
02201 };
02202
02203 static int unload_module(void)
02204 {
02205 ast_module_user_hangup_all();
02206 ast_cli_unregister_multiple(cli_agi, sizeof(cli_agi) / sizeof(struct ast_cli_entry));
02207 ast_unregister_application(eapp);
02208 ast_unregister_application(deadapp);
02209 return ast_unregister_application(app);
02210 }
02211
02212 static int load_module(void)
02213 {
02214 ast_cli_register_multiple(cli_agi, sizeof(cli_agi) / sizeof(struct ast_cli_entry));
02215 ast_register_application(deadapp, deadagi_exec, deadsynopsis, descrip);
02216 ast_register_application(eapp, eagi_exec, esynopsis, descrip);
02217 return ast_register_application(app, agi_exec, synopsis, descrip);
02218 }
02219
02220 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Asterisk Gateway Interface (AGI)",
02221 .load = load_module,
02222 .unload = unload_module,
02223 );