Fix silly read problem which would hang up if nobody called (bug #3042)
[asterisk/asterisk.git] / apps / app_read.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Trivial application to read a variable
5  * 
6  * Copyright (C) 2003, Digium
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
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>
24 #include <string.h>
25 #include <stdlib.h>
26
27 static char *tdesc = "Read Variable Application";
28
29 static char *app = "Read";
30
31 static char *synopsis = "Read a variable";
32
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";
45
46 STANDARD_LOCAL_USER;
47
48 LOCAL_USER_DECL;
49
50 static int read_exec(struct ast_channel *chan, void *data)
51 {
52         int res = 0;
53         struct localuser *u;
54         char tmp[256];
55         char argdata[256] = "";
56         char *varname;
57         char *filename;
58         char *stringp;
59         char *maxdigitstr;
60         char *options;
61         int option_skip = 0;
62         int option_noanswer = 0;
63         int maxdigits=255;
64         if (!data || ast_strlen_zero((char *)data)) {
65                 ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
66                 return -1;
67         }
68         strncpy(argdata, (char *)data, sizeof(argdata)-1);
69         stringp=argdata;
70         varname = strsep(&stringp, "|");
71         filename = strsep(&stringp, "|");
72         maxdigitstr = strsep(&stringp,"|");
73         options = strsep(&stringp, "|");
74         if (options && !strcasecmp(options, "skip"))
75                 option_skip = 1;
76         if (options && !strcasecmp(options, "noanswer"))
77                 option_noanswer = 1;
78         if (!(filename) || ast_strlen_zero(filename)) 
79                 filename = NULL;
80         if (maxdigitstr) {
81             maxdigits = atoi(maxdigitstr);
82             if ((maxdigits<1) || (maxdigits>255)) {
83                 maxdigits = 255;
84             } else
85                 ast_verbose(VERBOSE_PREFIX_3 "Accepting a maximum of %i digits.\n", maxdigits);
86         }       
87         if (!(varname) || ast_strlen_zero(varname)) {
88                 ast_log(LOG_WARNING, "Read requires an variable name\n");
89                 return -1;
90         }
91         LOCAL_USER_ADD(u);
92         if (chan->_state != AST_STATE_UP) {
93                 if (option_skip) {
94                         /* At the user's option, skip if the line is not up */
95                         pbx_builtin_setvar_helper(chan, varname, "\0");
96                         LOCAL_USER_REMOVE(u);
97                         return 0;
98                 } else if (!option_noanswer) {
99                         /* Otherwise answer unless we're supposed to read while on-hook */
100                         res = ast_answer(chan);
101                 }
102         }
103         if (!res) {
104                 ast_stopstream(chan);
105                 res = ast_app_getdata(chan, filename, tmp, maxdigits, 0);
106                 if (res > -1) {
107                         pbx_builtin_setvar_helper(chan, varname, tmp);
108                         ast_verbose(VERBOSE_PREFIX_3 "User entered '%s'\n", tmp);
109                         res = 0;
110                 } else {
111                         ast_verbose(VERBOSE_PREFIX_3 "User disconnected\n");
112                 }
113         }
114         LOCAL_USER_REMOVE(u);
115         return res;
116 }
117
118 int unload_module(void)
119 {
120         STANDARD_HANGUP_LOCALUSERS;
121         return ast_unregister_application(app);
122 }
123
124 int load_module(void)
125 {
126         return ast_register_application(app, read_exec, synopsis, descrip);
127 }
128
129 char *description(void)
130 {
131         return tdesc;
132 }
133
134 int usecount(void)
135 {
136         int res;
137         STANDARD_USECOUNT(res);
138         return res;
139 }
140
141 char *key()
142 {
143         return ASTERISK_GPL_KEY;
144 }