fixes some coding guideline issue
[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 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37
38 #include "asterisk/module.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/bridging.h"
41 #include "asterisk/bridging_technology.h"
42 #include "asterisk/frame.h"
43 #include "asterisk/file.h"
44 #include "asterisk/app.h"
45 #include "asterisk/astobj2.h"
46
47 /*! \brief Helper function that presents dialtone and grabs extension */
48 static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len, const char *context)
49 {
50         int res;
51
52         /* Play the simple "transfer" prompt out and wait */
53         res = ast_stream_and_wait(chan, "pbx-transfer", AST_DIGIT_ANY);
54         ast_stopstream(chan);
55
56         /* If the person hit a DTMF digit while the above played back stick it into the buffer */
57         if (res) {
58                 exten[0] = (char)res;
59         }
60
61         /* Drop to dialtone so they can enter the extension they want to transfer to */
62         res = ast_app_dtget(chan, context, exten, exten_len, 100, 1000);
63
64         return res;
65 }
66
67 /*! \brief Helper function that creates an outgoing channel and returns it immediately */
68 static struct ast_channel *dial_transfer(const struct ast_channel *caller, const char *exten, const char *context)
69 {
70         char destination[AST_MAX_EXTENSION+AST_MAX_CONTEXT+1] = "";
71         struct ast_channel *chan = NULL;
72         int cause;
73
74         /* Fill the variable with the extension and context we want to call */
75         snprintf(destination, sizeof(destination), "%s@%s", exten, context);
76
77         /* Now we request that chan_local prepare to call the destination */
78         if (!(chan = ast_request("Local", caller->nativeformats, caller, destination, &cause))) {
79                 return NULL;
80         }
81
82         /* Before we actually dial out let's inherit the appropriate dialplan variables */
83         ast_channel_inherit_variables(caller, chan);
84
85         /* Since the above worked fine now we actually call it and return the channel */
86         if (ast_call(chan, destination, 0)) {
87                 ast_hangup(chan);
88                 return NULL;
89         }
90
91         return chan;
92 }
93
94 /*! \brief Internal built in feature for blind transfers */
95 static int feature_blind_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
96 {
97         char exten[AST_MAX_EXTENSION] = "";
98         struct ast_channel *chan = NULL;
99         struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
100         const char *context = (blind_transfer && !ast_strlen_zero(blind_transfer->context) ? blind_transfer->context : bridge_channel->chan->context);
101
102         /* Grab the extension to transfer to */
103         if (!grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
104                 ast_stream_and_wait(bridge_channel->chan, "pbx-invalid", AST_DIGIT_ANY);
105                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
106                 return 0;
107         }
108
109         /* Get a channel that is the destination we wish to call */
110         if (!(chan = dial_transfer(bridge_channel->chan, exten, context))) {
111                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_ANY);
112                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
113                 return 0;
114         }
115
116         /* This is sort of the fun part. We impart the above channel onto the bridge, and have it take our place. */
117         ast_bridge_impart(bridge, chan, bridge_channel->chan, NULL);
118
119         return 0;
120 }
121
122 /*! \brief Attended transfer feature to turn it into a threeway call */
123 static int attended_threeway_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
124 {
125         /* This is sort of abusing the depart state but in this instance it is only going to be handled in the below function so it is okay */
126         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_DEPART);
127         return 0;
128 }
129
130 /*! \brief Attended transfer abort feature */
131 static int attended_abort_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
132 {
133         struct ast_bridge_channel *called_bridge_channel = NULL;
134
135         /* It is possible (albeit unlikely) that the bridge channels list may change, so we have to ensure we do all of our magic while locked */
136         ao2_lock(bridge);
137
138         if (AST_LIST_FIRST(&bridge->channels) != bridge_channel) {
139                 called_bridge_channel = AST_LIST_FIRST(&bridge->channels);
140         } else {
141                 called_bridge_channel = AST_LIST_LAST(&bridge->channels);
142         }
143
144         /* Now we basically eject the other channel from the bridge. This will cause their thread to hang them up, and our own code to consider the transfer failed. */
145         if (called_bridge_channel) {
146                 ast_bridge_change_state(called_bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
147         }
148
149         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
150
151         ao2_unlock(bridge);
152
153         return 0;
154 }
155
156 /*! \brief Internal built in feature for attended transfers */
157 static int feature_attended_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
158 {
159         char exten[AST_MAX_EXTENSION] = "";
160         struct ast_channel *chan = NULL;
161         struct ast_bridge *attended_bridge = NULL;
162         struct ast_bridge_features caller_features, called_features;
163         enum ast_bridge_channel_state attended_bridge_result;
164         struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
165         const char *context = (attended_transfer && !ast_strlen_zero(attended_transfer->context) ? attended_transfer->context : bridge_channel->chan->context);
166
167         /* Grab the extension to transfer to */
168         if (!grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
169                 ast_stream_and_wait(bridge_channel->chan, "pbx-invalid", AST_DIGIT_ANY);
170                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
171                 return 0;
172         }
173
174         /* Get a channel that is the destination we wish to call */
175         if (!(chan = dial_transfer(bridge_channel->chan, exten, context))) {
176                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_ANY);
177                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
178                 return 0;
179         }
180
181         /* Create a bridge to use to talk to the person we are calling */
182         if (!(attended_bridge = ast_bridge_new(AST_BRIDGE_CAPABILITY_1TO1MIX, 0))) {
183                 ast_hangup(chan);
184                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_ANY);
185                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
186                 return 0;
187         }
188
189         /* Setup our called features structure so that if they hang up we immediately get thrown out of the bridge */
190         ast_bridge_features_init(&called_features);
191         ast_bridge_features_set_flag(&called_features, AST_BRIDGE_FLAG_DISSOLVE);
192
193         /* This is how this is going down, we are imparting the channel we called above into this bridge first */
194         ast_bridge_impart(attended_bridge, chan, NULL, &called_features);
195
196         /* Before we join setup a features structure with the hangup option, just in case they want to use DTMF */
197         ast_bridge_features_init(&caller_features);
198         ast_bridge_features_enable(&caller_features, AST_BRIDGE_BUILTIN_HANGUP,
199                                    (attended_transfer && !ast_strlen_zero(attended_transfer->complete) ? attended_transfer->complete : "*1"), NULL);
200         ast_bridge_features_hook(&caller_features, (attended_transfer && !ast_strlen_zero(attended_transfer->threeway) ? attended_transfer->threeway : "*2"),
201                                  attended_threeway_transfer, NULL);
202         ast_bridge_features_hook(&caller_features, (attended_transfer && !ast_strlen_zero(attended_transfer->abort) ? attended_transfer->abort : "*3"),
203                                  attended_abort_transfer, NULL);
204
205         /* But for the caller we want to join the bridge in a blocking fashion so we don't spin around in this function doing nothing while waiting */
206         attended_bridge_result = ast_bridge_join(attended_bridge, bridge_channel->chan, NULL, &caller_features);
207
208         /* Since the above returned the caller features structure is of no more use */
209         ast_bridge_features_cleanup(&caller_features);
210
211         /* Drop the channel we are transferring to out of the above bridge since it has ended */
212         if ((attended_bridge_result != AST_BRIDGE_CHANNEL_STATE_HANGUP) && !ast_bridge_depart(attended_bridge, chan)) {
213                 /* If the user wants to turn this into a threeway transfer then do so, otherwise they take our place */
214                 if (attended_bridge_result == AST_BRIDGE_CHANNEL_STATE_DEPART) {
215                         /* We want to impart them upon the bridge and just have us return to it as normal */
216                         ast_bridge_impart(bridge, chan, NULL, NULL);
217                 } else {
218                         ast_bridge_impart(bridge, chan, bridge_channel->chan, NULL);
219                 }
220         } else {
221                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_ANY);
222                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
223         }
224
225         /* Now that all channels are out of it we can destroy the bridge and the called features structure */
226         ast_bridge_features_cleanup(&called_features);
227         ast_bridge_destroy(attended_bridge);
228
229         return 0;
230 }
231
232 /*! \brief Internal built in feature for hangup */
233 static int feature_hangup(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
234 {
235         /* This is very simple, we basically change the state on the bridge channel to end and the core takes care of the rest */
236         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
237         return 0;
238 }
239
240 static int unload_module(void)
241 {
242         return 0;
243 }
244
245 static int load_module(void)
246 {
247         ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
248         ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
249         ast_bridge_features_register(AST_BRIDGE_BUILTIN_HANGUP, feature_hangup, NULL);
250
251         /* Bump up our reference count so we can't be unloaded */
252         ast_module_ref(ast_module_info->self);
253
254         return AST_MODULE_LOAD_SUCCESS;
255 }
256
257 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging features");