Don't do SIP contact/route DNS if we're not using the result.
[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 /*** MODULEINFO
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/app.h"
41 #include "asterisk/channel.h"
42
43 /*** DOCUMENTATION
44         <application name="Transfer" language="en_US">
45                 <synopsis>
46                         Transfer caller to remote extension.
47                 </synopsis>
48                 <syntax>
49                         <parameter name="dest" required="true" argsep="">
50                                 <argument name="Tech/" />
51                                 <argument name="destination" required="true" />
52                         </parameter>
53                 </syntax>
54                 <description>
55                         <para>Requests the remote caller be transferred
56                         to a given destination. If TECH (SIP, IAX2, LOCAL etc) is used, only
57                         an incoming call with the same channel technology will be transfered.
58                         Note that for SIP, if you transfer before call is setup, a 302 redirect
59                         SIP message will be returned to the caller.</para>
60                         <para>The result of the application will be reported in the <variable>TRANSFERSTATUS</variable>
61                         channel variable:</para>
62                         <variablelist>
63                                 <variable name="TRANSFERSTATUS">
64                                         <value name="SUCCESS">
65                                                 Transfer succeeded.
66                                         </value>
67                                         <value name="FAILURE">
68                                                 Transfer failed.
69                                         </value>
70                                         <value name="UNSUPPORTED">
71                                                 Transfer unsupported by channel driver.
72                                         </value>
73                                 </variable>
74                         </variablelist>
75                 </description>
76         </application>
77  ***/
78
79 static const char * const app = "Transfer";
80
81 static int transfer_exec(struct ast_channel *chan, const char *data)
82 {
83         int res;
84         int len;
85         char *slash;
86         char *tech = NULL;
87         char *dest = NULL;
88         char *status;
89         char *parse;
90         AST_DECLARE_APP_ARGS(args,
91                 AST_APP_ARG(dest);
92         );
93
94         if (ast_strlen_zero((char *)data)) {
95                 ast_log(LOG_WARNING, "Transfer requires an argument ([Tech/]destination)\n");
96                 pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "FAILURE");
97                 return 0;
98         } else
99                 parse = ast_strdupa(data);
100
101         AST_STANDARD_APP_ARGS(args, parse);
102
103         dest = args.dest;
104
105         if ((slash = strchr(dest, '/')) && (len = (slash - dest))) {
106                 tech = dest;
107                 dest = slash + 1;
108                 /* Allow execution only if the Tech/destination agrees with the type of the channel */
109                 if (strncasecmp(ast_channel_tech(chan)->type, tech, len)) {
110                         pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "FAILURE");
111                         return 0;
112                 }
113         }
114
115         /* Check if the channel supports transfer before we try it */
116         if (!ast_channel_tech(chan)->transfer) {
117                 pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "UNSUPPORTED");
118                 return 0;
119         }
120
121         res = ast_transfer(chan, dest);
122
123         if (res < 0) {
124                 status = "FAILURE";
125                 res = 0;
126         } else {
127                 status = "SUCCESS";
128                 res = 0;
129         }
130
131         pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", status);
132
133         return res;
134 }
135
136 static int unload_module(void)
137 {
138         return ast_unregister_application(app);
139 }
140
141 static int load_module(void)
142 {
143         return ast_register_application_xml(app, transfer_exec);
144 }
145
146 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Transfers a caller to another extension");