| 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 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/file.h" |
| 35 |
#include "asterisk/pbx.h" |
| 36 |
#include "asterisk/channel.h" |
| 37 |
#include "asterisk/app.h" |
| 38 |
#include "asterisk/module.h" |
| 39 |
#include "asterisk/indications.h" |
| 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 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
enum read_option_flags { |
| 140 |
OPT_SKIP = (1 << 0), |
| 141 |
OPT_INDICATION = (1 << 1), |
| 142 |
OPT_NOANSWER = (1 << 2), |
| 143 |
OPT_TERMINATOR = (1 << 3), |
| 144 |
OPT_KEEP_TERMINATOR = (1 << 4), |
| 145 |
}; |
| 146 |
|
| 147 |
enum { |
| 148 |
OPT_ARG_TERMINATOR, |
| 149 |
|
| 150 |
OPT_ARG_ARRAY_SIZE, |
| 151 |
}; |
| 152 |
|
| 153 |
AST_APP_OPTIONS(read_app_options, { |
| 154 |
AST_APP_OPTION('s', OPT_SKIP), |
| 155 |
AST_APP_OPTION('i', OPT_INDICATION), |
| 156 |
AST_APP_OPTION('n', OPT_NOANSWER), |
| 157 |
AST_APP_OPTION_ARG('t', OPT_TERMINATOR, OPT_ARG_TERMINATOR), |
| 158 |
AST_APP_OPTION('e', OPT_KEEP_TERMINATOR), |
| 159 |
}); |
| 160 |
|
| 161 |
static char *app = "Read"; |
| 162 |
|
| 163 |
static int read_exec(struct ast_channel *chan, const char *data) |
| 164 |
{ |
| 165 |
int res = 0; |
| 166 |
char tmp[256] = ""; |
| 167 |
int maxdigits = 255; |
| 168 |
int tries = 1, to = 0, x = 0; |
| 169 |
double tosec; |
| 170 |
char *argcopy = NULL; |
| 171 |
char *opt_args[OPT_ARG_ARRAY_SIZE]; |
| 172 |
struct ast_tone_zone_sound *ts = NULL; |
| 173 |
struct ast_flags flags = {0}; |
| 174 |
const char *status = "ERROR"; |
| 175 |
char *terminator = "#"; |
| 176 |
|
| 177 |
AST_DECLARE_APP_ARGS(arglist, |
| 178 |
AST_APP_ARG(variable); |
| 179 |
AST_APP_ARG(filename); |
| 180 |
AST_APP_ARG(maxdigits); |
| 181 |
AST_APP_ARG(options); |
| 182 |
AST_APP_ARG(attempts); |
| 183 |
AST_APP_ARG(timeout); |
| 184 |
); |
| 185 |
|
| 186 |
pbx_builtin_setvar_helper(chan, "READSTATUS", status); |
| 187 |
if (ast_strlen_zero(data)) { |
| 188 |
ast_log(LOG_WARNING, "Read requires an argument (variable)\n"); |
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
argcopy = ast_strdupa(data); |
| 193 |
|
| 194 |
AST_STANDARD_APP_ARGS(arglist, argcopy); |
| 195 |
|
| 196 |
if (!ast_strlen_zero(arglist.options)) { |
| 197 |
ast_app_parse_options(read_app_options, &flags, opt_args, arglist.options); |
| 198 |
} |
| 199 |
|
| 200 |
if (!ast_strlen_zero(arglist.attempts)) { |
| 201 |
tries = atoi(arglist.attempts); |
| 202 |
if (tries <= 0) |
| 203 |
tries = 1; |
| 204 |
} |
| 205 |
|
| 206 |
if (!ast_strlen_zero(arglist.timeout)) { |
| 207 |
tosec = atof(arglist.timeout); |
| 208 |
if (tosec <= 0) |
| 209 |
to = 0; |
| 210 |
else |
| 211 |
to = tosec * 1000.0; |
| 212 |
} |
| 213 |
|
| 214 |
if (ast_strlen_zero(arglist.filename)) { |
| 215 |
arglist.filename = NULL; |
| 216 |
} |
| 217 |
if (!ast_strlen_zero(arglist.maxdigits)) { |
| 218 |
maxdigits = atoi(arglist.maxdigits); |
| 219 |
if ((maxdigits < 1) || (maxdigits > 255)) { |
| 220 |
maxdigits = 255; |
| 221 |
} else |
| 222 |
ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits); |
| 223 |
} |
| 224 |
if (ast_strlen_zero(arglist.variable)) { |
| 225 |
ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n"); |
| 226 |
return 0; |
| 227 |
} |
| 228 |
if (ast_test_flag(&flags, OPT_INDICATION)) { |
| 229 |
if (!ast_strlen_zero(arglist.filename)) { |
| 230 |
ts = ast_get_indication_tone(ast_channel_zone(chan), arglist.filename); |
| 231 |
} |
| 232 |
} |
| 233 |
if (ast_test_flag(&flags, OPT_TERMINATOR)) { |
| 234 |
if (!ast_strlen_zero(opt_args[OPT_ARG_TERMINATOR])) { |
| 235 |
terminator = opt_args[OPT_ARG_TERMINATOR]; |
| 236 |
} else { |
| 237 |
terminator = ""; |
| 238 |
} |
| 239 |
} |
| 240 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 241 |
if (ast_test_flag(&flags, OPT_SKIP)) { |
| 242 |
|
| 243 |
if (ts) { |
| 244 |
ts = ast_tone_zone_sound_unref(ts); |
| 245 |
} |
| 246 |
pbx_builtin_setvar_helper(chan, arglist.variable, ""); |
| 247 |
pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED"); |
| 248 |
return 0; |
| 249 |
} else if (!ast_test_flag(&flags, OPT_NOANSWER)) { |
| 250 |
|
| 251 |
res = ast_answer(chan); |
| 252 |
} |
| 253 |
} |
| 254 |
if (!res) { |
| 255 |
while (tries && !res) { |
| 256 |
ast_stopstream(chan); |
| 257 |
if (ts && ts->data[0]) { |
| 258 |
if (!to) |
| 259 |
to = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 6000; |
| 260 |
res = ast_playtones_start(chan, 0, ts->data, 0); |
| 261 |
for (x = 0; x < maxdigits; ) { |
| 262 |
res = ast_waitfordigit(chan, to); |
| 263 |
ast_playtones_stop(chan); |
| 264 |
if (res < 1) { |
| 265 |
if (res == 0) |
| 266 |
status = "TIMEOUT"; |
| 267 |
tmp[x]='\0'; |
| 268 |
break; |
| 269 |
} |
| 270 |
tmp[x++] = res; |
| 271 |
if (terminator && strchr(terminator, tmp[x-1])) { |
| 272 |
tmp[x-1] = '\0'; |
| 273 |
status = "OK"; |
| 274 |
break; |
| 275 |
} |
| 276 |
if (x >= maxdigits) { |
| 277 |
status = "OK"; |
| 278 |
} |
| 279 |
} |
| 280 |
} else { |
| 281 |
res = ast_app_getdata_terminator(chan, arglist.filename, tmp, maxdigits, to, terminator); |
| 282 |
if (res == AST_GETDATA_COMPLETE) { |
| 283 |
status = "OK"; |
| 284 |
} else if (res == AST_GETDATA_EMPTY_END_TERMINATED) { |
| 285 |
if (ast_test_flag(&flags, OPT_KEEP_TERMINATOR)) { |
| 286 |
|
| 287 |
|
| 288 |
ast_copy_string(tmp, terminator, sizeof(tmp)); |
| 289 |
} |
| 290 |
status = "OK"; |
| 291 |
} else if (res == AST_GETDATA_TIMEOUT) { |
| 292 |
status = "TIMEOUT"; |
| 293 |
} else if (res == AST_GETDATA_INTERRUPTED) { |
| 294 |
status = "INTERRUPTED"; |
| 295 |
} |
| 296 |
} |
| 297 |
if (res > -1) { |
| 298 |
pbx_builtin_setvar_helper(chan, arglist.variable, tmp); |
| 299 |
if (!ast_strlen_zero(tmp)) { |
| 300 |
ast_verb(3, "User entered '%s'\n", tmp); |
| 301 |
tries = 0; |
| 302 |
} else { |
| 303 |
tries--; |
| 304 |
if (tries) |
| 305 |
ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : ""); |
| 306 |
else |
| 307 |
ast_verb(3, "User entered nothing.\n"); |
| 308 |
} |
| 309 |
res = 0; |
| 310 |
} else { |
| 311 |
pbx_builtin_setvar_helper(chan, arglist.variable, tmp); |
| 312 |
ast_verb(3, "User disconnected\n"); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
if (ts) { |
| 318 |
ts = ast_tone_zone_sound_unref(ts); |
| 319 |
} |
| 320 |
|
| 321 |
if (ast_check_hangup(chan)) |
| 322 |
status = "HANGUP"; |
| 323 |
pbx_builtin_setvar_helper(chan, "READSTATUS", status); |
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
static int unload_module(void) |
| 328 |
{ |
| 329 |
return ast_unregister_application(app); |
| 330 |
} |
| 331 |
|
| 332 |
static int load_module(void) |
| 333 |
{ |
| 334 |
return ast_register_application_xml(app, read_exec); |
| 335 |
} |
| 336 |
|
| 337 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read Variable Application"); |