Skip to content
Snippets Groups Projects
res_pjsip_mwi.c 45.4 KiB
Newer Older
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2013, Digium, Inc.
 *
 * Mark Michelson <mmichelson@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>pjproject</depend>
	<depend>res_pjsip</depend>
	<depend>res_pjsip_pubsub</depend>
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include <pjsip.h>
#include <pjsip_simple.h>
#include <pjlib.h>

#include "asterisk/res_pjsip.h"
#include "asterisk/res_pjsip_pubsub.h"
#include "asterisk/res_pjsip_body_generator_types.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/astobj2.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/serializer.h"
#include "asterisk/sorcery.h"
#include "asterisk/stasis.h"
#include "asterisk/mwi.h"

struct mwi_subscription;

AO2_GLOBAL_OBJ_STATIC(mwi_unsolicited);
AO2_GLOBAL_OBJ_STATIC(mwi_solicited);
#define STASIS_BUCKETS 13
#define MWI_BUCKETS 53

#define MWI_TYPE "application"
#define MWI_SUBTYPE "simple-message-summary"

#define MWI_DATASTORE "MWI datastore"

/*! Number of serializers in pool if one not supplied. */
#define MWI_SERIALIZER_POOL_SIZE 8

/*! Max timeout for all threads to join during an unload. */
#define MAX_UNLOAD_TIMEOUT_TIME 10 /* Seconds */

/*! Pool of serializers to use if not supplied. */
static struct ast_serializer_pool *mwi_serializer_pool;
static void mwi_subscription_shutdown(struct ast_sip_subscription *sub);
static void mwi_to_ami(struct ast_sip_subscription *sub, struct ast_str **buf);
static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
		const char *resource);
static int mwi_subscription_established(struct ast_sip_subscription *sub);
static void *mwi_get_notify_data(struct ast_sip_subscription *sub);

static struct ast_sip_notifier mwi_notifier = {
	.default_accept = MWI_TYPE"/"MWI_SUBTYPE,
	.new_subscribe = mwi_new_subscribe,
	.subscription_established = mwi_subscription_established,
	.get_notify_data = mwi_get_notify_data,

static struct ast_sip_subscription_handler mwi_handler = {
	.event_name = "message-summary",
	.body_type = AST_SIP_MESSAGE_ACCUMULATOR,
	.accept = { MWI_TYPE"/"MWI_SUBTYPE, },
	.subscription_shutdown = mwi_subscription_shutdown,
	.to_ami = mwi_to_ami,
	.notifier = &mwi_notifier,
};

/*!
 * \brief Wrapper for stasis subscription
 *
 * An MWI subscription has a container of these. This
 * represents a stasis subscription for MWI state.
 */
struct mwi_stasis_subscription {
	/*! The MWI stasis subscription */
	struct ast_mwi_subscriber *mwi_subscriber;
	/*! The mailbox corresponding with the MWI subscription. Used as a hash key */
	char mailbox[1];
};

/*!
 * \brief A subscription for MWI
 *
 * This subscription is the basis for MWI for an endpoint. Each
 * endpoint that uses MWI will have a corresponding mwi_subscription.
 *
 * This structure acts as the owner for the underlying SIP subscription.
 * When the mwi_subscription is destroyed, the SIP subscription dies, too.
 * The mwi_subscription's lifetime is governed by its underlying stasis
 * subscriptions. When all stasis subscriptions are destroyed, the
 * mwi_subscription is destroyed as well.
 */
struct mwi_subscription {
	/*! Container of \ref mwi_stasis_subscription structures.
	 * A single MWI subscription may be for multiple mailboxes, thus
	 * requiring multiple stasis subscriptions
	 */
	struct ao2_container *stasis_subs;
	/*! The SIP subscription. Unsolicited MWI does not use this */
	struct ast_sip_subscription *sip_sub;
	/*! AORs we should react to for unsolicited MWI NOTIFY */
	char *aors;
	/*! Is the MWI solicited (i.e. Initiated with an external SUBSCRIBE) ? */
	unsigned int is_solicited;
	/*! True if this subscription is to be terminated */
	unsigned int terminate;
	/*! Identifier for the subscription.
	 * The identifier is the same as the corresponding endpoint's stasis ID.
	 * Used as a hash key
	 */
	char id[1];
};

static void mwi_stasis_cb(void *userdata, struct stasis_subscription *sub,
		struct stasis_message *msg);

static struct mwi_stasis_subscription *mwi_stasis_subscription_alloc(const char *mailbox, struct mwi_subscription *mwi_sub)
{
	struct mwi_stasis_subscription *mwi_stasis_sub;
	if (!mwi_sub) {
		return NULL;
	}
	mwi_stasis_sub = ao2_alloc(sizeof(*mwi_stasis_sub) + strlen(mailbox), NULL);
	if (!mwi_stasis_sub) {
		return NULL;
	}

