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