Merge "astobj2: Create function to copy weak proxied objects from container."
[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 #include "asterisk/file.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/app.h"
36 #include "asterisk/module.h"
37 #include "asterisk/indications.h"
38 #include "asterisk/channel.h"
39
40 /*** DOCUMENTATION
41         <application name="ReadExten" language="en_US">
42                 <synopsis>
43                         Read an extension into a variable.
44                 </synopsis>
45                 <syntax>
46                         <parameter name="variable" required="true" />
47                         <parameter name="filename">
48                                 <para>File to play before reading digits or tone with option <literal>i</literal></para>
49                         </parameter>
50                         <parameter name="context">
51                                 <para>Context in which to match extensions.</para>
52                         </parameter>
53                         <parameter name="option">
54                                 <optionlist>
55                                         <option name="s">
56                                                 <para>Return immediately if the channel is not answered.</para>
57                                         </option>
58                                         <option name="i">
59                                                 <para>Play <replaceable>filename</replaceable> as an indication tone from your
60                                                 <filename>indications.conf</filename> or a directly specified list of
61                                                 frequencies and durations.</para>
62                                         </option>
63                                         <option name="n">
64                                                 <para>Read digits even if the channel is not answered.</para>
65                                         </option>
66                                 </optionlist>
67                         </parameter>
68                         <parameter name="timeout">
69                                 <para>An integer number of seconds to wait for a digit response. If
70                                 greater than <literal>0</literal>, that value will override the default timeout.</para>
71                         </parameter>
72                 </syntax>
73                 <description>
74                         <para>Reads a <literal>#</literal> terminated string of digits from the user into the given variable.</para>
75                         <para>Will set READEXTENSTATUS on exit with one of the following statuses:</para>
76                         <variablelist>
77                                 <variable name="READEXTENSTATUS">
78                                         <value name="OK">
79                                                 A valid extension exists in ${variable}.
80                                         </value>
81                                         <value name="TIMEOUT">
82                                                 No extension was entered in the specified time.  Also sets ${variable} to "t".
83                                         </value>
84                                         <value name="INVALID">
85                                                 An invalid extension, ${INVALID_EXTEN}, was entered.  Also sets ${variable} to "i".
86                                         </value>
87                                         <value name="SKIP">
88                                                 Line was not up and the option 's' was specified.
89                                         </value>
90                                         <value name="ERROR">
91                                                 Invalid arguments were passed.
92                                         </value>
93                                 </variable>
94                         </variablelist>
95                 </description>
96         </application>
97  ***/
98
99 enum readexten_option_flags {
100         OPT_SKIP = (1 << 0),
101         OPT_INDICATION = (1 << 1),
102         OPT_NOANSWER = (1 << 2),
103 };
104
105 AST_APP_OPTIONS(readexten_app_options, {
106         AST_APP_OPTION('s', OPT_SKIP),
107         AST_APP_OPTION('i', OPT_INDICATION),
108         AST_APP_OPTION('n', OPT_NOANSWER),
109 });
110
111 static char *app = "ReadExten";
112
113 static int readexten_exec(struct ast_channel *chan, const char *data)
114 {
115         int res = 0;
116         char exten[256] = "";
117         int maxdigits = sizeof(exten) - 1;
118         int timeout = 0, digit_timeout = 0, x = 0;
119         char *argcopy = NULL, *status = "";
120         struct ast_tone_zone_sound *ts = NULL;
121         struct ast_flags flags = {0};
122
123          AST_DECLARE_APP_ARGS(arglist,
124                 AST_APP_ARG(variable);
125                 AST_APP_ARG(filename);
126                 AST_APP_ARG(context);
127                 AST_APP_ARG(options);
128                 AST_APP_ARG(timeout);
129         );
130
131         if (ast_strlen_zero(data)) {
132                 ast_log(LOG_WARNING, "ReadExten requires at least one argument\n");
133                 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
134                 return 0;
135         }
136
137         argcopy = ast_strdupa(data);
138         AST_STANDARD_APP_ARGS(arglist, argcopy);
139
140         if (ast_strlen_zero(arglist.variable)) {
141                 ast_log(LOG_WARNING, "Usage: ReadExten(variable[,filename[,context[,options[,timeout]]]])\n");
142                 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
143                 return 0;
144         }
145
146         if (ast_strlen_zero(arglist.filename)) {
147                 arglist.filename = NULL;
148         }
149
150         if (ast_strlen_zero(arglist.context)) {
151                 arglist.context = ast_strdupa(ast_channel_context(chan));
152         }
153
154         if (!ast_strlen_zero(arglist.options)) {
155                 ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options);
156         }
157
158         if (!ast_strlen_zero(arglist.timeout)) {
159                 timeout = atoi(arglist.timeout);
160                 if (timeout > 0)
161                         timeout *= 1000;
162         }
163
164         if (timeout <= 0)
165                 timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 10000;
166
167         if (digit_timeout <= 0)
168                 digit_timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->dtimeoutms : 5000;
169
170         if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename)) {
171                 ts = ast_get_indication_tone(ast_channel_zone(chan), arglist.filename);
172         }
173
174         do {
175                 if (ast_channel_state(chan) != AST_STATE_UP) {
176                         if (ast_test_flag(&flags, OPT_SKIP)) {
177                                 /* At the user's option, skip if the line is not up */
178                                 pbx_builtin_setvar_helper(chan, arglist.variable, "");
179                                 status = "SKIP";
180                                 break;
181                         } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
182                                 /* Otherwise answer unless we're supposed to read while on-hook */
183                                 res = ast_answer(chan);
184                         }
185                 }
186
187                 if (res < 0) {
188                         status = "HANGUP";
189                         break;
190                 }
191
192                 ast_playtones_stop(chan);
193                 ast_stopstream(chan);
194
195                 if (ts && ts->data[0]) {
196                         res = ast_playtones_start(chan, 0, ts->data, 0);
197                 } else if (arglist.filename) {
198                         if (ast_test_flag(&flags, OPT_INDICATION) && ast_fileexists(arglist.filename, NULL, ast_channel_language(chan)) <= 0) {
199                                 /*
200                                  * We were asked to play an indication that did not exist in the config.
201                                  * If no such file exists, play it as a tonelist.  With any luck they won't
202                                  * have a file named "350+440.ulaw"
203                                  * (but honestly, who would do something so silly?)
204                                  */
205                                 res = ast_playtones_start(chan, 0, arglist.filename, 0);
206                         } else {
207                                 res = ast_streamfile(chan, arglist.filename, ast_channel_language(chan));
208                         }
209                 }
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 if (x == 0) {
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 */,
231                                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
232                                 if (!ast_exists_extension(chan, arglist.context, exten, 1,
233                                         S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))
234                                         && res == '#') {
235                                         exten[x] = '\0';
236                                 }
237                                 break;
238                         }
239                 }
240
241                 if (!ast_strlen_zero(status))
242                         break;
243
244                 if (ast_exists_extension(chan, arglist.context, exten, 1,
245                         S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
246                         ast_debug(3, "User entered valid extension '%s'\n", exten);
247                         pbx_builtin_setvar_helper(chan, arglist.variable, exten);
248                         status = "OK";
249                 } else {
250                         ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, ast_channel_name(chan));
251                         pbx_builtin_setvar_helper(chan, arglist.variable, "i");
252                         pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
253                         status = "INVALID";
254                 }
255         } while (0);
256
257         if (ts) {
258                 ts = ast_tone_zone_sound_unref(ts);
259         }
260
261         pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
262
263         return status[0] == 'H' ? -1 : 0;
264 }
265
266 static int unload_module(void)
267 {
268         int res = ast_unregister_application(app);
269         return res;
270 }
271
272 static int load_module(void)
273 {
274         int res = ast_register_application_xml(app, readexten_exec);
275         return res;
276 }
277
278 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");