res_pjsip: Allow configuration of endpoint identifier query order
[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 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
131
132 #include "asterisk/module.h"
133 #include "asterisk/res_mwi_external.h"
134 #include "asterisk/manager.h"
135
136 /* ------------------------------------------------------------------- */
137
138 /*!
139  * \internal
140  * \brief Get the requested mailboxes.
141  * \since 12.1.0
142  *
143  * \param s AMI session.
144  * \param m AMI message.
145  *
146  * \retval 0 to keep AMI connection.
147  * \retval -1 to disconnect AMI connection.
148  */
149 static int mwi_mailbox_get(struct mansession *s, const struct message *m)
150 {
151         char id_text[256];
152         const char *id;
153         const char *mailbox_id = astman_get_header(m, "Mailbox");
154         const struct ast_mwi_mailbox_object *mailbox;
155         struct ao2_container *mailboxes;
156         unsigned count;
157         struct ao2_iterator iter;
158
159         if (ast_strlen_zero(mailbox_id)) {
160                 astman_send_error(s, m, "Missing mailbox parameter in request");
161                 return 0;
162         }
163
164         if (*mailbox_id == '/') {
165                 struct ast_str *regex_string;
166
167                 regex_string = ast_str_create(strlen(mailbox_id) + 1);
168                 if (!regex_string) {
169                         astman_send_error(s, m, "Memory Allocation Failure");
170                         return 0;
171                 }
172
173                 /* Make "/regex/" into "regex" */
174                 if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
175                         astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
176                         ast_free(regex_string);
177                         return 0;
178                 }
179
180                 mailboxes = ast_mwi_mailbox_get_by_regex(ast_str_buffer(regex_string));
181                 ast_free(regex_string);
182         } else {
183                 mailboxes = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
184                 if (mailboxes) {
185                         mailbox = ast_mwi_mailbox_get(mailbox_id);
186                         if (mailbox) {
187                                 if (!ao2_link(mailboxes, (void *) mailbox)) {
188                                         ao2_ref(mailboxes, -1);
189                                         mailboxes = NULL;
190                                 }
191                                 ast_mwi_mailbox_unref(mailbox);
192                         }
193                 }
194         }
195         if (!mailboxes) {
196                 astman_send_error(s, m, "Mailbox container creation failure");
197                 return 0;
198         }
199
200         astman_send_listack(s, m, "Mailboxes will follow", "start");
201
202         id = astman_get_header(m, "ActionID");
203         if (!ast_strlen_zero(id)) {
204                 snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
205         } else {
206                 id_text[0] = '\0';
207         }
208
209         /* Output mailbox list. */
210         count = 0;
211         iter = ao2_iterator_init(mailboxes, AO2_ITERATOR_UNLINK);
212         for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
213                 ++count;
214                 astman_append(s,
215                         "Event: MWIGet\r\n"
216                         "Mailbox: %s\r\n"
217                         "OldMessages: %u\r\n"
218                         "NewMessages: %u\r\n"
219                         "%s"
220                         "\r\n",
221                         ast_mwi_mailbox_get_id(mailbox),
222                         ast_mwi_mailbox_get_msgs_old(mailbox),
223                         ast_mwi_mailbox_get_msgs_new(mailbox),
224                         id_text);
225         }
226         ao2_iterator_destroy(&iter);
227         ao2_ref(mailboxes, -1);
228
229         astman_send_list_complete_start(s, m, "MWIGetComplete", count);
230         astman_send_list_complete_end(s);
231
232         return 0;
233 }
234
235 /*!
236  * \internal
237  * \brief Delete the requested mailboxes.
238  * \since 12.1.0
239  *
240  * \param s AMI session.
241  * \param m AMI message.
242  *
243  * \retval 0 to keep AMI connection.
244  * \retval -1 to disconnect AMI connection.
245  */
246 static int mwi_mailbox_delete(struct mansession *s, const struct message *m)
247 {
248         const char *mailbox_id = astman_get_header(m, "Mailbox");
249
250         if (ast_strlen_zero(mailbox_id)) {
251                 astman_send_error(s, m, "Missing mailbox parameter in request");
252                 return 0;
253         }
254
255         if (*mailbox_id == '/') {
256                 struct ast_str *regex_string;
257
258                 regex_string = ast_str_create(strlen(mailbox_id) + 1);
259                 if (!regex_string) {
260                         astman_send_error(s, m, "Memory Allocation Failure");
261                         return 0;
262                 }
263
264                 /* Make "/regex/" into "regex" */
265                 if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
266                         astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
267                         ast_free(regex_string);
268                         return 0;
269                 }
270
271                 ast_mwi_mailbox_delete_by_regex(ast_str_buffer(regex_string));
272                 ast_free(regex_string);
273         } else {
274                 ast_mwi_mailbox_delete(mailbox_id);
275         }
276
277         astman_send_ack(s, m, NULL);
278         return 0;
279 }
280
281 /*!
282  * \internal
283  * \brief Update the specified mailbox.
284  * \since 12.1.0
285  *
286  * \param s AMI session.
287  * \param m AMI message.
288  *
289  * \retval 0 to keep AMI connection.
290  * \retval -1 to disconnect AMI connection.
291  */
292 static int mwi_mailbox_update(struct mansession *s, const struct message *m)
293 {
294         const char *mailbox_id = astman_get_header(m, "Mailbox");
295         const char *msgs_old = astman_get_header(m, "OldMessages");
296         const char *msgs_new = astman_get_header(m, "NewMessages");
297         struct ast_mwi_mailbox_object *mailbox;
298         unsigned int num_old;
299         unsigned int num_new;
300
301         if (ast_strlen_zero(mailbox_id)) {
302                 astman_send_error(s, m, "Missing mailbox parameter in request");
303                 return 0;
304         }
305
306         num_old = 0;
307         if (!ast_strlen_zero(msgs_old)) {
308                 if (sscanf(msgs_old, "%u", &num_old) != 1) {
309                         astman_send_error_va(s, m, "Invalid OldMessages: %s", msgs_old);
310                         return 0;
311                 }
312         }
313
314         num_new = 0;
315         if (!ast_strlen_zero(msgs_new)) {
316                 if (sscanf(msgs_new, "%u", &num_new) != 1) {
317                         astman_send_error_va(s, m, "Invalid NewMessages: %s", msgs_new);
318                         return 0;
319                 }
320         }
321
322         mailbox = ast_mwi_mailbox_alloc(mailbox_id);
323         if (!mailbox) {
324                 astman_send_error(s, m, "Mailbox object creation failure");
325                 return 0;
326         }
327
328         /* Update external mailbox. */
329         ast_mwi_mailbox_set_msgs_old(mailbox, num_old);
330         ast_mwi_mailbox_set_msgs_new(mailbox, num_new);
331         if (ast_mwi_mailbox_update(mailbox)) {
332                 astman_send_error(s, m, "Update attempt failed");
333         } else {
334                 astman_send_ack(s, m, NULL);
335         }
336         ast_mwi_mailbox_unref(mailbox);
337
338         return 0;
339 }
340
341 static int unload_module(void)
342 {
343         ast_manager_unregister("MWIGet");
344         ast_manager_unregister("MWIDelete");
345         ast_manager_unregister("MWIUpdate");
346
347         /* Must be done last */
348         ast_mwi_external_unref();
349         return 0;
350 }
351
352 static int load_module(void)
353 {
354         int res;
355
356         /* Must be done first */
357         ast_mwi_external_ref();
358
359         res = 0;
360         res |= ast_manager_register_xml_core("MWIGet", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING, mwi_mailbox_get);
361         res |= ast_manager_register_xml_core("MWIDelete", EVENT_FLAG_CALL, mwi_mailbox_delete);
362         res |= ast_manager_register_xml_core("MWIUpdate", EVENT_FLAG_CALL, mwi_mailbox_update);
363         if (res) {
364                 unload_module();
365                 return AST_MODULE_LOAD_DECLINE;
366         }
367
368         return AST_MODULE_LOAD_SUCCESS;
369 }
370
371 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "AMI support for external MWI",
372         .support_level = AST_MODULE_SUPPORT_CORE,
373         .load = load_module,
374         .unload = unload_module,
375 );
376