bc25defe2dfdf2cb671a2da53695c1f2fa80e7c2
[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         ast_party_connected_line_copy(&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         ast_party_connected_line_free(&connected_caller);
108
109         ast_channel_lock(chan);
110         ast_connected_line_copy_from_caller(&connected_caller, &chan->cid);
111         ast_channel_unlock(chan);
112         connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
113         ast_channel_queue_connected_line_update(chan, &connected_caller);
114         ast_party_connected_line_free(&connected_caller);
115
116         if ((res = ast_answer(chan))) {
117                 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
118                 return -1;
119         }
120
121         if ((res = ast_queue_control(chan, AST_CONTROL_ANSWER))) {
122                 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
123                 return -1;
124         }
125
126         if ((res = ast_channel_masquerade(target, chan))) {
127                 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
128                 return -1;
129         }
130
131         return res;
132 }
133
134 /* Helper function that determines whether a channel is capable of being picked up */
135 static int can_pickup(struct ast_channel *chan)
136 {
137         if (!chan->pbx && (chan->_state == AST_STATE_RINGING || chan->_state == AST_STATE_RING || chan->_state == AST_STATE_DOWN))
138                 return 1;
139         else
140                 return 0;
141 }
142
143 struct pickup_by_name_args {
144         const char *name;
145         size_t len;
146 };
147
148 static int pickup_by_name_cb(void *obj, void *arg, void *data, int flags)
149 {
150         struct ast_channel *chan = obj;
151         struct pickup_by_name_args *args = data;
152
153         ast_channel_lock(chan);
154         if (!strncasecmp(chan->name, args->name, args->len) && can_pickup(chan)) {
155                 /* Return with the channel still locked on purpose */
156                 return CMP_MATCH | CMP_STOP;
157         }
158         ast_channel_unlock(chan);
159
160         return 0;
161 }
162
163 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
164 static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
165 {
166         char *chkchan;
167         struct pickup_by_name_args pickup_args;
168
169         pickup_args.len = strlen(channame) + 2;
170
171         chkchan = alloca(pickup_args.len);
172
173         /* need to append a '-' for the comparison so we check full channel name,
174          * i.e SIP/hgc- , use a temporary variable so original stays the same for
175          * debugging.
176          */
177         strcpy(chkchan, channame);
178         strcat(chkchan, "-");
179
180         pickup_args.name = chkchan;
181
182         return ast_channel_callback(pickup_by_name_cb, NULL, &pickup_args, 0);
183 }
184
185 /*! \brief Attempt to pick up specified channel named , does not use context */
186 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
187 {
188         int res = 0;
189         struct ast_channel *target;
190
191         if (!(target = my_ast_get_channel_by_name_locked(pickup))) {
192                 return -1;
193         }
194
195         /* Just check that we are not picking up the SAME as target */
196         if (chan != target) {
197                 res = pickup_do(chan, target);
198         }
199
200         ast_channel_unlock(target);
201         target = ast_channel_unref(target);
202
203         return res;
204 }
205
206 /* Attempt to pick up specified extension with context */
207 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
208 {
209         struct ast_channel *target = NULL;
210         struct ast_channel_iterator *iter;
211         int res = -1;
212
213         if (!(iter = ast_channel_iterator_by_exten_new(exten, context))) {
214                 return -1;
215         }
216
217         while ((target = ast_channel_iterator_next(iter))) {
218                 ast_channel_lock(target);
219                 if ((chan != target) && can_pickup(target)) {
220                         break;
221                 }
222                 ast_channel_unlock(target);
223                 target = ast_channel_unref(target);
224         }
225
226         ast_channel_iterator_destroy(iter);
227
228         if (target) {
229                 res = pickup_do(chan, target);
230                 ast_channel_unlock(target);
231                 target = ast_channel_unref(target);
232         }
233
234         return res;
235 }
236
237 static int find_by_mark(void *obj, void *arg, void *data, int flags)
238 {
239         struct ast_channel *c = obj;
240         const char *mark = data;
241         const char *tmp;
242         int res;
243
244         ast_channel_lock(c);
245
246         res = (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
247                 !strcasecmp(tmp, mark) &&
248                 can_pickup(c);
249
250         ast_channel_unlock(c);
251
252         return res ? CMP_MATCH | CMP_STOP : 0;
253 }
254
255 /* Attempt to pick up specified mark */
256 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
257 {
258         struct ast_channel *target;
259         int res = -1;
260
261         if ((target = ast_channel_callback(find_by_mark, NULL, (char *) mark, 0))) {
262                 ast_channel_lock(target);
263                 res = pickup_do(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         int res = 0;
275         char *tmp = ast_strdupa(data);
276         char *exten = NULL, *context = NULL;
277
278         if (ast_strlen_zero(data)) {
279                 res = ast_pickup_call(chan);
280                 return res;
281         }
282         
283         /* Parse extension (and context if there) */
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                                 break;
290                 } else {
291                         if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context))
292                                 break;
293                 }
294                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
295         }
296
297         return res;
298 }
299
300 /* application entry point for PickupChan() */
301 static int pickupchan_exec(struct ast_channel *chan, const char *data)
302 {
303         int res = 0;
304         char *tmp = ast_strdupa(data);
305         char *pickup = NULL;
306
307         if (ast_strlen_zero(data)) {
308                 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
309                 return -1;      
310         }
311
312         /* Parse channel */
313         while (!ast_strlen_zero(tmp) && (pickup = strsep(&tmp, "&"))) {
314                 if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
315                         ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
316                 } else {
317                         if (!pickup_by_channel(chan, pickup)) {
318                                 break;
319                         }
320                         ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
321                 }
322         }
323
324         return res;
325 }
326
327 static int unload_module(void)
328 {
329         int res;
330
331         res = ast_unregister_application(app);
332         res |= ast_unregister_application(app2);
333
334         return res;
335 }
336
337 static int load_module(void)
338 {
339         int res;
340
341         res = ast_register_application_xml(app, pickup_exec);
342         res |= ast_register_application_xml(app2, pickupchan_exec);
343
344         return res;
345 }
346
347 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");