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