Okay, that didn't fix the crash. It didn't really do anything useful.
[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         connected_caller = target->connected;
102         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
103         if (ast_channel_connected_line_macro(NULL, chan, &connected_caller, 0, 0)) {
104                 ast_channel_update_connected_line(chan, &connected_caller);
105         }
106
107         ast_channel_lock(chan);
108         ast_connected_line_copy_from_caller(&connected_caller, &chan->cid);
109         ast_channel_unlock(chan);
110         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
111         ast_channel_queue_connected_line_update(chan, &connected_caller);
112         ast_party_connected_line_free(&connected_caller);
113
114         if ((res = ast_answer(chan))) {
115                 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
116                 return -1;
117         }
118
119         if ((res = ast_queue_control(chan, AST_CONTROL_ANSWER))) {
120                 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
121                 return -1;
122         }
123
124         if ((res = ast_channel_masquerade(target, chan))) {
125                 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
126                 return -1;
127         }
128
129         return res;
130 }
131
132 /* Helper function that determines whether a channel is capable of being picked up */
133 static int can_pickup(struct ast_channel *chan)
134 {
135         if (!chan->pbx && (chan->_state == AST_STATE_RINGING || chan->_state == AST_STATE_RING || chan->_state == AST_STATE_DOWN))
136                 return 1;
137         else
138                 return 0;
139 }
140
141 struct pickup_by_name_args {
142         const char *name;
143         size_t len;
144 };
145
146 static int pickup_by_name_cb(void *obj, void *arg, void *data, int flags)
147 {
148         struct ast_channel *chan = obj;
149         struct pickup_by_name_args *args = data;
150
151         ast_channel_lock(chan);
152         if (!strncasecmp(chan->name, args->name, args->len) && can_pickup(chan)) {
153                 /* Return with the channel still locked on purpose */
154                 return CMP_MATCH | CMP_STOP;
155         }
156         ast_channel_unlock(chan);
157
158         return 0;
159 }
160
161 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
162 static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
163 {
164         char *chkchan;
165         struct pickup_by_name_args pickup_args;
166
167         pickup_args.len = strlen(channame) + 2;
168
169         chkchan = alloca(pickup_args.len);
170
171         /* need to append a '-' for the comparison so we check full channel name,
172          * i.e SIP/hgc- , use a temporary variable so original stays the same for
173          * debugging.
174          */
175         strcpy(chkchan, channame);
176         strcat(chkchan, "-");
177
178         pickup_args.name = chkchan;
179
180         return ast_channel_callback(pickup_by_name_cb, NULL, &pickup_args, 0);
181 }
182
183 /*! \brief Attempt to pick up specified channel named , does not use context */
184 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
185 {
186         int res = 0;
187         struct ast_channel *target;
188
189         if (!(target = my_ast_get_channel_by_name_locked(pickup))) {
190                 return -1;
191         }
192
193         /* Just check that we are not picking up the SAME as target */
194         if (chan != target) {
195                 res = pickup_do(chan, target);
196         }
197
198         ast_channel_unlock(target);
199         target = ast_channel_unref(target);
200
201         return res;
202 }
203
204 /* Attempt to pick up specified extension with context */
205 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
206 {
207         struct ast_channel *target = NULL;
208         struct ast_channel_iterator *iter;
209         int res = -1;
210
211         if (!(iter = ast_channel_iterator_by_exten_new(0, exten, context))) {
212                 return -1;
213         }
214
215         while ((target = ast_channel_iterator_next(iter))) {
216                 ast_channel_lock(target);
217                 if (can_pickup(target)) {
218                         break;
219                 }
220                 ast_channel_unlock(target);
221                 target = ast_channel_unref(target);
222         }
223
224         if (target) {
225                 res = pickup_do(chan, target);
226                 ast_channel_unlock(target);
227                 target = ast_channel_unref(target);
228         }
229
230         return res;
231 }
232
233 static int find_by_mark(void *obj, void *arg, void *data, int flags)
234 {
235         struct ast_channel *c = obj;
236         const char *mark = data;
237         const char *tmp;
238         int res;
239
240         ast_channel_lock(c);
241
242         res = (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
243                 !strcasecmp(tmp, mark) &&
244                 can_pickup(c);
245
246         ast_channel_unlock(c);
247
248         return res ? CMP_MATCH | CMP_STOP : 0;
249 }
250
251 /* Attempt to pick up specified mark */
252 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
253 {
254         struct ast_channel *target;
255         int res = -1;
256
257         if ((target = ast_channel_callback(find_by_mark, NULL, (char *) mark, 0))) {
258                 ast_channel_lock(target);
259                 res = pickup_do(chan, target);
260                 ast_channel_unlock(target);
261                 target = ast_channel_unref(target);
262         }
263
264         return res;
265 }
266
267 /* application entry point for Pickup() */
268 static int pickup_exec(struct ast_channel *chan, const char *data)
269 {
270         int res = 0;
271         char *tmp = ast_strdupa(data);
272         char *exten = NULL, *context = NULL;
273
274         if (ast_strlen_zero(data)) {
275                 res = ast_pickup_call(chan);
276                 return res;
277         }
278         
279         /* Parse extension (and context if there) */
280         while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
281                 if ((context = strchr(exten, '@')))
282                         *context++ = '\0';
283                 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
284                         if (!pickup_by_mark(chan, exten))
285                                 break;
286                 } else {
287                         if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context))
288                                 break;
289                 }
290                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
291         }
292
293         return res;
294 }
295
296 /* application entry point for PickupChan() */
297 static int pickupchan_exec(struct ast_channel *chan, const char *data)
298 {
299         int res = 0;
300         char *tmp = ast_strdupa(data);
301         char *pickup = NULL;
302
303         if (ast_strlen_zero(data)) {
304                 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
305                 return -1;      
306         }
307
308         /* Parse channel */
309         while (!ast_strlen_zero(tmp) && (pickup = strsep(&tmp, "&"))) {
310                 if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
311                         ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
312                 } else {
313                         if (!pickup_by_channel(chan, pickup)) {
314                                 break;
315                         }
316                         ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
317                 }
318         }
319
320         return res;
321 }
322
323 static int unload_module(void)
324 {
325         int res;
326
327         res = ast_unregister_application(app);
328         res |= ast_unregister_application(app2);
329
330         return res;
331 }
332
333 static int load_module(void)
334 {
335         int res;
336
337         res = ast_register_application_xml(app, pickup_exec);
338         res |= ast_register_application_xml(app2, pickupchan_exec);
339
340         return res;
341 }
342
343 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");