3255ade9f7d8dfb78380d4c2ae459dd6891425ce
[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 LOCAL_USER_DECL;
54
55 static int pickup_exec(struct ast_channel *chan, void *data)
56 {
57         int res = 0;
58         struct localuser *u = NULL;
59         struct ast_channel *origin = NULL, *target = NULL;
60         char *tmp = NULL, *exten = NULL, *context = NULL, *rest=data;
61         char workspace[256] = "";
62
63         if (ast_strlen_zero(data)) {
64                 ast_log(LOG_WARNING, "Pickup requires an argument (extension) !\n");
65                 return -1;      
66         }
67
68         LOCAL_USER_ADD(u);
69         
70         while (!target && (exten = rest) ) {
71                 res = 0;
72                 rest = strchr(exten, '&');
73                 if (rest)
74                         *rest++ = 0;
75
76                 /* Get the extension and context if present */
77                 context = strchr(exten, '@');
78                 if (context)
79                         *context++ = '\0';
80
81                 /* Find a channel to pickup */
82                 origin = ast_get_channel_by_exten_locked(exten, context);
83                 if (origin) {
84                         ast_cdr_getvar(origin->cdr, "dstchannel", &tmp, workspace,
85                                         sizeof(workspace), 0);
86                         if (tmp) {
87                                 /* We have a possible channel... now we need to find it! */
88                                 target = ast_get_channel_by_name_locked(tmp);
89                         } else {
90                                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
91                                 res = -1;
92                         }
93                         ast_mutex_unlock(&origin->lock);
94
95                 } else {
96                         ast_log(LOG_DEBUG, "No originating channel found.\n");
97                 }
98
99                 if (res)
100                         continue;
101
102                 if (target && (!target->pbx) && ((target->_state == AST_STATE_RINGING) || (target->_state == AST_STATE_RING) ) ) {
103                         ast_log(LOG_DEBUG, "Call pickup on chan '%s' by '%s'\n", target->name,
104                                         chan->name);
105                         res = ast_answer(chan);
106                         if (res) {
107                                 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
108                                 res = -1;
109                                 break;
110                         }
111                         res = ast_queue_control(chan, AST_CONTROL_ANSWER);
112                         if (res) {
113                                 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n",
114                                                 chan->name);
115                                 res = -1;
116                                 break;
117                         }
118                         res = ast_channel_masquerade(target, chan);
119                         if (res) {
120                                 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
121                                 res = -1;
122                                 break;
123                         }
124                 } else {
125                         ast_log(LOG_NOTICE, "No call pickup possible for %s...\n", exten);
126                         res = -1;
127                 }
128         }
129         if (target) 
130                 ast_mutex_unlock(&target->lock);
131         
132         LOCAL_USER_REMOVE(u);
133
134         return res;
135 }
136
137 int unload_module(void)
138 {
139         int res;
140
141         res = ast_unregister_application(app);
142         
143         STANDARD_HANGUP_LOCALUSERS;
144
145         return res;
146 }
147
148 int load_module(void)
149 {
150         return ast_register_application(app, pickup_exec, synopsis, descrip);
151 }
152
153 char *description(void)
154 {
155         return (char *) tdesc;
156 }
157
158 int usecount(void)
159 {
160         int res;
161
162         STANDARD_USECOUNT(res);
163
164         return res;
165 }
166
167 char *key()
168 {
169         return ASTERISK_GPL_KEY;
170 }