Fri Aug 24 02:22:23 2007

Asterisk developer's documentation


app.c File Reference

Convenient Application Routines. More...

#include "asterisk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <regex.h>
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/file.h"
#include "asterisk/app.h"
#include "asterisk/dsp.h"
#include "asterisk/logger.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/indications.h"
#include "asterisk/linkedlists.h"

Include dependency graph for app.c:

Go to the source code of this file.

Data Structures

struct  linear_state

Defines

#define MAX_OTHER_FORMATS   10
#define RES_EXIT   (1 << 17)
#define RES_REPEAT   (1 << 18)
#define RES_RESTART   ((1 << 19) | RES_REPEAT)
#define RES_UPONE   (1 << 16)

Functions

static int __ast_play_and_record (struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, int beep, int silencethreshold, int maxsilence, const char *path, int prepend, const char *acceptdtmf, const char *canceldtmf)
int ast_app_dtget (struct ast_channel *chan, const char *context, char *collect, size_t size, int maxlen, int timeout)
 Present a dialtone and collect a certain length extension.
int ast_app_getdata (struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout)
 Plays a stream and gets DTMF data from a channel.
int ast_app_getdata_full (struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout, int audiofd, int ctrlfd)
 Full version with audiofd and controlfd. NOTE: returns '2' on ctrlfd available, not '1' like other full functions.
int ast_app_group_discard (struct ast_channel *chan)
int ast_app_group_get_count (const char *group, const char *category)
ast_group_infoast_app_group_list_head (void)
int ast_app_group_list_lock (void)
int ast_app_group_list_unlock (void)
int ast_app_group_match_get_count (const char *groupmatch, const char *category)
int ast_app_group_set_channel (struct ast_channel *chan, const char *data)
int ast_app_group_split_group (const char *data, char *group, int group_max, char *category, int category_max)
int ast_app_group_update (struct ast_channel *old, struct ast_channel *new)
int ast_app_has_voicemail (const char *mailbox, const char *folder)
int ast_app_inboxcount (const char *mailbox, int *newmsgs, int *oldmsgs)
int ast_app_messagecount (const char *context, const char *mailbox, const char *folder)
int ast_app_parse_options (const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
 Parses a string containing application options and sets flags/arguments.
unsigned int ast_app_separate_args (char *buf, char delim, char **array, int arraylen)
 Separate a string into arguments in an array.
int ast_control_streamfile (struct ast_channel *chan, const char *file, const char *fwd, const char *rev, const char *stop, const char *pause, const char *restart, int skipms)
int ast_dtmf_stream (struct ast_channel *chan, struct ast_channel *peer, const char *digits, int between)
 Send DTMF to a channel.
void ast_install_vm_functions (int(*has_voicemail_func)(const char *mailbox, const char *folder), int(*inboxcount_func)(const char *mailbox, int *newmsgs, int *oldmsgs), int(*messagecount_func)(const char *context, const char *mailbox, const char *folder))
int ast_ivr_menu_run (struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata)
 Runs an IVR menu.
static int ast_ivr_menu_run_internal (struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata)
int ast_linear_stream (struct ast_channel *chan, const char *filename, int fd, int allowoverride)
static AST_LIST_HEAD_STATIC (groups, ast_group_info)
enum AST_LOCK_RESULT ast_lock_path (const char *path)
 Lock a filesystem path.
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)
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)
int ast_play_and_record_full (struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, int silencethreshold, int maxsilence, const char *path, const char *acceptdtmf, const char *canceldtmf)
int ast_play_and_wait (struct ast_channel *chan, const char *fn)
char * ast_read_textfile (const char *filename)
int ast_record_review (struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, const char *path)
void ast_uninstall_vm_functions (void)
int ast_unlock_path (const char *path)
static int ivr_dispatch (struct ast_channel *chan, struct ast_ivr_option *option, char *exten, void *cbdata)
static void * linear_alloc (struct ast_channel *chan, void *params)
static int linear_generator (struct ast_channel *chan, void *data, int len, int samples)
static void linear_release (struct ast_channel *chan, void *params)
static int option_exists (struct ast_ivr_menu *menu, char *option)
static int option_matchmore (struct ast_ivr_menu *menu, char *option)
static int read_newoption (struct ast_channel *chan, struct ast_ivr_menu *menu, char *exten, int maxexten)

Variables

static int(*) ast_has_voicemail_func (const char *mailbox, const char *folder) = NULL
static int(*) ast_inboxcount_func (const char *mailbox, int *newmsgs, int *oldmsgs) = NULL
static int(*) ast_messagecount_func (const char *context, const char *mailbox, const char *folder) = NULL
static char default_acceptdtmf [] = "#"
static char default_canceldtmf [] = ""
static int global_maxsilence = 0
static int global_silence_threshold = 128
static struct ast_generator linearstream


Detailed Description

Convenient Application Routines.

Author:
Mark Spencer <markster@digium.com>

Definition in file app.c.


Define Documentation

#define MAX_OTHER_FORMATS   10

Definition at line 54 of file app.c.

Referenced by __ast_play_and_record().

#define RES_EXIT   (1 << 17)

Definition at line 1148 of file app.c.

Referenced by ast_ivr_menu_run_internal(), and ivr_dispatch().

#define RES_REPEAT   (1 << 18)

Definition at line 1149 of file app.c.

Referenced by ast_ivr_menu_run_internal(), and ivr_dispatch().

#define RES_RESTART   ((1 << 19) | RES_REPEAT)

Definition at line 1150 of file app.c.

Referenced by ast_ivr_menu_run_internal(), and ivr_dispatch().

#define RES_UPONE   (1 << 16)

Definition at line 1147 of file app.c.

Referenced by ast_ivr_menu_run_internal(), and ivr_dispatch().


Function Documentation

static int __ast_play_and_record ( struct ast_channel chan,
const char *  playfile,
const char *  recordfile,
int  maxtime,
const char *  fmt,
int *  duration,
int  beep,
int  silencethreshold,
int  maxsilence,
const char *  path,
int  prepend,
const char *  acceptdtmf,
const char *  canceldtmf 
) [static]

Optionally play a sound file or a beep, then record audio and video from the channel.

Parameters:
chan Channel to playback to/record from.
playfile Filename of sound to play before recording begins.
recordfile Filename to record to.
maxtime Maximum length of recording (in milliseconds).
fmt Format(s) to record message in. Multiple formats may be specified by separating them with a '|'.
duration Where to store actual length of the recorded message (in milliseconds).
beep Whether to play a beep before starting to record.
silencethreshold 
maxsilence Length of silence that will end a recording (in milliseconds).
path Optional filesystem path to unlock.
prepend If true, prepend the recorded audio to an existing file.
acceptdtmf DTMF digits that will end the recording.
canceldtmf DTMF digits that will cancel the recording.

Definition at line 502 of file app.c.

References ast_channel_start_silence_generator(), ast_channel_stop_silence_generator(), ast_closestream(), AST_CONTROL_VIDUPDATE, ast_dsp_free(), ast_dsp_new(), ast_dsp_set_threshold(), ast_dsp_silence(), ast_filedelete(), ast_filerename(), AST_FORMAT_SLINEAR, AST_FRAME_DTMF, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frfree(), ast_getformatname(), ast_indicate(), ast_log(), ast_opt_transmit_silence, ast_play_and_wait(), ast_read(), ast_readfile(), ast_readframe(), ast_set_read_format(), ast_strdupa, ast_stream_and_wait(), ast_stream_rewind(), ast_truncstream(), ast_unlock_path(), ast_verbose(), ast_waitfor(), ast_writefile(), ast_writestream(), f, LOG_DEBUG, LOG_WARNING, MAX, MAX_OTHER_FORMATS, option_debug, option_verbose, ast_channel::readformat, strsep(), ast_dsp::totalsilence, VERBOSE_PREFIX_3, and VERBOSE_PREFIX_4.

Referenced by ast_play_and_prepend(), ast_play_and_record(), and ast_play_and_record_full().

