Merged revisions 329200 via svnmerge from
[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 /*** MODULEINFO
33         <support_level>core</support_level>
34  ***/
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39
40 #include "asterisk/file.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/module.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/app.h"
46 #include "asterisk/features.h"
47 #include "asterisk/manager.h"
48 #include "asterisk/callerid.h"
49 #include "asterisk/cel.h"
50
51 #define PICKUPMARK "PICKUPMARK"
52
53 /*** DOCUMENTATION
54         <application name="Pickup" language="en_US">
55                 <synopsis>
56                         Directed extension call pickup.
57                 </synopsis>
58                 <syntax argsep="&amp;">
59                         <parameter name="ext" argsep="@" required="true">
60                                 <argument name="extension" required="true"/>
61                                 <argument name="context" />
62                         </parameter>
63                         <parameter name="ext2" argsep="@" multiple="true">
64                                 <argument name="extension2" required="true"/>
65                                 <argument name="context2"/>
66                         </parameter>
67                 </syntax>
68                 <description>
69                         <para>This application can pickup any ringing channel that is calling
70                         the specified <replaceable>extension</replaceable>. If no <replaceable>context</replaceable>
71                         is specified, the current context will be used. If you use the special string <literal>PICKUPMARK</literal>
72                         for the context parameter, for example 10@PICKUPMARK, this application
73                         tries to find a channel which has defined a <variable>PICKUPMARK</variable>
74                         channel variable with the same value as <replaceable>extension</replaceable>
75                         (in this example, <literal>10</literal>). When no parameter is specified, the application
76                         will pickup a channel matching the pickup group of the active channel.</para>
77                 </description>
78         </application>
79         <application name="PickupChan" language="en_US">
80                 <synopsis>
81                         Pickup a ringing channel.
82                 </synopsis>
83                 <syntax >
84                         <parameter name="Technology/Resource" argsep="&amp;" required="true">
85                                 <argument name="Technology/Resource" required="true" />
86                                 <argument name="Technology2/Resource2" required="false" multiple="true" />
87                         </parameter>
88                         <parameter name="options" required="false">
89                                 <optionlist>
90                                         <option name="p">
91                                                 <para>Channel name specified partial name. Used when find channel by callid.</para>
92                                         </option>
93                                 </optionlist>
94                         </parameter>
95                 </syntax>
96                 <description>
97                         <para>This will pickup a specified <replaceable>channel</replaceable> if ringing.</para>
98                 </description>
99         </application>
100  ***/
101
102 static const char app[] = "Pickup";
103 static const char app2[] = "PickupChan";
104 /*! \todo This application should return a result code, like PICKUPRESULT */
105
106 struct pickup_by_name_args {
107         const char *name;
108         size_t len;
109 };
110
111 static int pickup_by_name_cb(void *obj, void *arg, void *data, int flags)
112 {
113         struct ast_channel *target = obj;/*!< Potential pickup target */
114         struct pickup_by_name_args *args = data;
115
116         ast_channel_lock(target);
117         if (!strncasecmp(target->name, args->name, args->len) && ast_can_pickup(target)) {
118                 /* Return with the channel still locked on purpose */
119                 return CMP_MATCH | CMP_STOP;
120         }
121         ast_channel_unlock(target);
122
123         return 0;
124 }
125
126 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
127 static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
128 {
129         char *chkchan;
130         struct pickup_by_name_args pickup_args;
131
132         /* Check if channel name contains a '-'.
133          * In this case the channel name will be interpreted as full channel name.
134          */
135         if (strchr(channame, '-')) {
136                 /* check full channel name */
137                 pickup_args.len = strlen(channame);
138                 pickup_args.name = channame;
139         } else {
140                 /* need to append a '-' for the comparison so we check full channel name,
141                  * i.e SIP/hgc- , use a temporary variable so original stays the same for
142                  * debugging.
143                  */
144                 pickup_args.len = strlen(channame) + 1;
145                 chkchan = alloca(pickup_args.len + 1);
146                 strcpy(chkchan, channame);
147                 strcat(chkchan, "-");
148                 pickup_args.name = chkchan;
149         }
150
151         return ast_channel_callback(pickup_by_name_cb, NULL, &pickup_args, 0);
152 }
153
154 /*! \brief Attempt to pick up named channel, does not use context */
155 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
156 {
157         int res = -1;
158         struct ast_channel *target;/*!< Potential pickup target */
159
160         target = my_ast_get_channel_by_name_locked(pickup);
161         if (target) {
162                 /* Just check that we are not picking up the SAME as target. (i.e. ourself) */
163                 if (chan != target) {
164                         res = ast_do_pickup(chan, target);
165                 }
166                 ast_channel_unlock(target);
167                 target = ast_channel_unref(target);
168         }
169
170         return res;
171 }
172
173 /* Attempt to pick up specified extension with context */
174 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
175 {
176         struct ast_channel *target = NULL;/*!< Potential pickup target */
177         struct ast_channel_iterator *iter;
178         int res = -1;
179
180         if (!(iter = ast_channel_iterator_by_exten_new(exten, context))) {
181                 return -1;
182         }
183
184         while ((target = ast_channel_iterator_next(iter))) {
185                 ast_channel_lock(target);
186                 if ((chan != target) && ast_can_pickup(target)) {
187                         ast_log(LOG_NOTICE, "%s pickup by %s\n", target->name, chan->name);
188                         break;
189                 }
190                 ast_channel_unlock(target);
191                 target = ast_channel_unref(target);
192         }
193
194         ast_channel_iterator_destroy(iter);
195
196         if (target) {
197                 res = ast_do_pickup(chan, target);
198                 ast_channel_unlock(target);
199                 target = ast_channel_unref(target);
200         }
201
202         return res;
203 }
204
205 static int find_by_mark(void *obj, void *arg, void *data, int flags)
206 {
207         struct ast_channel *target = obj;/*!< Potential pickup target */
208         const char *mark = data;
209         const char *tmp;
210
211         ast_channel_lock(target);
212         tmp = pbx_builtin_getvar_helper(target, PICKUPMARK);
213         if (tmp && !strcasecmp(tmp, mark) && ast_can_pickup(target)) {
214                 /* Return with the channel still locked on purpose */
215                 return CMP_MATCH | CMP_STOP;
216         }
217         ast_channel_unlock(target);
218
219         return 0;
220 }
221
222 /* Attempt to pick up specified mark */
223 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
224 {
225         struct ast_channel *target;/*!< Potential pickup target */
226         int res = -1;
227
228         /* The found channel is already locked. */
229         target = ast_channel_callback(find_by_mark, NULL, (char *) mark, 0);
230         if (target) {
231                 res = ast_do_pickup(chan, target);
232                 ast_channel_unlock(target);
233                 target = ast_channel_unref(target);
234         }
235
236         return res;
237 }
238
239 static int find_channel_by_group(void *obj, void *arg, void *data, int flags)
240 {
241         struct ast_channel *target = obj;/*!< Potential pickup target */
242         struct ast_channel *chan = data;/*!< Channel wanting to pickup call */
243
244         ast_channel_lock(target);
245         if (chan != target && (chan->pickupgroup & target->callgroup)
246                 && ast_can_pickup(target)) {
247                 /* Return with the channel still locked on purpose */
248                 return CMP_MATCH | CMP_STOP;
249         }
250         ast_channel_unlock(target);
251
252         return 0;
253 }
254
255 static int pickup_by_group(struct ast_channel *chan)
256 {
257         struct ast_channel *target;/*!< Potential pickup target */
258         int res = -1;
259
260         /* The found channel is already locked. */
261         target = ast_channel_callback(find_channel_by_group, NULL, chan, 0);
262         if (target) {
263                 ast_log(LOG_NOTICE, "pickup %s attempt by %s\n", target->name, chan->name);
264                 res = ast_do_pickup(chan, target);
265                 ast_channel_unlock(target);
266                 target = ast_channel_unref(target);
267         }
268
269         return res;
270 }
271
272 /* application entry point for Pickup() */
273 static int pickup_exec(struct ast_channel *chan, const char *data)
274 {
275         char *tmp = ast_strdupa(data);
276         char *exten = NULL, *context = NULL;
277
278         if (ast_strlen_zero(data)) {
279                 return pickup_by_group(chan) ? 0 : -1;
280         }
281
282         /* Parse extension (and context if there) */
283         while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
284                 if ((context = strchr(exten, '@')))
285                         *context++ = '\0';
286                 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
287                         if (!pickup_by_mark(chan, exten)) {
288                                 /* Pickup successful.  Stop the dialplan this channel is a zombie. */
289                                 return -1;
290                         }
291                 } else {
292                         if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context)) {
293                                 /* Pickup successful.  Stop the dialplan this channel is a zombie. */
294                                 return -1;
295                         }
296                 }
297                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
298         }
299
300         /* Pickup failed.  Keep going in the dialplan. */
301         return 0;
302 }
303
304 /* Find channel for pick up specified by partial channel name */ 
305 static int find_by_part(void *obj, void *arg, void *data, int flags)
306 {
307         struct ast_channel *target = obj;/*!< Potential pickup target */
308         const char *part = data;
309         int len = strlen(part);
310
311         ast_channel_lock(target);
312         if (len <= strlen(target->name) && !strncmp(target->name, part, len)
313                 && ast_can_pickup(target)) {
314                 /* Return with the channel still locked on purpose */
315                 return CMP_MATCH | CMP_STOP;
316         }
317         ast_channel_unlock(target);
318
319         return 0;
320 }
321
322 /* Attempt to pick up specified by partial channel name */ 
323 static int pickup_by_part(struct ast_channel *chan, const char *part)
324 {
325         struct ast_channel *target;/*!< Potential pickup target */
326         int res = -1;
327
328         /* The found channel is already locked. */
329         target = ast_channel_callback(find_by_part, NULL, (char *) part, 0);
330         if (target) {
331                 res = ast_do_pickup(chan, target);
332                 ast_channel_unlock(target);
333                 target = ast_channel_unref(target);
334         }
335
336         return res;
337 }
338
339 /* application entry point for PickupChan() */
340 static int pickupchan_exec(struct ast_channel *chan, const char *data)
341 {
342         int partial_pickup = 0;
343         char *pickup = NULL;
344         char *parse = ast_strdupa(data);
345         AST_DECLARE_APP_ARGS(args,
346                 AST_APP_ARG(channel);
347                 AST_APP_ARG(options);
348         );
349         AST_STANDARD_APP_ARGS(args, parse);
350
351         if (ast_strlen_zero(args.channel)) {
352                 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
353                 /* Pickup failed.  Keep going in the dialplan. */
354                 return 0;
355         }
356
357         if (!ast_strlen_zero(args.options) && strchr(args.options, 'p')) {
358                 partial_pickup = 1;
359         }
360
361         /* Parse channel */
362         while (!ast_strlen_zero(args.channel) && (pickup = strsep(&args.channel, "&"))) {
363                 if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
364                         ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
365                 } else {
366                         if (partial_pickup) {
367                                 if (!pickup_by_part(chan, pickup)) {
368                                         /* Pickup successful.  Stop the dialplan this channel is a zombie. */
369                                         return -1;
370                                 }
371                         } else if (!pickup_by_channel(chan, pickup)) {
372                                 /* Pickup successful.  Stop the dialplan this channel is a zombie. */
373                                 return -1;
374                         }
375                         ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
376                 }
377         }
378
379         /* Pickup failed.  Keep going in the dialplan. */
380         return 0;
381 }
382
383 static int unload_module(void)
384 {
385         int res;
386
387         res = ast_unregister_application(app);
388         res |= ast_unregister_application(app2);
389
390         return res;
391 }
392
393 static int load_module(void)
394 {
395         int res;
396
397         res = ast_register_application_xml(app, pickup_exec);
398         res |= ast_register_application_xml(app2, pickupchan_exec);
399
400         return res;
401 }
402
403 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");