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