summaryrefslogtreecommitdiff
path: root/res/res_stasis_mailbox.c
blob: 3f6bede8f31fc64da609cf85e30f4cf4330cee21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * 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"

#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);
	ao2_ref(mailboxes, -1);

	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)
{
	return AST_MODULE_LOAD_SUCCESS;
}

static int unload_module(void)
{
	return 0;
}

AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis application mailbox support",
	.support_level = AST_MODULE_SUPPORT_CORE,
	.load = load_module,
	.unload = unload_module,
	.requires = "res_stasis,res_mwi_external"
);