| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#include "asterisk.h" |
| 34 |
|
| 35 |
#include "asterisk/pbx.h" |
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/app.h" |
| 38 |
#include "asterisk/manager.h" |
| 39 |
#include "asterisk/channel.h" |
| 40 |
#include "asterisk/agi.h" |
| 41 |
#include "asterisk/stasis_channels.h" |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 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 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
static const char app_gosub[] = "Gosub"; |
| 256 |
static const char app_gosubif[] = "GosubIf"; |
| 257 |
static const char app_return[] = "Return"; |
| 258 |
static const char app_pop[] = "StackPop"; |
| 259 |
|
| 260 |
static void gosub_free(void *data); |
| 261 |
|
| 262 |
static const struct ast_datastore_info stack_info = { |
| 263 |
.type = "GOSUB", |
| 264 |
.destroy = gosub_free, |
| 265 |
}; |
| 266 |
|
| 267 |
struct gosub_stack_frame { |
| 268 |
AST_LIST_ENTRY(gosub_stack_frame) entries; |
| 269 |
|
| 270 |
unsigned char arguments; |
| 271 |
struct varshead varshead; |
| 272 |
int priority; |
| 273 |
|
| 274 |
unsigned int is_special:1; |
| 275 |
|
| 276 |
unsigned int in_subroutine:1; |
| 277 |
char *context; |
| 278 |
char extension[0]; |
| 279 |
}; |
| 280 |
|
| 281 |
AST_LIST_HEAD(gosub_stack_list, gosub_stack_frame); |
| 282 |
|
| 283 |
static int frame_set_var(struct ast_channel *chan, struct gosub_stack_frame *frame, const char *var, const char *value) |
| 284 |
{ |
| 285 |
struct ast_var_t *variables; |
| 286 |
int found = 0; |
| 287 |
int len; |
| 288 |
RAII_VAR(char *, local_buffer, NULL, ast_free); |
| 289 |
|
| 290 |
|
| 291 |
AST_LIST_TRAVERSE(&frame->varshead, variables, entries) { |
| 292 |
if (!strcmp(var, ast_var_name(variables))) { |
| 293 |
found = 1; |
| 294 |
break; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
if (!found) { |
| 299 |
if ((variables = ast_var_assign(var, ""))) { |
| 300 |
AST_LIST_INSERT_HEAD(&frame->varshead, variables, entries); |
| 301 |
} |
| 302 |
pbx_builtin_pushvar_helper(chan, var, value); |
| 303 |
} else { |
| 304 |
pbx_builtin_setvar_helper(chan, var, value); |
| 305 |
} |
| 306 |
|
| 307 |
len = 8 + strlen(var); |
| 308 |
local_buffer = ast_malloc(len); |
| 309 |
if (!local_buffer) { |
| 310 |
return 0; |
| 311 |
} |
| 312 |
sprintf(local_buffer, "LOCAL(%s)", var); |
| 313 |
ast_channel_publish_varset(chan, local_buffer, value); |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
static void gosub_release_frame(struct ast_channel *chan, struct gosub_stack_frame *frame) |
| 318 |
{ |
| 319 |
struct ast_var_t *vardata; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
while ((vardata = AST_LIST_REMOVE_HEAD(&frame->varshead, entries))) { |
| 328 |
if (chan) |
| 329 |
pbx_builtin_setvar_helper(chan, ast_var_name(vardata), NULL); |
| 330 |
ast_var_delete(vardata); |
| 331 |
} |
| 332 |
|
| 333 |
ast_free(frame); |
| 334 |
} |
| 335 |
|
| 336 |
static struct gosub_stack_frame *gosub_allocate_frame(const char *context, const char *extension, int priority, int in_subroutine, unsigned char arguments) |
| 337 |
{ |
| 338 |
struct gosub_stack_frame *new = NULL; |
| 339 |
int len_extension = strlen(extension) + 1; |
| 340 |
int len_context = strlen(context) + 1; |
| 341 |
|
| 342 |
if ((new = ast_calloc(1, sizeof(*new) + len_extension + len_context))) { |
| 343 |
AST_LIST_HEAD_INIT_NOLOCK(&new->varshead); |
| 344 |
ast_copy_string(new->extension, extension, len_extension); |
| 345 |
new->context = new->extension + len_extension; |
| 346 |
ast_copy_string(new->context, context, len_context); |
| 347 |
new->priority = priority; |
| 348 |
new->in_subroutine = in_subroutine ? 1 : 0; |
| 349 |
new->arguments = arguments; |
| 350 |
} |
| 351 |
return new; |
| 352 |
} |
| 353 |
|
| 354 |
static void gosub_free(void *data) |
| 355 |
{ |
| 356 |
struct gosub_stack_list *oldlist = data; |
| 357 |
struct gosub_stack_frame *oldframe; |
| 358 |
|
| 359 |
AST_LIST_LOCK(oldlist); |
| 360 |
while ((oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries))) { |
| 361 |
gosub_release_frame(NULL, oldframe); |
| 362 |
} |
| 363 |
AST_LIST_UNLOCK(oldlist); |
| 364 |
AST_LIST_HEAD_DESTROY(oldlist); |
| 365 |
ast_free(oldlist); |
| 366 |
} |
| 367 |
|
| 368 |
static int pop_exec(struct ast_channel *chan, const char *data) |
| 369 |
{ |
| 370 |
struct ast_datastore *stack_store; |
| 371 |
struct gosub_stack_frame *oldframe; |
| 372 |
struct gosub_stack_list *oldlist; |
| 373 |
int res = 0; |
| 374 |
|
| 375 |
ast_channel_lock(chan); |
| 376 |
if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) { |
| 377 |
ast_log(LOG_WARNING, "%s called with no gosub stack allocated.\n", app_pop); |
| 378 |
ast_channel_unlock(chan); |
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
oldlist = stack_store->data; |
| 383 |
AST_LIST_LOCK(oldlist); |
| 384 |
oldframe = AST_LIST_FIRST(oldlist); |
| 385 |
if (oldframe) { |
| 386 |
if (oldframe->is_special) { |
| 387 |
ast_debug(1, "%s attempted to pop special return location.\n", app_pop); |
| 388 |
|
| 389 |
|
| 390 |
res = -1; |
| 391 |
} else { |
| 392 |
AST_LIST_REMOVE_HEAD(oldlist, entries); |
| 393 |
gosub_release_frame(chan, oldframe); |
| 394 |
} |
| 395 |
} else { |
| 396 |
ast_debug(1, "%s called with an empty gosub stack\n", app_pop); |
| 397 |
} |
| 398 |
AST_LIST_UNLOCK(oldlist); |
| 399 |
ast_channel_unlock(chan); |
| 400 |
return res; |
| 401 |
} |
| 402 |
|
| 403 |
static int return_exec(struct ast_channel *chan, const char *data) |
| 404 |
{ |
| 405 |
struct ast_datastore *stack_store; |
| 406 |
struct gosub_stack_frame *oldframe; |
| 407 |
struct gosub_stack_list *oldlist; |
| 408 |
const char *retval = data; |
| 409 |
int res = 0; |
| 410 |
|
| 411 |
ast_channel_lock(chan); |
| 412 |
if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) { |
| 413 |
ast_log(LOG_ERROR, "Return without Gosub: stack is unallocated\n"); |
| 414 |
ast_channel_unlock(chan); |
| 415 |
return -1; |
| 416 |
} |
| 417 |
|
| 418 |
oldlist = stack_store->data; |
| 419 |
AST_LIST_LOCK(oldlist); |
| 420 |
oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries); |
| 421 |
AST_LIST_UNLOCK(oldlist); |
| 422 |
|
| 423 |
if (!oldframe) { |
| 424 |
ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n"); |
| 425 |
ast_channel_unlock(chan); |
| 426 |
return -1; |
| 427 |
} |
| 428 |
if (oldframe->is_special) { |
| 429 |
|
| 430 |
res = -1; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
ast_channel_context_set(chan, oldframe->context); |
| 439 |
ast_channel_exten_set(chan, oldframe->extension); |
| 440 |
if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP)) { |
| 441 |
--oldframe->priority; |
| 442 |
} |
| 443 |
ast_channel_priority_set(chan, oldframe->priority); |
| 444 |
ast_set2_flag(ast_channel_flags(chan), oldframe->in_subroutine, AST_FLAG_SUBROUTINE_EXEC); |
| 445 |
|
| 446 |
gosub_release_frame(chan, oldframe); |
| 447 |
|
| 448 |
|
| 449 |
pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", S_OR(retval, "")); |
| 450 |
ast_channel_unlock(chan); |
| 451 |
return res; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
static const char *expand_gosub_args(struct ast_channel *chan, const char *args) |
| 474 |
{ |
| 475 |
int len; |
| 476 |
char *parse; |
| 477 |
char *label; |
| 478 |
char *new_args; |
| 479 |
const char *context; |
| 480 |
const char *exten; |
| 481 |
const char *pri; |
| 482 |
|
| 483 |
|
| 484 |
parse = ast_strdupa(args); |
| 485 |
label = strsep(&parse, "("); |
| 486 |
if (parse) { |
| 487 |
char *endparen; |
| 488 |
|
| 489 |
endparen = strrchr(parse, ')'); |
| 490 |
if (endparen) { |
| 491 |
*endparen = '\0'; |
| 492 |
} else { |
| 493 |
ast_log(LOG_WARNING, "Ouch. No closing paren: '%s'?\n", args); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
context = strsep(&label, ","); |
| 499 |
exten = strsep(&label, ","); |
| 500 |
pri = strsep(&label, ","); |
| 501 |
if (!exten) { |
| 502 |
|
| 503 |
pri = context; |
| 504 |
exten = NULL; |
| 505 |
context = NULL; |
| 506 |
} else if (!pri) { |
| 507 |
|
| 508 |
pri = exten; |
| 509 |
exten = context; |
| 510 |
context = NULL; |
| 511 |
} |
| 512 |
|
| 513 |
ast_channel_lock(chan); |
| 514 |
if (ast_strlen_zero(exten)) { |
| 515 |
exten = ast_channel_exten(chan); |
| 516 |
} |
| 517 |
if (ast_strlen_zero(context)) { |
| 518 |
context = ast_channel_context(chan); |
| 519 |
} |
| 520 |
len = strlen(context) + strlen(exten) + strlen(pri) + 3; |
| 521 |
if (!ast_strlen_zero(parse)) { |
| 522 |
len += 2 + strlen(parse); |
| 523 |
} |
| 524 |
new_args = ast_malloc(len); |
| 525 |
if (new_args) { |
| 526 |
if (ast_strlen_zero(parse)) { |
| 527 |
snprintf(new_args, len, "%s,%s,%s", context, exten, pri); |
| 528 |
} else { |
| 529 |
snprintf(new_args, len, "%s,%s,%s(%s)", context, exten, pri, parse); |
| 530 |
} |
| 531 |
} |
| 532 |
ast_channel_unlock(chan); |
| 533 |
|
| 534 |
ast_debug(4, "Gosub args:%s new_args:%s\n", args, new_args ? new_args : ""); |
| 535 |
|
| 536 |
return new_args; |
| 537 |
} |
| 538 |
|
| 539 |
static int gosub_exec(struct ast_channel *chan, const char *data) |
| 540 |
{ |
| 541 |
struct ast_datastore *stack_store; |
| 542 |
struct gosub_stack_list *oldlist; |
| 543 |
struct gosub_stack_frame *newframe; |
| 544 |
struct gosub_stack_frame *lastframe; |
| 545 |
char argname[15]; |
| 546 |
char *parse; |
| 547 |
char *label; |
| 548 |
char *caller_id; |
| 549 |
char *orig_context; |
| 550 |
char *orig_exten; |
| 551 |
char *dest_context; |
| 552 |
char *dest_exten; |
| 553 |
int orig_in_subroutine; |
| 554 |
int orig_priority; |
| 555 |
int dest_priority; |
| 556 |
int i; |
| 557 |
int max_argc = 0; |
| 558 |
AST_DECLARE_APP_ARGS(args2, |
| 559 |
AST_APP_ARG(argval)[100]; |
| 560 |
); |
| 561 |
|
| 562 |
if (ast_strlen_zero(data)) { |
| 563 |
ast_log(LOG_ERROR, "%s requires an argument: %s([[context,]exten,]priority[(arg1[,...][,argN])])\n", app_gosub, app_gosub); |
| 564 |
return -1; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
parse = ast_strdupa(data); |
| 574 |
label = strsep(&parse, "("); |
| 575 |
if (parse) { |
| 576 |
char *endparen; |
| 577 |
|
| 578 |
endparen = strrchr(parse, ')'); |
| 579 |
if (endparen) { |
| 580 |
*endparen = '\0'; |
| 581 |
} else { |
| 582 |
ast_log(LOG_WARNING, "Ouch. No closing paren: '%s'?\n", data); |
| 583 |
} |
| 584 |
AST_STANDARD_RAW_ARGS(args2, parse); |
| 585 |
} else { |
| 586 |
args2.argc = 0; |
| 587 |
} |
| 588 |
|
| 589 |
ast_channel_lock(chan); |
| 590 |
orig_context = ast_strdupa(ast_channel_context(chan)); |
| 591 |
orig_exten = ast_strdupa(ast_channel_exten(chan)); |
| 592 |
orig_priority = ast_channel_priority(chan); |
| 593 |
orig_in_subroutine = ast_test_flag(ast_channel_flags(chan), AST_FLAG_SUBROUTINE_EXEC); |
| 594 |
ast_channel_unlock(chan); |
| 595 |
|
| 596 |
if (ast_parseable_goto(chan, label)) { |
| 597 |
ast_log(LOG_ERROR, "%s address is invalid: '%s'\n", app_gosub, data); |
| 598 |
goto error_exit; |
| 599 |
} |
| 600 |
|
| 601 |
ast_channel_lock(chan); |
| 602 |
dest_context = ast_strdupa(ast_channel_context(chan)); |
| 603 |
dest_exten = ast_strdupa(ast_channel_exten(chan)); |
| 604 |
dest_priority = ast_channel_priority(chan); |
| 605 |
if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP)) { |
| 606 |
++dest_priority; |
| 607 |
} |
| 608 |
caller_id = S_COR(ast_channel_caller(chan)->id.number.valid, |
| 609 |
ast_channel_caller(chan)->id.number.str, NULL); |
| 610 |
if (caller_id) { |
| 611 |
caller_id = ast_strdupa(caller_id); |
| 612 |
} |
| 613 |
ast_channel_unlock(chan); |
| 614 |
|
| 615 |
if (!ast_exists_extension(chan, dest_context, dest_exten, dest_priority, caller_id)) { |
| 616 |
ast_log(LOG_ERROR, "%s attempted to reach non-existent destination '%s,%s,%d' from '%s,%s,%d'", |
| 617 |
app_gosub, dest_context, dest_exten, dest_priority, orig_context, orig_exten, orig_priority); |
| 618 |
goto error_exit; |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
ast_channel_lock(chan); |
| 624 |
|
| 625 |
|
| 626 |
if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) { |
| 627 |
ast_debug(1, "Channel %s has no datastore, so we're allocating one.\n", |
| 628 |
ast_channel_name(chan)); |
| 629 |
stack_store = ast_datastore_alloc(&stack_info, NULL); |
| 630 |
if (!stack_store) { |
| 631 |
ast_log(LOG_ERROR, "Unable to allocate new datastore. %s failed.\n", |
| 632 |
app_gosub); |
| 633 |
goto error_exit_locked; |
| 634 |
} |
| 635 |
|
| 636 |
oldlist = ast_calloc(1, sizeof(*oldlist)); |
| 637 |
if (!oldlist) { |
| 638 |
ast_log(LOG_ERROR, "Unable to allocate datastore list head. %s failed.\n", |
| 639 |
app_gosub); |
| 640 |
ast_datastore_free(stack_store); |
| 641 |
goto error_exit_locked; |
| 642 |
} |
| 643 |
AST_LIST_HEAD_INIT(oldlist); |
| 644 |
|
| 645 |
stack_store->data = oldlist; |
| 646 |
ast_channel_datastore_add(chan, stack_store); |
| 647 |
} else { |
| 648 |
oldlist = stack_store->data; |
| 649 |
} |
| 650 |
|
| 651 |
if ((lastframe = AST_LIST_FIRST(oldlist))) { |
| 652 |
max_argc = lastframe->arguments; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
if (args2.argc > max_argc) { |
| 657 |
max_argc = args2.argc; |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
newframe = gosub_allocate_frame(orig_context, orig_exten, orig_priority + 1, orig_in_subroutine, max_argc); |
| 662 |
if (!newframe) { |
| 663 |
goto error_exit_locked; |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
for (i = 0; i < max_argc; i++) { |
| 668 |
snprintf(argname, sizeof(argname), "ARG%d", i + 1); |
| 669 |
frame_set_var(chan, newframe, argname, i < args2.argc ? args2.argval[i] : ""); |
| 670 |
ast_debug(1, "Setting '%s' to '%s'\n", argname, i < args2.argc ? args2.argval[i] : ""); |
| 671 |
} |
| 672 |
snprintf(argname, sizeof(argname), "%u", args2.argc); |
| 673 |
frame_set_var(chan, newframe, "ARGC", argname); |
| 674 |
|
| 675 |
ast_set_flag(ast_channel_flags(chan), AST_FLAG_SUBROUTINE_EXEC); |
| 676 |
|
| 677 |
|
| 678 |
AST_LIST_LOCK(oldlist); |
| 679 |
AST_LIST_INSERT_HEAD(oldlist, newframe, entries); |
| 680 |
AST_LIST_UNLOCK(oldlist); |
| 681 |
ast_channel_unlock(chan); |
| 682 |
|
| 683 |
return 0; |
| 684 |
|
| 685 |
error_exit: |
| 686 |
ast_channel_lock(chan); |
| 687 |
|
| 688 |
error_exit_locked: |
| 689 |
|
| 690 |
ast_channel_context_set(chan, orig_context); |
| 691 |
ast_channel_exten_set(chan, orig_exten); |
| 692 |
ast_channel_priority_set(chan, orig_priority); |
| 693 |
ast_channel_unlock(chan); |
| 694 |
return -1; |
| 695 |
} |
| 696 |
|
| 697 |
static int gosubif_exec(struct ast_channel *chan, const char *data) |
| 698 |
{ |
| 699 |
char *args; |
| 700 |
int res=0; |
| 701 |
AST_DECLARE_APP_ARGS(cond, |
| 702 |
AST_APP_ARG(ition); |
| 703 |
AST_APP_ARG(labels); |
| 704 |
); |
| 705 |
AST_DECLARE_APP_ARGS(label, |
| 706 |
AST_APP_ARG(iftrue); |
| 707 |
AST_APP_ARG(iffalse); |
| 708 |
); |
| 709 |
|
| 710 |
if (ast_strlen_zero(data)) { |
| 711 |
ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n"); |
| 712 |
return 0; |
| 713 |
} |
| 714 |
|
| 715 |
args = ast_strdupa(data); |
| 716 |
AST_NONSTANDARD_RAW_ARGS(cond, args, '?'); |
| 717 |
if (cond.argc != 2) { |
| 718 |
ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n"); |
| 719 |
return 0; |
| 720 |
} |
| 721 |
|
| 722 |
AST_NONSTANDARD_RAW_ARGS(label, cond.labels, ':'); |
| 723 |
|
| 724 |
if (pbx_checkcondition(cond.ition)) { |
| 725 |
if (!ast_strlen_zero(label.iftrue)) |
| 726 |
res = gosub_exec(chan, label.iftrue); |
| 727 |
} else if (!ast_strlen_zero(label.iffalse)) { |
| 728 |
res = gosub_exec(chan, label.iffalse); |
| 729 |
} |
| 730 |
|
| 731 |
return res; |
| 732 |
} |
| 733 |
|
| 734 |
static int local_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 735 |
{ |
| 736 |
struct ast_datastore *stack_store; |
| 737 |
struct gosub_stack_list *oldlist; |
| 738 |
struct gosub_stack_frame *frame; |
| 739 |
struct ast_var_t *variables; |
| 740 |
|
| 741 |
if (!chan) { |
| 742 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 743 |
return -1; |
| 744 |
} |
| 745 |
|
| 746 |
ast_channel_lock(chan); |
| 747 |
if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) { |
| 748 |
ast_channel_unlock(chan); |
| 749 |
return -1; |
| 750 |
} |
| 751 |
|
| 752 |
oldlist = stack_store->data; |
| 753 |
AST_LIST_LOCK(oldlist); |
| 754 |
if (!(frame = AST_LIST_FIRST(oldlist))) { |
| 755 |
|
| 756 |
AST_LIST_UNLOCK(oldlist); |
| 757 |
ast_channel_unlock(chan); |
| 758 |
return -1; |
| 759 |
} |
| 760 |
|
| 761 |
AST_LIST_TRAVERSE(&frame->varshead, variables, entries) { |
| 762 |
if (!strcmp(data, ast_var_name(variables))) { |
| 763 |
const char *tmp; |
| 764 |
tmp = pbx_builtin_getvar_helper(chan, data); |
| 765 |
ast_copy_string(buf, S_OR(tmp, ""), len); |
| 766 |
break; |
| 767 |
} |
| 768 |
} |
| 769 |
AST_LIST_UNLOCK(oldlist); |
| 770 |
ast_channel_unlock(chan); |
| 771 |
return 0; |
| 772 |
} |
| 773 |
|
| 774 |
static int local_write(struct ast_channel *chan, const char *cmd, char *var, const char *value) |
| 775 |
{ |
| 776 |
struct ast_datastore *stack_store; |
| 777 |
struct gosub_stack_list *oldlist; |
| 778 |
struct gosub_stack_frame *frame; |
| 779 |
|
| 780 |
if (!chan) { |
| 781 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 782 |
return -1; |
| 783 |
} |
| 784 |
|
| 785 |
ast_channel_lock(chan); |
| 786 |
if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) { |
| 787 |
ast_log(LOG_ERROR, "Tried to set LOCAL(%s), but we aren't within a Gosub routine\n", var); |
| 788 |
ast_channel_unlock(chan); |
| 789 |
return -1; |
| 790 |
} |
| 791 |
|
| 792 |
oldlist = stack_store->data; |
| 793 |
AST_LIST_LOCK(oldlist); |
| 794 |
frame = AST_LIST_FIRST(oldlist); |
| 795 |
|
| 796 |
if (frame) { |
| 797 |
frame_set_var(chan, frame, var, value); |
| 798 |
} |
| 799 |
|
| 800 |
AST_LIST_UNLOCK(oldlist); |
| 801 |
ast_channel_unlock(chan); |
| 802 |
|
| 803 |
return 0; |
| 804 |
} |
| 805 |
|
| 806 |
static struct ast_custom_function local_function = { |
| 807 |
.name = "LOCAL", |
| 808 |
.write = local_write, |
| 809 |
.read = local_read, |
| 810 |
}; |
| 811 |
|
| 812 |
static int peek_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 813 |
{ |
| 814 |
int found = 0, n; |
| 815 |
struct ast_var_t *variables; |
| 816 |
AST_DECLARE_APP_ARGS(args, |
| 817 |
AST_APP_ARG(n); |
| 818 |
AST_APP_ARG(name); |
| 819 |
); |
| 820 |
|
| 821 |
if (!chan) { |
| 822 |
ast_log(LOG_ERROR, "LOCAL_PEEK must be called on an active channel\n"); |
| 823 |
return -1; |
| 824 |
} |
| 825 |
|
| 826 |
AST_STANDARD_RAW_ARGS(args, data); |
| 827 |
|
| 828 |
if (ast_strlen_zero(args.n) || ast_strlen_zero(args.name)) { |
| 829 |
ast_log(LOG_ERROR, "LOCAL_PEEK requires parameters n and varname\n"); |
| 830 |
return -1; |
| 831 |
} |
| 832 |
|
| 833 |
n = atoi(args.n); |
| 834 |
*buf = '\0'; |
| 835 |
|
| 836 |
ast_channel_lock(chan); |
| 837 |
AST_LIST_TRAVERSE(ast_channel_varshead(chan), variables, entries) { |
| 838 |
if (!strcmp(args.name, ast_var_name(variables)) && ++found > n) { |
| 839 |
ast_copy_string(buf, ast_var_value(variables), len); |
| 840 |
break; |
| 841 |
} |
| 842 |
} |
| 843 |
ast_channel_unlock(chan); |
| 844 |
return 0; |
| 845 |
} |
| 846 |
|
| 847 |
static struct ast_custom_function peek_function = { |
| 848 |
.name = "LOCAL_PEEK", |
| 849 |
.read = peek_read, |
| 850 |
}; |
| 851 |
|
| 852 |
static int stackpeek_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len) |
| 853 |
{ |
| 854 |
struct ast_datastore *stack_store; |
| 855 |
struct gosub_stack_list *oldlist; |
| 856 |
struct gosub_stack_frame *frame; |
| 857 |
int n; |
| 858 |
AST_DECLARE_APP_ARGS(args, |
| 859 |
AST_APP_ARG(n); |
| 860 |
AST_APP_ARG(which); |
| 861 |
AST_APP_ARG(suppress); |
| 862 |
); |
| 863 |
|
| 864 |
if (!chan) { |
| 865 |
ast_log(LOG_ERROR, "STACK_PEEK must be called on an active channel\n"); |
| 866 |
return -1; |
| 867 |
} |
| 868 |
|
| 869 |
data = ast_strdupa(data); |
| 870 |
AST_STANDARD_APP_ARGS(args, data); |
| 871 |
|
| 872 |
if (ast_strlen_zero(args.n) || ast_strlen_zero(args.which)) { |
| 873 |
ast_log(LOG_ERROR, "STACK_PEEK requires parameters n and which\n"); |
| 874 |
return -1; |
| 875 |
} |
| 876 |
|
| 877 |
n = atoi(args.n); |
| 878 |
if (n <= 0) { |
| 879 |
ast_log(LOG_ERROR, "STACK_PEEK must be called with a positive peek value\n"); |
| 880 |
return -1; |
| 881 |
} |
| 882 |
|
| 883 |
ast_channel_lock(chan); |
| 884 |
if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) { |
| 885 |
if (!ast_true(args.suppress)) { |
| 886 |
ast_log(LOG_ERROR, "STACK_PEEK called on a channel without a gosub stack\n"); |
| 887 |
} |
| 888 |
ast_channel_unlock(chan); |
| 889 |
return -1; |
| 890 |
} |
| 891 |
|
| 892 |
oldlist = stack_store->data; |
| 893 |
|
| 894 |
AST_LIST_LOCK(oldlist); |
| 895 |
AST_LIST_TRAVERSE(oldlist, frame, entries) { |
| 896 |
if (--n == 0) { |
| 897 |
break; |
| 898 |
} |
| 899 |
} |
| 900 |
|
| 901 |
if (!frame) { |
| 902 |
|
| 903 |
if (!ast_true(args.suppress)) { |
| 904 |
ast_log(LOG_ERROR, "Stack peek of '%s' is more stack frames than I have\n", args.n); |
| 905 |
} |
| 906 |
AST_LIST_UNLOCK(oldlist); |
| 907 |
ast_channel_unlock(chan); |
| 908 |
return -1; |
| 909 |
} |
| 910 |
|
| 911 |
args.which = ast_skip_blanks(args.which); |
| 912 |
|
| 913 |
switch (args.which[0]) { |
| 914 |
case 'l': |
| 915 |
ast_str_set(str, len, "%s,%s,%d", frame->context, frame->extension, frame->priority - 1); |
| 916 |
break; |
| 917 |
case 'c': |
| 918 |
ast_str_set(str, len, "%s", frame->context); |
| 919 |
break; |
| 920 |
case 'e': |
| 921 |
ast_str_set(str, len, "%s", frame->extension); |
| 922 |
break; |
| 923 |
case 'p': |
| 924 |
ast_str_set(str, len, "%d", frame->priority - 1); |
| 925 |
break; |
| 926 |
default: |
| 927 |
ast_log(LOG_ERROR, "Unknown argument '%s' to STACK_PEEK\n", args.which); |
| 928 |
break; |
| 929 |
} |
| 930 |
|
| 931 |
AST_LIST_UNLOCK(oldlist); |
| 932 |
ast_channel_unlock(chan); |
| 933 |
|
| 934 |
return 0; |
| 935 |
} |
| 936 |
|
| 937 |
static struct ast_custom_function stackpeek_function = { |
| 938 |
.name = "STACK_PEEK", |
| 939 |
.read2 = stackpeek_read, |
| 940 |
}; |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
static void balance_stack(struct ast_channel *chan) |
| 952 |
{ |
| 953 |
struct ast_datastore *stack_store; |
| 954 |
struct gosub_stack_list *oldlist; |
| 955 |
struct gosub_stack_frame *oldframe; |
| 956 |
int found; |
| 957 |
|
| 958 |
stack_store = ast_channel_datastore_find(chan, &stack_info, NULL); |
| 959 |
if (!stack_store) { |
| 960 |
ast_log(LOG_WARNING, "No %s stack allocated.\n", app_gosub); |
| 961 |
return; |
| 962 |
} |
| 963 |
|
| 964 |
oldlist = stack_store->data; |
| 965 |
AST_LIST_LOCK(oldlist); |
| 966 |
do { |
| 967 |
oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries); |
| 968 |
if (!oldframe) { |
| 969 |
break; |
| 970 |
} |
| 971 |
found = oldframe->is_special; |
| 972 |
gosub_release_frame(chan, oldframe); |
| 973 |
} while (!found); |
| 974 |
AST_LIST_UNLOCK(oldlist); |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
static int gosub_run(struct ast_channel *chan, const char *sub_args, int ignore_hangup) |
| 992 |
{ |
| 993 |
const char *saved_context; |
| 994 |
const char *saved_exten; |
| 995 |
int saved_priority; |
| 996 |
int saved_hangup_flags; |
| 997 |
int saved_autoloopflag; |
| 998 |
int saved_in_subroutine; |
| 999 |
int res; |
| 1000 |
|
| 1001 |
ast_channel_lock(chan); |
| 1002 |
|
| 1003 |
ast_verb(3, "%s Internal %s(%s) start\n", |
| 1004 |
ast_channel_name(chan), app_gosub, sub_args); |
| 1005 |
|
| 1006 |
|
| 1007 |
saved_hangup_flags = ast_channel_softhangup_internal_flag(chan) |
| 1008 |
& AST_SOFTHANGUP_ASYNCGOTO; |
| 1009 |
if (saved_hangup_flags) { |
| 1010 |
ast_channel_clear_softhangup(chan, AST_SOFTHANGUP_ASYNCGOTO); |
| 1011 |
} |
| 1012 |
|
| 1013 |
|
| 1014 |
saved_autoloopflag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP); |
| 1015 |
ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP); |
| 1016 |
|
| 1017 |
|
| 1018 |
saved_context = ast_strdupa(ast_channel_context(chan)); |
| 1019 |
saved_exten = ast_strdupa(ast_channel_exten(chan)); |
| 1020 |
saved_priority = ast_channel_priority(chan); |
| 1021 |
|
| 1022 |
|
| 1023 |
saved_in_subroutine = ast_test_flag(ast_channel_flags(chan), AST_FLAG_SUBROUTINE_EXEC); |
| 1024 |
|
| 1025 |
ast_debug(4, "%s Original location: %s,%s,%d\n", ast_channel_name(chan), |
| 1026 |
saved_context, saved_exten, saved_priority); |
| 1027 |
|
| 1028 |
ast_channel_unlock(chan); |
| 1029 |
res = gosub_exec(chan, sub_args); |
| 1030 |
ast_debug(4, "%s exited with status %d\n", app_gosub, res); |
| 1031 |
ast_channel_lock(chan); |
| 1032 |
if (!res) { |
| 1033 |
struct ast_datastore *stack_store; |
| 1034 |
|
| 1035 |
|
| 1036 |
stack_store = ast_channel_datastore_find(chan, &stack_info, NULL); |
| 1037 |
if (!stack_store) { |
| 1038 |
|
| 1039 |
ast_log(LOG_ERROR, "No %s stack!\n", app_gosub); |
| 1040 |
res = -1; |
| 1041 |
} else { |
| 1042 |
struct gosub_stack_list *oldlist; |
| 1043 |
struct gosub_stack_frame *cur; |
| 1044 |
|
| 1045 |
oldlist = stack_store->data; |
| 1046 |
cur = AST_LIST_FIRST(oldlist); |
| 1047 |
cur->is_special = 1; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
if (!res) { |
| 1051 |
int found = 0; |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
do { |
| 1061 |
|
| 1062 |
if (ast_check_hangup(chan)) { |
| 1063 |
if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) { |
| 1064 |
ast_log(LOG_ERROR, "%s An async goto just messed up our execution location.\n", |
| 1065 |
ast_channel_name(chan)); |
| 1066 |
break; |
| 1067 |
} |
| 1068 |
if (!ignore_hangup) { |
| 1069 |
break; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
ast_channel_priority_set(chan, ast_channel_priority(chan) + 1); |
| 1075 |
|
| 1076 |
ast_channel_unlock(chan); |
| 1077 |
res = ast_spawn_extension(chan, ast_channel_context(chan), |
| 1078 |
ast_channel_exten(chan), ast_channel_priority(chan), |
| 1079 |
S_COR(ast_channel_caller(chan)->id.number.valid, |
| 1080 |
ast_channel_caller(chan)->id.number.str, NULL), |
| 1081 |
&found, 1); |
| 1082 |
ast_channel_lock(chan); |
| 1083 |
} while (!res); |
| 1084 |
if (found && res) { |
| 1085 |
|
| 1086 |
ast_debug(1, "Spawn extension (%s,%s,%d) exited with %d on '%s'\n", |
| 1087 |
ast_channel_context(chan), ast_channel_exten(chan), |
| 1088 |
ast_channel_priority(chan), res, ast_channel_name(chan)); |
| 1089 |
ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", |
| 1090 |
ast_channel_context(chan), ast_channel_exten(chan), |
| 1091 |
ast_channel_priority(chan), ast_channel_name(chan)); |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
if (ast_channel_priority(chan) == saved_priority |
| 1096 |
&& !strcmp(ast_channel_context(chan), saved_context) |
| 1097 |
&& !strcmp(ast_channel_exten(chan), saved_exten)) { |
| 1098 |
ast_verb(3, "%s Internal %s(%s) complete GOSUB_RETVAL=%s\n", |
| 1099 |
ast_channel_name(chan), app_gosub, sub_args, |
| 1100 |
S_OR(pbx_builtin_getvar_helper(chan, "GOSUB_RETVAL"), "")); |
| 1101 |
} else { |
| 1102 |
ast_log(LOG_WARNING, "%s Abnormal '%s(%s)' exit. Popping routine return locations.\n", |
| 1103 |
ast_channel_name(chan), app_gosub, sub_args); |
| 1104 |
balance_stack(chan); |
| 1105 |
pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", ""); |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
res = 0; |
| 1110 |
} |
| 1111 |
|
| 1112 |
ast_debug(4, "%s Ending location: %s,%s,%d\n", ast_channel_name(chan), |
| 1113 |
ast_channel_context(chan), ast_channel_exten(chan), |
| 1114 |
ast_channel_priority(chan)); |
| 1115 |
|
| 1116 |
|
| 1117 |
if (!(ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO)) { |
| 1118 |
ast_channel_context_set(chan, saved_context); |
| 1119 |
ast_channel_exten_set(chan, saved_exten); |
| 1120 |
ast_channel_priority_set(chan, saved_priority); |
| 1121 |
} |
| 1122 |
|
| 1123 |
|
| 1124 |
ast_set2_flag(ast_channel_flags(chan), saved_autoloopflag, AST_FLAG_IN_AUTOLOOP); |
| 1125 |
|
| 1126 |
|
| 1127 |
ast_set2_flag(ast_channel_flags(chan), saved_in_subroutine, AST_FLAG_SUBROUTINE_EXEC); |
| 1128 |
|
| 1129 |
|
| 1130 |
if (saved_hangup_flags) { |
| 1131 |
ast_softhangup_nolock(chan, saved_hangup_flags); |
| 1132 |
} |
| 1133 |
|
| 1134 |
ast_channel_unlock(chan); |
| 1135 |
|
| 1136 |
return res; |
| 1137 |
} |
| 1138 |
|
| 1139 |
static int handle_gosub(struct ast_channel *chan, AGI *agi, int argc, const char * const *argv) |
| 1140 |
{ |
| 1141 |
int res; |
| 1142 |
int priority; |
| 1143 |
int old_autoloopflag; |
| 1144 |
int old_in_subroutine; |
| 1145 |
int old_priority; |
| 1146 |
const char *old_context; |
| 1147 |
const char *old_extension; |
| 1148 |
char *gosub_args; |
| 1149 |
|
| 1150 |
if (argc < 4 || argc > 5) { |
| 1151 |
return RESULT_SHOWUSAGE; |
| 1152 |
} |
| 1153 |
|
| 1154 |
ast_debug(1, "Gosub called with %d arguments: 0:%s 1:%s 2:%s 3:%s 4:%s\n", argc, argv[0], argv[1], argv[2], argv[3], argc == 5 ? argv[4] : ""); |
| 1155 |
|
| 1156 |
if (sscanf(argv[3], "%30d", &priority) != 1 || priority < 1) { |
| 1157 |
|
| 1158 |
priority = ast_findlabel_extension(chan, argv[1], argv[2], argv[3], |
| 1159 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL)); |
| 1160 |
if (priority < 0) { |
| 1161 |
ast_log(LOG_ERROR, "Priority '%s' not found in '%s@%s'\n", argv[3], argv[2], argv[1]); |
| 1162 |
ast_agi_send(agi->fd, chan, "200 result=-1 Gosub label not found\n"); |
| 1163 |
return RESULT_FAILURE; |
| 1164 |
} |
| 1165 |
} else if (!ast_exists_extension(chan, argv[1], argv[2], priority, |
| 1166 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { |
| 1167 |
ast_agi_send(agi->fd, chan, "200 result=-1 Gosub label not found\n"); |
| 1168 |
return RESULT_FAILURE; |
| 1169 |
} |
| 1170 |
|
| 1171 |
if (argc == 5) { |
| 1172 |
if (ast_asprintf(&gosub_args, "%s,%s,%d(%s)", argv[1], argv[2], priority, argv[4]) < 0) { |
| 1173 |
gosub_args = NULL; |
| 1174 |
} |
| 1175 |
} else { |
| 1176 |
if (ast_asprintf(&gosub_args, "%s,%s,%d", argv[1], argv[2], priority) < 0) { |
| 1177 |
gosub_args = NULL; |
| 1178 |
} |
| 1179 |
} |
| 1180 |
if (!gosub_args) { |
| 1181 |
ast_agi_send(agi->fd, chan, "503 result=-2 Memory allocation failure\n"); |
| 1182 |
return RESULT_FAILURE; |
| 1183 |
} |
| 1184 |
|
| 1185 |
ast_channel_lock(chan); |
| 1186 |
|
| 1187 |
ast_verb(3, "%s AGI %s(%s) start\n", ast_channel_name(chan), app_gosub, gosub_args); |
| 1188 |
|
| 1189 |
|
| 1190 |
old_autoloopflag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP); |
| 1191 |
ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP); |
| 1192 |
|
| 1193 |
|
| 1194 |
old_in_subroutine = ast_test_flag(ast_channel_flags(chan), AST_FLAG_SUBROUTINE_EXEC); |
| 1195 |
|
| 1196 |
|
| 1197 |
old_context = ast_strdupa(ast_channel_context(chan)); |
| 1198 |
old_extension = ast_strdupa(ast_channel_exten(chan)); |
| 1199 |
old_priority = ast_channel_priority(chan); |
| 1200 |
|
| 1201 |
ast_debug(4, "%s Original location: %s,%s,%d\n", ast_channel_name(chan), |
| 1202 |
old_context, old_extension, old_priority); |
| 1203 |
ast_channel_unlock(chan); |
| 1204 |
|
| 1205 |
res = gosub_exec(chan, gosub_args); |
| 1206 |
if (!res) { |
| 1207 |
struct ast_datastore *stack_store; |
| 1208 |
|
| 1209 |
|
| 1210 |
ast_channel_lock(chan); |
| 1211 |
stack_store = ast_channel_datastore_find(chan, &stack_info, NULL); |
| 1212 |
if (!stack_store) { |
| 1213 |
|
| 1214 |
ast_log(LOG_ERROR, "No %s stack!\n", app_gosub); |
| 1215 |
res = -1; |
| 1216 |
} else { |
| 1217 |
struct gosub_stack_list *oldlist; |
| 1218 |
struct gosub_stack_frame *cur; |
| 1219 |
|
| 1220 |
oldlist = stack_store->data; |
| 1221 |
cur = AST_LIST_FIRST(oldlist); |
| 1222 |
cur->is_special = 1; |
| 1223 |
} |
| 1224 |
ast_channel_unlock(chan); |
| 1225 |
} |
| 1226 |
if (!res) { |
| 1227 |
struct ast_pbx *pbx; |
| 1228 |
struct ast_pbx_args args; |
| 1229 |
int abnormal_exit; |
| 1230 |
|
| 1231 |
memset(&args, 0, sizeof(args)); |
| 1232 |
args.no_hangup_chan = 1; |
| 1233 |
|
| 1234 |
ast_channel_lock(chan); |
| 1235 |
|
| 1236 |
|
| 1237 |
ast_channel_priority_set(chan, ast_channel_priority(chan) + 1); |
| 1238 |
|
| 1239 |
|
| 1240 |
pbx = ast_channel_pbx(chan); |
| 1241 |
ast_channel_pbx_set(chan, NULL); |
| 1242 |
ast_channel_unlock(chan); |
| 1243 |
|
| 1244 |
ast_agi_send(agi->fd, chan, "100 result=0 Trying...\n"); |
| 1245 |
ast_pbx_run_args(chan, &args); |
| 1246 |
|
| 1247 |
ast_channel_lock(chan); |
| 1248 |
ast_free(ast_channel_pbx(chan)); |
| 1249 |
ast_channel_pbx_set(chan, pbx); |
| 1250 |
|
| 1251 |
|
| 1252 |
if (ast_channel_priority(chan) == old_priority |
| 1253 |
&& !strcmp(ast_channel_context(chan), old_context) |
| 1254 |
&& !strcmp(ast_channel_exten(chan), old_extension)) { |
| 1255 |
ast_verb(3, "%s AGI %s(%s) complete GOSUB_RETVAL=%s\n", |
| 1256 |
ast_channel_name(chan), app_gosub, gosub_args, |
| 1257 |
S_OR(pbx_builtin_getvar_helper(chan, "GOSUB_RETVAL"), "")); |
| 1258 |
abnormal_exit = 0; |
| 1259 |
} else { |
| 1260 |
ast_log(LOG_NOTICE, "%s Abnormal AGI %s(%s) exit. Popping routine return locations.\n", |
| 1261 |
ast_channel_name(chan), app_gosub, gosub_args); |
| 1262 |
balance_stack(chan); |
| 1263 |
pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", ""); |
| 1264 |
abnormal_exit = 1; |
| 1265 |
} |
| 1266 |
ast_channel_unlock(chan); |
| 1267 |
|
| 1268 |
ast_agi_send(agi->fd, chan, "200 result=0 Gosub complete%s\n", |
| 1269 |
abnormal_exit ? " (abnormal exit)" : ""); |
| 1270 |
} else { |
| 1271 |
ast_agi_send(agi->fd, chan, "200 result=%d Gosub failed\n", res); |
| 1272 |
} |
| 1273 |
|
| 1274 |
ast_free(gosub_args); |
| 1275 |
|
| 1276 |
ast_channel_lock(chan); |
| 1277 |
ast_debug(4, "%s Ending location: %s,%s,%d\n", ast_channel_name(chan), |
| 1278 |
ast_channel_context(chan), ast_channel_exten(chan), |
| 1279 |
ast_channel_priority(chan)); |
| 1280 |
|
| 1281 |
|
| 1282 |
ast_channel_context_set(chan, old_context); |
| 1283 |
ast_channel_exten_set(chan, old_extension); |
| 1284 |
ast_channel_priority_set(chan, old_priority); |
| 1285 |
|
| 1286 |
|
| 1287 |
ast_set2_flag(ast_channel_flags(chan), old_autoloopflag, AST_FLAG_IN_AUTOLOOP); |
| 1288 |
|
| 1289 |
|
| 1290 |
ast_set2_flag(ast_channel_flags(chan), old_in_subroutine, AST_FLAG_SUBROUTINE_EXEC); |
| 1291 |
ast_channel_unlock(chan); |
| 1292 |
|
| 1293 |
return RESULT_SUCCESS; |
| 1294 |
} |
| 1295 |
|
| 1296 |
static struct agi_command gosub_agi_command = |
| 1297 |
{ { "gosub", NULL }, handle_gosub, NULL, NULL, 0 }; |
| 1298 |
|
| 1299 |
static int unload_module(void) |
| 1300 |
{ |
| 1301 |
ast_install_stack_functions(NULL); |
| 1302 |
|
| 1303 |
ast_agi_unregister(&gosub_agi_command); |
| 1304 |
|
| 1305 |
ast_unregister_application(app_return); |
| 1306 |
ast_unregister_application(app_pop); |
| 1307 |
ast_unregister_application(app_gosubif); |
| 1308 |
ast_unregister_application(app_gosub); |
| 1309 |
ast_custom_function_unregister(&local_function); |
| 1310 |
ast_custom_function_unregister(&peek_function); |
| 1311 |
ast_custom_function_unregister(&stackpeek_function); |
| 1312 |
|
| 1313 |
return 0; |
| 1314 |
} |
| 1315 |
|
| 1316 |
static int load_module(void) |
| 1317 |
{ |
| 1318 |
|
| 1319 |
static struct ast_app_stack_funcs funcs = { |
| 1320 |
.run_sub = gosub_run, |
| 1321 |
.expand_sub_args = expand_gosub_args, |
| 1322 |
}; |
| 1323 |
|
| 1324 |
ast_agi_register(ast_module_info->self, &gosub_agi_command); |
| 1325 |
|
| 1326 |
ast_register_application_xml(app_pop, pop_exec); |
| 1327 |
ast_register_application_xml(app_return, return_exec); |
| 1328 |
ast_register_application_xml(app_gosubif, gosubif_exec); |
| 1329 |
ast_register_application_xml(app_gosub, gosub_exec); |
| 1330 |
ast_custom_function_register(&local_function); |
| 1331 |
ast_custom_function_register(&peek_function); |
| 1332 |
ast_custom_function_register(&stackpeek_function); |
| 1333 |
|
| 1334 |
funcs.module = ast_module_info->self, |
| 1335 |
ast_install_stack_functions(&funcs); |
| 1336 |
|
| 1337 |
return 0; |
| 1338 |
} |
| 1339 |
|
| 1340 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT | AST_MODFLAG_LOAD_ORDER, "Dialplan subroutines (Gosub, Return, etc)", |
| 1341 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1342 |
.load = load_module, |
| 1343 |
.unload = unload_module, |
| 1344 |
.load_pri = AST_MODPRI_APP_DEPEND, |
| 1345 |
.optional_modules = "res_agi", |
| 1346 |
); |