| 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 |
#include "asterisk.h" |
| 27 |
|
| 28 |
#include "res_parking.h" |
| 29 |
#include "asterisk/config.h" |
| 30 |
#include "asterisk/config_options.h" |
| 31 |
#include "asterisk/utils.h" |
| 32 |
#include "asterisk/astobj2.h" |
| 33 |
#include "asterisk/features.h" |
| 34 |
#include "asterisk/module.h" |
| 35 |
#include "asterisk/app.h" |
| 36 |
#include "asterisk/say.h" |
| 37 |
#include "asterisk/bridge_basic.h" |
| 38 |
#include "asterisk/format_cache.h" |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 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 |
#define PARK_AND_ANNOUNCE_APPLICATION "ParkAndAnnounce" |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
enum park_args { |
| 251 |
OPT_ARG_COMEBACK, |
| 252 |
OPT_ARG_TIMEOUT, |
| 253 |
OPT_ARG_MUSICONHOLD, |
| 254 |
OPT_ARG_ARRAY_SIZE |
| 255 |
}; |
| 256 |
|
| 257 |
enum park_flags { |
| 258 |
MUXFLAG_RINGING = (1 << 0), |
| 259 |
MUXFLAG_RANDOMIZE = (1 << 1), |
| 260 |
MUXFLAG_NOANNOUNCE = (1 << 2), |
| 261 |
MUXFLAG_COMEBACK_OVERRIDE = (1 << 3), |
| 262 |
MUXFLAG_TIMEOUT_OVERRIDE = (1 << 4), |
| 263 |
MUXFLAG_MUSICONHOLD = (1 << 5), |
| 264 |
}; |
| 265 |
|
| 266 |
AST_APP_OPTIONS(park_opts, { |
| 267 |
AST_APP_OPTION('r', MUXFLAG_RINGING), |
| 268 |
AST_APP_OPTION('R', MUXFLAG_RANDOMIZE), |
| 269 |
AST_APP_OPTION('s', MUXFLAG_NOANNOUNCE), |
| 270 |
AST_APP_OPTION_ARG('c', MUXFLAG_COMEBACK_OVERRIDE, OPT_ARG_COMEBACK), |
| 271 |
AST_APP_OPTION_ARG('t', MUXFLAG_TIMEOUT_OVERRIDE, OPT_ARG_TIMEOUT), |
| 272 |
AST_APP_OPTION_ARG('m', MUXFLAG_MUSICONHOLD, OPT_ARG_MUSICONHOLD), |
| 273 |
}); |
| 274 |
|
| 275 |
static int apply_option_timeout (int *var, char *timeout_arg) |
| 276 |
{ |
| 277 |
if (ast_strlen_zero(timeout_arg)) { |
| 278 |
ast_log(LOG_ERROR, "No duration value provided for the timeout ('t') option.\n"); |
| 279 |
return -1; |
| 280 |
} |
| 281 |
|
| 282 |
if (sscanf(timeout_arg, "%d", var) != 1 || *var < 0) { |
| 283 |
ast_log(LOG_ERROR, "Duration value provided for timeout ('t') option must be 0 or greater.\n"); |
| 284 |
return -1; |
| 285 |
} |
| 286 |
|
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
static int park_app_parse_data(const char *data, int *disable_announce, int *use_ringing, int *randomize, int *time_limit, |
| 291 |
char **comeback_override, char **lot_name, char **musicclass) |
| 292 |
{ |
| 293 |
char *parse; |
| 294 |
struct ast_flags flags = { 0 }; |
| 295 |
|
| 296 |
AST_DECLARE_APP_ARGS(args, |
| 297 |
AST_APP_ARG(lot_name); |
| 298 |
AST_APP_ARG(options); |
| 299 |
AST_APP_ARG(other); |
| 300 |
); |
| 301 |
|
| 302 |
parse = ast_strdupa(data); |
| 303 |
AST_STANDARD_APP_ARGS(args, parse); |
| 304 |
|
| 305 |
if (args.options) { |
| 306 |
char *opts[OPT_ARG_ARRAY_SIZE] = { NULL, }; |
| 307 |
ast_app_parse_options(park_opts, &flags, opts, args.options); |
| 308 |
if (ast_test_flag(&flags, MUXFLAG_TIMEOUT_OVERRIDE)) { |
| 309 |
if (apply_option_timeout(time_limit, opts[OPT_ARG_TIMEOUT])) { |
| 310 |
return -1; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
if (ast_test_flag(&flags, MUXFLAG_COMEBACK_OVERRIDE)) { |
| 315 |
*comeback_override = ast_strdup(opts[OPT_ARG_COMEBACK]); |
| 316 |
} |
| 317 |
|
| 318 |
if (ast_test_flag(&flags, MUXFLAG_NOANNOUNCE)) { |
| 319 |
if (disable_announce) { |
| 320 |
*disable_announce = 1; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
if (ast_test_flag(&flags, MUXFLAG_MUSICONHOLD)) { |
| 325 |
*musicclass = ast_strdup(opts[OPT_ARG_MUSICONHOLD]); |
| 326 |
} |
| 327 |
|
| 328 |
if (ast_test_flag(&flags, MUXFLAG_RINGING)) { |
| 329 |
*use_ringing = 1; |
| 330 |
} |
| 331 |
|
| 332 |
if (ast_test_flag(&flags, MUXFLAG_RANDOMIZE)) { |
| 333 |
*randomize = 1; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
if (!ast_strlen_zero(args.lot_name)) { |
| 338 |
*lot_name = ast_strdup(args.lot_name); |
| 339 |
} |
| 340 |
|
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
void park_common_datastore_free(struct park_common_datastore *datastore) |
| 345 |
{ |
| 346 |
if (!datastore) { |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
ast_free(datastore->parker_uuid); |
| 351 |
ast_free(datastore->parker_dial_string); |
| 352 |
ast_free(datastore->comeback_override); |
| 353 |
ast_free(datastore); |
| 354 |
} |
| 355 |
|
| 356 |
static void park_common_datastore_destroy(void *data) |
| 357 |
{ |
| 358 |
struct park_common_datastore *datastore = data; |
| 359 |
park_common_datastore_free(datastore); |
| 360 |
} |
| 361 |
|
| 362 |
static const struct ast_datastore_info park_common_info = { |
| 363 |
.type = "park entry data", |
| 364 |
.destroy = park_common_datastore_destroy, |
| 365 |
}; |
| 366 |
|
| 367 |
static void wipe_park_common_datastore(struct ast_channel *chan) |
| 368 |
{ |
| 369 |
struct ast_datastore *datastore; |
| 370 |
|
| 371 |
ast_channel_lock(chan); |
| 372 |
datastore = ast_channel_datastore_find(chan, &park_common_info, NULL); |
| 373 |
if (datastore) { |
| 374 |
ast_channel_datastore_remove(chan, datastore); |
| 375 |
ast_datastore_free(datastore); |
| 376 |
} |
| 377 |
ast_channel_unlock(chan); |
| 378 |
} |
| 379 |
|
| 380 |
static int setup_park_common_datastore(struct ast_channel *parkee, const char *parker_uuid, const char *comeback_override, int randomize, int time_limit, int silence_announce) |
| 381 |
{ |
| 382 |
struct ast_datastore *datastore = NULL; |
| 383 |
struct park_common_datastore *park_datastore; |
| 384 |
const char *attended_transfer; |
| 385 |
const char *blind_transfer; |
| 386 |
char *parker_dial_string = NULL; |
| 387 |
|
| 388 |
wipe_park_common_datastore(parkee); |
| 389 |
|
| 390 |
if (!(datastore = ast_datastore_alloc(&park_common_info, NULL))) { |
| 391 |
return -1; |
| 392 |
} |
| 393 |
|
| 394 |
if (!(park_datastore = ast_calloc(1, sizeof(*park_datastore)))) { |
| 395 |
ast_datastore_free(datastore); |
| 396 |
return -1; |
| 397 |
} |
| 398 |
datastore->data = park_datastore; |
| 399 |
|
| 400 |
park_datastore->parker_uuid = ast_strdup(parker_uuid); |
| 401 |
if (!park_datastore->parker_uuid) { |
| 402 |
ast_datastore_free(datastore); |
| 403 |
return -1; |
| 404 |
} |
| 405 |
|
| 406 |
ast_channel_lock(parkee); |
| 407 |
attended_transfer = pbx_builtin_getvar_helper(parkee, "ATTENDEDTRANSFER"); |
| 408 |
blind_transfer = pbx_builtin_getvar_helper(parkee, "BLINDTRANSFER"); |
| 409 |
if (!ast_strlen_zero(attended_transfer)) { |
| 410 |
parker_dial_string = ast_strdupa(attended_transfer); |
| 411 |
} else if (!ast_strlen_zero(blind_transfer)) { |
| 412 |
parker_dial_string = ast_strdupa(blind_transfer); |
| 413 |
|
| 414 |
attended_transfer = NULL; |
| 415 |
} |
| 416 |
ast_channel_unlock(parkee); |
| 417 |
|
| 418 |
if (!ast_strlen_zero(parker_dial_string)) { |
| 419 |
ast_channel_name_to_dial_string(parker_dial_string); |
| 420 |
ast_verb(4, "Setting Parker dial string to %s from %s value\n", |
| 421 |
parker_dial_string, |
| 422 |
attended_transfer ? "ATTENDEDTRANSFER" : "BLINDTRANSFER"); |
| 423 |
park_datastore->parker_dial_string = ast_strdup(parker_dial_string); |
| 424 |
if (!park_datastore->parker_dial_string) { |
| 425 |
ast_datastore_free(datastore); |
| 426 |
return -1; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
park_datastore->randomize = randomize; |
| 431 |
park_datastore->time_limit = time_limit; |
| 432 |
park_datastore->silence_announce = silence_announce; |
| 433 |
|
| 434 |
if (comeback_override) { |
| 435 |
park_datastore->comeback_override = ast_strdup(comeback_override); |
| 436 |
if (!park_datastore->comeback_override) { |
| 437 |
ast_datastore_free(datastore); |
| 438 |
return -1; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
ast_channel_lock(parkee); |
| 444 |
ast_channel_datastore_add(parkee, datastore); |
| 445 |
ast_channel_unlock(parkee); |
| 446 |
|
| 447 |
return 0; |
| 448 |
} |
| 449 |
|
| 450 |
struct park_common_datastore *get_park_common_datastore_copy(struct ast_channel *parkee) |
| 451 |
{ |
| 452 |
struct ast_datastore *datastore; |
| 453 |
struct park_common_datastore *data; |
| 454 |
struct park_common_datastore *data_copy; |
| 455 |
|
| 456 |
SCOPED_CHANNELLOCK(lock, parkee); |
| 457 |
|
| 458 |
if (!(datastore = ast_channel_datastore_find(parkee, &park_common_info, NULL))) { |
| 459 |
return NULL; |
| 460 |
} |
| 461 |
|
| 462 |
data = datastore->data; |
| 463 |
|
| 464 |
|
| 465 |
ast_assert(data != NULL); |
| 466 |
|
| 467 |
data_copy = ast_calloc(1, sizeof(*data_copy)); |
| 468 |
if (!data_copy) { |
| 469 |
return NULL; |
| 470 |
} |
| 471 |
|
| 472 |
data_copy->parker_uuid = ast_strdup(data->parker_uuid); |
| 473 |
if (!data_copy->parker_uuid) { |
| 474 |
park_common_datastore_free(data_copy); |
| 475 |
return NULL; |
| 476 |
} |
| 477 |
|
| 478 |
data_copy->randomize = data->randomize; |
| 479 |
data_copy->time_limit = data->time_limit; |
| 480 |
data_copy->silence_announce = data->silence_announce; |
| 481 |
|
| 482 |
if (data->comeback_override) { |
| 483 |
data_copy->comeback_override = ast_strdup(data->comeback_override); |
| 484 |
if (!data_copy->comeback_override) { |
| 485 |
park_common_datastore_free(data_copy); |
| 486 |
return NULL; |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
if (data->parker_dial_string) { |
| 491 |
data_copy->parker_dial_string = ast_strdup(data->parker_dial_string); |
| 492 |
if (!data_copy->parker_dial_string) { |
| 493 |
park_common_datastore_free(data_copy); |
| 494 |
return NULL; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
return data_copy; |
| 499 |
} |
| 500 |
|
| 501 |
static struct ast_bridge *park_common_setup2(struct ast_channel *parkee, struct ast_channel *parker, |
| 502 |
const char *lot_name, const char *comeback_override, const char *musicclass, |
| 503 |
int use_ringing, int randomize, int time_limit, int silence_announcements) |
| 504 |
{ |
| 505 |
struct ast_bridge *parking_bridge; |
| 506 |
RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup); |
| 507 |
|
| 508 |
if (!parker) { |
| 509 |
parker = parkee; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
if (ast_strlen_zero(lot_name)) { |
| 514 |
ast_channel_lock(parker); |
| 515 |
lot_name = ast_strdupa(find_channel_parking_lot_name(parker)); |
| 516 |
ast_channel_unlock(parker); |
| 517 |
} |
| 518 |
|
| 519 |
lot = parking_lot_find_by_name(lot_name); |
| 520 |
if (!lot) { |
| 521 |
lot = parking_create_dynamic_lot(lot_name, parker); |
| 522 |
} |
| 523 |
if (!lot) { |
| 524 |
ast_log(LOG_ERROR, "Could not find parking lot: '%s'\n", lot_name); |
| 525 |
return NULL; |
| 526 |
} |
| 527 |
|
| 528 |
ao2_lock(lot); |
| 529 |
parking_bridge = parking_lot_get_bridge(lot); |
| 530 |
ao2_unlock(lot); |
| 531 |
|
| 532 |
if (!parking_bridge) { |
| 533 |
return NULL; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
parking_channel_set_roles(parkee, lot, use_ringing); |
| 538 |
|
| 539 |
if (!ast_strlen_zero(musicclass)) { |
| 540 |
ast_channel_set_bridge_role_option(parkee, "holding_participant", "moh_class", musicclass); |
| 541 |
} |
| 542 |
setup_park_common_datastore(parkee, ast_channel_uniqueid(parker), comeback_override, randomize, time_limit, |
| 543 |
silence_announcements); |
| 544 |
return parking_bridge; |
| 545 |
} |
| 546 |
|
| 547 |
struct ast_bridge *park_common_setup(struct ast_channel *parkee, struct ast_channel *parker, |
| 548 |
const char *lot_name, const char *comeback_override, |
| 549 |
int use_ringing, int randomize, int time_limit, int silence_announcements) |
| 550 |
{ |
| 551 |
return park_common_setup2(parkee, parker, lot_name, comeback_override, NULL, use_ringing, randomize, time_limit, silence_announcements); |
| 552 |
} |
| 553 |
|
| 554 |
struct ast_bridge *park_application_setup(struct ast_channel *parkee, struct ast_channel *parker, const char *app_data, |
| 555 |
int *silence_announcements) |
| 556 |
{ |
| 557 |
int use_ringing = 0; |
| 558 |
int randomize = 0; |
| 559 |
int time_limit = -1; |
| 560 |
|
| 561 |
RAII_VAR(char *, comeback_override, NULL, ast_free); |
| 562 |
RAII_VAR(char *, lot_name_app_arg, NULL, ast_free); |
| 563 |
RAII_VAR(char *, musicclass, NULL, ast_free); |
| 564 |
|
| 565 |
if (app_data) { |
| 566 |
park_app_parse_data(app_data, silence_announcements, &use_ringing, &randomize, &time_limit, &comeback_override, &lot_name_app_arg, &musicclass); |
| 567 |
} |
| 568 |
|
| 569 |
return park_common_setup2(parkee, parker, lot_name_app_arg, comeback_override, musicclass, use_ringing, |
| 570 |
randomize, time_limit, silence_announcements ? *silence_announcements : 0); |
| 571 |
|
| 572 |
} |
| 573 |
|
| 574 |
static int park_app_exec(struct ast_channel *chan, const char *data) |
| 575 |
{ |
| 576 |
RAII_VAR(struct ast_bridge *, parking_bridge, NULL, ao2_cleanup); |
| 577 |
|
| 578 |
struct ast_bridge_features chan_features; |
| 579 |
int res = 0; |
| 580 |
int silence_announcements = 0; |
| 581 |
int blind_transfer; |
| 582 |
|
| 583 |
|
| 584 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 585 |
ast_answer(chan); |
| 586 |
} |
| 587 |
|
| 588 |
ast_channel_lock(chan); |
| 589 |
blind_transfer = !ast_strlen_zero(pbx_builtin_getvar_helper(chan, "BLINDTRANSFER")); |
| 590 |
ast_channel_unlock(chan); |
| 591 |
|
| 592 |
|
| 593 |
if (!(parking_bridge = park_application_setup(chan, NULL, data, &silence_announcements))) { |
| 594 |
if (!silence_announcements && !blind_transfer) { |
| 595 |
ast_stream_and_wait(chan, "pbx-parkingfailed", ""); |
| 596 |
} |
| 597 |
publish_parked_call_failure(chan); |
| 598 |
return 0; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
res = ast_bridge_features_init(&chan_features); |
| 603 |
|
| 604 |
if (res || ast_bridge_join(parking_bridge, chan, NULL, &chan_features, NULL, 0)) { |
| 605 |
if (!silence_announcements && !blind_transfer) { |
| 606 |
ast_stream_and_wait(chan, "pbx-parkingfailed", ""); |
| 607 |
} |
| 608 |
ast_bridge_features_cleanup(&chan_features); |
| 609 |
publish_parked_call_failure(chan); |
| 610 |
return res; |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
res = -1; |
| 619 |
|
| 620 |
ast_channel_lock(chan); |
| 621 |
if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) { |
| 622 |
res = 0; |
| 623 |
} |
| 624 |
ast_channel_unlock(chan); |
| 625 |
|
| 626 |
ast_bridge_features_cleanup(&chan_features); |
| 627 |
|
| 628 |
return res; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
static int parked_call_app_exec(struct ast_channel *chan, const char *data) |
| 634 |
{ |
| 635 |
RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup); |
| 636 |
RAII_VAR(struct parked_user *, pu, NULL, ao2_cleanup); |
| 637 |
struct ast_bridge *retrieval_bridge; |
| 638 |
int res; |
| 639 |
int target_space = -1; |
| 640 |
struct ast_bridge_features chan_features; |
| 641 |
char *parse; |
| 642 |
const char *lot_name; |
| 643 |
|
| 644 |
AST_DECLARE_APP_ARGS(args, |
| 645 |
AST_APP_ARG(lot_name); |
| 646 |
AST_APP_ARG(parking_space); |
| 647 |
AST_APP_ARG(other); |
| 648 |
); |
| 649 |
|
| 650 |
parse = ast_strdupa(data); |
| 651 |
AST_STANDARD_APP_ARGS(args, parse); |
| 652 |
|
| 653 |
|
| 654 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 655 |
ast_answer(chan); |
| 656 |
} |
| 657 |
|
| 658 |
lot_name = args.lot_name; |
| 659 |
|
| 660 |
|
| 661 |
if (ast_strlen_zero(lot_name)) { |
| 662 |
ast_channel_lock(chan); |
| 663 |
lot_name = ast_strdupa(find_channel_parking_lot_name(chan)); |
| 664 |
ast_channel_unlock(chan); |
| 665 |
} |
| 666 |
|
| 667 |
lot = parking_lot_find_by_name(lot_name); |
| 668 |
if (!lot) { |
| 669 |
ast_log(LOG_ERROR, "Could not find the requested parking lot\n"); |
| 670 |
ast_stream_and_wait(chan, "pbx-invalidpark", ""); |
| 671 |
return -1; |
| 672 |
} |
| 673 |
|
| 674 |
if (!ast_strlen_zero(args.parking_space)) { |
| 675 |
if (sscanf(args.parking_space, "%d", &target_space) != 1 || target_space < 0) { |
| 676 |
ast_stream_and_wait(chan, "pbx-invalidpark", ""); |
| 677 |
ast_log(LOG_ERROR, "value '%s' for parking_space argument is invalid. Must be an integer greater than 0.\n", args.parking_space); |
| 678 |
return -1; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
pu = parking_lot_retrieve_parked_user(lot, target_space); |
| 684 |
if (!pu) { |
| 685 |
ast_stream_and_wait(chan, "pbx-invalidpark", ""); |
| 686 |
return -1; |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
ast_assert(pu->retriever == NULL); |
| 691 |
pu->retriever = ast_channel_snapshot_create(chan); |
| 692 |
|
| 693 |
|
| 694 |
retrieval_bridge = ast_bridge_basic_new(); |
| 695 |
if (!retrieval_bridge) { |
| 696 |
return -1; |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
if (ast_bridge_move(retrieval_bridge, lot->parking_bridge, pu->chan, NULL, 0)) { |
| 701 |
ast_bridge_destroy(retrieval_bridge, 0); |
| 702 |
return -1; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
res = ast_bridge_features_init(&chan_features); |
| 707 |
if (res) { |
| 708 |
ast_bridge_destroy(retrieval_bridge, 0); |
| 709 |
ast_bridge_features_cleanup(&chan_features); |
| 710 |
return -1; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
parked_call_retrieve_enable_features(chan, lot, AST_FEATURE_FLAG_BYCALLER); |
| 715 |
|
| 716 |
|
| 717 |
if (lot->cfg->parkedplay & AST_FEATURE_FLAG_BYCALLER) { |
| 718 |
ast_stream_and_wait(chan, lot->cfg->courtesytone, NULL); |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
ast_bridge_join(retrieval_bridge, chan, NULL, &chan_features, NULL, |
| 723 |
AST_BRIDGE_JOIN_PASS_REFERENCE); |
| 724 |
|
| 725 |
ast_bridge_features_cleanup(&chan_features); |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
return -1; |
| 731 |
} |
| 732 |
|
| 733 |
struct park_announce_subscription_data { |
| 734 |
char *parkee_uuid; |
| 735 |
char *dial_string; |
| 736 |
char *announce_string; |
| 737 |
}; |
| 738 |
|
| 739 |
static void park_announce_subscription_data_destroy(void *data) |
| 740 |
{ |
| 741 |
struct park_announce_subscription_data *pa_data = data; |
| 742 |
ast_free(pa_data->parkee_uuid); |
| 743 |
ast_free(pa_data->dial_string); |
| 744 |
ast_free(pa_data->announce_string); |
| 745 |
ast_free(pa_data); |
| 746 |
} |
| 747 |
|
| 748 |
static struct park_announce_subscription_data *park_announce_subscription_data_create(const char *parkee_uuid, |
| 749 |
const char *dial_string, |
| 750 |
const char *announce_string) |
| 751 |
{ |
| 752 |
struct park_announce_subscription_data *pa_data; |
| 753 |
|
| 754 |
if (!(pa_data = ast_calloc(1, sizeof(*pa_data)))) { |
| 755 |
return NULL; |
| 756 |
} |
| 757 |
|
| 758 |
if (!(pa_data->parkee_uuid = ast_strdup(parkee_uuid)) |
| 759 |
|| !(pa_data->dial_string = ast_strdup(dial_string)) |
| 760 |
|| !(pa_data->announce_string = ast_strdup(announce_string))) { |
| 761 |
park_announce_subscription_data_destroy(pa_data); |
| 762 |
return NULL; |
| 763 |
} |
| 764 |
|
| 765 |
return pa_data; |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
static void inherit_channel_vars_from_id(struct outgoing_helper *oh, const char *channel_id) |
| 775 |
{ |
| 776 |
struct ast_channel *chan = ast_channel_get_by_name(channel_id); |
| 777 |
struct ast_var_t *current; |
| 778 |
struct ast_variable *newvar; |
| 779 |
const char *varname; |
| 780 |
int vartype; |
| 781 |
|
| 782 |
|
| 783 |
if (!chan) { |
| 784 |
|
| 785 |
return; |
| 786 |
} |
| 787 |
|
| 788 |
ast_channel_lock(chan); |
| 789 |
|
| 790 |
AST_LIST_TRAVERSE(ast_channel_varshead((struct ast_channel *) chan), current, entries) { |
| 791 |
varname = ast_var_full_name(current); |
| 792 |
if (!varname) { |
| 793 |
continue; |
| 794 |
} |
| 795 |
|
| 796 |
vartype = 0; |
| 797 |
if (varname[0] == '_') { |
| 798 |
vartype = 1; |
| 799 |
if (varname[1] == '_') { |
| 800 |
vartype = 2; |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
switch (vartype) { |
| 805 |
case 1: |
| 806 |
newvar = ast_variable_new(&varname[1], ast_var_value(current), ""); |
| 807 |
break; |
| 808 |
case 2: |
| 809 |
newvar = ast_variable_new(varname, ast_var_value(current), ""); |
| 810 |
break; |
| 811 |
default: |
| 812 |
continue; |
| 813 |
} |
| 814 |
if (newvar) { |
| 815 |
ast_debug(1, "Inheriting variable %s from %s.\n", |
| 816 |
newvar->name, ast_channel_name(chan)); |
| 817 |
if (oh->vars) { |
| 818 |
newvar->next = oh->vars; |
| 819 |
oh->vars = newvar; |
| 820 |
} |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
ast_channel_unlock(chan); |
| 825 |
ast_channel_cleanup(chan); |
| 826 |
} |
| 827 |
|
| 828 |
static void announce_to_dial(char *dial_string, char *announce_string, int parkingspace, struct ast_channel_snapshot *parkee_snapshot) |
| 829 |
{ |
| 830 |
struct ast_channel *dchan; |
| 831 |
struct outgoing_helper oh = { 0, }; |
| 832 |
int outstate; |
| 833 |
struct ast_format_cap *cap_slin = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); |
| 834 |
char buf[13]; |
| 835 |
char *dial_tech; |
| 836 |
char *cur_announce; |
| 837 |
|
| 838 |
dial_tech = strsep(&dial_string, "/"); |
| 839 |
ast_verb(3, "Dial Tech,String: (%s,%s)\n", dial_tech, dial_string); |
| 840 |
|
| 841 |
if (!cap_slin) { |
| 842 |
ast_log(LOG_WARNING, "PARK: Failed to announce park.\n"); |
| 843 |
goto announce_cleanup; |
| 844 |
} |
| 845 |
ast_format_cap_append(cap_slin, ast_format_slin, 0); |
| 846 |
|
| 847 |
snprintf(buf, sizeof(buf), "%d", parkingspace); |
| 848 |
oh.vars = ast_variable_new("_PARKEDAT", buf, ""); |
| 849 |
|
| 850 |
inherit_channel_vars_from_id(&oh, parkee_snapshot->base->uniqueid); |
| 851 |
|
| 852 |
dchan = __ast_request_and_dial(dial_tech, cap_slin, NULL, NULL, dial_string, 30000, |
| 853 |
&outstate, |
| 854 |
parkee_snapshot->caller->number, |
| 855 |
parkee_snapshot->caller->name, |
| 856 |
&oh); |
| 857 |
|
| 858 |
ast_variables_destroy(oh.vars); |
| 859 |
if (!dchan) { |
| 860 |
ast_log(LOG_WARNING, "PARK: Unable to allocate announce channel.\n"); |
| 861 |
goto announce_cleanup; |
| 862 |
} |
| 863 |
|
| 864 |
ast_verb(4, "Announce Template: %s\n", announce_string); |
| 865 |
|
| 866 |
for (cur_announce = strsep(&announce_string, ":"); cur_announce; cur_announce = strsep(&announce_string, ":")) { |
| 867 |
ast_verb(4, "Announce:%s\n", cur_announce); |
| 868 |
if (!strcmp(cur_announce, "PARKED")) { |
| 869 |
ast_say_digits(dchan, parkingspace, "", ast_channel_language(dchan)); |
| 870 |
} else { |
| 871 |
int dres = ast_streamfile(dchan, cur_announce, ast_channel_language(dchan)); |
| 872 |
if (!dres) { |
| 873 |
dres = ast_waitstream(dchan, ""); |
| 874 |
} else { |
| 875 |
ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", cur_announce, ast_channel_name(dchan)); |
| 876 |
} |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
ast_stopstream(dchan); |
| 881 |
ast_hangup(dchan); |
| 882 |
|
| 883 |
announce_cleanup: |
| 884 |
ao2_cleanup(cap_slin); |
| 885 |
} |
| 886 |
|
| 887 |
static void park_announce_update_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 888 |
{ |
| 889 |
struct park_announce_subscription_data *pa_data = data; |
| 890 |
char *dial_string = pa_data->dial_string; |
| 891 |
|
| 892 |
struct ast_parked_call_payload *payload = stasis_message_data(message); |
| 893 |
|
| 894 |
if (stasis_subscription_final_message(sub, message)) { |
| 895 |
park_announce_subscription_data_destroy(data); |
| 896 |
return; |
| 897 |
} |
| 898 |
|
| 899 |
if (ast_parked_call_type() != stasis_message_type(message)) { |
| 900 |
return; |
| 901 |
} |
| 902 |
|
| 903 |
if (payload->event_type != PARKED_CALL) { |
| 904 |
|
| 905 |
return; |
| 906 |
} |
| 907 |
|
| 908 |
if (strcmp(payload->parkee->base->uniqueid, pa_data->parkee_uuid)) { |
| 909 |
|
| 910 |
return; |
| 911 |
} |
| 912 |
|
| 913 |
if (!ast_strlen_zero(dial_string)) { |
| 914 |
announce_to_dial(dial_string, pa_data->announce_string, payload->parkingspace, payload->parkee); |
| 915 |
} |
| 916 |
|
| 917 |
*dial_string = '\0'; |
| 918 |
} |
| 919 |
|
| 920 |
static int park_and_announce_app_exec(struct ast_channel *chan, const char *data) |
| 921 |
{ |
| 922 |
struct ast_bridge_features chan_features; |
| 923 |
char *parse; |
| 924 |
int res; |
| 925 |
int silence_announcements = 1; |
| 926 |
|
| 927 |
struct stasis_subscription *parking_subscription; |
| 928 |
struct park_announce_subscription_data *pa_data; |
| 929 |
|
| 930 |
RAII_VAR(struct ast_bridge *, parking_bridge, NULL, ao2_cleanup); |
| 931 |
|
| 932 |
AST_DECLARE_APP_ARGS(args, |
| 933 |
AST_APP_ARG(lot_name); |
| 934 |
AST_APP_ARG(options); |
| 935 |
AST_APP_ARG(announce_template); |
| 936 |
AST_APP_ARG(dial); |
| 937 |
AST_APP_ARG(others); |
| 938 |
); |
| 939 |
|
| 940 |
if (ast_strlen_zero(data)) { |
| 941 |
ast_log(LOG_ERROR, "ParkAndAnnounce has required arguments. No arguments were provided.\n"); |
| 942 |
return -1; |
| 943 |
} |
| 944 |
|
| 945 |
parse = ast_strdupa(data); |
| 946 |
AST_STANDARD_APP_ARGS(args, parse); |
| 947 |
|
| 948 |
if (ast_strlen_zero(args.announce_template)) { |
| 949 |
|
| 950 |
ast_log(LOG_ERROR, "ParkAndAnnounce requires the announce_template argument.\n"); |
| 951 |
return -1; |
| 952 |
} |
| 953 |
|
| 954 |
if (ast_strlen_zero(args.dial)) { |
| 955 |
|
| 956 |
ast_log(LOG_ERROR, "ParkAndAnnounce requires the dial argument.\n"); |
| 957 |
return -1; |
| 958 |
} |
| 959 |
|
| 960 |
if (!strchr(args.dial, '/')) { |
| 961 |
ast_log(LOG_ERROR, "ParkAndAnnounce dial string '%s' is improperly formed.\n", args.dial); |
| 962 |
return -1; |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
if (!(parking_bridge = park_application_setup(chan, NULL, data, &silence_announcements))) { |
| 967 |
return 0; |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
res = ast_bridge_features_init(&chan_features); |
| 972 |
if (res) { |
| 973 |
ast_bridge_features_cleanup(&chan_features); |
| 974 |
return -1; |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
pa_data = park_announce_subscription_data_create(ast_channel_uniqueid(chan), args.dial, args.announce_template); |
| 979 |
if (!pa_data) { |
| 980 |
return -1; |
| 981 |
} |
| 982 |
|
| 983 |
if (!(parking_subscription = stasis_subscribe_pool(ast_parking_topic(), park_announce_update_cb, pa_data))) { |
| 984 |
|
| 985 |
park_announce_subscription_data_destroy(pa_data); |
| 986 |
return -1; |
| 987 |
} |
| 988 |
|
| 989 |
stasis_subscription_accept_message_type(parking_subscription, ast_parked_call_type()); |
| 990 |
stasis_subscription_accept_message_type(parking_subscription, stasis_subscription_change_type()); |
| 991 |
stasis_subscription_set_filter(parking_subscription, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 992 |
|
| 993 |
|
| 994 |
ast_bridge_join(parking_bridge, chan, NULL, &chan_features, NULL, 0); |
| 995 |
|
| 996 |
|
| 997 |
stasis_unsubscribe(parking_subscription); |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
res = -1; |
| 1005 |
|
| 1006 |
ast_channel_lock(chan); |
| 1007 |
if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) { |
| 1008 |
res = 0; |
| 1009 |
} |
| 1010 |
ast_channel_unlock(chan); |
| 1011 |
|
| 1012 |
ast_bridge_features_cleanup(&chan_features); |
| 1013 |
|
| 1014 |
return res; |
| 1015 |
} |
| 1016 |
|
| 1017 |
int load_parking_applications(void) |
| 1018 |
{ |
| 1019 |
if (ast_register_application_xml(PARK_APPLICATION, park_app_exec)) { |
| 1020 |
return -1; |
| 1021 |
} |
| 1022 |
|
| 1023 |
if (ast_register_application_xml(PARKED_CALL_APPLICATION, parked_call_app_exec)) { |
| 1024 |
return -1; |
| 1025 |
} |
| 1026 |
|
| 1027 |
if (ast_register_application_xml(PARK_AND_ANNOUNCE_APPLICATION, park_and_announce_app_exec)) { |
| 1028 |
return -1; |
| 1029 |
} |
| 1030 |
|
| 1031 |
return 0; |
| 1032 |
} |
| 1033 |
|
| 1034 |
void unload_parking_applications(void) |
| 1035 |
{ |
| 1036 |
ast_unregister_application(PARK_APPLICATION); |
| 1037 |
ast_unregister_application(PARKED_CALL_APPLICATION); |
| 1038 |
ast_unregister_application(PARK_AND_ANNOUNCE_APPLICATION); |
| 1039 |
} |