94dc1e1f87f3fd28416eeac285cd0c12414878d7
[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         <function name="VALID_EXTEN" language="en_US">
100                 <synopsis>
101                         Determine whether an extension exists or not.
102                 </synopsis>
103                 <syntax>
104                         <parameter name="context">
105                                 <para>Defaults to the current context</para>
106                         </parameter>
107                         <parameter name="extension" required="true" />
108                         <parameter name="priority">
109                                 <para>Priority defaults to <literal>1</literal>.</para>
110                         </parameter>
111                 </syntax>
112                 <description>
113                         <para>Returns a true value if the indicated <replaceable>context</replaceable>,
114                         <replaceable>extension</replaceable>, and <replaceable>priority</replaceable> exist.</para>
115                 </description>
116         </function>
117  ***/
118
119 enum readexten_option_flags {
120         OPT_SKIP = (1 << 0),
121         OPT_INDICATION = (1 << 1),
122         OPT_NOANSWER = (1 << 2),
123 };
124
125 AST_APP_OPTIONS(readexten_app_options, {
126         AST_APP_OPTION('s', OPT_SKIP),
127         AST_APP_OPTION('i', OPT_INDICATION),
128         AST_APP_OPTION('n', OPT_NOANSWER),
129 });
130
131 static char *app = "ReadExten";
132
133 static int readexten_exec(struct ast_channel *chan, const char *data)
134 {
135         int res = 0;
136         char exten[256] = "";
137         int maxdigits = sizeof(exten) - 1;
138         int timeout = 0, digit_timeout = 0, x = 0;
139         char *argcopy = NULL, *status = "";
140         struct ast_tone_zone_sound *ts = NULL;
141         struct ast_flags flags = {0};
142
143          AST_DECLARE_APP_ARGS(arglist,
144                 AST_APP_ARG(variable);
145                 AST_APP_ARG(filename);
146                 AST_APP_ARG(context);
147                 AST_APP_ARG(options);
148                 AST_APP_ARG(timeout);
149         );
150         
151         if (ast_strlen_zero(data)) {
152                 ast_log(LOG_WARNING, "ReadExten requires at least one argument\n");
153                 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
154                 return 0;
155         }
156
157         argcopy = ast_strdupa(data);
158         AST_STANDARD_APP_ARGS(arglist, argcopy);
159
160         if (ast_strlen_zero(arglist.variable)) {
161                 ast_log(LOG_WARNING, "Usage: ReadExten(variable[,filename[,context[,options[,timeout]]]])\n");
162                 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
163                 return 0;
164         }
165
166         if (ast_strlen_zero(arglist.filename))
167                 arglist.filename = NULL;
168
169         if (ast_strlen_zero(arglist.context))
170                 arglist.context = chan->context;
171
172         if (!ast_strlen_zero(arglist.options))
173                 ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options);
174
175         if (!ast_strlen_zero(arglist.timeout)) {
176                 timeout = atoi(arglist.timeout);
177                 if (timeout > 0)
178                         timeout *= 1000;
179         }
180
181         if (timeout <= 0)
182                 timeout = chan->pbx ? chan->pbx->rtimeoutms : 10000;
183
184         if (digit_timeout <= 0)
185                 digit_timeout = chan->pbx ? chan->pbx->dtimeoutms : 5000;
186
187         if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename)) {
188                 ts = ast_get_indication_tone(chan->zone, arglist.filename);
189         }
190
191         do {
192                 if (chan->_state != AST_STATE_UP) {
193                         if (ast_test_flag(&flags, OPT_SKIP)) {
194                                 /* At the user's option, skip if the line is not up */
195                                 pbx_builtin_setvar_helper(chan, arglist.variable, "");
196                                 status = "SKIP";
197                                 break;
198                         } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
199                                 /* Otherwise answer unless we're supposed to read while on-hook */
200                                 res = ast_answer(chan);
201                         }
202                 }
203
204                 if (res < 0) {
205                         status = "HANGUP";
206                         break;
207                 }
208
209                 ast_playtones_stop(chan);
210                 ast_stopstream(chan);
211
212                 if (ts && ts->data[0]) {
213                         res = ast_playtones_start(chan, 0, ts->data, 0);
214                 } else if (arglist.filename) {
215                         if (ast_test_flag(&flags, OPT_INDICATION) && ast_fileexists(arglist.filename, NULL, chan->language) <= 0) {
216                                 /*
217                                  * We were asked to play an indication that did not exist in the config.
218                                  * If no such file exists, play it as a tonelist.  With any luck they won't
219                                  * have a file named "350+440.ulaw"
220                                  * (but honestly, who would do something so silly?)
221                                  */
222                                 res = ast_playtones_start(chan, 0, arglist.filename, 0);
223                         } else {
224                                 res = ast_streamfile(chan, arglist.filename, chan->language);
225                         }
226                 }
227
228                 for (x = 0; x < maxdigits; x++) {
229                         ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout);
230                         res = ast_waitfordigit(chan, timeout);
231
232                         ast_playtones_stop(chan);
233                         ast_stopstream(chan);
234                         timeout = digit_timeout;
235
236                         if (res < 1) {          /* timeout expired or hangup */
237                                 if (ast_check_hangup(chan)) {
238                                         status = "HANGUP";
239                                 } else if (x == 0) {
240                                         pbx_builtin_setvar_helper(chan, arglist.variable, "t");
241                                         status = "TIMEOUT";
242                                 }
243                                 break;
244                         }
245
246                         exten[x] = res;
247                         if (!ast_matchmore_extension(chan, arglist.context, exten, 1 /* priority */,
248                                 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
249                                 if (!ast_exists_extension(chan, arglist.context, exten, 1,
250                                         S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))
251                                         && res == '#') {
252                                         exten[x] = '\0';
253                                 }
254                                 break;
255                         }
256                 }
257
258                 if (!ast_strlen_zero(status))
259                         break;
260
261                 if (ast_exists_extension(chan, arglist.context, exten, 1,
262                         S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
263                         ast_debug(3, "User entered valid extension '%s'\n", exten);
264                         pbx_builtin_setvar_helper(chan, arglist.variable, exten);
265                         status = "OK";
266                 } else {
267                         ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, chan->name);
268                         pbx_builtin_setvar_helper(chan, arglist.variable, "i");
269                         pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
270                         status = "INVALID";
271                 }
272         } while (0);
273
274         if (ts) {
275                 ts = ast_tone_zone_sound_unref(ts);
276         }
277
278         pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
279
280         return status[0] == 'H' ? -1 : 0;
281 }
282
283 static int acf_isexten_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
284 {
285         int priority_int;
286         AST_DECLARE_APP_ARGS(args,
287                 AST_APP_ARG(context);
288                 AST_APP_ARG(extension);
289                 AST_APP_ARG(priority);
290         );
291
292         AST_STANDARD_APP_ARGS(args, parse);
293
294         if (ast_strlen_zero(args.context))
295                 args.context = chan->context;
296
297         if (ast_strlen_zero(args.extension)) {
298                 ast_log(LOG_WARNING, "Syntax: VALID_EXTEN([<context>],<extension>[,<priority>]) - missing argument <extension>!\n");
299                 return -1;
300         }
301
302         if (ast_strlen_zero(args.priority))
303                 priority_int = 1;
304         else
305                 priority_int = atoi(args.priority);
306
307         if (ast_exists_extension(chan, args.context, args.extension, priority_int,
308                 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
309             ast_copy_string(buffer, "1", buflen);
310         } else {
311             ast_copy_string(buffer, "0", buflen);
312         }
313
314         return 0;
315 }
316
317 static struct ast_custom_function acf_isexten = {
318         .name = "VALID_EXTEN",
319         .read = acf_isexten_exec,
320 };
321
322 static int unload_module(void)
323 {
324         int res = ast_unregister_application(app);
325         res |= ast_custom_function_unregister(&acf_isexten);
326
327         return res;     
328 }
329
330 static int load_module(void)
331 {
332         int res = ast_register_application_xml(app, readexten_exec);
333         res |= ast_custom_function_register(&acf_isexten);
334         return res;
335 }
336
337 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");