update comment to match the state of the code
[asterisk-bristuff.git] / apps / app_directed_pickup.c
blob1d5318f56dd5610f1ce9376b1fc8b706461bfe40
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, Joshua Colp
6 * Joshua Colp <jcolp@digium.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.
19 /*! \file
21 * \brief Directed Call Pickup Support
23 * \author Joshua Colp <jcolp@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/lock.h"
43 #include "asterisk/app.h"
44 #include "asterisk/options.h"
46 #define PICKUPMARK "PICKUPMARK"
48 static const char *app = "Pickup";
49 static const char *synopsis = "Directed Call Pickup";
50 static const char *descrip =
51 " Pickup(extension[@context][&extension2@context...]): This application can pickup any ringing channel\n"
52 "that is calling the specified extension. If no context is specified, the current\n"
53 "context will be used. If you use the special string \"PICKUPMARK\" for the context parameter, for example\n"
54 "10@PICKUPMARK, this application tries to find a channel which has defined a channel variable with the same content\n"
55 "as \"extension\".";
57 /* Perform actual pickup between two channels */
58 static int pickup_do(struct ast_channel *chan, struct ast_channel *target)
60 int res = 0;
62 if (option_debug)
63 ast_log(LOG_DEBUG, "Call pickup on '%s' by '%s'\n", target->name, chan->name);
65 if ((res = ast_answer(chan))) {
66 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
67 return -1;
70 if ((res = ast_queue_control(chan, AST_CONTROL_ANSWER))) {
71 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
72 return -1;
75 if ((res = ast_channel_masquerade(target, chan))) {
76 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
77 return -1;
80 return res;
83 /* Helper function that determines whether a channel is capable of being picked up */
84 static int can_pickup(struct ast_channel *chan)
86 if (!chan->pbx && (chan->_state == AST_STATE_RINGING || chan->_state == AST_STATE_RING))
87 return 1;
88 else
89 return 0;
92 /* Attempt to pick up specified extension with context */
93 static int pickup_by_exten(struct ast_channel *chan, char *exten, char *context)
95 int res = -1;
96 struct ast_channel *target = NULL;
98 while ((target = ast_channel_walk_locked(target))) {
99 if ((!strcasecmp(target->macroexten, exten) || !strcasecmp(target->exten, exten)) &&
100 !strcasecmp(target->dialcontext, context) &&
101 can_pickup(target)) {
102 res = pickup_do(chan, target);
103 ast_channel_unlock(target);
104 break;
106 ast_channel_unlock(target);
109 return res;
112 /* Attempt to pick up specified mark */
113 static int pickup_by_mark(struct ast_channel *chan, char *mark)
115 int res = -1;
116 const char *tmp = NULL;
117 struct ast_channel *target = NULL;
119 while ((target = ast_channel_walk_locked(target))) {
120 if ((tmp = pbx_builtin_getvar_helper(target, PICKUPMARK)) &&
121 !strcasecmp(tmp, mark) &&
122 can_pickup(target)) {
123 res = pickup_do(chan, target);
124 ast_channel_unlock(target);
125 break;
127 ast_channel_unlock(target);
130 return res;
133 /* Main application entry point */
134 static int pickup_exec(struct ast_channel *chan, void *data)
136 int res = 0;
137 struct ast_module_user *u = NULL;
138 char *tmp = ast_strdupa(data);
139 char *exten = NULL, *context = NULL;
141 if (ast_strlen_zero(data)) {
142 ast_log(LOG_WARNING, "Pickup requires an argument (extension)!\n");
143 return -1;
146 u = ast_module_user_add(chan);
148 /* Parse extension (and context if there) */
149 while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
150 if ((context = strchr(exten, '@')))
151 *context++ = '\0';
152 if (context && !strcasecmp(context, PICKUPMARK)) {
153 if (!pickup_by_mark(chan, exten))
154 break;
155 } else {
156 if (!pickup_by_exten(chan, exten, context ? context : chan->context))
157 break;
159 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
162 ast_module_user_remove(u);
164 return res;
167 static int unload_module(void)
169 int res;
171 res = ast_unregister_application(app);
173 return res;
176 static int load_module(void)
178 return ast_register_application(app, pickup_exec, synopsis, descrip);
181 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");