app_queue: Cleanup queue_ref / queue_unref routines.
[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 #include "asterisk/file.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/lock.h"
43 #include "asterisk/app.h"
44 #include "asterisk/pickup.h"
45 #include "asterisk/manager.h"
46 #include "asterisk/callerid.h"
47
48 #define PICKUPMARK "PICKUPMARK"
49
50 /*** DOCUMENTATION
51         <application name="Pickup" language="en_US">
52                 <synopsis>
53                         Directed extension call pickup.
54                 </synopsis>
55                 <syntax>
56                         <parameter name="targets" argsep="&amp;">
57                                 <argument name="extension" argsep="@" required="true">
58                                         <para>Specification of the pickup target.</para>
59                                         <argument name="extension" required="true"/>
60                                         <argument name="context" />
61                                 </argument>
62                                 <argument name="extension2" argsep="@" multiple="true">
63                                         <para>Additional specifications of pickup targets.</para>
64                                         <argument name="extension2" required="true"/>
65                                         <argument name="context2"/>
66                                 </argument>
67                         </parameter>
68                 </syntax>
69                 <description>
70                         <para>This application can pickup a specified ringing channel.  The channel
71                         to pickup can be specified in the following ways.</para>
72                         <para>1) If no <replaceable>extension</replaceable> targets are specified,
73                         the application will pickup a channel matching the pickup group of the
74                         requesting channel.</para>
75                         <para>2) If the <replaceable>extension</replaceable> is specified with a
76                         <replaceable>context</replaceable> of the special string
77                         <literal>PICKUPMARK</literal> (for example 10@PICKUPMARK), the application
78                         will pickup a channel which has defined the channel variable
79                         <variable>PICKUPMARK</variable> with the same value as
80                         <replaceable>extension</replaceable> (in this example,
81                         <literal>10</literal>).</para>
82                         <para>3) If the <replaceable>extension</replaceable> is specified
83                         with or without a <replaceable>context</replaceable>, the channel with a
84                         matching <replaceable>extension</replaceable> and <replaceable>context</replaceable>
85                         will be picked up.  If no <replaceable>context</replaceable> is specified,
86                         the current context will be used.</para>
87                         <note><para>The <replaceable>extension</replaceable> is typically set on
88                         matching channels by the dial application that created the channel.  The
89                         <replaceable>context</replaceable> is set on matching channels by the
90                         channel driver for the device.</para></note>
91                 </description>
92         </application>
93         <application name="PickupChan" language="en_US">
94                 <synopsis>
95                         Pickup a ringing channel.
96                 </synopsis>
97                 <syntax >
98                         <parameter name="channel" argsep="&amp;" required="true">
99                                 <argument name="channel" required="true" />
100                                 <argument name="channel2" required="false" multiple="true" />
101                                 <para>List of channel names or channel uniqueids to pickup if ringing.
102                                         For example, a channel name could be <literal>SIP/bob</literal> or
103                                         <literal>SIP/bob-00000000</literal> to find
104                                         <literal>SIP/bob-00000000</literal>.
105                                 </para>
106                         </parameter>
107                         <parameter name="options" required="false">
108                                 <optionlist>
109                                         <option name="p">
110                                                 <para>Supplied channel names are prefixes.  For example,
111                                                         <literal>SIP/bob</literal> will match
112                                                         <literal>SIP/bob-00000000</literal> and
113                                                         <literal>SIP/bobby-00000000</literal>.
114                                                 </para>
115                                         </option>
116                                 </optionlist>
117                         </parameter>
118                 </syntax>
119                 <description>
120                         <para>Pickup a specified <replaceable>channel</replaceable> if ringing.</para>
121                 </description>
122         </application>
123  ***/
124
125 static const char app[] = "Pickup";
126 static const char app2[] = "PickupChan";
127
128 struct pickup_by_name_args {
129         /*! Channel attempting to pickup a call. */
130         struct ast_channel *chan;
131         /*! Channel uniqueid or partial channel name to match. */
132         const char *name;
133         /*! Length of partial channel name to match. */
134         size_t len;
135 };
136
137 static int find_by_name(void *obj, void *arg, void *data, int flags)
138 {
139         struct ast_channel *target = obj;/*!< Potential pickup target */
140         struct pickup_by_name_args *args = data;
141
142         if (args->chan == target) {
143                 /* The channel attempting to pickup a call cannot pickup itself. */
144                 return 0;
145         }
146
147         ast_channel_lock(target);
148         if (!strncasecmp(ast_channel_name(target), args->name, args->len)
149                 && ast_can_pickup(target)) {
150                 /* Return with the channel still locked on purpose */
151                 return CMP_MATCH | CMP_STOP;
152         }
153         ast_channel_unlock(target);
154
155         return 0;
156 }
157
158 static int find_by_uniqueid(void *obj, void *arg, void *data, int flags)
159 {
160         struct ast_channel *target = obj;/*!< Potential pickup target */
161         struct pickup_by_name_args *args = data;
162
163         if (args->chan == target) {
164                 /* The channel attempting to pickup a call cannot pickup itself. */
165                 return 0;
166         }
167
168         ast_channel_lock(target);
169         if (!strcasecmp(ast_channel_uniqueid(target), args->name)
170                 && ast_can_pickup(target)) {
171                 /* Return with the channel still locked on purpose */
172                 return CMP_MATCH | CMP_STOP;
173         }
174         ast_channel_unlock(target);
175
176         return 0;
177 }
178
179 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
180 static struct ast_channel *find_by_channel(struct ast_channel *chan, const char *channame)
181 {
182         struct ast_channel *target;
183         char *chkchan;
184         struct pickup_by_name_args pickup_args;
185
186         pickup_args.chan = chan;
187
188         if (strchr(channame, '-')) {
189                 /*
190                  * Use the given channel name string as-is.  This allows a full channel
191                  * name with a typical sequence number to be used as well as still
192                  * allowing the odd partial channel name that has a '-' in it to still
193                  * work, i.e. Local/bob@en-phone.
194                  */
195                 pickup_args.len = strlen(channame);
196                 pickup_args.name = channame;
197         } else {
198                 /*
199                  * Append a '-' for the comparison so we check the channel name less
200                  * a sequence number, i.e Find SIP/bob- and not SIP/bobby.
201                  */
202                 pickup_args.len = strlen(channame) + 1;
203                 chkchan = ast_alloca(pickup_args.len + 1);
204                 strcpy(chkchan, channame);/* Safe */
205                 strcat(chkchan, "-");
206                 pickup_args.name = chkchan;
207         }
208         target = ast_channel_callback(find_by_name, NULL, &pickup_args, 0);
209         if (target) {
210                 return target;
211         }
212
213         /* Now try a search for uniqueid. */
214         pickup_args.name = channame;
215         pickup_args.len = 0;
216         return ast_channel_callback(find_by_uniqueid, NULL, &pickup_args, 0);
217 }
218
219 /*! \brief Attempt to pick up named channel. */
220 static int pickup_by_channel(struct ast_channel *chan, const char *name)
221 {
222         int res = -1;
223         struct ast_channel *target;/*!< Potential pickup target */
224
225         /* The found channel is already locked. */
226         target = find_by_channel(chan, name);
227         if (target) {
228                 res = ast_do_pickup(chan, target);
229                 ast_channel_unlock(target);
230                 target = ast_channel_unref(target);
231         }
232
233         return res;
234 }
235
236 /* Attempt to pick up specified extension with context */
237 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
238 {
239         struct ast_channel *target = NULL;/*!< Potential pickup target */
240         struct ast_channel_iterator *iter;
241         int res = -1;
242
243         if (!(iter = ast_channel_iterator_by_exten_new(exten, context))) {
244                 return -1;
245         }
246
247         while ((target = ast_channel_iterator_next(iter))) {
248                 ast_channel_lock(target);
249                 if ((chan != target) && ast_can_pickup(target)) {
250                         ast_log(LOG_NOTICE, "%s pickup by %s\n", ast_channel_name(target), ast_channel_name(chan));
251                         break;
252                 }
253                 ast_channel_unlock(target);
254                 target = ast_channel_unref(target);
255         }
256
257         ast_channel_iterator_destroy(iter);
258
259         if (target) {
260                 res = ast_do_pickup(chan, target);
261                 ast_channel_unlock(target);
262                 target = ast_channel_unref(target);
263         }
264
265         return res;
266 }
267
268 static int find_by_mark(void *obj, void *arg, void *data, int flags)
269 {
270         struct ast_channel *target = obj;/*!< Potential pickup target */
271         struct ast_channel *chan = arg;
272         const char *mark = data;
273         const char *tmp;
274
275         if (chan == target) {
276                 /* The channel attempting to pickup a call cannot pickup itself. */
277                 return 0;
278         }
279
280         ast_channel_lock(target);
281         tmp = pbx_builtin_getvar_helper(target, PICKUPMARK);
282         if (tmp && !strcasecmp(tmp, mark) && ast_can_pickup(target)) {
283                 /* Return with the channel still locked on purpose */
284                 return CMP_MATCH | CMP_STOP;
285         }
286         ast_channel_unlock(target);
287
288         return 0;
289 }
290
291 /* Attempt to pick up specified mark */
292 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
293 {
294         struct ast_channel *target;/*!< Potential pickup target */
295         int res = -1;
296
297         /* The found channel is already locked. */
298         target = ast_channel_callback(find_by_mark, chan, (char *) mark, 0);
299         if (target) {
300                 res = ast_do_pickup(chan, target);
301                 ast_channel_unlock(target);
302                 target = ast_channel_unref(target);
303         }
304
305         return res;
306 }
307
308 static int pickup_by_group(struct ast_channel *chan)
309 {
310         struct ast_channel *target;/*!< Potential pickup target */
311         int res = -1;
312
313         /* The found channel is already locked. */
314         target = ast_pickup_find_by_group(chan);
315         if (target) {
316                 ast_log(LOG_NOTICE, "pickup %s attempt by %s\n", ast_channel_name(target), ast_channel_name(chan));
317                 res = ast_do_pickup(chan, target);
318                 ast_channel_unlock(target);
319                 target = ast_channel_unref(target);
320         }
321
322         return res;
323 }
324
325 /* application entry point for Pickup() */
326 static int pickup_exec(struct ast_channel *chan, const char *data)
327 {
328         char *parse;
329         char *exten;
330         char *context;
331
332         if (ast_strlen_zero(data)) {
333                 return pickup_by_group(chan) ? 0 : -1;
334         }
335
336         /* Parse extension (and context if there) */
337         parse = ast_strdupa(data);
338         for (;;) {
339                 if (ast_strlen_zero(parse)) {
340                         break;
341                 }
342                 exten = strsep(&parse, "&");
343                 if (ast_strlen_zero(exten)) {
344                         continue;
345                 }
346
347                 context = strchr(exten, '@');
348                 if (context) {
349                         *context++ = '\0';
350                 }
351                 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
352                         if (!pickup_by_mark(chan, exten)) {
353                                 /* Pickup successful.  Stop the dialplan this channel is a zombie. */
354                                 return -1;
355                         }
356                 } else {
357                         if (ast_strlen_zero(context)) {
358                                 context = (char *) ast_channel_context(chan);
359                         }
360                         if (!pickup_by_exten(chan, exten, context)) {
361                                 /* Pickup successful.  Stop the dialplan this channel is a zombie. */
362                                 return -1;
363                         }
364                 }
365                 ast_log(LOG_NOTICE, "No target channel found for %s@%s.\n", exten, context);
366         }
367
368         /* Pickup failed.  Keep going in the dialplan. */
369         return 0;
370 }
371
372 /* Find channel for pick up specified by partial channel name */
373 static struct ast_channel *find_by_part(struct ast_channel *chan, const char *part)
374 {
375         struct ast_channel *target;
376         struct pickup_by_name_args pickup_args;
377
378         pickup_args.chan = chan;
379
380         /* Try a partial channel name search. */
381         pickup_args.name = part;
382         pickup_args.len = strlen(part);
383         target = ast_channel_callback(find_by_name, NULL, &pickup_args, 0);
384         if (target) {
385                 return target;
386         }
387
388         /* Now try a search for uniqueid. */
389         pickup_args.name = part;
390         pickup_args.len = 0;
391         return ast_channel_callback(find_by_uniqueid, NULL, &pickup_args, 0);
392 }
393
394 /* Attempt to pick up specified by partial channel name */
395 static int pickup_by_part(struct ast_channel *chan, const char *part)
396 {
397         struct ast_channel *target;/*!< Potential pickup target */
398         int res = -1;
399
400         /* The found channel is already locked. */
401         target = find_by_part(chan, part);
402         if (target) {
403                 res = ast_do_pickup(chan, target);
404                 ast_channel_unlock(target);
405                 target = ast_channel_unref(target);
406         }
407
408         return res;
409 }
410
411 enum OPT_PICKUPCHAN_FLAGS {
412         OPT_PICKUPCHAN_PARTIAL =   (1 << 0),    /* Channel name is a partial name. */
413 };
414
415 AST_APP_OPTIONS(pickupchan_opts, BEGIN_OPTIONS
416         AST_APP_OPTION('p', OPT_PICKUPCHAN_PARTIAL),
417 END_OPTIONS);
418
419 /* application entry point for PickupChan() */
420 static int pickupchan_exec(struct ast_channel *chan, const char *data)
421 {
422         char *pickup = NULL;
423         char *parse = ast_strdupa(data);
424         AST_DECLARE_APP_ARGS(args,
425                 AST_APP_ARG(channel);
426                 AST_APP_ARG(options);
427                 AST_APP_ARG(other);     /* Any remining unused arguments */
428         );
429         struct ast_flags opts;
430
431         AST_STANDARD_APP_ARGS(args, parse);
432
433         if (ast_strlen_zero(args.channel)) {
434                 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
435                 /* Pickup failed.  Keep going in the dialplan. */
436                 return 0;
437         }
438         if (ast_app_parse_options(pickupchan_opts, &opts, NULL, args.options)) {
439                 /*
440                  * General invalid option syntax.
441                  * Pickup failed.  Keep going in the dialplan.
442                  */
443                 return 0;
444         }
445
446         /* Parse channel */
447         for (;;) {
448                 if (ast_strlen_zero(args.channel)) {
449                         break;
450                 }
451                 pickup = strsep(&args.channel, "&");
452                 if (ast_strlen_zero(pickup)) {
453                         continue;
454                 }
455
456                 if (ast_test_flag(&opts, OPT_PICKUPCHAN_PARTIAL)) {
457                         if (!pickup_by_part(chan, pickup)) {
458                                 /* Pickup successful.  Stop the dialplan this channel is a zombie. */
459                                 return -1;
460                         }
461                 } else if (!pickup_by_channel(chan, pickup)) {
462                         /* Pickup successful.  Stop the dialplan this channel is a zombie. */
463                         return -1;
464                 }
465                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
466         }
467
468         /* Pickup failed.  Keep going in the dialplan. */
469         return 0;
470 }
471
472 static int unload_module(void)
473 {
474         int res;
475
476         res = ast_unregister_application(app);
477         res |= ast_unregister_application(app2);
478
479         return res;
480 }
481
482 static int load_module(void)
483 {
484         int res;
485
486         res = ast_register_application_xml(app, pickup_exec);
487         res |= ast_register_application_xml(app2, pickupchan_exec);
488
489         return res;
490 }
491
492 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");