	/* Safe strcpy */
	strcpy(mwi_stasis_sub->mailbox, mailbox);
	ao2_ref(mwi_sub, +1);
	mwi_stasis_sub->mwi_subscriber = ast_mwi_subscribe_pool(mailbox, mwi_stasis_cb, mwi_sub);
	if (!mwi_stasis_sub->mwi_subscriber) {
		/* Failed to subscribe. */
		ao2_ref(mwi_stasis_sub, -1);
		ao2_ref(mwi_sub, -1);

	stasis_subscription_accept_message_type(
		ast_mwi_subscriber_subscription(mwi_stasis_sub->mwi_subscriber),
		stasis_subscription_change_type());

	return mwi_stasis_sub;
}

static int stasis_sub_hash(const void *obj, const int flags)
	const struct mwi_stasis_subscription *object;
	const char *key;
	switch (flags & OBJ_SEARCH_MASK) {
	case OBJ_SEARCH_KEY:
		key = obj;
		break;
	case OBJ_SEARCH_OBJECT:
		object = obj;
		key = object->mailbox;
		break;
	default:
		ast_assert(0);
		return 0;
	}
	return ast_str_hash(key);
}

static int stasis_sub_cmp(void *obj, void *arg, int flags)
{
	const struct mwi_stasis_subscription *sub_left = obj;
	const struct mwi_stasis_subscription *sub_right = arg;
	const char *right_key = arg;
	int cmp;

	switch (flags & OBJ_SEARCH_MASK) {
	case OBJ_SEARCH_OBJECT:
		right_key = sub_right->mailbox;
		/* Fall through */
	case OBJ_SEARCH_KEY:
		cmp = strcmp(sub_left->mailbox, right_key);
		break;
	case OBJ_SEARCH_PARTIAL_KEY:
		cmp = strncmp(sub_left->mailbox, right_key, strlen(right_key));
		break;
	default:
		cmp = 0;
		break;
	}
	if (cmp) {
		return 0;
	}
	return CMP_MATCH;
}

static void mwi_subscription_destructor(void *obj)
{
	struct mwi_subscription *sub = obj;

	ast_debug(3, "Destroying MWI subscription for endpoint %s\n", sub->id);
	if (sub->is_solicited) {
		ast_sip_subscription_destroy(sub->sip_sub);
	}
	ao2_cleanup(sub->stasis_subs);
}

static struct mwi_subscription *mwi_subscription_alloc(struct ast_sip_endpoint *endpoint,
		unsigned int is_solicited, struct ast_sip_subscription *sip_sub)
{
	struct mwi_subscription *sub;
	const char *endpoint_id = ast_sorcery_object_get_id(endpoint);

	sub = ao2_alloc(sizeof(*sub) + strlen(endpoint_id),
			mwi_subscription_destructor);

	if (!sub) {
		return NULL;
	}

	/* Safe strcpy */
	strcpy(sub->id, endpoint_id);
	/* Unsolicited MWI doesn't actually result in a SIP subscription being
	 * created. This is because a SIP subscription associates with a dialog.
	 * Most devices expect unsolicited MWI NOTIFYs to appear out of dialog. If
	 * they receive an in-dialog MWI NOTIFY (i.e. with a to-tag), then they
	 * will reject the NOTIFY with a 481, thus resulting in message-waiting
	 * state not being updated on the device
	 */
	if (is_solicited) {
	sub->stasis_subs = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
		STASIS_BUCKETS, stasis_sub_hash, NULL, stasis_sub_cmp);
	if (!sub->stasis_subs) {
		ao2_cleanup(sub);
		return NULL;
	}
	sub->is_solicited = is_solicited;

	if (!is_solicited && !ast_strlen_zero(endpoint->aors)) {
		sub->aors = ast_strdup(endpoint->aors);
		if (!sub->aors) {
			ao2_ref(sub, -1);
			return NULL;
		}
	}

	ast_debug(3, "Created %s MWI subscription for endpoint %s\n", is_solicited ? "solicited" : "unsolicited", sub->id);

	return sub;
}

static int mwi_sub_hash(const void *obj, const int flags)
	const struct mwi_subscription *object;
	const char *key;
	switch (flags & OBJ_SEARCH_MASK) {
	case OBJ_SEARCH_KEY:
		key = obj;
		break;
	case OBJ_SEARCH_OBJECT:
		object = obj;
		key = object->id;
		break;
	default:
		ast_assert(0);
		return 0;
	}
	return ast_str_hash(key);
}

