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