3fa7e42b31ecd9bc0d5894f70f1f24378fa5bdcc
[asterisk/asterisk.git] / res / parking / parking_bridge_features.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Jonathan Rose <jrose@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Parking Bridge DTMF and Interval features
22  *
23  * \author Jonathan Rose <jrose@digium.com>
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/bridge.h"
34 #include "asterisk/bridge_internal.h"
35 #include "asterisk/bridge_channel.h"
36 #include "asterisk/bridge_features.h"
37 #include "asterisk/features.h"
38 #include "asterisk/say.h"
39 #include "asterisk/datastore.h"
40 #include "asterisk/stasis.h"
41 #include "asterisk/module.h"
42 #include "asterisk/core_local.h"
43 #include "asterisk/causes.h"
44
45 struct parked_subscription_datastore {
46         struct stasis_subscription *parked_subscription;
47 };
48
49 struct parked_subscription_data {
50         struct transfer_channel_data *transfer_data;
51         char *parkee_uuid;
52         int hangup_after:1;
53         char parker_uuid[0];
54 };
55
56 static void parked_subscription_datastore_destroy(void *data)
57 {
58         struct parked_subscription_datastore *subscription_datastore = data;
59
60         stasis_unsubscribe(subscription_datastore->parked_subscription);
61         subscription_datastore->parked_subscription = NULL;
62
63         ast_free(subscription_datastore);
64 }
65
66 static const struct ast_datastore_info parked_subscription_info = {
67         .type = "park subscription",
68         .destroy = parked_subscription_datastore_destroy,
69 };
70
71 static void wipe_subscription_datastore(struct ast_channel *chan)
72 {
73         struct ast_datastore *datastore;
74
75         ast_channel_lock(chan);
76
77         datastore = ast_channel_datastore_find(chan, &parked_subscription_info, NULL);
78         if (datastore) {
79                 ast_channel_datastore_remove(chan, datastore);
80                 ast_datastore_free(datastore);
81         }
82         ast_channel_unlock(chan);
83 }
84
85 static void parker_parked_call_message_response(struct ast_parked_call_payload *message, struct parked_subscription_data *data,
86         struct stasis_subscription *sub)
87 {
88         const char *parkee_to_act_on = data->parkee_uuid;
89         char saynum_buf[16];
90         struct ast_channel_snapshot *parkee_snapshot = message->parkee;
91         RAII_VAR(struct ast_channel *, parker, NULL, ast_channel_cleanup);
92         RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
93
94         if (strcmp(parkee_to_act_on, parkee_snapshot->uniqueid)) {
95                 return;
96         }
97
98         if (message->event_type != PARKED_CALL && message->event_type != PARKED_CALL_FAILED) {
99                 /* We only care about these two event types */
100                 return;
101         }
102
103         parker = ast_channel_get_by_name(data->parker_uuid);
104         if (!parker) {
105                 return;
106         }
107
108         ast_channel_lock(parker);
109         bridge_channel = ast_channel_get_bridge_channel(parker);
110         ast_channel_unlock(parker);
111         if (!bridge_channel) {
112                 return;
113         }
114
115         /* This subscription callback will block for the duration of the announcement if
116          * parked_subscription_data is tracking a transfer_channel_data struct. */
117         if (message->event_type == PARKED_CALL) {
118                 /* queue the saynum on the bridge channel and hangup */
119                 snprintf(saynum_buf, sizeof(saynum_buf), "%d %u", data->hangup_after, message->parkingspace);
120                 if (!data->transfer_data) {
121                         ast_bridge_channel_queue_playfile(bridge_channel, say_parking_space, saynum_buf, NULL);
122                 } else {
123                         ast_bridge_channel_queue_playfile_sync(bridge_channel, say_parking_space, saynum_buf, NULL);
124                         data->transfer_data->completed = 1;
125                 }
126                 wipe_subscription_datastore(parker);
127         } else if (message->event_type == PARKED_CALL_FAILED) {
128                 if (!data->transfer_data) {
129                         ast_bridge_channel_queue_playfile(bridge_channel, NULL, "pbx-parkingfailed", NULL);
130                 } else {
131                         ast_bridge_channel_queue_playfile_sync(bridge_channel, NULL, "pbx-parkingfailed", NULL);
132                         data->transfer_data->completed = 1;
133                 }
134                 wipe_subscription_datastore(parker);
135         }
136 }
137
138 static void parker_update_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
139 {
140         if (stasis_subscription_final_message(sub, message)) {
141                 struct parked_subscription_data *ps_data = data;
142                 ao2_cleanup(ps_data->transfer_data);
143                 ps_data->transfer_data = NULL;
144                 ast_free(data);
145                 return;
146         }
147
148         if (stasis_message_type(message) == ast_parked_call_type()) {
149                 struct ast_parked_call_payload *parked_call_message = stasis_message_data(message);
150                 parker_parked_call_message_response(parked_call_message, data, sub);
151         }
152 }
153
154 static int create_parked_subscription_full(struct ast_channel *chan, const char *parkee_uuid, int hangup_after,
155         struct transfer_channel_data *parked_channel_data)
156 {
157         struct ast_datastore *datastore;
158         struct parked_subscription_datastore *parked_datastore;
159         struct parked_subscription_data *subscription_data;
160
161         char *parker_uuid = ast_strdupa(ast_channel_uniqueid(chan));
162         size_t parker_uuid_size = strlen(parker_uuid) + 1;
163
164         /* If there is already a subscription, get rid of it. */
165         wipe_subscription_datastore(chan);
166
167         if (!(datastore = ast_datastore_alloc(&parked_subscription_info, NULL))) {
168                 return -1;
169         }
170
171         if (!(parked_datastore = ast_calloc(1, sizeof(*parked_datastore)))) {
172                 ast_datastore_free(datastore);
173                 return -1;
174         }
175
176         if (!(subscription_data = ast_calloc(1, sizeof(*subscription_data) + parker_uuid_size +
177                         strlen(parkee_uuid) + 1))) {
178                 ast_datastore_free(datastore);
179                 ast_free(parked_datastore);
180                 return -1;
181         }
182
183         if (parked_channel_data) {
184                 subscription_data->transfer_data = parked_channel_data;
185                 ao2_ref(parked_channel_data, +1);
186         }
187
188         subscription_data->hangup_after = hangup_after;
189         subscription_data->parkee_uuid = subscription_data->parker_uuid + parker_uuid_size;
190         strcpy(subscription_data->parkee_uuid, parkee_uuid);
191         strcpy(subscription_data->parker_uuid, parker_uuid);
192
193         if (!(parked_datastore->parked_subscription = stasis_subscribe_pool(ast_parking_topic(), parker_update_cb, subscription_data))) {
194                 return -1;
195         }
196
197         datastore->data = parked_datastore;
198
199         ast_channel_lock(chan);
200         ast_channel_datastore_add(chan, datastore);
201         ast_channel_unlock(chan);
202
203         return 0;
204 }
205
206 int create_parked_subscription(struct ast_channel *chan, const char *parkee_uuid, int hangup_after)
207 {
208         return create_parked_subscription_full(chan, parkee_uuid, hangup_after, NULL);
209 }
210
211 /*!
212  * \internal
213  * \brief Helper function that creates an outgoing channel and returns it immediately. This function is nearly
214  *        identical to the dial_transfer function in bridge_basic.c, however it doesn't swap the
215  *        local channel and the channel that instigated the park.
216  */
217 static struct ast_channel *park_local_transfer(struct ast_channel *parker, const char *context, const char *exten, struct transfer_channel_data *parked_channel_data)
218 {
219         char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
220         struct ast_channel *parkee;
221         struct ast_channel *parkee_side_2;
222         int cause;
223
224         /* Fill the variable with the extension and context we want to call */
225         snprintf(destination, sizeof(destination), "%s@%s", exten, context);
226
227         /* Now we request that chan_local prepare to call the destination */
228         parkee = ast_request("Local", ast_channel_nativeformats(parker), NULL, parker, destination,
229                 &cause);
230         if (!parkee) {
231                 return NULL;
232         }
233
234         /* Before we actually dial out let's inherit appropriate information. */
235         ast_channel_lock_both(parker, parkee);
236         ast_channel_req_accountcodes(parkee, parker, AST_CHANNEL_REQUESTOR_REPLACEMENT);
237         ast_channel_parkinglot_set(parkee, ast_channel_parkinglot(parker));
238         ast_connected_line_copy_from_caller(ast_channel_connected(parkee), ast_channel_caller(parker));
239         ast_channel_inherit_variables(parker, parkee);
240         ast_bridge_set_transfer_variables(parkee, ast_channel_name(parker), 0);
241         ast_channel_datastore_inherit(parker, parkee);
242         ast_channel_unlock(parker);
243
244         parkee_side_2 = ast_local_get_peer(parkee);
245         ast_assert(parkee_side_2 != NULL);
246         ast_channel_unlock(parkee);
247
248         /* We need to have the parker subscribe to the new local channel before hand. */
249         if (create_parked_subscription_full(parker, ast_channel_uniqueid(parkee_side_2), 1, parked_channel_data)) {
250                 ast_channel_unref(parkee_side_2);
251                 ast_hangup(parkee);
252                 return NULL;
253         }
254
255         ast_channel_unref(parkee_side_2);
256
257         /* Since the above worked fine now we actually call it and return the channel */
258         if (ast_call(parkee, destination, 0)) {
259                 ast_hangup(parkee);
260                 return NULL;
261         }
262
263         return parkee;
264 }
265
266 /*!
267  * \internal
268  * \brief Determine if an extension is a parking extension
269  */
270 static int parking_is_exten_park(const char *context, const char *exten)
271 {
272         struct ast_exten *exten_obj;
273         struct pbx_find_info info = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
274         const char *app_at_exten;
275
276         ast_debug(4, "Checking if %s@%s is a parking exten\n", exten, context);
277         exten_obj = pbx_find_extension(NULL, NULL, &info, context, exten, 1, NULL, NULL, E_MATCH);
278         if (!exten_obj) {
279                 return 0;
280         }
281
282         app_at_exten = ast_get_extension_app(exten_obj);
283         if (!app_at_exten || strcasecmp(PARK_APPLICATION, app_at_exten)) {
284                 return 0;
285         }
286
287         return 1;
288 }
289
290 /*!
291  * \internal
292  * \since 12.0.0
293  * \brief Perform a blind transfer to a parking lot
294  *
295  * In general, most parking features should work to call this function. This will safely
296  * park either a channel in the bridge with \ref bridge_channel or will park the entire
297  * bridge if more than one channel is in the bridge. It will create the correct data to
298  * pass to the \ref AstBridging Bridging API to safely park the channel.
299  *
300  * \param bridge_channel The bridge_channel representing the channel performing the park
301  * \param context The context to blind transfer to
302  * \param exten The extension to blind transfer to
303  * \param parked_channel_cb Optional callback executed prior to sending the parked channel into the bridge
304  * \param parked_channel_data Data for the parked_channel_cb
305  *
306  * \retval 0 on success
307  * \retval non-zero on error
308  */
309 static int parking_blind_transfer_park(struct ast_bridge_channel *bridge_channel,
310                 const char *context, const char *exten, transfer_channel_cb parked_channel_cb,
311                 struct transfer_channel_data *parked_channel_data)
312 {
313         RAII_VAR(struct ast_bridge_channel *, other, NULL, ao2_cleanup);
314         RAII_VAR(struct ast_channel *, other_chan, NULL, ast_channel_cleanup);
315
316         struct ast_exten *e;
317         struct pbx_find_info find_info = { .stacklen = 0 };
318         int peer_count;
319
320         if (ast_strlen_zero(context) || ast_strlen_zero(exten)) {
321                 return -1;
322         }
323
324         if (!bridge_channel->in_bridge) {
325                 return -1;
326         }
327
328         if (!parking_is_exten_park(context, exten)) {
329                 return -1;
330         }
331
332         ast_bridge_channel_lock_bridge(bridge_channel);
333         peer_count = bridge_channel->bridge->num_channels;
334         if (peer_count == 2) {
335                 other = ast_bridge_channel_peer(bridge_channel);
336                 ao2_ref(other, +1);
337                 other_chan = other->chan;
338                 ast_channel_ref(other_chan);
339         }
340         ast_bridge_unlock(bridge_channel->bridge);
341
342         if (peer_count < 2) {
343                 /* There is nothing to do if there is no one to park. */
344                 return -1;
345         }
346
347         /* With a multiparty bridge, we need to do a regular blind transfer. We link the
348          * existing bridge to the parking lot with a Local channel rather than
349          * transferring others. */
350         if (peer_count > 2) {
351                 struct ast_channel *transfer_chan = NULL;
352
353                 transfer_chan = park_local_transfer(bridge_channel->chan, context, exten, parked_channel_data);
354                 if (!transfer_chan) {
355                         return -1;
356                 }
357                 ast_channel_ref(transfer_chan);
358
359                 if (parked_channel_cb) {
360                         parked_channel_cb(transfer_chan, parked_channel_data, AST_BRIDGE_TRANSFER_MULTI_PARTY);
361                 }
362
363                 if (ast_bridge_impart(bridge_channel->bridge, transfer_chan, NULL, NULL,
364                         AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
365                         ast_hangup(transfer_chan);
366                         ast_channel_unref(transfer_chan);
367                         return -1;
368                 }
369
370                 ast_channel_unref(transfer_chan);
371
372                 return 0;
373         }
374
375         /* Subscribe to park messages with the other channel entering */
376         if (create_parked_subscription_full(bridge_channel->chan, ast_channel_uniqueid(other->chan), 1, parked_channel_data)) {
377                 return -1;
378         }
379
380         if (parked_channel_cb) {
381                 parked_channel_cb(other_chan, parked_channel_data, AST_BRIDGE_TRANSFER_SINGLE_PARTY);
382         }
383
384         e = pbx_find_extension(NULL, NULL, &find_info, context, exten, 1, NULL, NULL, E_MATCH);
385
386         /* Write the park frame with the intended recipient and other data out to the bridge. */
387         ast_bridge_channel_write_park(bridge_channel,
388                 ast_channel_uniqueid(other_chan),
389                 ast_channel_uniqueid(bridge_channel->chan),
390                 e ? ast_get_extension_app_data(e) : NULL);
391
392         return 0;
393 }
394
395 /*!
396  * \internal
397  * \since 12.0.0
398  * \brief Perform a direct park on a channel in a bridge
399  *
400  * \note This will be called from within the \ref AstBridging Bridging API
401  *
402  * \param bridge_channel The bridge_channel representing the channel to be parked
403  * \param uuid_parkee The UUID of the channel being parked
404  * \param uuid_parker The UUID of the channel performing the park
405  * \param app_data Application parseable data to pass to the parking application
406  */
407 static int parking_park_bridge_channel(struct ast_bridge_channel *bridge_channel, const char *uuid_parkee, const char *uuid_parker, const char *app_data)
408 {
409         RAII_VAR(struct ast_bridge *, parking_bridge, NULL, ao2_cleanup);
410         RAII_VAR(struct ast_bridge *, original_bridge, NULL, ao2_cleanup);
411         RAII_VAR(struct ast_channel *, parker, NULL, ao2_cleanup);
412
413         if (strcmp(ast_channel_uniqueid(bridge_channel->chan), uuid_parkee)) {
414                 /* We aren't the parkee, so ignore this action. */
415                 return -1;
416         }
417
418         parker = ast_channel_get_by_name(uuid_parker);
419
420         if (!parker) {
421                 ast_log(LOG_NOTICE, "Channel with uuid %s left before we could start parking the call. Parking canceled.\n", uuid_parker);
422                 publish_parked_call_failure(bridge_channel->chan);
423                 return -1;
424         }
425
426         if (!(parking_bridge = park_application_setup(bridge_channel->chan, parker, app_data, NULL))) {
427                 publish_parked_call_failure(bridge_channel->chan);
428                 return -1;
429         }
430
431         ast_bridge_set_transfer_variables(bridge_channel->chan, ast_channel_name(parker), 0);
432
433         /* bridge_channel must be locked so we can get a reference to the bridge it is currently on */
434         ao2_lock(bridge_channel);
435
436         original_bridge = bridge_channel->bridge;
437         if (!original_bridge) {
438                 ao2_unlock(bridge_channel);
439                 publish_parked_call_failure(bridge_channel->chan);
440                 return -1;
441         }
442
443         ao2_ref(original_bridge, +1); /* Cleaned by RAII_VAR */
444
445         ao2_unlock(bridge_channel);
446
447         if (ast_bridge_move(parking_bridge, original_bridge, bridge_channel->chan, NULL, 1)) {
448                 ast_log(LOG_ERROR, "Failed to move %s into the parking bridge.\n",
449                         ast_channel_name(bridge_channel->chan));
450                 return -1;
451         }
452
453         return 0;
454 }
455
456 /*!
457  * \internal
458  * \since 12.0.0
459  * \brief Park a call
460  *
461  * \param parker The bridge_channel parking the call
462  * \param exten Optional. The extension where the call was parked.
463  * \param length Optional. If \c exten is specified, the length of the buffer.
464  *
465  * \note This will determine the context and extension to park the channel based on
466  * the configuration of the \ref ast_channel associated with \ref parker. It will then
467  * park either the channel or the entire bridge.
468  *
469  * \retval 0 on success
470  * \retval -1 on error
471  */
472 static int parking_park_call(struct ast_bridge_channel *parker, char *exten, size_t length)
473 {
474         RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup);
475         const char *lot_name;
476
477         ast_channel_lock(parker->chan);
478         lot_name = ast_strdupa(find_channel_parking_lot_name(parker->chan));
479         ast_channel_unlock(parker->chan);
480
481         lot = parking_lot_find_by_name(lot_name);
482         if (!lot) {
483                 lot = parking_create_dynamic_lot(lot_name, parker->chan);
484         }
485         if (!lot) {
486                 ast_log(AST_LOG_WARNING, "Cannot Park %s: lot %s unknown\n",
487                         ast_channel_name(parker->chan), lot_name);
488                 return -1;
489         }
490
491         if (exten) {
492                 ast_copy_string(exten, lot->cfg->parkext, length);
493         }
494         return parking_blind_transfer_park(parker, lot->cfg->parking_con, lot->cfg->parkext, NULL, NULL);
495 }
496
497 static int feature_park_call(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
498 {
499         SCOPED_MODULE_USE(AST_MODULE_SELF);
500
501         parking_park_call(bridge_channel, NULL, 0);
502         return 0;
503 }
504
505 /*!
506  * \internal
507  * \brief Setup the caller features for when that channel is dialed.
508  * \since 12.0.0
509  *
510  * \param chan Parked channel leaving the parking lot.
511  * \param cfg Parking lot configuration.
512  *
513  * \return Nothing
514  */
515 static void parking_timeout_set_caller_features(struct ast_channel *chan, struct parking_lot_cfg *cfg)
516 {
517         char features[5];
518         char *pos;
519
520         /*
521          * We are setting the callee Dial flag values because in the
522          * timeout case, the caller is who is being called back.
523          */
524         pos = features;
525         if (cfg->parkedcalltransfers & AST_FEATURE_FLAG_BYCALLER) {
526                 *pos++ = 't';
527         }
528         if (cfg->parkedcallreparking & AST_FEATURE_FLAG_BYCALLER) {
529                 *pos++ = 'k';
530         }
531         if (cfg->parkedcallhangup & AST_FEATURE_FLAG_BYCALLER) {
532                 *pos++ = 'h';
533         }
534         if (cfg->parkedcallrecording & AST_FEATURE_FLAG_BYCALLER) {
535                 *pos++ = 'x';
536         }
537         *pos = '\0';
538
539         pbx_builtin_setvar_helper(chan, "BRIDGE_FEATURES", features);
540 }
541
542 /*! \internal
543  * \brief Interval hook. Pulls a parked call from the parking bridge after the timeout is passed and sets the resolution to timeout.
544  *
545  * \param bridge_channel bridge channel this interval hook is being executed on
546  * \param hook_pvt A pointer to the parked_user struct associated with the channel is stuffed in here
547  */
548 static int parking_duration_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
549 {
550         struct parked_user *user = hook_pvt;
551         struct ast_channel *chan = user->chan;
552         struct ast_context *park_dial_context;
553         const char *dial_string;
554         char *dial_string_flat;
555         char parking_space[AST_MAX_EXTENSION];
556
557         char returnexten[AST_MAX_EXTENSION];
558         char *duplicate_returnexten;
559         struct ast_exten *existing_exten;
560         struct pbx_find_info pbx_finder = { .stacklen = 0 }; /* The rest is reset in pbx_find_extension */
561
562
563         /* We are still in the bridge, so it's possible for other stuff to mess with the parked call before we leave the bridge
564            to deal with this, lock the parked user, check and set resolution. */
565         ao2_lock(user);
566         if (user->resolution != PARK_UNSET) {
567                 /* Abandon timeout since something else has resolved the parked user before we got to it. */
568                 ao2_unlock(user);
569                 return -1;
570         }
571         user->resolution = PARK_TIMEOUT;
572         ao2_unlock(user);
573
574         ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE,
575                 AST_CAUSE_NORMAL_CLEARING);
576
577         dial_string = user->parker_dial_string;
578         dial_string_flat = ast_strdupa(dial_string);
579         flatten_dial_string(dial_string_flat);
580
581         /* Set parking timeout channel variables */
582         snprintf(parking_space, sizeof(parking_space), "%d", user->parking_space);
583         ast_channel_lock(chan);
584         ast_channel_stage_snapshot(chan);
585         pbx_builtin_setvar_helper(chan, "PARKING_SPACE", parking_space);
586         pbx_builtin_setvar_helper(chan, "PARKINGSLOT", parking_space); /* Deprecated version of PARKING_SPACE */
587         pbx_builtin_setvar_helper(chan, "PARKEDLOT", user->lot->name);
588         pbx_builtin_setvar_helper(chan, "PARKER", dial_string);
589         pbx_builtin_setvar_helper(chan, "PARKER_FLAT", dial_string_flat);
590         parking_timeout_set_caller_features(chan, user->lot->cfg);
591         ast_channel_stage_snapshot_done(chan);
592         ast_channel_unlock(chan);
593
594         /* Dialplan generation for park-dial extensions */
595
596         if (ast_wrlock_contexts()) {
597                 ast_log(LOG_ERROR, "Failed to lock the contexts list. Can't add the park-dial extension.\n");
598                 return -1;
599         }
600
601         if (!(park_dial_context = ast_context_find_or_create(NULL, NULL, PARK_DIAL_CONTEXT, BASE_REGISTRAR))) {
602                 ast_log(LOG_ERROR, "Parking dial context '%s' does not exist and unable to create\n", PARK_DIAL_CONTEXT);
603                 if (ast_unlock_contexts()) {
604                         ast_assert(0);
605                 }
606                 goto abandon_extension_creation;
607         }
608
609         if (ast_wrlock_context(park_dial_context)) {
610                 ast_log(LOG_ERROR, "failed to obtain write lock on context '%s'\n", PARK_DIAL_CONTEXT);
611                 if (ast_unlock_contexts()) {
612                         ast_assert(0);
613                 }
614                 goto abandon_extension_creation;
615         }
616
617         if (ast_unlock_contexts()) {
618                 ast_assert(0);
619         }
620
621         snprintf(returnexten, sizeof(returnexten), "%s,%u", dial_string,
622                 user->lot->cfg->comebackdialtime);
623
624         duplicate_returnexten = ast_strdup(returnexten);
625         if (!duplicate_returnexten) {
626                 ast_log(LOG_ERROR, "Failed to create parking redial parker extension %s@%s - Dial(%s)\n",
627                         dial_string_flat, PARK_DIAL_CONTEXT, returnexten);
628         }
629
630         /* If an extension already exists here because we registered it for another parked call timing out, then we may overwrite it. */
631         if ((existing_exten = pbx_find_extension(NULL, NULL, &pbx_finder, PARK_DIAL_CONTEXT, dial_string_flat, 1, NULL, NULL, E_MATCH)) &&
632             (strcmp(ast_get_extension_registrar(existing_exten), BASE_REGISTRAR))) {
633                 ast_debug(3, "An extension for '%s@%s' was already registered by another registrar '%s'\n",
634                         dial_string_flat, PARK_DIAL_CONTEXT, ast_get_extension_registrar(existing_exten));
635         } else if (ast_add_extension2_nolock(park_dial_context, 1, dial_string_flat, 1, NULL, NULL,
636                         "Dial", duplicate_returnexten, ast_free_ptr, BASE_REGISTRAR)) {
637                         ast_free(duplicate_returnexten);
638                 ast_log(LOG_ERROR, "Failed to create parking redial parker extension %s@%s - Dial(%s)\n",
639                         dial_string_flat, PARK_DIAL_CONTEXT, returnexten);
640         }
641
642         if (ast_unlock_context(park_dial_context)) {
643                 ast_assert(0);
644         }
645
646 abandon_extension_creation:
647
648         /* async_goto the proper PBX destination - this should happen when we come out of the bridge */
649         if (!ast_strlen_zero(user->comeback)) {
650                 ast_async_parseable_goto(chan, user->comeback);
651         } else {
652                 comeback_goto(user, user->lot);
653         }
654
655         return -1;
656 }
657
658 void say_parking_space(struct ast_bridge_channel *bridge_channel, const char *payload)
659 {
660         unsigned int numeric_value;
661         unsigned int hangup_after;
662
663         if (sscanf(payload, "%u %u", &hangup_after, &numeric_value) != 2) {
664                 /* If say_parking_space is called with a non-numeric string, we have a problem. */
665                 ast_assert(0);
666                 ast_bridge_channel_leave_bridge(bridge_channel,
667                         BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
668                 return;
669         }
670
671         ast_say_digits(bridge_channel->chan, numeric_value, "",
672                 ast_channel_language(bridge_channel->chan));
673
674         if (hangup_after) {
675                 ast_bridge_channel_leave_bridge(bridge_channel,
676                         BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
677         }
678 }
679
680 void parking_set_duration(struct ast_bridge_features *features, struct parked_user *user)
681 {
682         unsigned int time_limit;
683
684         time_limit = user->time_limit * 1000;
685
686         if (!time_limit) {
687                 /* There is no duration limit that we need to apply. */
688                 return;
689         }
690
691         /* If the time limit has already been passed, set a really low time limit so we can kick them out immediately. */
692         time_limit = ast_remaining_ms(user->start, time_limit);
693         if (time_limit <= 0) {
694                 time_limit = 1;
695         }
696
697         /* The interval hook is going to need a reference to the parked_user */
698         ao2_ref(user, +1);
699
700         if (ast_bridge_interval_hook(features, 0, time_limit,
701                 parking_duration_callback, user, __ao2_cleanup, AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
702                 ast_log(LOG_ERROR, "Failed to apply duration limit to the parked call.\n");
703                 ao2_ref(user, -1);
704         }
705 }
706
707 struct ast_parking_bridge_feature_fn_table parking_provider = {
708         .module_version = PARKING_MODULE_VERSION,
709         .module_name = __FILE__,
710         .parking_is_exten_park = parking_is_exten_park,
711         .parking_blind_transfer_park = parking_blind_transfer_park,
712         .parking_park_bridge_channel = parking_park_bridge_channel,
713         .parking_park_call = parking_park_call,
714 };
715
716 void unload_parking_bridge_features(void)
717 {
718         ast_bridge_features_unregister(AST_BRIDGE_BUILTIN_PARKCALL);
719         ast_parking_unregister_bridge_features(parking_provider.module_name);
720 }
721
722 int load_parking_bridge_features(void)
723 {
724         parking_provider.module = AST_MODULE_SELF;
725
726         if (ast_parking_register_bridge_features(&parking_provider)) {
727                 return -1;
728         }
729
730         if (ast_bridge_features_register(AST_BRIDGE_BUILTIN_PARKCALL, feature_park_call, NULL)) {
731                 return -1;
732         }
733
734         return 0;
735 }