Fix a typo in the non-DEBUG_THREADS version of the recently added DEADLOCK_AVOIDANCE()
[asterisk-bristuff.git] / apps / app_transfer.c
blobcda2914c701802f4cdec95c82464cbc540bb62f1
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
19 /*! \file
21 * \brief Transfer a caller
23 * \author Mark Spencer <markster@digium.com>
25 * Requires transfer support from channel driver
27 * \ingroup applications
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
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"
49 static const char *app = "Transfer";
51 static const char *synopsis = "Transfer caller to remote extension";
53 static const char *descrip =
54 " Transfer([Tech/]dest[|options]): Requests the remote caller be transferred\n"
55 "to a given destination. If TECH (SIP, IAX2, LOCAL etc) is used, only\n"
56 "an incoming call with the same channel technology will be transfered.\n"
57 "Note that for SIP, if you transfer before call is setup, a 302 redirect\n"
58 "SIP message will be returned to the caller.\n"
59 "\nThe result of the application will be reported in the TRANSFERSTATUS\n"
60 "channel variable:\n"
61 " SUCCESS Transfer succeeded\n"
62 " FAILURE Transfer failed\n"
63 " UNSUPPORTED Transfer unsupported by channel driver\n"
64 "The option string many contain the following character:\n"
65 "'j' -- jump to n+101 priority if the channel transfer attempt\n"
66 " fails\n";
68 static int transfer_exec(struct ast_channel *chan, void *data)
70 int res;
71 int len;
72 struct ast_module_user *u;
73 char *slash;
74 char *tech = NULL;
75 char *dest = NULL;
76 char *status;
77 char *parse;
78 int priority_jump = 0;
79 AST_DECLARE_APP_ARGS(args,
80 AST_APP_ARG(dest);
81 AST_APP_ARG(options);
84 u = ast_module_user_add(chan);
86 if (ast_strlen_zero((char *)data)) {
87 ast_log(LOG_WARNING, "Transfer requires an argument ([Tech/]destination[|options])\n");
88 ast_module_user_remove(u);
89 pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "FAILURE");
90 return 0;
91 } else
92 parse = ast_strdupa(data);
94 AST_STANDARD_APP_ARGS(args, parse);
96 if (args.options) {
97 if (strchr(args.options, 'j'))
98 priority_jump = 1;
101 dest = args.dest;
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(chan->tech->type, tech, len)) {
108 pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "FAILURE");
109 ast_module_user_remove(u);
110 return 0;
114 /* Check if the channel supports transfer before we try it */
115 if (!chan->tech->transfer) {
116 pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "UNSUPPORTED");
117 ast_module_user_remove(u);
118 return 0;
121 res = ast_transfer(chan, dest);
123 if (res < 0) {
124 status = "FAILURE";
125 if (priority_jump || ast_opt_priority_jumping)
126 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
127 res = 0;
128 } else {
129 status = "SUCCESS";
130 res = 0;
133 pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", status);
135 ast_module_user_remove(u);
137 return res;
140 static int unload_module(void)
142 int res;
144 res = ast_unregister_application(app);
146 ast_module_user_hangup_all();
148 return res;
151 static int load_module(void)
153 return ast_register_application(app, transfer_exec, synopsis, descrip);
156 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Transfer");