Merged revisions 335014 via svnmerge from
[asterisk/asterisk.git] / apps / app_readexten.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007-2008, Trinity College Computing Center
5  * Written by David Chappell <David.Chappell@trincoll.edu>
6  *
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17
18 /*! \file
19  *
20  * \brief Trivial application to read an extension into a variable
21  *
22  * \author David Chappell <David.Chappell@trincoll.edu>
23  * 
24  * \ingroup applications
25  */
26
27 /*** MODULEINFO
28         <support_level>core</support_level>
29  ***/
30  
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/file.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/app.h"
38 #include "asterisk/module.h"
39 #include "asterisk/indications.h"
40 #include "asterisk/channel.h"
41
42 /*** DOCUMENTATION
43         <application name="ReadExten" language="en_US">
44                 <synopsis>
45                         Read an extension into a variable.
46                 </synopsis>
47                 <syntax>
48                         <parameter name="variable" required="true" />
49                         <parameter name="filename">
50                                 <para>File to play before reading digits or tone with option <literal>i</literal></para>
51                         </parameter>
52                         <parameter name="context">
53                                 <para>Context in which to match extensions.</para>
54                         </parameter>
55                         <parameter name="option">
56                                 <optionlist>
57                                         <option name="s">
58                                                 <para>Return immediately if the channel is not answered.</para>
59                                         </option>
60                                         <option name="i">
61                                                 <para>Play <replaceable>filename</replaceable> as an indication tone from your
62                                                 <filename>indications.conf</filename> or a directly specified list of
63                                                 frequencies and durations.</para>
64                                         </option>
65                                         <option name="n">
66                                                 <para>Read digits even if the channel is not answered.</para>
67                                         </option>
68                                 </optionlist>
69                         </parameter>
70                         <parameter name="timeout">
71                                 <para>An integer number of seconds to wait for a digit response. If
72                                 greater than <literal>0</literal>, that value will override the default timeout.</para>
73                         </parameter>
74                 </syntax>
75                 <description>
76                         <para>Reads a <literal>#</literal> terminated string of digits from the user into the given variable.</para>
77                         <para>Will set READEXTENSTATUS on exit with one of the following statuses:</para>
78                         <variablelist>
79                                 <variable name="READEXTENSTATUS">
80                                         <value name="OK">
81                                                 A valid extension exists in ${variable}.
82                                         </value>
83                                         <value name="TIMEOUT">
84                                                 No extension was entered in the specified time.  Also sets ${variable} to "t".
85                                         </value>
86                                         <value name="INVALID">
87                                                 An invalid extension, ${INVALID_EXTEN}, was entered.  Also sets ${variable} to "i".
88                                         </value>
89                                         <value name="SKIP">
90                                                 Line was not up and the option 's' was specified.
91                                         </value>
92                                         <value name="ERROR">
93                                                 Invalid arguments were passed.
94                                         </value>
95                                 </variable>
96                         </variablelist>
97                 </description>
98         </application>
99  ***/
100
101 enum readexten_option_flags {
102         OPT_SKIP = (1 << 0),
103         OPT_INDICATION = (1 << 1),
104         OPT_NOANSWER = (1 << 2),
105 };
106
107 AST_APP_OPTIONS(readexten_app_options, {
108         AST_APP_OPTION('s', OPT_SKIP),
109         AST_APP_OPTION('i', OPT_INDICATION),
110         AST_APP_OPTION('n', OPT_NOANSWER),
111 });
112
113 static char *app = "ReadExten";
114
115 static int readexten_exec(struct ast_channel *chan, const char *data)
116 {
117         int res = 0;
118         char exten[256] = "";
119         int maxdigits = sizeof(exten) - 1;
120         int timeout = 0, digit_timeout = 0, x = 0;
121         char *argcopy = NULL, *status = "";
122         struct ast_tone_zone_sound *ts = NULL;
123         struct ast_flags flags = {0};
124
125          AST_DECLARE_APP_ARGS(arglist,
126                 AST_APP_ARG(variable);
127                 AST_APP_ARG(filename);
128                 AST_APP_ARG(context);
129                 AST_APP_ARG(options);
130                 AST_APP_ARG(timeout);
131         );
132         
133         if (ast_strlen_zero(data)) {
134                 ast_log(LOG_WARNING, "ReadExten requires at least one argument\n");
135                 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
136                 return 0;
137         }
138
139         argcopy = ast_strdupa(data);
140         AST_STANDARD_APP_ARGS(arglist, argcopy);
141
142         if (ast_strlen_zero(arglist.variable)) {
143                 ast_log(LOG_WARNING, "Usage: ReadExten(variable[,filename[,context[,options[,timeout]]]])\n");
144                 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
145                 return 0;
146         }
147
148         if (ast_strlen_zero(arglist.filename))
149                 arglist.filename = NULL;
150
151         if (ast_strlen_zero(arglist.context))
152                 arglist.context = chan->context;
153
154         if (!ast_strlen_zero(arglist.options))
155                 ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options);
156
157         if (!ast_strlen_zero(arglist.timeout)) {
158                 timeout = atoi(arglist.timeout);
159                 if (timeout > 0)
160                         timeout *= 1000;
161         }
162
163         if (timeout <= 0)
164                 timeout = chan->pbx ? chan->pbx->rtimeoutms : 10000;
165
166         if (digit_timeout <= 0)
167                 digit_timeout = chan->pbx ? chan->pbx->dtimeoutms : 5000;
168
169         if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename)) {
170                 ts = ast_get_indication_tone(chan->zone, arglist.filename);
171         }
172
173         do {
174                 if (chan->_state != AST_STATE_UP) {
175                         if (ast_test_flag(&flags, OPT_SKIP)) {
176                                 /* At the user's option, skip if the line is not up */
177                                 pbx_builtin_setvar_helper(chan, arglist.variable, "");
178                                 status = "SKIP";
179                                 break;
180                         } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
181                                 /* Otherwise answer unless we're supposed to read while on-hook */
182                                 res = ast_answer(chan);
183                         }
184                 }
185
186                 if (res < 0) {
187                         status = "HANGUP";
188                         break;
189                 }
190
191                 ast_playtones_stop(chan);
192                 ast_stopstream(chan);
193
194                 if (ts && ts->data[0]) {
195                         res = ast_playtones_start(chan, 0, ts->data, 0);
196                 } else if (arglist.filename) {
197                         if (ast_test_flag(&flags, OPT_INDICATION) && ast_fileexists(arglist.filename, NULL, chan->language) <= 0) {
198                                 /*
199                                  * We were asked to play an indication that did not exist in the config.
200                                  * If no such file exists, play it as a tonelist.  With any luck they won't
201                                  * have a file named "350+440.ulaw"
202                                  * (but honestly, who would do something so silly?)
203                                  */
204                                 res = ast_playtones_start(chan, 0, arglist.filename, 0);
205                         } else {
206                                 res = ast_streamfile(chan, arglist.filename, chan->language);
207                         }
208                 }
209
210                 for (x = 0; x < maxdigits; x++) {
211                         ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout);
212                         res = ast_waitfordigit(chan, timeout);
213
214                         ast_playtones_stop(chan);
215                         ast_stopstream(chan);
216                         timeout = digit_timeout;
217
218                         if (res < 1) {          /* timeout expired or hangup */
219                                 if (ast_check_hangup(chan)) {
220                                         status = "HANGUP";
221                                 } else if (x == 0) {
222                                         pbx_builtin_setvar_helper(chan, arglist.variable, "t");
223                                         status = "TIMEOUT";
224                                 }
225                                 break;
226                         }
227
228                         exten[x] = res;
229                         if (!ast_matchmore_extension(chan, arglist.context, exten, 1 /* priority */,
230                                 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
231                                 if (!ast_exists_extension(chan, arglist.context, exten, 1,
232                                         S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))
233                                         && res == '#') {
234                                         exten[x] = '\0';
235                                 }
236                                 break;
237                         }
238                 }
239
240                 if (!ast_strlen_zero(status))
241                         break;
242
243                 if (ast_exists_extension(chan, arglist.context, exten, 1,
244                         S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
245                         ast_debug(3, "User entered valid extension '%s'\n", exten);
246                         pbx_builtin_setvar_helper(chan, arglist.variable, exten);
247                         status = "OK";
248                 } else {
249                         ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, chan->name);
250                         pbx_builtin_setvar_helper(chan, arglist.variable, "i");
251                         pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
252                         status = "INVALID";
253                 }
254         } while (0);
255
256         if (ts) {
257                 ts = ast_tone_zone_sound_unref(ts);
258         }
259
260         pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
261
262         return status[0] == 'H' ? -1 : 0;
263 }
264
265 static int unload_module(void)
266 {
267         int res = ast_unregister_application(app);
268         return res;
269 }
270
271 static int load_module(void)
272 {
273         int res = ast_register_application_xml(app, readexten_exec);
274         return res;
275 }
276
277 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");