ensure that TRANSFERSTATUS can return FAILURE (issue #5146)
[asterisk/asterisk.git] / apps / app_transfer.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Transfer a caller
5  * 
6  * Copyright (C) 1999 - 2005, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17
18 #include "asterisk.h"
19
20 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
21
22 #include "asterisk/lock.h"
23 #include "asterisk/file.h"
24 #include "asterisk/logger.h"
25 #include "asterisk/channel.h"
26 #include "asterisk/pbx.h"
27 #include "asterisk/module.h"
28 #include "asterisk/options.h"
29
30 static const char *tdesc = "Transfer";
31
32 static const char *app = "Transfer";
33
34 static const char *synopsis = "Transfer caller to remote extension";
35
36 static const char *descrip = 
37 "  Transfer([Tech/]dest):  Requests the remote caller be transfered\n"
38 "to a given extension. If TECH (SIP, IAX2, LOCAL etc) is used, only\n"
39 "an incoming call with the same channel technology will be transfered.\n"
40 "Note that for SIP, if you transfer before call is setup, a 302 redirect\n"
41 "SIP message will be returned to the caller.\n"
42 "\nThe result of the application will be reported in the TRANSFERSTATUS\n"
43 "channel variable:\n"
44 "       SUCCESS       Transfer succeeded\n"
45 "       FAILURE      Transfer failed\n"
46 "       UNSUPPORTED  Transfer unsupported by channel driver\n"
47 "Returns -1 on hangup, or 0 on completion regardless of whether the\n"
48 "transfer was successful.\n\n"
49 "Old depraciated behaviour: If the transfer was *not* supported or\n"
50 "successful and there exists a priority n + 101,\n"
51 "then that priority will be taken next.\n" ;
52
53 STANDARD_LOCAL_USER;
54
55 LOCAL_USER_DECL;
56
57 static int transfer_exec(struct ast_channel *chan, void *data)
58 {
59         int res;
60         int len;
61         struct localuser *u;
62         char *slash;
63         char *tech = NULL;
64         char *dest = data;
65         char *status;
66
67         if (!dest || ast_strlen_zero(dest)) {
68                 ast_log(LOG_WARNING, "Transfer requires an argument ([Tech/]destination)\n");
69                 pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "FAILURE");
70                 return 0;
71         }
72
73         if ((slash = strchr(dest, '/')) && (len = (slash - dest))) {
74                 tech = dest;
75                 dest = slash + 1;
76                 /* Allow execution only if the Tech/destination agrees with the type of the channel */
77                 if (strncasecmp(chan->type, tech, len)) {
78                         pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "FAILURE");
79                         return 0;
80                 }
81         }
82
83         /* Check if the channel supports transfer before we try it */
84         if (!chan->tech->transfer) {
85                 pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "UNSUPPORTED");
86                 return 0;
87         }
88
89         LOCAL_USER_ADD(u);
90
91         res = ast_transfer(chan, dest);
92
93         if (res < 0) {
94                 status = "FAILURE";
95                 if (option_priority_jumping)
96                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
97                 res = 0;
98         } else {
99                 status = "SUCCESS";
100                 res = 0;
101         }
102
103         pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", status);
104
105         LOCAL_USER_REMOVE(u);
106
107         return res;
108 }
109
110 int unload_module(void)
111 {
112         STANDARD_HANGUP_LOCALUSERS;
113
114         return ast_unregister_application(app);
115 }
116
117 int load_module(void)
118 {
119         return ast_register_application(app, transfer_exec, synopsis, descrip);
120 }
121
122 char *description(void)
123 {
124         return (char *) tdesc;
125 }
126
127 int usecount(void)
128 {
129         int res;
130
131         STANDARD_USECOUNT(res);
132
133         return res;
134 }
135
136 char *key()
137 {
138         return ASTERISK_GPL_KEY;
139 }