| 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/utils.h" |
| 30 |
#include "asterisk/astobj2.h" |
| 31 |
#include "asterisk/logger.h" |
| 32 |
#include "asterisk/pbx.h" |
| 33 |
#include "asterisk/app.h" |
| 34 |
#include "asterisk/bridge.h" |
| 35 |
#include "asterisk/bridge_internal.h" |
| 36 |
#include "asterisk/bridge_channel.h" |
| 37 |
#include "asterisk/bridge_features.h" |
| 38 |
#include "asterisk/conversions.h" |
| 39 |
#include "asterisk/features.h" |
| 40 |
#include "asterisk/say.h" |
| 41 |
#include "asterisk/datastore.h" |
| 42 |
#include "asterisk/stasis.h" |
| 43 |
#include "asterisk/module.h" |
| 44 |
#include "asterisk/core_local.h" |
| 45 |
#include "asterisk/causes.h" |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
struct parked_subscription_datastore { |
| 69 |
struct stasis_subscription *parked_subscription; |
| 70 |
}; |
| 71 |
|
| 72 |
struct parked_subscription_data { |
| 73 |
struct transfer_channel_data *transfer_data; |
| 74 |
char *parkee_uuid; |
| 75 |
unsigned int hangup_after:1; |
| 76 |
char parker_uuid[0]; |
| 77 |
}; |
| 78 |
|
| 79 |
static void parked_subscription_datastore_destroy(void *data) |
| 80 |
{ |
| 81 |
struct parked_subscription_datastore *subscription_datastore = data; |
| 82 |
|
| 83 |
stasis_unsubscribe(subscription_datastore->parked_subscription); |
| 84 |
subscription_datastore->parked_subscription = NULL; |
| 85 |
|
| 86 |
ast_free(subscription_datastore); |
| 87 |
} |
| 88 |
|
| 89 |
static const struct ast_datastore_info parked_subscription_info = { |
| 90 |
.type = "park subscription", |
| 91 |
.destroy = parked_subscription_datastore_destroy, |
| 92 |
}; |
| 93 |
|
| 94 |
static void wipe_subscription_datastore(struct ast_channel *chan) |
| 95 |
{ |
| 96 |
struct ast_datastore *datastore; |
| 97 |
|
| 98 |
ast_channel_lock(chan); |
| 99 |
|
| 100 |
datastore = ast_channel_datastore_find(chan, &parked_subscription_info, NULL); |
| 101 |
if (datastore) { |
| 102 |
ast_channel_datastore_remove(chan, datastore); |
| 103 |
ast_datastore_free(datastore); |
| 104 |
} |
| 105 |
ast_channel_unlock(chan); |
| 106 |
} |
| 107 |
|
| 108 |
static void parker_parked_call_message_response(struct ast_parked_call_payload *message, struct parked_subscription_data *data, |
| 109 |
struct stasis_subscription *sub) |
| 110 |
{ |
| 111 |
const char *parkee_to_act_on = data->parkee_uuid; |
| 112 |
char saynum_buf[16]; |
| 113 |
struct ast_channel_snapshot *parkee_snapshot = message->parkee; |
| 114 |
RAII_VAR(struct ast_channel *, parker, NULL, ast_channel_cleanup); |
| 115 |
RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup); |
| 116 |
|
| 117 |
if (strcmp(parkee_to_act_on, parkee_snapshot->base->uniqueid)) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
if (message->event_type != PARKED_CALL && message->event_type != PARKED_CALL_FAILED) { |
| 122 |
|
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
parker = ast_channel_get_by_name(data->parker_uuid); |
| 127 |
if (!parker) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
ast_channel_lock(parker); |
| 132 |
bridge_channel = ast_channel_get_bridge_channel(parker); |
| 133 |
ast_channel_unlock(parker); |
| 134 |
if (!bridge_channel) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
if (message->event_type == PARKED_CALL) { |
| 141 |
|
| 142 |
snprintf(saynum_buf, sizeof(saynum_buf), "%d %u", data->hangup_after, message->parkingspace); |
| 143 |
if (!data->transfer_data) { |
| 144 |
ast_bridge_channel_queue_playfile(bridge_channel, say_parking_space, saynum_buf, NULL); |
| 145 |
} else { |
| 146 |
ast_bridge_channel_queue_playfile_sync(bridge_channel, say_parking_space, saynum_buf, NULL); |
| 147 |
data->transfer_data->completed = 1; |
| 148 |
} |
| 149 |
wipe_subscription_datastore(parker); |
| 150 |
} else if (message->event_type == PARKED_CALL_FAILED) { |
| 151 |
if (!data->transfer_data) { |
| 152 |
ast_bridge_channel_queue_playfile(bridge_channel, NULL, "pbx-parkingfailed", NULL); |
| 153 |
} else { |
| 154 |
ast_bridge_channel_queue_playfile_sync(bridge_channel, NULL, "pbx-parkingfailed", NULL); |
| 155 |
data->transfer_data->completed = 1; |
| 156 |
} |
| 157 |
wipe_subscription_datastore(parker); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
static void parker_update_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 162 |
{ |
| 163 |
if (stasis_subscription_final_message(sub, message)) { |
| 164 |
struct parked_subscription_data *ps_data = data; |
| 165 |
ao2_cleanup(ps_data->transfer_data); |
| 166 |
ps_data->transfer_data = NULL; |
| 167 |
ast_free(data); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
if (stasis_message_type(message) == ast_parked_call_type()) { |
| 172 |
struct ast_parked_call_payload *parked_call_message = stasis_message_data(message); |
| 173 |
parker_parked_call_message_response(parked_call_message, data, sub); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
static int create_parked_subscription_full(struct ast_channel *chan, const char *parkee_uuid, int hangup_after, |
| 178 |
struct transfer_channel_data *parked_channel_data) |
| 179 |
{ |
| 180 |
struct ast_datastore *datastore; |
| 181 |
struct parked_subscription_datastore *parked_datastore; |
| 182 |
struct parked_subscription_data *subscription_data; |
| 183 |
|
| 184 |
char *parker_uuid = ast_strdupa(ast_channel_uniqueid(chan)); |
| 185 |
size_t parker_uuid_size; |
| 186 |
size_t parkee_uuid_size; |
| 187 |
|
| 188 |
|
| 189 |
wipe_subscription_datastore(chan); |
| 190 |
|
| 191 |
if (!(datastore = ast_datastore_alloc(&parked_subscription_info, NULL))) { |
| 192 |
return -1; |
| 193 |
} |
| 194 |
|
| 195 |
if (!(parked_datastore = ast_calloc(1, sizeof(*parked_datastore)))) { |
| 196 |
ast_datastore_free(datastore); |
| 197 |
return -1; |
| 198 |
} |
| 199 |
|
| 200 |
parker_uuid_size = strlen(parker_uuid) + 1; |
| 201 |
parkee_uuid_size = strlen(parkee_uuid) + 1; |
| 202 |
|
| 203 |
if (!(subscription_data = ast_calloc(1, sizeof(*subscription_data) + parker_uuid_size + |
| 204 |
parkee_uuid_size))) { |
| 205 |
ast_datastore_free(datastore); |
| 206 |
ast_free(parked_datastore); |
| 207 |
return -1; |
| 208 |
} |
| 209 |
|
| 210 |
if (parked_channel_data) { |
| 211 |
subscription_data->transfer_data = parked_channel_data; |
| 212 |
ao2_ref(parked_channel_data, +1); |
| 213 |
} |
| 214 |
|
| 215 |
subscription_data->hangup_after = hangup_after; |
| 216 |
subscription_data->parkee_uuid = subscription_data->parker_uuid + parker_uuid_size; |
| 217 |
ast_copy_string(subscription_data->parkee_uuid, parkee_uuid, parkee_uuid_size); |
| 218 |
ast_copy_string(subscription_data->parker_uuid, parker_uuid, parker_uuid_size); |
| 219 |
|
| 220 |
if (!(parked_datastore->parked_subscription = stasis_subscribe_pool(ast_parking_topic(), parker_update_cb, subscription_data))) { |
| 221 |
return -1; |
| 222 |
} |
| 223 |
stasis_subscription_accept_message_type(parked_datastore->parked_subscription, ast_parked_call_type()); |
| 224 |
stasis_subscription_accept_message_type(parked_datastore->parked_subscription, stasis_subscription_change_type()); |
| 225 |
stasis_subscription_set_filter(parked_datastore->parked_subscription, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 226 |
|
| 227 |
datastore->data = parked_datastore; |
| 228 |
|
| 229 |
ast_channel_lock(chan); |
| 230 |
ast_channel_datastore_add(chan, datastore); |
| 231 |
ast_channel_unlock(chan); |
| 232 |
|
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
int create_parked_subscription(struct ast_channel *chan, const char *parkee_uuid, int hangup_after) |
| 237 |
{ |
| 238 |
return create_parked_subscription_full(chan, parkee_uuid, hangup_after, NULL); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
static struct ast_channel *park_local_transfer(struct ast_channel *parker, const char *context, const char *exten, struct transfer_channel_data *parked_channel_data) |
| 248 |
{ |
| 249 |
char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1]; |
| 250 |
struct ast_channel *parkee; |
| 251 |
struct ast_channel *parkee_side_2; |
| 252 |
int cause; |
| 253 |
|
| 254 |
|
| 255 |
snprintf(destination, sizeof(destination), "%s@%s", exten, context); |
| 256 |
|
| 257 |
|
| 258 |
parkee = ast_request("Local", ast_channel_nativeformats(parker), NULL, parker, destination, |
| 259 |
&cause); |
| 260 |
if (!parkee) { |
| 261 |
return NULL; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
ast_channel_lock_both(parker, parkee); |
| 266 |
ast_channel_req_accountcodes(parkee, parker, AST_CHANNEL_REQUESTOR_REPLACEMENT); |
| 267 |
ast_channel_parkinglot_set(parkee, ast_channel_parkinglot(parker)); |
| 268 |
ast_connected_line_copy_from_caller(ast_channel_connected(parkee), ast_channel_caller(parker)); |
| 269 |
ast_channel_inherit_variables(parker, parkee); |
| 270 |
ast_bridge_set_transfer_variables(parkee, ast_channel_name(parker), 0); |
| 271 |
ast_channel_datastore_inherit(parker, parkee); |
| 272 |
ast_channel_unlock(parker); |
| 273 |
|
| 274 |
parkee_side_2 = ast_local_get_peer(parkee); |
| 275 |
ast_assert(parkee_side_2 != NULL); |
| 276 |
ast_channel_unlock(parkee); |
| 277 |
|
| 278 |
|
| 279 |
if (create_parked_subscription_full(parker, ast_channel_uniqueid(parkee_side_2), 1, parked_channel_data)) { |
| 280 |
ast_channel_unref(parkee_side_2); |
| 281 |
ast_hangup(parkee); |
| 282 |
return NULL; |
| 283 |
} |
| 284 |
|
| 285 |
ast_channel_unref(parkee_side_2); |
| 286 |
|
| 287 |
|
| 288 |
if (ast_call(parkee, destination, 0)) { |
| 289 |
ast_hangup(parkee); |
| 290 |
return NULL; |
| 291 |
} |
| 292 |
|
| 293 |
return parkee; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
static int parking_is_exten_park(const char *context, const char *exten) |
| 301 |
{ |
| 302 |
struct ast_exten *exten_obj; |
| 303 |
struct pbx_find_info info = { .stacklen = 0 }; |
| 304 |
const char *app_at_exten; |
| 305 |
|
| 306 |
ast_debug(4, "Checking if %s@%s is a parking exten\n", exten, context); |
| 307 |
exten_obj = pbx_find_extension(NULL, NULL, &info, context, exten, 1, NULL, NULL, E_MATCH); |
| 308 |
if (!exten_obj) { |
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
app_at_exten = ast_get_extension_app(exten_obj); |
| 313 |
if (!app_at_exten || strcasecmp(PARK_APPLICATION, app_at_exten)) { |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
return 1; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
static int parking_blind_transfer_park(struct ast_bridge_channel *bridge_channel, |
| 340 |
const char *context, const char *exten, transfer_channel_cb parked_channel_cb, |
| 341 |
struct transfer_channel_data *parked_channel_data) |
| 342 |
{ |
| 343 |
RAII_VAR(struct ast_bridge_channel *, other, NULL, ao2_cleanup); |
| 344 |
RAII_VAR(struct ast_channel *, other_chan, NULL, ast_channel_cleanup); |
| 345 |
|
| 346 |
struct ast_exten *e; |
| 347 |
struct pbx_find_info find_info = { .stacklen = 0 }; |
| 348 |
int peer_count; |
| 349 |
|
| 350 |
if (ast_strlen_zero(context) || ast_strlen_zero(exten)) { |
| 351 |
return -1; |
| 352 |
} |
| 353 |
|
| 354 |
if (!bridge_channel->in_bridge) { |
| 355 |
return -1; |
| 356 |
} |
| 357 |
|
| 358 |
if (!parking_is_exten_park(context, exten)) { |
| 359 |
return -1; |
| 360 |
} |
| 361 |
|
| 362 |
ast_bridge_channel_lock_bridge(bridge_channel); |
| 363 |
peer_count = bridge_channel->bridge->num_channels; |
| 364 |
if (peer_count == 2) { |
| 365 |
other = ast_bridge_channel_peer(bridge_channel); |
| 366 |
ao2_ref(other, +1); |
| 367 |
other_chan = other->chan; |
| 368 |
ast_channel_ref(other_chan); |
| 369 |
} |
| 370 |
ast_bridge_unlock(bridge_channel->bridge); |
| 371 |
|
| 372 |
if (peer_count < 2) { |
| 373 |
|
| 374 |
return -1; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
if (peer_count > 2) { |
| 381 |
struct ast_channel *transfer_chan = NULL; |
| 382 |
|
| 383 |
transfer_chan = park_local_transfer(bridge_channel->chan, context, exten, parked_channel_data); |
| 384 |
if (!transfer_chan) { |
| 385 |
return -1; |
| 386 |
} |
| 387 |
ast_channel_ref(transfer_chan); |
| 388 |
|
| 389 |
if (parked_channel_cb) { |
| 390 |
parked_channel_cb(transfer_chan, parked_channel_data, AST_BRIDGE_TRANSFER_MULTI_PARTY); |
| 391 |
} |
| 392 |
|
| 393 |
if (ast_bridge_impart(bridge_channel->bridge, transfer_chan, NULL, NULL, |
| 394 |
AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) { |
| 395 |
ast_hangup(transfer_chan); |
| 396 |
ast_channel_unref(transfer_chan); |
| 397 |
return -1; |
| 398 |
} |
| 399 |
|
| 400 |
ast_channel_unref(transfer_chan); |
| 401 |
|
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
if (create_parked_subscription_full(bridge_channel->chan, ast_channel_uniqueid(other->chan), 1, parked_channel_data)) { |
| 407 |
return -1; |
| 408 |
} |
| 409 |
|
| 410 |
if (parked_channel_cb) { |
| 411 |
parked_channel_cb(other_chan, parked_channel_data, AST_BRIDGE_TRANSFER_SINGLE_PARTY); |
| 412 |
} |
| 413 |
|
| 414 |
e = pbx_find_extension(NULL, NULL, &find_info, context, exten, 1, NULL, NULL, E_MATCH); |
| 415 |
|
| 416 |
|
| 417 |
ast_bridge_channel_write_park(bridge_channel, |
| 418 |
ast_channel_uniqueid(other_chan), |
| 419 |
ast_channel_uniqueid(bridge_channel->chan), |
| 420 |
e ? ast_get_extension_app_data(e) : NULL); |
| 421 |
|
| 422 |
return 0; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
static int parking_park_bridge_channel(struct ast_bridge_channel *bridge_channel, const char *uuid_parkee, const char *uuid_parker, const char *app_data) |
| 438 |
{ |
| 439 |
RAII_VAR(struct ast_bridge *, parking_bridge, NULL, ao2_cleanup); |
| 440 |
RAII_VAR(struct ast_bridge *, original_bridge, NULL, ao2_cleanup); |
| 441 |
RAII_VAR(struct ast_channel *, parker, NULL, ao2_cleanup); |
| 442 |
|
| 443 |
if (strcmp(ast_channel_uniqueid(bridge_channel->chan), uuid_parkee)) { |
| 444 |
|
| 445 |
return -1; |
| 446 |
} |
| 447 |
|
| 448 |
parker = ast_channel_get_by_name(uuid_parker); |
| 449 |
|
| 450 |
if (!parker) { |
| 451 |
ast_log(LOG_NOTICE, "Channel with uuid %s left before we could start parking the call. Parking canceled.\n", uuid_parker); |
| 452 |
publish_parked_call_failure(bridge_channel->chan); |
| 453 |
return -1; |
| 454 |
} |
| 455 |
|
| 456 |
if (!(parking_bridge = park_application_setup(bridge_channel->chan, parker, app_data, NULL))) { |
| 457 |
publish_parked_call_failure(bridge_channel->chan); |
| 458 |
return -1; |
| 459 |
} |
| 460 |
|
| 461 |
ast_bridge_set_transfer_variables(bridge_channel->chan, ast_channel_name(parker), 0); |
| 462 |
|
| 463 |
|
| 464 |
ao2_lock(bridge_channel); |
| 465 |
|
| 466 |
original_bridge = bridge_channel->bridge; |
| 467 |
if (!original_bridge) { |
| 468 |
ao2_unlock(bridge_channel); |
| 469 |
publish_parked_call_failure(bridge_channel->chan); |
| 470 |
return -1; |
| 471 |
} |
| 472 |
|
| 473 |
ao2_ref(original_bridge, +1); |
| 474 |
|
| 475 |
ao2_unlock(bridge_channel); |
| 476 |
|
| 477 |
if (ast_bridge_move(parking_bridge, original_bridge, bridge_channel->chan, NULL, 1)) { |
| 478 |
ast_log(LOG_ERROR, "Failed to move %s into the parking bridge.\n", |
| 479 |
ast_channel_name(bridge_channel->chan)); |
| 480 |
return -1; |
| 481 |
} |
| 482 |
|
| 483 |
return 0; |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
static int parking_park_call(struct ast_bridge_channel *parker, char *exten, size_t length) |
| 503 |
{ |
| 504 |
RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup); |
| 505 |
const char *lot_name; |
| 506 |
|
| 507 |
ast_channel_lock(parker->chan); |
| 508 |
lot_name = ast_strdupa(find_channel_parking_lot_name(parker->chan)); |
| 509 |
ast_channel_unlock(parker->chan); |
| 510 |
|
| 511 |
lot = parking_lot_find_by_name(lot_name); |
| 512 |
if (!lot) { |
| 513 |
lot = parking_create_dynamic_lot(lot_name, parker->chan); |
| 514 |
} |
| 515 |
if (!lot) { |
| 516 |
ast_log(AST_LOG_WARNING, "Cannot Park %s: lot %s unknown\n", |
| 517 |
ast_channel_name(parker->chan), lot_name); |
| 518 |
return -1; |
| 519 |
} |
| 520 |
|
| 521 |
if (exten) { |
| 522 |
ast_copy_string(exten, lot->cfg->parkext, length); |
| 523 |
} |
| 524 |
return parking_blind_transfer_park(parker, lot->cfg->parking_con, lot->cfg->parkext, NULL, NULL); |
| 525 |
} |
| 526 |
|
| 527 |
static int feature_park_call(struct ast_bridge_channel *bridge_channel, void *hook_pvt) |
| 528 |
{ |
| 529 |
SCOPED_MODULE_USE(AST_MODULE_SELF); |
| 530 |
|
| 531 |
parking_park_call(bridge_channel, NULL, 0); |
| 532 |
return 0; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
static void parking_timeout_set_caller_features(struct ast_channel *chan, struct parking_lot_cfg *cfg) |
| 544 |
{ |
| 545 |
char features[5]; |
| 546 |
char *pos; |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
pos = features; |
| 553 |
if (cfg->parkedcalltransfers & AST_FEATURE_FLAG_BYCALLER) { |
| 554 |
*pos++ = 't'; |
| 555 |
} |
| 556 |
if (cfg->parkedcallreparking & AST_FEATURE_FLAG_BYCALLER) { |
| 557 |
*pos++ = 'k'; |
| 558 |
} |
| 559 |
if (cfg->parkedcallhangup & AST_FEATURE_FLAG_BYCALLER) { |
| 560 |
*pos++ = 'h'; |
| 561 |
} |
| 562 |
if (cfg->parkedcallrecording & AST_FEATURE_FLAG_BYCALLER) { |
| 563 |
*pos++ = 'x'; |
| 564 |
} |
| 565 |
*pos = '\0'; |
| 566 |
|
| 567 |
pbx_builtin_setvar_helper(chan, "BRIDGE_FEATURES", features); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
static int parking_duration_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt) |
| 577 |
{ |
| 578 |
struct parked_user *user = hook_pvt; |
| 579 |
struct ast_channel *chan = user->chan; |
| 580 |
struct ast_context *park_dial_context; |
| 581 |
const char *dial_string; |
| 582 |
char *dial_string_flat; |
| 583 |
char parking_space[AST_MAX_EXTENSION]; |
| 584 |
|
| 585 |
char returnexten[AST_MAX_EXTENSION]; |
| 586 |
char *duplicate_returnexten; |
| 587 |
struct ast_exten *existing_exten; |
| 588 |
struct pbx_find_info pbx_finder = { .stacklen = 0 }; |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
ao2_lock(user); |
| 594 |
if (user->resolution != PARK_UNSET) { |
| 595 |
|
| 596 |
ao2_unlock(user); |
| 597 |
return -1; |
| 598 |
} |
| 599 |
user->resolution = PARK_TIMEOUT; |
| 600 |
ao2_unlock(user); |
| 601 |
|
| 602 |
ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, |
| 603 |
AST_CAUSE_NORMAL_CLEARING); |
| 604 |
|
| 605 |
dial_string = user->parker_dial_string; |
| 606 |
dial_string_flat = ast_strdupa(dial_string); |
| 607 |
flatten_dial_string(dial_string_flat); |
| 608 |
|
| 609 |
|
| 610 |
snprintf(parking_space, sizeof(parking_space), "%d", user->parking_space); |
| 611 |
ast_channel_lock(chan); |
| 612 |
ast_channel_stage_snapshot(chan); |
| 613 |
pbx_builtin_setvar_helper(chan, "PARKING_SPACE", parking_space); |
| 614 |
pbx_builtin_setvar_helper(chan, "PARKEDLOT", user->lot->name); |
| 615 |
pbx_builtin_setvar_helper(chan, "PARKER", dial_string); |
| 616 |
pbx_builtin_setvar_helper(chan, "PARKER_FLAT", dial_string_flat); |
| 617 |
parking_timeout_set_caller_features(chan, user->lot->cfg); |
| 618 |
ast_channel_stage_snapshot_done(chan); |
| 619 |
ast_channel_unlock(chan); |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
if (ast_wrlock_contexts()) { |
| 624 |
ast_log(LOG_ERROR, "Failed to lock the contexts list. Can't add the park-dial extension.\n"); |
| 625 |
return -1; |
| 626 |
} |
| 627 |
|
| 628 |
if (!(park_dial_context = ast_context_find_or_create(NULL, NULL, PARK_DIAL_CONTEXT, BASE_REGISTRAR))) { |
| 629 |
ast_log(LOG_ERROR, "Parking dial context '%s' does not exist and unable to create\n", PARK_DIAL_CONTEXT); |
| 630 |
if (ast_unlock_contexts()) { |
| 631 |
ast_assert(0); |
| 632 |
} |
| 633 |
goto abandon_extension_creation; |
| 634 |
} |
| 635 |
|
| 636 |
if (ast_wrlock_context(park_dial_context)) { |
| 637 |
ast_log(LOG_ERROR, "failed to obtain write lock on context '%s'\n", PARK_DIAL_CONTEXT); |
| 638 |
if (ast_unlock_contexts()) { |
| 639 |
ast_assert(0); |
| 640 |
} |
| 641 |
goto abandon_extension_creation; |
| 642 |
} |
| 643 |
|
| 644 |
if (ast_unlock_contexts()) { |
| 645 |
ast_assert(0); |
| 646 |
} |
| 647 |
|
| 648 |
snprintf(returnexten, sizeof(returnexten), "%s,%u", dial_string, |
| 649 |
user->lot->cfg->comebackdialtime); |
| 650 |
|
| 651 |
duplicate_returnexten = ast_strdup(returnexten); |
| 652 |
if (!duplicate_returnexten) { |
| 653 |
ast_log(LOG_ERROR, "Failed to create parking redial parker extension %s@%s - Dial(%s)\n", |
| 654 |
dial_string_flat, PARK_DIAL_CONTEXT, returnexten); |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
if ((existing_exten = pbx_find_extension(NULL, NULL, &pbx_finder, PARK_DIAL_CONTEXT, dial_string_flat, 1, NULL, NULL, E_MATCH)) && |
| 659 |
(strcmp(ast_get_extension_registrar(existing_exten), BASE_REGISTRAR))) { |
| 660 |
ast_debug(3, "An extension for '%s@%s' was already registered by another registrar '%s'\n", |
| 661 |
dial_string_flat, PARK_DIAL_CONTEXT, ast_get_extension_registrar(existing_exten)); |
| 662 |
} else if (ast_add_extension2_nolock(park_dial_context, 1, dial_string_flat, 1, NULL, NULL, |
| 663 |
"Dial", duplicate_returnexten, ast_free_ptr, BASE_REGISTRAR, NULL, 0)) { |
| 664 |
ast_log(LOG_ERROR, "Failed to create parking redial parker extension %s@%s - Dial(%s)\n", |
| 665 |
dial_string_flat, PARK_DIAL_CONTEXT, returnexten); |
| 666 |
} |
| 667 |
|
| 668 |
if (ast_unlock_context(park_dial_context)) { |
| 669 |
ast_assert(0); |
| 670 |
} |
| 671 |
|
| 672 |
abandon_extension_creation: |
| 673 |
|
| 674 |
|
| 675 |
if (!ast_strlen_zero(user->comeback)) { |
| 676 |
ast_async_parseable_goto(chan, user->comeback); |
| 677 |
} else { |
| 678 |
comeback_goto(user, user->lot); |
| 679 |
} |
| 680 |
|
| 681 |
return -1; |
| 682 |
} |
| 683 |
|
| 684 |
void say_parking_space(struct ast_bridge_channel *bridge_channel, const char *payload) |
| 685 |
{ |
| 686 |
unsigned int numeric_value; |
| 687 |
unsigned int hangup_after; |
| 688 |
|
| 689 |
if (sscanf(payload, "%u %u", &hangup_after, &numeric_value) != 2) { |
| 690 |
|
| 691 |
ast_assert(0); |
| 692 |
ast_bridge_channel_leave_bridge(bridge_channel, |
| 693 |
BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING); |
| 694 |
return; |
| 695 |
} |
| 696 |
|
| 697 |
ast_say_digits(bridge_channel->chan, numeric_value, "", |
| 698 |
ast_channel_language(bridge_channel->chan)); |
| 699 |
|
| 700 |
if (hangup_after) { |
| 701 |
ast_bridge_channel_leave_bridge(bridge_channel, |
| 702 |
BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
void parking_set_duration(struct ast_bridge_features *features, struct parked_user *user) |
| 707 |
{ |
| 708 |
unsigned int time_limit; |
| 709 |
|
| 710 |
time_limit = user->time_limit * 1000; |
| 711 |
|
| 712 |
if (!time_limit) { |
| 713 |
|
| 714 |
return; |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
time_limit = ast_remaining_ms(user->start, time_limit); |
| 719 |
if (time_limit <= 0) { |
| 720 |
time_limit = 1; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
ao2_ref(user, +1); |
| 725 |
|
| 726 |
if (ast_bridge_interval_hook(features, 0, time_limit, |
| 727 |
parking_duration_callback, user, __ao2_cleanup, AST_BRIDGE_HOOK_REMOVE_ON_PULL)) { |
| 728 |
ast_log(LOG_ERROR, "Failed to apply duration limit to the parked call.\n"); |
| 729 |
ao2_ref(user, -1); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
static int func_get_parkingslot_channel(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len) |
| 735 |
{ |
| 736 |
RAII_VAR(struct parked_user *, pu, NULL, ao2_cleanup); |
| 737 |
RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup); |
| 738 |
unsigned int space = 0; |
| 739 |
const char *content = NULL; |
| 740 |
|
| 741 |
AST_DECLARE_APP_ARGS(args, |
| 742 |
AST_APP_ARG(parking_space); |
| 743 |
AST_APP_ARG(parking_lot); |
| 744 |
AST_APP_ARG(other); |
| 745 |
); |
| 746 |
|
| 747 |
|
| 748 |
AST_STANDARD_APP_ARGS(args, data); |
| 749 |
|
| 750 |
if (args.argc < 2) { |
| 751 |
|
| 752 |
ast_log(LOG_ERROR, "Usage: %s(<parking_space>,<parking_lot>)\n", |
| 753 |
function); |
| 754 |
return -1; |
| 755 |
} |
| 756 |
|
| 757 |
lot = parking_lot_find_by_name(args.parking_lot); |
| 758 |
if (!lot) { |
| 759 |
ast_log(LOG_ERROR, "Could not find parking lot: '%s'\n", args.parking_lot); |
| 760 |
return -1; |
| 761 |
} |
| 762 |
|
| 763 |
if (!ast_strlen_zero(args.parking_space)) { |
| 764 |
if (ast_str_to_uint(args.parking_space, &space) != 0) { |
| 765 |
ast_log(LOG_ERROR, "value '%s' for parking_space argument is invalid. Must be an integer greater than 0.\n", |
| 766 |
args.parking_space); |
| 767 |
return -1; |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
pu = parking_lot_inspect_parked_user(lot, space); |
| 772 |
if (!pu) { |
| 773 |
return -1; |
| 774 |
} |
| 775 |
|
| 776 |
content = ast_channel_name(pu->chan); |
| 777 |
ast_copy_string(buf, content, len); |
| 778 |
|
| 779 |
return 0; |
| 780 |
} |
| 781 |
|
| 782 |
static struct ast_custom_function getparkingslotchannel_function = { |
| 783 |
.name = "PARK_GET_CHANNEL", |
| 784 |
.read = func_get_parkingslot_channel, |
| 785 |
}; |
| 786 |
|
| 787 |
struct ast_parking_bridge_feature_fn_table parking_provider = { |
| 788 |
.module_version = PARKING_MODULE_VERSION, |
| 789 |
.module_name = __FILE__, |
| 790 |
.parking_is_exten_park = parking_is_exten_park, |
| 791 |
.parking_blind_transfer_park = parking_blind_transfer_park, |
| 792 |
.parking_park_bridge_channel = parking_park_bridge_channel, |
| 793 |
.parking_park_call = parking_park_call, |
| 794 |
}; |
| 795 |
|
| 796 |
void unload_parking_bridge_features(void) |
| 797 |
{ |
| 798 |
ast_bridge_features_unregister(AST_BRIDGE_BUILTIN_PARKCALL); |
| 799 |
ast_parking_unregister_bridge_features(parking_provider.module_name); |
| 800 |
ast_custom_function_unregister(&getparkingslotchannel_function); |
| 801 |
} |
| 802 |
|
| 803 |
int load_parking_bridge_features(void) |
| 804 |
{ |
| 805 |
parking_provider.module = AST_MODULE_SELF; |
| 806 |
|
| 807 |
ast_custom_function_register(&getparkingslotchannel_function); |
| 808 |
|
| 809 |
if (ast_parking_register_bridge_features(&parking_provider)) { |
| 810 |
return -1; |
| 811 |
} |
| 812 |
|
| 813 |
if (ast_bridge_features_register(AST_BRIDGE_BUILTIN_PARKCALL, feature_park_call, NULL)) { |
| 814 |
return -1; |
| 815 |
} |
| 816 |
|
| 817 |
return 0; |
| 818 |
} |