forked from pttlink/Asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres_agi.c
2201 lines (1938 loc) · 66.5 KB
/
res_agi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2006, Digium, Inc.
*
* Mark Spencer <[email protected]>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief AGI - the Asterisk Gateway Interface
*
* \author Mark Spencer <[email protected]>
*/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 147386 $")
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/astdb.h"
#include "asterisk/callerid.h"
#include "asterisk/cli.h"
#include "asterisk/logger.h"
#include "asterisk/options.h"
#include "asterisk/image.h"
#include "asterisk/say.h"
#include "asterisk/app.h"
#include "asterisk/dsp.h"
#include "asterisk/musiconhold.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/strings.h"
#include "asterisk/agi.h"
#include "asterisk/features.h"
#define MAX_ARGS 128
#define MAX_COMMANDS 128
#define AGI_NANDFS_RETRY 3
#define AGI_BUF_LEN 2048
/* Recycle some stuff from the CLI interface */
#define fdprintf agi_debug_cli
static char *app = "AGI";
static char *eapp = "EAGI";
static char *deadapp = "DeadAGI";
static char *synopsis = "Executes an AGI compliant application";
static char *esynopsis = "Executes an EAGI compliant application";
static char *deadsynopsis = "Executes AGI on a hungup channel";
static char *descrip =
" [E|Dead]AGI(command|args): Executes an Asterisk Gateway Interface compliant\n"
"program on a channel. AGI allows Asterisk to launch external programs\n"
"written in any language to control a telephony channel, play audio,\n"
"read DTMF digits, etc. by communicating with the AGI protocol on stdin\n"
"and stdout.\n"
" This channel will stop dialplan execution on hangup inside of this\n"
"application, except when using DeadAGI. Otherwise, dialplan execution\n"
"will continue normally.\n"
" A locally executed AGI script will receive SIGHUP on hangup from the channel\n"
"except when using DeadAGI. This can be disabled by setting the AGISIGHUP channel\n"
"variable to \"no\" before executing the AGI application.\n"
" Using 'EAGI' provides enhanced AGI, with incoming audio available out of band\n"
"on file descriptor 3\n\n"
" Use the CLI command 'agi show' to list available agi commands\n"
" This application sets the following channel variable upon completion:\n"
" AGISTATUS The status of the attempt to the run the AGI script\n"
" text string, one of SUCCESS | FAILURE | HANGUP\n";
static int agidebug = 0;
#define TONE_BLOCK_SIZE 200
/* Max time to connect to an AGI remote host */
#define MAX_AGI_CONNECT 2000
#define AGI_PORT 4573
enum agi_result {
AGI_RESULT_FAILURE = -1,
AGI_RESULT_SUCCESS,
AGI_RESULT_SUCCESS_FAST,
AGI_RESULT_HANGUP
};
static int agi_debug_cli(int fd, char *fmt, ...)
{
char *stuff;
int res = 0;
va_list ap;
va_start(ap, fmt);
res = vasprintf(&stuff, fmt, ap);
va_end(ap);
if (res == -1) {
ast_log(LOG_ERROR, "Out of memory\n");
} else {
if (agidebug)
ast_verbose("AGI Tx >> %s", stuff); /* \n provided by caller */
res = ast_carefulwrite(fd, stuff, strlen(stuff), 100);
free(stuff);
}
return res;
}
/* launch_netscript: The fastagi handler.
FastAGI defaults to port 4573 */
static enum agi_result launch_netscript(char *agiurl, char *argv[], int *fds, int *efd, int *opid)
{
int s;
int flags;
struct pollfd pfds[1];
char *host;
char *c; int port = AGI_PORT;
char *script="";
struct sockaddr_in sin;
struct hostent *hp;
struct ast_hostent ahp;
int res;
/* agiusl is "agi://host.domain[:port][/script/name]" */
host = ast_strdupa(agiurl + 6); /* Remove agi:// */
/* Strip off any script name */
if ((c = strchr(host, '/'))) {
*c = '\0';
c++;
script = c;
}
if ((c = strchr(host, ':'))) {
*c = '\0';
c++;
port = atoi(c);
}
if (efd) {
ast_log(LOG_WARNING, "AGI URI's don't support Enhanced AGI yet\n");
return -1;
}
hp = ast_gethostbyname(host, &ahp);
if (!hp) {
ast_log(LOG_WARNING, "Unable to locate host '%s'\n", host);
return -1;
}
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
return -1;
}
flags = fcntl(s, F_GETFL);
if (flags < 0) {
ast_log(LOG_WARNING, "Fcntl(F_GETFL) failed: %s\n", strerror(errno));
close(s);
return -1;
}
if (fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) {
ast_log(LOG_WARNING, "Fnctl(F_SETFL) failed: %s\n", strerror(errno));
close(s);
return -1;
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) && (errno != EINPROGRESS)) {
ast_log(LOG_WARNING, "Connect failed with unexpected error: %s\n", strerror(errno));
close(s);
return AGI_RESULT_FAILURE;
}
pfds[0].fd = s;
pfds[0].events = POLLOUT;
while ((res = poll(pfds, 1, MAX_AGI_CONNECT)) != 1) {
if (errno != EINTR) {
if (!res) {
ast_log(LOG_WARNING, "FastAGI connection to '%s' timed out after MAX_AGI_CONNECT (%d) milliseconds.\n",
agiurl, MAX_AGI_CONNECT);
} else
ast_log(LOG_WARNING, "Connect to '%s' failed: %s\n", agiurl, strerror(errno));
close(s);
return AGI_RESULT_FAILURE;
}
}
if (fdprintf(s, "agi_network: yes\n") < 0) {
if (errno != EINTR) {
ast_log(LOG_WARNING, "Connect to '%s' failed: %s\n", agiurl, strerror(errno));
close(s);
return AGI_RESULT_FAILURE;
}
}
/* If we have a script parameter, relay it to the fastagi server */
if (!ast_strlen_zero(script))
fdprintf(s, "agi_network_script: %s\n", script);
if (option_debug > 3)
ast_log(LOG_DEBUG, "Wow, connected!\n");
fds[0] = s;
fds[1] = s;
*opid = -1;
return AGI_RESULT_SUCCESS_FAST;
}
static enum agi_result launch_script(char *script, char *argv[], int *fds, int *efd, int *opid)
{
char tmp[256];
int pid;
int toast[2];
int fromast[2];
int audio[2];
int x;
int res;
sigset_t signal_set, old_set;
if (!strncasecmp(script, "agi://", 6))
return launch_netscript(script, argv, fds, efd, opid);
if (script[0] != '/') {
snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_AGI_DIR, script);
script = tmp;
}
if (pipe(toast)) {
ast_log(LOG_WARNING, "Unable to create toast pipe: %s\n",strerror(errno));
return AGI_RESULT_FAILURE;
}
if (pipe(fromast)) {
ast_log(LOG_WARNING, "unable to create fromast pipe: %s\n", strerror(errno));
close(toast[0]);
close(toast[1]);
return AGI_RESULT_FAILURE;
}
if (efd) {
if (pipe(audio)) {
ast_log(LOG_WARNING, "unable to create audio pipe: %s\n", strerror(errno));
close(fromast[0]);
close(fromast[1]);
close(toast[0]);
close(toast[1]);
return AGI_RESULT_FAILURE;
}
res = fcntl(audio[1], F_GETFL);
if (res > -1)
res = fcntl(audio[1], F_SETFL, res | O_NONBLOCK);
if (res < 0) {
ast_log(LOG_WARNING, "unable to set audio pipe parameters: %s\n", strerror(errno));
close(fromast[0]);
close(fromast[1]);
close(toast[0]);
close(toast[1]);
close(audio[0]);
close(audio[1]);
return AGI_RESULT_FAILURE;
}
}
/* Block SIGHUP during the fork - prevents a race */
sigfillset(&signal_set);
pthread_sigmask(SIG_BLOCK, &signal_set, &old_set);
pid = fork();
if (pid < 0) {
ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno));
pthread_sigmask(SIG_SETMASK, &old_set, NULL);
return AGI_RESULT_FAILURE;
}
if (!pid) {
/* Pass paths to AGI via environmental variables */
setenv("AST_CONFIG_DIR", ast_config_AST_CONFIG_DIR, 1);
setenv("AST_CONFIG_FILE", ast_config_AST_CONFIG_FILE, 1);
setenv("AST_MODULE_DIR", ast_config_AST_MODULE_DIR, 1);
setenv("AST_SPOOL_DIR", ast_config_AST_SPOOL_DIR, 1);
setenv("AST_MONITOR_DIR", ast_config_AST_MONITOR_DIR, 1);
setenv("AST_VAR_DIR", ast_config_AST_VAR_DIR, 1);
setenv("AST_DATA_DIR", ast_config_AST_DATA_DIR, 1);
setenv("AST_LOG_DIR", ast_config_AST_LOG_DIR, 1);
setenv("AST_AGI_DIR", ast_config_AST_AGI_DIR, 1);
setenv("AST_KEY_DIR", ast_config_AST_KEY_DIR, 1);
setenv("AST_RUN_DIR", ast_config_AST_RUN_DIR, 1);
/* Don't run AGI scripts with realtime priority -- it causes audio stutter */
ast_set_priority(0);
/* Redirect stdin and out, provide enhanced audio channel if desired */
dup2(fromast[0], STDIN_FILENO);
dup2(toast[1], STDOUT_FILENO);
if (efd) {
dup2(audio[0], STDERR_FILENO + 1);
} else {
close(STDERR_FILENO + 1);
}
/* Before we unblock our signals, return our trapped signals back to the defaults */
signal(SIGHUP, SIG_DFL);
signal(SIGCHLD, SIG_DFL);
signal(SIGINT, SIG_DFL);
signal(SIGURG, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
signal(SIGXFSZ, SIG_DFL);
/* unblock important signal handlers */
if (pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL)) {
ast_log(LOG_WARNING, "unable to unblock signals for AGI script: %s\n", strerror(errno));
_exit(1);
}
/* Close everything but stdin/out/error */
for (x=STDERR_FILENO + 2;x<1024;x++)
close(x);
/* Execute script */
execv(script, argv);
/* Can't use ast_log since FD's are closed */
fprintf(stdout, "verbose \"Failed to execute '%s': %s\" 2\n", script, strerror(errno));
/* Special case to set status of AGI to failure */
fprintf(stdout, "failure\n");
fflush(stdout);
_exit(1);
}
pthread_sigmask(SIG_SETMASK, &old_set, NULL);
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Launched AGI Script %s\n", script);
fds[0] = toast[0];
fds[1] = fromast[1];
if (efd) {
*efd = audio[1];
}
/* close what we're not using in the parent */
close(toast[1]);
close(fromast[0]);
if (efd)
close(audio[0]);
*opid = pid;
return AGI_RESULT_SUCCESS;
}
static void setup_env(struct ast_channel *chan, char *request, int fd, int enhanced)
{
/* Print initial environment, with agi_request always being the first
thing */
fdprintf(fd, "agi_request: %s\n", request);
fdprintf(fd, "agi_channel: %s\n", chan->name);
fdprintf(fd, "agi_language: %s\n", chan->language);
fdprintf(fd, "agi_type: %s\n", chan->tech->type);
fdprintf(fd, "agi_uniqueid: %s\n", chan->uniqueid);
/* ANI/DNIS */
fdprintf(fd, "agi_callerid: %s\n", S_OR(chan->cid.cid_num, "unknown"));
fdprintf(fd, "agi_calleridname: %s\n", S_OR(chan->cid.cid_name, "unknown"));
fdprintf(fd, "agi_callingpres: %d\n", chan->cid.cid_pres);
fdprintf(fd, "agi_callingani2: %d\n", chan->cid.cid_ani2);
fdprintf(fd, "agi_callington: %d\n", chan->cid.cid_ton);
fdprintf(fd, "agi_callingtns: %d\n", chan->cid.cid_tns);
fdprintf(fd, "agi_dnid: %s\n", S_OR(chan->cid.cid_dnid, "unknown"));
fdprintf(fd, "agi_rdnis: %s\n", S_OR(chan->cid.cid_rdnis, "unknown"));
/* Context information */
fdprintf(fd, "agi_context: %s\n", chan->context);
fdprintf(fd, "agi_extension: %s\n", chan->exten);
fdprintf(fd, "agi_priority: %d\n", chan->priority);
fdprintf(fd, "agi_enhanced: %s\n", enhanced ? "1.0" : "0.0");
/* User information */
fdprintf(fd, "agi_accountcode: %s\n", chan->accountcode ? chan->accountcode : "");
/* End with empty return */
fdprintf(fd, "\n");
}
static int handle_answer(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
res = 0;
if (chan->_state != AST_STATE_UP) {
/* Answer the chan */
res = ast_answer(chan);
}
fdprintf(agi->fd, "200 result=%d\n", res);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_waitfordigit(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
int to;
if (argc != 4)
return RESULT_SHOWUSAGE;
if (sscanf(argv[3], "%d", &to) != 1)
return RESULT_SHOWUSAGE;
res = ast_waitfordigit_full(chan, to, agi->audio, agi->ctrl);
fdprintf(agi->fd, "200 result=%d\n", res);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_sendtext(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
if (argc != 3)
return RESULT_SHOWUSAGE;
/* At the moment, the parser (perhaps broken) returns with
the last argument PLUS the newline at the end of the input
buffer. This probably needs to be fixed, but I wont do that
because other stuff may break as a result. The right way
would probably be to strip off the trailing newline before
parsing, then here, add a newline at the end of the string
before sending it to ast_sendtext --DUDE */
res = ast_sendtext(chan, argv[2]);
fdprintf(agi->fd, "200 result=%d\n", res);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_recvchar(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
if (argc != 3)
return RESULT_SHOWUSAGE;
res = ast_recvchar(chan,atoi(argv[2]));
if (res == 0) {
fdprintf(agi->fd, "200 result=%d (timeout)\n", res);
return RESULT_SUCCESS;
}
if (res > 0) {
fdprintf(agi->fd, "200 result=%d\n", res);
return RESULT_SUCCESS;
}
else {
fdprintf(agi->fd, "200 result=%d (hangup)\n", res);
return RESULT_FAILURE;
}
}
static int handle_recvtext(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
char *buf;
if (argc != 3)
return RESULT_SHOWUSAGE;
buf = ast_recvtext(chan,atoi(argv[2]));
if (buf) {
fdprintf(agi->fd, "200 result=1 (%s)\n", buf);
free(buf);
} else {
fdprintf(agi->fd, "200 result=-1\n");
}
return RESULT_SUCCESS;
}
static int handle_tddmode(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res,x;
if (argc != 3)
return RESULT_SHOWUSAGE;
if (!strncasecmp(argv[2],"on",2))
x = 1;
else
x = 0;
if (!strncasecmp(argv[2],"mate",4))
x = 2;
if (!strncasecmp(argv[2],"tdd",3))
x = 1;
res = ast_channel_setoption(chan, AST_OPTION_TDD, &x, sizeof(char), 0);
if (res != RESULT_SUCCESS)
fdprintf(agi->fd, "200 result=0\n");
else
fdprintf(agi->fd, "200 result=1\n");
return RESULT_SUCCESS;
}
static int handle_sendimage(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
if (argc != 3)
return RESULT_SHOWUSAGE;
res = ast_send_image(chan, argv[2]);
if (!ast_check_hangup(chan))
res = 0;
fdprintf(agi->fd, "200 result=%d\n", res);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_controlstreamfile(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res = 0;
int skipms = 3000;
char *fwd = NULL;
char *rev = NULL;
char *pause = NULL;
char *stop = NULL;
if (argc < 5 || argc > 9)
return RESULT_SHOWUSAGE;
if (!ast_strlen_zero(argv[4]))
stop = argv[4];
else
stop = NULL;
if ((argc > 5) && (sscanf(argv[5], "%d", &skipms) != 1))
return RESULT_SHOWUSAGE;
if (argc > 6 && !ast_strlen_zero(argv[6]))
fwd = argv[6];
else
fwd = "#";
if (argc > 7 && !ast_strlen_zero(argv[7]))
rev = argv[7];
else
rev = "*";
if (argc > 8 && !ast_strlen_zero(argv[8]))
pause = argv[8];
else
pause = NULL;
res = ast_control_streamfile(chan, argv[3], fwd, rev, stop, pause, NULL, skipms);
fdprintf(agi->fd, "200 result=%d\n", res);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_streamfile(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
int vres;
struct ast_filestream *fs;
struct ast_filestream *vfs;
long sample_offset = 0;
long max_length;
char *edigits = "";
if (argc < 4 || argc > 5)
return RESULT_SHOWUSAGE;
if (argv[3])
edigits = argv[3];
if ((argc > 4) && (sscanf(argv[4], "%ld", &sample_offset) != 1))
return RESULT_SHOWUSAGE;
fs = ast_openstream(chan, argv[2], chan->language);
if (!fs) {
fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
return RESULT_SUCCESS;
}
vfs = ast_openvstream(chan, argv[2], chan->language);
if (vfs)
ast_log(LOG_DEBUG, "Ooh, found a video stream, too\n");
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Playing '%s' (escape_digits=%s) (sample_offset %ld)\n", argv[2], edigits, sample_offset);
ast_seekstream(fs, 0, SEEK_END);
max_length = ast_tellstream(fs);
ast_seekstream(fs, sample_offset, SEEK_SET);
res = ast_applystream(chan, fs);
if (vfs)
vres = ast_applystream(chan, vfs);
ast_playstream(fs);
if (vfs)
ast_playstream(vfs);
res = ast_waitstream_full(chan, argv[3], agi->audio, agi->ctrl);
/* this is to check for if ast_waitstream closed the stream, we probably are at
* the end of the stream, return that amount, else check for the amount */
sample_offset = (chan->stream) ? ast_tellstream(fs) : max_length;
ast_stopstream(chan);
if (res == 1) {
/* Stop this command, don't print a result line, as there is a new command */
return RESULT_SUCCESS;
}
fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
/* get option - really similar to the handle_streamfile, but with a timeout */
static int handle_getoption(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
int vres;
struct ast_filestream *fs;
struct ast_filestream *vfs;
long sample_offset = 0;
long max_length;
int timeout = 0;
char *edigits = "";
if ( argc < 4 || argc > 5 )
return RESULT_SHOWUSAGE;
if ( argv[3] )
edigits = argv[3];
if ( argc == 5 )
timeout = atoi(argv[4]);
else if (chan->pbx->dtimeout) {
/* by default dtimeout is set to 5sec */
timeout = chan->pbx->dtimeout * 1000; /* in msec */
}
fs = ast_openstream(chan, argv[2], chan->language);
if (!fs) {
fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
ast_log(LOG_WARNING, "Unable to open %s\n", argv[2]);
return RESULT_SUCCESS;
}
vfs = ast_openvstream(chan, argv[2], chan->language);
if (vfs)
ast_log(LOG_DEBUG, "Ooh, found a video stream, too\n");
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Playing '%s' (escape_digits=%s) (timeout %d)\n", argv[2], edigits, timeout);
ast_seekstream(fs, 0, SEEK_END);
max_length = ast_tellstream(fs);
ast_seekstream(fs, sample_offset, SEEK_SET);
res = ast_applystream(chan, fs);
if (vfs)
vres = ast_applystream(chan, vfs);
ast_playstream(fs);
if (vfs)
ast_playstream(vfs);
res = ast_waitstream_full(chan, argv[3], agi->audio, agi->ctrl);
/* this is to check for if ast_waitstream closed the stream, we probably are at
* the end of the stream, return that amount, else check for the amount */
sample_offset = (chan->stream)?ast_tellstream(fs):max_length;
ast_stopstream(chan);
if (res == 1) {
/* Stop this command, don't print a result line, as there is a new command */
return RESULT_SUCCESS;
}
/* If the user didnt press a key, wait for digitTimeout*/
if (res == 0 ) {
res = ast_waitfordigit_full(chan, timeout, agi->audio, agi->ctrl);
/* Make sure the new result is in the escape digits of the GET OPTION */
if ( !strchr(edigits,res) )
res=0;
}
fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
/*--- handle_saynumber: Say number in various language syntaxes ---*/
/* Need to add option for gender here as well. Coders wanted */
/* While waiting, we're sending a (char *) NULL. */
static int handle_saynumber(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
int num;
if (argc != 4)
return RESULT_SHOWUSAGE;
if (sscanf(argv[2], "%d", &num) != 1)
return RESULT_SHOWUSAGE;
res = ast_say_number_full(chan, num, argv[3], chan->language, (char *) NULL, agi->audio, agi->ctrl);
if (res == 1)
return RESULT_SUCCESS;
fdprintf(agi->fd, "200 result=%d\n", res);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_saydigits(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
int num;
if (argc != 4)
return RESULT_SHOWUSAGE;
if (sscanf(argv[2], "%d", &num) != 1)
return RESULT_SHOWUSAGE;
res = ast_say_digit_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
if (res == 1) /* New command */
return RESULT_SUCCESS;
fdprintf(agi->fd, "200 result=%d\n", res);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_sayalpha(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
if (argc != 4)
return RESULT_SHOWUSAGE;
res = ast_say_character_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
if (res == 1) /* New command */
return RESULT_SUCCESS;
fdprintf(agi->fd, "200 result=%d\n", res);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_saydate(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
int num;
if (argc != 4)
return RESULT_SHOWUSAGE;
if (sscanf(argv[2], "%d", &num) != 1)
return RESULT_SHOWUSAGE;
res = ast_say_date(chan, num, argv[3], chan->language);
if (res == 1)
return RESULT_SUCCESS;
fdprintf(agi->fd, "200 result=%d\n", res);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_saytime(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
int num;
if (argc != 4)
return RESULT_SHOWUSAGE;
if (sscanf(argv[2], "%d", &num) != 1)
return RESULT_SHOWUSAGE;
res = ast_say_time(chan, num, argv[3], chan->language);
if (res == 1)
return RESULT_SUCCESS;
fdprintf(agi->fd, "200 result=%d\n", res);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_saydatetime(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res=0;
time_t unixtime;
char *format, *zone=NULL;
if (argc < 4)
return RESULT_SHOWUSAGE;
if (argc > 4) {
format = argv[4];
} else {
/* XXX this doesn't belong here, but in the 'say' module */
if (!strcasecmp(chan->language, "de")) {
format = "A dBY HMS";
} else {
format = "ABdY 'digits/at' IMp";
}
}
if (argc > 5 && !ast_strlen_zero(argv[5]))
zone = argv[5];
if (ast_get_time_t(argv[2], &unixtime, 0, NULL))
return RESULT_SHOWUSAGE;
res = ast_say_date_with_format(chan, unixtime, argv[3], chan->language, format, zone);
if (res == 1)
return RESULT_SUCCESS;
fdprintf(agi->fd, "200 result=%d\n", res);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_sayphonetic(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
if (argc != 4)
return RESULT_SHOWUSAGE;
res = ast_say_phonetic_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
if (res == 1) /* New command */
return RESULT_SUCCESS;
fdprintf(agi->fd, "200 result=%d\n", res);
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
}
static int handle_getdata(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
int res;
char data[1024];
int max;
int timeout;
if (argc < 3)
return RESULT_SHOWUSAGE;
if (argc >= 4)
timeout = atoi(argv[3]);
else
timeout = 0;
if (argc >= 5)
max = atoi(argv[4]);
else
max = 1024;
res = ast_app_getdata_full(chan, argv[2], data, max, timeout, agi->audio, agi->ctrl);
if (res == 2) /* New command */
return RESULT_SUCCESS;
else if (res == 1)
fdprintf(agi->fd, "200 result=%s (timeout)\n", data);
else if (res < 0 )
fdprintf(agi->fd, "200 result=-1\n");
else
fdprintf(agi->fd, "200 result=%s\n", data);
return RESULT_SUCCESS;
}
static int handle_setcontext(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
if (argc != 3)
return RESULT_SHOWUSAGE;
ast_copy_string(chan->context, argv[2], sizeof(chan->context));
fdprintf(agi->fd, "200 result=0\n");
return RESULT_SUCCESS;
}
static int handle_setextension(struct ast_channel *chan, AGI *agi, int argc, char **argv)
{
if (argc != 3)
return RESULT_SHOWUSAGE;
ast_copy_string(chan->exten, argv[2], sizeof(chan->exten));
fdprintf(agi->fd, "200 result=0\n");
return RESULT_SUCCESS;
}
static int handle_setpriority(struct ast_channel *chan, AGI *agi, int argc, char **argv)
{
int pri;
if (argc != 3)
return RESULT_SHOWUSAGE;
if (sscanf(argv[2], "%d", &pri) != 1) {
if ((pri = ast_findlabel_extension(chan, chan->context, chan->exten, argv[2], chan->cid.cid_num)) < 1)
return RESULT_SHOWUSAGE;
}
ast_explicit_goto(chan, NULL, NULL, pri);
fdprintf(agi->fd, "200 result=0\n");
return RESULT_SUCCESS;
}
static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
{
struct ast_filestream *fs;
struct ast_frame *f;
struct timeval start;
long sample_offset = 0;
int res = 0;
int ms;
struct ast_dsp *sildet=NULL; /* silence detector dsp */
int totalsilence = 0;
int dspsilence = 0;
int silence = 0; /* amount of silence to allow */
int gotsilence = 0; /* did we timeout for silence? */
char *silencestr=NULL;
int rfmt=0;
/* XXX EAGI FIXME XXX */
if (argc < 6)
return RESULT_SHOWUSAGE;
if (sscanf(argv[5], "%d", &ms) != 1)
return RESULT_SHOWUSAGE;
if (argc > 6)
silencestr = strchr(argv[6],'s');
if ((argc > 7) && (!silencestr))
silencestr = strchr(argv[7],'s');
if ((argc > 8) && (!silencestr))
silencestr = strchr(argv[8],'s');
if (silencestr) {
if (strlen(silencestr) > 2) {
if ((silencestr[0] == 's') && (silencestr[1] == '=')) {
silencestr++;
silencestr++;
if (silencestr)
silence = atoi(silencestr);
if (silence > 0)
silence *= 1000;
}
}
}
if (silence > 0) {
rfmt = chan->readformat;
res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
if (res < 0) {
ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
return -1;
}
sildet = ast_dsp_new();
if (!sildet) {
ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
return -1;
}
ast_dsp_set_threshold(sildet, 256);
}
/* backward compatibility, if no offset given, arg[6] would have been
* caught below and taken to be a beep, else if it is a digit then it is a
* offset */
if ((argc >6) && (sscanf(argv[6], "%ld", &sample_offset) != 1) && (!strchr(argv[6], '=')))
res = ast_streamfile(chan, "beep", chan->language);
if ((argc > 7) && (!strchr(argv[7], '=')))
res = ast_streamfile(chan, "beep", chan->language);
if (!res)
res = ast_waitstream(chan, argv[4]);
if (res) {
fdprintf(agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset);
} else {
fs = ast_writefile(argv[2], argv[3], NULL, O_CREAT | O_WRONLY | (sample_offset ? O_APPEND : 0), 0, 0644);
if (!fs) {
res = -1;
fdprintf(agi->fd, "200 result=%d (writefile)\n", res);
if (sildet)
ast_dsp_free(sildet);
return RESULT_FAILURE;
}
/* Request a video update */
ast_indicate(chan, AST_CONTROL_VIDUPDATE);
chan->stream = fs;
ast_applystream(chan,fs);
/* really should have checks */
ast_seekstream(fs, sample_offset, SEEK_SET);
ast_truncstream(fs);
start = ast_tvnow();
while ((ms < 0) || ast_tvdiff_ms(ast_tvnow(), start) < ms) {
res = ast_waitfor(chan, -1);
if (res < 0) {
ast_closestream(fs);
fdprintf(agi->fd, "200 result=%d (waitfor) endpos=%ld\n", res,sample_offset);
if (sildet)
ast_dsp_free(sildet);
return RESULT_FAILURE;
}
f = ast_read(chan);
if (!f) {
fdprintf(agi->fd, "200 result=%d (hangup) endpos=%ld\n", -1, sample_offset);
ast_closestream(fs);
if (sildet)
ast_dsp_free(sildet);
return RESULT_FAILURE;
}
switch(f->frametype) {
case AST_FRAME_DTMF:
if (strchr(argv[4], f->subclass)) {
/* This is an interrupting chracter, so rewind to chop off any small
amount of DTMF that may have been recorded
*/
ast_stream_rewind(fs, 200);
ast_truncstream(fs);
sample_offset = ast_tellstream(fs);
fdprintf(agi->fd, "200 result=%d (dtmf) endpos=%ld\n", f->subclass, sample_offset);
ast_closestream(fs);
ast_frfree(f);