00503 {
00504    int d = 0;
00505    char *fmts;
00506    char comment[256];
00507    int x, fmtcnt = 1, res = -1, outmsg = 0;
00508    struct ast_filestream *others[MAX_OTHER_FORMATS];
00509    char *sfmt[MAX_OTHER_FORMATS];
00510    char *stringp = NULL;
00511    time_t start, end;
00512    struct ast_dsp *sildet = NULL;   /* silence detector dsp */
00513    int totalsilence = 0;
00514    int rfmt = 0;
00515    struct ast_silence_generator *silgen = NULL;
00516    char prependfile[80];
00517    int no_audio = 0;
00518 
00519    if (silencethreshold < 0)
00520       silencethreshold = global_silence_threshold;
00521 
00522    if (maxsilence < 0)
00523       maxsilence = global_maxsilence;
00524 
00525    /* barf if no pointer passed to store duration in */
00526    if (duration == NULL) {
00527       ast_log(LOG_WARNING, "Error play_and_record called without duration pointer\n");
00528       return -1;
00529    }
00530 
00531    if (option_debug)
00532       ast_log(LOG_DEBUG,"play_and_record: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
00533    snprintf(comment, sizeof(comment), "Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
00534 
00535    if (playfile || beep) {
00536       if (!beep)
00537          d = ast_play_and_wait(chan, playfile);
00538       if (d > -1)
00539          d = ast_stream_and_wait(chan, "beep", chan->language, "");
00540       if (d < 0)
00541          return -1;
00542    }
00543 
00544    if (prepend) {
00545       ast_copy_string(prependfile, recordfile, sizeof(prependfile)); 
00546       strncat(prependfile, "-prepend", sizeof(prependfile) - strlen(prependfile) - 1);
00547    }
00548 
00549    fmts = ast_strdupa(fmt);
00550 
00551    stringp = fmts;
00552    strsep(&stringp, "|");
00553    if (option_debug)
00554       ast_log(LOG_DEBUG, "Recording Formats: sfmts=%s\n", fmts);
00555    sfmt[0] = ast_strdupa(fmts);
00556 
00557    while ((fmt = strsep(&stringp, "|"))) {
00558       if (fmtcnt > MAX_OTHER_FORMATS - 1) {
00559          ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app.c\n");
00560          break;
00561       }
00562       sfmt[fmtcnt++] = ast_strdupa(fmt);
00563    }
00564 
00565    end = start = time(NULL);  /* pre-initialize end to be same as start in case we never get into loop */
00566    for (x = 0; x < fmtcnt; x++) {
00567       others[x] = ast_writefile(prepend ? prependfile : recordfile, sfmt[x], comment, O_TRUNC, 0, 0777);
00568       if (option_verbose > 2)
00569          ast_verbose(VERBOSE_PREFIX_3 "x=%d, open writing:  %s format: %s, %p\n", x, prepend ? prependfile : recordfile, sfmt[x], others[x]);
00570 
00571       if (!others[x])
00572          break;
00573    }
00574 
00575    if (path)
00576       ast_unlock_path(path);
00577 
00578    if (maxsilence > 0) {
00579       sildet = ast_dsp_new(); /* Create the silence detector */
00580       if (!sildet) {
00581          ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
00582          return -1;
00583       }
00584       ast_dsp_set_threshold(sildet, silencethreshold);
00585       rfmt = chan->readformat;
00586       res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
00587       if (res < 0) {
00588          ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
00589          ast_dsp_free(sildet);
00590          return -1;
00591       }
00592    }
00593 
00594    if (!prepend) {
00595       /* Request a video update */
00596       ast_indicate(chan, AST_CONTROL_VIDUPDATE);
00597 
00598       if (ast_opt_transmit_silence)
00599          silgen = ast_channel_start_silence_generator(chan);
00600    }
00601 
00602    if (x == fmtcnt) {
00603       /* Loop forever, writing the packets we read to the writer(s), until
00604          we read a digit or get a hangup */
00605       struct ast_frame *f;
00606       for (;;) {
00607          res = ast_waitfor(chan, 2000);
00608          if (!res) {
00609             if (option_debug)
00610                ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
00611             /* Try one more time in case of masq */
00612             no_audio = MAX(maxsilence, 4000);
00613             res = ast_waitfor(chan, no_audio - 2000);
00614             if (!res) {
00615                ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
00616                res = -1;
00617             } else {
00618                no_audio = 0;              
00619             }
00620          }
00621 
00622          if (res < 0) {
00623             f = NULL;
00624             break;
00625          }
00626          f = ast_read(chan);
00627          if (!f)
00628             break;
00629          if (f->frametype == AST_FRAME_VOICE) {
00630             /* write each format */
00631             for (x = 0; x < fmtcnt; x++) {
00632                if (prepend && !others[x])
00633                   break;
00634                res = ast_writestream(others[x], f);
00635             }
00636 
00637             /* Silence Detection */
00638             if (maxsilence > 0) {
00639                int dspsilence = 0;
00640                ast_dsp_silence(sildet, f, &dspsilence);
00641                if (dspsilence)
00642                   totalsilence = dspsilence;
00643                else
00644                   totalsilence = 0;
00645 
00646                if (totalsilence > maxsilence) {
00647                   /* Ended happily with silence */
00648                   if (option_verbose > 2)
00649                      ast_verbose( VERBOSE_PREFIX_3 "Recording automatically stopped after a silence of %d seconds\n", totalsilence/1000);
00650                   res = 'S';
00651                   outmsg = 2;
00652                   break;
00653                }
00654             }
00655             /* Exit on any error */
00656             if (res) {
00657                ast_log(LOG_WARNING, "Error writing frame\n");
00658                break;
00659             }
00660          } else if (f->frametype == AST_FRAME_VIDEO) {
00661             /* Write only once */
00662             ast_writestream(others[0], f);
00663          } else if (f->frametype == AST_FRAME_DTMF) {
00664             if (prepend) {
00665             /* stop recording with any digit */
00666                if (option_verbose > 2) 
00667                   ast_verbose(VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
00668                res = 't';
00669                outmsg = 2;
00670                break;
00671             }
00672             if (strchr(acceptdtmf, f->subclass)) {
00673                if (option_verbose > 2)
00674                   ast_verbose(VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
00675                res = f->subclass;
00676                outmsg = 2;
00677                break;
00678             }
00679             if (strchr(canceldtmf, f->subclass)) {
00680                if (option_verbose > 2)
00681                   ast_verbose(VERBOSE_PREFIX_3 "User cancelled message by pressing %c\n", f->subclass);
00682                res = f->subclass;
00683                outmsg = 0;
00684                break;
00685             }
00686          }
00687          if (maxtime) {
00688             end = time(NULL);
00689             if (maxtime < (end - start)) {
00690                if (option_verbose > 2)
00691                   ast_verbose(VERBOSE_PREFIX_3 "Took too long, cutting it short...\n");
00692                res = 't';
00693                outmsg = 2;
00694                break;
00695             }
00696          }
00697          ast_frfree(f);
00698       }
00699       if (!f) {
00700          if (option_verbose > 2)
00701             ast_verbose(VERBOSE_PREFIX_3 "User hung up\n");
00702          res = -1;
00703          outmsg = 1;
00704       } else {
00705          ast_frfree(f);
00706       }
00707       if (end == start)
00708          end = time(NULL);
00709    } else {
00710       ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", recordfile, sfmt[x]);
00711    }
00712 
00713    if (!prepend) {
00714       if (silgen)
00715          ast_channel_stop_silence_generator(chan, silgen);
00716    }
00717    *duration = end - start;
00718    if (no_audio) {
00719       *duration -= no_audio / 1000;
00720       *duration = MAX(*duration, 0);  /* just to make sure it does not get negative */
00721    }
00722 
00723    if (!prepend) {
00724       for (x = 0; x < fmtcnt; x++) {
00725          if (!others[x])
00726             break;
00727          if (res > 0)
00728             ast_stream_rewind(others[x], totalsilence ? totalsilence - 200 : 200);
00729          ast_truncstream(others[x]);
00730          ast_closestream(others[x]);
00731       }
00732    }
00733 
00734    if (prepend && outmsg) {
00735       struct ast_filestream *realfiles[MAX_OTHER_FORMATS];
00736       struct ast_frame *fr;
00737 
00738       for (x = 0; x < fmtcnt; x++) {
00739          snprintf(comment, sizeof(comment), "Opening the real file %s.%s\n", recordfile, sfmt[x]);
00740          realfiles[x] = ast_readfile(recordfile, sfmt[x], comment, O_RDONLY, 0, 0);
00741          if (!others[x] || !realfiles[x])
00742             break;
00743          ast_stream_rewind(others[x], totalsilence ? totalsilence - 200 : 200);
00744          ast_truncstream(others[x]);
00745          /* add the original file too */
00746          while ((fr = ast_readframe(realfiles[x]))) {
00747             ast_writestream(others[x], fr);
00748             ast_frfree(fr);
00749          }
00750          ast_closestream(others[x]);
00751          ast_closestream(realfiles[x]);
00752          ast_filerename(prependfile, recordfile, sfmt[x]);
00753          if (option_verbose > 3)
00754             ast_verbose(VERBOSE_PREFIX_4 "Recording Format: sfmts=%s, prependfile %s, recordfile %s\n", sfmt[x], prependfile, recordfile);
00755          ast_filedelete(prependfile, sfmt[x]);
00756       }
00757    }
00758    if (rfmt && ast_set_read_format(chan, rfmt)) {
00759       ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
00760    }
00761    if (outmsg == 2) {
00762       ast_stream_and_wait(chan, "auth-thankyou", chan->language, "");
00763    }
00764    if (sildet)
00765       ast_dsp_free(sildet);
00766    return res;
00767 }

int ast_app_dtget ( struct ast_channel chan,
const char *  context,
char *  collect,
size_t  size,
int  maxlen,
int  timeout 
)

Present a dialtone and collect a certain length extension.

Returns:
Returns 1 on valid extension entered, -1 on hangup, or 0 on invalid extension.
Note:
Note that if 'collect' holds digits already, new digits will be appended, so be sure it's initialized properly

Definition at line 65 of file app.c.

References ast_exists_extension(), ast_get_indication_tone(), ast_ignore_pattern(), ast_log(), ast_matchmore_extension(), ast_playtones_start(), ast_playtones_stop(), ast_waitfordigit(), ast_channel::cid, ast_callerid::cid_num, tone_zone_sound::data, ast_pbx::dtimeout, LOG_NOTICE, ast_channel::pbx, and ast_channel::zone.

Referenced by builtin_atxfer(), and builtin_blindtransfer().

00066 {
00067    struct tone_zone_sound *ts;
00068    int res=0, x=0;
00069 
00070    if (maxlen > size)
00071       maxlen = size;
00072    
00073    if (!timeout && chan->pbx)
00074       timeout = chan->pbx->dtimeout;
00075    else if (!timeout)
00076       timeout = 5;
00077    
00078    ts = ast_get_indication_tone(chan->zone,"dial");
00079    if (ts && ts->data[0])
00080       res = ast_playtones_start(chan, 0, ts->data, 0);
00081    else 
00082       ast_log(LOG_NOTICE,"Huh....? no dial for indications?\n");
00083    
00084    for (x = strlen(collect); x < maxlen; ) {
00085       res = ast_waitfordigit(chan, timeout);
00086       if (!ast_ignore_pattern(context, collect))
00087          ast_playtones_stop(chan);
00088       if (res < 1)
00089          break;
00090       if (res == '#')
00091          break;
00092       collect[x++] = res;
00093       if (!ast_matchmore_extension(chan, context, collect, 1, chan->cid.cid_num))
00094          break;
00095    }
00096    if (res >= 0)
00097       res = ast_exists_extension(chan, context, collect, 1, chan->cid.cid_num) ? 1 : 0;
00098    return res;
00099 }

int ast_app_getdata ( struct ast_channel c,
char *  prompt,
char *  s,
int  maxlen,
int  timeout 
)

Plays a stream and gets DTMF data from a channel.

Parameters:
c The channel to read from
prompt The file to stream to the channel
s The string to read in to. Must be at least the size of your length
maxlen How many digits to read (maximum)
timeout set timeout to 0 for "standard" timeouts. Set timeout to -1 for "ludicrous time" (essentially never times out)

Definition at line 107 of file app.c.

References AST_DIGIT_ANY, ast_readstring(), ast_stopstream(), ast_strdupa, ast_streamfile(), ast_waitstream(), ast_pbx::dtimeout, ast_channel::pbx, result, ast_pbx::rtimeout, and strsep().

Referenced by __login_exec(), auth_exec(), conf_exec(), dictate_exec(), find_conf(), read_exec(), testclient_exec(), testserver_exec(), and vm_exec().

00108 {
00109    int res=0,to,fto, result=0;
00110    /* XXX Merge with full version? XXX */
00111    if (maxlen)
00112       s[0] = '\0';
00113    if (prompt) {
00114                 char *front;
00115                 char *temp = ast_strdupa(prompt);
00116                 while(!res && (front = strsep(&temp, "&"))) {
00117                         if( (res = ast_streamfile(c, front, c->language)) ) {
00118             res = 0;
00119             break;
00120          }
00121          if(!res && !result)
00122             result = ast_waitstream(c, AST_DIGIT_ANY);
00123          if(result)
00124             break;
00125          ast_stopstream(c);
00126                 }
00127    }
00128    fto = c->pbx ? c->pbx->rtimeout * 1000 : 6000;
00129    to = c->pbx ? c->pbx->dtimeout * 1000 : 2000;
00130 
00131    if (timeout > 0) 
00132       fto = to = timeout;
00133    if (timeout < 0) 
00134       fto = to = 1000000000;
00135    res = ast_readstring(c, s, maxlen, to, fto, "#");
00136    if(result) {
00137       char tmp[256];
00138       snprintf(tmp, sizeof(tmp), "%c%s", result, s);
00139       snprintf(s, sizeof(tmp), "%s", tmp);
00140    }
00141    return res;
00142 }

int ast_app_getdata_full ( struct ast_channel c,
char *  prompt,
char *  s,
int  maxlen,
int  timeout,
int  audiofd,
int  ctrlfd 
)

Full version with audiofd and controlfd. NOTE: returns '2' on ctrlfd available, not '1' like other full functions.

Definition at line 145 of file app.c.

References ast_readstring_full(), and ast_streamfile().

Referenced by handle_getdata().

00146 {
00147    int res, to, fto;
00148    if (prompt) {
00149       res = ast_streamfile(c, prompt, c->language);
00150       if (res < 0)
00151          return res;
00152    }
00153    fto = 6000;
00154    to = 2000;
00155    if (timeout > 0) 
00156       fto = to = timeout;
00157    if (timeout < 0) 
00158       fto = to = 1000000000;
00159    res = ast_readstring_full(c, s, maxlen, to, fto, "#", audiofd, ctrlfd);
00160    return res;
00161 }

int ast_app_group_discard ( struct ast_channel chan  ) 

Discard all group counting for a channel

Definition at line 916 of file app.c.

References AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_group_info::chan, and free.

Referenced by ast_channel_free().

00917 {
00918    struct ast_group_info *gi = NULL;
00919    
00920    AST_LIST_LOCK(&groups);
00921    AST_LIST_TRAVERSE_SAFE_BEGIN(&groups, gi, list) {
00922       if (gi->chan == chan) {
00923          AST_LIST_REMOVE_CURRENT(&groups, list);
00924          free(gi);
00925       }
00926    }
00927         AST_LIST_TRAVERSE_SAFE_END
00928    AST_LIST_UNLOCK(&groups);
00929    
00930    return 0;
00931 }

int ast_app_group_get_count ( const char *  group,
const char *  category 
)

Get the current channel count of the specified group and category.

Definition at line 859 of file app.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_strlen_zero(), ast_group_info::category, and ast_group_info::group.

Referenced by group_count_function_read().

00860 {
00861    struct ast_group_info *gi = NULL;
00862    int count = 0;
00863 
00864    if (ast_strlen_zero(group))
00865       return 0;
00866    
00867    AST_LIST_LOCK(&groups);
00868    AST_LIST_TRAVERSE(&groups, gi, list) {
00869       if (!strcasecmp(gi->group, group) && (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category))))
00870          count++;
00871    }
00872    AST_LIST_UNLOCK(&groups);
00873 
00874    return count;
00875 }

struct ast_group_info* ast_app_group_list_head ( void   ) 

Get the head of the group count list

Definition at line 938 of file app.c.

References AST_LIST_FIRST.

Referenced by group_function_read(), group_list_function_read(), and group_show_channels().

00939 {
00940    return AST_LIST_FIRST(&groups);
00941 }

int ast_app_group_list_lock ( void   ) 

Lock the group count list

Definition at line 933 of file app.c.

References AST_LIST_LOCK.

Referenced by group_function_read(), group_list_function_read(), and group_show_channels().

00934 {
00935    return AST_LIST_LOCK(&groups);
00936 }

int ast_app_group_list_unlock ( void   ) 

Unlock the group count list

Definition at line 943 of file app.c.

References AST_LIST_UNLOCK.

Referenced by group_function_read(), group_list_function_read(), and group_show_channels().

00944 {
00945    return AST_LIST_UNLOCK(&groups);
00946 }

int ast_app_group_match_get_count ( const char *  groupmatch,
const char *  category 
)

Get the current channel count of all groups that match the specified pattern and category.

Definition at line 877 of file app.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_strlen_zero(), ast_group_info::category, and ast_group_info::group.

Referenced by group_match_count_function_read().

00878 {
00879    struct ast_group_info *gi = NULL;
00880    regex_t regexbuf;
00881    int count = 0;
00882 
00883    if (ast_strlen_zero(groupmatch))
00884       return 0;
00885 
00886    /* if regex compilation fails, return zero matches */
00887    if (regcomp(&regexbuf, groupmatch, REG_EXTENDED | REG_NOSUB))
00888       return 0;
00889 
00890    AST_LIST_LOCK(&groups);
00891    AST_LIST_TRAVERSE(&groups, gi, list) {
00892       if (!regexec(&regexbuf, gi->group, 0, NULL, 0) && (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category))))
00893          count++;
00894    }
00895    AST_LIST_UNLOCK(&groups);
00896 
00897    regfree(&regexbuf);
00898 
00899    return count;
00900 }

