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