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