app_softhangup.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include "asterisk/file.h"
  29. #include "asterisk/channel.h"
  30. #include "asterisk/pbx.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/lock.h"
  33. #include "asterisk/app.h"
  34. static char *synopsis = "Soft Hangup Application";
  35. static char *desc = " SoftHangup(Technology/resource[,options]):\n"
  36. "Hangs up the requested channel. If there are no channels to hangup,\n"
  37. "the application will report it.\n"
  38. " Options:\n"
  39. " 'a' - hang up all channels on a specified device instead of a single resource\n";
  40. static char *app = "SoftHangup";
  41. enum {
  42. OPTION_ALL = (1 << 0),
  43. };
  44. AST_APP_OPTIONS(app_opts,{
  45. AST_APP_OPTION('a', OPTION_ALL),
  46. });
  47. static int softhangup_exec(struct ast_channel *chan, void *data)
  48. {
  49. struct ast_channel *c = NULL;
  50. char *cut, *opts[0];
  51. char name[AST_CHANNEL_NAME] = "", *parse;
  52. struct ast_flags flags = {0};
  53. int lenmatch;
  54. AST_DECLARE_APP_ARGS(args,
  55. AST_APP_ARG(channel);
  56. AST_APP_ARG(options);
  57. );
  58. if (ast_strlen_zero(data)) {
  59. ast_log(LOG_WARNING, "SoftHangup requires an argument (Technology/resource)\n");
  60. return 0;
  61. }
  62. parse = ast_strdupa(data);
  63. AST_STANDARD_APP_ARGS(args, parse);
  64. if (args.argc == 2)
  65. ast_app_parse_options(app_opts, &flags, opts, args.options);
  66. lenmatch = strlen(args.channel);
  67. for (c = ast_walk_channel_by_name_prefix_locked(NULL, args.channel, lenmatch);
  68. c;
  69. c = ast_walk_channel_by_name_prefix_locked(c, args.channel, lenmatch)) {
  70. ast_copy_string(name, c->name, sizeof(name));
  71. if (ast_test_flag(&flags, OPTION_ALL)) {
  72. /* CAPI is set up like CAPI[foo/bar]/clcnt */
  73. if (!strcmp(c->tech->type, "CAPI")) {
  74. cut = strrchr(name, '/');
  75. /* Basically everything else is Foo/Bar-Z */
  76. } else {
  77. /* use strrchr() because Foo/Bar-Z could actually be Foo/B-a-r-Z */
  78. cut = strrchr(name,'-');
  79. }
  80. /* Get rid of what we've cut */
  81. if (cut)
  82. *cut = 0;
  83. }
  84. if (!strcasecmp(name, args.channel)) {
  85. ast_log(LOG_WARNING, "Soft hanging %s up.\n", c->name);
  86. ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
  87. if (!ast_test_flag(&flags, OPTION_ALL)) {
  88. ast_channel_unlock(c);
  89. break;
  90. }
  91. }
  92. ast_channel_unlock(c);
  93. }
  94. return 0;
  95. }
  96. static int unload_module(void)
  97. {
  98. return ast_unregister_application(app);
  99. }
  100. static int load_module(void)
  101. {
  102. return ast_register_application(app, softhangup_exec, synopsis, desc);
  103. }
  104. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hangs up the requested channel");