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