Merge "rtp_engine/res_rtp_asterisk: Fix RTP struct reentrancy crashes."
[asterisk/asterisk.git] / res / res_mwi_external_ami.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Richard Mudgett <rmudgett@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 /*!
20  * \file
21  * \brief AMI wrapper for external MWI.
22  *
23  * \author Richard Mudgett <rmudgett@digium.com>
24  *
25  * See Also:
26  * \arg \ref AstCREDITS
27  */
28
29 /*** MODULEINFO
30         <depend>res_mwi_external</depend>
31         <support_level>core</support_level>
32  ***/
33
34
35 /*** DOCUMENTATION
36         <manager name="MWIGet" language="en_US">
37                 <synopsis>
38                         Get selected mailboxes with message counts.
39                 </synopsis>
40                 <syntax>
41                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
42                         <parameter name="Mailbox" required="true">
43                                 <para>Mailbox ID in the form of
44                                 /<replaceable>regex</replaceable>/ for all mailboxes matching the regular
45                                 expression.  Otherwise it is for a specific mailbox.</para>
46                         </parameter>
47                 </syntax>
48                 <description>
49                         <para>Get a list of mailboxes with their message counts.</para>
50                 </description>
51         </manager>
52         <managerEvent language="en_US" name="MWIGet">
53                 <managerEventInstance class="EVENT_FLAG_REPORTING">
54                         <synopsis>
55                                 Raised in response to a MWIGet command.
56                         </synopsis>
57                         <syntax>
58                                 <parameter name="ActionID" required="false"/>
59                                 <parameter name="Mailbox">
60                                         <para>Specific mailbox ID.</para>
61                                 </parameter>
62                                 <parameter name="OldMessages">
63                                         <para>The number of old messages in the mailbox.</para>
64                                 </parameter>
65                                 <parameter name="NewMessages">
66                                         <para>The number of new messages in the mailbox.</para>
67                                 </parameter>
68                         </syntax>
69                         <see-also>
70                                 <ref type="manager">MWIGet</ref>
71                         </see-also>
72                 </managerEventInstance>
73         </managerEvent>
74         <managerEvent language="en_US" name="MWIGetComplete">
75                 <managerEventInstance class="EVENT_FLAG_REPORTING">
76                         <synopsis>
77                                 Raised in response to a MWIGet command.
78                         </synopsis>
79                         <syntax>
80                                 <parameter name="ActionID" required="false"/>
81                                 <parameter name="EventList" />
82                                 <parameter name="ListItems">
83                                         <para>The number of mailboxes reported.</para>
84                                 </parameter>
85                         </syntax>
86                         <see-also>
87                                 <ref type="manager">MWIGet</ref>
88                         </see-also>
89                 </managerEventInstance>
90         </managerEvent>
91         <manager name="MWIDelete" language="en_US">
92                 <synopsis>
93                         Delete selected mailboxes.
94                 </synopsis>
95                 <syntax>
96                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
97                         <xi:include xpointer="xpointer(/docs/manager[@name='MWIGet']/syntax/parameter[@name='Mailbox'])" />
98                 </syntax>
99                 <description>
100                         <para>Delete the specified mailboxes.</para>
101                 </description>
102         </manager>
103         <manager name="MWIUpdate" language="en_US">
104                 <synopsis>
105                         Update the mailbox message counts.
106                 </synopsis>
107                 <syntax>
108                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
109                         <parameter name="Mailbox" required="true">
110                                 <para>Specific mailbox ID.</para>
111                         </parameter>
112                         <parameter name="OldMessages">
113                                 <para>The number of old messages in the mailbox.  Defaults
114                                 to zero if missing.</para>
115                         </parameter>
116                         <parameter name="NewMessages">
117                                 <para>The number of new messages in the mailbox.  Defaults
118                                 to zero if missing.</para>
119                         </parameter>
120                 </syntax>
121                 <description>
122                         <para>Update the mailbox message counts.</para>
123                 </description>
124         </manager>
125  ***/
126
127
128 #include "asterisk.h"
129
130 #include "asterisk/module.h"
131 #include "asterisk/res_mwi_external.h"
132 #include "asterisk/manager.h"
133
134 /* ------------------------------------------------------------------- */
135
136 /*!
137  * \internal
138  * \brief Get the requested mailboxes.
139  * \since 12.1.0
140  *
141  * \param s AMI session.
142  * \param m AMI message.
143  *
144  * \retval 0 to keep AMI connection.
145  * \retval -1 to disconnect AMI connection.
146  */
147 static int mwi_mailbox_get(struct mansession *s, const struct message *m)
148 {
149         char id_text[256];
150         const char *id;
151         const char *mailbox_id = astman_get_header(m, "Mailbox");
152         const struct ast_mwi_mailbox_object *mailbox;
153         struct ao2_container *mailboxes;
154         unsigned count;
155         struct ao2_iterator iter;
156
157         if (ast_strlen_zero(mailbox_id)) {
158                 astman_send_error(s, m, "Missing mailbox parameter in request");
159                 return 0;
160         }
161
162         if (*mailbox_id == '/') {
163                 struct ast_str *regex_string;
164
165                 regex_string = ast_str_create(strlen(mailbox_id) + 1);
166                 if (!regex_string) {
167                         astman_send_error(s, m, "Memory Allocation Failure");
168                         return 0;
169                 }
170
171                 /* Make "/regex/" into "regex" */
172                 if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
173                         astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
174                         ast_free(regex_string);
175                         return 0;
176                 }
177
178                 mailboxes = ast_mwi_mailbox_get_by_regex(ast_str_buffer(regex_string));
179                 ast_free(regex_string);
180         } else {
181                 mailboxes = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
182                 if (mailboxes) {
183                         mailbox = ast_mwi_mailbox_get(mailbox_id);
184                         if (mailbox) {
185                                 if (!ao2_link(mailboxes, (void *) mailbox)) {
186                                         ao2_ref(mailboxes, -1);
187                                         mailboxes = NULL;
188                                 }
189                                 ast_mwi_mailbox_unref(mailbox);
190                         }
191                 }
192         }
193         if (!mailboxes) {
194                 astman_send_error(s, m, "Mailbox container creation failure");
195                 return 0;
196         }
197
198         astman_send_listack(s, m, "Mailboxes will follow", "start");
199
200         id = astman_get_header(m, "ActionID");
201         if (!ast_strlen_zero(id)) {
202                 snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
203         } else {
204                 id_text[0] = '\0';
205         }
206
207         /* Output mailbox list. */
208         count = 0;
209         iter = ao2_iterator_init(mailboxes, AO2_ITERATOR_UNLINK);
210         for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
211                 ++count;
212                 astman_append(s,
213                         "Event: MWIGet\r\n"
214                         "Mailbox: %s\r\n"
215                         "OldMessages: %u\r\n"
216                         "NewMessages: %u\r\n"
217                         "%s"
218                         "\r\n",
219                         ast_mwi_mailbox_get_id(mailbox),
220                         ast_mwi_mailbox_get_msgs_old(mailbox),
221                         ast_mwi_mailbox_get_msgs_new(mailbox),
222                         id_text);
223         }
224         ao2_iterator_destroy(&iter);
225         ao2_ref(mailboxes, -1);
226
227         astman_send_list_complete_start(s, m, "MWIGetComplete", count);
228         astman_send_list_complete_end(s);
229
230         return 0;
231 }
232
233 /*!
234  * \internal
235  * \brief Delete the requested mailboxes.
236  * \since 12.1.0
237  *
238  * \param s AMI session.
239  * \param m AMI message.
240  *
241  * \retval 0 to keep AMI connection.
242  * \retval -1 to disconnect AMI connection.
243  */
244 static int mwi_mailbox_delete(struct mansession *s, const struct message *m)
245 {
246         const char *mailbox_id = astman_get_header(m, "Mailbox");
247
248         if (ast_strlen_zero(mailbox_id)) {
249                 astman_send_error(s, m, "Missing mailbox parameter in request");
250                 return 0;
251         }
252
253         if (*mailbox_id == '/') {
254                 struct ast_str *regex_string;
255
256                 regex_string = ast_str_create(strlen(mailbox_id) + 1);
257                 if (!regex_string) {
258                         astman_send_error(s, m, "Memory Allocation Failure");
259                         return 0;
260                 }
261
262                 /* Make "/regex/" into "regex" */
263                 if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
264                         astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
265                         ast_free(regex_string);
266                         return 0;
267                 }
268
269                 ast_mwi_mailbox_delete_by_regex(ast_str_buffer(regex_string));
270                 ast_free(regex_string);
271         } else {
272                 ast_mwi_mailbox_delete(mailbox_id);
273         }
274
275         astman_send_ack(s, m, NULL);
276         return 0;
277 }
278
279 /*!
280  * \internal
281  * \brief Update the specified mailbox.
282  * \since 12.1.0
283  *
284  * \param s AMI session.
285  * \param m AMI message.
286  *
287  * \retval 0 to keep AMI connection.
288  * \retval -1 to disconnect AMI connection.
289  */
290 static int mwi_mailbox_update(struct mansession *s, const struct message *m)
291 {
292         const char *mailbox_id = astman_get_header(m, "Mailbox");
293         const char *msgs_old = astman_get_header(m, "OldMessages");
294         const char *msgs_new = astman_get_header(m, "NewMessages");
295         struct ast_mwi_mailbox_object *mailbox;
296         unsigned int num_old;
297         unsigned int num_new;
298
299         if (ast_strlen_zero(mailbox_id)) {
300                 astman_send_error(s, m, "Missing mailbox parameter in request");
301                 return 0;
302         }
303
304         num_old = 0;
305         if (!ast_strlen_zero(msgs_old)) {
306                 if (sscanf(msgs_old, "%u", &num_old) != 1) {
307                         astman_send_error_va(s, m, "Invalid OldMessages: %s", msgs_old);
308                         return 0;
309                 }
310         }
311
312         num_new = 0;
313         if (!ast_strlen_zero(msgs_new)) {
314                 if (sscanf(msgs_new, "%u", &num_new) != 1) {
315                         astman_send_error_va(s, m, "Invalid NewMessages: %s", msgs_new);
316                         return 0;
317                 }
318         }
319
320         mailbox = ast_mwi_mailbox_alloc(mailbox_id);
321         if (!mailbox) {
322                 astman_send_error(s, m, "Mailbox object creation failure");
323                 return 0;
324         }
325
326         /* Update external mailbox. */
327         ast_mwi_mailbox_set_msgs_old(mailbox, num_old);
328         ast_mwi_mailbox_set_msgs_new(mailbox, num_new);
329         if (ast_mwi_mailbox_update(mailbox)) {
330                 astman_send_error(s, m, "Update attempt failed");
331         } else {
332                 astman_send_ack(s, m, NULL);
333         }
334         ast_mwi_mailbox_unref(mailbox);
335
336         return 0;
337 }
338
339 static int unload_module(void)
340 {
341         ast_manager_unregister("MWIGet");
342         ast_manager_unregister("MWIDelete");
343         ast_manager_unregister("MWIUpdate");
344
345         /* Must be done last */
346         ast_mwi_external_unref();
347         return 0;
348 }
349
350 static int load_module(void)
351 {
352         int res;
353
354         /* Must be done first */
355         ast_mwi_external_ref();
356
357         res = 0;
358         res |= ast_manager_register_xml("MWIGet", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING, mwi_mailbox_get);
359         res |= ast_manager_register_xml("MWIDelete", EVENT_FLAG_CALL, mwi_mailbox_delete);
360         res |= ast_manager_register_xml("MWIUpdate", EVENT_FLAG_CALL, mwi_mailbox_update);
361         if (res) {
362                 unload_module();
363                 return AST_MODULE_LOAD_DECLINE;
364         }
365
366         return AST_MODULE_LOAD_SUCCESS;
367 }
368
369 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "AMI support for external MWI",
370         .support_level = AST_MODULE_SUPPORT_CORE,
371         .load = load_module,
372         .unload = unload_module,
373 );
374