res_stasis_mailbox.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. return array;
  84. }
  85. int stasis_app_mailbox_update(
  86. const char *name, int old_messages, int new_messages)
  87. {
  88. struct ast_mwi_mailbox_object *mailbox;
  89. int res = 0;
  90. mailbox = ast_mwi_mailbox_alloc(name);
  91. if (!mailbox) {
  92. return -1;
  93. }
  94. ast_mwi_mailbox_set_msgs_new(mailbox, new_messages);
  95. ast_mwi_mailbox_set_msgs_old(mailbox, old_messages);
  96. if (ast_mwi_mailbox_update(mailbox)) {
  97. res = -1;
  98. }
  99. ast_mwi_mailbox_unref(mailbox);
  100. return res;
  101. }
  102. enum stasis_mailbox_result stasis_app_mailbox_delete(
  103. const char *name)
  104. {
  105. const struct ast_mwi_mailbox_object *mailbox;
  106. /* Make sure the mailbox actually exists before we delete it */
  107. mailbox = ast_mwi_mailbox_get(name);
  108. if (!mailbox) {
  109. return STASIS_MAILBOX_MISSING;
  110. }
  111. ast_mwi_mailbox_unref(mailbox);
  112. mailbox = NULL;
  113. /* Now delete the mailbox */
  114. if (ast_mwi_mailbox_delete(name)) {
  115. return STASIS_MAILBOX_ERROR;
  116. }
  117. return STASIS_MAILBOX_OK;
  118. }
  119. static int load_module(void)
  120. {
  121. /* Must be done first */
  122. ast_mwi_external_ref();
  123. return AST_MODULE_LOAD_SUCCESS;
  124. }
  125. static int unload_module(void)
  126. {
  127. /* Must be done last */
  128. ast_mwi_external_unref();
  129. return 0;
  130. }
  131. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis application mailbox support",
  132. .support_level = AST_MODULE_SUPPORT_CORE,
  133. .load = load_module,
  134. .unload = unload_module,
  135. .nonoptreq = "res_stasis,res_mwi_external"
  136. );