app_pickupchan.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008, Gary Cook
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief Pickup a ringing channel
  19. *
  20. * \author Gary Cook
  21. *
  22. * \ingroup applications
  23. */
  24. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include "asterisk/file.h"
  31. #include "asterisk/logger.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/lock.h"
  36. #include "asterisk/app.h"
  37. #include "asterisk/options.h"
  38. static const char *app = "PickupChan";
  39. static const char *synopsis = "Pickup a ringing channel";
  40. static const char *descrip =
  41. " PickupChan(channel[&channel...]): This application can pickup any ringing channel\n";
  42. /*! \todo This application should return a result code, like PICKUPRESULT */
  43. /*! \brief Helper function that determines whether a channel is capable of being picked up */
  44. static int can_pickup(struct ast_channel *chan)
  45. {
  46. ast_debug(3, "Checking Pickup '%s' state '%s ( %d )'\n", chan->name, ast_state2str(chan->_state), chan->_state);
  47. if (!chan->pbx && (chan->_state == AST_STATE_RINGING || chan->_state == AST_STATE_RING)) {
  48. return 1;
  49. } else {
  50. return 0;
  51. }
  52. }
  53. /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
  54. static struct ast_channel *my_ast_get_channel_by_name_locked(char *channame)
  55. {
  56. struct ast_channel *chan;
  57. char *chkchan = alloca(strlen(channame) + 2);
  58. /* need to append a '-' for the comparison so we check full channel name,
  59. * i.e SIP/hgc- , use a temporary variable so original stays the same for
  60. * debugging.
  61. */
  62. strcpy(chkchan, channame);
  63. strcat(chkchan, "-");
  64. for (chan = ast_walk_channel_by_name_prefix_locked(NULL, channame, strlen(channame));
  65. chan;
  66. chan = ast_walk_channel_by_name_prefix_locked(chan, channame, strlen(channame))) {
  67. if (!strncasecmp(chan->name, chkchan, strlen(chkchan)) && can_pickup(chan))
  68. return chan;
  69. ast_channel_unlock(chan);
  70. }
  71. return NULL;
  72. }
  73. /*! \brief Perform actual pickup between two channels */
  74. static int pickup_do(struct ast_channel *chan, struct ast_channel *target)
  75. {
  76. int res = 0;
  77. ast_debug(3, "Call pickup on '%s' by '%s'\n", target->name, chan->name);
  78. if ((res = ast_answer(chan))) {
  79. ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
  80. return -1;
  81. }
  82. if ((res = ast_queue_control(chan, AST_CONTROL_ANSWER))) {
  83. ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
  84. return -1;
  85. }
  86. if ((res = ast_channel_masquerade(target, chan))) {
  87. ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, target->name);
  88. return -1;
  89. }
  90. return res;
  91. }
  92. /*! \brief Attempt to pick up specified channel named , does not use context */
  93. static int pickup_by_channel(struct ast_channel *chan, char *pickup)
  94. {
  95. int res = 0;
  96. struct ast_channel *target;
  97. if (!(target = my_ast_get_channel_by_name_locked(pickup)))
  98. return -1;
  99. /* Just check that we are not picking up the SAME as target */
  100. if (chan->name != target->name && chan != target) {
  101. res = pickup_do(chan, target);
  102. ast_channel_unlock(target);
  103. }
  104. return res;
  105. }
  106. /*! \brief Main application entry point */
  107. static int pickupchan_exec(struct ast_channel *chan, void *data)
  108. {
  109. int res = 0;
  110. struct ast_module_user *u = NULL;
  111. char *tmp = ast_strdupa(data);
  112. char *pickup = NULL, *context = NULL;
  113. if (ast_strlen_zero(data)) {
  114. ast_log(LOG_WARNING, "Pickup requires an argument (channel)!\n");
  115. return -1;
  116. }
  117. u = ast_module_user_add(chan);
  118. /* Parse channel (and ignore context if there) */
  119. while (!ast_strlen_zero(tmp) && (pickup = strsep(&tmp, "&"))) {
  120. if ((context = strchr(pickup , '@'))) {
  121. *context++ = '\0';
  122. }
  123. if (!strncasecmp(chan->name, pickup , strlen(pickup))) {
  124. ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
  125. } else {
  126. if (!pickup_by_channel(chan, pickup)) {
  127. break;
  128. }
  129. ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
  130. }
  131. }
  132. ast_module_user_remove(u);
  133. return res;
  134. }
  135. static int unload_module(void)
  136. {
  137. return ast_unregister_application(app);
  138. }
  139. static int load_module(void)
  140. {
  141. return ast_register_application(app, pickupchan_exec, synopsis, descrip);
  142. }
  143. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel Pickup Application");