Adds support for a core attended transfer function plus adds some hiding of masquerades.
[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 /* BUGBUG the peer needs to be put on hold for the transfer. */
186         ast_channel_lock(bridge_channel->chan);
187         context = ast_strdupa(get_transfer_context(bridge_channel->chan,
188                 blind_transfer ? blind_transfer->context : NULL));
189         goto_on_blindxfr = ast_strdupa(S_OR(pbx_builtin_getvar_helper(bridge_channel->chan,
190                 "GOTO_ON_BLINDXFR"), ""));
191         ast_channel_unlock(bridge_channel->chan);
192
193         /* Grab the extension to transfer to */
194         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
195                 return 0;
196         }
197
198         if (!ast_strlen_zero(goto_on_blindxfr)) {
199                 ast_debug(1, "After transfer, transferer %s goes to %s\n",
200                                 ast_channel_name(bridge_channel->chan), goto_on_blindxfr);
201                 ast_after_bridge_set_go_on(bridge_channel->chan, NULL, NULL, 0, goto_on_blindxfr);
202         }
203
204         if (ast_bridge_transfer_blind(bridge_channel->chan, exten, context, blind_transfer_cb,
205                         bridge_channel->chan) != AST_BRIDGE_TRANSFER_SUCCESS &&
206                         !ast_strlen_zero(goto_on_blindxfr)) {
207                 ast_after_bridge_goto_discard(bridge_channel->chan);
208         }
209
210         return 0;
211 }
212
213 /*! Attended transfer code */
214 enum atxfer_code {
215         /*! Party C hungup or other reason to abandon the transfer. */
216         ATXFER_INCOMPLETE,
217         /*! Transfer party C to party A. */
218         ATXFER_COMPLETE,
219         /*! Turn the transfer into a threeway call. */
220         ATXFER_THREEWAY,
221         /*! Hangup party C and return party B to the bridge. */
222         ATXFER_ABORT,
223 };
224
225 /*! \brief Attended transfer feature to complete transfer */
226 static int attended_transfer_complete(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
227 {
228         enum atxfer_code *transfer_code = hook_pvt;
229
230         *transfer_code = ATXFER_COMPLETE;
231         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
232         return 0;
233 }
234
235 /*! \brief Attended transfer feature to turn it into a threeway call */
236 static int attended_transfer_threeway(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
237 {
238         enum atxfer_code *transfer_code = hook_pvt;
239
240         *transfer_code = ATXFER_THREEWAY;
241         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
242         return 0;
243 }
244
245 /*! \brief Attended transfer feature to abort transfer */
246 static int attended_transfer_abort(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
247 {
248         enum atxfer_code *transfer_code = hook_pvt;
249
250         *transfer_code = ATXFER_ABORT;
251         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
252         return 0;
253 }
254
255 /*! \brief Internal built in feature for attended transfers */
256 static int feature_attended_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
257 {
258         char exten[AST_MAX_EXTENSION] = "";
259         struct ast_channel *peer;
260         struct ast_bridge *attended_bridge;
261         struct ast_bridge_features caller_features;
262         int xfer_failed;
263         struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
264         const char *context;
265         enum atxfer_code transfer_code = ATXFER_INCOMPLETE;
266
267         bridge = ast_bridge_channel_merge_inhibit(bridge_channel, +1);
268
269 /* BUGBUG the peer needs to be put on hold for the transfer. */
270         ast_channel_lock(bridge_channel->chan);
271         context = ast_strdupa(get_transfer_context(bridge_channel->chan,
272                 attended_transfer ? attended_transfer->context : NULL));
273         ast_channel_unlock(bridge_channel->chan);
274
275         /* Grab the extension to transfer to */
276         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
277                 ast_bridge_merge_inhibit(bridge, -1);
278                 ao2_ref(bridge, -1);
279                 return 0;
280         }
281
282         /* Get a channel that is the destination we wish to call */
283         peer = dial_transfer(bridge_channel->chan, exten, context);
284         if (!peer) {
285                 ast_bridge_merge_inhibit(bridge, -1);
286                 ao2_ref(bridge, -1);
287 /* BUGBUG beeperr needs to be configurable from features.conf */
288                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
289                 return 0;
290         }
291
292 /* BUGBUG bridging API features does not support features.conf featuremap */
293 /* BUGBUG bridging API features does not support the features.conf atxfer bounce between C & B channels */
294         /* Setup a DTMF menu to control the transfer. */
295         if (ast_bridge_features_init(&caller_features)
296                 || ast_bridge_hangup_hook(&caller_features,
297                         attended_transfer_complete, &transfer_code, NULL, 0)
298                 || ast_bridge_dtmf_hook(&caller_features,
299                         attended_transfer && !ast_strlen_zero(attended_transfer->abort)
300                                 ? attended_transfer->abort : "*1",
301                         attended_transfer_abort, &transfer_code, NULL, 0)
302                 || ast_bridge_dtmf_hook(&caller_features,
303                         attended_transfer && !ast_strlen_zero(attended_transfer->complete)
304                                 ? attended_transfer->complete : "*2",
305                         attended_transfer_complete, &transfer_code, NULL, 0)
306                 || ast_bridge_dtmf_hook(&caller_features,
307                         attended_transfer && !ast_strlen_zero(attended_transfer->threeway)
308                                 ? attended_transfer->threeway : "*3",
309                         attended_transfer_threeway, &transfer_code, NULL, 0)) {
310                 ast_bridge_features_cleanup(&caller_features);
311                 ast_hangup(peer);
312                 ast_bridge_merge_inhibit(bridge, -1);
313                 ao2_ref(bridge, -1);
314 /* BUGBUG beeperr needs to be configurable from features.conf */
315                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
316                 return 0;
317         }
318
319         /* Create a bridge to use to talk to the person we are calling */
320         attended_bridge = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX,
321                 AST_BRIDGE_FLAG_DISSOLVE_HANGUP);
322         if (!attended_bridge) {
323                 ast_bridge_features_cleanup(&caller_features);
324                 ast_hangup(peer);
325                 ast_bridge_merge_inhibit(bridge, -1);
326                 ao2_ref(bridge, -1);
327 /* BUGBUG beeperr needs to be configurable from features.conf */
328                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
329                 return 0;
330         }
331         ast_bridge_merge_inhibit(attended_bridge, +1);
332
333         /* This is how this is going down, we are imparting the channel we called above into this bridge first */
334 /* BUGBUG we should impart the peer as an independent and move it to the original bridge. */
335         if (ast_bridge_impart(attended_bridge, peer, NULL, NULL, 0)) {
336                 ast_bridge_destroy(attended_bridge);
337                 ast_bridge_features_cleanup(&caller_features);
338                 ast_hangup(peer);
339                 ast_bridge_merge_inhibit(bridge, -1);
340                 ao2_ref(bridge, -1);
341 /* BUGBUG beeperr needs to be configurable from features.conf */
342                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
343                 return 0;
344         }
345
346         /*
347          * For the caller we want to join the bridge in a blocking
348          * fashion so we don't spin around in this function doing
349          * nothing while waiting.
350          */
351         ast_bridge_join(attended_bridge, bridge_channel->chan, NULL, &caller_features, NULL, 0);
352
353 /*
354  * BUGBUG there is a small window where the channel does not point to the bridge_channel.
355  *
356  * This window is expected to go away when atxfer is redesigned
357  * to fully support existing functionality.  There will be one
358  * and only one ast_bridge_channel structure per channel.
359  */
360         /* Point the channel back to the original bridge and bridge_channel. */
361         ast_bridge_channel_lock(bridge_channel);
362         ast_channel_lock(bridge_channel->chan);
363         ast_channel_internal_bridge_channel_set(bridge_channel->chan, bridge_channel);
364         ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
365         ast_channel_unlock(bridge_channel->chan);
366         ast_bridge_channel_unlock(bridge_channel);
367
368         /* Wait for peer thread to exit bridge and die. */
369         if (!ast_autoservice_start(bridge_channel->chan)) {
370                 ast_bridge_depart(peer);
371                 ast_autoservice_stop(bridge_channel->chan);
372         } else {
373                 ast_bridge_depart(peer);
374         }
375
376         /* Now that all channels are out of it we can destroy the bridge and the feature structures */
377         ast_bridge_destroy(attended_bridge);
378         ast_bridge_features_cleanup(&caller_features);
379
380         xfer_failed = -1;
381         switch (transfer_code) {
382         case ATXFER_INCOMPLETE:
383                 /* Peer hungup */
384                 break;
385         case ATXFER_COMPLETE:
386                 /* The peer takes our place in the bridge. */
387                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
388                 xfer_failed = ast_bridge_impart(bridge_channel->bridge, peer, bridge_channel->chan, NULL, 1);
389                 break;
390         case ATXFER_THREEWAY:
391                 /*
392                  * Transferer wants to convert to a threeway call.
393                  *
394                  * Just impart the peer onto the bridge and have us return to it
395                  * as normal.
396                  */
397                 xfer_failed = ast_bridge_impart(bridge_channel->bridge, peer, NULL, NULL, 1);
398                 break;
399         case ATXFER_ABORT:
400                 /* Transferer decided not to transfer the call after all. */
401                 break;
402         }
403         ast_bridge_merge_inhibit(bridge, -1);
404         ao2_ref(bridge, -1);
405         if (xfer_failed) {
406                 ast_hangup(peer);
407                 if (!ast_check_hangup_locked(bridge_channel->chan)) {
408                         ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
409                 }
410         }
411
412         return 0;
413 }
414
415 /*! \brief Internal built in feature for hangup */
416 static int feature_hangup(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
417 {
418         /*
419          * This is very simple, we simply change the state on the
420          * bridge_channel to force the channel out of the bridge and the
421          * core takes care of the rest.
422          */
423         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
424         return 0;
425 }
426
427 static int unload_module(void)
428 {
429         return 0;
430 }
431
432 static int load_module(void)
433 {
434         ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
435         ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
436         ast_bridge_features_register(AST_BRIDGE_BUILTIN_HANGUP, feature_hangup, NULL);
437
438         /* Bump up our reference count so we can't be unloaded */
439         ast_module_ref(ast_module_info->self);
440
441         return AST_MODULE_LOAD_SUCCESS;
442 }
443
444 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging features");