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