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>
25 #include <asterisk/channel.h>
26 #include <asterisk/pbx.h>
27 #include <asterisk/file.h>
28 #include <asterisk/app.h>
29 #include <asterisk/dsp.h>
30 #include <asterisk/logger.h>
31 #include <asterisk/options.h>
32 #include <asterisk/utils.h>
33 #include <asterisk/lock.h>
34 #include <asterisk/indications.h>
38 #define MAX_OTHER_FORMATS 10
42 This function presents a dialtone and reads an extension into 'collect'
43 which must be a pointer to a **pre-initilized** array of char having a
44 size of 'size' suitable for writing to. It will collect no more than the smaller
45 of 'maxlen' or 'size' minus the original strlen() of collect digits.
47 int ast_app_dtget(struct ast_channel *chan, const char *context, char *collect, size_t size, int maxlen, int timeout)
49 struct tone_zone_sound *ts;
55 if(!timeout && chan->pbx)
56 timeout = chan->pbx->dtimeout;
60 ts = ast_get_indication_tone(chan->zone,"dial");
61 if (ts && ts->data[0])
62 res = ast_playtones_start(chan, 0, ts->data, 0);
64 ast_log(LOG_NOTICE,"Huh....? no dial for indications?\n");
66 for (x = strlen(collect); strlen(collect) < maxlen; ) {
67 res = ast_waitfordigit(chan, timeout);
68 if (!ast_ignore_pattern(context, collect))
69 ast_playtones_stop(chan);
73 if (!ast_matchmore_extension(chan, context, collect, 1, chan->cid.cid_num)) {
74 if (collect[x-1] == '#') {
75 /* Not a valid extension, ending in #, assume the # was to finish dialing */
82 if (ast_exists_extension(chan, context, collect, 1, chan->cid.cid_num))
92 /* set timeout to 0 for "standard" timeouts. Set timeout to -1 for
93 "ludicrous time" (essentially never times out) */
94 int ast_app_getdata(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout)
97 /* XXX Merge with full version? XXX */
101 res = ast_streamfile(c, prompt, c->language);
105 fto = c->pbx ? c->pbx->rtimeout * 1000 : 6000;
106 to = c->pbx ? c->pbx->dtimeout * 1000 : 2000;
111 fto = to = 1000000000;
112 res = ast_readstring(c, s, maxlen, to, fto, "#");
117 int ast_app_getdata_full(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout, int audiofd, int ctrlfd)
121 res = ast_streamfile(c, prompt, c->language);
130 fto = to = 1000000000;
131 res = ast_readstring_full(c, s, maxlen, to, fto, "#", audiofd, ctrlfd);
135 int ast_app_getvoice(struct ast_channel *c, char *dest, char *dstfmt, char *prompt, int silence, int maxsec)
138 struct ast_filestream *writer;
140 int totalms=0, total;
143 struct ast_dsp *sildet;
144 /* Play prompt if requested */
146 res = ast_streamfile(c, prompt, c->language);
149 res = ast_waitstream(c,"");
153 rfmt = c->readformat;
154 res = ast_set_read_format(c, AST_FORMAT_SLINEAR);
156 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
159 sildet = ast_dsp_new();
161 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
164 writer = ast_writefile(dest, dstfmt, "Voice file", 0, 0, 0666);
166 ast_log(LOG_WARNING, "Unable to open file '%s' in format '%s' for writing\n", dest, dstfmt);
167 ast_dsp_free(sildet);
171 if ((res = ast_waitfor(c, 2000)) < 0) {
172 ast_log(LOG_NOTICE, "Waitfor failed while recording file '%s' format '%s'\n", dest, dstfmt);
178 ast_log(LOG_NOTICE, "Hungup while recording file '%s' format '%s'\n", dest, dstfmt);
181 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
182 /* Ended happily with DTMF */
185 } else if (f->frametype == AST_FRAME_VOICE) {
186 ast_dsp_silence(sildet, f, &total);
187 if (total > silence) {
188 /* Ended happily with silence */
192 totalms += f->samples / 8;
193 if (totalms > maxsec * 1000) {
194 /* Ended happily with too much stuff */
195 ast_log(LOG_NOTICE, "Constraining voice on '%s' to %d seconds\n", c->name, maxsec);
203 res = ast_set_read_format(c, rfmt);
205 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", c->name);
206 ast_dsp_free(sildet);
207 ast_closestream(writer);
211 static int (*ast_has_voicemail_func)(const char *mailbox, const char *folder) = NULL;
212 static int (*ast_messagecount_func)(const char *mailbox, int *newmsgs, int *oldmsgs) = NULL;
214 void ast_install_vm_functions(int (*has_voicemail_func)(const char *mailbox, const char *folder),
215 int (*messagecount_func)(const char *mailbox, int *newmsgs, int *oldmsgs))
217 ast_has_voicemail_func = has_voicemail_func;
218 ast_messagecount_func = messagecount_func;
221 void ast_uninstall_vm_functions(void)
223 ast_has_voicemail_func = NULL;
224 ast_messagecount_func = NULL;
227 int ast_app_has_voicemail(const char *mailbox, const char *folder)
229 static int warned = 0;
230 if (ast_has_voicemail_func)
231 return ast_has_voicemail_func(mailbox, folder);
233 if ((option_verbose > 2) && !warned) {
234 ast_verbose(VERBOSE_PREFIX_3 "Message check requested for mailbox %s/folder %s but voicemail not loaded.", mailbox, folder ? folder : "INBOX");
241 int ast_app_messagecount(const char *mailbox, int *newmsgs, int *oldmsgs)
243 static int warned = 0;
248 if (ast_messagecount_func)
249 return ast_messagecount_func(mailbox, newmsgs, oldmsgs);
251 if (!warned && (option_verbose > 2)) {
253 ast_verbose(VERBOSE_PREFIX_3 "Message count requested for mailbox %s but voicemail not loaded.", mailbox);
259 int ast_dtmf_stream(struct ast_channel *chan,struct ast_channel *peer,char *digits,int between)
268 res = ast_autoservice_start(peer);
271 res = ast_waitfor(chan,100);
273 for (ptr=digits;*ptr;*ptr++) {
275 res = ast_safe_sleep(chan, 500);
280 memset(&f, 0, sizeof(f));
281 f.frametype = AST_FRAME_DTMF;
283 f.src = "ast_dtmf_stream";
284 if (strchr("0123456789*#abcdABCD",*ptr)==NULL) {
285 ast_log(LOG_WARNING, "Illegal DTMF character '%c' in string. (0-9*#aAbBcCdD allowed)\n",*ptr);
287 res = ast_write(chan, &f);
290 /* pause between digits */
291 res = ast_safe_sleep(chan,between);
298 res = ast_autoservice_stop(peer);
303 struct linear_state {
310 static void linear_release(struct ast_channel *chan, void *params)
312 struct linear_state *ls = params;
313 if (ls->origwfmt && ast_set_write_format(chan, ls->origwfmt)) {
314 ast_log(LOG_WARNING, "Unable to restore channel '%s' to format '%d'\n", chan->name, ls->origwfmt);
321 static int linear_generator(struct ast_channel *chan, void *data, int len, int samples)
324 short buf[2048 + AST_FRIENDLY_OFFSET / 2];
325 struct linear_state *ls = data;
328 if (len > sizeof(buf) - AST_FRIENDLY_OFFSET) {
329 ast_log(LOG_WARNING, "Can't generate %d bytes of data!\n" ,len);
330 len = sizeof(buf) - AST_FRIENDLY_OFFSET;
332 memset(&f, 0, sizeof(f));
333 res = read(ls->fd, buf + AST_FRIENDLY_OFFSET/2, len);
335 f.frametype = AST_FRAME_VOICE;
336 f.subclass = AST_FORMAT_SLINEAR;
337 f.data = buf + AST_FRIENDLY_OFFSET/2;
340 f.offset = AST_FRIENDLY_OFFSET;
348 static void *linear_alloc(struct ast_channel *chan, void *params)
350 struct linear_state *ls;
351 /* In this case, params is already malloc'd */
354 if (ls->allowoverride)
355 ast_set_flag(chan, AST_FLAG_WRITE_INT);
357 ast_clear_flag(chan, AST_FLAG_WRITE_INT);
358 ls->origwfmt = chan->writeformat;
359 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
360 ast_log(LOG_WARNING, "Unable to set '%s' to linear format (write)\n", chan->name);
368 static struct ast_generator linearstream =
371 release: linear_release,
372 generate: linear_generator,
375 int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, int allowoverride)
377 struct linear_state *lin;
382 if (!filename || ast_strlen_zero(filename))
385 if (filename[0] == '/')
386 strncpy(tmpf, filename, sizeof(tmpf) - 1);
388 snprintf(tmpf, sizeof(tmpf), "%s/%s/%s", (char *)ast_config_AST_VAR_DIR, "sounds", filename);
389 fd = open(tmpf, O_RDONLY);
391 ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", tmpf, strerror(errno));
395 lin = malloc(sizeof(struct linear_state));
397 memset(lin, 0, sizeof(lin));
399 lin->allowoverride = allowoverride;
400 lin->autoclose = autoclose;
401 res = ast_activate_generator(chan, &linearstream, lin);
406 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)
408 struct timeval started, ended;
409 long elapsed = 0,last_elapsed =0;
416 blen += strlen(stop);
418 blen += strlen(pause);
421 breaks = alloca(blen + 1);
423 strcat(breaks, stop);
424 strcat(breaks, pause);
426 if (chan->_state != AST_STATE_UP)
427 res = ast_answer(chan);
430 ast_stopstream(chan);
434 if ((end = strchr(file,':'))) {
435 if (!strcasecmp(end, ":end")) {
443 gettimeofday(&started,NULL);
446 ast_stopstream(chan);
447 res = ast_streamfile(chan, file, chan->language);
450 ast_seekstream(chan->stream, 0, SEEK_END);
455 ast_stream_fastforward(chan->stream, elapsed);
456 last_elapsed = elapsed - 200;
459 res = ast_waitstream_fr(chan, breaks, fwd, rev, skipms);
467 if (pause != NULL && strchr(pause, res)) {
468 gettimeofday(&ended, NULL);
469 elapsed = (((ended.tv_sec * 1000) + ended.tv_usec / 1000) - ((started.tv_sec * 1000) + started.tv_usec / 1000) + last_elapsed);
472 ast_stopstream(chan);
473 res = ast_waitfordigit(chan, 1000);
476 else if (res == -1 || strchr(pause, res) || (stop && strchr(stop, res)))
487 /* if we get one of our stop chars, return it to the calling function */
488 if (stop && strchr(stop, res)) {
494 ast_stopstream(chan);
499 int ast_play_and_wait(struct ast_channel *chan, const char *fn)
502 d = ast_streamfile(chan, fn, chan->language);
505 d = ast_waitstream(chan, AST_DIGIT_ANY);
506 ast_stopstream(chan);
510 static int global_silence_threshold = 128;
511 static int global_maxsilence = 0;
513 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)
517 int x, fmtcnt=1, res=-1,outmsg=0;
519 struct ast_filestream *others[MAX_OTHER_FORMATS];
520 char *sfmt[MAX_OTHER_FORMATS];
523 struct ast_dsp *sildet=NULL; /* silence detector dsp */
524 int totalsilence = 0;
526 int gotsilence = 0; /* did we timeout for silence? */
529 if (silencethreshold < 0)
530 silencethreshold = global_silence_threshold;
533 maxsilence = global_maxsilence;
535 /* barf if no pointer passed to store duration in */
536 if (duration == NULL) {
537 ast_log(LOG_WARNING, "Error play_and_record called without duration pointer\n");
541 ast_log(LOG_DEBUG,"play_and_record: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
542 snprintf(comment,sizeof(comment),"Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
545 d = ast_play_and_wait(chan, playfile);
547 d = ast_streamfile(chan, "beep",chan->language);
549 d = ast_waitstream(chan,"");
554 fmts = ast_strdupa(fmt);
557 strsep(&stringp, "|");
558 ast_log(LOG_DEBUG,"Recording Formats: sfmts=%s\n", fmts);
559 sfmt[0] = ast_strdupa(fmts);
561 while((fmt = strsep(&stringp, "|"))) {
562 if (fmtcnt > MAX_OTHER_FORMATS - 1) {
563 ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app_voicemail.c\n");
566 sfmt[fmtcnt++] = ast_strdupa(fmt);
570 end=start; /* pre-initialize end to be same as start in case we never get into loop */
571 for (x=0;x<fmtcnt;x++) {
572 others[x] = ast_writefile(recordfile, sfmt[x], comment, O_TRUNC, 0, 0700);
573 ast_verbose( VERBOSE_PREFIX_3 "x=%i, open writing: %s format: %s, %p\n", x, recordfile, sfmt[x], others[x]);
581 ast_unlock_path(path);
585 if (maxsilence > 0) {
586 sildet = ast_dsp_new(); /* Create the silence detector */
588 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
591 ast_dsp_set_threshold(sildet, silencethreshold);
592 rfmt = chan->readformat;
593 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
595 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
596 ast_dsp_free(sildet);
602 /* Loop forever, writing the packets we read to the writer(s), until
603 we read a # or get a hangup */
606 res = ast_waitfor(chan, 2000);
608 ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
609 /* Try one more time in case of masq */
610 res = ast_waitfor(chan, 2000);
612 ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
624 if (f->frametype == AST_FRAME_VOICE) {
625 /* write each format */
626 for (x=0;x<fmtcnt;x++) {
627 res = ast_writestream(others[x], f);
630 /* Silence Detection */
631 if (maxsilence > 0) {
633 ast_dsp_silence(sildet, f, &dspsilence);
635 totalsilence = dspsilence;
639 if (totalsilence > maxsilence) {
640 /* Ended happily with silence */
641 if (option_verbose > 2)
642 ast_verbose( VERBOSE_PREFIX_3 "Recording automatically stopped after a silence of %d seconds\n", totalsilence/1000);
649 /* Exit on any error */
651 ast_log(LOG_WARNING, "Error writing frame\n");
655 } else if (f->frametype == AST_FRAME_VIDEO) {
656 /* Write only once */
657 ast_writestream(others[0], f);
658 } else if (f->frametype == AST_FRAME_DTMF) {
659 if (f->subclass == '#') {
660 if (option_verbose > 2)
661 ast_verbose( VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
668 if (f->subclass == '0') {
669 /* Check for a '0' during message recording also, in case caller wants operator */
670 if (option_verbose > 2)
671 ast_verbose(VERBOSE_PREFIX_3 "User cancelled by pressing %c\n", f->subclass);
679 if (maxtime < (end - start)) {
680 if (option_verbose > 2)
681 ast_verbose( VERBOSE_PREFIX_3 "Took too long, cutting it short...\n");
690 if (end == start) time(&end);
692 if (option_verbose > 2)
693 ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
698 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", recordfile, sfmt[x]);
701 *duration = end - start;
703 for (x=0;x<fmtcnt;x++) {
708 ast_stream_rewind(others[x], totalsilence-200);
710 ast_stream_rewind(others[x], 200);
712 ast_truncstream(others[x]);
713 ast_closestream(others[x]);
716 if (ast_set_read_format(chan, rfmt)) {
717 ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
721 /* Let them know recording is stopped */
722 if(!ast_streamfile(chan, "auth-thankyou", chan->language))
723 ast_waitstream(chan, "");
726 ast_dsp_free(sildet);
730 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)
734 int x, fmtcnt=1, res=-1,outmsg=0;
736 struct ast_filestream *others[MAX_OTHER_FORMATS];
737 struct ast_filestream *realfiles[MAX_OTHER_FORMATS];
738 char *sfmt[MAX_OTHER_FORMATS];
741 struct ast_dsp *sildet; /* silence detector dsp */
742 int totalsilence = 0;
744 int gotsilence = 0; /* did we timeout for silence? */
746 char prependfile[80];
748 if (silencethreshold < 0)
749 silencethreshold = global_silence_threshold;
752 maxsilence = global_maxsilence;
754 /* barf if no pointer passed to store duration in */
755 if (duration == NULL) {
756 ast_log(LOG_WARNING, "Error play_and_prepend called without duration pointer\n");
760 ast_log(LOG_DEBUG,"play_and_prepend: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
761 snprintf(comment,sizeof(comment),"Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
763 if (playfile || beep) {
765 d = ast_play_and_wait(chan, playfile);
767 d = ast_streamfile(chan, "beep",chan->language);
769 d = ast_waitstream(chan,"");
773 strncpy(prependfile, recordfile, sizeof(prependfile) -1);
774 strncat(prependfile, "-prepend", sizeof(prependfile) - strlen(prependfile) - 1);
776 fmts = ast_strdupa(fmt);
779 strsep(&stringp, "|");
780 ast_log(LOG_DEBUG,"Recording Formats: sfmts=%s\n", fmts);
781 sfmt[0] = ast_strdupa(fmts);
783 while((fmt = strsep(&stringp, "|"))) {
784 if (fmtcnt > MAX_OTHER_FORMATS - 1) {
785 ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app_voicemail.c\n");
788 sfmt[fmtcnt++] = ast_strdupa(fmt);
792 end=start; /* pre-initialize end to be same as start in case we never get into loop */
793 for (x=0;x<fmtcnt;x++) {
794 others[x] = ast_writefile(prependfile, sfmt[x], comment, O_TRUNC, 0, 0700);
795 ast_verbose( VERBOSE_PREFIX_3 "x=%i, open writing: %s format: %s, %p\n", x, prependfile, sfmt[x], others[x]);
801 sildet = ast_dsp_new(); /* Create the silence detector */
803 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
806 ast_dsp_set_threshold(sildet, silencethreshold);
808 if (maxsilence > 0) {
809 rfmt = chan->readformat;
810 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
812 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
818 /* Loop forever, writing the packets we read to the writer(s), until
819 we read a # or get a hangup */
822 res = ast_waitfor(chan, 2000);
824 ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
825 /* Try one more time in case of masq */
826 res = ast_waitfor(chan, 2000);
828 ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
840 if (f->frametype == AST_FRAME_VOICE) {
841 /* write each format */
842 for (x=0;x<fmtcnt;x++) {
845 res = ast_writestream(others[x], f);
848 /* Silence Detection */
849 if (maxsilence > 0) {
851 ast_dsp_silence(sildet, f, &dspsilence);
853 totalsilence = dspsilence;
857 if (totalsilence > maxsilence) {
858 /* Ended happily with silence */
859 if (option_verbose > 2)
860 ast_verbose( VERBOSE_PREFIX_3 "Recording automatically stopped after a silence of %d seconds\n", totalsilence/1000);
867 /* Exit on any error */
869 ast_log(LOG_WARNING, "Error writing frame\n");
873 } else if (f->frametype == AST_FRAME_VIDEO) {
874 /* Write only once */
875 ast_writestream(others[0], f);
876 } else if (f->frametype == AST_FRAME_DTMF) {
877 /* stop recording with any digit */
878 if (option_verbose > 2)
879 ast_verbose( VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
887 if (maxtime < (end - start)) {
888 if (option_verbose > 2)
889 ast_verbose( VERBOSE_PREFIX_3 "Took too long, cutting it short...\n");
898 if (end == start) time(&end);
900 if (option_verbose > 2)
901 ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
905 /* delete all the prepend files */
906 for (x=0;x<fmtcnt;x++) {
909 ast_closestream(others[x]);
910 ast_filedelete(prependfile, sfmt[x]);
915 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", prependfile, sfmt[x]);
917 *duration = end - start;
923 struct ast_frame *fr;
924 for (x=0;x<fmtcnt;x++) {
925 snprintf(comment, sizeof(comment), "Opening the real file %s.%s\n", recordfile, sfmt[x]);
926 realfiles[x] = ast_readfile(recordfile, sfmt[x], comment, O_RDONLY, 0, 0);
927 if (!others[x] || !realfiles[x])
930 ast_stream_rewind(others[x], totalsilence-200);
932 ast_stream_rewind(others[x], 200);
933 ast_truncstream(others[x]);
934 /* add the original file too */
935 while ((fr = ast_readframe(realfiles[x]))) {
936 ast_writestream(others[x],fr);
938 ast_closestream(others[x]);
939 ast_closestream(realfiles[x]);
940 ast_filerename(prependfile, recordfile, sfmt[x]);
942 ast_verbose("Recording Format: sfmts=%s, prependfile %s, recordfile %s\n", sfmt[x],prependfile,recordfile);
944 ast_filedelete(prependfile, sfmt[x]);
948 if (ast_set_read_format(chan, rfmt)) {
949 ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
954 /* Let them know it worked */
955 ast_streamfile(chan, "auth-thankyou", chan->language);
956 ast_waitstream(chan, "");
962 /* Channel group core functions */
964 int ast_app_group_split_group(char *data, char *group, int group_max, char *category, int category_max)
968 char *grp=NULL, *cat=NULL;
970 if (data && !ast_strlen_zero(data)) {
971 strncpy(tmp, data, sizeof(tmp) - 1);
973 cat = strchr(tmp, '@');
980 if (grp && !ast_strlen_zero(grp))
981 strncpy(group, grp, group_max -1);
986 snprintf(category, category_max, "%s_%s", GROUP_CATEGORY_PREFIX, cat);
988 strncpy(category, GROUP_CATEGORY_PREFIX, category_max - 1);
993 int ast_app_group_set_channel(struct ast_channel *chan, char *data)
997 char category[80] = "";
999 if (!ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category))) {
1000 pbx_builtin_setvar_helper(chan, category, group);
1007 int ast_app_group_get_count(char *group, char *category)
1009 struct ast_channel *chan;
1014 if (category && !ast_strlen_zero(category)) {
1015 strncpy(cat, category, sizeof(cat) - 1);
1017 strncpy(cat, GROUP_CATEGORY_PREFIX, sizeof(cat) - 1);
1020 if (group && !ast_strlen_zero(group)) {
1021 chan = ast_channel_walk_locked(NULL);
1023 test = pbx_builtin_getvar_helper(chan, cat);
1024 if (test && !strcasecmp(test, group))
1026 ast_mutex_unlock(&chan->lock);
1027 chan = ast_channel_walk_locked(chan);
1034 int ast_app_group_match_get_count(char *groupmatch, char *category)
1037 struct ast_channel *chan;
1042 if (!groupmatch || ast_strlen_zero(groupmatch))
1045 /* if regex compilation fails, return zero matches */
1046 if (regcomp(®exbuf, groupmatch, REG_EXTENDED | REG_NOSUB))
1049 if (category && !ast_strlen_zero(category)) {
1050 strncpy(cat, category, sizeof(cat) - 1);
1052 strncpy(cat, GROUP_CATEGORY_PREFIX, sizeof(cat) - 1);
1055 chan = ast_channel_walk_locked(NULL);
1057 test = pbx_builtin_getvar_helper(chan, cat);
1058 if (test && !regexec(®exbuf, test, 0, NULL, 0))
1060 ast_mutex_unlock(&chan->lock);
1061 chan = ast_channel_walk_locked(chan);
1069 int ast_separate_app_args(char *buf, char delim, char **array, int arraylen)
1072 memset(array, 0, arraylen * sizeof(char *));
1075 for (array[x] = buf ; x < arraylen && array[x]; x++) {
1076 if ((array[x+1] = strchr(array[x], delim))) {
1084 int ast_lock_path(const char *path)
1091 s = alloca(strlen(path) + 10);
1092 fs = alloca(strlen(path) + 20);
1094 ast_log(LOG_WARNING, "Out of memory!\n");
1097 snprintf(fs, strlen(path) + 19, "%s/%s-%08x", path, ".lock", rand());
1098 fd = open(fs, O_WRONLY | O_CREAT | O_EXCL, 0600);
1100 fprintf(stderr, "Unable to create lock file: %s\n", strerror(errno));
1104 snprintf(s, strlen(path) + 9, "%s/%s", path, ".lock");
1106 while (((res = link(fs, s)) < 0) && (errno == EEXIST) && (time(NULL) - start < 5))
1109 ast_log(LOG_WARNING, "Failed to lock path '%s': %s\n", path, strerror(errno));
1112 ast_log(LOG_DEBUG, "Locked path '%s'\n", path);
1116 int ast_unlock_path(const char *path)
1119 s = alloca(strlen(path) + 10);
1122 snprintf(s, strlen(path) + 9, "%s/%s", path, ".lock");
1123 ast_log(LOG_DEBUG, "Unlocked path '%s'\n", path);
1127 int ast_record_review(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, const char *path)
1129 int silencethreshold = 128;
1133 int max_attempts = 3;
1136 int message_exists = 0;
1137 /* Note that urgent and private are for flagging messages as such in the future */
1139 /* barf if no pointer passed to store duration in */
1140 if (duration == NULL) {
1141 ast_log(LOG_WARNING, "Error ast_record_review called without duration pointer\n");
1145 cmd = '3'; /* Want to start by recording */
1147 while ((cmd >= 0) && (cmd != 't')) {
1150 if (!message_exists) {
1151 /* In this case, 1 is to record a message */
1155 ast_streamfile(chan, "vm-msgsaved", chan->language);
1156 ast_waitstream(chan, "");
1162 ast_verbose(VERBOSE_PREFIX_3 "Reviewing the recording\n");
1163 ast_streamfile(chan, recordfile, chan->language);
1164 cmd = ast_waitstream(chan, AST_DIGIT_ANY);
1170 ast_verbose(VERBOSE_PREFIX_3 "Re-recording\n");
1172 ast_verbose(VERBOSE_PREFIX_3 "Recording\n");
1174 cmd = ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, silencethreshold, maxsilence, path);
1176 /* User has hung up, no options to give */
1181 } else if (cmd == '*') {
1185 /* If all is well, a message exists */
1198 cmd = ast_play_and_wait(chan, "vm-sorry");
1201 if (message_exists) {
1202 cmd = ast_play_and_wait(chan, "vm-review");
1205 cmd = ast_play_and_wait(chan, "vm-torerecord");
1207 cmd = ast_waitfordigit(chan, 600);
1211 cmd = ast_waitfordigit(chan, 6000);
1215 if (attempts > max_attempts) {
1225 #define RES_UPONE (1 << 16)
1226 #define RES_EXIT (1 << 17)
1227 #define RES_REPEAT (1 << 18)
1228 #define RES_RESTART ((1 << 19) | RES_REPEAT)
1230 static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata);
1231 static int ivr_dispatch(struct ast_channel *chan, struct ast_ivr_option *option, char *exten, void *cbdata)
1234 int (*ivr_func)(struct ast_channel *, void *);
1238 switch(option->action) {
1239 case AST_ACTION_UPONE:
1241 case AST_ACTION_EXIT:
1242 return RES_EXIT | (((unsigned long)(option->adata)) & 0xffff);
1243 case AST_ACTION_REPEAT:
1244 return RES_REPEAT | (((unsigned long)(option->adata)) & 0xffff);
1245 case AST_ACTION_RESTART:
1246 return RES_RESTART ;
1247 case AST_ACTION_NOOP:
1249 case AST_ACTION_BACKGROUND:
1250 res = ast_streamfile(chan, (char *)option->adata, chan->language);
1252 res = ast_waitstream(chan, AST_DIGIT_ANY);
1254 ast_log(LOG_NOTICE, "Unable to find file '%s'!\n", (char *)option->adata);
1258 case AST_ACTION_PLAYBACK:
1259 res = ast_streamfile(chan, (char *)option->adata, chan->language);
1261 res = ast_waitstream(chan, "");
1263 ast_log(LOG_NOTICE, "Unable to find file '%s'!\n", (char *)option->adata);
1267 case AST_ACTION_MENU:
1268 res = ast_ivr_menu_run_internal(chan, (struct ast_ivr_menu *)option->adata, cbdata);
1269 /* Do not pass entry errors back up, treaat ast though ti was an "UPONE" */
1273 case AST_ACTION_WAITOPTION:
1274 res = ast_waitfordigit(chan, 1000 * (chan->pbx ? chan->pbx->rtimeout : 10));
1278 case AST_ACTION_CALLBACK:
1279 ivr_func = option->adata;
1280 res = ivr_func(chan, cbdata);
1282 case AST_ACTION_TRANSFER:
1283 res = ast_parseable_goto(chan, option->adata);
1285 case AST_ACTION_PLAYLIST:
1286 case AST_ACTION_BACKLIST:
1288 c = ast_strdupa(option->adata);
1290 while((n = strsep(&c, ";")))
1291 if ((res = ast_streamfile(chan, n, chan->language)) || (res = ast_waitstream(chan, (option->action == AST_ACTION_BACKLIST) ? AST_DIGIT_ANY : "")))
1293 ast_stopstream(chan);
1297 ast_log(LOG_NOTICE, "Unknown dispatch function %d, ignoring!\n", option->action);
1303 static int option_exists(struct ast_ivr_menu *menu, char *option)
1306 for (x=0;menu->options[x].option;x++)
1307 if (!strcasecmp(menu->options[x].option, option))
1312 static int option_matchmore(struct ast_ivr_menu *menu, char *option)
1315 for (x=0;menu->options[x].option;x++)
1316 if ((!strncasecmp(menu->options[x].option, option, strlen(option))) &&
1317 (menu->options[x].option[strlen(option)]))
1322 static int read_newoption(struct ast_channel *chan, struct ast_ivr_menu *menu, char *exten, int maxexten)
1326 while(option_matchmore(menu, exten)) {
1327 ms = chan->pbx ? chan->pbx->dtimeout : 5000;
1328 if (strlen(exten) >= maxexten - 1)
1330 res = ast_waitfordigit(chan, ms);
1333 exten[strlen(exten) + 1] = '\0';
1334 exten[strlen(exten)] = res;
1336 return res > 0 ? 0 : res;
1339 static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata)
1341 /* Execute an IVR menu structure */
1345 char exten[AST_MAX_EXTENSION] = "s";
1346 if (option_exists(menu, "s") < 0) {
1348 if (option_exists(menu, "g") < 0) {
1349 ast_log(LOG_WARNING, "No 's' nor 'g' extension in menu '%s'!\n", menu->title);
1354 while(menu->options[pos].option) {
1355 if (!strcasecmp(menu->options[pos].option, exten)) {
1356 res = ivr_dispatch(chan, menu->options + pos, exten, cbdata);
1357 ast_log(LOG_DEBUG, "IVR Dispatch of '%s' (pos %d) yields %d\n", exten, pos, res);
1360 else if (res & RES_UPONE)
1362 else if (res & RES_EXIT)
1364 else if (res & RES_REPEAT) {
1365 int maxretries = res & 0xffff;
1366 if ((res & RES_RESTART) == RES_RESTART) {
1372 if ((maxretries > 0) && (retries >= maxretries)) {
1373 ast_log(LOG_DEBUG, "Max retries %d exceeded\n", maxretries);
1376 if (option_exists(menu, "g") > -1)
1378 else if (option_exists(menu, "s") > -1)
1383 } else if (res && strchr(AST_DIGIT_ANY, res)) {
1384 ast_log(LOG_DEBUG, "Got start of extension, %c\n", res);
1387 if ((res = read_newoption(chan, menu, exten, sizeof(exten))))
1389 if (option_exists(menu, exten) < 0) {
1390 if (option_exists(menu, "i")) {
1391 ast_log(LOG_DEBUG, "Invalid extension entered, going to 'i'!\n");
1396 ast_log(LOG_DEBUG, "Aborting on invalid entry, with no 'i' option!\n");
1401 ast_log(LOG_DEBUG, "New existing extension: %s\n", exten);
1409 ast_log(LOG_DEBUG, "Stopping option '%s', res is %d\n", exten, res);
1411 if (!strcasecmp(exten, "s"))
1419 int ast_ivr_menu_run(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata)
1422 res = ast_ivr_menu_run_internal(chan, menu, cbdata);
1423 /* Hide internal coding */
1429 char *ast_read_textfile(const char *filename)
1433 struct stat filesize;
1436 if(stat(filename,&filesize)== -1){
1437 ast_log(LOG_WARNING,"Error can't stat %s\n", filename);
1440 count=filesize.st_size + 1;
1441 fd = open(filename, O_RDONLY);
1443 ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", filename, strerror(errno));
1446 output=(char *)malloc(count);
1448 res = read(fd, output, count - 1);
1449 if (res == count - 1) {
1452 ast_log(LOG_WARNING, "Short read of %s (%d of %d): %s\n", filename, res, count - 1, strerror(errno));
1457 ast_log(LOG_WARNING, "Out of memory!\n");
1462 int ast_parseoptions(const struct ast_option *options, struct ast_flags *flags, char **args, char *optstr)
1475 flags->flags |= options[curarg].flag;
1476 argloc = options[curarg].argoption;
1482 while(*s && (*s != ')')) s++;
1485 args[argloc - 1] = arg;
1489 ast_log(LOG_WARNING, "Missing closing parenthesis for argument '%c'\n", curarg);
1493 args[argloc - 1] = NULL;