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