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