| 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 <ctype.h> |
| 34 |
|
| 35 |
#include "asterisk/paths.h" |
| 36 |
#include "asterisk/file.h" |
| 37 |
#include "asterisk/pbx.h" |
| 38 |
#include "asterisk/module.h" |
| 39 |
#include "asterisk/say.h" |
| 40 |
#include "asterisk/app.h" |
| 41 |
#include "asterisk/utils.h" |
| 42 |
#include "asterisk/adsi.h" |
| 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 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
static const char app[] = "Directory"; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
#define VOICEMAIL_CONFIG "voicemail.conf" |
| 166 |
|
| 167 |
enum { |
| 168 |
OPT_LISTBYFIRSTNAME = (1 << 0), |
| 169 |
OPT_SAYEXTENSION = (1 << 1), |
| 170 |
OPT_FROMVOICEMAIL = (1 << 2), |
| 171 |
OPT_SELECTFROMMENU = (1 << 3), |
| 172 |
OPT_LISTBYLASTNAME = (1 << 4), |
| 173 |
OPT_LISTBYEITHER = OPT_LISTBYFIRSTNAME | OPT_LISTBYLASTNAME, |
| 174 |
OPT_PAUSE = (1 << 5), |
| 175 |
OPT_NOANSWER = (1 << 6), |
| 176 |
OPT_ALIAS = (1 << 7), |
| 177 |
OPT_CONFIG_FILE = (1 << 8), |
| 178 |
OPT_SKIP = (1 << 9), |
| 179 |
OPT_ADSI = (1 << 10), |
| 180 |
}; |
| 181 |
|
| 182 |
enum { |
| 183 |
OPT_ARG_FIRSTNAME = 0, |
| 184 |
OPT_ARG_LASTNAME = 1, |
| 185 |
OPT_ARG_EITHER = 2, |
| 186 |
OPT_ARG_PAUSE = 3, |
| 187 |
OPT_ARG_FILENAME = 4, |
| 188 |
|
| 189 |
OPT_ARG_ARRAY_SIZE = 5, |
| 190 |
}; |
| 191 |
|
| 192 |
struct directory_item { |
| 193 |
char exten[AST_MAX_EXTENSION + 1]; |
| 194 |
char name[AST_MAX_EXTENSION + 1]; |
| 195 |
char context[AST_MAX_CONTEXT + 1]; |
| 196 |
char key[50]; |
| 197 |
|
| 198 |
AST_LIST_ENTRY(directory_item) entry; |
| 199 |
}; |
| 200 |
|
| 201 |
AST_APP_OPTIONS(directory_app_options, { |
| 202 |
AST_APP_OPTION_ARG('f', OPT_LISTBYFIRSTNAME, OPT_ARG_FIRSTNAME), |
| 203 |
AST_APP_OPTION_ARG('l', OPT_LISTBYLASTNAME, OPT_ARG_LASTNAME), |
| 204 |
AST_APP_OPTION_ARG('b', OPT_LISTBYEITHER, OPT_ARG_EITHER), |
| 205 |
AST_APP_OPTION_ARG('p', OPT_PAUSE, OPT_ARG_PAUSE), |
| 206 |
AST_APP_OPTION('e', OPT_SAYEXTENSION), |
| 207 |
AST_APP_OPTION('v', OPT_FROMVOICEMAIL), |
| 208 |
AST_APP_OPTION('m', OPT_SELECTFROMMENU), |
| 209 |
AST_APP_OPTION('n', OPT_NOANSWER), |
| 210 |
AST_APP_OPTION('a', OPT_ALIAS), |
| 211 |
AST_APP_OPTION_ARG('c', OPT_CONFIG_FILE, OPT_ARG_FILENAME), |
| 212 |
AST_APP_OPTION('s', OPT_SKIP), |
| 213 |
AST_APP_OPTION('d', OPT_ADSI), |
| 214 |
}); |
| 215 |
|
| 216 |
static int adsi_search_input(struct ast_channel *chan) |
| 217 |
{ |
| 218 |
unsigned char buf[256]; |
| 219 |
int bytes = 0; |
| 220 |
unsigned char keys[6]; |
| 221 |
|
| 222 |
memset(keys, 0, sizeof(keys)); |
| 223 |
|
| 224 |
bytes += ast_adsi_display(buf + bytes, ADSI_COMM_PAGE, 3, ADSI_JUST_CENT, 0, " ", ""); |
| 225 |
bytes += ast_adsi_display(buf + bytes, ADSI_COMM_PAGE, 4, ADSI_JUST_CENT, 0, " ", ""); |
| 226 |
bytes += ast_adsi_set_line(buf + bytes, ADSI_COMM_PAGE, 1); |
| 227 |
bytes += ast_adsi_input_format(buf + bytes, 1, ADSI_DIR_FROM_LEFT, 0, "Query: ***", ""); |
| 228 |
bytes += ast_adsi_input_control(buf + bytes, ADSI_COMM_PAGE, 4, 1, 1, ADSI_JUST_LEFT); |
| 229 |
bytes += ast_adsi_load_soft_key(buf + bytes, ADSI_KEY_APPS + 3, "Search", "Search", "#", 1); |
| 230 |
bytes += ast_adsi_set_keys(buf + bytes, keys); |
| 231 |
bytes += ast_adsi_voice_mode(buf + bytes, 0); |
| 232 |
|
| 233 |
ast_debug(3, "Sending ADSI search input screen on %s\n", ast_channel_name(chan)); |
| 234 |
|
| 235 |
return ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DISPLAY); |
| 236 |
} |
| 237 |
|
| 238 |
static int adsi_confirm_match(struct ast_channel *chan, int seq, int total, const char *exten, const char *name, int showexten) |
| 239 |
{ |
| 240 |
unsigned char buf[4096]; |
| 241 |
int alignments[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT}; |
| 242 |
char *lines[5] = {NULL, NULL, NULL, NULL, NULL}; |
| 243 |
int x, bytes = 0; |
| 244 |
unsigned char keys[8]; |
| 245 |
char matchbuf[32]; |
| 246 |
|
| 247 |
snprintf(matchbuf, sizeof(matchbuf), "%d of %d", seq + 1, total); |
| 248 |
|
| 249 |
lines[0] = " "; |
| 250 |
lines[1] = matchbuf; |
| 251 |
lines[2] = (char*) name; |
| 252 |
|
| 253 |
if (showexten) { |
| 254 |
|
| 255 |
lines[3] = (char*) exten; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
for (x = 0; lines[x]; x++) { |
| 260 |
bytes += ast_adsi_display(buf + bytes, ADSI_INFO_PAGE, x + 1, alignments[x], 0, lines[x], ""); |
| 261 |
} |
| 262 |
bytes += ast_adsi_set_line(buf + bytes, ADSI_INFO_PAGE, 1); |
| 263 |
|
| 264 |
keys[3] = ADSI_KEY_APPS + 3; |
| 265 |
keys[4] = ADSI_KEY_APPS + 4; |
| 266 |
keys[5] = ADSI_KEY_APPS + 5; |
| 267 |
|
| 268 |
bytes += ast_adsi_load_soft_key(buf + bytes, ADSI_KEY_APPS + 3, "Dial", "Dial", "1", 0); |
| 269 |
bytes += ast_adsi_load_soft_key(buf + bytes, ADSI_KEY_APPS + 4, "Next", "Next", "*", 0); |
| 270 |
bytes += ast_adsi_load_soft_key(buf + bytes, ADSI_KEY_APPS + 5, "Exit", "Exit", "#", 0); |
| 271 |
bytes += ast_adsi_set_keys(buf + bytes, keys); |
| 272 |
bytes += ast_adsi_voice_mode(buf + bytes, 0); |
| 273 |
|
| 274 |
ast_debug(3, "Sending ADSI confirmation menu for %s\n", name); |
| 275 |
|
| 276 |
return ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DISPLAY); |
| 277 |
} |
| 278 |
|
| 279 |
static int compare(const char *text, const char *template) |
| 280 |
{ |
| 281 |
char digit; |
| 282 |
|
| 283 |
if (ast_strlen_zero(text)) { |
| 284 |
return -1; |
| 285 |
} |
| 286 |
|
| 287 |
while (*template) { |
| 288 |
digit = toupper(*text++); |
| 289 |
switch (digit) { |
| 290 |
case 0: |
| 291 |
return -1; |
| 292 |
case '1': |
| 293 |
digit = '1'; |
| 294 |
break; |
| 295 |
case '2': |
| 296 |
case 'A': |
| 297 |
case 'B': |
| 298 |
case 'C': |
| 299 |
digit = '2'; |
| 300 |
break; |
| 301 |
case '3': |
| 302 |
case 'D': |
| 303 |
case 'E': |
| 304 |
case 'F': |
| 305 |
digit = '3'; |
| 306 |
break; |
| 307 |
case '4': |
| 308 |
case 'G': |
| 309 |
case 'H': |
| 310 |
case 'I': |
| 311 |
digit = '4'; |
| 312 |
break; |
| 313 |
case '5': |
| 314 |
case 'J': |
| 315 |
case 'K': |
| 316 |
case 'L': |
| 317 |
digit = '5'; |
| 318 |
break; |
| 319 |
case '6': |
| 320 |
case 'M': |
| 321 |
case 'N': |
| 322 |
case 'O': |
| 323 |
digit = '6'; |
| 324 |
break; |
| 325 |
case '7': |
| 326 |
case 'P': |
| 327 |
case 'Q': |
| 328 |
case 'R': |
| 329 |
case 'S': |
| 330 |
digit = '7'; |
| 331 |
break; |
| 332 |
case '8': |
| 333 |
case 'T': |
| 334 |
case 'U': |
| 335 |
case 'V': |
| 336 |
digit = '8'; |
| 337 |
break; |
| 338 |
case '9': |
| 339 |
case 'W': |
| 340 |
case 'X': |
| 341 |
case 'Y': |
| 342 |
case 'Z': |
| 343 |
digit = '9'; |
| 344 |
break; |
| 345 |
|
| 346 |
default: |
| 347 |
if (digit > ' ') |
| 348 |
return -1; |
| 349 |
continue; |
| 350 |
} |
| 351 |
|
| 352 |
if (*template++ != digit) |
| 353 |
return -1; |
| 354 |
} |
| 355 |
|
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
static int goto_exten(struct ast_channel *chan, const char *dialcontext, char *ext) |
| 360 |
{ |
| 361 |
if (!ast_goto_if_exists(chan, S_OR(dialcontext, ast_channel_context(chan)), ext, 1)) { |
| 362 |
return 0; |
| 363 |
} else { |
| 364 |
ast_log(LOG_WARNING, "Can't find extension '%s' in current context. " |
| 365 |
"Not Exiting the Directory!\n", ext); |
| 366 |
return -1; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
static int play_mailbox_owner(struct ast_channel *chan, const char *context, |
| 376 |
const char *ext, const char *name, struct ast_flags *flags) |
| 377 |
{ |
| 378 |
int res = 0; |
| 379 |
char *mailbox_id; |
| 380 |
|
| 381 |
mailbox_id = ast_alloca(strlen(ext) + strlen(context) + 2); |
| 382 |
sprintf(mailbox_id, "%s@%s", ext, context); |
| 383 |
|
| 384 |
res = ast_app_sayname(chan, mailbox_id); |
| 385 |
if (res >= 0) { |
| 386 |
ast_stopstream(chan); |
| 387 |
|
| 388 |
if (ast_test_flag(flags, OPT_SAYEXTENSION)) { |
| 389 |
ast_stream_and_wait(chan, "vm-extension", AST_DIGIT_ANY); |
| 390 |
res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, ast_channel_language(chan), AST_SAY_CASE_NONE); |
| 391 |
} |
| 392 |
} else { |
| 393 |
res = ast_say_character_str(chan, S_OR(name, ext), AST_DIGIT_ANY, ast_channel_language(chan), AST_SAY_CASE_NONE); |
| 394 |
if (!ast_strlen_zero(name) && ast_test_flag(flags, OPT_SAYEXTENSION)) { |
| 395 |
ast_stream_and_wait(chan, "vm-extension", AST_DIGIT_ANY); |
| 396 |
res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, ast_channel_language(chan), AST_SAY_CASE_NONE); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
return res; |
| 401 |
} |
| 402 |
|
| 403 |
static int select_entry(struct ast_channel *chan, const char *dialcontext, const struct directory_item *item, struct ast_flags *flags) |
| 404 |
{ |
| 405 |
ast_debug(1, "Selecting '%s' - %s@%s\n", item->name, item->exten, S_OR(dialcontext, item->context)); |
| 406 |
|
| 407 |
if (ast_test_flag(flags, OPT_FROMVOICEMAIL)) { |
| 408 |
|
| 409 |
ast_channel_exten_set(chan, item->exten); |
| 410 |
} else if (ast_test_flag(flags, OPT_SKIP)) { |
| 411 |
|
| 412 |
pbx_builtin_setvar_helper(chan, "DIRECTORY_EXTEN", item->exten); |
| 413 |
} else if (ast_goto_if_exists(chan, S_OR(dialcontext, item->context), item->exten, 1)) { |
| 414 |
ast_log(LOG_WARNING, |
| 415 |
"Can't find extension '%s' in context '%s'. " |
| 416 |
"Did you pass the wrong context to Directory?\n", |
| 417 |
item->exten, S_OR(dialcontext, item->context)); |
| 418 |
return -1; |
| 419 |
} |
| 420 |
|
| 421 |
pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "SELECTED"); |
| 422 |
return 0; |
| 423 |
} |
| 424 |
|
| 425 |
static int select_item_pause(struct ast_channel *chan, struct ast_flags *flags, char *opts[]) |
| 426 |
{ |
| 427 |
int res = 0, opt_pause = 0; |
| 428 |
|
| 429 |
if (ast_test_flag(flags, OPT_PAUSE) && !ast_strlen_zero(opts[OPT_ARG_PAUSE])) { |
| 430 |
opt_pause = atoi(opts[OPT_ARG_PAUSE]); |
| 431 |
if (opt_pause > 3000) { |
| 432 |
opt_pause = 3000; |
| 433 |
} |
| 434 |
res = ast_waitfordigit(chan, opt_pause); |
| 435 |
} |
| 436 |
return res; |
| 437 |
} |
| 438 |
|
| 439 |
static int select_item_seq(struct ast_channel *chan, struct directory_item **items, int count, const char *dialcontext, struct ast_flags *flags, char *opts[]) |
| 440 |
{ |
| 441 |
struct directory_item *item, **ptr; |
| 442 |
int i, res, loop; |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
res = select_item_pause(chan, flags, opts); |
| 447 |
|
| 448 |
for (ptr = items, i = 0; i < count; i++, ptr++) { |
| 449 |
item = *ptr; |
| 450 |
|
| 451 |
if (ast_test_flag(flags, OPT_ADSI) && adsi_confirm_match(chan, i, count, item->exten, item->name, ast_test_flag(flags, OPT_SAYEXTENSION))) { |
| 452 |
return -1; |
| 453 |
} |
| 454 |
|
| 455 |
for (loop = 3 ; loop > 0; loop--) { |
| 456 |
if (!res) |
| 457 |
res = play_mailbox_owner(chan, item->context, item->exten, item->name, flags); |
| 458 |
if (!res) |
| 459 |
res = ast_stream_and_wait(chan, "dir-instr", AST_DIGIT_ANY); |
| 460 |
if (!res) |
| 461 |
res = ast_waitfordigit(chan, 3000); |
| 462 |
ast_stopstream(chan); |
| 463 |
|
| 464 |
if (res == '0') { |
| 465 |
goto_exten(chan, dialcontext, "o"); |
| 466 |
pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "OPERATOR"); |
| 467 |
return '0'; |
| 468 |
} else if (res == '1') { |
| 469 |
return select_entry(chan, dialcontext, item, flags) ? -1 : 1; |
| 470 |
} else if (res == '*') { |
| 471 |
|
| 472 |
break; |
| 473 |
} else if (res == '#') { |
| 474 |
|
| 475 |
pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "USEREXIT"); |
| 476 |
return res; |
| 477 |
} |
| 478 |
|
| 479 |
if (res < 0) |
| 480 |
return -1; |
| 481 |
|
| 482 |
res = 0; |
| 483 |
} |
| 484 |
res = 0; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
return 0; |
| 489 |
} |
| 490 |
|
| 491 |
static int select_item_menu(struct ast_channel *chan, struct directory_item **items, int count, const char *dialcontext, struct ast_flags *flags, char *opts[]) |
| 492 |
{ |
| 493 |
struct directory_item **block, *item; |
| 494 |
int i, limit, res = 0; |
| 495 |
char buf[7+12]; |
| 496 |
|
| 497 |
|
| 498 |
select_item_pause(chan, flags, opts); |
| 499 |
|
| 500 |
for (block = items; count; block += limit, count -= limit) { |
| 501 |
limit = count; |
| 502 |
if (limit > 8) |
| 503 |
limit = 8; |
| 504 |
|
| 505 |
for (i = 0; i < limit && !res; i++) { |
| 506 |
item = block[i]; |
| 507 |
|
| 508 |
snprintf(buf, sizeof(buf), "digits/%d", i + 1); |
| 509 |
|
| 510 |
res = ast_streamfile(chan, "dir-multi1", ast_channel_language(chan)); |
| 511 |
if (!res) |
| 512 |
res = ast_waitstream(chan, AST_DIGIT_ANY); |
| 513 |
if (!res) |
| 514 |
res = ast_streamfile(chan, buf, ast_channel_language(chan)); |
| 515 |
if (!res) |
| 516 |
res = ast_waitstream(chan, AST_DIGIT_ANY); |
| 517 |
if (!res) |
| 518 |
res = ast_streamfile(chan, "dir-multi2", ast_channel_language(chan)); |
| 519 |
if (!res) |
| 520 |
res = ast_waitstream(chan, AST_DIGIT_ANY); |
| 521 |
if (!res) |
| 522 |
res = play_mailbox_owner(chan, item->context, item->exten, item->name, flags); |
| 523 |
if (!res) |
| 524 |
res = ast_waitstream(chan, AST_DIGIT_ANY); |
| 525 |
if (!res) |
| 526 |
res = ast_waitfordigit(chan, 800); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
if (!res && count > limit) { |
| 531 |
res = ast_streamfile(chan, "dir-multi9", ast_channel_language(chan)); |
| 532 |
if (!res) |
| 533 |
res = ast_waitstream(chan, AST_DIGIT_ANY); |
| 534 |
} |
| 535 |
|
| 536 |
if (!res) { |
| 537 |
res = ast_waitfordigit(chan, 3000); |
| 538 |
} |
| 539 |
|
| 540 |
if (res && res > '0' && res < '1' + limit) { |
| 541 |
pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "SELECTED"); |
| 542 |
return select_entry(chan, dialcontext, block[res - '1'], flags) ? -1 : 1; |
| 543 |
} |
| 544 |
|
| 545 |
if (res < 0) |
| 546 |
return -1; |
| 547 |
|
| 548 |
res = 0; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
return 0; |
| 553 |
} |
| 554 |
|
| 555 |
AST_THREADSTORAGE(commonbuf); |
| 556 |
|
| 557 |
static struct ast_config *realtime_directory(char *context, const char *filename) |
| 558 |
{ |
| 559 |
struct ast_config *cfg; |
| 560 |
struct ast_config *rtdata = NULL; |
| 561 |
struct ast_category *cat; |
| 562 |
struct ast_variable *var; |
| 563 |
char *category = NULL; |
| 564 |
const char *fullname; |
| 565 |
const char *hidefromdir, *searchcontexts = NULL; |
| 566 |
struct ast_flags config_flags = { 0 }; |
| 567 |
struct ast_str *tmp = ast_str_thread_get(&commonbuf, 100); |
| 568 |
|
| 569 |
if (!tmp) { |
| 570 |
return NULL; |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
cfg = ast_config_load(filename, config_flags); |
| 575 |
|
| 576 |
if (!cfg) { |
| 577 |
|
| 578 |
ast_log(LOG_WARNING, "Loading config failed.\n"); |
| 579 |
return NULL; |
| 580 |
} else if (cfg == CONFIG_STATUS_FILEINVALID) { |
| 581 |
ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", filename); |
| 582 |
return NULL; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
if (ast_strlen_zero(context) && (searchcontexts = ast_variable_retrieve(cfg, "general", "searchcontexts"))) { |
| 588 |
if (ast_true(searchcontexts)) { |
| 589 |
rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", SENTINEL); |
| 590 |
context = NULL; |
| 591 |
} else { |
| 592 |
rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", "context", "default", SENTINEL); |
| 593 |
context = "default"; |
| 594 |
} |
| 595 |
} else if (!ast_strlen_zero(context)) { |
| 596 |
rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", "context", context, SENTINEL); |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
if (!rtdata) { |
| 601 |
return cfg; |
| 602 |
} |
| 603 |
|
| 604 |
while ((category = ast_category_browse(rtdata, category))) { |
| 605 |
const char *mailbox = ast_variable_retrieve(rtdata, category, "mailbox"); |
| 606 |
const char *ctx = ast_variable_retrieve(rtdata, category, "context"); |
| 607 |
|
| 608 |
if (ast_strlen_zero(mailbox)) { |
| 609 |
ast_debug(3, "Skipping result with missing or empty mailbox\n"); |
| 610 |
continue; |
| 611 |
} |
| 612 |
|
| 613 |
fullname = ast_variable_retrieve(rtdata, category, "fullname"); |
| 614 |
hidefromdir = ast_variable_retrieve(rtdata, category, "hidefromdir"); |
| 615 |
if (ast_true(hidefromdir)) { |
| 616 |
|
| 617 |
continue; |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
ast_str_set(&tmp, 0, "no-password,%s,,,", S_OR(fullname, "")); |
| 622 |
if (ast_variable_retrieve(rtdata, category, "alias")) { |
| 623 |
struct ast_variable *alias; |
| 624 |
for (alias = ast_variable_browse(rtdata, category); alias; alias = alias->next) { |
| 625 |
if (!strcasecmp(alias->name, "alias")) { |
| 626 |
ast_str_append(&tmp, 0, "|alias=%s", alias->value); |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
if (!(cat = ast_category_get(cfg, ctx, NULL))) { |
| 633 |
if (!(cat = ast_category_new_dynamic(ctx))) { |
| 634 |
ast_log(LOG_WARNING, "Out of memory\n"); |
| 635 |
ast_config_destroy(cfg); |
| 636 |
if (rtdata) { |
| 637 |
ast_config_destroy(rtdata); |
| 638 |
} |
| 639 |
return NULL; |
| 640 |
} |
| 641 |
ast_category_append(cfg, cat); |
| 642 |
} |
| 643 |
|
| 644 |
if ((var = ast_variable_new(mailbox, ast_str_buffer(tmp), ""))) { |
| 645 |
ast_variable_append(cat, var); |
| 646 |
} else { |
| 647 |
ast_log(LOG_WARNING, "Out of memory adding mailbox '%s'\n", mailbox); |
| 648 |
} |
| 649 |
} |
| 650 |
ast_config_destroy(rtdata); |
| 651 |
|
| 652 |
return cfg; |
| 653 |
} |
| 654 |
|
| 655 |
static int check_match(struct directory_item **result, const char *item_context, const char *item_fullname, const char *item_ext, const char *pattern_ext, int use_first_name) |
| 656 |
{ |
| 657 |
struct directory_item *item; |
| 658 |
const char *key = NULL; |
| 659 |
int namelen; |
| 660 |
|
| 661 |
if (ast_strlen_zero(item_fullname)) { |
| 662 |
return 0; |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
if (!use_first_name) |
| 667 |
key = strchr(item_fullname, ' '); |
| 668 |
|
| 669 |
if (key) |
| 670 |
key++; |
| 671 |
else |
| 672 |
key = item_fullname; |
| 673 |
|
| 674 |
if (compare(key, pattern_ext)) |
| 675 |
return 0; |
| 676 |
|
| 677 |
ast_debug(1, "Found match %s@%s\n", item_ext, item_context); |
| 678 |
|
| 679 |
|
| 680 |
item = ast_calloc(1, sizeof(*item)); |
| 681 |
if (!item) |
| 682 |
return -1; |
| 683 |
ast_copy_string(item->context, item_context, sizeof(item->context)); |
| 684 |
ast_copy_string(item->name, item_fullname, sizeof(item->name)); |
| 685 |
ast_copy_string(item->exten, item_ext, sizeof(item->exten)); |
| 686 |
|
| 687 |
ast_copy_string(item->key, key, sizeof(item->key)); |
| 688 |
if (key != item_fullname) { |
| 689 |
|
| 690 |
namelen = key - item_fullname - 1; |
| 691 |
if (namelen > sizeof(item->key) - strlen(item->key) - 1) |
| 692 |
namelen = sizeof(item->key) - strlen(item->key) - 1; |
| 693 |
strncat(item->key, item_fullname, namelen); |
| 694 |
} |
| 695 |
|
| 696 |
*result = item; |
| 697 |
return 1; |
| 698 |
} |
| 699 |
|
| 700 |
typedef AST_LIST_HEAD_NOLOCK(, directory_item) itemlist; |
| 701 |
|
| 702 |
static int search_directory_sub(const char *context, struct ast_config *vmcfg, struct ast_config *ucfg, const char *ext, struct ast_flags flags, itemlist *alist) |
| 703 |
{ |
| 704 |
struct ast_variable *v; |
| 705 |
struct ast_str *buf = ast_str_thread_get(&commonbuf, 100); |
| 706 |
char *name; |
| 707 |
char *options; |
| 708 |
char *alias; |
| 709 |
char *cat; |
| 710 |
struct directory_item *item; |
| 711 |
int res; |
| 712 |
|
| 713 |
if (!buf) { |
| 714 |
return -1; |
| 715 |
} |
| 716 |
|
| 717 |
ast_debug(2, "Pattern: %s\n", ext); |
| 718 |
|
| 719 |
for (v = ast_variable_browse(vmcfg, context); v; v = v->next) { |
| 720 |
ast_str_set(&buf, 0, "%s", v->value); |
| 721 |
options = ast_str_buffer(buf); |
| 722 |
|
| 723 |
|
| 724 |
strsep(&options, ","); |
| 725 |
name = strsep(&options, ","); |
| 726 |
strsep(&options, ","); |
| 727 |
strsep(&options, ","); |
| 728 |
|
| 729 |
|
| 730 |
if (options && strcasestr(options, "hidefromdir=yes")) { |
| 731 |
|
| 732 |
continue; |
| 733 |
} |
| 734 |
if (ast_strlen_zero(name)) { |
| 735 |
|
| 736 |
continue; |
| 737 |
} |
| 738 |
|
| 739 |
res = 0; |
| 740 |
if (ast_test_flag(&flags, OPT_LISTBYLASTNAME)) { |
| 741 |
res = check_match(&item, context, name, v->name, ext, 0 ); |
| 742 |
} |
| 743 |
if (!res && ast_test_flag(&flags, OPT_LISTBYFIRSTNAME)) { |
| 744 |
res = check_match(&item, context, name, v->name, ext, 1 ); |
| 745 |
} |
| 746 |
if (!res && ast_test_flag(&flags, OPT_ALIAS) |
| 747 |
&& options && (alias = strcasestr(options, "alias="))) { |
| 748 |
char *a; |
| 749 |
|
| 750 |
ast_debug(1, "Found alias: %s\n", alias); |
| 751 |
while ((a = strsep(&alias, "|"))) { |
| 752 |
if (!strncasecmp(a, "alias=", 6)) { |
| 753 |
if ((res = check_match(&item, context, a + 6, v->name, ext, 1))) { |
| 754 |
break; |
| 755 |
} |
| 756 |
} |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
if (!res) { |
| 761 |
continue; |
| 762 |
} else if (res < 0) { |
| 763 |
return -1; |
| 764 |
} |
| 765 |
|
| 766 |
AST_LIST_INSERT_TAIL(alist, item, entry); |
| 767 |
} |
| 768 |
|
| 769 |
if (ucfg) { |
| 770 |
for (cat = ast_category_browse(ucfg, NULL); cat ; cat = ast_category_browse(ucfg, cat)) { |
| 771 |
const char *position; |
| 772 |
|
| 773 |
if (!strcasecmp(cat, "general")) { |
| 774 |
continue; |
| 775 |
} |
| 776 |
if (!ast_true(ast_config_option(ucfg, cat, "hasdirectory"))) { |
| 777 |
continue; |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
if (!(position = ast_variable_retrieve(ucfg, cat, "fullname"))) { |
| 782 |
continue; |
| 783 |
} |
| 784 |
|
| 785 |
res = 0; |
| 786 |
if (ast_test_flag(&flags, OPT_LISTBYLASTNAME)) { |
| 787 |
res = check_match(&item, context, position, cat, ext, 0 ); |
| 788 |
} |
| 789 |
if (!res && ast_test_flag(&flags, OPT_LISTBYFIRSTNAME)) { |
| 790 |
res = check_match(&item, context, position, cat, ext, 1 ); |
| 791 |
} |
| 792 |
if (!res && ast_test_flag(&flags, OPT_ALIAS)) { |
| 793 |
for (v = ast_variable_browse(ucfg, cat); v; v = v->next) { |
| 794 |
if (!strcasecmp(v->name, "alias") |
| 795 |
&& (res = check_match(&item, context, v->value, cat, ext, 1))) { |
| 796 |
break; |
| 797 |
} |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
if (!res) { |
| 802 |
continue; |
| 803 |
} else if (res < 0) { |
| 804 |
return -1; |
| 805 |
} |
| 806 |
|
| 807 |
AST_LIST_INSERT_TAIL(alist, item, entry); |
| 808 |
} |
| 809 |
} |
| 810 |
return 0; |
| 811 |
} |
| 812 |
|
| 813 |
static int search_directory(const char *context, struct ast_config *vmcfg, struct ast_config *ucfg, const char *ext, struct ast_flags flags, itemlist *alist) |
| 814 |
{ |
| 815 |
const char *searchcontexts = ast_variable_retrieve(vmcfg, "general", "searchcontexts"); |
| 816 |
if (ast_strlen_zero(context)) { |
| 817 |
if (!ast_strlen_zero(searchcontexts) && ast_true(searchcontexts)) { |
| 818 |
|
| 819 |
int res; |
| 820 |
const char *catg; |
| 821 |
for (catg = ast_category_browse(vmcfg, NULL); catg; catg = ast_category_browse(vmcfg, catg)) { |
| 822 |
if (!strcmp(catg, "general") || !strcmp(catg, "zonemessages")) { |
| 823 |
continue; |
| 824 |
} |
| 825 |
|
| 826 |
if ((res = search_directory_sub(catg, vmcfg, ucfg, ext, flags, alist))) { |
| 827 |
return res; |
| 828 |
} |
| 829 |
} |
| 830 |
return 0; |
| 831 |
} else { |
| 832 |
ast_debug(1, "Searching by category default\n"); |
| 833 |
return search_directory_sub("default", vmcfg, ucfg, ext, flags, alist); |
| 834 |
} |
| 835 |
} else { |
| 836 |
|
| 837 |
ast_debug(1, "Searching by category %s\n", context); |
| 838 |
return search_directory_sub(context, vmcfg, ucfg, ext, flags, alist); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
static void sort_items(struct directory_item **sorted, int count) |
| 843 |
{ |
| 844 |
int reordered, i; |
| 845 |
struct directory_item **ptr, *tmp; |
| 846 |
|
| 847 |
if (count < 2) |
| 848 |
return; |
| 849 |
|
| 850 |
|
| 851 |
do { |
| 852 |
reordered = 0; |
| 853 |
for (ptr = sorted, i = 0; i < count - 1; i++, ptr++) { |
| 854 |
if (strcasecmp(ptr[0]->key, ptr[1]->key) > 0) { |
| 855 |
tmp = ptr[0]; |
| 856 |
ptr[0] = ptr[1]; |
| 857 |
ptr[1] = tmp; |
| 858 |
reordered++; |
| 859 |
} |
| 860 |
} |
| 861 |
} while (reordered); |
| 862 |
} |
| 863 |
|
| 864 |
static int do_directory(struct ast_channel *chan, struct ast_config *vmcfg, struct ast_config *ucfg, char *context, char *dialcontext, char digit, int digits, struct ast_flags *flags, char *opts[]) |
| 865 |
{ |
| 866 |
|
| 867 |
int res = 0; |
| 868 |
itemlist alist = AST_LIST_HEAD_NOLOCK_INIT_VALUE; |
| 869 |
struct directory_item *item, **ptr, **sorted = NULL; |
| 870 |
int count, i; |
| 871 |
char ext[10] = ""; |
| 872 |
|
| 873 |
if (digit == '0' && !goto_exten(chan, dialcontext, "o")) { |
| 874 |
pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "OPERATOR"); |
| 875 |
return digit; |
| 876 |
} |
| 877 |
|
| 878 |
if (digit == '*' && !goto_exten(chan, dialcontext, "a")) { |
| 879 |
pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "ASSISTANT"); |
| 880 |
return digit; |
| 881 |
} |
| 882 |
|
| 883 |
ext[0] = digit; |
| 884 |
if (ast_readstring(chan, ext + 1, digits - 1, 3000, 3000, "#") < 0) |
| 885 |
return -1; |
| 886 |
|
| 887 |
res = search_directory(context, vmcfg, ucfg, ext, *flags, &alist); |
| 888 |
if (res) |
| 889 |
goto exit; |
| 890 |
|
| 891 |
|
| 892 |
count = 0; |
| 893 |
AST_LIST_TRAVERSE(&alist, item, entry) { |
| 894 |
count++; |
| 895 |
} |
| 896 |
|
| 897 |
if (count < 1) { |
| 898 |
res = ast_streamfile(chan, "dir-nomatch", ast_channel_language(chan)); |
| 899 |
goto exit; |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
sorted = ast_calloc(count, sizeof(*sorted)); |
| 905 |
|
| 906 |
ptr = sorted; |
| 907 |
AST_LIST_TRAVERSE(&alist, item, entry) { |
| 908 |
*ptr++ = item; |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
sort_items(sorted, count); |
| 913 |
|
| 914 |
if (DEBUG_ATLEAST(2)) { |
| 915 |
ast_log(LOG_DEBUG, "Listing matching entries:\n"); |
| 916 |
for (ptr = sorted, i = 0; i < count; i++, ptr++) { |
| 917 |
ast_log(LOG_DEBUG, "%s: %s\n", ptr[0]->exten, ptr[0]->name); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
if (ast_test_flag(flags, OPT_SELECTFROMMENU)) { |
| 922 |
|
| 923 |
res = select_item_menu(chan, sorted, count, dialcontext, flags, opts); |
| 924 |
} else { |
| 925 |
|
| 926 |
res = select_item_seq(chan, sorted, count, dialcontext, flags, opts); |
| 927 |
} |
| 928 |
|
| 929 |
if (!res) { |
| 930 |
res = ast_streamfile(chan, "dir-nomore", ast_channel_language(chan)); |
| 931 |
} |
| 932 |
|
| 933 |
exit: |
| 934 |
if (sorted) |
| 935 |
ast_free(sorted); |
| 936 |
|
| 937 |
while ((item = AST_LIST_REMOVE_HEAD(&alist, entry))) |
| 938 |
ast_free(item); |
| 939 |
|
| 940 |
return res; |
| 941 |
} |
| 942 |
|
| 943 |
static int directory_exec(struct ast_channel *chan, const char *data) |
| 944 |
{ |
| 945 |
int res = 0, digit = 3; |
| 946 |
struct ast_config *cfg, *ucfg; |
| 947 |
const char *dirintro; |
| 948 |
char *parse, *opts[OPT_ARG_ARRAY_SIZE] = { 0, }; |
| 949 |
struct ast_flags flags = { 0 }; |
| 950 |
struct ast_flags config_flags = { 0 }; |
| 951 |
enum { FIRST, LAST, BOTH } which = LAST; |
| 952 |
char digits[9] = "digits/3"; |
| 953 |
AST_DECLARE_APP_ARGS(args, |
| 954 |
AST_APP_ARG(vmcontext); |
| 955 |
AST_APP_ARG(dialcontext); |
| 956 |
AST_APP_ARG(options); |
| 957 |
); |
| 958 |
|
| 959 |
parse = ast_strdupa(data); |
| 960 |
|
| 961 |
AST_STANDARD_APP_ARGS(args, parse); |
| 962 |
|
| 963 |
if (args.options && ast_app_parse_options(directory_app_options, &flags, opts, args.options)) |
| 964 |
return -1; |
| 965 |
|
| 966 |
cfg = realtime_directory(args.vmcontext, S_OR(opts[OPT_ARG_FILENAME], VOICEMAIL_CONFIG)); |
| 967 |
|
| 968 |
if (!cfg) { |
| 969 |
ast_log(LOG_ERROR, "Unable to read the configuration data!\n"); |
| 970 |
return -1; |
| 971 |
} |
| 972 |
|
| 973 |
if ((ucfg = ast_config_load("users.conf", config_flags)) == CONFIG_STATUS_FILEINVALID) { |
| 974 |
ast_log(LOG_ERROR, "Config file users.conf is in an invalid format. Aborting.\n"); |
| 975 |
ucfg = NULL; |
| 976 |
} |
| 977 |
|
| 978 |
dirintro = ast_variable_retrieve(cfg, args.vmcontext, "directoryintro"); |
| 979 |
if (ast_strlen_zero(dirintro)) |
| 980 |
dirintro = ast_variable_retrieve(cfg, "general", "directoryintro"); |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
if (ast_test_flag(&flags, OPT_LISTBYFIRSTNAME) && ast_test_flag(&flags, OPT_LISTBYLASTNAME)) { |
| 985 |
if (!ast_strlen_zero(opts[OPT_ARG_EITHER])) { |
| 986 |
digit = atoi(opts[OPT_ARG_EITHER]); |
| 987 |
} |
| 988 |
which = BOTH; |
| 989 |
} else if (ast_test_flag(&flags, OPT_LISTBYFIRSTNAME)) { |
| 990 |
if (!ast_strlen_zero(opts[OPT_ARG_FIRSTNAME])) { |
| 991 |
digit = atoi(opts[OPT_ARG_FIRSTNAME]); |
| 992 |
} |
| 993 |
which = FIRST; |
| 994 |
} else { |
| 995 |
if (!ast_strlen_zero(opts[OPT_ARG_LASTNAME])) { |
| 996 |
digit = atoi(opts[OPT_ARG_LASTNAME]); |
| 997 |
} |
| 998 |
which = LAST; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
if (!ast_test_flag(&flags, OPT_LISTBYFIRSTNAME) && !ast_test_flag(&flags, OPT_LISTBYLASTNAME)) { |
| 1003 |
ast_set_flag(&flags, OPT_LISTBYLASTNAME); |
| 1004 |
which = LAST; |
| 1005 |
} |
| 1006 |
|
| 1007 |
if (digit > 9) { |
| 1008 |
digit = 9; |
| 1009 |
} else if (digit < 1) { |
| 1010 |
digit = 3; |
| 1011 |
} |
| 1012 |
digits[7] = digit + '0'; |
| 1013 |
|
| 1014 |
if (ast_test_flag(&flags, OPT_ADSI)) { |
| 1015 |
if (!ast_adsi_available(chan)) { |
| 1016 |
ast_log(LOG_WARNING, "ADSI not available on %s\n", ast_channel_name(chan)); |
| 1017 |
ast_clear_flag(&flags, OPT_ADSI); |
| 1018 |
} else { |
| 1019 |
res = ast_adsi_load_session(chan, NULL, 0, 1); |
| 1020 |
if (res < 0) { |
| 1021 |
return res; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 1027 |
if (!ast_test_flag(&flags, OPT_NOANSWER)) { |
| 1028 |
|
| 1029 |
res = ast_answer(chan); |
| 1030 |
} |
| 1031 |
} |
| 1032 |
for (;;) { |
| 1033 |
if (ast_test_flag(&flags, OPT_ADSI) && adsi_search_input(chan)) { |
| 1034 |
return -1; |
| 1035 |
} |
| 1036 |
if (!ast_strlen_zero(dirintro) && !res) { |
| 1037 |
res = ast_stream_and_wait(chan, dirintro, AST_DIGIT_ANY); |
| 1038 |
} else if (!res) { |
| 1039 |
|
| 1040 |
res = ast_stream_and_wait(chan, "dir-welcome", AST_DIGIT_ANY); |
| 1041 |
if (!res) { |
| 1042 |
res = ast_stream_and_wait(chan, "dir-pls-enter", AST_DIGIT_ANY); |
| 1043 |
} |
| 1044 |
if (!res) { |
| 1045 |
res = ast_stream_and_wait(chan, digits, AST_DIGIT_ANY); |
| 1046 |
} |
| 1047 |
if (!res) { |
| 1048 |
res = ast_stream_and_wait(chan, |
| 1049 |
which == FIRST ? "dir-first" : |
| 1050 |
which == LAST ? "dir-last" : |
| 1051 |
"dir-firstlast", AST_DIGIT_ANY); |
| 1052 |
} |
| 1053 |
if (!res) { |
| 1054 |
res = ast_stream_and_wait(chan, "dir-usingkeypad", AST_DIGIT_ANY); |
| 1055 |
} |
| 1056 |
} |
| 1057 |
ast_stopstream(chan); |
| 1058 |
if (!res) |
| 1059 |
res = ast_waitfordigit(chan, 5000); |
| 1060 |
|
| 1061 |
if (res <= 0) { |
| 1062 |
if (res == 0) { |
| 1063 |
pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "TIMEOUT"); |
| 1064 |
} |
| 1065 |
break; |
| 1066 |
} |
| 1067 |
|
| 1068 |
res = do_directory(chan, cfg, ucfg, args.vmcontext, args.dialcontext, res, digit, &flags, opts); |
| 1069 |
if (res) |
| 1070 |
break; |
| 1071 |
|
| 1072 |
res = ast_waitstream(chan, AST_DIGIT_ANY); |
| 1073 |
ast_stopstream(chan); |
| 1074 |
if (res < 0) { |
| 1075 |
break; |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
if (ucfg) |
| 1080 |
ast_config_destroy(ucfg); |
| 1081 |
ast_config_destroy(cfg); |
| 1082 |
|
| 1083 |
if (ast_check_hangup(chan)) { |
| 1084 |
pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "HANGUP"); |
| 1085 |
} else if (res < 0) { |
| 1086 |
|
| 1087 |
pbx_builtin_setvar_helper(chan, "DIRECTORY_RESULT", "FAILED"); |
| 1088 |
} |
| 1089 |
|
| 1090 |
return res < 0 ? -1 : 0; |
| 1091 |
} |
| 1092 |
|
| 1093 |
static int unload_module(void) |
| 1094 |
{ |
| 1095 |
int res; |
| 1096 |
res = ast_unregister_application(app); |
| 1097 |
return res; |
| 1098 |
} |
| 1099 |
|
| 1100 |
static int load_module(void) |
| 1101 |
{ |
| 1102 |
return ast_register_application_xml(app, directory_exec); |
| 1103 |
} |
| 1104 |
|
| 1105 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Extension Directory"); |