Asterisk - The Open Source Telephony Project  18.5.0
app_transfer.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <[email protected]>
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 <[email protected]>
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, 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  <variable name="TRANSFERSTATUSPROTOCOL">
73  <value name="0">
74  No error.
75  </value>
76  <value name="3xx-6xx">
77  SIP example - Error result code.
78  </value>
79  </variable>
80  </variablelist>
81  </description>
82  </application>
83  ***/
84 
85 static const char * const app = "Transfer";
86 
87 static int transfer_exec(struct ast_channel *chan, const char *data)
88 {
89  int res;
90  int len;
91  char *slash;
92  char *tech = NULL;
93  char *dest = NULL;
94  char *status;
95  char *parse;
96  int protocol = 0;
97  char status_protocol[20];
99  AST_APP_ARG(dest);
100  );
101 
102  if (ast_strlen_zero((char *)data)) {
103  ast_log(LOG_WARNING, "Transfer requires an argument ([Tech/]destination)\n");
104  pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "FAILURE");
105  snprintf(status_protocol, sizeof(status_protocol), "%d", protocol);
106  pbx_builtin_setvar_helper(chan, "TRANSFERSTATUSPROTOCOL", status_protocol);
107  return 0;
108  } else
109  parse = ast_strdupa(data);
110 
111  AST_STANDARD_APP_ARGS(args, parse);
112 
113  dest = args.dest;
114 
115  if ((slash = strchr(dest, '/')) && (len = (slash - dest))) {
116  tech = dest;
117  dest = slash + 1;
118  /* Allow execution only if the Tech/destination agrees with the type of the channel */
119  if (strncasecmp(ast_channel_tech(chan)->type, tech, len)) {
120  pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "FAILURE");
121  snprintf(status_protocol, sizeof(status_protocol), "%d", protocol);
122  pbx_builtin_setvar_helper(chan, "TRANSFERSTATUSPROTOCOL", status_protocol);
123  return 0;
124  }
125  }
126 
127  /* Check if the channel supports transfer before we try it */
128  if (!ast_channel_tech(chan)->transfer) {
129  pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "UNSUPPORTED");
130  snprintf(status_protocol, sizeof(status_protocol), "%d", protocol);
131  pbx_builtin_setvar_helper(chan, "TRANSFERSTATUSPROTOCOL", status_protocol);
132  return 0;
133  }
134 
135  /* New transfer API returns a protocol code
136  SIP example, 0 = success, 3xx-6xx are sip error codes for the REFER */
137  res = ast_transfer_protocol(chan, dest, &protocol);
138 
139  if (res < 0) {
140  status = "FAILURE";
141  res = 0;
142  } else {
143  status = "SUCCESS";
144  res = 0;
145  }
146 
147  snprintf(status_protocol, sizeof(status_protocol), "%d", protocol);
148  ast_debug(1, "ast_transfer channel %s TRANSFERSTATUS=%s, TRANSFERSTATUSPROTOCOL=%s\n",
149  ast_channel_name(chan), status, status_protocol);
150  pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", status);
151  pbx_builtin_setvar_helper(chan, "TRANSFERSTATUSPROTOCOL", status_protocol);
152 
153  return res;
154 }
155 
156 static int unload_module(void)
157 {
159 }
160 
161 static int load_module(void)
162 {
164 }
165 
166 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Transfers a caller to another extension");
static const char type[]
Definition: chan_ooh323.c:109
Main Channel structure associated with a channel.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
static const char *const app
Definition: app_transfer.c:85
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
const char * args
#define NULL
Definition: resample.c:96
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
static int transfer
Definition: chan_mgcp.c:194
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
Core PBX routines and definitions.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int transfer_exec(struct ast_channel *chan, const char *data)
Definition: app_transfer.c:87
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
static int load_module(void)
Definition: app_transfer.c:161
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
const char * ast_channel_name(const struct ast_channel *chan)
int ast_transfer_protocol(struct ast_channel *chan, char *dest, int *protocol)
Transfer a channel (if supported) receieve protocol result.
Definition: channel.c:6595
static int unload_module(void)
Definition: app_transfer.c:156
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
const struct ast_channel_tech * ast_channel_tech(const struct ast_channel *chan)
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626
jack_status_t status
Definition: app_jack.c:146
#define AST_APP_ARG(name)
Define an application argument.