int ast_app_group_set_channel ( struct ast_channel chan,
const char *  data 
)

Set the group for a channel, splitting the provided data into group and category, if specified.

Definition at line 816 of file app.c.

References ast_app_group_split_group(), AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_strlen_zero(), calloc, ast_group_info::category, ast_group_info::chan, free, group, ast_group_info::group, and len.

Referenced by group_function_write().

00817 {
00818    int res = 0;
00819    char group[80] = "", category[80] = "";
00820    struct ast_group_info *gi = NULL;
00821    size_t len = 0;
00822    
00823    if (ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category)))
00824       return -1;
00825    
00826    /* Calculate memory we will need if this is new */
00827    len = sizeof(*gi) + strlen(group) + 1;
00828    if (!ast_strlen_zero(category))
00829       len += strlen(category) + 1;
00830    
00831    AST_LIST_LOCK(&groups);
00832    AST_LIST_TRAVERSE_SAFE_BEGIN(&groups, gi, list) {
00833       if ((gi->chan == chan) && ((ast_strlen_zero(category) && ast_strlen_zero(gi->category)) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))) {
00834          AST_LIST_REMOVE_CURRENT(&groups, list);
00835          free(gi);
00836          break;
00837       }
00838    }
00839    AST_LIST_TRAVERSE_SAFE_END
00840    
00841    if ((gi = calloc(1, len))) {
00842       gi->chan = chan;
00843       gi->group = (char *) gi + sizeof(*gi);
00844       strcpy(gi->group, group);
00845       if (!ast_strlen_zero(category)) {
00846          gi->category = (char *) gi + sizeof(*gi) + strlen(group) + 1;
00847          strcpy(gi->category, category);
00848       }
00849       AST_LIST_INSERT_TAIL(&groups, gi, list);
00850    } else {
00851       res = -1;
00852    }
00853    
00854    AST_LIST_UNLOCK(&groups);
00855    
00856    return res;
00857 }

