Wed Aug 15 01:24:13 2007

Asterisk developer's documentation


app.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Convenient Application Routines
00022  *
00023  * \author Mark Spencer <markster@digium.com> 
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 76891 $")
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <sys/time.h>
00034 #include <signal.h>
00035 #include <errno.h>
00036 #include <unistd.h>
00037 #include <dirent.h>
00038 #include <sys/types.h>
00039 #include <sys/stat.h>
00040 #include <regex.h>
00041 
00042 #include "asterisk/channel.h"
00043 #include "asterisk/pbx.h"
00044 #include "asterisk/file.h"
00045 #include "asterisk/app.h"
00046 #include "asterisk/dsp.h"
00047 #include "asterisk/logger.h"
00048 #include "asterisk/options.h"
00049 #include "asterisk/utils.h"
00050 #include "asterisk/lock.h"
00051 #include "asterisk/indications.h"
00052 #include "asterisk/linkedlists.h"
00053 
00054 #define MAX_OTHER_FORMATS 10
00055 
00056 static AST_LIST_HEAD_STATIC(groups, ast_group_info);
00057 
00058 /* !
00059 This function presents a dialtone and reads an extension into 'collect' 
00060 which must be a pointer to a **pre-initialized** array of char having a 
00061 size of 'size' suitable for writing to.  It will collect no more than the smaller 
00062 of 'maxlen' or 'size' minus the original strlen() of collect digits.
00063 \return 0 if extension does not exist, 1 if extension exists
00064 */
00065 int ast_app_dtget(struct ast_channel *chan, const char *context, char *collect, size_t size, int maxlen, int timeout) 
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 }
00100 
00101 /*! \param c The channel to read from
00102  *  \param prompt The file to stream to the channel
00103  *  \param s The string to read in to.  Must be at least the size of your length
00104  *  \param maxlen How many digits to read (maximum)
00105  *  \param timeout set timeout to 0 for "standard" timeouts. Set timeout to -1 for 
00106  *      "ludicrous time" (essentially never times out) */
00107 int ast_app_getdata(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout)
00108 {
00109    int res,to,fto;
00110    /* XXX Merge with full version? XXX */
00111    if (maxlen)
00112       s[0] = '\0';
00113    if (prompt) {
00114       res = ast_streamfile(c, prompt, c->language);
00115       if (res < 0)
00116          return res;
00117    }
00118    fto = c->pbx ? c->pbx->rtimeout * 1000 : 6000;
00119    to = c->pbx ? c->pbx->dtimeout * 1000 : 2000;
00120 
00121    if (timeout > 0) 
00122       fto = to = timeout;
00123    if (timeout < 0) 
00124       fto = to = 1000000000;
00125    res = ast_readstring(c, s, maxlen, to, fto, "#");
00126    return res;
00127 }
00128 
00129 
00130 int ast_app_getdata_full(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout, int audiofd, int ctrlfd)
00131 {
00132    int res, to, fto;
00133    if (prompt) {
00134       res = ast_streamfile(c, prompt, c->language);
00135       if (res < 0)
00136          return res;
00137    }
00138    fto = 6000;
00139    to = 2000;
00140    if (timeout > 0) 
00141       fto = to = timeout;
00142    if (timeout < 0) 
00143       fto = to = 1000000000;
00144    res = ast_readstring_full(c, s, maxlen, to, fto, "#", audiofd, ctrlfd);
00145    return res;
00146 }
00147 
00148 static int (*ast_has_voicemail_func)(const char *mailbox, const char *folder) = NULL;
00149 static int (*ast_inboxcount_func)(const char *mailbox, int *newmsgs, int *oldmsgs) = NULL;
00150 static int (*ast_messagecount_func)(const char *context, const char *mailbox, const char *folder) = NULL;
00151 
00152 void ast_install_vm_functions(int (*has_voicemail_func)(const char *mailbox, const char *folder),
00153                int (*inboxcount_func)(const char *mailbox, int *newmsgs, int *oldmsgs),
00154                int (*messagecount_func)(const char *context, const char *mailbox, const char *folder))
00155 {
00156    ast_has_voicemail_func = has_voicemail_func;
00157    ast_inboxcount_func = inboxcount_func;
00158    ast_messagecount_func = messagecount_func;
00159 }
00160 
00161 void ast_uninstall_vm_functions(void)
00162 {
00163    ast_has_voicemail_func = NULL;
00164    ast_inboxcount_func = NULL;
00165    ast_messagecount_func = NULL;
00166 }
00167 
00168 int ast_app_has_voicemail(const char *mailbox, const char *folder)
00169 {
00170    static int warned = 0;
00171    if (ast_has_voicemail_func)
00172       return ast_has_voicemail_func(mailbox, folder);
00173 
00174    if ((option_verbose > 2) && !warned) {
00175       ast_verbose(VERBOSE_PREFIX_3 "Message check requested for mailbox %s/folder %s but voicemail not loaded.\n", mailbox, folder ? folder : "INBOX");
00176       warned++;
00177    }
00178    return 0;
00179 }
00180 
00181 
00182 int ast_app_inboxcount(const char *mailbox, int *newmsgs, int *oldmsgs)
00183 {
00184    static int warned = 0;
00185    if (newmsgs)
00186       *newmsgs = 0;
00187    if (oldmsgs)
00188       *oldmsgs = 0;
00189    if (ast_inboxcount_func)
00190       return ast_inboxcount_func(mailbox, newmsgs, oldmsgs);
00191 
00192    if (!warned && (option_verbose > 2)) {
00193       warned++;
00194       ast_verbose(VERBOSE_PREFIX_3 "Message count requested for mailbox %s but voicemail not loaded.\n", mailbox);
00195    }
00196 
00197    return 0;
00198 }
00199 
00200 int ast_app_messagecount(const char *context, const char *mailbox, const char *folder)
00201 {
00202    static int warned = 0;
00203    if (ast_messagecount_func)
00204       return ast_messagecount_func(context, mailbox, folder);
00205 
00206    if (!warned && (option_verbose > 2)) {
00207       warned++;
00208       ast_verbose(VERBOSE_PREFIX_3 "Message count requested for mailbox %s@%s/%s but voicemail not loaded.\n", mailbox, context, folder);
00209    }
00210 
00211    return 0;
00212 }
00213 
00214 int ast_dtmf_stream(struct ast_channel *chan, struct ast_channel *peer, const char *digits, int between) 
00215 {
00216    const char *ptr;
00217    int res = 0;
00218 
00219    if (!between)
00220       between = 100;
00221 
00222    if (peer)
00223       res = ast_autoservice_start(peer);
00224 
00225    if (!res)
00226       res = ast_waitfor(chan, 100);
00227 
00228    /* ast_waitfor will return the number of remaining ms on success */
00229    if (res < 0)
00230       return res;
00231 
00232    for (ptr = digits; *ptr; ptr++) {
00233       if (*ptr == 'w') {
00234          /* 'w' -- wait half a second */
00235          if ((res = ast_safe_sleep(chan, 500)))
00236             break;
00237       } else if (strchr("0123456789*#abcdfABCDF", *ptr)) {
00238          /* Character represents valid DTMF */
00239          if (*ptr == 'f' || *ptr == 'F') {
00240             /* ignore return values if not supported by channel */
00241             ast_indicate(chan, AST_CONTROL_FLASH);
00242          } else
00243             ast_senddigit(chan, *ptr);
00244          /* pause between digits */
00245          if ((res = ast_safe_sleep(chan, between)))
00246             break;
00247       } else
00248          ast_log(LOG_WARNING, "Illegal DTMF character '%c' in string. (0-9*#aAbBcCdD allowed)\n",*ptr);
00249    }
00250 
00251    if (peer) {
00252       /* Stop autoservice on the peer channel, but don't overwrite any error condition 
00253          that has occurred previously while acting on the primary channel */
00254       if (ast_autoservice_stop(peer) && !res)
00255          res = -1;
00256    }
00257 
00258    return res;
00259 }
00260 
00261 struct linear_state {
00262    int fd;
00263    int autoclose;
00264    int allowoverride;
00265    int origwfmt;
00266 };
00267 
00268 static void linear_release(struct ast_channel *chan, void *params)
00269 {
00270    struct linear_state *ls = params;
00271    if (ls->origwfmt && ast_set_write_format(chan, ls->origwfmt)) {
00272       ast_log(LOG_WARNING, "Unable to restore channel '%s' to format '%d'\n", chan->name, ls->origwfmt);
00273    }
00274    if (ls->autoclose)
00275       close(ls->fd);
00276    free(params);
00277 }
00278 
00279 static int linear_generator(struct ast_channel *chan, void *data, int len, int samples)
00280 {
00281    struct ast_frame f;
00282    short buf[2048 + AST_FRIENDLY_OFFSET / 2];
00283    struct linear_state *ls = data;
00284    int res;
00285    len = samples * 2;
00286    if (len > sizeof(buf) - AST_FRIENDLY_OFFSET) {
00287       ast_log(LOG_WARNING, "Can't generate %d bytes of data!\n" ,len);
00288       len = sizeof(buf) - AST_FRIENDLY_OFFSET;
00289    }
00290    memset(&f, 0, sizeof(f));
00291    res = read(ls->fd, buf + AST_FRIENDLY_OFFSET/2, len);
00292    if (res > 0) {
00293       f.frametype = AST_FRAME_VOICE;
00294       f.subclass = AST_FORMAT_SLINEAR;
00295       f.data = buf + AST_FRIENDLY_OFFSET/2;
00296       f.datalen = res;
00297       f.samples = res / 2;
00298       f.offset = AST_FRIENDLY_OFFSET;
00299       ast_write(chan, &f);
00300       if (res == len)
00301          return 0;
00302    }
00303    return -1;
00304 }
00305 
00306 static void *linear_alloc(struct ast_channel *chan, void *params)
00307 {
00308    struct linear_state *ls;
00309    /* In this case, params is already malloc'd */
00310    if (params) {
00311       ls = params;
00312       if (ls->allowoverride)
00313          ast_set_flag(chan, AST_FLAG_WRITE_INT);
00314       else
00315          ast_clear_flag(chan, AST_FLAG_WRITE_INT);
00316       ls->origwfmt = chan->writeformat;
00317       if (ast_set_write_format(chan, AST_FORMAT_SLINEAR)) {
00318          ast_log(LOG_WARNING, "Unable to set '%s' to linear format (write)\n", chan->name);
00319          free(ls);
00320          ls = params = NULL;
00321       }
00322    }
00323    return params;
00324 }
00325 
00326 static struct ast_generator linearstream = 
00327 {
00328    alloc: linear_alloc,
00329    release: linear_release,
00330    generate: linear_generator,
00331 };
00332 
00333 int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, int allowoverride)
00334 {
00335    struct linear_state *lin;
00336    char tmpf[256];
00337    int res = -1;
00338    int autoclose = 0;
00339    if (fd < 0) {
00340       if (ast_strlen_zero(filename))
00341          return -1;
00342       autoclose = 1;
00343       if (filename[0] == '/') 
00344          ast_copy_string(tmpf, filename, sizeof(tmpf));
00345       else
00346          snprintf(tmpf, sizeof(tmpf), "%s/%s/%s", (char *)ast_config_AST_DATA_DIR, "sounds", filename);
00347       fd = open(tmpf, O_RDONLY);
00348       if (fd < 0){
00349          ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", tmpf, strerror(errno));
00350          return -1;
00351       }
00352    }
00353    if ((lin = ast_calloc(1, sizeof(*lin)))) {
00354       lin->fd = fd;
00355       lin->allowoverride = allowoverride;
00356       lin->autoclose = autoclose;
00357       res = ast_activate_generator(chan, &linearstream, lin);
00358    }
00359    return res;
00360 }
00361 
00362 int ast_control_streamfile(struct ast_channel *chan, const char *file,
00363             const char *fwd, const char *rev,
00364             const char *stop, const char *pause,
00365             const char *restart, int skipms) 
00366 {
00367    char *breaks = NULL;
00368    char *end = NULL;
00369    int blen = 2;
00370    int res;
00371    long pause_restart_point = 0;
00372 
00373    if (stop)
00374       blen += strlen(stop);
00375    if (pause)
00376       blen += strlen(pause);
00377    if (restart)
00378       blen += strlen(restart);
00379 
00380    if (blen > 2) {
00381       breaks = alloca(blen + 1);
00382       breaks[0] = '\0';
00383       if (stop)
00384          strcat(breaks, stop);
00385       if (pause)
00386          strcat(breaks, pause);
00387       if (restart)
00388          strcat(breaks, restart);
00389    }
00390    if (chan->_state != AST_STATE_UP)
00391       res = ast_answer(chan);
00392 
00393    if (file) {
00394       if ((end = strchr(file,':'))) {
00395          if (!strcasecmp(end, ":end")) {
00396             *end = '\0';
00397             end++;
00398          }
00399       }
00400    }
00401 
00402    for (;;) {
00403       ast_stopstream(chan);
00404       res = ast_streamfile(chan, file, chan->language);
00405       if (!res) {
00406          if (pause_restart_point) {
00407             ast_seekstream(chan->stream, pause_restart_point, SEEK_SET);
00408             pause_restart_point = 0;
00409          }
00410          else if (end) {
00411             ast_seekstream(chan->stream, 0, SEEK_END);
00412             end = NULL;
00413          };
00414          res = ast_waitstream_fr(chan, breaks, fwd, rev, skipms);
00415       }
00416 
00417       if (res < 1)
00418          break;
00419 
00420       /* We go at next loop if we got the restart char */
00421       if (restart && strchr(restart, res)) {
00422          if (option_debug)
00423             ast_log(LOG_DEBUG, "we'll restart the stream here at next loop\n");
00424          pause_restart_point = 0;
00425          continue;
00426       }
00427 
00428       if (pause && strchr(pause, res)) {
00429          pause_restart_point = ast_tellstream(chan->stream);
00430          for (;;) {
00431             ast_stopstream(chan);
00432             res = ast_waitfordigit(chan, 1000);
00433             if (!res)
00434                continue;
00435             else if (res == -1 || strchr(pause, res) || (stop && strchr(stop, res)))
00436                break;
00437          }
00438          if (res == *pause) {
00439             res = 0;
00440             continue;
00441          }
00442       }
00443 
00444       if (res == -1)
00445          break;
00446 
00447       /* if we get one of our stop chars, return it to the calling function */
00448       if (stop && strchr(stop, res))
00449          break;
00450    }
00451 
00452    ast_stopstream(chan);
00453 
00454    return res;
00455 }
00456 
00457 int ast_play_and_wait(struct ast_channel *chan, const char *fn)
00458 {
00459    int d;
00460    d = ast_streamfile(chan, fn, chan->language);
00461    if (d)
00462       return d;
00463    d = ast_waitstream(chan, AST_DIGIT_ANY);
00464    ast_stopstream(chan);
00465    return d;
00466 }
00467 
00468 static int global_silence_threshold = 128;
00469 static int global_maxsilence = 0;
00470 
00471 /*! Optionally play a sound file or a beep, then record audio and video from the channel.
00472  * @param chan Channel to playback to/record from.
00473  * @param playfile Filename of sound to play before recording begins.
00474  * @param recordfile Filename to record to.
00475  * @param maxtime Maximum length of recording (in milliseconds).
00476  * @param fmt Format(s) to record message in. Multiple formats may be specified by separating them with a '|'.
00477  * @param duration Where to store actual length of the recorded message (in milliseconds).
00478  * @param beep Whether to play a beep before starting to record.
00479  * @param silencethreshold 
00480  * @param maxsilence Length of silence that will end a recording (in milliseconds).
00481  * @param path Optional filesystem path to unlock.
00482  * @param prepend If true, prepend the recorded audio to an existing file.
00483  * @param acceptdtmf DTMF digits that will end the recording.
00484  * @param canceldtmf DTMF digits that will cancel the recording.
00485  */
00486 
00487 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)
00488 {
00489    int d = 0;
00490    char *fmts;
00491    char comment[256];
00492    int x, fmtcnt = 1, res = -1, outmsg = 0;
00493    struct ast_filestream *others[MAX_OTHER_FORMATS];
00494    char *sfmt[MAX_OTHER_FORMATS];
00495    char *stringp = NULL;
00496    time_t start, end;
00497    struct ast_dsp *sildet = NULL;   /* silence detector dsp */
00498    int totalsilence = 0;
00499    int rfmt = 0;
00500    struct ast_silence_generator *silgen = NULL;
00501    char prependfile[80];
00502 
00503    if (silencethreshold < 0)
00504       silencethreshold = global_silence_threshold;
00505 
00506    if (maxsilence < 0)
00507       maxsilence = global_maxsilence;
00508 
00509    /* barf if no pointer passed to store duration in */
00510    if (duration == NULL) {
00511       ast_log(LOG_WARNING, "Error play_and_record called without duration pointer\n");
00512       return -1;
00513    }
00514 
00515    if (option_debug)
00516       ast_log(LOG_DEBUG,"play_and_record: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
00517    snprintf(comment, sizeof(comment), "Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
00518 
00519    if (playfile || beep) {
00520       if (!beep)
00521          d = ast_play_and_wait(chan, playfile);
00522       if (d > -1)
00523          d = ast_stream_and_wait(chan, "beep", chan->language, "");
00524       if (d < 0)
00525          return -1;
00526    }
00527 
00528    if (prepend) {
00529       ast_copy_string(prependfile, recordfile, sizeof(prependfile)); 
00530       strncat(prependfile, "-prepend", sizeof(prependfile) - strlen(prependfile) - 1);
00531    }
00532 
00533    fmts = ast_strdupa(fmt);
00534 
00535    stringp = fmts;
00536    strsep(&stringp, "|");
00537    if (option_debug)
00538       ast_log(LOG_DEBUG, "Recording Formats: sfmts=%s\n", fmts);
00539    sfmt[0] = ast_strdupa(fmts);
00540 
00541    while ((fmt = strsep(&stringp, "|"))) {
00542       if (fmtcnt > MAX_OTHER_FORMATS - 1) {
00543          ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app.c\n");
00544          break;
00545       }
00546       sfmt[fmtcnt++] = ast_strdupa(fmt);
00547    }
00548 
00549    end = start = time(NULL);  /* pre-initialize end to be same as start in case we never get into loop */
00550    for (x = 0; x < fmtcnt; x++) {
00551       others[x] = ast_writefile(prepend ? prependfile : recordfile, sfmt[x], comment, O_TRUNC, 0, 0777);
00552       if (option_verbose > 2)
00553          ast_verbose(VERBOSE_PREFIX_3 "x=%d, open writing:  %s format: %s, %p\n", x, prepend ? prependfile : recordfile, sfmt[x], others[x]);
00554 
00555       if (!others[x])
00556          break;
00557    }
00558 
00559    if (path)
00560       ast_unlock_path(path);
00561 
00562    if (maxsilence > 0) {
00563       sildet = ast_dsp_new(); /* Create the silence detector */
00564       if (!sildet) {
00565          ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
00566          return -1;
00567       }
00568       ast_dsp_set_threshold(sildet, silencethreshold);
00569       rfmt = chan->readformat;
00570       res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
00571       if (res < 0) {
00572          ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
00573          ast_dsp_free(sildet);
00574          return -1;
00575       }
00576    }
00577 
00578    if (!prepend) {
00579       /* Request a video update */
00580       ast_indicate(chan, AST_CONTROL_VIDUPDATE);
00581 
00582       if (ast_opt_transmit_silence)
00583          silgen = ast_channel_start_silence_generator(chan);
00584    }
00585 
00586    if (x == fmtcnt) {
00587       /* Loop forever, writing the packets we read to the writer(s), until
00588          we read a digit or get a hangup */
00589       struct ast_frame *f;
00590       for (;;) {
00591          res = ast_waitfor(chan, 2000);
00592          if (!res) {
00593             if (option_debug)
00594                ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
00595             /* Try one more time in case of masq */
00596             res = ast_waitfor(chan, 2000);
00597             if (!res) {
00598                ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
00599                res = -1;
00600             }
00601          }
00602 
00603          if (res < 0) {
00604             f = NULL;
00605             break;
00606          }
00607          f = ast_read(chan);
00608          if (!f)
00609             break;
00610          if (f->frametype == AST_FRAME_VOICE) {
00611             /* write each format */
00612             for (x = 0; x < fmtcnt; x++) {
00613                if (prepend && !others[x])
00614                   break;
00615                res = ast_writestream(others[x], f);
00616             }
00617 
00618             /* Silence Detection */
00619             if (maxsilence > 0) {
00620                int dspsilence = 0;
00621                ast_dsp_silence(sildet, f, &dspsilence);
00622                if (dspsilence)
00623                   totalsilence = dspsilence;
00624                else
00625                   totalsilence = 0;
00626 
00627                if (totalsilence > maxsilence) {
00628                   /* Ended happily with silence */
00629                   if (option_verbose > 2)
00630                      ast_verbose( VERBOSE_PREFIX_3 "Recording automatically stopped after a silence of %d seconds\n", totalsilence/1000);
00631                   res = 'S';
00632                   outmsg = 2;
00633                   break;
00634                }
00635             }
00636             /* Exit on any error */
00637             if (res) {
00638                ast_log(LOG_WARNING, "Error writing frame\n");
00639                break;
00640             }
00641          } else if (f->frametype == AST_FRAME_VIDEO) {
00642             /* Write only once */
00643             ast_writestream(others[0], f);
00644          } else if (f->frametype == AST_FRAME_DTMF) {
00645             if (prepend) {
00646             /* stop recording with any digit */
00647                if (option_verbose > 2) 
00648                   ast_verbose(VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
00649                res = 't';
00650                outmsg = 2;
00651                break;
00652             }
00653             if (strchr(acceptdtmf, f->subclass)) {
00654                if (option_verbose > 2)
00655                   ast_verbose(VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
00656                res = f->subclass;
00657                outmsg = 2;
00658                break;
00659             }
00660             if (strchr(canceldtmf, f->subclass)) {
00661                if (option_verbose > 2)
00662                   ast_verbose(VERBOSE_PREFIX_3 "User cancelled message by pressing %c\n", f->subclass);
00663                res = f->subclass;
00664                outmsg = 0;
00665                break;
00666             }
00667          }
00668          if (maxtime) {
00669             end = time(NULL);
00670             if (maxtime < (end - start)) {
00671                if (option_verbose > 2)
00672                   ast_verbose(VERBOSE_PREFIX_3 "Took too long, cutting it short...\n");
00673                res = 't';
00674                outmsg = 2;
00675                break;
00676             }
00677          }
00678          ast_frfree(f);
00679       }
00680       if (!f) {
00681          if (option_verbose > 2)
00682             ast_verbose(VERBOSE_PREFIX_3 "User hung up\n");
00683          res = -1;
00684          outmsg = 1;
00685       } else {
00686          ast_frfree(f);
00687       }
00688       if (end == start)
00689          end = time(NULL);
00690    } else {
00691       ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", recordfile, sfmt[x]);
00692    }
00693 
00694    if (!prepend) {
00695       if (silgen)
00696          ast_channel_stop_silence_generator(chan, silgen);
00697    }
00698    *duration = end - start;
00699 
00700    if (!prepend) {
00701       for (x = 0; x < fmtcnt; x++) {
00702          if (!others[x])
00703             break;
00704          if (res > 0)
00705             ast_stream_rewind(others[x], totalsilence ? totalsilence - 200 : 200);
00706          ast_truncstream(others[x]);
00707          ast_closestream(others[x]);
00708       }
00709    }
00710 
00711    if (prepend && outmsg) {
00712       struct ast_filestream *realfiles[MAX_OTHER_FORMATS];
00713       struct ast_frame *fr;
00714 
00715       for (x = 0; x < fmtcnt; x++) {
00716          snprintf(comment, sizeof(comment), "Opening the real file %s.%s\n", recordfile, sfmt[x]);
00717          realfiles[x] = ast_readfile(recordfile, sfmt[x], comment, O_RDONLY, 0, 0);
00718          if (!others[x] || !realfiles[x])
00719             break;
00720          ast_stream_rewind(others[x], totalsilence ? totalsilence - 200 : 200);
00721          ast_truncstream(others[x]);
00722          /* add the original file too */
00723          while ((fr = ast_readframe(realfiles[x]))) {
00724             ast_writestream(others[x], fr);
00725             ast_frfree(fr);
00726          }
00727          ast_closestream(others[x]);
00728          ast_closestream(realfiles[x]);
00729          ast_filerename(prependfile, recordfile, sfmt[x]);
00730          if (option_verbose > 3)
00731             ast_verbose(VERBOSE_PREFIX_4 "Recording Format: sfmts=%s, prependfile %s, recordfile %s\n", sfmt[x], prependfile, recordfile);
00732          ast_filedelete(prependfile, sfmt[x]);
00733       }
00734    }
00735    if (rfmt && ast_set_read_format(chan, rfmt)) {
00736       ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
00737    }
00738    if (outmsg == 2) {
00739       ast_stream_and_wait(chan, "auth-thankyou", chan->language, "");
00740    }
00741    if (sildet)
00742       ast_dsp_free(sildet);
00743    return res;
00744 }
00745 
00746 static char default_acceptdtmf[] = "#";
00747 static char default_canceldtmf[] = "";
00748 
00749 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)
00750 {
00751    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));
00752 }
00753 
00754 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)
00755 {
00756    return __ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, 0, silencethreshold, maxsilence, path, 0, default_acceptdtmf, default_canceldtmf);
00757 }
00758 
00759 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)
00760 {
00761    return __ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, beep, silencethreshold, maxsilence, NULL, 1, default_acceptdtmf, default_canceldtmf);
00762 }
00763 
00764 /* Channel group core functions */
00765 
00766 int ast_app_group_split_group(const char *data, char *group, int group_max, char *category, int category_max)
00767 {
00768    int res=0;
00769    char tmp[256];
00770    char *grp=NULL, *cat=NULL;
00771 
00772    if (!ast_strlen_zero(data)) {
00773       ast_copy_string(tmp, data, sizeof(tmp));
00774       grp = tmp;
00775       cat = strchr(tmp, '@');
00776       if (cat) {
00777          *cat = '\0';
00778          cat++;
00779       }
00780    }
00781 
00782    if (!ast_strlen_zero(grp))
00783       ast_copy_string(group, grp, group_max);
00784    else
00785       res = -1;
00786 
00787    if (!ast_strlen_zero(cat))
00788       ast_copy_string(category, cat, category_max);
00789 
00790    return res;
00791 }
00792 
00793 int ast_app_group_set_channel(struct ast_channel *chan, const char *data)
00794 {
00795    int res = 0;
00796    char group[80] = "", category[80] = "";
00797    struct ast_group_info *gi = NULL;
00798    size_t len = 0;
00799    
00800    if (ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category)))
00801       return -1;
00802    
00803    /* Calculate memory we will need if this is new */
00804    len = sizeof(*gi) + strlen(group) + 1;
00805    if (!ast_strlen_zero(category))
00806       len += strlen(category) + 1;
00807    
00808    AST_LIST_LOCK(&groups);
00809    AST_LIST_TRAVERSE_SAFE_BEGIN(&groups, gi, list) {
00810       if ((gi->chan == chan) && ((ast_strlen_zero(category) && ast_strlen_zero(gi->category)) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))) {
00811          AST_LIST_REMOVE_CURRENT(&groups, list);
00812          free(gi);
00813          break;
00814       }
00815    }
00816    AST_LIST_TRAVERSE_SAFE_END
00817    
00818    if ((gi = calloc(1, len))) {
00819       gi->chan = chan;
00820       gi->group = (char *) gi + sizeof(*gi);
00821       strcpy(gi->group, group);
00822       if (!ast_strlen_zero(category)) {
00823          gi->category = (char *) gi + sizeof(*gi) + strlen(group) + 1;
00824          strcpy(gi->category, category);
00825       }
00826       AST_LIST_INSERT_TAIL(&groups, gi, list);
00827    } else {
00828       res = -1;
00829    }
00830    
00831    AST_LIST_UNLOCK(&groups);
00832    
00833    return res;
00834 }
00835 
00836 int ast_app_group_get_count(const char *group, const char *category)
00837 {
00838    struct ast_group_info *gi = NULL;
00839    int count = 0;
00840 
00841    if (ast_strlen_zero(group))
00842       return 0;
00843    
00844    AST_LIST_LOCK(&groups);
00845    AST_LIST_TRAVERSE(&groups, gi, list) {
00846       if (!strcasecmp(gi->group, group) && (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category))))
00847          count++;
00848    }
00849    AST_LIST_UNLOCK(&groups);
00850 
00851    return count;
00852 }
00853 
00854 int ast_app_group_match_get_count(const char *groupmatch, const char *category)
00855 {
00856    struct ast_group_info *gi = NULL;
00857    regex_t regexbuf;
00858    int count = 0;
00859 
00860    if (ast_strlen_zero(groupmatch))
00861       return 0;
00862 
00863    /* if regex compilation fails, return zero matches */
00864    if (regcomp(&regexbuf, groupmatch, REG_EXTENDED | REG_NOSUB))
00865       return 0;
00866 
00867    AST_LIST_LOCK(&groups);
00868    AST_LIST_TRAVERSE(&groups, gi, list) {
00869       if (!regexec(&regexbuf, gi->group, 0, NULL, 0) && (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category))))
00870          count++;
00871    }
00872    AST_LIST_UNLOCK(&groups);
00873 
00874    regfree(&regexbuf);
00875 
00876    return count;
00877 }
00878 
00879 int ast_app_group_update(struct ast_channel *old, struct ast_channel *new)
00880 {
00881    struct ast_group_info *gi = NULL;
00882 
00883    AST_LIST_LOCK(&groups);
00884    AST_LIST_TRAVERSE(&groups, gi, list) {
00885       if (gi->chan == old)
00886          gi->chan = new;
00887    }
00888    AST_LIST_UNLOCK(&groups);
00889 
00890    return 0;
00891 }
00892 
00893 int ast_app_group_discard(struct ast_channel *chan)
00894 {
00895    struct ast_group_info *gi = NULL;
00896    
00897    AST_LIST_LOCK(&groups);
00898    AST_LIST_TRAVERSE_SAFE_BEGIN(&groups, gi, list) {
00899       if (gi->chan == chan) {
00900          AST_LIST_REMOVE_CURRENT(&groups, list);
00901          free(gi);
00902       }
00903    }
00904         AST_LIST_TRAVERSE_SAFE_END
00905    AST_LIST_UNLOCK(&groups);
00906    
00907    return 0;
00908 }
00909 
00910 int ast_app_group_list_lock(void)
00911 {
00912    return AST_LIST_LOCK(&groups);
00913 }
00914 
00915 struct ast_group_info *ast_app_group_list_head(void)
00916 {
00917    return AST_LIST_FIRST(&groups);
00918 }
00919 
00920 int ast_app_group_list_unlock(void)
00921 {
00922    return AST_LIST_UNLOCK(&groups);
00923 }
00924 
00925 unsigned int ast_app_separate_args(char *buf, char delim, char **array, int arraylen)
00926 {
00927    int argc;
00928    char *scan;
00929    int paren = 0, quote = 0;
00930 
00931    if (!buf || !array || !arraylen)
00932       return 0;
00933 
00934    memset(array, 0, arraylen * sizeof(*array));
00935 
00936    scan = buf;
00937 
00938    for (argc = 0; *scan && (argc < arraylen - 1); argc++) {
00939       array[argc] = scan;
00940       for (; *scan; scan++) {
00941          if (*scan == '(')
00942             paren++;
00943          else if (*scan == ')') {
00944             if (paren)
00945                paren--;
00946          } else if (*scan == '"' && delim != '"') {
00947             quote = quote ? 0 : 1;
00948             /* Remove quote character from argument */
00949             memmove(scan, scan + 1, strlen(scan));
00950             scan--;
00951          } else if (*scan == '\\') {
00952             /* Literal character, don't parse */
00953             memmove(scan, scan + 1, strlen(scan));
00954          } else if ((*scan == delim) && !paren && !quote) {
00955             *scan++ = '\0';
00956             break;
00957          }
00958       }
00959    }
00960 
00961    if (*scan)
00962       array[argc++] = scan;
00963 
00964    return argc;
00965 }
00966 
00967 enum AST_LOCK_RESULT ast_lock_path(const char *path)
00968 {
00969    char *s;
00970    char *fs;
00971    int res;
00972    int fd;
00973    int lp = strlen(path);
00974    time_t start;
00975 
00976    if (!(s = alloca(lp + 10)) || !(fs = alloca(lp + 20))) {
00977       ast_log(LOG_WARNING, "Out of memory!\n");
00978       return AST_LOCK_FAILURE;
00979    }
00980 
00981    snprintf(fs, strlen(path) + 19, "%s/.lock-%08lx", path, ast_random());
00982    fd = open(fs, O_WRONLY | O_CREAT | O_EXCL, 0600);
00983    if (fd < 0) {
00984       ast_log(LOG_ERROR, "Unable to create lock file '%s': %s\n", path, strerror(errno));
00985       return AST_LOCK_PATH_NOT_FOUND;
00986    }
00987    close(fd);
00988 
00989    snprintf(s, strlen(path) + 9, "%s/.lock", path);
00990    start = time(NULL);
00991    while (((res = link(fs, s)) < 0) && (errno == EEXIST) && (time(NULL) - start < 5))
00992       usleep(1);
00993 
00994    unlink(fs);
00995 
00996    if (res) {
00997       ast_log(LOG_WARNING, "Failed to lock path '%s': %s\n", path, strerror(errno));
00998       return AST_LOCK_TIMEOUT;
00999    } else {
01000       if (option_debug)
01001          ast_log(LOG_DEBUG, "Locked path '%s'\n", path);
01002       return AST_LOCK_SUCCESS;
01003    }
01004 }
01005 
01006 int ast_unlock_path(const char *path)
01007 {
01008    char *s;
01009    int res;
01010 
01011    if (!(s = alloca(strlen(path) + 10))) {
01012       ast_log(LOG_WARNING, "Out of memory!\n");
01013       return -1;
01014    }
01015 
01016    snprintf(s, strlen(path) + 9, "%s/%s", path, ".lock");
01017 
01018    if ((res = unlink(s)))
01019       ast_log(LOG_ERROR, "Could not unlock path '%s': %s\n", path, strerror(errno));
01020    else {
01021       if (option_debug)
01022          ast_log(LOG_DEBUG, "Unlocked path '%s'\n", path);
01023    }
01024 
01025    return res;
01026 }
01027 
01028 int ast_record_review(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, const char *path) 
01029 {
01030    int silencethreshold = 128; 
01031    int maxsilence=0;
01032    int res = 0;
01033    int cmd = 0;
01034    int max_attempts = 3;
01035    int attempts = 0;
01036    int recorded = 0;
01037    int message_exists = 0;
01038    /* Note that urgent and private are for flagging messages as such in the future */
01039 
01040    /* barf if no pointer passed to store duration in */
01041    if (duration == NULL) {
01042       ast_log(LOG_WARNING, "Error ast_record_review called without duration pointer\n");
01043       return -1;
01044    }
01045 
01046    cmd = '3';   /* Want to start by recording */
01047 
01048    while ((cmd >= 0) && (cmd != 't')) {
01049       switch (cmd) {
01050       case '1':
01051          if (!message_exists) {
01052             /* In this case, 1 is to record a message */
01053             cmd = '3';
01054             break;
01055          } else {
01056             ast_stream_and_wait(chan, "vm-msgsaved", chan->language, "");
01057             cmd = 't';
01058             return res;
01059          }
01060       case '2':
01061          /* Review */
01062          ast_verbose(VERBOSE_PREFIX_3 "Reviewing the recording\n");
01063          cmd = ast_stream_and_wait(chan, recordfile, chan->language, AST_DIGIT_ANY);
01064          break;
01065       case '3':
01066          message_exists = 0;
01067          /* Record */
01068          if (recorded == 1)
01069             ast_verbose(VERBOSE_PREFIX_3 "Re-recording\n");
01070          else  
01071             ast_verbose(VERBOSE_PREFIX_3 "Recording\n");
01072          recorded = 1;
01073          cmd = ast_play_and_record(chan, playfile, recordfile, maxtime, fmt, duration, silencethreshold, maxsilence, path);
01074          if (cmd == -1) {
01075          /* User has hung up, no options to give */
01076             return cmd;
01077          }
01078          if (cmd == '0') {
01079             break;
01080          } else if (cmd == '*') {
01081             break;
01082          } 
01083          else {
01084             /* If all is well, a message exists */
01085             message_exists = 1;
01086             cmd = 0;
01087          }
01088          break;
01089       case '4':
01090       case '5':
01091       case '6':
01092       case '7':
01093       case '8':
01094       case '9':
01095       case '*':
01096       case '#':
01097          cmd = ast_play_and_wait(chan, "vm-sorry");
01098          break;
01099       default:
01100          if (message_exists) {
01101             cmd = ast_play_and_wait(chan, "vm-review");
01102          }
01103          else {
01104             cmd = ast_play_and_wait(chan, "vm-torerecord");
01105             if (!cmd)
01106                cmd = ast_waitfordigit(chan, 600);
01107          }
01108          
01109          if (!cmd)
01110             cmd = ast_waitfordigit(chan, 6000);
01111          if (!cmd) {
01112             attempts++;
01113          }
01114          if (attempts > max_attempts) {
01115             cmd = 't';
01116          }
01117       }
01118    }
01119    if (cmd == 't')
01120       cmd = 0;
01121    return cmd;
01122 }
01123 
01124 #define RES_UPONE (1 << 16)
01125 #define RES_EXIT  (1 << 17)
01126 #define RES_REPEAT (1 << 18)
01127 #define RES_RESTART ((1 << 19) | RES_REPEAT)
01128 
01129 static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata);
01130 
01131 static int ivr_dispatch(struct ast_channel *chan, struct ast_ivr_option *option, char *exten, void *cbdata)
01132 {
01133    int res;
01134    int (*ivr_func)(struct ast_channel *, void *);
01135    char *c;
01136    char *n;
01137    
01138    switch(option->action) {
01139    case AST_ACTION_UPONE:
01140       return RES_UPONE;
01141    case AST_ACTION_EXIT:
01142       return RES_EXIT | (((unsigned long)(option->adata)) & 0xffff);
01143    case AST_ACTION_REPEAT:
01144       return RES_REPEAT | (((unsigned long)(option->adata)) & 0xffff);
01145    case AST_ACTION_RESTART:
01146       return RES_RESTART ;
01147    case AST_ACTION_NOOP:
01148       return 0;
01149    case AST_ACTION_BACKGROUND:
01150       res = ast_stream_and_wait(chan, (char *)option->adata, chan->language, AST_DIGIT_ANY);
01151       if (res < 0) {
01152          ast_log(LOG_NOTICE, "Unable to find file '%s'!\n", (char *)option->adata);
01153          res = 0;
01154       }
01155       return res;
01156    case AST_ACTION_PLAYBACK:
01157       res = ast_stream_and_wait(chan, (char *)option->adata, chan->language, "");
01158       if (res < 0) {
01159          ast_log(LOG_NOTICE, "Unable to find file '%s'!\n", (char *)option->adata);
01160          res = 0;
01161       }
01162       return res;
01163    case AST_ACTION_MENU:
01164       res = ast_ivr_menu_run_internal(chan, (struct ast_ivr_menu *)option->adata, cbdata);
01165       /* Do not pass entry errors back up, treaat ast though ti was an "UPONE" */
01166       if (res == -2)
01167          res = 0;
01168       return res;
01169    case AST_ACTION_WAITOPTION:
01170       res = ast_waitfordigit(chan, 1000 * (chan->pbx ? chan->pbx->rtimeout : 10));
01171       if (!res)
01172          return 't';
01173       return res;
01174    case AST_ACTION_CALLBACK:
01175       ivr_func = option->adata;
01176       res = ivr_func(chan, cbdata);
01177       return res;
01178    case AST_ACTION_TRANSFER:
01179       res = ast_parseable_goto(chan, option->adata);
01180       return 0;
01181    case AST_ACTION_PLAYLIST:
01182    case AST_ACTION_BACKLIST:
01183       res = 0;
01184       c = ast_strdupa(option->adata);
01185       while ((n = strsep(&c, ";"))) {
01186          if ((res = ast_stream_and_wait(chan, n, chan->language,
01187                (option->action == AST_ACTION_BACKLIST) ? AST_DIGIT_ANY : "")))
01188             break;
01189       }
01190       ast_stopstream(chan);
01191       return res;
01192    default:
01193       ast_log(LOG_NOTICE, "Unknown dispatch function %d, ignoring!\n", option->action);
01194       return 0;
01195    };
01196    return -1;
01197 }
01198 
01199 static int option_exists(struct ast_ivr_menu *menu, char *option)
01200 {
01201    int x;
01202    for (x = 0; menu->options[x].option; x++)
01203       if (!strcasecmp(menu->options[x].option, option))
01204          return x;
01205    return -1;
01206 }
01207 
01208 static int option_matchmore(struct ast_ivr_menu *menu, char *option)
01209 {
01210    int x;
01211    for (x = 0; menu->options[x].option; x++)
01212       if ((!strncasecmp(menu->options[x].option, option, strlen(option))) && 
01213             (menu->options[x].option[strlen(option)]))
01214          return x;
01215    return -1;
01216 }
01217 
01218 static int read_newoption(struct ast_channel *chan, struct ast_ivr_menu *menu, char *exten, int maxexten)
01219 {
01220    int res=0;
01221    int ms;
01222    while (option_matchmore(menu, exten)) {
01223       ms = chan->pbx ? chan->pbx->dtimeout : 5000;
01224       if (strlen(exten) >= maxexten - 1) 
01225          break;
01226       res = ast_waitfordigit(chan, ms);
01227       if (res < 1)
01228          break;
01229       exten[strlen(exten) + 1] = '\0';
01230       exten[strlen(exten)] = res;
01231    }
01232    return res > 0 ? 0 : res;
01233 }
01234 
01235 static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata)
01236 {
01237    /* Execute an IVR menu structure */
01238    int res=0;
01239    int pos = 0;
01240    int retries = 0;
01241    char exten[AST_MAX_EXTENSION] = "s";
01242    if (option_exists(menu, "s") < 0) {
01243       strcpy(exten, "g");
01244       if (option_exists(menu, "g") < 0) {
01245          ast_log(LOG_WARNING, "No 's' nor 'g' extension in menu '%s'!\n", menu->title);
01246          return -1;
01247       }
01248    }
01249    while(!res) {
01250       while(menu->options[pos].option) {
01251          if (!strcasecmp(menu->options[pos].option, exten)) {
01252             res = ivr_dispatch(chan, menu->options + pos, exten, cbdata);
01253             if (option_debug)
01254                ast_log(LOG_DEBUG, "IVR Dispatch of '%s' (pos %d) yields %d\n", exten, pos, res);
01255             if (res < 0)
01256                break;
01257             else if (res & RES_UPONE)
01258                return 0;
01259             else if (res & RES_EXIT)
01260                return res;
01261             else if (res & RES_REPEAT) {
01262                int maxretries = res & 0xffff;
01263                if ((res & RES_RESTART) == RES_RESTART) {
01264                   retries = 0;
01265                } else
01266                   retries++;
01267                if (!maxretries)
01268                   maxretries = 3;
01269                if ((maxretries > 0) && (retries >= maxretries)) {
01270                   if (option_debug)
01271                      ast_log(LOG_DEBUG, "Max retries %d exceeded\n", maxretries);
01272                   return -2;
01273                } else {
01274                   if (option_exists(menu, "g") > -1) 
01275                      strcpy(exten, "g");
01276                   else if (option_exists(menu, "s") > -1)
01277                      strcpy(exten, "s");
01278                }
01279                pos = 0;
01280                continue;
01281             } else if (res && strchr(AST_DIGIT_ANY, res)) {
01282                if (option_debug)
01283                   ast_log(LOG_DEBUG, "Got start of extension, %c\n", res);
01284                exten[1] = '\0';
01285                exten[0] = res;
01286                if ((res = read_newoption(chan, menu, exten, sizeof(exten))))
01287                   break;
01288                if (option_exists(menu, exten) < 0) {
01289                   if (option_exists(menu, "i")) {
01290                      if (option_debug)
01291                         ast_log(LOG_DEBUG, "Invalid extension entered, going to 'i'!\n");
01292                      strcpy(exten, "i");
01293                      pos = 0;
01294                      continue;
01295                   } else {
01296                      if (option_debug)
01297                         ast_log(LOG_DEBUG, "Aborting on invalid entry, with no 'i' option!\n");
01298                      res = -2;
01299                      break;
01300                   }
01301                } else {
01302                   if (option_debug)
01303                      ast_log(LOG_DEBUG, "New existing extension: %s\n", exten);
01304                   pos = 0;
01305                   continue;
01306                }
01307             }
01308          }
01309          pos++;
01310       }
01311       if (option_debug)
01312          ast_log(LOG_DEBUG, "Stopping option '%s', res is %d\n", exten, res);
01313       pos = 0;
01314       if (!strcasecmp(exten, "s"))
01315          strcpy(exten, "g");
01316       else
01317          break;
01318    }
01319    return res;
01320 }
01321 
01322 int ast_ivr_menu_run(struct ast_channel *chan, struct ast_ivr_menu *menu, void *cbdata)
01323 {
01324    int res = ast_ivr_menu_run_internal(chan, menu, cbdata);
01325    /* Hide internal coding */
01326    return res > 0 ? 0 : res;
01327 }
01328    
01329 char *ast_read_textfile(const char *filename)
01330 {
01331    int fd;
01332    char *output = NULL;
01333    struct stat filesize;
01334    int count = 0;
01335    int res;
01336    if (stat(filename, &filesize) == -1) {
01337       ast_log(LOG_WARNING, "Error can't stat %s\n", filename);
01338       return NULL;
01339    }
01340    count = filesize.st_size + 1;
01341    fd = open(filename, O_RDONLY);
01342    if (fd < 0) {
01343       ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", filename, strerror(errno));
01344       return NULL;
01345    }
01346    if ((output = ast_malloc(count))) {
01347       res = read(fd, output, count - 1);
01348       if (res == count - 1) {
01349          output[res] = '\0';
01350       } else {
01351          ast_log(LOG_WARNING, "Short read of %s (%d of %d): %s\n", filename, res, count - 1, strerror(errno));
01352          free(output);
01353          output = NULL;
01354       }
01355    }
01356    close(fd);
01357    return output;
01358 }
01359 
01360 int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
01361 {
01362    char *s;
01363    int curarg;
01364    unsigned int argloc;
01365    char *arg;
01366    int res = 0;
01367 
01368    ast_clear_flag(flags, AST_FLAGS_ALL);
01369 
01370    if (!optstr)
01371       return 0;
01372 
01373    s = optstr;
01374    while (*s) {
01375       curarg = *s++ & 0x7f;   /* the array (in app.h) has 128 entries */
01376       ast_set_flag(flags, options[curarg].flag);
01377       argloc = options[curarg].arg_index;
01378       if (*s == '(') {
01379          /* Has argument */
01380          arg = ++s;
01381          if ((s = strchr(s, ')'))) {
01382             if (argloc)
01383                args[argloc - 1] = arg;
01384             *s++ = '\0';
01385          } else {
01386             ast_log(LOG_WARNING, "Missing closing parenthesis for argument '%c' in string '%s'\n", curarg, arg);
01387             res = -1;
01388             break;
01389          }
01390       } else if (argloc) {
01391          args[argloc - 1] = NULL;
01392       }
01393    }
01394 
01395    return res;
01396 }
01397 

Generated on Wed Aug 15 01:24:13 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.3