Fix a double free issue with the Pickup dialplan application.
[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         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
101         ast_channel_update_connected_line(chan, &connected_caller);
102
103         ast_channel_lock(chan);
104         ast_connected_line_copy_from_caller(&connected_caller, &chan->cid);
105         ast_channel_unlock(chan);
106         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
107         ast_channel_queue_connected_line_update(chan, &connected_caller);
108         ast_party_connected_line_init(&target->connected);
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 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
139 static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
140 {
141         struct ast_channel *chan;
142         char *chkchan;
143         size_t channame_len, chkchan_len;
144
145         channame_len = strlen(channame);
146         chkchan_len = channame_len + 2;
147
148         chkchan = alloca(chkchan_len);
149
150         /* need to append a '-' for the comparison so we check full channel name,
151          * i.e SIP/hgc- , use a temporary variable so original stays the same for
152          * debugging.
153          */
154         strcpy(chkchan, channame);
155         strcat(chkchan, "-");
156
157         for (chan = ast_walk_channel_by_name_prefix_locked(NULL, channame, channame_len);
158                  chan;
159                  chan = ast_walk_channel_by_name_prefix_locked(chan, channame, channame_len)) {
160                 if (!strncasecmp(chan->name, chkchan, chkchan_len) && can_pickup(chan)) {
161                         return chan;
162                 }
163                 ast_channel_unlock(chan);
164         }
165         return NULL;
166 }
167
168 /*! \brief Attempt to pick up specified channel named , does not use context */
169 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
170 {
171         int res = 0;
172         struct ast_channel *target;
173
174         if (!(target = my_ast_get_channel_by_name_locked(pickup)))
175                 return -1;
176
177         /* Just check that we are not picking up the SAME as target */
178         if (chan->name != target->name && chan != target) {
179                 res = pickup_do(chan, target);
180         }
181         ast_channel_unlock(target);
182
183         return res;
184 }
185
186 struct pickup_criteria {
187         const char *exten;
188         const char *context;
189 };
190
191 static int find_by_exten(struct ast_channel *c, void *data)
192 {
193         struct pickup_criteria *info = data;
194
195         return (!strcasecmp(c->macroexten, info->exten) || !strcasecmp(c->exten, info->exten)) &&
196                 !strcasecmp(c->dialcontext, info->context) &&
197                 can_pickup(c);
198 }
199
200 /* Attempt to pick up specified extension with context */
201 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
202 {
203         struct ast_channel *target = NULL;
204         struct pickup_criteria search = {
205                 .exten = exten,
206                 .context = context,
207         };
208
209         target = ast_channel_search_locked(find_by_exten, &search);
210
211         if (target) {
212                 int res = pickup_do(chan, target);
213                 ast_channel_unlock(target);
214                 target = NULL;
215                 return res;
216         }
217
218         return -1;
219 }
220
221 static int find_by_mark(struct ast_channel *c, void *data)
222 {
223         const char *mark = data;
224         const char *tmp;
225
226         return (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
227                 !strcasecmp(tmp, mark) &&
228                 can_pickup(c);
229 }
230
231 /* Attempt to pick up specified mark */
232 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
233 {
234         struct ast_channel *target = ast_channel_search_locked(find_by_mark, (char *) mark);
235
236         if (target) {
237                 int res = pickup_do(chan, target);
238                 ast_channel_unlock(target);
239                 target = NULL;
240                 return res;
241         }
242
243         return -1;
244 }
245
246 /* application entry point for Pickup() */
247 static int pickup_exec(struct ast_channel *chan, void *data)
248 {
249         int res = 0;
250         char *tmp = ast_strdupa(data);
251         char *exten = NULL, *context = NULL;
252
253         if (ast_strlen_zero(data)) {
254                 res = ast_pickup_call(chan);
255                 return res;
256         }
257         
258         /* Parse extension (and context if there) */
259         while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
260                 if ((context = strchr(exten, '@')))
261                         *context++ = '\0';
262                 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
263                         if (!pickup_by_mark(chan, exten))
264                                 break;
265                 } else {
266                         if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context))
267                                 break;
268                 }
269                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
270         }
271
272         return res;
273 }
274
275 /* application entry point for PickupChan() */
276 static int pickupchan_exec(struct ast_channel *chan, void *data)
277 {
278         int res = 0;
279         char *tmp = ast_strdupa(data);
280         char *pickup = NULL;
281
282         if (ast_strlen_zero(data)) {
283                 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
284                 return -1;      
285         }
286
287         /* Parse channel */
288         while (!ast_strlen_zero(tmp) && (pickup = strsep(&tmp, "&"))) {
289                 if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
290                         ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
291                 } else {
292                         if (!pickup_by_channel(chan, pickup)) {
293                                 break;
294                         }
295                         ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
296                 }
297         }
298
299         return res;
300 }
301
302 static int unload_module(void)
303 {
304         int res;
305
306         res = ast_unregister_application(app);
307         res |= ast_unregister_application(app2);
308
309         return res;
310 }
311
312 static int load_module(void)
313 {
314         int res;
315
316         res = ast_register_application_xml(app, pickup_exec);
317         res |= ast_register_application_xml(app2, pickupchan_exec);
318
319         return res;
320 }
321
322 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");