This rather large commit changes the way modules are loaded.
[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@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 *app = "Pickup";
46 static const char *synopsis = "Directed Call Pickup";
47 static const char *descrip =
48 "  Pickup(extension[@context][&extension2@context...]): This application can pickup any ringing channel\n"
49 "that is calling the specified extension. If no context is specified, the current\n"
50 "context will be used.\n";
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, *rest=data;
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         while (!target && (exten = rest) ) {
70                 res = 0;
71                 rest = strchr(exten, '&');
72                 if (rest)
73                         *rest++ = 0;
74
75                 /* Get the extension and context if present */
76                 context = strchr(exten, '@');
77                 if (context)
78                         *context++ = '\0';
79
80                 /* Find a channel to pickup */
81                 origin = ast_get_channel_by_exten_locked(exten, context);
82                 if (origin) {
83                         ast_cdr_getvar(origin->cdr, "dstchannel", &tmp, workspace,
84                                         sizeof(workspace), 0, 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_NOTICE, "No target channel found for %s.\n", exten);
90                                 res = -1;
91                         }
92                         ast_mutex_unlock(&origin->lock);
93
94                 } else {
95                         ast_log(LOG_DEBUG, "No originating channel found.\n");
96                 }
97
98                 if (res)
99                         continue;
100
101                 if (target && (!target->pbx) && ((target->_state == AST_STATE_RINGING) || (target->_state == AST_STATE_RING) ) ) {
102                         ast_log(LOG_DEBUG, "Call pickup on chan '%s' by '%s'\n", target->name,
103                                         chan->name);
104                         res = ast_answer(chan);
105                         if (res) {
106                                 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
107                                 res = -1;
108                                 break;
109                         }
110                         res = ast_queue_control(chan, AST_CONTROL_ANSWER);
111                         if (res) {
112                                 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n",
113                                                 chan->name);
114                                 res = -1;
115                                 break;
116                         }
117                         res = ast_channel_masquerade(target, chan);
118                         if (res) {
119                                 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
120                                 res = -1;
121                                 break;
122                         }
123                 } else {
124                         ast_log(LOG_NOTICE, "No call pickup possible for %s...\n", exten);
125                         res = -1;
126                 }
127         }
128         if (target) 
129                 ast_mutex_unlock(&target->lock);
130         
131         LOCAL_USER_REMOVE(u);
132
133         return res;
134 }
135
136 static int unload_module(void *mod)
137 {
138         int res;
139
140         res = ast_unregister_application(app);
141         
142         return res;
143 }
144
145 static int load_module(void *mod)
146 {
147         __mod_desc = mod;
148         return ast_register_application(app, pickup_exec, synopsis, descrip);
149 }
150
151 static const char *description(void)
152 {
153         return "Directed Call Pickup Application";
154 }
155
156 static const char *key(void)
157 {
158         return ASTERISK_GPL_KEY;
159 }
160
161 STD_MOD(MOD_1, NULL, NULL, NULL);