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