Asterisk - The Open Source Telephony Project GIT-master-f36a736
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 <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 <option name="p">
67 <para>The extension entered will be considered complete when a <literal>#</literal>
68 is entered.</para>
69 </option>
70 </optionlist>
71 </parameter>
72 <parameter name="timeout">
73 <para>An integer number of seconds to wait for a digit response. If
74 greater than <literal>0</literal>, that value will override the default timeout.</para>
75 </parameter>
76 </syntax>
77 <description>
78 <para>Reads a <literal>#</literal> terminated string of digits from the user into the given variable.</para>
79 <para>Will set READEXTENSTATUS on exit with one of the following statuses:</para>
80 <variablelist>
81 <variable name="READEXTENSTATUS">
82 <value name="OK">
83 A valid extension exists in ${variable}.
84 </value>
85 <value name="TIMEOUT">
86 No extension was entered in the specified time. Also sets ${variable} to "t".
87 </value>
88 <value name="INVALID">
89 An invalid extension, ${INVALID_EXTEN}, was entered. Also sets ${variable} to "i".
90 </value>
91 <value name="SKIP">
92 Line was not up and the option 's' was specified.
93 </value>
94 <value name="ERROR">
95 Invalid arguments were passed.
96 </value>
97 </variable>
98 </variablelist>
99 </description>
100 </application>
101 ***/
102
104 OPT_SKIP = (1 << 0),
105 OPT_INDICATION = (1 << 1),
106 OPT_NOANSWER = (1 << 2),
108};
109
115});
116
117static char *app = "ReadExten";
118
119static int readexten_exec(struct ast_channel *chan, const char *data)
120{
121 int res = 0;
122 char exten[256] = "";
123 int maxdigits = sizeof(exten) - 1;
124 int timeout = 0, digit_timeout = 0, x = 0;
125 char *argcopy = NULL, *status = "";
126 struct ast_tone_zone_sound *ts = NULL;
127 struct ast_flags flags = {0};
128
129 AST_DECLARE_APP_ARGS(arglist,
130 AST_APP_ARG(variable);
131 AST_APP_ARG(filename);
134 AST_APP_ARG(timeout);
135 );
136
137 if (ast_strlen_zero(data)) {
138 ast_log(LOG_WARNING, "ReadExten requires at least one argument\n");
139 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
140 return 0;
141 }
142
143 argcopy = ast_strdupa(data);
144 AST_STANDARD_APP_ARGS(arglist, argcopy);
145
146 if (ast_strlen_zero(arglist.variable)) {
147 ast_log(LOG_WARNING, "Usage: ReadExten(variable[,filename[,context[,options[,timeout]]]])\n");
148 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
149 return 0;
150 }
151
152 if (ast_strlen_zero(arglist.filename)) {
153 arglist.filename = NULL;
154 }
155
156 if (ast_strlen_zero(arglist.context)) {
157 arglist.context = ast_strdupa(ast_channel_context(chan));
158 }
159
160 if (!ast_strlen_zero(arglist.options)) {
162 }
163
164 if (!ast_strlen_zero(arglist.timeout)) {
165 timeout = atoi(arglist.timeout);
166 if (timeout > 0)
167 timeout *= 1000;
168 }
169
170 if (timeout <= 0)
171 timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 10000;
172
173 digit_timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->dtimeoutms : 5000;
174
175 if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename)) {
176 ts = ast_get_indication_tone(ast_channel_zone(chan), arglist.filename);
177 }
178
179 do {
180 if (ast_channel_state(chan) != AST_STATE_UP) {
182 /* At the user's option, skip if the line is not up */
183 pbx_builtin_setvar_helper(chan, arglist.variable, "");
184 status = "SKIP";
185 break;
186 } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
187 /* Otherwise answer unless we're supposed to read while on-hook */
188 res = ast_answer(chan);
189 }
190 }
191
192 if (res < 0) {
193 status = "HANGUP";
194 break;
195 }
196
197 ast_playtones_stop(chan);
198 ast_stopstream(chan);
199
200 if (ts && ts->data[0]) {
201 res = ast_playtones_start(chan, 0, ts->data, 0);
202 } else if (arglist.filename) {
203 if (ast_test_flag(&flags, OPT_INDICATION) && ast_fileexists(arglist.filename, NULL, ast_channel_language(chan)) <= 0) {
204 /*
205 * We were asked to play an indication that did not exist in the config.
206 * If no such file exists, play it as a tonelist. With any luck they won't
207 * have a file named "350+440.ulaw"
208 * (but honestly, who would do something so silly?)
209 */
210 res = ast_playtones_start(chan, 0, arglist.filename, 0);
211 } else {
212 res = ast_streamfile(chan, arglist.filename, ast_channel_language(chan));
213 }
214 }
215
216 for (x = 0; x < maxdigits; x++) {
217 ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout);
218 res = ast_waitfordigit(chan, timeout);
219
220 ast_playtones_stop(chan);
221 ast_stopstream(chan);
222 timeout = digit_timeout;
223
224 if (res < 1) { /* timeout expired or hangup */
225 if (ast_check_hangup(chan)) {
226 status = "HANGUP";
227 } else if (x == 0) {
228 pbx_builtin_setvar_helper(chan, arglist.variable, "t");
229 status = "TIMEOUT";
230 }
231 break;
232 }
233
234 if (ast_test_flag(&flags, OPT_POUND_TO_END) && res == '#') {
235 exten[x] = 0;
236 break;
237 }
238
239 exten[x] = res;
240 if (!ast_matchmore_extension(chan, arglist.context, exten, 1 /* priority */,
241 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
242 if (!ast_exists_extension(chan, arglist.context, exten, 1,
243 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))
244 && res == '#') {
245 exten[x] = '\0';
246 }
247 break;
248 }
249 }
250
252 break;
253
254 if (ast_exists_extension(chan, arglist.context, exten, 1,
255 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
256 ast_debug(3, "User entered valid extension '%s'\n", exten);
257 pbx_builtin_setvar_helper(chan, arglist.variable, exten);
258 status = "OK";
259 } else {
260 ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, ast_channel_name(chan));
261 pbx_builtin_setvar_helper(chan, arglist.variable, "i");
262 pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
263 status = "INVALID";
264 }
265 } while (0);
266
267 if (ts) {
269 }
270
271 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
272
273 return status[0] == 'H' ? -1 : 0;
274}
275
276static int unload_module(void)
277{
279 return res;
280}
281
282static int load_module(void)
283{
285 return res;
286}
287
288AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");
jack_status_t status
Definition: app_jack.c:146
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:3194
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:2824
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:1293
int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
Checks for the existence of a given file.
Definition: file.c:1129
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:4175
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:4195
#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:216
int dtimeoutms
Definition: pbx.h:215
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:154
static struct test_options options
#define ast_test_flag(p, flag)
Definition: utils.h:63