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
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
44 #define PICKUPMARK "PICKUPMARK"
47 <application name="Pickup" language="en_US">
49 Directed extension call pickup.
51 <syntax argsep="&">
52 <parameter name="ext" argsep="@" required="true">
53 <argument name="extension" required="true"/>
54 <argument name="context" />
56 <parameter name="ext2" argsep="@" multiple="true">
57 <argument name="extension2" required="true"/>
58 <argument name="context2"/>
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>
72 <application name="PickupChan" language="en_US">
74 Pickup a ringing channel.
77 <parameter name="channel" required="true" />
78 <parameter name="channel2" multiple="true" />
81 <para>This will pickup a specified <replaceable>channel</replaceable> if ringing.</para>
86 static const char *app = "Pickup";
87 static const char *app2 = "PickupChan";
88 /*! \todo This application should return a result code, like PICKUPRESULT */
90 /* Perform actual pickup between two channels */
91 static int pickup_do(struct ast_channel *chan, struct ast_channel *target)
95 ast_debug(1, "Call pickup on '%s' by '%s'\n", target->name, chan->name);
97 if ((res = ast_answer(chan))) {
98 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
102 if ((res = ast_queue_control(chan, AST_CONTROL_ANSWER))) {
103 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
107 if ((res = ast_channel_masquerade(target, chan))) {
108 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
115 /* Helper function that determines whether a channel is capable of being picked up */
116 static int can_pickup(struct ast_channel *chan)
118 if (!chan->pbx && (chan->_state == AST_STATE_RINGING || chan->_state == AST_STATE_RING))
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)
127 struct ast_channel *chan;
129 size_t channame_len, chkchan_len;
131 channame_len = strlen(channame);
132 chkchan_len = channame_len + 2;
134 chkchan = alloca(chkchan_len);
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
140 strcpy(chkchan, channame);
141 strcat(chkchan, "-");
143 for (chan = ast_walk_channel_by_name_prefix_locked(NULL, channame, channame_len);
145 chan = ast_walk_channel_by_name_prefix_locked(chan, channame, channame_len)) {
146 if (!strncasecmp(chan->name, chkchan, chkchan_len) && can_pickup(chan)) {
149 ast_channel_unlock(chan);
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)
158 struct ast_channel *target;
160 if (!(target = my_ast_get_channel_by_name_locked(pickup)))
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);
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)
176 struct ast_channel *target = NULL;
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);
186 ast_channel_unlock(target);
192 /* Attempt to pick up specified mark */
193 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
196 const char *tmp = NULL;
197 struct ast_channel *target = NULL;
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);
207 ast_channel_unlock(target);
213 /* application entry point for Pickup() */
214 static int pickup_exec(struct ast_channel *chan, void *data)
217 char *tmp = ast_strdupa(data);
218 char *exten = NULL, *context = NULL;
220 if (ast_strlen_zero(data)) {
221 res = ast_pickup_call(chan);
225 /* Parse extension (and context if there) */
226 while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
227 if ((context = strchr(exten, '@')))
229 if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
230 if (!pickup_by_mark(chan, exten))
233 if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context))
236 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
242 /* application entry point for PickupChan() */
243 static int pickupchan_exec(struct ast_channel *chan, void *data)
246 char *tmp = ast_strdupa(data);
249 if (ast_strlen_zero(data)) {
250 ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
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);
259 if (!pickup_by_channel(chan, pickup)) {
262 ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
269 static int unload_module(void)
273 res = ast_unregister_application(app);
274 res |= ast_unregister_application(app2);
279 static int load_module(void)
283 res = ast_register_application_xml(app, pickup_exec);
284 res |= ast_register_application_xml(app2, pickupchan_exec);
289 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");