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.
59 <parameter name="targets" argsep="&">
60 <argument name="exten" argsep="@" required="true">
61 <argument name="extension" required="true"/>
62 <argument name="context" />
64 <argument name="exten2" argsep="@" multiple="true">
65 <argument name="extension2" required="true"/>
66 <argument name="context2"/>
71 <para>This application can pickup any ringing channel that is calling
72 the specified dialplan <replaceable>extension</replaceable>. If no dialplan
73 <replaceable>context</replaceable> is specified, the current context will be
74 used. If you use the special string <literal>PICKUPMARK</literal>
75 for the context, for example 10@PICKUPMARK, this application tries to find a
76 channel which has defined a channel variable <variable>PICKUPMARK</variable>
77 with the same value as <replaceable>extension</replaceable> (in this example,
78 <literal>10</literal>). When no parameter is specified, the application will
79 pickup a channel matching the pickup group of the active channel.</para>
82 <application name="PickupChan" language="en_US">
84 Pickup a ringing channel.
87 <parameter name="Technology/Resource" argsep="&" required="true">
88 <argument name="Technology/Resource" required="true" />
89 <argument name="Technology2/Resource2" required="false" multiple="true" />
91 <parameter name="options" required="false">
94 <para>Channel name specified partial name. Used when find channel by callid.</para>
100 <para>This will pickup a specified <replaceable>channel</replaceable> if ringing.</para>
105 static const char app[] = "Pickup";
106 static const char app2[] = "PickupChan";
108 struct pickup_by_name_args {
113 static int pickup_by_name_cb(void *obj, void *arg, void *data, int flags)
115 struct ast_channel *target = obj;/*!< Potential pickup target */
116 struct pickup_by_name_args *args = data;
118 ast_channel_lock(target);
119 if (!strncasecmp(ast_channel_name(target), args->name, args->len) && ast_can_pickup(target)) {
120 /* Return with the channel still locked on purpose */
121 return CMP_MATCH | CMP_STOP;
123 ast_channel_unlock(target);
128 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
129 static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
132 struct pickup_by_name_args pickup_args;
134 /* Check if channel name contains a '-'.
135 * In this case the channel name will be interpreted as full channel name.
137 if (strchr(channame, '-')) {
138 /* check full channel name */
139 pickup_args.len = strlen(channame);
140 pickup_args.name = channame;
142 /* need to append a '-' for the comparison so we check full channel name,
143 * i.e SIP/hgc- , use a temporary variable so original stays the same for
146 pickup_args.len = strlen(channame) + 1;
147 chkchan = alloca(pickup_args.len + 1);
148 strcpy(chkchan, channame);
149 strcat(chkchan, "-");
150 pickup_args.name = chkchan;
153 return ast_channel_callback(pickup_by_name_cb, NULL, &pickup_args, 0);
156 /*! \brief Attempt to pick up named channel, does not use context */
157 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
160 struct ast_channel *target;/*!< Potential pickup target */
162 target = my_ast_get_channel_by_name_locked(pickup);
164 /* Just check that we are not picking up the SAME as target. (i.e. ourself) */
165 if (chan != target) {
166 res = ast_do_pickup(chan, target);
168 ast_channel_unlock(target);
169 target = ast_channel_unref(target);
175 /* Attempt to pick up specified extension with context */
176 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
178 struct ast_channel *target = NULL;/*!< Potential pickup target */
179 struct ast_channel_iterator *iter;
182 if (!(iter = ast_channel_iterator_by_exten_new(exten, context))) {
186 while ((target = ast_channel_iterator_next(iter))) {
187 ast_channel_lock(target);
188 if ((chan != target) && ast_can_pickup(target)) {
189 ast_log(LOG_NOTICE, "%s pickup by %s\n", ast_channel_name(target), ast_channel_name(chan));
192 ast_channel_unlock(target);
193 target = ast_channel_unref(target);
196 ast_channel_iterator_destroy(iter);
199 res = ast_do_pickup(chan, target);
200 ast_channel_unlock(target);
201 target = ast_channel_unref(target);
207 static int find_by_mark(void *obj, void *arg, void *data, int flags)
209 struct ast_channel *target = obj;/*!< Potential pickup target */
210 const char *mark = data;
213 ast_channel_lock(target);
214 tmp = pbx_builtin_getvar_helper(target, PICKUPMARK);
215 if (tmp && !strcasecmp(tmp, mark) && ast_can_pickup(target)) {
216 /* Return with the channel still locked on purpose */
217 return CMP_MATCH | CMP_STOP;
219 ast_channel_unlock(target);
224 /* Attempt to pick up specified mark */
225 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
227 struct ast_channel *target;/*!< Potential pickup target */
230 /* The found channel is already locked. */
231 target = ast_channel_callback(find_by_mark, NULL, (char *) mark, 0);
233 res = ast_do_pickup(chan, target);
234 ast_channel_unlock(target);
235 target = ast_channel_unref(target);
241 static int find_channel_by_group(void *obj, void *arg, void *data, int flags)
243 struct ast_channel *target = obj;/*!< Potential pickup target */
244 struct ast_channel *chan = data;/*!< Channel wanting to pickup call */
246 ast_channel_lock(target);
247 if (chan != target && (ast_channel_pickupgroup(chan) & ast_channel_callgroup(target))
248 && ast_can_pickup(target)) {
249 /* Return with the channel still locked on purpose */
250 return CMP_MATCH | CMP_STOP;
252 ast_channel_unlock(target);
257 static int pickup_by_group(struct ast_channel *chan)
259 struct ast_channel *target;/*!< Potential pickup target */
262 /* The found channel is already locked. */
263 target = ast_channel_callback(find_channel_by_group, NULL, chan, 0);
265 ast_log(LOG_NOTICE, "pickup %s attempt by %s\n", ast_channel_name(target), ast_channel_name(chan));
266 res = ast_do_pickup(chan, target);
267 ast_channel_unlock(target);
268 target = ast_channel_unref(target);
274 /* application entry point for Pickup() */
275 static int pickup_exec(struct ast_channel *chan, const char *data)
277 char *tmp = ast_strdupa(data);
278 char *exten = NULL, *context = NULL;
280 if (ast_strlen_zero(data)) {
281 return pickup_by_group(chan) ? 0 : -1;
284 /* Parse extension (and context if there) */
285 while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
286 if ((context = strchr(exten, '@')))
288 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
289 if (!pickup_by_mark(chan, exten)) {
290 /* Pickup successful. Stop the dialplan this channel is a zombie. */
294 if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : ast_channel_context(chan))) {
295 /* Pickup successful. Stop the dialplan this channel is a zombie. */
299 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
302 /* Pickup failed. Keep going in the dialplan. */
306 /* Find channel for pick up specified by partial channel name */
307 static int find_by_part(void *obj, void *arg, void *data, int flags)
309 struct ast_channel *target = obj;/*!< Potential pickup target */
310 const char *part = data;
311 int len = strlen(part);
313 ast_channel_lock(target);
314 if (len <= strlen(ast_channel_name(target)) && !strncmp(ast_channel_name(target), part, len)
315 && ast_can_pickup(target)) {
316 /* Return with the channel still locked on purpose */
317 return CMP_MATCH | CMP_STOP;
319 ast_channel_unlock(target);
324 /* Attempt to pick up specified by partial channel name */
325 static int pickup_by_part(struct ast_channel *chan, const char *part)
327 struct ast_channel *target;/*!< Potential pickup target */
330 /* The found channel is already locked. */
331 target = ast_channel_callback(find_by_part, NULL, (char *) part, 0);
333 res = ast_do_pickup(chan, target);
334 ast_channel_unlock(target);
335 target = ast_channel_unref(target);
341 /* application entry point for PickupChan() */
342 static int pickupchan_exec(struct ast_channel *chan, const char *data)
344 int partial_pickup = 0;
346 char *parse = ast_strdupa(data);
347 AST_DECLARE_APP_ARGS(args,
348 AST_APP_ARG(channel);
349 AST_APP_ARG(options);
351 AST_STANDARD_APP_ARGS(args, parse);
353 if (ast_strlen_zero(args.channel)) {
354 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
355 /* Pickup failed. Keep going in the dialplan. */
359 if (!ast_strlen_zero(args.options) && strchr(args.options, 'p')) {
364 while (!ast_strlen_zero(args.channel) && (pickup = strsep(&args.channel, "&"))) {
365 if (!strncasecmp(ast_channel_name(chan), pickup, strlen(pickup))) {
366 ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
368 if (partial_pickup) {
369 if (!pickup_by_part(chan, pickup)) {
370 /* Pickup successful. Stop the dialplan this channel is a zombie. */
373 } else if (!pickup_by_channel(chan, pickup)) {
374 /* Pickup successful. Stop the dialplan this channel is a zombie. */
377 ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
381 /* Pickup failed. Keep going in the dialplan. */
385 static int unload_module(void)
389 res = ast_unregister_application(app);
390 res |= ast_unregister_application(app2);
395 static int load_module(void)
399 res = ast_register_application_xml(app, pickup_exec);
400 res |= ast_register_application_xml(app2, pickupchan_exec);
405 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");