Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
app_readexten.c
Go to the documentation of this file.
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"
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
107 OPT_SKIP = (1 << 0),
108 OPT_INDICATION = (1 << 1),
109 OPT_NOANSWER = (1 << 2),
111};
112
118});
119
120static char *app = "ReadExten";
121
122static 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);
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)) {
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) {
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
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) {
272 }
273
274 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
275
276 return status[0] == 'H' ? -1 : 0;
277}
278
279static int unload_module(void)
280{
282 return res;
283}
284
285static int load_module(void)
286{
288 return res;
289}
290
291AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");
jack_status_t status
Definition: app_jack.c:149
static const struct ast_app_option readexten_app_options[128]
readexten_option_flags
@ OPT_NOANSWER
@ OPT_INDICATION
@ OPT_SKIP
@ OPT_POUND_TO_END
static char * app
static int readexten_exec(struct ast_channel *chan, const char *data)
static int load_module(void)
static int unload_module(void)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3203
const char * ast_channel_name(const struct ast_channel *chan)
const char * ast_channel_context(const struct ast_channel *chan)
int ast_check_hangup(struct ast_channel *chan)
Check to see if a channel is needing hang up.
Definition: channel.c:445
struct ast_tone_zone * ast_channel_zone(const struct ast_channel *chan)
const char * ast_channel_language(const struct ast_channel *chan)
struct ast_pbx * ast_channel_pbx(const struct ast_channel *chan)
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2834
ast_channel_state
ast_channel states
Definition: channelstate.h:35
@ AST_STATE_UP
Definition: channelstate.h:42
Generic File Format Support. Should be included by clients of the file handling routines....
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:222
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1301
int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
Checks for the existence of a given file.
Definition: file.c:1137
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_APP_OPTIONS(holder, options...)
Declares an array of options for an application.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define AST_APP_OPTION(option, flagno)
Declares an application option that does not accept an argument.
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:3066
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_WARNING
Tone Indication Support.
static struct ast_tone_zone_sound * ast_tone_zone_sound_unref(struct ast_tone_zone_sound *ts)
Release a reference to an ast_tone_zone_sound.
Definition: indications.h:227
int ast_playtones_start(struct ast_channel *chan, int vol, const char *tonelist, int interruptible)
Start playing a list of tones on a channel.
Definition: indications.c:302
void ast_playtones_stop(struct ast_channel *chan)
Stop playing tones on a channel.
Definition: indications.c:393
struct ast_tone_zone_sound * ast_get_indication_tone(const struct ast_tone_zone *zone, const char *indication)
Locate a tone zone sound.
Definition: indications.c:461
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
Core PBX routines and definitions.
int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Determine whether an extension exists.
Definition: pbx.c:4190
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
int ast_matchmore_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Looks to see if adding anything to this extension might match something. (exists ^ canmatch)
Definition: pbx.c:4210
#define NULL
Definition: resample.c:96
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
Definition: strings.h:87
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Main Channel structure associated with a channel.
char context[AST_MAX_CONTEXT]
Structure used to handle boolean flags.
Definition: utils.h:199
unsigned int flags
Definition: utils.h:200
int rtimeoutms
Definition: pbx.h:217
int dtimeoutms
Definition: pbx.h:216
Description of a tone.
Definition: indications.h:35
const char * data
Description of a tone.
Definition: indications.h:52
Number structure.
Definition: app_followme.c:157
static struct test_options options
#define ast_test_flag(p, flag)
Definition: utils.h:63