Debian Codegraph @

res/res_stasis_mailbox.c

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