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