2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2014, Digium, Inc.
6 * Jonathan Rose <jrose@digium.com>
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.
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.
20 <depend type="module">res_stasis</depend>
21 <depend type="module">res_mwi_external</depend>
22 <support_level>core</support_level>
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29 #include "asterisk/astdb.h"
30 #include "asterisk/astobj2.h"
31 #include "asterisk/module.h"
32 #include "asterisk/stasis_app_impl.h"
33 #include "asterisk/stasis_app_mailbox.h"
34 #include "asterisk/res_mwi_external.h"
36 /*! Number of hash buckets for mailboxes */
37 #define MAILBOX_BUCKETS 37
39 static struct ast_json *mailbox_to_json(
40 const struct ast_mwi_mailbox_object *mailbox)
42 return ast_json_pack("{s: s, s: i, s: i}",
43 "name", ast_mwi_mailbox_get_id(mailbox),
44 "old_messages", ast_mwi_mailbox_get_msgs_old(mailbox),
45 "new_messages", ast_mwi_mailbox_get_msgs_new(mailbox));
48 enum stasis_mailbox_result stasis_app_mailbox_to_json(
49 const char *name, struct ast_json **json)
51 struct ast_json *mailbox_json;
52 const struct ast_mwi_mailbox_object *mailbox;
54 mailbox = ast_mwi_mailbox_get(name);
56 return STASIS_MAILBOX_MISSING;
59 mailbox_json = mailbox_to_json(mailbox);
61 ast_mwi_mailbox_unref(mailbox);
62 return STASIS_MAILBOX_ERROR;
67 return STASIS_MAILBOX_OK;
70 struct ast_json *stasis_app_mailboxes_to_json()
72 struct ast_json *array = ast_json_array_create();
73 struct ao2_container *mailboxes;
74 struct ao2_iterator iter;
75 const struct ast_mwi_mailbox_object *mailbox;
81 mailboxes = ast_mwi_mailbox_get_all();
83 ast_json_unref(array);
87 iter = ao2_iterator_init(mailboxes, 0);
88 for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
89 struct ast_json *appending = mailbox_to_json(mailbox);
90 if (!appending || ast_json_array_append(array, appending)) {
91 /* Failed to append individual mailbox to the array. Abort. */
92 ast_json_unref(array);
97 ao2_iterator_destroy(&iter);
102 int stasis_app_mailbox_update(
103 const char *name, int old_messages, int new_messages)
105 struct ast_mwi_mailbox_object *mailbox;
108 mailbox = ast_mwi_mailbox_alloc(name);
112 ast_mwi_mailbox_set_msgs_new(mailbox, new_messages);
113 ast_mwi_mailbox_set_msgs_old(mailbox, old_messages);
114 if (ast_mwi_mailbox_update(mailbox)) {
118 ast_mwi_mailbox_unref(mailbox);
123 enum stasis_mailbox_result stasis_app_mailbox_delete(
126 const struct ast_mwi_mailbox_object *mailbox;
128 /* Make sure the mailbox actually exists before we delete it */
129 mailbox = ast_mwi_mailbox_get(name);
131 return STASIS_MAILBOX_MISSING;
134 ast_mwi_mailbox_unref(mailbox);
137 /* Now delete the mailbox */
138 if (ast_mwi_mailbox_delete(name)) {
139 return STASIS_MAILBOX_ERROR;
142 return STASIS_MAILBOX_OK;
145 static int load_module(void)
147 /* Must be done first */
148 ast_mwi_external_ref();
150 return AST_MODULE_LOAD_SUCCESS;
153 static int unload_module(void)
155 /* Must be done last */
156 ast_mwi_external_unref();
161 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis application mailbox support",
163 .unload = unload_module,
164 .nonoptreq = "res_stasis,res_mwi_external"