git migration: Refactor the ASTERISK_FILE_VERSION macro
[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_REGISTER_FILE()
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
152         if (ast_strlen_zero(arglist.context)) {
153                 arglist.context = ast_strdupa(ast_channel_context(chan));
154         }
155
156         if (!ast_strlen_zero(arglist.options)) {
157                 ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options);
158         }
159
160         if (!ast_strlen_zero(arglist.timeout)) {
161                 timeout = atoi(arglist.timeout);
162                 if (timeout > 0)
163                         timeout *= 1000;
164         }
165
166         if (timeout <= 0)
167                 timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 10000;
168
169         if (digit_timeout <= 0)
170                 digit_timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->dtimeoutms : 5000;
171
172         if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename)) {
173                 ts = ast_get_indication_tone(ast_channel_zone(chan), arglist.filename);
174         }
175
176         do {
177                 if (ast_channel_state(chan) != AST_STATE_UP) {
178                         if (ast_test_flag(&flags, OPT_SKIP)) {
179                                 /* At the user's option, skip if the line is not up */
180                                 pbx_builtin_setvar_helper(chan, arglist.variable, "");
181                                 status = "SKIP";
182                                 break;
183                         } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
184                                 /* Otherwise answer unless we're supposed to read while on-hook */
185                                 res = ast_answer(chan);
186                         }
187                 }
188
189                 if (res < 0) {
190                         status = "HANGUP";
191                         break;
192                 }
193
194                 ast_playtones_stop(chan);
195                 ast_stopstream(chan);
196
197                 if (ts && ts->data[0]) {
198                         res = ast_playtones_start(chan, 0, ts->data, 0);
199                 } else if (arglist.filename) {
200                         if (ast_test_flag(&flags, OPT_INDICATION) && ast_fileexists(arglist.filename, NULL, ast_channel_language(chan)) <= 0) {
201                                 /*
202                                  * We were asked to play an indication that did not exist in the config.
203                                  * If no such file exists, play it as a tonelist.  With any luck they won't
204                                  * have a file named "350+440.ulaw"
205                                  * (but honestly, who would do something so silly?)
206                                  */
207                                 res = ast_playtones_start(chan, 0, arglist.filename, 0);
208                         } else {
209                                 res = ast_streamfile(chan, arglist.filename, ast_channel_language(chan));
210                         }
211                 }
212
213                 for (x = 0; x < maxdigits; x++) {
214                         ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout);
215                         res = ast_waitfordigit(chan, timeout);
216
217                         ast_playtones_stop(chan);
218                         ast_stopstream(chan);
219                         timeout = digit_timeout;
220
221                         if (res < 1) {          /* timeout expired or hangup */
222                                 if (ast_check_hangup(chan)) {
223                                         status = "HANGUP";
224                                 } else if (x == 0) {
225                                         pbx_builtin_setvar_helper(chan, arglist.variable, "t");
226                                         status = "TIMEOUT";
227                                 }
228                                 break;
229                         }
230
231                         exten[x] = res;
232                         if (!ast_matchmore_extension(chan, arglist.context, exten, 1 /* priority */,
233                                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
234                                 if (!ast_exists_extension(chan, arglist.context, exten, 1,
235                                         S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))
236                                         && res == '#') {
237                                         exten[x] = '\0';
238                                 }
239                                 break;
240                         }
241                 }
242
243                 if (!ast_strlen_zero(status))
244                         break;
245
246                 if (ast_exists_extension(chan, arglist.context, exten, 1,
247                         S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
248                         ast_debug(3, "User entered valid extension '%s'\n", exten);
249                         pbx_builtin_setvar_helper(chan, arglist.variable, exten);
250                         status = "OK";
251                 } else {
252                         ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, ast_channel_name(chan));
253                         pbx_builtin_setvar_helper(chan, arglist.variable, "i");
254                         pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
255                         status = "INVALID";
256                 }
257         } while (0);
258
259         if (ts) {
260                 ts = ast_tone_zone_sound_unref(ts);
261         }
262
263         pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
264
265         return status[0] == 'H' ? -1 : 0;
266 }
267
268 static int unload_module(void)
269 {
270         int res = ast_unregister_application(app);
271         return res;
272 }
273
274 static int load_module(void)
275 {
276         int res = ast_register_application_xml(app, readexten_exec);
277         return res;
278 }
279
280 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");