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