static int mwi_sub_cmp(void *obj, void *arg, int flags)
{
	const struct mwi_subscription *sub_left = obj;
	const struct mwi_subscription *sub_right = arg;
	const char *right_key = arg;
	int cmp;

	switch (flags & OBJ_SEARCH_MASK) {
	case OBJ_SEARCH_OBJECT:
		right_key = sub_right->id;
		/* Fall through */
	case OBJ_SEARCH_KEY:
		cmp = strcmp(sub_left->id, right_key);
		break;
	case OBJ_SEARCH_PARTIAL_KEY:
		cmp = strncmp(sub_left->id, right_key, strlen(right_key));
		break;
	default:
		cmp = 0;
		break;
	}
	if (cmp) {
		return 0;
	}
	return CMP_MATCH;
}

static int get_message_count(void *obj, void *arg, int flags)
{
	struct mwi_stasis_subscription *mwi_stasis = obj;
	struct ast_sip_message_accumulator *counter = arg;
	struct ast_mwi_state *mwi_state;
	mwi_state = ast_mwi_subscriber_data(mwi_stasis->mwi_subscriber);
	if (!mwi_state) {
		return 0;
	}

	counter->old_msgs += mwi_state->old_msgs;
	counter->new_msgs += mwi_state->new_msgs;
static void set_voicemail_extension(pj_pool_t *pool, pjsip_sip_uri *local_uri,
	struct ast_sip_message_accumulator *counter, const char *voicemail_extension)
{
	pjsip_sip_uri *account_uri;
	const char *vm_exten;

	if (ast_strlen_zero(voicemail_extension)) {
		vm_exten = default_voicemail_extension;
	} else {
		vm_exten = voicemail_extension;
	}

	if (!ast_strlen_zero(vm_exten)) {
		account_uri = pjsip_uri_clone(pool, local_uri);
		pj_strdup2(pool, &account_uri->user, vm_exten);
		pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, account_uri, counter->message_account, sizeof(counter->message_account));
	}
}

struct unsolicited_mwi_data {
	struct mwi_subscription *sub;
	struct ast_sip_endpoint *endpoint;
	pjsip_evsub_state state;
	struct ast_sip_message_accumulator *counter;
};

