| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#include "asterisk/file.h" |
| 34 |
#include "asterisk/pbx.h" |
| 35 |
#include "asterisk/app.h" |
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/indications.h" |
| 38 |
#include "asterisk/channel.h" |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
enum readexten_option_flags { |
| 107 |
OPT_SKIP = (1 << 0), |
| 108 |
OPT_INDICATION = (1 << 1), |
| 109 |
OPT_NOANSWER = (1 << 2), |
| 110 |
OPT_POUND_TO_END = (1 << 3), |
| 111 |
}; |
| 112 |
|
| 113 |
AST_APP_OPTIONS(readexten_app_options, { |
| 114 |
AST_APP_OPTION('s', OPT_SKIP), |
| 115 |
AST_APP_OPTION('i', OPT_INDICATION), |
| 116 |
AST_APP_OPTION('n', OPT_NOANSWER), |
| 117 |
AST_APP_OPTION('p', OPT_POUND_TO_END), |
| 118 |
}); |
| 119 |
|
| 120 |
static char *app = "ReadExten"; |
| 121 |
|
| 122 |
static int readexten_exec(struct ast_channel *chan, const char *data) |
| 123 |
{ |
| 124 |
int res = 0; |
| 125 |
char exten[256] = ""; |
| 126 |
int maxdigits = sizeof(exten) - 1; |
| 127 |
int timeout = 0, digit_timeout = 0, x = 0; |
| 128 |
char *argcopy = NULL, *status = ""; |
| 129 |
struct ast_tone_zone_sound *ts = NULL; |
| 130 |
struct ast_flags flags = {0}; |
| 131 |
|
| 132 |
AST_DECLARE_APP_ARGS(arglist, |
| 133 |
AST_APP_ARG(variable); |
| 134 |
AST_APP_ARG(filename); |
| 135 |
AST_APP_ARG(context); |
| 136 |
AST_APP_ARG(options); |
| 137 |
AST_APP_ARG(timeout); |
| 138 |
); |
| 139 |
|
| 140 |
if (ast_strlen_zero(data)) { |
| 141 |
ast_log(LOG_WARNING, "ReadExten requires at least one argument\n"); |
| 142 |
pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR"); |
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
argcopy = ast_strdupa(data); |
| 147 |
AST_STANDARD_APP_ARGS(arglist, argcopy); |
| 148 |
|
| 149 |
if (ast_strlen_zero(arglist.variable)) { |
| 150 |
ast_log(LOG_WARNING, "Usage: ReadExten(variable[,filename[,context[,options[,timeout]]]])\n"); |
| 151 |
pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR"); |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
if (ast_strlen_zero(arglist.filename)) { |
| 156 |
arglist.filename = NULL; |
| 157 |
} |
| 158 |
|
| 159 |
if (ast_strlen_zero(arglist.context)) { |
| 160 |
arglist.context = ast_strdupa(ast_channel_context(chan)); |
| 161 |
} |
| 162 |
|
| 163 |
if (!ast_strlen_zero(arglist.options)) { |
| 164 |
ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options); |
| 165 |
} |
| 166 |
|
| 167 |
if (!ast_strlen_zero(arglist.timeout)) { |
| 168 |
timeout = atoi(arglist.timeout); |
| 169 |
if (timeout > 0) |
| 170 |
timeout *= 1000; |
| 171 |
} |
| 172 |
|
| 173 |
if (timeout <= 0) |
| 174 |
timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 10000; |
| 175 |
|
| 176 |
digit_timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->dtimeoutms : 5000; |
| 177 |
|
| 178 |
if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename)) { |
| 179 |
ts = ast_get_indication_tone(ast_channel_zone(chan), arglist.filename); |
| 180 |
} |
| 181 |
|
| 182 |
do { |
| 183 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 184 |
if (ast_test_flag(&flags, OPT_SKIP)) { |
| 185 |
|
| 186 |
pbx_builtin_setvar_helper(chan, arglist.variable, ""); |
| 187 |
status = "SKIP"; |
| 188 |
break; |
| 189 |
} else if (!ast_test_flag(&flags, OPT_NOANSWER)) { |
| 190 |
|
| 191 |
res = ast_answer(chan); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if (res < 0) { |
| 196 |
status = "HANGUP"; |
| 197 |
break; |
| 198 |
} |
| 199 |
|
| 200 |
ast_playtones_stop(chan); |
| 201 |
ast_stopstream(chan); |
| 202 |
|
| 203 |
if (ts && ts->data[0]) { |
| 204 |
res = ast_playtones_start(chan, 0, ts->data, 0); |
| 205 |
} else if (arglist.filename) { |
| 206 |
if (ast_test_flag(&flags, OPT_INDICATION) && ast_fileexists(arglist.filename, NULL, ast_channel_language(chan)) <= 0) { |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
res = ast_playtones_start(chan, 0, arglist.filename, 0); |
| 214 |
} else { |
| 215 |
res = ast_streamfile(chan, arglist.filename, ast_channel_language(chan)); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
for (x = 0; x < maxdigits; x++) { |
| 220 |
ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout); |
| 221 |
res = ast_waitfordigit(chan, timeout); |
| 222 |
|
| 223 |
ast_playtones_stop(chan); |
| 224 |
ast_stopstream(chan); |
| 225 |
timeout = digit_timeout; |
| 226 |
|
| 227 |
if (res < 1) { |
| 228 |
if (ast_check_hangup(chan)) { |
| 229 |
status = "HANGUP"; |
| 230 |
} else if (x == 0) { |
| 231 |
pbx_builtin_setvar_helper(chan, arglist.variable, "t"); |
| 232 |
status = "TIMEOUT"; |
| 233 |
} |
| 234 |
break; |
| 235 |
} |
| 236 |
|
| 237 |
if (ast_test_flag(&flags, OPT_POUND_TO_END) && res == '#') { |
| 238 |
exten[x] = 0; |
| 239 |
break; |
| 240 |
} |
| 241 |
|
| 242 |
exten[x] = res; |
| 243 |
if (!ast_matchmore_extension(chan, arglist.context, exten, 1 , |
| 244 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { |
| 245 |
if (!ast_exists_extension(chan, arglist.context, exten, 1, |
| 246 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL)) |
| 247 |
&& res == '#') { |
| 248 |
exten[x] = '\0'; |
| 249 |
} |
| 250 |
break; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
if (!ast_strlen_zero(status)) |
| 255 |
break; |
| 256 |
|
| 257 |
if (ast_exists_extension(chan, arglist.context, exten, 1, |
| 258 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { |
| 259 |
ast_debug(3, "User entered valid extension '%s'\n", exten); |
| 260 |
pbx_builtin_setvar_helper(chan, arglist.variable, exten); |
| 261 |
status = "OK"; |
| 262 |
} else { |
| 263 |
ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, ast_channel_name(chan)); |
| 264 |
pbx_builtin_setvar_helper(chan, arglist.variable, "i"); |
| 265 |
pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten); |
| 266 |
status = "INVALID"; |
| 267 |
} |
| 268 |
} while (0); |
| 269 |
|
| 270 |
if (ts) { |
| 271 |
ts = ast_tone_zone_sound_unref(ts); |
| 272 |
} |
| 273 |
|
| 274 |
pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status); |
| 275 |
|
| 276 |
return status[0] == 'H' ? -1 : 0; |
| 277 |
} |
| 278 |
|
| 279 |
static int unload_module(void) |
| 280 |
{ |
| 281 |
int res = ast_unregister_application(app); |
| 282 |
return res; |
| 283 |
} |
| 284 |
|
| 285 |
static int load_module(void) |
| 286 |
{ |
| 287 |
int res = ast_register_application_xml(app, readexten_exec); |
| 288 |
return res; |
| 289 |
} |
| 290 |
|
| 291 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity"); |