int ast_app_group_split_group ( const char *  data,
char *  group,
int  group_max,
char *  category,
int  category_max 
)

Split a group string into group and category, returning a default category if none is provided.

Definition at line 789 of file app.c.

References ast_strlen_zero().

Referenced by ast_app_group_set_channel(), group_count_function_read(), and group_match_count_function_read().

00790 {
00791    int res=0;
00792    char tmp[256];
00793    char *grp=NULL, *cat=NULL;
00794 
00795    if (!ast_strlen_zero(data)) {
00796       ast_copy_string(tmp, data, sizeof(tmp));
00797       grp = tmp;
00798       cat = strchr(tmp, '@');
00799       if (cat) {
00800          *cat = '\0';
00801          cat++;
00802       }
00803    }
00804 
00805    if (!ast_strlen_zero(grp))
00806       ast_copy_string(group, grp, group_max);
00807    else
00808       res = -1;
00809 
00810    if (!ast_strlen_zero(cat))
00811       ast_copy_string(category, cat, category_max);
00812 
00813    return res;
00814 }

int ast_app_group_update ( struct ast_channel oldchan,
struct ast_channel newchan 
)

Update all group counting for a channel to a new one

Definition at line 902 of file app.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, and ast_group_info::chan.

00903 {
00904    struct ast_group_info *gi = NULL;
00905 
00906    AST_LIST_LOCK(&groups);
00907    AST_LIST_TRAVERSE(&groups, gi, list) {
00908       if (gi->chan == old)
00909          gi->chan = new;
00910    }
00911    AST_LIST_UNLOCK(&groups);
00912 
00913    return 0;
00914 }

int ast_app_has_voicemail ( const char *  mailbox,
const char *  folder 
)

Determine if a given mailbox has any voicemail

Definition at line 183 of file app.c.

References ast_has_voicemail_func, ast_verbose(), option_verbose, and VERBOSE_PREFIX_3.

Referenced by action_mailboxstatus(), has_voicemail(), notify_new_message(), play_dialtone(), and run_externnotify().

00184 {
00185    static int warned = 0;
00186    if (ast_has_voicemail_func)
00187       return ast_has_voicemail_func(mailbox, folder);
00188 
00189    if ((option_verbose > 2) && !warned) {
00190       ast_verbose(VERBOSE_PREFIX_3 "Message check requested for mailbox %s/folder %s but voicemail not loaded.\n", mailbox, folder ? folder : "INBOX");
00191       warned++;
00192    }
00193    return 0;
00194 }

int ast_app_inboxcount ( const char *  mailbox,
int *  newmsgs,
int *  oldmsgs 
)

Determine number of new/old messages in a mailbox

Definition at line 197 of file app.c.

References ast_inboxcount_func, ast_verbose(), option_verbose, and VERBOSE_PREFIX_3.

Referenced by action_mailboxcount(), notify_new_message(), sip_send_mwi_to_peer(), and update_registry().

00198 {
00199    static int warned = 0;
00200    if (newmsgs)
00201       *newmsgs = 0;
00202    if (oldmsgs)
00203       *oldmsgs = 0;
00204    if (ast_inboxcount_func)
00205       return ast_inboxcount_func(mailbox, newmsgs, oldmsgs);
00206 
00207    if (!warned && (option_verbose > 2)) {
00208       warned++;
00209       ast_verbose(VERBOSE_PREFIX_3 "Message count requested for mailbox %s but voicemail not loaded.\n", mailbox);
00210    }
00211 
00212    return 0;
00213 }

int ast_app_messagecount ( const char *  context,
const char *  mailbox,
const char *  folder 
)

Determine number of messages in a given mailbox and folder

Definition at line 215 of file app.c.

References ast_messagecount_func, ast_verbose(), option_verbose, and VERBOSE_PREFIX_3.

Referenced by acf_vmcount_exec(), and hasvoicemail_exec().

00216 {
00217    static int warned = 0;
00218    if (ast_messagecount_func)
00219       return ast_messagecount_func(context, mailbox, folder);
00220 
00221    if (!warned && (option_verbose > 2)) {
00222       warned++;
00223       ast_verbose(VERBOSE_PREFIX_3 "Message count requested for mailbox %s@%s/%s but voicemail not loaded.\n", mailbox, context, folder);
00224    }
00225 
00226    return 0;
00227 }

int ast_app_parse_options ( const struct ast_app_option options,
struct ast_flags flags,
char **  args,
char *  optstr 
)

Parses a string containing application options and sets flags/arguments.

Parameters:
options The array of possible options declared with AST_APP_OPTIONS
flags The flag structure to have option flags set
args The array of argument pointers to hold arguments found
optstr The string containing the options to be parsed
Returns:
zero for success, non-zero if an error occurs
See also:
AST_APP_OPTIONS

Definition at line 1383 of file app.c.

References ast_app_option::arg_index, ast_clear_flag, AST_FLAGS_ALL, ast_log(), ast_set_flag, LOG_WARNING, and s.

Referenced by app_exec(), auth_exec(), cdr_read(), cdr_write(), chanspy_exec(), conf_exec(), extenspy_exec(), mixmonitor_exec(), page_exec(), pbx_builtin_background(), pbx_builtin_resetcdr(), pbx_builtin_waitexten(), read_exec(), vm_exec(), and vm_execmain().

01384 {
01385    char *s;
01386    int curarg;
01387    unsigned int argloc;
01388    char *arg;
01389    int res = 0;
01390 
01391    ast_clear_flag(flags, AST_FLAGS_ALL);
01392 
01393    if (!optstr)
01394       return 0;
01395 
01396    s = optstr;
01397    while (*s) {
01398       curarg = *s++ & 0x7f;   /* the array (in app.h) has 128 entries */
01399       ast_set_flag(flags, options[curarg].flag);
01400       argloc = options[curarg].arg_index;
01401       if (*s == '(') {
01402          /* Has argument */
01403          arg = ++s;
01404          if ((s = strchr(s, ')'))) {
01405             if (argloc)
01406                args[argloc - 1] = arg;
01407             *s++ = '\0';
01408          } else {
01409             ast_log(LOG_WARNING, "Missing closing parenthesis for argument '%c' in string '%s'\n", curarg, arg);
01410             res = -1;
01411             break;
01412          }
01413       } else if (argloc) {
01414          args[argloc - 1] = NULL;
01415       }
01416    }
01417 
01418    return res;
01419 }

unsigned int ast_app_separate_args ( char *  buf,
char  delim,
char **  array,
int  arraylen 
)

Separate a string into arguments in an array.

Parameters:
buf The string to be parsed (this must be a writable copy, as it will be modified)
delim The character to be used to delimit arguments
array An array of 'char *' to be filled in with pointers to the found arguments
arraylen The number of elements in the array (i.e. the number of arguments you will accept)
Note: if there are more arguments in the string than the array will hold, the last element of the array will contain the remaining arguments, not separated.

The array will be completely zeroed by this function before it populates any entries.

Returns:
The number of arguments found, or zero if the function arguments are not valid.

Definition at line 948 of file app.c.

References quote().

Referenced by app_exec(), chanspy_exec(), common_exec(), controlplayback_exec(), extenspy_exec(), pbx_builtin_setvar(), speech_background(), and speech_load().

