security_events: Fix error caused by DTD validation error
[asterisk/asterisk.git] / res / res_stasis_mailbox.c
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
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 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
28
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"
35
36 /*! Number of hash buckets for mailboxes */
37 #define MAILBOX_BUCKETS 37
38
39 static struct ast_json *mailbox_to_json(
40         const struct ast_mwi_mailbox_object *mailbox)
41 {
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));
46 }
47
48 enum stasis_mailbox_result stasis_app_mailbox_to_json(
49         const char *name, struct ast_json **json)
50 {
51         struct ast_json *mailbox_json;
52         const struct ast_mwi_mailbox_object *mailbox;
53
54         mailbox = ast_mwi_mailbox_get(name);
55         if (!mailbox) {
56                 return STASIS_MAILBOX_MISSING;
57         }
58
59         mailbox_json = mailbox_to_json(mailbox);
60         if (!mailbox_json) {
61                 ast_mwi_mailbox_unref(mailbox);
62                 return STASIS_MAILBOX_ERROR;
63         }
64
65         *json = mailbox_json;
66
67         return STASIS_MAILBOX_OK;
68 }
69
70 struct ast_json *stasis_app_mailboxes_to_json()
71 {
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;
76
77         if (!array) {
78                 return NULL;
79         }
80
81         mailboxes = ast_mwi_mailbox_get_all();
82         if (!mailboxes) {
83                 ast_json_unref(array);
84                 return NULL;
85         }
86
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);
93                         array = NULL;
94                         break;
95                 }
96         }
97         ao2_iterator_destroy(&iter);
98
99         return array;
100 }
101
102 int stasis_app_mailbox_update(
103         const char *name, int old_messages, int new_messages)
104 {
105         struct ast_mwi_mailbox_object *mailbox;
106         int res = 0;
107
108         mailbox = ast_mwi_mailbox_alloc(name);
109         if (!mailbox) {
110                 return -1;
111         }
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)) {
115                 res = -1;
116         }
117
118         ast_mwi_mailbox_unref(mailbox);
119
120         return res;
121 }
122
123 enum stasis_mailbox_result stasis_app_mailbox_delete(
124         const char *name)
125 {
126         const struct ast_mwi_mailbox_object *mailbox;
127
128         /* Make sure the mailbox actually exists before we delete it */
129         mailbox = ast_mwi_mailbox_get(name);
130         if (!mailbox) {
131                 return STASIS_MAILBOX_MISSING;
132         }
133
134         ast_mwi_mailbox_unref(mailbox);
135         mailbox = NULL;
136
137         /* Now delete the mailbox */
138         if (ast_mwi_mailbox_delete(name)) {
139                 return STASIS_MAILBOX_ERROR;
140         }
141
142         return STASIS_MAILBOX_OK;
143 }
144
145 static int load_module(void)
146 {
147         /* Must be done first */
148         ast_mwi_external_ref();
149
150         return AST_MODULE_LOAD_SUCCESS;
151 }
152
153 static int unload_module(void)
154 {
155         /* Must be done last */
156         ast_mwi_external_unref();
157
158         return 0;
159 }
160
161 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis application mailbox support",
162         .load = load_module,
163         .unload = unload_module,
164         .nonoptreq = "res_stasis,res_mwi_external"
165         );