Update chanelog
[asterisk/asterisk.git] / app.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Convenient Application Routines
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 <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/time.h>
18 #include <signal.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <dirent.h>
22 #include <asterisk/channel.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/file.h>
25 #include <asterisk/app.h>
26 #include <asterisk/dsp.h>
27 #include <asterisk/logger.h>
28 #include <asterisk/options.h>
29 #include <asterisk/utils.h>
30 #include <asterisk/lock.h>
31 #include "asterisk.h"
32 #include "astconf.h"
33
34 #define MAX_OTHER_FORMATS 10
35
36 /* set timeout to 0 for "standard" timeouts. Set timeout to -1 for 
37    "ludicrous time" (essentially never times out) */
38 int ast_app_getdata(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout)
39 {
40         int res,to,fto;
41         /* XXX Merge with full version? XXX */
42         if (maxlen)
43                 s[0] = '\0';
44         if (prompt) {
45                 res = ast_streamfile(c, prompt, c->language);
46                 if (res < 0)
47                         return res;
48         }
49         fto = c->pbx ? c->pbx->rtimeout * 1000 : 6000;
50         to = c->pbx ? c->pbx->dtimeout * 1000 : 2000;
51
52         if (timeout > 0) fto = to = timeout;
53         if (timeout < 0) fto = to = 1000000000;
54         res = ast_readstring(c, s, maxlen, to, fto, "#");
55         return res;
56 }
57
58
59 int ast_app_getdata_full(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout, int audiofd, int ctrlfd)
60 {
61         int res,to,fto;
62         if (prompt) {
63                 res = ast_streamfile(c, prompt, c->language);
64                 if (res < 0)
65                         return res;
66         }
67         fto = 6000;
68         to = 2000;
69         if (timeout > 0) fto = to = timeout;
70         if (timeout < 0) fto = to = 1000000000;
71         res = ast_readstring_full(c, s, maxlen, to, fto, "#", audiofd, ctrlfd);
72         return res;
73 }
74
75 int ast_app_getvoice(struct ast_channel *c, char *dest, char *dstfmt, char *prompt, int silence, int maxsec)
76 {
77         int res;
78         struct ast_filestream *writer;
79         int rfmt;
80         int totalms=0, total;
81         
82         struct ast_frame *f;
83         struct ast_dsp *sildet;
84         /* Play prompt if requested */
85         if (prompt) {
86                 res = ast_streamfile(c, prompt, c->language);
87                 if (res < 0)
88                         return res;
89                 res = ast_waitstream(c,"");
90                 if (res < 0)
91                         return res;
92         }
93         rfmt = c->readformat;
94         res = ast_set_read_format(c, AST_FORMAT_SLINEAR);
95         if (res < 0) {
96                 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
97                 return -1;
98         }
99         sildet = ast_dsp_new();
100         if (!sildet) {
101                 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
102                 return -1;
103         }
104         writer = ast_writefile(dest, dstfmt, "Voice file", 0, 0, 0666);
105         if (!writer) {
106                 ast_log(LOG_WARNING, "Unable to open file '%s' in format '%s' for writing\n", dest, dstfmt);
107                 ast_dsp_free(sildet);
108                 return -1;
109         }
110         for(;;) {
111                 if ((res = ast_waitfor(c, 2000)) < 0) {
112                         ast_log(LOG_NOTICE, "Waitfor failed while recording file '%s' format '%s'\n", dest, dstfmt);
113                         break;
114                 }
115                 if (res) {
116                         f = ast_read(c);
117                         if (!f) {
118                                 ast_log(LOG_NOTICE, "Hungup while recording file '%s' format '%s'\n", dest, dstfmt);
119                                 break;
120                         }
121                         if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
122                                 /* Ended happily with DTMF */
123                                 ast_frfree(f);
124                                 break;
125                         } else if (f->frametype == AST_FRAME_VOICE) {
126                                 ast_dsp_silence(sildet, f, &total); 
127                                 if (total > silence) {
128                                         /* Ended happily with silence */
129                                         ast_frfree(f);
130                                         break;
131                                 }
132                                 totalms += f->samples / 8;
133                                 if (totalms > maxsec * 1000) {
134                                         /* Ended happily with too much stuff */
135                                         ast_log(LOG_NOTICE, "Constraining voice on '%s' to %d seconds\n", c->name, maxsec);
136                                         ast_frfree(f);
137                                         break;
138                                 }
139                         }
140                         ast_frfree(f);
141                 }
142         }
143         res = ast_set_read_format(c, rfmt);
144         if (res)
145                 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", c->name);
146         ast_dsp_free(sildet);
147         ast_closestream(writer);
148         return 0;
149 }
150
151 int ast_app_has_voicemail(const char *mailbox)
152 {
153         DIR *dir;
154         struct dirent *de;
155         char fn[256];
156         char tmp[256]="";
157         char *mb, *cur;
158         char *context;
159         int ret;
160         /* If no mailbox, return immediately */
161         if (ast_strlen_zero(mailbox))
162                 return 0;
163         if (strchr(mailbox, ',')) {
164                 strncpy(tmp, mailbox, sizeof(tmp) - 1);
165                 mb = tmp;
166                 ret = 0;
167                 while((cur = strsep(&mb, ","))) {
168                         if (!ast_strlen_zero(cur)) {
169                                 if (ast_app_has_voicemail(cur))
170                                         return 1; 
171                         }
172                 }
173                 return 0;
174         }
175         strncpy(tmp, mailbox, sizeof(tmp) - 1);
176         context = strchr(tmp, '@');
177         if (context) {
178                 *context = '\0';
179                 context++;
180         } else
181                 context = "default";
182         snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/INBOX", (char *)ast_config_AST_SPOOL_DIR, context, tmp);
183         dir = opendir(fn);
184         if (!dir)
185                 return 0;
186         while ((de = readdir(dir))) {
187                 if (!strncasecmp(de->d_name, "msg", 3))
188                         break;
189         }
190         closedir(dir);
191         if (de)
192                 return 1;
193         return 0;
194 }
195
196 int ast_app_messagecount(const char *mailbox, int *newmsgs, int *oldmsgs)
197 {
198         DIR *dir;
199         struct dirent *de;
200         char fn[256];
201         char tmp[256]="";
202         char *mb, *cur;
203         char *context;
204         int ret;
205         if (newmsgs)
206                 *newmsgs = 0;
207         if (oldmsgs)
208                 *oldmsgs = 0;
209         /* If no mailbox, return immediately */
210         if (ast_strlen_zero(mailbox))
211                 return 0;
212         if (strchr(mailbox, ',')) {
213                 int tmpnew, tmpold;
214                 strncpy(tmp, mailbox, sizeof(tmp) - 1);
215                 mb = tmp;
216                 ret = 0;
217                 while((cur = strsep(&mb, ", "))) {
218                         if (!ast_strlen_zero(cur)) {
219                                 if (ast_app_messagecount(cur, newmsgs ? &tmpnew : NULL, oldmsgs ? &tmpold : NULL))
220                                         return -1;
221                                 else {
222                                         if (newmsgs)
223                                                 *newmsgs += tmpnew; 
224                                         if (oldmsgs)
225                                                 *oldmsgs += tmpold;
226                                 }
227                         }
228                 }
229                 return 0;
230         }
231         strncpy(tmp, mailbox, sizeof(tmp) - 1);
232         context = strchr(tmp, '@');
233         if (context) {
234                 *context = '\0';
235                 context++;
236         } else
237                 context = "default";
238         if (newmsgs) {
239                 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/INBOX", (char *)ast_config_AST_SPOOL_DIR, context, tmp);
240                 dir = opendir(fn);
241                 if (dir) {
242                         while ((de = readdir(dir))) {
243                                 if ((strlen(de->d_name) > 3) && !strncasecmp(de->d_name, "msg", 3) &&
244                                         !strcasecmp(de->d_name + strlen(de->d_name) - 3, "txt"))
245                                                 (*newmsgs)++;
246                                         
247                         }
248                         closedir(dir);
249                 }
250         }
251         if (oldmsgs) {
252                 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/Old", (char *)ast_config_AST_SPOOL_DIR, context, tmp);
253                 dir = opendir(fn);
254                 if (dir) {
255                         while ((de = readdir(dir))) {
256                                 if ((strlen(de->d_name) > 3) && !strncasecmp(de->d_name, "msg", 3) &&
257                                         !strcasecmp(de->d_name + strlen(de->d_name) - 3, "txt"))
258                                                 (*oldmsgs)++;
259                                         
260                         }
261                         closedir(dir);
262                 }
263         }
264         return 0;
265 }
266
267 int ast_dtmf_stream(struct ast_channel *chan,struct ast_channel *peer,char *digits,int between) 
268 {
269         char *ptr=NULL;
270         int res=0;
271         struct ast_frame f;
272         if (!between)
273                 between = 100;
274
275         if (peer)
276                 res = ast_autoservice_start(peer);
277
278         if (!res) {
279                 res = ast_waitfor(chan,100);
280                 if (res > -1) {
281                         for (ptr=digits;*ptr;*ptr++) {
282                                 if (*ptr == 'w') {
283                                         res = ast_safe_sleep(chan, 500);
284                                         if (res) 
285                                                 break;
286                                         continue;
287                                 }
288                                 memset(&f, 0, sizeof(f));
289                                 f.frametype = AST_FRAME_DTMF;
290                                 f.subclass = *ptr;
291                                 f.src = "ast_dtmf_stream";
292                                 if (strchr("0123456789*#abcdABCD",*ptr)==NULL) {
293                                         ast_log(LOG_WARNING, "Illegal DTMF character '%c' in string. (0-9*#aAbBcCdD allowed)\n",*ptr);
294                                 } else {
295                                         res = ast_write(chan, &f);
296                                         if (res) 
297                                                 break;
298                                         /* pause between digits */
299                                         res = ast_safe_sleep(chan,between);
300                                         if (res) 
301                                                 break;
302                                 }
303                         }
304                 }
305                 if (peer)
306                         res = ast_autoservice_stop(peer);
307         }
308         return res;
309 }
310
311 struct linear_state {
312         int fd;
313         int autoclose;
314         int allowoverride;
315         int origwfmt;
316 };
317
318 static void linear_release(struct ast_channel *chan, void *params)
319 {
320         struct linear_state *ls = params;
321         if (ls->origwfmt && ast_set_write_format(chan, ls->origwfmt)) {
322                 ast_log(LOG_WARNING, "Unable to restore channel '%s' to format '%d'\n", chan->name, ls->origwfmt);
323         }
324         if (ls->autoclose)
325                 close(ls->fd);
326         free(params);
327 }
328
329 static int linear_generator(struct ast_channel *chan, void *data, int len, int samples)
330 {
331         struct ast_frame f;
332         short buf[2048 + AST_FRIENDLY_OFFSET / 2];
333         struct linear_state *ls = data;
334         int res;
335         len = samples * 2;
336         if (len > sizeof(buf) - AST_FRIENDLY_OFFSET) {
337                 ast_log(LOG_WARNING, "Can't generate %d bytes of data!\n" ,len);
338                 len = sizeof(buf) - AST_FRIENDLY_OFFSET;
339         }
340         memset(&f, 0, sizeof(f));
341         res = read(ls->fd, buf + AST_FRIENDLY_OFFSET/2, len);
342         if (res > 0) {
343                 f.frametype = AST_FRAME_VOICE;
344                 f.subclass = AST_FORMAT_SLINEAR;
345                 f.data = buf + AST_FRIENDLY_OFFSET/2;
346                 f.datalen = res;
347                 f.samples = res / 2;
348                 f.offset = AST_FRIENDLY_OFFSET;
349                 ast_write(chan, &f);
350                 if (res == len)
351                         return 0;
352         }
353         return -1;
354 }
355
356 static void *linear_alloc(struct ast_channel *chan, void *params)
357 {
358         struct linear_state *ls;
359         /* In this case, params is already malloc'd */
360         if (params) {
361                 ls = params;
362                 if (ls->allowoverride)
363                         chan->writeinterrupt = 1;
364                 else
365                         chan->writeinterrupt = 0;
366                 ls->origwfmt = chan->writeformat;
367                 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
368                         ast_log(LOG_WARNING, "Unable to set '%s' to linear format (write)\n", chan->name);
369                         free(ls);
370                         ls = params = NULL;
371                 }
372         }
373         return params;
374 }
375
376 static struct ast_generator linearstream = 
377 {
378         alloc: linear_alloc,
379         release: linear_release,
380         generate: linear_generator,
381 };
382
383 int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, int allowoverride)
384 {
385         struct linear_state *lin;
386         char tmpf[256] = "";
387         int res = -1;
388         int autoclose = 0;
389         if (fd < 0) {
390                 if (!filename || ast_strlen_zero(filename))
391                         return -1;
392                 autoclose = 1;
393                 if (filename[0] == '/') 
394                         strncpy(tmpf, filename, sizeof(tmpf) - 1);
395                 else
396                         snprintf(tmpf, sizeof(tmpf), "%s/%s/%s", (char *)ast_config_AST_VAR_DIR, "sounds", filename);
397                 fd = open(tmpf, O_RDONLY);
398                 if (fd < 0){
399                         ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", tmpf, strerror(errno));
400                         return -1;
401                 }
402         }
403         lin = malloc(sizeof(struct linear_state));
404         if (lin) {
405                 memset(lin, 0, sizeof(lin));
406                 lin->fd = fd;
407                 lin->allowoverride = allowoverride;
408                 lin->autoclose = autoclose;
409                 res = ast_activate_generator(chan, &linearstream, lin);
410         }
411         return res;
412 }
413
414 int ast_control_streamfile(struct ast_channel *chan, char *file, char *fwd, char *rev, char *stop, char *pause, int skipms) 
415 {
416         struct timeval started, ended;
417         long elapsed = 0,last_elapsed =0;
418         char *breaks=NULL;
419         char *end=NULL;
420         int blen=2;
421         int res=0;
422
423         if (stop)
424                 blen += strlen(stop);
425         if (pause)
426                 blen += strlen(pause);
427
428         if (blen > 2) {
429                 breaks = alloca(blen + 1);
430                 breaks[0] = '\0';
431                 strcat(breaks, stop);
432                 strcat(breaks, pause);
433         }
434         if (chan->_state != AST_STATE_UP)
435                 res = ast_answer(chan);
436
437         if (chan)
438                 ast_stopstream(chan);
439
440
441         if (file) {
442                 if ((end = strchr(file,':'))) {
443                         if (!strcasecmp(end, ":end")) {
444                                 *end = '\0';
445                                 end++;
446                         }
447                 }
448         }
449
450         for (;;) {
451                 gettimeofday(&started,NULL);
452
453                 if (chan)
454                         ast_stopstream(chan);
455                 res = ast_streamfile(chan, file, chan->language);
456                 if (!res) {
457                         if (end) {
458                                 ast_seekstream(chan->stream, 0, SEEK_END);
459                                 end=NULL;
460                         }
461                         res = 1;
462                         if (elapsed) {
463                                 ast_stream_fastforward(chan->stream, elapsed);
464                                 last_elapsed = elapsed - 200;
465                         }
466                         if (res)
467                                 res = ast_waitstream_fr(chan, breaks, fwd, rev, skipms);
468                         else
469                                 break;
470                 }
471
472                 if (res < 1)
473                         break;
474
475                 if (pause != NULL && strchr(pause, res)) {
476                         gettimeofday(&ended, NULL);
477                         elapsed = (((ended.tv_sec * 1000) + ended.tv_usec / 1000) - ((started.tv_sec * 1000) + started.tv_usec / 1000) + last_elapsed);
478                         for(;;) {
479                                 if (chan)
480                                         ast_stopstream(chan);
481                                 res = ast_waitfordigit(chan, 1000);
482                                 if (res == 0)
483                                         continue;
484                                 else if (res == -1 || strchr(pause, res) || (stop && strchr(stop, res)))
485                                         break;
486                         }
487                         if (res == *pause) {
488                                 res = 0;
489                                 continue;
490                         }
491                 }
492                 if (res == -1)
493                         break;
494
495                 /* if we get one of our stop chars, return it to the calling function */
496                 if (stop && strchr(stop, res)) {
497                         /* res = 0; */
498                         break;
499                 }
500         }
501         if (chan)
502                 ast_stopstream(chan);
503
504         return res;
505 }
506
507 int ast_play_and_wait(struct ast_channel *chan, char *fn)
508 {
509         int d;
510         d = ast_streamfile(chan, fn, chan->language);
511         if (d)
512                 return d;
513         d = ast_waitstream(chan, AST_DIGIT_ANY);
514         ast_stopstream(chan);
515         return d;
516 }
517
518 static int global_silence_threshold = 128;
519 static int global_maxsilence = 0;
520
521 int ast_play_and_record(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime, char *fmt, int *duration, int silencethreshold, int maxsilence)
522 {
523         char d, *fmts;
524         char comment[256];
525         int x, fmtcnt=1, res=-1,outmsg=0;
526         struct ast_frame *f;
527         struct ast_filestream *others[MAX_OTHER_FORMATS];
528         char *sfmt[MAX_OTHER_FORMATS];
529         char *stringp=NULL;
530         time_t start, end;
531         struct ast_dsp *sildet;         /* silence detector dsp */
532         int totalsilence = 0;
533         int dspsilence = 0;
534         int gotsilence = 0;             /* did we timeout for silence? */
535         int rfmt=0;
536
537         if (silencethreshold < 0)
538                 silencethreshold = global_silence_threshold;
539
540         if (maxsilence < 0)
541                 maxsilence = global_maxsilence;
542
543         /* barf if no pointer passed to store duration in */
544         if (duration == NULL) {
545                 ast_log(LOG_WARNING, "Error play_and_record called without duration pointer\n");
546                 return -1;
547         }
548
549         ast_log(LOG_DEBUG,"play_and_record: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
550         snprintf(comment,sizeof(comment),"Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
551
552         if (playfile) {
553                 d = ast_play_and_wait(chan, playfile);
554                 if (d > -1)
555                         d = ast_streamfile(chan, "beep",chan->language);
556                 if (!d)
557                         d = ast_waitstream(chan,"");
558                 if (d < 0)
559                         return -1;
560         }
561
562         fmts = ast_strdupa(fmt);
563
564         stringp=fmts;
565         strsep(&stringp, "|");
566         ast_log(LOG_DEBUG,"Recording Formats: sfmts=%s\n", fmts);
567         sfmt[0] = ast_strdupa(fmts);
568
569         while((fmt = strsep(&stringp, "|"))) {
570                 if (fmtcnt > MAX_OTHER_FORMATS - 1) {
571                         ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app_voicemail.c\n");
572                         break;
573                 }
574                 sfmt[fmtcnt++] = ast_strdupa(fmt);
575         }
576
577         time(&start);
578         end=start;  /* pre-initialize end to be same as start in case we never get into loop */
579         for (x=0;x<fmtcnt;x++) {
580                 others[x] = ast_writefile(recordfile, sfmt[x], comment, O_TRUNC, 0, 0700);
581                 ast_verbose( VERBOSE_PREFIX_3 "x=%i, open writing:  %s format: %s, %p\n", x, recordfile, sfmt[x], others[x]);
582
583                 if (!others[x]) {
584                         break;
585                 }
586         }
587
588         sildet = ast_dsp_new(); /* Create the silence detector */
589         if (!sildet) {
590                 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
591                 return -1;
592         }
593         ast_dsp_set_threshold(sildet, silencethreshold);
594         
595         if (maxsilence > 0) {
596                 rfmt = chan->readformat;
597                 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
598                 if (res < 0) {
599                         ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
600                         return -1;
601                 }
602         }
603
604         if (x == fmtcnt) {
605         /* Loop forever, writing the packets we read to the writer(s), until
606            we read a # or get a hangup */
607                 f = NULL;
608                 for(;;) {
609                         res = ast_waitfor(chan, 2000);
610                         if (!res) {
611                                 ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
612                                 /* Try one more time in case of masq */
613                                 res = ast_waitfor(chan, 2000);
614                                 if (!res) {
615                                         ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
616                                         res = -1;
617                                 }
618                         }
619
620                         if (res < 0) {
621                                 f = NULL;
622                                 break;
623                         }
624                         f = ast_read(chan);
625                         if (!f)
626                                 break;
627                         if (f->frametype == AST_FRAME_VOICE) {
628                                 /* write each format */
629                                 for (x=0;x<fmtcnt;x++) {
630                                         res = ast_writestream(others[x], f);
631                                 }
632
633                                 /* Silence Detection */
634                                 if (maxsilence > 0) {
635                                         dspsilence = 0;
636                                         ast_dsp_silence(sildet, f, &dspsilence);
637                                         if (dspsilence)
638                                                 totalsilence = dspsilence;
639                                         else
640                                                 totalsilence = 0;
641
642                                         if (totalsilence > maxsilence) {
643                                         /* Ended happily with silence */
644                                         if (option_verbose > 2)
645                                                 ast_verbose( VERBOSE_PREFIX_3 "Recording automatically stopped after a silence of %d seconds\n", totalsilence/1000);
646                                         ast_frfree(f);
647                                         gotsilence = 1;
648                                         outmsg=2;
649                                         break;
650                                         }
651                                 }
652                                 /* Exit on any error */
653                                 if (res) {
654                                         ast_log(LOG_WARNING, "Error writing frame\n");
655                                         ast_frfree(f);
656                                         break;
657                                 }
658                         } else if (f->frametype == AST_FRAME_VIDEO) {
659                                 /* Write only once */
660                                 ast_writestream(others[0], f);
661                         } else if (f->frametype == AST_FRAME_DTMF) {
662                                 if (f->subclass == '#') {
663                                         if (option_verbose > 2)
664                                                 ast_verbose( VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
665                                         res = '#';
666                                         outmsg = 2;
667                                         ast_frfree(f);
668                                         break;
669                                 }
670                         }
671                                 if (f->subclass == '0') {
672                                 /* Check for a '0' during message recording also, in case caller wants operator */
673                                         if (option_verbose > 2)
674                                                 ast_verbose(VERBOSE_PREFIX_3 "User cancelled by pressing %c\n", f->subclass);
675                                         res = '0';
676                                         outmsg = 0;
677                                         ast_frfree(f);
678                                         break;
679                                 }
680                         if (maxtime) {
681                                 time(&end);
682                                 if (maxtime < (end - start)) {
683                                         if (option_verbose > 2)
684                                                 ast_verbose( VERBOSE_PREFIX_3 "Took too long, cutting it short...\n");
685                                         outmsg = 2;
686                                         res = 't';
687                                         ast_frfree(f);
688                                         break;
689                                 }
690                         }
691                         ast_frfree(f);
692                 }
693                 if (end == start) time(&end);
694                 if (!f) {
695                         if (option_verbose > 2)
696                                 ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
697                         res = -1;
698                         outmsg=1;
699                 }
700         } else {
701                 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", recordfile, sfmt[x]);
702         }
703
704         *duration = end - start;
705
706         for (x=0;x<fmtcnt;x++) {
707                 if (!others[x])
708                         break;
709                 if (totalsilence)
710                         ast_stream_rewind(others[x], totalsilence-200);
711                 else
712                         ast_stream_rewind(others[x], 200);
713                 ast_truncstream(others[x]);
714                 ast_closestream(others[x]);
715         }
716         if (rfmt) {
717                 if (ast_set_read_format(chan, rfmt)) {
718                         ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
719                 }
720         }
721         if (outmsg) {
722                 if (outmsg > 1) {
723                 /* Let them know recording is stopped */
724                         ast_streamfile(chan, "auth-thankyou", chan->language);
725                         ast_waitstream(chan, "");
726                 }
727         }
728
729         return res;
730 }
731
732 int ast_play_and_prepend(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime, char *fmt, int *duration, int beep, int silencethreshold, int maxsilence)
733 {
734         char d = 0, *fmts;
735         char comment[256];
736         int x, fmtcnt=1, res=-1,outmsg=0;
737         struct ast_frame *f;
738         struct ast_filestream *others[MAX_OTHER_FORMATS];
739         struct ast_filestream *realfiles[MAX_OTHER_FORMATS];
740         char *sfmt[MAX_OTHER_FORMATS];
741         char *stringp=NULL;
742         time_t start, end;
743         struct ast_dsp *sildet;         /* silence detector dsp */
744         int totalsilence = 0;
745         int dspsilence = 0;
746         int gotsilence = 0;             /* did we timeout for silence? */
747         int rfmt=0;     
748         char prependfile[80];
749         
750         if (silencethreshold < 0)
751                 silencethreshold = global_silence_threshold;
752
753         if (maxsilence < 0)
754                 maxsilence = global_maxsilence;
755
756         /* barf if no pointer passed to store duration in */
757         if (duration == NULL) {
758                 ast_log(LOG_WARNING, "Error play_and_prepend called without duration pointer\n");
759                 return -1;
760         }
761
762         ast_log(LOG_DEBUG,"play_and_prepend: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
763         snprintf(comment,sizeof(comment),"Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
764
765         if (playfile || beep) { 
766                 if (!beep)
767                         d = ast_play_and_wait(chan, playfile);
768                 if (d > -1)
769                         d = ast_streamfile(chan, "beep",chan->language);
770                 if (!d)
771                         d = ast_waitstream(chan,"");
772                 if (d < 0)
773                         return -1;
774         }
775         strncpy(prependfile, recordfile, sizeof(prependfile) -1);       
776         strncat(prependfile, "-prepend", sizeof(prependfile) - strlen(prependfile) - 1);
777                         
778         fmts = ast_strdupa(fmt);
779         
780         stringp=fmts;
781         strsep(&stringp, "|");
782         ast_log(LOG_DEBUG,"Recording Formats: sfmts=%s\n", fmts);       
783         sfmt[0] = ast_strdupa(fmts);
784         
785         while((fmt = strsep(&stringp, "|"))) {
786                 if (fmtcnt > MAX_OTHER_FORMATS - 1) {
787                         ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app_voicemail.c\n");
788                         break;
789                 }
790                 sfmt[fmtcnt++] = ast_strdupa(fmt);
791         }
792
793         time(&start);
794         end=start;  /* pre-initialize end to be same as start in case we never get into loop */
795         for (x=0;x<fmtcnt;x++) {
796                 others[x] = ast_writefile(prependfile, sfmt[x], comment, O_TRUNC, 0, 0700);
797                 ast_verbose( VERBOSE_PREFIX_3 "x=%i, open writing:  %s format: %s, %p\n", x, prependfile, sfmt[x], others[x]);
798                 if (!others[x]) {
799                         break;
800                 }
801         }
802         
803         sildet = ast_dsp_new(); /* Create the silence detector */
804         if (!sildet) {
805                 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
806                 return -1;
807         }
808         ast_dsp_set_threshold(sildet, silencethreshold);
809
810         if (maxsilence > 0) {
811                 rfmt = chan->readformat;
812                 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
813                 if (res < 0) {
814                         ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
815                         return -1;
816                 }
817         }
818                                                 
819         if (x == fmtcnt) {
820         /* Loop forever, writing the packets we read to the writer(s), until
821            we read a # or get a hangup */
822                 f = NULL;
823                 for(;;) {
824                         res = ast_waitfor(chan, 2000);
825                         if (!res) {
826                                 ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
827                                 /* Try one more time in case of masq */
828                                 res = ast_waitfor(chan, 2000);
829                                 if (!res) {
830                                         ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
831                                         res = -1;
832                                 }
833                         }
834                         
835                         if (res < 0) {
836                                 f = NULL;
837                                 break;
838                         }
839                         f = ast_read(chan);
840                         if (!f)
841                                 break;
842                         if (f->frametype == AST_FRAME_VOICE) {
843                                 /* write each format */
844                                 for (x=0;x<fmtcnt;x++) {
845                                         if (!others[x])
846                                                 break;
847                                         res = ast_writestream(others[x], f);
848                                 }
849                                 
850                                 /* Silence Detection */
851                                 if (maxsilence > 0) {
852                                         dspsilence = 0;
853                                         ast_dsp_silence(sildet, f, &dspsilence);
854                                         if (dspsilence)
855                                                 totalsilence = dspsilence;
856                                         else
857                                                 totalsilence = 0;
858                                         
859                                         if (totalsilence > maxsilence) {
860                                         /* Ended happily with silence */
861                                         if (option_verbose > 2) 
862                                                 ast_verbose( VERBOSE_PREFIX_3 "Recording automatically stopped after a silence of %d seconds\n", totalsilence/1000);
863                                         ast_frfree(f);
864                                         gotsilence = 1;
865                                         outmsg=2;
866                                         break;
867                                         }
868                                 }
869                                 /* Exit on any error */
870                                 if (res) {
871                                         ast_log(LOG_WARNING, "Error writing frame\n");
872                                         ast_frfree(f);
873                                         break;
874                                 }
875                         } else if (f->frametype == AST_FRAME_VIDEO) {
876                                 /* Write only once */
877                                 ast_writestream(others[0], f);
878                         } else if (f->frametype == AST_FRAME_DTMF) {
879                                 /* stop recording with any digit */
880                                 if (option_verbose > 2) 
881                                         ast_verbose( VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
882                                 res = 't';
883                                 outmsg = 2;
884                                 ast_frfree(f);
885                                 break;
886                         }
887                         if (maxtime) {
888                                 time(&end);
889                                 if (maxtime < (end - start)) {
890                                         if (option_verbose > 2)
891                                                 ast_verbose( VERBOSE_PREFIX_3 "Took too long, cutting it short...\n");
892                                         res = 't';
893                                         outmsg=2;
894                                         ast_frfree(f);
895                                         break;
896                                 }
897                         }
898                         ast_frfree(f);
899                 }
900                 if (end == start) time(&end);
901                 if (!f) {
902                         if (option_verbose > 2) 
903                                 ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
904                         res = -1;
905                         outmsg=1;
906 #if 0
907                         /* delete all the prepend files */
908                         for (x=0;x<fmtcnt;x++) {
909                                 if (!others[x])
910                                         break;
911                                 ast_closestream(others[x]);
912                                 ast_filedelete(prependfile, sfmt[x]);
913                         }
914 #endif
915                 }
916         } else {
917                 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", prependfile, sfmt[x]); 
918         }
919         *duration = end - start;
920 #if 0
921         if (outmsg > 1) {
922 #else
923         if (outmsg) {
924 #endif
925                 struct ast_frame *fr;
926                 for (x=0;x<fmtcnt;x++) {
927                         snprintf(comment, sizeof(comment), "Opening the real file %s.%s\n", recordfile, sfmt[x]);
928                         realfiles[x] = ast_readfile(recordfile, sfmt[x], comment, O_RDONLY, 0, 0);
929                         if (!others[x] || !realfiles[x])
930                                 break;
931                         if (totalsilence)
932                                 ast_stream_rewind(others[x], totalsilence-200);
933                         else
934                                 ast_stream_rewind(others[x], 200);
935                         ast_truncstream(others[x]);
936                         /* add the original file too */
937                         while ((fr = ast_readframe(realfiles[x]))) {
938                                 ast_writestream(others[x],fr);
939                         }
940                         ast_closestream(others[x]);
941                         ast_closestream(realfiles[x]);
942                         ast_filerename(prependfile, recordfile, sfmt[x]);
943 #if 0
944                         ast_verbose("Recording Format: sfmts=%s, prependfile %s, recordfile %s\n", sfmt[x],prependfile,recordfile);
945 #endif
946                         ast_filedelete(prependfile, sfmt[x]);
947                 }
948         }
949         if (rfmt) {
950                 if (ast_set_read_format(chan, rfmt)) {
951                         ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
952                 }
953         }
954         if (outmsg) {
955                 if (outmsg > 1) {
956                         /* Let them know it worked */
957                         ast_streamfile(chan, "auth-thankyou", chan->language);
958                         ast_waitstream(chan, "");
959                 }
960         }       
961         return res;
962 }
963