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