summaryrefslogtreecommitdiff
path: root/res/res_stasis_mailbox.c
diff options
context:
space:
mode:
authorJonathan Rose <jrose@digium.com>2014-01-14 23:44:57 +0000
committerJonathan Rose <jrose@digium.com>2014-01-14 23:44:57 +0000
commitaa9db707c56fa673560e25663f581954a66f3974 (patch)
treea539c9a84ad0679612606f15728bd5049ff2e441 /res/res_stasis_mailbox.c
parented0d083596bd22bf772433ac949f243dfc5fb28c (diff)
ARI: Add mailboxes resource for controlling and polling external MWI
Adds the following AMI commands: PUT mailboxes/mailboxName modifies mailbox state and implicitly creates new mailboxes GET mailboxes/mailboxName retrieves a JSON representation of a single mailbox if it exists GET mailboxes retrieves a JSON array of all mailboxes DELETE mailbox/mailboxName deletes a mailbox Note that res_mwi_external must be loaded for these functions to actually do anything. Review: https://reviewboard.asterisk.org/r/3117/ ........ Merged revisions 405553 from http://svn.asterisk.org/svn/asterisk/branches/12 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@405554 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res/res_stasis_mailbox.c')
-rw-r--r--res/res_stasis_mailbox.c165
1 files changed, 165 insertions, 0 deletions
diff --git a/res/res_stasis_mailbox.c b/res/res_stasis_mailbox.c
new file mode 100644
index 000000000..62f19f555
--- /dev/null
+++ b/res/res_stasis_mailbox.c
@@ -0,0 +1,165 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2014, Digium, Inc.
+ *
+ * Jonathan Rose <jrose@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*** MODULEINFO
+ <depend type="module">res_stasis</depend>
+ <depend type="module">res_mwi_external</depend>
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/astdb.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/module.h"
+#include "asterisk/stasis_app_impl.h"
+#include "asterisk/stasis_app_mailbox.h"
+#include "asterisk/res_mwi_external.h"
+
+/*! Number of hash buckets for mailboxes */
+#define MAILBOX_BUCKETS 37
+
+static struct ast_json *mailbox_to_json(
+ const struct ast_mwi_mailbox_object *mailbox)
+{
+ return ast_json_pack("{s: s, s: i, s: i}",
+ "name", ast_mwi_mailbox_get_id(mailbox),
+ "old_messages", ast_mwi_mailbox_get_msgs_old(mailbox),
+ "new_messages", ast_mwi_mailbox_get_msgs_new(mailbox));
+}
+
+enum stasis_mailbox_result stasis_app_mailbox_to_json(
+ const char *name, struct ast_json **json)
+{
+ struct ast_json *mailbox_json;
+ const struct ast_mwi_mailbox_object *mailbox;
+
+ mailbox = ast_mwi_mailbox_get(name);
+ if (!mailbox) {
+ return STASIS_MAILBOX_MISSING;
+ }
+
+ mailbox_json = mailbox_to_json(mailbox);
+ if (!mailbox_json) {
+ ast_mwi_mailbox_unref(mailbox);
+ return STASIS_MAILBOX_ERROR;
+ }
+
+ *json = mailbox_json;
+
+ return STASIS_MAILBOX_OK;
+}
+
+struct ast_json *stasis_app_mailboxes_to_json()
+{
+ struct ast_json *array = ast_json_array_create();
+ struct ao2_container *mailboxes;
+ struct ao2_iterator iter;
+ const struct ast_mwi_mailbox_object *mailbox;
+
+ if (!array) {
+ return NULL;
+ }
+
+ mailboxes = ast_mwi_mailbox_get_all();
+ if (!mailboxes) {
+ ast_json_unref(array);
+ return NULL;
+ }
+
+ iter = ao2_iterator_init(mailboxes, 0);
+ for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
+ struct ast_json *appending = mailbox_to_json(mailbox);
+ if (!appending || ast_json_array_append(array, appending)) {
+ /* Failed to append individual mailbox to the array. Abort. */
+ ast_json_unref(array);
+ array = NULL;
+ break;
+ }
+ }
+ ao2_iterator_destroy(&iter);
+
+ return array;
+}
+
+int stasis_app_mailbox_update(
+ const char *name, int old_messages, int new_messages)
+{
+ struct ast_mwi_mailbox_object *mailbox;
+ int res = 0;
+
+ mailbox = ast_mwi_mailbox_alloc(name);
+ if (!mailbox) {
+ return -1;
+ }
+ ast_mwi_mailbox_set_msgs_new(mailbox, new_messages);
+ ast_mwi_mailbox_set_msgs_old(mailbox, old_messages);
+ if (ast_mwi_mailbox_update(mailbox)) {
+ res = -1;
+ }
+
+ ast_mwi_mailbox_unref(mailbox);
+
+ return res;
+}
+
+enum stasis_mailbox_result stasis_app_mailbox_delete(
+ const char *name)
+{
+ const struct ast_mwi_mailbox_object *mailbox;
+
+ /* Make sure the mailbox actually exists before we delete it */
+ mailbox = ast_mwi_mailbox_get(name);
+ if (!mailbox) {
+ return STASIS_MAILBOX_MISSING;
+ }
+
+ ast_mwi_mailbox_unref(mailbox);
+ mailbox = NULL;
+
+ /* Now delete the mailbox */
+ if (ast_mwi_mailbox_delete(name)) {
+ return STASIS_MAILBOX_ERROR;
+ }
+
+ return STASIS_MAILBOX_OK;
+}
+
+static int load_module(void)
+{
+ /* Must be done first */
+ ast_mwi_external_ref();
+
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+ /* Must be done last */
+ ast_mwi_external_unref();
+
+ return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis application mailbox support",
+ .load = load_module,
+ .unload = unload_module,
+ .nonoptreq = "res_stasis,res_mwi_external"
+ );