Merge changes from team/group/appdocsxml
[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(char *channame)
126 {
127         struct ast_channel *chan;
128         char *chkchan = alloca(strlen(channame) + 2);
129
130         /* need to append a '-' for the comparison so we check full channel name,
131          * i.e SIP/hgc- , use a temporary variable so original stays the same for
132          * debugging.
133          */
134         strcpy(chkchan, channame);
135         strcat(chkchan, "-");
136
137         for (chan = ast_walk_channel_by_name_prefix_locked(NULL, channame, strlen(channame));
138                  chan;
139                  chan = ast_walk_channel_by_name_prefix_locked(chan, channame, strlen(channame))) {
140                 if (!strncasecmp(chan->name, chkchan, strlen(chkchan)) && can_pickup(chan))
141                         return chan;
142                 ast_channel_unlock(chan);
143         }
144         return NULL;
145 }
146
147 /*! \brief Attempt to pick up specified channel named , does not use context */
148 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
149 {
150         int res = 0;
151         struct ast_channel *target;
152
153         if (!(target = my_ast_get_channel_by_name_locked(pickup)))
154                 return -1;
155
156         /* Just check that we are not picking up the SAME as target */
157         if (chan->name != target->name && chan != target) {
158                 res = pickup_do(chan, target);
159                 ast_channel_unlock(target);
160         }
161
162         return res;
163 }
164
165 /* Attempt to pick up specified extension with context */
166 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
167 {
168         int res = -1;
169         struct ast_channel *target = NULL;
170
171         while ((target = ast_channel_walk_locked(target))) {
172                 if ((!strcasecmp(target->macroexten, exten) || !strcasecmp(target->exten, exten)) &&
173                         !strcasecmp(target->dialcontext, context) &&
174                         can_pickup(target)) {
175                         res = pickup_do(chan, target);
176                         ast_channel_unlock(target);
177                         break;
178                 }
179                 ast_channel_unlock(target);
180         }
181
182         return res;
183 }
184
185 /* Attempt to pick up specified mark */
186 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
187 {
188         int res = -1;
189         const char *tmp = NULL;
190         struct ast_channel *target = NULL;
191
192         while ((target = ast_channel_walk_locked(target))) {
193                 if ((tmp = pbx_builtin_getvar_helper(target, PICKUPMARK)) &&
194                         !strcasecmp(tmp, mark) &&
195                         can_pickup(target)) {
196                         res = pickup_do(chan, target);
197                         ast_channel_unlock(target);
198                         break;
199                 }
200                 ast_channel_unlock(target);
201         }
202
203         return res;
204 }
205
206 /* application entry point for Pickup() */
207 static int pickup_exec(struct ast_channel *chan, void *data)
208 {
209         int res = 0;
210         char *tmp = ast_strdupa(data);
211         char *exten = NULL, *context = NULL;
212
213         if (ast_strlen_zero(data)) {
214                 res = ast_pickup_call(chan);
215                 return res;
216         }
217         
218         /* Parse extension (and context if there) */
219         while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
220                 if ((context = strchr(exten, '@')))
221                         *context++ = '\0';
222                 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
223                         if (!pickup_by_mark(chan, exten))
224                                 break;
225                 } else {
226                         if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context))
227                                 break;
228                 }
229                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
230         }
231
232         return res;
233 }
234
235 /* application entry point for PickupChan() */
236 static int pickupchan_exec(struct ast_channel *chan, void *data)
237 {
238         int res = 0;
239         char *tmp = ast_strdupa(data);
240         char *pickup = NULL;
241
242         if (ast_strlen_zero(data)) {
243                 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
244                 return -1;      
245         }
246
247         /* Parse channel */
248         while (!ast_strlen_zero(tmp) && (pickup = strsep(&tmp, "&"))) {
249                 if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
250                         ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
251                 } else {
252                         if (!pickup_by_channel(chan, pickup)) {
253                                 break;
254                         }
255                         ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
256                 }
257         }
258
259         return res;
260 }
261
262 static int unload_module(void)
263 {
264         int res;
265
266         res = ast_unregister_application(app);
267         res |= ast_unregister_application(app2);
268
269         return res;
270 }
271
272 static int load_module(void)
273 {
274         int res;
275
276         res = ast_register_application_xml(app, pickup_exec);
277         res |= ast_register_application_xml(app2, pickupchan_exec);
278
279         return res;
280 }
281
282 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");