allow app_directed_pickup to try to pickup
[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@asterlink.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@asterlink.com>
24  *
25  * \ingroup applications
26  */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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
45 static const char *tdesc = "Directed Call Pickup Application";
46 static const char *app = "Pickup";
47 static const char *synopsis = "Directed Call Pickup";
48 static const char *descrip =
49 "  Pickup(extension[@context][&extension2@context...]): This application can pickup any ringing channel\n"
50 "that is calling the specified extension. If no context is specified, the current\n"
51 "context will be used.\n";
52
53 STANDARD_LOCAL_USER;
54
55 LOCAL_USER_DECL;
56
57 static int pickup_exec(struct ast_channel *chan, void *data)
58 {
59         int res = 0;
60         struct localuser *u = NULL;
61         struct ast_channel *origin = NULL, *target = NULL;
62         char *tmp = NULL, *exten = NULL, *context = NULL, *rest=data;
63         char workspace[256] = "";
64
65         if (ast_strlen_zero(data)) {
66                 ast_log(LOG_WARNING, "Pickup requires an argument (extension) !\n");
67                 return -1;      
68         }
69
70         LOCAL_USER_ADD(u);
71         
72         while (!target && (exten = rest) ) {
73                 res = 0;
74                 rest = strchr(exten, '&');
75                 if (rest)
76                         *rest++ = 0;
77
78                 /* Get the extension and context if present */
79                 context = strchr(exten, '@');
80                 if (context)
81                         *context++ = '\0';
82
83                 /* Find a channel to pickup */
84                 origin = ast_get_channel_by_exten_locked(exten, context);
85                 if (origin) {
86                         ast_cdr_getvar(origin->cdr, "dstchannel", &tmp, workspace,
87                                         sizeof(workspace), 0);
88                         if (tmp) {
89                                 /* We have a possible channel... now we need to find it! */
90                                 target = ast_get_channel_by_name_locked(tmp);
91                         } else {
92                                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
93                                 res = -1;
94                         }
95                         ast_mutex_unlock(&origin->lock);
96
97                 } else {
98                         ast_log(LOG_DEBUG, "No originating channel found.\n");
99                 }
100
101                 if (res)
102                         continue;
103
104                 if (target && (!target->pbx) && ((target->_state == AST_STATE_RINGING) || (target->_state == AST_STATE_RING) ) ) {
105                         ast_log(LOG_DEBUG, "Call pickup on chan '%s' by '%s'\n", target->name,
106                                         chan->name);
107                         res = ast_answer(chan);
108                         if (res) {
109                                 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
110                                 res = -1;
111                                 break;
112                         }
113                         res = ast_queue_control(chan, AST_CONTROL_ANSWER);
114                         if (res) {
115                                 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n",
116                                                 chan->name);
117                                 res = -1;
118                                 break;
119                         }
120                         res = ast_channel_masquerade(target, chan);
121                         if (res) {
122                                 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
123                                 res = -1;
124                                 break;
125                         }
126                 } else {
127                         ast_log(LOG_NOTICE, "No call pickup possible for %s...\n", exten);
128                         res = -1;
129                 }
130         }
131         if (target) 
132                 ast_mutex_unlock(&target->lock);
133         
134         LOCAL_USER_REMOVE(u);
135
136         return res;
137 }
138
139 int unload_module(void)
140 {
141         int res;
142
143         res = ast_unregister_application(app);
144         
145         STANDARD_HANGUP_LOCALUSERS;
146
147         return res;
148 }
149
150 int load_module(void)
151 {
152         return ast_register_application(app, pickup_exec, synopsis, descrip);
153 }
154
155 char *description(void)
156 {
157         return (char *) tdesc;
158 }
159
160 int usecount(void)
161 {
162         int res;
163
164         STANDARD_USECOUNT(res);
165
166         return res;
167 }
168
169 char *key()
170 {
171         return ASTERISK_GPL_KEY;
172 }