static int send_unsolicited_mwi_notify_to_contact(void *obj, void *arg, int flags)
{
	struct unsolicited_mwi_data *mwi_data = arg;
	struct mwi_subscription *sub = mwi_data->sub;
	struct ast_sip_endpoint *endpoint = mwi_data->endpoint;
	pjsip_evsub_state state = mwi_data->state;
	struct ast_sip_contact *contact = obj;
	const char *state_name;
	pjsip_tx_data *tdata;
	pjsip_sub_state_hdr *sub_state;
	pjsip_event_hdr *event;
	pjsip_from_hdr *from;
	pjsip_sip_uri *from_uri;
	const pjsip_hdr *allow_events = pjsip_evsub_get_allow_events_hdr(NULL);
	struct ast_sip_body body;
	struct ast_str *body_text;
	struct ast_sip_body_data body_data = {
		.body_type = AST_SIP_MESSAGE_ACCUMULATOR,
		.body_data = mwi_data->counter,
	};
	if (ast_sip_create_request("NOTIFY", NULL, endpoint, NULL, contact, &tdata)) {
		ast_log(LOG_WARNING, "Unable to create unsolicited NOTIFY request to endpoint %s URI %s\n", sub->id, contact->uri);
		return 0;
	}

	body.type = MWI_TYPE;
	body.subtype = MWI_SUBTYPE;
	body_text = ast_str_create(64);
	if (!body_text) {
		pjsip_tx_data_dec_ref(tdata);
	from = PJSIP_MSG_FROM_HDR(tdata->msg);
	from_uri = pjsip_uri_get_uri(from->uri);

	if (!ast_strlen_zero(endpoint->subscription.mwi.fromuser)) {
		pj_strdup2(tdata->pool, &from_uri->user, endpoint->subscription.mwi.fromuser);
	set_voicemail_extension(tdata->pool, from_uri, mwi_data->counter, endpoint->subscription.mwi.voicemail_extension);

	if (ast_sip_pubsub_generate_body_content(body.type, body.subtype, &body_data, &body_text)) {
		ast_log(LOG_WARNING, "Unable to generate SIP MWI NOTIFY body.\n");
		ast_free(body_text);
		pjsip_tx_data_dec_ref(tdata);
	switch (state) {
	case PJSIP_EVSUB_STATE_ACTIVE:
		state_name = "active";
		break;
	case PJSIP_EVSUB_STATE_TERMINATED:
	default:
		state_name = "terminated";
		break;
	}

	sub_state = pjsip_sub_state_hdr_create(tdata->pool);
	pj_cstr(&sub_state->sub_state, state_name);
	pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) sub_state);

	event = pjsip_event_hdr_create(tdata->pool);
	pj_cstr(&event->event_type, "message-summary");
	pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) event);

	pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_shallow_clone(tdata->pool, allow_events));
	ast_sip_send_request(tdata, NULL, endpoint, NULL, NULL);
static struct ast_sip_aor *find_aor_for_resource(struct ast_sip_endpoint *endpoint, const char *resource)
{
	struct ast_sip_aor *aor;
	char *aor_name;
	char *aors_copy;

	/* Direct match */
	if ((aor = ast_sip_location_retrieve_aor(resource))) {
		return aor;
	}

	if (!endpoint) {
		return NULL;
	}

	/*
	 * This may be a subscribe to the voicemail_extension.  If so,
	 * look for an aor belonging to this endpoint that has a matching
	 * voicemail_extension.
	 */
	aors_copy = ast_strdupa(endpoint->aors);
	while ((aor_name = ast_strip(strsep(&aors_copy, ",")))) {
		struct ast_sip_aor *check_aor = ast_sip_location_retrieve_aor(aor_name);

		if (!check_aor) {
			continue;
		}

		if (!ast_strlen_zero(check_aor->voicemail_extension)
			&& !strcasecmp(check_aor->voicemail_extension, resource)) {
			ast_debug(1, "Found an aor (%s) that matches voicemail_extension %s\n", aor_name, resource);
			return check_aor;
		}

		ao2_ref(check_aor, -1);
	}

	return NULL;
}

