more removal of redundant headers
[asterisk/asterisk.git] / apps / app_readexten.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007 Dave Chappell
5  *
6  * David Chappell <David.Chappell@trincoll.edu>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Trivial application to read an extension into a variable
22  *
23  * \author David Chappell <David.Chappell@trincoll.edu>
24  * 
25  * \ingroup applications
26  */
27  
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/file.h"
33 #include "asterisk/pbx.h"
34 #include "asterisk/app.h"
35 #include "asterisk/module.h"
36 #include "asterisk/indications.h"
37
38 enum {
39         OPT_SKIP = (1 << 0),
40         OPT_INDICATION = (1 << 1),
41         OPT_NOANSWER = (1 << 2),
42 } readexten_option_flags;
43
44 AST_APP_OPTIONS(readexten_app_options, {
45         AST_APP_OPTION('s', OPT_SKIP),
46         AST_APP_OPTION('i', OPT_INDICATION),
47         AST_APP_OPTION('n', OPT_NOANSWER),
48 });
49
50 static char *app = "ReadExten";
51
52 static char *synopsis = "Read an extension into a variable";
53
54 static char *descrip = 
55 "  ReadExten(<variable>[,[<filename>][,[<context>][,[<option>][,<timeout>]]]])\n\n"
56 "Reads a #-terminated string of digits from the user into the given variable.\n"
57 "  filename  file to play before reading digits or tone with option i\n"
58 "  context   context in which to match extensions\n"
59 "  option    options are:\n"
60 "              s - Return immediately if the channel is not answered,\n"
61 "              i - Play filename as an indication tone from your\n"
62 "                  indications.conf\n"
63 "              n - Read digits even if the channel is not answered.\n"
64 "  timeout   An integer number of seconds to wait for a digit response. If\n"
65 "            greater than 0, that value will override the default timeout.\n\n"
66 "ReadExten will set READEXTENSTATUS on exit with one of the following statuses:\n"
67 "  OK        A valid extension exists in ${variable}\n"
68 "  TIMEOUT   No extension was entered in the specified time\n"
69 "  INVALID   An invalid extension, ${INVALID_EXTEN}, was entered\n"
70 "  SKIP      Line was not up and the option 's' was specified\n"
71 "  ERROR     Invalid arguments were passed\n";
72
73
74 static int readexten_exec(struct ast_channel *chan, void *data)
75 {
76         int res = 0;
77         char exten[256] = "";
78         int maxdigits = sizeof(exten) - 1;
79         int timeout = 0, digit_timeout = 0, x = 0;
80         char *argcopy = NULL, *status = "";
81         struct ind_tone_zone_sound *ts = NULL;
82         struct ast_flags flags = {0};
83
84          AST_DECLARE_APP_ARGS(arglist,
85                 AST_APP_ARG(variable);
86                 AST_APP_ARG(filename);
87                 AST_APP_ARG(context);
88                 AST_APP_ARG(options);
89                 AST_APP_ARG(timeout);
90         );
91         
92         if (ast_strlen_zero(data)) {
93                 ast_log(LOG_WARNING, "ReadExten requires at least one argument\n");
94                 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
95                 return 0;
96         }
97
98         argcopy = ast_strdupa(data);
99         AST_STANDARD_APP_ARGS(arglist, argcopy);
100
101         if (ast_strlen_zero(arglist.variable)) {
102                 ast_log(LOG_WARNING, "Invalid! Usage: ReadExten(variable[|filename][|context][|options][|timeout])\n\n");
103                 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
104                 return 0;
105         }
106
107         if (ast_strlen_zero(arglist.filename))
108                 arglist.filename = NULL;
109
110         if (ast_strlen_zero(arglist.context))
111                 arglist.context = chan->context;
112
113         if (!ast_strlen_zero(arglist.options))
114                 ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options);
115
116         if (!ast_strlen_zero(arglist.timeout)) {
117                 timeout = atoi(arglist.timeout);
118                 if (timeout > 0)
119                         timeout *= 1000;
120         }
121
122         if (timeout <= 0)
123                 timeout = chan->pbx ? chan->pbx->rtimeout * 1000 : 10000;
124
125         if (digit_timeout <= 0)
126                 digit_timeout = chan->pbx ? chan->pbx->dtimeout * 1000 : 5000;
127
128         if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename))
129                 ts = ast_get_indication_tone(chan->zone, arglist.filename);
130
131         do {
132                 if (chan->_state != AST_STATE_UP) {
133                         if (ast_test_flag(&flags, OPT_SKIP)) {
134                                 /* At the user's option, skip if the line is not up */
135                                 pbx_builtin_setvar_helper(chan, arglist.variable, "");
136                                 status = "SKIP";
137                                 break;
138                         } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
139                                 /* Otherwise answer unless we're supposed to read while on-hook */
140                                 res = ast_answer(chan);
141                         }
142                 }
143
144                 if (res < 0) {
145                         status = "HANGUP";
146                         break;
147                 }
148
149                 ast_playtones_stop(chan);
150                 ast_stopstream(chan);
151
152                 if (ts && ts->data[0])
153                         res = ast_playtones_start(chan, 0, ts->data, 0);
154                 else if (arglist.filename)
155                         res = ast_streamfile(chan, arglist.filename, chan->language);
156
157                 for (x = 0; x < maxdigits; x++) {
158                         ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout);
159                         res = ast_waitfordigit(chan, timeout);
160
161                         ast_playtones_stop(chan);
162                         ast_stopstream(chan);
163                         timeout = digit_timeout;
164
165                         if (res < 1) {          /* timeout expired or hangup */
166                                 if (ast_check_hangup(chan))
167                                         status = "HANGUP";
168                                 else
169                                         status = "TIMEOUT";
170                                 break;
171                         } else if (res == '#') {
172                                 break;
173                         }
174
175                         exten[x] = res;
176                         if (!ast_matchmore_extension(chan, arglist.context, exten, 1 /* priority */, chan->cid.cid_num)) {
177                                 break;
178                         }
179                 }
180
181                 if (!ast_strlen_zero(status))
182                         break;
183
184                 if (ast_exists_extension(chan, arglist.context, exten, 1, chan->cid.cid_num)) {
185                         ast_debug(3, "User entered valid extension '%s'\n", exten);
186                         pbx_builtin_setvar_helper(chan, arglist.variable, exten);
187                         status = "OK";
188                 } else {
189                         ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, chan->name);
190                         pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
191                         status = "INVALID";
192                 }
193         } while (0);
194
195         pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
196
197         return status[0] == 'H' ? -1 : 0;
198 }
199
200 static int acf_isexten_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
201 {
202         int priority_int;
203         AST_DECLARE_APP_ARGS(args,
204                 AST_APP_ARG(context);
205                 AST_APP_ARG(extension);
206                 AST_APP_ARG(priority);
207         );
208
209         AST_STANDARD_APP_ARGS(args, parse);
210
211         if (ast_strlen_zero(args.context))
212                 args.context = chan->context;
213
214         if (ast_strlen_zero(args.extension)) {
215                 ast_log(LOG_WARNING, "Syntax: VALID_EXTEN([<context>],<extension>[,<priority>]) - missing argument <extension>!\n");
216                 return -1;
217         }
218
219         if (ast_strlen_zero(args.priority))
220                 priority_int = 1;
221         else
222                 priority_int = atoi(args.priority);
223
224         if (ast_exists_extension(chan, args.context, args.extension, priority_int, chan->cid.cid_num))
225             ast_copy_string(buffer, "1", buflen);
226         else
227             ast_copy_string(buffer, "0", buflen);
228
229         return 0;
230 }
231
232 static struct ast_custom_function acf_isexten = {
233         .name = "VALID_EXTEN",
234         .synopsis = "Determine whether an extension exists or not",
235         .syntax = "VALID_EXTEN([<context>],<extension>[,<priority>])",
236         .desc =
237 "Returns a true value if the indicated context, extension, and priority exist.\n"
238 "Context defaults to the current context, priority defaults to 1.\n",
239         .read = acf_isexten_exec,
240 };
241
242 static int unload_module(void)
243 {
244         int res = ast_unregister_application(app);
245         res |= ast_custom_function_unregister(&acf_isexten);
246
247         return res;     
248 }
249
250 static int load_module(void)
251 {
252         int res = ast_register_application(app, readexten_exec, synopsis, descrip);
253         res |= ast_custom_function_register(&acf_isexten);
254         return res;
255 }
256
257 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");