00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 168565 $")
00031
00032 #include "asterisk/file.h"
00033 #include "asterisk/pbx.h"
00034 #include "asterisk/app.h"
00035 #include "asterisk/module.h"
00036 #include "asterisk/indications.h"
00037 #include "asterisk/channel.h"
00038
00039 enum {
00040 OPT_SKIP = (1 << 0),
00041 OPT_INDICATION = (1 << 1),
00042 OPT_NOANSWER = (1 << 2),
00043 } readexten_option_flags;
00044
00045 AST_APP_OPTIONS(readexten_app_options, {
00046 AST_APP_OPTION('s', OPT_SKIP),
00047 AST_APP_OPTION('i', OPT_INDICATION),
00048 AST_APP_OPTION('n', OPT_NOANSWER),
00049 });
00050
00051 static char *app = "ReadExten";
00052
00053 static char *synopsis = "Read an extension into a variable";
00054
00055 static char *descrip =
00056 " ReadExten(<variable>[,[<filename>][,[<context>][,[<option>][,<timeout>]]]])\n\n"
00057 "Reads a #-terminated string of digits from the user into the given variable.\n"
00058 " filename file to play before reading digits or tone with option i\n"
00059 " context context in which to match extensions\n"
00060 " option options are:\n"
00061 " s - Return immediately if the channel is not answered,\n"
00062 " i - Play filename as an indication tone from your\n"
00063 " indications.conf\n"
00064 " n - Read digits even if the channel is not answered.\n"
00065 " timeout An integer number of seconds to wait for a digit response. If\n"
00066 " greater than 0, that value will override the default timeout.\n\n"
00067 "ReadExten will set READEXTENSTATUS on exit with one of the following statuses:\n"
00068 " OK A valid extension exists in ${variable}\n"
00069 " TIMEOUT No extension was entered in the specified time\n"
00070 " INVALID An invalid extension, ${INVALID_EXTEN}, was entered\n"
00071 " SKIP Line was not up and the option 's' was specified\n"
00072 " ERROR Invalid arguments were passed\n";
00073
00074
00075 static int readexten_exec(struct ast_channel *chan, void *data)
00076 {
00077 int res = 0;
00078 char exten[256] = "";
00079 int maxdigits = sizeof(exten) - 1;
00080 int timeout = 0, digit_timeout = 0, x = 0;
00081 char *argcopy = NULL, *status = "";
00082 struct tone_zone_sound *ts = NULL;
00083 struct ast_flags flags = {0};
00084
00085 AST_DECLARE_APP_ARGS(arglist,
00086 AST_APP_ARG(variable);
00087 AST_APP_ARG(filename);
00088 AST_APP_ARG(context);
00089 AST_APP_ARG(options);
00090 AST_APP_ARG(timeout);
00091 );
00092
00093 if (ast_strlen_zero(data)) {
00094 ast_log(LOG_WARNING, "ReadExten requires at least one argument\n");
00095 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
00096 return 0;
00097 }
00098
00099 argcopy = ast_strdupa(data);
00100 AST_STANDARD_APP_ARGS(arglist, argcopy);
00101
00102 if (ast_strlen_zero(arglist.variable)) {
00103 ast_log(LOG_WARNING, "Invalid! Usage: ReadExten(variable[|filename][|context][|options][|timeout])\n\n");
00104 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
00105 return 0;
00106 }
00107
00108 if (ast_strlen_zero(arglist.filename))
00109 arglist.filename = NULL;
00110
00111 if (ast_strlen_zero(arglist.context))
00112 arglist.context = chan->context;
00113
00114 if (!ast_strlen_zero(arglist.options))
00115 ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options);
00116
00117 if (!ast_strlen_zero(arglist.timeout)) {
00118 timeout = atoi(arglist.timeout);
00119 if (timeout > 0)
00120 timeout *= 1000;
00121 }
00122
00123 if (timeout <= 0)
00124 timeout = chan->pbx ? chan->pbx->rtimeoutms : 10000;
00125
00126 if (digit_timeout <= 0)
00127 digit_timeout = chan->pbx ? chan->pbx->dtimeoutms : 5000;
00128
00129 if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename))
00130 ts = ast_get_indication_tone(chan->zone, arglist.filename);
00131
00132 do {
00133 if (chan->_state != AST_STATE_UP) {
00134 if (ast_test_flag(&flags, OPT_SKIP)) {
00135
00136 pbx_builtin_setvar_helper(chan, arglist.variable, "");
00137 status = "SKIP";
00138 break;
00139 } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
00140
00141 res = ast_answer(chan);
00142 }
00143 }
00144
00145 if (res < 0) {
00146 status = "HANGUP";
00147 break;
00148 }
00149
00150 ast_playtones_stop(chan);
00151 ast_stopstream(chan);
00152
00153 if (ts && ts->data[0])
00154 res = ast_playtones_start(chan, 0, ts->data, 0);
00155 else if (arglist.filename)
00156 res = ast_streamfile(chan, arglist.filename, chan->language);
00157
00158 for (x = 0; x < maxdigits; x++) {
00159 ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout);
00160 res = ast_waitfordigit(chan, timeout);
00161
00162 ast_playtones_stop(chan);
00163 ast_stopstream(chan);
00164 timeout = digit_timeout;
00165
00166 if (res < 1) {
00167 if (ast_check_hangup(chan))
00168 status = "HANGUP";
00169 else
00170 status = "TIMEOUT";
00171 break;
00172 }
00173
00174 exten[x] = res;
00175 if (!ast_matchmore_extension(chan, arglist.context, exten, 1 , chan->cid.cid_num)) {
00176 if (!ast_exists_extension(chan, arglist.context, exten, 1, chan->cid.cid_num) && res == '#') {
00177 exten[x] = '\0';
00178 }
00179 break;
00180 }
00181 }
00182
00183 if (!ast_strlen_zero(status))
00184 break;
00185
00186 if (ast_exists_extension(chan, arglist.context, exten, 1, chan->cid.cid_num)) {
00187 ast_debug(3, "User entered valid extension '%s'\n", exten);
00188 pbx_builtin_setvar_helper(chan, arglist.variable, exten);
00189 status = "OK";
00190 } else {
00191 ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, chan->name);
00192 pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
00193 status = "INVALID";
00194 }
00195 } while (0);
00196
00197 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
00198
00199 return status[0] == 'H' ? -1 : 0;
00200 }
00201
00202 static int acf_isexten_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
00203 {
00204 int priority_int;
00205 AST_DECLARE_APP_ARGS(args,
00206 AST_APP_ARG(context);
00207 AST_APP_ARG(extension);
00208 AST_APP_ARG(priority);
00209 );
00210
00211 AST_STANDARD_APP_ARGS(args, parse);
00212
00213 if (ast_strlen_zero(args.context))
00214 args.context = chan->context;
00215
00216 if (ast_strlen_zero(args.extension)) {
00217 ast_log(LOG_WARNING, "Syntax: VALID_EXTEN([<context>],<extension>[,<priority>]) - missing argument <extension>!\n");
00218 return -1;
00219 }
00220
00221 if (ast_strlen_zero(args.priority))
00222 priority_int = 1;
00223 else
00224 priority_int = atoi(args.priority);
00225
00226 if (ast_exists_extension(chan, args.context, args.extension, priority_int, chan->cid.cid_num))
00227 ast_copy_string(buffer, "1", buflen);
00228 else
00229 ast_copy_string(buffer, "0", buflen);
00230
00231 return 0;
00232 }
00233
00234 static struct ast_custom_function acf_isexten = {
00235 .name = "VALID_EXTEN",
00236 .synopsis = "Determine whether an extension exists or not",
00237 .syntax = "VALID_EXTEN([<context>],<extension>[,<priority>])",
00238 .desc =
00239 "Returns a true value if the indicated context, extension, and priority exist.\n"
00240 "Context defaults to the current context, priority defaults to 1.\n",
00241 .read = acf_isexten_exec,
00242 };
00243
00244 static int unload_module(void)
00245 {
00246 int res = ast_unregister_application(app);
00247 res |= ast_custom_function_unregister(&acf_isexten);
00248
00249 return res;
00250 }
00251
00252 static int load_module(void)
00253 {
00254 int res = ast_register_application(app, readexten_exec, synopsis, descrip);
00255 res |= ast_custom_function_register(&acf_isexten);
00256 return res;
00257 }
00258
00259 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");