revert my pass through the tree to remove checks of the result of ast_strdupa
[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 <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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 *tdesc = "Read Variable Application";
61
62 static char *app = "Read";
63
64 static char *synopsis = "Read a variable";
65
66 static char *descrip = 
67 "  Read(variable[|filename][|maxdigits][|option][|attempts][|timeout])\n\n"
68 "Reads a #-terminated string of digits a certain number of times from the\n"
69 "user in to the given variable.\n"
70 "  filename   -- file to play before reading digits or tone with option i\n"
71 "  maxdigits  -- maximum acceptable number of digits. Stops reading after\n"
72 "                maxdigits have been entered (without requiring the user to\n"
73 "                press the '#' key).\n"
74 "                Defaults to 0 - no limit - wait for the user press the '#' key.\n"
75 "                Any value below 0 means the same. Max accepted value is 255.\n"
76 "  option     -- options are 's' , 'i', 'n'\n"
77 "                's' to return immediately if the line is not up,\n"
78 "                'i' to play  filename as an indication tone from your indications.conf\n"
79 "                'n' to read digits even if the line is not up.\n"
80 "  attempts   -- if greater than 1, that many attempts will be made in the \n"
81 "                event no data is entered.\n"
82 "  timeout    -- if greater than 0, that value will override the default timeout.\n\n"
83 "Read should disconnect if the function fails or errors out.\n";
84
85 STANDARD_LOCAL_USER;
86
87 LOCAL_USER_DECL;
88
89 #define ast_next_data(instr,ptr,delim) if((ptr=strchr(instr,delim))) { *(ptr) = '\0' ; ptr++;}
90
91 static int read_exec(struct ast_channel *chan, void *data)
92 {
93         int res = 0;
94         struct localuser *u;
95         char tmp[256];
96         int maxdigits=255;
97         int tries = 1;
98         int to = 0;
99         int x = 0;
100         char *argcopy = NULL;
101         struct tone_zone_sound *ts;
102         struct ast_flags flags = {0};
103
104          AST_DECLARE_APP_ARGS(arglist,
105                 AST_APP_ARG(variable);
106                 AST_APP_ARG(filename);
107                 AST_APP_ARG(maxdigits);
108                 AST_APP_ARG(options);
109                 AST_APP_ARG(attempts);
110                 AST_APP_ARG(timeout);
111         );
112         
113         if (ast_strlen_zero(data)) {
114                 ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
115                 return -1;
116         }
117
118         LOCAL_USER_ADD(u);
119         
120         argcopy = ast_strdupa(data);
121         if (!argcopy) {
122                 ast_log(LOG_ERROR, "Out of memory!\n");
123                 LOCAL_USER_REMOVE(u);
124                 return -1;
125         }
126
127         AST_STANDARD_APP_ARGS(arglist, argcopy);
128
129         if (!ast_strlen_zero(arglist.options)) {
130                 ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
131         }
132         
133         if (!ast_strlen_zero(arglist.attempts)) {
134                 tries = atoi(arglist.attempts);
135                 if (tries <= 0)
136                         tries = 1;
137         }
138
139         if (!ast_strlen_zero(arglist.timeout)) {
140                 to = atoi(arglist.timeout);
141                 if (to <= 0)
142                         to = 0;
143                 else
144                         to *= 1000;
145         }
146
147         if (ast_strlen_zero(arglist.filename)) {
148                 arglist.filename = NULL;
149         }
150         if (!ast_strlen_zero(arglist.maxdigits)) {
151                 maxdigits = atoi(arglist.maxdigits);
152                 if ((maxdigits<1) || (maxdigits>255)) {
153                         maxdigits = 255;
154                 } else if (option_verbose > 2)
155                         ast_verbose(VERBOSE_PREFIX_3 "Accepting a maximum of %d digits.\n", maxdigits);
156         }
157         if (ast_strlen_zero(arglist.variable)) {
158                 ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[|filename][|maxdigits][|option][|attempts][|timeout])\n\n");
159                 LOCAL_USER_REMOVE(u);
160                 return -1;
161         }
162         ts=NULL;
163         if (ast_test_flag(&flags,OPT_INDICATION)) {
164                 if (!ast_strlen_zero(arglist.filename)) {
165                         ts = ast_get_indication_tone(chan->zone,arglist.filename);
166                 }
167         }
168         if (chan->_state != AST_STATE_UP) {
169                 if (ast_test_flag(&flags,OPT_SKIP)) {
170                         /* At the user's option, skip if the line is not up */
171                         pbx_builtin_setvar_helper(chan, arglist.variable, "\0");
172                         LOCAL_USER_REMOVE(u);
173                         return 0;
174                 } else if (!ast_test_flag(&flags,OPT_NOANSWER)) {
175                         /* Otherwise answer unless we're supposed to read while on-hook */
176                         res = ast_answer(chan);
177                 }
178         }
179         if (!res) {
180                 while (tries && !res) {
181                         ast_stopstream(chan);
182                         if (ts && ts->data[0]) {
183                                 if (!to)
184                                         to = chan->pbx ? chan->pbx->rtimeout * 1000 : 6000;
185                                 res = ast_playtones_start(chan, 0, ts->data, 0);
186                                 for (x = 0; x < maxdigits; ) {
187                                         res = ast_waitfordigit(chan, to);
188                                         ast_playtones_stop(chan);
189                                         if (res < 1) {
190                                                 tmp[x]='\0';
191                                                 break;
192                                         }
193                                         tmp[x++] = res;
194                                         if (tmp[x-1] == '#') {
195                                                 tmp[x-1] = '\0';
196                                                 break;
197                                         }
198                                 }
199                         } else {
200                                 res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
201                         }
202                         if (res > -1) {
203                                 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
204                                 if (!ast_strlen_zero(tmp)) {
205                                         if (option_verbose > 2)
206                                                 ast_verbose(VERBOSE_PREFIX_3 "User entered '%s'\n", tmp);
207                                         tries = 0;
208                                 } else {
209                                         tries--;
210                                         if (option_verbose > 2) {
211                                                 if (tries)
212                                                         ast_verbose(VERBOSE_PREFIX_3 "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
213                                                 else
214                                                         ast_verbose(VERBOSE_PREFIX_3 "User entered nothing.\n");
215                                         }
216                                 }
217                                 res = 0;
218                         } else {
219                                 if (option_verbose > 2)
220                                         ast_verbose(VERBOSE_PREFIX_3 "User disconnected\n");
221                         }
222                 }
223         }
224         LOCAL_USER_REMOVE(u);
225         return res;
226 }
227
228 int unload_module(void)
229 {
230         int res;
231
232         res = ast_unregister_application(app);
233         
234         STANDARD_HANGUP_LOCALUSERS;
235
236         return res;     
237 }
238
239 int load_module(void)
240 {
241         return ast_register_application(app, read_exec, synopsis, descrip);
242 }
243
244 char *description(void)
245 {
246         return tdesc;
247 }
248
249 int usecount(void)
250 {
251         int res;
252         STANDARD_USECOUNT(res);
253         return res;
254 }
255
256 char *key()
257 {
258         return ASTERISK_GPL_KEY;
259 }