4443a78744b7a5bce6394697bff3beabe68bb20c
[asterisk/asterisk.git] / apps / app_transfer.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@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 Transfer a caller
22  * 
23  * Requires transfer support from channel driver
24  *
25  * \ingroup applications
26  */
27
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/lock.h"
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/options.h"
43 #include "asterisk/app.h"
44
45 STANDARD_LOCAL_USER;
46
47 LOCAL_USER_DECL;
48
49 static const char *tdesc = "Transfer";
50
51 static const char *app = "Transfer";
52
53 static const char *synopsis = "Transfer caller to remote extension";
54
55 static const char *descrip = 
56 "  Transfer([Tech/]dest[|options]):  Requests the remote caller be transferred\n"
57 "to a given destination. If TECH (SIP, IAX2, LOCAL etc) is used, only\n"
58 "an incoming call with the same channel technology will be transfered.\n"
59 "Note that for SIP, if you transfer before call is setup, a 302 redirect\n"
60 "SIP message will be returned to the caller.\n"
61 "\nThe result of the application will be reported in the TRANSFERSTATUS\n"
62 "channel variable:\n"
63 "       SUCCESS      Transfer succeeded\n"
64 "       FAILURE      Transfer failed\n"
65 "       UNSUPPORTED  Transfer unsupported by channel driver\n"
66 "The option string many contain the following character:\n"
67 "'j' -- jump to n+101 priority if the channel transfer attempt\n"
68 "       fails\n";
69
70 static int transfer_exec(struct ast_channel *chan, void *data)
71 {
72         int res;
73         int len;
74         struct localuser *u;
75         char *slash;
76         char *tech = NULL;
77         char *dest = NULL;
78         char *status;
79         char *parse;
80         int priority_jump = 0;
81         AST_DECLARE_APP_ARGS(args,
82                 AST_APP_ARG(dest);
83                 AST_APP_ARG(options);
84         );
85
86         LOCAL_USER_ADD(u);
87
88         if (ast_strlen_zero((char *)data)) {
89                 ast_log(LOG_WARNING, "Transfer requires an argument ([Tech/]destination[|options])\n");
90                 LOCAL_USER_REMOVE(u);
91                 pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "FAILURE");
92                 return 0;
93         } else {
94                 parse = ast_strdupa(data);
95                 if (!parse) {
96                         ast_log(LOG_ERROR, "Out of memory!\n");
97                         LOCAL_USER_REMOVE(u);
98                         return -1;
99                 }
100         }
101
102         AST_STANDARD_APP_ARGS(args, parse);
103
104         if (args.options) {
105                 if (strchr(args.options, 'j'))
106                         priority_jump = 1;
107         }
108
109         dest = args.dest;
110
111         if ((slash = strchr(dest, '/')) && (len = (slash - dest))) {
112                 tech = dest;
113                 dest = slash + 1;
114                 /* Allow execution only if the Tech/destination agrees with the type of the channel */
115                 if (strncasecmp(chan->type, tech, len)) {
116                         pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "FAILURE");
117                         LOCAL_USER_REMOVE(u);
118                         return 0;
119                 }
120         }
121
122         /* Check if the channel supports transfer before we try it */
123         if (!chan->tech->transfer) {
124                 pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "UNSUPPORTED");
125                 LOCAL_USER_REMOVE(u);
126                 return 0;
127         }
128
129         res = ast_transfer(chan, dest);
130
131         if (res < 0) {
132                 status = "FAILURE";
133                 if (priority_jump || option_priority_jumping)
134                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
135                 res = 0;
136         } else {
137                 status = "SUCCESS";
138                 res = 0;
139         }
140
141         pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", status);
142
143         LOCAL_USER_REMOVE(u);
144
145         return res;
146 }
147
148 int unload_module(void)
149 {
150         int res;
151
152         res = ast_unregister_application(app);
153
154         STANDARD_HANGUP_LOCALUSERS;
155
156         return res;     
157 }
158
159 int load_module(void)
160 {
161         return ast_register_application(app, transfer_exec, synopsis, descrip);
162 }
163
164 char *description(void)
165 {
166         return (char *) tdesc;
167 }
168
169 int usecount(void)
170 {
171         int res;
172
173         STANDARD_USECOUNT(res);
174
175         return res;
176 }
177
178 char *key()
179 {
180         return ASTERISK_GPL_KEY;
181 }