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