Allow the '#' sign to exist within an extension (inspired by issue #13330)
[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 #include "asterisk.h"
28
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30
31 #include "asterisk/file.h"
32 #include "asterisk/pbx.h"
33 #include "asterisk/app.h"
34 #include "asterisk/module.h"
35 #include "asterisk/indications.h"
36 #include "asterisk/channel.h"
37
38 /*** DOCUMENTATION
39         <application name="ReadExten" language="en_US">
40                 <synopsis>
41                         Read an extension into a variable.
42                 </synopsis>
43                 <syntax>
44                         <parameter name="variable" required="true" />
45                         <parameter name="filename">
46                                 <para>File to play before reading digits or tone with option <literal>i</literal></para>
47                         </parameter>
48                         <parameter name="context">
49                                 <para>Context in which to match extensions.</para>
50                         </parameter>
51                         <parameter name="option">
52                                 <optionlist>
53                                         <option name="s">
54                                                 <para>Return immediately if the channel is not answered.</para>
55                                         </option>
56                                         <option name="i">
57                                                 <para>Play <replaceable>filename</replaceable> as an indication tone from your
58                                                 <filename>indications.conf</filename></para>
59                                         </option>
60                                         <option name="n">
61                                                 <para>Read digits even if the channel is not answered.</para>
62                                         </option>
63                                 </optionlist>
64                         </parameter>
65                         <parameter name="timeout">
66                                 <para>An integer number of seconds to wait for a digit response. If
67                                 greater than <literal>0</literal>, that value will override the default timeout.</para>
68                         </parameter>
69                 </syntax>
70                 <description>
71                         <para>Reads a <literal>#</literal> terminated string of digits from the user into the given variable.</para>
72                         <para>Will set READEXTENSTATUS on exit with one of the following statuses:</para>
73                         <variablelist>
74                                 <variable name="READEXTENSTATUS">
75                                         <value name="OK">
76                                                 A valid extension exists in ${variable}.
77                                         </value>
78                                         <value name="TIMEOUT">
79                                                 No extension was entered in the specified time.  Also sets ${variable} to "t".
80                                         </value>
81                                         <value name="INVALID">
82                                                 An invalid extension, ${INVALID_EXTEN}, was entered.  Also sets ${variable} to "i".
83                                         </value>
84                                         <value name="SKIP">
85                                                 Line was not up and the option 's' was specified.
86                                         </value>
87                                         <value name="ERROR">
88                                                 Invalid arguments were passed.
89                                         </value>
90                                 </variable>
91                         </variablelist>
92                 </description>
93         </application>
94         <function name="VALID_EXTEN" language="en_US">
95                 <synopsis>
96                         Determine whether an extension exists or not.
97                 </synopsis>
98                 <syntax>
99                         <parameter name="context">
100                                 <para>Defaults to the current context</para>
101                         </parameter>
102                         <parameter name="extension" required="true" />
103                         <parameter name="priority">
104                                 <para>Priority defaults to <literal>1</literal>.</para>
105                         </parameter>
106                 </syntax>
107                 <description>
108                         <para>Returns a true value if the indicated <replaceable>context</replaceable>,
109                         <replaceable>extension</replaceable>, and <replaceable>priority</replaceable> exist.</para>
110                 </description>
111         </function>
112  ***/
113
114 enum {
115         OPT_SKIP = (1 << 0),
116         OPT_INDICATION = (1 << 1),
117         OPT_NOANSWER = (1 << 2),
118 } readexten_option_flags;
119
120 AST_APP_OPTIONS(readexten_app_options, {
121         AST_APP_OPTION('s', OPT_SKIP),
122         AST_APP_OPTION('i', OPT_INDICATION),
123         AST_APP_OPTION('n', OPT_NOANSWER),
124 });
125
126 static char *app = "ReadExten";
127
128 static int readexten_exec(struct ast_channel *chan, void *data)
129 {
130         int res = 0;
131         char exten[256] = "";
132         int maxdigits = sizeof(exten) - 1;
133         int timeout = 0, digit_timeout = 0, x = 0;
134         char *argcopy = NULL, *status = "";
135         struct ind_tone_zone_sound *ts = NULL;
136         struct ast_flags flags = {0};
137
138          AST_DECLARE_APP_ARGS(arglist,
139                 AST_APP_ARG(variable);
140                 AST_APP_ARG(filename);
141                 AST_APP_ARG(context);
142                 AST_APP_ARG(options);
143                 AST_APP_ARG(timeout);
144         );
145         
146         if (ast_strlen_zero(data)) {
147                 ast_log(LOG_WARNING, "ReadExten requires at least one argument\n");
148                 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
149                 return 0;
150         }
151
152         argcopy = ast_strdupa(data);
153         AST_STANDARD_APP_ARGS(arglist, argcopy);
154
155         if (ast_strlen_zero(arglist.variable)) {
156                 ast_log(LOG_WARNING, "Usage: ReadExten(variable[,filename[,context[,options[,timeout]]]])\n");
157                 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
158                 return 0;
159         }
160
161         if (ast_strlen_zero(arglist.filename))
162                 arglist.filename = NULL;
163
164         if (ast_strlen_zero(arglist.context))
165                 arglist.context = chan->context;
166
167         if (!ast_strlen_zero(arglist.options))
168                 ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options);
169
170         if (!ast_strlen_zero(arglist.timeout)) {
171                 timeout = atoi(arglist.timeout);
172                 if (timeout > 0)
173                         timeout *= 1000;
174         }
175
176         if (timeout <= 0)
177                 timeout = chan->pbx ? chan->pbx->rtimeoutms : 10000;
178
179         if (digit_timeout <= 0)
180                 digit_timeout = chan->pbx ? chan->pbx->dtimeoutms : 5000;
181
182         if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename))
183                 ts = ast_get_indication_tone(chan->zone, arglist.filename);
184
185         do {
186                 if (chan->_state != AST_STATE_UP) {
187                         if (ast_test_flag(&flags, OPT_SKIP)) {
188                                 /* At the user's option, skip if the line is not up */
189                                 pbx_builtin_setvar_helper(chan, arglist.variable, "");
190                                 status = "SKIP";
191                                 break;
192                         } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
193                                 /* Otherwise answer unless we're supposed to read while on-hook */
194                                 res = ast_answer(chan);
195                         }
196                 }
197
198                 if (res < 0) {
199                         status = "HANGUP";
200                         break;
201                 }
202
203                 ast_playtones_stop(chan);
204                 ast_stopstream(chan);
205
206                 if (ts && ts->data[0])
207                         res = ast_playtones_start(chan, 0, ts->data, 0);
208                 else if (arglist.filename)
209                         res = ast_streamfile(chan, arglist.filename, chan->language);
210
211                 for (x = 0; x < maxdigits; x++) {
212                         ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout);
213                         res = ast_waitfordigit(chan, timeout);
214
215                         ast_playtones_stop(chan);
216                         ast_stopstream(chan);
217                         timeout = digit_timeout;
218
219                         if (res < 1) {          /* timeout expired or hangup */
220                                 if (ast_check_hangup(chan)) {
221                                         status = "HANGUP";
222                                 } else {
223                                         pbx_builtin_setvar_helper(chan, arglist.variable, "t");
224                                         status = "TIMEOUT";
225                                 }
226                                 break;
227                         }
228
229                         exten[x] = res;
230                         if (!ast_matchmore_extension(chan, arglist.context, exten, 1 /* priority */, chan->cid.cid_num)) {
231                                 if (!ast_exists_extension(chan, arglist.context, exten, 1, chan->cid.cid_num) && res == '#') {
232                                         exten[x] = '\0';
233                                 }
234                                 break;
235                         }
236                 }
237
238                 if (!ast_strlen_zero(status))
239                         break;
240
241                 if (ast_exists_extension(chan, arglist.context, exten, 1, chan->cid.cid_num)) {
242                         ast_debug(3, "User entered valid extension '%s'\n", exten);
243                         pbx_builtin_setvar_helper(chan, arglist.variable, exten);
244                         status = "OK";
245                 } else {
246                         ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, chan->name);
247                         pbx_builtin_setvar_helper(chan, arglist.variable, "i");
248                         pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
249                         status = "INVALID";
250                 }
251         } while (0);
252
253         pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
254
255         return status[0] == 'H' ? -1 : 0;
256 }
257
258 static int acf_isexten_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
259 {
260         int priority_int;
261         AST_DECLARE_APP_ARGS(args,
262                 AST_APP_ARG(context);
263                 AST_APP_ARG(extension);
264                 AST_APP_ARG(priority);
265         );
266
267         AST_STANDARD_APP_ARGS(args, parse);
268
269         if (ast_strlen_zero(args.context))
270                 args.context = chan->context;
271
272         if (ast_strlen_zero(args.extension)) {
273                 ast_log(LOG_WARNING, "Syntax: VALID_EXTEN([<context>],<extension>[,<priority>]) - missing argument <extension>!\n");
274                 return -1;
275         }
276
277         if (ast_strlen_zero(args.priority))
278                 priority_int = 1;
279         else
280                 priority_int = atoi(args.priority);
281
282         if (ast_exists_extension(chan, args.context, args.extension, priority_int, chan->cid.cid_num))
283             ast_copy_string(buffer, "1", buflen);
284         else
285             ast_copy_string(buffer, "0", buflen);
286
287         return 0;
288 }
289
290 static struct ast_custom_function acf_isexten = {
291         .name = "VALID_EXTEN",
292         .read = acf_isexten_exec,
293 };
294
295 static int unload_module(void)
296 {
297         int res = ast_unregister_application(app);
298         res |= ast_custom_function_unregister(&acf_isexten);
299
300         return res;     
301 }
302
303 static int load_module(void)
304 {
305         int res = ast_register_application_xml(app, readexten_exec);
306         res |= ast_custom_function_register(&acf_isexten);
307         return res;
308 }
309
310 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");