00949 {
00950    int argc;
00951    char *scan;
00952    int paren = 0, quote = 0;
00953 
00954    if (!buf || !array || !arraylen)
00955       return 0;
00956 
00957    memset(array, 0, arraylen * sizeof(*array));
00958 
00959    scan = buf;
00960 
00961    for (argc = 0; *scan && (argc < arraylen - 1); argc++) {
00962       array[argc] = scan;
00963       for (; *scan; scan++) {
00964          if (*scan == '(')
00965             paren++;
00966          else if (*scan == ')') {
00967             if (paren)
00968                paren--;
00969          } else if (*scan == '"' && delim != '"') {
00970             quote = quote ? 0 : 1;
00971             /* Remove quote character from argument */
00972             memmove(scan, scan + 1, strlen(scan));
00973             scan--;
00974          } else if (*scan == '\\') {
00975             /* Literal character, don't parse */
00976             memmove(scan, scan + 1, strlen(scan));
00977          } else if ((*scan == delim) && !paren && !quote) {
00978             *scan++ = '\0';
00979             break;
00980          }
00981       }
00982    }
00983 
00984    if (*scan)
00985       array[argc++] = scan;
00986 
00987    return argc;
00988 }

int ast_control_streamfile ( struct ast_channel chan,
const char *  file,
const char *  fwd,
const char *  rev,
const char *  stop,
const char *  pause,
const char *  restart,
int  skipms 
)

Stream a file with fast forward, pause, reverse, restart.

Definition at line 377 of file app.c.

References ast_channel::_state, ast_answer(), ast_log(), ast_seekstream(), AST_STATE_UP, ast_stopstream(), ast_streamfile(), ast_tellstream(), ast_waitfordigit(), ast_waitstream_fr(), LOG_DEBUG, option_debug, and ast_channel::stream.

Referenced by controlplayback_exec(), handle_controlstreamfile(), and wait_file().

00381 {
00382    char *breaks = NULL;
00383    char *end = NULL;
00384    int blen = 2;
00385    int res;
00386    long pause_restart_point = 0;
00387 
00388    if (stop)
00389       blen += strlen(stop);
00390    if (pause)
00391       blen += strlen(pause);
00392    if (restart)
00393       blen += strlen(restart);
00394 
00395    if (blen > 2) {
00396       breaks = alloca(blen + 1);
00397       breaks[0] = '\0';
00398       if (stop)
00399          strcat(breaks, stop);
00400       if (pause)
00401          strcat(breaks, pause);
00402       if (restart)
00403          strcat(breaks, restart);
00404    }
00405    if (chan->_state != AST_STATE_UP)
00406       res = ast_answer(chan);
00407 
00408    if (file) {
00409       if ((end = strchr(file,':'))) {
00410          if (!strcasecmp(end, ":end")) {
00411             *end = '\0';
00412             end++;
00413          }
00414       }
00415    }
00416 
00417    for (;;) {
00418       ast_stopstream(chan);
00419       res = ast_streamfile(chan, file, chan->language);
00420       if (!res) {
00421          if (pause_restart_point) {
00422             ast_seekstream(chan->stream, pause_restart_point, SEEK_SET);
00423             pause_restart_point = 0;
00424          }
00425          else if (end) {
00426             ast_seekstream(chan->stream, 0, SEEK_END);
00427             end = NULL;
00428          };
00429          res = ast_waitstream_fr(chan, breaks, fwd, rev, skipms);
00430       }
00431 
00432       if (res < 1)
00433          break;
00434 
00435       /* We go at next loop if we got the restart char */
00436       if (restart && strchr(restart, res)) {
00437          if (option_debug)
00438             ast_log(LOG_DEBUG, "we'll restart the stream here at next loop\n");
00439          pause_restart_point = 0;
00440          continue;
00441       }
00442 
00443       if (pause && strchr(pause, res)) {
00444          pause_restart_point = ast_tellstream(chan->stream);
00445          for (;;) {
00446             ast_stopstream(chan);
00447             res = ast_waitfordigit(chan, 1000);
00448             if (!res)
00449                continue;
00450             else if (res == -1 || strchr(pause, res) || (stop && strchr(stop, res)))
00451                break;
00452          }
00453          if (res == *pause) {
00454             res = 0;
00455             continue;
00456          }
00457       }
00458 
00459       if (res == -1)
00460          break;
00461 
00462       /* if we get one of our stop chars, return it to the calling function */
00463       if (stop && strchr(stop, res))
00464          break;
00465    }
00466 
00467    ast_stopstream(chan);
00468 
00469    return res;
00470 }

int ast_dtmf_stream ( struct ast_channel chan,
struct ast_channel peer,
const char *  digits,
int  between 
)

Send DTMF to a channel.

Parameters:
chan The channel that will receive the DTMF frames
peer (optional) Peer channel that will be autoserviced while the primary channel is receiving DTMF
digits This is a string of characters representing the DTMF digits to be sent to the channel. Valid characters are "0123456789*#abcdABCD". Note: You can pass arguments 'f' or 'F', if you want to Flash the channel (if supported by the channel), or 'w' to add a 500 millisecond pause to the DTMF sequence.
between This is the number of milliseconds to wait in between each DTMF digit. If zero milliseconds is specified, then the default value of 100 will be used.

Definition at line 229 of file app.c.

References ast_autoservice_start(), ast_autoservice_stop(), AST_CONTROL_FLASH, ast_indicate(), ast_log(), ast_safe_sleep(), ast_senddigit(), ast_waitfor(), and LOG_WARNING.

Referenced by ast_bridge_call(), misdn_send_digit(), senddtmf_exec(), test1_client(), test1_server(), testclient_exec(), and testserver_exec().

00230 {
00231    const char *ptr;
00232    int res = 0;
00233 
00234    if (!between)
00235       between = 100;
00236 
00237    if (peer)
00238       res = ast_autoservice_start(peer);
00239 
00240    if (!res)
00241       res = ast_waitfor(chan, 100);
00242 
00243    /* ast_waitfor will return the number of remaining ms on success */
00244    if (res < 0)
00245       return res;
00246 
00247    for (ptr = digits; *ptr; ptr++) {
00248       if (*ptr == 'w') {
00249          /* 'w' -- wait half a second */
00250          if ((res = ast_safe_sleep(chan, 500)))
00251             break;
00252       } else if (strchr("0123456789*#abcdfABCDF", *ptr)) {
00253          /* Character represents valid DTMF */
00254          if (*ptr == 'f' || *ptr == 'F') {
00255             /* ignore return values if not supported by channel */
00256             ast_indicate(chan, AST_CONTROL_FLASH);
00257          } else
00258             ast_senddigit(chan, *ptr);
00259          /* pause between digits */
00260          if ((res = ast_safe_sleep(chan, between)))
00261             break;
00262       } else
00263          ast_log(LOG_WARNING, "Illegal DTMF character '%c' in string. (0-9*#aAbBcCdD allowed)\n",*ptr);
00264    }
00265 
00266    if (peer) {
00267       /* Stop autoservice on the peer channel, but don't overwrite any error condition 
00268          that has occurred previously while acting on the primary channel */
00269       if (ast_autoservice_stop(peer) && !res)
00270          res = -1;
00271    }
00272 
00273    return res;
00274 }

void ast_install_vm_functions ( int(*)(const char *mailbox, const char *folder)  has_voicemail_func,
int(*)(const char *mailbox, int *newmsgs, int *oldmsgs)  inboxcount_func,
int(*)(const char *context, const char *mailbox, const char *folder)  messagecount_func 
)

Definition at line 167 of file app.c.

References ast_has_voicemail_func, ast_inboxcount_func, and ast_messagecount_func.

Referenced by load_module().

00170 {
00171    ast_has_voicemail_func = has_voicemail_func;
00172    ast_inboxcount_func = inboxcount_func;
00173    ast_messagecount_func = messagecount_func;
00174 }

int ast_ivr_menu_run ( struct ast_channel c,
struct ast_ivr_menu menu,
void *  cbdata 
)

Runs an IVR menu.

Returns:
returns 0 on successful completion, -1 on hangup, or -2 on user error in menu

Definition at line 1345 of file app.c.

References ast_ivr_menu_run_internal().

Referenced by skel_exec().

01346 {
01347    int res = ast_ivr_menu_run_internal(chan, menu, cbdata);
01348    /* Hide internal coding */
01349    return res > 0 ? 0 : res;
01350 }

static int ast_ivr_menu_run_internal ( struct ast_channel chan,
struct ast_ivr_menu menu,
void *  cbdata 
) [static]

Definition at line 1258 of file app.c.

References AST_DIGIT_ANY, ast_log(), AST_MAX_EXTENSION, exten, ivr_dispatch(), LOG_DEBUG, LOG_WARNING, maxretries, ast_ivr_option::option, option_debug, option_exists(), ast_ivr_menu::options, read_newoption(), RES_EXIT, RES_REPEAT, RES_RESTART, RES_UPONE, and ast_ivr_menu::title.

Referenced by ast_ivr_menu_run(), and ivr_dispatch().

