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