32e9202beff60af2230a7d2485dceb726eb3aea4
[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 "asterisk/lock.h"
33 #include "asterisk/file.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/app.h"
38 #include "asterisk/module.h"
39 #include "asterisk/translate.h"
40 #include "asterisk/options.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/indications.h"
43
44 enum {
45         OPT_SKIP = (1 << 0),
46         OPT_INDICATION = (1 << 1),
47         OPT_NOANSWER = (1 << 2),
48 } read_option_flags;
49
50 AST_APP_OPTIONS(read_app_options, {
51         AST_APP_OPTION('s', OPT_SKIP),
52         AST_APP_OPTION('i', OPT_INDICATION),
53         AST_APP_OPTION('n', OPT_NOANSWER),
54 });
55
56 static char *app = "Read";
57
58 static char *synopsis = "Read a variable";
59
60 static char *descrip = 
61 "  Read(variable[,filename[&filename2...]][,maxdigits][,option][,attempts][,timeout])\n\n"
62 "Reads a #-terminated string of digits a certain number of times from the\n"
63 "user in to the given variable.\n"
64 "  filename   -- file(s) to play before reading digits or tone with option i\n"
65 "  maxdigits  -- maximum acceptable number of digits. Stops reading after\n"
66 "                maxdigits have been entered (without requiring the user to\n"
67 "                press the '#' key).\n"
68 "                Defaults to 0 - no limit - wait for the user press the '#' key.\n"
69 "                Any value below 0 means the same. Max accepted value is 255.\n"
70 "  option     -- options are 's' , 'i', 'n'\n"
71 "                's' to return immediately if the line is not up,\n"
72 "                'i' to play  filename as an indication tone from your indications.conf\n"
73 "                'n' to read digits even if the line is not up.\n"
74 "  attempts   -- if greater than 1, that many attempts will be made in the \n"
75 "                event no data is entered.\n"
76 "  timeout    -- The number of seconds to wait for a digit response. If greater\n"
77 "                than 0, that value will override the default timeout. Can be floating point.\n\n"
78 "Read should disconnect if the function fails or errors out.\n";
79
80
81 #define ast_next_data(instr,ptr,delim) if((ptr=strchr(instr,delim))) { *(ptr) = '\0' ; ptr++;}
82
83 static int read_exec(struct ast_channel *chan, void *data)
84 {
85         int res = 0;
86         char tmp[256] = "";
87         int maxdigits = 255;
88         int tries = 1, to = 0, x = 0;
89         double tosec;
90         char *argcopy = NULL;
91         struct ind_tone_zone_sound *ts;
92         struct ast_flags flags = {0};
93
94          AST_DECLARE_APP_ARGS(arglist,
95                 AST_APP_ARG(variable);
96                 AST_APP_ARG(filename);
97                 AST_APP_ARG(maxdigits);
98                 AST_APP_ARG(options);
99                 AST_APP_ARG(attempts);
100                 AST_APP_ARG(timeout);
101         );
102         
103         if (ast_strlen_zero(data)) {
104                 ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
105                 return -1;
106         }
107         
108         argcopy = ast_strdupa(data);
109
110         AST_STANDARD_APP_ARGS(arglist, argcopy);
111
112         if (!ast_strlen_zero(arglist.options)) {
113                 ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
114         }
115         
116         if (!ast_strlen_zero(arglist.attempts)) {
117                 tries = atoi(arglist.attempts);
118                 if (tries <= 0)
119                         tries = 1;
120         }
121
122         if (!ast_strlen_zero(arglist.timeout)) {
123                 tosec = atof(arglist.timeout);
124                 if (tosec <= 0)
125                         to = 0;
126                 else {
127                         to = tosec * 1000.0;
128                         if (to <= 0) {
129                                 /* Make sure we don't use the default timeout. */
130                                 ast_log(LOG_WARNING, "Read timeout less than 1 ms, assuming 1 ms timeout\n");
131                                 to = 1;
132                         }
133                 }
134         }
135
136         if (ast_strlen_zero(arglist.filename)) {
137                 arglist.filename = NULL;
138         }
139         if (!ast_strlen_zero(arglist.maxdigits)) {
140                 maxdigits = atoi(arglist.maxdigits);
141                 if ((maxdigits<1) || (maxdigits>255)) {
142                         maxdigits = 255;
143                 } else
144                         ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits);
145         }
146         if (ast_strlen_zero(arglist.variable)) {
147                 ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n");
148                 return -1;
149         }
150         ts=NULL;
151         if (ast_test_flag(&flags,OPT_INDICATION)) {
152                 if (!ast_strlen_zero(arglist.filename)) {
153                         ts = ast_get_indication_tone(chan->zone,arglist.filename);
154                 }
155         }
156         if (chan->_state != AST_STATE_UP) {
157                 if (ast_test_flag(&flags,OPT_SKIP)) {
158                         /* At the user's option, skip if the line is not up */
159                         pbx_builtin_setvar_helper(chan, arglist.variable, "\0");
160                         return 0;
161                 } else if (!ast_test_flag(&flags,OPT_NOANSWER)) {
162                         /* Otherwise answer unless we're supposed to read while on-hook */
163                         res = ast_answer(chan);
164                 }
165         }
166         if (!res) {
167                 while (tries && !res) {
168                         ast_stopstream(chan);
169                         if (ts && ts->data[0]) {
170                                 if (!to)
171                                         to = chan->pbx ? chan->pbx->rtimeout * 1000 : 6000;
172                                 res = ast_playtones_start(chan, 0, ts->data, 0);
173                                 for (x = 0; x < maxdigits; ) {
174                                         res = ast_waitfordigit(chan, to);
175                                         ast_playtones_stop(chan);
176                                         if (res < 1) {
177                                                 tmp[x]='\0';
178                                                 break;
179                                         }
180                                         tmp[x++] = res;
181                                         if (tmp[x-1] == '#') {
182                                                 tmp[x-1] = '\0';
183                                                 break;
184                                         }
185                                 }
186                         } else {
187                                 res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
188                         }
189                         if (res > -1) {
190                                 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
191                                 if (!ast_strlen_zero(tmp)) {
192                                         ast_verb(3, "User entered '%s'\n", tmp);
193                                         tries = 0;
194                                 } else {
195                                         tries--;
196                                                 if (tries)
197                                                 ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
198                                                 else
199                                                 ast_verb(3, "User entered nothing.\n");
200                                         }
201                                 res = 0;
202                         } else {
203                                 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
204                                 ast_verb(3, "User disconnected\n");
205                         }
206                 }
207         }
208
209         return res;
210 }
211
212 static int unload_module(void)
213 {
214         return ast_unregister_application(app);
215 }
216
217 static int load_module(void)
218 {
219         return ast_register_application(app, read_exec, synopsis, descrip);
220 }
221
222 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read Variable Application");