make CLI output use singular/plural when appropriate (bug #4654)
[asterisk/asterisk.git] / apps / app_read.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Trivial application to read a variable
5  * 
6  * Copyright (C) 2003, Digium
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
14 #include <string.h>
15 #include <stdlib.h>
16
17 #include "asterisk.h"
18
19 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
20
21 #include "asterisk/lock.h"
22 #include "asterisk/file.h"
23 #include "asterisk/logger.h"
24 #include "asterisk/channel.h"
25 #include "asterisk/pbx.h"
26 #include "asterisk/app.h"
27 #include "asterisk/module.h"
28 #include "asterisk/translate.h"
29 #include "asterisk/options.h"
30 #include "asterisk/utils.h"
31
32 static char *tdesc = "Read Variable Application";
33
34 static char *app = "Read";
35
36 static char *synopsis = "Read a variable";
37
38 static char *descrip = 
39 "  Read(variable[|filename][|maxdigits][|option][|attempts][|timeout])\n\n"
40 "Reads a #-terminated string of digits a certain number of times from the\n"
41 "user in to the given variable.\n"
42 "  filename   -- file to play before reading digits.\n"
43 "  maxdigits  -- maximum acceptable number of digits. Stops reading after\n"
44 "                maxdigits have been entered (without requiring the user to\n"
45 "                press the '#' key).\n"
46 "                Defaults to 0 - no limit - wait for the user press the '#' key.\n"
47 "                Any value below 0 means the same. Max accepted value is 255.\n"
48 "  option     -- may be 'skip' to return immediately if the line is not up,\n"
49 "                or 'noanswer' to read digits even if the line is not up.\n"
50 "  attempts   -- if greater than 1, that many attempts will be made in the \n"
51 "                event no data is entered.\n"
52 "  timeout    -- if greater than 0, that value will override the default timeout.\n\n"
53 "Returns -1 on hangup or error and 0 otherwise.\n";
54
55 STANDARD_LOCAL_USER;
56
57 LOCAL_USER_DECL;
58
59 #define ast_next_data(instr,ptr,delim) if((ptr=strchr(instr,delim))) { *(ptr) = '\0' ; ptr++;}
60
61 static int read_exec(struct ast_channel *chan, void *data)
62 {
63         int res = 0;
64         struct localuser *u;
65         char tmp[256];
66         char *timeout = NULL;
67         char *varname = NULL;
68         char *filename = NULL;
69         char *loops;
70         char *maxdigitstr=NULL;
71         char *options=NULL;
72         int option_skip = 0;
73         int option_noanswer = 0;
74         int maxdigits=255;
75         int tries = 1;
76         int to = 0;
77         int x = 0;
78         char *argcopy = NULL;
79         char *args[8];
80
81         if (data)
82                 argcopy = ast_strdupa((char *)data);
83
84         if (ast_separate_app_args(argcopy, '|', args, sizeof(args) / sizeof(args[0])) < 1) {
85                 ast_log(LOG_WARNING, "Cannot Parse Arguements.\n");
86                 return -1;
87         }
88
89         varname = args[x++];
90         filename = args[x++];
91         maxdigitstr = args[x++];
92         options = args[x++];
93         loops = args[x++];
94         timeout = args[x++];
95         
96         if (options) { 
97                 if (!strcasecmp(options, "skip"))
98                         option_skip = 1;
99                 else if (!strcasecmp(options, "noanswer"))
100                         option_noanswer = 1;
101                 else {
102                         if (strchr(options, 's'))
103                                 option_skip = 1;
104                         if (strchr(options, 'n'))
105                                 option_noanswer = 1;
106                 }
107         }
108
109         if(loops) {
110                 tries = atoi(loops);
111                 if(tries <= 0)
112                         tries = 1;
113         }
114
115         if(timeout) {
116                 to = atoi(timeout);
117                 if (to <= 0)
118                         to = 0;
119                 else
120                         to *= 1000;
121         }
122
123         if (!(filename) || ast_strlen_zero(filename)) 
124                 filename = NULL;
125         if (maxdigitstr) {
126                 maxdigits = atoi(maxdigitstr);
127                 if ((maxdigits<1) || (maxdigits>255)) {
128                         maxdigits = 255;
129                 } else if (option_verbose > 2)
130                         ast_verbose(VERBOSE_PREFIX_3 "Accepting a maximum of %d digits.\n", maxdigits);
131         }
132         if (!(varname) || ast_strlen_zero(varname)) {
133                 ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[|filename][|maxdigits][|option][|attempts][|timeout])\n\n");
134                 return -1;
135         }
136         LOCAL_USER_ADD(u);
137         if (chan->_state != AST_STATE_UP) {
138                 if (option_skip) {
139                         /* At the user's option, skip if the line is not up */
140                         pbx_builtin_setvar_helper(chan, varname, "\0");
141                         LOCAL_USER_REMOVE(u);
142                         return 0;
143                 } else if (!option_noanswer) {
144                         /* Otherwise answer unless we're supposed to read while on-hook */
145                         res = ast_answer(chan);
146                 }
147         }
148         if (!res) {
149                 while(tries && !res) {
150                         ast_stopstream(chan);
151                         res = ast_app_getdata(chan, filename, tmp, maxdigits, to);
152                         if (res > -1) {
153                                 pbx_builtin_setvar_helper(chan, varname, tmp);
154                                 if (!ast_strlen_zero(tmp)) {
155                                         if (option_verbose > 2)
156                                                 ast_verbose(VERBOSE_PREFIX_3 "User entered '%s'\n", tmp);
157                                         tries = 0;
158                                 } else {
159                                         tries--;
160                                         if (option_verbose > 2) {
161                                                 if (tries)
162                                                         ast_verbose(VERBOSE_PREFIX_3 "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
163                                                 else
164                                                         ast_verbose(VERBOSE_PREFIX_3 "User entered nothing.\n");
165                                         }
166                                 }
167                                 res = 0;
168                         } else {
169                                 if (option_verbose > 2)
170                                         ast_verbose(VERBOSE_PREFIX_3 "User disconnected\n");
171                         }
172                 }
173         }
174         LOCAL_USER_REMOVE(u);
175         return res;
176 }
177
178 int unload_module(void)
179 {
180         STANDARD_HANGUP_LOCALUSERS;
181         return ast_unregister_application(app);
182 }
183
184 int load_module(void)
185 {
186         return ast_register_application(app, read_exec, synopsis, descrip);
187 }
188
189 char *description(void)
190 {
191         return tdesc;
192 }
193
194 int usecount(void)
195 {
196         int res;
197         STANDARD_USECOUNT(res);
198         return res;
199 }
200
201 char *key()
202 {
203         return ASTERISK_GPL_KEY;
204 }