res_manager_presencestate.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@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. /*** MODULEINFO
  19. <support_level>core</support_level>
  20. ***/
  21. /*** DOCUMENTATION
  22. <manager name="PresenceStateList" language="en_US">
  23. <synopsis>
  24. List the current known presence states.
  25. </synopsis>
  26. <syntax>
  27. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  28. </syntax>
  29. <description>
  30. <para>This will list out all known presence states in a
  31. sequence of <replaceable>PresenceStateChange</replaceable> events.
  32. When finished, a <replaceable>PresenceStateListComplete</replaceable> event
  33. will be emitted.</para>
  34. </description>
  35. <see-also>
  36. <ref type="manager">PresenceState</ref>
  37. <ref type="managerEvent">PresenceStatus</ref>
  38. <ref type="function">PRESENCE_STATE</ref>
  39. </see-also>
  40. <responses>
  41. <list-elements>
  42. <xi:include xpointer="xpointer(/docs/managerEvent[@name='PresenceStateChange'])" />
  43. </list-elements>
  44. <managerEvent name="PresenceStateListComplete" language="en_US">
  45. <managerEventInstance class="EVENT_FLAG_COMMAND">
  46. <synopsis>
  47. Indicates the end of the list the current known extension states.
  48. </synopsis>
  49. <syntax>
  50. <parameter name="EventList">
  51. <para>Conveys the status of the event list.</para>
  52. </parameter>
  53. <parameter name="ListItems">
  54. <para>Conveys the number of statuses reported.</para>
  55. </parameter>
  56. </syntax>
  57. </managerEventInstance>
  58. </managerEvent>
  59. </responses>
  60. </manager>
  61. ***/
  62. #include "asterisk.h"
  63. #include "asterisk/module.h"
  64. #include "asterisk/manager.h"
  65. #include "asterisk/stasis.h"
  66. #include "asterisk/presencestate.h"
  67. static struct stasis_forward *topic_forwarder;
  68. static int action_presencestatelist(struct mansession *s, const struct message *m)
  69. {
  70. RAII_VAR(struct ao2_container *, presence_states, NULL, ao2_cleanup);
  71. const char *action_id = astman_get_header(m, "ActionID");
  72. struct stasis_message *msg;
  73. struct ao2_iterator it_states;
  74. int count = 0;
  75. presence_states = stasis_cache_dump(ast_presence_state_cache(),
  76. ast_presence_state_message_type());
  77. if (!presence_states) {
  78. astman_send_error(s, m, "Memory Allocation Failure");
  79. return 0;
  80. }
  81. astman_send_listack(s, m, "Presence State Changes will follow", "start");
  82. it_states = ao2_iterator_init(presence_states, 0);
  83. for (; (msg = ao2_iterator_next(&it_states)); ao2_ref(msg, -1)) {
  84. struct ast_manager_event_blob *blob = stasis_message_to_ami(msg);
  85. if (!blob) {
  86. continue;
  87. }
  88. count++;
  89. astman_append(s, "Event: %s\r\n", blob->manager_event);
  90. if (!ast_strlen_zero(action_id)) {
  91. astman_append(s, "ActionID: %s\r\n", action_id);
  92. }
  93. astman_append(s, "%s\r\n", blob->extra_fields);
  94. ao2_ref(blob, -1);
  95. }
  96. ao2_iterator_destroy(&it_states);
  97. astman_append(s, "Event: PresenceStateListComplete\r\n");
  98. if (!ast_strlen_zero(action_id)) {
  99. astman_append(s, "ActionID: %s\r\n", action_id);
  100. }
  101. astman_append(s, "EventList: Complete\r\n"
  102. "ListItems: %d\r\n\r\n", count);
  103. return 0;
  104. }
  105. static int unload_module(void)
  106. {
  107. ast_manager_unregister("PresenceStateList");
  108. topic_forwarder = stasis_forward_cancel(topic_forwarder);
  109. return 0;
  110. }
  111. static int load_module(void)
  112. {
  113. struct stasis_topic *manager_topic;
  114. manager_topic = ast_manager_get_topic();
  115. if (!manager_topic) {
  116. return AST_MODULE_LOAD_DECLINE;
  117. }
  118. topic_forwarder = stasis_forward_all(ast_presence_state_topic_all(), manager_topic);
  119. if (!topic_forwarder) {
  120. return AST_MODULE_LOAD_DECLINE;
  121. }
  122. if (ast_manager_register_xml("PresenceStateList", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING,
  123. action_presencestatelist)) {
  124. topic_forwarder = stasis_forward_cancel(topic_forwarder);
  125. return AST_MODULE_LOAD_DECLINE;
  126. }
  127. return AST_MODULE_LOAD_SUCCESS;
  128. }
  129. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Manager Presence State Topic Forwarder",
  130. .support_level = AST_MODULE_SUPPORT_CORE,
  131. .load = load_module,
  132. .unload = unload_module,
  133. .load_pri = AST_MODPRI_DEVSTATE_CONSUMER,
  134. );