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>
28 static char *tdesc = "Read Variable Application";
30 static char *app = "Read";
32 static char *synopsis = "Read a variable";
34 static char *descrip =
35 " Read(variable[|filename][|maxdigits]): Reads a #-terminated string of digits from\n"
36 "the user, optionally playing a given filename first. Returns -1 on hangup or\n"
37 "error and 0 otherwise.\n"
38 " maxdigits -- maximum acceptable number of digits. Stops reading after maxdigits\n"
39 " have been entered (without requiring the user 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";
47 static int read_exec(struct ast_channel *chan, void *data)
57 if (!data || ast_strlen_zero((char *)data)) {
58 ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
61 strncpy(tmp, (char *)data, sizeof(tmp)-1);
62 stringp=(char *)calloc(1,strlen(tmp)+1);
63 snprintf(stringp,strlen(tmp)+1,"%s",tmp);
64 varname = strsep(&stringp, "|");
65 filename = strsep(&stringp, "|");
66 maxdigitstr = strsep(&stringp,"|");
67 if (!(filename) || ast_strlen_zero(filename))
70 maxdigits = atoi(maxdigitstr);
71 if ((maxdigits<1) || (maxdigits>255)) {
74 ast_verbose(VERBOSE_PREFIX_3 "Accepting a maximum of %i digits.\n", maxdigits);
76 if (!(varname) || ast_strlen_zero(varname)) {
77 ast_log(LOG_WARNING, "Read requires an variable name\n");
81 if (chan->_state != AST_STATE_UP) {
82 /* Answer if the line isn't up. */
83 res = ast_answer(chan);
85 strncpy(tmp, (char *)varname, sizeof(tmp)-1);
88 res = ast_app_getdata(chan, filename, tmp, maxdigits, 0);
90 pbx_builtin_setvar_helper(chan, varname, tmp);
91 ast_verbose(VERBOSE_PREFIX_3 "User entered '%s'\n", tmp);
97 int unload_module(void)
99 STANDARD_HANGUP_LOCALUSERS;
100 return ast_unregister_application(app);
103 int load_module(void)
105 return ast_register_application(app, read_exec, synopsis, descrip);
108 char *description(void)
116 STANDARD_USECOUNT(res);
122 return ASTERISK_GPL_KEY;