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