manager_mwi.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Matt Jordan <mjordan@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 The Asterisk Management Interface - AMI (MWI event handling)
  21. *
  22. * \author Matt Jordan <mjordan@digium.com>
  23. */
  24. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include "asterisk/manager.h"
  27. #include "asterisk/app.h"
  28. #include "asterisk/channel.h"
  29. #include "asterisk/stasis_message_router.h"
  30. #include "asterisk/stasis.h"
  31. struct stasis_message_router *mwi_state_router;
  32. /*** DOCUMENTATION
  33. ***/
  34. /*! \brief The \ref stasis subscription returned by the forwarding of the MWI topic
  35. * to the manager topic
  36. */
  37. static struct stasis_forward *topic_forwarder;
  38. /*! \brief Callback function used by \ref mwi_app_event_cb to weed out "Event" keys */
  39. static int exclude_event_cb(const char *key)
  40. {
  41. if (!strcmp(key, "Event")) {
  42. return -1;
  43. }
  44. return 0;
  45. }
  46. /*! \brief Generic MWI event callback used for one-off events from voicemail modules */
  47. static void mwi_app_event_cb(void *data, struct stasis_subscription *sub,
  48. struct stasis_message *message)
  49. {
  50. struct ast_mwi_blob *payload = stasis_message_data(message);
  51. RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
  52. RAII_VAR(struct ast_str *, event_buffer, NULL, ast_free);
  53. struct ast_json *event_json = ast_json_object_get(payload->blob, "Event");
  54. if (!event_json) {
  55. return;
  56. }
  57. if (payload->mwi_state && payload->mwi_state->snapshot) {
  58. channel_event_string = ast_manager_build_channel_state_string(payload->mwi_state->snapshot);
  59. }
  60. event_buffer = ast_manager_str_from_json_object(payload->blob, exclude_event_cb);
  61. if (!event_buffer) {
  62. ast_log(AST_LOG_WARNING, "Failed to create payload for event %s\n", ast_json_string_get(event_json));
  63. return;
  64. }
  65. manager_event(EVENT_FLAG_CALL, ast_json_string_get(event_json),
  66. "Mailbox: %s\r\n"
  67. "%s"
  68. "%s",
  69. payload->mwi_state ? payload->mwi_state->uniqueid : "Unknown",
  70. ast_str_buffer(event_buffer),
  71. channel_event_string ? ast_str_buffer(channel_event_string) : "");
  72. }
  73. static void mwi_update_cb(void *data, struct stasis_subscription *sub,
  74. struct stasis_message *message)
  75. {
  76. struct ast_mwi_state *mwi_state;
  77. RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
  78. if (ast_mwi_state_type() != stasis_message_type(message)) {
  79. return;
  80. }
  81. mwi_state = stasis_message_data(message);
  82. if (!mwi_state) {
  83. return;
  84. }
  85. if (mwi_state->snapshot) {
  86. channel_event_string = ast_manager_build_channel_state_string(mwi_state->snapshot);
  87. }
  88. /*** DOCUMENTATION
  89. <managerEventInstance>
  90. <synopsis>Raised when the state of messages in a voicemail mailbox
  91. has changed or when a channel has finished interacting with a
  92. mailbox.</synopsis>
  93. <syntax>
  94. <channel_snapshot/>
  95. <parameter name="Mailbox">
  96. <para>The mailbox with the new message, specified as <literal>mailbox</literal>@<literal>context</literal></para>
  97. </parameter>
  98. <parameter name="Waiting">
  99. <para>Whether or not the mailbox has messages waiting for it.</para>
  100. </parameter>
  101. <parameter name="New">
  102. <para>The number of new messages.</para>
  103. </parameter>
  104. <parameter name="Old">
  105. <para>The number of old messages.</para>
  106. </parameter>
  107. </syntax>
  108. <description>
  109. <note><para>The Channel related parameters are only present if a
  110. channel was involved in the manipulation of a mailbox. If no
  111. channel is involved, the parameters are not included with the
  112. event.</para>
  113. </note>
  114. </description>
  115. </managerEventInstance>
  116. ***/
  117. manager_event(EVENT_FLAG_CALL, "MessageWaiting",
  118. "%s"
  119. "Mailbox: %s\r\n"
  120. "Waiting: %d\r\n"
  121. "New: %d\r\n"
  122. "Old: %d\r\n",
  123. AS_OR(channel_event_string, ""),
  124. mwi_state->uniqueid,
  125. ast_app_has_voicemail(mwi_state->uniqueid, NULL),
  126. mwi_state->new_msgs,
  127. mwi_state->old_msgs);
  128. }
  129. static void manager_mwi_shutdown(void)
  130. {
  131. stasis_forward_cancel(topic_forwarder);
  132. topic_forwarder = NULL;
  133. }
  134. int manager_mwi_init(void)
  135. {
  136. int ret = 0;
  137. struct stasis_topic *manager_topic;
  138. struct stasis_topic *mwi_topic;
  139. struct stasis_message_router *message_router;
  140. manager_topic = ast_manager_get_topic();
  141. if (!manager_topic) {
  142. return -1;
  143. }
  144. message_router = ast_manager_get_message_router();
  145. if (!message_router) {
  146. return -1;
  147. }
  148. mwi_topic = ast_mwi_topic_all();
  149. if (!mwi_topic) {
  150. return -1;
  151. }
  152. topic_forwarder = stasis_forward_all(mwi_topic, manager_topic);
  153. if (!topic_forwarder) {
  154. return -1;
  155. }
  156. ast_register_cleanup(manager_mwi_shutdown);
  157. ret |= stasis_message_router_add(message_router,
  158. ast_mwi_state_type(),
  159. mwi_update_cb,
  160. NULL);
  161. ret |= stasis_message_router_add(message_router,
  162. ast_mwi_vm_app_type(),
  163. mwi_app_event_cb,
  164. NULL);
  165. /* If somehow we failed to add any routes, just shut down the whole
  166. * thing and fail it.
  167. */
  168. if (ret) {
  169. manager_mwi_shutdown();
  170. return -1;
  171. }
  172. return 0;
  173. }