| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/_private.h" |
| 33 |
#include "asterisk/paths.h" |
| 34 |
#include <ctype.h> |
| 35 |
#include <time.h> |
| 36 |
#include <sys/time.h> |
| 37 |
#if defined(HAVE_SYSINFO) |
| 38 |
#include <sys/sysinfo.h> |
| 39 |
#endif |
| 40 |
#if defined(SOLARIS) |
| 41 |
#include <sys/loadavg.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include "asterisk/lock.h" |
| 45 |
#include "asterisk/cli.h" |
| 46 |
#include "asterisk/pbx.h" |
| 47 |
#include "asterisk/channel.h" |
| 48 |
#include "asterisk/file.h" |
| 49 |
#include "asterisk/callerid.h" |
| 50 |
#include "asterisk/cdr.h" |
| 51 |
#include "asterisk/config.h" |
| 52 |
#include "asterisk/term.h" |
| 53 |
#include "asterisk/time.h" |
| 54 |
#include "asterisk/manager.h" |
| 55 |
#include "asterisk/ast_expr.h" |
| 56 |
#include "asterisk/linkedlists.h" |
| 57 |
#define SAY_STUBS |
| 58 |
#include "asterisk/say.h" |
| 59 |
#include "asterisk/utils.h" |
| 60 |
#include "asterisk/causes.h" |
| 61 |
#include "asterisk/musiconhold.h" |
| 62 |
#include "asterisk/app.h" |
| 63 |
#include "asterisk/devicestate.h" |
| 64 |
#include "asterisk/presencestate.h" |
| 65 |
#include "asterisk/hashtab.h" |
| 66 |
#include "asterisk/module.h" |
| 67 |
#include "asterisk/indications.h" |
| 68 |
#include "asterisk/taskprocessor.h" |
| 69 |
#include "asterisk/xmldoc.h" |
| 70 |
#include "asterisk/astobj2.h" |
| 71 |
#include "asterisk/stasis_channels.h" |
| 72 |
#include "asterisk/dial.h" |
| 73 |
#include "asterisk/vector.h" |
| 74 |
#include "pbx_private.h" |
| 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 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
#ifdef LOW_MEMORY |
| 229 |
#define EXT_DATA_SIZE 256 |
| 230 |
#else |
| 231 |
#define EXT_DATA_SIZE 8192 |
| 232 |
#endif |
| 233 |
|
| 234 |
#define SWITCH_DATA_LENGTH 256 |
| 235 |
|
| 236 |
#define VAR_NORMAL 1 |
| 237 |
#define VAR_SOFTTRAN 2 |
| 238 |
#define VAR_HARDTRAN 3 |
| 239 |
|
| 240 |
struct ast_context; |
| 241 |
struct ast_app; |
| 242 |
|
| 243 |
AST_THREADSTORAGE(switch_data); |
| 244 |
AST_THREADSTORAGE(extensionstate_buf); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
struct ast_exten { |
| 253 |
char *exten; |
| 254 |
char *name; |
| 255 |
int matchcid; |
| 256 |
const char *cidmatch; |
| 257 |
const char *cidmatch_display; |
| 258 |
int priority; |
| 259 |
const char *label; |
| 260 |
struct ast_context *parent; |
| 261 |
const char *app; |
| 262 |
struct ast_app *cached_app; |
| 263 |
void *data; |
| 264 |
void (*datad)(void *); |
| 265 |
struct ast_exten *peer; |
| 266 |
struct ast_hashtab *peer_table; |
| 267 |
struct ast_hashtab *peer_label_table; |
| 268 |
const char *registrar; |
| 269 |
const char *registrar_file; |
| 270 |
int registrar_line; |
| 271 |
struct ast_exten *next; |
| 272 |
char stuff[0]; |
| 273 |
}; |
| 274 |
|
| 275 |
|
| 276 |
struct match_char |
| 277 |
{ |
| 278 |
int is_pattern; |
| 279 |
int deleted; |
| 280 |
int specificity; |
| 281 |
struct match_char *alt_char; |
| 282 |
struct match_char *next_char; |
| 283 |
struct ast_exten *exten; |
| 284 |
char x[1]; |
| 285 |
}; |
| 286 |
|
| 287 |
struct scoreboard |
| 288 |
{ |
| 289 |
int total_specificity; |
| 290 |
int total_length; |
| 291 |
char last_char; |
| 292 |
int canmatch; |
| 293 |
struct match_char *node; |
| 294 |
struct ast_exten *canmatch_exten; |
| 295 |
struct ast_exten *exten; |
| 296 |
}; |
| 297 |
|
| 298 |
|
| 299 |
struct ast_context { |
| 300 |
const char *name; |
| 301 |
const char *registrar; |
| 302 |
|
| 303 |
ast_rwlock_t lock; |
| 304 |
struct ast_exten *root; |
| 305 |
struct ast_hashtab *root_table; |
| 306 |
struct match_char *pattern_tree; |
| 307 |
struct ast_context *next; |
| 308 |
struct ast_includes includes; |
| 309 |
struct ast_ignorepats ignorepats; |
| 310 |
struct ast_sws alts; |
| 311 |
int refcount; |
| 312 |
int autohints; |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
char data[]; |
| 320 |
}; |
| 321 |
|
| 322 |
|
| 323 |
struct ast_state_cb { |
| 324 |
|
| 325 |
int id; |
| 326 |
|
| 327 |
void *data; |
| 328 |
|
| 329 |
int extended; |
| 330 |
|
| 331 |
ast_state_cb_type change_cb; |
| 332 |
|
| 333 |
ast_state_cb_destroy_type destroy_cb; |
| 334 |
|
| 335 |
AST_LIST_ENTRY(ast_state_cb) entry; |
| 336 |
}; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
struct ast_hint { |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
struct ast_exten *exten; |
| 354 |
struct ao2_container *callbacks; |
| 355 |
|
| 356 |
|
| 357 |
int laststate; |
| 358 |
|
| 359 |
|
| 360 |
int last_presence_state; |
| 361 |
char *last_presence_subtype; |
| 362 |
char *last_presence_message; |
| 363 |
|
| 364 |
char context_name[AST_MAX_CONTEXT]; |
| 365 |
char exten_name[AST_MAX_EXTENSION]; |
| 366 |
|
| 367 |
AST_VECTOR(, char *) devices; |
| 368 |
}; |
| 369 |
|
| 370 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(hint_change_message_type); |
| 371 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(hint_remove_message_type); |
| 372 |
|
| 373 |
#define HINTDEVICE_DATA_LENGTH 16 |
| 374 |
AST_THREADSTORAGE(hintdevice_data); |
| 375 |
|
| 376 |
|
| 377 |
#ifdef LOW_MEMORY |
| 378 |
#define HASH_EXTENHINT_SIZE 17 |
| 379 |
#else |
| 380 |
#define HASH_EXTENHINT_SIZE 563 |
| 381 |
#endif |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
static struct ao2_container *hintdevices; |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
struct ast_hintdevice { |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
struct ast_hint *hint; |
| 397 |
|
| 398 |
char hintdevice[1]; |
| 399 |
}; |
| 400 |
|
| 401 |
|
| 402 |
static struct ao2_container *autohints; |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
struct ast_autohint { |
| 408 |
|
| 409 |
char *registrar; |
| 410 |
|
| 411 |
char context[1]; |
| 412 |
}; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
static int hintdevice_hash_cb(const void *obj, const int flags) |
| 418 |
{ |
| 419 |
const struct ast_hintdevice *ext; |
| 420 |
const char *key; |
| 421 |
|
| 422 |
switch (flags & OBJ_SEARCH_MASK) { |
| 423 |
case OBJ_SEARCH_KEY: |
| 424 |
key = obj; |
| 425 |
break; |
| 426 |
case OBJ_SEARCH_OBJECT: |
| 427 |
ext = obj; |
| 428 |
key = ext->hintdevice; |
| 429 |
break; |
| 430 |
default: |
| 431 |
ast_assert(0); |
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
return ast_str_case_hash(key); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
static int hintdevice_cmp_multiple(void *obj, void *arg, int flags) |
| 444 |
{ |
| 445 |
struct ast_hintdevice *left = obj; |
| 446 |
struct ast_hintdevice *right = arg; |
| 447 |
const char *right_key = arg; |
| 448 |
int cmp; |
| 449 |
|
| 450 |
switch (flags & OBJ_SEARCH_MASK) { |
| 451 |
case OBJ_SEARCH_OBJECT: |
| 452 |
right_key = right->hintdevice; |
| 453 |
|
| 454 |
case OBJ_SEARCH_KEY: |
| 455 |
cmp = strcasecmp(left->hintdevice, right_key); |
| 456 |
break; |
| 457 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
cmp = strncmp(left->hintdevice, right_key, strlen(right_key)); |
| 463 |
break; |
| 464 |
default: |
| 465 |
ast_assert(0); |
| 466 |
cmp = 0; |
| 467 |
break; |
| 468 |
} |
| 469 |
return cmp ? 0 : CMP_MATCH; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
static int autohint_hash_cb(const void *obj, const int flags) |
| 476 |
{ |
| 477 |
const struct ast_autohint *autohint; |
| 478 |
const char *key; |
| 479 |
|
| 480 |
switch (flags & OBJ_SEARCH_MASK) { |
| 481 |
case OBJ_SEARCH_KEY: |
| 482 |
key = obj; |
| 483 |
break; |
| 484 |
case OBJ_SEARCH_OBJECT: |
| 485 |
autohint = obj; |
| 486 |
key = autohint->context; |
| 487 |
break; |
| 488 |
default: |
| 489 |
ast_assert(0); |
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
return ast_str_case_hash(key); |
| 494 |
} |
| 495 |
|
| 496 |
static int autohint_cmp(void *obj, void *arg, int flags) |
| 497 |
{ |
| 498 |
struct ast_autohint *left = obj; |
| 499 |
struct ast_autohint *right = arg; |
| 500 |
const char *right_key = arg; |
| 501 |
int cmp; |
| 502 |
|
| 503 |
switch (flags & OBJ_SEARCH_MASK) { |
| 504 |
case OBJ_SEARCH_OBJECT: |
| 505 |
right_key = right->context; |
| 506 |
|
| 507 |
case OBJ_SEARCH_KEY: |
| 508 |
cmp = strcasecmp(left->context, right_key); |
| 509 |
break; |
| 510 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
cmp = strncmp(left->context, right_key, strlen(right_key)); |
| 516 |
break; |
| 517 |
default: |
| 518 |
ast_assert(0); |
| 519 |
cmp = 0; |
| 520 |
break; |
| 521 |
} |
| 522 |
return cmp ? 0 : CMP_MATCH | CMP_STOP; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
static int hintdevice_remove_cb(void *obj, void *arg, void *data, int flags) |
| 527 |
{ |
| 528 |
struct ast_hintdevice *candidate = obj; |
| 529 |
char *device = arg; |
| 530 |
struct ast_hint *hint = data; |
| 531 |
|
| 532 |
if (!strcasecmp(candidate->hintdevice, device) |
| 533 |
&& candidate->hint == hint) { |
| 534 |
return CMP_MATCH; |
| 535 |
} |
| 536 |
return 0; |
| 537 |
} |
| 538 |
|
| 539 |
static int remove_hintdevice(struct ast_hint *hint) |
| 540 |
{ |
| 541 |
while (AST_VECTOR_SIZE(&hint->devices) > 0) { |
| 542 |
char *device = AST_VECTOR_GET(&hint->devices, 0); |
| 543 |
|
| 544 |
ao2_t_callback_data(hintdevices, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA, |
| 545 |
hintdevice_remove_cb, device, hint, "Remove device from container"); |
| 546 |
AST_VECTOR_REMOVE_UNORDERED(&hint->devices, 0); |
| 547 |
ast_free(device); |
| 548 |
} |
| 549 |
|
| 550 |
return 0; |
| 551 |
} |
| 552 |
|
| 553 |
static char *parse_hint_device(struct ast_str *hint_args); |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
static void hintdevice_destroy(void *obj) |
| 561 |
{ |
| 562 |
struct ast_hintdevice *doomed = obj; |
| 563 |
|
| 564 |
if (doomed->hint) { |
| 565 |
ao2_ref(doomed->hint, -1); |
| 566 |
doomed->hint = NULL; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
static int add_hintdevice(struct ast_hint *hint, const char *devicelist) |
| 573 |
{ |
| 574 |
struct ast_str *str; |
| 575 |
char *parse; |
| 576 |
char *cur; |
| 577 |
struct ast_hintdevice *device; |
| 578 |
int devicelength; |
| 579 |
|
| 580 |
if (!hint || !devicelist) { |
| 581 |
|
| 582 |
return 0; |
| 583 |
} |
| 584 |
if (!(str = ast_str_thread_get(&hintdevice_data, 16))) { |
| 585 |
return -1; |
| 586 |
} |
| 587 |
ast_str_set(&str, 0, "%s", devicelist); |
| 588 |
parse = ast_str_buffer(str); |
| 589 |
|
| 590 |
|
| 591 |
while ((cur = strsep(&parse, "&,"))) { |
| 592 |
char *device_name; |
| 593 |
|
| 594 |
devicelength = strlen(cur); |
| 595 |
if (!devicelength) { |
| 596 |
continue; |
| 597 |
} |
| 598 |
|
| 599 |
device_name = ast_strdup(cur); |
| 600 |
if (!device_name) { |
| 601 |
return -1; |
| 602 |
} |
| 603 |
|
| 604 |
device = ao2_t_alloc(sizeof(*device) + devicelength, hintdevice_destroy, |
| 605 |
"allocating a hintdevice structure"); |
| 606 |
if (!device) { |
| 607 |
ast_free(device_name); |
| 608 |
return -1; |
| 609 |
} |
| 610 |
strcpy(device->hintdevice, cur); |
| 611 |
ao2_ref(hint, +1); |
| 612 |
device->hint = hint; |
| 613 |
if (AST_VECTOR_APPEND(&hint->devices, device_name)) { |
| 614 |
ast_free(device_name); |
| 615 |
ao2_ref(device, -1); |
| 616 |
return -1; |
| 617 |
} |
| 618 |
ao2_t_link(hintdevices, device, "Linking device into hintdevice container."); |
| 619 |
ao2_t_ref(device, -1, "hintdevice is linked so we can unref"); |
| 620 |
} |
| 621 |
|
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
static const struct cfextension_states { |
| 627 |
int extension_state; |
| 628 |
const char * const text; |
| 629 |
} extension_states[] = { |
| 630 |
{ AST_EXTENSION_NOT_INUSE, "Idle" }, |
| 631 |
{ AST_EXTENSION_INUSE, "InUse" }, |
| 632 |
{ AST_EXTENSION_BUSY, "Busy" }, |
| 633 |
{ AST_EXTENSION_UNAVAILABLE, "Unavailable" }, |
| 634 |
{ AST_EXTENSION_RINGING, "Ringing" }, |
| 635 |
{ AST_EXTENSION_INUSE | AST_EXTENSION_RINGING, "InUse&Ringing" }, |
| 636 |
{ AST_EXTENSION_ONHOLD, "Hold" }, |
| 637 |
{ AST_EXTENSION_INUSE | AST_EXTENSION_ONHOLD, "InUse&Hold" } |
| 638 |
}; |
| 639 |
|
| 640 |
struct pbx_exception { |
| 641 |
AST_DECLARE_STRING_FIELDS( |
| 642 |
AST_STRING_FIELD(context); |
| 643 |
AST_STRING_FIELD(exten); |
| 644 |
AST_STRING_FIELD(reason); |
| 645 |
); |
| 646 |
|
| 647 |
int priority; |
| 648 |
}; |
| 649 |
|
| 650 |
static int matchcid(const char *cidpattern, const char *callerid); |
| 651 |
#ifdef NEED_DEBUG |
| 652 |
static void log_match_char_tree(struct match_char *node, char *prefix); |
| 653 |
#endif |
| 654 |
static void new_find_extension(const char *str, struct scoreboard *score, |
| 655 |
struct match_char *tree, int length, int spec, const char *callerid, |
| 656 |
const char *label, enum ext_match_t action); |
| 657 |
static struct match_char *already_in_tree(struct match_char *current, char *pat, int is_pattern); |
| 658 |
static struct match_char *add_exten_to_pattern_tree(struct ast_context *con, |
| 659 |
struct ast_exten *e1, int findonly); |
| 660 |
static void create_match_char_tree(struct ast_context *con); |
| 661 |
static struct ast_exten *get_canmatch_exten(struct match_char *node); |
| 662 |
static void destroy_pattern_tree(struct match_char *pattern_tree); |
| 663 |
static int hashtab_compare_extens(const void *ha_a, const void *ah_b); |
| 664 |
static int hashtab_compare_exten_numbers(const void *ah_a, const void *ah_b); |
| 665 |
static int hashtab_compare_exten_labels(const void *ah_a, const void *ah_b); |
| 666 |
static unsigned int hashtab_hash_extens(const void *obj); |
| 667 |
static unsigned int hashtab_hash_priority(const void *obj); |
| 668 |
static unsigned int hashtab_hash_labels(const void *obj); |
| 669 |
static void __ast_internal_context_destroy( struct ast_context *con); |
| 670 |
static int ast_add_extension_nolock(const char *context, int replace, const char *extension, |
| 671 |
int priority, const char *label, const char *callerid, |
| 672 |
const char *application, void *data, void (*datad)(void *), const char *registrar); |
| 673 |
static int ast_add_extension2_lockopt(struct ast_context *con, |
| 674 |
int replace, const char *extension, int priority, const char *label, const char *callerid, |
| 675 |
const char *application, void *data, void (*datad)(void *), |
| 676 |
const char *registrar, const char *registrar_file, int registrar_line, |
| 677 |
int lock_context); |
| 678 |
static struct ast_context *find_context_locked(const char *context); |
| 679 |
static struct ast_context *find_context(const char *context); |
| 680 |
static void get_device_state_causing_channels(struct ao2_container *c); |
| 681 |
static unsigned int ext_strncpy(char *dst, const char *src, size_t dst_size, int nofluff); |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
static int compare_char(const void *a, const void *b) |
| 695 |
{ |
| 696 |
const unsigned char *ac = a; |
| 697 |
const unsigned char *bc = b; |
| 698 |
|
| 699 |
return *ac - *bc; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b) |
| 704 |
{ |
| 705 |
const struct ast_context *ac = ah_a; |
| 706 |
const struct ast_context *bc = ah_b; |
| 707 |
if (!ac || !bc) |
| 708 |
return 1; |
| 709 |
|
| 710 |
return strcmp(ac->name, bc->name); |
| 711 |
} |
| 712 |
|
| 713 |
static int hashtab_compare_extens(const void *ah_a, const void *ah_b) |
| 714 |
{ |
| 715 |
const struct ast_exten *ac = ah_a; |
| 716 |
const struct ast_exten *bc = ah_b; |
| 717 |
int x = strcmp(ac->exten, bc->exten); |
| 718 |
if (x) { |
| 719 |
return x; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
if (ac->matchcid == AST_EXT_MATCHCID_ANY || bc->matchcid == AST_EXT_MATCHCID_ANY) { |
| 725 |
return 0; |
| 726 |
} |
| 727 |
if (ac->matchcid == AST_EXT_MATCHCID_OFF && bc->matchcid == AST_EXT_MATCHCID_OFF) { |
| 728 |
return 0; |
| 729 |
} |
| 730 |
if (ac->matchcid != bc->matchcid) { |
| 731 |
return 1; |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
if (ast_strlen_zero(ac->cidmatch) && ast_strlen_zero(bc->cidmatch)) { |
| 736 |
return 0; |
| 737 |
} |
| 738 |
return strcmp(ac->cidmatch, bc->cidmatch); |
| 739 |
} |
| 740 |
|
| 741 |
static int hashtab_compare_exten_numbers(const void *ah_a, const void *ah_b) |
| 742 |
{ |
| 743 |
const struct ast_exten *ac = ah_a; |
| 744 |
const struct ast_exten *bc = ah_b; |
| 745 |
return ac->priority != bc->priority; |
| 746 |
} |
| 747 |
|
| 748 |
static int hashtab_compare_exten_labels(const void *ah_a, const void *ah_b) |
| 749 |
{ |
| 750 |
const struct ast_exten *ac = ah_a; |
| 751 |
const struct ast_exten *bc = ah_b; |
| 752 |
return strcmp(S_OR(ac->label, ""), S_OR(bc->label, "")); |
| 753 |
} |
| 754 |
|
| 755 |
unsigned int ast_hashtab_hash_contexts(const void *obj) |
| 756 |
{ |
| 757 |
const struct ast_context *ac = obj; |
| 758 |
return ast_hashtab_hash_string(ac->name); |
| 759 |
} |
| 760 |
|
| 761 |
static unsigned int hashtab_hash_extens(const void *obj) |
| 762 |
{ |
| 763 |
const struct ast_exten *ac = obj; |
| 764 |
unsigned int x = ast_hashtab_hash_string(ac->exten); |
| 765 |
unsigned int y = 0; |
| 766 |
if (ac->matchcid == AST_EXT_MATCHCID_ON) |
| 767 |
y = ast_hashtab_hash_string(ac->cidmatch); |
| 768 |
return x+y; |
| 769 |
} |
| 770 |
|
| 771 |
static unsigned int hashtab_hash_priority(const void *obj) |
| 772 |
{ |
| 773 |
const struct ast_exten *ac = obj; |
| 774 |
return ast_hashtab_hash_int(ac->priority); |
| 775 |
} |
| 776 |
|
| 777 |
static unsigned int hashtab_hash_labels(const void *obj) |
| 778 |
{ |
| 779 |
const struct ast_exten *ac = obj; |
| 780 |
return ast_hashtab_hash_string(S_OR(ac->label, "")); |
| 781 |
} |
| 782 |
|
| 783 |
static int autofallthrough = 1; |
| 784 |
static int extenpatternmatchnew = 0; |
| 785 |
static char *overrideswitch = NULL; |
| 786 |
|
| 787 |
|
| 788 |
static struct stasis_subscription *device_state_sub; |
| 789 |
|
| 790 |
static struct stasis_subscription *presence_state_sub; |
| 791 |
|
| 792 |
AST_MUTEX_DEFINE_STATIC(maxcalllock); |
| 793 |
static int countcalls; |
| 794 |
static int totalcalls; |
| 795 |
|
| 796 |
static struct ast_context *contexts; |
| 797 |
static struct ast_hashtab *contexts_table = NULL; |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
AST_MUTEX_DEFINE_STATIC(conlock); |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
AST_MUTEX_DEFINE_STATIC(context_merge_lock); |
| 811 |
|
| 812 |
static int stateid = 1; |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
static struct ao2_container *hints; |
| 822 |
|
| 823 |
static struct ao2_container *statecbs; |
| 824 |
|
| 825 |
#ifdef CONTEXT_DEBUG |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
void check_contexts_trouble(void); |
| 840 |
|
| 841 |
void check_contexts_trouble(void) |
| 842 |
{ |
| 843 |
int x = 1; |
| 844 |
x = 2; |
| 845 |
} |
| 846 |
|
| 847 |
int check_contexts(char *, int); |
| 848 |
|
| 849 |
int check_contexts(char *file, int line ) |
| 850 |
{ |
| 851 |
struct ast_hashtab_iter *t1; |
| 852 |
struct ast_context *c1, *c2; |
| 853 |
int found = 0; |
| 854 |
struct ast_exten *e1, *e2, *e3; |
| 855 |
struct ast_exten ex; |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
if (!contexts_table) { |
| 861 |
ast_log(LOG_NOTICE,"Called from: %s:%d: No contexts_table!\n", file, line); |
| 862 |
usleep(500000); |
| 863 |
} |
| 864 |
|
| 865 |
t1 = ast_hashtab_start_traversal(contexts_table); |
| 866 |
while( (c1 = ast_hashtab_next(t1))) { |
| 867 |
for(c2=contexts;c2;c2=c2->next) { |
| 868 |
if (!strcmp(c1->name, c2->name)) { |
| 869 |
found = 1; |
| 870 |
break; |
| 871 |
} |
| 872 |
} |
| 873 |
if (!found) { |
| 874 |
ast_log(LOG_NOTICE,"Called from: %s:%d: Could not find the %s context in the linked list\n", file, line, c1->name); |
| 875 |
check_contexts_trouble(); |
| 876 |
} |
| 877 |
} |
| 878 |
ast_hashtab_end_traversal(t1); |
| 879 |
for(c2=contexts;c2;c2=c2->next) { |
| 880 |
c1 = find_context_locked(c2->name); |
| 881 |
if (!c1) { |
| 882 |
ast_log(LOG_NOTICE,"Called from: %s:%d: Could not find the %s context in the hashtab\n", file, line, c2->name); |
| 883 |
check_contexts_trouble(); |
| 884 |
} else |
| 885 |
ast_unlock_contexts(); |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
for(c2=contexts;c2;c2=c2->next) { |
| 891 |
c1 = find_context_locked(c2->name); |
| 892 |
if (c1) { |
| 893 |
ast_unlock_contexts(); |
| 894 |
|
| 895 |
|
| 896 |
for(e1 = c1->root; e1; e1=e1->next) |
| 897 |
{ |
| 898 |
char dummy_name[1024]; |
| 899 |
ex.exten = dummy_name; |
| 900 |
ex.matchcid = e1->matchcid; |
| 901 |
ex.cidmatch = e1->cidmatch; |
| 902 |
ast_copy_string(dummy_name, e1->exten, sizeof(dummy_name)); |
| 903 |
e2 = ast_hashtab_lookup(c1->root_table, &ex); |
| 904 |
if (!e2) { |
| 905 |
if (e1->matchcid == AST_EXT_MATCHCID_ON) { |
| 906 |
ast_log(LOG_NOTICE, "Called from: %s:%d: The %s context records " |
| 907 |
"the exten %s (CID match: %s) but it is not in its root_table\n", |
| 908 |
file, line, c2->name, dummy_name, e1->cidmatch_display); |
| 909 |
} else { |
| 910 |
ast_log(LOG_NOTICE, "Called from: %s:%d: The %s context records " |
| 911 |
"the exten %s but it is not in its root_table\n", |
| 912 |
file, line, c2->name, dummy_name); |
| 913 |
} |
| 914 |
check_contexts_trouble(); |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
if (!c2->root_table) { |
| 920 |
if (c2->root) { |
| 921 |
ast_log(LOG_NOTICE,"Called from: %s:%d: No c2->root_table for context %s!\n", file, line, c2->name); |
| 922 |
usleep(500000); |
| 923 |
} |
| 924 |
} else { |
| 925 |
t1 = ast_hashtab_start_traversal(c2->root_table); |
| 926 |
while( (e2 = ast_hashtab_next(t1)) ) { |
| 927 |
for(e1=c2->root;e1;e1=e1->next) { |
| 928 |
if (!strcmp(e1->exten, e2->exten)) { |
| 929 |
found = 1; |
| 930 |
break; |
| 931 |
} |
| 932 |
} |
| 933 |
if (!found) { |
| 934 |
ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context records the exten %s but it is not in its root_table\n", file, line, c2->name, e2->exten); |
| 935 |
check_contexts_trouble(); |
| 936 |
} |
| 937 |
|
| 938 |
} |
| 939 |
ast_hashtab_end_traversal(t1); |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
for(e1 = c2->root; e1; e1 = e1->next) { |
| 948 |
|
| 949 |
for(e2=e1;e2;e2=e2->peer) { |
| 950 |
ex.priority = e2->priority; |
| 951 |
if (e2 != e1 && e2->peer_table) { |
| 952 |
ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority has a peer_table entry, and shouldn't!\n", file, line, c2->name, e1->exten, e2->priority ); |
| 953 |
check_contexts_trouble(); |
| 954 |
} |
| 955 |
|
| 956 |
if (e2 != e1 && e2->peer_label_table) { |
| 957 |
ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority has a peer_label_table entry, and shouldn't!\n", file, line, c2->name, e1->exten, e2->priority ); |
| 958 |
check_contexts_trouble(); |
| 959 |
} |
| 960 |
|
| 961 |
if (e2 == e1 && !e2->peer_table){ |
| 962 |
ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority doesn't have a peer_table!\n", file, line, c2->name, e1->exten, e2->priority ); |
| 963 |
check_contexts_trouble(); |
| 964 |
} |
| 965 |
|
| 966 |
if (e2 == e1 && !e2->peer_label_table) { |
| 967 |
ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority doesn't have a peer_label_table!\n", file, line, c2->name, e1->exten, e2->priority ); |
| 968 |
check_contexts_trouble(); |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
e3 = ast_hashtab_lookup(e1->peer_table, &ex); |
| 973 |
if (!e3) { |
| 974 |
ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority is not reflected in the peer_table\n", file, line, c2->name, e1->exten, e2->priority ); |
| 975 |
check_contexts_trouble(); |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
if (!e1->peer_table){ |
| 980 |
ast_log(LOG_NOTICE,"Called from: %s:%d: No e1->peer_table!\n", file, line); |
| 981 |
usleep(500000); |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
t1 = ast_hashtab_start_traversal(e1->peer_table); |
| 986 |
while( (e2 = ast_hashtab_next(t1)) ) { |
| 987 |
for(e3=e1;e3;e3=e3->peer) { |
| 988 |
if (e3->priority == e2->priority) { |
| 989 |
found = 1; |
| 990 |
break; |
| 991 |
} |
| 992 |
} |
| 993 |
if (!found) { |
| 994 |
ast_log(LOG_NOTICE,"Called from: %s:%d: The %s context, %s exten, %d priority is not reflected in the peer list\n", file, line, c2->name, e1->exten, e2->priority ); |
| 995 |
check_contexts_trouble(); |
| 996 |
} |
| 997 |
} |
| 998 |
ast_hashtab_end_traversal(t1); |
| 999 |
} |
| 1000 |
} |
| 1001 |
return 0; |
| 1002 |
} |
| 1003 |
#endif |
| 1004 |
|
| 1005 |
static void pbx_destroy(struct ast_pbx *p) |
| 1006 |
{ |
| 1007 |
ast_free(p); |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
static void update_scoreboard(struct scoreboard *board, int length, int spec, struct ast_exten *exten, char last, const char *callerid, int deleted, struct match_char *node) |
| 1085 |
{ |
| 1086 |
|
| 1087 |
|
| 1088 |
if (deleted) |
| 1089 |
return; |
| 1090 |
board->total_specificity = spec; |
| 1091 |
board->total_length = length; |
| 1092 |
board->exten = exten; |
| 1093 |
board->last_char = last; |
| 1094 |
board->node = node; |
| 1095 |
#ifdef NEED_DEBUG_HERE |
| 1096 |
ast_log(LOG_NOTICE,"Scoreboarding (LONGER) %s, len=%d, score=%d\n", exten->exten, length, spec); |
| 1097 |
#endif |
| 1098 |
} |
| 1099 |
|
| 1100 |
#ifdef NEED_DEBUG |
| 1101 |
static void log_match_char_tree(struct match_char *node, char *prefix) |
| 1102 |
{ |
| 1103 |
char extenstr[40]; |
| 1104 |
struct ast_str *my_prefix = ast_str_alloca(1024); |
| 1105 |
|
| 1106 |
extenstr[0] = '\0'; |
| 1107 |
|
| 1108 |
if (node && node->exten) |
| 1109 |
snprintf(extenstr, sizeof(extenstr), "(%p)", node->exten); |
| 1110 |
|
| 1111 |
if (strlen(node->x) > 1) { |
| 1112 |
ast_debug(1, "%s[%s]:%c:%c:%d:%s%s%s\n", prefix, node->x, node->is_pattern ? 'Y':'N', |
| 1113 |
node->deleted? 'D':'-', node->specificity, node->exten? "EXTEN:":"", |
| 1114 |
node->exten ? node->exten->exten : "", extenstr); |
| 1115 |
} else { |
| 1116 |
ast_debug(1, "%s%s:%c:%c:%d:%s%s%s\n", prefix, node->x, node->is_pattern ? 'Y':'N', |
| 1117 |
node->deleted? 'D':'-', node->specificity, node->exten? "EXTEN:":"", |
| 1118 |
node->exten ? node->exten->exten : "", extenstr); |
| 1119 |
} |
| 1120 |
|
| 1121 |
ast_str_set(&my_prefix, 0, "%s+ ", prefix); |
| 1122 |
|
| 1123 |
if (node->next_char) |
| 1124 |
log_match_char_tree(node->next_char, ast_str_buffer(my_prefix)); |
| 1125 |
|
| 1126 |
if (node->alt_char) |
| 1127 |
log_match_char_tree(node->alt_char, prefix); |
| 1128 |
} |
| 1129 |
#endif |
| 1130 |
|
| 1131 |
static void cli_match_char_tree(struct match_char *node, char *prefix, int fd) |
| 1132 |
{ |
| 1133 |
char extenstr[40]; |
| 1134 |
struct ast_str *my_prefix = ast_str_alloca(1024); |
| 1135 |
|
| 1136 |
extenstr[0] = '\0'; |
| 1137 |
|
| 1138 |
if (node->exten) { |
| 1139 |
snprintf(extenstr, sizeof(extenstr), "(%p)", node->exten); |
| 1140 |
} |
| 1141 |
|
| 1142 |
if (strlen(node->x) > 1) { |
| 1143 |
ast_cli(fd, "%s[%s]:%c:%c:%d:%s%s%s\n", prefix, node->x, node->is_pattern ? 'Y' : 'N', |
| 1144 |
node->deleted ? 'D' : '-', node->specificity, node->exten? "EXTEN:" : "", |
| 1145 |
node->exten ? node->exten->name : "", extenstr); |
| 1146 |
} else { |
| 1147 |
ast_cli(fd, "%s%s:%c:%c:%d:%s%s%s\n", prefix, node->x, node->is_pattern ? 'Y' : 'N', |
| 1148 |
node->deleted ? 'D' : '-', node->specificity, node->exten? "EXTEN:" : "", |
| 1149 |
node->exten ? node->exten->name : "", extenstr); |
| 1150 |
} |
| 1151 |
|
| 1152 |
ast_str_set(&my_prefix, 0, "%s+ ", prefix); |
| 1153 |
|
| 1154 |
if (node->next_char) |
| 1155 |
cli_match_char_tree(node->next_char, ast_str_buffer(my_prefix), fd); |
| 1156 |
|
| 1157 |
if (node->alt_char) |
| 1158 |
cli_match_char_tree(node->alt_char, prefix, fd); |
| 1159 |
} |
| 1160 |
|
| 1161 |
static struct ast_exten *get_canmatch_exten(struct match_char *node) |
| 1162 |
{ |
| 1163 |
|
| 1164 |
struct match_char *node2 = node; |
| 1165 |
|
| 1166 |
for (node2 = node; node2; node2 = node2->next_char) { |
| 1167 |
if (node2->exten) { |
| 1168 |
#ifdef NEED_DEBUG_HERE |
| 1169 |
ast_log(LOG_NOTICE,"CanMatch_exten returns exten %s(%p)\n", node2->exten->exten, node2->exten); |
| 1170 |
#endif |
| 1171 |
return node2->exten; |
| 1172 |
} |
| 1173 |
} |
| 1174 |
#ifdef NEED_DEBUG_HERE |
| 1175 |
ast_log(LOG_NOTICE,"CanMatch_exten returns NULL, match_char=%s\n", node->x); |
| 1176 |
#endif |
| 1177 |
return 0; |
| 1178 |
} |
| 1179 |
|
| 1180 |
static struct ast_exten *trie_find_next_match(struct match_char *node) |
| 1181 |
{ |
| 1182 |
struct match_char *m3; |
| 1183 |
struct match_char *m4; |
| 1184 |
struct ast_exten *e3; |
| 1185 |
|
| 1186 |
if (node && node->x[0] == '.' && !node->x[1]) { |
| 1187 |
return node->exten; |
| 1188 |
} |
| 1189 |
|
| 1190 |
if (node && node->x[0] == '!' && !node->x[1]) { |
| 1191 |
return node->exten; |
| 1192 |
} |
| 1193 |
|
| 1194 |
if (!node || !node->next_char) { |
| 1195 |
return NULL; |
| 1196 |
} |
| 1197 |
|
| 1198 |
m3 = node->next_char; |
| 1199 |
|
| 1200 |
if (m3->exten) { |
| 1201 |
return m3->exten; |
| 1202 |
} |
| 1203 |
for (m4 = m3->alt_char; m4; m4 = m4->alt_char) { |
| 1204 |
if (m4->exten) { |
| 1205 |
return m4->exten; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
for (m4 = m3; m4; m4 = m4->alt_char) { |
| 1209 |
e3 = trie_find_next_match(m3); |
| 1210 |
if (e3) { |
| 1211 |
return e3; |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
return NULL; |
| 1216 |
} |
| 1217 |
|
| 1218 |
#ifdef DEBUG_THIS |
| 1219 |
static char *action2str(enum ext_match_t action) |
| 1220 |
{ |
| 1221 |
switch (action) { |
| 1222 |
case E_MATCH: |
| 1223 |
return "MATCH"; |
| 1224 |
case E_CANMATCH: |
| 1225 |
return "CANMATCH"; |
| 1226 |
case E_MATCHMORE: |
| 1227 |
return "MATCHMORE"; |
| 1228 |
case E_FINDLABEL: |
| 1229 |
return "FINDLABEL"; |
| 1230 |
case E_SPAWN: |
| 1231 |
return "SPAWN"; |
| 1232 |
default: |
| 1233 |
return "?ACTION?"; |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
#endif |
| 1238 |
|
| 1239 |
static const char *candidate_exten_advance(const char *str) |
| 1240 |
{ |
| 1241 |
str++; |
| 1242 |
while (*str == '-') { |
| 1243 |
str++; |
| 1244 |
} |
| 1245 |
return str; |
| 1246 |
} |
| 1247 |
|
| 1248 |
#define MORE(s) (*candidate_exten_advance(s)) |
| 1249 |
#define ADVANCE(s) candidate_exten_advance(s) |
| 1250 |
|
| 1251 |
static void new_find_extension(const char *str, struct scoreboard *score, struct match_char *tree, int length, int spec, const char *callerid, const char *label, enum ext_match_t action) |
| 1252 |
{ |
| 1253 |
struct match_char *p; |
| 1254 |
struct ast_exten pattern = { .label = label }; |
| 1255 |
#ifdef DEBUG_THIS |
| 1256 |
if (tree) |
| 1257 |
ast_log(LOG_NOTICE,"new_find_extension called with %s on (sub)tree %s action=%s\n", str, tree->x, action2str(action)); |
| 1258 |
else |
| 1259 |
ast_log(LOG_NOTICE,"new_find_extension called with %s on (sub)tree NULL action=%s\n", str, action2str(action)); |
| 1260 |
#endif |
| 1261 |
for (p = tree; p; p = p->alt_char) { |
| 1262 |
if (p->is_pattern) { |
| 1263 |
if (p->x[0] == 'N') { |
| 1264 |
if (p->x[1] == 0 && *str >= '2' && *str <= '9' ) { |
| 1265 |
#define NEW_MATCHER_CHK_MATCH \ |
| 1266 |
if (p->exten && !MORE(str)) { \ |
| 1267 |
if (action == E_MATCH || action == E_SPAWN || action == E_FINDLABEL) { \ |
| 1268 |
update_scoreboard(score, length + 1, spec + p->specificity, p->exten, 0, callerid, p->deleted, p); \ |
| 1269 |
if (!p->deleted) { \ |
| 1270 |
if (action == E_FINDLABEL) { \ |
| 1271 |
if (ast_hashtab_lookup(score->exten->peer_label_table, &pattern)) { \ |
| 1272 |
ast_debug(4, "Found label in preferred extension\n"); \ |
| 1273 |
return; \ |
| 1274 |
} \ |
| 1275 |
} else { \ |
| 1276 |
ast_debug(4, "returning an exact match-- first found-- %s\n", p->exten->name); \ |
| 1277 |
return; \ |
| 1278 |
} \ |
| 1279 |
} \ |
| 1280 |
} \ |
| 1281 |
} |
| 1282 |
|
| 1283 |
#define NEW_MATCHER_RECURSE \ |
| 1284 |
if (p->next_char && (MORE(str) || (p->next_char->x[0] == '/' && p->next_char->x[1] == 0) \ |
| 1285 |
|| p->next_char->x[0] == '!')) { \ |
| 1286 |
if (MORE(str) || p->next_char->x[0] == '!') { \ |
| 1287 |
new_find_extension(ADVANCE(str), score, p->next_char, length + 1, spec + p->specificity, callerid, label, action); \ |
| 1288 |
if (score->exten) { \ |
| 1289 |
ast_debug(4 ,"returning an exact match-- %s\n", score->exten->name); \ |
| 1290 |
return; \ |
| 1291 |
} \ |
| 1292 |
} else { \ |
| 1293 |
new_find_extension("/", score, p->next_char, length + 1, spec + p->specificity, callerid, label, action); \ |
| 1294 |
if (score->exten || ((action == E_CANMATCH || action == E_MATCHMORE) && score->canmatch)) { \ |
| 1295 |
ast_debug(4,"returning a (can/more) match--- %s\n", score->exten ? score->exten->name : \ |
| 1296 |
"NULL"); \ |
| 1297 |
return; \ |
| 1298 |
} \ |
| 1299 |
} \ |
| 1300 |
} else if ((p->next_char || action == E_CANMATCH) && !MORE(str)) { \ |
| 1301 |
score->canmatch = 1; \ |
| 1302 |
score->canmatch_exten = get_canmatch_exten(p); \ |
| 1303 |
if (action == E_CANMATCH || action == E_MATCHMORE) { \ |
| 1304 |
ast_debug(4, "returning a canmatch/matchmore--- str=%s\n", str); \ |
| 1305 |
return; \ |
| 1306 |
} \ |
| 1307 |
} |
| 1308 |
|
| 1309 |
NEW_MATCHER_CHK_MATCH; |
| 1310 |
NEW_MATCHER_RECURSE; |
| 1311 |
} |
| 1312 |
} else if (p->x[0] == 'Z') { |
| 1313 |
if (p->x[1] == 0 && *str >= '1' && *str <= '9' ) { |
| 1314 |
NEW_MATCHER_CHK_MATCH; |
| 1315 |
NEW_MATCHER_RECURSE; |
| 1316 |
} |
| 1317 |
} else if (p->x[0] == 'X') { |
| 1318 |
if (p->x[1] == 0 && *str >= '0' && *str <= '9' ) { |
| 1319 |
NEW_MATCHER_CHK_MATCH; |
| 1320 |
NEW_MATCHER_RECURSE; |
| 1321 |
} |
| 1322 |
} else if (p->x[0] == '.' && p->x[1] == 0) { |
| 1323 |
|
| 1324 |
int i = 0; |
| 1325 |
const char *str2 = str; |
| 1326 |
while (*str2 && *str2 != '/') { |
| 1327 |
str2++; |
| 1328 |
i++; |
| 1329 |
} |
| 1330 |
if (p->exten && *str2 != '/') { |
| 1331 |
update_scoreboard(score, length + i, spec + (i * p->specificity), p->exten, '.', callerid, p->deleted, p); |
| 1332 |
if (score->exten) { |
| 1333 |
ast_debug(4, "return because scoreboard has a match with '/'--- %s\n", |
| 1334 |
score->exten->name); |
| 1335 |
return; |
| 1336 |
} |
| 1337 |
} |
| 1338 |
if (p->next_char && p->next_char->x[0] == '/' && p->next_char->x[1] == 0) { |
| 1339 |
new_find_extension("/", score, p->next_char, length + i, spec+(p->specificity*i), callerid, label, action); |
| 1340 |
if (score->exten || ((action == E_CANMATCH || action == E_MATCHMORE) && score->canmatch)) { |
| 1341 |
ast_debug(4, "return because scoreboard has exact match OR " |
| 1342 |
"CANMATCH/MATCHMORE & canmatch set--- %s\n", |
| 1343 |
score->exten ? score->exten->name : "NULL"); |
| 1344 |
return; |
| 1345 |
} |
| 1346 |
} |
| 1347 |
} else if (p->x[0] == '!' && p->x[1] == 0) { |
| 1348 |
|
| 1349 |
int i = 1; |
| 1350 |
const char *str2 = str; |
| 1351 |
while (*str2 && *str2 != '/') { |
| 1352 |
str2++; |
| 1353 |
i++; |
| 1354 |
} |
| 1355 |
if (p->exten && *str2 != '/') { |
| 1356 |
update_scoreboard(score, length + 1, spec + (p->specificity * i), p->exten, '!', callerid, p->deleted, p); |
| 1357 |
if (score->exten) { |
| 1358 |
ast_debug(4, "return because scoreboard has a '!' match--- %s\n", |
| 1359 |
score->exten->name); |
| 1360 |
return; |
| 1361 |
} |
| 1362 |
} |
| 1363 |
if (p->next_char && p->next_char->x[0] == '/' && p->next_char->x[1] == 0) { |
| 1364 |
new_find_extension("/", score, p->next_char, length + i, spec + (p->specificity * i), callerid, label, action); |
| 1365 |
if (score->exten || ((action == E_CANMATCH || action == E_MATCHMORE) && score->canmatch)) { |
| 1366 |
ast_debug(4, "return because scoreboard has exact match OR " |
| 1367 |
"CANMATCH/MATCHMORE & canmatch set with '/' and '!'--- %s\n", |
| 1368 |
score->exten ? score->exten->name : "NULL"); |
| 1369 |
return; |
| 1370 |
} |
| 1371 |
} |
| 1372 |
} else if (p->x[0] == '/' && p->x[1] == 0) { |
| 1373 |
|
| 1374 |
if (p->next_char && callerid && *callerid) { |
| 1375 |
new_find_extension(callerid, score, p->next_char, length + 1, spec, callerid, label, action); |
| 1376 |
if (score->exten || ((action == E_CANMATCH || action == E_MATCHMORE) && score->canmatch)) { |
| 1377 |
ast_debug(4, "return because scoreboard has exact match OR " |
| 1378 |
"CANMATCH/MATCHMORE & canmatch set with '/'--- %s\n", |
| 1379 |
score->exten ? score->exten->name : "NULL"); |
| 1380 |
return; |
| 1381 |
} |
| 1382 |
} |
| 1383 |
} else if (strchr(p->x, *str)) { |
| 1384 |
ast_debug(4, "Nothing strange about this match\n"); |
| 1385 |
NEW_MATCHER_CHK_MATCH; |
| 1386 |
NEW_MATCHER_RECURSE; |
| 1387 |
} |
| 1388 |
} else if (strchr(p->x, *str)) { |
| 1389 |
ast_debug(4, "Nothing strange about this match\n"); |
| 1390 |
NEW_MATCHER_CHK_MATCH; |
| 1391 |
NEW_MATCHER_RECURSE; |
| 1392 |
} |
| 1393 |
} |
| 1394 |
ast_debug(4, "return at end of func\n"); |
| 1395 |
} |
| 1396 |
|
| 1397 |
#undef MORE |
| 1398 |
#undef ADVANCE |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
static struct match_char *already_in_tree(struct match_char *current, char *pat, int is_pattern) |
| 1418 |
{ |
| 1419 |
struct match_char *t; |
| 1420 |
|
| 1421 |
if (!current) { |
| 1422 |
return 0; |
| 1423 |
} |
| 1424 |
|
| 1425 |
for (t = current; t; t = t->alt_char) { |
| 1426 |
if (is_pattern == t->is_pattern && !strcmp(pat, t->x)) { |
| 1427 |
return t; |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
return 0; |
| 1432 |
} |
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
static void insert_in_next_chars_alt_char_list(struct match_char **parent_ptr, struct match_char *node) |
| 1439 |
{ |
| 1440 |
struct match_char *curr, *lcurr; |
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
if (!(*parent_ptr)) { |
| 1445 |
*parent_ptr = node; |
| 1446 |
return; |
| 1447 |
} |
| 1448 |
|
| 1449 |
if ((*parent_ptr)->specificity > node->specificity) { |
| 1450 |
|
| 1451 |
node->alt_char = (*parent_ptr); |
| 1452 |
*parent_ptr = node; |
| 1453 |
return; |
| 1454 |
} |
| 1455 |
|
| 1456 |
lcurr = *parent_ptr; |
| 1457 |
for (curr = (*parent_ptr)->alt_char; curr; curr = curr->alt_char) { |
| 1458 |
if (curr->specificity > node->specificity) { |
| 1459 |
node->alt_char = curr; |
| 1460 |
lcurr->alt_char = node; |
| 1461 |
break; |
| 1462 |
} |
| 1463 |
lcurr = curr; |
| 1464 |
} |
| 1465 |
|
| 1466 |
if (!curr) { |
| 1467 |
lcurr->alt_char = node; |
| 1468 |
} |
| 1469 |
|
| 1470 |
} |
| 1471 |
|
| 1472 |
struct pattern_node { |
| 1473 |
|
| 1474 |
int specif; |
| 1475 |
|
| 1476 |
char buf[256]; |
| 1477 |
}; |
| 1478 |
|
| 1479 |
static struct match_char *add_pattern_node(struct ast_context *con, struct match_char *current, const struct pattern_node *pattern, int is_pattern, int already, struct match_char **nextcharptr) |
| 1480 |
{ |
| 1481 |
struct match_char *m; |
| 1482 |
|
| 1483 |
if (!(m = ast_calloc(1, sizeof(*m) + strlen(pattern->buf)))) { |
| 1484 |
return NULL; |
| 1485 |
} |
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
strcpy(m->x, pattern->buf); |
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
m->is_pattern = is_pattern; |
| 1495 |
if (pattern->specif == 1 && is_pattern && pattern->buf[0] == 'N') { |
| 1496 |
m->specificity = 0x0832; |
| 1497 |
} else if (pattern->specif == 1 && is_pattern && pattern->buf[0] == 'Z') { |
| 1498 |
m->specificity = 0x0931; |
| 1499 |
} else if (pattern->specif == 1 && is_pattern && pattern->buf[0] == 'X') { |
| 1500 |
m->specificity = 0x0a30; |
| 1501 |
} else if (pattern->specif == 1 && is_pattern && pattern->buf[0] == '.') { |
| 1502 |
m->specificity = 0x18000; |
| 1503 |
} else if (pattern->specif == 1 && is_pattern && pattern->buf[0] == '!') { |
| 1504 |
m->specificity = 0x28000; |
| 1505 |
} else { |
| 1506 |
m->specificity = pattern->specif; |
| 1507 |
} |
| 1508 |
|
| 1509 |
if (!con->pattern_tree) { |
| 1510 |
insert_in_next_chars_alt_char_list(&con->pattern_tree, m); |
| 1511 |
} else { |
| 1512 |
if (already) { |
| 1513 |
insert_in_next_chars_alt_char_list(nextcharptr, m); |
| 1514 |
} else { |
| 1515 |
insert_in_next_chars_alt_char_list(¤t->next_char, m); |
| 1516 |
} |
| 1517 |
} |
| 1518 |
|
| 1519 |
return m; |
| 1520 |
} |
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
static const char *get_pattern_node(struct pattern_node *node, const char *src, int pattern, const char *extenbuf) |
| 1534 |
{ |
| 1535 |
#define INC_DST_OVERFLOW_CHECK \ |
| 1536 |
do { \ |
| 1537 |
if (dst - node->buf < sizeof(node->buf) - 1) { \ |
| 1538 |
++dst; \ |
| 1539 |
} else { \ |
| 1540 |
overflow = 1; \ |
| 1541 |
} \ |
| 1542 |
} while (0) |
| 1543 |
|
| 1544 |
node->specif = 0; |
| 1545 |
node->buf[0] = '\0'; |
| 1546 |
while (*src) { |
| 1547 |
if (*src == '[' && pattern) { |
| 1548 |
char *dst = node->buf; |
| 1549 |
const char *src_next; |
| 1550 |
int length; |
| 1551 |
int overflow = 0; |
| 1552 |
|
| 1553 |
|
| 1554 |
++src; |
| 1555 |
for (;;) { |
| 1556 |
if (*src == '\\') { |
| 1557 |
|
| 1558 |
++src; |
| 1559 |
if (*src == '[' || *src == '\\' || *src == '-' || *src == ']') { |
| 1560 |
*dst = *src++; |
| 1561 |
INC_DST_OVERFLOW_CHECK; |
| 1562 |
} |
| 1563 |
} else if (*src == '-') { |
| 1564 |
unsigned char first; |
| 1565 |
unsigned char last; |
| 1566 |
|
| 1567 |
src_next = src; |
| 1568 |
first = *(src_next - 1); |
| 1569 |
last = *++src_next; |
| 1570 |
|
| 1571 |
if (last == '\\') { |
| 1572 |
|
| 1573 |
last = *++src_next; |
| 1574 |
} |
| 1575 |
|
| 1576 |
|
| 1577 |
if (node->buf[0] && last) { |
| 1578 |
|
| 1579 |
while (++first <= last) { |
| 1580 |
*dst = first; |
| 1581 |
INC_DST_OVERFLOW_CHECK; |
| 1582 |
} |
| 1583 |
src = src_next + 1; |
| 1584 |
} else { |
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
*dst = *src++; |
| 1590 |
INC_DST_OVERFLOW_CHECK; |
| 1591 |
} |
| 1592 |
} else if (*src == '\0') { |
| 1593 |
ast_log(LOG_WARNING, |
| 1594 |
"A matching ']' was not found for '[' in exten pattern '%s'\n", |
| 1595 |
extenbuf); |
| 1596 |
break; |
| 1597 |
} else if (*src == ']') { |
| 1598 |
++src; |
| 1599 |
break; |
| 1600 |
} else { |
| 1601 |
*dst = *src++; |
| 1602 |
INC_DST_OVERFLOW_CHECK; |
| 1603 |
} |
| 1604 |
} |
| 1605 |
|
| 1606 |
*dst = '\0'; |
| 1607 |
|
| 1608 |
if (overflow) { |
| 1609 |
ast_log(LOG_ERROR, |
| 1610 |
"Expanded character set too large to deal with in exten pattern '%s'. Ignoring character set.\n", |
| 1611 |
extenbuf); |
| 1612 |
node->buf[0] = '\0'; |
| 1613 |
continue; |
| 1614 |
} |
| 1615 |
|
| 1616 |
|
| 1617 |
length = strlen(node->buf); |
| 1618 |
if (!length) { |
| 1619 |
ast_log(LOG_WARNING, "Empty character set in exten pattern '%s'. Ignoring.\n", |
| 1620 |
extenbuf); |
| 1621 |
node->buf[0] = '\0'; |
| 1622 |
continue; |
| 1623 |
} |
| 1624 |
qsort(node->buf, length, 1, compare_char); |
| 1625 |
|
| 1626 |
|
| 1627 |
dst = node->buf; |
| 1628 |
src_next = node->buf; |
| 1629 |
while (*src_next++) { |
| 1630 |
if (*dst != *src_next) { |
| 1631 |
*++dst = *src_next; |
| 1632 |
} |
| 1633 |
} |
| 1634 |
|
| 1635 |
length = strlen(node->buf); |
| 1636 |
length <<= 8; |
| 1637 |
node->specif = length | (unsigned char) node->buf[0]; |
| 1638 |
break; |
| 1639 |
} else if (*src == '-') { |
| 1640 |
|
| 1641 |
++src; |
| 1642 |
} else { |
| 1643 |
if (*src == '\\') { |
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
node->buf[0] = *++src; |
| 1650 |
if (!node->buf[0]) { |
| 1651 |
break; |
| 1652 |
} |
| 1653 |
} else { |
| 1654 |
node->buf[0] = *src; |
| 1655 |
if (pattern) { |
| 1656 |
|
| 1657 |
if (node->buf[0] == 'n') { |
| 1658 |
node->buf[0] = 'N'; |
| 1659 |
} else if (node->buf[0] == 'x') { |
| 1660 |
node->buf[0] = 'X'; |
| 1661 |
} else if (node->buf[0] == 'z') { |
| 1662 |
node->buf[0] = 'Z'; |
| 1663 |
} |
| 1664 |
} |
| 1665 |
} |
| 1666 |
node->buf[1] = '\0'; |
| 1667 |
node->specif = 1; |
| 1668 |
++src; |
| 1669 |
break; |
| 1670 |
} |
| 1671 |
} |
| 1672 |
return src; |
| 1673 |
|
| 1674 |
#undef INC_DST_OVERFLOW_CHECK |
| 1675 |
} |
| 1676 |
|
| 1677 |
#define MAX_EXTENBUF_SIZE 512 |
| 1678 |
static struct match_char *add_exten_to_pattern_tree(struct ast_context *con, struct ast_exten *e1, int findonly) |
| 1679 |
{ |
| 1680 |
struct match_char *m1 = NULL; |
| 1681 |
struct match_char *m2 = NULL; |
| 1682 |
struct match_char **m0; |
| 1683 |
const char *pos; |
| 1684 |
int already; |
| 1685 |
int pattern = 0; |
| 1686 |
int idx_cur; |
| 1687 |
int idx_next; |
| 1688 |
char extenbuf[MAX_EXTENBUF_SIZE]; |
| 1689 |
volatile size_t required_space = strlen(e1->exten) + 1; |
| 1690 |
struct pattern_node pat_node[2]; |
| 1691 |
|
| 1692 |
if (e1->matchcid) { |
| 1693 |
required_space += (strlen(e1->cidmatch) + 2 ); |
| 1694 |
if (required_space > MAX_EXTENBUF_SIZE) { |
| 1695 |
ast_log(LOG_ERROR, |
| 1696 |
"The pattern %s/%s is too big to deal with: it will be ignored! Disaster!\n", |
| 1697 |
e1->exten, e1->cidmatch); |
| 1698 |
return NULL; |
| 1699 |
} |
| 1700 |
sprintf(extenbuf, "%s/%s", e1->exten, e1->cidmatch); |
| 1701 |
} else { |
| 1702 |
if (required_space > MAX_EXTENBUF_SIZE) { |
| 1703 |
ast_log(LOG_ERROR, |
| 1704 |
"The pattern %s/%s is too big to deal with: it will be ignored! Disaster!\n", |
| 1705 |
e1->exten, e1->cidmatch); |
| 1706 |
return NULL; |
| 1707 |
} |
| 1708 |
ast_copy_string(extenbuf, e1->exten, required_space); |
| 1709 |
} |
| 1710 |
|
| 1711 |
#ifdef NEED_DEBUG |
| 1712 |
ast_debug(1, "Adding exten %s to tree\n", extenbuf); |
| 1713 |
#endif |
| 1714 |
m1 = con->pattern_tree; |
| 1715 |
m0 = &con->pattern_tree; |
| 1716 |
already = 1; |
| 1717 |
|
| 1718 |
pos = extenbuf; |
| 1719 |
if (*pos == '_') { |
| 1720 |
pattern = 1; |
| 1721 |
++pos; |
| 1722 |
} |
| 1723 |
idx_cur = 0; |
| 1724 |
pos = get_pattern_node(&pat_node[idx_cur], pos, pattern, extenbuf); |
| 1725 |
for (; pat_node[idx_cur].buf[0]; idx_cur = idx_next) { |
| 1726 |
idx_next = (idx_cur + 1) % ARRAY_LEN(pat_node); |
| 1727 |
pos = get_pattern_node(&pat_node[idx_next], pos, pattern, extenbuf); |
| 1728 |
|
| 1729 |
|
| 1730 |
m2 = NULL; |
| 1731 |
if (already && (m2 = already_in_tree(m1, pat_node[idx_cur].buf, pattern)) |
| 1732 |
&& m2->next_char) { |
| 1733 |
if (!pat_node[idx_next].buf[0]) { |
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
if (findonly) { |
| 1740 |
return m2; |
| 1741 |
} |
| 1742 |
if (m2->exten) { |
| 1743 |
ast_log(LOG_WARNING, "Found duplicate exten. Had %s found %s\n", |
| 1744 |
m2->deleted ? "(deleted/invalid)" : m2->exten->name, e1->name); |
| 1745 |
} |
| 1746 |
m2->exten = e1; |
| 1747 |
m2->deleted = 0; |
| 1748 |
} |
| 1749 |
m1 = m2->next_char; |
| 1750 |
m0 = &m2->next_char; |
| 1751 |
} else { |
| 1752 |
if (m2) { |
| 1753 |
if (findonly) { |
| 1754 |
return m2; |
| 1755 |
} |
| 1756 |
m1 = m2; |
| 1757 |
} else { |
| 1758 |
if (findonly) { |
| 1759 |
return m1; |
| 1760 |
} |
| 1761 |
m1 = add_pattern_node(con, m1, &pat_node[idx_cur], pattern, already, m0); |
| 1762 |
if (!m1) { |
| 1763 |
return NULL; |
| 1764 |
} |
| 1765 |
m0 = &m1->next_char; |
| 1766 |
} |
| 1767 |
if (!pat_node[idx_next].buf[0]) { |
| 1768 |
if (m2 && m2->exten) { |
| 1769 |
ast_log(LOG_WARNING, "Found duplicate exten. Had %s found %s\n", |
| 1770 |
m2->deleted ? "(deleted/invalid)" : m2->exten->name, e1->name); |
| 1771 |
} |
| 1772 |
m1->deleted = 0; |
| 1773 |
m1->exten = e1; |
| 1774 |
} |
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
already = 0; |
| 1780 |
} |
| 1781 |
} |
| 1782 |
return m1; |
| 1783 |
} |
| 1784 |
|
| 1785 |
static void create_match_char_tree(struct ast_context *con) |
| 1786 |
{ |
| 1787 |
struct ast_hashtab_iter *t1; |
| 1788 |
struct ast_exten *e1; |
| 1789 |
#ifdef NEED_DEBUG |
| 1790 |
int biggest_bucket, resizes, numobjs, numbucks; |
| 1791 |
|
| 1792 |
ast_debug(1, "Creating Extension Trie for context %s(%p)\n", con->name, con); |
| 1793 |
ast_hashtab_get_stats(con->root_table, &biggest_bucket, &resizes, &numobjs, &numbucks); |
| 1794 |
ast_debug(1, "This tree has %d objects in %d bucket lists, longest list=%d objects, and has resized %d times\n", |
| 1795 |
numobjs, numbucks, biggest_bucket, resizes); |
| 1796 |
#endif |
| 1797 |
t1 = ast_hashtab_start_traversal(con->root_table); |
| 1798 |
while ((e1 = ast_hashtab_next(t1))) { |
| 1799 |
if (e1->exten) { |
| 1800 |
add_exten_to_pattern_tree(con, e1, 0); |
| 1801 |
} else { |
| 1802 |
ast_log(LOG_ERROR, "Attempt to create extension with no extension name.\n"); |
| 1803 |
} |
| 1804 |
} |
| 1805 |
ast_hashtab_end_traversal(t1); |
| 1806 |
} |
| 1807 |
|
| 1808 |
static void destroy_pattern_tree(struct match_char *pattern_tree) |
| 1809 |
{ |
| 1810 |
|
| 1811 |
if (pattern_tree->alt_char) { |
| 1812 |
destroy_pattern_tree(pattern_tree->alt_char); |
| 1813 |
pattern_tree->alt_char = 0; |
| 1814 |
} |
| 1815 |
|
| 1816 |
if (pattern_tree->next_char) { |
| 1817 |
destroy_pattern_tree(pattern_tree->next_char); |
| 1818 |
pattern_tree->next_char = 0; |
| 1819 |
} |
| 1820 |
pattern_tree->exten = 0; |
| 1821 |
ast_free(pattern_tree); |
| 1822 |
} |
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
static int ext_cmp_exten_strlen(const char *str) |
| 1833 |
{ |
| 1834 |
int len; |
| 1835 |
|
| 1836 |
len = 0; |
| 1837 |
for (;;) { |
| 1838 |
|
| 1839 |
while (*str == '-') { |
| 1840 |
++str; |
| 1841 |
} |
| 1842 |
if (!*str) { |
| 1843 |
break; |
| 1844 |
} |
| 1845 |
++str; |
| 1846 |
++len; |
| 1847 |
} |
| 1848 |
return len; |
| 1849 |
} |
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
static int ext_cmp_exten_partial(const char *left, const char *right) |
| 1863 |
{ |
| 1864 |
int cmp; |
| 1865 |
|
| 1866 |
for (;;) { |
| 1867 |
|
| 1868 |
while (*left == '-') { |
| 1869 |
++left; |
| 1870 |
} |
| 1871 |
while (*right == '-') { |
| 1872 |
++right; |
| 1873 |
} |
| 1874 |
|
| 1875 |
if (!*right) { |
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
cmp = 0; |
| 1881 |
break; |
| 1882 |
} |
| 1883 |
|
| 1884 |
cmp = *left - *right; |
| 1885 |
if (cmp) { |
| 1886 |
break; |
| 1887 |
} |
| 1888 |
++left; |
| 1889 |
++right; |
| 1890 |
} |
| 1891 |
return cmp; |
| 1892 |
} |
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
static int ext_cmp_exten(const char *left, const char *right) |
| 1906 |
{ |
| 1907 |
int cmp; |
| 1908 |
|
| 1909 |
for (;;) { |
| 1910 |
|
| 1911 |
while (*left == '-') { |
| 1912 |
++left; |
| 1913 |
} |
| 1914 |
while (*right == '-') { |
| 1915 |
++right; |
| 1916 |
} |
| 1917 |
|
| 1918 |
cmp = *left - *right; |
| 1919 |
if (cmp) { |
| 1920 |
break; |
| 1921 |
} |
| 1922 |
if (!*left) { |
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
break; |
| 1928 |
} |
| 1929 |
++left; |
| 1930 |
++right; |
| 1931 |
} |
| 1932 |
return cmp; |
| 1933 |
} |
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
static int ext_cmp_pattern_pos(const char **p, unsigned char *bitwise) |
| 1993 |
{ |
| 1994 |
#define BITS_PER 8 |
| 1995 |
unsigned char c; |
| 1996 |
unsigned char cmin; |
| 1997 |
int count; |
| 1998 |
const char *end; |
| 1999 |
|
| 2000 |
do { |
| 2001 |
|
| 2002 |
do { |
| 2003 |
c = *(*p)++; |
| 2004 |
} while (c == '-'); |
| 2005 |
|
| 2006 |
|
| 2007 |
switch (c) { |
| 2008 |
default: |
| 2009 |
|
| 2010 |
bitwise[c / BITS_PER] = 1 << ((BITS_PER - 1) - (c % BITS_PER)); |
| 2011 |
return 0x0100 | c; |
| 2012 |
|
| 2013 |
case 'n': |
| 2014 |
case 'N': |
| 2015 |
|
| 2016 |
bitwise[6] = 0x3f; |
| 2017 |
bitwise[7] = 0xc0; |
| 2018 |
return 0x0800 | '2'; |
| 2019 |
|
| 2020 |
case 'x': |
| 2021 |
case 'X': |
| 2022 |
|
| 2023 |
bitwise[6] = 0xff; |
| 2024 |
bitwise[7] = 0xc0; |
| 2025 |
return 0x0A00 | '0'; |
| 2026 |
|
| 2027 |
case 'z': |
| 2028 |
case 'Z': |
| 2029 |
|
| 2030 |
bitwise[6] = 0x7f; |
| 2031 |
bitwise[7] = 0xc0; |
| 2032 |
return 0x0900 | '1'; |
| 2033 |
|
| 2034 |
case '.': |
| 2035 |
|
| 2036 |
return 0x18000; |
| 2037 |
|
| 2038 |
case '!': |
| 2039 |
|
| 2040 |
return 0x28000; |
| 2041 |
|
| 2042 |
case '\0': |
| 2043 |
|
| 2044 |
*p = NULL; |
| 2045 |
return 0x30000; |
| 2046 |
|
| 2047 |
case '[': |
| 2048 |
|
| 2049 |
break; |
| 2050 |
} |
| 2051 |
|
| 2052 |
end = strchr(*p, ']'); |
| 2053 |
|
| 2054 |
if (!end) { |
| 2055 |
ast_log(LOG_WARNING, "Wrong usage of [] in the extension\n"); |
| 2056 |
return 0x40000; |
| 2057 |
} |
| 2058 |
|
| 2059 |
count = 0; |
| 2060 |
cmin = 0xFF; |
| 2061 |
for (; *p < end; ++*p) { |
| 2062 |
unsigned char c1; |
| 2063 |
unsigned char c2; |
| 2064 |
|
| 2065 |
c1 = (*p)[0]; |
| 2066 |
if (*p + 2 < end && (*p)[1] == '-') { |
| 2067 |
c2 = (*p)[2]; |
| 2068 |
*p += 2; |
| 2069 |
} else { |
| 2070 |
c2 = c1; |
| 2071 |
} |
| 2072 |
if (c1 < cmin) { |
| 2073 |
cmin = c1; |
| 2074 |
} |
| 2075 |
for (; c1 <= c2; ++c1) { |
| 2076 |
unsigned char mask = 1 << ((BITS_PER - 1) - (c1 % BITS_PER)); |
| 2077 |
|
| 2078 |
|
| 2079 |
|
| 2080 |
|
| 2081 |
|
| 2082 |
|
| 2083 |
|
| 2084 |
if (!(bitwise[c1 / BITS_PER] & mask)) { |
| 2085 |
|
| 2086 |
bitwise[c1 / BITS_PER] |= mask; |
| 2087 |
count += 0x100; |
| 2088 |
} |
| 2089 |
} |
| 2090 |
} |
| 2091 |
++*p; |
| 2092 |
} while (!count); |
| 2093 |
return count | cmin; |
| 2094 |
} |
| 2095 |
|
| 2096 |
|
| 2097 |
|
| 2098 |
|
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
|
| 2103 |
|
| 2104 |
|
| 2105 |
|
| 2106 |
|
| 2107 |
static int ext_cmp_pattern(const char *left, const char *right) |
| 2108 |
{ |
| 2109 |
int cmp; |
| 2110 |
int left_pos; |
| 2111 |
int right_pos; |
| 2112 |
|
| 2113 |
for (;;) { |
| 2114 |
unsigned char left_bitwise[32] = { 0, }; |
| 2115 |
unsigned char right_bitwise[32] = { 0, }; |
| 2116 |
|
| 2117 |
left_pos = ext_cmp_pattern_pos(&left, left_bitwise); |
| 2118 |
right_pos = ext_cmp_pattern_pos(&right, right_bitwise); |
| 2119 |
cmp = left_pos - right_pos; |
| 2120 |
if (!cmp) { |
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
cmp = memcmp(right_bitwise, left_bitwise, ARRAY_LEN(left_bitwise)); |
| 2129 |
} |
| 2130 |
if (cmp) { |
| 2131 |
break; |
| 2132 |
} |
| 2133 |
if (!left) { |
| 2134 |
|
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
break; |
| 2139 |
} |
| 2140 |
} |
| 2141 |
return cmp; |
| 2142 |
} |
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
|
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
static int ext_cmp(const char *left, const char *right) |
| 2156 |
{ |
| 2157 |
|
| 2158 |
if (left[0] != '_') { |
| 2159 |
if (right[0] == '_') { |
| 2160 |
return -1; |
| 2161 |
} |
| 2162 |
|
| 2163 |
return ext_cmp_exten(left, right); |
| 2164 |
} |
| 2165 |
if (right[0] != '_') { |
| 2166 |
return 1; |
| 2167 |
} |
| 2168 |
|
| 2169 |
|
| 2170 |
|
| 2171 |
|
| 2172 |
|
| 2173 |
|
| 2174 |
return ext_cmp_pattern(left + 1, right + 1); |
| 2175 |
} |
| 2176 |
|
| 2177 |
static int ext_fluff_count(const char *exten) |
| 2178 |
{ |
| 2179 |
int fluff = 0; |
| 2180 |
|
| 2181 |
if (*exten != '_') { |
| 2182 |
|
| 2183 |
while (*exten) { |
| 2184 |
if (*exten == '-') { |
| 2185 |
fluff++; |
| 2186 |
} |
| 2187 |
exten++; |
| 2188 |
} |
| 2189 |
|
| 2190 |
return fluff; |
| 2191 |
} |
| 2192 |
|
| 2193 |
|
| 2194 |
while (*exten) { |
| 2195 |
if (*exten == '-') { |
| 2196 |
fluff++; |
| 2197 |
} else if (*exten == '[') { |
| 2198 |
|
| 2199 |
exten = strchr(exten, ']'); |
| 2200 |
|
| 2201 |
if (!exten) { |
| 2202 |
|
| 2203 |
return fluff; |
| 2204 |
} |
| 2205 |
} |
| 2206 |
exten++; |
| 2207 |
} |
| 2208 |
|
| 2209 |
return fluff; |
| 2210 |
} |
| 2211 |
|
| 2212 |
int ast_extension_cmp(const char *a, const char *b) |
| 2213 |
{ |
| 2214 |
int cmp; |
| 2215 |
|
| 2216 |
cmp = ext_cmp(a, b); |
| 2217 |
if (cmp < 0) { |
| 2218 |
return -1; |
| 2219 |
} |
| 2220 |
if (cmp > 0) { |
| 2221 |
return 1; |
| 2222 |
} |
| 2223 |
return 0; |
| 2224 |
} |
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
static int _extension_match_core(const char *pattern, const char *data, enum ext_match_t mode) |
| 2239 |
{ |
| 2240 |
mode &= E_MATCH_MASK; |
| 2241 |
|
| 2242 |
#ifdef NEED_DEBUG_HERE |
| 2243 |
ast_log(LOG_NOTICE,"match core: pat: '%s', dat: '%s', mode=%d\n", pattern, data, (int)mode); |
| 2244 |
#endif |
| 2245 |
|
| 2246 |
if (pattern[0] != '_') { |
| 2247 |
int lp = ext_cmp_exten_strlen(pattern); |
| 2248 |
int ld = ext_cmp_exten_strlen(data); |
| 2249 |
|
| 2250 |
if (lp < ld) { |
| 2251 |
#ifdef NEED_DEBUG_HERE |
| 2252 |
ast_log(LOG_NOTICE,"return (0) - pattern too short, cannot match\n"); |
| 2253 |
#endif |
| 2254 |
return 0; |
| 2255 |
} |
| 2256 |
|
| 2257 |
if (mode == E_MATCH) { |
| 2258 |
#ifdef NEED_DEBUG_HERE |
| 2259 |
ast_log(LOG_NOTICE,"return (!ext_cmp_exten(%s,%s) when mode== E_MATCH)\n", pattern, data); |
| 2260 |
#endif |
| 2261 |
return !ext_cmp_exten(pattern, data); |
| 2262 |
} |
| 2263 |
if (ld == 0 || !ext_cmp_exten_partial(pattern, data)) { |
| 2264 |
#ifdef NEED_DEBUG_HERE |
| 2265 |
ast_log(LOG_NOTICE,"return (mode(%d) == E_MATCHMORE ? lp(%d) > ld(%d) : 1)\n", mode, lp, ld); |
| 2266 |
#endif |
| 2267 |
return (mode == E_MATCHMORE) ? lp > ld : 1; |
| 2268 |
} else { |
| 2269 |
#ifdef NEED_DEBUG_HERE |
| 2270 |
ast_log(LOG_NOTICE,"return (0) when ld(%d) > 0 && pattern(%s) != data(%s)\n", ld, pattern, data); |
| 2271 |
#endif |
| 2272 |
return 0; |
| 2273 |
} |
| 2274 |
} |
| 2275 |
if (mode == E_MATCH && data[0] == '_') { |
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
#ifdef NEED_DEBUG_HERE |
| 2286 |
ast_log(LOG_NOTICE, "Comparing as patterns first. pattern:%s data:%s\n", pattern, data); |
| 2287 |
#endif |
| 2288 |
if (!ext_cmp_pattern(pattern + 1, data + 1)) { |
| 2289 |
#ifdef NEED_DEBUG_HERE |
| 2290 |
ast_log(LOG_NOTICE,"return (1) - pattern matches pattern\n"); |
| 2291 |
#endif |
| 2292 |
return 1; |
| 2293 |
} |
| 2294 |
} |
| 2295 |
|
| 2296 |
++pattern; |
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
for (;;) { |
| 2302 |
const char *end; |
| 2303 |
|
| 2304 |
|
| 2305 |
while (*data == '-') { |
| 2306 |
++data; |
| 2307 |
} |
| 2308 |
while (*pattern == '-') { |
| 2309 |
++pattern; |
| 2310 |
} |
| 2311 |
if (!*data || !*pattern || *pattern == '/') { |
| 2312 |
break; |
| 2313 |
} |
| 2314 |
|
| 2315 |
switch (*pattern) { |
| 2316 |
case '[': |
| 2317 |
++pattern; |
| 2318 |
end = strchr(pattern, ']'); |
| 2319 |
if (!end) { |
| 2320 |
ast_log(LOG_WARNING, "Wrong usage of [] in the extension\n"); |
| 2321 |
return 0; |
| 2322 |
} |
| 2323 |
if (pattern == end) { |
| 2324 |
|
| 2325 |
++pattern; |
| 2326 |
continue; |
| 2327 |
} |
| 2328 |
for (; pattern < end; ++pattern) { |
| 2329 |
if (pattern+2 < end && pattern[1] == '-') { |
| 2330 |
if (*data >= pattern[0] && *data <= pattern[2]) |
| 2331 |
break; |
| 2332 |
else { |
| 2333 |
pattern += 2; |
| 2334 |
continue; |
| 2335 |
} |
| 2336 |
} else if (*data == pattern[0]) |
| 2337 |
break; |
| 2338 |
} |
| 2339 |
if (pattern >= end) { |
| 2340 |
#ifdef NEED_DEBUG_HERE |
| 2341 |
ast_log(LOG_NOTICE,"return (0) when pattern>=end\n"); |
| 2342 |
#endif |
| 2343 |
return 0; |
| 2344 |
} |
| 2345 |
pattern = end; |
| 2346 |
break; |
| 2347 |
case 'n': |
| 2348 |
case 'N': |
| 2349 |
if (*data < '2' || *data > '9') { |
| 2350 |
#ifdef NEED_DEBUG_HERE |
| 2351 |
ast_log(LOG_NOTICE,"return (0) N is not matched\n"); |
| 2352 |
#endif |
| 2353 |
return 0; |
| 2354 |
} |
| 2355 |
break; |
| 2356 |
case 'x': |
| 2357 |
case 'X': |
| 2358 |
if (*data < '0' || *data > '9') { |
| 2359 |
#ifdef NEED_DEBUG_HERE |
| 2360 |
ast_log(LOG_NOTICE,"return (0) X is not matched\n"); |
| 2361 |
#endif |
| 2362 |
return 0; |
| 2363 |
} |
| 2364 |
break; |
| 2365 |
case 'z': |
| 2366 |
case 'Z': |
| 2367 |
if (*data < '1' || *data > '9') { |
| 2368 |
#ifdef NEED_DEBUG_HERE |
| 2369 |
ast_log(LOG_NOTICE,"return (0) Z is not matched\n"); |
| 2370 |
#endif |
| 2371 |
return 0; |
| 2372 |
} |
| 2373 |
break; |
| 2374 |
case '.': |
| 2375 |
#ifdef NEED_DEBUG_HERE |
| 2376 |
ast_log(LOG_NOTICE, "return (1) when '.' is matched\n"); |
| 2377 |
#endif |
| 2378 |
return 1; |
| 2379 |
case '!': |
| 2380 |
#ifdef NEED_DEBUG_HERE |
| 2381 |
ast_log(LOG_NOTICE, "return (2) when '!' is matched\n"); |
| 2382 |
#endif |
| 2383 |
return 2; |
| 2384 |
default: |
| 2385 |
if (*data != *pattern) { |
| 2386 |
#ifdef NEED_DEBUG_HERE |
| 2387 |
ast_log(LOG_NOTICE, "return (0) when *data(%c) != *pattern(%c)\n", *data, *pattern); |
| 2388 |
#endif |
| 2389 |
return 0; |
| 2390 |
} |
| 2391 |
break; |
| 2392 |
} |
| 2393 |
++data; |
| 2394 |
++pattern; |
| 2395 |
} |
| 2396 |
if (*data) { |
| 2397 |
#ifdef NEED_DEBUG_HERE |
| 2398 |
ast_log(LOG_NOTICE, "return (0) when data longer than pattern\n"); |
| 2399 |
#endif |
| 2400 |
return 0; |
| 2401 |
} |
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
|
| 2406 |
|
| 2407 |
if (*pattern == '\0' || *pattern == '/') { |
| 2408 |
#ifdef NEED_DEBUG_HERE |
| 2409 |
ast_log(LOG_NOTICE, "at end, return (%d) in 'exact match'\n", (mode==E_MATCHMORE) ? 0 : 1); |
| 2410 |
#endif |
| 2411 |
return (mode == E_MATCHMORE) ? 0 : 1; |
| 2412 |
} else if (*pattern == '!') { |
| 2413 |
#ifdef NEED_DEBUG_HERE |
| 2414 |
ast_log(LOG_NOTICE, "at end, return (2) when '!' is matched\n"); |
| 2415 |
#endif |
| 2416 |
return 2; |
| 2417 |
} else { |
| 2418 |
#ifdef NEED_DEBUG_HERE |
| 2419 |
ast_log(LOG_NOTICE, "at end, return (%d) which deps on E_MATCH\n", (mode == E_MATCH) ? 0 : 1); |
| 2420 |
#endif |
| 2421 |
return (mode == E_MATCH) ? 0 : 1; |
| 2422 |
} |
| 2423 |
} |
| 2424 |
|
| 2425 |
|
| 2426 |
|
| 2427 |
|
| 2428 |
|
| 2429 |
static int extension_match_core(const char *pattern, const char *data, enum ext_match_t mode) |
| 2430 |
{ |
| 2431 |
int i; |
| 2432 |
static int prof_id = -2; |
| 2433 |
if (prof_id == -2) { |
| 2434 |
prof_id = ast_add_profile("ext_match", 0); |
| 2435 |
} |
| 2436 |
ast_mark(prof_id, 1); |
| 2437 |
i = _extension_match_core(ast_strlen_zero(pattern) ? "" : pattern, ast_strlen_zero(data) ? "" : data, mode); |
| 2438 |
ast_mark(prof_id, 0); |
| 2439 |
return i; |
| 2440 |
} |
| 2441 |
|
| 2442 |
int ast_extension_match(const char *pattern, const char *extension) |
| 2443 |
{ |
| 2444 |
return extension_match_core(pattern, extension, E_MATCH); |
| 2445 |
} |
| 2446 |
|
| 2447 |
int ast_extension_close(const char *pattern, const char *data, int needmore) |
| 2448 |
{ |
| 2449 |
if (needmore != E_MATCHMORE && needmore != E_CANMATCH) |
| 2450 |
ast_log(LOG_WARNING, "invalid argument %d\n", needmore); |
| 2451 |
return extension_match_core(pattern, data, needmore); |
| 2452 |
} |
| 2453 |
|
| 2454 |
struct ast_context *ast_context_find(const char *name) |
| 2455 |
{ |
| 2456 |
struct ast_context *tmp; |
| 2457 |
struct ast_context item = { |
| 2458 |
.name = name, |
| 2459 |
}; |
| 2460 |
|
| 2461 |
if (!name) { |
| 2462 |
return NULL; |
| 2463 |
} |
| 2464 |
ast_rdlock_contexts(); |
| 2465 |
if (contexts_table) { |
| 2466 |
tmp = ast_hashtab_lookup(contexts_table, &item); |
| 2467 |
} else { |
| 2468 |
tmp = NULL; |
| 2469 |
while ((tmp = ast_walk_contexts(tmp))) { |
| 2470 |
if (!strcasecmp(name, tmp->name)) { |
| 2471 |
break; |
| 2472 |
} |
| 2473 |
} |
| 2474 |
} |
| 2475 |
ast_unlock_contexts(); |
| 2476 |
return tmp; |
| 2477 |
} |
| 2478 |
|
| 2479 |
#define STATUS_NO_CONTEXT 1 |
| 2480 |
#define STATUS_NO_EXTENSION 2 |
| 2481 |
#define STATUS_NO_PRIORITY 3 |
| 2482 |
#define STATUS_NO_LABEL 4 |
| 2483 |
#define STATUS_SUCCESS 5 |
| 2484 |
|
| 2485 |
static int matchcid(const char *cidpattern, const char *callerid) |
| 2486 |
{ |
| 2487 |
|
| 2488 |
|
| 2489 |
|
| 2490 |
if (ast_strlen_zero(callerid)) { |
| 2491 |
return ast_strlen_zero(cidpattern) ? 1 : 0; |
| 2492 |
} |
| 2493 |
|
| 2494 |
return ast_extension_match(cidpattern, callerid); |
| 2495 |
} |
| 2496 |
|
| 2497 |
struct ast_exten *pbx_find_extension(struct ast_channel *chan, |
| 2498 |
struct ast_context *bypass, struct pbx_find_info *q, |
| 2499 |
const char *context, const char *exten, int priority, |
| 2500 |
const char *label, const char *callerid, enum ext_match_t action) |
| 2501 |
{ |
| 2502 |
int x, res; |
| 2503 |
struct ast_context *tmp = NULL; |
| 2504 |
struct ast_exten *e = NULL, *eroot = NULL; |
| 2505 |
struct ast_exten pattern = {NULL, }; |
| 2506 |
struct scoreboard score = {0, }; |
| 2507 |
struct ast_str *tmpdata = NULL; |
| 2508 |
int idx; |
| 2509 |
|
| 2510 |
pattern.label = label; |
| 2511 |
pattern.priority = priority; |
| 2512 |
#ifdef NEED_DEBUG_HERE |
| 2513 |
ast_log(LOG_NOTICE, "Looking for cont/ext/prio/label/action = %s/%s/%d/%s/%d\n", context, exten, priority, label, (int) action); |
| 2514 |
#endif |
| 2515 |
|
| 2516 |
|
| 2517 |
if (q->stacklen == 0) { |
| 2518 |
q->status = STATUS_NO_CONTEXT; |
| 2519 |
q->swo = NULL; |
| 2520 |
q->data = NULL; |
| 2521 |
q->foundcontext = NULL; |
| 2522 |
} else if (q->stacklen >= AST_PBX_MAX_STACK) { |
| 2523 |
ast_log(LOG_WARNING, "Maximum PBX stack (%d) exceeded. Too many includes?\n", AST_PBX_MAX_STACK); |
| 2524 |
return NULL; |
| 2525 |
} |
| 2526 |
|
| 2527 |
|
| 2528 |
for (x = 0; x < q->stacklen; x++) { |
| 2529 |
if (!strcasecmp(q->incstack[x], context)) |
| 2530 |
return NULL; |
| 2531 |
} |
| 2532 |
|
| 2533 |
if (bypass) { |
| 2534 |
tmp = bypass; |
| 2535 |
} else { |
| 2536 |
tmp = find_context(context); |
| 2537 |
if (!tmp) { |
| 2538 |
return NULL; |
| 2539 |
} |
| 2540 |
} |
| 2541 |
|
| 2542 |
if (q->status < STATUS_NO_EXTENSION) |
| 2543 |
q->status = STATUS_NO_EXTENSION; |
| 2544 |
|
| 2545 |
|
| 2546 |
|
| 2547 |
eroot = NULL; |
| 2548 |
score.total_specificity = 0; |
| 2549 |
score.exten = 0; |
| 2550 |
score.total_length = 0; |
| 2551 |
if (!tmp->pattern_tree && tmp->root_table) { |
| 2552 |
create_match_char_tree(tmp); |
| 2553 |
#ifdef NEED_DEBUG |
| 2554 |
ast_debug(1, "Tree Created in context %s:\n", context); |
| 2555 |
log_match_char_tree(tmp->pattern_tree," "); |
| 2556 |
#endif |
| 2557 |
} |
| 2558 |
#ifdef NEED_DEBUG |
| 2559 |
ast_log(LOG_NOTICE, "The Trie we are searching in:\n"); |
| 2560 |
log_match_char_tree(tmp->pattern_tree, ":: "); |
| 2561 |
#endif |
| 2562 |
|
| 2563 |
do { |
| 2564 |
if (!ast_strlen_zero(overrideswitch)) { |
| 2565 |
char *osw = ast_strdupa(overrideswitch), *name; |
| 2566 |
struct ast_switch *asw; |
| 2567 |
ast_switch_f *aswf = NULL; |
| 2568 |
char *datap; |
| 2569 |
int eval = 0; |
| 2570 |
|
| 2571 |
name = strsep(&osw, "/"); |
| 2572 |
asw = pbx_findswitch(name); |
| 2573 |
|
| 2574 |
if (!asw) { |
| 2575 |
ast_log(LOG_WARNING, "No such switch '%s'\n", name); |
| 2576 |
break; |
| 2577 |
} |
| 2578 |
|
| 2579 |
if (osw && strchr(osw, '$')) { |
| 2580 |
eval = 1; |
| 2581 |
} |
| 2582 |
|
| 2583 |
if (eval && !(tmpdata = ast_str_thread_get(&switch_data, 512))) { |
| 2584 |
ast_log(LOG_WARNING, "Can't evaluate overrideswitch?!\n"); |
| 2585 |
break; |
| 2586 |
} else if (eval) { |
| 2587 |
|
| 2588 |
pbx_substitute_variables_helper(chan, osw, ast_str_buffer(tmpdata), ast_str_size(tmpdata)); |
| 2589 |
datap = ast_str_buffer(tmpdata); |
| 2590 |
} else { |
| 2591 |
datap = osw; |
| 2592 |
} |
| 2593 |
|
| 2594 |
|
| 2595 |
if (action == E_CANMATCH) |
| 2596 |
aswf = asw->canmatch; |
| 2597 |
else if (action == E_MATCHMORE) |
| 2598 |
aswf = asw->matchmore; |
| 2599 |
else |
| 2600 |
aswf = asw->exists; |
| 2601 |
if (!aswf) { |
| 2602 |
res = 0; |
| 2603 |
} else { |
| 2604 |
if (chan) { |
| 2605 |
ast_autoservice_start(chan); |
| 2606 |
} |
| 2607 |
res = aswf(chan, context, exten, priority, callerid, datap); |
| 2608 |
if (chan) { |
| 2609 |
ast_autoservice_stop(chan); |
| 2610 |
} |
| 2611 |
} |
| 2612 |
if (res) { |
| 2613 |
q->swo = asw; |
| 2614 |
q->data = datap; |
| 2615 |
q->foundcontext = context; |
| 2616 |
|
| 2617 |
return NULL; |
| 2618 |
} |
| 2619 |
} |
| 2620 |
} while (0); |
| 2621 |
|
| 2622 |
if (extenpatternmatchnew) { |
| 2623 |
new_find_extension(exten, &score, tmp->pattern_tree, 0, 0, callerid, label, action); |
| 2624 |
eroot = score.exten; |
| 2625 |
|
| 2626 |
if (score.last_char == '!' && action == E_MATCHMORE) { |
| 2627 |
|
| 2628 |
|
| 2629 |
|
| 2630 |
#ifdef NEED_DEBUG_HERE |
| 2631 |
ast_log(LOG_NOTICE,"Returning MATCHMORE NULL with exclamation point.\n"); |
| 2632 |
#endif |
| 2633 |
return NULL; |
| 2634 |
} |
| 2635 |
|
| 2636 |
if (!eroot && (action == E_CANMATCH || action == E_MATCHMORE) && score.canmatch_exten) { |
| 2637 |
q->status = STATUS_SUCCESS; |
| 2638 |
#ifdef NEED_DEBUG_HERE |
| 2639 |
ast_log(LOG_NOTICE,"Returning CANMATCH exten %s\n", score.canmatch_exten->exten); |
| 2640 |
#endif |
| 2641 |
return score.canmatch_exten; |
| 2642 |
} |
| 2643 |
|
| 2644 |
if ((action == E_MATCHMORE || action == E_CANMATCH) && eroot) { |
| 2645 |
if (score.node) { |
| 2646 |
struct ast_exten *z = trie_find_next_match(score.node); |
| 2647 |
if (z) { |
| 2648 |
#ifdef NEED_DEBUG_HERE |
| 2649 |
ast_log(LOG_NOTICE,"Returning CANMATCH/MATCHMORE next_match exten %s\n", z->exten); |
| 2650 |
#endif |
| 2651 |
} else { |
| 2652 |
if (score.canmatch_exten) { |
| 2653 |
#ifdef NEED_DEBUG_HERE |
| 2654 |
ast_log(LOG_NOTICE,"Returning CANMATCH/MATCHMORE canmatchmatch exten %s(%p)\n", score.canmatch_exten->exten, score.canmatch_exten); |
| 2655 |
#endif |
| 2656 |
return score.canmatch_exten; |
| 2657 |
} else { |
| 2658 |
#ifdef NEED_DEBUG_HERE |
| 2659 |
ast_log(LOG_NOTICE,"Returning CANMATCH/MATCHMORE next_match exten NULL\n"); |
| 2660 |
#endif |
| 2661 |
} |
| 2662 |
} |
| 2663 |
return z; |
| 2664 |
} |
| 2665 |
#ifdef NEED_DEBUG_HERE |
| 2666 |
ast_log(LOG_NOTICE, "Returning CANMATCH/MATCHMORE NULL (no next_match)\n"); |
| 2667 |
#endif |
| 2668 |
return NULL; |
| 2669 |
} |
| 2670 |
|
| 2671 |
if (eroot) { |
| 2672 |
|
| 2673 |
if (q->status < STATUS_NO_PRIORITY) |
| 2674 |
q->status = STATUS_NO_PRIORITY; |
| 2675 |
e = NULL; |
| 2676 |
if (action == E_FINDLABEL && label ) { |
| 2677 |
if (q->status < STATUS_NO_LABEL) |
| 2678 |
q->status = STATUS_NO_LABEL; |
| 2679 |
e = ast_hashtab_lookup(eroot->peer_label_table, &pattern); |
| 2680 |
} else { |
| 2681 |
e = ast_hashtab_lookup(eroot->peer_table, &pattern); |
| 2682 |
} |
| 2683 |
if (e) { |
| 2684 |
q->status = STATUS_SUCCESS; |
| 2685 |
q->foundcontext = context; |
| 2686 |
#ifdef NEED_DEBUG_HERE |
| 2687 |
ast_log(LOG_NOTICE,"Returning complete match of exten %s\n", e->exten); |
| 2688 |
#endif |
| 2689 |
return e; |
| 2690 |
} |
| 2691 |
} |
| 2692 |
} else { |
| 2693 |
|
| 2694 |
|
| 2695 |
eroot = NULL; |
| 2696 |
while ( (eroot = ast_walk_context_extensions(tmp, eroot)) ) { |
| 2697 |
int match = extension_match_core(eroot->exten, exten, action); |
| 2698 |
|
| 2699 |
|
| 2700 |
if (!match || (eroot->matchcid && !matchcid(eroot->cidmatch, callerid))) |
| 2701 |
continue; |
| 2702 |
if (match == 2 && action == E_MATCHMORE) { |
| 2703 |
|
| 2704 |
|
| 2705 |
|
| 2706 |
return NULL; |
| 2707 |
} |
| 2708 |
|
| 2709 |
if (q->status < STATUS_NO_PRIORITY) |
| 2710 |
q->status = STATUS_NO_PRIORITY; |
| 2711 |
e = NULL; |
| 2712 |
if (action == E_FINDLABEL && label ) { |
| 2713 |
if (q->status < STATUS_NO_LABEL) |
| 2714 |
q->status = STATUS_NO_LABEL; |
| 2715 |
e = ast_hashtab_lookup(eroot->peer_label_table, &pattern); |
| 2716 |
} else { |
| 2717 |
e = ast_hashtab_lookup(eroot->peer_table, &pattern); |
| 2718 |
} |
| 2719 |
if (e) { |
| 2720 |
q->status = STATUS_SUCCESS; |
| 2721 |
q->foundcontext = context; |
| 2722 |
return e; |
| 2723 |
} |
| 2724 |
} |
| 2725 |
} |
| 2726 |
|
| 2727 |
|
| 2728 |
for (idx = 0; idx < ast_context_switches_count(tmp); idx++) { |
| 2729 |
const struct ast_sw *sw = ast_context_switches_get(tmp, idx); |
| 2730 |
struct ast_switch *asw = pbx_findswitch(ast_get_switch_name(sw)); |
| 2731 |
ast_switch_f *aswf = NULL; |
| 2732 |
const char *datap; |
| 2733 |
|
| 2734 |
if (!asw) { |
| 2735 |
ast_log(LOG_WARNING, "No such switch '%s'\n", ast_get_switch_name(sw)); |
| 2736 |
continue; |
| 2737 |
} |
| 2738 |
|
| 2739 |
|
| 2740 |
if (ast_get_switch_eval(sw)) { |
| 2741 |
if (!(tmpdata = ast_str_thread_get(&switch_data, 512))) { |
| 2742 |
ast_log(LOG_WARNING, "Can't evaluate switch?!\n"); |
| 2743 |
continue; |
| 2744 |
} |
| 2745 |
pbx_substitute_variables_helper(chan, ast_get_switch_data(sw), |
| 2746 |
ast_str_buffer(tmpdata), ast_str_size(tmpdata)); |
| 2747 |
datap = ast_str_buffer(tmpdata); |
| 2748 |
} else { |
| 2749 |
datap = ast_get_switch_data(sw); |
| 2750 |
} |
| 2751 |
|
| 2752 |
|
| 2753 |
if (action == E_CANMATCH) |
| 2754 |
aswf = asw->canmatch; |
| 2755 |
else if (action == E_MATCHMORE) |
| 2756 |
aswf = asw->matchmore; |
| 2757 |
else |
| 2758 |
aswf = asw->exists; |
| 2759 |
if (!aswf) |
| 2760 |
res = 0; |
| 2761 |
else { |
| 2762 |
if (chan) |
| 2763 |
ast_autoservice_start(chan); |
| 2764 |
res = aswf(chan, context, exten, priority, callerid, datap); |
| 2765 |
if (chan) |
| 2766 |
ast_autoservice_stop(chan); |
| 2767 |
} |
| 2768 |
if (res) { |
| 2769 |
q->swo = asw; |
| 2770 |
q->data = datap; |
| 2771 |
q->foundcontext = context; |
| 2772 |
|
| 2773 |
return NULL; |
| 2774 |
} |
| 2775 |
} |
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
q->incstack[q->stacklen++] = tmp->data; |
| 2780 |
|
| 2781 |
for (idx = 0; idx < ast_context_includes_count(tmp); idx++) { |
| 2782 |
const struct ast_include *i = ast_context_includes_get(tmp, idx); |
| 2783 |
|
| 2784 |
if (include_valid(i)) { |
| 2785 |
if ((e = pbx_find_extension(chan, bypass, q, include_rname(i), exten, priority, label, callerid, action))) { |
| 2786 |
#ifdef NEED_DEBUG_HERE |
| 2787 |
ast_log(LOG_NOTICE,"Returning recursive match of %s\n", e->exten); |
| 2788 |
#endif |
| 2789 |
return e; |
| 2790 |
} |
| 2791 |
if (q->swo) |
| 2792 |
return NULL; |
| 2793 |
} |
| 2794 |
} |
| 2795 |
return NULL; |
| 2796 |
} |
| 2797 |
|
| 2798 |
static void exception_store_free(void *data) |
| 2799 |
{ |
| 2800 |
struct pbx_exception *exception = data; |
| 2801 |
ast_string_field_free_memory(exception); |
| 2802 |
ast_free(exception); |
| 2803 |
} |
| 2804 |
|
| 2805 |
static const struct ast_datastore_info exception_store_info = { |
| 2806 |
.type = "EXCEPTION", |
| 2807 |
.destroy = exception_store_free, |
| 2808 |
}; |
| 2809 |
|
| 2810 |
|
| 2811 |
|
| 2812 |
|
| 2813 |
|
| 2814 |
|
| 2815 |
|
| 2816 |
|
| 2817 |
|
| 2818 |
|
| 2819 |
|
| 2820 |
|
| 2821 |
int raise_exception(struct ast_channel *chan, const char *reason, int priority) |
| 2822 |
{ |
| 2823 |
struct ast_datastore *ds; |
| 2824 |
struct pbx_exception *exception; |
| 2825 |
|
| 2826 |
ast_channel_lock(chan); |
| 2827 |
ds = ast_channel_datastore_find(chan, &exception_store_info, NULL); |
| 2828 |
if (!ds) { |
| 2829 |
ds = ast_datastore_alloc(&exception_store_info, NULL); |
| 2830 |
if (!ds) { |
| 2831 |
ast_channel_unlock(chan); |
| 2832 |
return -1; |
| 2833 |
} |
| 2834 |
if (!(exception = ast_calloc_with_stringfields(1, struct pbx_exception, 128))) { |
| 2835 |
ast_datastore_free(ds); |
| 2836 |
ast_channel_unlock(chan); |
| 2837 |
return -1; |
| 2838 |
} |
| 2839 |
ds->data = exception; |
| 2840 |
ast_channel_datastore_add(chan, ds); |
| 2841 |
} else |
| 2842 |
exception = ds->data; |
| 2843 |
|
| 2844 |
ast_string_field_set(exception, reason, reason); |
| 2845 |
ast_string_field_set(exception, context, ast_channel_context(chan)); |
| 2846 |
ast_string_field_set(exception, exten, ast_channel_exten(chan)); |
| 2847 |
exception->priority = ast_channel_priority(chan); |
| 2848 |
ast_channel_unlock(chan); |
| 2849 |
|
| 2850 |
set_ext_pri(chan, "e", priority); |
| 2851 |
return 0; |
| 2852 |
} |
| 2853 |
|
| 2854 |
static int acf_exception_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen) |
| 2855 |
{ |
| 2856 |
struct ast_datastore *ds; |
| 2857 |
struct pbx_exception *exception; |
| 2858 |
int res = 0; |
| 2859 |
|
| 2860 |
ast_channel_lock(chan); |
| 2861 |
ds = ast_channel_datastore_find(chan, &exception_store_info, NULL); |
| 2862 |
if (!ds || !ds->data) { |
| 2863 |
ast_channel_unlock(chan); |
| 2864 |
return -1; |
| 2865 |
} |
| 2866 |
exception = ds->data; |
| 2867 |
if (!strcasecmp(data, "REASON")) |
| 2868 |
ast_copy_string(buf, exception->reason, buflen); |
| 2869 |
else if (!strcasecmp(data, "CONTEXT")) |
| 2870 |
ast_copy_string(buf, exception->context, buflen); |
| 2871 |
else if (!strncasecmp(data, "EXTEN", 5)) |
| 2872 |
ast_copy_string(buf, exception->exten, buflen); |
| 2873 |
else if (!strcasecmp(data, "PRIORITY")) |
| 2874 |
snprintf(buf, buflen, "%d", exception->priority); |
| 2875 |
else |
| 2876 |
res = -1; |
| 2877 |
|
| 2878 |
ast_channel_unlock(chan); |
| 2879 |
return res; |
| 2880 |
} |
| 2881 |
|
| 2882 |
static struct ast_custom_function exception_function = { |
| 2883 |
.name = "EXCEPTION", |
| 2884 |
.read = acf_exception_read, |
| 2885 |
}; |
| 2886 |
|
| 2887 |
|
| 2888 |
|
| 2889 |
|
| 2890 |
|
| 2891 |
|
| 2892 |
|
| 2893 |
|
| 2894 |
|
| 2895 |
|
| 2896 |
|
| 2897 |
|
| 2898 |
|
| 2899 |
|
| 2900 |
|
| 2901 |
|
| 2902 |
|
| 2903 |
|
| 2904 |
|
| 2905 |
static int pbx_extension_helper(struct ast_channel *c, struct ast_context *con, |
| 2906 |
const char *context, const char *exten, int priority, |
| 2907 |
const char *label, const char *callerid, enum ext_match_t action, int *found, int combined_find_spawn) |
| 2908 |
{ |
| 2909 |
struct ast_exten *e; |
| 2910 |
struct ast_app *app; |
| 2911 |
char *substitute = NULL; |
| 2912 |
struct pbx_find_info q = { .stacklen = 0 }; |
| 2913 |
char passdata[EXT_DATA_SIZE]; |
| 2914 |
int matching_action = (action == E_MATCH || action == E_CANMATCH || action == E_MATCHMORE); |
| 2915 |
|
| 2916 |
ast_rdlock_contexts(); |
| 2917 |
|
| 2918 |
if (!context) { |
| 2919 |
context = con->name; |
| 2920 |
} |
| 2921 |
|
| 2922 |
if (found) |
| 2923 |
*found = 0; |
| 2924 |
|
| 2925 |
e = pbx_find_extension(c, con, &q, context, exten, priority, label, callerid, action); |
| 2926 |
if (e) { |
| 2927 |
if (found) |
| 2928 |
*found = 1; |
| 2929 |
if (matching_action) { |
| 2930 |
ast_unlock_contexts(); |
| 2931 |
return -1; |
| 2932 |
} else if (action == E_FINDLABEL) { |
| 2933 |
int res = e->priority; |
| 2934 |
|
| 2935 |
ast_unlock_contexts(); |
| 2936 |
|
| 2937 |
|
| 2938 |
return res; |
| 2939 |
} else { |
| 2940 |
if (!e->cached_app) |
| 2941 |
e->cached_app = pbx_findapp(e->app); |
| 2942 |
app = e->cached_app; |
| 2943 |
if (ast_strlen_zero(e->data)) { |
| 2944 |
*passdata = '\0'; |
| 2945 |
} else { |
| 2946 |
const char *tmp; |
| 2947 |
if ((!(tmp = strchr(e->data, '$'))) || (!strstr(tmp, "${") && !strstr(tmp, "$["))) { |
| 2948 |
|
| 2949 |
ast_copy_string(passdata, e->data, sizeof(passdata)); |
| 2950 |
} else { |
| 2951 |
|
| 2952 |
substitute = ast_strdupa(e->data); |
| 2953 |
} |
| 2954 |
} |
| 2955 |
ast_unlock_contexts(); |
| 2956 |
if (!app) { |
| 2957 |
ast_log(LOG_WARNING, "No application '%s' for extension (%s, %s, %d)\n", e->app, context, exten, priority); |
| 2958 |
return -1; |
| 2959 |
} |
| 2960 |
ast_channel_lock(c); |
| 2961 |
if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_ASYNCGOTO) { |
| 2962 |
ast_channel_unlock(c); |
| 2963 |
return 0; |
| 2964 |
} |
| 2965 |
if (ast_channel_context(c) != context) |
| 2966 |
ast_channel_context_set(c, context); |
| 2967 |
if (ast_channel_exten(c) != exten) |
| 2968 |
ast_channel_exten_set(c, exten); |
| 2969 |
ast_channel_priority_set(c, priority); |
| 2970 |
ast_channel_unlock(c); |
| 2971 |
if (substitute) { |
| 2972 |
pbx_substitute_variables_helper(c, substitute, passdata, sizeof(passdata)-1); |
| 2973 |
} |
| 2974 |
ast_debug(1, "Launching '%s'\n", app_name(app)); |
| 2975 |
if (VERBOSITY_ATLEAST(3)) { |
| 2976 |
ast_verb(3, "Executing [%s@%s:%d] " COLORIZE_FMT "(\"" COLORIZE_FMT "\", \"" COLORIZE_FMT "\") %s\n", |
| 2977 |
exten, context, priority, |
| 2978 |
COLORIZE(COLOR_BRCYAN, 0, app_name(app)), |
| 2979 |
COLORIZE(COLOR_BRMAGENTA, 0, ast_channel_name(c)), |
| 2980 |
COLORIZE(COLOR_BRMAGENTA, 0, passdata), |
| 2981 |
"in new stack"); |
| 2982 |
} |
| 2983 |
return pbx_exec(c, app, passdata); |
| 2984 |
} |
| 2985 |
} else if (q.swo) { |
| 2986 |
if (found) |
| 2987 |
*found = 1; |
| 2988 |
ast_unlock_contexts(); |
| 2989 |
if (matching_action) { |
| 2990 |
return -1; |
| 2991 |
} else { |
| 2992 |
if (!q.swo->exec) { |
| 2993 |
ast_log(LOG_WARNING, "No execution engine for switch %s\n", q.swo->name); |
| 2994 |
return -1; |
| 2995 |
} |
| 2996 |
return q.swo->exec(c, q.foundcontext ? q.foundcontext : context, exten, priority, callerid, q.data); |
| 2997 |
} |
| 2998 |
} else { |
| 2999 |
ast_unlock_contexts(); |
| 3000 |
|
| 3001 |
switch (q.status) { |
| 3002 |
case STATUS_NO_CONTEXT: |
| 3003 |
if (!matching_action && !combined_find_spawn) |
| 3004 |
ast_log(LOG_NOTICE, "Cannot find extension context '%s'\n", S_OR(context, "")); |
| 3005 |
break; |
| 3006 |
case STATUS_NO_EXTENSION: |
| 3007 |
if (!matching_action && !combined_find_spawn) |
| 3008 |
ast_log(LOG_NOTICE, "Cannot find extension '%s' in context '%s'\n", exten, S_OR(context, "")); |
| 3009 |
break; |
| 3010 |
case STATUS_NO_PRIORITY: |
| 3011 |
if (!matching_action && !combined_find_spawn) |
| 3012 |
ast_log(LOG_NOTICE, "No such priority %d in extension '%s' in context '%s'\n", priority, exten, S_OR(context, "")); |
| 3013 |
break; |
| 3014 |
case STATUS_NO_LABEL: |
| 3015 |
if (context && !combined_find_spawn) |
| 3016 |
ast_log(LOG_NOTICE, "No such label '%s' in extension '%s' in context '%s'\n", S_OR(label, ""), exten, S_OR(context, "")); |
| 3017 |
break; |
| 3018 |
default: |
| 3019 |
ast_debug(1, "Shouldn't happen!\n"); |
| 3020 |
} |
| 3021 |
|
| 3022 |
return (matching_action) ? 0 : -1; |
| 3023 |
} |
| 3024 |
} |
| 3025 |
|
| 3026 |
|
| 3027 |
static struct ast_exten *ast_hint_extension_nolock(struct ast_channel *c, const char *context, const char *exten) |
| 3028 |
{ |
| 3029 |
struct pbx_find_info q = { .stacklen = 0 }; |
| 3030 |
return pbx_find_extension(c, NULL, &q, context, exten, PRIORITY_HINT, NULL, "", E_MATCH); |
| 3031 |
} |
| 3032 |
|
| 3033 |
static struct ast_exten *ast_hint_extension(struct ast_channel *c, const char *context, const char *exten) |
| 3034 |
{ |
| 3035 |
struct ast_exten *e; |
| 3036 |
ast_rdlock_contexts(); |
| 3037 |
e = ast_hint_extension_nolock(c, context, exten); |
| 3038 |
ast_unlock_contexts(); |
| 3039 |
return e; |
| 3040 |
} |
| 3041 |
|
| 3042 |
enum ast_extension_states ast_devstate_to_extenstate(enum ast_device_state devstate) |
| 3043 |
{ |
| 3044 |
switch (devstate) { |
| 3045 |
case AST_DEVICE_ONHOLD: |
| 3046 |
return AST_EXTENSION_ONHOLD; |
| 3047 |
case AST_DEVICE_BUSY: |
| 3048 |
return AST_EXTENSION_BUSY; |
| 3049 |
case AST_DEVICE_UNKNOWN: |
| 3050 |
return AST_EXTENSION_NOT_INUSE; |
| 3051 |
case AST_DEVICE_UNAVAILABLE: |
| 3052 |
case AST_DEVICE_INVALID: |
| 3053 |
return AST_EXTENSION_UNAVAILABLE; |
| 3054 |
case AST_DEVICE_RINGINUSE: |
| 3055 |
return (AST_EXTENSION_INUSE | AST_EXTENSION_RINGING); |
| 3056 |
case AST_DEVICE_RINGING: |
| 3057 |
return AST_EXTENSION_RINGING; |
| 3058 |
case AST_DEVICE_INUSE: |
| 3059 |
return AST_EXTENSION_INUSE; |
| 3060 |
case AST_DEVICE_NOT_INUSE: |
| 3061 |
return AST_EXTENSION_NOT_INUSE; |
| 3062 |
case AST_DEVICE_TOTAL: |
| 3063 |
break; |
| 3064 |
} |
| 3065 |
|
| 3066 |
return AST_EXTENSION_NOT_INUSE; |
| 3067 |
} |
| 3068 |
|
| 3069 |
|
| 3070 |
|
| 3071 |
|
| 3072 |
|
| 3073 |
static char *parse_hint_presence(struct ast_str *hint_args) |
| 3074 |
{ |
| 3075 |
char *copy = ast_strdupa(ast_str_buffer(hint_args)); |
| 3076 |
char *tmp = ""; |
| 3077 |
|
| 3078 |
if ((tmp = strrchr(copy, ','))) { |
| 3079 |
*tmp = '\0'; |
| 3080 |
tmp++; |
| 3081 |
} else { |
| 3082 |
return NULL; |
| 3083 |
} |
| 3084 |
ast_str_set(&hint_args, 0, "%s", tmp); |
| 3085 |
return ast_str_buffer(hint_args); |
| 3086 |
} |
| 3087 |
|
| 3088 |
|
| 3089 |
|
| 3090 |
|
| 3091 |
|
| 3092 |
static char *parse_hint_device(struct ast_str *hint_args) |
| 3093 |
{ |
| 3094 |
char *copy = ast_strdupa(ast_str_buffer(hint_args)); |
| 3095 |
char *tmp; |
| 3096 |
|
| 3097 |
if ((tmp = strrchr(copy, ','))) { |
| 3098 |
*tmp = '\0'; |
| 3099 |
} |
| 3100 |
|
| 3101 |
ast_str_set(&hint_args, 0, "%s", copy); |
| 3102 |
return ast_str_buffer(hint_args); |
| 3103 |
} |
| 3104 |
|
| 3105 |
static void device_state_info_dt(void *obj) |
| 3106 |
{ |
| 3107 |
struct ast_device_state_info *info = obj; |
| 3108 |
|
| 3109 |
ao2_cleanup(info->causing_channel); |
| 3110 |
} |
| 3111 |
|
| 3112 |
static struct ao2_container *alloc_device_state_info(void) |
| 3113 |
{ |
| 3114 |
return ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL); |
| 3115 |
} |
| 3116 |
|
| 3117 |
static int ast_extension_state3(struct ast_str *hint_app, struct ao2_container *device_state_info) |
| 3118 |
{ |
| 3119 |
char *cur; |
| 3120 |
char *rest; |
| 3121 |
struct ast_devstate_aggregate agg; |
| 3122 |
|
| 3123 |
|
| 3124 |
rest = parse_hint_device(hint_app); |
| 3125 |
|
| 3126 |
ast_devstate_aggregate_init(&agg); |
| 3127 |
while ((cur = strsep(&rest, "&"))) { |
| 3128 |
enum ast_device_state state = ast_device_state(cur); |
| 3129 |
|
| 3130 |
ast_devstate_aggregate_add(&agg, state); |
| 3131 |
if (device_state_info) { |
| 3132 |
struct ast_device_state_info *obj; |
| 3133 |
|
| 3134 |
obj = ao2_alloc_options(sizeof(*obj) + strlen(cur), device_state_info_dt, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 3135 |
|
| 3136 |
if (obj) { |
| 3137 |
obj->device_state = state; |
| 3138 |
strcpy(obj->device_name, cur); |
| 3139 |
ao2_link(device_state_info, obj); |
| 3140 |
ao2_ref(obj, -1); |
| 3141 |
} |
| 3142 |
} |
| 3143 |
} |
| 3144 |
|
| 3145 |
return ast_devstate_to_extenstate(ast_devstate_aggregate_result(&agg)); |
| 3146 |
} |
| 3147 |
|
| 3148 |
|
| 3149 |
static int ast_extension_state2(struct ast_exten *e, struct ao2_container *device_state_info) |
| 3150 |
{ |
| 3151 |
struct ast_str *hint_app = ast_str_thread_get(&extensionstate_buf, 32); |
| 3152 |
|
| 3153 |
if (!e || !hint_app) { |
| 3154 |
return -1; |
| 3155 |
} |
| 3156 |
|
| 3157 |
ast_str_set(&hint_app, 0, "%s", ast_get_extension_app(e)); |
| 3158 |
return ast_extension_state3(hint_app, device_state_info); |
| 3159 |
} |
| 3160 |
|
| 3161 |
|
| 3162 |
const char *ast_extension_state2str(int extension_state) |
| 3163 |
{ |
| 3164 |
int i; |
| 3165 |
|
| 3166 |
for (i = 0; (i < ARRAY_LEN(extension_states)); i++) { |
| 3167 |
if (extension_states[i].extension_state == extension_state) |
| 3168 |
return extension_states[i].text; |
| 3169 |
} |
| 3170 |
return "Unknown"; |
| 3171 |
} |
| 3172 |
|
| 3173 |
|
| 3174 |
|
| 3175 |
|
| 3176 |
|
| 3177 |
static int internal_extension_state_extended(struct ast_channel *c, const char *context, const char *exten, |
| 3178 |
struct ao2_container *device_state_info) |
| 3179 |
{ |
| 3180 |
struct ast_exten *e; |
| 3181 |
|
| 3182 |
if (!(e = ast_hint_extension(c, context, exten))) { |
| 3183 |
return -1; |
| 3184 |
} |
| 3185 |
|
| 3186 |
if (e->exten[0] == '_') { |
| 3187 |
|
| 3188 |
|
| 3189 |
|
| 3190 |
|
| 3191 |
ao2_lock(hints); |
| 3192 |
ast_add_extension(e->parent->name, 0, exten, e->priority, e->label, |
| 3193 |
e->matchcid ? e->cidmatch : NULL, e->app, ast_strdup(e->data), ast_free_ptr, |
| 3194 |
e->registrar); |
| 3195 |
ao2_unlock(hints); |
| 3196 |
if (!(e = ast_hint_extension(c, context, exten))) { |
| 3197 |
|
| 3198 |
return -1; |
| 3199 |
} |
| 3200 |
} |
| 3201 |
|
| 3202 |
return ast_extension_state2(e, device_state_info); |
| 3203 |
} |
| 3204 |
|
| 3205 |
|
| 3206 |
int ast_extension_state(struct ast_channel *c, const char *context, const char *exten) |
| 3207 |
{ |
| 3208 |
return internal_extension_state_extended(c, context, exten, NULL); |
| 3209 |
} |
| 3210 |
|
| 3211 |
|
| 3212 |
int ast_extension_state_extended(struct ast_channel *c, const char *context, const char *exten, |
| 3213 |
struct ao2_container **device_state_info) |
| 3214 |
{ |
| 3215 |
struct ao2_container *container = NULL; |
| 3216 |
int ret; |
| 3217 |
|
| 3218 |
if (device_state_info) { |
| 3219 |
container = alloc_device_state_info(); |
| 3220 |
} |
| 3221 |
|
| 3222 |
ret = internal_extension_state_extended(c, context, exten, container); |
| 3223 |
if (ret < 0 && container) { |
| 3224 |
ao2_ref(container, -1); |
| 3225 |
container = NULL; |
| 3226 |
} |
| 3227 |
|
| 3228 |
if (device_state_info) { |
| 3229 |
get_device_state_causing_channels(container); |
| 3230 |
*device_state_info = container; |
| 3231 |
} |
| 3232 |
|
| 3233 |
return ret; |
| 3234 |
} |
| 3235 |
|
| 3236 |
static int extension_presence_state_helper(struct ast_exten *e, char **subtype, char **message) |
| 3237 |
{ |
| 3238 |
struct ast_str *hint_app = ast_str_thread_get(&extensionstate_buf, 32); |
| 3239 |
char *presence_provider; |
| 3240 |
const char *app; |
| 3241 |
|
| 3242 |
if (!e || !hint_app) { |
| 3243 |
return -1; |
| 3244 |
} |
| 3245 |
|
| 3246 |
app = ast_get_extension_app(e); |
| 3247 |
if (ast_strlen_zero(app)) { |
| 3248 |
return -1; |
| 3249 |
} |
| 3250 |
|
| 3251 |
ast_str_set(&hint_app, 0, "%s", app); |
| 3252 |
presence_provider = parse_hint_presence(hint_app); |
| 3253 |
|
| 3254 |
if (ast_strlen_zero(presence_provider)) { |
| 3255 |
|
| 3256 |
return 0; |
| 3257 |
} |
| 3258 |
|
| 3259 |
return ast_presence_state(presence_provider, subtype, message); |
| 3260 |
} |
| 3261 |
|
| 3262 |
int ast_hint_presence_state(struct ast_channel *c, const char *context, const char *exten, char **subtype, char **message) |
| 3263 |
{ |
| 3264 |
struct ast_exten *e; |
| 3265 |
|
| 3266 |
if (!(e = ast_hint_extension(c, context, exten))) { |
| 3267 |
return -1; |
| 3268 |
} |
| 3269 |
|
| 3270 |
if (e->exten[0] == '_') { |
| 3271 |
|
| 3272 |
ao2_lock(hints); |
| 3273 |
ast_add_extension(e->parent->name, 0, exten, e->priority, e->label, |
| 3274 |
e->matchcid ? e->cidmatch : NULL, e->app, ast_strdup(e->data), ast_free_ptr, |
| 3275 |
e->registrar); |
| 3276 |
ao2_unlock(hints); |
| 3277 |
if (!(e = ast_hint_extension(c, context, exten))) { |
| 3278 |
|
| 3279 |
return -1; |
| 3280 |
} |
| 3281 |
} |
| 3282 |
|
| 3283 |
return extension_presence_state_helper(e, subtype, message); |
| 3284 |
} |
| 3285 |
|
| 3286 |
static int execute_state_callback(ast_state_cb_type cb, |
| 3287 |
const char *context, |
| 3288 |
const char *exten, |
| 3289 |
void *data, |
| 3290 |
enum ast_state_cb_update_reason reason, |
| 3291 |
struct ast_hint *hint, |
| 3292 |
struct ao2_container *device_state_info) |
| 3293 |
{ |
| 3294 |
int res = 0; |
| 3295 |
struct ast_state_cb_info info = { 0, }; |
| 3296 |
|
| 3297 |
info.reason = reason; |
| 3298 |
|
| 3299 |
|
| 3300 |
if (hint) { |
| 3301 |
ao2_lock(hint); |
| 3302 |
info.exten_state = hint->laststate; |
| 3303 |
info.device_state_info = device_state_info; |
| 3304 |
info.presence_state = hint->last_presence_state; |
| 3305 |
if (!(ast_strlen_zero(hint->last_presence_subtype))) { |
| 3306 |
info.presence_subtype = ast_strdupa(hint->last_presence_subtype); |
| 3307 |
} else { |
| 3308 |
info.presence_subtype = ""; |
| 3309 |
} |
| 3310 |
if (!(ast_strlen_zero(hint->last_presence_message))) { |
| 3311 |
info.presence_message = ast_strdupa(hint->last_presence_message); |
| 3312 |
} else { |
| 3313 |
info.presence_message = ""; |
| 3314 |
} |
| 3315 |
ao2_unlock(hint); |
| 3316 |
} else { |
| 3317 |
info.exten_state = AST_EXTENSION_REMOVED; |
| 3318 |
} |
| 3319 |
|
| 3320 |
res = cb(context, exten, &info, data); |
| 3321 |
|
| 3322 |
return res; |
| 3323 |
} |
| 3324 |
|
| 3325 |
|
| 3326 |
|
| 3327 |
|
| 3328 |
|
| 3329 |
|
| 3330 |
|
| 3331 |
|
| 3332 |
static void get_device_state_causing_channels(struct ao2_container *c) |
| 3333 |
{ |
| 3334 |
struct ao2_iterator iter; |
| 3335 |
struct ast_device_state_info *info; |
| 3336 |
struct ast_channel *chan; |
| 3337 |
|
| 3338 |
if (!c || !ao2_container_count(c)) { |
| 3339 |
return; |
| 3340 |
} |
| 3341 |
iter = ao2_iterator_init(c, 0); |
| 3342 |
for (; (info = ao2_iterator_next(&iter)); ao2_ref(info, -1)) { |
| 3343 |
enum ast_channel_state search_state = 0; |
| 3344 |
char match[AST_CHANNEL_NAME]; |
| 3345 |
struct ast_channel_iterator *chan_iter; |
| 3346 |
struct timeval chantime = {0, }; |
| 3347 |
|
| 3348 |
switch (info->device_state) { |
| 3349 |
case AST_DEVICE_RINGING: |
| 3350 |
case AST_DEVICE_RINGINUSE: |
| 3351 |
|
| 3352 |
search_state = AST_STATE_RINGING; |
| 3353 |
break; |
| 3354 |
case AST_DEVICE_BUSY: |
| 3355 |
|
| 3356 |
search_state = AST_STATE_BUSY; |
| 3357 |
break; |
| 3358 |
case AST_DEVICE_ONHOLD: |
| 3359 |
case AST_DEVICE_INUSE: |
| 3360 |
|
| 3361 |
search_state = AST_STATE_UP; |
| 3362 |
break; |
| 3363 |
case AST_DEVICE_UNKNOWN: |
| 3364 |
case AST_DEVICE_NOT_INUSE: |
| 3365 |
case AST_DEVICE_INVALID: |
| 3366 |
case AST_DEVICE_UNAVAILABLE: |
| 3367 |
case AST_DEVICE_TOTAL : |
| 3368 |
|
| 3369 |
continue; |
| 3370 |
} |
| 3371 |
|
| 3372 |
|
| 3373 |
snprintf(match, sizeof(match), "%s-", info->device_name); |
| 3374 |
chan_iter = ast_channel_iterator_by_name_new(match, strlen(match)); |
| 3375 |
for (; (chan = ast_channel_iterator_next(chan_iter)); ast_channel_unref(chan)) { |
| 3376 |
ast_channel_lock(chan); |
| 3377 |
|
| 3378 |
if (search_state != ast_channel_state(chan)) { |
| 3379 |
ast_channel_unlock(chan); |
| 3380 |
continue; |
| 3381 |
} |
| 3382 |
|
| 3383 |
if (search_state != AST_STATE_RINGING) { |
| 3384 |
ast_channel_unlock(chan); |
| 3385 |
info->causing_channel = chan; |
| 3386 |
break; |
| 3387 |
} |
| 3388 |
|
| 3389 |
if (!info->causing_channel) { |
| 3390 |
chantime = ast_channel_creationtime(chan); |
| 3391 |
ast_channel_ref(chan); |
| 3392 |
info->causing_channel = chan; |
| 3393 |
} else if (ast_tvcmp(ast_channel_creationtime(chan), chantime) < 0) { |
| 3394 |
chantime = ast_channel_creationtime(chan); |
| 3395 |
ast_channel_unref(info->causing_channel); |
| 3396 |
ast_channel_ref(chan); |
| 3397 |
info->causing_channel = chan; |
| 3398 |
} |
| 3399 |
ast_channel_unlock(chan); |
| 3400 |
} |
| 3401 |
ast_channel_iterator_destroy(chan_iter); |
| 3402 |
} |
| 3403 |
ao2_iterator_destroy(&iter); |
| 3404 |
} |
| 3405 |
|
| 3406 |
static void device_state_notify_callbacks(struct ast_hint *hint, struct ast_str **hint_app) |
| 3407 |
{ |
| 3408 |
struct ao2_iterator cb_iter; |
| 3409 |
struct ast_state_cb *state_cb; |
| 3410 |
int state; |
| 3411 |
int same_state; |
| 3412 |
struct ao2_container *device_state_info; |
| 3413 |
int first_extended_cb_call = 1; |
| 3414 |
char context_name[AST_MAX_CONTEXT]; |
| 3415 |
char exten_name[AST_MAX_EXTENSION]; |
| 3416 |
|
| 3417 |
ao2_lock(hint); |
| 3418 |
if (!hint->exten) { |
| 3419 |
|
| 3420 |
ao2_unlock(hint); |
| 3421 |
return; |
| 3422 |
} |
| 3423 |
|
| 3424 |
|
| 3425 |
|
| 3426 |
|
| 3427 |
|
| 3428 |
ast_copy_string(context_name, |
| 3429 |
ast_get_context_name(ast_get_extension_context(hint->exten)), |
| 3430 |
sizeof(context_name)); |
| 3431 |
ast_copy_string(exten_name, ast_get_extension_name(hint->exten), |
| 3432 |
sizeof(exten_name)); |
| 3433 |
ast_str_set(hint_app, 0, "%s", ast_get_extension_app(hint->exten)); |
| 3434 |
ao2_unlock(hint); |
| 3435 |
|
| 3436 |
|
| 3437 |
|
| 3438 |
|
| 3439 |
|
| 3440 |
|
| 3441 |
|
| 3442 |
|
| 3443 |
|
| 3444 |
|
| 3445 |
|
| 3446 |
|
| 3447 |
device_state_info = alloc_device_state_info(); |
| 3448 |
|
| 3449 |
state = ast_extension_state3(*hint_app, device_state_info); |
| 3450 |
same_state = state == hint->laststate; |
| 3451 |
if (same_state && (~state & AST_EXTENSION_RINGING)) { |
| 3452 |
ao2_cleanup(device_state_info); |
| 3453 |
return; |
| 3454 |
} |
| 3455 |
|
| 3456 |
|
| 3457 |
hint->laststate = state; |
| 3458 |
|
| 3459 |
|
| 3460 |
if (!same_state) { |
| 3461 |
cb_iter = ao2_iterator_init(statecbs, 0); |
| 3462 |
for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) { |
| 3463 |
execute_state_callback(state_cb->change_cb, |
| 3464 |
context_name, |
| 3465 |
exten_name, |
| 3466 |
state_cb->data, |
| 3467 |
AST_HINT_UPDATE_DEVICE, |
| 3468 |
hint, |
| 3469 |
NULL); |
| 3470 |
} |
| 3471 |
ao2_iterator_destroy(&cb_iter); |
| 3472 |
} |
| 3473 |
|
| 3474 |
|
| 3475 |
|
| 3476 |
|
| 3477 |
|
| 3478 |
cb_iter = ao2_iterator_init(hint->callbacks, 0); |
| 3479 |
for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) { |
| 3480 |
if (state_cb->extended && first_extended_cb_call) { |
| 3481 |
|
| 3482 |
first_extended_cb_call = 0; |
| 3483 |
get_device_state_causing_channels(device_state_info); |
| 3484 |
} |
| 3485 |
if (state_cb->extended || !same_state) { |
| 3486 |
execute_state_callback(state_cb->change_cb, |
| 3487 |
context_name, |
| 3488 |
exten_name, |
| 3489 |
state_cb->data, |
| 3490 |
AST_HINT_UPDATE_DEVICE, |
| 3491 |
hint, |
| 3492 |
state_cb->extended ? device_state_info : NULL); |
| 3493 |
} |
| 3494 |
} |
| 3495 |
ao2_iterator_destroy(&cb_iter); |
| 3496 |
|
| 3497 |
ao2_cleanup(device_state_info); |
| 3498 |
} |
| 3499 |
|
| 3500 |
static void presence_state_notify_callbacks(struct ast_hint *hint, struct ast_str **hint_app, |
| 3501 |
struct ast_presence_state_message *presence_state) |
| 3502 |
{ |
| 3503 |
struct ao2_iterator cb_iter; |
| 3504 |
struct ast_state_cb *state_cb; |
| 3505 |
char context_name[AST_MAX_CONTEXT]; |
| 3506 |
char exten_name[AST_MAX_EXTENSION]; |
| 3507 |
|
| 3508 |
ao2_lock(hint); |
| 3509 |
if (!hint->exten) { |
| 3510 |
|
| 3511 |
ao2_unlock(hint); |
| 3512 |
return; |
| 3513 |
} |
| 3514 |
|
| 3515 |
|
| 3516 |
|
| 3517 |
|
| 3518 |
|
| 3519 |
ast_copy_string(context_name, |
| 3520 |
ast_get_context_name(ast_get_extension_context(hint->exten)), |
| 3521 |
sizeof(context_name)); |
| 3522 |
ast_copy_string(exten_name, ast_get_extension_name(hint->exten), |
| 3523 |
sizeof(exten_name)); |
| 3524 |
ast_str_set(hint_app, 0, "%s", ast_get_extension_app(hint->exten)); |
| 3525 |
ao2_unlock(hint); |
| 3526 |
|
| 3527 |
|
| 3528 |
if ((hint->last_presence_state == presence_state->state) && |
| 3529 |
((hint->last_presence_subtype && presence_state->subtype && |
| 3530 |
!strcmp(hint->last_presence_subtype, presence_state->subtype)) || |
| 3531 |
(!hint->last_presence_subtype && !presence_state->subtype)) && |
| 3532 |
((hint->last_presence_message && presence_state->message && |
| 3533 |
!strcmp(hint->last_presence_message, presence_state->message)) || |
| 3534 |
(!hint->last_presence_message && !presence_state->message))) { |
| 3535 |
|
| 3536 |
return; |
| 3537 |
} |
| 3538 |
|
| 3539 |
|
| 3540 |
ast_free(hint->last_presence_subtype); |
| 3541 |
ast_free(hint->last_presence_message); |
| 3542 |
hint->last_presence_state = presence_state->state; |
| 3543 |
hint->last_presence_subtype = presence_state->subtype ? ast_strdup(presence_state->subtype) : NULL; |
| 3544 |
hint->last_presence_message = presence_state->message ? ast_strdup(presence_state->message) : NULL; |
| 3545 |
|
| 3546 |
|
| 3547 |
cb_iter = ao2_iterator_init(statecbs, 0); |
| 3548 |
for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) { |
| 3549 |
execute_state_callback(state_cb->change_cb, |
| 3550 |
context_name, |
| 3551 |
exten_name, |
| 3552 |
state_cb->data, |
| 3553 |
AST_HINT_UPDATE_PRESENCE, |
| 3554 |
hint, |
| 3555 |
NULL); |
| 3556 |
} |
| 3557 |
ao2_iterator_destroy(&cb_iter); |
| 3558 |
|
| 3559 |
|
| 3560 |
cb_iter = ao2_iterator_init(hint->callbacks, 0); |
| 3561 |
for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_cleanup(state_cb)) { |
| 3562 |
execute_state_callback(state_cb->change_cb, |
| 3563 |
context_name, |
| 3564 |
exten_name, |
| 3565 |
state_cb->data, |
| 3566 |
AST_HINT_UPDATE_PRESENCE, |
| 3567 |
hint, |
| 3568 |
NULL); |
| 3569 |
} |
| 3570 |
ao2_iterator_destroy(&cb_iter); |
| 3571 |
} |
| 3572 |
|
| 3573 |
static int handle_hint_change_message_type(struct stasis_message *msg, enum ast_state_cb_update_reason reason) |
| 3574 |
{ |
| 3575 |
struct ast_hint *hint; |
| 3576 |
struct ast_str *hint_app; |
| 3577 |
|
| 3578 |
if (hint_change_message_type() != stasis_message_type(msg)) { |
| 3579 |
return 0; |
| 3580 |
} |
| 3581 |
|
| 3582 |
if (!(hint_app = ast_str_create(1024))) { |
| 3583 |
return -1; |
| 3584 |
} |
| 3585 |
|
| 3586 |
hint = stasis_message_data(msg); |
| 3587 |
|
| 3588 |
switch (reason) { |
| 3589 |
case AST_HINT_UPDATE_DEVICE: |
| 3590 |
device_state_notify_callbacks(hint, &hint_app); |
| 3591 |
break; |
| 3592 |
case AST_HINT_UPDATE_PRESENCE: |
| 3593 |
{ |
| 3594 |
char *presence_subtype = NULL; |
| 3595 |
char *presence_message = NULL; |
| 3596 |
int state; |
| 3597 |
|
| 3598 |
state = extension_presence_state_helper( |
| 3599 |
hint->exten, &presence_subtype, &presence_message); |
| 3600 |
{ |
| 3601 |
struct ast_presence_state_message presence_state = { |
| 3602 |
.state = state > 0 ? state : AST_PRESENCE_INVALID, |
| 3603 |
.subtype = presence_subtype, |
| 3604 |
.message = presence_message |
| 3605 |
}; |
| 3606 |
|
| 3607 |
presence_state_notify_callbacks(hint, &hint_app, &presence_state); |
| 3608 |
} |
| 3609 |
|
| 3610 |
ast_free(presence_subtype); |
| 3611 |
ast_free(presence_message); |
| 3612 |
} |
| 3613 |
break; |
| 3614 |
} |
| 3615 |
|
| 3616 |
ast_free(hint_app); |
| 3617 |
return 1; |
| 3618 |
} |
| 3619 |
|
| 3620 |
static void device_state_cb(void *unused, struct stasis_subscription *sub, struct stasis_message *msg) |
| 3621 |
{ |
| 3622 |
struct ast_device_state_message *dev_state; |
| 3623 |
struct ast_str *hint_app; |
| 3624 |
struct ast_hintdevice *device; |
| 3625 |
struct ast_hintdevice *cmpdevice; |
| 3626 |
struct ao2_iterator *dev_iter; |
| 3627 |
struct ao2_iterator auto_iter; |
| 3628 |
struct ast_autohint *autohint; |
| 3629 |
char *virtual_device; |
| 3630 |
char *type; |
| 3631 |
char *device_name; |
| 3632 |
|
| 3633 |
if (handle_hint_change_message_type(msg, AST_HINT_UPDATE_DEVICE)) { |
| 3634 |
return; |
| 3635 |
} |
| 3636 |
|
| 3637 |
if (hint_remove_message_type() == stasis_message_type(msg)) { |
| 3638 |
|
| 3639 |
struct ast_state_cb *state_cb; |
| 3640 |
struct ao2_iterator cb_iter; |
| 3641 |
struct ast_hint *hint = stasis_message_data(msg); |
| 3642 |
|
| 3643 |
ao2_lock(hint); |
| 3644 |
hint->laststate = AST_EXTENSION_DEACTIVATED; |
| 3645 |
ao2_unlock(hint); |
| 3646 |
|
| 3647 |
cb_iter = ao2_iterator_init(hint->callbacks, 0); |
| 3648 |
for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) { |
| 3649 |
execute_state_callback(state_cb->change_cb, |
| 3650 |
hint->context_name, |
| 3651 |
hint->exten_name, |
| 3652 |
state_cb->data, |
| 3653 |
AST_HINT_UPDATE_DEVICE, |
| 3654 |
hint, |
| 3655 |
NULL); |
| 3656 |
} |
| 3657 |
ao2_iterator_destroy(&cb_iter); |
| 3658 |
return; |
| 3659 |
} |
| 3660 |
|
| 3661 |
if (ast_device_state_message_type() != stasis_message_type(msg)) { |
| 3662 |
return; |
| 3663 |
} |
| 3664 |
|
| 3665 |
dev_state = stasis_message_data(msg); |
| 3666 |
if (dev_state->eid) { |
| 3667 |
|
| 3668 |
return; |
| 3669 |
} |
| 3670 |
|
| 3671 |
if (ao2_container_count(hintdevices) == 0 && ao2_container_count(autohints) == 0) { |
| 3672 |
|
| 3673 |
return; |
| 3674 |
} |
| 3675 |
|
| 3676 |
hint_app = ast_str_create(1024); |
| 3677 |
if (!hint_app) { |
| 3678 |
return; |
| 3679 |
} |
| 3680 |
|
| 3681 |
cmpdevice = ast_alloca(sizeof(*cmpdevice) + strlen(dev_state->device)); |
| 3682 |
strcpy(cmpdevice->hintdevice, dev_state->device); |
| 3683 |
|
| 3684 |
ast_mutex_lock(&context_merge_lock); |
| 3685 |
|
| 3686 |
|
| 3687 |
dev_iter = ao2_t_callback(hintdevices, |
| 3688 |
OBJ_SEARCH_OBJECT | OBJ_MULTIPLE, |
| 3689 |
hintdevice_cmp_multiple, |
| 3690 |
cmpdevice, |
| 3691 |
"find devices in container"); |
| 3692 |
if (dev_iter) { |
| 3693 |
for (; (device = ao2_iterator_next(dev_iter)); ao2_t_ref(device, -1, "Next device")) { |
| 3694 |
if (device->hint) { |
| 3695 |
device_state_notify_callbacks(device->hint, &hint_app); |
| 3696 |
} |
| 3697 |
} |
| 3698 |
ao2_iterator_destroy(dev_iter); |
| 3699 |
} |
| 3700 |
|
| 3701 |
|
| 3702 |
|
| 3703 |
|
| 3704 |
type = ast_strdupa(dev_state->device); |
| 3705 |
if (ast_strlen_zero(type)) { |
| 3706 |
goto end; |
| 3707 |
} |
| 3708 |
|
| 3709 |
|
| 3710 |
virtual_device = strchr(type, ':'); |
| 3711 |
device_name = strchr(type, '/'); |
| 3712 |
if (virtual_device && (!device_name || (virtual_device < device_name))) { |
| 3713 |
device_name = virtual_device; |
| 3714 |
} |
| 3715 |
|
| 3716 |
|
| 3717 |
if (ast_strlen_zero(device_name)) { |
| 3718 |
goto end; |
| 3719 |
} |
| 3720 |
|
| 3721 |
*device_name++ = '\0'; |
| 3722 |
|
| 3723 |
auto_iter = ao2_iterator_init(autohints, 0); |
| 3724 |
for (; (autohint = ao2_iterator_next(&auto_iter)); ao2_t_ref(autohint, -1, "Next autohint")) { |
| 3725 |
if (ast_get_hint(NULL, 0, NULL, 0, NULL, autohint->context, device_name)) { |
| 3726 |
continue; |
| 3727 |
} |
| 3728 |
|
| 3729 |
|
| 3730 |
ast_add_extension(autohint->context, 0, device_name, |
| 3731 |
PRIORITY_HINT, NULL, NULL, dev_state->device, |
| 3732 |
ast_strdup(dev_state->device), ast_free_ptr, autohint->registrar); |
| 3733 |
|
| 3734 |
|
| 3735 |
} |
| 3736 |
ao2_iterator_destroy(&auto_iter); |
| 3737 |
|
| 3738 |
end: |
| 3739 |
ast_mutex_unlock(&context_merge_lock); |
| 3740 |
ast_free(hint_app); |
| 3741 |
return; |
| 3742 |
} |
| 3743 |
|
| 3744 |
|
| 3745 |
|
| 3746 |
|
| 3747 |
|
| 3748 |
|
| 3749 |
|
| 3750 |
static void destroy_state_cb(void *doomed) |
| 3751 |
{ |
| 3752 |
struct ast_state_cb *state_cb = doomed; |
| 3753 |
|
| 3754 |
if (state_cb->destroy_cb) { |
| 3755 |
state_cb->destroy_cb(state_cb->id, state_cb->data); |
| 3756 |
} |
| 3757 |
} |
| 3758 |
|
| 3759 |
|
| 3760 |
|
| 3761 |
|
| 3762 |
|
| 3763 |
static int extension_state_add_destroy(const char *context, const char *exten, |
| 3764 |
ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data, int extended) |
| 3765 |
{ |
| 3766 |
struct ast_hint *hint; |
| 3767 |
struct ast_state_cb *state_cb; |
| 3768 |
struct ast_exten *e; |
| 3769 |
int id; |
| 3770 |
|
| 3771 |
|
| 3772 |
if (!context && !exten) { |
| 3773 |
|
| 3774 |
ao2_lock(statecbs); |
| 3775 |
|
| 3776 |
|
| 3777 |
ao2_find(statecbs, change_cb, OBJ_UNLINK | OBJ_NODATA); |
| 3778 |
|
| 3779 |
|
| 3780 |
if (!(state_cb = ao2_alloc(sizeof(*state_cb), destroy_state_cb))) { |
| 3781 |
ao2_unlock(statecbs); |
| 3782 |
return -1; |
| 3783 |
} |
| 3784 |
state_cb->id = 0; |
| 3785 |
state_cb->change_cb = change_cb; |
| 3786 |
state_cb->destroy_cb = destroy_cb; |
| 3787 |
state_cb->data = data; |
| 3788 |
state_cb->extended = extended; |
| 3789 |
ao2_link(statecbs, state_cb); |
| 3790 |
|
| 3791 |
ao2_ref(state_cb, -1); |
| 3792 |
ao2_unlock(statecbs); |
| 3793 |
return 0; |
| 3794 |
} |
| 3795 |
|
| 3796 |
if (!context || !exten) |
| 3797 |
return -1; |
| 3798 |
|
| 3799 |
|
| 3800 |
e = ast_hint_extension(NULL, context, exten); |
| 3801 |
if (!e) { |
| 3802 |
return -1; |
| 3803 |
} |
| 3804 |
|
| 3805 |
|
| 3806 |
|
| 3807 |
|
| 3808 |
|
| 3809 |
if (e->exten[0] == '_') { |
| 3810 |
ao2_lock(hints); |
| 3811 |
ast_add_extension(e->parent->name, 0, exten, e->priority, e->label, |
| 3812 |
e->matchcid ? e->cidmatch : NULL, e->app, ast_strdup(e->data), ast_free_ptr, |
| 3813 |
e->registrar); |
| 3814 |
ao2_unlock(hints); |
| 3815 |
e = ast_hint_extension(NULL, context, exten); |
| 3816 |
if (!e || e->exten[0] == '_') { |
| 3817 |
return -1; |
| 3818 |
} |
| 3819 |
} |
| 3820 |
|
| 3821 |
|
| 3822 |
ao2_lock(hints); |
| 3823 |
hint = ao2_find(hints, e, 0); |
| 3824 |
if (!hint) { |
| 3825 |
ao2_unlock(hints); |
| 3826 |
return -1; |
| 3827 |
} |
| 3828 |
|
| 3829 |
|
| 3830 |
if (!(state_cb = ao2_alloc(sizeof(*state_cb), destroy_state_cb))) { |
| 3831 |
ao2_ref(hint, -1); |
| 3832 |
ao2_unlock(hints); |
| 3833 |
return -1; |
| 3834 |
} |
| 3835 |
do { |
| 3836 |
id = stateid++; |
| 3837 |
|
| 3838 |
} while (id == -1 || id == 0); |
| 3839 |
state_cb->id = id; |
| 3840 |
state_cb->change_cb = change_cb; |
| 3841 |
state_cb->destroy_cb = destroy_cb; |
| 3842 |
state_cb->data = data; |
| 3843 |
state_cb->extended = extended; |
| 3844 |
ao2_link(hint->callbacks, state_cb); |
| 3845 |
|
| 3846 |
ao2_ref(state_cb, -1); |
| 3847 |
ao2_ref(hint, -1); |
| 3848 |
ao2_unlock(hints); |
| 3849 |
|
| 3850 |
return id; |
| 3851 |
} |
| 3852 |
|
| 3853 |
int ast_extension_state_add_destroy(const char *context, const char *exten, |
| 3854 |
ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data) |
| 3855 |
{ |
| 3856 |
return extension_state_add_destroy(context, exten, change_cb, destroy_cb, data, 0); |
| 3857 |
} |
| 3858 |
|
| 3859 |
int ast_extension_state_add(const char *context, const char *exten, |
| 3860 |
ast_state_cb_type change_cb, void *data) |
| 3861 |
{ |
| 3862 |
return extension_state_add_destroy(context, exten, change_cb, NULL, data, 0); |
| 3863 |
} |
| 3864 |
|
| 3865 |
int ast_extension_state_add_destroy_extended(const char *context, const char *exten, |
| 3866 |
ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data) |
| 3867 |
{ |
| 3868 |
return extension_state_add_destroy(context, exten, change_cb, destroy_cb, data, 1); |
| 3869 |
} |
| 3870 |
|
| 3871 |
int ast_extension_state_add_extended(const char *context, const char *exten, |
| 3872 |
ast_state_cb_type change_cb, void *data) |
| 3873 |
{ |
| 3874 |
return extension_state_add_destroy(context, exten, change_cb, NULL, data, 1); |
| 3875 |
} |
| 3876 |
|
| 3877 |
|
| 3878 |
static int find_hint_by_cb_id(void *obj, void *arg, int flags) |
| 3879 |
{ |
| 3880 |
struct ast_state_cb *state_cb; |
| 3881 |
const struct ast_hint *hint = obj; |
| 3882 |
int *id = arg; |
| 3883 |
|
| 3884 |
if ((state_cb = ao2_find(hint->callbacks, id, 0))) { |
| 3885 |
ao2_ref(state_cb, -1); |
| 3886 |
return CMP_MATCH | CMP_STOP; |
| 3887 |
} |
| 3888 |
|
| 3889 |
return 0; |
| 3890 |
} |
| 3891 |
|
| 3892 |
int ast_extension_state_del(int id, ast_state_cb_type change_cb) |
| 3893 |
{ |
| 3894 |
struct ast_state_cb *p_cur; |
| 3895 |
int ret = -1; |
| 3896 |
|
| 3897 |
if (!id) { |
| 3898 |
if (!change_cb) { |
| 3899 |
return ret; |
| 3900 |
} |
| 3901 |
p_cur = ao2_find(statecbs, change_cb, OBJ_UNLINK); |
| 3902 |
if (p_cur) { |
| 3903 |
ret = 0; |
| 3904 |
ao2_ref(p_cur, -1); |
| 3905 |
} |
| 3906 |
} else { |
| 3907 |
struct ast_hint *hint; |
| 3908 |
|
| 3909 |
ao2_lock(hints); |
| 3910 |
hint = ao2_callback(hints, 0, find_hint_by_cb_id, &id); |
| 3911 |
if (hint) { |
| 3912 |
p_cur = ao2_find(hint->callbacks, &id, OBJ_UNLINK); |
| 3913 |
if (p_cur) { |
| 3914 |
ret = 0; |
| 3915 |
ao2_ref(p_cur, -1); |
| 3916 |
} |
| 3917 |
ao2_ref(hint, -1); |
| 3918 |
} |
| 3919 |
ao2_unlock(hints); |
| 3920 |
} |
| 3921 |
|
| 3922 |
return ret; |
| 3923 |
} |
| 3924 |
|
| 3925 |
static int hint_id_cmp(void *obj, void *arg, int flags) |
| 3926 |
{ |
| 3927 |
const struct ast_state_cb *cb = obj; |
| 3928 |
int *id = arg; |
| 3929 |
|
| 3930 |
return (cb->id == *id) ? CMP_MATCH | CMP_STOP : 0; |
| 3931 |
} |
| 3932 |
|
| 3933 |
|
| 3934 |
|
| 3935 |
|
| 3936 |
|
| 3937 |
|
| 3938 |
|
| 3939 |
static void destroy_hint(void *obj) |
| 3940 |
{ |
| 3941 |
struct ast_hint *hint = obj; |
| 3942 |
int i; |
| 3943 |
|
| 3944 |
ao2_cleanup(hint->callbacks); |
| 3945 |
|
| 3946 |
for (i = 0; i < AST_VECTOR_SIZE(&hint->devices); i++) { |
| 3947 |
char *device = AST_VECTOR_GET(&hint->devices, i); |
| 3948 |
ast_free(device); |
| 3949 |
} |
| 3950 |
AST_VECTOR_FREE(&hint->devices); |
| 3951 |
ast_free(hint->last_presence_subtype); |
| 3952 |
ast_free(hint->last_presence_message); |
| 3953 |
} |
| 3954 |
|
| 3955 |
|
| 3956 |
static int publish_hint_remove(struct ast_hint *hint) |
| 3957 |
{ |
| 3958 |
struct stasis_message *message; |
| 3959 |
|
| 3960 |
if (!hint_remove_message_type()) { |
| 3961 |
return -1; |
| 3962 |
} |
| 3963 |
|
| 3964 |
if (!(message = stasis_message_create(hint_remove_message_type(), hint))) { |
| 3965 |
ao2_ref(hint, -1); |
| 3966 |
return -1; |
| 3967 |
} |
| 3968 |
|
| 3969 |
stasis_publish(ast_device_state_topic_all(), message); |
| 3970 |
|
| 3971 |
ao2_ref(message, -1); |
| 3972 |
|
| 3973 |
return 0; |
| 3974 |
} |
| 3975 |
|
| 3976 |
|
| 3977 |
static int ast_remove_hint(struct ast_exten *e) |
| 3978 |
{ |
| 3979 |
|
| 3980 |
struct ast_hint *hint; |
| 3981 |
|
| 3982 |
if (!e) { |
| 3983 |
return -1; |
| 3984 |
} |
| 3985 |
|
| 3986 |
hint = ao2_find(hints, e, OBJ_UNLINK); |
| 3987 |
if (!hint) { |
| 3988 |
return -1; |
| 3989 |
} |
| 3990 |
|
| 3991 |
remove_hintdevice(hint); |
| 3992 |
|
| 3993 |
|
| 3994 |
|
| 3995 |
|
| 3996 |
|
| 3997 |
ao2_lock(hint); |
| 3998 |
ast_copy_string(hint->context_name, |
| 3999 |
ast_get_context_name(ast_get_extension_context(hint->exten)), |
| 4000 |
sizeof(hint->context_name)); |
| 4001 |
ast_copy_string(hint->exten_name, ast_get_extension_name(hint->exten), |
| 4002 |
sizeof(hint->exten_name)); |
| 4003 |
hint->exten = NULL; |
| 4004 |
ao2_unlock(hint); |
| 4005 |
|
| 4006 |
publish_hint_remove(hint); |
| 4007 |
|
| 4008 |
ao2_ref(hint, -1); |
| 4009 |
|
| 4010 |
return 0; |
| 4011 |
} |
| 4012 |
|
| 4013 |
|
| 4014 |
static int ast_add_hint(struct ast_exten *e) |
| 4015 |
{ |
| 4016 |
struct ast_hint *hint_new; |
| 4017 |
struct ast_hint *hint_found; |
| 4018 |
char *message = NULL; |
| 4019 |
char *subtype = NULL; |
| 4020 |
int presence_state; |
| 4021 |
|
| 4022 |
if (!e) { |
| 4023 |
return -1; |
| 4024 |
} |
| 4025 |
|
| 4026 |
|
| 4027 |
|
| 4028 |
|
| 4029 |
|
| 4030 |
|
| 4031 |
hint_new = ao2_alloc(sizeof(*hint_new), destroy_hint); |
| 4032 |
if (!hint_new) { |
| 4033 |
return -1; |
| 4034 |
} |
| 4035 |
AST_VECTOR_INIT(&hint_new->devices, 8); |
| 4036 |
|
| 4037 |
|
| 4038 |
hint_new->callbacks = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, hint_id_cmp); |
| 4039 |
if (!hint_new->callbacks) { |
| 4040 |
ao2_ref(hint_new, -1); |
| 4041 |
return -1; |
| 4042 |
} |
| 4043 |
hint_new->exten = e; |
| 4044 |
if (strstr(e->app, "${") && e->exten[0] == '_') { |
| 4045 |
|
| 4046 |
hint_new->laststate = AST_DEVICE_INVALID; |
| 4047 |
hint_new->last_presence_state = AST_PRESENCE_INVALID; |
| 4048 |
} else { |
| 4049 |
hint_new->laststate = ast_extension_state2(e, NULL); |
| 4050 |
if ((presence_state = extension_presence_state_helper(e, &subtype, &message)) > 0) { |
| 4051 |
hint_new->last_presence_state = presence_state; |
| 4052 |
hint_new->last_presence_subtype = subtype; |
| 4053 |
hint_new->last_presence_message = message; |
| 4054 |
} |
| 4055 |
} |
| 4056 |
|
| 4057 |
|
| 4058 |
ao2_lock(hints); |
| 4059 |
|
| 4060 |
|
| 4061 |
hint_found = ao2_find(hints, e, 0); |
| 4062 |
if (hint_found) { |
| 4063 |
ao2_ref(hint_found, -1); |
| 4064 |
ao2_unlock(hints); |
| 4065 |
ao2_ref(hint_new, -1); |
| 4066 |
ast_debug(2, "HINTS: Not re-adding existing hint %s: %s\n", |
| 4067 |
ast_get_extension_name(e), ast_get_extension_app(e)); |
| 4068 |
return -1; |
| 4069 |
} |
| 4070 |
|
| 4071 |
|
| 4072 |
ast_debug(2, "HINTS: Adding hint %s: %s\n", |
| 4073 |
ast_get_extension_name(e), ast_get_extension_app(e)); |
| 4074 |
ao2_link(hints, hint_new); |
| 4075 |
if (add_hintdevice(hint_new, ast_get_extension_app(e))) { |
| 4076 |
ast_log(LOG_WARNING, "Could not add devices for hint: %s@%s.\n", |
| 4077 |
ast_get_extension_name(e), |
| 4078 |
ast_get_context_name(ast_get_extension_context(e))); |
| 4079 |
} |
| 4080 |
|
| 4081 |
|
| 4082 |
if (!(strstr(e->app, "${") && e->exten[0] == '_')) { |
| 4083 |
struct ast_state_cb *state_cb; |
| 4084 |
struct ao2_iterator cb_iter; |
| 4085 |
|
| 4086 |
|
| 4087 |
cb_iter = ao2_iterator_init(statecbs, 0); |
| 4088 |
for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) { |
| 4089 |
execute_state_callback(state_cb->change_cb, |
| 4090 |
ast_get_context_name(ast_get_extension_context(e)), |
| 4091 |
ast_get_extension_name(e), |
| 4092 |
state_cb->data, |
| 4093 |
AST_HINT_UPDATE_DEVICE, |
| 4094 |
hint_new, |
| 4095 |
NULL); |
| 4096 |
} |
| 4097 |
ao2_iterator_destroy(&cb_iter); |
| 4098 |
} |
| 4099 |
ao2_unlock(hints); |
| 4100 |
ao2_ref(hint_new, -1); |
| 4101 |
|
| 4102 |
return 0; |
| 4103 |
} |
| 4104 |
|
| 4105 |
|
| 4106 |
static int publish_hint_change(struct ast_hint *hint, struct ast_exten *ne) |
| 4107 |
{ |
| 4108 |
struct stasis_message *message; |
| 4109 |
|
| 4110 |
if (!hint_change_message_type()) { |
| 4111 |
return -1; |
| 4112 |
} |
| 4113 |
|
| 4114 |
if (!(message = stasis_message_create(hint_change_message_type(), hint))) { |
| 4115 |
ao2_ref(hint, -1); |
| 4116 |
return -1; |
| 4117 |
} |
| 4118 |
|
| 4119 |
stasis_publish(ast_device_state_topic_all(), message); |
| 4120 |
stasis_publish(ast_presence_state_topic_all(), message); |
| 4121 |
|
| 4122 |
ao2_ref(message, -1); |
| 4123 |
|
| 4124 |
return 0; |
| 4125 |
} |
| 4126 |
|
| 4127 |
|
| 4128 |
static int ast_change_hint(struct ast_exten *oe, struct ast_exten *ne) |
| 4129 |
{ |
| 4130 |
struct ast_hint *hint; |
| 4131 |
|
| 4132 |
if (!oe || !ne) { |
| 4133 |
return -1; |
| 4134 |
} |
| 4135 |
|
| 4136 |
ao2_lock(hints); |
| 4137 |
|
| 4138 |
|
| 4139 |
|
| 4140 |
|
| 4141 |
|
| 4142 |
hint = ao2_find(hints, oe, OBJ_UNLINK); |
| 4143 |
if (!hint) { |
| 4144 |
ao2_unlock(hints); |
| 4145 |
ast_mutex_unlock(&context_merge_lock); |
| 4146 |
return -1; |
| 4147 |
} |
| 4148 |
|
| 4149 |
remove_hintdevice(hint); |
| 4150 |
|
| 4151 |
|
| 4152 |
ao2_lock(hint); |
| 4153 |
hint->exten = ne; |
| 4154 |
|
| 4155 |
ao2_unlock(hint); |
| 4156 |
|
| 4157 |
ao2_link(hints, hint); |
| 4158 |
if (add_hintdevice(hint, ast_get_extension_app(ne))) { |
| 4159 |
ast_log(LOG_WARNING, "Could not add devices for hint: %s@%s.\n", |
| 4160 |
ast_get_extension_name(ne), |
| 4161 |
ast_get_context_name(ast_get_extension_context(ne))); |
| 4162 |
} |
| 4163 |
ao2_unlock(hints); |
| 4164 |
|
| 4165 |
publish_hint_change(hint, ne); |
| 4166 |
|
| 4167 |
ao2_ref(hint, -1); |
| 4168 |
|
| 4169 |
return 0; |
| 4170 |
} |
| 4171 |
|
| 4172 |
|
| 4173 |
int ast_get_hint(char *hint, int hintsize, char *name, int namesize, struct ast_channel *c, const char *context, const char *exten) |
| 4174 |
{ |
| 4175 |
struct ast_exten *e = ast_hint_extension(c, context, exten); |
| 4176 |
|
| 4177 |
if (e) { |
| 4178 |
if (hint) |
| 4179 |
ast_copy_string(hint, ast_get_extension_app(e), hintsize); |
| 4180 |
if (name) { |
| 4181 |
const char *tmp = ast_get_extension_app_data(e); |
| 4182 |
if (tmp) |
| 4183 |
ast_copy_string(name, tmp, namesize); |
| 4184 |
} |
| 4185 |
return -1; |
| 4186 |
} |
| 4187 |
return 0; |
| 4188 |
} |
| 4189 |
|
| 4190 |
|
| 4191 |
int ast_str_get_hint(struct ast_str **hint, ssize_t hintsize, struct ast_str **name, ssize_t namesize, struct ast_channel *c, const char *context, const char *exten) |
| 4192 |
{ |
| 4193 |
struct ast_exten *e = ast_hint_extension(c, context, exten); |
| 4194 |
|
| 4195 |
if (!e) { |
| 4196 |
return 0; |
| 4197 |
} |
| 4198 |
|
| 4199 |
if (hint) { |
| 4200 |
ast_str_set(hint, hintsize, "%s", ast_get_extension_app(e)); |
| 4201 |
} |
| 4202 |
if (name) { |
| 4203 |
const char *tmp = ast_get_extension_app_data(e); |
| 4204 |
if (tmp) { |
| 4205 |
ast_str_set(name, namesize, "%s", tmp); |
| 4206 |
} |
| 4207 |
} |
| 4208 |
return -1; |
| 4209 |
} |
| 4210 |
|
| 4211 |
int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid) |
| 4212 |
{ |
| 4213 |
return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, E_MATCH, 0, 0); |
| 4214 |
} |
| 4215 |
|
| 4216 |
int ast_findlabel_extension(struct ast_channel *c, const char *context, const char *exten, const char *label, const char *callerid) |
| 4217 |
{ |
| 4218 |
return pbx_extension_helper(c, NULL, context, exten, 0, label, callerid, E_FINDLABEL, 0, 0); |
| 4219 |
} |
| 4220 |
|
| 4221 |
int ast_findlabel_extension2(struct ast_channel *c, struct ast_context *con, const char *exten, const char *label, const char *callerid) |
| 4222 |
{ |
| 4223 |
return pbx_extension_helper(c, con, NULL, exten, 0, label, callerid, E_FINDLABEL, 0, 0); |
| 4224 |
} |
| 4225 |
|
| 4226 |
int ast_canmatch_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid) |
| 4227 |
{ |
| 4228 |
return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, E_CANMATCH, 0, 0); |
| 4229 |
} |
| 4230 |
|
| 4231 |
int ast_matchmore_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid) |
| 4232 |
{ |
| 4233 |
return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, E_MATCHMORE, 0, 0); |
| 4234 |
} |
| 4235 |
|
| 4236 |
int ast_spawn_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid, int *found, int combined_find_spawn) |
| 4237 |
{ |
| 4238 |
return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, E_SPAWN, found, combined_find_spawn); |
| 4239 |
} |
| 4240 |
|
| 4241 |
void ast_pbx_h_exten_run(struct ast_channel *chan, const char *context) |
| 4242 |
{ |
| 4243 |
int autoloopflag; |
| 4244 |
int found; |
| 4245 |
int spawn_error; |
| 4246 |
|
| 4247 |
ast_channel_lock(chan); |
| 4248 |
|
| 4249 |
|
| 4250 |
|
| 4251 |
|
| 4252 |
|
| 4253 |
ast_softhangup_nolock(chan, AST_SOFTHANGUP_HANGUP_EXEC); |
| 4254 |
|
| 4255 |
|
| 4256 |
if (context != ast_channel_context(chan)) { |
| 4257 |
ast_channel_context_set(chan, context); |
| 4258 |
} |
| 4259 |
ast_channel_exten_set(chan, "h"); |
| 4260 |
ast_channel_priority_set(chan, 1); |
| 4261 |
|
| 4262 |
|
| 4263 |
autoloopflag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP); |
| 4264 |
ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP); |
| 4265 |
ast_channel_unlock(chan); |
| 4266 |
|
| 4267 |
for (;;) { |
| 4268 |
spawn_error = ast_spawn_extension(chan, ast_channel_context(chan), |
| 4269 |
ast_channel_exten(chan), ast_channel_priority(chan), |
| 4270 |
S_COR(ast_channel_caller(chan)->id.number.valid, |
| 4271 |
ast_channel_caller(chan)->id.number.str, NULL), &found, 1); |
| 4272 |
|
| 4273 |
ast_channel_lock(chan); |
| 4274 |
if (spawn_error) { |
| 4275 |
|
| 4276 |
break; |
| 4277 |
} |
| 4278 |
ast_channel_priority_set(chan, ast_channel_priority(chan) + 1); |
| 4279 |
ast_channel_unlock(chan); |
| 4280 |
} |
| 4281 |
if (found && spawn_error) { |
| 4282 |
|
| 4283 |
ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", |
| 4284 |
ast_channel_context(chan), ast_channel_exten(chan), |
| 4285 |
ast_channel_priority(chan), ast_channel_name(chan)); |
| 4286 |
ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", |
| 4287 |
ast_channel_context(chan), ast_channel_exten(chan), |
| 4288 |
ast_channel_priority(chan), ast_channel_name(chan)); |
| 4289 |
} |
| 4290 |
|
| 4291 |
|
| 4292 |
ast_set_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_RUN); |
| 4293 |
|
| 4294 |
|
| 4295 |
ast_set2_flag(ast_channel_flags(chan), autoloopflag, AST_FLAG_IN_AUTOLOOP); |
| 4296 |
ast_channel_unlock(chan); |
| 4297 |
} |
| 4298 |
|
| 4299 |
|
| 4300 |
void set_ext_pri(struct ast_channel *c, const char *exten, int pri) |
| 4301 |
{ |
| 4302 |
ast_channel_lock(c); |
| 4303 |
ast_channel_exten_set(c, exten); |
| 4304 |
ast_channel_priority_set(c, pri); |
| 4305 |
ast_channel_unlock(c); |
| 4306 |
} |
| 4307 |
|
| 4308 |
|
| 4309 |
|
| 4310 |
|
| 4311 |
|
| 4312 |
|
| 4313 |
|
| 4314 |
|
| 4315 |
static int collect_digits(struct ast_channel *c, int waittime, char *buf, int buflen, int pos) |
| 4316 |
{ |
| 4317 |
int digit; |
| 4318 |
|
| 4319 |
buf[pos] = '\0'; |
| 4320 |
while (ast_matchmore_extension(c, ast_channel_context(c), buf, 1, |
| 4321 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4322 |
|
| 4323 |
|
| 4324 |
digit = ast_waitfordigit(c, waittime); |
| 4325 |
if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_ASYNCGOTO) { |
| 4326 |
ast_channel_clear_softhangup(c, AST_SOFTHANGUP_ASYNCGOTO); |
| 4327 |
} else { |
| 4328 |
if (!digit) |
| 4329 |
break; |
| 4330 |
if (digit < 0) |
| 4331 |
return -1; |
| 4332 |
if (pos < buflen - 1) { |
| 4333 |
buf[pos++] = digit; |
| 4334 |
buf[pos] = '\0'; |
| 4335 |
} |
| 4336 |
waittime = ast_channel_pbx(c)->dtimeoutms; |
| 4337 |
} |
| 4338 |
} |
| 4339 |
return 0; |
| 4340 |
} |
| 4341 |
|
| 4342 |
static enum ast_pbx_result __ast_pbx_run(struct ast_channel *c, |
| 4343 |
struct ast_pbx_args *args) |
| 4344 |
{ |
| 4345 |
int found = 0; |
| 4346 |
int res = 0; |
| 4347 |
int autoloopflag; |
| 4348 |
int error = 0; |
| 4349 |
struct ast_pbx *pbx; |
| 4350 |
ast_callid callid; |
| 4351 |
|
| 4352 |
|
| 4353 |
if (ast_channel_pbx(c)) { |
| 4354 |
ast_log(LOG_WARNING, "%s already has PBX structure??\n", ast_channel_name(c)); |
| 4355 |
|
| 4356 |
ast_free(ast_channel_pbx(c)); |
| 4357 |
} |
| 4358 |
if (!(pbx = ast_calloc(1, sizeof(*pbx)))) { |
| 4359 |
return AST_PBX_FAILED; |
| 4360 |
} |
| 4361 |
|
| 4362 |
callid = ast_read_threadstorage_callid(); |
| 4363 |
|
| 4364 |
if (!callid) { |
| 4365 |
|
| 4366 |
|
| 4367 |
|
| 4368 |
callid = ast_channel_callid(c); |
| 4369 |
if (!callid) { |
| 4370 |
callid = ast_create_callid(); |
| 4371 |
if (callid) { |
| 4372 |
ast_channel_lock(c); |
| 4373 |
ast_channel_callid_set(c, callid); |
| 4374 |
ast_channel_unlock(c); |
| 4375 |
} |
| 4376 |
} |
| 4377 |
ast_callid_threadassoc_add(callid); |
| 4378 |
callid = 0; |
| 4379 |
} |
| 4380 |
|
| 4381 |
ast_channel_pbx_set(c, pbx); |
| 4382 |
|
| 4383 |
ast_channel_pbx(c)->rtimeoutms = 10000; |
| 4384 |
ast_channel_pbx(c)->dtimeoutms = 5000; |
| 4385 |
|
| 4386 |
ast_channel_lock(c); |
| 4387 |
autoloopflag = ast_test_flag(ast_channel_flags(c), AST_FLAG_IN_AUTOLOOP); |
| 4388 |
ast_set_flag(ast_channel_flags(c), AST_FLAG_IN_AUTOLOOP); |
| 4389 |
ast_channel_unlock(c); |
| 4390 |
|
| 4391 |
if (ast_strlen_zero(ast_channel_exten(c))) { |
| 4392 |
|
| 4393 |
ast_verb(2, "Starting %s at %s,%s,%d failed so falling back to exten 's'\n", ast_channel_name(c), ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c)); |
| 4394 |
|
| 4395 |
|
| 4396 |
|
| 4397 |
|
| 4398 |
set_ext_pri(c, "s", 1); |
| 4399 |
} |
| 4400 |
|
| 4401 |
for (;;) { |
| 4402 |
char dst_exten[256]; |
| 4403 |
int pos = 0; |
| 4404 |
int digit = 0; |
| 4405 |
int invalid = 0; |
| 4406 |
int timeout = 0; |
| 4407 |
|
| 4408 |
|
| 4409 |
dst_exten[pos] = '\0'; |
| 4410 |
|
| 4411 |
|
| 4412 |
while (!(res = ast_spawn_extension(c, ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c), |
| 4413 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL), |
| 4414 |
&found, 1))) { |
| 4415 |
|
| 4416 |
if (!ast_check_hangup(c)) { |
| 4417 |
ast_channel_priority_set(c, ast_channel_priority(c) + 1); |
| 4418 |
continue; |
| 4419 |
} |
| 4420 |
|
| 4421 |
|
| 4422 |
if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_ASYNCGOTO) { |
| 4423 |
ast_channel_clear_softhangup(c, AST_SOFTHANGUP_ASYNCGOTO); |
| 4424 |
continue; |
| 4425 |
} |
| 4426 |
if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_TIMEOUT) { |
| 4427 |
if (ast_exists_extension(c, ast_channel_context(c), "T", 1, |
| 4428 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4429 |
set_ext_pri(c, "T", 1); |
| 4430 |
|
| 4431 |
memset(ast_channel_whentohangup(c), 0, sizeof(*ast_channel_whentohangup(c))); |
| 4432 |
ast_channel_clear_softhangup(c, AST_SOFTHANGUP_TIMEOUT); |
| 4433 |
continue; |
| 4434 |
} else if (ast_exists_extension(c, ast_channel_context(c), "e", 1, |
| 4435 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4436 |
raise_exception(c, "ABSOLUTETIMEOUT", 1); |
| 4437 |
|
| 4438 |
memset(ast_channel_whentohangup(c), 0, sizeof(*ast_channel_whentohangup(c))); |
| 4439 |
ast_channel_clear_softhangup(c, AST_SOFTHANGUP_TIMEOUT); |
| 4440 |
continue; |
| 4441 |
} |
| 4442 |
|
| 4443 |
|
| 4444 |
error = 1; |
| 4445 |
break; |
| 4446 |
} |
| 4447 |
ast_debug(1, "Extension %s, priority %d returned normally even though call was hung up\n", |
| 4448 |
ast_channel_exten(c), ast_channel_priority(c)); |
| 4449 |
error = 1; |
| 4450 |
break; |
| 4451 |
} |
| 4452 |
if (found && res) { |
| 4453 |
|
| 4454 |
if (strchr("0123456789ABCDEF*#", res)) { |
| 4455 |
ast_debug(1, "Oooh, got something to jump out with ('%c')!\n", res); |
| 4456 |
pos = 0; |
| 4457 |
dst_exten[pos++] = digit = res; |
| 4458 |
dst_exten[pos] = '\0'; |
| 4459 |
} else if (res == AST_PBX_INCOMPLETE) { |
| 4460 |
ast_debug(1, "Spawn extension (%s,%s,%d) exited INCOMPLETE on '%s'\n", ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c), ast_channel_name(c)); |
| 4461 |
ast_verb(2, "Spawn extension (%s, %s, %d) exited INCOMPLETE on '%s'\n", ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c), ast_channel_name(c)); |
| 4462 |
|
| 4463 |
|
| 4464 |
if (!ast_matchmore_extension(c, ast_channel_context(c), ast_channel_exten(c), 1, |
| 4465 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4466 |
invalid = 1; |
| 4467 |
} else { |
| 4468 |
ast_copy_string(dst_exten, ast_channel_exten(c), sizeof(dst_exten)); |
| 4469 |
digit = 1; |
| 4470 |
pos = strlen(dst_exten); |
| 4471 |
} |
| 4472 |
} else { |
| 4473 |
ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c), ast_channel_name(c)); |
| 4474 |
ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c), ast_channel_name(c)); |
| 4475 |
|
| 4476 |
if ((res == AST_PBX_ERROR) |
| 4477 |
&& ast_exists_extension(c, ast_channel_context(c), "e", 1, |
| 4478 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4479 |
|
| 4480 |
if (!strcmp(ast_channel_exten(c), "e")) { |
| 4481 |
ast_verb(2, "Spawn extension (%s, %s, %d) exited ERROR while already on 'e' exten on '%s'\n", ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c), ast_channel_name(c)); |
| 4482 |
error = 1; |
| 4483 |
} else { |
| 4484 |
raise_exception(c, "ERROR", 1); |
| 4485 |
continue; |
| 4486 |
} |
| 4487 |
} |
| 4488 |
|
| 4489 |
if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_ASYNCGOTO) { |
| 4490 |
ast_channel_clear_softhangup(c, AST_SOFTHANGUP_ASYNCGOTO); |
| 4491 |
continue; |
| 4492 |
} |
| 4493 |
if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_TIMEOUT) { |
| 4494 |
if (ast_exists_extension(c, ast_channel_context(c), "T", 1, |
| 4495 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4496 |
set_ext_pri(c, "T", 1); |
| 4497 |
|
| 4498 |
memset(ast_channel_whentohangup(c), 0, sizeof(*ast_channel_whentohangup(c))); |
| 4499 |
ast_channel_clear_softhangup(c, AST_SOFTHANGUP_TIMEOUT); |
| 4500 |
continue; |
| 4501 |
} else if (ast_exists_extension(c, ast_channel_context(c), "e", 1, |
| 4502 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4503 |
raise_exception(c, "ABSOLUTETIMEOUT", 1); |
| 4504 |
|
| 4505 |
memset(ast_channel_whentohangup(c), 0, sizeof(*ast_channel_whentohangup(c))); |
| 4506 |
ast_channel_clear_softhangup(c, AST_SOFTHANGUP_TIMEOUT); |
| 4507 |
continue; |
| 4508 |
} |
| 4509 |
|
| 4510 |
} |
| 4511 |
error = 1; |
| 4512 |
break; |
| 4513 |
} |
| 4514 |
} |
| 4515 |
if (error) |
| 4516 |
break; |
| 4517 |
|
| 4518 |
|
| 4519 |
|
| 4520 |
|
| 4521 |
|
| 4522 |
|
| 4523 |
if (invalid |
| 4524 |
|| (ast_strlen_zero(dst_exten) && |
| 4525 |
!ast_exists_extension(c, ast_channel_context(c), ast_channel_exten(c), 1, |
| 4526 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL)))) { |
| 4527 |
|
| 4528 |
|
| 4529 |
|
| 4530 |
|
| 4531 |
|
| 4532 |
if (ast_exists_extension(c, ast_channel_context(c), "i", 1, |
| 4533 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4534 |
ast_verb(3, "Channel '%s' sent to invalid extension: context,exten,priority=%s,%s,%d\n", |
| 4535 |
ast_channel_name(c), ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c)); |
| 4536 |
pbx_builtin_setvar_helper(c, "INVALID_EXTEN", ast_channel_exten(c)); |
| 4537 |
set_ext_pri(c, "i", 1); |
| 4538 |
} else if (ast_exists_extension(c, ast_channel_context(c), "e", 1, |
| 4539 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4540 |
raise_exception(c, "INVALID", 1); |
| 4541 |
} else { |
| 4542 |
ast_log(LOG_WARNING, "Channel '%s' sent to invalid extension but no invalid handler: context,exten,priority=%s,%s,%d\n", |
| 4543 |
ast_channel_name(c), ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c)); |
| 4544 |
error = 1; |
| 4545 |
break; |
| 4546 |
} |
| 4547 |
} else if (ast_channel_softhangup_internal_flag(c) & AST_SOFTHANGUP_TIMEOUT) { |
| 4548 |
|
| 4549 |
ast_channel_clear_softhangup(c, AST_SOFTHANGUP_TIMEOUT); |
| 4550 |
} else { |
| 4551 |
int waittime = 0; |
| 4552 |
if (digit) |
| 4553 |
waittime = ast_channel_pbx(c)->dtimeoutms; |
| 4554 |
else if (!autofallthrough) |
| 4555 |
waittime = ast_channel_pbx(c)->rtimeoutms; |
| 4556 |
if (!waittime) { |
| 4557 |
const char *status = pbx_builtin_getvar_helper(c, "DIALSTATUS"); |
| 4558 |
if (!status) |
| 4559 |
status = "UNKNOWN"; |
| 4560 |
ast_verb(3, "Auto fallthrough, channel '%s' status is '%s'\n", ast_channel_name(c), status); |
| 4561 |
if (!strcasecmp(status, "CONGESTION")) |
| 4562 |
res = indicate_congestion(c, "10"); |
| 4563 |
else if (!strcasecmp(status, "CHANUNAVAIL")) |
| 4564 |
res = indicate_congestion(c, "10"); |
| 4565 |
else if (!strcasecmp(status, "BUSY")) |
| 4566 |
res = indicate_busy(c, "10"); |
| 4567 |
error = 1; |
| 4568 |
break; |
| 4569 |
} |
| 4570 |
|
| 4571 |
if (collect_digits(c, waittime, dst_exten, sizeof(dst_exten), pos)) |
| 4572 |
break; |
| 4573 |
if (res == AST_PBX_INCOMPLETE && ast_strlen_zero(&dst_exten[pos])) |
| 4574 |
timeout = 1; |
| 4575 |
if (!timeout |
| 4576 |
&& ast_exists_extension(c, ast_channel_context(c), dst_exten, 1, |
| 4577 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4578 |
set_ext_pri(c, dst_exten, 1); |
| 4579 |
} else { |
| 4580 |
|
| 4581 |
if (!timeout && !ast_strlen_zero(dst_exten)) { |
| 4582 |
|
| 4583 |
if (ast_exists_extension(c, ast_channel_context(c), "i", 1, |
| 4584 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4585 |
ast_verb(3, "Invalid extension '%s' in context '%s' on %s\n", dst_exten, ast_channel_context(c), ast_channel_name(c)); |
| 4586 |
pbx_builtin_setvar_helper(c, "INVALID_EXTEN", dst_exten); |
| 4587 |
set_ext_pri(c, "i", 1); |
| 4588 |
} else if (ast_exists_extension(c, ast_channel_context(c), "e", 1, |
| 4589 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4590 |
raise_exception(c, "INVALID", 1); |
| 4591 |
} else { |
| 4592 |
ast_log(LOG_WARNING, |
| 4593 |
"Invalid extension '%s', but no rule 'i' or 'e' in context '%s'\n", |
| 4594 |
dst_exten, ast_channel_context(c)); |
| 4595 |
found = 1; |
| 4596 |
break; |
| 4597 |
} |
| 4598 |
} else { |
| 4599 |
|
| 4600 |
if (ast_exists_extension(c, ast_channel_context(c), "t", 1, |
| 4601 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4602 |
ast_verb(3, "Timeout on %s\n", ast_channel_name(c)); |
| 4603 |
set_ext_pri(c, "t", 1); |
| 4604 |
} else if (ast_exists_extension(c, ast_channel_context(c), "e", 1, |
| 4605 |
S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) { |
| 4606 |
raise_exception(c, "RESPONSETIMEOUT", 1); |
| 4607 |
} else { |
| 4608 |
ast_log(LOG_WARNING, |
| 4609 |
"Timeout, but no rule 't' or 'e' in context '%s'\n", |
| 4610 |
ast_channel_context(c)); |
| 4611 |
found = 1; |
| 4612 |
break; |
| 4613 |
} |
| 4614 |
} |
| 4615 |
} |
| 4616 |
} |
| 4617 |
} |
| 4618 |
|
| 4619 |
if (!found && !error) { |
| 4620 |
ast_log(LOG_WARNING, "Don't know what to do with '%s'\n", ast_channel_name(c)); |
| 4621 |
} |
| 4622 |
|
| 4623 |
if (!args || !args->no_hangup_chan) { |
| 4624 |
ast_softhangup(c, AST_SOFTHANGUP_APPUNLOAD); |
| 4625 |
if (!ast_test_flag(ast_channel_flags(c), AST_FLAG_BRIDGE_HANGUP_RUN) |
| 4626 |
&& ast_exists_extension(c, ast_channel_context(c), "h", 1, |
| 4627 |
S_COR(ast_channel_caller(c)->id.number.valid, |
| 4628 |
ast_channel_caller(c)->id.number.str, NULL))) { |
| 4629 |
ast_pbx_h_exten_run(c, ast_channel_context(c)); |
| 4630 |
} |
| 4631 |
ast_pbx_hangup_handler_run(c); |
| 4632 |
} |
| 4633 |
|
| 4634 |
ast_channel_lock(c); |
| 4635 |
ast_set2_flag(ast_channel_flags(c), autoloopflag, AST_FLAG_IN_AUTOLOOP); |
| 4636 |
ast_clear_flag(ast_channel_flags(c), AST_FLAG_BRIDGE_HANGUP_RUN); |
| 4637 |
ast_channel_unlock(c); |
| 4638 |
pbx_destroy(ast_channel_pbx(c)); |
| 4639 |
ast_channel_pbx_set(c, NULL); |
| 4640 |
|
| 4641 |
if (!args || !args->no_hangup_chan) { |
| 4642 |
ast_hangup(c); |
| 4643 |
} |
| 4644 |
|
| 4645 |
return AST_PBX_SUCCESS; |
| 4646 |
} |
| 4647 |
|
| 4648 |
|
| 4649 |
|
| 4650 |
|
| 4651 |
|
| 4652 |
|
| 4653 |
static int increase_call_count(const struct ast_channel *c) |
| 4654 |
{ |
| 4655 |
int failed = 0; |
| 4656 |
double curloadavg; |
| 4657 |
#if defined(HAVE_SYSINFO) |
| 4658 |
struct sysinfo sys_info; |
| 4659 |
#endif |
| 4660 |
|
| 4661 |
ast_mutex_lock(&maxcalllock); |
| 4662 |
if (ast_option_maxcalls) { |
| 4663 |
if (countcalls >= ast_option_maxcalls) { |
| 4664 |
ast_log(LOG_WARNING, "Maximum call limit of %d calls exceeded by '%s'!\n", ast_option_maxcalls, ast_channel_name(c)); |
| 4665 |
failed = -1; |
| 4666 |
} |
| 4667 |
} |
| 4668 |
if (ast_option_maxload) { |
| 4669 |
getloadavg(&curloadavg, 1); |
| 4670 |
if (curloadavg >= ast_option_maxload) { |
| 4671 |
ast_log(LOG_WARNING, "Maximum loadavg limit of %f load exceeded by '%s' (currently %f)!\n", ast_option_maxload, ast_channel_name(c), curloadavg); |
| 4672 |
failed = -1; |
| 4673 |
} |
| 4674 |
} |
| 4675 |
#if defined(HAVE_SYSINFO) |
| 4676 |
if (option_minmemfree) { |
| 4677 |
|
| 4678 |
if (!sysinfo(&sys_info)) { |
| 4679 |
|
| 4680 |
|
| 4681 |
uint64_t curfreemem = sys_info.freeram + sys_info.bufferram; |
| 4682 |
curfreemem *= sys_info.mem_unit; |
| 4683 |
curfreemem /= 1024 * 1024; |
| 4684 |
if (curfreemem < option_minmemfree) { |
| 4685 |
ast_log(LOG_WARNING, "Available system memory (~%" PRIu64 "MB) is below the configured low watermark (%ldMB)\n", |
| 4686 |
curfreemem, option_minmemfree); |
| 4687 |
failed = -1; |
| 4688 |
} |
| 4689 |
} |
| 4690 |
} |
| 4691 |
#endif |
| 4692 |
|
| 4693 |
if (!failed) { |
| 4694 |
countcalls++; |
| 4695 |
totalcalls++; |
| 4696 |
} |
| 4697 |
ast_mutex_unlock(&maxcalllock); |
| 4698 |
|
| 4699 |
return failed; |
| 4700 |
} |
| 4701 |
|
| 4702 |
static void decrease_call_count(void) |
| 4703 |
{ |
| 4704 |
ast_mutex_lock(&maxcalllock); |
| 4705 |
if (countcalls > 0) |
| 4706 |
countcalls--; |
| 4707 |
ast_mutex_unlock(&maxcalllock); |
| 4708 |
} |
| 4709 |
|
| 4710 |
static void destroy_exten(struct ast_exten *e) |
| 4711 |
{ |
| 4712 |
if (e->priority == PRIORITY_HINT) |
| 4713 |
ast_remove_hint(e); |
| 4714 |
|
| 4715 |
if (e->peer_table) |
| 4716 |
ast_hashtab_destroy(e->peer_table,0); |
| 4717 |
if (e->peer_label_table) |
| 4718 |
ast_hashtab_destroy(e->peer_label_table, 0); |
| 4719 |
if (e->datad) |
| 4720 |
e->datad(e->data); |
| 4721 |
ast_free(e); |
| 4722 |
} |
| 4723 |
|
| 4724 |
static void *pbx_thread(void *data) |
| 4725 |
{ |
| 4726 |
|
| 4727 |
|
| 4728 |
|
| 4729 |
|
| 4730 |
|
| 4731 |
|
| 4732 |
|
| 4733 |
|
| 4734 |
struct ast_channel *c = data; |
| 4735 |
|
| 4736 |
__ast_pbx_run(c, NULL); |
| 4737 |
decrease_call_count(); |
| 4738 |
|
| 4739 |
pthread_exit(NULL); |
| 4740 |
|
| 4741 |
return NULL; |
| 4742 |
} |
| 4743 |
|
| 4744 |
enum ast_pbx_result ast_pbx_start(struct ast_channel *c) |
| 4745 |
{ |
| 4746 |
pthread_t t; |
| 4747 |
|
| 4748 |
if (!c) { |
| 4749 |
ast_log(LOG_WARNING, "Asked to start thread on NULL channel?\n"); |
| 4750 |
return AST_PBX_FAILED; |
| 4751 |
} |
| 4752 |
|
| 4753 |
if (!ast_fully_booted) { |
| 4754 |
ast_log(LOG_WARNING, "PBX requires Asterisk to be fully booted\n"); |
| 4755 |
return AST_PBX_FAILED; |
| 4756 |
} |
| 4757 |
|
| 4758 |
if (increase_call_count(c)) |
| 4759 |
return AST_PBX_CALL_LIMIT; |
| 4760 |
|
| 4761 |
|
| 4762 |
if (ast_pthread_create_detached(&t, NULL, pbx_thread, c)) { |
| 4763 |
ast_log(LOG_WARNING, "Failed to create new channel thread\n"); |
| 4764 |
decrease_call_count(); |
| 4765 |
return AST_PBX_FAILED; |
| 4766 |
} |
| 4767 |
|
| 4768 |
return AST_PBX_SUCCESS; |
| 4769 |
} |
| 4770 |
|
| 4771 |
enum ast_pbx_result ast_pbx_run_args(struct ast_channel *c, struct ast_pbx_args *args) |
| 4772 |
{ |
| 4773 |
enum ast_pbx_result res = AST_PBX_SUCCESS; |
| 4774 |
|
| 4775 |
if (!ast_fully_booted) { |
| 4776 |
ast_log(LOG_WARNING, "PBX requires Asterisk to be fully booted\n"); |
| 4777 |
return AST_PBX_FAILED; |
| 4778 |
} |
| 4779 |
|
| 4780 |
if (increase_call_count(c)) { |
| 4781 |
return AST_PBX_CALL_LIMIT; |
| 4782 |
} |
| 4783 |
|
| 4784 |
res = __ast_pbx_run(c, args); |
| 4785 |
|
| 4786 |
decrease_call_count(); |
| 4787 |
|
| 4788 |
return res; |
| 4789 |
} |
| 4790 |
|
| 4791 |
enum ast_pbx_result ast_pbx_run(struct ast_channel *c) |
| 4792 |
{ |
| 4793 |
return ast_pbx_run_args(c, NULL); |
| 4794 |
} |
| 4795 |
|
| 4796 |
int ast_active_calls(void) |
| 4797 |
{ |
| 4798 |
return countcalls; |
| 4799 |
} |
| 4800 |
|
| 4801 |
int ast_processed_calls(void) |
| 4802 |
{ |
| 4803 |
return totalcalls; |
| 4804 |
} |
| 4805 |
|
| 4806 |
int pbx_set_autofallthrough(int newval) |
| 4807 |
{ |
| 4808 |
int oldval = autofallthrough; |
| 4809 |
autofallthrough = newval; |
| 4810 |
return oldval; |
| 4811 |
} |
| 4812 |
|
| 4813 |
int pbx_set_extenpatternmatchnew(int newval) |
| 4814 |
{ |
| 4815 |
int oldval = extenpatternmatchnew; |
| 4816 |
extenpatternmatchnew = newval; |
| 4817 |
return oldval; |
| 4818 |
} |
| 4819 |
|
| 4820 |
void pbx_set_overrideswitch(const char *newval) |
| 4821 |
{ |
| 4822 |
if (overrideswitch) { |
| 4823 |
ast_free(overrideswitch); |
| 4824 |
} |
| 4825 |
if (!ast_strlen_zero(newval)) { |
| 4826 |
overrideswitch = ast_strdup(newval); |
| 4827 |
} else { |
| 4828 |
overrideswitch = NULL; |
| 4829 |
} |
| 4830 |
} |
| 4831 |
|
| 4832 |
|
| 4833 |
|
| 4834 |
|
| 4835 |
|
| 4836 |
static struct ast_context *find_context(const char *context) |
| 4837 |
{ |
| 4838 |
struct ast_context item = { |
| 4839 |
.name = context, |
| 4840 |
}; |
| 4841 |
|
| 4842 |
return ast_hashtab_lookup(contexts_table, &item); |
| 4843 |
} |
| 4844 |
|
| 4845 |
|
| 4846 |
|
| 4847 |
|
| 4848 |
|
| 4849 |
|
| 4850 |
static struct ast_context *find_context_locked(const char *context) |
| 4851 |
{ |
| 4852 |
struct ast_context *c; |
| 4853 |
struct ast_context item = { |
| 4854 |
.name = context, |
| 4855 |
}; |
| 4856 |
|
| 4857 |
ast_rdlock_contexts(); |
| 4858 |
c = ast_hashtab_lookup(contexts_table, &item); |
| 4859 |
if (!c) { |
| 4860 |
ast_unlock_contexts(); |
| 4861 |
} |
| 4862 |
|
| 4863 |
return c; |
| 4864 |
} |
| 4865 |
|
| 4866 |
|
| 4867 |
|
| 4868 |
|
| 4869 |
|
| 4870 |
|
| 4871 |
|
| 4872 |
int ast_context_remove_include(const char *context, const char *include, const char *registrar) |
| 4873 |
{ |
| 4874 |
int ret = -1; |
| 4875 |
struct ast_context *c; |
| 4876 |
|
| 4877 |
c = find_context_locked(context); |
| 4878 |
if (c) { |
| 4879 |
|
| 4880 |
ret = ast_context_remove_include2(c, include, registrar); |
| 4881 |
ast_unlock_contexts(); |
| 4882 |
} |
| 4883 |
return ret; |
| 4884 |
} |
| 4885 |
|
| 4886 |
|
| 4887 |
|
| 4888 |
|
| 4889 |
|
| 4890 |
|
| 4891 |
|
| 4892 |
|
| 4893 |
|
| 4894 |
|
| 4895 |
int ast_context_remove_include2(struct ast_context *con, const char *include, const char *registrar) |
| 4896 |
{ |
| 4897 |
int ret = -1; |
| 4898 |
int idx; |
| 4899 |
|
| 4900 |
ast_wrlock_context(con); |
| 4901 |
|
| 4902 |
|
| 4903 |
for (idx = 0; idx < ast_context_includes_count(con); idx++) { |
| 4904 |
struct ast_include *i = AST_VECTOR_GET(&con->includes, idx); |
| 4905 |
|
| 4906 |
if (!strcmp(ast_get_include_name(i), include) && |
| 4907 |
(!registrar || !strcmp(ast_get_include_registrar(i), registrar))) { |
| 4908 |
|
| 4909 |
|
| 4910 |
ast_verb(3, "Removing inclusion of context '%s' in context '%s; registrar=%s'\n", include, ast_get_context_name(con), registrar); |
| 4911 |
AST_VECTOR_REMOVE_ORDERED(&con->includes, idx); |
| 4912 |
|
| 4913 |
|
| 4914 |
include_free(i); |
| 4915 |
ret = 0; |
| 4916 |
break; |
| 4917 |
} |
| 4918 |
} |
| 4919 |
|
| 4920 |
ast_unlock_context(con); |
| 4921 |
|
| 4922 |
return ret; |
| 4923 |
} |
| 4924 |
|
| 4925 |
|
| 4926 |
|
| 4927 |
|
| 4928 |
|
| 4929 |
|
| 4930 |
int ast_context_remove_switch(const char *context, const char *sw, const char *data, const char *registrar) |
| 4931 |
{ |
| 4932 |
int ret = -1; |
| 4933 |
struct ast_context *c; |
| 4934 |
|
| 4935 |
c = find_context_locked(context); |
| 4936 |
if (c) { |
| 4937 |
|
| 4938 |
ret = ast_context_remove_switch2(c, sw, data, registrar); |
| 4939 |
ast_unlock_contexts(); |
| 4940 |
} |
| 4941 |
return ret; |
| 4942 |
} |
| 4943 |
|
| 4944 |
|
| 4945 |
|
| 4946 |
|
| 4947 |
|
| 4948 |
|
| 4949 |
|
| 4950 |
|
| 4951 |
|
| 4952 |
int ast_context_remove_switch2(struct ast_context *con, const char *sw, const char *data, const char *registrar) |
| 4953 |
{ |
| 4954 |
int idx; |
| 4955 |
int ret = -1; |
| 4956 |
|
| 4957 |
ast_wrlock_context(con); |
| 4958 |
|
| 4959 |
|
| 4960 |
for (idx = 0; idx < ast_context_switches_count(con); idx++) { |
| 4961 |
struct ast_sw *i = AST_VECTOR_GET(&con->alts, idx); |
| 4962 |
|
| 4963 |
if (!strcmp(ast_get_switch_name(i), sw) && |
| 4964 |
!strcmp(ast_get_switch_data(i), data) && |
| 4965 |
(!registrar || !strcmp(ast_get_switch_registrar(i), registrar))) { |
| 4966 |
|
| 4967 |
|
| 4968 |
ast_verb(3, "Removing switch '%s' from context '%s; registrar=%s'\n", sw, ast_get_context_name(con), registrar); |
| 4969 |
AST_VECTOR_REMOVE_ORDERED(&con->alts, idx); |
| 4970 |
|
| 4971 |
|
| 4972 |
sw_free(i); |
| 4973 |
ret = 0; |
| 4974 |
break; |
| 4975 |
} |
| 4976 |
} |
| 4977 |
|
| 4978 |
ast_unlock_context(con); |
| 4979 |
|
| 4980 |
return ret; |
| 4981 |
} |
| 4982 |
|
| 4983 |
|
| 4984 |
int ast_context_remove_extension(const char *context, const char *extension, int priority, const char *registrar) |
| 4985 |
{ |
| 4986 |
return ast_context_remove_extension_callerid(context, extension, priority, NULL, AST_EXT_MATCHCID_ANY, registrar); |
| 4987 |
} |
| 4988 |
|
| 4989 |
int ast_context_remove_extension_callerid(const char *context, const char *extension, int priority, const char *callerid, int matchcallerid, const char *registrar) |
| 4990 |
{ |
| 4991 |
int ret = -1; |
| 4992 |
struct ast_context *c; |
| 4993 |
|
| 4994 |
c = find_context_locked(context); |
| 4995 |
if (c) { |
| 4996 |
ret = ast_context_remove_extension_callerid2(c, extension, priority, callerid, |
| 4997 |
matchcallerid, registrar, 0); |
| 4998 |
ast_unlock_contexts(); |
| 4999 |
} |
| 5000 |
|
| 5001 |
return ret; |
| 5002 |
} |
| 5003 |
|
| 5004 |
|
| 5005 |
|
| 5006 |
|
| 5007 |
|
| 5008 |
|
| 5009 |
|
| 5010 |
|
| 5011 |
|
| 5012 |
|
| 5013 |
|
| 5014 |
int ast_context_remove_extension2(struct ast_context *con, const char *extension, int priority, const char *registrar, int already_locked) |
| 5015 |
{ |
| 5016 |
return ast_context_remove_extension_callerid2(con, extension, priority, NULL, AST_EXT_MATCHCID_ANY, registrar, already_locked); |
| 5017 |
} |
| 5018 |
|
| 5019 |
int ast_context_remove_extension_callerid2(struct ast_context *con, const char *extension, int priority, const char *callerid, int matchcallerid, const char *registrar, int already_locked) |
| 5020 |
{ |
| 5021 |
struct ast_exten *exten, *prev_exten = NULL; |
| 5022 |
struct ast_exten *peer; |
| 5023 |
struct ast_exten ex, *exten2, *exten3; |
| 5024 |
char dummy_name[1024]; |
| 5025 |
char dummy_cid[1024]; |
| 5026 |
struct ast_exten *previous_peer = NULL; |
| 5027 |
struct ast_exten *next_peer = NULL; |
| 5028 |
int found = 0; |
| 5029 |
|
| 5030 |
if (!already_locked) |
| 5031 |
ast_wrlock_context(con); |
| 5032 |
|
| 5033 |
#ifdef NEED_DEBUG |
| 5034 |
ast_verb(3,"Removing %s/%s/%d%s%s from trees, registrar=%s\n", con->name, extension, priority, matchcallerid ? "/" : "", matchcallerid ? callerid : "", registrar); |
| 5035 |
#endif |
| 5036 |
#ifdef CONTEXT_DEBUG |
| 5037 |
check_contexts(__FILE__, __LINE__); |
| 5038 |
#endif |
| 5039 |
|
| 5040 |
ex.exten = dummy_name; |
| 5041 |
ext_strncpy(dummy_name, extension, sizeof(dummy_name), 1); |
| 5042 |
ex.matchcid = matchcallerid; |
| 5043 |
if (callerid) { |
| 5044 |
ex.cidmatch = dummy_cid; |
| 5045 |
ext_strncpy(dummy_cid, callerid, sizeof(dummy_cid), 1); |
| 5046 |
} else { |
| 5047 |
ex.cidmatch = NULL; |
| 5048 |
} |
| 5049 |
exten = ast_hashtab_lookup(con->root_table, &ex); |
| 5050 |
if (exten) { |
| 5051 |
if (priority == 0) { |
| 5052 |
exten2 = ast_hashtab_remove_this_object(con->root_table, exten); |
| 5053 |
if (!exten2) |
| 5054 |
ast_log(LOG_ERROR,"Trying to delete the exten %s from context %s, but could not remove from the root_table\n", extension, con->name); |
| 5055 |
if (con->pattern_tree) { |
| 5056 |
struct match_char *x = add_exten_to_pattern_tree(con, exten, 1); |
| 5057 |
|
| 5058 |
if (x->exten) { |
| 5059 |
x->deleted = 1; |
| 5060 |
x->exten = 0; |
| 5061 |
} else { |
| 5062 |
ast_log(LOG_WARNING,"Trying to delete an exten from a context, but the pattern tree node returned isn't a full extension\n"); |
| 5063 |
} |
| 5064 |
} |
| 5065 |
} else { |
| 5066 |
ex.priority = priority; |
| 5067 |
exten2 = ast_hashtab_lookup(exten->peer_table, &ex); |
| 5068 |
if (exten2) { |
| 5069 |
if (exten2->label) { |
| 5070 |
exten3 = ast_hashtab_remove_this_object(exten->peer_label_table,exten2); |
| 5071 |
if (!exten3) { |
| 5072 |
ast_log(LOG_ERROR, "Did not remove this priority label (%d/%s) " |
| 5073 |
"from the peer_label_table of context %s, extension %s!\n", |
| 5074 |
priority, exten2->label, con->name, exten2->name); |
| 5075 |
} |
| 5076 |
} |
| 5077 |
|
| 5078 |
exten3 = ast_hashtab_remove_this_object(exten->peer_table, exten2); |
| 5079 |
if (!exten3) { |
| 5080 |
ast_log(LOG_ERROR, "Did not remove this priority (%d) from the " |
| 5081 |
"peer_table of context %s, extension %s!\n", |
| 5082 |
priority, con->name, exten2->name); |
| 5083 |
} |
| 5084 |
if (exten2 == exten && exten2->peer) { |
| 5085 |
exten2 = ast_hashtab_remove_this_object(con->root_table, exten); |
| 5086 |
ast_hashtab_insert_immediate(con->root_table, exten2->peer); |
| 5087 |
} |
| 5088 |
if (ast_hashtab_size(exten->peer_table) == 0) { |
| 5089 |
|
| 5090 |
|
| 5091 |
exten3 = ast_hashtab_remove_this_object(con->root_table, exten); |
| 5092 |
if (!exten3) { |
| 5093 |
ast_log(LOG_ERROR, "Did not remove this exten (%s) from the " |
| 5094 |
"context root_table (%s) (priority %d)\n", |
| 5095 |
exten->name, con->name, priority); |
| 5096 |
} |
| 5097 |
if (con->pattern_tree) { |
| 5098 |
struct match_char *x = add_exten_to_pattern_tree(con, exten, 1); |
| 5099 |
if (x->exten) { |
| 5100 |
x->deleted = 1; |
| 5101 |
x->exten = 0; |
| 5102 |
} |
| 5103 |
} |
| 5104 |
} |
| 5105 |
} else { |
| 5106 |
ast_debug(3,"Could not find priority %d of exten %s in context %s!\n", |
| 5107 |
priority, exten->name, con->name); |
| 5108 |
} |
| 5109 |
} |
| 5110 |
} else { |
| 5111 |
|
| 5112 |
ast_log(LOG_WARNING,"Cannot find extension %s in root_table in context %s\n", |
| 5113 |
extension, con->name); |
| 5114 |
} |
| 5115 |
#ifdef NEED_DEBUG |
| 5116 |
if (con->pattern_tree) { |
| 5117 |
ast_log(LOG_NOTICE,"match char tree after exten removal:\n"); |
| 5118 |
log_match_char_tree(con->pattern_tree, " "); |
| 5119 |
} |
| 5120 |
#endif |
| 5121 |
|
| 5122 |
|
| 5123 |
for (exten = con->root; exten; prev_exten = exten, exten = exten->next) { |
| 5124 |
if (!strcmp(exten->exten, ex.exten) && |
| 5125 |
(!matchcallerid || |
| 5126 |
(!ast_strlen_zero(ex.cidmatch) && !ast_strlen_zero(exten->cidmatch) && !strcmp(exten->cidmatch, ex.cidmatch)) || |
| 5127 |
(ast_strlen_zero(ex.cidmatch) && ast_strlen_zero(exten->cidmatch)))) { |
| 5128 |
break; |
| 5129 |
} |
| 5130 |
} |
| 5131 |
if (!exten) { |
| 5132 |
|
| 5133 |
if (!already_locked) |
| 5134 |
ast_unlock_context(con); |
| 5135 |
return -1; |
| 5136 |
} |
| 5137 |
|
| 5138 |
|
| 5139 |
for (peer = exten, next_peer = exten->peer ? exten->peer : exten->next; |
| 5140 |
peer && !strcmp(peer->exten, ex.exten) && |
| 5141 |
(!callerid || (!matchcallerid && !peer->matchcid) || (matchcallerid && peer->matchcid && !strcmp(peer->cidmatch, ex.cidmatch))) ; |
| 5142 |
peer = next_peer, next_peer = next_peer ? (next_peer->peer ? next_peer->peer : next_peer->next) : NULL) { |
| 5143 |
|
| 5144 |
if ((priority == 0 || peer->priority == priority) && |
| 5145 |
(!registrar || !strcmp(peer->registrar, registrar) )) { |
| 5146 |
found = 1; |
| 5147 |
|
| 5148 |
|
| 5149 |
if (!previous_peer) { |
| 5150 |
|
| 5151 |
|
| 5152 |
|
| 5153 |
|
| 5154 |
struct ast_exten *next_node = peer->peer ? peer->peer : peer->next; |
| 5155 |
if (peer->peer) { |
| 5156 |
|
| 5157 |
|
| 5158 |
peer->peer->peer_table = peer->peer_table; |
| 5159 |
peer->peer->peer_label_table = peer->peer_label_table; |
| 5160 |
peer->peer_table = NULL; |
| 5161 |
peer->peer_label_table = NULL; |
| 5162 |
} |
| 5163 |
if (!prev_exten) { |
| 5164 |
con->root = next_node; |
| 5165 |
} else { |
| 5166 |
prev_exten->next = next_node; |
| 5167 |
} |
| 5168 |
if (peer->peer) { |
| 5169 |
peer->peer->next = peer->next; |
| 5170 |
} |
| 5171 |
} else { |
| 5172 |
previous_peer->peer = peer->peer; |
| 5173 |
} |
| 5174 |
|
| 5175 |
|
| 5176 |
|
| 5177 |
destroy_exten(peer); |
| 5178 |
} else { |
| 5179 |
previous_peer = peer; |
| 5180 |
} |
| 5181 |
} |
| 5182 |
if (!already_locked) |
| 5183 |
ast_unlock_context(con); |
| 5184 |
return found ? 0 : -1; |
| 5185 |
} |
| 5186 |
|
| 5187 |
|
| 5188 |
|
| 5189 |
|
| 5190 |
|
| 5191 |
|
| 5192 |
static char *handle_show_hints(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 5193 |
{ |
| 5194 |
struct ast_hint *hint; |
| 5195 |
int num = 0; |
| 5196 |
int watchers; |
| 5197 |
struct ao2_iterator i; |
| 5198 |
char buf[AST_MAX_EXTENSION+AST_MAX_CONTEXT+2]; |
| 5199 |
|
| 5200 |
switch (cmd) { |
| 5201 |
case CLI_INIT: |
| 5202 |
e->command = "core show hints"; |
| 5203 |
e->usage = |
| 5204 |
"Usage: core show hints\n" |
| 5205 |
" List registered hints.\n" |
| 5206 |
" Hint details are shown in five columns. In order from left to right, they are:\n" |
| 5207 |
" 1. Hint extension URI.\n" |
| 5208 |
" 2. List of mapped device or presence state identifiers.\n" |
| 5209 |
" 3. Current extension state. The aggregate of mapped device states.\n" |
| 5210 |
" 4. Current presence state for the mapped presence state provider.\n" |
| 5211 |
" 5. Watchers - number of subscriptions and other entities watching this hint.\n"; |
| 5212 |
return NULL; |
| 5213 |
case CLI_GENERATE: |
| 5214 |
return NULL; |
| 5215 |
} |
| 5216 |
|
| 5217 |
if (ao2_container_count(hints) == 0) { |
| 5218 |
ast_cli(a->fd, "There are no registered dialplan hints\n"); |
| 5219 |
return CLI_SUCCESS; |
| 5220 |
} |
| 5221 |
|
| 5222 |
ast_cli(a->fd, "\n -= Registered Asterisk Dial Plan Hints =-\n"); |
| 5223 |
|
| 5224 |
i = ao2_iterator_init(hints, 0); |
| 5225 |
for (; (hint = ao2_iterator_next(&i)); ao2_ref(hint, -1)) { |
| 5226 |
ao2_lock(hint); |
| 5227 |
if (!hint->exten) { |
| 5228 |
|
| 5229 |
ao2_unlock(hint); |
| 5230 |
continue; |
| 5231 |
} |
| 5232 |
watchers = ao2_container_count(hint->callbacks); |
| 5233 |
snprintf(buf, sizeof(buf), "%s@%s", |
| 5234 |
ast_get_extension_name(hint->exten), |
| 5235 |
ast_get_context_name(ast_get_extension_context(hint->exten))); |
| 5236 |
|
| 5237 |
ast_cli(a->fd, "%-30.30s: %-60.60s State:%-15.15s Presence:%-15.15s Watchers %2d\n", |
| 5238 |
buf, |
| 5239 |
ast_get_extension_app(hint->exten), |
| 5240 |
ast_extension_state2str(hint->laststate), |
| 5241 |
ast_presence_state2str(hint->last_presence_state), |
| 5242 |
watchers); |
| 5243 |
|
| 5244 |
ao2_unlock(hint); |
| 5245 |
num++; |
| 5246 |
} |
| 5247 |
ao2_iterator_destroy(&i); |
| 5248 |
|
| 5249 |
ast_cli(a->fd, "----------------\n"); |
| 5250 |
ast_cli(a->fd, "- %d hints registered\n", num); |
| 5251 |
return CLI_SUCCESS; |
| 5252 |
} |
| 5253 |
|
| 5254 |
|
| 5255 |
static char *complete_core_show_hint(const char *line, const char *word, int pos, int state) |
| 5256 |
{ |
| 5257 |
struct ast_hint *hint; |
| 5258 |
char *ret = NULL; |
| 5259 |
int which = 0; |
| 5260 |
int wordlen; |
| 5261 |
struct ao2_iterator i; |
| 5262 |
|
| 5263 |
if (pos != 3) |
| 5264 |
return NULL; |
| 5265 |
|
| 5266 |
wordlen = strlen(word); |
| 5267 |
|
| 5268 |
|
| 5269 |
i = ao2_iterator_init(hints, 0); |
| 5270 |
for (; (hint = ao2_iterator_next(&i)); ao2_ref(hint, -1)) { |
| 5271 |
ao2_lock(hint); |
| 5272 |
if (!hint->exten) { |
| 5273 |
|
| 5274 |
ao2_unlock(hint); |
| 5275 |
continue; |
| 5276 |
} |
| 5277 |
if (!strncasecmp(word, ast_get_extension_name(hint->exten), wordlen) && ++which > state) { |
| 5278 |
ret = ast_strdup(ast_get_extension_name(hint->exten)); |
| 5279 |
ao2_unlock(hint); |
| 5280 |
ao2_ref(hint, -1); |
| 5281 |
break; |
| 5282 |
} |
| 5283 |
ao2_unlock(hint); |
| 5284 |
} |
| 5285 |
ao2_iterator_destroy(&i); |
| 5286 |
|
| 5287 |
return ret; |
| 5288 |
} |
| 5289 |
|
| 5290 |
|
| 5291 |
static char *handle_show_hint(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 5292 |
{ |
| 5293 |
struct ast_hint *hint; |
| 5294 |
int watchers; |
| 5295 |
int num = 0, extenlen; |
| 5296 |
struct ao2_iterator i; |
| 5297 |
char buf[AST_MAX_EXTENSION+AST_MAX_CONTEXT+2]; |
| 5298 |
|
| 5299 |
switch (cmd) { |
| 5300 |
case CLI_INIT: |
| 5301 |
e->command = "core show hint"; |
| 5302 |
e->usage = |
| 5303 |
"Usage: core show hint <exten>\n" |
| 5304 |
" List registered hint.\n" |
| 5305 |
" Hint details are shown in five columns. In order from left to right, they are:\n" |
| 5306 |
" 1. Hint extension URI.\n" |
| 5307 |
" 2. List of mapped device or presence state identifiers.\n" |
| 5308 |
" 3. Current extension state. The aggregate of mapped device states.\n" |
| 5309 |
" 4. Current presence state for the mapped presence state provider.\n" |
| 5310 |
" 5. Watchers - number of subscriptions and other entities watching this hint.\n"; |
| 5311 |
return NULL; |
| 5312 |
case CLI_GENERATE: |
| 5313 |
return complete_core_show_hint(a->line, a->word, a->pos, a->n); |
| 5314 |
} |
| 5315 |
|
| 5316 |
if (a->argc < 4) |
| 5317 |
return CLI_SHOWUSAGE; |
| 5318 |
|
| 5319 |
if (ao2_container_count(hints) == 0) { |
| 5320 |
ast_cli(a->fd, "There are no registered dialplan hints\n"); |
| 5321 |
return CLI_SUCCESS; |
| 5322 |
} |
| 5323 |
|
| 5324 |
extenlen = strlen(a->argv[3]); |
| 5325 |
i = ao2_iterator_init(hints, 0); |
| 5326 |
for (; (hint = ao2_iterator_next(&i)); ao2_ref(hint, -1)) { |
| 5327 |
ao2_lock(hint); |
| 5328 |
if (!hint->exten) { |
| 5329 |
|
| 5330 |
ao2_unlock(hint); |
| 5331 |
continue; |
| 5332 |
} |
| 5333 |
if (!strncasecmp(ast_get_extension_name(hint->exten), a->argv[3], extenlen)) { |
| 5334 |
watchers = ao2_container_count(hint->callbacks); |
| 5335 |
sprintf(buf, "%s@%s", |
| 5336 |
ast_get_extension_name(hint->exten), |
| 5337 |
ast_get_context_name(ast_get_extension_context(hint->exten))); |
| 5338 |
ast_cli(a->fd, "%-30.30s: %-60.60s State:%-15.15s Presence:%-15.15s Watchers %2d\n", |
| 5339 |
buf, |
| 5340 |
ast_get_extension_app(hint->exten), |
| 5341 |
ast_extension_state2str(hint->laststate), |
| 5342 |
ast_presence_state2str(hint->last_presence_state), |
| 5343 |
watchers); |
| 5344 |
num++; |
| 5345 |
} |
| 5346 |
ao2_unlock(hint); |
| 5347 |
} |
| 5348 |
ao2_iterator_destroy(&i); |
| 5349 |
if (!num) |
| 5350 |
ast_cli(a->fd, "No hints matching extension %s\n", a->argv[3]); |
| 5351 |
else |
| 5352 |
ast_cli(a->fd, "%d hint%s matching extension %s\n", num, (num!=1 ? "s":""), a->argv[3]); |
| 5353 |
return CLI_SUCCESS; |
| 5354 |
} |
| 5355 |
|
| 5356 |
#if 0 |
| 5357 |
|
| 5358 |
|
| 5359 |
|
| 5360 |
|
| 5361 |
|
| 5362 |
|
| 5363 |
|
| 5364 |
|
| 5365 |
|
| 5366 |
|
| 5367 |
|
| 5368 |
|
| 5369 |
|
| 5370 |
|
| 5371 |
|
| 5372 |
static char *handle_eat_memory(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 5373 |
{ |
| 5374 |
void **blocks; |
| 5375 |
int blocks_pos = 0; |
| 5376 |
const int blocks_max = 50000; |
| 5377 |
long long int allocated = 0; |
| 5378 |
int sizes[] = { |
| 5379 |
100 * 1024 * 1024, |
| 5380 |
100 * 1024, |
| 5381 |
2 * 1024, |
| 5382 |
400, |
| 5383 |
0 |
| 5384 |
}; |
| 5385 |
int i; |
| 5386 |
|
| 5387 |
switch (cmd) { |
| 5388 |
case CLI_INIT: |
| 5389 |
|
| 5390 |
e->command = "core eat memory"; |
| 5391 |
e->usage = |
| 5392 |
"Usage: core eat memory\n" |
| 5393 |
" Eats all available memory so you can test if the system survives\n"; |
| 5394 |
return NULL; |
| 5395 |
case CLI_GENERATE: |
| 5396 |
return NULL; |
| 5397 |
} |
| 5398 |
|
| 5399 |
blocks = ast_malloc(sizeof(void*) * blocks_max); |
| 5400 |
if (!blocks) { |
| 5401 |
ast_log(LOG_ERROR, "Already out of mem?\n"); |
| 5402 |
return CLI_SUCCESS; |
| 5403 |
} |
| 5404 |
|
| 5405 |
for (i = 0; sizes[i]; ++i) { |
| 5406 |
int alloc_size = sizes[i]; |
| 5407 |
ast_log(LOG_WARNING, "Allocating %d sized blocks (got %d blocks already)\n", alloc_size, blocks_pos); |
| 5408 |
while (1) { |
| 5409 |
void *block; |
| 5410 |
if (blocks_pos >= blocks_max) { |
| 5411 |
ast_log(LOG_ERROR, "Memory buffer too small? Run me again :)\n"); |
| 5412 |
break; |
| 5413 |
} |
| 5414 |
|
| 5415 |
block = ast_malloc(alloc_size); |
| 5416 |
if (!block) { |
| 5417 |
break; |
| 5418 |
} |
| 5419 |
|
| 5420 |
blocks[blocks_pos++] = block; |
| 5421 |
allocated += alloc_size; |
| 5422 |
} |
| 5423 |
} |
| 5424 |
|
| 5425 |
|
| 5426 |
ast_log(LOG_WARNING, "Allocated %lld bytes total!\n", allocated); |
| 5427 |
return CLI_SUCCESS; |
| 5428 |
} |
| 5429 |
#endif |
| 5430 |
|
| 5431 |
|
| 5432 |
|
| 5433 |
|
| 5434 |
static char *complete_show_dialplan_context(const char *line, const char *word, int pos, |
| 5435 |
int state) |
| 5436 |
{ |
| 5437 |
struct ast_context *c = NULL; |
| 5438 |
char *ret = NULL; |
| 5439 |
int which = 0; |
| 5440 |
int wordlen; |
| 5441 |
|
| 5442 |
|
| 5443 |
if (pos != 2) |
| 5444 |
return NULL; |
| 5445 |
|
| 5446 |
ast_rdlock_contexts(); |
| 5447 |
|
| 5448 |
wordlen = strlen(word); |
| 5449 |
|
| 5450 |
|
| 5451 |
while ( (c = ast_walk_contexts(c)) ) { |
| 5452 |
if (!strncasecmp(word, ast_get_context_name(c), wordlen) && ++which > state) { |
| 5453 |
ret = ast_strdup(ast_get_context_name(c)); |
| 5454 |
break; |
| 5455 |
} |
| 5456 |
} |
| 5457 |
|
| 5458 |
ast_unlock_contexts(); |
| 5459 |
|
| 5460 |
return ret; |
| 5461 |
} |
| 5462 |
|
| 5463 |
|
| 5464 |
struct dialplan_counters { |
| 5465 |
int total_items; |
| 5466 |
int total_context; |
| 5467 |
int total_exten; |
| 5468 |
int total_prio; |
| 5469 |
int context_existence; |
| 5470 |
int extension_existence; |
| 5471 |
}; |
| 5472 |
|
| 5473 |
|
| 5474 |
static void print_ext(struct ast_exten *e, char * buf, int buflen) |
| 5475 |
{ |
| 5476 |
int prio = ast_get_extension_priority(e); |
| 5477 |
if (prio == PRIORITY_HINT) { |
| 5478 |
snprintf(buf, buflen, "hint: %s", |
| 5479 |
ast_get_extension_app(e)); |
| 5480 |
} else { |
| 5481 |
snprintf(buf, buflen, "%d. %s(%s)", |
| 5482 |
prio, ast_get_extension_app(e), |
| 5483 |
(!ast_strlen_zero(ast_get_extension_app_data(e)) ? (char *)ast_get_extension_app_data(e) : "")); |
| 5484 |
} |
| 5485 |
} |
| 5486 |
|
| 5487 |
|
| 5488 |
static void show_dialplan_helper_extension_output(int fd, char *buf1, char *buf2, struct ast_exten *exten) |
| 5489 |
{ |
| 5490 |
if (ast_get_extension_registrar_file(exten)) { |
| 5491 |
ast_cli(fd, " %-17s %-45s [%s:%d]\n", |
| 5492 |
buf1, buf2, |
| 5493 |
ast_get_extension_registrar_file(exten), |
| 5494 |
ast_get_extension_registrar_line(exten)); |
| 5495 |
return; |
| 5496 |
} |
| 5497 |
|
| 5498 |
ast_cli(fd, " %-17s %-45s [%s]\n", |
| 5499 |
buf1, buf2, ast_get_extension_registrar(exten)); |
| 5500 |
} |
| 5501 |
|
| 5502 |
|
| 5503 |
static int show_dialplan_helper(int fd, const char *context, const char *exten, struct dialplan_counters *dpc, const struct ast_include *rinclude, int includecount, const char *includes[]) |
| 5504 |
{ |
| 5505 |
struct ast_context *c = NULL; |
| 5506 |
int res = 0, old_total_exten = dpc->total_exten; |
| 5507 |
|
| 5508 |
ast_rdlock_contexts(); |
| 5509 |
|
| 5510 |
|
| 5511 |
while ( (c = ast_walk_contexts(c)) ) { |
| 5512 |
int idx; |
| 5513 |
struct ast_exten *e; |
| 5514 |
#ifndef LOW_MEMORY |
| 5515 |
char buf[1024], buf2[1024]; |
| 5516 |
#else |
| 5517 |
char buf[256], buf2[256]; |
| 5518 |
#endif |
| 5519 |
int context_info_printed = 0; |
| 5520 |
|
| 5521 |
if (context && strcmp(ast_get_context_name(c), context)) |
| 5522 |
continue; |
| 5523 |
|
| 5524 |
dpc->context_existence = 1; |
| 5525 |
|
| 5526 |
ast_rdlock_context(c); |
| 5527 |
|
| 5528 |
|
| 5529 |
|
| 5530 |
|
| 5531 |
|
| 5532 |
|
| 5533 |
|
| 5534 |
if (!exten) { |
| 5535 |
dpc->total_context++; |
| 5536 |
ast_cli(fd, "[ Context '%s' created by '%s' ]\n", |
| 5537 |
ast_get_context_name(c), ast_get_context_registrar(c)); |
| 5538 |
if (c->autohints) { |
| 5539 |
ast_cli(fd, "Autohints support enabled\n"); |
| 5540 |
} |
| 5541 |
context_info_printed = 1; |
| 5542 |
} |
| 5543 |
|
| 5544 |
|
| 5545 |
e = NULL; |
| 5546 |
while ( (e = ast_walk_context_extensions(c, e)) ) { |
| 5547 |
struct ast_exten *p; |
| 5548 |
|
| 5549 |
if (exten && !ast_extension_match(ast_get_extension_name(e), exten)) |
| 5550 |
continue; |
| 5551 |
|
| 5552 |
dpc->extension_existence = 1; |
| 5553 |
|
| 5554 |
|
| 5555 |
if (!context_info_printed) { |
| 5556 |
dpc->total_context++; |
| 5557 |
if (rinclude) { |
| 5558 |
ast_cli(fd, "[ Included context '%s' created by '%s' ]\n", |
| 5559 |
ast_get_context_name(c), ast_get_context_registrar(c)); |
| 5560 |
} else { |
| 5561 |
ast_cli(fd, "[ Context '%s' created by '%s' ]\n", |
| 5562 |
ast_get_context_name(c), ast_get_context_registrar(c)); |
| 5563 |
if (c->autohints) { |
| 5564 |
ast_cli(fd, "Autohints support enabled\n"); |
| 5565 |
} |
| 5566 |
} |
| 5567 |
context_info_printed = 1; |
| 5568 |
} |
| 5569 |
dpc->total_prio++; |
| 5570 |
|
| 5571 |
|
| 5572 |
if (e->matchcid == AST_EXT_MATCHCID_ON) |
| 5573 |
snprintf(buf, sizeof(buf), "'%s' (CID match '%s') => ", ast_get_extension_name(e), e->cidmatch); |
| 5574 |
else |
| 5575 |
snprintf(buf, sizeof(buf), "'%s' =>", ast_get_extension_name(e)); |
| 5576 |
|
| 5577 |
print_ext(e, buf2, sizeof(buf2)); |
| 5578 |
|
| 5579 |
show_dialplan_helper_extension_output(fd, buf, buf2, e); |
| 5580 |
|
| 5581 |
dpc->total_exten++; |
| 5582 |
|
| 5583 |
p = e; |
| 5584 |
while ( (p = ast_walk_extension_priorities(e, p)) ) { |
| 5585 |
const char *el = ast_get_extension_label(p); |
| 5586 |
dpc->total_prio++; |
| 5587 |
if (el) |
| 5588 |
snprintf(buf, sizeof(buf), " [%s]", el); |
| 5589 |
else |
| 5590 |
buf[0] = '\0'; |
| 5591 |
print_ext(p, buf2, sizeof(buf2)); |
| 5592 |
|
| 5593 |
show_dialplan_helper_extension_output(fd, buf, buf2, p); |
| 5594 |
} |
| 5595 |
} |
| 5596 |
|
| 5597 |
|
| 5598 |
for (idx = 0; idx < ast_context_includes_count(c); idx++) { |
| 5599 |
const struct ast_include *i = ast_context_includes_get(c, idx); |
| 5600 |
|
| 5601 |
snprintf(buf, sizeof(buf), "'%s'", ast_get_include_name(i)); |
| 5602 |
if (exten) { |
| 5603 |
|
| 5604 |
if (includecount >= AST_PBX_MAX_STACK) { |
| 5605 |
ast_log(LOG_WARNING, "Maximum include depth exceeded!\n"); |
| 5606 |
} else { |
| 5607 |
int dupe = 0; |
| 5608 |
int x; |
| 5609 |
for (x = 0; x < includecount; x++) { |
| 5610 |
if (!strcasecmp(includes[x], ast_get_include_name(i))) { |
| 5611 |
dupe++; |
| 5612 |
break; |
| 5613 |
} |
| 5614 |
} |
| 5615 |
if (!dupe) { |
| 5616 |
includes[includecount] = ast_get_include_name(i); |
| 5617 |
show_dialplan_helper(fd, ast_get_include_name(i), exten, dpc, i, includecount + 1, includes); |
| 5618 |
} else { |
| 5619 |
ast_log(LOG_WARNING, "Avoiding circular include of %s within %s\n", ast_get_include_name(i), context); |
| 5620 |
} |
| 5621 |
} |
| 5622 |
} else { |
| 5623 |
ast_cli(fd, " Include => %-45s [%s]\n", |
| 5624 |
buf, ast_get_include_registrar(i)); |
| 5625 |
} |
| 5626 |
} |
| 5627 |
|
| 5628 |
|
| 5629 |
for (idx = 0; idx < ast_context_ignorepats_count(c); idx++) { |
| 5630 |
const struct ast_ignorepat *ip = ast_context_ignorepats_get(c, idx); |
| 5631 |
const char *ipname = ast_get_ignorepat_name(ip); |
| 5632 |
char ignorepat[AST_MAX_EXTENSION]; |
| 5633 |
|
| 5634 |
snprintf(buf, sizeof(buf), "'%s'", ipname); |
| 5635 |
snprintf(ignorepat, sizeof(ignorepat), "_%s.", ipname); |
| 5636 |
if (!exten || ast_extension_match(ignorepat, exten)) { |
| 5637 |
ast_cli(fd, " Ignore pattern => %-45s [%s]\n", |
| 5638 |
buf, ast_get_ignorepat_registrar(ip)); |
| 5639 |
} |
| 5640 |
} |
| 5641 |
if (!rinclude) { |
| 5642 |
for (idx = 0; idx < ast_context_switches_count(c); idx++) { |
| 5643 |
const struct ast_sw *sw = ast_context_switches_get(c, idx); |
| 5644 |
|
| 5645 |
snprintf(buf, sizeof(buf), "'%s/%s'", |
| 5646 |
ast_get_switch_name(sw), |
| 5647 |
ast_get_switch_data(sw)); |
| 5648 |
ast_cli(fd, " Alt. Switch => %-45s [%s]\n", |
| 5649 |
buf, ast_get_switch_registrar(sw)); |
| 5650 |
} |
| 5651 |
} |
| 5652 |
|
| 5653 |
ast_unlock_context(c); |
| 5654 |
|
| 5655 |
|
| 5656 |
if (context_info_printed) |
| 5657 |
ast_cli(fd, "\n"); |
| 5658 |
} |
| 5659 |
ast_unlock_contexts(); |
| 5660 |
|
| 5661 |
return (dpc->total_exten == old_total_exten) ? -1 : res; |
| 5662 |
} |
| 5663 |
|
| 5664 |
static int show_debug_helper(int fd, const char *context, const char *exten, struct dialplan_counters *dpc, struct ast_include *rinclude, int includecount, const char *includes[]) |
| 5665 |
{ |
| 5666 |
struct ast_context *c = NULL; |
| 5667 |
int res = 0, old_total_exten = dpc->total_exten; |
| 5668 |
|
| 5669 |
ast_cli(fd,"\n In-mem exten Trie for Fast Extension Pattern Matching:\n\n"); |
| 5670 |
|
| 5671 |
ast_cli(fd,"\n Explanation: Node Contents Format = <char(s) to match>:<pattern?>:<specif>:[matched extension]\n"); |
| 5672 |
ast_cli(fd, " Where <char(s) to match> is a set of chars, any one of which should match the current character\n"); |
| 5673 |
ast_cli(fd, " <pattern?>: Y if this a pattern match (eg. _XZN[5-7]), N otherwise\n"); |
| 5674 |
ast_cli(fd, " <specif>: an assigned 'exactness' number for this matching char. The lower the number, the more exact the match\n"); |
| 5675 |
ast_cli(fd, " [matched exten]: If all chars matched to this point, which extension this matches. In form: EXTEN:<exten string>\n"); |
| 5676 |
ast_cli(fd, " In general, you match a trie node to a string character, from left to right. All possible matching chars\n"); |
| 5677 |
ast_cli(fd, " are in a string vertically, separated by an unbroken string of '+' characters.\n\n"); |
| 5678 |
ast_rdlock_contexts(); |
| 5679 |
|
| 5680 |
|
| 5681 |
while ( (c = ast_walk_contexts(c)) ) { |
| 5682 |
int context_info_printed = 0; |
| 5683 |
|
| 5684 |
if (context && strcmp(ast_get_context_name(c), context)) |
| 5685 |
continue; |
| 5686 |
|
| 5687 |
dpc->context_existence = 1; |
| 5688 |
|
| 5689 |
if (!c->pattern_tree) { |
| 5690 |
|
| 5691 |
ast_exists_extension(NULL, c->name, "s", 1, ""); |
| 5692 |
} |
| 5693 |
|
| 5694 |
ast_rdlock_context(c); |
| 5695 |
|
| 5696 |
dpc->total_context++; |
| 5697 |
ast_cli(fd, "[ Context '%s' created by '%s' ]\n", |
| 5698 |
ast_get_context_name(c), ast_get_context_registrar(c)); |
| 5699 |
context_info_printed = 1; |
| 5700 |
|
| 5701 |
if (c->pattern_tree) |
| 5702 |
{ |
| 5703 |
cli_match_char_tree(c->pattern_tree, " ", fd); |
| 5704 |
} else { |
| 5705 |
ast_cli(fd,"\n No Pattern Trie present. Perhaps the context is empty...or there is trouble...\n\n"); |
| 5706 |
} |
| 5707 |
|
| 5708 |
ast_unlock_context(c); |
| 5709 |
|
| 5710 |
|
| 5711 |
if (context_info_printed) |
| 5712 |
ast_cli(fd, "\n"); |
| 5713 |
} |
| 5714 |
ast_unlock_contexts(); |
| 5715 |
|
| 5716 |
return (dpc->total_exten == old_total_exten) ? -1 : res; |
| 5717 |
} |
| 5718 |
|
| 5719 |
static char *handle_show_dialplan(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 5720 |
{ |
| 5721 |
char *exten = NULL, *context = NULL; |
| 5722 |
|
| 5723 |
struct dialplan_counters counters; |
| 5724 |
const char *incstack[AST_PBX_MAX_STACK]; |
| 5725 |
|
| 5726 |
switch (cmd) { |
| 5727 |
case CLI_INIT: |
| 5728 |
e->command = "dialplan show"; |
| 5729 |
e->usage = |
| 5730 |
"Usage: dialplan show [[exten@]context]\n" |
| 5731 |
" Show dialplan\n"; |
| 5732 |
return NULL; |
| 5733 |
case CLI_GENERATE: |
| 5734 |
return complete_show_dialplan_context(a->line, a->word, a->pos, a->n); |
| 5735 |
} |
| 5736 |
|
| 5737 |
memset(&counters, 0, sizeof(counters)); |
| 5738 |
|
| 5739 |
if (a->argc != 2 && a->argc != 3) |
| 5740 |
return CLI_SHOWUSAGE; |
| 5741 |
|
| 5742 |
|
| 5743 |
if (a->argc == 3) { |
| 5744 |
if (strchr(a->argv[2], '@')) { |
| 5745 |
context = ast_strdupa(a->argv[2]); |
| 5746 |
exten = strsep(&context, "@"); |
| 5747 |
|
| 5748 |
if (ast_strlen_zero(exten)) |
| 5749 |
exten = NULL; |
| 5750 |
} else { |
| 5751 |
context = ast_strdupa(a->argv[2]); |
| 5752 |
} |
| 5753 |
if (ast_strlen_zero(context)) |
| 5754 |
context = NULL; |
| 5755 |
} |
| 5756 |
|
| 5757 |
show_dialplan_helper(a->fd, context, exten, &counters, NULL, 0, incstack); |
| 5758 |
|
| 5759 |
|
| 5760 |
if (context && !counters.context_existence) { |
| 5761 |
ast_cli(a->fd, "There is no existence of '%s' context\n", context); |
| 5762 |
return CLI_FAILURE; |
| 5763 |
} |
| 5764 |
|
| 5765 |
if (exten && !counters.extension_existence) { |
| 5766 |
if (context) |
| 5767 |
ast_cli(a->fd, "There is no existence of %s@%s extension\n", |
| 5768 |
exten, context); |
| 5769 |
else |
| 5770 |
ast_cli(a->fd, |
| 5771 |
"There is no existence of '%s' extension in all contexts\n", |
| 5772 |
exten); |
| 5773 |
return CLI_FAILURE; |
| 5774 |
} |
| 5775 |
|
| 5776 |
ast_cli(a->fd,"-= %d %s (%d %s) in %d %s. =-\n", |
| 5777 |
counters.total_exten, counters.total_exten == 1 ? "extension" : "extensions", |
| 5778 |
counters.total_prio, counters.total_prio == 1 ? "priority" : "priorities", |
| 5779 |
counters.total_context, counters.total_context == 1 ? "context" : "contexts"); |
| 5780 |
|
| 5781 |
|
| 5782 |
return CLI_SUCCESS; |
| 5783 |
} |
| 5784 |
|
| 5785 |
|
| 5786 |
static char *handle_debug_dialplan(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 5787 |
{ |
| 5788 |
char *exten = NULL, *context = NULL; |
| 5789 |
|
| 5790 |
struct dialplan_counters counters; |
| 5791 |
const char *incstack[AST_PBX_MAX_STACK]; |
| 5792 |
|
| 5793 |
switch (cmd) { |
| 5794 |
case CLI_INIT: |
| 5795 |
e->command = "dialplan debug"; |
| 5796 |
e->usage = |
| 5797 |
"Usage: dialplan debug [context]\n" |
| 5798 |
" Show dialplan context Trie(s). Usually only useful to folks debugging the deep internals of the fast pattern matcher\n"; |
| 5799 |
return NULL; |
| 5800 |
case CLI_GENERATE: |
| 5801 |
return complete_show_dialplan_context(a->line, a->word, a->pos, a->n); |
| 5802 |
} |
| 5803 |
|
| 5804 |
memset(&counters, 0, sizeof(counters)); |
| 5805 |
|
| 5806 |
if (a->argc != 2 && a->argc != 3) |
| 5807 |
return CLI_SHOWUSAGE; |
| 5808 |
|
| 5809 |
|
| 5810 |
|
| 5811 |
if (a->argc == 3) { |
| 5812 |
if (strchr(a->argv[2], '@')) { |
| 5813 |
context = ast_strdupa(a->argv[2]); |
| 5814 |
exten = strsep(&context, "@"); |
| 5815 |
|
| 5816 |
if (ast_strlen_zero(exten)) |
| 5817 |
exten = NULL; |
| 5818 |
} else { |
| 5819 |
context = ast_strdupa(a->argv[2]); |
| 5820 |
} |
| 5821 |
if (ast_strlen_zero(context)) |
| 5822 |
context = NULL; |
| 5823 |
} |
| 5824 |
|
| 5825 |
show_debug_helper(a->fd, context, exten, &counters, NULL, 0, incstack); |
| 5826 |
|
| 5827 |
|
| 5828 |
if (context && !counters.context_existence) { |
| 5829 |
ast_cli(a->fd, "There is no existence of '%s' context\n", context); |
| 5830 |
return CLI_FAILURE; |
| 5831 |
} |
| 5832 |
|
| 5833 |
|
| 5834 |
ast_cli(a->fd,"-= %d %s. =-\n", |
| 5835 |
counters.total_context, counters.total_context == 1 ? "context" : "contexts"); |
| 5836 |
|
| 5837 |
|
| 5838 |
return CLI_SUCCESS; |
| 5839 |
} |
| 5840 |
|
| 5841 |
|
| 5842 |
static void manager_dpsendack(struct mansession *s, const struct message *m) |
| 5843 |
{ |
| 5844 |
astman_send_listack(s, m, "DialPlan list will follow", "start"); |
| 5845 |
} |
| 5846 |
|
| 5847 |
|
| 5848 |
|
| 5849 |
|
| 5850 |
|
| 5851 |
static int manager_show_dialplan_helper(struct mansession *s, const struct message *m, |
| 5852 |
const char *actionidtext, const char *context, |
| 5853 |
const char *exten, struct dialplan_counters *dpc, |
| 5854 |
const struct ast_include *rinclude, |
| 5855 |
int includecount, const char *includes[]) |
| 5856 |
{ |
| 5857 |
struct ast_context *c; |
| 5858 |
int res = 0, old_total_exten = dpc->total_exten; |
| 5859 |
|
| 5860 |
if (ast_strlen_zero(exten)) |
| 5861 |
exten = NULL; |
| 5862 |
if (ast_strlen_zero(context)) |
| 5863 |
context = NULL; |
| 5864 |
|
| 5865 |
ast_debug(3, "manager_show_dialplan: Context: -%s- Extension: -%s-\n", context, exten); |
| 5866 |
|
| 5867 |
|
| 5868 |
if (ast_rdlock_contexts()) { |
| 5869 |
astman_send_error(s, m, "Failed to lock contexts"); |
| 5870 |
ast_log(LOG_WARNING, "Failed to lock contexts list for manager: listdialplan\n"); |
| 5871 |
return -1; |
| 5872 |
} |
| 5873 |
|
| 5874 |
c = NULL; |
| 5875 |
while ( (c = ast_walk_contexts(c)) ) { |
| 5876 |
int idx; |
| 5877 |
struct ast_exten *e; |
| 5878 |
|
| 5879 |
if (context && strcmp(ast_get_context_name(c), context) != 0) |
| 5880 |
continue; |
| 5881 |
|
| 5882 |
dpc->context_existence = 1; |
| 5883 |
dpc->total_context++; |
| 5884 |
|
| 5885 |
ast_debug(3, "manager_show_dialplan: Found Context: %s \n", ast_get_context_name(c)); |
| 5886 |
|
| 5887 |
if (ast_rdlock_context(c)) { |
| 5888 |
ast_debug(3, "manager_show_dialplan: Failed to lock context\n"); |
| 5889 |
continue; |
| 5890 |
} |
| 5891 |
|
| 5892 |
|
| 5893 |
e = NULL; |
| 5894 |
while ( (e = ast_walk_context_extensions(c, e)) ) { |
| 5895 |
struct ast_exten *p; |
| 5896 |
|
| 5897 |
|
| 5898 |
if (exten && !ast_extension_match(ast_get_extension_name(e), exten)) { |
| 5899 |
|
| 5900 |
ast_debug(3, "manager_show_dialplan: Skipping extension %s\n", ast_get_extension_name(e)); |
| 5901 |
continue; |
| 5902 |
} |
| 5903 |
ast_debug(3, "manager_show_dialplan: Found Extension: %s \n", ast_get_extension_name(e)); |
| 5904 |
|
| 5905 |
dpc->extension_existence = 1; |
| 5906 |
|
| 5907 |
dpc->total_exten++; |
| 5908 |
|
| 5909 |
p = NULL; |
| 5910 |
while ( (p = ast_walk_extension_priorities(e, p)) ) { |
| 5911 |
int prio = ast_get_extension_priority(p); |
| 5912 |
|
| 5913 |
dpc->total_prio++; |
| 5914 |
if (!dpc->total_items++) |
| 5915 |
manager_dpsendack(s, m); |
| 5916 |
astman_append(s, "Event: ListDialplan\r\n%s", actionidtext); |
| 5917 |
astman_append(s, "Context: %s\r\nExtension: %s\r\n", ast_get_context_name(c), ast_get_extension_name(e) ); |
| 5918 |
|
| 5919 |
|
| 5920 |
if (ast_get_extension_label(p)) |
| 5921 |
astman_append(s, "ExtensionLabel: %s\r\n", ast_get_extension_label(p)); |
| 5922 |
|
| 5923 |
if (prio == PRIORITY_HINT) { |
| 5924 |
astman_append(s, "Priority: hint\r\nApplication: %s\r\n", ast_get_extension_app(p)); |
| 5925 |
} else { |
| 5926 |
astman_append(s, "Priority: %d\r\nApplication: %s\r\nAppData: %s\r\n", prio, ast_get_extension_app(p), (char *) ast_get_extension_app_data(p)); |
| 5927 |
} |
| 5928 |
astman_append(s, "Registrar: %s\r\n\r\n", ast_get_extension_registrar(e)); |
| 5929 |
} |
| 5930 |
} |
| 5931 |
|
| 5932 |
for (idx = 0; idx < ast_context_includes_count(c); idx++) { |
| 5933 |
const struct ast_include *i = ast_context_includes_get(c, idx); |
| 5934 |
|
| 5935 |
if (exten) { |
| 5936 |
|
| 5937 |
if (includecount >= AST_PBX_MAX_STACK) { |
| 5938 |
ast_log(LOG_WARNING, "Maximum include depth exceeded!\n"); |
| 5939 |
} else { |
| 5940 |
int dupe = 0; |
| 5941 |
int x; |
| 5942 |
for (x = 0; x < includecount; x++) { |
| 5943 |
if (!strcasecmp(includes[x], ast_get_include_name(i))) { |
| 5944 |
dupe++; |
| 5945 |
break; |
| 5946 |
} |
| 5947 |
} |
| 5948 |
if (!dupe) { |
| 5949 |
includes[includecount] = ast_get_include_name(i); |
| 5950 |
manager_show_dialplan_helper(s, m, actionidtext, ast_get_include_name(i), exten, dpc, i, includecount + 1, includes); |
| 5951 |
} else { |
| 5952 |
ast_log(LOG_WARNING, "Avoiding circular include of %s within %s\n", ast_get_include_name(i), context); |
| 5953 |
} |
| 5954 |
} |
| 5955 |
} else { |
| 5956 |
if (!dpc->total_items++) |
| 5957 |
manager_dpsendack(s, m); |
| 5958 |
astman_append(s, "Event: ListDialplan\r\n%s", actionidtext); |
| 5959 |
astman_append(s, "Context: %s\r\nIncludeContext: %s\r\nRegistrar: %s\r\n", ast_get_context_name(c), ast_get_include_name(i), ast_get_include_registrar(i)); |
| 5960 |
astman_append(s, "\r\n"); |
| 5961 |
ast_debug(3, "manager_show_dialplan: Found Included context: %s \n", ast_get_include_name(i)); |
| 5962 |
} |
| 5963 |
} |
| 5964 |
|
| 5965 |
for (idx = 0; idx < ast_context_ignorepats_count(c); idx++) { |
| 5966 |
const struct ast_ignorepat *ip = ast_context_ignorepats_get(c, idx); |
| 5967 |
const char *ipname = ast_get_ignorepat_name(ip); |
| 5968 |
char ignorepat[AST_MAX_EXTENSION]; |
| 5969 |
|
| 5970 |
snprintf(ignorepat, sizeof(ignorepat), "_%s.", ipname); |
| 5971 |
if (!exten || ast_extension_match(ignorepat, exten)) { |
| 5972 |
if (!dpc->total_items++) |
| 5973 |
manager_dpsendack(s, m); |
| 5974 |
astman_append(s, "Event: ListDialplan\r\n%s", actionidtext); |
| 5975 |
astman_append(s, "Context: %s\r\nIgnorePattern: %s\r\nRegistrar: %s\r\n", ast_get_context_name(c), ipname, ast_get_ignorepat_registrar(ip)); |
| 5976 |
astman_append(s, "\r\n"); |
| 5977 |
} |
| 5978 |
} |
| 5979 |
if (!rinclude) { |
| 5980 |
for (idx = 0; idx < ast_context_switches_count(c); idx++) { |
| 5981 |
const struct ast_sw *sw = ast_context_switches_get(c, idx); |
| 5982 |
|
| 5983 |
if (!dpc->total_items++) |
| 5984 |
manager_dpsendack(s, m); |
| 5985 |
astman_append(s, "Event: ListDialplan\r\n%s", actionidtext); |
| 5986 |
astman_append(s, "Context: %s\r\nSwitch: %s/%s\r\nRegistrar: %s\r\n", ast_get_context_name(c), ast_get_switch_name(sw), ast_get_switch_data(sw), ast_get_switch_registrar(sw)); |
| 5987 |
astman_append(s, "\r\n"); |
| 5988 |
ast_debug(3, "manager_show_dialplan: Found Switch : %s \n", ast_get_switch_name(sw)); |
| 5989 |
} |
| 5990 |
} |
| 5991 |
|
| 5992 |
ast_unlock_context(c); |
| 5993 |
} |
| 5994 |
ast_unlock_contexts(); |
| 5995 |
|
| 5996 |
if (dpc->total_exten == old_total_exten) { |
| 5997 |
ast_debug(3, "manager_show_dialplan: Found nothing new\n"); |
| 5998 |
|
| 5999 |
return -1; |
| 6000 |
} else { |
| 6001 |
return res; |
| 6002 |
} |
| 6003 |
} |
| 6004 |
|
| 6005 |
|
| 6006 |
static int manager_show_dialplan(struct mansession *s, const struct message *m) |
| 6007 |
{ |
| 6008 |
const char *exten, *context; |
| 6009 |
const char *id = astman_get_header(m, "ActionID"); |
| 6010 |
const char *incstack[AST_PBX_MAX_STACK]; |
| 6011 |
char idtext[256]; |
| 6012 |
|
| 6013 |
|
| 6014 |
struct dialplan_counters counters; |
| 6015 |
|
| 6016 |
if (!ast_strlen_zero(id)) |
| 6017 |
snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id); |
| 6018 |
else |
| 6019 |
idtext[0] = '\0'; |
| 6020 |
|
| 6021 |
memset(&counters, 0, sizeof(counters)); |
| 6022 |
|
| 6023 |
exten = astman_get_header(m, "Extension"); |
| 6024 |
context = astman_get_header(m, "Context"); |
| 6025 |
|
| 6026 |
manager_show_dialplan_helper(s, m, idtext, context, exten, &counters, NULL, 0, incstack); |
| 6027 |
|
| 6028 |
if (!ast_strlen_zero(context) && !counters.context_existence) { |
| 6029 |
char errorbuf[BUFSIZ]; |
| 6030 |
|
| 6031 |
snprintf(errorbuf, sizeof(errorbuf), "Did not find context %s", context); |
| 6032 |
astman_send_error(s, m, errorbuf); |
| 6033 |
return 0; |
| 6034 |
} |
| 6035 |
if (!ast_strlen_zero(exten) && !counters.extension_existence) { |
| 6036 |
char errorbuf[BUFSIZ]; |
| 6037 |
|
| 6038 |
if (!ast_strlen_zero(context)) |
| 6039 |
snprintf(errorbuf, sizeof(errorbuf), "Did not find extension %s@%s", exten, context); |
| 6040 |
else |
| 6041 |
snprintf(errorbuf, sizeof(errorbuf), "Did not find extension %s in any context", exten); |
| 6042 |
astman_send_error(s, m, errorbuf); |
| 6043 |
return 0; |
| 6044 |
} |
| 6045 |
|
| 6046 |
if (!counters.total_items) { |
| 6047 |
manager_dpsendack(s, m); |
| 6048 |
} |
| 6049 |
|
| 6050 |
astman_send_list_complete_start(s, m, "ShowDialPlanComplete", counters.total_items); |
| 6051 |
astman_append(s, |
| 6052 |
"ListExtensions: %d\r\n" |
| 6053 |
"ListPriorities: %d\r\n" |
| 6054 |
"ListContexts: %d\r\n", |
| 6055 |
counters.total_exten, counters.total_prio, counters.total_context); |
| 6056 |
astman_send_list_complete_end(s); |
| 6057 |
|
| 6058 |
|
| 6059 |
return 0; |
| 6060 |
} |
| 6061 |
|
| 6062 |
#ifdef AST_DEVMODE |
| 6063 |
static char *handle_show_device2extenstate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 6064 |
{ |
| 6065 |
struct ast_devstate_aggregate agg; |
| 6066 |
int i, j, exten, combined; |
| 6067 |
|
| 6068 |
switch (cmd) { |
| 6069 |
case CLI_INIT: |
| 6070 |
e->command = "core show device2extenstate"; |
| 6071 |
e->usage = |
| 6072 |
"Usage: core show device2extenstate\n" |
| 6073 |
" Lists device state to extension state combinations.\n"; |
| 6074 |
case CLI_GENERATE: |
| 6075 |
return NULL; |
| 6076 |
} |
| 6077 |
for (i = 0; i < AST_DEVICE_TOTAL; i++) { |
| 6078 |
for (j = 0; j < AST_DEVICE_TOTAL; j++) { |
| 6079 |
ast_devstate_aggregate_init(&agg); |
| 6080 |
ast_devstate_aggregate_add(&agg, i); |
| 6081 |
ast_devstate_aggregate_add(&agg, j); |
| 6082 |
combined = ast_devstate_aggregate_result(&agg); |
| 6083 |
exten = ast_devstate_to_extenstate(combined); |
| 6084 |
ast_cli(a->fd, "\n Exten:%14s CombinedDevice:%12s Dev1:%12s Dev2:%12s", ast_extension_state2str(exten), ast_devstate_str(combined), ast_devstate_str(j), ast_devstate_str(i)); |
| 6085 |
} |
| 6086 |
} |
| 6087 |
ast_cli(a->fd, "\n"); |
| 6088 |
return CLI_SUCCESS; |
| 6089 |
} |
| 6090 |
#endif |
| 6091 |
|
| 6092 |
static char *handle_set_extenpatternmatchnew(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 6093 |
{ |
| 6094 |
int oldval = 0; |
| 6095 |
|
| 6096 |
switch (cmd) { |
| 6097 |
case CLI_INIT: |
| 6098 |
e->command = "dialplan set extenpatternmatchnew true"; |
| 6099 |
e->usage = |
| 6100 |
"Usage: dialplan set extenpatternmatchnew true|false\n" |
| 6101 |
" Use the NEW extension pattern matching algorithm, true or false.\n"; |
| 6102 |
return NULL; |
| 6103 |
case CLI_GENERATE: |
| 6104 |
return NULL; |
| 6105 |
} |
| 6106 |
|
| 6107 |
if (a->argc != 4) |
| 6108 |
return CLI_SHOWUSAGE; |
| 6109 |
|
| 6110 |
oldval = pbx_set_extenpatternmatchnew(1); |
| 6111 |
|
| 6112 |
if (oldval) |
| 6113 |
ast_cli(a->fd, "\n -- Still using the NEW pattern match algorithm for extension names in the dialplan.\n"); |
| 6114 |
else |
| 6115 |
ast_cli(a->fd, "\n -- Switched to using the NEW pattern match algorithm for extension names in the dialplan.\n"); |
| 6116 |
|
| 6117 |
return CLI_SUCCESS; |
| 6118 |
} |
| 6119 |
|
| 6120 |
static char *handle_unset_extenpatternmatchnew(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 6121 |
{ |
| 6122 |
int oldval = 0; |
| 6123 |
|
| 6124 |
switch (cmd) { |
| 6125 |
case CLI_INIT: |
| 6126 |
e->command = "dialplan set extenpatternmatchnew false"; |
| 6127 |
e->usage = |
| 6128 |
"Usage: dialplan set extenpatternmatchnew true|false\n" |
| 6129 |
" Use the NEW extension pattern matching algorithm, true or false.\n"; |
| 6130 |
return NULL; |
| 6131 |
case CLI_GENERATE: |
| 6132 |
return NULL; |
| 6133 |
} |
| 6134 |
|
| 6135 |
if (a->argc != 4) |
| 6136 |
return CLI_SHOWUSAGE; |
| 6137 |
|
| 6138 |
oldval = pbx_set_extenpatternmatchnew(0); |
| 6139 |
|
| 6140 |
if (!oldval) |
| 6141 |
ast_cli(a->fd, "\n -- Still using the OLD pattern match algorithm for extension names in the dialplan.\n"); |
| 6142 |
else |
| 6143 |
ast_cli(a->fd, "\n -- Switched to using the OLD pattern match algorithm for extension names in the dialplan.\n"); |
| 6144 |
|
| 6145 |
return CLI_SUCCESS; |
| 6146 |
} |
| 6147 |
|
| 6148 |
|
| 6149 |
|
| 6150 |
|
| 6151 |
static struct ast_cli_entry pbx_cli[] = { |
| 6152 |
#if 0 |
| 6153 |
AST_CLI_DEFINE(handle_eat_memory, "Eats all available memory"), |
| 6154 |
#endif |
| 6155 |
AST_CLI_DEFINE(handle_show_hints, "Show dialplan hints"), |
| 6156 |
AST_CLI_DEFINE(handle_show_hint, "Show dialplan hint"), |
| 6157 |
#ifdef AST_DEVMODE |
| 6158 |
AST_CLI_DEFINE(handle_show_device2extenstate, "Show expected exten state from multiple device states"), |
| 6159 |
#endif |
| 6160 |
AST_CLI_DEFINE(handle_show_dialplan, "Show dialplan"), |
| 6161 |
AST_CLI_DEFINE(handle_debug_dialplan, "Show fast extension pattern matching data structures"), |
| 6162 |
AST_CLI_DEFINE(handle_unset_extenpatternmatchnew, "Use the Old extension pattern matching algorithm."), |
| 6163 |
AST_CLI_DEFINE(handle_set_extenpatternmatchnew, "Use the New extension pattern matching algorithm."), |
| 6164 |
}; |
| 6165 |
|
| 6166 |
void unreference_cached_app(struct ast_app *app) |
| 6167 |
{ |
| 6168 |
struct ast_context *context = NULL; |
| 6169 |
struct ast_exten *eroot = NULL, *e = NULL; |
| 6170 |
|
| 6171 |
ast_rdlock_contexts(); |
| 6172 |
while ((context = ast_walk_contexts(context))) { |
| 6173 |
while ((eroot = ast_walk_context_extensions(context, eroot))) { |
| 6174 |
while ((e = ast_walk_extension_priorities(eroot, e))) { |
| 6175 |
if (e->cached_app == app) |
| 6176 |
e->cached_app = NULL; |
| 6177 |
} |
| 6178 |
} |
| 6179 |
} |
| 6180 |
ast_unlock_contexts(); |
| 6181 |
|
| 6182 |
return; |
| 6183 |
} |
| 6184 |
|
| 6185 |
struct ast_context *ast_context_find_or_create(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *name, const char *registrar) |
| 6186 |
{ |
| 6187 |
struct ast_context *tmp, **local_contexts; |
| 6188 |
struct ast_context search = { |
| 6189 |
.name = name, |
| 6190 |
}; |
| 6191 |
size_t name_bytes = strlen(name); |
| 6192 |
size_t registrar_bytes = strlen(registrar); |
| 6193 |
int length = sizeof(struct ast_context) + name_bytes + registrar_bytes + 2; |
| 6194 |
|
| 6195 |
if (!contexts_table) { |
| 6196 |
|
| 6197 |
ast_wrlock_contexts(); |
| 6198 |
if (!contexts_table) { |
| 6199 |
contexts_table = ast_hashtab_create(17, |
| 6200 |
ast_hashtab_compare_contexts, |
| 6201 |
ast_hashtab_resize_java, |
| 6202 |
ast_hashtab_newsize_java, |
| 6203 |
ast_hashtab_hash_contexts, |
| 6204 |
0); |
| 6205 |
} |
| 6206 |
ast_unlock_contexts(); |
| 6207 |
} |
| 6208 |
|
| 6209 |
if (!extcontexts) { |
| 6210 |
ast_rdlock_contexts(); |
| 6211 |
local_contexts = &contexts; |
| 6212 |
tmp = ast_hashtab_lookup(contexts_table, &search); |
| 6213 |
if (tmp) { |
| 6214 |
tmp->refcount++; |
| 6215 |
ast_unlock_contexts(); |
| 6216 |
return tmp; |
| 6217 |
} |
| 6218 |
} else { |
| 6219 |
local_contexts = extcontexts; |
| 6220 |
tmp = ast_hashtab_lookup(exttable, &search); |
| 6221 |
if (tmp) { |
| 6222 |
tmp->refcount++; |
| 6223 |
return tmp; |
| 6224 |
} |
| 6225 |
} |
| 6226 |
|
| 6227 |
if ((tmp = ast_calloc(1, length))) { |
| 6228 |
ast_rwlock_init(&tmp->lock); |
| 6229 |
tmp->name = memcpy(&tmp->data[0], name, name_bytes); |
| 6230 |
tmp->registrar = memcpy(&tmp->data[name_bytes + 1], registrar, registrar_bytes); |
| 6231 |
tmp->root = NULL; |
| 6232 |
tmp->root_table = NULL; |
| 6233 |
AST_VECTOR_INIT(&tmp->includes, 0); |
| 6234 |
AST_VECTOR_INIT(&tmp->ignorepats, 0); |
| 6235 |
AST_VECTOR_INIT(&tmp->alts, 0); |
| 6236 |
tmp->refcount = 1; |
| 6237 |
|
| 6238 |
|
| 6239 |
|
| 6240 |
|
| 6241 |
ast_assert(tmp->name == &tmp->data[0]); |
| 6242 |
} else { |
| 6243 |
ast_log(LOG_ERROR, "Danger! We failed to allocate a context for %s!\n", name); |
| 6244 |
if (!extcontexts) { |
| 6245 |
ast_unlock_contexts(); |
| 6246 |
} |
| 6247 |
return NULL; |
| 6248 |
} |
| 6249 |
|
| 6250 |
if (!extcontexts) { |
| 6251 |
tmp->next = *local_contexts; |
| 6252 |
*local_contexts = tmp; |
| 6253 |
ast_hashtab_insert_safe(contexts_table, tmp); |
| 6254 |
ast_unlock_contexts(); |
| 6255 |
} else { |
| 6256 |
tmp->next = *local_contexts; |
| 6257 |
if (exttable) |
| 6258 |
ast_hashtab_insert_immediate(exttable, tmp); |
| 6259 |
|
| 6260 |
*local_contexts = tmp; |
| 6261 |
} |
| 6262 |
ast_debug(1, "Registered extension context '%s'; registrar: %s\n", tmp->name, registrar); |
| 6263 |
return tmp; |
| 6264 |
} |
| 6265 |
|
| 6266 |
void ast_context_set_autohints(struct ast_context *con, int enabled) |
| 6267 |
{ |
| 6268 |
con->autohints = enabled; |
| 6269 |
} |
| 6270 |
|
| 6271 |
void __ast_context_destroy(struct ast_context *list, struct ast_hashtab *contexttab, struct ast_context *con, const char *registrar); |
| 6272 |
|
| 6273 |
struct store_hint { |
| 6274 |
char *context; |
| 6275 |
char *exten; |
| 6276 |
AST_LIST_HEAD_NOLOCK(, ast_state_cb) callbacks; |
| 6277 |
int laststate; |
| 6278 |
int last_presence_state; |
| 6279 |
char *last_presence_subtype; |
| 6280 |
char *last_presence_message; |
| 6281 |
|
| 6282 |
AST_LIST_ENTRY(store_hint) list; |
| 6283 |
char data[0]; |
| 6284 |
}; |
| 6285 |
|
| 6286 |
AST_LIST_HEAD_NOLOCK(store_hints, store_hint); |
| 6287 |
|
| 6288 |
static void context_merge_incls_swits_igps_other_registrars(struct ast_context *new, struct ast_context *old, const char *registrar) |
| 6289 |
{ |
| 6290 |
int idx; |
| 6291 |
|
| 6292 |
ast_debug(1, "merging incls/swits/igpats from old(%s) to new(%s) context, registrar = %s\n", ast_get_context_name(old), ast_get_context_name(new), registrar); |
| 6293 |
|
| 6294 |
|
| 6295 |
for (idx = 0; idx < ast_context_includes_count(old); idx++) { |
| 6296 |
const struct ast_include *i = ast_context_includes_get(old, idx); |
| 6297 |
|
| 6298 |
if (!strcmp(ast_get_include_registrar(i), registrar)) { |
| 6299 |
continue; |
| 6300 |
} |
| 6301 |
ast_context_add_include2(new, ast_get_include_name(i), ast_get_include_registrar(i)); |
| 6302 |
} |
| 6303 |
|
| 6304 |
|
| 6305 |
for (idx = 0; idx < ast_context_switches_count(old); idx++) { |
| 6306 |
const struct ast_sw *sw = ast_context_switches_get(old, idx); |
| 6307 |
|
| 6308 |
if (!strcmp(ast_get_switch_registrar(sw), registrar)) { |
| 6309 |
continue; |
| 6310 |
} |
| 6311 |
ast_context_add_switch2(new, ast_get_switch_name(sw), ast_get_switch_data(sw), ast_get_switch_eval(sw), ast_get_switch_registrar(sw)); |
| 6312 |
} |
| 6313 |
|
| 6314 |
|
| 6315 |
for (idx = 0; idx < ast_context_ignorepats_count(old); idx++) { |
| 6316 |
const struct ast_ignorepat *ip = ast_context_ignorepats_get(old, idx); |
| 6317 |
|
| 6318 |
if (strcmp(ast_get_ignorepat_registrar(ip), registrar) == 0) { |
| 6319 |
continue; |
| 6320 |
} |
| 6321 |
ast_context_add_ignorepat2(new, ast_get_ignorepat_name(ip), ast_get_ignorepat_registrar(ip)); |
| 6322 |
} |
| 6323 |
} |
| 6324 |
|
| 6325 |
|
| 6326 |
static void context_table_create_autohints(struct ast_hashtab *table) |
| 6327 |
{ |
| 6328 |
struct ast_context *con; |
| 6329 |
struct ast_hashtab_iter *iter; |
| 6330 |
|
| 6331 |
|
| 6332 |
ao2_callback(autohints, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL); |
| 6333 |
|
| 6334 |
iter = ast_hashtab_start_traversal(table); |
| 6335 |
while ((con = ast_hashtab_next(iter))) { |
| 6336 |
size_t name_len = strlen(con->name) + 1; |
| 6337 |
size_t registrar_len = strlen(con->registrar) + 1; |
| 6338 |
struct ast_autohint *autohint; |
| 6339 |
|
| 6340 |
if (!con->autohints) { |
| 6341 |
continue; |
| 6342 |
} |
| 6343 |
|
| 6344 |
autohint = ao2_alloc_options(sizeof(*autohint) + name_len + registrar_len, NULL, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 6345 |
if (!autohint) { |
| 6346 |
continue; |
| 6347 |
} |
| 6348 |
|
| 6349 |
ast_copy_string(autohint->context, con->name, name_len); |
| 6350 |
autohint->registrar = autohint->context + name_len; |
| 6351 |
ast_copy_string(autohint->registrar, con->registrar, registrar_len); |
| 6352 |
|
| 6353 |
ao2_link(autohints, autohint); |
| 6354 |
ao2_ref(autohint, -1); |
| 6355 |
|
| 6356 |
ast_verb(3, "Enabled autohints support on context '%s'\n", con->name); |
| 6357 |
} |
| 6358 |
ast_hashtab_end_traversal(iter); |
| 6359 |
} |
| 6360 |
|
| 6361 |
|
| 6362 |
|
| 6363 |
static void context_merge(struct ast_context **extcontexts, struct ast_hashtab *exttable, struct ast_context *context, const char *registrar) |
| 6364 |
{ |
| 6365 |
struct ast_context *new = ast_hashtab_lookup(exttable, context); |
| 6366 |
struct ast_exten *exten_item, *prio_item, *new_exten_item, *new_prio_item; |
| 6367 |
struct ast_hashtab_iter *exten_iter; |
| 6368 |
struct ast_hashtab_iter *prio_iter; |
| 6369 |
int insert_count = 0; |
| 6370 |
int first = 1; |
| 6371 |
|
| 6372 |
|
| 6373 |
|
| 6374 |
|
| 6375 |
|
| 6376 |
|
| 6377 |
if (context->root_table) { |
| 6378 |
exten_iter = ast_hashtab_start_traversal(context->root_table); |
| 6379 |
while ((exten_item=ast_hashtab_next(exten_iter))) { |
| 6380 |
if (new) { |
| 6381 |
new_exten_item = ast_hashtab_lookup(new->root_table, exten_item); |
| 6382 |
} else { |
| 6383 |
new_exten_item = NULL; |
| 6384 |
} |
| 6385 |
prio_iter = ast_hashtab_start_traversal(exten_item->peer_table); |
| 6386 |
while ((prio_item=ast_hashtab_next(prio_iter))) { |
| 6387 |
int res1; |
| 6388 |
char *dupdstr; |
| 6389 |
|
| 6390 |
if (new_exten_item) { |
| 6391 |
new_prio_item = ast_hashtab_lookup(new_exten_item->peer_table, prio_item); |
| 6392 |
} else { |
| 6393 |
new_prio_item = NULL; |
| 6394 |
} |
| 6395 |
if (strcmp(prio_item->registrar,registrar) == 0) { |
| 6396 |
continue; |
| 6397 |
} |
| 6398 |
|
| 6399 |
if (!new) { |
| 6400 |
new = ast_context_find_or_create(extcontexts, exttable, context->name, prio_item->registrar); |
| 6401 |
if (new) { |
| 6402 |
new->autohints = context->autohints; |
| 6403 |
} |
| 6404 |
} |
| 6405 |
|
| 6406 |
|
| 6407 |
if (first) { |
| 6408 |
context_merge_incls_swits_igps_other_registrars(new, context, registrar); |
| 6409 |
first = 0; |
| 6410 |
} |
| 6411 |
|
| 6412 |
if (!new) { |
| 6413 |
ast_log(LOG_ERROR,"Could not allocate a new context for %s in merge_and_delete! Danger!\n", context->name); |
| 6414 |
ast_hashtab_end_traversal(prio_iter); |
| 6415 |
ast_hashtab_end_traversal(exten_iter); |
| 6416 |
return; |
| 6417 |
} |
| 6418 |
|
| 6419 |
|
| 6420 |
|
| 6421 |
dupdstr = ast_strdup(prio_item->data); |
| 6422 |
|
| 6423 |
res1 = ast_add_extension2(new, 0, prio_item->name, prio_item->priority, prio_item->label, |
| 6424 |
prio_item->matchcid ? prio_item->cidmatch : NULL, prio_item->app, dupdstr, ast_free_ptr, prio_item->registrar, |
| 6425 |
prio_item->registrar_file, prio_item->registrar_line); |
| 6426 |
if (!res1 && new_exten_item && new_prio_item){ |
| 6427 |
ast_verb(3,"Dropping old dialplan item %s/%s/%d [%s(%s)] (registrar=%s) due to conflict with new dialplan\n", |
| 6428 |
context->name, prio_item->name, prio_item->priority, prio_item->app, (char*)prio_item->data, prio_item->registrar); |
| 6429 |
} else { |
| 6430 |
|
| 6431 |
|
| 6432 |
insert_count++; |
| 6433 |
} |
| 6434 |
} |
| 6435 |
ast_hashtab_end_traversal(prio_iter); |
| 6436 |
} |
| 6437 |
ast_hashtab_end_traversal(exten_iter); |
| 6438 |
} else if (new) { |
| 6439 |
|
| 6440 |
|
| 6441 |
|
| 6442 |
context_merge_incls_swits_igps_other_registrars(new, context, registrar); |
| 6443 |
} |
| 6444 |
|
| 6445 |
if (!insert_count && !new && (strcmp(context->registrar, registrar) != 0 || |
| 6446 |
(strcmp(context->registrar, registrar) == 0 && context->refcount > 1))) { |
| 6447 |
|
| 6448 |
|
| 6449 |
new = ast_context_find_or_create(extcontexts, exttable, context->name, context->registrar); |
| 6450 |
|
| 6451 |
if (new) { |
| 6452 |
new->autohints = context->autohints; |
| 6453 |
} |
| 6454 |
|
| 6455 |
|
| 6456 |
context_merge_incls_swits_igps_other_registrars(new, context, registrar); |
| 6457 |
} |
| 6458 |
} |
| 6459 |
|
| 6460 |
|
| 6461 |
|
| 6462 |
void ast_merge_contexts_and_delete(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *registrar) |
| 6463 |
{ |
| 6464 |
double ft; |
| 6465 |
struct ast_context *tmp; |
| 6466 |
struct ast_context *oldcontextslist; |
| 6467 |
struct ast_hashtab *oldtable; |
| 6468 |
struct store_hints hints_stored = AST_LIST_HEAD_NOLOCK_INIT_VALUE; |
| 6469 |
struct store_hints hints_removed = AST_LIST_HEAD_NOLOCK_INIT_VALUE; |
| 6470 |
struct store_hint *saved_hint; |
| 6471 |
struct ast_hint *hint; |
| 6472 |
struct ast_exten *exten; |
| 6473 |
int length; |
| 6474 |
struct ast_state_cb *thiscb; |
| 6475 |
struct ast_hashtab_iter *iter; |
| 6476 |
struct ao2_iterator i; |
| 6477 |
int ctx_count = 0; |
| 6478 |
struct timeval begintime; |
| 6479 |
struct timeval writelocktime; |
| 6480 |
struct timeval endlocktime; |
| 6481 |
struct timeval enddeltime; |
| 6482 |
|
| 6483 |
|
| 6484 |
|
| 6485 |
|
| 6486 |
|
| 6487 |
|
| 6488 |
|
| 6489 |
|
| 6490 |
|
| 6491 |
|
| 6492 |
|
| 6493 |
|
| 6494 |
|
| 6495 |
begintime = ast_tvnow(); |
| 6496 |
ast_mutex_lock(&context_merge_lock); |
| 6497 |
ast_wrlock_contexts(); |
| 6498 |
|
| 6499 |
if (!contexts_table) { |
| 6500 |
|
| 6501 |
context_table_create_autohints(exttable); |
| 6502 |
|
| 6503 |
|
| 6504 |
contexts_table = exttable; |
| 6505 |
contexts = *extcontexts; |
| 6506 |
ast_unlock_contexts(); |
| 6507 |
ast_mutex_unlock(&context_merge_lock); |
| 6508 |
return; |
| 6509 |
} |
| 6510 |
|
| 6511 |
iter = ast_hashtab_start_traversal(contexts_table); |
| 6512 |
while ((tmp = ast_hashtab_next(iter))) { |
| 6513 |
++ctx_count; |
| 6514 |
context_merge(extcontexts, exttable, tmp, registrar); |
| 6515 |
} |
| 6516 |
ast_hashtab_end_traversal(iter); |
| 6517 |
|
| 6518 |
ao2_lock(hints); |
| 6519 |
writelocktime = ast_tvnow(); |
| 6520 |
|
| 6521 |
|
| 6522 |
i = ao2_iterator_init(hints, AO2_ITERATOR_DONTLOCK); |
| 6523 |
for (; (hint = ao2_iterator_next(&i)); ao2_ref(hint, -1)) { |
| 6524 |
if (ao2_container_count(hint->callbacks)) { |
| 6525 |
size_t exten_len; |
| 6526 |
|
| 6527 |
ao2_lock(hint); |
| 6528 |
if (!hint->exten) { |
| 6529 |
|
| 6530 |
ao2_unlock(hint); |
| 6531 |
continue; |
| 6532 |
} |
| 6533 |
|
| 6534 |
exten_len = strlen(hint->exten->exten) + 1; |
| 6535 |
length = exten_len + strlen(hint->exten->parent->name) + 1 |
| 6536 |
+ sizeof(*saved_hint); |
| 6537 |
if (!(saved_hint = ast_calloc(1, length))) { |
| 6538 |
ao2_unlock(hint); |
| 6539 |
continue; |
| 6540 |
} |
| 6541 |
|
| 6542 |
|
| 6543 |
while ((thiscb = ao2_callback(hint->callbacks, OBJ_UNLINK, NULL, NULL))) { |
| 6544 |
AST_LIST_INSERT_TAIL(&saved_hint->callbacks, thiscb, entry); |
| 6545 |
|
| 6546 |
|
| 6547 |
|
| 6548 |
|
| 6549 |
} |
| 6550 |
|
| 6551 |
saved_hint->laststate = hint->laststate; |
| 6552 |
saved_hint->context = saved_hint->data; |
| 6553 |
strcpy(saved_hint->data, hint->exten->parent->name); |
| 6554 |
saved_hint->exten = saved_hint->data + strlen(saved_hint->context) + 1; |
| 6555 |
ast_copy_string(saved_hint->exten, hint->exten->exten, exten_len); |
| 6556 |
if (hint->last_presence_subtype) { |
| 6557 |
saved_hint->last_presence_subtype = ast_strdup(hint->last_presence_subtype); |
| 6558 |
} |
| 6559 |
if (hint->last_presence_message) { |
| 6560 |
saved_hint->last_presence_message = ast_strdup(hint->last_presence_message); |
| 6561 |
} |
| 6562 |
saved_hint->last_presence_state = hint->last_presence_state; |
| 6563 |
ao2_unlock(hint); |
| 6564 |
AST_LIST_INSERT_HEAD(&hints_stored, saved_hint, list); |
| 6565 |
} |
| 6566 |
} |
| 6567 |
ao2_iterator_destroy(&i); |
| 6568 |
|
| 6569 |
|
| 6570 |
oldtable = contexts_table; |
| 6571 |
oldcontextslist = contexts; |
| 6572 |
|
| 6573 |
|
| 6574 |
contexts_table = exttable; |
| 6575 |
contexts = *extcontexts; |
| 6576 |
|
| 6577 |
|
| 6578 |
|
| 6579 |
|
| 6580 |
|
| 6581 |
while ((saved_hint = AST_LIST_REMOVE_HEAD(&hints_stored, list))) { |
| 6582 |
struct pbx_find_info q = { .stacklen = 0 }; |
| 6583 |
|
| 6584 |
exten = pbx_find_extension(NULL, NULL, &q, saved_hint->context, saved_hint->exten, |
| 6585 |
PRIORITY_HINT, NULL, "", E_MATCH); |
| 6586 |
|
| 6587 |
|
| 6588 |
|
| 6589 |
|
| 6590 |
|
| 6591 |
if (exten && exten->exten[0] == '_') { |
| 6592 |
ast_add_extension_nolock(exten->parent->name, 0, saved_hint->exten, |
| 6593 |
PRIORITY_HINT, NULL, 0, exten->app, ast_strdup(exten->data), ast_free_ptr, |
| 6594 |
exten->registrar); |
| 6595 |
|
| 6596 |
exten = ast_hint_extension_nolock(NULL, saved_hint->context, |
| 6597 |
saved_hint->exten); |
| 6598 |
} |
| 6599 |
|
| 6600 |
|
| 6601 |
hint = exten ? ao2_find(hints, exten, 0) : NULL; |
| 6602 |
if (!hint) { |
| 6603 |
|
| 6604 |
|
| 6605 |
|
| 6606 |
|
| 6607 |
AST_LIST_INSERT_HEAD(&hints_removed, saved_hint, list); |
| 6608 |
} else { |
| 6609 |
ao2_lock(hint); |
| 6610 |
while ((thiscb = AST_LIST_REMOVE_HEAD(&saved_hint->callbacks, entry))) { |
| 6611 |
ao2_link(hint->callbacks, thiscb); |
| 6612 |
|
| 6613 |
ao2_ref(thiscb, -1); |
| 6614 |
} |
| 6615 |
hint->laststate = saved_hint->laststate; |
| 6616 |
hint->last_presence_state = saved_hint->last_presence_state; |
| 6617 |
hint->last_presence_subtype = saved_hint->last_presence_subtype; |
| 6618 |
hint->last_presence_message = saved_hint->last_presence_message; |
| 6619 |
ao2_unlock(hint); |
| 6620 |
ao2_ref(hint, -1); |
| 6621 |
|
| 6622 |
|
| 6623 |
|
| 6624 |
|
| 6625 |
ast_free(saved_hint); |
| 6626 |
} |
| 6627 |
} |
| 6628 |
|
| 6629 |
|
| 6630 |
context_table_create_autohints(contexts_table); |
| 6631 |
|
| 6632 |
|
| 6633 |
|
| 6634 |
ctx_count = ast_hashtab_size(contexts_table); |
| 6635 |
|
| 6636 |
ao2_unlock(hints); |
| 6637 |
ast_unlock_contexts(); |
| 6638 |
|
| 6639 |
|
| 6640 |
|
| 6641 |
|
| 6642 |
|
| 6643 |
while ((saved_hint = AST_LIST_REMOVE_HEAD(&hints_removed, list))) { |
| 6644 |
|
| 6645 |
while ((thiscb = AST_LIST_REMOVE_HEAD(&saved_hint->callbacks, entry))) { |
| 6646 |
execute_state_callback(thiscb->change_cb, |
| 6647 |
saved_hint->context, |
| 6648 |
saved_hint->exten, |
| 6649 |
thiscb->data, |
| 6650 |
AST_HINT_UPDATE_DEVICE, |
| 6651 |
NULL, |
| 6652 |
NULL); |
| 6653 |
|
| 6654 |
ao2_ref(thiscb, -1); |
| 6655 |
} |
| 6656 |
ast_free(saved_hint->last_presence_subtype); |
| 6657 |
ast_free(saved_hint->last_presence_message); |
| 6658 |
ast_free(saved_hint); |
| 6659 |
} |
| 6660 |
|
| 6661 |
ast_mutex_unlock(&context_merge_lock); |
| 6662 |
endlocktime = ast_tvnow(); |
| 6663 |
|
| 6664 |
|
| 6665 |
|
| 6666 |
|
| 6667 |
|
| 6668 |
|
| 6669 |
|
| 6670 |
ast_hashtab_destroy(oldtable, NULL); |
| 6671 |
|
| 6672 |
for (tmp = oldcontextslist; tmp; ) { |
| 6673 |
struct ast_context *next; |
| 6674 |
|
| 6675 |
next = tmp->next; |
| 6676 |
__ast_internal_context_destroy(tmp); |
| 6677 |
tmp = next; |
| 6678 |
} |
| 6679 |
enddeltime = ast_tvnow(); |
| 6680 |
|
| 6681 |
ft = ast_tvdiff_us(writelocktime, begintime); |
| 6682 |
ft /= 1000000.0; |
| 6683 |
ast_verb(5,"Time to scan old dialplan and merge leftovers back into the new: %8.6f sec\n", ft); |
| 6684 |
|
| 6685 |
ft = ast_tvdiff_us(endlocktime, writelocktime); |
| 6686 |
ft /= 1000000.0; |
| 6687 |
ast_verb(5,"Time to restore hints and swap in new dialplan: %8.6f sec\n", ft); |
| 6688 |
|
| 6689 |
ft = ast_tvdiff_us(enddeltime, endlocktime); |
| 6690 |
ft /= 1000000.0; |
| 6691 |
ast_verb(5,"Time to delete the old dialplan: %8.6f sec\n", ft); |
| 6692 |
|
| 6693 |
ft = ast_tvdiff_us(enddeltime, begintime); |
| 6694 |
ft /= 1000000.0; |
| 6695 |
ast_verb(5,"Total time merge_contexts_delete: %8.6f sec\n", ft); |
| 6696 |
ast_verb(5, "%s successfully loaded %d contexts (enable debug for details).\n", registrar, ctx_count); |
| 6697 |
} |
| 6698 |
|
| 6699 |
|
| 6700 |
|
| 6701 |
|
| 6702 |
|
| 6703 |
|
| 6704 |
int ast_context_add_include(const char *context, const char *include, const char *registrar) |
| 6705 |
{ |
| 6706 |
int ret = -1; |
| 6707 |
struct ast_context *c; |
| 6708 |
|
| 6709 |
c = find_context_locked(context); |
| 6710 |
if (c) { |
| 6711 |
ret = ast_context_add_include2(c, include, registrar); |
| 6712 |
ast_unlock_contexts(); |
| 6713 |
} |
| 6714 |
return ret; |
| 6715 |
} |
| 6716 |
|
| 6717 |
|
| 6718 |
|
| 6719 |
|
| 6720 |
|
| 6721 |
|
| 6722 |
|
| 6723 |
|
| 6724 |
int ast_context_add_include2(struct ast_context *con, const char *value, |
| 6725 |
const char *registrar) |
| 6726 |
{ |
| 6727 |
struct ast_include *new_include; |
| 6728 |
int idx; |
| 6729 |
|
| 6730 |
|
| 6731 |
new_include = include_alloc(value, registrar); |
| 6732 |
if (!new_include) { |
| 6733 |
return -1; |
| 6734 |
} |
| 6735 |
|
| 6736 |
ast_wrlock_context(con); |
| 6737 |
|
| 6738 |
|
| 6739 |
for (idx = 0; idx < ast_context_includes_count(con); idx++) { |
| 6740 |
const struct ast_include *i = ast_context_includes_get(con, idx); |
| 6741 |
|
| 6742 |
if (!strcasecmp(ast_get_include_name(i), ast_get_include_name(new_include))) { |
| 6743 |
include_free(new_include); |
| 6744 |
ast_unlock_context(con); |
| 6745 |
errno = EEXIST; |
| 6746 |
return -1; |
| 6747 |
} |
| 6748 |
} |
| 6749 |
|
| 6750 |
|
| 6751 |
if (AST_VECTOR_APPEND(&con->includes, new_include)) { |
| 6752 |
include_free(new_include); |
| 6753 |
ast_unlock_context(con); |
| 6754 |
return -1; |
| 6755 |
} |
| 6756 |
ast_debug(1, "Including context '%s' in context '%s'\n", |
| 6757 |
ast_get_include_name(new_include), ast_get_context_name(con)); |
| 6758 |
|
| 6759 |
ast_unlock_context(con); |
| 6760 |
|
| 6761 |
return 0; |
| 6762 |
} |
| 6763 |
|
| 6764 |
|
| 6765 |
|
| 6766 |
|
| 6767 |
|
| 6768 |
|
| 6769 |
int ast_context_add_switch(const char *context, const char *sw, const char *data, int eval, const char *registrar) |
| 6770 |
{ |
| 6771 |
int ret = -1; |
| 6772 |
struct ast_context *c; |
| 6773 |
|
| 6774 |
c = find_context_locked(context); |
| 6775 |
if (c) { |
| 6776 |
ret = ast_context_add_switch2(c, sw, data, eval, registrar); |
| 6777 |
ast_unlock_contexts(); |
| 6778 |
} |
| 6779 |
return ret; |
| 6780 |
} |
| 6781 |
|
| 6782 |
|
| 6783 |
|
| 6784 |
|
| 6785 |
|
| 6786 |
|
| 6787 |
|
| 6788 |
|
| 6789 |
int ast_context_add_switch2(struct ast_context *con, const char *value, |
| 6790 |
const char *data, int eval, const char *registrar) |
| 6791 |
{ |
| 6792 |
int idx; |
| 6793 |
struct ast_sw *new_sw; |
| 6794 |
|
| 6795 |
|
| 6796 |
if (!(new_sw = sw_alloc(value, data, eval, registrar))) { |
| 6797 |
return -1; |
| 6798 |
} |
| 6799 |
|
| 6800 |
|
| 6801 |
ast_wrlock_context(con); |
| 6802 |
|
| 6803 |
|
| 6804 |
for (idx = 0; idx < ast_context_switches_count(con); idx++) { |
| 6805 |
const struct ast_sw *i = ast_context_switches_get(con, idx); |
| 6806 |
|
| 6807 |
if (!strcasecmp(ast_get_switch_name(i), ast_get_switch_name(new_sw)) && |
| 6808 |
!strcasecmp(ast_get_switch_data(i), ast_get_switch_data(new_sw))) { |
| 6809 |
sw_free(new_sw); |
| 6810 |
ast_unlock_context(con); |
| 6811 |
errno = EEXIST; |
| 6812 |
return -1; |
| 6813 |
} |
| 6814 |
} |
| 6815 |
|
| 6816 |
|
| 6817 |
if (AST_VECTOR_APPEND(&con->alts, new_sw)) { |
| 6818 |
sw_free(new_sw); |
| 6819 |
ast_unlock_context(con); |
| 6820 |
return -1; |
| 6821 |
} |
| 6822 |
|
| 6823 |
ast_verb(3, "Including switch '%s/%s' in context '%s'\n", |
| 6824 |
ast_get_switch_name(new_sw), ast_get_switch_data(new_sw), ast_get_context_name(con)); |
| 6825 |
|
| 6826 |
ast_unlock_context(con); |
| 6827 |
|
| 6828 |
return 0; |
| 6829 |
} |
| 6830 |
|
| 6831 |
|
| 6832 |
|
| 6833 |
|
| 6834 |
|
| 6835 |
int ast_context_remove_ignorepat(const char *context, const char *ignorepat, const char *registrar) |
| 6836 |
{ |
| 6837 |
int ret = -1; |
| 6838 |
struct ast_context *c; |
| 6839 |
|
| 6840 |
c = find_context_locked(context); |
| 6841 |
if (c) { |
| 6842 |
ret = ast_context_remove_ignorepat2(c, ignorepat, registrar); |
| 6843 |
ast_unlock_contexts(); |
| 6844 |
} |
| 6845 |
return ret; |
| 6846 |
} |
| 6847 |
|
| 6848 |
int ast_context_remove_ignorepat2(struct ast_context *con, const char *ignorepat, const char *registrar) |
| 6849 |
{ |
| 6850 |
int idx; |
| 6851 |
|
| 6852 |
ast_wrlock_context(con); |
| 6853 |
|
| 6854 |
for (idx = 0; idx < ast_context_ignorepats_count(con); idx++) { |
| 6855 |
struct ast_ignorepat *ip = AST_VECTOR_GET(&con->ignorepats, idx); |
| 6856 |
|
| 6857 |
if (!strcmp(ast_get_ignorepat_name(ip), ignorepat) && |
| 6858 |
(!registrar || (registrar == ast_get_ignorepat_registrar(ip)))) { |
| 6859 |
AST_VECTOR_REMOVE_ORDERED(&con->ignorepats, idx); |
| 6860 |
ignorepat_free(ip); |
| 6861 |
ast_unlock_context(con); |
| 6862 |
return 0; |
| 6863 |
} |
| 6864 |
} |
| 6865 |
|
| 6866 |
ast_unlock_context(con); |
| 6867 |
errno = EINVAL; |
| 6868 |
return -1; |
| 6869 |
} |
| 6870 |
|
| 6871 |
|
| 6872 |
|
| 6873 |
|
| 6874 |
|
| 6875 |
int ast_context_add_ignorepat(const char *context, const char *value, const char *registrar) |
| 6876 |
{ |
| 6877 |
int ret = -1; |
| 6878 |
struct ast_context *c; |
| 6879 |
|
| 6880 |
c = find_context_locked(context); |
| 6881 |
if (c) { |
| 6882 |
ret = ast_context_add_ignorepat2(c, value, registrar); |
| 6883 |
ast_unlock_contexts(); |
| 6884 |
} |
| 6885 |
return ret; |
| 6886 |
} |
| 6887 |
|
| 6888 |
int ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar) |
| 6889 |
{ |
| 6890 |
struct ast_ignorepat *ignorepat = ignorepat_alloc(value, registrar); |
| 6891 |
int idx; |
| 6892 |
|
| 6893 |
if (!ignorepat) { |
| 6894 |
return -1; |
| 6895 |
} |
| 6896 |
|
| 6897 |
ast_wrlock_context(con); |
| 6898 |
for (idx = 0; idx < ast_context_ignorepats_count(con); idx++) { |
| 6899 |
const struct ast_ignorepat *i = ast_context_ignorepats_get(con, idx); |
| 6900 |
|
| 6901 |
if (!strcasecmp(ast_get_ignorepat_name(i), value)) { |
| 6902 |
|
| 6903 |
ast_unlock_context(con); |
| 6904 |
ignorepat_free(ignorepat); |
| 6905 |
errno = EEXIST; |
| 6906 |
return -1; |
| 6907 |
} |
| 6908 |
} |
| 6909 |
if (AST_VECTOR_APPEND(&con->ignorepats, ignorepat)) { |
| 6910 |
ignorepat_free(ignorepat); |
| 6911 |
ast_unlock_context(con); |
| 6912 |
return -1; |
| 6913 |
} |
| 6914 |
ast_unlock_context(con); |
| 6915 |
|
| 6916 |
return 0; |
| 6917 |
} |
| 6918 |
|
| 6919 |
int ast_ignore_pattern(const char *context, const char *pattern) |
| 6920 |
{ |
| 6921 |
int ret = 0; |
| 6922 |
struct ast_context *con; |
| 6923 |
|
| 6924 |
ast_rdlock_contexts(); |
| 6925 |
con = ast_context_find(context); |
| 6926 |
if (con) { |
| 6927 |
int idx; |
| 6928 |
|
| 6929 |
for (idx = 0; idx < ast_context_ignorepats_count(con); idx++) { |
| 6930 |
const struct ast_ignorepat *pat = ast_context_ignorepats_get(con, idx); |
| 6931 |
|
| 6932 |
if (ast_extension_match(ast_get_ignorepat_name(pat), pattern)) { |
| 6933 |
ret = 1; |
| 6934 |
break; |
| 6935 |
} |
| 6936 |
} |
| 6937 |
} |
| 6938 |
ast_unlock_contexts(); |
| 6939 |
|
| 6940 |
return ret; |
| 6941 |
} |
| 6942 |
|
| 6943 |
|
| 6944 |
|
| 6945 |
|
| 6946 |
|
| 6947 |
|
| 6948 |
static int ast_add_extension_nolock(const char *context, int replace, const char *extension, |
| 6949 |
int priority, const char *label, const char *callerid, |
| 6950 |
const char *application, void *data, void (*datad)(void *), const char *registrar) |
| 6951 |
{ |
| 6952 |
int ret = -1; |
| 6953 |
struct ast_context *c; |
| 6954 |
|
| 6955 |
c = find_context(context); |
| 6956 |
if (c) { |
| 6957 |
ret = ast_add_extension2_lockopt(c, replace, extension, priority, label, callerid, |
| 6958 |
application, data, datad, registrar, NULL, 0, 1); |
| 6959 |
} |
| 6960 |
|
| 6961 |
return ret; |
| 6962 |
} |
| 6963 |
|
| 6964 |
|
| 6965 |
|
| 6966 |
|
| 6967 |
|
| 6968 |
int ast_add_extension(const char *context, int replace, const char *extension, |
| 6969 |
int priority, const char *label, const char *callerid, |
| 6970 |
const char *application, void *data, void (*datad)(void *), const char *registrar) |
| 6971 |
{ |
| 6972 |
int ret = -1; |
| 6973 |
struct ast_context *c; |
| 6974 |
|
| 6975 |
c = find_context_locked(context); |
| 6976 |
if (c) { |
| 6977 |
ret = ast_add_extension2(c, replace, extension, priority, label, callerid, |
| 6978 |
application, data, datad, registrar, NULL, 0); |
| 6979 |
ast_unlock_contexts(); |
| 6980 |
} |
| 6981 |
|
| 6982 |
return ret; |
| 6983 |
} |
| 6984 |
|
| 6985 |
int ast_explicit_goto(struct ast_channel *chan, const char *context, const char *exten, int priority) |
| 6986 |
{ |
| 6987 |
if (!chan) |
| 6988 |
return -1; |
| 6989 |
|
| 6990 |
ast_channel_lock(chan); |
| 6991 |
|
| 6992 |
if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) { |
| 6993 |
ast_channel_unlock(chan); |
| 6994 |
return -1; |
| 6995 |
} |
| 6996 |
if (!ast_strlen_zero(context)) |
| 6997 |
ast_channel_context_set(chan, context); |
| 6998 |
if (!ast_strlen_zero(exten)) |
| 6999 |
ast_channel_exten_set(chan, exten); |
| 7000 |
if (priority > -1) { |
| 7001 |
|
| 7002 |
if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP)) { |
| 7003 |
--priority; |
| 7004 |
} |
| 7005 |
ast_channel_priority_set(chan, priority); |
| 7006 |
} |
| 7007 |
|
| 7008 |
ast_channel_unlock(chan); |
| 7009 |
|
| 7010 |
return 0; |
| 7011 |
} |
| 7012 |
|
| 7013 |
int ast_async_goto(struct ast_channel *chan, const char *context, const char *exten, int priority) |
| 7014 |
{ |
| 7015 |
struct ast_channel *newchan; |
| 7016 |
|
| 7017 |
ast_channel_lock(chan); |
| 7018 |
|
| 7019 |
if (ast_channel_is_bridged(chan) || ast_channel_pbx(chan)) { |
| 7020 |
if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP)) { |
| 7021 |
priority += 1; |
| 7022 |
} |
| 7023 |
ast_explicit_goto(chan, context, exten, priority); |
| 7024 |
ast_softhangup_nolock(chan, AST_SOFTHANGUP_ASYNCGOTO); |
| 7025 |
ast_channel_unlock(chan); |
| 7026 |
return 0; |
| 7027 |
} |
| 7028 |
ast_channel_unlock(chan); |
| 7029 |
|
| 7030 |
|
| 7031 |
newchan = ast_channel_yank(chan); |
| 7032 |
if (!newchan) { |
| 7033 |
ast_log(LOG_WARNING, "Unable to gain control of channel %s\n", ast_channel_name(chan)); |
| 7034 |
return -1; |
| 7035 |
} |
| 7036 |
ast_explicit_goto(newchan, context, exten, priority); |
| 7037 |
if (ast_pbx_start(newchan)) { |
| 7038 |
ast_hangup(newchan); |
| 7039 |
ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(newchan)); |
| 7040 |
return -1; |
| 7041 |
} |
| 7042 |
|
| 7043 |
return 0; |
| 7044 |
} |
| 7045 |
|
| 7046 |
int ast_async_goto_by_name(const char *channame, const char *context, const char *exten, int priority) |
| 7047 |
{ |
| 7048 |
struct ast_channel *chan; |
| 7049 |
int res = -1; |
| 7050 |
|
| 7051 |
if ((chan = ast_channel_get_by_name(channame))) { |
| 7052 |
res = ast_async_goto(chan, context, exten, priority); |
| 7053 |
chan = ast_channel_unref(chan); |
| 7054 |
} |
| 7055 |
|
| 7056 |
return res; |
| 7057 |
} |
| 7058 |
|
| 7059 |
|
| 7060 |
|
| 7061 |
|
| 7062 |
|
| 7063 |
|
| 7064 |
|
| 7065 |
|
| 7066 |
|
| 7067 |
|
| 7068 |
|
| 7069 |
|
| 7070 |
static unsigned int ext_strncpy(char *dst, const char *src, size_t dst_size, int nofluff) |
| 7071 |
{ |
| 7072 |
unsigned int count; |
| 7073 |
unsigned int insquares; |
| 7074 |
unsigned int is_pattern; |
| 7075 |
|
| 7076 |
if (!dst_size--) { |
| 7077 |
|
| 7078 |
return 0; |
| 7079 |
} |
| 7080 |
|
| 7081 |
count = 0; |
| 7082 |
insquares = 0; |
| 7083 |
is_pattern = *src == '_'; |
| 7084 |
while (*src && count < dst_size) { |
| 7085 |
if (*src == '[') { |
| 7086 |
if (is_pattern) { |
| 7087 |
insquares = 1; |
| 7088 |
} |
| 7089 |
} else if (*src == ']') { |
| 7090 |
insquares = 0; |
| 7091 |
} else if (*src == ' ' && !insquares) { |
| 7092 |
++src; |
| 7093 |
continue; |
| 7094 |
} else if (*src == '-' && !insquares && nofluff) { |
| 7095 |
++src; |
| 7096 |
continue; |
| 7097 |
} |
| 7098 |
*dst++ = *src++; |
| 7099 |
++count; |
| 7100 |
} |
| 7101 |
*dst = '\0'; |
| 7102 |
|
| 7103 |
return count + 1; |
| 7104 |
} |
| 7105 |
|
| 7106 |
|
| 7107 |
|
| 7108 |
|
| 7109 |
|
| 7110 |
|
| 7111 |
static int add_priority(struct ast_context *con, struct ast_exten *tmp, |
| 7112 |
struct ast_exten *el, struct ast_exten *e, int replace) |
| 7113 |
{ |
| 7114 |
struct ast_exten *ep; |
| 7115 |
struct ast_exten *eh=e; |
| 7116 |
int repeated_label = 0; |
| 7117 |
|
| 7118 |
for (ep = NULL; e ; ep = e, e = e->peer) { |
| 7119 |
if (e->label && tmp->label && e->priority != tmp->priority && !strcmp(e->label, tmp->label)) { |
| 7120 |
if (strcmp(e->name, tmp->name)) { |
| 7121 |
ast_log(LOG_WARNING, |
| 7122 |
"Extension '%s' priority %d in '%s', label '%s' already in use at aliased extension '%s' priority %d\n", |
| 7123 |
tmp->name, tmp->priority, con->name, tmp->label, e->name, e->priority); |
| 7124 |
} else { |
| 7125 |
ast_log(LOG_WARNING, |
| 7126 |
"Extension '%s' priority %d in '%s', label '%s' already in use at priority %d\n", |
| 7127 |
tmp->name, tmp->priority, con->name, tmp->label, e->priority); |
| 7128 |
} |
| 7129 |
repeated_label = 1; |
| 7130 |
} |
| 7131 |
if (e->priority >= tmp->priority) { |
| 7132 |
break; |
| 7133 |
} |
| 7134 |
} |
| 7135 |
|
| 7136 |
if (repeated_label) { |
| 7137 |
tmp->label = NULL; |
| 7138 |
} |
| 7139 |
|
| 7140 |
if (!e) { |
| 7141 |
ast_hashtab_insert_safe(eh->peer_table, tmp); |
| 7142 |
|
| 7143 |
if (tmp->label) { |
| 7144 |
ast_hashtab_insert_safe(eh->peer_label_table, tmp); |
| 7145 |
} |
| 7146 |
ep->peer = tmp; |
| 7147 |
return 0; |
| 7148 |
} |
| 7149 |
if (e->priority == tmp->priority) { |
| 7150 |
|
| 7151 |
|
| 7152 |
if (!replace) { |
| 7153 |
if (strcmp(e->name, tmp->name)) { |
| 7154 |
ast_log(LOG_WARNING, |
| 7155 |
"Unable to register extension '%s' priority %d in '%s', already in use by aliased extension '%s'\n", |
| 7156 |
tmp->name, tmp->priority, con->name, e->name); |
| 7157 |
} else { |
| 7158 |
ast_log(LOG_WARNING, |
| 7159 |
"Unable to register extension '%s' priority %d in '%s', already in use\n", |
| 7160 |
tmp->name, tmp->priority, con->name); |
| 7161 |
} |
| 7162 |
|
| 7163 |
return -1; |
| 7164 |
} |
| 7165 |
|
| 7166 |
|
| 7167 |
|
| 7168 |
tmp->next = e->next; |
| 7169 |
tmp->peer = e->peer; |
| 7170 |
if (ep) { |
| 7171 |
ast_hashtab_remove_object_via_lookup(eh->peer_table,e); |
| 7172 |
|
| 7173 |
if (e->label) { |
| 7174 |
ast_hashtab_remove_object_via_lookup(eh->peer_label_table,e); |
| 7175 |
} |
| 7176 |
|
| 7177 |
ast_hashtab_insert_safe(eh->peer_table,tmp); |
| 7178 |
if (tmp->label) { |
| 7179 |
ast_hashtab_insert_safe(eh->peer_label_table,tmp); |
| 7180 |
} |
| 7181 |
|
| 7182 |
ep->peer = tmp; |
| 7183 |
} else if (el) { |
| 7184 |
struct match_char *x = add_exten_to_pattern_tree(con, e, 1); |
| 7185 |
tmp->peer_table = e->peer_table; |
| 7186 |
tmp->peer_label_table = e->peer_label_table; |
| 7187 |
ast_hashtab_remove_object_via_lookup(tmp->peer_table,e); |
| 7188 |
ast_hashtab_insert_safe(tmp->peer_table,tmp); |
| 7189 |
if (e->label) { |
| 7190 |
ast_hashtab_remove_object_via_lookup(tmp->peer_label_table, e); |
| 7191 |
} |
| 7192 |
if (tmp->label) { |
| 7193 |
ast_hashtab_insert_safe(tmp->peer_label_table, tmp); |
| 7194 |
} |
| 7195 |
|
| 7196 |
ast_hashtab_remove_object_via_lookup(con->root_table, e); |
| 7197 |
ast_hashtab_insert_safe(con->root_table, tmp); |
| 7198 |
el->next = tmp; |
| 7199 |
|
| 7200 |
|
| 7201 |
if (x) { |
| 7202 |
if (x->exten) { |
| 7203 |
x->exten = tmp; |
| 7204 |
} else { |
| 7205 |
ast_log(LOG_ERROR,"Trying to delete an exten from a context, but the pattern tree node returned isn't an extension\n"); |
| 7206 |
} |
| 7207 |
} |
| 7208 |
} else { |
| 7209 |
struct match_char *x = add_exten_to_pattern_tree(con, e, 1); |
| 7210 |
ast_hashtab_remove_object_via_lookup(con->root_table, e); |
| 7211 |
ast_hashtab_insert_safe(con->root_table, tmp); |
| 7212 |
tmp->peer_table = e->peer_table; |
| 7213 |
tmp->peer_label_table = e->peer_label_table; |
| 7214 |
ast_hashtab_remove_object_via_lookup(tmp->peer_table, e); |
| 7215 |
ast_hashtab_insert_safe(tmp->peer_table, tmp); |
| 7216 |
if (e->label) { |
| 7217 |
ast_hashtab_remove_object_via_lookup(tmp->peer_label_table, e); |
| 7218 |
} |
| 7219 |
if (tmp->label) { |
| 7220 |
ast_hashtab_insert_safe(tmp->peer_label_table, tmp); |
| 7221 |
} |
| 7222 |
|
| 7223 |
ast_hashtab_remove_object_via_lookup(con->root_table, e); |
| 7224 |
ast_hashtab_insert_safe(con->root_table, tmp); |
| 7225 |
con->root = tmp; |
| 7226 |
|
| 7227 |
|
| 7228 |
if (x) { |
| 7229 |
if (x->exten) { |
| 7230 |
x->exten = tmp; |
| 7231 |
} else { |
| 7232 |
ast_log(LOG_ERROR,"Trying to delete an exten from a context, but the pattern tree node returned isn't an extension\n"); |
| 7233 |
} |
| 7234 |
} |
| 7235 |
} |
| 7236 |
if (tmp->priority == PRIORITY_HINT) |
| 7237 |
ast_change_hint(e,tmp); |
| 7238 |
|
| 7239 |
if (e->datad) |
| 7240 |
e->datad(e->data); |
| 7241 |
ast_free(e); |
| 7242 |
} else { |
| 7243 |
tmp->peer = e; |
| 7244 |
tmp->next = e->next; |
| 7245 |
if (ep) { |
| 7246 |
if (tmp->label) { |
| 7247 |
ast_hashtab_insert_safe(eh->peer_label_table, tmp); |
| 7248 |
} |
| 7249 |
ast_hashtab_insert_safe(eh->peer_table, tmp); |
| 7250 |
ep->peer = tmp; |
| 7251 |
} else { |
| 7252 |
tmp->peer_table = e->peer_table; |
| 7253 |
tmp->peer_label_table = e->peer_label_table; |
| 7254 |
e->peer_table = 0; |
| 7255 |
e->peer_label_table = 0; |
| 7256 |
ast_hashtab_insert_safe(tmp->peer_table, tmp); |
| 7257 |
if (tmp->label) { |
| 7258 |
ast_hashtab_insert_safe(tmp->peer_label_table, tmp); |
| 7259 |
} |
| 7260 |
ast_hashtab_remove_object_via_lookup(con->root_table, e); |
| 7261 |
ast_hashtab_insert_safe(con->root_table, tmp); |
| 7262 |
if (el) |
| 7263 |
el->next = tmp; |
| 7264 |
else |
| 7265 |
con->root = tmp; |
| 7266 |
e->next = NULL; |
| 7267 |
} |
| 7268 |
|
| 7269 |
if (tmp->priority == PRIORITY_HINT) { |
| 7270 |
ast_add_hint(tmp); |
| 7271 |
} |
| 7272 |
} |
| 7273 |
return 0; |
| 7274 |
} |
| 7275 |
|
| 7276 |
|
| 7277 |
|
| 7278 |
|
| 7279 |
|
| 7280 |
|
| 7281 |
|
| 7282 |
|
| 7283 |
|
| 7284 |
|
| 7285 |
|
| 7286 |
|
| 7287 |
|
| 7288 |
|
| 7289 |
|
| 7290 |
|
| 7291 |
|
| 7292 |
|
| 7293 |
|
| 7294 |
|
| 7295 |
|
| 7296 |
|
| 7297 |
|
| 7298 |
|
| 7299 |
|
| 7300 |
|
| 7301 |
int ast_add_extension2(struct ast_context *con, |
| 7302 |
int replace, const char *extension, int priority, const char *label, const char *callerid, |
| 7303 |
const char *application, void *data, void (*datad)(void *), |
| 7304 |
const char *registrar, const char *registrar_file, int registrar_line) |
| 7305 |
{ |
| 7306 |
return ast_add_extension2_lockopt(con, replace, extension, priority, label, callerid, |
| 7307 |
application, data, datad, registrar, registrar_file, registrar_line, 1); |
| 7308 |
} |
| 7309 |
|
| 7310 |
int ast_add_extension2_nolock(struct ast_context *con, |
| 7311 |
int replace, const char *extension, int priority, const char *label, const char *callerid, |
| 7312 |
const char *application, void *data, void (*datad)(void *), |
| 7313 |
const char *registrar, const char *registrar_file, int registrar_line) |
| 7314 |
{ |
| 7315 |
return ast_add_extension2_lockopt(con, replace, extension, priority, label, callerid, |
| 7316 |
application, data, datad, registrar, registrar_file, registrar_line, 0); |
| 7317 |
} |
| 7318 |
|
| 7319 |
|
| 7320 |
|
| 7321 |
|
| 7322 |
|
| 7323 |
|
| 7324 |
|
| 7325 |
|
| 7326 |
|
| 7327 |
static int ast_add_extension2_lockopt(struct ast_context *con, |
| 7328 |
int replace, const char *extension, int priority, const char *label, const char *callerid, |
| 7329 |
const char *application, void *data, void (*datad)(void *), |
| 7330 |
const char *registrar, const char *registrar_file, int registrar_line, int lock_context) |
| 7331 |
{ |
| 7332 |
|
| 7333 |
|
| 7334 |
|
| 7335 |
|
| 7336 |
|
| 7337 |
|
| 7338 |
struct ast_exten *tmp, *tmp2, *e, *el = NULL; |
| 7339 |
int res; |
| 7340 |
int length; |
| 7341 |
char *p; |
| 7342 |
char expand_buf[VAR_BUF_SIZE]; |
| 7343 |
struct ast_exten dummy_exten = {0}; |
| 7344 |
char dummy_name[1024]; |
| 7345 |
int exten_fluff; |
| 7346 |
int callerid_fluff; |
| 7347 |
|
| 7348 |
if (ast_strlen_zero(extension)) { |
| 7349 |
ast_log(LOG_ERROR,"You have to be kidding-- add exten '' to context %s? Figure out a name and call me back. Action ignored.\n", |
| 7350 |
con->name); |
| 7351 |
|
| 7352 |
if (datad) { |
| 7353 |
datad(data); |
| 7354 |
} |
| 7355 |
return -1; |
| 7356 |
} |
| 7357 |
|
| 7358 |
|
| 7359 |
if (priority == PRIORITY_HINT && strstr(application, "${") && extension[0] != '_') { |
| 7360 |
int inhibited; |
| 7361 |
struct ast_channel *c = ast_dummy_channel_alloc(); |
| 7362 |
|
| 7363 |
if (c) { |
| 7364 |
ast_channel_exten_set(c, extension); |
| 7365 |
ast_channel_context_set(c, con->name); |
| 7366 |
} |
| 7367 |
|
| 7368 |
|
| 7369 |
|
| 7370 |
|
| 7371 |
|
| 7372 |
|
| 7373 |
inhibited = ast_thread_inhibit_escalations_swap(0); |
| 7374 |
pbx_substitute_variables_helper(c, application, expand_buf, sizeof(expand_buf)); |
| 7375 |
if (0 < inhibited) { |
| 7376 |
ast_thread_inhibit_escalations(); |
| 7377 |
} |
| 7378 |
|
| 7379 |
application = expand_buf; |
| 7380 |
if (c) { |
| 7381 |
ast_channel_unref(c); |
| 7382 |
} |
| 7383 |
} |
| 7384 |
|
| 7385 |
if (priority == PRIORITY_HINT) { |
| 7386 |
|
| 7387 |
|
| 7388 |
exten_fluff = 0; |
| 7389 |
} else { |
| 7390 |
exten_fluff = ext_fluff_count(extension); |
| 7391 |
} |
| 7392 |
|
| 7393 |
callerid_fluff = callerid ? ext_fluff_count(callerid) : 0; |
| 7394 |
|
| 7395 |
length = sizeof(struct ast_exten); |
| 7396 |
length += strlen(extension) + 1; |
| 7397 |
if (exten_fluff) { |
| 7398 |
length += strlen(extension) + 1 - exten_fluff; |
| 7399 |
} |
| 7400 |
length += strlen(application) + 1; |
| 7401 |
if (label) { |
| 7402 |
length += strlen(label) + 1; |
| 7403 |
} |
| 7404 |
if (callerid) { |
| 7405 |
length += strlen(callerid) + 1; |
| 7406 |
if (callerid_fluff) { |
| 7407 |
length += strlen(callerid) + 1 - callerid_fluff; |
| 7408 |
} |
| 7409 |
} else { |
| 7410 |
length ++; |
| 7411 |
} |
| 7412 |
if (registrar_file) { |
| 7413 |
length += strlen(registrar_file) + 1; |
| 7414 |
} |
| 7415 |
|
| 7416 |
|
| 7417 |
tmp = ast_calloc(1, length); |
| 7418 |
if (!tmp) { |
| 7419 |
|
| 7420 |
if (datad) { |
| 7421 |
datad(data); |
| 7422 |
} |
| 7423 |
return -1; |
| 7424 |
} |
| 7425 |
|
| 7426 |
if (ast_strlen_zero(label)) |
| 7427 |
label = 0; |
| 7428 |
|
| 7429 |
|
| 7430 |
p = tmp->stuff; |
| 7431 |
if (label) { |
| 7432 |
tmp->label = p; |
| 7433 |
strcpy(p, label); |
| 7434 |
p += strlen(label) + 1; |
| 7435 |
} |
| 7436 |
tmp->name = p; |
| 7437 |
p += ext_strncpy(p, extension, strlen(extension) + 1, 0); |
| 7438 |
if (exten_fluff) { |
| 7439 |
tmp->exten = p; |
| 7440 |
p += ext_strncpy(p, extension, strlen(extension) + 1 - exten_fluff, 1); |
| 7441 |
} else { |
| 7442 |
|
| 7443 |
tmp->exten = tmp->name; |
| 7444 |
} |
| 7445 |
tmp->priority = priority; |
| 7446 |
tmp->cidmatch_display = tmp->cidmatch = p; |
| 7447 |
|
| 7448 |
|
| 7449 |
if (callerid) { |
| 7450 |
p += ext_strncpy(p, callerid, strlen(callerid) + 1, 0); |
| 7451 |
if (callerid_fluff) { |
| 7452 |
tmp->cidmatch = p; |
| 7453 |
p += ext_strncpy(p, callerid, strlen(callerid) + 1 - callerid_fluff, 1); |
| 7454 |
} |
| 7455 |
tmp->matchcid = AST_EXT_MATCHCID_ON; |
| 7456 |
} else { |
| 7457 |
*p++ = '\0'; |
| 7458 |
tmp->matchcid = AST_EXT_MATCHCID_OFF; |
| 7459 |
} |
| 7460 |
|
| 7461 |
if (registrar_file) { |
| 7462 |
tmp->registrar_file = p; |
| 7463 |
strcpy(p, registrar_file); |
| 7464 |
p += strlen(registrar_file) + 1; |
| 7465 |
} else { |
| 7466 |
tmp->registrar_file = NULL; |
| 7467 |
} |
| 7468 |
|
| 7469 |
tmp->app = p; |
| 7470 |
strcpy(p, application); |
| 7471 |
tmp->parent = con; |
| 7472 |
tmp->data = data; |
| 7473 |
tmp->datad = datad; |
| 7474 |
tmp->registrar = registrar; |
| 7475 |
tmp->registrar_line = registrar_line; |
| 7476 |
|
| 7477 |
if (lock_context) { |
| 7478 |
ast_wrlock_context(con); |
| 7479 |
} |
| 7480 |
|
| 7481 |
if (con->pattern_tree) { |
| 7482 |
|
| 7483 |
ext_strncpy(dummy_name, tmp->exten, sizeof(dummy_name), 1); |
| 7484 |
dummy_exten.exten = dummy_name; |
| 7485 |
dummy_exten.matchcid = AST_EXT_MATCHCID_OFF; |
| 7486 |
dummy_exten.cidmatch = 0; |
| 7487 |
tmp2 = ast_hashtab_lookup(con->root_table, &dummy_exten); |
| 7488 |
if (!tmp2) { |
| 7489 |
|
| 7490 |
add_exten_to_pattern_tree(con, tmp, 0); |
| 7491 |
ast_hashtab_insert_safe(con->root_table, tmp); |
| 7492 |
} |
| 7493 |
} |
| 7494 |
res = 0; |
| 7495 |
for (e = con->root; e; el = e, e = e->next) { |
| 7496 |
res = ext_cmp(e->exten, tmp->exten); |
| 7497 |
if (res == 0) { |
| 7498 |
if (e->matchcid == AST_EXT_MATCHCID_OFF && tmp->matchcid == AST_EXT_MATCHCID_OFF) |
| 7499 |
res = 0; |
| 7500 |
else if (tmp->matchcid == AST_EXT_MATCHCID_ON && e->matchcid == AST_EXT_MATCHCID_OFF) |
| 7501 |
res = 1; |
| 7502 |
else if (e->matchcid == AST_EXT_MATCHCID_ON && tmp->matchcid == AST_EXT_MATCHCID_OFF) |
| 7503 |
res = -1; |
| 7504 |
else |
| 7505 |
res = ext_cmp(e->cidmatch, tmp->cidmatch); |
| 7506 |
} |
| 7507 |
if (res >= 0) |
| 7508 |
break; |
| 7509 |
} |
| 7510 |
if (e && res == 0) { |
| 7511 |
res = add_priority(con, tmp, el, e, replace); |
| 7512 |
if (res < 0) { |
| 7513 |
if (con->pattern_tree) { |
| 7514 |
struct match_char *x = add_exten_to_pattern_tree(con, tmp, 1); |
| 7515 |
|
| 7516 |
if (x->exten) { |
| 7517 |
x->deleted = 1; |
| 7518 |
x->exten = 0; |
| 7519 |
} |
| 7520 |
|
| 7521 |
ast_hashtab_remove_this_object(con->root_table, tmp); |
| 7522 |
} |
| 7523 |
|
| 7524 |
if (tmp->datad) { |
| 7525 |
tmp->datad(tmp->data); |
| 7526 |
|
| 7527 |
tmp->data = NULL; |
| 7528 |
} |
| 7529 |
|
| 7530 |
ast_free(tmp); |
| 7531 |
} |
| 7532 |
if (lock_context) { |
| 7533 |
ast_unlock_context(con); |
| 7534 |
} |
| 7535 |
if (res < 0) { |
| 7536 |
errno = EEXIST; |
| 7537 |
return -1; |
| 7538 |
} |
| 7539 |
} else { |
| 7540 |
|
| 7541 |
|
| 7542 |
|
| 7543 |
|
| 7544 |
tmp->next = e; |
| 7545 |
tmp->peer_table = ast_hashtab_create(13, |
| 7546 |
hashtab_compare_exten_numbers, |
| 7547 |
ast_hashtab_resize_java, |
| 7548 |
ast_hashtab_newsize_java, |
| 7549 |
hashtab_hash_priority, |
| 7550 |
0); |
| 7551 |
tmp->peer_label_table = ast_hashtab_create(7, |
| 7552 |
hashtab_compare_exten_labels, |
| 7553 |
ast_hashtab_resize_java, |
| 7554 |
ast_hashtab_newsize_java, |
| 7555 |
hashtab_hash_labels, |
| 7556 |
0); |
| 7557 |
|
| 7558 |
if (el) { |
| 7559 |
el->next = tmp; |
| 7560 |
} else { |
| 7561 |
if (!con->root_table) { |
| 7562 |
con->root_table = ast_hashtab_create(27, |
| 7563 |
hashtab_compare_extens, |
| 7564 |
ast_hashtab_resize_java, |
| 7565 |
ast_hashtab_newsize_java, |
| 7566 |
hashtab_hash_extens, |
| 7567 |
0); |
| 7568 |
} |
| 7569 |
con->root = tmp; |
| 7570 |
} |
| 7571 |
if (label) { |
| 7572 |
ast_hashtab_insert_safe(tmp->peer_label_table, tmp); |
| 7573 |
} |
| 7574 |
ast_hashtab_insert_safe(tmp->peer_table, tmp); |
| 7575 |
ast_hashtab_insert_safe(con->root_table, tmp); |
| 7576 |
|
| 7577 |
if (lock_context) { |
| 7578 |
ast_unlock_context(con); |
| 7579 |
} |
| 7580 |
if (tmp->priority == PRIORITY_HINT) { |
| 7581 |
ast_add_hint(tmp); |
| 7582 |
} |
| 7583 |
} |
| 7584 |
if (DEBUG_ATLEAST(1)) { |
| 7585 |
if (tmp->matchcid == AST_EXT_MATCHCID_ON) { |
| 7586 |
ast_log(LOG_DEBUG, "Added extension '%s' priority %d (CID match '%s') to %s (%p)\n", |
| 7587 |
tmp->name, tmp->priority, tmp->cidmatch_display, con->name, con); |
| 7588 |
} else { |
| 7589 |
ast_log(LOG_DEBUG, "Added extension '%s' priority %d to %s (%p)\n", |
| 7590 |
tmp->name, tmp->priority, con->name, con); |
| 7591 |
} |
| 7592 |
} |
| 7593 |
|
| 7594 |
return 0; |
| 7595 |
} |
| 7596 |
|
| 7597 |
|
| 7598 |
struct pbx_outgoing { |
| 7599 |
|
| 7600 |
struct ast_dial *dial; |
| 7601 |
|
| 7602 |
ast_cond_t cond; |
| 7603 |
|
| 7604 |
char app[AST_MAX_APP]; |
| 7605 |
|
| 7606 |
char *appdata; |
| 7607 |
|
| 7608 |
char context[AST_MAX_CONTEXT]; |
| 7609 |
|
| 7610 |
char exten[AST_MAX_EXTENSION]; |
| 7611 |
|
| 7612 |
int priority; |
| 7613 |
|
| 7614 |
int dial_res; |
| 7615 |
|
| 7616 |
unsigned int dialed:1; |
| 7617 |
|
| 7618 |
unsigned int in_separate_thread:1; |
| 7619 |
}; |
| 7620 |
|
| 7621 |
|
| 7622 |
static void pbx_outgoing_destroy(void *obj) |
| 7623 |
{ |
| 7624 |
struct pbx_outgoing *outgoing = obj; |
| 7625 |
|
| 7626 |
if (outgoing->dial) { |
| 7627 |
ast_dial_destroy(outgoing->dial); |
| 7628 |
} |
| 7629 |
|
| 7630 |
ast_cond_destroy(&outgoing->cond); |
| 7631 |
|
| 7632 |
ast_free(outgoing->appdata); |
| 7633 |
} |
| 7634 |
|
| 7635 |
|
| 7636 |
static void *pbx_outgoing_exec(void *data) |
| 7637 |
{ |
| 7638 |
RAII_VAR(struct pbx_outgoing *, outgoing, data, ao2_cleanup); |
| 7639 |
enum ast_dial_result res; |
| 7640 |
struct ast_channel *chan; |
| 7641 |
|
| 7642 |
res = ast_dial_run(outgoing->dial, NULL, 0); |
| 7643 |
|
| 7644 |
if (outgoing->in_separate_thread) { |
| 7645 |
|
| 7646 |
ao2_lock(outgoing); |
| 7647 |
outgoing->dial_res = res; |
| 7648 |
outgoing->dialed = 1; |
| 7649 |
ast_cond_signal(&outgoing->cond); |
| 7650 |
ao2_unlock(outgoing); |
| 7651 |
} else { |
| 7652 |
|
| 7653 |
outgoing->dial_res = res; |
| 7654 |
} |
| 7655 |
|
| 7656 |
|
| 7657 |
if (res != AST_DIAL_RESULT_ANSWERED) { |
| 7658 |
return NULL; |
| 7659 |
} |
| 7660 |
|
| 7661 |
|
| 7662 |
chan = ast_dial_answered_steal(outgoing->dial); |
| 7663 |
|
| 7664 |
if (!ast_strlen_zero(outgoing->app)) { |
| 7665 |
struct ast_app *app = pbx_findapp(outgoing->app); |
| 7666 |
|
| 7667 |
if (app) { |
| 7668 |
ast_verb(4, "Launching %s(%s) on %s\n", outgoing->app, S_OR(outgoing->appdata, ""), |
| 7669 |
ast_channel_name(chan)); |
| 7670 |
pbx_exec(chan, app, outgoing->appdata); |
| 7671 |
} else { |
| 7672 |
ast_log(LOG_WARNING, "No such application '%s'\n", outgoing->app); |
| 7673 |
} |
| 7674 |
|
| 7675 |
ast_hangup(chan); |
| 7676 |
} else { |
| 7677 |
if (!ast_strlen_zero(outgoing->context)) { |
| 7678 |
ast_channel_context_set(chan, outgoing->context); |
| 7679 |
} |
| 7680 |
|
| 7681 |
if (!ast_strlen_zero(outgoing->exten)) { |
| 7682 |
ast_channel_exten_set(chan, outgoing->exten); |
| 7683 |
} |
| 7684 |
|
| 7685 |
if (outgoing->priority > 0) { |
| 7686 |
ast_channel_priority_set(chan, outgoing->priority); |
| 7687 |
} |
| 7688 |
|
| 7689 |
if (ast_pbx_run(chan)) { |
| 7690 |
ast_log(LOG_ERROR, "Failed to start PBX on %s\n", ast_channel_name(chan)); |
| 7691 |
ast_hangup(chan); |
| 7692 |
} |
| 7693 |
} |
| 7694 |
|
| 7695 |
return NULL; |
| 7696 |
} |
| 7697 |
|
| 7698 |
|
| 7699 |
static void pbx_outgoing_state_callback(struct ast_dial *dial) |
| 7700 |
{ |
| 7701 |
struct ast_channel *channel; |
| 7702 |
|
| 7703 |
if (ast_dial_state(dial) != AST_DIAL_RESULT_PROGRESS) { |
| 7704 |
return; |
| 7705 |
} |
| 7706 |
|
| 7707 |
if (!(channel = ast_dial_get_channel(dial, 0))) { |
| 7708 |
return; |
| 7709 |
} |
| 7710 |
|
| 7711 |
ast_verb(4, "Treating progress as answer on '%s' due to early media option\n", |
| 7712 |
ast_channel_name(channel)); |
| 7713 |
|
| 7714 |
ast_queue_control(channel, AST_CONTROL_ANSWER); |
| 7715 |
} |
| 7716 |
|
| 7717 |
|
| 7718 |
|
| 7719 |
|
| 7720 |
|
| 7721 |
|
| 7722 |
|
| 7723 |
|
| 7724 |
|
| 7725 |
static enum ast_control_frame_type pbx_dial_reason(enum ast_dial_result dial_result, int cause) |
| 7726 |
{ |
| 7727 |
enum ast_control_frame_type pbx_reason; |
| 7728 |
|
| 7729 |
if (dial_result == AST_DIAL_RESULT_ANSWERED) { |
| 7730 |
|
| 7731 |
pbx_reason = AST_CONTROL_ANSWER; |
| 7732 |
} else if (dial_result == AST_DIAL_RESULT_HANGUP) { |
| 7733 |
|
| 7734 |
pbx_reason = AST_CONTROL_HANGUP; |
| 7735 |
} else { |
| 7736 |
switch (cause) { |
| 7737 |
case AST_CAUSE_USER_BUSY: |
| 7738 |
pbx_reason = AST_CONTROL_BUSY; |
| 7739 |
break; |
| 7740 |
case AST_CAUSE_CALL_REJECTED: |
| 7741 |
case AST_CAUSE_NETWORK_OUT_OF_ORDER: |
| 7742 |
case AST_CAUSE_DESTINATION_OUT_OF_ORDER: |
| 7743 |
case AST_CAUSE_NORMAL_TEMPORARY_FAILURE: |
| 7744 |
case AST_CAUSE_SWITCH_CONGESTION: |
| 7745 |
case AST_CAUSE_NORMAL_CIRCUIT_CONGESTION: |
| 7746 |
pbx_reason = AST_CONTROL_CONGESTION; |
| 7747 |
break; |
| 7748 |
case AST_CAUSE_ANSWERED_ELSEWHERE: |
| 7749 |
case AST_CAUSE_NO_ANSWER: |
| 7750 |
|
| 7751 |
pbx_reason = AST_CONTROL_RINGING; |
| 7752 |
break; |
| 7753 |
case AST_CAUSE_UNALLOCATED: |
| 7754 |
default: |
| 7755 |
|
| 7756 |
pbx_reason = 0; |
| 7757 |
break; |
| 7758 |
} |
| 7759 |
} |
| 7760 |
|
| 7761 |
return pbx_reason; |
| 7762 |
} |
| 7763 |
|
| 7764 |
static int pbx_outgoing_attempt(const char *type, struct ast_format_cap *cap, |
| 7765 |
const char *addr, int timeout, const char *context, const char *exten, int priority, |
| 7766 |
const char *app, const char *appdata, int *reason, int synchronous, |
| 7767 |
const char *cid_num, const char *cid_name, struct ast_variable *vars, |
| 7768 |
const char *account, struct ast_channel **locked_channel, int early_media, |
| 7769 |
const struct ast_assigned_ids *assignedids, const char *predial_callee) |
| 7770 |
{ |
| 7771 |
RAII_VAR(struct pbx_outgoing *, outgoing, NULL, ao2_cleanup); |
| 7772 |
struct ast_channel *dialed; |
| 7773 |
pthread_t thread; |
| 7774 |
char tmp_cid_name[128]; |
| 7775 |
char tmp_cid_num[128]; |
| 7776 |
|
| 7777 |
outgoing = ao2_alloc(sizeof(*outgoing), pbx_outgoing_destroy); |
| 7778 |
if (!outgoing) { |
| 7779 |
return -1; |
| 7780 |
} |
| 7781 |
ast_cond_init(&outgoing->cond, NULL); |
| 7782 |
|
| 7783 |
if (!ast_strlen_zero(app)) { |
| 7784 |
ast_copy_string(outgoing->app, app, sizeof(outgoing->app)); |
| 7785 |
outgoing->appdata = ast_strdup(appdata); |
| 7786 |
} else { |
| 7787 |
ast_copy_string(outgoing->context, context, sizeof(outgoing->context)); |
| 7788 |
ast_copy_string(outgoing->exten, exten, sizeof(outgoing->exten)); |
| 7789 |
outgoing->priority = priority; |
| 7790 |
} |
| 7791 |
|
| 7792 |
if (!(outgoing->dial = ast_dial_create())) { |
| 7793 |
return -1; |
| 7794 |
} |
| 7795 |
|
| 7796 |
if (ast_dial_append(outgoing->dial, type, addr, assignedids)) { |
| 7797 |
return -1; |
| 7798 |
} |
| 7799 |
|
| 7800 |
ast_dial_set_global_timeout(outgoing->dial, timeout); |
| 7801 |
|
| 7802 |
if (!ast_strlen_zero(predial_callee)) { |
| 7803 |
|
| 7804 |
ast_dial_option_global_enable(outgoing->dial, AST_DIAL_OPTION_PREDIAL, (void *)predial_callee); |
| 7805 |
} |
| 7806 |
|
| 7807 |
if (ast_dial_prerun(outgoing->dial, NULL, cap)) { |
| 7808 |
if (synchronous && reason) { |
| 7809 |
*reason = pbx_dial_reason(AST_DIAL_RESULT_FAILED, |
| 7810 |
ast_dial_reason(outgoing->dial, 0)); |
| 7811 |
} |
| 7812 |
return -1; |
| 7813 |
} |
| 7814 |
|
| 7815 |
dialed = ast_dial_get_channel(outgoing->dial, 0); |
| 7816 |
if (!dialed) { |
| 7817 |
return -1; |
| 7818 |
} |
| 7819 |
|
| 7820 |
if (vars) { |
| 7821 |
ast_set_variables(dialed, vars); |
| 7822 |
} |
| 7823 |
ast_channel_lock(dialed); |
| 7824 |
if (!ast_strlen_zero(account)) { |
| 7825 |
ast_channel_stage_snapshot(dialed); |
| 7826 |
ast_channel_accountcode_set(dialed, account); |
| 7827 |
ast_channel_peeraccount_set(dialed, account); |
| 7828 |
ast_channel_stage_snapshot_done(dialed); |
| 7829 |
} |
| 7830 |
ast_set_flag(ast_channel_flags(dialed), AST_FLAG_ORIGINATED); |
| 7831 |
|
| 7832 |
if (!ast_strlen_zero(predial_callee)) { |
| 7833 |
char *tmp = NULL; |
| 7834 |
|
| 7835 |
|
| 7836 |
|
| 7837 |
|
| 7838 |
|
| 7839 |
tmp = S_COR(ast_channel_caller(dialed)->id.number.valid, ast_channel_caller(dialed)->id.number.str, NULL); |
| 7840 |
if (tmp) { |
| 7841 |
ast_copy_string(tmp_cid_num, tmp, sizeof(tmp_cid_num)); |
| 7842 |
cid_num = tmp_cid_num; |
| 7843 |
} |
| 7844 |
tmp = S_COR(ast_channel_caller(dialed)->id.name.valid, ast_channel_caller(dialed)->id.name.str, NULL); |
| 7845 |
if (tmp) { |
| 7846 |
ast_copy_string(tmp_cid_name, tmp, sizeof(tmp_cid_name)); |
| 7847 |
cid_name = tmp_cid_name; |
| 7848 |
} |
| 7849 |
} |
| 7850 |
ast_channel_unlock(dialed); |
| 7851 |
|
| 7852 |
if (!ast_strlen_zero(cid_num) || !ast_strlen_zero(cid_name)) { |
| 7853 |
struct ast_party_connected_line connected; |
| 7854 |
|
| 7855 |
|
| 7856 |
|
| 7857 |
|
| 7858 |
|
| 7859 |
|
| 7860 |
|
| 7861 |
|
| 7862 |
|
| 7863 |
ast_set_callerid(dialed, cid_num, cid_name, cid_num); |
| 7864 |
|
| 7865 |
ast_party_connected_line_set_init(&connected, ast_channel_connected(dialed)); |
| 7866 |
if (!ast_strlen_zero(cid_num)) { |
| 7867 |
connected.id.number.valid = 1; |
| 7868 |
connected.id.number.str = (char *) cid_num; |
| 7869 |
connected.id.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED; |
| 7870 |
} |
| 7871 |
if (!ast_strlen_zero(cid_name)) { |
| 7872 |
connected.id.name.valid = 1; |
| 7873 |
connected.id.name.str = (char *) cid_name; |
| 7874 |
connected.id.name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED; |
| 7875 |
} |
| 7876 |
ast_channel_set_connected_line(dialed, &connected, NULL); |
| 7877 |
} |
| 7878 |
|
| 7879 |
if (early_media) { |
| 7880 |
ast_dial_set_state_callback(outgoing->dial, pbx_outgoing_state_callback); |
| 7881 |
} |
| 7882 |
|
| 7883 |
if (locked_channel) { |
| 7884 |
|
| 7885 |
|
| 7886 |
|
| 7887 |
|
| 7888 |
|
| 7889 |
ast_channel_ref(dialed); |
| 7890 |
if (!synchronous) { |
| 7891 |
|
| 7892 |
|
| 7893 |
|
| 7894 |
|
| 7895 |
|
| 7896 |
ast_channel_lock(dialed); |
| 7897 |
} |
| 7898 |
} |
| 7899 |
|
| 7900 |
|
| 7901 |
ao2_ref(outgoing, +1); |
| 7902 |
|
| 7903 |
if (synchronous == AST_OUTGOING_WAIT_COMPLETE) { |
| 7904 |
|
| 7905 |
|
| 7906 |
|
| 7907 |
|
| 7908 |
|
| 7909 |
pbx_outgoing_exec(outgoing); |
| 7910 |
} else { |
| 7911 |
outgoing->in_separate_thread = 1; |
| 7912 |
|
| 7913 |
if (ast_pthread_create_detached(&thread, NULL, pbx_outgoing_exec, outgoing)) { |
| 7914 |
ast_log(LOG_WARNING, "Unable to spawn dialing thread for '%s/%s'\n", type, addr); |
| 7915 |
ao2_ref(outgoing, -1); |
| 7916 |
if (locked_channel) { |
| 7917 |
if (!synchronous) { |
| 7918 |
ast_channel_unlock(dialed); |
| 7919 |
} |
| 7920 |
ast_channel_unref(dialed); |
| 7921 |
} |
| 7922 |
return -1; |
| 7923 |
} |
| 7924 |
|
| 7925 |
if (synchronous) { |
| 7926 |
ao2_lock(outgoing); |
| 7927 |
|
| 7928 |
while (!outgoing->dialed) { |
| 7929 |
ast_cond_wait(&outgoing->cond, ao2_object_get_lockaddr(outgoing)); |
| 7930 |
} |
| 7931 |
ao2_unlock(outgoing); |
| 7932 |
} |
| 7933 |
} |
| 7934 |
|
| 7935 |
if (synchronous) { |
| 7936 |
|
| 7937 |
if (reason) { |
| 7938 |
*reason = pbx_dial_reason(outgoing->dial_res, |
| 7939 |
ast_dial_reason(outgoing->dial, 0)); |
| 7940 |
} |
| 7941 |
|
| 7942 |
if (outgoing->dial_res != AST_DIAL_RESULT_ANSWERED) { |
| 7943 |
|
| 7944 |
if (locked_channel) { |
| 7945 |
ast_channel_unref(dialed); |
| 7946 |
} |
| 7947 |
return -1; |
| 7948 |
} |
| 7949 |
if (locked_channel) { |
| 7950 |
ast_channel_lock(dialed); |
| 7951 |
} |
| 7952 |
} |
| 7953 |
|
| 7954 |
if (locked_channel) { |
| 7955 |
*locked_channel = dialed; |
| 7956 |
} |
| 7957 |
return 0; |
| 7958 |
} |
| 7959 |
|
| 7960 |
int ast_pbx_outgoing_exten(const char *type, struct ast_format_cap *cap, const char *addr, |
| 7961 |
int timeout, const char *context, const char *exten, int priority, int *reason, |
| 7962 |
int synchronous, const char *cid_num, const char *cid_name, struct ast_variable *vars, |
| 7963 |
const char *account, struct ast_channel **locked_channel, int early_media, |
| 7964 |
const struct ast_assigned_ids *assignedids) |
| 7965 |
{ |
| 7966 |
return ast_pbx_outgoing_exten_predial(type, cap, addr, timeout, context, exten, priority, reason, |
| 7967 |
synchronous, cid_num, cid_name, vars, account, locked_channel, early_media, assignedids, NULL); |
| 7968 |
} |
| 7969 |
|
| 7970 |
int ast_pbx_outgoing_exten_predial(const char *type, struct ast_format_cap *cap, const char *addr, |
| 7971 |
int timeout, const char *context, const char *exten, int priority, int *reason, |
| 7972 |
int synchronous, const char *cid_num, const char *cid_name, struct ast_variable *vars, |
| 7973 |
const char *account, struct ast_channel **locked_channel, int early_media, |
| 7974 |
const struct ast_assigned_ids *assignedids, const char *predial_callee) |
| 7975 |
{ |
| 7976 |
int res; |
| 7977 |
int my_reason; |
| 7978 |
|
| 7979 |
if (!reason) { |
| 7980 |
reason = &my_reason; |
| 7981 |
} |
| 7982 |
*reason = 0; |
| 7983 |
if (locked_channel) { |
| 7984 |
*locked_channel = NULL; |
| 7985 |
} |
| 7986 |
|
| 7987 |
res = pbx_outgoing_attempt(type, cap, addr, timeout, context, exten, priority, |
| 7988 |
NULL, NULL, reason, synchronous, cid_num, cid_name, vars, account, locked_channel, |
| 7989 |
early_media, assignedids, predial_callee); |
| 7990 |
|
| 7991 |
if (res < 0 |
| 7992 |
&& 0 < synchronous |
| 7993 |
&& ast_exists_extension(NULL, context, "failed", 1, NULL)) { |
| 7994 |
struct ast_channel *failed; |
| 7995 |
|
| 7996 |
|
| 7997 |
ast_assert(!locked_channel || !*locked_channel); |
| 7998 |
|
| 7999 |
|
| 8000 |
|
| 8001 |
|
| 8002 |
|
| 8003 |
failed = ast_channel_alloc(0, AST_STATE_DOWN, cid_num, cid_name, account, |
| 8004 |
"failed", context, NULL, NULL, 0, "OutgoingSpoolFailed"); |
| 8005 |
if (failed) { |
| 8006 |
char failed_reason[12]; |
| 8007 |
|
| 8008 |
snprintf(failed_reason, sizeof(failed_reason), "%d", *reason); |
| 8009 |
pbx_builtin_setvar_helper(failed, "REASON", failed_reason); |
| 8010 |
ast_channel_unlock(failed); |
| 8011 |
ast_set_variables(failed, vars); |
| 8012 |
|
| 8013 |
if (ast_pbx_run(failed)) { |
| 8014 |
ast_log(LOG_ERROR, "Unable to run PBX on '%s'\n", |
| 8015 |
ast_channel_name(failed)); |
| 8016 |
ast_hangup(failed); |
| 8017 |
} |
| 8018 |
} |
| 8019 |
} |
| 8020 |
|
| 8021 |
return res; |
| 8022 |
} |
| 8023 |
|
| 8024 |
int ast_pbx_outgoing_app(const char *type, struct ast_format_cap *cap, const char *addr, |
| 8025 |
int timeout, const char *app, const char *appdata, int *reason, int synchronous, |
| 8026 |
const char *cid_num, const char *cid_name, struct ast_variable *vars, |
| 8027 |
const char *account, struct ast_channel **locked_channel, |
| 8028 |
const struct ast_assigned_ids *assignedids) |
| 8029 |
{ |
| 8030 |
return ast_pbx_outgoing_app_predial(type, cap, addr, timeout, app, appdata, reason, synchronous, |
| 8031 |
cid_num, cid_name, vars, account, locked_channel, assignedids, NULL); |
| 8032 |
} |
| 8033 |
|
| 8034 |
int ast_pbx_outgoing_app_predial(const char *type, struct ast_format_cap *cap, const char *addr, |
| 8035 |
int timeout, const char *app, const char *appdata, int *reason, int synchronous, |
| 8036 |
const char *cid_num, const char *cid_name, struct ast_variable *vars, |
| 8037 |
const char *account, struct ast_channel **locked_channel, |
| 8038 |
const struct ast_assigned_ids *assignedids, const char *predial_callee) |
| 8039 |
{ |
| 8040 |
if (reason) { |
| 8041 |
*reason = 0; |
| 8042 |
} |
| 8043 |
if (locked_channel) { |
| 8044 |
*locked_channel = NULL; |
| 8045 |
} |
| 8046 |
if (ast_strlen_zero(app)) { |
| 8047 |
return -1; |
| 8048 |
} |
| 8049 |
|
| 8050 |
return pbx_outgoing_attempt(type, cap, addr, timeout, NULL, NULL, 0, app, appdata, |
| 8051 |
reason, synchronous, cid_num, cid_name, vars, account, locked_channel, 0, |
| 8052 |
assignedids, predial_callee); |
| 8053 |
} |
| 8054 |
|
| 8055 |
|
| 8056 |
|
| 8057 |
|
| 8058 |
|
| 8059 |
static void __ast_internal_context_destroy( struct ast_context *con) |
| 8060 |
{ |
| 8061 |
struct ast_exten *e, *el, *en; |
| 8062 |
struct ast_context *tmp = con; |
| 8063 |
|
| 8064 |
|
| 8065 |
AST_VECTOR_CALLBACK_VOID(&tmp->includes, include_free); |
| 8066 |
AST_VECTOR_FREE(&tmp->includes); |
| 8067 |
|
| 8068 |
|
| 8069 |
AST_VECTOR_CALLBACK_VOID(&tmp->ignorepats, ignorepat_free); |
| 8070 |
AST_VECTOR_FREE(&tmp->ignorepats); |
| 8071 |
|
| 8072 |
|
| 8073 |
AST_VECTOR_CALLBACK_VOID(&tmp->alts, sw_free); |
| 8074 |
AST_VECTOR_FREE(&tmp->alts); |
| 8075 |
|
| 8076 |
|
| 8077 |
if (tmp->root_table) { |
| 8078 |
ast_hashtab_destroy(tmp->root_table, 0); |
| 8079 |
} |
| 8080 |
|
| 8081 |
if (tmp->pattern_tree) |
| 8082 |
destroy_pattern_tree(tmp->pattern_tree); |
| 8083 |
|
| 8084 |
for (e = tmp->root; e;) { |
| 8085 |
for (en = e->peer; en;) { |
| 8086 |
el = en; |
| 8087 |
en = en->peer; |
| 8088 |
destroy_exten(el); |
| 8089 |
} |
| 8090 |
el = e; |
| 8091 |
e = e->next; |
| 8092 |
destroy_exten(el); |
| 8093 |
} |
| 8094 |
tmp->root = NULL; |
| 8095 |
ast_rwlock_destroy(&tmp->lock); |
| 8096 |
ast_free(tmp); |
| 8097 |
} |
| 8098 |
|
| 8099 |
|
| 8100 |
void __ast_context_destroy(struct ast_context *list, struct ast_hashtab *contexttab, struct ast_context *con, const char *registrar) |
| 8101 |
{ |
| 8102 |
struct ast_context *tmp, *tmpl=NULL; |
| 8103 |
struct ast_exten *exten_item, *prio_item; |
| 8104 |
|
| 8105 |
for (tmp = list; tmp; ) { |
| 8106 |
struct ast_context *next = NULL; |
| 8107 |
|
| 8108 |
|
| 8109 |
|
| 8110 |
|
| 8111 |
ast_debug(1, "Investigate ctx %s %s\n", tmp->name, tmp->registrar); |
| 8112 |
if (con) { |
| 8113 |
for (; tmp; tmpl = tmp, tmp = tmp->next) { |
| 8114 |
ast_debug(1, "check ctx %s %s\n", tmp->name, tmp->registrar); |
| 8115 |
if ( !strcasecmp(tmp->name, con->name) ) { |
| 8116 |
break; |
| 8117 |
} |
| 8118 |
} |
| 8119 |
} |
| 8120 |
|
| 8121 |
if (!tmp) |
| 8122 |
break; |
| 8123 |
ast_wrlock_context(tmp); |
| 8124 |
|
| 8125 |
if (registrar) { |
| 8126 |
|
| 8127 |
struct ast_hashtab_iter *exten_iter; |
| 8128 |
struct ast_hashtab_iter *prio_iter; |
| 8129 |
int idx; |
| 8130 |
|
| 8131 |
|
| 8132 |
for (idx = ast_context_ignorepats_count(tmp) - 1; idx >= 0; idx--) { |
| 8133 |
struct ast_ignorepat *ip = AST_VECTOR_GET(&tmp->ignorepats, idx); |
| 8134 |
|
| 8135 |
if (!strcmp(ast_get_ignorepat_registrar(ip), registrar)) { |
| 8136 |
AST_VECTOR_REMOVE_ORDERED(&tmp->ignorepats, idx); |
| 8137 |
ignorepat_free(ip); |
| 8138 |
} |
| 8139 |
} |
| 8140 |
|
| 8141 |
for (idx = ast_context_includes_count(tmp) - 1; idx >= 0; idx--) { |
| 8142 |
struct ast_include *i = AST_VECTOR_GET(&tmp->includes, idx); |
| 8143 |
|
| 8144 |
if (!strcmp(ast_get_include_registrar(i), registrar)) { |
| 8145 |
AST_VECTOR_REMOVE_ORDERED(&tmp->includes, idx); |
| 8146 |
include_free(i); |
| 8147 |
} |
| 8148 |
} |
| 8149 |
|
| 8150 |
for (idx = ast_context_switches_count(tmp) - 1; idx >= 0; idx--) { |
| 8151 |
struct ast_sw *sw = AST_VECTOR_GET(&tmp->alts, idx); |
| 8152 |
|
| 8153 |
if (!strcmp(ast_get_switch_registrar(sw), registrar)) { |
| 8154 |
AST_VECTOR_REMOVE_ORDERED(&tmp->alts, idx); |
| 8155 |
sw_free(sw); |
| 8156 |
} |
| 8157 |
} |
| 8158 |
|
| 8159 |
if (tmp->root_table) { |
| 8160 |
exten_iter = ast_hashtab_start_traversal(tmp->root_table); |
| 8161 |
while ((exten_item=ast_hashtab_next(exten_iter))) { |
| 8162 |
int end_traversal = 1; |
| 8163 |
|
| 8164 |
|
| 8165 |
|
| 8166 |
|
| 8167 |
|
| 8168 |
|
| 8169 |
if (!exten_item->peer_table) { |
| 8170 |
continue; |
| 8171 |
} |
| 8172 |
|
| 8173 |
prio_iter = ast_hashtab_start_traversal(exten_item->peer_table); |
| 8174 |
while ((prio_item=ast_hashtab_next(prio_iter))) { |
| 8175 |
char extension[AST_MAX_EXTENSION]; |
| 8176 |
char cidmatch[AST_MAX_EXTENSION]; |
| 8177 |
if (!prio_item->registrar || strcmp(prio_item->registrar, registrar) != 0) { |
| 8178 |
continue; |
| 8179 |
} |
| 8180 |
ast_verb(5, "Remove %s/%s/%d, registrar=%s; con=%s(%p); con->root=%p\n", |
| 8181 |
tmp->name, prio_item->name, prio_item->priority, registrar, con? con->name : "<nil>", con, con? con->root_table: NULL); |
| 8182 |
ast_copy_string(extension, prio_item->exten, sizeof(extension)); |
| 8183 |
if (prio_item->cidmatch) { |
| 8184 |
ast_copy_string(cidmatch, prio_item->cidmatch, sizeof(cidmatch)); |
| 8185 |
} |
| 8186 |
end_traversal &= ast_context_remove_extension_callerid2(tmp, extension, prio_item->priority, cidmatch, prio_item->matchcid, NULL, 1); |
| 8187 |
} |
| 8188 |
|
| 8189 |
|
| 8190 |
|
| 8191 |
|
| 8192 |
|
| 8193 |
|
| 8194 |
|
| 8195 |
|
| 8196 |
if (end_traversal) { |
| 8197 |
ast_hashtab_end_traversal(prio_iter); |
| 8198 |
} else { |
| 8199 |
ast_free(prio_iter); |
| 8200 |
} |
| 8201 |
} |
| 8202 |
ast_hashtab_end_traversal(exten_iter); |
| 8203 |
} |
| 8204 |
|
| 8205 |
|
| 8206 |
|
| 8207 |
|
| 8208 |
if (strcmp(tmp->registrar, registrar) == 0 && tmp->refcount < 2 && !tmp->root && !ast_context_ignorepats_count(tmp) && !ast_context_includes_count(tmp) && !ast_context_switches_count(tmp)) { |
| 8209 |
ast_debug(1, "delete ctx %s %s\n", tmp->name, tmp->registrar); |
| 8210 |
ast_hashtab_remove_this_object(contexttab, tmp); |
| 8211 |
|
| 8212 |
next = tmp->next; |
| 8213 |
if (tmpl) |
| 8214 |
tmpl->next = next; |
| 8215 |
else |
| 8216 |
contexts = next; |
| 8217 |
|
| 8218 |
|
| 8219 |
ast_unlock_context(tmp); |
| 8220 |
__ast_internal_context_destroy(tmp); |
| 8221 |
} else { |
| 8222 |
ast_debug(1,"Couldn't delete ctx %s/%s; refc=%d; tmp.root=%p\n", tmp->name, tmp->registrar, |
| 8223 |
tmp->refcount, tmp->root); |
| 8224 |
ast_unlock_context(tmp); |
| 8225 |
next = tmp->next; |
| 8226 |
tmpl = tmp; |
| 8227 |
} |
| 8228 |
} else if (con) { |
| 8229 |
ast_verb(3, "Deleting context %s registrar=%s\n", tmp->name, tmp->registrar); |
| 8230 |
ast_debug(1, "delete ctx %s %s\n", tmp->name, tmp->registrar); |
| 8231 |
ast_hashtab_remove_this_object(contexttab, tmp); |
| 8232 |
|
| 8233 |
next = tmp->next; |
| 8234 |
if (tmpl) |
| 8235 |
tmpl->next = next; |
| 8236 |
else |
| 8237 |
contexts = next; |
| 8238 |
|
| 8239 |
|
| 8240 |
ast_unlock_context(tmp); |
| 8241 |
__ast_internal_context_destroy(tmp); |
| 8242 |
} |
| 8243 |
|
| 8244 |
|
| 8245 |
tmp = con ? NULL : next; |
| 8246 |
} |
| 8247 |
} |
| 8248 |
|
| 8249 |
int ast_context_destroy_by_name(const char *context, const char *registrar) |
| 8250 |
{ |
| 8251 |
struct ast_context *con; |
| 8252 |
int ret = -1; |
| 8253 |
|
| 8254 |
ast_wrlock_contexts(); |
| 8255 |
con = ast_context_find(context); |
| 8256 |
if (con) { |
| 8257 |
ast_context_destroy(con, registrar); |
| 8258 |
ret = 0; |
| 8259 |
} |
| 8260 |
ast_unlock_contexts(); |
| 8261 |
|
| 8262 |
return ret; |
| 8263 |
} |
| 8264 |
|
| 8265 |
void ast_context_destroy(struct ast_context *con, const char *registrar) |
| 8266 |
{ |
| 8267 |
ast_wrlock_contexts(); |
| 8268 |
__ast_context_destroy(contexts, contexts_table, con,registrar); |
| 8269 |
ast_unlock_contexts(); |
| 8270 |
} |
| 8271 |
|
| 8272 |
void wait_for_hangup(struct ast_channel *chan, const void *data) |
| 8273 |
{ |
| 8274 |
int res; |
| 8275 |
struct ast_frame *f; |
| 8276 |
double waitsec; |
| 8277 |
int waittime; |
| 8278 |
|
| 8279 |
if (ast_strlen_zero(data) || (sscanf(data, "%30lg", &waitsec) != 1) || (waitsec < 0)) |
| 8280 |
waitsec = -1; |
| 8281 |
if (waitsec > -1) { |
| 8282 |
waittime = waitsec * 1000.0; |
| 8283 |
ast_safe_sleep_without_silence(chan, waittime); |
| 8284 |
} else do { |
| 8285 |
res = ast_waitfor(chan, -1); |
| 8286 |
if (res < 0) |
| 8287 |
return; |
| 8288 |
f = ast_read(chan); |
| 8289 |
if (f) |
| 8290 |
ast_frfree(f); |
| 8291 |
} while(f); |
| 8292 |
} |
| 8293 |
|
| 8294 |
|
| 8295 |
|
| 8296 |
|
| 8297 |
static int testtime_write(struct ast_channel *chan, const char *cmd, char *var, const char *value) |
| 8298 |
{ |
| 8299 |
struct ast_tm tm; |
| 8300 |
struct timeval tv; |
| 8301 |
char *remainder, result[30], timezone[80]; |
| 8302 |
|
| 8303 |
|
| 8304 |
if (!pbx_checkcondition(value)) { |
| 8305 |
pbx_builtin_setvar_helper(chan, "TESTTIME", NULL); |
| 8306 |
return 0; |
| 8307 |
} |
| 8308 |
|
| 8309 |
|
| 8310 |
if (!(remainder = ast_strptime(value, "%Y/%m/%d %H:%M:%S", &tm))) { |
| 8311 |
return -1; |
| 8312 |
} |
| 8313 |
sscanf(remainder, "%79s", timezone); |
| 8314 |
tv = ast_mktime(&tm, S_OR(timezone, NULL)); |
| 8315 |
|
| 8316 |
snprintf(result, sizeof(result), "%ld", (long) tv.tv_sec); |
| 8317 |
pbx_builtin_setvar_helper(chan, "__TESTTIME", result); |
| 8318 |
return 0; |
| 8319 |
} |
| 8320 |
|
| 8321 |
static struct ast_custom_function testtime_function = { |
| 8322 |
.name = "TESTTIME", |
| 8323 |
.write = testtime_write, |
| 8324 |
}; |
| 8325 |
|
| 8326 |
int pbx_checkcondition(const char *condition) |
| 8327 |
{ |
| 8328 |
int res; |
| 8329 |
if (ast_strlen_zero(condition)) { |
| 8330 |
return 0; |
| 8331 |
} else if (sscanf(condition, "%30d", &res) == 1) { |
| 8332 |
return res; |
| 8333 |
} else { |
| 8334 |
return 1; |
| 8335 |
} |
| 8336 |
} |
| 8337 |
|
| 8338 |
static void presence_state_cb(void *unused, struct stasis_subscription *sub, struct stasis_message *msg) |
| 8339 |
{ |
| 8340 |
struct ast_presence_state_message *presence_state; |
| 8341 |
struct ast_str *hint_app = NULL; |
| 8342 |
struct ast_hintdevice *device; |
| 8343 |
struct ast_hintdevice *cmpdevice; |
| 8344 |
struct ao2_iterator *dev_iter; |
| 8345 |
|
| 8346 |
if (stasis_message_type(msg) != ast_presence_state_message_type()) { |
| 8347 |
return; |
| 8348 |
} |
| 8349 |
|
| 8350 |
presence_state = stasis_message_data(msg); |
| 8351 |
|
| 8352 |
if (ao2_container_count(hintdevices) == 0) { |
| 8353 |
|
| 8354 |
return; |
| 8355 |
} |
| 8356 |
|
| 8357 |
hint_app = ast_str_create(1024); |
| 8358 |
if (!hint_app) { |
| 8359 |
return; |
| 8360 |
} |
| 8361 |
|
| 8362 |
cmpdevice = ast_alloca(sizeof(*cmpdevice) + strlen(presence_state->provider)); |
| 8363 |
strcpy(cmpdevice->hintdevice, presence_state->provider); |
| 8364 |
|
| 8365 |
ast_mutex_lock(&context_merge_lock); |
| 8366 |
dev_iter = ao2_t_callback(hintdevices, |
| 8367 |
OBJ_POINTER | OBJ_MULTIPLE, |
| 8368 |
hintdevice_cmp_multiple, |
| 8369 |
cmpdevice, |
| 8370 |
"find devices in container"); |
| 8371 |
if (!dev_iter) { |
| 8372 |
ast_mutex_unlock(&context_merge_lock); |
| 8373 |
ast_free(hint_app); |
| 8374 |
return; |
| 8375 |
} |
| 8376 |
|
| 8377 |
for (; (device = ao2_iterator_next(dev_iter)); ao2_t_ref(device, -1, "Next device")) { |
| 8378 |
if (device->hint) { |
| 8379 |
presence_state_notify_callbacks(device->hint, &hint_app, presence_state); |
| 8380 |
} |
| 8381 |
} |
| 8382 |
ao2_iterator_destroy(dev_iter); |
| 8383 |
ast_mutex_unlock(&context_merge_lock); |
| 8384 |
|
| 8385 |
ast_free(hint_app); |
| 8386 |
} |
| 8387 |
|
| 8388 |
static int action_extensionstatelist(struct mansession *s, const struct message *m) |
| 8389 |
{ |
| 8390 |
const char *action_id = astman_get_header(m, "ActionID"); |
| 8391 |
struct ast_hint *hint; |
| 8392 |
struct ao2_iterator it_hints; |
| 8393 |
int hint_count = 0; |
| 8394 |
|
| 8395 |
if (!hints) { |
| 8396 |
astman_send_error(s, m, "No dialplan hints are available"); |
| 8397 |
return 0; |
| 8398 |
} |
| 8399 |
|
| 8400 |
astman_send_listack(s, m, "Extension Statuses will follow", "start"); |
| 8401 |
|
| 8402 |
ao2_lock(hints); |
| 8403 |
it_hints = ao2_iterator_init(hints, 0); |
| 8404 |
for (; (hint = ao2_iterator_next(&it_hints)); ao2_ref(hint, -1)) { |
| 8405 |
|
| 8406 |
ao2_lock(hint); |
| 8407 |
|
| 8408 |
|
| 8409 |
|
| 8410 |
|
| 8411 |
|
| 8412 |
if (hint->exten->exten[0] == '_') { |
| 8413 |
ao2_unlock(hint); |
| 8414 |
continue; |
| 8415 |
} |
| 8416 |
|
| 8417 |
++hint_count; |
| 8418 |
|
| 8419 |
astman_append(s, "Event: ExtensionStatus\r\n"); |
| 8420 |
if (!ast_strlen_zero(action_id)) { |
| 8421 |
astman_append(s, "ActionID: %s\r\n", action_id); |
| 8422 |
} |
| 8423 |
astman_append(s, |
| 8424 |
"Exten: %s\r\n" |
| 8425 |
"Context: %s\r\n" |
| 8426 |
"Hint: %s\r\n" |
| 8427 |
"Status: %d\r\n" |
| 8428 |
"StatusText: %s\r\n\r\n", |
| 8429 |
hint->exten->exten, |
| 8430 |
hint->exten->parent->name, |
| 8431 |
hint->exten->app, |
| 8432 |
hint->laststate, |
| 8433 |
ast_extension_state2str(hint->laststate)); |
| 8434 |
ao2_unlock(hint); |
| 8435 |
} |
| 8436 |
|
| 8437 |
ao2_iterator_destroy(&it_hints); |
| 8438 |
ao2_unlock(hints); |
| 8439 |
|
| 8440 |
astman_send_list_complete_start(s, m, "ExtensionStateListComplete", hint_count); |
| 8441 |
astman_send_list_complete_end(s); |
| 8442 |
|
| 8443 |
return 0; |
| 8444 |
} |
| 8445 |
|
| 8446 |
|
| 8447 |
|
| 8448 |
|
| 8449 |
|
| 8450 |
|
| 8451 |
|
| 8452 |
|
| 8453 |
static void unload_pbx(void) |
| 8454 |
{ |
| 8455 |
presence_state_sub = stasis_unsubscribe_and_join(presence_state_sub); |
| 8456 |
device_state_sub = stasis_unsubscribe_and_join(device_state_sub); |
| 8457 |
|
| 8458 |
ast_manager_unregister("ShowDialPlan"); |
| 8459 |
ast_manager_unregister("ExtensionStateList"); |
| 8460 |
ast_cli_unregister_multiple(pbx_cli, ARRAY_LEN(pbx_cli)); |
| 8461 |
ast_custom_function_unregister(&exception_function); |
| 8462 |
ast_custom_function_unregister(&testtime_function); |
| 8463 |
} |
| 8464 |
|
| 8465 |
int load_pbx(void) |
| 8466 |
{ |
| 8467 |
int res = 0; |
| 8468 |
|
| 8469 |
ast_register_cleanup(unload_pbx); |
| 8470 |
|
| 8471 |
|
| 8472 |
ast_verb(1, "Asterisk PBX Core Initializing\n"); |
| 8473 |
|
| 8474 |
ast_verb(5, "Registering builtin functions:\n"); |
| 8475 |
ast_cli_register_multiple(pbx_cli, ARRAY_LEN(pbx_cli)); |
| 8476 |
__ast_custom_function_register(&exception_function, NULL); |
| 8477 |
__ast_custom_function_register(&testtime_function, NULL); |
| 8478 |
|
| 8479 |
|
| 8480 |
res |= ast_manager_register_xml_core("ShowDialPlan", EVENT_FLAG_CONFIG | EVENT_FLAG_REPORTING, manager_show_dialplan); |
| 8481 |
res |= ast_manager_register_xml_core("ExtensionStateList", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING, action_extensionstatelist); |
| 8482 |
|
| 8483 |
if (res) { |
| 8484 |
return -1; |
| 8485 |
} |
| 8486 |
|
| 8487 |
if (!(device_state_sub = stasis_subscribe(ast_device_state_topic_all(), device_state_cb, NULL))) { |
| 8488 |
return -1; |
| 8489 |
} |
| 8490 |
stasis_subscription_accept_message_type(device_state_sub, ast_device_state_message_type()); |
| 8491 |
stasis_subscription_accept_message_type(device_state_sub, hint_change_message_type()); |
| 8492 |
stasis_subscription_accept_message_type(device_state_sub, hint_remove_message_type()); |
| 8493 |
stasis_subscription_set_filter(device_state_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 8494 |
|
| 8495 |
if (!(presence_state_sub = stasis_subscribe(ast_presence_state_topic_all(), presence_state_cb, NULL))) { |
| 8496 |
return -1; |
| 8497 |
} |
| 8498 |
stasis_subscription_accept_message_type(presence_state_sub, ast_presence_state_message_type()); |
| 8499 |
stasis_subscription_set_filter(presence_state_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 8500 |
|
| 8501 |
return 0; |
| 8502 |
} |
| 8503 |
|
| 8504 |
|
| 8505 |
|
| 8506 |
|
| 8507 |
int ast_wrlock_contexts(void) |
| 8508 |
{ |
| 8509 |
return ast_mutex_lock(&conlock); |
| 8510 |
} |
| 8511 |
|
| 8512 |
int ast_rdlock_contexts(void) |
| 8513 |
{ |
| 8514 |
return ast_mutex_lock(&conlock); |
| 8515 |
} |
| 8516 |
|
| 8517 |
int ast_unlock_contexts(void) |
| 8518 |
{ |
| 8519 |
return ast_mutex_unlock(&conlock); |
| 8520 |
} |
| 8521 |
|
| 8522 |
|
| 8523 |
|
| 8524 |
|
| 8525 |
int ast_wrlock_context(struct ast_context *con) |
| 8526 |
{ |
| 8527 |
return ast_rwlock_wrlock(&con->lock); |
| 8528 |
} |
| 8529 |
|
| 8530 |
int ast_rdlock_context(struct ast_context *con) |
| 8531 |
{ |
| 8532 |
return ast_rwlock_rdlock(&con->lock); |
| 8533 |
} |
| 8534 |
|
| 8535 |
int ast_unlock_context(struct ast_context *con) |
| 8536 |
{ |
| 8537 |
return ast_rwlock_unlock(&con->lock); |
| 8538 |
} |
| 8539 |
|
| 8540 |
|
| 8541 |
|
| 8542 |
|
| 8543 |
const char *ast_get_context_name(struct ast_context *con) |
| 8544 |
{ |
| 8545 |
return con ? con->name : NULL; |
| 8546 |
} |
| 8547 |
|
| 8548 |
struct ast_context *ast_get_extension_context(struct ast_exten *exten) |
| 8549 |
{ |
| 8550 |
return exten ? exten->parent : NULL; |
| 8551 |
} |
| 8552 |
|
| 8553 |
const char *ast_get_extension_name(struct ast_exten *exten) |
| 8554 |
{ |
| 8555 |
return exten ? exten->name : NULL; |
| 8556 |
} |
| 8557 |
|
| 8558 |
const char *ast_get_extension_label(struct ast_exten *exten) |
| 8559 |
{ |
| 8560 |
return exten ? exten->label : NULL; |
| 8561 |
} |
| 8562 |
|
| 8563 |
int ast_get_extension_priority(struct ast_exten *exten) |
| 8564 |
{ |
| 8565 |
return exten ? exten->priority : -1; |
| 8566 |
} |
| 8567 |
|
| 8568 |
|
| 8569 |
|
| 8570 |
|
| 8571 |
const char *ast_get_context_registrar(struct ast_context *c) |
| 8572 |
{ |
| 8573 |
return c ? c->registrar : NULL; |
| 8574 |
} |
| 8575 |
|
| 8576 |
const char *ast_get_extension_registrar(struct ast_exten *e) |
| 8577 |
{ |
| 8578 |
return e ? e->registrar : NULL; |
| 8579 |
} |
| 8580 |
|
| 8581 |
const char *ast_get_extension_registrar_file(struct ast_exten *e) |
| 8582 |
{ |
| 8583 |
return e ? e->registrar_file : NULL; |
| 8584 |
} |
| 8585 |
|
| 8586 |
int ast_get_extension_registrar_line(struct ast_exten *e) |
| 8587 |
{ |
| 8588 |
return e ? e->registrar_line : 0; |
| 8589 |
} |
| 8590 |
|
| 8591 |
int ast_get_extension_matchcid(struct ast_exten *e) |
| 8592 |
{ |
| 8593 |
return e ? e->matchcid : 0; |
| 8594 |
} |
| 8595 |
|
| 8596 |
const char *ast_get_extension_cidmatch(struct ast_exten *e) |
| 8597 |
{ |
| 8598 |
return e ? e->cidmatch_display : NULL; |
| 8599 |
} |
| 8600 |
|
| 8601 |
const char *ast_get_extension_app(struct ast_exten *e) |
| 8602 |
{ |
| 8603 |
return e ? e->app : NULL; |
| 8604 |
} |
| 8605 |
|
| 8606 |
void *ast_get_extension_app_data(struct ast_exten *e) |
| 8607 |
{ |
| 8608 |
return e ? e->data : NULL; |
| 8609 |
} |
| 8610 |
|
| 8611 |
int ast_get_extension_data(char *buf, int bufsize, struct ast_channel *c, |
| 8612 |
const char *context, const char *exten, int priority) |
| 8613 |
{ |
| 8614 |
struct ast_exten *e; |
| 8615 |
struct pbx_find_info q = { .stacklen = 0 }; |
| 8616 |
ast_rdlock_contexts(); |
| 8617 |
e = pbx_find_extension(c, NULL, &q, context, exten, priority, NULL, "", E_MATCH); |
| 8618 |
if (e) { |
| 8619 |
if (buf) { |
| 8620 |
const char *tmp = ast_get_extension_app_data(e); |
| 8621 |
if (tmp) { |
| 8622 |
ast_copy_string(buf, tmp, bufsize); |
| 8623 |
} |
| 8624 |
} |
| 8625 |
ast_unlock_contexts(); |
| 8626 |
return 0; |
| 8627 |
} |
| 8628 |
ast_unlock_contexts(); |
| 8629 |
return -1; |
| 8630 |
} |
| 8631 |
|
| 8632 |
|
| 8633 |
|
| 8634 |
|
| 8635 |
struct ast_context *ast_walk_contexts(struct ast_context *con) |
| 8636 |
{ |
| 8637 |
return con ? con->next : contexts; |
| 8638 |
} |
| 8639 |
|
| 8640 |
struct ast_exten *ast_walk_context_extensions(struct ast_context *con, |
| 8641 |
struct ast_exten *exten) |
| 8642 |
{ |
| 8643 |
if (!exten) |
| 8644 |
return con ? con->root : NULL; |
| 8645 |
else |
| 8646 |
return exten->next; |
| 8647 |
} |
| 8648 |
|
| 8649 |
const struct ast_sw *ast_walk_context_switches(const struct ast_context *con, |
| 8650 |
const struct ast_sw *sw) |
| 8651 |
{ |
| 8652 |
if (sw) { |
| 8653 |
int idx; |
| 8654 |
int next = 0; |
| 8655 |
|
| 8656 |
for (idx = 0; idx < ast_context_switches_count(con); idx++) { |
| 8657 |
const struct ast_sw *s = ast_context_switches_get(con, idx); |
| 8658 |
|
| 8659 |
if (next) { |
| 8660 |
return s; |
| 8661 |
} |
| 8662 |
|
| 8663 |
if (sw == s) { |
| 8664 |
next = 1; |
| 8665 |
} |
| 8666 |
} |
| 8667 |
|
| 8668 |
return NULL; |
| 8669 |
} |
| 8670 |
|
| 8671 |
if (!ast_context_switches_count(con)) { |
| 8672 |
return NULL; |
| 8673 |
} |
| 8674 |
|
| 8675 |
return ast_context_switches_get(con, 0); |
| 8676 |
} |
| 8677 |
|
| 8678 |
int ast_context_switches_count(const struct ast_context *con) |
| 8679 |
{ |
| 8680 |
return AST_VECTOR_SIZE(&con->alts); |
| 8681 |
} |
| 8682 |
|
| 8683 |
const struct ast_sw *ast_context_switches_get(const struct ast_context *con, int idx) |
| 8684 |
{ |
| 8685 |
return AST_VECTOR_GET(&con->alts, idx); |
| 8686 |
} |
| 8687 |
|
| 8688 |
struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten, |
| 8689 |
struct ast_exten *priority) |
| 8690 |
{ |
| 8691 |
return priority ? priority->peer : exten; |
| 8692 |
} |
| 8693 |
|
| 8694 |
const struct ast_include *ast_walk_context_includes(const struct ast_context *con, |
| 8695 |
const struct ast_include *inc) |
| 8696 |
{ |
| 8697 |
if (inc) { |
| 8698 |
int idx; |
| 8699 |
int next = 0; |
| 8700 |
|
| 8701 |
for (idx = 0; idx < ast_context_includes_count(con); idx++) { |
| 8702 |
const struct ast_include *include = AST_VECTOR_GET(&con->includes, idx); |
| 8703 |
|
| 8704 |
if (next) { |
| 8705 |
return include; |
| 8706 |
} |
| 8707 |
|
| 8708 |
if (inc == include) { |
| 8709 |
next = 1; |
| 8710 |
} |
| 8711 |
} |
| 8712 |
|
| 8713 |
return NULL; |
| 8714 |
} |
| 8715 |
|
| 8716 |
if (!ast_context_includes_count(con)) { |
| 8717 |
return NULL; |
| 8718 |
} |
| 8719 |
|
| 8720 |
return ast_context_includes_get(con, 0); |
| 8721 |
} |
| 8722 |
|
| 8723 |
int ast_context_includes_count(const struct ast_context *con) |
| 8724 |
{ |
| 8725 |
return AST_VECTOR_SIZE(&con->includes); |
| 8726 |
} |
| 8727 |
|
| 8728 |
const struct ast_include *ast_context_includes_get(const struct ast_context *con, int idx) |
| 8729 |
{ |
| 8730 |
return AST_VECTOR_GET(&con->includes, idx); |
| 8731 |
} |
| 8732 |
|
| 8733 |
const struct ast_ignorepat *ast_walk_context_ignorepats(const struct ast_context *con, |
| 8734 |
const struct ast_ignorepat *ip) |
| 8735 |
{ |
| 8736 |
if (!con) { |
| 8737 |
return NULL; |
| 8738 |
} |
| 8739 |
|
| 8740 |
if (ip) { |
| 8741 |
int idx; |
| 8742 |
int next = 0; |
| 8743 |
|
| 8744 |
for (idx = 0; idx < ast_context_ignorepats_count(con); idx++) { |
| 8745 |
const struct ast_ignorepat *i = ast_context_ignorepats_get(con, idx); |
| 8746 |
|
| 8747 |
if (next) { |
| 8748 |
return i; |
| 8749 |
} |
| 8750 |
|
| 8751 |
if (ip == i) { |
| 8752 |
next = 1; |
| 8753 |
} |
| 8754 |
} |
| 8755 |
|
| 8756 |
return NULL; |
| 8757 |
} |
| 8758 |
|
| 8759 |
if (!ast_context_ignorepats_count(con)) { |
| 8760 |
return NULL; |
| 8761 |
} |
| 8762 |
|
| 8763 |
return ast_context_ignorepats_get(con, 0); |
| 8764 |
} |
| 8765 |
|
| 8766 |
int ast_context_ignorepats_count(const struct ast_context *con) |
| 8767 |
{ |
| 8768 |
return AST_VECTOR_SIZE(&con->ignorepats); |
| 8769 |
} |
| 8770 |
|
| 8771 |
const struct ast_ignorepat *ast_context_ignorepats_get(const struct ast_context *con, int idx) |
| 8772 |
{ |
| 8773 |
return AST_VECTOR_GET(&con->ignorepats, idx); |
| 8774 |
} |
| 8775 |
|
| 8776 |
int ast_context_verify_includes(struct ast_context *con) |
| 8777 |
{ |
| 8778 |
int idx; |
| 8779 |
int res = 0; |
| 8780 |
int includecount = ast_context_includes_count(con); |
| 8781 |
|
| 8782 |
if (includecount >= AST_PBX_MAX_STACK) { |
| 8783 |
ast_log(LOG_WARNING, "Context %s contains too many includes (%d). Maximum is %d.\n", |
| 8784 |
ast_get_context_name(con), includecount, AST_PBX_MAX_STACK); |
| 8785 |
} |
| 8786 |
|
| 8787 |
for (idx = 0; idx < includecount; idx++) { |
| 8788 |
const struct ast_include *inc = ast_context_includes_get(con, idx); |
| 8789 |
|
| 8790 |
if (ast_context_find(include_rname(inc))) { |
| 8791 |
continue; |
| 8792 |
} |
| 8793 |
|
| 8794 |
res = -1; |
| 8795 |
ast_log(LOG_WARNING, "Context '%s' tries to include nonexistent context '%s'\n", |
| 8796 |
ast_get_context_name(con), include_rname(inc)); |
| 8797 |
break; |
| 8798 |
} |
| 8799 |
|
| 8800 |
return res; |
| 8801 |
} |
| 8802 |
|
| 8803 |
|
| 8804 |
static int __ast_goto_if_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, int async) |
| 8805 |
{ |
| 8806 |
int (*goto_func)(struct ast_channel *chan, const char *context, const char *exten, int priority); |
| 8807 |
|
| 8808 |
if (!chan) |
| 8809 |
return -2; |
| 8810 |
|
| 8811 |
if (context == NULL) |
| 8812 |
context = ast_channel_context(chan); |
| 8813 |
if (exten == NULL) |
| 8814 |
exten = ast_channel_exten(chan); |
| 8815 |
|
| 8816 |
goto_func = (async) ? ast_async_goto : ast_explicit_goto; |
| 8817 |
if (ast_exists_extension(chan, context, exten, priority, |
| 8818 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) |
| 8819 |
return goto_func(chan, context, exten, priority); |
| 8820 |
else { |
| 8821 |
return AST_PBX_GOTO_FAILED; |
| 8822 |
} |
| 8823 |
} |
| 8824 |
|
| 8825 |
int ast_goto_if_exists(struct ast_channel *chan, const char* context, const char *exten, int priority) |
| 8826 |
{ |
| 8827 |
return __ast_goto_if_exists(chan, context, exten, priority, 0); |
| 8828 |
} |
| 8829 |
|
| 8830 |
int ast_async_goto_if_exists(struct ast_channel *chan, const char * context, const char *exten, int priority) |
| 8831 |
{ |
| 8832 |
return __ast_goto_if_exists(chan, context, exten, priority, 1); |
| 8833 |
} |
| 8834 |
|
| 8835 |
int pbx_parse_location(struct ast_channel *chan, char **contextp, char **extenp, char **prip, int *ipri, int *mode, char *rest) |
| 8836 |
{ |
| 8837 |
char *context, *exten, *pri; |
| 8838 |
|
| 8839 |
if (!*extenp) { |
| 8840 |
|
| 8841 |
*prip = *contextp; |
| 8842 |
*extenp = NULL; |
| 8843 |
*contextp = NULL; |
| 8844 |
} else if (!*prip) { |
| 8845 |
|
| 8846 |
*prip = *extenp; |
| 8847 |
*extenp = *contextp; |
| 8848 |
*contextp = NULL; |
| 8849 |
} |
| 8850 |
context = *contextp; |
| 8851 |
exten = *extenp; |
| 8852 |
pri = *prip; |
| 8853 |
if (mode) { |
| 8854 |
if (*pri == '+') { |
| 8855 |
*mode = 1; |
| 8856 |
pri++; |
| 8857 |
} else if (*pri == '-') { |
| 8858 |
*mode = -1; |
| 8859 |
pri++; |
| 8860 |
} |
| 8861 |
} |
| 8862 |
if ((rest && sscanf(pri, "%30d%1s", ipri, rest) != 1) || sscanf(pri, "%30d", ipri) != 1) { |
| 8863 |
*ipri = ast_findlabel_extension(chan, context ? context : ast_channel_context(chan), |
| 8864 |
exten ? exten : ast_channel_exten(chan), pri, |
| 8865 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL)); |
| 8866 |
if (*ipri < 1) { |
| 8867 |
ast_log(LOG_WARNING, "Priority '%s' must be a number > 0, or valid label\n", pri); |
| 8868 |
return -1; |
| 8869 |
} else if (mode) { |
| 8870 |
*mode = 0; |
| 8871 |
} |
| 8872 |
} |
| 8873 |
return 0; |
| 8874 |
} |
| 8875 |
|
| 8876 |
static int pbx_parseable_goto(struct ast_channel *chan, const char *goto_string, int async) |
| 8877 |
{ |
| 8878 |
char *exten, *pri, *context; |
| 8879 |
char *stringp; |
| 8880 |
int ipri; |
| 8881 |
int mode = 0; |
| 8882 |
char rest[2] = ""; |
| 8883 |
|
| 8884 |
if (ast_strlen_zero(goto_string)) { |
| 8885 |
ast_log(LOG_WARNING, "Goto requires an argument ([[context,]extension,]priority)\n"); |
| 8886 |
return -1; |
| 8887 |
} |
| 8888 |
stringp = ast_strdupa(goto_string); |
| 8889 |
context = strsep(&stringp, ","); |
| 8890 |
exten = strsep(&stringp, ","); |
| 8891 |
pri = strsep(&stringp, ","); |
| 8892 |
|
| 8893 |
if (pbx_parse_location(chan, &context, &exten, &pri, &ipri, &mode, rest)) { |
| 8894 |
return -1; |
| 8895 |
} |
| 8896 |
|
| 8897 |
|
| 8898 |
if (mode) |
| 8899 |
ipri = ast_channel_priority(chan) + (ipri * mode); |
| 8900 |
|
| 8901 |
if (async) |
| 8902 |
ast_async_goto(chan, context, exten, ipri); |
| 8903 |
else |
| 8904 |
ast_explicit_goto(chan, context, exten, ipri); |
| 8905 |
|
| 8906 |
return 0; |
| 8907 |
|
| 8908 |
} |
| 8909 |
|
| 8910 |
int ast_parseable_goto(struct ast_channel *chan, const char *goto_string) |
| 8911 |
{ |
| 8912 |
return pbx_parseable_goto(chan, goto_string, 0); |
| 8913 |
} |
| 8914 |
|
| 8915 |
int ast_async_parseable_goto(struct ast_channel *chan, const char *goto_string) |
| 8916 |
{ |
| 8917 |
return pbx_parseable_goto(chan, goto_string, 1); |
| 8918 |
} |
| 8919 |
|
| 8920 |
static int hint_hash(const void *obj, const int flags) |
| 8921 |
{ |
| 8922 |
const struct ast_hint *hint = obj; |
| 8923 |
const char *exten_name; |
| 8924 |
int res; |
| 8925 |
|
| 8926 |
exten_name = ast_get_extension_name(hint->exten); |
| 8927 |
if (ast_strlen_zero(exten_name)) { |
| 8928 |
|
| 8929 |
|
| 8930 |
|
| 8931 |
|
| 8932 |
res = 0; |
| 8933 |
} else { |
| 8934 |
res = ast_str_case_hash(exten_name); |
| 8935 |
} |
| 8936 |
|
| 8937 |
return res; |
| 8938 |
} |
| 8939 |
|
| 8940 |
static int hint_cmp(void *obj, void *arg, int flags) |
| 8941 |
{ |
| 8942 |
const struct ast_hint *hint = obj; |
| 8943 |
const struct ast_exten *exten = arg; |
| 8944 |
|
| 8945 |
return (hint->exten == exten) ? CMP_MATCH | CMP_STOP : 0; |
| 8946 |
} |
| 8947 |
|
| 8948 |
static int statecbs_cmp(void *obj, void *arg, int flags) |
| 8949 |
{ |
| 8950 |
const struct ast_state_cb *state_cb = obj; |
| 8951 |
ast_state_cb_type change_cb = arg; |
| 8952 |
|
| 8953 |
return (state_cb->change_cb == change_cb) ? CMP_MATCH | CMP_STOP : 0; |
| 8954 |
} |
| 8955 |
|
| 8956 |
|
| 8957 |
|
| 8958 |
|
| 8959 |
|
| 8960 |
static void pbx_shutdown(void) |
| 8961 |
{ |
| 8962 |
STASIS_MESSAGE_TYPE_CLEANUP(hint_change_message_type); |
| 8963 |
STASIS_MESSAGE_TYPE_CLEANUP(hint_remove_message_type); |
| 8964 |
|
| 8965 |
if (hints) { |
| 8966 |
ao2_container_unregister("hints"); |
| 8967 |
ao2_ref(hints, -1); |
| 8968 |
hints = NULL; |
| 8969 |
} |
| 8970 |
if (hintdevices) { |
| 8971 |
ao2_container_unregister("hintdevices"); |
| 8972 |
ao2_ref(hintdevices, -1); |
| 8973 |
hintdevices = NULL; |
| 8974 |
} |
| 8975 |
if (autohints) { |
| 8976 |
ao2_container_unregister("autohints"); |
| 8977 |
ao2_ref(autohints, -1); |
| 8978 |
autohints = NULL; |
| 8979 |
} |
| 8980 |
if (statecbs) { |
| 8981 |
ao2_container_unregister("statecbs"); |
| 8982 |
ao2_ref(statecbs, -1); |
| 8983 |
statecbs = NULL; |
| 8984 |
} |
| 8985 |
if (contexts_table) { |
| 8986 |
ast_hashtab_destroy(contexts_table, NULL); |
| 8987 |
} |
| 8988 |
} |
| 8989 |
|
| 8990 |
static void print_hints_key(void *v_obj, void *where, ao2_prnt_fn *prnt) |
| 8991 |
{ |
| 8992 |
struct ast_hint *hint = v_obj; |
| 8993 |
|
| 8994 |
if (!hint) { |
| 8995 |
return; |
| 8996 |
} |
| 8997 |
prnt(where, "%s@%s", ast_get_extension_name(hint->exten), |
| 8998 |
ast_get_context_name(ast_get_extension_context(hint->exten))); |
| 8999 |
} |
| 9000 |
|
| 9001 |
static void print_hintdevices_key(void *v_obj, void *where, ao2_prnt_fn *prnt) |
| 9002 |
{ |
| 9003 |
struct ast_hintdevice *hintdevice = v_obj; |
| 9004 |
|
| 9005 |
if (!hintdevice) { |
| 9006 |
return; |
| 9007 |
} |
| 9008 |
prnt(where, "%s => %s@%s", hintdevice->hintdevice, |
| 9009 |
ast_get_extension_name(hintdevice->hint->exten), |
| 9010 |
ast_get_context_name(ast_get_extension_context(hintdevice->hint->exten))); |
| 9011 |
} |
| 9012 |
|
| 9013 |
static void print_autohint_key(void *v_obj, void *where, ao2_prnt_fn *prnt) |
| 9014 |
{ |
| 9015 |
struct ast_autohint *autohint = v_obj; |
| 9016 |
|
| 9017 |
if (!autohint) { |
| 9018 |
return; |
| 9019 |
} |
| 9020 |
prnt(where, "%s", autohint->context); |
| 9021 |
} |
| 9022 |
|
| 9023 |
static void print_statecbs_key(void *v_obj, void *where, ao2_prnt_fn *prnt) |
| 9024 |
{ |
| 9025 |
struct ast_state_cb *state_cb = v_obj; |
| 9026 |
|
| 9027 |
if (!state_cb) { |
| 9028 |
return; |
| 9029 |
} |
| 9030 |
prnt(where, "%d", state_cb->id); |
| 9031 |
} |
| 9032 |
|
| 9033 |
int ast_pbx_init(void) |
| 9034 |
{ |
| 9035 |
hints = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 9036 |
HASH_EXTENHINT_SIZE, hint_hash, NULL, hint_cmp); |
| 9037 |
if (hints) { |
| 9038 |
ao2_container_register("hints", hints, print_hints_key); |
| 9039 |
} |
| 9040 |
hintdevices = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 9041 |
HASH_EXTENHINT_SIZE, hintdevice_hash_cb, NULL, hintdevice_cmp_multiple); |
| 9042 |
if (hintdevices) { |
| 9043 |
ao2_container_register("hintdevices", hintdevices, print_hintdevices_key); |
| 9044 |
} |
| 9045 |
|
| 9046 |
autohints = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, HASH_EXTENHINT_SIZE, |
| 9047 |
autohint_hash_cb, NULL, autohint_cmp); |
| 9048 |
if (autohints) { |
| 9049 |
ao2_container_register("autohints", autohints, print_autohint_key); |
| 9050 |
} |
| 9051 |
statecbs = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, statecbs_cmp); |
| 9052 |
if (statecbs) { |
| 9053 |
ao2_container_register("statecbs", statecbs, print_statecbs_key); |
| 9054 |
} |
| 9055 |
|
| 9056 |
ast_register_cleanup(pbx_shutdown); |
| 9057 |
|
| 9058 |
if (STASIS_MESSAGE_TYPE_INIT(hint_change_message_type) != 0) { |
| 9059 |
return -1; |
| 9060 |
} |
| 9061 |
if (STASIS_MESSAGE_TYPE_INIT(hint_remove_message_type) != 0) { |
| 9062 |
return -1; |
| 9063 |
} |
| 9064 |
|
| 9065 |
return (hints && hintdevices && autohints && statecbs) ? 0 : -1; |
| 9066 |
} |