2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
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.
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.
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <support_level>core</support_level>
28 #include <pjsip_simple.h>
31 #include "asterisk/res_pjsip.h"
32 #include "asterisk/res_pjsip_pubsub.h"
33 #include "asterisk/module.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/astobj2.h"
36 #include "asterisk/sorcery.h"
37 #include "asterisk/stasis.h"
38 #include "asterisk/app.h"
41 <depend>pjproject</depend>
42 <depend>res_pjsip</depend>
43 <depend>res_pjsip_pubsub</depend>
44 <support_level>core</support_level>
47 struct mwi_subscription;
48 AO2_GLOBAL_OBJ_STATIC(unsolicited_mwi);
50 #define STASIS_BUCKETS 13
51 #define MWI_BUCKETS 53
52 static void mwi_subscription_shutdown(struct ast_sip_subscription *sub);
53 static struct ast_sip_subscription *mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
54 pjsip_rx_data *rdata);
55 static void mwi_resubscribe(struct ast_sip_subscription *sub, pjsip_rx_data *rdata,
56 struct ast_sip_subscription_response_data *response_data);
57 static void mwi_subscription_timeout(struct ast_sip_subscription *sub);
58 static void mwi_subscription_terminated(struct ast_sip_subscription *sub, pjsip_rx_data *rdata);
59 static void mwi_notify_response(struct ast_sip_subscription *sub, pjsip_rx_data *rdata);
60 static void mwi_notify_request(struct ast_sip_subscription *sub, pjsip_rx_data *rdata,
61 struct ast_sip_subscription_response_data *response_data);
62 static int mwi_refresh_subscription(struct ast_sip_subscription *sub);
64 static struct ast_sip_subscription_handler mwi_handler = {
65 .event_name = "message-summary",
66 .accept = { "application/simple-message-summary", },
67 .handles_default_accept = 1,
68 .subscription_shutdown = mwi_subscription_shutdown,
69 .new_subscribe = mwi_new_subscribe,
70 .resubscribe = mwi_resubscribe,
71 .subscription_timeout = mwi_subscription_timeout,
72 .subscription_terminated = mwi_subscription_terminated,
73 .notify_response = mwi_notify_response,
74 .notify_request = mwi_notify_request,
75 .refresh_subscription = mwi_refresh_subscription,
79 * \brief Wrapper for stasis subscription
81 * An MWI subscription has a container of these. This
82 * represents a stasis subscription for MWI state.
84 struct mwi_stasis_subscription {
85 /*! The MWI stasis subscription */
86 struct stasis_subscription *stasis_sub;
87 /*! The mailbox corresponding with the MWI subscription. Used as a hash key */
92 * \brief A subscription for MWI
94 * This subscription is the basis for MWI for an endpoint. Each
95 * endpoint that uses MWI will have a corresponding mwi_subscription.
97 * This structure acts as the owner for the underlying SIP subscription.
98 * When the mwi_subscription is destroyed, the SIP subscription dies, too.
99 * The mwi_subscription's lifetime is governed by its underlying stasis
100 * subscriptions. When all stasis subscriptions are destroyed, the
101 * mwi_subscription is destroyed as well.
103 struct mwi_subscription {
104 /*! Container of \ref mwi_stasis_subscription structures.
105 * A single MWI subscription may be fore multiple mailboxes, thus
106 * requiring multiple stasis subscriptions
108 struct ao2_container *stasis_subs;
109 /*! The SIP subscription. Unsolicited MWI does not use this */
110 struct ast_sip_subscription *sip_sub;
111 /*! Is the MWI solicited (i.e. Initiated with an external SUBSCRIBE) ? */
112 unsigned int is_solicited;
113 /*! Identifier for the subscription.
114 * The identifier is the same as the corresponding endpoint's stasis ID.
120 static void mwi_stasis_cb(void *userdata, struct stasis_subscription *sub,
121 struct stasis_topic *topic, struct stasis_message *msg);
123 static struct mwi_stasis_subscription *mwi_stasis_subscription_alloc(const char *mailbox, struct mwi_subscription *mwi_sub)
125 struct mwi_stasis_subscription *mwi_stasis_sub;
126 struct stasis_topic *topic;
132 mwi_stasis_sub = ao2_alloc(sizeof(*mwi_stasis_sub) + strlen(mailbox), NULL);
133 if (!mwi_stasis_sub) {
137 topic = ast_mwi_topic(mailbox);
140 strcpy(mwi_stasis_sub->mailbox, mailbox);
141 ao2_ref(mwi_sub, +1);
142 ast_debug(3, "Creating stasis MWI subscription to mailbox %s for endpoint %s\n", mailbox, mwi_sub->id);
143 mwi_stasis_sub->stasis_sub = stasis_subscribe(topic, mwi_stasis_cb, mwi_sub);
144 return mwi_stasis_sub;
147 static int stasis_sub_hash(const void *obj, int flags)
149 const struct mwi_stasis_subscription *mwi_stasis = obj;
151 return ast_str_hash(mwi_stasis->mailbox);
154 static int stasis_sub_cmp(void *obj, void *arg, int flags)
156 struct mwi_stasis_subscription *mwi_stasis1 = obj;
157 struct mwi_stasis_subscription *mwi_stasis2 = arg;
159 return strcmp(mwi_stasis1->mailbox, mwi_stasis2->mailbox) ? 0 : CMP_MATCH;
162 static void mwi_subscription_destructor(void *obj)
164 struct mwi_subscription *sub = obj;
166 ast_debug(3, "Destroying MWI subscription for endpoint %s\n", sub->id);
167 ao2_cleanup(sub->sip_sub);
168 ao2_cleanup(sub->stasis_subs);
171 static struct mwi_subscription *mwi_subscription_alloc(struct ast_sip_endpoint *endpoint,
172 enum ast_sip_subscription_role role, unsigned int is_solicited, pjsip_rx_data *rdata)
174 struct mwi_subscription *sub;
175 const char *endpoint_id = ast_sorcery_object_get_id(endpoint);
177 sub = ao2_alloc(sizeof(*sub) + strlen(endpoint_id),
178 mwi_subscription_destructor);
185 strcpy(sub->id, endpoint_id);
186 /* Unsolicited MWI doesn't actually result in a SIP subscription being
187 * created. This is because a SIP subscription associates with a dialog.
188 * Most devices expect unsolicited MWI NOTIFYs to appear out of dialog. If
189 * they receive an in-dialog MWI NOTIFY (i.e. with a to-tag), then they
190 * will reject the NOTIFY with a 481, thus resulting in message-waiting
191 * state not being updated on the device
194 sub->sip_sub = ast_sip_create_subscription(&mwi_handler,
195 role, endpoint, rdata);
197 ast_log(LOG_WARNING, "Unable to create MWI SIP subscription for endpoint %s\n", sub->id);
203 sub->stasis_subs = ao2_container_alloc(STASIS_BUCKETS, stasis_sub_hash, stasis_sub_cmp);
204 if (!sub->stasis_subs) {
208 sub->is_solicited = is_solicited;
210 ast_debug(3, "Created %s MWI subscription for endpoint %s\n", is_solicited ? "solicited" : "unsolicited", sub->id);
215 static int mwi_sub_hash(const void *obj, int flags)
217 const struct mwi_subscription *mwi_sub = obj;
219 return ast_str_hash(mwi_sub->id);
222 static int mwi_sub_cmp(void *obj, void *arg, int flags)
224 struct mwi_subscription *mwi_sub1 = obj;
225 struct mwi_subscription *mwi_sub2 = arg;
227 return strcmp(mwi_sub1->id, mwi_sub2->id) ? 0 : CMP_MATCH;
230 struct message_accumulator {
236 static int get_message_count(void *obj, void *arg, int flags)
238 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
239 struct mwi_stasis_subscription *mwi_stasis = obj;
240 struct message_accumulator *counter = arg;
241 struct ast_mwi_state *mwi_state;
243 msg = stasis_cache_get(ast_mwi_state_cache(), ast_mwi_state_type(), mwi_stasis->mailbox);
248 mwi_state = stasis_message_data(msg);
249 counter->old_msgs += mwi_state->old_msgs;
250 counter->new_msgs += mwi_state->new_msgs;
254 struct unsolicited_mwi_data {
255 struct mwi_subscription *sub;
256 struct ast_sip_endpoint *endpoint;
257 pjsip_evsub_state state;
259 const pjsip_media_type *mwi_type;
260 const pj_str_t *body_text;
263 static int send_unsolicited_mwi_notify_to_contact(void *obj, void *arg, int flags)
265 struct unsolicited_mwi_data *mwi_data = arg;
266 struct mwi_subscription *sub = mwi_data->sub;
267 struct ast_sip_endpoint *endpoint = mwi_data->endpoint;
268 pjsip_evsub_state state = mwi_data->state;
269 const char *reason = mwi_data->reason;
270 const pjsip_media_type *mwi_type = mwi_data->mwi_type;
271 const pj_str_t *body_text = mwi_data->body_text;
272 struct ast_sip_contact *contact = obj;
273 const char *state_name;
274 pjsip_tx_data *tdata;
275 pjsip_msg_body *msg_body;
276 pjsip_sub_state_hdr *sub_state;
277 pjsip_event_hdr *event;
278 const pjsip_hdr *allow_events = pjsip_evsub_get_allow_events_hdr(NULL);
280 if (ast_sip_create_request("NOTIFY", NULL, endpoint, contact->uri, &tdata)) {
281 ast_log(LOG_WARNING, "Unable to create unsolicited NOTIFY request to endpoint %s URI %s\n", sub->id, contact->uri);
285 if (!ast_strlen_zero(endpoint->subscription.mwi.fromuser)) {
286 pjsip_fromto_hdr *from = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM, NULL);
287 pjsip_name_addr *from_name_addr = (pjsip_name_addr *) from->uri;
288 pjsip_sip_uri *from_uri = pjsip_uri_get_uri(from_name_addr->uri);
290 pj_strdup2(tdata->pool, &from_uri->user, endpoint->subscription.mwi.fromuser);
294 case PJSIP_EVSUB_STATE_ACTIVE:
295 state_name = "active";
297 case PJSIP_EVSUB_STATE_TERMINATED:
299 state_name = "terminated";
303 sub_state = pjsip_sub_state_hdr_create(tdata->pool);
304 pj_cstr(&sub_state->sub_state, state_name);
306 pj_cstr(&sub_state->reason_param, reason);
308 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) sub_state);
310 event = pjsip_event_hdr_create(tdata->pool);
311 pj_cstr(&event->event_type, "message-summary");
312 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) event);
314 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_shallow_clone(tdata->pool, allow_events));
315 msg_body = pjsip_msg_body_create(tdata->pool, &mwi_type->type, &mwi_type->subtype, body_text);
316 tdata->msg->body = msg_body;
317 ast_sip_send_request(tdata, NULL, endpoint);
322 static void send_unsolicited_mwi_notify(struct mwi_subscription *sub, pjsip_evsub_state state, const char *reason,
323 const pjsip_media_type *mwi_type, const pj_str_t *body_text)
325 RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(),
326 "endpoint", sub->id), ao2_cleanup);
331 ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because endpoint does not exist\n",
335 if (ast_strlen_zero(endpoint->aors)) {
336 ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because the endpoint has no"
337 " configured AORs\n", sub->id);
341 endpoint_aors = ast_strdupa(endpoint->aors);
343 while ((aor_name = strsep(&endpoint_aors, ","))) {
344 RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
345 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
346 struct unsolicited_mwi_data mwi_data = {
348 .endpoint = endpoint,
351 .mwi_type = mwi_type,
352 .body_text = body_text,
356 ast_log(LOG_WARNING, "Unable to locate AOR %s for unsolicited MWI\n", aor_name);
360 contacts = ast_sip_location_retrieve_aor_contacts(aor);
361 if (!contacts || (ao2_container_count(contacts) == 0)) {
362 ast_log(LOG_WARNING, "No contacts bound to AOR %s. Cannot send unsolicited MWI.\n", aor_name);
366 ao2_callback(contacts, OBJ_NODATA, send_unsolicited_mwi_notify_to_contact, &mwi_data);
370 static void send_mwi_notify(struct mwi_subscription *sub, pjsip_evsub_state state, const char *reason)
372 const pj_str_t *reason_str_ptr = NULL;
373 static pjsip_media_type mwi_type = {
374 .type = { "application", 11 },
375 .subtype = { "simple-message-summary", 22 },
377 struct message_accumulator counter = {
381 RAII_VAR(struct ast_str *, body, ast_str_create(64), ast_free_ptr);
382 pjsip_tx_data *tdata;
386 ao2_callback(sub->stasis_subs, OBJ_NODATA, get_message_count, &counter);
389 pj_cstr(&reason_str, reason);
390 reason_str_ptr = &reason_str;
392 ast_str_append(&body, 0, "Messages-Waiting: %s\r\n", counter.new_msgs ? "yes" : "no");
393 ast_str_append(&body, 0, "Voice-Message: %d/%d (0/0)\r\n", counter.new_msgs, counter.old_msgs);
394 pj_cstr(&pj_body, ast_str_buffer(body));
396 ast_debug(5, "Sending %s MWI NOTIFY to endpoint %s, new messages: %d, old messages: %d\n",
397 sub->is_solicited ? "solicited" : "unsolicited", sub->id, counter.new_msgs,
400 if (sub->is_solicited) {
401 if (pjsip_mwi_notify(ast_sip_subscription_get_evsub(sub->sip_sub),
407 &tdata) != PJ_SUCCESS) {
408 ast_log(LOG_WARNING, "Unable to create MWI NOTIFY request to %s.\n", sub->id);
411 if (ast_sip_subscription_send_request(sub->sip_sub, tdata) != PJ_SUCCESS) {
412 ast_log(LOG_WARNING, "Unable to send MWI NOTIFY request to %s\n", sub->id);
416 send_unsolicited_mwi_notify(sub, state, reason, &mwi_type, &pj_body);
420 static int unsubscribe_stasis(void *obj, void *arg, int flags)
422 struct mwi_stasis_subscription *mwi_stasis = obj;
423 if (mwi_stasis->stasis_sub) {
424 ast_debug(3, "Removing stasis subscription to mailbox %s\n", mwi_stasis->mailbox);
425 mwi_stasis->stasis_sub = stasis_unsubscribe(mwi_stasis->stasis_sub);
430 static void mwi_subscription_shutdown(struct ast_sip_subscription *sub)
432 struct mwi_subscription *mwi_sub;
433 RAII_VAR(struct ast_datastore *, mwi_datastore,
434 ast_sip_subscription_get_datastore(sub, "MWI datastore"), ao2_cleanup);
436 if (!mwi_datastore) {
440 mwi_sub = mwi_datastore->data;
441 ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
444 static struct ast_datastore_info mwi_ds_info = { };
446 static int add_mwi_datastore(struct mwi_subscription *sub)
448 RAII_VAR(struct ast_datastore *, mwi_datastore, NULL, ao2_cleanup);
450 mwi_datastore = ast_sip_subscription_alloc_datastore(&mwi_ds_info, "MWI datastore");
451 if (!mwi_datastore) {
454 mwi_datastore->data = sub;
456 ast_sip_subscription_add_datastore(sub->sip_sub, mwi_datastore);
460 static struct ast_sip_subscription *mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
461 pjsip_rx_data *rdata)
463 RAII_VAR(struct ast_sip_aor *, aor, NULL, ao2_cleanup);
464 /* It's not obvious here, but the reference(s) to this subscription,
465 * once this function exits, is held by the stasis subscription(s)
466 * created in mwi_stasis_subscription_alloc()
468 RAII_VAR(struct mwi_subscription *, sub, NULL, ao2_cleanup);
469 pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
470 pjsip_sip_uri *sip_ruri;
476 if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
477 ast_log(LOG_WARNING, "Attempt to SUBSCRIBE to a non-SIP URI\n");
480 sip_ruri = pjsip_uri_get_uri(ruri);
481 ast_copy_pj_str(aor_name, &sip_ruri->user, sizeof(aor_name));
483 aor = ast_sip_location_retrieve_aor(aor_name);
485 ast_log(LOG_WARNING, "Unable to locate aor %s. MWI subscription failed.\n", aor_name);
489 if (ast_strlen_zero(aor->mailboxes)) {
490 ast_log(LOG_WARNING, "AOR %s has no configured mailboxes. MWI subscription failed\n", aor_name);
494 sub = mwi_subscription_alloc(endpoint, AST_SIP_NOTIFIER, 1, rdata);
499 if (add_mwi_datastore(sub)) {
500 ast_log(LOG_WARNING, "Unable to allocate datastore on MWI subscription from %s\n", sub->id);
504 mailboxes = ast_strdupa(aor->mailboxes);
505 while ((mailbox = strsep(&mailboxes, ","))) {
506 RAII_VAR(struct mwi_stasis_subscription *, mwi_stasis_sub,
507 mwi_stasis_subscription_alloc(mailbox, sub), ao2_cleanup);
508 if (mwi_stasis_sub) {
509 ao2_link(sub->stasis_subs, mwi_stasis_sub);
513 evsub = ast_sip_subscription_get_evsub(sub->sip_sub);
514 pjsip_evsub_accept(evsub, rdata, 200, NULL);
515 send_mwi_notify(sub, PJSIP_EVSUB_STATE_ACTIVE, NULL);
520 static void mwi_resubscribe(struct ast_sip_subscription *sub,
521 pjsip_rx_data *rdata, struct ast_sip_subscription_response_data *response_data)
523 pjsip_tx_data *tdata;
525 pjsip_mwi_current_notify(ast_sip_subscription_get_evsub(sub), &tdata);
526 ast_sip_subscription_send_request(sub, tdata);
529 static void mwi_subscription_timeout(struct ast_sip_subscription *sub)
531 struct mwi_subscription *mwi_sub;
532 RAII_VAR(struct ast_datastore *, mwi_datastore,
533 ast_sip_subscription_get_datastore(sub, "MWI datastore"), ao2_cleanup);
535 if (!mwi_datastore) {
540 mwi_sub = mwi_datastore->data;
542 ast_log(LOG_NOTICE, "MWI subscription for %s has timed out.\n", mwi_sub->id);
544 send_mwi_notify(mwi_sub, PJSIP_EVSUB_STATE_TERMINATED, "timeout");
547 static void mwi_subscription_terminated(struct ast_sip_subscription *sub, pjsip_rx_data *rdata)
549 struct mwi_subscription *mwi_sub;
550 RAII_VAR(struct ast_datastore *, mwi_datastore,
551 ast_sip_subscription_get_datastore(sub, "MWI datastore"), ao2_cleanup);
553 if (!mwi_datastore) {
557 mwi_sub = mwi_datastore->data;
559 ast_log(LOG_NOTICE, "MWI subscription for %s has been terminated\n", mwi_sub->id);
561 send_mwi_notify(mwi_sub, PJSIP_EVSUB_STATE_TERMINATED, NULL);
564 static void mwi_notify_response(struct ast_sip_subscription *sub, pjsip_rx_data *rdata)
566 /* We don't really care about NOTIFY responses for the moment */
569 static void mwi_notify_request(struct ast_sip_subscription *sub, pjsip_rx_data *rdata,
570 struct ast_sip_subscription_response_data *response_data)
572 ast_log(LOG_WARNING, "Received an MWI NOTIFY request? This should not happen\n");
575 static int mwi_refresh_subscription(struct ast_sip_subscription *sub)
577 ast_log(LOG_WARNING, "Being told to refresh an MWI subscription? This should not happen\n");
581 static int serialized_notify(void *userdata)
583 struct mwi_subscription *mwi_sub = userdata;
585 send_mwi_notify(mwi_sub, PJSIP_EVSUB_STATE_ACTIVE, NULL);
586 ao2_ref(mwi_sub, -1);
590 static int serialized_cleanup(void *userdata)
592 struct mwi_subscription *mwi_sub = userdata;
594 /* This is getting rid of the reference that was added
595 * just before this serialized task was pushed.
597 ao2_cleanup(mwi_sub);
598 /* This is getting rid of the reference held by the
599 * stasis subscription
601 ao2_cleanup(mwi_sub);
605 static void mwi_stasis_cb(void *userdata, struct stasis_subscription *sub,
606 struct stasis_topic *topic, struct stasis_message *msg)
608 struct mwi_subscription *mwi_sub = userdata;
610 if (stasis_subscription_final_message(sub, msg)) {
611 ao2_ref(mwi_sub, +1);
612 ast_sip_push_task(NULL, serialized_cleanup, mwi_sub);
616 if (ast_mwi_state_type() == stasis_message_type(msg)) {
617 struct ast_taskprocessor *serializer = mwi_sub->is_solicited ? ast_sip_subscription_get_serializer(mwi_sub->sip_sub) : NULL;
618 ao2_ref(mwi_sub, +1);
619 ast_sip_push_task(serializer, serialized_notify, mwi_sub);
623 static int create_mwi_subscriptions_for_endpoint(void *obj, void *arg, int flags)
625 RAII_VAR(struct mwi_subscription *, aggregate_sub, NULL, ao2_cleanup);
626 struct ast_sip_endpoint *endpoint = obj;
627 struct ao2_container *mwi_subscriptions = arg;
631 if (ast_strlen_zero(endpoint->subscription.mwi.mailboxes)) {
635 if (endpoint->subscription.mwi.aggregate) {
636 aggregate_sub = mwi_subscription_alloc(endpoint, AST_SIP_NOTIFIER, 0, NULL);
637 if (!aggregate_sub) {
642 mailboxes = ast_strdupa(endpoint->subscription.mwi.mailboxes);
643 while ((mailbox = strsep(&mailboxes, ","))) {
644 struct mwi_subscription *sub = aggregate_sub ?:
645 mwi_subscription_alloc(endpoint, AST_SIP_SUBSCRIBER, 0, NULL);
646 RAII_VAR(struct mwi_stasis_subscription *, mwi_stasis_sub,
647 mwi_stasis_subscription_alloc(mailbox, sub), ao2_cleanup);
648 if (mwi_stasis_sub) {
649 ao2_link(sub->stasis_subs, mwi_stasis_sub);
651 if (!aggregate_sub) {
652 ao2_link(mwi_subscriptions, sub);
656 ao2_link(mwi_subscriptions, aggregate_sub);
660 static int unsubscribe(void *obj, void *arg, int flags)
662 struct mwi_subscription *mwi_sub = obj;
664 ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
668 static void create_mwi_subscriptions(void)
670 struct ao2_container *mwi_subscriptions = ao2_container_alloc(MWI_BUCKETS, mwi_sub_hash, mwi_sub_cmp);
671 RAII_VAR(struct ao2_container *, old_mwi_subscriptions, ao2_global_obj_ref(unsolicited_mwi), ao2_cleanup);
672 RAII_VAR(struct ao2_container *, endpoints, ast_sorcery_retrieve_by_fields(
673 ast_sip_get_sorcery(), "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL),
676 if (!mwi_subscriptions) {
680 /* We remove all the old stasis subscriptions first before applying the new configuration. This
681 * prevents a situation where there might be multiple overlapping stasis subscriptions for an
682 * endpoint for mailboxes. Though there may be mailbox changes during the gap between unsubscribing
683 * and resubscribing, up-to-date mailbox state will be sent out to the endpoint when the
684 * new stasis subscription is established
686 if (old_mwi_subscriptions) {
687 ao2_callback(old_mwi_subscriptions, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
689 ao2_callback(endpoints, OBJ_NODATA, create_mwi_subscriptions_for_endpoint, mwi_subscriptions);
690 ao2_global_obj_replace_unref(unsolicited_mwi, mwi_subscriptions);
693 static int reload(void)
695 create_mwi_subscriptions();
699 static int load_module(void)
701 if (ast_sip_register_subscription_handler(&mwi_handler)) {
702 return AST_MODULE_LOAD_DECLINE;
704 create_mwi_subscriptions();
705 return AST_MODULE_LOAD_SUCCESS;
708 static int unload_module(void)
710 RAII_VAR(struct ao2_container *, mwi_subscriptions, ao2_global_obj_ref(unsolicited_mwi), ao2_cleanup);
711 if (mwi_subscriptions) {
712 ao2_callback(mwi_subscriptions, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
713 ao2_global_obj_release(unsolicited_mwi);
715 ast_sip_unregister_subscription_handler(&mwi_handler);
719 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP MWI resource",
721 .unload = unload_module,
723 .load_pri = AST_MODPRI_CHANNEL_DEPEND,