app_softhangup.c 3.5 KB

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