res_manager_presencestate.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_send_list_complete_start(s, m, "PresenceStateListComplete", count);
  98. astman_send_list_complete_end(s);
  99. return 0;
  100. }
  101. static int unload_module(void)
  102. {
  103. ast_manager_unregister("PresenceStateList");
  104. topic_forwarder = stasis_forward_cancel(topic_forwarder);
  105. return 0;
  106. }
  107. static int load_module(void)
  108. {
  109. struct stasis_topic *manager_topic;
  110. manager_topic = ast_manager_get_topic();
  111. if (!manager_topic) {
  112. return AST_MODULE_LOAD_DECLINE;
  113. }
  114. topic_forwarder = stasis_forward_all(ast_presence_state_topic_all(), manager_topic);
  115. if (!topic_forwarder) {
  116. return AST_MODULE_LOAD_DECLINE;
  117. }
  118. if (ast_manager_register_xml("PresenceStateList", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING,
  119. action_presencestatelist)) {
  120. topic_forwarder = stasis_forward_cancel(topic_forwarder);
  121. return AST_MODULE_LOAD_DECLINE;
  122. }
  123. return AST_MODULE_LOAD_SUCCESS;
  124. }
  125. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Manager Presence State Topic Forwarder",
  126. .support_level = AST_MODULE_SUPPORT_CORE,
  127. .load = load_module,
  128. .unload = unload_module,
  129. .load_pri = AST_MODPRI_DEVSTATE_CONSUMER,
  130. );