2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, Joshua Colp
6 * Joshua Colp <jcolp@asterlink.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * Directed Call Pickup Support
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/file.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/lock.h"
39 #include "asterisk/app.h"
41 static const char *tdesc = "Directed Call Pickup Application";
42 static const char *app = "Pickup";
43 static const char *synopsis = "Directed Call Pickup application.";
44 static const char *descrip =
45 " Pickup(extension@context):\n"
46 "Steals any calls to a specified extension that are in a ringing state and bridges them to the current channel. Context is an optional argument.\n";
52 static int pickup_exec(struct ast_channel *chan, void *data)
55 struct localuser *u = NULL;
56 struct ast_channel *origin = NULL, *target = NULL;
57 char *tmp = NULL, *exten = NULL, *context = NULL;
58 char workspace[256] = "";
60 /* Get the extension and context if present */
62 context = strchr(data, '@');
68 /* Make sure we atleast have an extension to work with */
70 ast_log(LOG_WARNING, "%s requires atleast one argument (extension)\n", app);
76 /* Find a channel to pickup */
77 origin = ast_get_channel_by_exten_locked(exten, context);
79 ast_cdr_getvar(origin->cdr, "dstchannel", &tmp, workspace,
80 sizeof(workspace), 0);
82 /* We have a possible channel... now we need to find it! */
83 target = ast_get_channel_by_name_locked(tmp);
85 ast_log(LOG_DEBUG, "No target channel found.\n");
88 ast_mutex_unlock(&origin->lock);
90 ast_log(LOG_DEBUG, "No originating channel found.\n");
96 if (target && (!target->pbx) && ((target->_state == AST_STATE_RINGING) || (target->_state == AST_STATE_RING))) {
97 ast_log(LOG_DEBUG, "Call pickup on chan '%s' by '%s'\n", target->name,
99 res = ast_answer(chan);
101 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
105 res = ast_queue_control(chan, AST_CONTROL_ANSWER);
107 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n",
112 res = ast_channel_masquerade(target, chan);
114 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
119 ast_mutex_unlock(&target->lock);
121 ast_log(LOG_DEBUG, "No call pickup possible...\n");
126 LOCAL_USER_REMOVE(u);
131 int unload_module(void)
133 STANDARD_HANGUP_LOCALUSERS;
135 return ast_unregister_application(app);
138 int load_module(void)
140 return ast_register_application(app, pickup_exec, synopsis, descrip);
143 char *description(void)
145 return (char *) tdesc;
152 STANDARD_USECOUNT(res);
159 return ASTERISK_GPL_KEY;