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 .subscription_shutdown = mwi_subscription_shutdown,
68 .new_subscribe = mwi_new_subscribe,
69 .resubscribe = mwi_resubscribe,
70 .subscription_timeout = mwi_subscription_timeout,
71 .subscription_terminated = mwi_subscription_terminated,
72 .notify_response = mwi_notify_response,
73 .notify_request = mwi_notify_request,
74 .refresh_subscription = mwi_refresh_subscription,
78 * \brief Wrapper for stasis subscription
80 * An MWI subscription has a container of these. This
81 * represents a stasis subscription for MWI state.
83 struct mwi_stasis_subscription {
84 /*! The MWI stasis subscription */
85 struct stasis_subscription *stasis_sub;
86 /*! The mailbox corresponding with the MWI subscription. Used as a hash key */
91 * \brief A subscription for MWI
93 * This subscription is the basis for MWI for an endpoint. Each
94 * endpoint that uses MWI will have a corresponding mwi_subscription.
96 * This structure acts as the owner for the underlying SIP subscription.
97 * When the mwi_subscription is destroyed, the SIP subscription dies, too.
98 * The mwi_subscription's lifetime is governed by its underlying stasis
99 * subscriptions. When all stasis subscriptions are destroyed, the
100 * mwi_subscription is destroyed as well.
102 struct mwi_subscription {
103 /*! Container of \ref mwi_stasis_subscription structures.
104 * A single MWI subscription may be fore multiple mailboxes, thus
105 * requiring multiple stasis subscriptions
107 struct ao2_container *stasis_subs;
108 /*! The SIP subscription. Unsolicited MWI does not use this */
109 struct ast_sip_subscription *sip_sub;
110 /*! Is the MWI solicited (i.e. Initiated with an external SUBSCRIBE) ? */
111 unsigned int is_solicited;
112 /*! Identifier for the subscription.
113 * The identifier is the same as the corresponding endpoint's stasis ID.
119 static void mwi_stasis_cb(void *userdata, struct stasis_subscription *sub,
120 struct stasis_topic *topic, struct stasis_message *msg);
122 static struct mwi_stasis_subscription *mwi_stasis_subscription_alloc(const char *mailbox, struct mwi_subscription *mwi_sub)
124 struct mwi_stasis_subscription *mwi_stasis_sub;
125 struct stasis_topic *topic;
131 mwi_stasis_sub = ao2_alloc(sizeof(*mwi_stasis_sub) + strlen(mailbox), NULL);
132 if (!mwi_stasis_sub) {
136 topic = ast_mwi_topic(mailbox);
139 strcpy(mwi_stasis_sub->mailbox, mailbox);
140 ao2_ref(mwi_sub, +1);
141 ast_debug(3, "Creating stasis MWI subscription to mailbox %s for endpoint %s\n", mailbox, mwi_sub->id);
142 mwi_stasis_sub->stasis_sub = stasis_subscribe(topic, mwi_stasis_cb, mwi_sub);
143 return mwi_stasis_sub;
146 static int stasis_sub_hash(const void *obj, int flags)
148 const struct mwi_stasis_subscription *mwi_stasis = obj;
150 return ast_str_hash(mwi_stasis->mailbox);
153 static int stasis_sub_cmp(void *obj, void *arg, int flags)
155 struct mwi_stasis_subscription *mwi_stasis1 = obj;
156 struct mwi_stasis_subscription *mwi_stasis2 = arg;
158 return strcmp(mwi_stasis1->mailbox, mwi_stasis2->mailbox) ? 0 : CMP_MATCH;
161 static void mwi_subscription_destructor(void *obj)
163 struct mwi_subscription *sub = obj;
165 ast_debug(3, "Destroying MWI subscription for endpoint %s\n", sub->id);
166 ao2_cleanup(sub->sip_sub);
167 ao2_cleanup(sub->stasis_subs);
170 static struct mwi_subscription *mwi_subscription_alloc(struct ast_sip_endpoint *endpoint,
171 enum ast_sip_subscription_role role, unsigned int is_solicited, pjsip_rx_data *rdata)
173 struct mwi_subscription *sub;
174 const char *endpoint_id = ast_sorcery_object_get_id(endpoint);
176 sub = ao2_alloc(sizeof(*sub) + strlen(endpoint_id),
177 mwi_subscription_destructor);
184 strcpy(sub->id, endpoint_id);
185 /* Unsolicited MWI doesn't actually result in a SIP subscription being
186 * created. This is because a SIP subscription associates with a dialog.
187 * Most devices expect unsolicited MWI NOTIFYs to appear out of dialog. If
188 * they receive an in-dialog MWI NOTIFY (i.e. with a to-tag), then they
189 * will reject the NOTIFY with a 481, thus resulting in message-waiting
190 * state not being updated on the device
193 sub->sip_sub = ast_sip_create_subscription(&mwi_handler,
194 role, endpoint, rdata);
196 ast_log(LOG_WARNING, "Unable to create MWI SIP subscription for endpoint %s\n", sub->id);
202 sub->stasis_subs = ao2_container_alloc(STASIS_BUCKETS, stasis_sub_hash, stasis_sub_cmp);
203 if (!sub->stasis_subs) {
207 sub->is_solicited = is_solicited;
209 ast_debug(3, "Created %s MWI subscription for endpoint %s\n", is_solicited ? "solicited" : "unsolicited", sub->id);
214 static int mwi_sub_hash(const void *obj, int flags)
216 const struct mwi_subscription *mwi_sub = obj;
218 return ast_str_hash(mwi_sub->id);
221 static int mwi_sub_cmp(void *obj, void *arg, int flags)
223 struct mwi_subscription *mwi_sub1 = obj;
224 struct mwi_subscription *mwi_sub2 = arg;
226 return strcmp(mwi_sub1->id, mwi_sub2->id) ? 0 : CMP_MATCH;
229 struct message_accumulator {
235 static int get_message_count(void *obj, void *arg, int flags)
237 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
238 struct mwi_stasis_subscription *mwi_stasis = obj;
239 struct message_accumulator *counter = arg;
240 struct ast_mwi_state *mwi_state;
242 msg = stasis_cache_get(ast_mwi_state_cache(), ast_mwi_state_type(), mwi_stasis->mailbox);
247 mwi_state = stasis_message_data(msg);
248 counter->old_msgs += mwi_state->old_msgs;
249 counter->new_msgs += mwi_state->new_msgs;
253 struct unsolicited_mwi_data {
254 struct mwi_subscription *sub;
255 struct ast_sip_endpoint *endpoint;
256 pjsip_evsub_state state;
258 const pjsip_media_type *mwi_type;
259 const pj_str_t *body_text;
262 static int send_unsolicited_mwi_notify_to_contact(void *obj, void *arg, int flags)
264 struct unsolicited_mwi_data *mwi_data = arg;
265 struct mwi_subscription *sub = mwi_data->sub;
266 struct ast_sip_endpoint *endpoint = mwi_data->endpoint;
267 pjsip_evsub_state state = mwi_data->state;
268 const char *reason = mwi_data->reason;
269 const pjsip_media_type *mwi_type = mwi_data->mwi_type;
270 const pj_str_t *body_text = mwi_data->body_text;
271 struct ast_sip_contact *contact = obj;
272 const char *state_name;
273 pjsip_tx_data *tdata;
274 pjsip_msg_body *msg_body;
275 pjsip_sub_state_hdr *sub_state;
276 pjsip_event_hdr *event;
277 const pjsip_hdr *allow_events = pjsip_evsub_get_allow_events_hdr(NULL);
279 if (ast_sip_create_request("NOTIFY", NULL, endpoint, contact->uri, &tdata)) {
280 ast_log(LOG_WARNING, "Unable to create unsolicited NOTIFY request to endpoint %s URI %s\n", sub->id, contact->uri);
284 if (!ast_strlen_zero(endpoint->subscription.mwi.fromuser)) {
285 pjsip_fromto_hdr *from = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM, NULL);
286 pjsip_name_addr *from_name_addr = (pjsip_name_addr *) from->uri;
287 pjsip_sip_uri *from_uri = pjsip_uri_get_uri(from_name_addr->uri);
289 pj_strdup2(tdata->pool, &from_uri->user, endpoint->subscription.mwi.fromuser);
293 case PJSIP_EVSUB_STATE_ACTIVE:
294 state_name = "active";
296 case PJSIP_EVSUB_STATE_TERMINATED:
298 state_name = "terminated";
302 sub_state = pjsip_sub_state_hdr_create(tdata->pool);
303 pj_cstr(&sub_state->sub_state, state_name);
305 pj_cstr(&sub_state->reason_param, reason);
307 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) sub_state);
309 event = pjsip_event_hdr_create(tdata->pool);
310 pj_cstr(&event->event_type, "message-summary");
311 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) event);
313 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_shallow_clone(tdata->pool, allow_events));
314 msg_body = pjsip_msg_body_create(tdata->pool, &mwi_type->type, &mwi_type->subtype, body_text);
315 tdata->msg->body = msg_body;
316 ast_sip_send_request(tdata, NULL, endpoint);
321 static void send_unsolicited_mwi_notify(struct mwi_subscription *sub, pjsip_evsub_state state, const char *reason,
322 const pjsip_media_type *mwi_type, const pj_str_t *body_text)
324 RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(),
325 "endpoint", sub->id), ao2_cleanup);
330 ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because endpoint does not exist\n",
334 if (ast_strlen_zero(endpoint->aors)) {
335 ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because the endpoint has no"
336 " configured AORs\n", sub->id);
340 endpoint_aors = ast_strdupa(endpoint->aors);
342 while ((aor_name = strsep(&endpoint_aors, ","))) {
343 RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
344 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
345 struct unsolicited_mwi_data mwi_data = {
347 .endpoint = endpoint,
350 .mwi_type = mwi_type,
351 .body_text = body_text,
355 ast_log(LOG_WARNING, "Unable to locate AOR %s for unsolicited MWI\n", aor_name);
359 contacts = ast_sip_location_retrieve_aor_contacts(aor);
360 if (!contacts || (ao2_container_count(contacts) == 0)) {
361 ast_log(LOG_WARNING, "No contacts bound to AOR %s. Cannot send unsolicited MWI.\n", aor_name);
365 ao2_callback(contacts, OBJ_NODATA, send_unsolicited_mwi_notify_to_contact, &mwi_data);
369 static void send_mwi_notify(struct mwi_subscription *sub, pjsip_evsub_state state, const char *reason)
371 const pj_str_t *reason_str_ptr = NULL;
372 static pjsip_media_type mwi_type = {
373 .type = { "application", 11 },
374 .subtype = { "simple-message-summary", 22 },
376 struct message_accumulator counter = {
380 RAII_VAR(struct ast_str *, body, ast_str_create(64), ast_free_ptr);
381 pjsip_tx_data *tdata;
385 ao2_callback(sub->stasis_subs, OBJ_NODATA, get_message_count, &counter);
388 pj_cstr(&reason_str, reason);
389 reason_str_ptr = &reason_str;
391 ast_str_append(&body, 0, "Messages-Waiting: %s\r\n", counter.new_msgs ? "yes" : "no");
392 ast_str_append(&body, 0, "Voice-Message: %d/%d (0/0)\r\n", counter.new_msgs, counter.old_msgs);
393 pj_cstr(&pj_body, ast_str_buffer(body));
395 ast_debug(5, "Sending %s MWI NOTIFY to endpoint %s, new messages: %d, old messages: %d\n",
396 sub->is_solicited ? "solicited" : "unsolicited", sub->id, counter.new_msgs,
399 if (sub->is_solicited) {
400 if (pjsip_mwi_notify(ast_sip_subscription_get_evsub(sub->sip_sub),
406 &tdata) != PJ_SUCCESS) {
407 ast_log(LOG_WARNING, "Unable to create MWI NOTIFY request to %s.\n", sub->id);
410 if (ast_sip_subscription_send_request(sub->sip_sub, tdata) != PJ_SUCCESS) {
411 ast_log(LOG_WARNING, "Unable to send MWI NOTIFY request to %s\n", sub->id);
415 send_unsolicited_mwi_notify(sub, state, reason, &mwi_type, &pj_body);
419 static int unsubscribe_stasis(void *obj, void *arg, int flags)
421 struct mwi_stasis_subscription *mwi_stasis = obj;
422 if (mwi_stasis->stasis_sub) {
423 ast_debug(3, "Removing stasis subscription to mailbox %s\n", mwi_stasis->mailbox);
424 mwi_stasis->stasis_sub = stasis_unsubscribe(mwi_stasis->stasis_sub);
429 static void mwi_subscription_shutdown(struct ast_sip_subscription *sub)
431 struct mwi_subscription *mwi_sub;
432 RAII_VAR(struct ast_datastore *, mwi_datastore,
433 ast_sip_subscription_get_datastore(sub, "MWI datastore"), ao2_cleanup);
435 if (!mwi_datastore) {
439 mwi_sub = mwi_datastore->data;
440 ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
443 static struct ast_datastore_info mwi_ds_info = { };
445 static int add_mwi_datastore(struct mwi_subscription *sub)
447 RAII_VAR(struct ast_datastore *, mwi_datastore, NULL, ao2_cleanup);
449 mwi_datastore = ast_sip_subscription_alloc_datastore(&mwi_ds_info, "MWI datastore");
450 if (!mwi_datastore) {
453 mwi_datastore->data = sub;
455 ast_sip_subscription_add_datastore(sub->sip_sub, mwi_datastore);
459 static struct ast_sip_subscription *mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
460 pjsip_rx_data *rdata)
462 RAII_VAR(struct ast_sip_aor *, aor, NULL, ao2_cleanup);
463 /* It's not obvious here, but the reference(s) to this subscription,
464 * once this function exits, is held by the stasis subscription(s)
465 * created in mwi_stasis_subscription_alloc()
467 RAII_VAR(struct mwi_subscription *, sub, NULL, ao2_cleanup);
468 pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
469 pjsip_sip_uri *sip_ruri;
475 if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
476 ast_log(LOG_WARNING, "Attempt to SUBSCRIBE to a non-SIP URI\n");
479 sip_ruri = pjsip_uri_get_uri(ruri);
480 ast_copy_pj_str(aor_name, &sip_ruri->user, sizeof(aor_name));
482 aor = ast_sip_location_retrieve_aor(aor_name);
484 ast_log(LOG_WARNING, "Unable to locate aor %s. MWI subscription failed.\n", aor_name);
488 if (ast_strlen_zero(aor->mailboxes)) {
489 ast_log(LOG_WARNING, "AOR %s has no configured mailboxes. MWI subscription failed\n", aor_name);
493 sub = mwi_subscription_alloc(endpoint, AST_SIP_NOTIFIER, 1, rdata);
498 if (add_mwi_datastore(sub)) {
499 ast_log(LOG_WARNING, "Unable to allocate datastore on MWI subscription from %s\n", sub->id);
503 mailboxes = ast_strdupa(aor->mailboxes);
504 while ((mailbox = strsep(&mailboxes, ","))) {
505 RAII_VAR(struct mwi_stasis_subscription *, mwi_stasis_sub,
506 mwi_stasis_subscription_alloc(mailbox, sub), ao2_cleanup);
507 if (mwi_stasis_sub) {
508 ao2_link(sub->stasis_subs, mwi_stasis_sub);
512 evsub = ast_sip_subscription_get_evsub(sub->sip_sub);
513 pjsip_evsub_accept(evsub, rdata, 200, NULL);
514 send_mwi_notify(sub, PJSIP_EVSUB_STATE_ACTIVE, NULL);
519 static void mwi_resubscribe(struct ast_sip_subscription *sub,
520 pjsip_rx_data *rdata, struct ast_sip_subscription_response_data *response_data)
522 pjsip_tx_data *tdata;
524 pjsip_mwi_current_notify(ast_sip_subscription_get_evsub(sub), &tdata);
525 ast_sip_subscription_send_request(sub, tdata);
528 static void mwi_subscription_timeout(struct ast_sip_subscription *sub)
530 struct mwi_subscription *mwi_sub;
531 RAII_VAR(struct ast_datastore *, mwi_datastore,
532 ast_sip_subscription_get_datastore(sub, "MWI datastore"), ao2_cleanup);
534 if (!mwi_datastore) {
539 mwi_sub = mwi_datastore->data;
541 ast_log(LOG_NOTICE, "MWI subscription for %s has timed out.\n", mwi_sub->id);
543 send_mwi_notify(mwi_sub, PJSIP_EVSUB_STATE_TERMINATED, "timeout");
546 static void mwi_subscription_terminated(struct ast_sip_subscription *sub, pjsip_rx_data *rdata)
548 struct mwi_subscription *mwi_sub;
549 RAII_VAR(struct ast_datastore *, mwi_datastore,
550 ast_sip_subscription_get_datastore(sub, "MWI datastore"), ao2_cleanup);
552 if (!mwi_datastore) {
556 mwi_sub = mwi_datastore->data;
558 ast_log(LOG_NOTICE, "MWI subscription for %s has been terminated\n", mwi_sub->id);
560 send_mwi_notify(mwi_sub, PJSIP_EVSUB_STATE_TERMINATED, NULL);
563 static void mwi_notify_response(struct ast_sip_subscription *sub, pjsip_rx_data *rdata)
565 /* We don't really care about NOTIFY responses for the moment */
568 static void mwi_notify_request(struct ast_sip_subscription *sub, pjsip_rx_data *rdata,
569 struct ast_sip_subscription_response_data *response_data)
571 ast_log(LOG_WARNING, "Received an MWI NOTIFY request? This should not happen\n");
574 static int mwi_refresh_subscription(struct ast_sip_subscription *sub)
576 ast_log(LOG_WARNING, "Being told to refresh an MWI subscription? This should not happen\n");
580 static int serialized_notify(void *userdata)
582 struct mwi_subscription *mwi_sub = userdata;
584 send_mwi_notify(mwi_sub, PJSIP_EVSUB_STATE_ACTIVE, NULL);
585 ao2_ref(mwi_sub, -1);
589 static int serialized_cleanup(void *userdata)
591 struct mwi_subscription *mwi_sub = userdata;
593 /* This is getting rid of the reference that was added
594 * just before this serialized task was pushed.
596 ao2_cleanup(mwi_sub);
597 /* This is getting rid of the reference held by the
598 * stasis subscription
600 ao2_cleanup(mwi_sub);
604 static void mwi_stasis_cb(void *userdata, struct stasis_subscription *sub,
605 struct stasis_topic *topic, struct stasis_message *msg)
607 struct mwi_subscription *mwi_sub = userdata;
609 if (stasis_subscription_final_message(sub, msg)) {
610 ao2_ref(mwi_sub, +1);
611 ast_sip_push_task(NULL, serialized_cleanup, mwi_sub);
615 if (ast_mwi_state_type() == stasis_message_type(msg)) {
616 struct ast_taskprocessor *serializer = mwi_sub->is_solicited ? ast_sip_subscription_get_serializer(mwi_sub->sip_sub) : NULL;
617 ao2_ref(mwi_sub, +1);
618 ast_sip_push_task(serializer, serialized_notify, mwi_sub);
622 static int create_mwi_subscriptions_for_endpoint(void *obj, void *arg, int flags)
624 RAII_VAR(struct mwi_subscription *, aggregate_sub, NULL, ao2_cleanup);
625 struct ast_sip_endpoint *endpoint = obj;
626 struct ao2_container *mwi_subscriptions = arg;
630 if (ast_strlen_zero(endpoint->subscription.mwi.mailboxes)) {
634 if (endpoint->subscription.mwi.aggregate) {
635 aggregate_sub = mwi_subscription_alloc(endpoint, AST_SIP_NOTIFIER, 0, NULL);
636 if (!aggregate_sub) {
641 mailboxes = ast_strdupa(endpoint->subscription.mwi.mailboxes);
642 while ((mailbox = strsep(&mailboxes, ","))) {
643 struct mwi_subscription *sub = aggregate_sub ?:
644 mwi_subscription_alloc(endpoint, AST_SIP_SUBSCRIBER, 0, NULL);
645 RAII_VAR(struct mwi_stasis_subscription *, mwi_stasis_sub,
646 mwi_stasis_subscription_alloc(mailbox, sub), ao2_cleanup);
647 if (mwi_stasis_sub) {
648 ao2_link(sub->stasis_subs, mwi_stasis_sub);
650 if (!aggregate_sub) {
651 ao2_link(mwi_subscriptions, sub);
655 ao2_link(mwi_subscriptions, aggregate_sub);
659 static int unsubscribe(void *obj, void *arg, int flags)
661 struct mwi_subscription *mwi_sub = obj;
663 ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
667 static void create_mwi_subscriptions(void)
669 struct ao2_container *mwi_subscriptions = ao2_container_alloc(MWI_BUCKETS, mwi_sub_hash, mwi_sub_cmp);
670 RAII_VAR(struct ao2_container *, old_mwi_subscriptions, ao2_global_obj_ref(unsolicited_mwi), ao2_cleanup);
671 RAII_VAR(struct ao2_container *, endpoints, ast_sorcery_retrieve_by_fields(
672 ast_sip_get_sorcery(), "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL),
675 if (!mwi_subscriptions) {
679 /* We remove all the old stasis subscriptions first before applying the new configuration. This
680 * prevents a situation where there might be multiple overlapping stasis subscriptions for an
681 * endpoint for mailboxes. Though there may be mailbox changes during the gap between unsubscribing
682 * and resubscribing, up-to-date mailbox state will be sent out to the endpoint when the
683 * new stasis subscription is established
685 if (old_mwi_subscriptions) {
686 ao2_callback(old_mwi_subscriptions, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
688 ao2_callback(endpoints, OBJ_NODATA, create_mwi_subscriptions_for_endpoint, mwi_subscriptions);
689 ao2_global_obj_replace_unref(unsolicited_mwi, mwi_subscriptions);
692 static int reload(void)
694 create_mwi_subscriptions();
698 static int load_module(void)
700 if (ast_sip_register_subscription_handler(&mwi_handler)) {
701 return AST_MODULE_LOAD_DECLINE;
703 create_mwi_subscriptions();
704 return AST_MODULE_LOAD_SUCCESS;
707 static int unload_module(void)
709 RAII_VAR(struct ao2_container *, mwi_subscriptions, ao2_global_obj_ref(unsolicited_mwi), ao2_cleanup);
710 if (mwi_subscriptions) {
711 ao2_callback(mwi_subscriptions, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
712 ao2_global_obj_release(unsolicited_mwi);
714 ast_sip_unregister_subscription_handler(&mwi_handler);
718 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP MWI resource",
720 .unload = unload_module,
722 .load_pri = AST_MODPRI_CHANNEL_DEPEND,