Newer
Older
Mark Michelson
committed
}
return 0;
}
static int unsubscribe(void *obj, void *arg, int flags)
{
struct mwi_subscription *mwi_sub = obj;
ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
Joshua Colp
committed
return CMP_MATCH;
}
static void create_mwi_subscriptions(void)
{
struct ao2_container *endpoints;
struct ast_variable *var;
var = ast_variable_new("mailboxes !=", "", "");
endpoints = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "endpoint",
AST_RETRIEVE_FLAG_MULTIPLE, var);
ast_variables_destroy(var);
if (!endpoints) {
return;
}
/* We remove all the old stasis subscriptions first before applying the new configuration. This
* prevents a situation where there might be multiple overlapping stasis subscriptions for an
* endpoint for mailboxes. Though there may be mailbox changes during the gap between unsubscribing
* and resubscribing, up-to-date mailbox state will be sent out to the endpoint when the
* new stasis subscription is established
*/
ao2_lock(unsolicited_mwi);
ao2_callback(unsolicited_mwi, OBJ_NOLOCK | OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
ao2_callback(endpoints, OBJ_NODATA, create_mwi_subscriptions_for_endpoint, NULL);
ao2_unlock(unsolicited_mwi);
ao2_ref(endpoints, -1);
Joshua Colp
committed
/*! \brief Function called to send MWI NOTIFY on any unsolicited mailboxes relating to this AOR */
static int send_contact_notify(void *obj, void *arg, int flags)
{
struct mwi_subscription *mwi_sub = obj;
const char *aor = arg;
if (!mwi_sub->aors || !strstr(mwi_sub->aors, aor)) {
return 0;
}
if (ast_sip_push_task(NULL, serialized_notify, ao2_bump(mwi_sub))) {
ao2_ref(mwi_sub, -1);
}
return 0;
}
/*! \brief Function called when a contact is updated */
static void mwi_contact_updated(const void *object)
Joshua Colp
committed
{
char *id = ast_strdupa(ast_sorcery_object_get_id(object)), *aor = NULL;
aor = strsep(&id, ";@");
ao2_callback(unsolicited_mwi, OBJ_NODATA, send_contact_notify, aor);
}
/*! \brief Function called when a contact is added */
static void mwi_contact_added(const void *object)
{
const struct ast_sip_contact *contact = object;
struct ao2_iterator *mwi_subs;
struct mwi_subscription *mwi_sub;
const char *endpoint_id = ast_sorcery_object_get_id(contact->endpoint);
if (ast_strlen_zero(contact->endpoint->subscription.mwi.mailboxes)) {
Joshua Colp
committed
return;
}
ao2_lock(unsolicited_mwi);
mwi_subs = ao2_find(unsolicited_mwi, endpoint_id, OBJ_SEARCH_KEY | OBJ_MULTIPLE | OBJ_NOLOCK | OBJ_UNLINK);
if (mwi_subs) {
for (; (mwi_sub = ao2_iterator_next(mwi_subs)); ao2_cleanup(mwi_sub)) {
unsubscribe(mwi_sub, NULL, 0);
}
ao2_iterator_destroy(mwi_subs);
}
create_mwi_subscriptions_for_endpoint(contact->endpoint, NULL, 0);
ao2_unlock(unsolicited_mwi);
mwi_contact_updated(object);
Joshua Colp
committed
}
/*! \brief Observer for contacts so unsolicited MWI is sent when a contact changes */
static const struct ast_sorcery_observer mwi_contact_observer = {
.created = mwi_contact_added,
.updated = mwi_contact_updated,
Joshua Colp
committed
};
/*! \brief Task invoked to send initial MWI NOTIFY for unsolicited */
static int send_initial_notify_all(void *obj)
{
ao2_callback(unsolicited_mwi, OBJ_NODATA, send_notify, NULL);
Joshua Colp
committed
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
return 0;
}
/*! \brief Event callback which fires initial unsolicited MWI NOTIFY messages when we're fully booted */
static void mwi_startup_event_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
{
struct ast_json_payload *payload;
const char *type;
if (stasis_message_type(message) != ast_manager_get_generic_type()) {
return;
}
payload = stasis_message_data(message);
type = ast_json_string_get(ast_json_object_get(payload->json, "type"));
if (strcmp(type, "FullyBooted")) {
return;
}
ast_sip_push_task(NULL, send_initial_notify_all, NULL);
stasis_unsubscribe(sub);
}
George Joseph
committed
static void global_loaded(const char *object_type)
{
ast_free(default_voicemail_extension);
default_voicemail_extension = ast_sip_get_default_voicemail_extension();
}
static struct ast_sorcery_observer global_observer = {
.loaded = global_loaded,
};
static int reload(void)
{
create_mwi_subscriptions();
return 0;
}
static int load_module(void)
{
CHECK_PJSIP_MODULE_LOADED();
if (ast_sip_register_subscription_handler(&mwi_handler)) {
return AST_MODULE_LOAD_DECLINE;
}
unsolicited_mwi = ao2_container_alloc(MWI_BUCKETS, mwi_sub_hash, mwi_sub_cmp);
if (!unsolicited_mwi) {
ast_sip_unregister_subscription_handler(&mwi_handler);
return AST_MODULE_LOAD_DECLINE;
}
Joshua Colp
committed
ast_sorcery_observer_add(ast_sip_get_sorcery(), "contact", &mwi_contact_observer);
George Joseph
committed
ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &global_observer);
ast_sorcery_reload_object(ast_sip_get_sorcery(), "global");
Joshua Colp
committed
if (ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)) {
ast_sip_push_task(NULL, send_initial_notify_all, NULL);
} else {
stasis_subscribe_pool(ast_manager_get_topic(), mwi_startup_event_cb, NULL);
}
return AST_MODULE_LOAD_SUCCESS;
}
static int unload_module(void)
{
ao2_callback(unsolicited_mwi, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
ao2_ref(unsolicited_mwi, -1);
George Joseph
committed
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "global", &global_observer);
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "contact", &mwi_contact_observer);
ast_sip_unregister_subscription_handler(&mwi_handler);
George Joseph
committed
ast_free(default_voicemail_extension);
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP MWI resource",
Rodrigo Ramírez Norambuena
committed
.support_level = AST_MODULE_SUPPORT_CORE,
.load = load_module,
.unload = unload_module,
.reload = reload,
.load_pri = AST_MODPRI_CHANNEL_DEPEND,