01259 {
01260    /* Execute an IVR menu structure */
01261    int res=0;
01262    int pos = 0;
01263    int retries = 0;
01264    char exten[AST_MAX_EXTENSION] = "s";
01265    if (option_exists(menu, "s") < 0) {
01266       strcpy(exten, "g");
01267       if (option_exists(menu, "g") < 0) {
01268          ast_log(LOG_WARNING, "No 's' nor 'g' extension in menu '%s'!\n", menu->title);
01269          return -1;
01270       }
01271    }
01272    while(!res) {
01273       while(menu->options[pos].option) {
01274          if (!strcasecmp(menu->options[pos].option, exten)) {
01275             res = ivr_dispatch(chan, menu->options + pos, exten, cbdata);
01276             if (option_debug)
01277                ast_log(LOG_DEBUG, "IVR Dispatch of '%s' (pos %d) yields %d\n", exten, pos, res);
01278             if (res < 0)
01279                break;
01280             else if (res & RES_UPONE)
01281                return 0;
01282             else if (res & RES_EXIT)
01283                return res;
01284             else if (res & RES_REPEAT) {
01285                int maxretries = res & 0xffff;
01286                if ((res & RES_RESTART) == RES_RESTART) {
01287                   retries = 0;
01288                } else
01289                   retries++;
01290                if (!maxretries)
01291                   maxretries = 3;
01292                if ((maxretries > 0) && (retries >= maxretries)) {
01293                   if (option_debug)
01294                      ast_log(LOG_DEBUG, "Max retries %d exceeded\n", maxretries);
01295                   return -2;
01296                } else {
01297                   if (option_exists(menu, "g") > -1) 
01298                      strcpy(exten, "g");
01299                   else if (option_exists(menu, "s") > -1)
01300                      strcpy(exten, "s");
01301                }
01302                pos = 0;
01303                continue;
01304             } else if (res && strchr(AST_DIGIT_ANY, res)) {
01305                if (option_debug)
01306                   ast_log(LOG_DEBUG, "Got start of extension, %c\n", res);
01307                exten[1] = '\0';
01308                exten[0] = res;
01309                if ((res = read_newoption(chan, menu, exten, sizeof(exten))))
01310                   break;
01311                if (option_exists(menu, exten) < 0) {
01312                   if (option_exists(menu, "i")) {
01313                      if (option_debug)
01314                         ast_log(LOG_DEBUG, "Invalid extension entered, going to 'i'!\n");
01315                      strcpy(exten, "i");
01316                      pos = 0;
01317                      continue;
01318                   } else {
01319                      if (option_debug)
01320                         ast_log(LOG_DEBUG, "Aborting on invalid entry, with no 'i' option!\n");
01321                      res = -2;
01322                      break;
01323                   }
01324                } else {
01325                   if (option_debug)
01326                      ast_log(LOG_DEBUG, "New existing extension: %s\n", exten);
01327                   pos = 0;
01328                   continue;
01329                }
01330             }
01331          }
01332          pos++;
01333       }
01334       if (option_debug)
01335          ast_log(LOG_DEBUG, "Stopping option '%s', res is %d\n", exten, res);
01336       pos = 0;
01337       if (!strcasecmp(exten, "s"))
01338          strcpy(exten, "g");
01339       else
01340          break;
01341    }
01342    return res;
01343 }

int ast_linear_stream ( struct ast_channel chan,
const char *  filename,
int  fd,
int  allowoverride 
)

Stream a filename (or file descriptor) as a generator.

Definition at line 348 of file app.c.

References ast_activate_generator(), ast_calloc, ast_config_AST_DATA_DIR, ast_log(), ast_strlen_zero(), linear_state::autoclose, linearstream, and LOG_WARNING.

00349 {
00350    struct linear_state *lin;
00351    char tmpf[256];
00352    int res = -1;
00353    int autoclose = 0;
00354    if (fd < 0) {
00355       if (ast_strlen_zero(filename))
00356          return -1;
00357       autoclose = 1;
00358       if (filename[0] == '/') 
00359          ast_copy_string(tmpf, filename, sizeof(tmpf));
00360       else
00361          snprintf(tmpf, sizeof(tmpf), "%s/%s/%s", (char *)ast_config_AST_DATA_DIR, "sounds", filename);
00362       fd = open(tmpf, O_RDONLY);
00363       if (fd < 0){
00364          ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", tmpf, strerror(errno));
00365          return -1;
00366       }
00367    }
00368    if ((lin = ast_calloc(1, sizeof(*lin)))) {
00369       lin->fd = fd;
00370       lin->allowoverride = allowoverride;
00371       lin->autoclose = autoclose;
00372       res = ast_activate_generator(chan, &linearstream, lin);
00373    }
00374    return res;
00375 }

static AST_LIST_HEAD_STATIC ( groups  ,
ast_group_info   
) [static]

enum AST_LOCK_RESULT ast_lock_path ( const char *  path  ) 

Lock a filesystem path.

Parameters:
path the path to be locked
Returns:
one of AST_LOCK_RESULT values

Definition at line 990 of file app.c.

References AST_LOCK_FAILURE, AST_LOCK_PATH_NOT_FOUND, AST_LOCK_SUCCESS, AST_LOCK_TIMEOUT, ast_log(), ast_random(), LOG_DEBUG, LOG_ERROR, LOG_WARNING, option_debug, and s.

Referenced by vm_lock_path().

00991 {
00992    char *s;
00993    char *fs;
00994    int res;
00995    int fd;
00996    int lp = strlen(path);
00997    time_t start;
00998 
00999    if (!(s = alloca(lp + 10)) || !(fs = alloca(lp + 20))) {
01000       ast_log(LOG_WARNING, "Out of memory!\n");
01001       return AST_LOCK_FAILURE;
01002    }
01003 
01004    snprintf(fs, strlen(path) + 19, "%s/.lock-%08lx", path, ast_random());
01005    fd = open(fs, O_WRONLY | O_CREAT | O_EXCL, 0600);
01006    if (fd < 0) {
01007       ast_log(LOG_ERROR, "Unable to create lock file '%s': %s\n", path, strerror(errno));
01008       return AST_LOCK_PATH_NOT_FOUND;
01009    }
01010    close(fd);
01011 
01012    snprintf(s, strlen(path) + 9, "%s/.lock", path);
01013    start = time(NULL);
01014    while (((res = link(fs, s)) < 0) && (errno == EEXIST) && (time(NULL) - start < 5))
01015       usleep(1);
01016 
01017    unlink(fs);
01018 
01019    if (res) {
01020       ast_log(LOG_WARNING, "Failed to lock path '%s': %s\n", path, strerror(errno));
01021       return AST_LOCK_TIMEOUT;
01022    } else {
01023       if (option_debug)
01024          ast_log(LOG_DEBUG, "Locked path '%s'\n", path);
01025       return AST_LOCK_SUCCESS;
01026    }
01027 }

int ast_play_and_prepend ( struct ast_channel chan,
char *  playfile,
char *  recordfile,
int  maxtime_sec,
char *  fmt,
int *  duration,
int  beep,
int  silencethreshold,
int  maxsilence_ms 
)

Record a message and prepend the message to the given record file after playing the optional playfile (or a beep), storing the duration in 'duration' and with a maximum
permitted silence time in milliseconds of 'maxsilence' under 'silencethreshold' or use '-1' for either or both parameters for defaults.

Definition at line 782 of file app.c.

References __ast_play_and_record().

Referenced by vm_forwardoptions().

00783 {
00784    return __ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, beep, silencethreshold, maxsilence, NULL, 1, default_acceptdtmf, default_canceldtmf);
00785 }

int ast_play_and_record ( struct ast_channel chan,
const char *  playfile,
const char *  recordfile,
int  maxtime_sec,
const char *  fmt,
int *  duration,
int  silencethreshold,
int  maxsilence_ms,
const char *  path 
)

Record a file for a max amount of time (in seconds), in a given list of formats separated by '|', outputting the duration of the recording, and with a maximum
permitted silence time in milliseconds of 'maxsilence' under 'silencethreshold' or use '-1' for either or both parameters for defaults. calls ast_unlock_path() on 'path' if passed

Definition at line 777 of file app.c.

References __ast_play_and_record().

Referenced by app_exec(), ast_record_review(), and conf_run().

00778 {
00779    return __ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, 0, silencethreshold, maxsilence, path, 0, default_acceptdtmf, default_canceldtmf);
00780 }

int ast_play_and_record_full ( struct ast_channel chan,
const char *  playfile,
const char *  recordfile,
int  maxtime,
const char *  fmt,
int *  duration,
int  silencethreshold,
int  maxsilence,
const char *  path,
const char *  acceptdtmf,
const char *  canceldtmf 
)

Definition at line 772 of file app.c.

References __ast_play_and_record(), and S_OR.

Referenced by play_record_review().

00773 {
00774    return __ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, 0, silencethreshold, maxsilence, path, 0, S_OR(acceptdtmf, default_acceptdtmf), S_OR(canceldtmf, default_canceldtmf));
00775 }

