a43d4251a2bbb5078bcf7c60452ed74a67c2f143
[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/callerid.h"
44 #include "asterisk/cel.h"
45
46 #define PICKUPMARK "PICKUPMARK"
47
48 /*** DOCUMENTATION
49         <application name="Pickup" language="en_US">
50                 <synopsis>
51                         Directed extension call pickup.
52                 </synopsis>
53                 <syntax argsep="&amp;">
54                         <parameter name="ext" argsep="@" required="true">
55                                 <argument name="extension" required="true"/>
56                                 <argument name="context" />
57                         </parameter>
58                         <parameter name="ext2" argsep="@" multiple="true">
59                                 <argument name="extension2" required="true"/>
60                                 <argument name="context2"/>
61                         </parameter>
62                 </syntax>
63                 <description>
64                         <para>This application can pickup any ringing channel that is calling
65                         the specified <replaceable>extension</replaceable>. If no <replaceable>context</replaceable>
66                         is specified, the current context will be used. If you use the special string <literal>PICKUPMARK</literal>
67                         for the context parameter, for example 10@PICKUPMARK, this application
68                         tries to find a channel which has defined a <variable>PICKUPMARK</variable>
69                         channel variable with the same value as <replaceable>extension</replaceable>
70                         (in this example, <literal>10</literal>). When no parameter is specified, the application
71                         will pickup a channel matching the pickup group of the active channel.</para>
72                 </description>
73         </application>
74         <application name="PickupChan" language="en_US">
75                 <synopsis>
76                         Pickup a ringing channel.
77                 </synopsis>
78                 <syntax>
79                         <parameter name="channel" required="true" />
80                         <parameter name="channel2" multiple="true" />
81                 </syntax>
82                 <description>
83                         <para>This will pickup a specified <replaceable>channel</replaceable> if ringing.</para>
84                 </description>
85         </application>
86  ***/
87
88 static const char app[] = "Pickup";
89 static const char app2[] = "PickupChan";
90 /*! \todo This application should return a result code, like PICKUPRESULT */
91
92 /* Perform actual pickup between two channels */
93 static int pickup_do(struct ast_channel *chan, struct ast_channel *target)
94 {
95         int res = 0;
96         struct ast_party_connected_line connected_caller;
97
98         ast_debug(1, "Call pickup on '%s' by '%s'\n", target->name, chan->name);
99         ast_cel_report_event(target, AST_CEL_PICKUP, NULL, NULL, chan);
100
101         ast_party_connected_line_init(&connected_caller);
102         connected_caller = target->connected;
103         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
104         if (ast_channel_connected_line_macro(NULL, chan, &connected_caller, 0, 0)) {
105                 ast_channel_update_connected_line(chan, &connected_caller);
106         }
107
108         ast_channel_lock(chan);
109         ast_connected_line_copy_from_caller(&connected_caller, &chan->cid);
110         ast_channel_unlock(chan);
111         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
112         ast_channel_queue_connected_line_update(chan, &connected_caller);
113         ast_party_connected_line_free(&connected_caller);
114
115         if ((res = ast_answer(chan))) {
116                 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
117                 return -1;
118         }
119
120         if ((res = ast_queue_control(chan, AST_CONTROL_ANSWER))) {
121                 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
122                 return -1;
123         }
124
125         if ((res = ast_channel_masquerade(target, chan))) {
126                 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
127                 return -1;
128         }
129
130         return res;
131 }
132
133 /* Helper function that determines whether a channel is capable of being picked up */
134 static int can_pickup(struct ast_channel *chan)
135 {
136         if (!chan->pbx && (chan->_state == AST_STATE_RINGING || chan->_state == AST_STATE_RING || chan->_state == AST_STATE_DOWN))
137                 return 1;
138         else
139                 return 0;
140 }
141
142 struct pickup_by_name_args {
143         const char *name;
144         size_t len;
145 };
146
147 static int pickup_by_name_cb(void *obj, void *arg, void *data, int flags)
148 {
149         struct ast_channel *chan = obj;
150         struct pickup_by_name_args *args = data;
151
152         ast_channel_lock(chan);
153         if (!strncasecmp(chan->name, args->name, args->len) && can_pickup(chan)) {
154                 /* Return with the channel still locked on purpose */
155                 return CMP_MATCH | CMP_STOP;
156         }
157         ast_channel_unlock(chan);
158
159         return 0;
160 }
161
162 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
163 static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
164 {
165         char *chkchan;
166         struct pickup_by_name_args pickup_args;
167
168         pickup_args.len = strlen(channame) + 2;
169
170         chkchan = alloca(pickup_args.len);
171
172         /* need to append a '-' for the comparison so we check full channel name,
173          * i.e SIP/hgc- , use a temporary variable so original stays the same for
174          * debugging.
175          */
176         strcpy(chkchan, channame);
177         strcat(chkchan, "-");
178
179         pickup_args.name = chkchan;
180
181         return ast_channel_callback(pickup_by_name_cb, NULL, &pickup_args, 0);
182 }
183
184 /*! \brief Attempt to pick up specified channel named , does not use context */
185 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
186 {
187         int res = 0;
188         struct ast_channel *target;
189
190         if (!(target = my_ast_get_channel_by_name_locked(pickup))) {
191                 return -1;
192         }
193
194         /* Just check that we are not picking up the SAME as target */
195         if (chan != target) {
196                 res = pickup_do(chan, target);
197         }
198
199         ast_channel_unlock(target);
200         target = ast_channel_unref(target);
201
202         return res;
203 }
204
205 /* Attempt to pick up specified extension with context */
206 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
207 {
208         struct ast_channel *target = NULL;
209         struct ast_channel_iterator *iter;
210         int res = -1;
211
212         if (!(iter = ast_channel_iterator_by_exten_new(0, exten, context))) {
213                 return -1;
214         }
215
216         while ((target = ast_channel_iterator_next(iter))) {
217                 ast_channel_lock(target);
218                 if (can_pickup(target)) {
219                         break;
220                 }
221                 ast_channel_unlock(target);
222                 target = ast_channel_unref(target);
223         }
224
225         if (target) {
226                 res = pickup_do(chan, target);
227                 ast_channel_unlock(target);
228                 target = ast_channel_unref(target);
229         }
230
231         return res;
232 }
233
234 static int find_by_mark(void *obj, void *arg, void *data, int flags)
235 {
236         struct ast_channel *c = obj;
237         const char *mark = data;
238         const char *tmp;
239         int res;
240
241         ast_channel_lock(c);
242
243         res = (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
244                 !strcasecmp(tmp, mark) &&
245                 can_pickup(c);
246
247         ast_channel_unlock(c);
248
249         return res ? CMP_MATCH | CMP_STOP : 0;
250 }
251
252 /* Attempt to pick up specified mark */
253 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
254 {
255         struct ast_channel *target;
256         int res = -1;
257
258         if ((target = ast_channel_callback(find_by_mark, NULL, (char *) mark, 0))) {
259                 ast_channel_lock(target);
260                 res = pickup_do(chan, target);
261                 ast_channel_unlock(target);
262                 target = ast_channel_unref(target);
263         }
264
265         return res;
266 }
267
268 /* application entry point for Pickup() */
269 static int pickup_exec(struct ast_channel *chan, const char *data)
270 {
271         int res = 0;
272         char *tmp = ast_strdupa(data);
273         char *exten = NULL, *context = NULL;
274
275         if (ast_strlen_zero(data)) {
276                 res = ast_pickup_call(chan);
277                 return res;
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                                 break;
287                 } else {
288                         if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context))
289                                 break;
290                 }
291                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
292         }
293
294         return res;
295 }
296
297 /* application entry point for PickupChan() */
298 static int pickupchan_exec(struct ast_channel *chan, const char *data)
299 {
300         int res = 0;
301         char *tmp = ast_strdupa(data);
302         char *pickup = NULL;
303
304         if (ast_strlen_zero(data)) {
305                 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
306                 return -1;      
307         }
308
309         /* Parse channel */
310         while (!ast_strlen_zero(tmp) && (pickup = strsep(&tmp, "&"))) {
311                 if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
312                         ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
313                 } else {
314                         if (!pickup_by_channel(chan, pickup)) {
315                                 break;
316                         }
317                         ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
318                 }
319         }
320
321         return res;
322 }
323
324 static int unload_module(void)
325 {
326         int res;
327
328         res = ast_unregister_application(app);
329         res |= ast_unregister_application(app2);
330
331         return res;
332 }
333
334 static int load_module(void)
335 {
336         int res;
337
338         res = ast_register_application_xml(app, pickup_exec);
339         res |= ast_register_application_xml(app2, pickupchan_exec);
340
341         return res;
342 }
343
344 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");