Merge "ARI: Run 'make ari-stubs'"
[asterisk/asterisk.git] / apps / app_bridgeaddchan.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2015, Digium, Inc.
5  *
6  * Alec Davis <sivad.a@paradise.net.nz>
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 Application to place the channel into an existing Bridge
22  *
23  * \author Alec Davis
24  *
25  * \ingroup applications
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 #include "asterisk/file.h"
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/bridge.h"
38 #include "asterisk/features.h"
39
40 /*** DOCUMENTATION
41         <application name="BridgeAdd" language="en_US">
42                 <synopsis>
43                         Join a bridge that contains the specified channel.
44                 </synopsis>
45                 <syntax>
46                         <parameter name="name">
47                                 <para>Name of the channel in an existing bridge
48                                 </para>
49                         </parameter>
50                 </syntax>
51                 <description>
52                         <para>This application places the incoming channel into
53                         the bridge containing the specified channel. The specified
54                         channel only needs to be the prefix of a full channel name
55                         IE. 'SIP/cisco0001'.
56                         </para>
57                 </description>
58         </application>
59  ***/
60
61 static const char app[] = "BridgeAdd";
62
63 static int bridgeadd_exec(struct ast_channel *chan, const char *data)
64 {
65         struct ast_channel *c_ref;
66         struct ast_bridge_features chan_features;
67         struct ast_bridge *bridge;
68         char *c_name;
69
70         /* Answer the channel if needed */
71         if (ast_channel_state(chan) != AST_STATE_UP) {
72                 ast_answer(chan);
73         }
74
75         if (!(c_ref = ast_channel_get_by_name_prefix(data, strlen(data)))) {
76                 ast_log(LOG_WARNING, "Channel %s not found\n", data);
77                 return -1;
78         }
79
80         c_name = ast_strdupa(ast_channel_name(c_ref));
81
82         ast_channel_lock(c_ref);
83         bridge = ast_channel_get_bridge(c_ref);
84         ast_channel_unlock(c_ref);
85
86         ast_channel_unref(c_ref);
87
88         if (!bridge) {
89                 ast_log(LOG_WARNING, "Channel %s is not in a bridge\n", c_name);
90                 return -1;
91         }
92
93         ast_verb(3, "%s is joining %s in bridge %s\n", ast_channel_name(chan),
94                 c_name, bridge->uniqueid);
95
96         if (ast_bridge_features_init(&chan_features)
97                 || ast_bridge_join(bridge, chan, NULL, &chan_features, NULL, 0)) {
98
99                 ast_log(LOG_WARNING, "%s failed to join %s in bridge %s\n", ast_channel_name(chan),
100                          c_name, bridge->uniqueid);
101
102                 ast_bridge_features_cleanup(&chan_features);
103                 ao2_cleanup(bridge);
104                 return -1;
105         }
106
107         ast_bridge_features_cleanup(&chan_features);
108         ao2_cleanup(bridge);
109         return 0;
110 }
111
112 static int unload_module(void)
113 {
114         return ast_unregister_application(app);
115 }
116
117 static int load_module(void)
118 {
119         return ast_register_application_xml(app, bridgeadd_exec);
120 }
121
122 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Bridge Add Channel Application");