2 * Asterisk -- A telephony toolkit for Linux.
4 * Convenient Application Routines
6 * Copyright (C) 1999 - 2005, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
22 #include <sys/types.h>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/channel.h"
31 #include "asterisk/pbx.h"
32 #include "asterisk/file.h"
33 #include "asterisk/app.h"
34 #include "asterisk/dsp.h"
35 #include "asterisk/logger.h"
36 #include "asterisk/options.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/lock.h"
39 #include "asterisk/indications.h"
41 #define MAX_OTHER_FORMATS 10
45 This function presents a dialtone and reads an extension into 'collect'
46 which must be a pointer to a **pre-initilized** array of char having a
47 size of 'size' suitable for writing to. It will collect no more than the smaller
48 of 'maxlen' or 'size' minus the original strlen() of collect digits.
50 int ast_app_dtget(struct ast_channel *chan, const char *context, char *collect, size_t size, int maxlen, int timeout)
52 struct tone_zone_sound *ts;
58 if(!timeout && chan->pbx)
59 timeout = chan->pbx->dtimeout;
63 ts = ast_get_indication_tone(chan->zone,"dial");
64 if (ts && ts->data[0])
65 res = ast_playtones_start(chan, 0, ts->data, 0);
67 ast_log(LOG_NOTICE,"Huh....? no dial for indications?\n");
69 for (x = strlen(collect); strlen(collect) < maxlen; ) {
70 res = ast_waitfordigit(chan, timeout);
71 if (!ast_ignore_pattern(context, collect))
72 ast_playtones_stop(chan);
76 if (!ast_matchmore_extension(chan, context, collect, 1, chan->cid.cid_num)) {
77 if (collect[x-1] == '#') {
78 /* Not a valid extension, ending in #, assume the # was to finish dialing */
85 if (ast_exists_extension(chan, context, collect, 1, chan->cid.cid_num))
95 /* set timeout to 0 for "standard" timeouts. Set timeout to -1 for
96 "ludicrous time" (essentially never times out) */
97 int ast_app_getdata(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout)
100 /* XXX Merge with full version? XXX */
104 res = ast_streamfile(c, prompt, c->language);
108 fto = c->pbx ? c->pbx->rtimeout * 1000 : 6000;
109 to = c->pbx ? c->pbx->dtimeout * 1000 : 2000;
114 fto = to = 1000000000;
115 res = ast_readstring(c, s, maxlen, to, fto, "#");
120 int ast_app_getdata_full(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout, int audiofd, int ctrlfd)
124 res = ast_streamfile(c, prompt, c->language);
133 fto = to = 1000000000;
134 res = ast_readstring_full(c, s, maxlen, to, fto, "#", audiofd, ctrlfd);
138 int ast_app_getvoice(struct ast_channel *c, char *dest, char *dstfmt, char *prompt, int silence, int maxsec)
141 struct ast_filestream *writer;
143 int totalms=0, total;
146 struct ast_dsp *sildet;
147 /* Play prompt if requested */
149 res = ast_streamfile(c, prompt, c->language);
152 res = ast_waitstream(c,"");
156 rfmt = c->readformat;
157 res = ast_set_read_format(c, AST_FORMAT_SLINEAR);
159 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
162 sildet = ast_dsp_new();
164 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
167 writer = ast_writefile(dest, dstfmt, "Voice file", 0, 0, 0666);
169 ast_log(LOG_WARNING, "Unable to open file '%s' in format '%s' for writing\n", dest, dstfmt);
170 ast_dsp_free(sildet);
174 if ((res = ast_waitfor(c, 2000)) < 0) {
175 ast_log(LOG_NOTICE, "Waitfor failed while recording file '%s' format '%s'\n", dest, dstfmt);
181 ast_log(LOG_NOTICE, "Hungup while recording file '%s' format '%s'\n", dest, dstfmt);
184 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
185 /* Ended happily with DTMF */
188 } else if (f->frametype == AST_FRAME_VOICE) {
189 ast_dsp_silence(sildet, f, &total);
190 if (total > silence) {
191 /* Ended happily with silence */
195 totalms += f->samples / 8;
196 if (totalms > maxsec * 1000) {
197 /* Ended happily with too much stuff */
198 ast_log(LOG_NOTICE, "Constraining voice on '%s' to %d seconds\n", c->name, maxsec);
206 res = ast_set_read_format(c, rfmt);
208 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", c->name);
209 ast_dsp_free(sildet);
210 ast_closestream(writer);
214 static int (*ast_has_voicemail_func)(const char *mailbox, const char *folder) = NULL;
215 static int (*ast_messagecount_func)(const char *mailbox, int *newmsgs, int *oldmsgs) = NULL;
217 void ast_install_vm_functions(int (*has_voicemail_func)(const char *mailbox, const char *folder),
218 int (*messagecount_func)(const char *mailbox, int *newmsgs, int *oldmsgs))
220 ast_has_voicemail_func = has_voicemail_func;
221 ast_messagecount_func = messagecount_func;
224 void ast_uninstall_vm_functions(void)
226 ast_has_voicemail_func = NULL;
227 ast_messagecount_func = NULL;
230 int ast_app_has_voicemail(const char *mailbox, const char *folder)
232 static int warned = 0;
233 if (ast_has_voicemail_func)
234 return ast_has_voicemail_func(mailbox, folder);
236 if ((option_verbose > 2) && !warned) {
237 ast_verbose(VERBOSE_PREFIX_3 "Message check requested for mailbox %s/folder %s but voicemail not loaded.\n", mailbox, folder ? folder : "INBOX");
244 int ast_app_messagecount(const char *mailbox, int *newmsgs, int *oldmsgs)
246 static int warned = 0;
251 if (ast_messagecount_func)
252 return ast_messagecount_func(mailbox, newmsgs, oldmsgs);
254 if (!warned && (option_verbose > 2)) {
256 ast_verbose(VERBOSE_PREFIX_3 "Message count requested for mailbox %s but voicemail not loaded.\n", mailbox);
262 int ast_dtmf_stream(struct ast_channel *chan,struct ast_channel *peer,char *digits,int between)
271 res = ast_autoservice_start(peer);
274 res = ast_waitfor(chan,100);
276 for (ptr=digits; *ptr; ptr++) {
278 res = ast_safe_sleep(chan, 500);
283 memset(&f, 0, sizeof(f));
284 f.frametype = AST_FRAME_DTMF;
286 f.src = "ast_dtmf_stream";
287 if (strchr("0123456789*#abcdABCD",*ptr)==NULL) {
288 ast_log(LOG_WARNING, "Illegal DTMF character '%c' in string. (0-9*#aAbBcCdD allowed)\n",*ptr);
290 res = ast_write(chan, &f);
293 /* pause between digits */
294 res = ast_safe_sleep(chan,between);
301 res = ast_autoservice_stop(peer);
306 struct linear_state {
313 static void linear_release(struct ast_channel *chan, void *params)
315 struct linear_state *ls = params;
316 if (ls->origwfmt && ast_set_write_format(chan, ls->origwfmt)) {
317 ast_log(LOG_WARNING, "Unable to restore channel '%s' to format '%d'\n", chan->name, ls->origwfmt);
324 static int linear_generator(struct ast_channel *chan, void *data, int len, int samples)
327 short buf[2048 + AST_FRIENDLY_OFFSET / 2];
328 struct linear_state *ls = data;
331 if (len > sizeof(buf) - AST_FRIENDLY_OFFSET) {
332 ast_log(LOG_WARNING, "Can't generate %d bytes of data!\n" ,len);
333 len = sizeof(buf) - AST_FRIENDLY_OFFSET;
335 memset(&f, 0, sizeof(f));
336 res = read(ls->fd, buf + AST_FRIENDLY_OFFSET/2, len);
338 f.frametype = AST_FRAME_VOICE;
339 f.subclass = AST_FORMAT_SLINEAR;
340 f.data = buf + AST_FRIENDLY_OFFSET/2;
343 f.offset = AST_FRIENDLY_OFFSET;
351 static void *linear_alloc(struct ast_channel *chan, void *params)
353 struct linear_state *ls;
354 /* In this case, params is already malloc'd */
357 if (ls->allowoverride)
358 ast_set_flag(chan, AST_FLAG_WRITE_INT);
360 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
361 ls->origwfmt = chan->writeformat;
362 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
363 ast_log(LOG_WARNING, "Unable to set '%s' to linear format (write)\n", chan->name);
371 static struct ast_generator linearstream =
374 release: linear_release,
375 generate: linear_generator,
378 int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, int allowoverride)
380 struct linear_state *lin;
385 if (!filename || ast_strlen_zero(filename))
388 if (filename[0] == '/')
389 ast_copy_string(tmpf, filename, sizeof(tmpf));
391 snprintf(tmpf, sizeof(tmpf), "%s/%s/%s", (char *)ast_config_AST_VAR_DIR, "sounds", filename);
392 fd = open(tmpf, O_RDONLY);
394 ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", tmpf, strerror(errno));
398 lin = malloc(sizeof(struct linear_state));
400 memset(lin, 0, sizeof(lin));
402 lin->allowoverride = allowoverride;
403 lin->autoclose = autoclose;
404 res = ast_activate_generator(chan, &linearstream, lin);
409 int ast_control_streamfile(struct ast_channel *chan, const char *file, const char *fwd, const char *rev, const char *stop, const char *pause, int skipms)
411 long elapsed = 0,last_elapsed =0;
418 blen += strlen(stop);
420 blen += strlen(pause);
423 breaks = alloca(blen + 1);
425 strcat(breaks, stop);
426 strcat(breaks, pause);
428 if (chan->_state != AST_STATE_UP)
429 res = ast_answer(chan);
432 ast_stopstream(chan);
436 if ((end = strchr(file,':'))) {
437 if (!strcasecmp(end, ":end")) {
445 struct timeval started = ast_tvnow();
448 ast_stopstream(chan);
449 res = ast_streamfile(chan, file, chan->language);
452 ast_seekstream(chan->stream, 0, SEEK_END);
457 ast_stream_fastforward(chan->stream, elapsed);
458 last_elapsed = elapsed - 200;
461 res = ast_waitstream_fr(chan, breaks, fwd, rev, skipms);
469 if (pause != NULL && strchr(pause, res)) {
470 elapsed = ast_tvdiff_ms(ast_tvnow(), started) + last_elapsed;
473 ast_stopstream(chan);
474 res = ast_waitfordigit(chan, 1000);
477 else if (res == -1 || strchr(pause, res) || (stop && strchr(stop, res)))
488 /* if we get one of our stop chars, return it to the calling function */
489 if (stop && strchr(stop, res)) {
495 ast_stopstream(chan);
500 int ast_play_and_wait(struct ast_channel *chan, const char *fn)
503 d = ast_streamfile(chan, fn, chan->language);
506 d = ast_waitstream(chan, AST_DIGIT_ANY);
507 ast_stopstream(chan);
511 static int global_silence_threshold = 128;
512 static int global_maxsilence = 0;
514 int ast_play_and_record(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, int silencethreshold, int maxsilence, const char *path)
519 int x, fmtcnt=1, res=-1,outmsg=0;
521 struct ast_filestream *others[MAX_OTHER_FORMATS];
522 char *sfmt[MAX_OTHER_FORMATS];
525 struct ast_dsp *sildet=NULL; /* silence detector dsp */
526 int totalsilence = 0;
528 int gotsilence = 0; /* did we timeout for silence? */
531 if (silencethreshold < 0)
532 silencethreshold = global_silence_threshold;
535 maxsilence = global_maxsilence;
537 /* barf if no pointer passed to store duration in */
538 if (duration == NULL) {
539 ast_log(LOG_WARNING, "Error play_and_record called without duration pointer\n");
543 ast_log(LOG_DEBUG,"play_and_record: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
544 snprintf(comment,sizeof(comment),"Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
547 d = ast_play_and_wait(chan, playfile);
549 d = ast_streamfile(chan, "beep",chan->language);
551 d = ast_waitstream(chan,"");
556 fmts = ast_strdupa(fmt);
559 strsep(&stringp, "|");
560 ast_log(LOG_DEBUG,"Recording Formats: sfmts=%s\n", fmts);
561 sfmt[0] = ast_strdupa(fmts);
563 while((fmt = strsep(&stringp, "|"))) {
564 if (fmtcnt > MAX_OTHER_FORMATS - 1) {
565 ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app_voicemail.c\n");
568 sfmt[fmtcnt++] = ast_strdupa(fmt);
572 end=start; /* pre-initialize end to be same as start in case we never get into loop */
573 for (x=0;x<fmtcnt;x++) {
574 others[x] = ast_writefile(recordfile, sfmt[x], comment, O_TRUNC, 0, 0700);
575 ast_verbose( VERBOSE_PREFIX_3 "x=%d, open writing: %s format: %s, %p\n", x, recordfile, sfmt[x], others[x]);
583 ast_unlock_path(path);
587 if (maxsilence > 0) {
588 sildet = ast_dsp_new(); /* Create the silence detector */
590 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
593 ast_dsp_set_threshold(sildet, silencethreshold);
594 rfmt = chan->readformat;
595 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
597 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
598 ast_dsp_free(sildet);
604 /* Loop forever, writing the packets we read to the writer(s), until
605 we read a # or get a hangup */
608 res = ast_waitfor(chan, 2000);
610 ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
611 /* Try one more time in case of masq */
612 res = ast_waitfor(chan, 2000);
614 ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
626 if (f->frametype == AST_FRAME_VOICE) {
627 /* write each format */
628 for (x=0;x<fmtcnt;x++) {
629 res = ast_writestream(others[x], f);
632 /* Silence Detection */
633 if (maxsilence > 0) {
635 ast_dsp_silence(sildet, f, &dspsilence);
637 totalsilence = dspsilence;
641 if (totalsilence > maxsilence) {
642 /* Ended happily with silence */
643 if (option_verbose > 2)
644 ast_verbose( VERBOSE_PREFIX_3 "Recording automatically stopped after a silence of %d seconds\n", totalsilence/1000);
651 /* Exit on any error */
653 ast_log(LOG_WARNING, "Error writing frame\n");
657 } else if (f->frametype == AST_FRAME_VIDEO) {
658 /* Write only once */
659 ast_writestream(others[0], f);
660 } else if (f->frametype == AST_FRAME_DTMF) {
661 if (f->subclass == '#') {
662 if (option_verbose > 2)
663 ast_verbose( VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
670 if (f->subclass == '0') {
671 /* Check for a '0' during message recording also, in case caller wants operator */
672 if (option_verbose > 2)
673 ast_verbose(VERBOSE_PREFIX_3 "User cancelled by pressing %c\n", f->subclass);
681 if (maxtime < (end - start)) {
682 if (option_verbose > 2)
683 ast_verbose( VERBOSE_PREFIX_3 "Took too long, cutting it short...\n");
692 if (end == start) time(&end);
694 if (option_verbose > 2)
695 ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
700 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", recordfile, sfmt[x]);
703 *duration = end - start;
705 for (x=0;x<fmtcnt;x++) {
710 ast_stream_rewind(others[x], totalsilence-200);
712 ast_stream_rewind(others[x], 200);
714 ast_truncstream(others[x]);
715 ast_closestream(others[x]);
718 if (ast_set_read_format(chan, rfmt)) {
719 ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
723 /* Let them know recording is stopped */
724 if(!ast_streamfile(chan, "auth-thankyou", chan->language))
725 ast_waitstream(chan, "");
728 ast_dsp_free(sildet);
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)
737 int x, fmtcnt=1, res=-1,outmsg=0;
739 struct ast_filestream *others[MAX_OTHER_FORMATS];
740 struct ast_filestream *realfiles[MAX_OTHER_FORMATS];
741 char *sfmt[MAX_OTHER_FORMATS];
744 struct ast_dsp *sildet; /* silence detector dsp */
745 int totalsilence = 0;
747 int gotsilence = 0; /* did we timeout for silence? */
749 char prependfile[80];
751 if (silencethreshold < 0)
752 silencethreshold = global_silence_threshold;
755 maxsilence = global_maxsilence;
757 /* barf if no pointer passed to store duration in */
758 if (duration == NULL) {
759 ast_log(LOG_WARNING, "Error play_and_prepend called without duration pointer\n");
763 ast_log(LOG_DEBUG,"play_and_prepend: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
764 snprintf(comment,sizeof(comment),"Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
766 if (playfile || beep) {
768 d = ast_play_and_wait(chan, playfile);
770 d = ast_streamfile(chan, "beep",chan->language);
772 d = ast_waitstream(chan,"");
776 ast_copy_string(prependfile, recordfile, sizeof(prependfile));
777 strncat(prependfile, "-prepend", sizeof(prependfile) - strlen(prependfile) - 1);
779 fmts = ast_strdupa(fmt);
782 strsep(&stringp, "|");
783 ast_log(LOG_DEBUG,"Recording Formats: sfmts=%s\n", fmts);
784 sfmt[0] = ast_strdupa(fmts);
786 while((fmt = strsep(&stringp, "|"))) {
787 if (fmtcnt > MAX_OTHER_FORMATS - 1) {
788 ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app_voicemail.c\n");
791 sfmt[fmtcnt++] = ast_strdupa(fmt);
795 end=start; /* pre-initialize end to be same as start in case we never get into loop */
796 for (x=0;x<fmtcnt;x++) {
797 others[x] = ast_writefile(prependfile, sfmt[x], comment, O_TRUNC, 0, 0700);
798 ast_verbose( VERBOSE_PREFIX_3 "x=%d, open writing: %s format: %s, %p\n", x, prependfile, sfmt[x], others[x]);
804 sildet = ast_dsp_new(); /* Create the silence detector */
806 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
809 ast_dsp_set_threshold(sildet, silencethreshold);
811 if (maxsilence > 0) {
812 rfmt = chan->readformat;
813 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
815 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
821 /* Loop forever, writing the packets we read to the writer(s), until
822 we read a # or get a hangup */
825 res = ast_waitfor(chan, 2000);
827 ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
828 /* Try one more time in case of masq */
829 res = ast_waitfor(chan, 2000);
831 ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
843 if (f->frametype == AST_FRAME_VOICE) {
844 /* write each format */
845 for (x=0;x<fmtcnt;x++) {
848 res = ast_writestream(others[x], f);
851 /* Silence Detection */
852 if (maxsilence > 0) {
854 ast_dsp_silence(sildet, f, &dspsilence);
856 totalsilence = dspsilence;
860 if (totalsilence > maxsilence) {
861 /* Ended happily with silence */
862 if (option_verbose > 2)
863 ast_verbose( VERBOSE_PREFIX_3 "Recording automatically stopped after a silence of %d seconds\n", totalsilence/1000);
870 /* Exit on any error */
872 ast_log(LOG_WARNING, "Error writing frame\n");
876 } else if (f->frametype == AST_FRAME_VIDEO) {
877 /* Write only once */
878 ast_writestream(others[0], f);
879 } else if (f->frametype == AST_FRAME_DTMF) {
880 /* stop recording with any digit */
881 if (option_verbose > 2)
882 ast_verbose( VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
890 if (maxtime < (end - start)) {
891 if (option_verbose > 2)
892 ast_verbose( VERBOSE_PREFIX_3 "Took too long, cutting it short...\n");
901 if (end == start) time(&end);
903 if (option_verbose > 2)
904 ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
908 /* delete all the prepend files */
909 for (x=0;x<fmtcnt;x++) {
912 ast_closestream(others[x]);
913 ast_filedelete(prependfile, sfmt[x]);
918 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", prependfile, sfmt[x]);
920 *duration = end - start;
926 struct ast_frame *fr;
927 for (x=0;x<fmtcnt;x++) {
928 snprintf(comment, sizeof(comment), "Opening the real file %s.%s\n", recordfile, sfmt[x]);
929 realfiles[x] = ast_readfile(recordfile, sfmt[x], comment, O_RDONLY, 0, 0);
930 if (!others[x] || !realfiles[x])
933 ast_stream_rewind(others[x], totalsilence-200);
935 ast_stream_rewind(others[x], 200);
936 ast_truncstream(others[x]);
937 /* add the original file too */
938 while ((fr = ast_readframe(realfiles[x]))) {
939 ast_writestream(others[x],fr);
941 ast_closestream(others[x]);
942 ast_closestream(realfiles[x]);
943 ast_filerename(prependfile, recordfile, sfmt[x]);
945 ast_verbose("Recording Format: sfmts=%s, prependfile %s, recordfile %s\n", sfmt[x],prependfile,recordfile);
947 ast_filedelete(prependfile, sfmt[x]);
951 if (ast_set_read_format(chan, rfmt)) {
952 ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
957 /* Let them know it worked */
958 ast_streamfile(chan, "auth-thankyou", chan->language);
959 ast_waitstream(chan, "");
965 /* Channel group core functions */
967 int ast_app_group_split_group(char *data, char *group, int group_max, char *category, int category_max)
971 char *grp=NULL, *cat=NULL;
973 if (data && !ast_strlen_zero(data)) {
974 ast_copy_string(tmp, data, sizeof(tmp));
976 cat = strchr(tmp, '@');
983 if (grp && !ast_strlen_zero(grp))
984 ast_copy_string(group, grp, group_max);
989 snprintf(category, category_max, "%s_%s", GROUP_CATEGORY_PREFIX, cat);
991 ast_copy_string(category, GROUP_CATEGORY_PREFIX, category_max);
996 int ast_app_group_set_channel(struct ast_channel *chan, char *data)
1000 char category[80] = "";
1002 if (!ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category))) {
1003 pbx_builtin_setvar_helper(chan, category, group);
1010 int ast_app_group_get_count(char *group, char *category)
1012 struct ast_channel *chan;
1018 if (group == NULL || ast_strlen_zero(group))
1021 s = (category && !ast_strlen_zero(category)) ? category : GROUP_CATEGORY_PREFIX;
1022 ast_copy_string(cat, s, sizeof(cat));
1025 while ((chan = ast_channel_walk_locked(chan)) != NULL) {
1026 test = pbx_builtin_getvar_helper(chan, cat);
1027 if (test && !strcasecmp(test, group))
1029 ast_mutex_unlock(&chan->lock);
1035 int ast_app_group_match_get_count(char *groupmatch, char *category)
1038 struct ast_channel *chan;
1044 if (!groupmatch || ast_strlen_zero(groupmatch))
1047 /* if regex compilation fails, return zero matches */
1048 if (regcomp(®exbuf, groupmatch, REG_EXTENDED | REG_NOSUB))
1051 s = (category && !ast_strlen_zero(category)) ? category : GROUP_CATEGORY_PREFIX;
1052 ast_copy_string(cat, s, sizeof(cat));
1055 while ((chan = ast_channel_walk_locked(chan)) != NULL) {
1056 test = pbx_builtin_getvar_helper(chan, cat);
1057 if (test && !regexec(®exbuf, test, 0, NULL, 0))
1059 ast_mutex_unlock(&chan->lock);
1067 int ast_separate_app_args(char *buf, char delim, char **array, int arraylen)
1073 if (!buf || !array || !arraylen)
1076 memset(array, 0, arraylen * sizeof(*array));
1080 for (argc = 0; *scan && (argc < arraylen - 1); argc++) {
1082 for (; *scan; scan++) {
1085 else if (*scan == ')') {
1088 } else if ((*scan == delim) && !paren) {
1096 array[argc++] = scan;
1101 enum AST_LOCK_RESULT ast_lock_path(const char *path)
1109 s = alloca(strlen(path) + 10);
1110 fs = alloca(strlen(path) + 20);
1113 ast_log(LOG_WARNING, "Out of memory!\n");
1114 return AST_LOCK_FAILURE;
1117 snprintf(fs, strlen(path) + 19, "%s/.lock-%08x", path, rand());
1118 fd = open(fs, O_WRONLY | O_CREAT | O_EXCL, 0600);
1120 fprintf(stderr, "Unable to create lock file '%s': %s\n", path, strerror(errno));
1121 return AST_LOCK_PATH_NOT_FOUND;
1125 snprintf(s, strlen(path) + 9, "%s/.lock", path);
1127 while (((res = link(fs, s)) < 0) && (errno == EEXIST) && (time(NULL) - start < 5))
1130 ast_log(LOG_WARNING, "Failed to lock path '%s': %s\n", path, strerror(errno));
1131 return AST_LOCK_TIMEOUT;
1134 ast_log(LOG_DEBUG, "Locked path '%s'\n", path);
1135 return AST_LOCK_SUCCESS;
1139 int ast_unlock_path(const char *path)
1142 s = alloca(strlen(path) + 10);
1145 snprintf(s, strlen(path) + 9, "%s/%s", path, ".lock");
1146 ast_log(LOG_DEBUG, "Unlocked path '%s'\n", path);
1150 int ast_record_review(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, const char *path)
1152 int silencethreshold = 128;
1156 int max_attempts = 3;
1159 int message_exists = 0;
1160 /* Note that urgent and private are for flagging messages as such in the future */
1162 /* barf if no pointer passed to store duration in */
1163 if (duration == NULL) {
1164 ast_log(LOG_WARNING, "Error ast_record_review called without duration pointer\n");
1168 cmd = '3'; /* Want to start by recording */
1170 while ((cmd >= 0) && (cmd != 't')) {
1173 if (!message_exists) {
1174 /* In this case, 1 is to record a message */
1178 ast_streamfile(chan, "vm-msgsaved", chan->language);
1179 ast_waitstream(chan, "");
1185 ast_verbose(VERBOSE_PREFIX_3 "Reviewing the recording\n");
1186 ast_streamfile(chan, recordfile, chan->language);
1187 cmd = ast_waitstream(chan, AST_DIGIT_ANY);
1193 ast_verbose(VERBOSE_PREFIX_3 "Re-recording\n");
1195 ast_verbose(VERBOSE_PREFIX_3 "Recording\n");
1197 cmd = ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, silencethreshold, maxsilence, path);
1199 /* User has hung up, no options to give */
1204 } else if (cmd == '*') {
1208 /* If all is well, a message exists */
1221 cmd = ast_play_and_wait(chan, "vm-sorry");
1224 if (message_exists) {
1225 cmd = ast_play_and_wait(chan, "vm-review");
1228 cmd = ast_play_and_wait(chan, "vm-torerecord");
1230 cmd = ast_waitfordigit(chan, 600);
1234 cmd = ast_waitfordigit(chan, 6000);
1238 if (attempts > max_attempts) {
1248 #define RES_UPONE (1 << 16)
1249 #define RES_EXIT (1 << 17)
1250 #define RES_REPEAT (1 << 18)
1251 #define RES_RESTART ((1 << 19) | RES_REPEAT)
1253 static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata);
1254 static int ivr_dispatch(struct ast_channel *chan, struct ast_ivr_option *option, char *exten, void *cbdata)
1257 int (*ivr_func)(struct ast_channel *, void *);
1261 switch(option->action) {
1262 case AST_ACTION_UPONE:
1264 case AST_ACTION_EXIT:
1265 return RES_EXIT | (((unsigned long)(option->adata)) & 0xffff);
1266 case AST_ACTION_REPEAT:
1267 return RES_REPEAT | (((unsigned long)(option->adata)) & 0xffff);
1268 case AST_ACTION_RESTART:
1269 return RES_RESTART ;
1270 case AST_ACTION_NOOP:
1272 case AST_ACTION_BACKGROUND:
1273 res = ast_streamfile(chan, (char *)option->adata, chan->language);
1275 res = ast_waitstream(chan, AST_DIGIT_ANY);
1277 ast_log(LOG_NOTICE, "Unable to find file '%s'!\n", (char *)option->adata);
1281 case AST_ACTION_PLAYBACK:
1282 res = ast_streamfile(chan, (char *)option->adata, chan->language);
1284 res = ast_waitstream(chan, "");
1286 ast_log(LOG_NOTICE, "Unable to find file '%s'!\n", (char *)option->adata);
1290 case AST_ACTION_MENU:
1291 res = ast_ivr_menu_run_internal(chan, (struct ast_ivr_menu *)option->adata, cbdata);
1292 /* Do not pass entry errors back up, treaat ast though ti was an "UPONE" */
1296 case AST_ACTION_WAITOPTION:
1297 res = ast_waitfordigit(chan, 1000 * (chan->pbx ? chan->pbx->rtimeout : 10));
1301 case AST_ACTION_CALLBACK:
1302 ivr_func = option->adata;
1303 res = ivr_func(chan, cbdata);
1305 case AST_ACTION_TRANSFER:
1306 res = ast_parseable_goto(chan, option->adata);
1308 case AST_ACTION_PLAYLIST:
1309 case AST_ACTION_BACKLIST:
1311 c = ast_strdupa(option->adata);
1313 while((n = strsep(&c, ";")))
1314 if ((res = ast_streamfile(chan, n, chan->language)) || (res = ast_waitstream(chan, (option->action == AST_ACTION_BACKLIST) ? AST_DIGIT_ANY : "")))
1316 ast_stopstream(chan);
1320 ast_log(LOG_NOTICE, "Unknown dispatch function %d, ignoring!\n", option->action);
1326 static int option_exists(struct ast_ivr_menu *menu, char *option)
1329 for (x=0;menu->options[x].option;x++)
1330 if (!strcasecmp(menu->options[x].option, option))
1335 static int option_matchmore(struct ast_ivr_menu *menu, char *option)
1338 for (x=0;menu->options[x].option;x++)
1339 if ((!strncasecmp(menu->options[x].option, option, strlen(option))) &&
1340 (menu->options[x].option[strlen(option)]))
1345 static int read_newoption(struct ast_channel *chan, struct ast_ivr_menu *menu, char *exten, int maxexten)
1349 while(option_matchmore(menu, exten)) {
1350 ms = chan->pbx ? chan->pbx->dtimeout : 5000;
1351 if (strlen(exten) >= maxexten - 1)
1353 res = ast_waitfordigit(chan, ms);
1356 exten[strlen(exten) + 1] = '\0';
1357 exten[strlen(exten)] = res;
1359 return res > 0 ? 0 : res;
1362 static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata)
1364 /* Execute an IVR menu structure */
1368 char exten[AST_MAX_EXTENSION] = "s";
1369 if (option_exists(menu, "s") < 0) {
1371 if (option_exists(menu, "g") < 0) {
1372 ast_log(LOG_WARNING, "No 's' nor 'g' extension in menu '%s'!\n", menu->title);
1377 while(menu->options[pos].option) {
1378 if (!strcasecmp(menu->options[pos].option, exten)) {
1379 res = ivr_dispatch(chan, menu->options + pos, exten, cbdata);
1380 ast_log(LOG_DEBUG, "IVR Dispatch of '%s' (pos %d) yields %d\n", exten, pos, res);
1383 else if (res & RES_UPONE)
1385 else if (res & RES_EXIT)
1387 else if (res & RES_REPEAT) {
1388 int maxretries = res & 0xffff;
1389 if ((res & RES_RESTART) == RES_RESTART) {
1395 if ((maxretries > 0) && (retries >= maxretries)) {
1396 ast_log(LOG_DEBUG, "Max retries %d exceeded\n", maxretries);
1399 if (option_exists(menu, "g") > -1)
1401 else if (option_exists(menu, "s") > -1)
1406 } else if (res && strchr(AST_DIGIT_ANY, res)) {
1407 ast_log(LOG_DEBUG, "Got start of extension, %c\n", res);
1410 if ((res = read_newoption(chan, menu, exten, sizeof(exten))))
1412 if (option_exists(menu, exten) < 0) {
1413 if (option_exists(menu, "i")) {
1414 ast_log(LOG_DEBUG, "Invalid extension entered, going to 'i'!\n");
1419 ast_log(LOG_DEBUG, "Aborting on invalid entry, with no 'i' option!\n");
1424 ast_log(LOG_DEBUG, "New existing extension: %s\n", exten);
1432 ast_log(LOG_DEBUG, "Stopping option '%s', res is %d\n", exten, res);
1434 if (!strcasecmp(exten, "s"))
1442 int ast_ivr_menu_run(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata)
1445 res = ast_ivr_menu_run_internal(chan, menu, cbdata);
1446 /* Hide internal coding */
1452 char *ast_read_textfile(const char *filename)
1456 struct stat filesize;
1459 if(stat(filename,&filesize)== -1){
1460 ast_log(LOG_WARNING,"Error can't stat %s\n", filename);
1463 count=filesize.st_size + 1;
1464 fd = open(filename, O_RDONLY);
1466 ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", filename, strerror(errno));
1469 output=(char *)malloc(count);
1471 res = read(fd, output, count - 1);
1472 if (res == count - 1) {
1475 ast_log(LOG_WARNING, "Short read of %s (%d of %d): %s\n", filename, res, count - 1, strerror(errno));
1480 ast_log(LOG_WARNING, "Out of memory!\n");
1485 int ast_parseoptions(const struct ast_option *options, struct ast_flags *flags, char **args, char *optstr)
1498 flags->flags |= options[curarg].flag;
1499 argloc = options[curarg].argoption;
1505 while(*s && (*s != ')')) s++;
1508 args[argloc - 1] = arg;
1512 ast_log(LOG_WARNING, "Missing closing parenthesis for argument '%c'\n", curarg);
1516 args[argloc - 1] = NULL;