2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, Joshua Colp
6 * Joshua Colp <jcolp@digium.com>
8 * Portions merged from app_pickupchan, which was
9 * Copyright (C) 2008, Gary Cook
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.
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.
24 * \brief Directed Call Pickup Support
26 * \author Joshua Colp <jcolp@digium.com>
29 * \ingroup applications
33 <support_level>core</support_level>
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include "asterisk/file.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/module.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/app.h"
46 #include "asterisk/features.h"
47 #include "asterisk/manager.h"
48 #include "asterisk/callerid.h"
49 #include "asterisk/cel.h"
51 #define PICKUPMARK "PICKUPMARK"
54 <application name="Pickup" language="en_US">
56 Directed extension call pickup.
58 <syntax argsep="&">
59 <parameter name="ext" argsep="@" required="true">
60 <argument name="extension" required="true"/>
61 <argument name="context" />
63 <parameter name="ext2" argsep="@" multiple="true">
64 <argument name="extension2" required="true"/>
65 <argument name="context2"/>
69 <para>This application can pickup any ringing channel that is calling
70 the specified <replaceable>extension</replaceable>. If no <replaceable>context</replaceable>
71 is specified, the current context will be used. If you use the special string <literal>PICKUPMARK</literal>
72 for the context parameter, for example 10@PICKUPMARK, this application
73 tries to find a channel which has defined a <variable>PICKUPMARK</variable>
74 channel variable with the same value as <replaceable>extension</replaceable>
75 (in this example, <literal>10</literal>). When no parameter is specified, the application
76 will pickup a channel matching the pickup group of the active channel.</para>
79 <application name="PickupChan" language="en_US">
81 Pickup a ringing channel.
84 <parameter name="Technology/Resource" argsep="&" required="true">
85 <argument name="Technology/Resource" required="true" />
86 <argument name="Technology2/Resource2" required="false" multiple="true" />
88 <parameter name="options" required="false">
91 <para>Channel name specified partial name. Used when find channel by callid.</para>
97 <para>This will pickup a specified <replaceable>channel</replaceable> if ringing.</para>
102 static const char app[] = "Pickup";
103 static const char app2[] = "PickupChan";
105 struct pickup_by_name_args {
110 static int pickup_by_name_cb(void *obj, void *arg, void *data, int flags)
112 struct ast_channel *target = obj;/*!< Potential pickup target */
113 struct pickup_by_name_args *args = data;
115 ast_channel_lock(target);
116 if (!strncasecmp(target->name, args->name, args->len) && ast_can_pickup(target)) {
117 /* Return with the channel still locked on purpose */
118 return CMP_MATCH | CMP_STOP;
120 ast_channel_unlock(target);
125 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
126 static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
129 struct pickup_by_name_args pickup_args;
131 /* Check if channel name contains a '-'.
132 * In this case the channel name will be interpreted as full channel name.
134 if (strchr(channame, '-')) {
135 /* check full channel name */
136 pickup_args.len = strlen(channame);
137 pickup_args.name = channame;
139 /* need to append a '-' for the comparison so we check full channel name,
140 * i.e SIP/hgc- , use a temporary variable so original stays the same for
143 pickup_args.len = strlen(channame) + 1;
144 chkchan = alloca(pickup_args.len + 1);
145 strcpy(chkchan, channame);
146 strcat(chkchan, "-");
147 pickup_args.name = chkchan;
150 return ast_channel_callback(pickup_by_name_cb, NULL, &pickup_args, 0);
153 /*! \brief Attempt to pick up named channel, does not use context */
154 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
157 struct ast_channel *target;/*!< Potential pickup target */
159 target = my_ast_get_channel_by_name_locked(pickup);
161 /* Just check that we are not picking up the SAME as target. (i.e. ourself) */
162 if (chan != target) {
163 res = ast_do_pickup(chan, target);
165 ast_channel_unlock(target);
166 target = ast_channel_unref(target);
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)
175 struct ast_channel *target = NULL;/*!< Potential pickup target */
176 struct ast_channel_iterator *iter;
179 if (!(iter = ast_channel_iterator_by_exten_new(exten, context))) {
183 while ((target = ast_channel_iterator_next(iter))) {
184 ast_channel_lock(target);
185 if ((chan != target) && ast_can_pickup(target)) {
186 ast_log(LOG_NOTICE, "%s pickup by %s\n", target->name, chan->name);
189 ast_channel_unlock(target);
190 target = ast_channel_unref(target);
193 ast_channel_iterator_destroy(iter);
196 res = ast_do_pickup(chan, target);
197 ast_channel_unlock(target);
198 target = ast_channel_unref(target);
204 static int find_by_mark(void *obj, void *arg, void *data, int flags)
206 struct ast_channel *target = obj;/*!< Potential pickup target */
207 const char *mark = data;
210 ast_channel_lock(target);
211 tmp = pbx_builtin_getvar_helper(target, PICKUPMARK);
212 if (tmp && !strcasecmp(tmp, mark) && ast_can_pickup(target)) {
213 /* Return with the channel still locked on purpose */
214 return CMP_MATCH | CMP_STOP;
216 ast_channel_unlock(target);
221 /* Attempt to pick up specified mark */
222 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
224 struct ast_channel *target;/*!< Potential pickup target */
227 /* The found channel is already locked. */
228 target = ast_channel_callback(find_by_mark, NULL, (char *) mark, 0);
230 res = ast_do_pickup(chan, target);
231 ast_channel_unlock(target);
232 target = ast_channel_unref(target);
238 static int find_channel_by_group(void *obj, void *arg, void *data, int flags)
240 struct ast_channel *target = obj;/*!< Potential pickup target */
241 struct ast_channel *chan = data;/*!< Channel wanting to pickup call */
243 ast_channel_lock(target);
244 if (chan != target && (chan->pickupgroup & target->callgroup)
245 && ast_can_pickup(target)) {
246 /* Return with the channel still locked on purpose */
247 return CMP_MATCH | CMP_STOP;
249 ast_channel_unlock(target);
254 static int pickup_by_group(struct ast_channel *chan)
256 struct ast_channel *target;/*!< Potential pickup target */
259 /* The found channel is already locked. */
260 target = ast_channel_callback(find_channel_by_group, NULL, chan, 0);
262 ast_log(LOG_NOTICE, "pickup %s attempt by %s\n", target->name, chan->name);
263 res = ast_do_pickup(chan, target);
264 ast_channel_unlock(target);
265 target = ast_channel_unref(target);
271 /* application entry point for Pickup() */
272 static int pickup_exec(struct ast_channel *chan, const char *data)
274 char *tmp = ast_strdupa(data);
275 char *exten = NULL, *context = NULL;
277 if (ast_strlen_zero(data)) {
278 return pickup_by_group(chan) ? 0 : -1;
281 /* Parse extension (and context if there) */
282 while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
283 if ((context = strchr(exten, '@')))
285 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
286 if (!pickup_by_mark(chan, exten)) {
287 /* Pickup successful. Stop the dialplan this channel is a zombie. */
291 if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context)) {
292 /* Pickup successful. Stop the dialplan this channel is a zombie. */
296 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
299 /* Pickup failed. Keep going in the dialplan. */
303 /* Find channel for pick up specified by partial channel name */
304 static int find_by_part(void *obj, void *arg, void *data, int flags)
306 struct ast_channel *target = obj;/*!< Potential pickup target */
307 const char *part = data;
308 int len = strlen(part);
310 ast_channel_lock(target);
311 if (len <= strlen(target->name) && !strncmp(target->name, part, len)
312 && ast_can_pickup(target)) {
313 /* Return with the channel still locked on purpose */
314 return CMP_MATCH | CMP_STOP;
316 ast_channel_unlock(target);
321 /* Attempt to pick up specified by partial channel name */
322 static int pickup_by_part(struct ast_channel *chan, const char *part)
324 struct ast_channel *target;/*!< Potential pickup target */
327 /* The found channel is already locked. */
328 target = ast_channel_callback(find_by_part, NULL, (char *) part, 0);
330 res = ast_do_pickup(chan, target);
331 ast_channel_unlock(target);
332 target = ast_channel_unref(target);
338 /* application entry point for PickupChan() */
339 static int pickupchan_exec(struct ast_channel *chan, const char *data)
341 int partial_pickup = 0;
343 char *parse = ast_strdupa(data);
344 AST_DECLARE_APP_ARGS(args,
345 AST_APP_ARG(channel);
346 AST_APP_ARG(options);
348 AST_STANDARD_APP_ARGS(args, parse);
350 if (ast_strlen_zero(args.channel)) {
351 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
352 /* Pickup failed. Keep going in the dialplan. */
356 if (!ast_strlen_zero(args.options) && strchr(args.options, 'p')) {
361 while (!ast_strlen_zero(args.channel) && (pickup = strsep(&args.channel, "&"))) {
362 if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
363 ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
365 if (partial_pickup) {
366 if (!pickup_by_part(chan, pickup)) {
367 /* Pickup successful. Stop the dialplan this channel is a zombie. */
370 } else if (!pickup_by_channel(chan, pickup)) {
371 /* Pickup successful. Stop the dialplan this channel is a zombie. */
374 ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
378 /* Pickup failed. Keep going in the dialplan. */
382 static int unload_module(void)
386 res = ast_unregister_application(app);
387 res |= ast_unregister_application(app2);
392 static int load_module(void)
396 res = ast_register_application_xml(app, pickup_exec);
397 res |= ast_register_application_xml(app2, pickupchan_exec);
402 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");