Fix some whitespace.
[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         auto int find_by_exten(struct ast_channel *c);
176         int find_by_exten(struct ast_channel *c) {
177                 return (!strcasecmp(c->macroexten, exten) || !strcasecmp(c->exten, exten)) &&
178                         !strcasecmp(c->dialcontext, context) &&
179                         can_pickup(c);
180         }
181
182         struct ast_channel *target = ast_channel_search_locked(find_by_exten);
183
184         if (target) {
185                 int res = pickup_do(chan, target);
186                 ast_channel_unlock(target);
187                 target = NULL;
188                 return res;
189         }
190
191         return -1;
192 }
193
194 /* Attempt to pick up specified mark */
195 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
196 {
197         auto int find_by_mark(struct ast_channel *);
198         int find_by_mark(struct ast_channel *c) {
199                 const char *tmp;
200                 return (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
201                         !strcasecmp(tmp, mark) &&
202                         can_pickup(c);
203         }       
204
205         struct ast_channel *target = ast_channel_search_locked(find_by_mark);
206
207         if (target) {
208                 int res = pickup_do(chan, target);
209                 ast_channel_unlock(target);
210                 target = NULL;
211                 return res;
212         }
213
214         return -1;
215 }
216
217 /* application entry point for Pickup() */
218 static int pickup_exec(struct ast_channel *chan, void *data)
219 {
220         int res = 0;
221         char *tmp = ast_strdupa(data);
222         char *exten = NULL, *context = NULL;
223
224         if (ast_strlen_zero(data)) {
225                 res = ast_pickup_call(chan);
226                 return res;
227         }
228         
229         /* Parse extension (and context if there) */
230         while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
231                 if ((context = strchr(exten, '@')))
232                         *context++ = '\0';
233                 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
234                         if (!pickup_by_mark(chan, exten))
235                                 break;
236                 } else {
237                         if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context))
238                                 break;
239                 }
240                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
241         }
242
243         return res;
244 }
245
246 /* application entry point for PickupChan() */
247 static int pickupchan_exec(struct ast_channel *chan, void *data)
248 {
249         int res = 0;
250         char *tmp = ast_strdupa(data);
251         char *pickup = NULL;
252
253         if (ast_strlen_zero(data)) {
254                 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
255                 return -1;      
256         }
257
258         /* Parse channel */
259         while (!ast_strlen_zero(tmp) && (pickup = strsep(&tmp, "&"))) {
260                 if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
261                         ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
262                 } else {
263                         if (!pickup_by_channel(chan, pickup)) {
264                                 break;
265                         }
266                         ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
267                 }
268         }
269
270         return res;
271 }
272
273 static int unload_module(void)
274 {
275         int res;
276
277         res = ast_unregister_application(app);
278         res |= ast_unregister_application(app2);
279
280         return res;
281 }
282
283 static int load_module(void)
284 {
285         int res;
286
287         res = ast_register_application_xml(app, pickup_exec);
288         res |= ast_register_application_xml(app2, pickupchan_exec);
289
290         return res;
291 }
292
293 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");