Add get option command (bug #2868, thanks junky!)
[asterisk/asterisk.git] / res / res_agi.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Asterisk Gateway Interface
5  * 
6  * Copyright (C) 1999-2004, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <sys/types.h>
15 #include <netdb.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <netinet/tcp.h>
19 #include <arpa/inet.h>
20 #include <asterisk/file.h>
21 #include <asterisk/logger.h>
22 #include <asterisk/channel.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/module.h>
25 #include <asterisk/astdb.h>
26 #include <asterisk/callerid.h>
27 #include <math.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <signal.h>
33 #include <sys/time.h>
34 #include <stdio.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <asterisk/cli.h>
38 #include <asterisk/logger.h>
39 #include <asterisk/options.h>
40 #include <asterisk/image.h>
41 #include <asterisk/say.h>
42 #include <asterisk/app.h>
43 #include <asterisk/dsp.h>
44 #include <asterisk/musiconhold.h>
45 #include <asterisk/manager.h>
46 #include <asterisk/utils.h>
47 #include <asterisk/lock.h>
48 #include <asterisk/agi.h>
49 #include "../asterisk.h"
50 #include "../astconf.h"
51
52 #define MAX_ARGS 128
53 #define MAX_COMMANDS 128
54
55 /* Recycle some stuff from the CLI interface */
56 #define fdprintf agi_debug_cli
57
58 static char *tdesc = "Asterisk Gateway Interface (AGI)";
59
60 static char *app = "AGI";
61
62 static char *eapp = "EAGI";
63
64 static char *deadapp = "DeadAGI";
65
66 static char *synopsis = "Executes an AGI compliant application";
67 static char *esynopsis = "Executes an EAGI compliant application";
68 static char *deadsynopsis = "Executes AGI on a hungup channel";
69
70 static char *descrip =
71 "  [E|Dead]AGI(command|args): Executes an Asterisk Gateway Interface compliant\n"
72 "program on a channel. AGI allows Asterisk to launch external programs\n"
73 "written in any language to control a telephony channel, play audio,\n"
74 "read DTMF digits, etc. by communicating with the AGI protocol on stdin\n"
75 "and stdout.\n"
76 "Returns -1 on hangup (except for DeadAGI) or if application requested\n"
77 " hangup, or 0 on non-hangup exit. \n"
78 "Using 'EAGI' provides enhanced AGI, with incoming audio available out of band"
79 "on file descriptor 3\n\n"
80 "Use the CLI command 'show agi' to list available agi commands\n";
81
82 static int agidebug = 0;
83
84 STANDARD_LOCAL_USER;
85
86 LOCAL_USER_DECL;
87
88
89 #define TONE_BLOCK_SIZE 200
90
91 /* Max time to connect to an AGI remote host */
92 #define MAX_AGI_CONNECT 2000
93
94 #define AGI_PORT 4573
95
96 static void agi_debug_cli(int fd, char *fmt, ...)
97 {
98         char *stuff;
99         int res = 0;
100
101         va_list ap;
102         va_start(ap, fmt);
103         res = vasprintf(&stuff, fmt, ap);
104         va_end(ap);
105         if (res == -1) {
106                 ast_log(LOG_ERROR, "Out of memory\n");
107         } else {
108                 if (agidebug)
109                         ast_verbose("AGI Tx >> %s", stuff);
110                 ast_carefulwrite(fd, stuff, strlen(stuff), 100);
111                 free(stuff);
112         }
113 }
114
115 static int launch_netscript(char *agiurl, char *argv[], int *fds, int *efd, int *opid)
116 {
117         int s;
118         int flags;
119         struct pollfd pfds[1];
120         char *host;
121         char *c; int port = AGI_PORT;
122         char *script;
123         struct sockaddr_in sin;
124         struct hostent *hp;
125         struct ast_hostent ahp;
126         ast_log(LOG_DEBUG, "Blah\n");
127         host = ast_strdupa(agiurl + 6);
128         if (!host)
129                 return -1;
130         /* Strip off any script name */
131         if ((c = strchr(host, '/'))) {
132                 *c = '\0';
133                 c++;
134                 script = c;
135         }
136         if ((c = strchr(host, ':'))) {
137                 *c = '\0';
138                 c++;
139                 port = atoi(c);
140         }
141         if (efd) {
142                 ast_log(LOG_WARNING, "AGI URI's don't support Enhanced AGI yet\n");
143                 return -1;
144         }
145         hp = ast_gethostbyname(host, &ahp);
146         if (!hp) {
147                 ast_log(LOG_WARNING, "Unable to locate host '%s'\n", host);
148                 return -1;
149         }
150         s = socket(AF_INET, SOCK_STREAM, 0);
151         if (s < 0) {
152                 ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
153                 return -1;
154         }
155         flags = fcntl(s, F_GETFL);
156         if (flags < 0) {
157                 ast_log(LOG_WARNING, "Fcntl(F_GETFL) failed: %s\n", strerror(errno));
158                 close(s);
159                 return -1;
160         }
161         if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) {
162                 ast_log(LOG_WARNING, "Fnctl(F_SETFL) failed: %s\n", strerror(errno));
163                 close(s);
164                 return -1;
165         }
166         memset(&sin, 0, sizeof(sin));
167         sin.sin_family = AF_INET;
168         sin.sin_port = htons(port);
169         memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
170         if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) && (errno != EINPROGRESS)) {
171                 ast_log(LOG_WARNING, "Connect failed with unexpected error: %s\n", strerror(errno));
172                 close(s);
173                 return -1;
174         }
175         pfds[0].fd = s;
176         pfds[0].events = POLLOUT;
177         if (poll(pfds, 1, MAX_AGI_CONNECT) != 1) {
178                 ast_log(LOG_WARNING, "Connect to '%s' failed!\n", agiurl);
179                 close(s);
180                 return -1;
181         }
182         if (write(s, "agi_network: yes\n", strlen("agi_network: yes\n")) < 0) {
183                 ast_log(LOG_WARNING, "Connect to '%s' failed: %s\n", agiurl, strerror(errno));
184                 close(s);
185                 return -1;
186         }
187         ast_log(LOG_DEBUG, "Wow, connected!\n");
188         fds[0] = s;
189         fds[1] = s;
190         *opid = -1;
191         return 0;
192 }
193
194 static int launch_script(char *script, char *argv[], int *fds, int *efd, int *opid)
195 {
196         char tmp[256];
197         int pid;
198         int toast[2];
199         int fromast[2];
200         int audio[2];
201         int x;
202         int res;
203         
204         if (!strncasecmp(script, "agi://", 6))
205                 return launch_netscript(script, argv, fds, efd, opid);
206         
207         if (script[0] != '/') {
208                 snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_AGI_DIR, script);
209                 script = tmp;
210         }
211         if (pipe(toast)) {
212                 ast_log(LOG_WARNING, "Unable to create toast pipe: %s\n",strerror(errno));
213                 return -1;
214         }
215         if (pipe(fromast)) {
216                 ast_log(LOG_WARNING, "unable to create fromast pipe: %s\n", strerror(errno));
217                 close(toast[0]);
218                 close(toast[1]);
219                 return -1;
220         }
221         if (efd) {
222                 if (pipe(audio)) {
223                         ast_log(LOG_WARNING, "unable to create audio pipe: %s\n", strerror(errno));
224                         close(fromast[0]);
225                         close(fromast[1]);
226                         close(toast[0]);
227                         close(toast[1]);
228                         return -1;
229                 }
230                 res = fcntl(audio[1], F_GETFL);
231                 if (res > -1) 
232                         res = fcntl(audio[1], F_SETFL, res | O_NONBLOCK);
233                 if (res < 0) {
234                         ast_log(LOG_WARNING, "unable to set audio pipe parameters: %s\n", strerror(errno));
235                         close(fromast[0]);
236                         close(fromast[1]);
237                         close(toast[0]);
238                         close(toast[1]);
239                         close(audio[0]);
240                         close(audio[1]);
241                         return -1;
242                 }
243         }
244         pid = fork();
245         if (pid < 0) {
246                 ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno));
247                 return -1;
248         }
249         if (!pid) {
250                 /* Redirect stdin and out, provide enhanced audio channel if desired */
251                 dup2(fromast[0], STDIN_FILENO);
252                 dup2(toast[1], STDOUT_FILENO);
253                 if (efd) {
254                         dup2(audio[0], STDERR_FILENO + 1);
255                 } else {
256                         close(STDERR_FILENO + 1);
257                 }
258                 /* Close everything but stdin/out/error */
259                 for (x=STDERR_FILENO + 2;x<1024;x++) 
260                         close(x);
261                 /* Execute script */
262                 execv(script, argv);
263                 /* Can't use ast_log since FD's are closed */
264                 fprintf(stderr, "Failed to execute '%s': %s\n", script, strerror(errno));
265                 exit(1);
266         }
267         if (option_verbose > 2) 
268                 ast_verbose(VERBOSE_PREFIX_3 "Launched AGI Script %s\n", script);
269         fds[0] = toast[0];
270         fds[1] = fromast[1];
271         if (efd) {
272                 *efd = audio[1];
273         }
274         /* close what we're not using in the parent */
275         close(toast[1]);
276         close(fromast[0]);
277
278         if (efd) {
279                 // [PHM 12/18/03]
280                 close(audio[0]);
281         }
282
283         *opid = pid;
284         return 0;
285                 
286 }
287
288 static void setup_env(struct ast_channel *chan, char *request, int fd, int enhanced)
289 {
290         /* Print initial environment, with agi_request always being the first
291            thing */
292         fdprintf(fd, "agi_request: %s\n", request);
293         fdprintf(fd, "agi_channel: %s\n", chan->name);
294         fdprintf(fd, "agi_language: %s\n", chan->language);
295         fdprintf(fd, "agi_type: %s\n", chan->type);
296         fdprintf(fd, "agi_uniqueid: %s\n", chan->uniqueid);
297
298         /* ANI/DNIS */
299         fdprintf(fd, "agi_callerid: %s\n", chan->cid.cid_num ? chan->cid.cid_num : "unknown");
300         fdprintf(fd, "agi_calleridname: %s\n", chan->cid.cid_name ? chan->cid.cid_name : "unknown");
301         fdprintf(fd, "agi_dnid: %s\n", chan->cid.cid_dnid ? chan->cid.cid_dnid : "unknown");
302         fdprintf(fd, "agi_rdnis: %s\n", chan->cid.cid_rdnis ? chan->cid.cid_rdnis : "unknown");
303
304         /* Context information */
305         fdprintf(fd, "agi_context: %s\n", chan->context);
306         fdprintf(fd, "agi_extension: %s\n", chan->exten);
307         fdprintf(fd, "agi_priority: %d\n", chan->priority);
308         fdprintf(fd, "agi_enhanced: %s\n", enhanced ? "1.0" : "0.0");
309
310     /* User information */
311     fdprintf(fd, "agi_accountcode: %s\n", chan->accountcode ? chan->accountcode : "");
312     
313         /* End with empty return */
314         fdprintf(fd, "\n");
315 }
316
317 static int handle_answer(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
318 {
319         int res;
320         res = 0;
321         if (chan->_state != AST_STATE_UP) {
322                 /* Answer the chan */
323                 res = ast_answer(chan);
324         }
325         fdprintf(agi->fd, "200 result=%d\n", res);
326         if (res >= 0)
327                 return RESULT_SUCCESS;
328         else
329                 return RESULT_FAILURE;
330 }
331
332 static int handle_waitfordigit(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
333 {
334         int res;
335         int to;
336         if (argc != 4)
337                 return RESULT_SHOWUSAGE;
338         if (sscanf(argv[3], "%i", &to) != 1)
339                 return RESULT_SHOWUSAGE;
340         res = ast_waitfordigit_full(chan, to, agi->audio, agi->ctrl);
341         fdprintf(agi->fd, "200 result=%d\n", res);
342         if (res >= 0)
343                 return RESULT_SUCCESS;
344         else
345                 return RESULT_FAILURE;
346 }
347
348 static int handle_sendtext(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
349 {
350         int res;
351         if (argc != 3)
352                 return RESULT_SHOWUSAGE;
353         /* At the moment, the parser (perhaps broken) returns with
354            the last argument PLUS the newline at the end of the input
355            buffer. This probably needs to be fixed, but I wont do that
356            because other stuff may break as a result. The right way
357            would probably be to strip off the trailing newline before
358            parsing, then here, add a newline at the end of the string
359            before sending it to ast_sendtext --DUDE */
360         res = ast_sendtext(chan, argv[2]);
361         fdprintf(agi->fd, "200 result=%d\n", res);
362         if (res >= 0)
363                 return RESULT_SUCCESS;
364         else
365                 return RESULT_FAILURE;
366 }
367
368 static int handle_recvchar(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
369 {
370         int res;
371         if (argc != 3)
372                 return RESULT_SHOWUSAGE;
373         res = ast_recvchar(chan,atoi(argv[2]));
374         if (res == 0) {
375                 fdprintf(agi->fd, "200 result=%d (timeout)\n", res);
376                 return RESULT_SUCCESS;
377         }
378         if (res > 0) {
379                 fdprintf(agi->fd, "200 result=%d\n", res);
380                 return RESULT_SUCCESS;
381         }
382         else {
383                 fdprintf(agi->fd, "200 result=%d (hangup)\n", res);
384                 return RESULT_FAILURE;
385         }
386 }
387
388 static int handle_tddmode(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
389 {
390         int res,x;
391         if (argc != 3)
392                 return RESULT_SHOWUSAGE;
393         if (!strncasecmp(argv[2],"on",2)) x = 1; else x = 0;
394         if (!strncasecmp(argv[2],"mate",4)) x = 2;
395         if (!strncasecmp(argv[2],"tdd",3)) x = 1;
396         res = ast_channel_setoption(chan,AST_OPTION_TDD,&x,sizeof(char),0);
397         fdprintf(agi->fd, "200 result=%d\n", res);
398         if (res >= 0) 
399                 return RESULT_SUCCESS;
400         else
401                 return RESULT_FAILURE;
402 }
403
404 static int handle_sendimage(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
405 {
406         int res;
407         if (argc != 3)
408                 return RESULT_SHOWUSAGE;
409         res = ast_send_image(chan, argv[2]);
410         if (!ast_check_hangup(chan))
411                 res = 0;
412         fdprintf(agi->fd, "200 result=%d\n", res);
413         if (res >= 0)
414                 return RESULT_SUCCESS;
415         else
416                 return RESULT_FAILURE;
417 }
418
419 static int handle_streamfile(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
420 {
421         int res;
422         struct ast_filestream *fs;
423         long sample_offset = 0;
424         long max_length;
425
426         if (argc < 4)
427                 return RESULT_SHOWUSAGE;
428         if (argc > 5)
429                 return RESULT_SHOWUSAGE;
430         if ((argc > 4) && (sscanf(argv[4], "%ld", &sample_offset) != 1))
431                 return RESULT_SHOWUSAGE;
432         
433         fs = ast_openstream(chan, argv[2], chan->language);
434         if(!fs){
435                 fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
436                 ast_log(LOG_WARNING, "Unable to open %s\n", argv[2]);
437                 return RESULT_FAILURE;
438         }
439         ast_seekstream(fs, 0, SEEK_END);
440         max_length = ast_tellstream(fs);
441         ast_seekstream(fs, sample_offset, SEEK_SET);
442         res = ast_applystream(chan, fs);
443         res = ast_playstream(fs);
444         if (res) {
445                 fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
446                 if (res >= 0)
447                         return RESULT_SHOWUSAGE;
448                 else
449                         return RESULT_FAILURE;
450         }
451         res = ast_waitstream_full(chan, argv[3], agi->audio, agi->ctrl);
452         /* this is to check for if ast_waitstream closed the stream, we probably are at
453          * the end of the stream, return that amount, else check for the amount */
454         sample_offset = (chan->stream)?ast_tellstream(fs):max_length;
455         ast_stopstream(chan);
456         if (res == 1) {
457                 /* Stop this command, don't print a result line, as there is a new command */
458                 return RESULT_SUCCESS;
459         }
460         fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
461         if (res >= 0)
462                 return RESULT_SUCCESS;
463         else
464                 return RESULT_FAILURE;
465 }
466
467 /* get option - really similar to the handle_streamfile, but with a timeout */
468 static int handle_getoption(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
469 {
470         int res;
471         struct ast_filestream *fs;
472         long sample_offset = 0;
473         long max_length;
474         int timeout = 0;
475         char *edigits = NULL;
476
477         if ( argc < 4 || argc > 5 )
478                 return RESULT_SHOWUSAGE;
479
480         if ( argv[3] ) 
481                 edigits = argv[3];
482
483         if ( argc == 5 )
484                 timeout = atoi(argv[4]);
485         else if (chan->pbx->dtimeout) {
486                 /* by default dtimeout is set to 5sec */
487                 timeout = chan->pbx->dtimeout * 1000; //in msec
488         }
489
490         fs = ast_openstream(chan, argv[2], chan->language);
491         if(!fs){
492                 fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
493                 ast_log(LOG_WARNING, "Unable to open %s\n", argv[2]);
494                 return RESULT_FAILURE;
495         }
496         if (option_verbose > 2)
497                 ast_verbose(VERBOSE_PREFIX_3 "Playing '%s' (escape_digits=%s) (timeout %d)\n", argv[2], edigits, timeout);
498
499         ast_seekstream(fs, 0, SEEK_END);
500         max_length = ast_tellstream(fs);
501         ast_seekstream(fs, sample_offset, SEEK_SET);
502         res = ast_applystream(chan, fs);
503         res = ast_playstream(fs);
504         if (res) {
505                 fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
506                 if (res >= 0)
507                         return RESULT_SHOWUSAGE;
508                 else
509                         return RESULT_FAILURE;
510         }
511         res = ast_waitstream_full(chan, argv[3], agi->audio, agi->ctrl);
512         /* this is to check for if ast_waitstream closed the stream, we probably are at
513          * the end of the stream, return that amount, else check for the amount */
514         sample_offset = (chan->stream)?ast_tellstream(fs):max_length;
515         ast_stopstream(chan);
516         if (res == 1) {
517                 /* Stop this command, don't print a result line, as there is a new command */
518                 return RESULT_SUCCESS;
519         }
520
521         /* If the user didnt press a key, wait for digitTimeout*/
522         if (res == 0 ) {
523                 res = ast_waitfordigit_full(chan, timeout, agi->audio, agi->ctrl);
524                 /* Make sure the new result is in the escape digits of the GET OPTION */
525                 if ( !strchr(edigits,res) )
526                         res=0;
527         }
528
529         fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
530         if (res >= 0)
531                 return RESULT_SUCCESS;
532         else
533                 return RESULT_FAILURE;
534 }
535
536
537
538
539 /*--- handle_saynumber: Say number in various language syntaxes ---*/
540 /* Need to add option for gender here as well. Coders wanted */
541 /* While waiting, we're sending a (char *) NULL.  */
542 static int handle_saynumber(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
543 {
544         int res;
545         int num;
546         if (argc != 4)
547                 return RESULT_SHOWUSAGE;
548         if (sscanf(argv[2], "%i", &num) != 1)
549                 return RESULT_SHOWUSAGE;
550         res = ast_say_number_full(chan, num, argv[3], chan->language, (char *) NULL, agi->audio, agi->ctrl);
551         if (res == 1)
552                 return RESULT_SUCCESS;
553         fdprintf(agi->fd, "200 result=%d\n", res);
554         if (res >= 0)
555                 return RESULT_SUCCESS;
556         else
557                 return RESULT_FAILURE;
558 }
559
560 static int handle_saydigits(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
561 {
562         int res;
563         int num;
564
565         if (argc != 4)
566                 return RESULT_SHOWUSAGE;
567         if (sscanf(argv[2], "%i", &num) != 1)
568                 return RESULT_SHOWUSAGE;
569
570         res = ast_say_digit_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
571         if (res == 1) /* New command */
572                 return RESULT_SUCCESS;
573         fdprintf(agi->fd, "200 result=%d\n", res);
574         if (res >= 0)
575                 return RESULT_SUCCESS;
576         else
577                 return RESULT_FAILURE;
578 }
579
580 static int handle_saytime(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
581 {
582         int res;
583         int num;
584         if (argc != 4)
585                 return RESULT_SHOWUSAGE;
586         if (sscanf(argv[2], "%i", &num) != 1)
587                 return RESULT_SHOWUSAGE;
588         res = ast_say_time(chan, num, argv[3], chan->language);
589         if (res == 1)
590                 return RESULT_SUCCESS;
591         fdprintf(agi->fd, "200 result=%d\n", res);
592         if (res >= 0)
593                 return RESULT_SUCCESS;
594         else
595                 return RESULT_FAILURE;
596 }
597
598 static int handle_sayphonetic(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
599 {
600         int res;
601
602         if (argc != 4)
603                 return RESULT_SHOWUSAGE;
604
605         res = ast_say_phonetic_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
606         if (res == 1) /* New command */
607                 return RESULT_SUCCESS;
608         fdprintf(agi->fd, "200 result=%d\n", res);
609         if (res >= 0)
610                 return RESULT_SUCCESS;
611         else
612                 return RESULT_FAILURE;
613 }
614
615 static int handle_getdata(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
616 {
617         int res;
618         char data[1024];
619         int max;
620         int timeout;
621
622         if (argc < 3)
623                 return RESULT_SHOWUSAGE;
624         if (argc >= 4) timeout = atoi(argv[3]); else timeout = 0;
625         if (argc >= 5) max = atoi(argv[4]); else max = 1024;
626         res = ast_app_getdata_full(chan, argv[2], data, max, timeout, agi->audio, agi->ctrl);
627         if (res == 2)                   /* New command */
628                 return RESULT_SUCCESS;
629         else if (res == 1)
630                 fdprintf(agi->fd, "200 result=%s (timeout)\n", data);
631         else if (res < 0 )
632                 fdprintf(agi->fd, "200 result=-1\n");
633         else
634                 fdprintf(agi->fd, "200 result=%s\n", data);
635         if (res >= 0)
636                 return RESULT_SUCCESS;
637         else
638                 return RESULT_FAILURE;
639 }
640
641 static int handle_setcontext(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
642 {
643
644         if (argc != 3)
645                 return RESULT_SHOWUSAGE;
646         strncpy(chan->context, argv[2], sizeof(chan->context)-1);
647         fdprintf(agi->fd, "200 result=0\n");
648         return RESULT_SUCCESS;
649 }
650         
651 static int handle_setextension(struct ast_channel *chan, AGI *agi, int argc, char **argv)
652 {
653         if (argc != 3)
654                 return RESULT_SHOWUSAGE;
655         strncpy(chan->exten, argv[2], sizeof(chan->exten)-1);
656         fdprintf(agi->fd, "200 result=0\n");
657         return RESULT_SUCCESS;
658 }
659
660 static int handle_setpriority(struct ast_channel *chan, AGI *agi, int argc, char **argv)
661 {
662         int pri;
663         if (argc != 3)
664                 return RESULT_SHOWUSAGE;        
665         if (sscanf(argv[2], "%i", &pri) != 1)
666                 return RESULT_SHOWUSAGE;
667         chan->priority = pri - 1;
668         fdprintf(agi->fd, "200 result=0\n");
669         return RESULT_SUCCESS;
670 }
671                 
672 static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
673 {
674         struct ast_filestream *fs;
675         struct ast_frame *f;
676         struct timeval tv, start;
677         long sample_offset = 0;
678         int res = 0;
679         int ms;
680
681         struct ast_dsp *sildet=NULL;         /* silence detector dsp */
682         int totalsilence = 0;
683         int dspsilence = 0;
684         int silence = 0;                /* amount of silence to allow */
685         int gotsilence = 0;             /* did we timeout for silence? */
686         char *silencestr=NULL;
687         int rfmt=0;
688
689
690         /* XXX EAGI FIXME XXX */
691
692         if (argc < 6)
693                 return RESULT_SHOWUSAGE;
694         if (sscanf(argv[5], "%i", &ms) != 1)
695                 return RESULT_SHOWUSAGE;
696
697         if (argc > 6)
698                 silencestr = strchr(argv[6],'s');
699         if ((argc > 7) && (!silencestr))
700                 silencestr = strchr(argv[7],'s');
701         if ((argc > 8) && (!silencestr))
702                 silencestr = strchr(argv[8],'s');
703
704         if (silencestr) {
705                 if (strlen(silencestr) > 2) {
706                         if ((silencestr[0] == 's') && (silencestr[1] == '=')) {
707                                 silencestr++;
708                                 silencestr++;
709                                 if (silencestr)
710                                         silence = atoi(silencestr);
711                                 if (silence > 0)
712                                         silence *= 1000;
713                         }
714                 }
715         }
716
717         if (silence > 0) {
718                 rfmt = chan->readformat;
719                 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
720                 if (res < 0) {
721                         ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
722                         return -1;
723                 }
724                 sildet = ast_dsp_new();
725                 if (!sildet) {
726                         ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
727                         return -1;
728                 }
729                 ast_dsp_set_threshold(sildet, 256);
730         }
731
732         /* backward compatibility, if no offset given, arg[6] would have been
733          * caught below and taken to be a beep, else if it is a digit then it is a
734          * offset */
735         if ((argc >6) && (sscanf(argv[6], "%ld", &sample_offset) != 1) && (!strchr(argv[6], '=')))
736                 res = ast_streamfile(chan, "beep", chan->language);
737
738         if ((argc > 7) && (!strchr(argv[7], '=')))
739                 res = ast_streamfile(chan, "beep", chan->language);
740
741         if (!res)
742                 res = ast_waitstream(chan, argv[4]);
743         if (!res) {
744                 fs = ast_writefile(argv[2], argv[3], NULL, O_CREAT | O_WRONLY | (sample_offset ? O_APPEND : 0), 0, 0644);
745                 if (!fs) {
746                         res = -1;
747                         fdprintf(agi->fd, "200 result=%d (writefile)\n", res);
748                         if (sildet)
749                                 ast_dsp_free(sildet);
750                         return RESULT_FAILURE;
751                 }
752                 
753                 chan->stream = fs;
754                 ast_applystream(chan,fs);
755                 /* really should have checks */
756                 ast_seekstream(fs, sample_offset, SEEK_SET);
757                 ast_truncstream(fs);
758                 
759                 gettimeofday(&start, NULL);
760                 gettimeofday(&tv, NULL);
761                 while ((ms < 0) || (((tv.tv_sec - start.tv_sec) * 1000 + (tv.tv_usec - start.tv_usec)/1000) < ms)) {
762                         res = ast_waitfor(chan, -1);
763                         if (res < 0) {
764                                 ast_closestream(fs);
765                                 fdprintf(agi->fd, "200 result=%d (waitfor) endpos=%ld\n", res,sample_offset);
766                                 if (sildet)
767                                         ast_dsp_free(sildet);
768                                 return RESULT_FAILURE;
769                         }
770                         f = ast_read(chan);
771                         if (!f) {
772                                 fdprintf(agi->fd, "200 result=%d (hangup) endpos=%ld\n", 0, sample_offset);
773                                 ast_closestream(fs);
774                                 if (sildet)
775                                         ast_dsp_free(sildet);
776                                 return RESULT_FAILURE;
777                         }
778                         switch(f->frametype) {
779                         case AST_FRAME_DTMF:
780                                 if (strchr(argv[4], f->subclass)) {
781                                         /* This is an interrupting chracter */
782                                         sample_offset = ast_tellstream(fs);
783                                         fdprintf(agi->fd, "200 result=%d (dtmf) endpos=%ld\n", f->subclass, sample_offset);
784                                         ast_closestream(fs);
785                                         ast_frfree(f);
786                                         if (sildet)
787                                                 ast_dsp_free(sildet);
788                                         return RESULT_SUCCESS;
789                                 }
790                                 break;
791                         case AST_FRAME_VOICE:
792                                 ast_writestream(fs, f);
793                                 /* this is a safe place to check progress since we know that fs
794                                  * is valid after a write, and it will then have our current
795                                  * location */
796                                 sample_offset = ast_tellstream(fs);
797                                 if (silence > 0) {
798                                         dspsilence = 0;
799                                         ast_dsp_silence(sildet, f, &dspsilence);
800                                         if (dspsilence) {
801                                                 totalsilence = dspsilence;
802                                         } else {
803                                                 totalsilence = 0;
804                                         }
805                                         if (totalsilence > silence) {
806                                              /* Ended happily with silence */
807                                                 ast_frfree(f);
808                                                 gotsilence = 1;
809                                                 break;
810                                         }
811                                 }
812                                 break;
813                         }
814                         ast_frfree(f);
815                         gettimeofday(&tv, NULL);
816                         if (gotsilence)
817                                 break;
818         }
819
820                 if (gotsilence) {
821                         ast_stream_rewind(fs, silence-1000);
822                         ast_truncstream(fs);
823                         sample_offset = ast_tellstream(fs);
824                 }               
825                 fdprintf(agi->fd, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset);
826                 ast_closestream(fs);
827         } else
828                 fdprintf(agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset);
829
830         if (silence > 0) {
831                 res = ast_set_read_format(chan, rfmt);
832                 if (res)
833                         ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
834                 ast_dsp_free(sildet);
835         }
836         return RESULT_SUCCESS;
837 }
838
839 static int handle_autohangup(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
840 {
841         int timeout;
842
843         if (argc != 3)
844                 return RESULT_SHOWUSAGE;
845         if (sscanf(argv[2], "%d", &timeout) != 1)
846                 return RESULT_SHOWUSAGE;
847         if (timeout < 0)
848                 timeout = 0;
849         if (timeout)
850                 chan->whentohangup = time(NULL) + timeout;
851         else
852                 chan->whentohangup = 0;
853         fdprintf(agi->fd, "200 result=0\n");
854         return RESULT_SUCCESS;
855 }
856
857 static int handle_hangup(struct ast_channel *chan, AGI *agi, int argc, char **argv)
858 {
859         struct ast_channel *c;
860         if (argc==1) {
861             /* no argument: hangup the current channel */
862             ast_softhangup(chan,AST_SOFTHANGUP_EXPLICIT);
863             fdprintf(agi->fd, "200 result=1\n");
864             return RESULT_SUCCESS;
865         } else if (argc==2) {
866             /* one argument: look for info on the specified channel */
867             c = ast_channel_walk_locked(NULL);
868             while (c) {
869                 if (strcasecmp(argv[1],c->name)==0) {
870                     /* we have a matching channel */
871                             ast_softhangup(c,AST_SOFTHANGUP_EXPLICIT);
872                             fdprintf(agi->fd, "200 result=1\n");
873                                         ast_mutex_unlock(&c->lock);
874                     return RESULT_SUCCESS;
875                 }
876                                 ast_mutex_unlock(&c->lock);
877                 c = ast_channel_walk_locked(c);
878             }
879             /* if we get this far no channel name matched the argument given */
880             fdprintf(agi->fd, "200 result=-1\n");
881             return RESULT_SUCCESS;
882         } else {
883             return RESULT_SHOWUSAGE;
884         }
885 }
886
887 static int handle_exec(struct ast_channel *chan, AGI *agi, int argc, char **argv)
888 {
889         int res;
890         struct ast_app *app;
891
892         if (argc < 2)
893                 return RESULT_SHOWUSAGE;
894
895         if (option_verbose > 2)
896                 ast_verbose(VERBOSE_PREFIX_3 "AGI Script Executing Application: (%s) Options: (%s)\n", argv[1], argv[2]);
897
898         app = pbx_findapp(argv[1]);
899
900         if (app) {
901                 res = pbx_exec(chan, app, argv[2], 1);
902         } else {
903                 ast_log(LOG_WARNING, "Could not find application (%s)\n", argv[1]);
904                 res = -2;
905         }
906         fdprintf(agi->fd, "200 result=%d\n", res);
907
908         return res;
909 }
910
911 static int handle_setcallerid(struct ast_channel *chan, AGI *agi, int argc, char **argv)
912 {
913         char tmp[256]="";
914         char *l = NULL, *n = NULL;
915         if (argv[2]) {
916                 strncpy(tmp, argv[2], sizeof(tmp) - 1);
917                 ast_callerid_parse(tmp, &n, &l);
918                 if (l)
919                         ast_shrink_phone_number(l);
920                 else
921                         l = "";
922                 if (!n)
923                         n = "";
924                 ast_set_callerid(chan, l, n, NULL);
925         }
926
927         fdprintf(agi->fd, "200 result=1\n");
928         return RESULT_SUCCESS;
929 }
930
931 static int handle_channelstatus(struct ast_channel *chan, AGI *agi, int argc, char **argv)
932 {
933         struct ast_channel *c;
934         if (argc==2) {
935                 /* no argument: supply info on the current channel */
936                 fdprintf(agi->fd, "200 result=%d\n", chan->_state);
937                 return RESULT_SUCCESS;
938         } else if (argc==3) {
939                 /* one argument: look for info on the specified channel */
940                 c = ast_channel_walk_locked(NULL);
941                 while (c) {
942                         if (strcasecmp(argv[2],c->name)==0) {
943                                 fdprintf(agi->fd, "200 result=%d\n", c->_state);
944                                 ast_mutex_unlock(&c->lock);
945                                 return RESULT_SUCCESS;
946                         }
947                         ast_mutex_unlock(&c->lock);
948                         c = ast_channel_walk_locked(c);
949                 }
950                 /* if we get this far no channel name matched the argument given */
951                 fdprintf(agi->fd, "200 result=-1\n");
952                 return RESULT_SUCCESS;
953         } else {
954                 return RESULT_SHOWUSAGE;
955         }
956 }
957
958 static int handle_setvariable(struct ast_channel *chan, AGI *agi, int argc, char **argv)
959 {
960         if (argv[3])
961                 pbx_builtin_setvar_helper(chan, argv[2], argv[3]);
962
963         fdprintf(agi->fd, "200 result=1\n");
964         return RESULT_SUCCESS;
965 }
966
967 static int handle_getvariable(struct ast_channel *chan, AGI *agi, int argc, char **argv)
968 {
969         char *tempstr;
970
971         if ((tempstr = pbx_builtin_getvar_helper(chan, argv[2]))) 
972                 fdprintf(agi->fd, "200 result=1 (%s)\n", tempstr);
973         else
974                 fdprintf(agi->fd, "200 result=0\n");
975
976         return RESULT_SUCCESS;
977 }
978
979 static int handle_verbose(struct ast_channel *chan, AGI *agi, int argc, char **argv)
980 {
981         int level = 0;
982         char *prefix;
983
984         if (argc < 2)
985                 return RESULT_SHOWUSAGE;
986
987         if (argv[2])
988                 sscanf(argv[2], "%d", &level);
989
990         switch (level) {
991                 case 4:
992                         prefix = VERBOSE_PREFIX_4;
993                         break;
994                 case 3:
995                         prefix = VERBOSE_PREFIX_3;
996                         break;
997                 case 2:
998                         prefix = VERBOSE_PREFIX_2;
999                         break;
1000                 case 1:
1001                 default:
1002                         prefix = VERBOSE_PREFIX_1;
1003                         break;
1004         }
1005
1006         if (level <= option_verbose)
1007                 ast_verbose("%s %s: %s\n", prefix, chan->data, argv[1]);
1008         
1009         fdprintf(agi->fd, "200 result=1\n");
1010         
1011         return RESULT_SUCCESS;
1012 }
1013
1014 static int handle_dbget(struct ast_channel *chan, AGI *agi, int argc, char **argv)
1015 {
1016         int res;
1017         char tmp[256];
1018         if (argc != 4)
1019                 return RESULT_SHOWUSAGE;
1020         res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp));
1021         if (res) 
1022                 fdprintf(agi->fd, "200 result=0\n");
1023         else
1024                 fdprintf(agi->fd, "200 result=1 (%s)\n", tmp);
1025
1026         return RESULT_SUCCESS;
1027 }
1028
1029 static int handle_dbput(struct ast_channel *chan, AGI *agi, int argc, char **argv)
1030 {
1031         int res;
1032         if (argc != 5)
1033                 return RESULT_SHOWUSAGE;
1034         res = ast_db_put(argv[2], argv[3], argv[4]);
1035         if (res) 
1036                 fdprintf(agi->fd, "200 result=0\n");
1037         else
1038                 fdprintf(agi->fd, "200 result=1\n");
1039
1040         return RESULT_SUCCESS;
1041 }
1042
1043 static int handle_dbdel(struct ast_channel *chan, AGI *agi, int argc, char **argv)
1044 {
1045         int res;
1046         if (argc != 4)
1047                 return RESULT_SHOWUSAGE;
1048         res = ast_db_del(argv[2], argv[3]);
1049         if (res) 
1050                 fdprintf(agi->fd, "200 result=0\n");
1051         else
1052                 fdprintf(agi->fd, "200 result=1\n");
1053
1054         return RESULT_SUCCESS;
1055 }
1056
1057 static int handle_dbdeltree(struct ast_channel *chan, AGI *agi, int argc, char **argv)
1058 {
1059         int res;
1060         if ((argc < 3) || (argc > 4))
1061                 return RESULT_SHOWUSAGE;
1062         if (argc == 4)
1063                 res = ast_db_deltree(argv[2], argv[3]);
1064         else
1065                 res = ast_db_deltree(argv[2], NULL);
1066
1067         if (res) 
1068                 fdprintf(agi->fd, "200 result=0\n");
1069         else
1070                 fdprintf(agi->fd, "200 result=1\n");
1071         return RESULT_SUCCESS;
1072 }
1073
1074 static char debug_usage[] = 
1075 "Usage: agi debug\n"
1076 "       Enables dumping of AGI transactions for debugging purposes\n";
1077
1078 static char no_debug_usage[] = 
1079 "Usage: agi no debug\n"
1080 "       Disables dumping of AGI transactions for debugging purposes\n";
1081
1082 static int agi_do_debug(int fd, int argc, char *argv[])
1083 {
1084         if (argc != 2)
1085                 return RESULT_SHOWUSAGE;
1086         agidebug = 1;
1087         ast_cli(fd, "AGI Debugging Enabled\n");
1088         return RESULT_SUCCESS;
1089 }
1090
1091 static int agi_no_debug(int fd, int argc, char *argv[])
1092 {
1093         if (argc != 3)
1094                 return RESULT_SHOWUSAGE;
1095         agidebug = 0;
1096         ast_cli(fd, "AGI Debugging Disabled\n");
1097         return RESULT_SUCCESS;
1098 }
1099
1100 static struct ast_cli_entry  cli_debug =
1101         { { "agi", "debug", NULL }, agi_do_debug, "Enable AGI debugging", debug_usage };
1102
1103 static struct ast_cli_entry  cli_no_debug =
1104         { { "agi", "no", "debug", NULL }, agi_no_debug, "Disable AGI debugging", no_debug_usage };
1105
1106 static int handle_noop(struct ast_channel *chan, AGI *agi, int arg, char *argv[])
1107 {
1108         fdprintf(agi->fd, "200 result=0\n");
1109         return RESULT_SUCCESS;
1110 }
1111
1112 static int handle_setmusic(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
1113 {
1114         if (!strncasecmp(argv[2],"on",2)) {
1115                 if (argc > 3)
1116                         ast_moh_start(chan, argv[3]);
1117                 else
1118                         ast_moh_start(chan, NULL);
1119         }
1120         if (!strncasecmp(argv[2],"off",3)) {
1121                 ast_moh_stop(chan);
1122         }
1123         fdprintf(agi->fd, "200 result=0\n");
1124         return RESULT_SUCCESS;
1125 }
1126
1127 static char usage_setmusic[] =
1128 " Usage: SET MUSIC ON <on|off> <class>\n"
1129 "       Enables/Disables the music on hold generator.  If <class> is\n"
1130 " not specified then the default music on hold class will be used.\n"
1131 " Always returns 0\n";
1132
1133 static char usage_dbput[] =
1134 " Usage: DATABASE PUT <family> <key> <value>\n"
1135 "       Adds or updates an entry in the Asterisk database for a\n"
1136 " given family, key, and value.\n"
1137 " Returns 1 if succesful, 0 otherwise\n";
1138
1139 static char usage_dbget[] =
1140 " Usage: DATABASE GET <family> <key>\n"
1141 "       Retrieves an entry in the Asterisk database for a\n"
1142 " given family and key.\n"
1143 "       Returns 0 if <key> is not set.  Returns 1 if <key>\n"
1144 " is set and returns the variable in parenthesis\n"
1145 " example return code: 200 result=1 (testvariable)\n";
1146
1147 static char usage_dbdel[] =
1148 " Usage: DATABASE DEL <family> <key>\n"
1149 "       Deletes an entry in the Asterisk database for a\n"
1150 " given family and key.\n"
1151 " Returns 1 if succesful, 0 otherwise\n";
1152
1153 static char usage_dbdeltree[] =
1154 " Usage: DATABASE DELTREE <family> [keytree]\n"
1155 "       Deletes a family or specific keytree withing a family\n"
1156 " in the Asterisk database.\n"
1157 " Returns 1 if succesful, 0 otherwise\n";
1158
1159 static char usage_verbose[] =
1160 " Usage: VERBOSE <message> <level>\n"
1161 "       Sends <message> to the console via verbose message system.\n"
1162 "       <level> is the the verbose level (1-4)\n"
1163 "       Always returns 1\n";
1164
1165 static char usage_getvariable[] =
1166 " Usage: GET VARIABLE <variablename>\n"
1167 "       Returns 0 if <variablename> is not set.  Returns 1 if <variablename>\n"
1168 " is set and returns the variable in parenthesis\n"
1169 " example return code: 200 result=1 (testvariable)\n";
1170
1171 static char usage_setvariable[] =
1172 " Usage: SET VARIABLE <variablename> <value>\n";
1173
1174 static char usage_channelstatus[] =
1175 " Usage: CHANNEL STATUS [<channelname>]\n"
1176 "       Returns the status of the specified channel.\n" 
1177 "       If no channel name is given the returns the status of the\n"
1178 "       current channel.\n"
1179 "       Return values:\n"
1180 " 0 Channel is down and available\n"
1181 " 1 Channel is down, but reserved\n"
1182 " 2 Channel is off hook\n"
1183 " 3 Digits (or equivalent) have been dialed\n"
1184 " 4 Line is ringing\n"
1185 " 5 Remote end is ringing\n"
1186 " 6 Line is up\n"
1187 " 7 Line is busy\n";
1188
1189 static char usage_setcallerid[] =
1190 " Usage: SET CALLERID <number>\n"
1191 "       Changes the callerid of the current channel.\n";
1192
1193 static char usage_exec[] =
1194 " Usage: EXEC <application> <options>\n"
1195 "       Executes <application> with given <options>.\n"
1196 "       Returns whatever the application returns, or -2 on failure to find application\n";
1197
1198 static char usage_hangup[] =
1199 " Usage: HANGUP [<channelname>]\n"
1200 "       Hangs up the specified channel.\n"
1201 "       If no channel name is given, hangs up the current channel\n";
1202
1203 static char usage_answer[] = 
1204 " Usage: ANSWER\n"
1205 "        Answers channel if not already in answer state. Returns -1 on\n"
1206 " channel failure, or 0 if successful.\n";
1207
1208 static char usage_waitfordigit[] = 
1209 " Usage: WAIT FOR DIGIT <timeout>\n"
1210 "        Waits up to 'timeout' milliseconds for channel to receive a DTMF digit.\n"
1211 " Returns -1 on channel failure, 0 if no digit is received in the timeout, or\n"
1212 " the numerical value of the ascii of the digit if one is received.  Use -1\n"
1213 " for the timeout value if you desire the call to block indefinitely.\n";
1214
1215 static char usage_sendtext[] =
1216 " Usage: SEND TEXT \"<text to send>\"\n"
1217 "        Sends the given text on a channel.  Most channels do not support the\n"
1218 " transmission of text.  Returns 0 if text is sent, or if the channel does not\n"
1219 " support text transmission.  Returns -1 only on error/hangup.  Text\n"
1220 " consisting of greater than one word should be placed in quotes since the\n"
1221 " command only accepts a single argument.\n";
1222
1223 static char usage_recvchar[] =
1224 " Usage: RECEIVE CHAR <timeout>\n"
1225 "        Receives a character of text on a channel.  Specify timeout to be the\n"
1226 " maximum time to wait for input in milliseconds, or 0 for infinite. Most channels\n"
1227 " do not support the reception of text.  Returns the decimal value of the character\n"
1228 " if one is received, or 0 if the channel does not support text reception.  Returns\n"
1229 " -1 only on error/hangup.\n";
1230
1231 static char usage_tddmode[] =
1232 " Usage: TDD MODE <on|off>\n"
1233 "        Enable/Disable TDD transmission/reception on a channel. Returns 1 if\n"
1234 " successful, or 0 if channel is not TDD-capable.\n";
1235
1236 static char usage_sendimage[] =
1237 " Usage: SEND IMAGE <image>\n"
1238 "        Sends the given image on a channel.  Most channels do not support the\n"
1239 " transmission of images.  Returns 0 if image is sent, or if the channel does not\n"
1240 " support image transmission.  Returns -1 only on error/hangup.  Image names\n"
1241 " should not include extensions.\n";
1242
1243 static char usage_streamfile[] =
1244 " Usage: STREAM FILE <filename> <escape digits> [sample offset]\n"
1245 "        Send the given file, allowing playback to be interrupted by the given\n"
1246 " digits, if any.  Use double quotes for the digits if you wish none to be\n"
1247 " permitted.  If sample offset is provided then the audio will seek to sample\n"
1248 " offset before play starts.  Returns 0 if playback completes without a digit\n"
1249 " being pressed, or the ASCII numerical value of the digit if one was pressed,\n"
1250 " or -1 on error or if the channel was disconnected.  Remember, the file\n"
1251 " extension must not be included in the filename.\n";
1252
1253 static char usage_getoption[] = 
1254 " Usage: GET OPTION <filename> <escape_digits> [timeout]\n"
1255 " Exactly like the STREAM FILE but used with a timeout option\n";
1256
1257 static char usage_saynumber[] =
1258 " Usage: SAY NUMBER <number> <escape digits>\n"
1259 "        Say a given number, returning early if any of the given DTMF digits\n"
1260 " are received on the channel.  Returns 0 if playback completes without a digit\n"
1261 " being pressed, or the ASCII numerical value of the digit if one was pressed or\n"
1262 " -1 on error/hangup.\n";
1263
1264 static char usage_saydigits[] =
1265 " Usage: SAY DIGITS <number> <escape digits>\n"
1266 "        Say a given digit string, returning early if any of the given DTMF digits\n"
1267 " are received on the channel.  Returns 0 if playback completes without a digit\n"
1268 " being pressed, or the ASCII numerical value of the digit if one was pressed or\n"
1269 " -1 on error/hangup.\n";
1270
1271 static char usage_saytime[] =
1272 " Usage: SAY TIME <time> <escape digits>\n"
1273 "        Say a given time, returning early if any of the given DTMF digits are\n"
1274 " received on the channel.  <time> is number of seconds elapsed since 00:00:00\n"
1275 " on January 1, 1970, Coordinated Universal Time (UTC).  Returns 0 if playback\n"
1276 " completes without a digit being pressed, or the ASCII numerical value of the\n"
1277 " digit if one was pressed or -1 on error/hangup.\n";
1278
1279 static char usage_sayphonetic[] =
1280 " Usage: SAY PHONETIC <string> <escape digits>\n"
1281 "        Say a given character string with phonetics, returning early if any of the given DTMF digits\n"
1282 " are received on the channel.  Returns 0 if playback completes without a digit\n"
1283 " being pressed, or the ASCII numerical value of the digit if one was pressed or\n"
1284 " -1 on error/hangup.\n";
1285
1286 static char usage_getdata[] =
1287 " Usage: GET DATA <file to be streamed> [timeout] [max digits]\n"
1288 "        Stream the given file, and recieve DTMF data. Returns the digits recieved\n"
1289 "from the channel at the other end.\n";
1290
1291 static char usage_setcontext[] =
1292 " Usage: SET CONTEXT <desired context>\n"
1293 "        Sets the context for continuation upon exiting the application.\n";
1294
1295 static char usage_setextension[] =
1296 " Usage: SET EXTENSION <new extension>\n"
1297 "        Changes the extension for continuation upon exiting the application.\n";
1298
1299 static char usage_setpriority[] =
1300 " Usage: SET PRIORITY <num>\n"
1301 "        Changes the priority for continuation upon exiting the application.\n";
1302
1303 static char usage_recordfile[] =
1304 " Usage: RECORD FILE <filename> <format> <escape digits> <timeout> [offset samples] [BEEP] [s=silence]\n"
1305 "        Record to a file until a given dtmf digit in the sequence is received\n"
1306 " Returns -1 on hangup or error.  The format will specify what kind of file\n"
1307 " will be recorded.  The timeout is the maximum record time in milliseconds, or\n"
1308 " -1 for no timeout. Offset samples is optional, and if provided will seek to\n"
1309 " the offset without exceeding the end of the file.  \"silence\" is the number\n"
1310 " of seconds of silence allowed before the function returns despite the\n"
1311 " lack of dtmf digits or reaching timeout.  Silence value must be\n"
1312 " preceeded by \"s=\" and is optional.\n";
1313
1314
1315 static char usage_autohangup[] =
1316 " Usage: SET AUTOHANGUP <time>\n"
1317 "    Cause the channel to automatically hangup at <time> seconds in the\n"
1318 "future.  Of course it can be hungup before then as well.   Setting to\n"
1319 "0 will cause the autohangup feature to be disabled on this channel.\n";
1320
1321 static char usage_noop[] =
1322 " Usage: NOOP\n"
1323 "    Does nothing.\n";
1324
1325 static agi_command commands[MAX_COMMANDS] = {
1326         { { "answer", NULL }, handle_answer, "Asserts answer", usage_answer },
1327         { { "wait", "for", "digit", NULL }, handle_waitfordigit, "Waits for a digit to be pressed", usage_waitfordigit },
1328         { { "send", "text", NULL }, handle_sendtext, "Sends text to channels supporting it", usage_sendtext },
1329         { { "receive", "char", NULL }, handle_recvchar, "Receives text from channels supporting it", usage_recvchar },
1330         { { "tdd", "mode", NULL }, handle_tddmode, "Sends text to channels supporting it", usage_tddmode },
1331         { { "stream", "file", NULL }, handle_streamfile, "Sends audio file on channel", usage_streamfile },
1332         { { "get", "option", NULL }, handle_getoption, "Stream File", usage_getoption },
1333         { { "send", "image", NULL }, handle_sendimage, "Sends images to channels supporting it", usage_sendimage },
1334         { { "say", "digits", NULL }, handle_saydigits, "Says a given digit string", usage_saydigits },
1335         { { "say", "number", NULL }, handle_saynumber, "Says a given number", usage_saynumber },
1336         { { "say", "phonetic", NULL }, handle_sayphonetic, "Says a given character string with phonetics", usage_sayphonetic },
1337         { { "say", "time", NULL }, handle_saytime, "Says a given time", usage_saytime },
1338         { { "get", "data", NULL }, handle_getdata, "Gets data on a channel", usage_getdata },
1339         { { "set", "context", NULL }, handle_setcontext, "Sets channel context", usage_setcontext },
1340         { { "set", "extension", NULL }, handle_setextension, "Changes channel extension", usage_setextension },
1341         { { "set", "priority", NULL }, handle_setpriority, "Prioritizes the channel", usage_setpriority },
1342         { { "record", "file", NULL }, handle_recordfile, "Records to a given file", usage_recordfile },
1343         { { "set", "autohangup", NULL }, handle_autohangup, "Autohangup channel in some time", usage_autohangup },
1344         { { "hangup", NULL }, handle_hangup, "Hangup the current channel", usage_hangup },
1345         { { "exec", NULL }, handle_exec, "Executes a given Application", usage_exec },
1346         { { "set", "callerid", NULL }, handle_setcallerid, "Sets callerid for the current channel", usage_setcallerid },
1347         { { "channel", "status", NULL }, handle_channelstatus, "Returns status of the connected channel", usage_channelstatus },
1348         { { "set", "variable", NULL }, handle_setvariable, "Sets a channel variable", usage_setvariable },
1349         { { "get", "variable", NULL }, handle_getvariable, "Gets a channel variable", usage_getvariable },
1350         { { "verbose", NULL }, handle_verbose, "Logs a message to the asterisk verbose log", usage_verbose },
1351         { { "database", "get", NULL }, handle_dbget, "Gets database value", usage_dbget },
1352         { { "database", "put", NULL }, handle_dbput, "Adds/updates database value", usage_dbput },
1353         { { "database", "del", NULL }, handle_dbdel, "Removes database key/value", usage_dbdel },
1354         { { "database", "deltree", NULL }, handle_dbdeltree, "Removes database keytree/value", usage_dbdeltree },
1355         { { "noop", NULL }, handle_noop, "Does nothing", usage_noop },
1356         { { "set", "music", NULL }, handle_setmusic, "Enable/Disable Music on hold generator", usage_setmusic }
1357 };
1358
1359 static void join(char *s, size_t len, char *w[])
1360 {
1361         int x;
1362         /* Join words into a string */
1363         if (!s) {
1364                 return;
1365         }
1366         s[0] = '\0';
1367         for (x=0;w[x];x++) {
1368                 if (x)
1369                         strncat(s, " ", len - strlen(s) - 1);
1370                 strncat(s, w[x], len - strlen(s) - 1);
1371         }
1372 }
1373
1374 static int help_workhorse(int fd, char *match[])
1375 {
1376         char fullcmd[80];
1377         char matchstr[80];
1378         int x;
1379         struct agi_command *e;
1380         if (match)
1381                 join(matchstr, sizeof(matchstr), match);
1382         for (x=0;x<sizeof(commands)/sizeof(commands[0]);x++) {
1383                 if (!commands[x].cmda[0]) break;
1384                 e = &commands[x]; 
1385                 if (e)
1386                         join(fullcmd, sizeof(fullcmd), e->cmda);
1387                 /* Hide commands that start with '_' */
1388                 if (fullcmd[0] == '_')
1389                         continue;
1390                 if (match) {
1391                         if (strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
1392                                 continue;
1393                         }
1394                 }
1395                 ast_cli(fd, "%20.20s   %s\n", fullcmd, e->summary);
1396         }
1397         return 0;
1398 }
1399
1400 int agi_register(agi_command *agi)
1401 {
1402         int x;
1403         for (x=0;x<MAX_COMMANDS - 1;x++) {
1404                 if (commands[x].cmda[0] == agi->cmda[0]) {
1405                         ast_log(LOG_WARNING, "Command already registered!\n");
1406                         return -1;
1407                 }
1408         }
1409         for (x=0;x<MAX_COMMANDS - 1;x++) {
1410                 if (!commands[x].cmda[0]) {
1411                         commands[x] = *agi;
1412                         return 0;
1413                 }
1414         }
1415         ast_log(LOG_WARNING, "No more room for new commands!\n");
1416         return -1;
1417 }
1418
1419 void agi_unregister(agi_command *agi)
1420 {
1421         int x;
1422         for (x=0;x<MAX_COMMANDS - 1;x++) {
1423                 if (commands[x].cmda[0] == agi->cmda[0]) {
1424                         memset(&commands[x], 0, sizeof(agi_command));
1425                 }
1426         }
1427 }
1428
1429 static agi_command *find_command(char *cmds[], int exact)
1430 {
1431         int x;
1432         int y;
1433         int match;
1434         for (x=0;x < sizeof(commands) / sizeof(commands[0]);x++) {
1435                 if (!commands[x].cmda[0]) break;
1436                 /* start optimistic */
1437                 match = 1;
1438                 for (y=0;match && cmds[y]; y++) {
1439                         /* If there are no more words in the command (and we're looking for
1440                            an exact match) or there is a difference between the two words,
1441                            then this is not a match */
1442                         if (!commands[x].cmda[y] && !exact)
1443                                 break;
1444                         /* don't segfault if the next part of a command doesn't exist */
1445                         if (!commands[x].cmda[y]) return NULL;
1446                         if (strcasecmp(commands[x].cmda[y], cmds[y]))
1447                                 match = 0;
1448                 }
1449                 /* If more words are needed to complete the command then this is not
1450                    a candidate (unless we're looking for a really inexact answer  */
1451                 if ((exact > -1) && commands[x].cmda[y])
1452                         match = 0;
1453                 if (match)
1454                         return &commands[x];
1455         }
1456         return NULL;
1457 }
1458
1459
1460 static int parse_args(char *s, int *max, char *argv[])
1461 {
1462         int x=0;
1463         int quoted=0;
1464         int escaped=0;
1465         int whitespace=1;
1466         char *cur;
1467
1468         cur = s;
1469         while(*s) {
1470                 switch(*s) {
1471                 case '"':
1472                         /* If it's escaped, put a literal quote */
1473                         if (escaped) 
1474                                 goto normal;
1475                         else 
1476                                 quoted = !quoted;
1477                         if (quoted && whitespace) {
1478                                 /* If we're starting a quote, coming off white space start a new word, too */
1479                                 argv[x++] = cur;
1480                                 whitespace=0;
1481                         }
1482                         escaped = 0;
1483                 break;
1484                 case ' ':
1485                 case '\t':
1486                         if (!quoted && !escaped) {
1487                                 /* If we're not quoted, mark this as whitespace, and
1488                                    end the previous argument */
1489                                 whitespace = 1;
1490                                 *(cur++) = '\0';
1491                         } else
1492                                 /* Otherwise, just treat it as anything else */ 
1493                                 goto normal;
1494                         break;
1495                 case '\\':
1496                         /* If we're escaped, print a literal, otherwise enable escaping */
1497                         if (escaped) {
1498                                 goto normal;
1499                         } else {
1500                                 escaped=1;
1501                         }
1502                         break;
1503                 default:
1504 normal:
1505                         if (whitespace) {
1506                                 if (x >= MAX_ARGS -1) {
1507                                         ast_log(LOG_WARNING, "Too many arguments, truncating\n");
1508                                         break;
1509                                 }
1510                                 /* Coming off of whitespace, start the next argument */
1511                                 argv[x++] = cur;
1512                                 whitespace=0;
1513                         }
1514                         *(cur++) = *s;
1515                         escaped=0;
1516                 }
1517                 s++;
1518         }
1519         /* Null terminate */
1520         *(cur++) = '\0';
1521         argv[x] = NULL;
1522         *max = x;
1523         return 0;
1524 }
1525
1526 static int agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf)
1527 {
1528         char *argv[MAX_ARGS];
1529         int argc = 0;
1530         int res;
1531         agi_command *c;
1532         argc = MAX_ARGS;
1533         parse_args(buf, &argc, argv);
1534 #if     0
1535         { int x;
1536         for (x=0;x<argc;x++) 
1537                 fprintf(stderr, "Got Arg%d: %s\n", x, argv[x]); }
1538 #endif
1539         c = find_command(argv, 0);
1540         if (c) {
1541                 res = c->handler(chan, agi, argc, argv);
1542                 switch(res) {
1543                 case RESULT_SHOWUSAGE:
1544                         fdprintf(agi->fd, "520-Invalid command syntax.  Proper usage follows:\n");
1545                         fdprintf(agi->fd, c->usage);
1546                         fdprintf(agi->fd, "520 End of proper usage.\n");
1547                         break;
1548                 case AST_PBX_KEEPALIVE:
1549                         /* We've been asked to keep alive, so do so */
1550                         return AST_PBX_KEEPALIVE;
1551                         break;
1552                 case RESULT_FAILURE:
1553                         /* They've already given the failure.  We've been hung up on so handle this
1554                            appropriately */
1555                         return -1;
1556                 }
1557         } else {
1558                 fdprintf(agi->fd, "510 Invalid or unknown command\n");
1559         }
1560         return 0;
1561 }
1562 #define RETRY   3
1563 static int run_agi(struct ast_channel *chan, char *request, AGI *agi, int pid, int dead)
1564 {
1565         struct ast_channel *c;
1566         int outfd;
1567         int ms;
1568         int returnstatus = 0;
1569         struct ast_frame *f;
1570         char buf[2048];
1571         FILE *readf;
1572         /* how many times we'll retry if ast_waitfor_nandfs will return without either 
1573           channel or file descriptor in case select is interrupted by a system call (EINTR) */
1574         int retry = RETRY;
1575
1576         if (!(readf = fdopen(agi->ctrl, "r"))) {
1577                 ast_log(LOG_WARNING, "Unable to fdopen file descriptor\n");
1578                 if (pid > -1)
1579                         kill(pid, SIGHUP);
1580                 close(agi->ctrl);
1581                 return -1;
1582         }
1583         setlinebuf(readf);
1584         setup_env(chan, request, agi->fd, (agi->audio > -1));
1585         for (;;) {
1586                 ms = -1;
1587                 c = ast_waitfor_nandfds(&chan, dead ? 0 : 1, &agi->ctrl, 1, NULL, &outfd, &ms);
1588                 if (c) {
1589                         retry = RETRY;
1590                         /* Idle the channel until we get a command */
1591                         f = ast_read(c);
1592                         if (!f) {
1593                                 ast_log(LOG_DEBUG, "%s hungup\n", chan->name);
1594                                 returnstatus = -1;
1595                                 break;
1596                         } else {
1597                                 /* If it's voice, write it to the audio pipe */
1598                                 if ((agi->audio > -1) && (f->frametype == AST_FRAME_VOICE)) {
1599                                         /* Write, ignoring errors */
1600                                         write(agi->audio, f->data, f->datalen);
1601                                 }
1602                                 ast_frfree(f);
1603                         }
1604                 } else if (outfd > -1) {
1605                         retry = RETRY;
1606                         if (!fgets(buf, sizeof(buf), readf)) {
1607                                 /* Program terminated */
1608                                 if (returnstatus)
1609                                         returnstatus = -1;
1610                                 if (option_verbose > 2) 
1611                                         ast_verbose(VERBOSE_PREFIX_3 "AGI Script %s completed, returning %d\n", request, returnstatus);
1612                                 /* No need to kill the pid anymore, since they closed us */
1613                                 pid = -1;
1614                                 break;
1615                         }
1616                           /* get rid of trailing newline, if any */
1617                         if (*buf && buf[strlen(buf) - 1] == '\n')
1618                                 buf[strlen(buf) - 1] = 0;
1619                         if (agidebug)
1620                                 ast_verbose("AGI Rx << %s\n", buf);
1621                         returnstatus |= agi_handle_command(chan, agi, buf);
1622                         /* If the handle_command returns -1, we need to stop */
1623                         if ((returnstatus < 0) || (returnstatus == AST_PBX_KEEPALIVE)) {
1624                                 break;
1625                         }
1626                 } else {
1627                         if (--retry <= 0) {
1628                                 ast_log(LOG_WARNING, "No channel, no fd?\n");
1629                                 returnstatus = -1;
1630                                 break;
1631                         }
1632                 }
1633         }
1634         /* Notify process */
1635         if (pid > -1)
1636                 kill(pid, SIGHUP);
1637         fclose(readf);
1638         return returnstatus;
1639 }
1640
1641 static int handle_showagi(int fd, int argc, char *argv[]) {
1642         struct agi_command *e;
1643         char fullcmd[80];
1644         if ((argc < 2))
1645                 return RESULT_SHOWUSAGE;
1646         if (argc > 2) {
1647                 e = find_command(argv + 2, 1);
1648                 if (e) 
1649                         ast_cli(fd, e->usage);
1650                 else {
1651                         if (find_command(argv + 2, -1)) {
1652                                 return help_workhorse(fd, argv + 1);
1653                         } else {
1654                                 join(fullcmd, sizeof(fullcmd), argv+1);
1655                                 ast_cli(fd, "No such command '%s'.\n", fullcmd);
1656                         }
1657                 }
1658         } else {
1659                 return help_workhorse(fd, NULL);
1660         }
1661         return RESULT_SUCCESS;
1662 }
1663
1664 static int handle_dumpagihtml(int fd, int argc, char *argv[]) {
1665         struct agi_command *e;
1666         char fullcmd[80];
1667         char *tempstr;
1668         int x;
1669         FILE *htmlfile;
1670
1671         if ((argc < 3))
1672                 return RESULT_SHOWUSAGE;
1673
1674         if (!(htmlfile = fopen(argv[2], "wt"))) {
1675                 ast_cli(fd, "Could not create file '%s'\n", argv[2]);
1676                 return RESULT_SHOWUSAGE;
1677         }
1678
1679         fprintf(htmlfile, "<HTML>\n<HEAD>\n<TITLE>AGI Commands</TITLE>\n</HEAD>\n");
1680         fprintf(htmlfile, "<BODY>\n<CENTER><B><H1>AGI Commands</H1></B></CENTER>\n\n");
1681
1682
1683         fprintf(htmlfile, "<TABLE BORDER=\"0\" CELLSPACING=\"10\">\n");
1684
1685         for (x=0;x<sizeof(commands)/sizeof(commands[0]);x++) {
1686                 char *stringp=NULL;
1687                 if (!commands[x].cmda[0]) break;
1688                 e = &commands[x]; 
1689                 if (e)
1690                         join(fullcmd, sizeof(fullcmd), e->cmda);
1691                 /* Hide commands that start with '_' */
1692                 if (fullcmd[0] == '_')
1693                         continue;
1694
1695                 fprintf(htmlfile, "<TR><TD><TABLE BORDER=\"1\" CELLPADDING=\"5\" WIDTH=\"100%%\">\n");
1696                 fprintf(htmlfile, "<TR><TH ALIGN=\"CENTER\"><B>%s - %s</B></TD></TR>\n", fullcmd,e->summary);
1697
1698
1699                 stringp=e->usage;
1700                 tempstr = strsep(&stringp, "\n");
1701
1702                 fprintf(htmlfile, "<TR><TD ALIGN=\"CENTER\">%s</TD></TR>\n", tempstr);
1703                 
1704                 fprintf(htmlfile, "<TR><TD ALIGN=\"CENTER\">\n");
1705                 while ((tempstr = strsep(&stringp, "\n")) != NULL) {
1706                 fprintf(htmlfile, "%s<BR>\n",tempstr);
1707
1708                 }
1709                 fprintf(htmlfile, "</TD></TR>\n");
1710                 fprintf(htmlfile, "</TABLE></TD></TR>\n\n");
1711
1712         }
1713
1714         fprintf(htmlfile, "</TABLE>\n</BODY>\n</HTML>\n");
1715         fclose(htmlfile);
1716         ast_cli(fd, "AGI HTML Commands Dumped to: %s\n", argv[2]);
1717         return RESULT_SUCCESS;
1718 }
1719
1720 static int agi_exec_full(struct ast_channel *chan, void *data, int enhanced, int dead)
1721 {
1722         int res=0;
1723         struct localuser *u;
1724         char *argv[MAX_ARGS];
1725         char buf[2048]="";
1726         char *tmp = (char *)buf;
1727         int argc = 0;
1728         int fds[2];
1729         int efd = -1;
1730         int pid;
1731         char *stringp;
1732         AGI agi;
1733         if (!data || ast_strlen_zero(data)) {
1734                 ast_log(LOG_WARNING, "AGI requires an argument (script)\n");
1735                 return -1;
1736         }
1737         strncpy(buf, data, sizeof(buf) - 1);
1738
1739         memset(&agi, 0, sizeof(agi));
1740         while ((stringp = strsep(&tmp, "|"))) {
1741                 argv[argc++] = stringp;
1742         }
1743         argv[argc] = NULL;
1744
1745         LOCAL_USER_ADD(u);
1746 #if 0
1747          /* Answer if need be */
1748         if (chan->_state != AST_STATE_UP) {
1749                 if (ast_answer(chan)) {
1750                         LOCAL_USER_REMOVE(u);
1751                         return -1;
1752                 }
1753         }
1754 #endif
1755         res = launch_script(argv[0], argv, fds, enhanced ? &efd : NULL, &pid);
1756         if (!res) {
1757                 agi.fd = fds[1];
1758                 agi.ctrl = fds[0];
1759                 agi.audio = efd;
1760                 res = run_agi(chan, argv[0], &agi, pid, dead);
1761                 close(fds[1]);
1762                 if (efd > -1)
1763                         close(efd);
1764         }
1765         LOCAL_USER_REMOVE(u);
1766         return res;
1767 }
1768
1769 static int agi_exec(struct ast_channel *chan, void *data)
1770 {
1771         if (chan->_softhangup)
1772                 ast_log(LOG_WARNING, "If you want to run AGI on hungup channels you should use DeadAGI!\n");
1773         return agi_exec_full(chan, data, 0, 0);
1774 }
1775
1776 static int eagi_exec(struct ast_channel *chan, void *data)
1777 {
1778         int readformat;
1779         int res;
1780         if (chan->_softhangup)
1781                 ast_log(LOG_WARNING, "If you want to run AGI on hungup channels you should use DeadAGI!\n");
1782         readformat = chan->readformat;
1783         if (ast_set_read_format(chan, AST_FORMAT_SLINEAR)) {
1784                 ast_log(LOG_WARNING, "Unable to set channel '%s' to linear mode\n", chan->name);
1785                 return -1;
1786         }
1787         res = agi_exec_full(chan, data, 1, 0);
1788         if (!res) {
1789                 if (ast_set_read_format(chan, readformat)) {
1790                         ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %s\n", chan->name, ast_getformatname(readformat));
1791                 }
1792         }
1793         return res;
1794 }
1795
1796 static int deadagi_exec(struct ast_channel *chan, void *data)
1797 {
1798         return agi_exec_full(chan, data, 0, 1);
1799 }
1800
1801 static char showagi_help[] =
1802 "Usage: show agi [topic]\n"
1803 "       When called with a topic as an argument, displays usage\n"
1804 "       information on the given command.  If called without a\n"
1805 "       topic, it provides a list of AGI commands.\n";
1806
1807
1808 static char dumpagihtml_help[] =
1809 "Usage: dump agihtml <filename>\n"
1810 "       Dumps the agi command list in html format to given filename\n";
1811
1812 static struct ast_cli_entry showagi = 
1813 { { "show", "agi", NULL }, handle_showagi, "Show AGI commands or specific help", showagi_help };
1814
1815 static struct ast_cli_entry dumpagihtml = 
1816 { { "dump", "agihtml", NULL }, handle_dumpagihtml, "Dumps a list of agi command in html format", dumpagihtml_help };
1817
1818 int unload_module(void)
1819 {
1820         STANDARD_HANGUP_LOCALUSERS;
1821         ast_cli_unregister(&showagi);
1822         ast_cli_unregister(&dumpagihtml);
1823         ast_cli_unregister(&cli_debug);
1824         ast_cli_unregister(&cli_no_debug);
1825         ast_unregister_application(eapp);
1826         ast_unregister_application(deadapp);
1827         return ast_unregister_application(app);
1828 }
1829
1830 int load_module(void)
1831 {
1832         ast_cli_register(&showagi);
1833         ast_cli_register(&dumpagihtml);
1834         ast_cli_register(&cli_debug);
1835         ast_cli_register(&cli_no_debug);
1836         ast_register_application(deadapp, deadagi_exec, deadsynopsis, descrip);
1837         ast_register_application(eapp, eagi_exec, esynopsis, descrip);
1838         return ast_register_application(app, agi_exec, synopsis, descrip);
1839 }
1840
1841 char *description(void)
1842 {
1843         return tdesc;
1844 }
1845
1846 int usecount(void)
1847 {
1848         int res;
1849         STANDARD_USECOUNT(res);
1850         return res;
1851 }
1852
1853 char *key()
1854 {
1855         return ASTERISK_GPL_KEY;
1856 }
1857