update the descriptions of a couple more apps
[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";
46 static const char *descrip =
47 "  Pickup(extension[@context]): This application can pickup any ringing channel\n"
48 "that is calling the specified extension. If no context is specified, the current\n"
49 "context will be used.\n";
50
51 STANDARD_LOCAL_USER;
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;
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         /* Get the extension and context if present */
71         exten = data;
72         context = strchr(data, '@');
73         if (context) {
74                 *context = '\0';
75                 context++;
76         }
77
78         /* Find a channel to pickup */
79         origin = ast_get_channel_by_exten_locked(exten, context);
80         if (origin) {
81                 ast_cdr_getvar(origin->cdr, "dstchannel", &tmp, workspace,
82                                sizeof(workspace), 0);
83                 if (tmp) {
84                         /* We have a possible channel... now we need to find it! */
85                         target = ast_get_channel_by_name_locked(tmp);
86                 } else {
87                         ast_log(LOG_DEBUG, "No target channel found.\n");
88                         res = -1;
89                 }
90                 ast_mutex_unlock(&origin->lock);
91         } else {
92                 ast_log(LOG_DEBUG, "No originating channel found.\n");
93         }
94         
95         if (res)
96                 goto out;
97
98         if (target && (!target->pbx) && ((target->_state == AST_STATE_RINGING) || (target->_state == AST_STATE_RING))) {
99                 ast_log(LOG_DEBUG, "Call pickup on chan '%s' by '%s'\n", target->name,
100                         chan->name);
101                 res = ast_answer(chan);
102                 if (res) {
103                         ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
104                         res = -1;
105                         goto out;
106                 }
107                 res = ast_queue_control(chan, AST_CONTROL_ANSWER);
108                 if (res) {
109                         ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n",
110                                 chan->name);
111                         res = -1;
112                         goto out;
113                 }
114                 res = ast_channel_masquerade(target, chan);
115                 if (res) {
116                         ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
117                         res = -1;
118                         goto out;
119                 }
120         } else {
121                 ast_log(LOG_DEBUG, "No call pickup possible...\n");
122                 res = -1;
123         }
124         /* Done */
125  out:
126         if (target) 
127                 ast_mutex_unlock(&target->lock);
128         
129         LOCAL_USER_REMOVE(u);
130
131         return res;
132 }
133
134 int unload_module(void)
135 {
136         int res;
137
138         res = ast_unregister_application(app);
139         
140         STANDARD_HANGUP_LOCALUSERS;
141
142         return res;
143 }
144
145 int load_module(void)
146 {
147         return ast_register_application(app, pickup_exec, synopsis, descrip);
148 }
149
150 char *description(void)
151 {
152         return (char *) tdesc;
153 }
154
155 int usecount(void)
156 {
157         int res;
158
159         STANDARD_USECOUNT(res);
160
161         return res;
162 }
163
164 char *key()
165 {
166         return ASTERISK_GPL_KEY;
167 }