adds 'p' option to PickupChan
[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 /* Perform actual pickup between two channels */
101 static int pickup_do(struct ast_channel *chan, struct ast_channel *target)
102 {
103         int res = 0;
104         struct ast_party_connected_line connected_caller;
105         struct ast_channel *chans[2] = { chan, target };
106
107         ast_debug(1, "Call pickup on '%s' by '%s'\n", target->name, chan->name);
108         ast_cel_report_event(target, AST_CEL_PICKUP, NULL, NULL, chan);
109
110         ast_party_connected_line_init(&connected_caller);
111         ast_party_connected_line_copy(&connected_caller, &target->connected);
112         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
113         if (ast_channel_connected_line_macro(NULL, chan, &connected_caller, 0, 0)) {
114                 ast_channel_update_connected_line(chan, &connected_caller);
115         }
116         ast_party_connected_line_free(&connected_caller);
117
118         ast_channel_lock(chan);
119         ast_connected_line_copy_from_caller(&connected_caller, &chan->cid);
120         ast_channel_unlock(chan);
121         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
122         ast_channel_queue_connected_line_update(chan, &connected_caller);
123         ast_party_connected_line_free(&connected_caller);
124
125         if ((res = ast_answer(chan))) {
126                 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
127                 return -1;
128         }
129
130         if ((res = ast_queue_control(chan, AST_CONTROL_ANSWER))) {
131                 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
132                 return -1;
133         }
134
135         if ((res = ast_channel_masquerade(target, chan))) {
136                 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
137                 return -1;
138         }
139
140         /* If you want UniqueIDs, set channelvars in manager.conf to CHANNEL(uniqueid) */
141         ast_manager_event_multichan(EVENT_FLAG_CALL, "Pickup", 2, chans,
142                 "Channel: %s\r\nTargetChannel: %s\r\n", chan->name, target->name);
143
144         return res;
145 }
146
147 /* Helper function that determines whether a channel is capable of being picked up */
148 static int can_pickup(struct ast_channel *chan)
149 {
150         if (!chan->pbx && (chan->_state == AST_STATE_RINGING || chan->_state == AST_STATE_RING || chan->_state == AST_STATE_DOWN))
151                 return 1;
152         else
153                 return 0;
154 }
155
156 struct pickup_by_name_args {
157         const char *name;
158         size_t len;
159 };
160
161 static int pickup_by_name_cb(void *obj, void *arg, void *data, int flags)
162 {
163         struct ast_channel *chan = obj;
164         struct pickup_by_name_args *args = data;
165
166         ast_channel_lock(chan);
167         if (!strncasecmp(chan->name, args->name, args->len) && can_pickup(chan)) {
168                 /* Return with the channel still locked on purpose */
169                 return CMP_MATCH | CMP_STOP;
170         }
171         ast_channel_unlock(chan);
172
173         return 0;
174 }
175
176 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
177 static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
178 {
179         char *chkchan;
180         struct pickup_by_name_args pickup_args;
181
182         pickup_args.len = strlen(channame) + 2;
183
184         chkchan = alloca(pickup_args.len);
185
186         /* need to append a '-' for the comparison so we check full channel name,
187          * i.e SIP/hgc- , use a temporary variable so original stays the same for
188          * debugging.
189          */
190         strcpy(chkchan, channame);
191         strcat(chkchan, "-");
192
193         pickup_args.name = chkchan;
194
195         return ast_channel_callback(pickup_by_name_cb, NULL, &pickup_args, 0);
196 }
197
198 /*! \brief Attempt to pick up specified channel named , does not use context */
199 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
200 {
201         int res = 0;
202         struct ast_channel *target;
203
204         if (!(target = my_ast_get_channel_by_name_locked(pickup))) {
205                 return -1;
206         }
207
208         /* Just check that we are not picking up the SAME as target */
209         if (chan != target) {
210                 res = pickup_do(chan, target);
211         }
212
213         ast_channel_unlock(target);
214         target = ast_channel_unref(target);
215
216         return res;
217 }
218
219 /* Attempt to pick up specified extension with context */
220 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
221 {
222         struct ast_channel *target = NULL;
223         struct ast_channel_iterator *iter;
224         int res = -1;
225
226         if (!(iter = ast_channel_iterator_by_exten_new(exten, context))) {
227                 return -1;
228         }
229
230         while ((target = ast_channel_iterator_next(iter))) {
231                 ast_channel_lock(target);
232                 if ((chan != target) && can_pickup(target)) {
233                         break;
234                 }
235                 ast_channel_unlock(target);
236                 target = ast_channel_unref(target);
237         }
238
239         ast_channel_iterator_destroy(iter);
240
241         if (target) {
242                 res = pickup_do(chan, target);
243                 ast_channel_unlock(target);
244                 target = ast_channel_unref(target);
245         }
246
247         return res;
248 }
249
250 static int find_by_mark(void *obj, void *arg, void *data, int flags)
251 {
252         struct ast_channel *c = obj;
253         const char *mark = data;
254         const char *tmp;
255         int res;
256
257         ast_channel_lock(c);
258
259         res = (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
260                 !strcasecmp(tmp, mark) &&
261                 can_pickup(c);
262
263         ast_channel_unlock(c);
264
265         return res ? CMP_MATCH | CMP_STOP : 0;
266 }
267
268 /* Attempt to pick up specified mark */
269 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
270 {
271         struct ast_channel *target;
272         int res = -1;
273
274         if ((target = ast_channel_callback(find_by_mark, NULL, (char *) mark, 0))) {
275                 ast_channel_lock(target);
276                 res = pickup_do(chan, target);
277                 ast_channel_unlock(target);
278                 target = ast_channel_unref(target);
279         }
280
281         return res;
282 }
283
284 /* application entry point for Pickup() */
285 static int pickup_exec(struct ast_channel *chan, const char *data)
286 {
287         int res = 0;
288         char *tmp = ast_strdupa(data);
289         char *exten = NULL, *context = NULL;
290
291         if (ast_strlen_zero(data)) {
292                 res = ast_pickup_call(chan);
293                 return res;
294         }
295         
296         /* Parse extension (and context if there) */
297         while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
298                 if ((context = strchr(exten, '@')))
299                         *context++ = '\0';
300                 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
301                         if (!pickup_by_mark(chan, exten))
302                                 break;
303                 } else {
304                         if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context))
305                                 break;
306                 }
307                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
308         }
309
310         return res;
311 }
312
313 /* Find channel for pick up specified by partial channel name */ 
314 static int find_by_part(void *obj, void *arg, void *data, int flags)
315 {
316         struct ast_channel *c = obj; 
317         const char *part = data;
318         int res = 0;
319         int len = strlen(part);
320
321         ast_channel_lock(c);
322         if (len <= strlen(c->name)) {
323                 res = !(strncmp(c->name, part, len)) && (can_pickup(c));
324         }
325         ast_channel_unlock(c);
326
327         return res ? CMP_MATCH | CMP_STOP : 0;
328 }
329
330 /* Attempt to pick up specified by partial channel name */ 
331 static int pickup_by_part(struct ast_channel *chan, const char *part)
332 {
333         struct ast_channel *target;
334         int res = -1;
335
336         if ((target = ast_channel_callback(find_by_part, NULL, (char *) part, 0))) {
337                 ast_channel_lock(target);
338                 res = pickup_do(chan, target);
339                 ast_channel_unlock(target);
340                 target = ast_channel_unref(target);
341         }
342
343         return res;
344 }
345
346 /* application entry point for PickupChan() */
347 static int pickupchan_exec(struct ast_channel *chan, const char *data)
348 {
349         int res = 0;
350         int partial_pickup = 0;
351         char *pickup = NULL;
352         char *parse = ast_strdupa(data);
353         AST_DECLARE_APP_ARGS(args,
354                 AST_APP_ARG(channel);
355                 AST_APP_ARG(options);
356         );
357         AST_STANDARD_APP_ARGS(args, parse);
358
359         if (ast_strlen_zero(args.channel)) {
360                 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
361                 return -1;
362         }
363
364         if (!ast_strlen_zero(args.options) && strchr(args.options, 'p')) {
365                 partial_pickup = 1;
366         }
367
368         /* Parse channel */
369         while (!ast_strlen_zero(args.channel) && (pickup = strsep(&args.channel, "&"))) {
370                 if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
371                         ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
372                 } else {
373                         if (partial_pickup) {
374                                 if (!pickup_by_part(chan, pickup)) {
375                                         break;
376                                 }
377                         } else if (!pickup_by_channel(chan, pickup)) {
378                                 break;
379                         }
380                         ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
381                 }
382         }
383
384         return res;
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");