static void send_unsolicited_mwi_notify(struct mwi_subscription *sub,
		struct ast_sip_message_accumulator *counter)
{
	RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(),
				"endpoint", sub->id), ao2_cleanup);
	char *endpoint_aors;
	char *aor_name;

	if (!endpoint) {
		ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because endpoint does not exist\n",
				sub->id);
		return;
	}
	if (ast_strlen_zero(endpoint->aors)) {
		ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because the endpoint has no"
				" configured AORs\n", sub->id);
		return;
	}

	endpoint_aors = ast_strdupa(endpoint->aors);

	ast_debug(5, "Sending unsolicited MWI NOTIFY to endpoint %s, new messages: %d, old messages: %d\n",
			sub->id, counter->new_msgs, counter->old_msgs);

	while ((aor_name = ast_strip(strsep(&endpoint_aors, ",")))) {
		RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
		RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
		struct unsolicited_mwi_data mwi_data = {
			.sub = sub,
			.endpoint = endpoint,
		};

		if (!aor) {
			ast_log(LOG_WARNING, "Unable to locate AOR %s for unsolicited MWI\n", aor_name);
			continue;
		}

		contacts = ast_sip_location_retrieve_aor_contacts(aor);
		if (!contacts || (ao2_container_count(contacts) == 0)) {
			ast_debug(1, "No contacts bound to AOR %s. Cannot send unsolicited MWI until a contact registers.\n", aor_name);
			continue;
		}

		ao2_callback(contacts, OBJ_NODATA, send_unsolicited_mwi_notify_to_contact, &mwi_data);
	}
}

static void send_mwi_notify(struct mwi_subscription *sub)
	struct ast_sip_message_accumulator counter = {
		.old_msgs = 0,
		.new_msgs = 0,
	struct ast_sip_body_data data = {
		.body_type = AST_SIP_MESSAGE_ACCUMULATOR,
		.body_data = &counter,
	};

	ao2_callback(sub->stasis_subs, OBJ_NODATA, get_message_count, &counter);

	if (sub->is_solicited) {
		const char *resource = ast_sip_subscription_get_resource_name(sub->sip_sub);
		struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sub->sip_sub);
		struct ast_sip_aor *aor = find_aor_for_resource(endpoint, resource);
		pjsip_dialog *dlg = ast_sip_subscription_get_dialog(sub->sip_sub);
		pjsip_sip_uri *sip_uri = ast_sip_subscription_get_sip_uri(sub->sip_sub);

		if (aor && dlg && sip_uri) {
			set_voicemail_extension(dlg->pool, sip_uri, &counter, aor->voicemail_extension);
		}

		ao2_cleanup(aor);
		ast_sip_subscription_notify(sub->sip_sub, &data, sub->terminate);
	send_unsolicited_mwi_notify(sub, &counter);
}

