2 * Asterisk -- A telephony toolkit for Linux.
4 * Asterisk Gateway Interface
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/pbx.h>
18 #include <asterisk/module.h>
19 #include <asterisk/astdb.h>
25 #include <sys/signal.h>
30 #include <asterisk/cli.h>
31 #include <asterisk/logger.h>
32 #include <asterisk/options.h>
33 #include <asterisk/image.h>
34 #include <asterisk/say.h>
35 #include <asterisk/app.h>
36 #include "../asterisk.h"
37 #include "../astconf.h"
43 /* Recycle some stuff from the CLI interface */
44 #define fdprintf ast_cli
46 typedef struct agi_state {
47 int fd; /* FD for general output */
48 int audio; /* FD for audio output */
49 int ctrl; /* FD for input control */
52 typedef struct agi_command {
53 /* Null terminated list of the words of the command */
54 char *cmda[AST_MAX_CMD_LEN];
55 /* Handler for the command (channel, AGI state, # of arguments, argument list).
56 Returns RESULT_SHOWUSAGE for improper arguments */
57 int (*handler)(struct ast_channel *chan, AGI *agi, int argc, char *argv[]);
58 /* Summary of the command (< 60 characters) */
60 /* Detailed usage information */
64 static char *tdesc = "Asterisk Gateway Interface (AGI)";
66 static char *app = "AGI";
68 static char *eapp = "EAGI";
70 static char *synopsis = "Executes an AGI compliant application";
72 static char *descrip =
73 " [E]AGI(command|args): Executes an Asterisk Gateway Interface compliant\n"
74 "program on a channel. AGI allows Asterisk to launch external programs\n"
75 "written in any language to control a telephony channel, play audio,\n"
76 "read DTMF digits, etc. by communicating with the AGI protocol on stdin\n"
77 "and stdout. Returns -1 on hangup or if application requested hangup, or\n"
78 "0 on non-hangup exit. Using 'EAGI' provides enhanced AGI, with audio\n"
79 "available out of band on file descriptor 3\n";
86 #define TONE_BLOCK_SIZE 200
88 static int launch_script(char *script, char *args, int *fds, int *efd, int *opid)
97 if (script[0] != '/') {
98 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_AGI_DIR, script);
102 ast_log(LOG_WARNING, "Unable to create toast pipe: %s\n",strerror(errno));
106 ast_log(LOG_WARNING, "unable to create fromast pipe: %s\n", strerror(errno));
113 ast_log(LOG_WARNING, "unable to create audio pipe: %s\n", strerror(errno));
120 res = fcntl(audio[1], F_GETFL);
122 res = fcntl(audio[1], F_SETFL, res | O_NONBLOCK);
124 ast_log(LOG_WARNING, "unable to set audio pipe parameters: %s\n", strerror(errno));
136 ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno));
140 /* Redirect stdin and out, provide enhanced audio channel if desired */
141 dup2(fromast[0], STDIN_FILENO);
142 dup2(toast[1], STDOUT_FILENO);
144 dup2(audio[0], STDERR_FILENO + 1);
146 close(STDERR_FILENO + 1);
148 /* Close everything but stdin/out/error */
149 for (x=STDERR_FILENO + 2;x<1024;x++)
152 execl(script, script, args, NULL);
153 /* Can't use ast_log since FD's are closed */
154 fprintf(stderr, "Failed to execute '%s': %s\n", script, strerror(errno));
157 if (option_verbose > 2)
158 ast_verbose(VERBOSE_PREFIX_3 "Launched AGI Script %s\n", script);
164 /* close what we're not using in the parent */
172 static void setup_env(struct ast_channel *chan, char *request, int fd, int enhanced)
174 /* Print initial environment, with agi_request always being the first
176 fdprintf(fd, "agi_request: %s\n", request);
177 fdprintf(fd, "agi_channel: %s\n", chan->name);
178 fdprintf(fd, "agi_language: %s\n", chan->language);
179 fdprintf(fd, "agi_type: %s\n", chan->type);
182 fdprintf(fd, "agi_callerid: %s\n", chan->callerid ? chan->callerid : "");
183 fdprintf(fd, "agi_dnid: %s\n", chan->dnid ? chan->dnid : "");
184 fdprintf(fd, "agi_rdnis: %s\n", chan->rdnis ? chan->rdnis : "");
186 /* Context information */
187 fdprintf(fd, "agi_context: %s\n", chan->context);
188 fdprintf(fd, "agi_extension: %s\n", chan->exten);
189 fdprintf(fd, "agi_priority: %d\n", chan->priority);
190 fdprintf(fd, "agi_enhanced: %s\n", enhanced ? "1.0" : "0.0");
192 /* End with empty return */
196 static int handle_answer(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
200 if (chan->_state != AST_STATE_UP) {
201 /* Answer the chan */
202 res = ast_answer(chan);
204 fdprintf(agi->fd, "200 result=%d\n", res);
206 return RESULT_SUCCESS;
208 return RESULT_FAILURE;
211 static int handle_waitfordigit(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
216 return RESULT_SHOWUSAGE;
217 if (sscanf(argv[3], "%i", &to) != 1)
218 return RESULT_SHOWUSAGE;
219 res = ast_waitfordigit_full(chan, to, agi->audio, agi->ctrl);
220 fdprintf(agi->fd, "200 result=%d\n", res);
222 return RESULT_SUCCESS;
224 return RESULT_FAILURE;
227 static int handle_sendtext(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
231 return RESULT_SHOWUSAGE;
232 /* At the moment, the parser (perhaps broken) returns with
233 the last argument PLUS the newline at the end of the input
234 buffer. This probably needs to be fixed, but I wont do that
235 because other stuff may break as a result. The right way
236 would probably be to strip off the trailing newline before
237 parsing, then here, add a newline at the end of the string
238 before sending it to ast_sendtext --DUDE */
239 res = ast_sendtext(chan, argv[2]);
240 fdprintf(agi->fd, "200 result=%d\n", res);
242 return RESULT_SUCCESS;
244 return RESULT_FAILURE;
247 static int handle_recvchar(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
251 return RESULT_SHOWUSAGE;
252 res = ast_recvchar(chan,atoi(argv[2]));
254 fdprintf(agi->fd, "200 result=%d (timeout)\n", res);
255 return RESULT_SUCCESS;
258 fdprintf(agi->fd, "200 result=%d\n", res);
259 return RESULT_SUCCESS;
262 fdprintf(agi->fd, "200 result=%d (hangup)\n", res);
263 return RESULT_FAILURE;
267 static int handle_tddmode(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
271 return RESULT_SHOWUSAGE;
272 if (!strncasecmp(argv[2],"on",2)) x = 1; else x = 0;
273 if (!strncasecmp(argv[2],"mate",4)) x = 2;
274 if (!strncasecmp(argv[2],"tdd",3)) x = 1;
275 res = ast_channel_setoption(chan,AST_OPTION_TDD,&x,sizeof(char),0);
276 fdprintf(agi->fd, "200 result=%d\n", res);
278 return RESULT_SUCCESS;
280 return RESULT_FAILURE;
283 static int handle_sendimage(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
287 return RESULT_SHOWUSAGE;
288 res = ast_send_image(chan, argv[2]);
289 if (!ast_check_hangup(chan))
291 fdprintf(agi->fd, "200 result=%d\n", res);
293 return RESULT_SUCCESS;
295 return RESULT_FAILURE;
298 static int handle_streamfile(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
301 struct ast_filestream *fs;
302 long sample_offset = 0;
306 return RESULT_SHOWUSAGE;
308 return RESULT_SHOWUSAGE;
309 if ((argc > 4) && (sscanf(argv[4], "%ld", &sample_offset) != 1))
310 return RESULT_SHOWUSAGE;
312 fs = ast_openstream(chan, argv[2], chan->language);
314 fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
315 ast_log(LOG_WARNING, "Unable to open %s\n", argv[2]);
316 return RESULT_FAILURE;
318 ast_seekstream(fs, 0, SEEK_END);
319 max_length = ast_tellstream(fs);
320 ast_seekstream(fs, sample_offset, SEEK_SET);
321 res = ast_applystream(chan, fs);
322 res = ast_playstream(fs);
324 fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
326 return RESULT_SHOWUSAGE;
328 return RESULT_FAILURE;
330 res = ast_waitstream_full(chan, argv[3], agi->audio, agi->ctrl);
331 /* this is to check for if ast_waitstream closed the stream, we probably are at
332 * the end of the stream, return that amount, else check for the amount */
333 sample_offset = (chan->stream)?ast_tellstream(fs):max_length;
334 ast_stopstream(chan);
336 /* Stop this command, don't print a result line, as there is a new command */
337 return RESULT_SUCCESS;
339 fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
341 return RESULT_SUCCESS;
343 return RESULT_FAILURE;
346 static int handle_saynumber(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
351 return RESULT_SHOWUSAGE;
352 if (sscanf(argv[2], "%i", &num) != 1)
353 return RESULT_SHOWUSAGE;
354 res = ast_say_number_full(chan, num, argv[3], chan->language, agi->audio, agi->ctrl);
356 return RESULT_SUCCESS;
357 fdprintf(agi->fd, "200 result=%d\n", res);
359 return RESULT_SUCCESS;
361 return RESULT_FAILURE;
364 static int handle_saydigits(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
369 return RESULT_SHOWUSAGE;
370 if (sscanf(argv[2], "%i", &num) != 1)
371 return RESULT_SHOWUSAGE;
372 res = ast_say_digit_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
373 if (res == 1) /* New command */
374 return RESULT_SUCCESS;
375 fdprintf(agi->fd, "200 result=%d\n", res);
377 return RESULT_SUCCESS;
379 return RESULT_FAILURE;
382 static int handle_getdata(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
390 return RESULT_SHOWUSAGE;
391 if (argc >= 4) timeout = atoi(argv[3]); else timeout = 0;
392 if (argc >= 5) max = atoi(argv[4]); else max = 1024;
393 res = ast_app_getdata_full(chan, argv[2], data, max, timeout, agi->audio, agi->ctrl);
394 if (res == 2) /* New command */
395 return RESULT_SUCCESS;
397 fdprintf(agi->fd, "200 result=%s (timeout)\n", data);
399 fdprintf(agi->fd, "200 result=%s\n", data);
401 return RESULT_SUCCESS;
403 return RESULT_FAILURE;
406 static int handle_setcontext(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
410 return RESULT_SHOWUSAGE;
411 strncpy(chan->context, argv[2], sizeof(chan->context)-1);
412 fdprintf(agi->fd, "200 result=0\n");
413 return RESULT_SUCCESS;
416 static int handle_setextension(struct ast_channel *chan, AGI *agi, int argc, char **argv)
419 return RESULT_SHOWUSAGE;
420 strncpy(chan->exten, argv[2], sizeof(chan->exten)-1);
421 fdprintf(agi->fd, "200 result=0\n");
422 return RESULT_SUCCESS;
425 static int handle_setpriority(struct ast_channel *chan, AGI *agi, int argc, char **argv)
429 return RESULT_SHOWUSAGE;
430 if (sscanf(argv[2], "%i", &pri) != 1)
431 return RESULT_SHOWUSAGE;
432 chan->priority = pri - 1;
433 fdprintf(agi->fd, "200 result=0\n");
434 return RESULT_SUCCESS;
437 static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
439 struct ast_filestream *fs;
441 struct timeval tv, start;
442 long sample_offset = 0;
446 /* XXX EAGI FIXME XXX */
449 return RESULT_SHOWUSAGE;
450 if (sscanf(argv[5], "%i", &ms) != 1)
451 return RESULT_SHOWUSAGE;
452 /* backward compatibility, if no offset given, arg[6] would have been
453 * caught below and taken to be a beep, else if it is a digit then it is a
455 if ((argc >6) && (sscanf(argv[6], "%ld", &sample_offset) != 1))
456 res = ast_streamfile(chan, "beep", chan->language);
459 res = ast_streamfile(chan, "beep", chan->language);
461 res = ast_waitstream(chan, argv[4]);
463 fs = ast_writefile(argv[2], argv[3], NULL, O_CREAT | O_WRONLY, 0, 0644);
466 fdprintf(agi->fd, "200 result=%d (writefile)\n", res);
467 return RESULT_FAILURE;
471 ast_applystream(chan,fs);
472 /* really should have checks */
473 ast_seekstream(fs, sample_offset, SEEK_SET);
476 gettimeofday(&start, NULL);
477 gettimeofday(&tv, NULL);
478 while ((ms < 0) || (((tv.tv_sec - start.tv_sec) * 1000 + (tv.tv_usec - start.tv_usec)/1000) < ms)) {
479 res = ast_waitfor(chan, -1);
482 fdprintf(agi->fd, "200 result=%d (waitfor) endpos=%ld\n", res,sample_offset);
483 return RESULT_FAILURE;
487 fdprintf(agi->fd, "200 result=%d (hangup) endpos=%ld\n", 0, sample_offset);
489 return RESULT_FAILURE;
491 switch(f->frametype) {
493 if (strchr(argv[4], f->subclass)) {
494 /* This is an interrupting chracter */
495 sample_offset = ast_tellstream(fs);
496 fdprintf(agi->fd, "200 result=%d (dtmf) endpos=%ld\n", f->subclass, sample_offset);
499 return RESULT_SUCCESS;
502 case AST_FRAME_VOICE:
503 ast_writestream(fs, f);
504 /* this is a safe place to check progress since we know that fs
505 * is valid after a write, and it will then have our current
507 sample_offset = ast_tellstream(fs);
511 gettimeofday(&tv, NULL);
513 fdprintf(agi->fd, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset);
516 fdprintf(agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset);
517 return RESULT_SUCCESS;
520 static int handle_autohangup(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
525 return RESULT_SHOWUSAGE;
526 if (sscanf(argv[2], "%d", &timeout) != 1)
527 return RESULT_SHOWUSAGE;
531 chan->whentohangup = time(NULL) + timeout;
533 chan->whentohangup = 0;
534 fdprintf(agi->fd, "200 result=0\n");
535 return RESULT_SUCCESS;
538 static int handle_hangup(struct ast_channel *chan, AGI *agi, int argc, char **argv)
540 struct ast_channel *c;
542 /* no argument: hangup the current channel */
543 ast_softhangup(chan,AST_SOFTHANGUP_EXPLICIT);
544 fdprintf(agi->fd, "200 result=1\n");
545 return RESULT_SUCCESS;
546 } else if (argc==2) {
547 /* one argument: look for info on the specified channel */
548 c = ast_channel_walk(NULL);
550 if (strcasecmp(argv[1],c->name)==0) {
551 /* we have a matching channel */
552 ast_softhangup(c,AST_SOFTHANGUP_EXPLICIT);
553 fdprintf(agi->fd, "200 result=1\n");
554 return RESULT_SUCCESS;
556 c = ast_channel_walk(c);
558 /* if we get this far no channel name matched the argument given */
559 fdprintf(agi->fd, "200 result=-1\n");
560 return RESULT_SUCCESS;
562 return RESULT_SHOWUSAGE;
566 static int handle_exec(struct ast_channel *chan, AGI *agi, int argc, char **argv)
572 return RESULT_SHOWUSAGE;
574 if (option_verbose > 2)
575 ast_verbose(VERBOSE_PREFIX_3 "AGI Script Executing Application: (%s) Options: (%s)\n", argv[1], argv[2]);
577 app = pbx_findapp(argv[1]);
580 res = pbx_exec(chan, app, argv[2], 1);
582 ast_log(LOG_WARNING, "Could not find application (%s)\n", argv[1]);
585 fdprintf(agi->fd, "200 result=%d\n", res);
590 static int handle_setcallerid(struct ast_channel *chan, AGI *agi, int argc, char **argv)
593 ast_set_callerid(chan, argv[2], 0);
595 /* strncpy(chan->callerid, argv[2], sizeof(chan->callerid)-1);
596 */ fdprintf(agi->fd, "200 result=1\n");
597 return RESULT_SUCCESS;
600 static int handle_channelstatus(struct ast_channel *chan, AGI *agi, int argc, char **argv)
602 struct ast_channel *c;
604 /* no argument: supply info on the current channel */
605 fdprintf(agi->fd, "200 result=%d\n", chan->_state);
606 return RESULT_SUCCESS;
607 } else if (argc==3) {
608 /* one argument: look for info on the specified channel */
609 c = ast_channel_walk(NULL);
611 if (strcasecmp(argv[2],c->name)==0) {
612 fdprintf(agi->fd, "200 result=%d\n", c->_state);
613 return RESULT_SUCCESS;
615 c = ast_channel_walk(c);
617 /* if we get this far no channel name matched the argument given */
618 fdprintf(agi->fd, "200 result=-1\n");
619 return RESULT_SUCCESS;
621 return RESULT_SHOWUSAGE;
625 static int handle_setvariable(struct ast_channel *chan, AGI *agi, int argc, char **argv)
628 pbx_builtin_setvar_helper(chan, argv[2], argv[3]);
630 fdprintf(agi->fd, "200 result=1\n");
631 return RESULT_SUCCESS;
634 static int handle_getvariable(struct ast_channel *chan, AGI *agi, int argc, char **argv)
638 if ((tempstr = pbx_builtin_getvar_helper(chan, argv[2])) )
639 fdprintf(agi->fd, "200 result=1 (%s)\n", tempstr);
641 fdprintf(agi->fd, "200 result=0\n");
643 return RESULT_SUCCESS;
646 static int handle_verbose(struct ast_channel *chan, AGI *agi, int argc, char **argv)
652 return RESULT_SHOWUSAGE;
655 sscanf(argv[2], "%d", &level);
659 prefix = VERBOSE_PREFIX_4;
662 prefix = VERBOSE_PREFIX_3;
665 prefix = VERBOSE_PREFIX_2;
669 prefix = VERBOSE_PREFIX_1;
673 if (level <= option_verbose)
674 ast_verbose("%s %s: %s\n", prefix, chan->data, argv[1]);
676 fdprintf(agi->fd, "200 result=1\n");
678 return RESULT_SUCCESS;
681 static int handle_dbget(struct ast_channel *chan, AGI *agi, int argc, char **argv)
686 return RESULT_SHOWUSAGE;
687 res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp));
689 fdprintf(agi->fd, "200 result=0\n");
691 fdprintf(agi->fd, "200 result=1 (%s)\n", tmp);
693 return RESULT_SUCCESS;
696 static int handle_dbput(struct ast_channel *chan, AGI *agi, int argc, char **argv)
700 return RESULT_SHOWUSAGE;
701 res = ast_db_put(argv[2], argv[3], argv[4]);
703 fdprintf(agi->fd, "200 result=0\n");
705 fdprintf(agi->fd, "200 result=1\n");
707 return RESULT_SUCCESS;
710 static int handle_dbdel(struct ast_channel *chan, AGI *agi, int argc, char **argv)
714 return RESULT_SHOWUSAGE;
715 res = ast_db_del(argv[2], argv[3]);
717 fdprintf(agi->fd, "200 result=0\n");
719 fdprintf(agi->fd, "200 result=1\n");
721 return RESULT_SUCCESS;
724 static int handle_dbdeltree(struct ast_channel *chan, AGI *agi, int argc, char **argv)
727 if ((argc < 3) || (argc > 4))
728 return RESULT_SHOWUSAGE;
730 res = ast_db_deltree(argv[2], argv[3]);
732 res = ast_db_deltree(argv[2], NULL);
735 fdprintf(agi->fd, "200 result=0\n");
737 fdprintf(agi->fd, "200 result=1\n");
738 return RESULT_SUCCESS;
741 static int handle_noop(struct ast_channel *chan, AGI *agi, int arg, char *argv[])
743 fdprintf(agi->fd, "200 result=0\n");
744 return RESULT_SUCCESS;
747 static char usage_dbput[] =
748 " Usage: DATABASE PUT <family> <key> <value>\n"
749 " Adds or updates an entry in the Asterisk database for a\n"
750 " given family, key, and value.\n"
751 " Returns 1 if succesful, 0 otherwise\n";
753 static char usage_dbget[] =
754 " Usage: DATABASE GET <family> <key>\n"
755 " Retrieves an entry in the Asterisk database for a\n"
756 " given family and key.\n"
757 " Returns 0 if <key> is not set. Returns 1 if <key>\n"
758 " is set and returns the variable in parenthesis\n"
759 " example return code: 200 result=1 (testvariable)\n";
761 static char usage_dbdel[] =
762 " Usage: DATABASE DEL <family> <key>\n"
763 " Deletes an entry in the Asterisk database for a\n"
764 " given family and key.\n"
765 " Returns 1 if succesful, 0 otherwise\n";
767 static char usage_dbdeltree[] =
768 " Usage: DATABASE DELTREE <family> [keytree]\n"
769 " Deletes a family or specific keytree withing a family\n"
770 " in the Asterisk database.\n"
771 " Returns 1 if succesful, 0 otherwise\n";
773 static char usage_verbose[] =
774 " Usage: VERBOSE <message> <level>\n"
775 " Sends <message> to the console via verbose message system.\n"
776 " <level> is the the verbose level (1-4)\n"
777 " Always returns 1\n";
779 static char usage_getvariable[] =
780 " Usage: GET VARIABLE <variablename>\n"
781 " Returns 0 if <variablename> is not set. Returns 1 if <variablename>\n"
782 " is set and returns the variable in parenthesis\n"
783 " example return code: 200 result=1 (testvariable)\n";
785 static char usage_setvariable[] =
786 " Usage: SET VARIABLE <variablename> <value>\n";
788 static char usage_channelstatus[] =
789 " Usage: CHANNEL STATUS [<channelname>]\n"
790 " Returns the status of the specified channel.\n"
791 " If no channel name is given the returns the status of the\n"
792 " current channel.\n"
794 " 0 Channel is down and available\n"
795 " 1 Channel is down, but reserved\n"
796 " 2 Channel is off hook\n"
797 " 3 Digits (or equivalent) have been dialed\n"
798 " 4 Line is ringing\n"
799 " 5 Remote end is ringing\n"
803 static char usage_setcallerid[] =
804 " Usage: SET CALLERID <number>\n"
805 " Changes the callerid of the current channel.\n";
807 static char usage_exec[] =
808 " Usage: EXEC <application> <options>\n"
809 " Executes <application> with given <options>.\n"
810 " Returns whatever the application returns, or -2 on failure to find application\n";
812 static char usage_hangup[] =
813 " Usage: HANGUP [<channelname>]\n"
814 " Hangs up the specified channel.\n"
815 " If no channel name is given, hangs up the current channel\n";
817 static char usage_answer[] =
819 " Answers channel if not already in answer state. Returns -1 on\n"
820 " channel failure, or 0 if successful.\n";
822 static char usage_waitfordigit[] =
823 " Usage: WAIT FOR DIGIT <timeout>\n"
824 " Waits up to 'timeout' milliseconds for channel to receive a DTMF digit.\n"
825 " Returns -1 on channel failure, 0 if no digit is received in the timeout, or\n"
826 " the numerical value of the ascii of the digit if one is received. Use -1\n"
827 " for the timeout value if you desire the call to block indefinitely.\n";
829 static char usage_sendtext[] =
830 " Usage: SEND TEXT \"<text to send>\"\n"
831 " Sends the given text on a channel. Most channels do not support the\n"
832 " transmission of text. Returns 0 if text is sent, or if the channel does not\n"
833 " support text transmission. Returns -1 only on error/hangup. Text\n"
834 " consisting of greater than one word should be placed in quotes since the\n"
835 " command only accepts a single argument.\n";
837 static char usage_recvchar[] =
838 " Usage: RECEIVE CHAR <timeout>\n"
839 " Receives a character of text on a channel. Specify timeout to be the\n"
840 " maximum time to wait for input in milliseconds, or 0 for infinite. Most channels\n"
841 " do not support the reception of text. Returns the decimal value of the character\n"
842 " if one is received, or 0 if the channel does not support text reception. Returns\n"
843 " -1 only on error/hangup.\n";
845 static char usage_tddmode[] =
846 " Usage: TDD MODE <on|off>\n"
847 " Enable/Disable TDD transmission/reception on a channel. Returns 1 if\n"
848 " successful, or 0 if channel is not TDD-capable.\n";
850 static char usage_sendimage[] =
851 " Usage: SEND IMAGE <image>\n"
852 " Sends the given image on a channel. Most channels do not support the\n"
853 " transmission of images. Returns 0 if image is sent, or if the channel does not\n"
854 " support image transmission. Returns -1 only on error/hangup. Image names\n"
855 " should not include extensions.\n";
857 static char usage_streamfile[] =
858 " Usage: STREAM FILE <filename> <escape digits> [sample offset]\n"
859 " Send the given file, allowing playback to be interrupted by the given\n"
860 " digits, if any. Use double quotes for the digits if you wish none to be\n"
861 " permitted. If sample offset is provided then the audio will seek to sample\n"
862 " offset before play starts. Returns 0 if playback completes without a digit\n"
863 " being pressed, or the ASCII numerical value of the digit if one was pressed,\n"
864 " or -1 on error or if the channel was disconnected. Remember, the file\n"
865 " extension must not be included in the filename.\n";
867 static char usage_saynumber[] =
868 " Usage: SAY NUMBER <number> <escape digits>\n"
869 " Say a given number, returning early if any of the given DTMF digits\n"
870 " are received on the channel. Returns 0 if playback completes without a digit\n"
871 " being pressed, or the ASCII numerical value of the digit if one was pressed or\n"
872 " -1 on error/hangup.\n";
874 static char usage_saydigits[] =
875 " Usage: SAY DIGITS <number> <escape digits>\n"
876 " Say a given digit string, returning early if any of the given DTMF digits\n"
877 " are received on the channel. Returns 0 if playback completes without a digit\n"
878 " being pressed, or the ASCII numerical value of the digit if one was pressed or\n"
879 " -1 on error/hangup.\n";
881 static char usage_getdata[] =
882 " Usage: GET DATA <file to be streamed> [timeout] [max digits]\n"
883 " Stream the given file, and recieve DTMF data. Returns the digits recieved\n"
884 "from the channel at the other end.\n";
886 static char usage_setcontext[] =
887 " Usage: SET CONTEXT <desired context>\n"
888 " Sets the context for continuation upon exiting the application.\n";
890 static char usage_setextension[] =
891 " Usage: SET EXTENSION <new extension>\n"
892 " Changes the extension for continuation upon exiting the application.\n";
894 static char usage_setpriority[] =
895 " Usage: SET PRIORITY <num>\n"
896 " Changes the priority for continuation upon exiting the application.\n";
898 static char usage_recordfile[] =
899 " Usage: RECORD FILE <filename> <format> <escape digits> <timeout> [offset samples] [BEEP]\n"
900 " Record to a file until a given dtmf digit in the sequence is received\n"
901 " Returns -1 on hangup or error. The format will specify what kind of file\n"
902 " will be recorded. The timeout is the maximum record time in milliseconds, or\n"
903 " -1 for no timeout. Offset samples is optional, and if provided will seek to\n"
904 " the offset without exceeding the end of the file\n";
906 static char usage_autohangup[] =
907 " Usage: SET AUTOHANGUP <time>\n"
908 " Cause the channel to automatically hangup at <time> seconds in the\n"
909 "future. Of course it can be hungup before then as well. Setting to\n"
910 "0 will cause the autohangup feature to be disabled on this channel.\n";
912 static char usage_noop[] =
916 agi_command commands[] = {
917 { { "answer", NULL }, handle_answer, "Asserts answer", usage_answer },
918 { { "wait", "for", "digit", NULL }, handle_waitfordigit, "Waits for a digit to be pressed", usage_waitfordigit },
919 { { "send", "text", NULL }, handle_sendtext, "Sends text to channels supporting it", usage_sendtext },
920 { { "receive", "char", NULL }, handle_recvchar, "Receives text from channels supporting it", usage_recvchar },
921 { { "tdd", "mode", NULL }, handle_tddmode, "Sends text to channels supporting it", usage_tddmode },
922 { { "stream", "file", NULL }, handle_streamfile, "Sends audio file on channel", usage_streamfile },
923 { { "send", "image", NULL }, handle_sendimage, "Sends images to channels supporting it", usage_sendimage },
924 { { "say", "digits", NULL }, handle_saydigits, "Says a given digit string", usage_saydigits },
925 { { "say", "number", NULL }, handle_saynumber, "Says a given number", usage_saynumber },
926 { { "get", "data", NULL }, handle_getdata, "Gets data on a channel", usage_getdata },
927 { { "set", "context", NULL }, handle_setcontext, "Sets channel context", usage_setcontext },
928 { { "set", "extension", NULL }, handle_setextension, "Changes channel extension", usage_setextension },
929 { { "set", "priority", NULL }, handle_setpriority, "Prioritizes the channel", usage_setpriority },
930 { { "record", "file", NULL }, handle_recordfile, "Records to a given file", usage_recordfile },
931 { { "set", "autohangup", NULL }, handle_autohangup, "Autohangup channel in some time", usage_autohangup },
932 { { "hangup", NULL }, handle_hangup, "Hangup the current channel", usage_hangup },
933 { { "exec", NULL }, handle_exec, "Executes a given Application", usage_exec },
934 { { "set", "callerid", NULL }, handle_setcallerid, "Sets callerid for the current channel", usage_setcallerid },
935 { { "channel", "status", NULL }, handle_channelstatus, "Returns status of the connected channel", usage_channelstatus },
936 { { "set", "variable", NULL }, handle_setvariable, "Sets a channel variable", usage_setvariable },
937 { { "get", "variable", NULL }, handle_getvariable, "Gets a channel variable", usage_getvariable },
938 { { "verbose", NULL }, handle_verbose, "Logs a message to the asterisk verbose log", usage_verbose },
939 { { "database", "get", NULL }, handle_dbget, "Gets database value", usage_dbget },
940 { { "database", "put", NULL }, handle_dbput, "Adds/updates database value", usage_dbput },
941 { { "database", "del", NULL }, handle_dbdel, "Removes database key/value", usage_dbdel },
942 { { "database", "deltree", NULL }, handle_dbdeltree, "Removes database keytree/value", usage_dbdeltree },
943 { { "noop", NULL }, handle_noop, "Does nothing", usage_noop }
946 static void join(char *s, int len, char *w[])
949 /* Join words into a string */
953 strncat(s, " ", len - strlen(s));
954 strncat(s, w[x], len - strlen(s));
958 static int help_workhorse(int fd, char *match[])
963 struct agi_command *e;
965 join(matchstr, sizeof(matchstr), match);
966 for (x=0;x<sizeof(commands)/sizeof(commands[0]);x++) {
969 join(fullcmd, sizeof(fullcmd), e->cmda);
970 /* Hide commands that start with '_' */
971 if (fullcmd[0] == '_')
974 if (strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
978 ast_cli(fd, "%20.20s %s\n", fullcmd, e->summary);
983 static agi_command *find_command(char *cmds[], int exact)
988 for (x=0;x < sizeof(commands) / sizeof(commands[0]);x++) {
989 /* start optimistic */
991 for (y=0;match && cmds[y]; y++) {
992 /* If there are no more words in the command (and we're looking for
993 an exact match) or there is a difference between the two words,
994 then this is not a match */
995 if (!commands[x].cmda[y] && !exact)
997 /* don't segfault if the next part of a command doesn't exist */
998 if (!commands[x].cmda[y]) return NULL;
999 if (strcasecmp(commands[x].cmda[y], cmds[y]))
1002 /* If more words are needed to complete the command then this is not
1003 a candidate (unless we're looking for a really inexact answer */
1004 if ((exact > -1) && commands[x].cmda[y])
1007 return &commands[x];
1013 static int parse_args(char *s, int *max, char *argv[])
1025 /* If it's escaped, put a literal quote */
1030 if (quoted && whitespace) {
1031 /* If we're starting a quote, coming off white space start a new word, too */
1039 if (!quoted && !escaped) {
1040 /* If we're not quoted, mark this as whitespace, and
1041 end the previous argument */
1045 /* Otherwise, just treat it as anything else */
1049 /* If we're escaped, print a literal, otherwise enable escaping */
1059 if (x >= MAX_ARGS -1) {
1060 ast_log(LOG_WARNING, "Too many arguments, truncating\n");
1063 /* Coming off of whitespace, start the next argument */
1072 /* Null terminate */
1079 static int agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf)
1081 char *argv[MAX_ARGS];
1086 parse_args(buf, &argc, argv);
1089 for (x=0;x<argc;x++)
1090 fprintf(stderr, "Got Arg%d: %s\n", x, argv[x]); }
1092 c = find_command(argv, 0);
1094 res = c->handler(chan, agi, argc, argv);
1096 case RESULT_SHOWUSAGE:
1097 fdprintf(agi->fd, "520-Invalid command syntax. Proper usage follows:\n");
1098 fdprintf(agi->fd, c->usage);
1099 fdprintf(agi->fd, "520 End of proper usage.\n");
1101 case RESULT_FAILURE:
1102 /* They've already given the failure. We've been hung up on so handle this
1107 fdprintf(agi->fd, "510 Invalid or unknown command\n");
1112 static int run_agi(struct ast_channel *chan, char *request, AGI *agi, int pid)
1114 struct ast_channel *c;
1117 int returnstatus = 0;
1118 struct ast_frame *f;
1121 if (!(readf = fdopen(agi->ctrl, "r"))) {
1122 ast_log(LOG_WARNING, "Unable to fdopen file descriptor\n");
1127 setup_env(chan, request, agi->fd, (agi->audio > -1));
1130 c = ast_waitfor_nandfds(&chan, 1, &agi->ctrl, 1, NULL, &outfd, &ms);
1132 /* Idle the channel until we get a command */
1135 ast_log(LOG_DEBUG, "%s hungup\n", chan->name);
1139 /* If it's voice, write it to the audio pipe */
1140 if ((agi->audio > -1) && (f->frametype == AST_FRAME_VOICE)) {
1141 /* Write, ignoring errors */
1142 write(agi->audio, f->data, f->datalen);
1146 } else if (outfd > -1) {
1147 if (!fgets(buf, sizeof(buf), readf)) {
1148 /* Program terminated */
1151 if (option_verbose > 2)
1152 ast_verbose(VERBOSE_PREFIX_3 "AGI Script %s completed, returning %d\n", request, returnstatus);
1153 /* No need to kill the pid anymore, since they closed us */
1157 /* get rid of trailing newline, if any */
1158 if (*buf && buf[strlen(buf) - 1] == '\n')
1159 buf[strlen(buf) - 1] = 0;
1161 returnstatus |= agi_handle_command(chan, agi, buf);
1162 /* If the handle_command returns -1, we need to stop */
1163 if (returnstatus < 0) {
1167 ast_log(LOG_WARNING, "No channel, no fd?\n");
1172 /* Notify process */
1176 return returnstatus;
1179 static int handle_showagi(int fd, int argc, char *argv[]) {
1180 struct agi_command *e;
1183 return RESULT_SHOWUSAGE;
1185 e = find_command(argv + 2, 1);
1187 ast_cli(fd, e->usage);
1189 if (find_command(argv + 2, -1)) {
1190 return help_workhorse(fd, argv + 1);
1192 join(fullcmd, sizeof(fullcmd), argv+1);
1193 ast_cli(fd, "No such command '%s'.\n", fullcmd);
1197 return help_workhorse(fd, NULL);
1199 return RESULT_SUCCESS;
1202 static int handle_dumpagihtml(int fd, int argc, char *argv[]) {
1203 struct agi_command *e;
1210 return RESULT_SHOWUSAGE;
1212 if (!(htmlfile = fopen(argv[2], "wt"))) {
1213 ast_cli(fd, "Could not create file '%s'\n", argv[2]);
1214 return RESULT_SHOWUSAGE;
1217 fprintf(htmlfile, "<HTML>\n<HEAD>\n<TITLE>AGI Commands</TITLE>\n</HEAD>\n");
1218 fprintf(htmlfile, "<BODY>\n<CENTER><B><H1>AGI Commands</H1></B></CENTER>\n\n");
1221 fprintf(htmlfile, "<TABLE BORDER=\"0\" CELLSPACING=\"10\">\n");
1223 for (x=0;x<sizeof(commands)/sizeof(commands[0]);x++) {
1227 join(fullcmd, sizeof(fullcmd), e->cmda);
1228 /* Hide commands that start with '_' */
1229 if (fullcmd[0] == '_')
1232 fprintf(htmlfile, "<TR><TD><TABLE BORDER=\"1\" CELLPADDING=\"5\" WIDTH=\"100%%\">\n");
1233 fprintf(htmlfile, "<TR><TH ALIGN=\"CENTER\"><B>%s - %s</B></TD></TR>\n", fullcmd,e->summary);
1237 tempstr = strsep(&stringp, "\n");
1239 fprintf(htmlfile, "<TR><TD ALIGN=\"CENTER\">%s</TD></TR>\n", tempstr);
1241 fprintf(htmlfile, "<TR><TD ALIGN=\"CENTER\">\n");
1242 while ((tempstr = strsep(&stringp, "\n")) != NULL) {
1243 fprintf(htmlfile, "%s<BR>\n",tempstr);
1246 fprintf(htmlfile, "</TD></TR>\n");
1247 fprintf(htmlfile, "</TABLE></TD></TR>\n\n");
1251 fprintf(htmlfile, "</TABLE>\n</BODY>\n</HTML>\n");
1253 ast_cli(fd, "AGI HTML Commands Dumped to: %s\n", argv[2]);
1254 return RESULT_SUCCESS;
1257 static int agi_exec_full(struct ast_channel *chan, void *data, int enhanced)
1260 struct localuser *u;
1268 if (!data || !strlen(data)) {
1269 ast_log(LOG_WARNING, "AGI requires an argument (script)\n");
1274 memset(&agi, 0, sizeof(agi));
1275 strncpy(tmp, data, sizeof(tmp)-1);
1276 strsep(&stringp, "|");
1277 args = strsep(&stringp, "|");
1278 ringy = strsep(&stringp,"|");
1283 /* Answer if need be */
1284 if (chan->_state != AST_STATE_UP) {
1285 if (ringy) { /* if for ringing first */
1286 /* a little ringy-dingy first */
1287 ast_indicate(chan, AST_CONTROL_RINGING);
1290 if (ast_answer(chan)) {
1291 LOCAL_USER_REMOVE(u);
1296 res = launch_script(tmp, args, fds, enhanced ? &efd : NULL, &pid);
1301 res = run_agi(chan, tmp, &agi, pid);
1307 LOCAL_USER_REMOVE(u);
1311 static int agi_exec(struct ast_channel *chan, void *data)
1313 return agi_exec_full(chan, data, 0);
1316 static int eagi_exec(struct ast_channel *chan, void *data)
1320 readformat = chan->readformat;
1321 if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
1322 ast_log(LOG_WARNING, "Unable to set channel '%s' to linear mode\n", chan->name);
1325 res = agi_exec_full(chan, data, 1);
1327 if (ast_set_read_format(chan, readformat)) {
1328 ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %d\n", chan->name, readformat);
1334 static char showagi_help[] =
1335 "Usage: show agi [topic]\n"
1336 " When called with a topic as an argument, displays usage\n"
1337 " information on the given command. If called without a\n"
1338 " topic, it provides a list of AGI commands.\n";
1341 static char dumpagihtml_help[] =
1342 "Usage: dump agihtml <filename>\n"
1343 " Dumps the agi command list in html format to given filename\n";
1345 struct ast_cli_entry showagi =
1346 { { "show", "agi", NULL }, handle_showagi, "Show AGI commands or specific help", showagi_help };
1348 struct ast_cli_entry dumpagihtml =
1349 { { "dump", "agihtml", NULL }, handle_dumpagihtml, "Dumps a list of agi command in html format", dumpagihtml_help };
1351 int unload_module(void)
1353 STANDARD_HANGUP_LOCALUSERS;
1354 ast_cli_unregister(&showagi);
1355 ast_cli_unregister(&dumpagihtml);
1356 ast_unregister_application(eapp);
1357 return ast_unregister_application(app);
1360 int load_module(void)
1362 ast_cli_register(&showagi);
1363 ast_cli_register(&dumpagihtml);
1364 ast_register_application(eapp, eagi_exec, synopsis, descrip);
1365 return ast_register_application(app, agi_exec, synopsis, descrip);
1368 char *description(void)
1376 STANDARD_USECOUNT(res);
1382 return ASTERISK_GPL_KEY;