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