Merge in the bridge_construction branch to make the system use the Bridging API.
[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 /*! \brief Helper function that creates an outgoing channel and returns it immediately */
99 static struct ast_channel *dial_transfer(struct ast_channel *caller, const char *exten, const char *context)
100 {
101         char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
102         struct ast_channel *chan;
103         int cause;
104
105         /* Fill the variable with the extension and context we want to call */
106         snprintf(destination, sizeof(destination), "%s@%s", exten, context);
107
108         /* Now we request a local channel to prepare to call the destination */
109         chan = ast_request("Local", ast_channel_nativeformats(caller), caller, destination,
110                 &cause);
111         if (!chan) {
112                 return NULL;
113         }
114
115         /* Before we actually dial out let's inherit appropriate information. */
116         ast_channel_lock_both(caller, chan);
117         ast_connected_line_copy_from_caller(ast_channel_connected(chan), ast_channel_caller(caller));
118         ast_channel_inherit_variables(caller, chan);
119         ast_channel_datastore_inherit(caller, chan);
120         ast_channel_unlock(chan);
121         ast_channel_unlock(caller);
122
123         /* Since the above worked fine now we actually call it and return the channel */
124         if (ast_call(chan, destination, 0)) {
125                 ast_hangup(chan);
126                 return NULL;
127         }
128
129         return chan;
130 }
131
132 /*!
133  * \internal
134  * \brief Determine the transfer context to use.
135  * \since 12.0.0
136  *
137  * \param transferer Channel initiating the transfer.
138  * \param context User supplied context if available.  May be NULL.
139  *
140  * \return The context to use for the transfer.
141  */
142 static const char *get_transfer_context(struct ast_channel *transferer, const char *context)
143 {
144         if (!ast_strlen_zero(context)) {
145                 return context;
146         }
147         context = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
148         if (!ast_strlen_zero(context)) {
149                 return context;
150         }
151         context = ast_channel_macrocontext(transferer);
152         if (!ast_strlen_zero(context)) {
153                 return context;
154         }
155         context = ast_channel_context(transferer);
156         if (!ast_strlen_zero(context)) {
157                 return context;
158         }
159         return "default";
160 }
161
162 /*! \brief Internal built in feature for blind transfers */
163 static int feature_blind_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
164 {
165         char exten[AST_MAX_EXTENSION] = "";
166         struct ast_channel *chan = NULL;
167         struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
168         const char *context;
169         struct ast_exten *park_exten;
170
171 /* BUGBUG the peer needs to be put on hold for the transfer. */
172         ast_channel_lock(bridge_channel->chan);
173         context = ast_strdupa(get_transfer_context(bridge_channel->chan,
174                 blind_transfer ? blind_transfer->context : NULL));
175         ast_channel_unlock(bridge_channel->chan);
176
177         /* Grab the extension to transfer to */
178         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
179                 return 0;
180         }
181
182         /* Parking blind transfer override - phase this out for something more general purpose in the future. */
183         park_exten = ast_get_parking_exten(exten, bridge_channel->chan, context);
184         if (park_exten) {
185                 /* We are transfering the transferee to a parking lot. */
186                 if (ast_park_blind_xfer(bridge, bridge_channel, park_exten)) {
187                         ast_log(LOG_ERROR, "%s attempted to transfer to park application and failed.\n", ast_channel_name(bridge_channel->chan));
188                 };
189                 return 0;
190         }
191
192 /* BUGBUG just need to ast_async_goto the peer so this bridge will go away and not accumulate local channels and bridges if the destination is to an application. */
193 /* ast_async_goto actually is a blind transfer. */
194 /* BUGBUG Use the bridge count to determine if can do DTMF transfer features.  If count is not 2 then don't allow it. */
195
196         /* Get a channel that is the destination we wish to call */
197         chan = dial_transfer(bridge_channel->chan, exten, context);
198         if (!chan) {
199                 return 0;
200         }
201
202         /* Impart the new channel onto the bridge, and have it take our place. */
203         if (ast_bridge_impart(bridge_channel->bridge, chan, bridge_channel->chan, NULL, 1)) {
204                 ast_hangup(chan);
205                 return 0;
206         }
207
208         return 0;
209 }
210
211 /*! Attended transfer code */
212 enum atxfer_code {
213         /*! Party C hungup or other reason to abandon the transfer. */
214         ATXFER_INCOMPLETE,
215         /*! Transfer party C to party A. */
216         ATXFER_COMPLETE,
217         /*! Turn the transfer into a threeway call. */
218         ATXFER_THREEWAY,
219         /*! Hangup party C and return party B to the bridge. */
220         ATXFER_ABORT,
221 };
222
223 /*! \brief Attended transfer feature to complete transfer */
224 static int attended_transfer_complete(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
225 {
226         enum atxfer_code *transfer_code = hook_pvt;
227
228         *transfer_code = ATXFER_COMPLETE;
229         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
230         return 0;
231 }
232
233 /*! \brief Attended transfer feature to turn it into a threeway call */
234 static int attended_transfer_threeway(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
235 {
236         enum atxfer_code *transfer_code = hook_pvt;
237
238         *transfer_code = ATXFER_THREEWAY;
239         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
240         return 0;
241 }
242
243 /*! \brief Attended transfer feature to abort transfer */
244 static int attended_transfer_abort(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
245 {
246         enum atxfer_code *transfer_code = hook_pvt;
247
248         *transfer_code = ATXFER_ABORT;
249         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
250         return 0;
251 }
252
253 /*! \brief Internal built in feature for attended transfers */
254 static int feature_attended_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
255 {
256         char exten[AST_MAX_EXTENSION] = "";
257         struct ast_channel *peer;
258         struct ast_bridge *attended_bridge;
259         struct ast_bridge_features caller_features;
260         int xfer_failed;
261         struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
262         const char *context;
263         enum atxfer_code transfer_code = ATXFER_INCOMPLETE;
264
265         bridge = ast_bridge_channel_merge_inhibit(bridge_channel, +1);
266
267 /* BUGBUG the peer needs to be put on hold for the transfer. */
268         ast_channel_lock(bridge_channel->chan);
269         context = ast_strdupa(get_transfer_context(bridge_channel->chan,
270                 attended_transfer ? attended_transfer->context : NULL));
271         ast_channel_unlock(bridge_channel->chan);
272
273         /* Grab the extension to transfer to */
274         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
275                 ast_bridge_merge_inhibit(bridge, -1);
276                 ao2_ref(bridge, -1);
277                 return 0;
278         }
279
280         /* Get a channel that is the destination we wish to call */
281         peer = dial_transfer(bridge_channel->chan, exten, context);
282         if (!peer) {
283                 ast_bridge_merge_inhibit(bridge, -1);
284                 ao2_ref(bridge, -1);
285 /* BUGBUG beeperr needs to be configurable from features.conf */
286                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
287                 return 0;
288         }
289
290 /* BUGBUG bridging API features does not support features.conf featuremap */
291 /* BUGBUG bridging API features does not support the features.conf atxfer bounce between C & B channels */
292         /* Setup a DTMF menu to control the transfer. */
293         if (ast_bridge_features_init(&caller_features)
294                 || ast_bridge_hangup_hook(&caller_features,
295                         attended_transfer_complete, &transfer_code, NULL, 0)
296                 || ast_bridge_dtmf_hook(&caller_features,
297                         attended_transfer && !ast_strlen_zero(attended_transfer->abort)
298                                 ? attended_transfer->abort : "*1",
299                         attended_transfer_abort, &transfer_code, NULL, 0)
300                 || ast_bridge_dtmf_hook(&caller_features,
301                         attended_transfer && !ast_strlen_zero(attended_transfer->complete)
302                                 ? attended_transfer->complete : "*2",
303                         attended_transfer_complete, &transfer_code, NULL, 0)
304                 || ast_bridge_dtmf_hook(&caller_features,
305                         attended_transfer && !ast_strlen_zero(attended_transfer->threeway)
306                                 ? attended_transfer->threeway : "*3",
307                         attended_transfer_threeway, &transfer_code, NULL, 0)) {
308                 ast_bridge_features_cleanup(&caller_features);
309                 ast_hangup(peer);
310                 ast_bridge_merge_inhibit(bridge, -1);
311                 ao2_ref(bridge, -1);
312 /* BUGBUG beeperr needs to be configurable from features.conf */
313                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
314                 return 0;
315         }
316
317         /* Create a bridge to use to talk to the person we are calling */
318         attended_bridge = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX,
319                 AST_BRIDGE_FLAG_DISSOLVE_HANGUP);
320         if (!attended_bridge) {
321                 ast_bridge_features_cleanup(&caller_features);
322                 ast_hangup(peer);
323                 ast_bridge_merge_inhibit(bridge, -1);
324                 ao2_ref(bridge, -1);
325 /* BUGBUG beeperr needs to be configurable from features.conf */
326                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
327                 return 0;
328         }
329         ast_bridge_merge_inhibit(attended_bridge, +1);
330
331         /* This is how this is going down, we are imparting the channel we called above into this bridge first */
332 /* BUGBUG we should impart the peer as an independent and move it to the original bridge. */
333         if (ast_bridge_impart(attended_bridge, peer, NULL, NULL, 0)) {
334                 ast_bridge_destroy(attended_bridge);
335                 ast_bridge_features_cleanup(&caller_features);
336                 ast_hangup(peer);
337                 ast_bridge_merge_inhibit(bridge, -1);
338                 ao2_ref(bridge, -1);
339 /* BUGBUG beeperr needs to be configurable from features.conf */
340                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
341                 return 0;
342         }
343
344         /*
345          * For the caller we want to join the bridge in a blocking
346          * fashion so we don't spin around in this function doing
347          * nothing while waiting.
348          */
349         ast_bridge_join(attended_bridge, bridge_channel->chan, NULL, &caller_features, NULL, 0);
350
351 /*
352  * BUGBUG there is a small window where the channel does not point to the bridge_channel.
353  *
354  * This window is expected to go away when atxfer is redesigned
355  * to fully support existing functionality.  There will be one
356  * and only one ast_bridge_channel structure per channel.
357  */
358         /* Point the channel back to the original bridge and bridge_channel. */
359         ast_bridge_channel_lock(bridge_channel);
360         ast_channel_lock(bridge_channel->chan);
361         ast_channel_internal_bridge_channel_set(bridge_channel->chan, bridge_channel);
362         ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
363         ast_channel_unlock(bridge_channel->chan);
364         ast_bridge_channel_unlock(bridge_channel);
365
366         /* Wait for peer thread to exit bridge and die. */
367         if (!ast_autoservice_start(bridge_channel->chan)) {
368                 ast_bridge_depart(peer);
369                 ast_autoservice_stop(bridge_channel->chan);
370         } else {
371                 ast_bridge_depart(peer);
372         }
373
374         /* Now that all channels are out of it we can destroy the bridge and the feature structures */
375         ast_bridge_destroy(attended_bridge);
376         ast_bridge_features_cleanup(&caller_features);
377
378         xfer_failed = -1;
379         switch (transfer_code) {
380         case ATXFER_INCOMPLETE:
381                 /* Peer hungup */
382                 break;
383         case ATXFER_COMPLETE:
384                 /* The peer takes our place in the bridge. */
385                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
386                 xfer_failed = ast_bridge_impart(bridge_channel->bridge, peer, bridge_channel->chan, NULL, 1);
387                 break;
388         case ATXFER_THREEWAY:
389                 /*
390                  * Transferer wants to convert to a threeway call.
391                  *
392                  * Just impart the peer onto the bridge and have us return to it
393                  * as normal.
394                  */
395                 xfer_failed = ast_bridge_impart(bridge_channel->bridge, peer, NULL, NULL, 1);
396                 break;
397         case ATXFER_ABORT:
398                 /* Transferer decided not to transfer the call after all. */
399                 break;
400         }
401         ast_bridge_merge_inhibit(bridge, -1);
402         ao2_ref(bridge, -1);
403         if (xfer_failed) {
404                 ast_hangup(peer);
405                 if (!ast_check_hangup_locked(bridge_channel->chan)) {
406                         ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_NONE);
407                 }
408         }
409
410         return 0;
411 }
412
413 /*! \brief Internal built in feature for hangup */
414 static int feature_hangup(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
415 {
416         /*
417          * This is very simple, we simply change the state on the
418          * bridge_channel to force the channel out of the bridge and the
419          * core takes care of the rest.
420          */
421         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
422         return 0;
423 }
424
425 static int unload_module(void)
426 {
427         return 0;
428 }
429
430 static int load_module(void)
431 {
432         ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
433         ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
434         ast_bridge_features_register(AST_BRIDGE_BUILTIN_HANGUP, feature_hangup, NULL);
435
436         /* Bump up our reference count so we can't be unloaded */
437         ast_module_ref(ast_module_info->self);
438
439         return AST_MODULE_LOAD_SUCCESS;
440 }
441
442 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging features");