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
00029
00030
00031 #include "asterisk.h"
00032
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 357542 $")
00034
00035 #include "asterisk/file.h"
00036 #include "asterisk/pbx.h"
00037 #include "asterisk/app.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/indications.h"
00040 #include "asterisk/channel.h"
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 enum readexten_option_flags {
00102 OPT_SKIP = (1 << 0),
00103 OPT_INDICATION = (1 << 1),
00104 OPT_NOANSWER = (1 << 2),
00105 };
00106
00107 AST_APP_OPTIONS(readexten_app_options, {
00108 AST_APP_OPTION('s', OPT_SKIP),
00109 AST_APP_OPTION('i', OPT_INDICATION),
00110 AST_APP_OPTION('n', OPT_NOANSWER),
00111 });
00112
00113 static char *app = "ReadExten";
00114
00115 static int readexten_exec(struct ast_channel *chan, const char *data)
00116 {
00117 int res = 0;
00118 char exten[256] = "";
00119 int maxdigits = sizeof(exten) - 1;
00120 int timeout = 0, digit_timeout = 0, x = 0;
00121 char *argcopy = NULL, *status = "";
00122 struct ast_tone_zone_sound *ts = NULL;
00123 struct ast_flags flags = {0};
00124
00125 AST_DECLARE_APP_ARGS(arglist,
00126 AST_APP_ARG(variable);
00127 AST_APP_ARG(filename);
00128 AST_APP_ARG(context);
00129 AST_APP_ARG(options);
00130 AST_APP_ARG(timeout);
00131 );
00132
00133 if (ast_strlen_zero(data)) {
00134 ast_log(LOG_WARNING, "ReadExten requires at least one argument\n");
00135 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
00136 return 0;
00137 }
00138
00139 argcopy = ast_strdupa(data);
00140 AST_STANDARD_APP_ARGS(arglist, argcopy);
00141
00142 if (ast_strlen_zero(arglist.variable)) {
00143 ast_log(LOG_WARNING, "Usage: ReadExten(variable[,filename[,context[,options[,timeout]]]])\n");
00144 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
00145 return 0;
00146 }
00147
00148 if (ast_strlen_zero(arglist.filename)) {
00149 arglist.filename = NULL;
00150 }
00151
00152 if (ast_strlen_zero(arglist.context)) {
00153 arglist.context = ast_strdupa(ast_channel_context(chan));
00154 }
00155
00156 if (!ast_strlen_zero(arglist.options)) {
00157 ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options);
00158 }
00159
00160 if (!ast_strlen_zero(arglist.timeout)) {
00161 timeout = atoi(arglist.timeout);
00162 if (timeout > 0)
00163 timeout *= 1000;
00164 }
00165
00166 if (timeout <= 0)
00167 timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 10000;
00168
00169 if (digit_timeout <= 0)
00170 digit_timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->dtimeoutms : 5000;
00171
00172 if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename)) {
00173 ts = ast_get_indication_tone(ast_channel_zone(chan), arglist.filename);
00174 }
00175
00176 do {
00177 if (ast_channel_state(chan) != AST_STATE_UP) {
00178 if (ast_test_flag(&flags, OPT_SKIP)) {
00179
00180 pbx_builtin_setvar_helper(chan, arglist.variable, "");
00181 status = "SKIP";
00182 break;
00183 } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
00184
00185 res = ast_answer(chan);
00186 }
00187 }
00188
00189 if (res < 0) {
00190 status = "HANGUP";
00191 break;
00192 }
00193
00194 ast_playtones_stop(chan);
00195 ast_stopstream(chan);
00196
00197 if (ts && ts->data[0]) {
00198 res = ast_playtones_start(chan, 0, ts->data, 0);
00199 } else if (arglist.filename) {
00200 if (ast_test_flag(&flags, OPT_INDICATION) && ast_fileexists(arglist.filename, NULL, ast_channel_language(chan)) <= 0) {
00201
00202
00203
00204
00205
00206
00207 res = ast_playtones_start(chan, 0, arglist.filename, 0);
00208 } else {
00209 res = ast_streamfile(chan, arglist.filename, ast_channel_language(chan));
00210 }
00211 }
00212
00213 for (x = 0; x < maxdigits; x++) {
00214 ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout);
00215 res = ast_waitfordigit(chan, timeout);
00216
00217 ast_playtones_stop(chan);
00218 ast_stopstream(chan);
00219 timeout = digit_timeout;
00220
00221 if (res < 1) {
00222 if (ast_check_hangup(chan)) {
00223 status = "HANGUP";
00224 } else if (x == 0) {
00225 pbx_builtin_setvar_helper(chan, arglist.variable, "t");
00226 status = "TIMEOUT";
00227 }
00228 break;
00229 }
00230
00231 exten[x] = res;
00232 if (!ast_matchmore_extension(chan, arglist.context, exten, 1 ,
00233 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
00234 if (!ast_exists_extension(chan, arglist.context, exten, 1,
00235 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))
00236 && res == '#') {
00237 exten[x] = '\0';
00238 }
00239 break;
00240 }
00241 }
00242
00243 if (!ast_strlen_zero(status))
00244 break;
00245
00246 if (ast_exists_extension(chan, arglist.context, exten, 1,
00247 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
00248 ast_debug(3, "User entered valid extension '%s'\n", exten);
00249 pbx_builtin_setvar_helper(chan, arglist.variable, exten);
00250 status = "OK";
00251 } else {
00252 ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, ast_channel_name(chan));
00253 pbx_builtin_setvar_helper(chan, arglist.variable, "i");
00254 pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
00255 status = "INVALID";
00256 }
00257 } while (0);
00258
00259 if (ts) {
00260 ts = ast_tone_zone_sound_unref(ts);
00261 }
00262
00263 pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
00264
00265 return status[0] == 'H' ? -1 : 0;
00266 }
00267
00268 static int unload_module(void)
00269 {
00270 int res = ast_unregister_application(app);
00271 return res;
00272 }
00273
00274 static int load_module(void)
00275 {
00276 int res = ast_register_application_xml(app, readexten_exec);
00277 return res;
00278 }
00279
00280 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");