Asterisk data retrieval API.
[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/file.h"
33 #include "asterisk/pbx.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/app.h"
36 #include "asterisk/module.h"
37 #include "asterisk/indications.h"
38
39 /*** DOCUMENTATION
40         <application name="Read" language="en_US">
41                 <synopsis>
42                         Read a variable.
43                 </synopsis>
44                 <syntax>
45                         <parameter name="variable" required="true">
46                                 <para>The input digits will be stored in the given <replaceable>variable</replaceable>
47                                 name.</para>
48                         </parameter>
49                         <parameter name="filenames" argsep="&amp;">
50                                 <argument name="filename" required="true">
51                                         <para>file(s) to play before reading digits or tone with option i</para>
52                                 </argument>
53                                 <argument name="filename2" multiple="true" />
54                         </parameter>
55                         <parameter name="maxdigits">
56                                 <para>Maximum acceptable number of digits. Stops reading after
57                                 <replaceable>maxdigits</replaceable> have been entered (without
58                                 requiring the user to press the <literal>#</literal> key).</para>
59                                 <para>Defaults to <literal>0</literal> - no limit - wait for the
60                                 user press the <literal>#</literal> key. Any value below
61                                 <literal>0</literal> means the same. Max accepted value is
62                                 <literal>255</literal>.</para>
63                         </parameter>
64                         <parameter name="options">
65                                 <optionlist>
66                                         <option name="s">
67                                                 <para>to return immediately if the line is not up.</para>
68                                         </option>
69                                         <option name="i">
70                                                 <para>to play  filename as an indication tone from your
71                                                 <filename>indications.conf</filename>.</para>
72                                         </option>
73                                         <option name="n">
74                                                 <para>to read digits even if the line is not up.</para>
75                                         </option>
76                                 </optionlist>
77                         </parameter>
78                         <parameter name="attempts">
79                                 <para>If greater than <literal>1</literal>, that many
80                                 <replaceable>attempts</replaceable> will be made in the
81                                 event no data is entered.</para>
82                         </parameter>
83                         <parameter name="timeout">
84                                 <para>The number of seconds to wait for a digit response. If greater
85                                 than <literal>0</literal>, that value will override the default timeout.
86                                 Can be floating point.</para>
87                         </parameter>
88                 </syntax>
89                 <description>
90                         <para>Reads a #-terminated string of digits a certain number of times from the
91                         user in to the given <replaceable>variable</replaceable>.</para>
92                         <para>This application sets the following channel variable upon completion:</para>
93                         <variablelist>
94                                 <variable name="READSTATUS">
95                                         <para>This is the status of the read operation.</para>
96                                         <value name="OK" />
97                                         <value name="ERROR" />
98                                         <value name="HANGUP" />
99                                         <value name="INTERRUPTED" />
100                                         <value name="SKIPPED" />
101                                         <value name="TIMEOUT" />
102                                 </variable>
103                         </variablelist>
104                 </description>
105                 <see-also>
106                         <ref type="application">SendDTMF</ref>
107                 </see-also>
108         </application>
109  ***/
110
111 enum read_option_flags {
112         OPT_SKIP = (1 << 0),
113         OPT_INDICATION = (1 << 1),
114         OPT_NOANSWER = (1 << 2),
115 };
116
117 AST_APP_OPTIONS(read_app_options, {
118         AST_APP_OPTION('s', OPT_SKIP),
119         AST_APP_OPTION('i', OPT_INDICATION),
120         AST_APP_OPTION('n', OPT_NOANSWER),
121 });
122
123 static char *app = "Read";
124
125 static int read_exec(struct ast_channel *chan, const char *data)
126 {
127         int res = 0;
128         char tmp[256] = "";
129         int maxdigits = 255;
130         int tries = 1, to = 0, x = 0;
131         double tosec;
132         char *argcopy = NULL;
133         struct ast_tone_zone_sound *ts = NULL;
134         struct ast_flags flags = {0};
135         const char *status = "ERROR";
136
137         AST_DECLARE_APP_ARGS(arglist,
138                 AST_APP_ARG(variable);
139                 AST_APP_ARG(filename);
140                 AST_APP_ARG(maxdigits);
141                 AST_APP_ARG(options);
142                 AST_APP_ARG(attempts);
143                 AST_APP_ARG(timeout);
144         );
145         
146         pbx_builtin_setvar_helper(chan, "READSTATUS", status);
147         if (ast_strlen_zero(data)) {
148                 ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
149                 return 0;
150         }
151         
152         argcopy = ast_strdupa(data);
153
154         AST_STANDARD_APP_ARGS(arglist, argcopy);
155
156         if (!ast_strlen_zero(arglist.options)) {
157                 ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
158         }
159         
160         if (!ast_strlen_zero(arglist.attempts)) {
161                 tries = atoi(arglist.attempts);
162                 if (tries <= 0)
163                         tries = 1;
164         }
165
166         if (!ast_strlen_zero(arglist.timeout)) {
167                 tosec = atof(arglist.timeout);
168                 if (tosec <= 0)
169                         to = 0;
170                 else
171                         to = tosec * 1000.0;
172         }
173
174         if (ast_strlen_zero(arglist.filename)) {
175                 arglist.filename = NULL;
176         }
177         if (!ast_strlen_zero(arglist.maxdigits)) {
178                 maxdigits = atoi(arglist.maxdigits);
179                 if ((maxdigits < 1) || (maxdigits > 255)) {
180                         maxdigits = 255;
181                 } else
182                         ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits);
183         }
184         if (ast_strlen_zero(arglist.variable)) {
185                 ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n");
186                 return 0;
187         }
188         if (ast_test_flag(&flags, OPT_INDICATION)) {
189                 if (!ast_strlen_zero(arglist.filename)) {
190                         ts = ast_get_indication_tone(chan->zone, arglist.filename);
191                 }
192         }
193         if (chan->_state != AST_STATE_UP) {
194                 if (ast_test_flag(&flags, OPT_SKIP)) {
195                         /* At the user's option, skip if the line is not up */
196                         pbx_builtin_setvar_helper(chan, arglist.variable, "");
197                         pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED");
198                         return 0;
199                 } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
200                         /* Otherwise answer unless we're supposed to read while on-hook */
201                         res = ast_answer(chan);
202                 }
203         }
204         if (!res) {
205                 while (tries && !res) {
206                         ast_stopstream(chan);
207                         if (ts && ts->data[0]) {
208                                 if (!to)
209                                         to = chan->pbx ? chan->pbx->rtimeoutms : 6000;
210                                 res = ast_playtones_start(chan, 0, ts->data, 0);
211                                 for (x = 0; x < maxdigits; ) {
212                                         res = ast_waitfordigit(chan, to);
213                                         ast_playtones_stop(chan);
214                                         if (res < 1) {
215                                                 if (res == 0)
216                                                         status = "TIMEOUT";
217                                                 tmp[x]='\0';
218                                                 break;
219                                         }
220                                         tmp[x++] = res;
221                                         if (tmp[x-1] == '#') {
222                                                 tmp[x-1] = '\0';
223                                                 status = "OK";
224                                                 break;
225                                         }
226                                         if (x >= maxdigits) {
227                                                 status = "OK";
228                                         }
229                                 }
230                         } else {
231                                 res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
232                                 if (res == AST_GETDATA_COMPLETE || res == AST_GETDATA_EMPTY_END_TERMINATED)
233                                         status = "OK";
234                                 else if (res == AST_GETDATA_TIMEOUT)
235                                         status = "TIMEOUT";
236                                 else if (res == AST_GETDATA_INTERRUPTED)
237                                         status = "INTERRUPTED";
238                         }
239                         if (res > -1) {
240                                 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
241                                 if (!ast_strlen_zero(tmp)) {
242                                         ast_verb(3, "User entered '%s'\n", tmp);
243                                         tries = 0;
244                                 } else {
245                                         tries--;
246                                         if (tries)
247                                                 ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
248                                         else
249                                                 ast_verb(3, "User entered nothing.\n");
250                                 }
251                                 res = 0;
252                         } else {
253                                 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
254                                 ast_verb(3, "User disconnected\n");
255                         }
256                 }
257         }
258
259         if (ts) {
260                 ts = ast_tone_zone_sound_unref(ts);
261         }
262
263         if (ast_check_hangup(chan))
264                 status = "HANGUP";
265         pbx_builtin_setvar_helper(chan, "READSTATUS", status);
266         return 0;
267 }
268
269 static int unload_module(void)
270 {
271         return ast_unregister_application(app);
272 }
273
274 static int load_module(void)
275 {
276         return ast_register_application_xml(app, read_exec);
277 }
278
279 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read Variable Application");