static int unsubscribe_stasis(void *obj, void *arg, int flags)
{
	struct mwi_stasis_subscription *mwi_stasis = obj;
	if (mwi_stasis->mwi_subscriber) {
		ast_debug(3, "Removing stasis subscription to mailbox %s\n", mwi_stasis->mailbox);
		mwi_stasis->mwi_subscriber = ast_mwi_unsubscribe_and_join(mwi_stasis->mwi_subscriber);
static int create_unsolicited_mwi_subscriptions(struct ast_sip_endpoint *endpoint,
	int recreate, int send_now, struct ao2_container *unsolicited_mwi, struct ao2_container *solicited_mwi);
static void mwi_subscription_shutdown(struct ast_sip_subscription *sub)
{
	struct mwi_subscription *mwi_sub;
	struct ast_datastore *mwi_datastore;
	struct ast_sip_endpoint *endpoint = NULL;
	struct ao2_container *unsolicited_mwi;
	struct ao2_container *solicited_mwi;
	mwi_datastore = ast_sip_subscription_get_datastore(sub, MWI_DATASTORE);
	if (!mwi_datastore) {
		return;
	}

	mwi_sub = mwi_datastore->data;
	ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
	ast_sip_subscription_remove_datastore(sub, MWI_DATASTORE);
	endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", mwi_sub->id);

	solicited_mwi = ao2_global_obj_ref(mwi_solicited);
	if (solicited_mwi) {
		ao2_unlink(solicited_mwi, mwi_sub);
	}

	/*
	 * When a solicited subscription is removed it's possible an unsolicited one
	 * needs to be [re-]created. Attempt to establish unsolicited MWI.
	 */
	unsolicited_mwi = ao2_global_obj_ref(mwi_unsolicited);
	if (unsolicited_mwi && endpoint) {
		ao2_lock(unsolicited_mwi);
		create_unsolicited_mwi_subscriptions(endpoint, 1, 1, unsolicited_mwi, solicited_mwi);
		ao2_ref(unsolicited_mwi, -1);
	ao2_cleanup(solicited_mwi);
static void mwi_ds_destroy(void *data)
{
	struct mwi_subscription *sub = data;

	ao2_ref(sub, -1);
}

static struct ast_datastore_info mwi_ds_info = {
	.destroy = mwi_ds_destroy,
};

static int add_mwi_datastore(struct mwi_subscription *sub)
{
	struct ast_datastore *mwi_datastore;
	mwi_datastore = ast_sip_subscription_alloc_datastore(&mwi_ds_info, MWI_DATASTORE);
	if (!mwi_datastore) {
		return -1;
	}
	mwi_datastore->data = sub;

	/*
	 * NOTE:  Adding the datastore to the subscription creates a ref loop
	 * that must be manually broken.
	 */
	res = ast_sip_subscription_add_datastore(sub->sip_sub, mwi_datastore);
	ao2_ref(mwi_datastore, -1);
 * \internal
 * \brief Determine if an MWI subscription already exists for the given endpoint/mailbox
 *
 * Search the given container, and attempt to find out if the given endpoint has a
 * current subscription within. If so pass back the associated mwi_subscription and
 * mwi_stasis_subscription objects.
 *
 * \note If a subscription is located then the caller is responsible for removing the
 * references to the passed back mwi_subscription and mwi_stasis_subscription objects.
 *
 * \note Must be called with the given container already locked.
 * \param container The ao2_container to search
 * \param endpoint The endpoint to find
 * \param mailbox The mailbox potentially subscribed
Alexander Traud's avatar
Alexander Traud committed
 * \param[out] mwi_sub May contain the located mwi_subscription
 * \param[out] mwi_stasis May contain the located mwi_stasis_subscription
 *
 * \retval 1 if a subscription was located, 0 otherwise
static int has_mwi_subscription(struct ao2_container *container,
		struct ast_sip_endpoint *endpoint, const char *mailbox,
		struct mwi_subscription **mwi_sub, struct mwi_stasis_subscription **mwi_stasis)
	mwi_subs = ao2_find(container, ast_sorcery_object_get_id(endpoint),
						OBJ_SEARCH_KEY | OBJ_MULTIPLE | OBJ_NOLOCK);
	while ((*mwi_sub = ao2_iterator_next(mwi_subs))) {
		*mwi_stasis = ao2_find((*mwi_sub)->stasis_subs, mailbox, OBJ_SEARCH_KEY);
		if (*mwi_stasis) {
			/* If found then caller is responsible for unrefs of passed back objects */
			break;
	return *mwi_stasis ? 1 : 0;
}

/*!
 * \internal
 * \brief Allow and/or replace the unsolicited subscription
 *
 * Checks to see if solicited subscription is allowed. If allowed, and an
 * unsolicited one exists then prepare for replacement by removing the
 * current unsolicited subscription.
 *
 * \param endpoint The endpoint
 * \param mailbox The mailbox
 * \param unsolicited_mwi A container of unsolicited mwi objects
 *
 * \retval 1 if a solicited subscription is allowed for the endpoint/mailbox
 *         0 otherwise
 */
static int allow_and_or_replace_unsolicited(struct ast_sip_endpoint *endpoint, const char *mailbox,
	struct ao2_container *unsolicited_mwi)
{
	struct mwi_subscription *mwi_sub;
	struct mwi_stasis_subscription *mwi_stasis;

	if (!has_mwi_subscription(unsolicited_mwi, endpoint, mailbox, &mwi_sub, &mwi_stasis)) {
		/* If no unsolicited subscription then allow the solicited one */
		return 1;
	}

	if (!endpoint->subscription.mwi.subscribe_replaces_unsolicited) {
		/* Has unsolicited subscription and can't replace, so disallow */
		ao2_ref(mwi_stasis, -1);
		ao2_ref(mwi_sub, -1);
		return 0;
	}

	/*
	 * The unsolicited subscription exists, and it is allowed to be replaced.
	 * So, first remove the unsolicited stasis subscription, and if aggregation
	 * is not enabled then also remove the mwi_subscription object as well.
	 */
	ast_debug(1, "Unsolicited subscription being replaced by solicited for "
			"endpoint '%s' mailbox '%s'\n", ast_sorcery_object_get_id(endpoint), mailbox);

	unsubscribe_stasis(mwi_stasis, NULL, 0);
	ao2_unlink(mwi_sub->stasis_subs, mwi_stasis);

	if (!endpoint->subscription.mwi.aggregate) {
		ao2_unlink(unsolicited_mwi, mwi_sub);
	}

	ao2_ref(mwi_stasis, -1);
	ao2_ref(mwi_sub, -1);

	/* This solicited subscription is replacing an unsolicited one, so allow */
	return 1;
}

static int send_notify(void *obj, void *arg, int flags);

/*!
 * \internal
 * \brief Determine if an unsolicited MWI subscription is allowed
 *
 * \param endpoint The endpoint
 * \param mailbox The mailbox
 * \param unsolicited_mwi A container of unsolicited mwi objects
 * \param solicited_mwi A container of solicited mwi objects
 *
 * \retval 1 if an unsolicited subscription is allowed for the endpoint/mailbox
 *         0 otherwise
 */
static int is_unsolicited_allowed(struct ast_sip_endpoint *endpoint, const char *mailbox,
	struct ao2_container *unsolicited_mwi, struct ao2_container *solicited_mwi)
{
	struct mwi_subscription *mwi_sub;
	struct mwi_stasis_subscription *mwi_stasis;

	if (ast_strlen_zero(mailbox)) {
		return 0;
	}

	/*
	 * First check if an unsolicited subscription exists. If it does then we don't
	 * want to add another one.
	 */
	if (has_mwi_subscription(unsolicited_mwi, endpoint, mailbox, &mwi_sub, &mwi_stasis)) {
		ao2_ref(mwi_stasis, -1);
		ao2_ref(mwi_sub, -1);
		return 0;
	}

	/*
	 * If there is no unsolicited subscription, next check to see if a solicited
	 * subscription exists for the endpoint/mailbox. If not, then allow.
	 */
	if (!has_mwi_subscription(solicited_mwi, endpoint, mailbox, &mwi_sub, &mwi_stasis)) {
		return 1;
	}

	/*
	 * If however, a solicited subscription does exist then we'll need to see if that
	 * subscription is allowed to replace the unsolicited one. If is allowed to replace
	 * then disallow the unsolicited one.
	 */
	if (endpoint->subscription.mwi.subscribe_replaces_unsolicited) {
		ao2_ref(mwi_stasis, -1);
		ao2_ref(mwi_sub, -1);
		return 0;
	}

	/* Otherwise, shutdown the solicited subscription and allow the unsolicited */
	mwi_sub->terminate = 1;
	send_notify(mwi_sub, NULL, 0);

	ao2_ref(mwi_stasis, -1);
	ao2_ref(mwi_sub, -1);

	return 1;
}

/*!
 * \brief Determine if an endpoint is a candidate to be able to subscribe for MWI
 *
 * Currently, this just makes sure that the endpoint is not already receiving unsolicted
 * MWI for any of an AOR's configured mailboxes.
 *
 * \param obj The AOR to which the endpoint is subscribing.
 * \param arg The endpoint that is attempting to subscribe.
 * \param flags Unused.
 * \retval 0 Endpoint is a candidate to subscribe to MWI on the AOR.
 * \retval -1 The endpoint cannot subscribe to MWI on the AOR.
 */
static int mwi_validate_for_aor(void *obj, void *arg, int flags)
{
	struct ast_sip_aor *aor = obj;
	struct ast_sip_endpoint *endpoint = arg;
	char *mailboxes;
	char *mailbox;
	struct ao2_container *unsolicited_mwi;
	/* A reload could be taking place so lock while checking if allowed */
	unsolicited_mwi = ao2_global_obj_ref(mwi_unsolicited);
	if (unsolicited_mwi) {
		ao2_lock(unsolicited_mwi);
	}

	mailboxes = ast_strdupa(aor->mailboxes);
	while ((mailbox = ast_strip(strsep(&mailboxes, ",")))) {
		if (ast_strlen_zero(mailbox)) {
			continue;
		}

		if (!allow_and_or_replace_unsolicited(endpoint, mailbox, unsolicited_mwi)) {
			ast_debug(1, "Endpoint '%s' already configured for unsolicited MWI for mailbox '%s'. "
					"Denying MWI subscription to %s\n", ast_sorcery_object_get_id(endpoint), mailbox,
					ast_sorcery_object_get_id(aor));

			if (unsolicited_mwi) {
				ao2_unlock(unsolicited_mwi);
				ao2_ref(unsolicited_mwi, -1);
			}

	if (unsolicited_mwi) {
		ao2_unlock(unsolicited_mwi);
		ao2_ref(unsolicited_mwi, -1);
	}
static int mwi_on_aor(void *obj, void *arg, int flags)
	struct ast_sip_aor *aor = obj;
	struct mwi_subscription *sub = arg;
	char *mailboxes;
	char *mailbox;

	if (ast_strlen_zero(aor->mailboxes)) {
		return 0;
	}

	mailboxes = ast_strdupa(aor->mailboxes);
	while ((mailbox = ast_strip(strsep(&mailboxes, ",")))) {
		struct mwi_stasis_subscription *mwi_stasis_sub;

		mwi_stasis_sub = mwi_stasis_subscription_alloc(mailbox, sub);
		if (!mwi_stasis_sub) {
			continue;

		ao2_link(sub->stasis_subs, mwi_stasis_sub);
		ao2_ref(mwi_stasis_sub, -1);
	}

	return 0;
}

static struct mwi_subscription *mwi_create_subscription(
	struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub)
	struct mwi_subscription *sub = mwi_subscription_alloc(endpoint, 1, sip_sub);
	if (add_mwi_datastore(sub)) {
		ast_log(LOG_WARNING, "Unable to add datastore for MWI subscription to %s\n",
			sub->id);
		ao2_ref(sub, -1);
		return NULL;
	}

	return sub;
}

static struct mwi_subscription *mwi_subscribe_single(
	struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub, const char *name)
	struct ast_sip_aor *aor;
	struct mwi_subscription *sub;

	aor = find_aor_for_resource(endpoint, name);
		ast_log(LOG_WARNING, "Unable to locate aor %s. MWI subscription failed.\n", name);
	sub = mwi_create_subscription(endpoint, sip_sub);
	if (sub) {
		mwi_on_aor(aor, sub, 0);
	return sub;
}

static struct mwi_subscription *mwi_subscribe_all(
	struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub)
	sub = mwi_create_subscription(endpoint, sip_sub);
	ast_sip_for_each_aor(endpoint->aors, mwi_on_aor, sub);
	return sub;
}

static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
		const char *resource)
	RAII_VAR(struct ast_sip_aor *, aor, NULL, ao2_cleanup);
	if (ast_strlen_zero(resource)) {
		if (ast_sip_for_each_aor(endpoint->aors, mwi_validate_for_aor, endpoint)) {
			return 500;
		}
		return 200;
	aor = find_aor_for_resource(endpoint, resource);
		ast_debug(1, "Unable to locate aor %s. MWI subscription failed.\n", resource);
	if (ast_strlen_zero(aor->mailboxes)) {
		ast_debug(1, "AOR %s has no configured mailboxes. MWI subscription failed.\n",
	if (mwi_validate_for_aor(aor, endpoint, 0)) {
		return 500;
	}