Fix segfault on directed pickup when no CDR is available (bug #5998)
[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 && origin->cdr) {
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                 if (origin)
93                         ast_mutex_unlock(&origin->lock);
94                 ast_log(LOG_DEBUG, "No originating channel found.\n");
95         }
96         
97         if (res)
98                 goto out;
99
100         if (target && (!target->pbx) && ((target->_state == AST_STATE_RINGING) || (target->_state == AST_STATE_RING))) {
101                 ast_log(LOG_DEBUG, "Call pickup on chan '%s' by '%s'\n", target->name,
102                         chan->name);
103                 res = ast_answer(chan);
104                 if (res) {
105                         ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
106                         res = -1;
107                         goto out;
108                 }
109                 res = ast_queue_control(chan, AST_CONTROL_ANSWER);
110                 if (res) {
111                         ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n",
112                                 chan->name);
113                         res = -1;
114                         goto out;
115                 }
116                 res = ast_channel_masquerade(target, chan);
117                 if (res) {
118                         ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
119                         res = -1;
120                         goto out;
121                 }
122         } else {
123                 ast_log(LOG_DEBUG, "No call pickup possible...\n");
124                 res = -1;
125         }
126         /* Done */
127  out:
128         if (target) 
129                 ast_mutex_unlock(&target->lock);
130         
131         LOCAL_USER_REMOVE(u);
132
133         return res;
134 }
135
136 int unload_module(void)
137 {
138         int res;
139
140         res = ast_unregister_application(app);
141         
142         STANDARD_HANGUP_LOCALUSERS;
143
144         return res;
145 }
146
147 int load_module(void)
148 {
149         return ast_register_application(app, pickup_exec, synopsis, descrip);
150 }
151
152 char *description(void)
153 {
154         return (char *) tdesc;
155 }
156
157 int usecount(void)
158 {
159         int res;
160
161         STANDARD_USECOUNT(res);
162
163         return res;
164 }
165
166 char *key()
167 {
168         return ASTERISK_GPL_KEY;
169 }