This update fixes the problem reported in bug 8551; that ast_app_getdata() behaves...
[asterisk/asterisk.git] / main / app.c
index b6a7f73..b2e2b8d 100644 (file)
@@ -99,27 +99,53 @@ int ast_app_dtget(struct ast_channel *chan, const char *context, char *collect,
        return res;
 }
 
-/*! \param timeout set timeout to 0 for "standard" timeouts. Set timeout to -1 for 
-   "ludicrous time" (essentially never times out) */
-int ast_app_getdata(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout)
+/*! \param c The channel to read from
+ *  \param prompt The file to stream to the channel
+ *  \param s The string to read in to.  Must be at least the size of your length
+ *  \param maxlen How many digits to read (maximum)
+ *  \param timeout set timeout to 0 for "standard" timeouts. Set timeout to -1 for 
+ *      "ludicrous time" (essentially never times out) */
+int ast_app_getdata(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout)
 {
-       int res,to,fto;
+       int res = 0, to, fto;
+       char *front, *filename;
+
        /* XXX Merge with full version? XXX */
+       
        if (maxlen)
                s[0] = '\0';
-       if (prompt) {
-               res = ast_streamfile(c, prompt, c->language);
-               if (res < 0)
+
+       if (!prompt)
+               prompt="";
+
+       filename = ast_strdupa(prompt);
+       while ((front = strsep(&filename, "&"))) {
+               if (!ast_strlen_zero(front)) {
+                       res = ast_streamfile(c, front, c->language);
+                       if (res)
+                               continue;
+               }
+               if (ast_strlen_zero(filename)) {
+                       /* set timeouts for the last prompt */
+                       fto = c->pbx ? c->pbx->rtimeout * 1000 : 6000;
+                       to = c->pbx ? c->pbx->dtimeout * 1000 : 2000;
+
+                       if (timeout > 0) 
+                               fto = to = timeout;
+                       if (timeout < 0) 
+                               fto = to = 1000000000;
+               } else {
+                       /* there is more than one prompt, so
+                          get rid of the long timeout between 
+                          prompts, and make it 50ms */
+                       fto = 50;
+                       to = c->pbx ? c->pbx->dtimeout * 1000 : 2000;
+               }
+               res = ast_readstring(c, s, maxlen, to, fto, "#");
+               if (!ast_strlen_zero(s))
                        return res;
        }
-       fto = c->pbx ? c->pbx->rtimeout * 1000 : 6000;
-       to = c->pbx ? c->pbx->dtimeout * 1000 : 2000;
-
-       if (timeout > 0) 
-               fto = to = timeout;
-       if (timeout < 0) 
-               fto = to = 1000000000;
-       res = ast_readstring(c, s, maxlen, to, fto, "#");
+       
        return res;
 }
 
@@ -340,7 +366,7 @@ int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, in
                if (filename[0] == '/') 
                        ast_copy_string(tmpf, filename, sizeof(tmpf));
                else
-                       snprintf(tmpf, sizeof(tmpf), "%s/%s/%s", (char *)ast_config_AST_DATA_DIR, "sounds", filename);
+                       snprintf(tmpf, sizeof(tmpf), "%s/%s/%s", ast_config_AST_DATA_DIR, "sounds", filename);
                fd = open(tmpf, O_RDONLY);
                if (fd < 0){
                        ast_log(LOG_WARNING, "Unable to open file '%s': %s\n", tmpf, strerror(errno));
@@ -416,7 +442,8 @@ int ast_control_streamfile(struct ast_channel *chan, const char *file,
 
                /* We go at next loop if we got the restart char */
                if (restart && strchr(restart, res)) {
-                       ast_log(LOG_DEBUG, "we'll restart the stream here at next loop\n");
+                       if (option_debug)
+                               ast_log(LOG_DEBUG, "we'll restart the stream here at next loop\n");
                        pause_restart_point = 0;
                        continue;
                }
@@ -508,14 +535,15 @@ static int __ast_play_and_record(struct ast_channel *chan, const char *playfile,
                return -1;
        }
 
-       ast_log(LOG_DEBUG,"play_and_record: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
+       if (option_debug)
+               ast_log(LOG_DEBUG,"play_and_record: %s, %s, '%s'\n", playfile ? playfile : "<None>", recordfile, fmt);
        snprintf(comment, sizeof(comment), "Playing %s, Recording to: %s on %s\n", playfile ? playfile : "<None>", recordfile, chan->name);
 
        if (playfile || beep) {
                if (!beep)
                        d = ast_play_and_wait(chan, playfile);
                if (d > -1)
-                       d = ast_stream_and_wait(chan, "beep", chan->language, "");
+                       d = ast_stream_and_wait(chan, "beep", "");
                if (d < 0)
                        return -1;
        }
@@ -529,7 +557,8 @@ static int __ast_play_and_record(struct ast_channel *chan, const char *playfile,
 
        stringp = fmts;
        strsep(&stringp, "|");
-       ast_log(LOG_DEBUG, "Recording Formats: sfmts=%s\n", fmts);
+       if (option_debug)
+               ast_log(LOG_DEBUG, "Recording Formats: sfmts=%s\n", fmts);
        sfmt[0] = ast_strdupa(fmts);
 
        while ((fmt = strsep(&stringp, "|"))) {
@@ -584,7 +613,8 @@ static int __ast_play_and_record(struct ast_channel *chan, const char *playfile,
                for (;;) {
                        res = ast_waitfor(chan, 2000);
                        if (!res) {
-                               ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "One waitfor failed, trying another\n");
                                /* Try one more time in case of masq */
                                res = ast_waitfor(chan, 2000);
                                if (!res) {
@@ -729,7 +759,7 @@ static int __ast_play_and_record(struct ast_channel *chan, const char *playfile,
                ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
        }
        if (outmsg == 2) {
-               ast_stream_and_wait(chan, "auth-thankyou", chan->language, "");
+               ast_stream_and_wait(chan, "auth-thankyou", "");
        }
        if (sildet)
                ast_dsp_free(sildet);
@@ -931,7 +961,8 @@ enum AST_LOCK_RESULT ast_lock_path(const char *path)
                ast_log(LOG_WARNING, "Failed to lock path '%s': %s\n", path, strerror(errno));
                return AST_LOCK_TIMEOUT;
        } else {
-               ast_log(LOG_DEBUG, "Locked path '%s'\n", path);
+               if (option_debug)
+                       ast_log(LOG_DEBUG, "Locked path '%s'\n", path);
                return AST_LOCK_SUCCESS;
        }
 }
@@ -950,8 +981,10 @@ int ast_unlock_path(const char *path)
 
        if ((res = unlink(s)))
                ast_log(LOG_ERROR, "Could not unlock path '%s': %s\n", path, strerror(errno));
-       else
-               ast_log(LOG_DEBUG, "Unlocked path '%s'\n", path);
+       else {
+               if (option_debug)
+                       ast_log(LOG_DEBUG, "Unlocked path '%s'\n", path);
+       }
 
        return res;
 }
@@ -984,14 +1017,14 @@ int ast_record_review(struct ast_channel *chan, const char *playfile, const char
                                cmd = '3';
                                break;
                        } else {
-                               ast_stream_and_wait(chan, "vm-msgsaved", chan->language, "");
+                               ast_stream_and_wait(chan, "vm-msgsaved", "");
                                cmd = 't';
                                return res;
                        }
                case '2':
                        /* Review */
                        ast_verbose(VERBOSE_PREFIX_3 "Reviewing the recording\n");
-                       cmd = ast_stream_and_wait(chan, recordfile, chan->language, AST_DIGIT_ANY);
+                       cmd = ast_stream_and_wait(chan, recordfile, AST_DIGIT_ANY);
                        break;
                case '3':
                        message_exists = 0;
@@ -1078,14 +1111,14 @@ static int ivr_dispatch(struct ast_channel *chan, struct ast_ivr_option *option,
        case AST_ACTION_NOOP:
                return 0;
        case AST_ACTION_BACKGROUND:
-               res = ast_stream_and_wait(chan, (char *)option->adata, chan->language, AST_DIGIT_ANY);
+               res = ast_stream_and_wait(chan, (char *)option->adata, AST_DIGIT_ANY);
                if (res < 0) {
                        ast_log(LOG_NOTICE, "Unable to find file '%s'!\n", (char *)option->adata);
                        res = 0;
                }
                return res;
        case AST_ACTION_PLAYBACK:
-               res = ast_stream_and_wait(chan, (char *)option->adata, chan->language, "");
+               res = ast_stream_and_wait(chan, (char *)option->adata, "");
                if (res < 0) {
                        ast_log(LOG_NOTICE, "Unable to find file '%s'!\n", (char *)option->adata);
                        res = 0;
@@ -1114,7 +1147,7 @@ static int ivr_dispatch(struct ast_channel *chan, struct ast_ivr_option *option,
                res = 0;
                c = ast_strdupa(option->adata);
                while ((n = strsep(&c, ";"))) {
-                       if ((res = ast_stream_and_wait(chan, n, chan->language,
+                       if ((res = ast_stream_and_wait(chan, n,
                                        (option->action == AST_ACTION_BACKLIST) ? AST_DIGIT_ANY : "")))
                                break;
                }
@@ -1181,7 +1214,8 @@ static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_me
                while(menu->options[pos].option) {
                        if (!strcasecmp(menu->options[pos].option, exten)) {
                                res = ivr_dispatch(chan, menu->options + pos, exten, cbdata);
-                               ast_log(LOG_DEBUG, "IVR Dispatch of '%s' (pos %d) yields %d\n", exten, pos, res);
+                               if (option_debug)
+                                       ast_log(LOG_DEBUG, "IVR Dispatch of '%s' (pos %d) yields %d\n", exten, pos, res);
                                if (res < 0)
                                        break;
                                else if (res & RES_UPONE)
@@ -1197,7 +1231,8 @@ static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_me
                                        if (!maxretries)
                                                maxretries = 3;
                                        if ((maxretries > 0) && (retries >= maxretries)) {
-                                               ast_log(LOG_DEBUG, "Max retries %d exceeded\n", maxretries);
+                                               if (option_debug)
+                                                       ast_log(LOG_DEBUG, "Max retries %d exceeded\n", maxretries);
                                                return -2;
                                        } else {
                                                if (option_exists(menu, "g") > -1) 
@@ -1208,24 +1243,28 @@ static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_me
                                        pos = 0;
                                        continue;
                                } else if (res && strchr(AST_DIGIT_ANY, res)) {
-                                       ast_log(LOG_DEBUG, "Got start of extension, %c\n", res);
+                                       if (option_debug)
+                                               ast_log(LOG_DEBUG, "Got start of extension, %c\n", res);
                                        exten[1] = '\0';
                                        exten[0] = res;
                                        if ((res = read_newoption(chan, menu, exten, sizeof(exten))))
                                                break;
                                        if (option_exists(menu, exten) < 0) {
                                                if (option_exists(menu, "i")) {
-                                                       ast_log(LOG_DEBUG, "Invalid extension entered, going to 'i'!\n");
+                                                       if (option_debug)
+                                                               ast_log(LOG_DEBUG, "Invalid extension entered, going to 'i'!\n");
                                                        strcpy(exten, "i");
                                                        pos = 0;
                                                        continue;
                                                } else {
-                                                       ast_log(LOG_DEBUG, "Aborting on invalid entry, with no 'i' option!\n");
+                                                       if (option_debug)
+                                                               ast_log(LOG_DEBUG, "Aborting on invalid entry, with no 'i' option!\n");
                                                        res = -2;
                                                        break;
                                                }
                                        } else {
-                                               ast_log(LOG_DEBUG, "New existing extension: %s\n", exten);
+                                               if (option_debug)
+                                                       ast_log(LOG_DEBUG, "New existing extension: %s\n", exten);
                                                pos = 0;
                                                continue;
                                        }
@@ -1233,7 +1272,8 @@ static int ast_ivr_menu_run_internal(struct ast_channel *chan, struct ast_ivr_me
                        }
                        pos++;
                }
-               ast_log(LOG_DEBUG, "Stopping option '%s', res is %d\n", exten, res);
+               if (option_debug)
+                       ast_log(LOG_DEBUG, "Stopping option '%s', res is %d\n", exten, res);
                pos = 0;
                if (!strcasecmp(exten, "s"))
                        strcpy(exten, "g");
@@ -1302,14 +1342,14 @@ int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags
                if (*s == '(') {
                        /* Has argument */
                        arg = ++s;
-                       s = strchr(s, ')');
-                       if (*s) {
+                       if ((s = strchr(s, ')'))) {
                                if (argloc)
                                        args[argloc - 1] = arg;
                                *s++ = '\0';
                        } else {
                                ast_log(LOG_WARNING, "Missing closing parenthesis for argument '%c' in string '%s'\n", curarg, arg);
                                res = -1;
+                               break;
                        }
                } else if (argloc) {
                        args[argloc - 1] = NULL;