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