int ast_play_and_wait ( struct ast_channel chan,
const char *  fn 
)

Play a stream and wait for a digit, returning the digit that was pressed

Definition at line 472 of file app.c.

References AST_DIGIT_ANY, ast_stopstream(), ast_streamfile(), and ast_waitstream().

Referenced by __ast_play_and_record(), advanced_options(), ast_record_review(), dialout(), forward_message(), get_folder(), get_folder2(), leave_voicemail(), play_message_category(), play_message_duration(), play_record_review(), vm_authenticate(), vm_browse_messages_en(), vm_browse_messages_es(), vm_browse_messages_gr(), vm_browse_messages_it(), vm_browse_messages_pt(), vm_execmain(), vm_forwardoptions(), vm_instructions(), vm_intro(), vm_intro_cz(), vm_intro_de(), vm_intro_en(), vm_intro_es(), vm_intro_fr(), vm_intro_gr(), vm_intro_it(), vm_intro_nl(), vm_intro_no(), vm_intro_pl(), vm_intro_pt(), vm_intro_pt_BR(), vm_intro_ru(), vm_intro_se(), vm_intro_ua(), vm_newuser(), vm_options(), vm_play_folder_name(), vm_play_folder_name_gr(), vm_play_folder_name_pl(), vm_play_folder_name_ua(), vm_tempgreeting(), and vmauthenticate().

00473 {
00474    int d;
00475    d = ast_streamfile(chan, fn, chan->language);
00476    if (d)
00477       return d;
00478    d = ast_waitstream(chan, AST_DIGIT_ANY);
00479    ast_stopstream(chan);
00480    return d;
00481 }

char* ast_read_textfile ( const char *  file  ) 

Read a file into asterisk

Definition at line 1352 of file app.c.

References ast_log(), ast_malloc, free, and LOG_WARNING.

Referenced by readfile_exec().

01353 {
01354    int fd;
01355    char *output = NULL;
01356    struct stat filesize;
01357    int count = 0;
01358    int res;
01359    if (stat(filename, &filesize) == -1) {
01360       ast_log(LOG_WARNING, "Error can't stat %s\n", filename);
01361       return NULL;
01362    }
01363    count = filesize.st_size + 1;
01364    fd = open(filename, O_RDONLY);
01365    if (fd < 0) {
01366       ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", filename, strerror(errno));
01367       return NULL;
01368    }
01369    if ((output = ast_malloc(count))) {
01370       res = read(fd, output, count - 1);
01371       if (res == count - 1) {
01372          output[res] = '\0';
01373       } else {
01374          ast_log(LOG_WARNING, "Short read of %s (%d of %d): %s\n", filename, res, count - 1, strerror(errno));
01375          free(output);
01376          output = NULL;
01377       }
01378    }
01379    close(fd);
01380    return output;
01381 }

int ast_record_review ( struct ast_channel chan,
const char *  playfile,
const char *  recordfile,
int  maxtime,
const char *  fmt,
int *  duration,
const char *  path 
)

Allow to record message and have a review option

Definition at line 1051 of file app.c.

References AST_DIGIT_ANY, ast_log(), ast_play_and_record(), ast_play_and_wait(), ast_stream_and_wait(), ast_verbose(), ast_waitfordigit(), ast_group_info::chan, LOG_WARNING, maxsilence, silencethreshold, and VERBOSE_PREFIX_3.

Referenced by conf_run().

01052 {
01053    int silencethreshold = 128; 
01054    int maxsilence=0;
01055    int res = 0;
01056    int cmd = 0;
01057    int max_attempts = 3;
01058    int attempts = 0;
01059    int recorded = 0;
01060    int message_exists = 0;
01061    /* Note that urgent and private are for flagging messages as such in the future */
01062 
01063    /* barf if no pointer passed to store duration in */
01064    if (duration == NULL) {
01065       ast_log(LOG_WARNING, "Error ast_record_review called without duration pointer\n");
01066       return -1;
01067    }
01068 
01069    cmd = '3';   /* Want to start by recording */
01070 
01071    while ((cmd >= 0) && (cmd != 't')) {
01072       switch (cmd) {
01073       case '1':
01074          if (!message_exists) {
01075             /* In this case, 1 is to record a message */
01076             cmd = '3';
01077             break;
01078          } else {
01079             ast_stream_and_wait(chan, "vm-msgsaved", chan->language, "");
01080             cmd = 't';
01081             return res;
01082          }
01083       case '2':
01084          /* Review */
01085          ast_verbose(VERBOSE_PREFIX_3 "Reviewing the recording\n");
01086          cmd = ast_stream_and_wait(chan, recordfile, chan->language, AST_DIGIT_ANY);
01087          break;
01088       case '3':
01089          message_exists = 0;
01090          /* Record */
01091          if (recorded == 1)
01092             ast_verbose(VERBOSE_PREFIX_3 "Re-recording\n");
01093          else  
01094             ast_verbose(VERBOSE_PREFIX_3 "Recording\n");
01095          recorded = 1;
01096          cmd = ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, silencethreshold, maxsilence, path);
01097          if (cmd == -1) {
01098          /* User has hung up, no options to give */
01099             return cmd;
01100          }
01101          if (cmd == '0') {
01102             break;
01103          } else if (cmd == '*') {
01104             break;
01105          } 
01106          else {
01107             /* If all is well, a message exists */
01108             message_exists = 1;
01109             cmd = 0;
01110          }
01111          break;
01112       case '4':
01113       case '5':
01114       case '6':
01115       case '7':
01116       case '8':
01117       case '9':
01118       case '*':
01119       case '#':
01120          cmd = ast_play_and_wait(chan, "vm-sorry");
01121          break;
01122       default:
01123          if (message_exists) {
01124             cmd = ast_play_and_wait(chan, "vm-review");
01125          }
01126          else {
01127             cmd = ast_play_and_wait(chan, "vm-torerecord");
01128             if (!cmd)
01129                cmd = ast_waitfordigit(chan, 600);
01130          }
01131          
01132          if (!cmd)
01133             cmd = ast_waitfordigit(chan, 6000);
01134          if (!cmd) {
01135             attempts++;
01136          }
01137          if (attempts > max_attempts) {
01138             cmd = 't';
01139          }
01140       }
01141    }
01142    if (cmd == 't')
01143       cmd = 0;
01144    return cmd;
01145 }

void ast_uninstall_vm_functions ( void   ) 

Definition at line 176 of file app.c.

References ast_has_voicemail_func, ast_inboxcount_func, and ast_messagecount_func.

Referenced by unload_module().

00177 {
00178    ast_has_voicemail_func = NULL;
00179    ast_inboxcount_func = NULL;
00180    ast_messagecount_func = NULL;
00181 }

int ast_unlock_path ( const char *  path  ) 

Unlock a path

Definition at line 1029 of file app.c.

References ast_log(), LOG_DEBUG, LOG_ERROR, LOG_WARNING, option_debug, and s.

Referenced by __ast_play_and_record(), copy_message(), count_messages(), last_message_index(), leave_voicemail(), resequence_mailbox(), and save_to_folder().

01030 {
01031    char *s;
01032    int res;
01033 
01034    if (!(s = alloca(strlen(path) + 10))) {
01035       ast_log(LOG_WARNING, "Out of memory!\n");
01036       return -1;
01037    }
01038 
01039    snprintf(s, strlen(path) + 9, "%s/%s", path, ".lock");
01040 
01041    if ((res = unlink(s)))
01042       ast_log(LOG_ERROR, "Could not unlock path '%s': %s\n", path, strerror(errno));
01043    else {
01044       if (option_debug)
01045          ast_log(LOG_DEBUG, "Unlocked path '%s'\n", path);
01046    }
01047 
01048    return res;
01049 }

static int ivr_dispatch ( struct ast_channel chan,
struct ast_ivr_option option,
char *  exten,
void *  cbdata 
) [static]

Definition at line 1154 of file app.c.

References ast_ivr_option::action, ast_ivr_option::adata, AST_ACTION_BACKGROUND, AST_ACTION_BACKLIST, AST_ACTION_CALLBACK, AST_ACTION_EXIT, AST_ACTION_MENU, AST_ACTION_NOOP, AST_ACTION_PLAYBACK, AST_ACTION_PLAYLIST, AST_ACTION_REPEAT, AST_ACTION_RESTART, AST_ACTION_TRANSFER, AST_ACTION_UPONE, AST_ACTION_WAITOPTION, AST_DIGIT_ANY, ast_ivr_menu_run_internal(), ast_log(), ast_parseable_goto(), ast_stopstream(), ast_strdupa, ast_stream_and_wait(), ast_waitfordigit(), LOG_NOTICE, ast_channel::pbx, RES_EXIT, RES_REPEAT, RES_RESTART, RES_UPONE, ast_pbx::rtimeout, and strsep().

Referenced by ast_ivr_menu_run_internal().

