pbx_hangup_handler.c 7.0 KB

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