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