01155 {
01156    int res;
01157    int (*ivr_func)(struct ast_channel *, void *);
01158    char *c;
01159    char *n;
01160    
01161    switch(option->action) {
01162    case AST_ACTION_UPONE:
01163       return RES_UPONE;
01164    case AST_ACTION_EXIT:
01165       return RES_EXIT | (((unsigned long)(option->adata)) & 0xffff);
01166    case AST_ACTION_REPEAT:
01167       return RES_REPEAT | (((unsigned long)(option->adata)) & 0xffff);
01168    case AST_ACTION_RESTART:
01169       return RES_RESTART ;
01170    case AST_ACTION_NOOP:
01171       return 0;
01172    case AST_ACTION_BACKGROUND:
01173       res = ast_stream_and_wait(chan, (char *)option->adata, chan->language, AST_DIGIT_ANY);
01174       if (res < 0) {
01175          ast_log(LOG_NOTICE, "Unable to find file '%s'!\n", (char *)option->adata);
01176          res = 0;
01177       }
01178       return res;
01179    case AST_ACTION_PLAYBACK:
01180       res = ast_stream_and_wait(chan, (char *)option->adata, chan->language, "");
01181       if (res < 0) {
01182          ast_log(LOG_NOTICE, "Unable to find file '%s'!\n", (char *)option->adata);
01183          res = 0;
01184       }
01185       return res;
01186    case AST_ACTION_MENU:
01187       res = ast_ivr_menu_run_internal(chan, (struct ast_ivr_menu *)option->adata, cbdata);
01188       /* Do not pass entry errors back up, treaat ast though ti was an "UPONE" */
01189       if (res == -2)
01190          res = 0;
01191       return res;
01192    case AST_ACTION_WAITOPTION:
01193       res = ast_waitfordigit(chan, 1000 * (chan->pbx ? chan->pbx->rtimeout : 10));
01194       if (!res)
01195          return 't';
01196       return res;
01197    case AST_ACTION_CALLBACK:
01198       ivr_func = option->adata;
01199       res = ivr_func(chan, cbdata);
01200       return res;
01201    case AST_ACTION_TRANSFER:
01202       res = ast_parseable_goto(chan, option->adata);
01203       return 0;
01204    case AST_ACTION_PLAYLIST:
01205    case AST_ACTION_BACKLIST:
01206       res = 0;
01207       c = ast_strdupa(option->adata);
01208       while ((n = strsep(&c, ";"))) {
01209          if ((res = ast_stream_and_wait(chan, n, chan->language,
01210                (option->action == AST_ACTION_BACKLIST) ? AST_DIGIT_ANY : "")))
01211             break;
01212       }
01213       ast_stopstream(chan);
01214       return res;
01215    default:
01216       ast_log(LOG_NOTICE, "Unknown dispatch function %d, ignoring!\n", option->action);
01217       return 0;
01218    };
01219    return -1;
01220 }

static void* linear_alloc ( struct ast_channel chan,
void *  params 
) [static]

Definition at line 321 of file app.c.

References linear_state::allowoverride, ast_clear_flag, AST_FLAG_WRITE_INT, AST_FORMAT_SLINEAR, ast_log(), ast_set_flag, ast_set_write_format(), free, LOG_WARNING, linear_state::origwfmt, and ast_channel::writeformat.

00322 {
00323    struct linear_state *ls;
00324    /* In this case, params is already malloc'd */
00325    if (params) {
00326       ls = params;
00327       if (ls->allowoverride)
00328          ast_set_flag(chan, AST_FLAG_WRITE_INT);
00329       else
00330          ast_clear_flag(chan, AST_FLAG_WRITE_INT);
00331       ls->origwfmt = chan->writeformat;
00332       if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
00333          ast_log(LOG_WARNING, "Unable to set '%s' to linear format (write)\n", chan->name);
00334          free(ls);
00335          ls = params = NULL;
00336       }
00337    }
00338    return params;
00339 }

static int linear_generator ( struct ast_channel chan,
void *  data,
int  len,
int  samples 
) [static]

Definition at line 294 of file app.c.

References AST_FORMAT_SLINEAR, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), ast_write(), f, linear_state::fd, and LOG_WARNING.

00295 {
00296    struct ast_frame f;
00297    short buf[2048 + AST_FRIENDLY_OFFSET / 2];
00298    struct linear_state *ls = data;
00299    int res;
00300    len = samples * 2;
00301    if (len > sizeof(buf) - AST_FRIENDLY_OFFSET) {
00302       ast_log(LOG_WARNING, "Can't generate %d bytes of data!\n" ,len);
00303       len = sizeof(buf) - AST_FRIENDLY_OFFSET;
00304    }
00305    memset(&f, 0, sizeof(f));
00306    res = read(ls->fd, buf + AST_FRIENDLY_OFFSET/2, len);
00307    if (res > 0) {
00308       f.frametype = AST_FRAME_VOICE;
00309       f.subclass = AST_FORMAT_SLINEAR;
00310       f.data = buf + AST_FRIENDLY_OFFSET/2;
00311       f.datalen = res;
00312       f.samples = res / 2;
00313       f.offset = AST_FRIENDLY_OFFSET;
00314       ast_write(chan, &f);
00315       if (res == len)
00316          return 0;
00317    }
00318    return -1;
00319 }

static void linear_release ( struct ast_channel chan,
void *  params 
) [static]

Definition at line 283 of file app.c.

References ast_log(), ast_set_write_format(), linear_state::autoclose, linear_state::fd, free, LOG_WARNING, and linear_state::origwfmt.

00284 {
00285    struct linear_state *ls = params;
00286    if (ls->origwfmt && ast_set_write_format(chan, ls->origwfmt)) {
00287       ast_log(LOG_WARNING, "Unable to restore channel '%s' to format '%d'\n", chan->name, ls->origwfmt);
00288    }
00289    if (ls->autoclose)
00290       close(ls->fd);
00291    free(params);
00292 }

static int option_exists ( struct ast_ivr_menu menu,
char *  option 
) [static]

Definition at line 1222 of file app.c.

References ast_ivr_option::option, and ast_ivr_menu::options.

Referenced by ast_ivr_menu_run_internal().

01223 {
01224    int x;
01225    for (x = 0; menu->options[x].option; x++)
01226       if (!strcasecmp(menu->options[x].option, option))
01227          return x;
01228    return -1;
01229 }

static int option_matchmore ( struct ast_ivr_menu menu,
char *  option 
) [static]

Definition at line 1231 of file app.c.

References ast_ivr_option::option, and ast_ivr_menu::options.

Referenced by read_newoption().

01232 {
01233    int x;
01234    for (x = 0; menu->options[x].option; x++)
01235       if ((!strncasecmp(menu->options[x].option, option, strlen(option))) && 
01236             (menu->options[x].option[strlen(option)]))
01237          return x;
01238    return -1;
01239 }

static int read_newoption ( struct ast_channel chan,
struct ast_ivr_menu menu,
char *  exten,
int  maxexten 
) [static]

Definition at line 1241 of file app.c.

References ast_waitfordigit(), ast_pbx::dtimeout, option_matchmore(), and ast_channel::pbx.

Referenced by ast_ivr_menu_run_internal().

01242 {
01243    int res=0;
01244    int ms;
01245    while (option_matchmore(menu, exten)) {
01246       ms = chan->pbx ? chan->pbx->dtimeout : 5000;
01247       if (strlen(exten) >= maxexten - 1) 
01248          break;
01249       res = ast_waitfordigit(chan, ms);
01250       if (res < 1)
01251          break;
01252       exten[strlen(exten) + 1] = '\0';
01253       exten[strlen(exten)] = res;
01254    }
01255    return res > 0 ? 0 : res;
01256 }


Variable Documentation

int(*) ast_has_voicemail_func(const char *mailbox, const char *folder) = NULL [static]

Definition at line 163 of file app.c.

Referenced by ast_app_has_voicemail(), ast_install_vm_functions(), and ast_uninstall_vm_functions().

int(*) ast_inboxcount_func(const char *mailbox, int *newmsgs, int *oldmsgs) = NULL [static]

Definition at line 164 of file app.c.

Referenced by ast_app_inboxcount(), ast_install_vm_functions(), and ast_uninstall_vm_functions().

int(*) ast_messagecount_func(const char *context, const char *mailbox, const char *folder) = NULL [static]

Definition at line 165 of file app.c.

Referenced by ast_app_messagecount(), ast_install_vm_functions(), and ast_uninstall_vm_functions().

char default_acceptdtmf[] = "#" [static]

Definition at line 769 of file app.c.

char default_canceldtmf[] = "" [static]

Definition at line 770 of file app.c.

int global_maxsilence = 0 [static]

Definition at line 484 of file app.c.

int global_silence_threshold = 128 [static]

Definition at line 483 of file app.c.

struct ast_generator linearstream [static]

Definition at line 341 of file app.c.

Referenced by ast_linear_stream().


Generated on Fri Aug 24 02:22:23 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1