update doxygen docs to specify authors
[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]): 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;
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         /* Get the extension and context if present */
73         exten = data;
74         context = strchr(data, '@');
75         if (context) {
76                 *context = '\0';
77                 context++;
78         }
79
80         /* Find a channel to pickup */
81         origin = ast_get_channel_by_exten_locked(exten, context);
82         if (origin && origin->cdr) {
83                 ast_cdr_getvar(origin->cdr, "dstchannel", &tmp, workspace,
84                                sizeof(workspace), 0);
85                 if (tmp) {
86                         /* We have a possible channel... now we need to find it! */
87                         target = ast_get_channel_by_name_locked(tmp);
88                 } else {
89                         ast_log(LOG_DEBUG, "No target channel found.\n");
90                         res = -1;
91                 }
92                 ast_mutex_unlock(&origin->lock);
93         } else {
94                 if (origin)
95                         ast_mutex_unlock(&origin->lock);
96                 ast_log(LOG_DEBUG, "No originating channel found.\n");
97         }
98         
99         if (res)
100                 goto out;
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                         goto out;
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                         goto out;
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                         goto out;
123                 }
124         } else {
125                 ast_log(LOG_DEBUG, "No call pickup possible...\n");
126                 res = -1;
127         }
128         /* Done */
129  out:
130         if (target) 
131                 ast_mutex_unlock(&target->lock);
132         
133         LOCAL_USER_REMOVE(u);
134
135         return res;
136 }
137
138 int unload_module(void)
139 {
140         int res;
141
142         res = ast_unregister_application(app);
143         
144         STANDARD_HANGUP_LOCALUSERS;
145
146         return res;
147 }
148
149 int load_module(void)
150 {
151         return ast_register_application(app, pickup_exec, synopsis, descrip);
152 }
153
154 char *description(void)
155 {
156         return (char *) tdesc;
157 }
158
159 int usecount(void)
160 {
161         int res;
162
163         STANDARD_USECOUNT(res);
164
165         return res;
166 }
167
168 char *key()
169 {
170         return ASTERISK_GPL_KEY;
171 }