53f39649fb3806ea31abe7ebb9349ba0260326f4
[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 /*
20  *
21  * Directed Call Pickup Support
22  *
23  */
24
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include "asterisk/file.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/lock.h"
39 #include "asterisk/app.h"
40
41 static const char *tdesc = "Directed Call Pickup Application";
42 static const char *app = "Pickup";
43 static const char *synopsis = "Directed Call Pickup application.";
44 static const char *descrip =
45 " Pickup(extension@context):\n"
46 "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";
47
48 STANDARD_LOCAL_USER;
49
50 LOCAL_USER_DECL;
51
52 static int pickup_exec(struct ast_channel *chan, void *data)
53 {
54         int res = -1;
55         struct localuser *u = NULL;
56         struct ast_channel *origin = NULL, *target = NULL;
57         char *tmp = NULL, *exten = NULL, *context = NULL;
58         char workspace[256] = "";
59
60         /* Get the extension and context if present */
61         exten = data;
62         context = strchr(data, '@');
63         if (context) {
64                 *context = '\0';
65                 context++;
66         }
67
68         /* Make sure we atleast have an extension to work with */
69         if (!exten) {
70                 ast_log(LOG_WARNING, "%s requires atleast one argument (extension)\n", app);
71                 return -1;
72         }
73
74         LOCAL_USER_ADD(u);
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                 /* Done */
119                 ast_mutex_unlock(&target->lock);
120         } else {
121                 ast_log(LOG_DEBUG, "No call pickup possible...\n");
122                 res = -1;
123         }
124         
125  out:
126         LOCAL_USER_REMOVE(u);
127
128         return res;
129 }
130
131 int unload_module(void)
132 {
133         STANDARD_HANGUP_LOCALUSERS;
134
135         return ast_unregister_application(app);
136 }
137
138 int load_module(void)
139 {
140         return ast_register_application(app, pickup_exec, synopsis, descrip);
141 }
142
143 char *description(void)
144 {
145         return (char *) tdesc;
146 }
147
148 int usecount(void)
149 {
150         int res;
151
152         STANDARD_USECOUNT(res);
153
154         return res;
155 }
156
157 char *key()
158 {
159         return ASTERISK_GPL_KEY;
160 }