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])\n\n"
35 "Reads a #-terminated string of digits from the user in to the given variable,\n"
36 "optionally playing a given filename first.\n"
37 " maxdigits -- maximum acceptable number of digits. Stops reading after\n"
38 " maxdigits have been entered (without requiring the user to\n"
39 " press the '#' key).\n"
40 " Defaults to 0 - no limit - wait for the user press the '#' key.\n"
41 " Any value below 0 means the same. Max accepted value is 255.\n"
42 " option -- may be 'skip' to return immediately if the line is not up,\n"
43 " or 'noanswer' to read digits even if the line is not up.\n\n"
44 "Returns -1 on hangup or error and 0 otherwise.\n";
50 static int read_exec(struct ast_channel *chan, void *data)
55 char argdata[256] = "";
62 int option_noanswer = 0;
64 if (!data || ast_strlen_zero((char *)data)) {
65 ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
68 strncpy(argdata, (char *)data, sizeof(argdata)-1);
70 varname = strsep(&stringp, "|");
71 filename = strsep(&stringp, "|");
72 maxdigitstr = strsep(&stringp,"|");
73 options = strsep(&stringp, "|");
74 if (options && !strcasecmp(options, "skip"))
76 if (options && !strcasecmp(options, "noanswer"))
78 if (!(filename) || ast_strlen_zero(filename))
81 maxdigits = atoi(maxdigitstr);
82 if ((maxdigits<1) || (maxdigits>255)) {
85 ast_verbose(VERBOSE_PREFIX_3 "Accepting a maximum of %i digits.\n", maxdigits);
87 if (!(varname) || ast_strlen_zero(varname)) {
88 ast_log(LOG_WARNING, "Read requires an variable name\n");
92 if (chan->_state != AST_STATE_UP) {
94 /* At the user's option, skip if the line is not up */
95 pbx_builtin_setvar_helper(chan, varname, "\0");
98 } else if (!option_noanswer) {
99 /* Otherwise answer unless we're supposed to read while on-hook */
100 res = ast_answer(chan);
104 ast_stopstream(chan);
105 res = ast_app_getdata(chan, filename, tmp, maxdigits, 0);
107 pbx_builtin_setvar_helper(chan, varname, tmp);
108 ast_verbose(VERBOSE_PREFIX_3 "User entered '%s'\n", tmp);
111 ast_verbose(VERBOSE_PREFIX_3 "User disconnected\n");
114 LOCAL_USER_REMOVE(u);
118 int unload_module(void)
120 STANDARD_HANGUP_LOCALUSERS;
121 return ast_unregister_application(app);
124 int load_module(void)
126 return ast_register_application(app, read_exec, synopsis, descrip);
129 char *description(void)
137 STANDARD_USECOUNT(res);
143 return ASTERISK_GPL_KEY;