Fixed crashes from issue8824 review board channel locking changes.
[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 #include "asterisk/callerid.h"
44
45 #define PICKUPMARK "PICKUPMARK"
46
47 /*** DOCUMENTATION
48         <application name="Pickup" language="en_US">
49                 <synopsis>
50                         Directed extension call pickup.
51                 </synopsis>
52                 <syntax argsep="&amp;">
53                         <parameter name="ext" argsep="@" required="true">
54                                 <argument name="extension" required="true"/>
55                                 <argument name="context" />
56                         </parameter>
57                         <parameter name="ext2" argsep="@" multiple="true">
58                                 <argument name="extension2" required="true"/>
59                                 <argument name="context2"/>
60                         </parameter>
61                 </syntax>
62                 <description>
63                         <para>This application can pickup any ringing channel that is calling
64                         the specified <replaceable>extension</replaceable>. If no <replaceable>context</replaceable>
65                         is specified, the current context will be used. If you use the special string <literal>PICKUPMARK</literal>
66                         for the context parameter, for example 10@PICKUPMARK, this application
67                         tries to find a channel which has defined a <variable>PICKUPMARK</variable>
68                         channel variable with the same value as <replaceable>extension</replaceable>
69                         (in this example, <literal>10</literal>). When no parameter is specified, the application
70                         will pickup a channel matching the pickup group of the active channel.</para>
71                 </description>
72         </application>
73         <application name="PickupChan" language="en_US">
74                 <synopsis>
75                         Pickup a ringing channel.
76                 </synopsis>
77                 <syntax>
78                         <parameter name="channel" required="true" />
79                         <parameter name="channel2" multiple="true" />
80                 </syntax>
81                 <description>
82                         <para>This will pickup a specified <replaceable>channel</replaceable> if ringing.</para>
83                 </description>
84         </application>
85  ***/
86
87 static const char *app = "Pickup";
88 static const char *app2 = "PickupChan";
89 /*! \todo This application should return a result code, like PICKUPRESULT */
90
91 /* Perform actual pickup between two channels */
92 static int pickup_do(struct ast_channel *chan, struct ast_channel *target)
93 {
94         int res = 0;
95         struct ast_party_connected_line connected_caller;
96
97         ast_debug(1, "Call pickup on '%s' by '%s'\n", target->name, chan->name);
98
99         connected_caller = target->connected;
100         ast_party_connected_line_init(&target->connected);
101         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
102         ast_channel_update_connected_line(chan, &connected_caller);
103
104         ast_channel_lock(chan);
105         ast_connected_line_copy_from_caller(&connected_caller, &chan->cid);
106         ast_channel_unlock(chan);
107         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
108         ast_channel_queue_connected_line_update(chan, &connected_caller);
109         ast_party_connected_line_free(&connected_caller);
110
111         if ((res = ast_answer(chan))) {
112                 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
113                 return -1;
114         }
115
116         if ((res = ast_queue_control(chan, AST_CONTROL_ANSWER))) {
117                 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
118                 return -1;
119         }
120
121         if ((res = ast_channel_masquerade(target, chan))) {
122                 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
123                 return -1;
124         }
125
126         return res;
127 }
128
129 /* Helper function that determines whether a channel is capable of being picked up */
130 static int can_pickup(struct ast_channel *chan)
131 {
132         if (!chan->pbx && (chan->_state == AST_STATE_RINGING || chan->_state == AST_STATE_RING || chan->_state == AST_STATE_DOWN))
133                 return 1;
134         else
135                 return 0;
136 }
137
138 struct pickup_by_name_args {
139         const char *name;
140         size_t len;
141 };
142
143 static int pickup_by_name_cb(void *obj, void *arg, void *data, int flags)
144 {
145         struct ast_channel *chan = obj;
146         struct pickup_by_name_args *args = data;
147
148         ast_channel_lock(chan);
149         if (!strncasecmp(chan->name, args->name, args->len) && can_pickup(chan)) {
150                 /* Return with the channel still locked on purpose */
151                 return CMP_MATCH | CMP_STOP;
152         }
153         ast_channel_unlock(chan);
154
155         return 0;
156 }
157
158 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
159 static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
160 {
161         char *chkchan;
162         struct pickup_by_name_args pickup_args;
163
164         pickup_args.len = strlen(channame) + 2;
165
166         chkchan = alloca(pickup_args.len);
167
168         /* need to append a '-' for the comparison so we check full channel name,
169          * i.e SIP/hgc- , use a temporary variable so original stays the same for
170          * debugging.
171          */
172         strcpy(chkchan, channame);
173         strcat(chkchan, "-");
174
175         pickup_args.name = chkchan;
176
177         return ast_channel_callback(pickup_by_name_cb, NULL, &pickup_args, 0);
178 }
179
180 /*! \brief Attempt to pick up specified channel named , does not use context */
181 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
182 {
183         int res = 0;
184         struct ast_channel *target;
185
186         if (!(target = my_ast_get_channel_by_name_locked(pickup))) {
187                 return -1;
188         }
189
190         /* Just check that we are not picking up the SAME as target */
191         if (chan != target) {
192                 res = pickup_do(chan, target);
193         }
194
195         ast_channel_unlock(target);
196         target = ast_channel_unref(target);
197
198         return res;
199 }
200
201 /* Attempt to pick up specified extension with context */
202 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
203 {
204         struct ast_channel *target = NULL;
205         struct ast_channel_iterator *iter;
206         int res = -1;
207
208         if (!(iter = ast_channel_iterator_by_exten_new(0, exten, context))) {
209                 return -1;
210         }
211
212         while ((target = ast_channel_iterator_next(iter))) {
213                 ast_channel_lock(target);
214                 if (can_pickup(target)) {
215                         break;
216                 }
217                 ast_channel_unlock(target);
218                 target = ast_channel_unref(target);
219         }
220
221         if (target) {
222                 res = pickup_do(chan, target);
223                 ast_channel_unlock(target);
224                 target = ast_channel_unref(target);
225         }
226
227         return res;
228 }
229
230 static int find_by_mark(void *obj, void *arg, void *data, int flags)
231 {
232         struct ast_channel *c = obj;
233         const char *mark = data;
234         const char *tmp;
235         int res;
236
237         ast_channel_lock(c);
238
239         res = (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
240                 !strcasecmp(tmp, mark) &&
241                 can_pickup(c);
242
243         ast_channel_unlock(c);
244
245         return res ? CMP_MATCH | CMP_STOP : 0;
246 }
247
248 /* Attempt to pick up specified mark */
249 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
250 {
251         struct ast_channel *target;
252         int res = -1;
253
254         if ((target = ast_channel_callback(find_by_mark, NULL, (char *) mark, 0))) {
255                 ast_channel_lock(target);
256                 res = pickup_do(chan, target);
257                 ast_channel_unlock(target);
258                 target = ast_channel_unref(target);
259         }
260
261         return res;
262 }
263
264 /* application entry point for Pickup() */
265 static int pickup_exec(struct ast_channel *chan, void *data)
266 {
267         int res = 0;
268         char *tmp = ast_strdupa(data);
269         char *exten = NULL, *context = NULL;
270
271         if (ast_strlen_zero(data)) {
272                 res = ast_pickup_call(chan);
273                 return res;
274         }
275         
276         /* Parse extension (and context if there) */
277         while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
278                 if ((context = strchr(exten, '@')))
279                         *context++ = '\0';
280                 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
281                         if (!pickup_by_mark(chan, exten))
282                                 break;
283                 } else {
284                         if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context))
285                                 break;
286                 }
287                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
288         }
289
290         return res;
291 }
292
293 /* application entry point for PickupChan() */
294 static int pickupchan_exec(struct ast_channel *chan, void *data)
295 {
296         int res = 0;
297         char *tmp = ast_strdupa(data);
298         char *pickup = NULL;
299
300         if (ast_strlen_zero(data)) {
301                 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
302                 return -1;      
303         }
304
305         /* Parse channel */
306         while (!ast_strlen_zero(tmp) && (pickup = strsep(&tmp, "&"))) {
307                 if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
308                         ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
309                 } else {
310                         if (!pickup_by_channel(chan, pickup)) {
311                                 break;
312                         }
313                         ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
314                 }
315         }
316
317         return res;
318 }
319
320 static int unload_module(void)
321 {
322         int res;
323
324         res = ast_unregister_application(app);
325         res |= ast_unregister_application(app2);
326
327         return res;
328 }
329
330 static int load_module(void)
331 {
332         int res;
333
334         res = ast_register_application_xml(app, pickup_exec);
335         res |= ast_register_application_xml(app2, pickupchan_exec);
336
337         return res;
338 }
339
340 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");