Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / apps / app_read.c
blob9174210101a759aeee6592dfa8f18e82b205957b
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Trivial application to read a variable
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/file.h"
33 #include "asterisk/pbx.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/app.h"
36 #include "asterisk/module.h"
37 #include "asterisk/indications.h"
39 enum {
40 OPT_SKIP = (1 << 0),
41 OPT_INDICATION = (1 << 1),
42 OPT_NOANSWER = (1 << 2),
43 } read_option_flags;
45 AST_APP_OPTIONS(read_app_options, {
46 AST_APP_OPTION('s', OPT_SKIP),
47 AST_APP_OPTION('i', OPT_INDICATION),
48 AST_APP_OPTION('n', OPT_NOANSWER),
49 });
51 static char *app = "Read";
53 static char *synopsis = "Read a variable";
55 static char *descrip =
56 " Read(variable[,filename[&filename2...]][,maxdigits][,option][,attempts][,timeout])\n\n"
57 "Reads a #-terminated string of digits a certain number of times from the\n"
58 "user in to the given variable.\n"
59 " filename -- file(s) to play before reading digits or tone with option i\n"
60 " maxdigits -- maximum acceptable number of digits. Stops reading after\n"
61 " maxdigits have been entered (without requiring the user to\n"
62 " press the '#' key).\n"
63 " Defaults to 0 - no limit - wait for the user press the '#' key.\n"
64 " Any value below 0 means the same. Max accepted value is 255.\n"
65 " option -- options are 's' , 'i', 'n'\n"
66 " 's' to return immediately if the line is not up,\n"
67 " 'i' to play filename as an indication tone from your indications.conf\n"
68 " 'n' to read digits even if the line is not up.\n"
69 " attempts -- if greater than 1, that many attempts will be made in the \n"
70 " event no data is entered.\n"
71 " timeout -- The number of seconds to wait for a digit response. If greater\n"
72 " than 0, that value will override the default timeout. Can be floating point.\n"
73 "This application sets the following channel variable upon completion:\n"
74 " READSTATUS - This is the status of the read operation.\n"
75 " Possible values are:\n"
76 " OK | ERROR | HANGUP | INTERRUPTED | SKIPPED | TIMEOUT\n";
79 #define ast_next_data(instr,ptr,delim) if((ptr=strchr(instr,delim))) { *(ptr) = '\0' ; ptr++;}
81 static int read_exec(struct ast_channel *chan, void *data)
83 int res = 0;
84 char tmp[256] = "";
85 int maxdigits = 255;
86 int tries = 1, to = 0, x = 0;
87 double tosec;
88 char *argcopy = NULL;
89 struct ind_tone_zone_sound *ts = NULL;
90 struct ast_flags flags = {0};
91 const char *status = "ERROR";
93 AST_DECLARE_APP_ARGS(arglist,
94 AST_APP_ARG(variable);
95 AST_APP_ARG(filename);
96 AST_APP_ARG(maxdigits);
97 AST_APP_ARG(options);
98 AST_APP_ARG(attempts);
99 AST_APP_ARG(timeout);
102 pbx_builtin_setvar_helper(chan, "READSTATUS", status);
103 if (ast_strlen_zero(data)) {
104 ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
105 return 0;
108 argcopy = ast_strdupa(data);
110 AST_STANDARD_APP_ARGS(arglist, argcopy);
112 if (!ast_strlen_zero(arglist.options)) {
113 ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
116 if (!ast_strlen_zero(arglist.attempts)) {
117 tries = atoi(arglist.attempts);
118 if (tries <= 0)
119 tries = 1;
122 if (!ast_strlen_zero(arglist.timeout)) {
123 tosec = atof(arglist.timeout);
124 if (tosec <= 0)
125 to = 0;
126 else
127 to = tosec * 1000.0;
130 if (ast_strlen_zero(arglist.filename)) {
131 arglist.filename = NULL;
133 if (!ast_strlen_zero(arglist.maxdigits)) {
134 maxdigits = atoi(arglist.maxdigits);
135 if ((maxdigits < 1) || (maxdigits > 255)) {
136 maxdigits = 255;
137 } else
138 ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits);
140 if (ast_strlen_zero(arglist.variable)) {
141 ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n");
142 return 0;
144 if (ast_test_flag(&flags, OPT_INDICATION)) {
145 if (! ast_strlen_zero(arglist.filename)) {
146 ts = ast_get_indication_tone(chan->zone, arglist.filename);
149 if (chan->_state != AST_STATE_UP) {
150 if (ast_test_flag(&flags, OPT_SKIP)) {
151 /* At the user's option, skip if the line is not up */
152 pbx_builtin_setvar_helper(chan, arglist.variable, "");
153 pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED");
154 return 0;
155 } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
156 /* Otherwise answer unless we're supposed to read while on-hook */
157 res = ast_answer(chan);
160 if (!res) {
161 while (tries && !res) {
162 ast_stopstream(chan);
163 if (ts && ts->data[0]) {
164 if (!to)
165 to = chan->pbx ? chan->pbx->rtimeoutms : 6000;
166 res = ast_playtones_start(chan, 0, ts->data, 0);
167 for (x = 0; x < maxdigits; ) {
168 res = ast_waitfordigit(chan, to);
169 ast_playtones_stop(chan);
170 if (res < 1) {
171 if (res == 0)
172 status = "TIMEOUT";
173 tmp[x]='\0';
174 break;
176 tmp[x++] = res;
177 if (tmp[x-1] == '#') {
178 tmp[x-1] = '\0';
179 status = "OK";
180 break;
182 if (x >= maxdigits) {
183 status = "OK";
186 } else {
187 res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
188 if (res == 0)
189 status = "OK";
190 else if (res == 1)
191 status = "TIMEOUT";
192 else if (res == 2)
193 status = "INTERRUPTED";
195 if (res > -1) {
196 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
197 if (!ast_strlen_zero(tmp)) {
198 ast_verb(3, "User entered '%s'\n", tmp);
199 tries = 0;
200 } else {
201 tries--;
202 if (tries)
203 ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
204 else
205 ast_verb(3, "User entered nothing.\n");
207 res = 0;
208 } else {
209 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
210 ast_verb(3, "User disconnected\n");
215 if (ast_check_hangup(chan))
216 status = "HANGUP";
217 pbx_builtin_setvar_helper(chan, "READSTATUS", status);
218 return 0;
221 static int unload_module(void)
223 return ast_unregister_application(app);
226 static int load_module(void)
228 return ast_register_application(app, read_exec, synopsis, descrip);
231 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read Variable Application");