cfba99fceffc985401b8c56de56d47914929aa84
[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 *app = "Pickup";
88 static const char *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 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
138 static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
139 {
140         struct ast_channel *chan;
141         char *chkchan;
142         size_t channame_len, chkchan_len;
143
144         channame_len = strlen(channame);
145         chkchan_len = channame_len + 2;
146
147         chkchan = alloca(chkchan_len);
148
149         /* need to append a '-' for the comparison so we check full channel name,
150          * i.e SIP/hgc- , use a temporary variable so original stays the same for
151          * debugging.
152          */
153         strcpy(chkchan, channame);
154         strcat(chkchan, "-");
155
156         for (chan = ast_walk_channel_by_name_prefix_locked(NULL, channame, channame_len);
157                  chan;
158                  chan = ast_walk_channel_by_name_prefix_locked(chan, channame, channame_len)) {
159                 if (!strncasecmp(chan->name, chkchan, chkchan_len) && can_pickup(chan)) {
160                         return chan;
161                 }
162                 ast_channel_unlock(chan);
163         }
164         return NULL;
165 }
166
167 /*! \brief Attempt to pick up specified channel named , does not use context */
168 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
169 {
170         int res = 0;
171         struct ast_channel *target;
172
173         if (!(target = my_ast_get_channel_by_name_locked(pickup)))
174                 return -1;
175
176         /* Just check that we are not picking up the SAME as target */
177         if (chan->name != target->name && chan != target) {
178                 res = pickup_do(chan, target);
179         }
180         ast_channel_unlock(target);
181
182         return res;
183 }
184
185 struct pickup_criteria {
186         const char *exten;
187         const char *context;
188 };
189
190 static int find_by_exten(struct ast_channel *c, void *data)
191 {
192         struct pickup_criteria *info = data;
193
194         return (!strcasecmp(c->macroexten, info->exten) || !strcasecmp(c->exten, info->exten)) &&
195                 !strcasecmp(c->dialcontext, info->context) &&
196                 can_pickup(c);
197 }
198
199 /* Attempt to pick up specified extension with context */
200 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
201 {
202         struct ast_channel *target = NULL;
203         struct pickup_criteria search = {
204                 .exten = exten,
205                 .context = context,
206         };
207
208         target = ast_channel_search_locked(find_by_exten, &search);
209
210         if (target) {
211                 int res = pickup_do(chan, target);
212                 ast_channel_unlock(target);
213                 target = NULL;
214                 return res;
215         }
216
217         return -1;
218 }
219
220 static int find_by_mark(struct ast_channel *c, void *data)
221 {
222         const char *mark = data;
223         const char *tmp;
224
225         return (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
226                 !strcasecmp(tmp, mark) &&
227                 can_pickup(c);
228 }
229
230 /* Attempt to pick up specified mark */
231 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
232 {
233         struct ast_channel *target = ast_channel_search_locked(find_by_mark, (char *) mark);
234
235         if (target) {
236                 int res = pickup_do(chan, target);
237                 ast_channel_unlock(target);
238                 target = NULL;
239                 return res;
240         }
241
242         return -1;
243 }
244
245 /* application entry point for Pickup() */
246 static int pickup_exec(struct ast_channel *chan, void *data)
247 {
248         int res = 0;
249         char *tmp = ast_strdupa(data);
250         char *exten = NULL, *context = NULL;
251
252         if (ast_strlen_zero(data)) {
253                 res = ast_pickup_call(chan);
254                 return res;
255         }
256         
257         /* Parse extension (and context if there) */
258         while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
259                 if ((context = strchr(exten, '@')))
260                         *context++ = '\0';
261                 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
262                         if (!pickup_by_mark(chan, exten))
263                                 break;
264                 } else {
265                         if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context))
266                                 break;
267                 }
268                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
269         }
270
271         return res;
272 }
273
274 /* application entry point for PickupChan() */
275 static int pickupchan_exec(struct ast_channel *chan, void *data)
276 {
277         int res = 0;
278         char *tmp = ast_strdupa(data);
279         char *pickup = NULL;
280
281         if (ast_strlen_zero(data)) {
282                 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
283                 return -1;      
284         }
285
286         /* Parse channel */
287         while (!ast_strlen_zero(tmp) && (pickup = strsep(&tmp, "&"))) {
288                 if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
289                         ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
290                 } else {
291                         if (!pickup_by_channel(chan, pickup)) {
292                                 break;
293                         }
294                         ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
295                 }
296         }
297
298         return res;
299 }
300
301 static int unload_module(void)
302 {
303         int res;
304
305         res = ast_unregister_application(app);
306         res |= ast_unregister_application(app2);
307
308         return res;
309 }
310
311 static int load_module(void)
312 {
313         int res;
314
315         res = ast_register_application_xml(app, pickup_exec);
316         res |= ast_register_application_xml(app2, pickupchan_exec);
317
318         return res;
319 }
320
321 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");