app_transfer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /*! \file
  19. *
  20. * \brief Transfer a caller
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * Requires transfer support from channel driver
  25. *
  26. * \ingroup applications
  27. */
  28. /*** MODULEINFO
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/app.h"
  35. #include "asterisk/channel.h"
  36. /*** DOCUMENTATION
  37. <application name="Transfer" language="en_US">
  38. <synopsis>
  39. Transfer caller to remote extension.
  40. </synopsis>
  41. <syntax>
  42. <parameter name="dest" required="true" argsep="">
  43. <argument name="Tech/" />
  44. <argument name="destination" required="true" />
  45. </parameter>
  46. </syntax>
  47. <description>
  48. <para>Requests the remote caller be transferred
  49. to a given destination. If TECH (SIP, IAX2, etc) is used, only
  50. an incoming call with the same channel technology will be transferred.
  51. Note that for SIP, if you transfer before call is setup, a 302 redirect
  52. SIP message will be returned to the caller.</para>
  53. <para>The result of the application will be reported in the <variable>TRANSFERSTATUS</variable>
  54. channel variable:</para>
  55. <variablelist>
  56. <variable name="TRANSFERSTATUS">
  57. <value name="SUCCESS">
  58. Transfer succeeded.
  59. </value>
  60. <value name="FAILURE">
  61. Transfer failed.
  62. </value>
  63. <value name="UNSUPPORTED">
  64. Transfer unsupported by channel driver.
  65. </value>
  66. </variable>
  67. <variable name="TRANSFERSTATUSPROTOCOL">
  68. <value name="0">
  69. No error.
  70. </value>
  71. <value name="3xx-6xx">
  72. SIP example - Error result code.
  73. </value>
  74. </variable>
  75. </variablelist>
  76. </description>
  77. </application>
  78. ***/
  79. static const char * const app = "Transfer";
  80. static int transfer_exec(struct ast_channel *chan, const char *data)
  81. {
  82. int res;
  83. int len;
  84. char *slash;
  85. char *tech = NULL;
  86. char *dest = NULL;
  87. char *status;
  88. char *parse;
  89. int protocol = 0;
  90. char status_protocol[20];
  91. AST_DECLARE_APP_ARGS(args,
  92. AST_APP_ARG(dest);
  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. snprintf(status_protocol, sizeof(status_protocol), "%d", protocol);
  98. pbx_builtin_setvar_helper(chan, "TRANSFERSTATUSPROTOCOL", status_protocol);
  99. return 0;
  100. } else
  101. parse = ast_strdupa(data);
  102. AST_STANDARD_APP_ARGS(args, parse);
  103. dest = args.dest;
  104. if ((slash = strchr(dest, '/')) && (len = (slash - dest))) {
  105. tech = dest;
  106. dest = slash + 1;
  107. /* Allow execution only if the Tech/destination agrees with the type of the channel */
  108. if (strncasecmp(ast_channel_tech(chan)->type, tech, len)) {
  109. pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", "FAILURE");
  110. snprintf(status_protocol, sizeof(status_protocol), "%d", protocol);
  111. pbx_builtin_setvar_helper(chan, "TRANSFERSTATUSPROTOCOL", status_protocol);
  112. return 0;
  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. snprintf(status_protocol, sizeof(status_protocol), "%d", protocol);
  119. pbx_builtin_setvar_helper(chan, "TRANSFERSTATUSPROTOCOL", status_protocol);
  120. return 0;
  121. }
  122. /* New transfer API returns a protocol code
  123. SIP example, 0 = success, 3xx-6xx are sip error codes for the REFER */
  124. res = ast_transfer_protocol(chan, dest, &protocol);
  125. if (res < 0) {
  126. status = "FAILURE";
  127. res = 0;
  128. } else {
  129. status = "SUCCESS";
  130. res = 0;
  131. }
  132. snprintf(status_protocol, sizeof(status_protocol), "%d", protocol);
  133. ast_debug(1, "ast_transfer channel %s TRANSFERSTATUS=%s, TRANSFERSTATUSPROTOCOL=%s\n",
  134. ast_channel_name(chan), status, status_protocol);
  135. pbx_builtin_setvar_helper(chan, "TRANSFERSTATUS", status);
  136. pbx_builtin_setvar_helper(chan, "TRANSFERSTATUSPROTOCOL", status_protocol);
  137. return res;
  138. }
  139. static int unload_module(void)
  140. {
  141. return ast_unregister_application(app);
  142. }
  143. static int load_module(void)
  144. {
  145. return ast_register_application_xml(app, transfer_exec);
  146. }
  147. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Transfers a caller to another extension");