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