res_stasis_mailbox.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Jonathan Rose <jrose@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. <depend type="module">res_stasis</depend>
  20. <depend type="module">res_mwi_external</depend>
  21. <support_level>core</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  25. #include "asterisk/astdb.h"
  26. #include "asterisk/astobj2.h"
  27. #include "asterisk/module.h"
  28. #include "asterisk/stasis_app_impl.h"
  29. #include "asterisk/stasis_app_mailbox.h"
  30. #include "asterisk/res_mwi_external.h"
  31. /*! Number of hash buckets for mailboxes */
  32. #define MAILBOX_BUCKETS 37
  33. static struct ast_json *mailbox_to_json(
  34. const struct ast_mwi_mailbox_object *mailbox)
  35. {
  36. return ast_json_pack("{s: s, s: i, s: i}",
  37. "name", ast_mwi_mailbox_get_id(mailbox),
  38. "old_messages", ast_mwi_mailbox_get_msgs_old(mailbox),
  39. "new_messages", ast_mwi_mailbox_get_msgs_new(mailbox));
  40. }
  41. enum stasis_mailbox_result stasis_app_mailbox_to_json(
  42. const char *name, struct ast_json **json)
  43. {
  44. struct ast_json *mailbox_json;
  45. const struct ast_mwi_mailbox_object *mailbox;
  46. mailbox = ast_mwi_mailbox_get(name);
  47. if (!mailbox) {
  48. return STASIS_MAILBOX_MISSING;
  49. }
  50. mailbox_json = mailbox_to_json(mailbox);
  51. if (!mailbox_json) {
  52. ast_mwi_mailbox_unref(mailbox);
  53. return STASIS_MAILBOX_ERROR;
  54. }
  55. *json = mailbox_json;
  56. return STASIS_MAILBOX_OK;
  57. }
  58. struct ast_json *stasis_app_mailboxes_to_json()
  59. {
  60. struct ast_json *array = ast_json_array_create();
  61. struct ao2_container *mailboxes;
  62. struct ao2_iterator iter;
  63. const struct ast_mwi_mailbox_object *mailbox;
  64. if (!array) {
  65. return NULL;
  66. }
  67. mailboxes = ast_mwi_mailbox_get_all();
  68. if (!mailboxes) {
  69. ast_json_unref(array);
  70. return NULL;
  71. }
  72. iter = ao2_iterator_init(mailboxes, 0);
  73. for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
  74. struct ast_json *appending = mailbox_to_json(mailbox);
  75. if (!appending || ast_json_array_append(array, appending)) {
  76. /* Failed to append individual mailbox to the array. Abort. */
  77. ast_json_unref(array);
  78. array = NULL;
  79. break;
  80. }
  81. }
  82. ao2_iterator_destroy(&iter);
  83. ao2_ref(mailboxes, -1);
  84. return array;
  85. }
  86. int stasis_app_mailbox_update(
  87. const char *name, int old_messages, int new_messages)
  88. {
  89. struct ast_mwi_mailbox_object *mailbox;
  90. int res = 0;
  91. mailbox = ast_mwi_mailbox_alloc(name);
  92. if (!mailbox) {
  93. return -1;
  94. }
  95. ast_mwi_mailbox_set_msgs_new(mailbox, new_messages);
  96. ast_mwi_mailbox_set_msgs_old(mailbox, old_messages);
  97. if (ast_mwi_mailbox_update(mailbox)) {
  98. res = -1;
  99. }
  100. ast_mwi_mailbox_unref(mailbox);
  101. return res;
  102. }
  103. enum stasis_mailbox_result stasis_app_mailbox_delete(
  104. const char *name)
  105. {
  106. const struct ast_mwi_mailbox_object *mailbox;
  107. /* Make sure the mailbox actually exists before we delete it */
  108. mailbox = ast_mwi_mailbox_get(name);
  109. if (!mailbox) {
  110. return STASIS_MAILBOX_MISSING;
  111. }
  112. ast_mwi_mailbox_unref(mailbox);
  113. mailbox = NULL;
  114. /* Now delete the mailbox */
  115. if (ast_mwi_mailbox_delete(name)) {
  116. return STASIS_MAILBOX_ERROR;
  117. }
  118. return STASIS_MAILBOX_OK;
  119. }
  120. static int load_module(void)
  121. {
  122. /* Must be done first */
  123. ast_mwi_external_ref();
  124. return AST_MODULE_LOAD_SUCCESS;
  125. }
  126. static int unload_module(void)
  127. {
  128. /* Must be done last */
  129. ast_mwi_external_unref();
  130. return 0;
  131. }
  132. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis application mailbox support",
  133. .support_level = AST_MODULE_SUPPORT_CORE,
  134. .load = load_module,
  135. .unload = unload_module,
  136. );