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