Debian Codegraph @

main/manager_mwi.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Matt Jordan <[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 /*! \file
20 *
21 * \brief The Asterisk Management Interface - AMI (MWI event handling)
22 *
23 * \author Matt Jordan <[email protected]>
24 */
25
26 #include "asterisk.h"
27
28 #include "asterisk/manager.h"
29 #include "asterisk/app.h"
30 #include "asterisk/mwi.h"
31 #include "asterisk/channel.h"
32 #include "asterisk/stasis_message_router.h"
33 #include "asterisk/stasis.h"
34
35 struct stasis_message_router *mwi_state_router;
36
37 /*** DOCUMENTATION
38 ***/
39
40 /*! \brief The \ref stasis subscription returned by the forwarding of the MWI topic
41 * to the manager topic
42 */
43 static struct stasis_forward *topic_forwarder;
44
45 /*! \brief Callback function used by \ref mwi_app_event_cb to weed out "Event" keys */
46 static int exclude_event_cb(const char *key)
47 {
48 if (!strcmp(key, "Event")) {
49 return -1;
50 }
51 return 0;
52 }
53
54 /*! \brief Generic MWI event callback used for one-off events from voicemail modules */
55 static void mwi_app_event_cb(void *data, struct stasis_subscription *sub,
56 struct stasis_message *message)
57 {
58 struct ast_mwi_blob *payload = stasis_message_data(message);
59 RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
60 RAII_VAR(struct ast_str *, event_buffer, NULL, ast_free);
61 struct ast_json *event_json = ast_json_object_get(payload->blob, "Event");
62
63 if (!event_json) {
64 return;
65 }
66
67 if (payload->mwi_state && payload->mwi_state->snapshot) {
68 channel_event_string = ast_manager_build_channel_state_string(payload->mwi_state->snapshot);
69 }
70
71 event_buffer = ast_manager_str_from_json_object(payload->blob, exclude_event_cb);
72 if (!event_buffer) {
73 ast_log(AST_LOG_WARNING, "Failed to create payload for event %s\n", ast_json_string_get(event_json));
74 return;
75 }
76
77 manager_event(EVENT_FLAG_CALL, ast_json_string_get(event_json),
78 "Mailbox: %s\r\n"
79 "%s"
80 "%s",
81 payload->mwi_state ? payload->mwi_state->uniqueid : "Unknown",
82 ast_str_buffer(event_buffer),
83 channel_event_string ? ast_str_buffer(channel_event_string) : "");
84 }
85
86 static void mwi_update_cb(void *data, struct stasis_subscription *sub,
87 struct stasis_message *message)
88 {
89 struct ast_mwi_state *mwi_state;
90 RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
91
92 if (ast_mwi_state_type() != stasis_message_type(message)) {
93 return;
94 }
95
96 mwi_state = stasis_message_data(message);
97 if (!mwi_state) {
98 return;
99 }
100
101 if (mwi_state->snapshot) {
102 channel_event_string = ast_manager_build_channel_state_string(mwi_state->snapshot);
103 }
104
105 /*** DOCUMENTATION
106 <managerEvent language="en_US" name="MessageWaiting">
107 <managerEventInstance class="EVENT_FLAG_CALL">
108 <since>
109 <version>16.24.0</version>
110 <version>18.10.0</version>
111 <version>19.2.0</version>
112 </since>
113 <synopsis>Raised when the state of messages in a voicemail mailbox
114 has changed or when a channel has finished interacting with a
115 mailbox.</synopsis>
116 <syntax>
117 <channel_snapshot/>
118 <parameter name="Mailbox">
119 <para>The mailbox with the new message, specified as <literal>mailbox</literal>@<literal>context</literal></para>
120 </parameter>
121 <parameter name="Waiting">
122 <para>Whether or not the mailbox has messages waiting for it.</para>
123 </parameter>
124 <parameter name="New">
125 <para>The number of new messages.</para>
126 </parameter>
127 <parameter name="Old">
128 <para>The number of old messages.</para>
129 </parameter>
130 </syntax>
131 <description>
132 <note><para>The Channel related parameters are only present if a
133 channel was involved in the manipulation of a mailbox. If no
134 channel is involved, the parameters are not included with the
135 event.</para>
136 </note>
137 </description>
138 </managerEventInstance>
139 </managerEvent>
140 ***/
141 manager_event(EVENT_FLAG_CALL, "MessageWaiting",
142 "%s"
143 "Mailbox: %s\r\n"
144 "Waiting: %d\r\n"
145 "New: %d\r\n"
146 "Old: %d\r\n",
147 AS_OR(channel_event_string, ""),
148 mwi_state->uniqueid,
149 ast_app_has_voicemail(mwi_state->uniqueid, NULL),
150 mwi_state->new_msgs,
151 mwi_state->old_msgs);
152 }
153
154 static void manager_mwi_shutdown(void)
155 {
156 stasis_forward_cancel(topic_forwarder);
157 topic_forwarder = NULL;
158 }
159
160 int manager_mwi_init(void)
161 {
162 int ret = 0;
163 struct stasis_topic *manager_topic;
164 struct stasis_topic *mwi_topic;
165 struct stasis_message_router *message_router;
166
167 manager_topic = ast_manager_get_topic();
168 if (!manager_topic) {
169 return -1;
170 }
171 message_router = ast_manager_get_message_router();
172 if (!message_router) {
173 return -1;
174 }
175 mwi_topic = ast_mwi_topic_all();
176 if (!mwi_topic) {
177 return -1;
178 }
179
180 topic_forwarder = stasis_forward_all(mwi_topic, manager_topic);
181 if (!topic_forwarder) {
182 return -1;
183 }
184
185 ast_register_cleanup(manager_mwi_shutdown);
186
187 ret |= stasis_message_router_add(message_router,
188 ast_mwi_state_type(),
189 mwi_update_cb,
190 NULL);
191
192 ret |= stasis_message_router_add(message_router,
193 ast_mwi_vm_app_type(),
194 mwi_app_event_cb,
195 NULL);
196
197 /* If somehow we failed to add any routes, just shut down the whole
198 * thing and fail it.
199 */
200 if (ret) {
201 manager_mwi_shutdown();
202 return -1;
203 }
204
205 return 0;
206 }