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