Debian Codegraph @

apps/app_readexten.c

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