Add the ability to specify multiple prompts to the Read() dialplan application,
[asterisk/asterisk.git] / apps / app_read.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Trivial application to read a variable
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27  
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "asterisk/lock.h"
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/app.h"
42 #include "asterisk/module.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/options.h"
45 #include "asterisk/utils.h"
46 #include "asterisk/indications.h"
47
48 enum {
49         OPT_SKIP = (1 << 0),
50         OPT_INDICATION = (1 << 1),
51         OPT_NOANSWER = (1 << 2),
52 } read_option_flags;
53
54 AST_APP_OPTIONS(read_app_options, {
55         AST_APP_OPTION('s', OPT_SKIP),
56         AST_APP_OPTION('i', OPT_INDICATION),
57         AST_APP_OPTION('n', OPT_NOANSWER),
58 });
59
60 static char *app = "Read";
61
62 static char *synopsis = "Read a variable";
63
64 static char *descrip = 
65 "  Read(variable[|filename[&filename2...]][|maxdigits][|option][|attempts][|timeout])\n\n"
66 "Reads a #-terminated string of digits a certain number of times from the\n"
67 "user in to the given variable.\n"
68 "  filename   -- file(s) to play before reading digits or tone with option i\n"
69 "  maxdigits  -- maximum acceptable number of digits. Stops reading after\n"
70 "                maxdigits have been entered (without requiring the user to\n"
71 "                press the '#' key).\n"
72 "                Defaults to 0 - no limit - wait for the user press the '#' key.\n"
73 "                Any value below 0 means the same. Max accepted value is 255.\n"
74 "  option     -- options are 's' , 'i', 'n'\n"
75 "                's' to return immediately if the line is not up,\n"
76 "                'i' to play  filename as an indication tone from your indications.conf\n"
77 "                'n' to read digits even if the line is not up.\n"
78 "  attempts   -- if greater than 1, that many attempts will be made in the \n"
79 "                event no data is entered.\n"
80 "  timeout    -- The number of seconds to wait for a digit response. If greater\n"
81 "                than 0, that value will override the default timeout. Can be floating point.\n\n"
82 "Read should disconnect if the function fails or errors out.\n";
83
84
85 #define ast_next_data(instr,ptr,delim) if((ptr=strchr(instr,delim))) { *(ptr) = '\0' ; ptr++;}
86
87 static int read_exec(struct ast_channel *chan, void *data)
88 {
89         int res = 0;
90         struct ast_module_user *u;
91         char tmp[256] = "";
92         int maxdigits = 255;
93         int tries = 1, to = 0, x = 0;
94         double tosec;
95         char *argcopy = NULL;
96         struct tone_zone_sound *ts;
97         struct ast_flags flags = {0};
98
99          AST_DECLARE_APP_ARGS(arglist,
100                 AST_APP_ARG(variable);
101                 AST_APP_ARG(filename);
102                 AST_APP_ARG(maxdigits);
103                 AST_APP_ARG(options);
104                 AST_APP_ARG(attempts);
105                 AST_APP_ARG(timeout);
106         );
107         
108         if (ast_strlen_zero(data)) {
109                 ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
110                 return -1;
111         }
112
113         u = ast_module_user_add(chan);
114         
115         argcopy = ast_strdupa(data);
116
117         AST_STANDARD_APP_ARGS(arglist, argcopy);
118
119         if (!ast_strlen_zero(arglist.options)) {
120                 ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
121         }
122         
123         if (!ast_strlen_zero(arglist.attempts)) {
124                 tries = atoi(arglist.attempts);
125                 if (tries <= 0)
126                         tries = 1;
127         }
128
129         if (!ast_strlen_zero(arglist.timeout)) {
130                 tosec = atof(arglist.timeout);
131                 if (tosec <= 0)
132                         to = 0;
133                 else
134                         to = tosec * 1000.0;
135         }
136
137         if (ast_strlen_zero(arglist.filename)) {
138                 arglist.filename = NULL;
139         }
140         if (!ast_strlen_zero(arglist.maxdigits)) {
141                 maxdigits = atoi(arglist.maxdigits);
142                 if ((maxdigits<1) || (maxdigits>255)) {
143                         maxdigits = 255;
144                 } else if (option_verbose > 2)
145                         ast_verbose(VERBOSE_PREFIX_3 "Accepting a maximum of %d digits.\n", maxdigits);
146         }
147         if (ast_strlen_zero(arglist.variable)) {
148                 ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[|filename][|maxdigits][|option][|attempts][|timeout])\n\n");
149                 ast_module_user_remove(u);
150                 return -1;
151         }
152         ts=NULL;
153         if (ast_test_flag(&flags,OPT_INDICATION)) {
154                 if (!ast_strlen_zero(arglist.filename)) {
155                         ts = ast_get_indication_tone(chan->zone,arglist.filename);
156                 }
157         }
158         if (chan->_state != AST_STATE_UP) {
159                 if (ast_test_flag(&flags,OPT_SKIP)) {
160                         /* At the user's option, skip if the line is not up */
161                         pbx_builtin_setvar_helper(chan, arglist.variable, "\0");
162                         ast_module_user_remove(u);
163                         return 0;
164                 } else if (!ast_test_flag(&flags,OPT_NOANSWER)) {
165                         /* Otherwise answer unless we're supposed to read while on-hook */
166                         res = ast_answer(chan);
167                 }
168         }
169         if (!res) {
170                 while (tries && !res) {
171                         ast_stopstream(chan);
172                         if (ts && ts->data[0]) {
173                                 if (!to)
174                                         to = chan->pbx ? chan->pbx->rtimeout * 1000 : 6000;
175                                 res = ast_playtones_start(chan, 0, ts->data, 0);
176                                 for (x = 0; x < maxdigits; ) {
177                                         res = ast_waitfordigit(chan, to);
178                                         ast_playtones_stop(chan);
179                                         if (res < 1) {
180                                                 tmp[x]='\0';
181                                                 break;
182                                         }
183                                         tmp[x++] = res;
184                                         if (tmp[x-1] == '#') {
185                                                 tmp[x-1] = '\0';
186                                                 break;
187                                         }
188                                 }
189                         } else {
190                                 res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
191                         }
192                         if (res > -1) {
193                                 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
194                                 if (!ast_strlen_zero(tmp)) {
195                                         if (option_verbose > 2)
196                                                 ast_verbose(VERBOSE_PREFIX_3 "User entered '%s'\n", tmp);
197                                         tries = 0;
198                                 } else {
199                                         tries--;
200                                         if (option_verbose > 2) {
201                                                 if (tries)
202                                                         ast_verbose(VERBOSE_PREFIX_3 "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
203                                                 else
204                                                         ast_verbose(VERBOSE_PREFIX_3 "User entered nothing.\n");
205                                         }
206                                 }
207                                 res = 0;
208                         } else {
209                                 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
210                                 if (option_verbose > 2)
211                                         ast_verbose(VERBOSE_PREFIX_3 "User disconnected\n");
212                         }
213                 }
214         }
215         ast_module_user_remove(u);
216         return res;
217 }
218
219 static int unload_module(void)
220 {
221         int res;
222
223         res = ast_unregister_application(app);
224         
225         ast_module_user_hangup_all();
226
227         return res;     
228 }
229
230 static int load_module(void)
231 {
232         return ast_register_application(app, read_exec, synopsis, descrip);
233 }
234
235 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read Variable Application");