fix a leaked channel lock (and future deadlock) when we try to pick up our own channel
[asterisk/asterisk.git] / apps / app_directed_pickup.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005, Joshua Colp
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * Portions merged from app_pickupchan, which was
9  * Copyright (C) 2008, Gary Cook
10  *
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  * This program is free software, distributed under the terms of
18  * the GNU General Public License Version 2. See the LICENSE file
19  * at the top of the source tree.
20  */
21
22 /*! \file
23  *
24  * \brief Directed Call Pickup Support
25  *
26  * \author Joshua Colp <jcolp@digium.com>
27  * \author Gary Cook
28  *
29  * \ingroup applications
30  */
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/file.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/lock.h"
41 #include "asterisk/app.h"
42 #include "asterisk/features.h"
43
44 #define PICKUPMARK "PICKUPMARK"
45
46 /*** DOCUMENTATION
47         <application name="Pickup" language="en_US">
48                 <synopsis>
49                         Directed extension call pickup.
50                 </synopsis>
51                 <syntax argsep="&amp;">
52                         <parameter name="ext" argsep="@" required="true">
53                                 <argument name="extension" required="true"/>
54                                 <argument name="context" />
55                         </parameter>
56                         <parameter name="ext2" argsep="@" multiple="true">
57                                 <argument name="extension2" required="true"/>
58                                 <argument name="context2"/>
59                         </parameter>
60                 </syntax>
61                 <description>
62                         <para>This application can pickup any ringing channel that is calling
63                         the specified <replaceable>extension</replaceable>. If no <replaceable>context</replaceable>
64                         is specified, the current context will be used. If you use the special string <literal>PICKUPMARK</literal>
65                         for the context parameter, for example 10@PICKUPMARK, this application
66                         tries to find a channel which has defined a <variable>PICKUPMARK</variable>
67                         channel variable with the same value as <replaceable>extension</replaceable>
68                         (in this example, <literal>10</literal>). When no parameter is specified, the application
69                         will pickup a channel matching the pickup group of the active channel.</para>
70                 </description>
71         </application>
72         <application name="PickupChan" language="en_US">
73                 <synopsis>
74                         Pickup a ringing channel.
75                 </synopsis>
76                 <syntax>
77                         <parameter name="channel" required="true" />
78                         <parameter name="channel2" multiple="true" />
79                 </syntax>
80                 <description>
81                         <para>This will pickup a specified <replaceable>channel</replaceable> if ringing.</para>
82                 </description>
83         </application>
84  ***/
85
86 static const char *app = "Pickup";
87 static const char *app2 = "PickupChan";
88 /*! \todo This application should return a result code, like PICKUPRESULT */
89
90 /* Perform actual pickup between two channels */
91 static int pickup_do(struct ast_channel *chan, struct ast_channel *target)
92 {
93         int res = 0;
94
95         ast_debug(1, "Call pickup on '%s' by '%s'\n", target->name, chan->name);
96
97         if ((res = ast_answer(chan))) {
98                 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
99                 return -1;
100         }
101
102         if ((res = ast_queue_control(chan, AST_CONTROL_ANSWER))) {
103                 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
104                 return -1;
105         }
106
107         if ((res = ast_channel_masquerade(target, chan))) {
108                 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
109                 return -1;
110         }
111
112         return res;
113 }
114
115 /* Helper function that determines whether a channel is capable of being picked up */
116 static int can_pickup(struct ast_channel *chan)
117 {
118         if (!chan->pbx && (chan->_state == AST_STATE_RINGING || chan->_state == AST_STATE_RING || chan->_state == AST_STATE_DOWN))
119                 return 1;
120         else
121                 return 0;
122 }
123
124 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
125 static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
126 {
127         struct ast_channel *chan;
128         char *chkchan;
129         size_t channame_len, chkchan_len;
130
131         channame_len = strlen(channame);
132         chkchan_len = channame_len + 2;
133
134         chkchan = alloca(chkchan_len);
135
136         /* need to append a '-' for the comparison so we check full channel name,
137          * i.e SIP/hgc- , use a temporary variable so original stays the same for
138          * debugging.
139          */
140         strcpy(chkchan, channame);
141         strcat(chkchan, "-");
142
143         for (chan = ast_walk_channel_by_name_prefix_locked(NULL, channame, channame_len);
144                  chan;
145                  chan = ast_walk_channel_by_name_prefix_locked(chan, channame, channame_len)) {
146                 if (!strncasecmp(chan->name, chkchan, chkchan_len) && can_pickup(chan)) {
147                         return chan;
148                 }
149                 ast_channel_unlock(chan);
150         }
151         return NULL;
152 }
153
154 /*! \brief Attempt to pick up specified channel named , does not use context */
155 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
156 {
157         int res = 0;
158         struct ast_channel *target;
159
160         if (!(target = my_ast_get_channel_by_name_locked(pickup)))
161                 return -1;
162
163         /* Just check that we are not picking up the SAME as target */
164         if (chan->name != target->name && chan != target) {
165                 res = pickup_do(chan, target);
166         }
167         ast_channel_unlock(target);
168
169         return res;
170 }
171
172 struct pickup_criteria {
173         const char *exten;
174         const char *context;
175 };
176
177 static int find_by_exten(struct ast_channel *c, void *data)
178 {
179         struct pickup_criteria *info = data;
180
181         return (!strcasecmp(c->macroexten, info->exten) || !strcasecmp(c->exten, info->exten)) &&
182                 !strcasecmp(c->dialcontext, info->context) &&
183                 can_pickup(c);
184 }
185
186 /* Attempt to pick up specified extension with context */
187 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
188 {
189         struct ast_channel *target = NULL;
190         struct pickup_criteria search = {
191                 .exten = exten,
192                 .context = context,
193         };
194
195         target = ast_channel_search_locked(find_by_exten, &search);
196
197         if (target) {
198                 int res = pickup_do(chan, target);
199                 ast_channel_unlock(target);
200                 target = NULL;
201                 return res;
202         }
203
204         return -1;
205 }
206
207 static int find_by_mark(struct ast_channel *c, void *data)
208 {
209         const char *mark = data;
210         const char *tmp;
211
212         return (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
213                 !strcasecmp(tmp, mark) &&
214                 can_pickup(c);
215 }
216
217 /* Attempt to pick up specified mark */
218 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
219 {
220         struct ast_channel *target = ast_channel_search_locked(find_by_mark, (char *) mark);
221
222         if (target) {
223                 int res = pickup_do(chan, target);
224                 ast_channel_unlock(target);
225                 target = NULL;
226                 return res;
227         }
228
229         return -1;
230 }
231
232 /* application entry point for Pickup() */
233 static int pickup_exec(struct ast_channel *chan, void *data)
234 {
235         int res = 0;
236         char *tmp = ast_strdupa(data);
237         char *exten = NULL, *context = NULL;
238
239         if (ast_strlen_zero(data)) {
240                 res = ast_pickup_call(chan);
241                 return res;
242         }
243         
244         /* Parse extension (and context if there) */
245         while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
246                 if ((context = strchr(exten, '@')))
247                         *context++ = '\0';
248                 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
249                         if (!pickup_by_mark(chan, exten))
250                                 break;
251                 } else {
252                         if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context))
253                                 break;
254                 }
255                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
256         }
257
258         return res;
259 }
260
261 /* application entry point for PickupChan() */
262 static int pickupchan_exec(struct ast_channel *chan, void *data)
263 {
264         int res = 0;
265         char *tmp = ast_strdupa(data);
266         char *pickup = NULL;
267
268         if (ast_strlen_zero(data)) {
269                 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
270                 return -1;      
271         }
272
273         /* Parse channel */
274         while (!ast_strlen_zero(tmp) && (pickup = strsep(&tmp, "&"))) {
275                 if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
276                         ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
277                 } else {
278                         if (!pickup_by_channel(chan, pickup)) {
279                                 break;
280                         }
281                         ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
282                 }
283         }
284
285         return res;
286 }
287
288 static int unload_module(void)
289 {
290         int res;
291
292         res = ast_unregister_application(app);
293         res |= ast_unregister_application(app2);
294
295         return res;
296 }
297
298 static int load_module(void)
299 {
300         int res;
301
302         res = ast_register_application_xml(app, pickup_exec);
303         res |= ast_register_application_xml(app2, pickupchan_exec);
304
305         return res;
306 }
307
308 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");