Fixup hold/unhold with attended and blind transfers.
[asterisk/asterisk.git] / bridges / bridge_builtin_features.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@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 Built in bridging features
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  *
25  * \ingroup bridges
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #include "asterisk/module.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/bridging.h"
45 #include "asterisk/bridging_technology.h"
46 #include "asterisk/frame.h"
47 #include "asterisk/file.h"
48 #include "asterisk/app.h"
49 #include "asterisk/astobj2.h"
50 #include "asterisk/pbx.h"
51 #include "asterisk/parking.h"
52
53 /*!
54  * \brief Helper function that presents dialtone and grabs extension
55  *
56  * \retval 0 on success
57  * \retval -1 on failure
58  */
59 static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len, const char *context)
60 {
61         int res;
62
63         /* Play the simple "transfer" prompt out and wait */
64         res = ast_stream_and_wait(chan, "pbx-transfer", AST_DIGIT_ANY);
65         ast_stopstream(chan);
66         if (res < 0) {
67                 /* Hangup or error */
68                 return -1;
69         }
70         if (res) {
71                 /* Store the DTMF digit that interrupted playback of the file. */
72                 exten[0] = res;
73         }
74
75         /* Drop to dialtone so they can enter the extension they want to transfer to */
76 /* BUGBUG the timeout needs to be configurable from features.conf. */
77         res = ast_app_dtget(chan, context, exten, exten_len, exten_len - 1, 3000);
78         if (res < 0) {
79                 /* Hangup or error */
80                 res = -1;
81         } else if (!res) {
82                 /* 0 for invalid extension dialed. */
83                 if (ast_strlen_zero(exten)) {
84                         ast_debug(1, "%s dialed no digits.\n", ast_channel_name(chan));
85                 } else {
86                         ast_debug(1, "%s dialed '%s@%s' does not exist.\n",
87                                 ast_channel_name(chan), exten, context);
88                 }
89                 ast_stream_and_wait(chan, "pbx-invalid", AST_DIGIT_NONE);
90                 res = -1;
91         } else {
92                 /* Dialed extension is valid. */
93                 res = 0;
94         }
95         return res;
96 }
97
98 static void copy_caller_data(struct ast_channel *dest, struct ast_channel *caller)
99 {
100         ast_channel_lock_both(caller, dest);
101         ast_connected_line_copy_from_caller(ast_channel_connected(dest), ast_channel_caller(caller));
102         ast_channel_inherit_variables(caller, dest);
103         ast_channel_datastore_inherit(caller, dest);
104         ast_channel_unlock(dest);
105         ast_channel_unlock(caller);
106 }
107
108 /*! \brief Helper function that creates an outgoing channel and returns it immediately */
109 static struct ast_channel *dial_transfer(struct ast_channel *caller, const char *exten, const char *context)
110 {
111         char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
112         struct ast_channel *chan;
113         int cause;
114
115         /* Fill the variable with the extension and context we want to call */
116         snprintf(destination, sizeof(destination), "%s@%s", exten, context);
117
118         /* Now we request a local channel to prepare to call the destination */
119         chan = ast_request("Local", ast_channel_nativeformats(caller), caller, destination,
120                 &cause);
121         if (!chan) {
122                 return NULL;
123         }
124
125         /* Before we actually dial out let's inherit appropriate information. */
126         copy_caller_data(chan, caller);
127
128         /* Since the above worked fine now we actually call it and return the channel */
129         if (ast_call(chan, destination, 0)) {
130                 ast_hangup(chan);
131                 return NULL;
132         }
133
134         return chan;
135 }
136
137 /*!
138  * \internal
139  * \brief Determine the transfer context to use.
140  * \since 12.0.0
141  *
142  * \param transferer Channel initiating the transfer.
143  * \param context User supplied context if available.  May be NULL.
144  *
145  * \return The context to use for the transfer.
146  */
147 static const char *get_transfer_context(struct ast_channel *transferer, const char *context)
148 {
149         if (!ast_strlen_zero(context)) {
150                 return context;
151         }
152         context = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
153         if (!ast_strlen_zero(context)) {
154                 return context;
155         }
156         context = ast_channel_macrocontext(transferer);
157         if (!ast_strlen_zero(context)) {
158                 return context;
159         }
160         context = ast_channel_context(transferer);
161         if (!ast_strlen_zero(context)) {
162                 return context;
163         }
164         return "default";
165 }
166
167 static void blind_transfer_cb(struct ast_channel *new_channel, void *user_data,
168                 enum ast_transfer_type transfer_type)
169 {
170         struct ast_channel *transferer_channel = user_data;
171
172         if (transfer_type == AST_BRIDGE_TRANSFER_MULTI_PARTY) {
173                 copy_caller_data(new_channel, transferer_channel);
174         }
175 }
176
177 /*! \brief Internal built in feature for blind transfers */
178 static int feature_blind_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
179 {
180         char exten[AST_MAX_EXTENSION] = "";
181         struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
182         const char *context;
183         char *goto_on_blindxfr;
184
185         ast_bridge_channel_write_hold(bridge_channel, NULL);
186
187         ast_channel_lock(bridge_channel->chan);
188         context = ast_strdupa(get_transfer_context(bridge_channel->chan,
189                 blind_transfer ? blind_transfer->context : NULL));
190         goto_on_blindxfr = ast_strdupa(S_OR(pbx_builtin_getvar_helper(bridge_channel->chan,
191                 "GOTO_ON_BLINDXFR"), ""));
192         ast_channel_unlock(bridge_channel->chan);
193
194         /* Grab the extension to transfer to */
195         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
196                 ast_bridge_channel_write_unhold(bridge_channel);
197                 return 0;
198         }
199
200         if (!ast_strlen_zero(goto_on_blindxfr)) {
201                 ast_debug(1, "After transfer, transferer %s goes to %s\n",
202                                 ast_channel_name(bridge_channel->chan), goto_on_blindxfr);
203                 ast_after_bridge_set_go_on(bridge_channel->chan, NULL, NULL, 0, goto_on_blindxfr);
204         }
205
206         if (ast_bridge_transfer_blind(bridge_channel->chan, exten, context, blind_transfer_cb,
207                         bridge_channel->chan) != AST_BRIDGE_TRANSFER_SUCCESS &&
208                         !ast_strlen_zero(goto_on_blindxfr)) {
209                 ast_after_bridge_goto_discard(bridge_channel->chan);
210         }
211
212         return 0;
213 }
214
215 /*! Attended transfer code */
216 enum atxfer_code {
217         /*! Party C hungup or other reason to abandon the transfer. */
218         ATXFER_INCOMPLETE,
219         /*! Transfer party C to party A. */
220         ATXFER_COMPLETE,
221         /*! Turn the transfer into a threeway call. */
222         ATXFER_THREEWAY,
223         /*! Hangup party C and return party B to the bridge. */
224         ATXFER_ABORT,
225 };
226
227 /*! \brief Attended transfer feature to complete transfer */
228 static int attended_transfer_complete(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
229 {
230         enum atxfer_code *transfer_code = hook_pvt;
231
232         *transfer_code = ATXFER_COMPLETE;
233         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
234         return 0;
235 }
236
237 /*! \brief Attended transfer feature to turn it into a threeway call */
238 static int attended_transfer_threeway(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
239 {
240         enum atxfer_code *transfer_code = hook_pvt;
241
242         *transfer_code = ATXFER_THREEWAY;
243         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
244         return 0;
245 }
246
247 /*! \brief Attended transfer feature to abort transfer */
248 static int attended_transfer_abort(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
249 {
250         enum atxfer_code *transfer_code = hook_pvt;
251
252         *transfer_code = ATXFER_ABORT;
253         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
254         return 0;
255 }
256
257 /*! \brief Internal built in feature for attended transfers */
258 static int feature_attended_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
259 {
260         char exten[AST_MAX_EXTENSION] = "";
261         struct ast_channel *peer;
262         struct ast_bridge *attended_bridge;
263         struct ast_bridge_features caller_features;
264         int xfer_failed;
265         struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
266         const char *context;
267         enum atxfer_code transfer_code = ATXFER_INCOMPLETE;
268
269         ast_bridge_channel_write_hold(bridge_channel, NULL);
270
271         bridge = ast_bridge_channel_merge_inhibit(bridge_channel, +1);
272
273         ast_channel_lock(bridge_channel->chan);
274         context = ast_strdupa(get_transfer_context(bridge_channel->chan,
275                 attended_transfer ? attended_transfer->context : NULL));
276         ast_channel_unlock(bridge_channel->chan);
277
278         /* Grab the extension to transfer to */
279         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
280                 ast_bridge_merge_inhibit(bridge, -1);
281                 ao2_ref(bridge, -1);
282                 ast_bridge_channel_write_unhold(bridge_channel);
283                 return 0;
284         }
285
286         /* Get a channel that is the destination we wish to call */
287         peer = dial_transfer(bridge_channel->chan, exten, context);
288         if (!peer) {
289                 ast_bridge_merge_inhibit(bridge, -1);
290                 ao2_ref(bridge, -1);
291 /* BUGBUG beeperr needs to be configurable from features.conf */
292                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
293                 ast_bridge_channel_write_unhold(bridge_channel);
294                 return 0;
295         }
296
297 /* BUGBUG bridging API features does not support features.conf featuremap */
298 /* BUGBUG bridging API features does not support the features.conf atxfer bounce between C & B channels */
299         /* Setup a DTMF menu to control the transfer. */
300         if (ast_bridge_features_init(&caller_features)
301                 || ast_bridge_hangup_hook(&caller_features,
302                         attended_transfer_complete, &transfer_code, NULL, 0)
303                 || ast_bridge_dtmf_hook(&caller_features,
304                         attended_transfer && !ast_strlen_zero(attended_transfer->abort)
305                                 ? attended_transfer->abort : "*1",
306                         attended_transfer_abort, &transfer_code, NULL, 0)
307                 || ast_bridge_dtmf_hook(&caller_features,
308                         attended_transfer && !ast_strlen_zero(attended_transfer->complete)
309                                 ? attended_transfer->complete : "*2",
310                         attended_transfer_complete, &transfer_code, NULL, 0)
311                 || ast_bridge_dtmf_hook(&caller_features,
312                         attended_transfer && !ast_strlen_zero(attended_transfer->threeway)
313                                 ? attended_transfer->threeway : "*3",
314                         attended_transfer_threeway, &transfer_code, NULL, 0)) {
315                 ast_bridge_features_cleanup(&caller_features);
316                 ast_hangup(peer);
317                 ast_bridge_merge_inhibit(bridge, -1);
318                 ao2_ref(bridge, -1);
319 /* BUGBUG beeperr needs to be configurable from features.conf */
320                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
321                 ast_bridge_channel_write_unhold(bridge_channel);
322                 return 0;
323         }
324
325         /* Create a bridge to use to talk to the person we are calling */
326         attended_bridge = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX,
327                 AST_BRIDGE_FLAG_DISSOLVE_HANGUP);
328         if (!attended_bridge) {
329                 ast_bridge_features_cleanup(&caller_features);
330                 ast_hangup(peer);
331                 ast_bridge_merge_inhibit(bridge, -1);
332                 ao2_ref(bridge, -1);
333 /* BUGBUG beeperr needs to be configurable from features.conf */
334                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
335                 ast_bridge_channel_write_unhold(bridge_channel);
336                 return 0;
337         }
338         ast_bridge_merge_inhibit(attended_bridge, +1);
339
340         /* This is how this is going down, we are imparting the channel we called above into this bridge first */
341 /* BUGBUG we should impart the peer as an independent and move it to the original bridge. */
342         if (ast_bridge_impart(attended_bridge, peer, NULL, NULL, 0)) {
343                 ast_bridge_destroy(attended_bridge);
344                 ast_bridge_features_cleanup(&caller_features);
345                 ast_hangup(peer);
346                 ast_bridge_merge_inhibit(bridge, -1);
347                 ao2_ref(bridge, -1);
348 /* BUGBUG beeperr needs to be configurable from features.conf */
349                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
350                 ast_bridge_channel_write_unhold(bridge_channel);
351                 return 0;
352         }
353
354         /*
355          * For the caller we want to join the bridge in a blocking
356          * fashion so we don't spin around in this function doing
357          * nothing while waiting.
358          */
359         ast_bridge_join(attended_bridge, bridge_channel->chan, NULL, &caller_features, NULL, 0);
360
361 /*
362  * BUGBUG there is a small window where the channel does not point to the bridge_channel.
363  *
364  * This window is expected to go away when atxfer is redesigned
365  * to fully support existing functionality.  There will be one
366  * and only one ast_bridge_channel structure per channel.
367  */
368         /* Point the channel back to the original bridge and bridge_channel. */
369         ast_bridge_channel_lock(bridge_channel);
370         ast_channel_lock(bridge_channel->chan);
371         ast_channel_internal_bridge_channel_set(bridge_channel->chan, bridge_channel);
372         ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
373         ast_channel_unlock(bridge_channel->chan);
374         ast_bridge_channel_unlock(bridge_channel);
375
376         /* Wait for peer thread to exit bridge and die. */
377         if (!ast_autoservice_start(bridge_channel->chan)) {
378                 ast_bridge_depart(peer);
379                 ast_autoservice_stop(bridge_channel->chan);
380         } else {
381                 ast_bridge_depart(peer);
382         }
383
384         /* Now that all channels are out of it we can destroy the bridge and the feature structures */
385         ast_bridge_destroy(attended_bridge);
386         ast_bridge_features_cleanup(&caller_features);
387
388         xfer_failed = -1;
389         switch (transfer_code) {
390         case ATXFER_INCOMPLETE:
391                 /* Peer hungup */
392                 break;
393         case ATXFER_COMPLETE:
394                 /* The peer takes our place in the bridge. */
395                 ast_bridge_channel_write_unhold(bridge_channel);
396                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
397                 xfer_failed = ast_bridge_impart(bridge_channel->bridge, peer, bridge_channel->chan, NULL, 1);
398                 break;
399         case ATXFER_THREEWAY:
400                 /*
401                  * Transferer wants to convert to a threeway call.
402                  *
403                  * Just impart the peer onto the bridge and have us return to it
404                  * as normal.
405                  */
406                 ast_bridge_channel_write_unhold(bridge_channel);
407                 xfer_failed = ast_bridge_impart(bridge_channel->bridge, peer, NULL, NULL, 1);
408                 break;
409         case ATXFER_ABORT:
410                 /* Transferer decided not to transfer the call after all. */
411                 break;
412         }
413         ast_bridge_merge_inhibit(bridge, -1);
414         ao2_ref(bridge, -1);
415         if (xfer_failed) {
416                 ast_hangup(peer);
417                 if (!ast_check_hangup_locked(bridge_channel->chan)) {
418                         ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
419                 }
420                 ast_bridge_channel_write_unhold(bridge_channel);
421         }
422
423         return 0;
424 }
425
426 /*! \brief Internal built in feature for hangup */
427 static int feature_hangup(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
428 {
429         /*
430          * This is very simple, we simply change the state on the
431          * bridge_channel to force the channel out of the bridge and the
432          * core takes care of the rest.
433          */
434         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
435         return 0;
436 }
437
438 static int unload_module(void)
439 {
440         return 0;
441 }
442
443 static int load_module(void)
444 {
445         ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
446         ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
447         ast_bridge_features_register(AST_BRIDGE_BUILTIN_HANGUP, feature_hangup, NULL);
448
449         /* Bump up our reference count so we can't be unloaded */
450         ast_module_ref(ast_module_info->self);
451
452         return AST_MODULE_LOAD_SUCCESS;
453 }
454
455 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging features");