app_softhangup.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 SoftHangup application
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/file.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. /*** DOCUMENTATION
  38. <application name="SoftHangup" language="en_US">
  39. <synopsis>
  40. Hangs up the requested channel.
  41. </synopsis>
  42. <syntax>
  43. <parameter name="Technology/Resource" required="true" />
  44. <parameter name="options">
  45. <optionlist>
  46. <option name="a">
  47. <para>Hang up all channels on a specified device instead of a single resource</para>
  48. </option>
  49. </optionlist>
  50. </parameter>
  51. </syntax>
  52. <description>
  53. <para>Hangs up the requested channel. If there are no channels to
  54. hangup, the application will report it.</para>
  55. </description>
  56. </application>
  57. ***/
  58. static char *app = "SoftHangup";
  59. enum {
  60. OPTION_ALL = (1 << 0),
  61. };
  62. AST_APP_OPTIONS(app_opts,{
  63. AST_APP_OPTION('a', OPTION_ALL),
  64. });
  65. static int softhangup_exec(struct ast_channel *chan, const char *data)
  66. {
  67. struct ast_channel *c = NULL;
  68. char *cut, *opts[0];
  69. char name[AST_CHANNEL_NAME] = "", *parse;
  70. struct ast_flags flags = {0};
  71. int lenmatch;
  72. AST_DECLARE_APP_ARGS(args,
  73. AST_APP_ARG(channel);
  74. AST_APP_ARG(options);
  75. );
  76. struct ast_channel_iterator *iter;
  77. if (ast_strlen_zero(data)) {
  78. ast_log(LOG_WARNING, "SoftHangup requires an argument (Technology/resource)\n");
  79. return 0;
  80. }
  81. parse = ast_strdupa(data);
  82. AST_STANDARD_APP_ARGS(args, parse);
  83. if (args.argc == 2)
  84. ast_app_parse_options(app_opts, &flags, opts, args.options);
  85. lenmatch = strlen(args.channel);
  86. if (!(iter = ast_channel_iterator_by_name_new(args.channel, lenmatch))) {
  87. return -1;
  88. }
  89. while ((c = ast_channel_iterator_next(iter))) {
  90. ast_channel_lock(c);
  91. ast_copy_string(name, ast_channel_name(c), sizeof(name));
  92. if (ast_test_flag(&flags, OPTION_ALL)) {
  93. /* CAPI is set up like CAPI[foo/bar]/clcnt */
  94. if (!strcmp(ast_channel_tech(c)->type, "CAPI")) {
  95. cut = strrchr(name, '/');
  96. /* Basically everything else is Foo/Bar-Z */
  97. } else {
  98. /* use strrchr() because Foo/Bar-Z could actually be Foo/B-a-r-Z */
  99. cut = strrchr(name,'-');
  100. }
  101. /* Get rid of what we've cut */
  102. if (cut)
  103. *cut = 0;
  104. }
  105. if (!strcasecmp(name, args.channel)) {
  106. ast_log(LOG_WARNING, "Soft hanging %s up.\n", ast_channel_name(c));
  107. ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
  108. if (!ast_test_flag(&flags, OPTION_ALL)) {
  109. ast_channel_unlock(c);
  110. c = ast_channel_unref(c);
  111. break;
  112. }
  113. }
  114. ast_channel_unlock(c);
  115. c = ast_channel_unref(c);
  116. }
  117. ast_channel_iterator_destroy(iter);
  118. return 0;
  119. }
  120. static int unload_module(void)
  121. {
  122. return ast_unregister_application(app);
  123. }
  124. static int load_module(void)
  125. {
  126. return ast_register_application_xml(app, softhangup_exec);
  127. }
  128. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hangs up the requested channel");