bug #8076 check option_debug before printing to debug channel.
[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@digium.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@digium.com>
24  *
25  * \ingroup applications
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
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 #include "asterisk/options.h"
45
46 #define PICKUPMARK "PICKUPMARK"
47
48 static const char *app = "Pickup";
49 static const char *synopsis = "Directed Call Pickup";
50 static const char *descrip =
51 "  Pickup(extension[@context][&extension2@context...]): This application can pickup any ringing channel\n"
52 "that is calling the specified extension. If no context is specified, the current\n"
53 "context will be used. If you use the special string \"PICKUPMARK\" for the context parameter, for example\n"
54 "10@PICKUPMARK, this application tries to find a channel which has defined a channel variable with the same context\n"
55 "as \"extension\".";
56
57
58 static int pickup_exec(struct ast_channel *chan, void *data)
59 {
60         int res = 0;
61         struct ast_module_user *u = NULL;
62         struct ast_channel *origin = NULL, *target = NULL;
63         char *tmp = NULL, *exten = NULL, *context = NULL, *rest=data;
64         char workspace[256] = "";
65         const char *tmp2 = NULL;
66
67         if (ast_strlen_zero(data)) {
68                 ast_log(LOG_WARNING, "Pickup requires an argument (extension) !\n");
69                 return -1;      
70         }
71
72         u = ast_module_user_add(chan);
73         
74         while (!target && (exten = rest) ) {
75                 res = 0;
76                 rest = strchr(exten, '&');
77                 if (rest)
78                         *rest++ = 0;
79
80                 /* Get the extension and context if present */
81                 context = strchr(exten, '@');
82                 if (context)
83                         *context++ = '\0';
84
85                 /* If the context is the pickup mark, iterate through all channels finding the right origin one */
86                 if (context && !strcmp(context, PICKUPMARK)) {
87                         while ((origin = ast_channel_walk_locked(origin))) {
88                                 if (origin) {
89                                         tmp2 = pbx_builtin_getvar_helper(origin, PICKUPMARK);
90                                         if (tmp2 && !strcmp(tmp2, exten))
91                                                 break;
92                                         ast_mutex_unlock(&origin->lock);
93                                 }
94                         }
95                 } else {
96                         /* Use the classic mode of searching */
97                         origin = ast_get_channel_by_exten_locked(exten, context);
98                 }
99
100                 if (origin) {
101                         ast_cdr_getvar(origin->cdr, "dstchannel", &tmp, workspace,
102                                         sizeof(workspace), 0, 0);
103                         if (tmp) {
104                                 /* We have a possible channel... now we need to find it! */
105                                 target = ast_get_channel_by_name_locked(tmp);
106                         } else {
107                                 ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
108                                 res = -1;
109                         }
110                         ast_mutex_unlock(&origin->lock);
111
112                 } else {
113                         if (option_debug)
114                                 ast_log(LOG_DEBUG, "No originating channel found.\n");
115                 }
116
117                 if (res)
118                         continue;
119
120                 if (target && (!target->pbx) && ((target->_state == AST_STATE_RINGING) || (target->_state == AST_STATE_RING) ) ) {
121                         if (option_debug)
122                                 ast_log(LOG_DEBUG, "Call pickup on chan '%s' by '%s'\n", target->name,
123                                         chan->name);
124                         res = ast_answer(chan);
125                         if (res) {
126                                 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
127                                 res = -1;
128                                 break;
129                         }
130                         res = ast_queue_control(chan, AST_CONTROL_ANSWER);
131                         if (res) {
132                                 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n",
133                                                 chan->name);
134                                 res = -1;
135                                 break;
136                         }
137                         res = ast_channel_masquerade(target, chan);
138                         if (res) {
139                                 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
140                                 res = -1;
141                                 break;
142                         }
143                 } else {
144                         ast_log(LOG_NOTICE, "No call pickup possible for %s...\n", exten);
145                         res = -1;
146                 }
147         }
148         if (target) 
149                 ast_mutex_unlock(&target->lock);
150         
151         ast_module_user_remove(u);
152
153         return res;
154 }
155
156 static int unload_module(void)
157 {
158         int res;
159
160         res = ast_unregister_application(app);
161         
162         return res;
163 }
164
165 static int load_module(void)
166 {
167         return ast_register_application(app, pickup_exec, synopsis, descrip);
168 }
169
170 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");