pbx_hangup_handler.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2016, CFWare, LLC
  5. *
  6. * Corey Farrell <git@cfware.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 PBX Hangup Handler management routines.
  21. *
  22. * \author Corey Farrell <git@cfware.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/_private.h"
  30. #include "asterisk/app.h"
  31. #include "asterisk/cli.h"
  32. #include "asterisk/linkedlists.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/stasis_channels.h"
  35. #include "asterisk/utils.h"
  36. /*!
  37. * \internal
  38. * \brief Publish a hangup handler related message to \ref stasis
  39. */
  40. static void publish_hangup_handler_message(const char *action, struct ast_channel *chan, const char *handler)
  41. {
  42. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  43. blob = ast_json_pack("{s: s, s: s}",
  44. "type", action,
  45. "handler", S_OR(handler, ""));
  46. if (!blob) {
  47. return;
  48. }
  49. ast_channel_publish_blob(chan, ast_channel_hangup_handler_type(), blob);
  50. }
  51. int ast_pbx_hangup_handler_run(struct ast_channel *chan)
  52. {
  53. struct ast_hangup_handler_list *handlers;
  54. struct ast_hangup_handler *h_handler;
  55. ast_channel_lock(chan);
  56. handlers = ast_channel_hangup_handlers(chan);
  57. if (AST_LIST_EMPTY(handlers)) {
  58. ast_channel_unlock(chan);
  59. return 0;
  60. }
  61. /*
  62. * Make sure that the channel is marked as hungup since we are
  63. * going to run the hangup handlers on it.
  64. */
  65. ast_softhangup_nolock(chan, AST_SOFTHANGUP_HANGUP_EXEC);
  66. for (;;) {
  67. handlers = ast_channel_hangup_handlers(chan);
  68. h_handler = AST_LIST_REMOVE_HEAD(handlers, node);
  69. if (!h_handler) {
  70. break;
  71. }
  72. publish_hangup_handler_message("run", chan, h_handler->args);
  73. ast_channel_unlock(chan);
  74. ast_app_exec_sub(NULL, chan, h_handler->args, 1);
  75. ast_free(h_handler);
  76. ast_channel_lock(chan);
  77. }
  78. ast_channel_unlock(chan);
  79. return 1;
  80. }
  81. void ast_pbx_hangup_handler_init(struct ast_channel *chan)
  82. {
  83. struct ast_hangup_handler_list *handlers;
  84. handlers = ast_channel_hangup_handlers(chan);
  85. AST_LIST_HEAD_INIT_NOLOCK(handlers);
  86. }
  87. void ast_pbx_hangup_handler_destroy(struct ast_channel *chan)
  88. {
  89. struct ast_hangup_handler_list *handlers;
  90. struct ast_hangup_handler *h_handler;
  91. ast_channel_lock(chan);
  92. /* Get rid of each of the hangup handlers on the channel */
  93. handlers = ast_channel_hangup_handlers(chan);
  94. while ((h_handler = AST_LIST_REMOVE_HEAD(handlers, node))) {
  95. ast_free(h_handler);
  96. }
  97. ast_channel_unlock(chan);
  98. }
  99. int ast_pbx_hangup_handler_pop(struct ast_channel *chan)
  100. {
  101. struct ast_hangup_handler_list *handlers;
  102. struct ast_hangup_handler *h_handler;
  103. ast_channel_lock(chan);
  104. handlers = ast_channel_hangup_handlers(chan);
  105. h_handler = AST_LIST_REMOVE_HEAD(handlers, node);
  106. if (h_handler) {
  107. publish_hangup_handler_message("pop", chan, h_handler->args);
  108. }
  109. ast_channel_unlock(chan);
  110. if (h_handler) {
  111. ast_free(h_handler);
  112. return 1;
  113. }
  114. return 0;
  115. }
  116. void ast_pbx_hangup_handler_push(struct ast_channel *chan, const char *handler)
  117. {
  118. struct ast_hangup_handler_list *handlers;
  119. struct ast_hangup_handler *h_handler;
  120. const char *expanded_handler;
  121. if (ast_strlen_zero(handler)) {
  122. return;
  123. }
  124. expanded_handler = ast_app_expand_sub_args(chan, handler);
  125. if (!expanded_handler) {
  126. return;
  127. }
  128. h_handler = ast_malloc(sizeof(*h_handler) + 1 + strlen(expanded_handler));
  129. if (!h_handler) {
  130. ast_free((char *) expanded_handler);
  131. return;
  132. }
  133. strcpy(h_handler->args, expanded_handler);/* Safe */
  134. ast_free((char *) expanded_handler);
  135. ast_channel_lock(chan);
  136. handlers = ast_channel_hangup_handlers(chan);
  137. AST_LIST_INSERT_HEAD(handlers, h_handler, node);
  138. publish_hangup_handler_message("push", chan, h_handler->args);
  139. ast_channel_unlock(chan);
  140. }
  141. #define HANDLER_FORMAT "%-30s %s\n"
  142. /*!
  143. * \internal
  144. * \brief CLI output the hangup handler headers.
  145. * \since 11.0
  146. *
  147. * \param fd CLI file descriptor to use.
  148. *
  149. * \return Nothing
  150. */
  151. static void ast_pbx_hangup_handler_headers(int fd)
  152. {
  153. ast_cli(fd, HANDLER_FORMAT, "Channel", "Handler");
  154. }
  155. /*!
  156. * \internal
  157. * \brief CLI output the channel hangup handlers.
  158. * \since 11.0
  159. *
  160. * \param fd CLI file descriptor to use.
  161. * \param chan Channel to show hangup handlers.
  162. *
  163. * \return Nothing
  164. */
  165. static void ast_pbx_hangup_handler_show(int fd, struct ast_channel *chan)
  166. {
  167. struct ast_hangup_handler_list *handlers;
  168. struct ast_hangup_handler *h_handler;
  169. int first = 1;
  170. ast_channel_lock(chan);
  171. handlers = ast_channel_hangup_handlers(chan);
  172. AST_LIST_TRAVERSE(handlers, h_handler, node) {
  173. ast_cli(fd, HANDLER_FORMAT, first ? ast_channel_name(chan) : "", h_handler->args);
  174. first = 0;
  175. }
  176. ast_channel_unlock(chan);
  177. }
  178. /*
  179. * \brief 'show hanguphandlers <channel>' CLI command implementation function...
  180. */
  181. static char *handle_show_hangup_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  182. {
  183. struct ast_channel *chan;
  184. switch (cmd) {
  185. case CLI_INIT:
  186. e->command = "core show hanguphandlers";
  187. e->usage =
  188. "Usage: core show hanguphandlers <channel>\n"
  189. " Show hangup handlers of a specified channel.\n";
  190. return NULL;
  191. case CLI_GENERATE:
  192. return ast_complete_channels(a->line, a->word, a->pos, a->n, e->args);
  193. }
  194. if (a->argc < 4) {
  195. return CLI_SHOWUSAGE;
  196. }
  197. chan = ast_channel_get_by_name(a->argv[3]);
  198. if (!chan) {
  199. ast_cli(a->fd, "Channel does not exist.\n");
  200. return CLI_FAILURE;
  201. }
  202. ast_pbx_hangup_handler_headers(a->fd);
  203. ast_pbx_hangup_handler_show(a->fd, chan);
  204. ast_channel_unref(chan);
  205. return CLI_SUCCESS;
  206. }
  207. /*
  208. * \brief 'show hanguphandlers all' CLI command implementation function...
  209. */
  210. static char *handle_show_hangup_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  211. {
  212. struct ast_channel_iterator *iter;
  213. struct ast_channel *chan;
  214. switch (cmd) {
  215. case CLI_INIT:
  216. e->command = "core show hanguphandlers all";
  217. e->usage =
  218. "Usage: core show hanguphandlers all\n"
  219. " Show hangup handlers for all channels.\n";
  220. return NULL;
  221. case CLI_GENERATE:
  222. return NULL;
  223. }
  224. if (a->argc < 4) {
  225. return CLI_SHOWUSAGE;
  226. }
  227. iter = ast_channel_iterator_all_new();
  228. if (!iter) {
  229. return CLI_FAILURE;
  230. }
  231. ast_pbx_hangup_handler_headers(a->fd);
  232. for (; (chan = ast_channel_iterator_next(iter)); ast_channel_unref(chan)) {
  233. ast_pbx_hangup_handler_show(a->fd, chan);
  234. }
  235. ast_channel_iterator_destroy(iter);
  236. return CLI_SUCCESS;
  237. }
  238. static struct ast_cli_entry cli[] = {
  239. AST_CLI_DEFINE(handle_show_hangup_all, "Show hangup handlers of all channels"),
  240. AST_CLI_DEFINE(handle_show_hangup_channel, "Show hangup handlers of a specified channel"),
  241. };
  242. static void unload_pbx_hangup_handler(void)
  243. {
  244. ast_cli_unregister_multiple(cli, ARRAY_LEN(cli));
  245. }
  246. int load_pbx_hangup_handler(void)
  247. {
  248. ast_cli_register_multiple(cli, ARRAY_LEN(cli));
  249. ast_register_cleanup(unload_pbx_hangup_handler);
  250. return 0;
  251. }