It's new directed pickup! This now features a more sane way of finding the channel...
[asterisk/asterisk.git] / apps / app_directed_pickup.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005, Joshua Colp
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Directed Call Pickup Support
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  *
25  * \ingroup applications
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36
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"
45
46 #define PICKUPMARK "PICKUPMARK"
47
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\".";
56
57 /* Perform actual pickup between two channels */
58 static int pickup_do(struct ast_channel *chan, struct ast_channel *target)
59 {
60         int res = 0;
61
62         if (option_debug)
63                 ast_log(LOG_DEBUG, "Call pickup on '%s' by '%s'\n", target->name, chan->name);
64
65         if ((res = ast_answer(chan))) {
66                 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
67                 return -1;
68         }
69
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;
73         }
74
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;
78         }
79
80         return res;
81 }
82
83 /* Helper function that determines whether a channel is capable of being picked up */
84 static int can_pickup(struct ast_channel *chan)
85 {
86         if (!chan->pbx && (chan->_state == AST_STATE_RINGING || chan->_state == AST_STATE_RING))
87                 return 1;
88         else
89                 return 0;
90 }
91
92 /* Attempt to pick up specified extension with context */
93 static int pickup_by_exten(struct ast_channel *chan, char *exten, char *context)
94 {
95         int res = -1;
96         struct ast_channel *target = NULL;
97
98         while ((target = ast_channel_walk_locked(target))) {
99                 if (!strcasecmp(target->exten, exten) &&
100                     !strcasecmp(target->context, context) &&
101                     can_pickup(target)) {
102                         res = pickup_do(chan, target);
103                         ast_channel_unlock(target);
104                         break;
105                 }
106                 ast_channel_unlock(target);
107         }
108
109         return res;
110 }
111
112 /* Attempt to pick up specified mark */
113 static int pickup_by_mark(struct ast_channel *chan, char *mark)
114 {
115         int res = -1;
116         const char *tmp = NULL;
117         struct ast_channel *target = NULL;
118
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;
126                 }
127                 ast_channel_unlock(target);
128         }
129
130         return res;
131 }
132
133 /* Main application entry point */
134 static int pickup_exec(struct ast_channel *chan, void *data)
135 {
136         int res = 0;
137         struct ast_module_user *u = NULL;
138         char *tmp = ast_strdupa(data);
139         char *exten = NULL, *context = NULL;
140
141         if (ast_strlen_zero(data)) {
142                 ast_log(LOG_WARNING, "Pickup requires an argument (extension)!\n");
143                 return -1;      
144         }
145
146         u = ast_module_user_add(chan);
147         
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;
158                 }
159                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
160         }
161
162         ast_module_user_remove(u);
163
164         return res;
165 }
166
167 static int unload_module(void)
168 {
169         int res;
170
171         res = ast_unregister_application(app);
172         
173         return res;
174 }
175
176 static int load_module(void)
177 {
178         return ast_register_application(app, pickup_exec, synopsis, descrip);
179 }
180
181 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");