2 * Asterisk -- A telephony toolkit for Linux.
4 * Trivial application to read a variable
6 * Copyright (C) 2003, Digium
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/app.h>
20 #include <asterisk/module.h>
21 #include <asterisk/translate.h>
22 #include <asterisk/options.h>
23 #include <asterisk/utils.h>
27 static char *tdesc = "Read Variable Application";
29 static char *app = "Read";
31 static char *synopsis = "Read a variable";
33 static char *descrip =
34 " Read(variable[|filename][|maxdigits][|option][|attempts][|timeout])\n\n"
35 "Reads a #-terminated string of digits a certain number of times from the\n"
36 "user in to the given variable.\n"
37 " filename -- file to play before reading digits.\n"
38 " maxdigits -- maximum acceptable number of digits. Stops reading after\n"
39 " maxdigits have been entered (without requiring the user to\n"
40 " press the '#' key).\n"
41 " Defaults to 0 - no limit - wait for the user press the '#' key.\n"
42 " Any value below 0 means the same. Max accepted value is 255.\n"
43 " option -- may be 'skip' to return immediately if the line is not up,\n"
44 " or 'noanswer' to read digits even if the line is not up.\n"
45 " attempts -- if greater than 1, that many attempts will be made in the \n"
46 " event no data is entered.\n"
47 " timeout -- if greater than 0, that value will override the default timeoft.\n\n"
48 "Returns -1 on hangup or error and 0 otherwise.\n";
54 #define ast_next_data(instr,ptr,delim) if((ptr=strchr(instr,delim))) { *(ptr) = '\0' ; ptr++;}
56 static int read_exec(struct ast_channel *chan, void *data)
63 char *filename = NULL;
65 char *maxdigitstr=NULL;
68 int option_noanswer = 0;
77 argcopy = ast_strdupa((char *)data);
79 if (ast_seperate_app_args(argcopy, '|', args, sizeof(args) / sizeof(args[0])) < 1) {
80 ast_log(LOG_WARNING, "Cannot Parse Arguements.\n");
86 maxdigitstr = args[x++];
91 if (!strcasecmp(options, "skip"))
93 else if (!strcasecmp(options, "noanswer"))
96 if (strchr(options, 's'))
98 if (strchr(options, 'n'))
115 if (!(filename) || ast_strlen_zero(filename))
118 maxdigits = atoi(maxdigitstr);
119 if ((maxdigits<1) || (maxdigits>255)) {
121 } else if (option_verbose > 2)
122 ast_verbose(VERBOSE_PREFIX_3 "Accepting a maximum of %i digits.\n", maxdigits);
124 if (!(varname) || ast_strlen_zero(varname)) {
125 ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[|filename][|maxdigits][|option][|attempts][|timeout])\n\n");
129 if (chan->_state != AST_STATE_UP) {
131 /* At the user's option, skip if the line is not up */
132 pbx_builtin_setvar_helper(chan, varname, "\0");
133 LOCAL_USER_REMOVE(u);
135 } else if (!option_noanswer) {
136 /* Otherwise answer unless we're supposed to read while on-hook */
137 res = ast_answer(chan);
141 while(tries && !res) {
142 ast_stopstream(chan);
143 res = ast_app_getdata(chan, filename, tmp, maxdigits, 0);
145 pbx_builtin_setvar_helper(chan, varname, tmp);
146 if (!ast_strlen_zero(tmp)) {
147 if (option_verbose > 2)
148 ast_verbose(VERBOSE_PREFIX_3 "User entered '%s'\n", tmp);
152 if (option_verbose > 2) {
154 ast_verbose(VERBOSE_PREFIX_3 "User entered nothing, %d chance(s) left\n", tries);
156 ast_verbose(VERBOSE_PREFIX_3 "User entered nothing.\n");
161 if (option_verbose > 2)
162 ast_verbose(VERBOSE_PREFIX_3 "User disconnected\n");
166 LOCAL_USER_REMOVE(u);
170 int unload_module(void)
172 STANDARD_HANGUP_LOCALUSERS;
173 return ast_unregister_application(app);
176 int load_module(void)
178 return ast_register_application(app, read_exec, synopsis, descrip);
181 char *description(void)
189 STANDARD_USECOUNT(res);
195 return ASTERISK_GPL_KEY;