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]): Reads a #-terminated string of digits from\n"
35 "the user, optionally playing a given filename first. Returns -1 on hangup or\n"
36 "error and 0 otherwise.\n"
37 " maxdigits -- maximum acceptable number of digits. Stops reading after maxdigits\n"
38 " have been entered (without requiring the user press the '#' key).\n"
39 " Defaults to 0 - no limit - wait for the user press the '#' key.\n"
40 " Any value below 0 means the same. Max accepted value is 255.\n";
46 static int read_exec(struct ast_channel *chan, void *data)
56 if (!data || ast_strlen_zero((char *)data)) {
57 ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
60 strncpy(tmp, (char *)data, sizeof(tmp)-1);
61 stringp=(char *)calloc(1,strlen(tmp)+1);
62 snprintf(stringp,strlen(tmp)+1,"%s",tmp);
63 varname = strsep(&stringp, "|");
64 filename = strsep(&stringp, "|");
65 maxdigitstr = strsep(&stringp,"|");
66 if (!(filename) || ast_strlen_zero(filename))
69 maxdigits = atoi(maxdigitstr);
70 if ((maxdigits<1) || (maxdigits>255)) {
73 ast_verbose(VERBOSE_PREFIX_3 "Accepting a maximum of %i digits.\n", maxdigits);
75 if (!(varname) || ast_strlen_zero(varname)) {
76 ast_log(LOG_WARNING, "Read requires an variable name\n");
80 if (chan->_state != AST_STATE_UP) {
81 /* Answer if the line isn't up. */
82 res = ast_answer(chan);
84 strncpy(tmp, (char *)varname, sizeof(tmp)-1);
87 res = ast_app_getdata(chan, filename, tmp, maxdigits, 0);
89 pbx_builtin_setvar_helper(chan, varname, tmp);
90 ast_verbose(VERBOSE_PREFIX_3 "User entered '%s'\n", tmp);
96 int unload_module(void)
98 STANDARD_HANGUP_LOCALUSERS;
99 return ast_unregister_application(app);
102 int load_module(void)
104 return ast_register_application(app, read_exec, synopsis, descrip);
107 char *description(void)
115 STANDARD_USECOUNT(res);
121 return ASTERISK_GPL_KEY;