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 <depend>res_pjsip_pubsub</depend>
23 <support_level>core</support_level>
29 #include <pjsip_simple.h>
32 #include "asterisk/res_pjsip.h"
33 #include "asterisk/res_pjsip_pubsub.h"
34 #include "asterisk/res_pjsip_body_generator_types.h"
35 #include "asterisk/module.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/astobj2.h"
38 #include "asterisk/sorcery.h"
39 #include "asterisk/stasis.h"
40 #include "asterisk/app.h"
42 struct mwi_subscription;
43 static struct ao2_container *unsolicited_mwi;
45 #define STASIS_BUCKETS 13
46 #define MWI_BUCKETS 53
48 #define MWI_TYPE "application"
49 #define MWI_SUBTYPE "simple-message-summary"
51 #define MWI_DATASTORE "MWI datastore"
53 static void mwi_subscription_shutdown(struct ast_sip_subscription *sub);
54 static void mwi_to_ami(struct ast_sip_subscription *sub, struct ast_str **buf);
55 static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
56 const char *resource);
57 static int mwi_subscription_established(struct ast_sip_subscription *sub);
58 static void *mwi_get_notify_data(struct ast_sip_subscription *sub);
60 static struct ast_sip_notifier mwi_notifier = {
61 .default_accept = MWI_TYPE"/"MWI_SUBTYPE,
62 .new_subscribe = mwi_new_subscribe,
63 .subscription_established = mwi_subscription_established,
64 .get_notify_data = mwi_get_notify_data,
67 static struct ast_sip_subscription_handler mwi_handler = {
68 .event_name = "message-summary",
69 .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
70 .accept = { MWI_TYPE"/"MWI_SUBTYPE, },
71 .subscription_shutdown = mwi_subscription_shutdown,
73 .notifier = &mwi_notifier,
77 * \brief Wrapper for stasis subscription
79 * An MWI subscription has a container of these. This
80 * represents a stasis subscription for MWI state.
82 struct mwi_stasis_subscription {
83 /*! The MWI stasis subscription */
84 struct stasis_subscription *stasis_sub;
85 /*! The mailbox corresponding with the MWI subscription. Used as a hash key */
90 * \brief A subscription for MWI
92 * This subscription is the basis for MWI for an endpoint. Each
93 * endpoint that uses MWI will have a corresponding mwi_subscription.
95 * This structure acts as the owner for the underlying SIP subscription.
96 * When the mwi_subscription is destroyed, the SIP subscription dies, too.
97 * The mwi_subscription's lifetime is governed by its underlying stasis
98 * subscriptions. When all stasis subscriptions are destroyed, the
99 * mwi_subscription is destroyed as well.
101 struct mwi_subscription {
102 /*! Container of \ref mwi_stasis_subscription structures.
103 * A single MWI subscription may be for multiple mailboxes, thus
104 * requiring multiple stasis subscriptions
106 struct ao2_container *stasis_subs;
107 /*! The SIP subscription. Unsolicited MWI does not use this */
108 struct ast_sip_subscription *sip_sub;
109 /*! AORs we should react to for unsolicited MWI NOTIFY */
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_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_pool(topic, mwi_stasis_cb, mwi_sub);
144 return mwi_stasis_sub;
147 static int stasis_sub_hash(const void *obj, const int flags)
149 const struct mwi_stasis_subscription *object;
152 switch (flags & OBJ_SEARCH_MASK) {
156 case OBJ_SEARCH_OBJECT:
158 key = object->mailbox;
164 return ast_str_hash(key);
167 static int stasis_sub_cmp(void *obj, void *arg, int flags)
169 const struct mwi_stasis_subscription *sub_left = obj;
170 const struct mwi_stasis_subscription *sub_right = arg;
171 const char *right_key = arg;
174 switch (flags & OBJ_SEARCH_MASK) {
175 case OBJ_SEARCH_OBJECT:
176 right_key = sub_right->mailbox;
179 cmp = strcmp(sub_left->mailbox, right_key);
181 case OBJ_SEARCH_PARTIAL_KEY:
182 cmp = strncmp(sub_left->mailbox, right_key, strlen(right_key));
194 static void mwi_subscription_destructor(void *obj)
196 struct mwi_subscription *sub = obj;
198 ast_debug(3, "Destroying MWI subscription for endpoint %s\n", sub->id);
199 ao2_cleanup(sub->sip_sub);
200 ao2_cleanup(sub->stasis_subs);
204 static struct mwi_subscription *mwi_subscription_alloc(struct ast_sip_endpoint *endpoint,
205 unsigned int is_solicited, struct ast_sip_subscription *sip_sub)
207 struct mwi_subscription *sub;
208 const char *endpoint_id = ast_sorcery_object_get_id(endpoint);
210 sub = ao2_alloc(sizeof(*sub) + strlen(endpoint_id),
211 mwi_subscription_destructor);
218 strcpy(sub->id, endpoint_id);
220 /* Unsolicited MWI doesn't actually result in a SIP subscription being
221 * created. This is because a SIP subscription associates with a dialog.
222 * Most devices expect unsolicited MWI NOTIFYs to appear out of dialog. If
223 * they receive an in-dialog MWI NOTIFY (i.e. with a to-tag), then they
224 * will reject the NOTIFY with a 481, thus resulting in message-waiting
225 * state not being updated on the device
228 sub->sip_sub = ao2_bump(sip_sub);
231 sub->stasis_subs = ao2_container_alloc(STASIS_BUCKETS, stasis_sub_hash, stasis_sub_cmp);
232 if (!sub->stasis_subs) {
236 sub->is_solicited = is_solicited;
238 if (!is_solicited && !ast_strlen_zero(endpoint->aors)) {
239 sub->aors = ast_strdup(endpoint->aors);
246 ast_debug(3, "Created %s MWI subscription for endpoint %s\n", is_solicited ? "solicited" : "unsolicited", sub->id);
251 static int mwi_sub_hash(const void *obj, const int flags)
253 const struct mwi_subscription *object;
256 switch (flags & OBJ_SEARCH_MASK) {
260 case OBJ_SEARCH_OBJECT:
268 return ast_str_hash(key);
271 static int mwi_sub_cmp(void *obj, void *arg, int flags)
273 const struct mwi_subscription *sub_left = obj;
274 const struct mwi_subscription *sub_right = arg;
275 const char *right_key = arg;
278 switch (flags & OBJ_SEARCH_MASK) {
279 case OBJ_SEARCH_OBJECT:
280 right_key = sub_right->id;
283 cmp = strcmp(sub_left->id, right_key);
285 case OBJ_SEARCH_PARTIAL_KEY:
286 cmp = strncmp(sub_left->id, right_key, strlen(right_key));
298 static int get_message_count(void *obj, void *arg, int flags)
300 struct stasis_message *msg;
301 struct mwi_stasis_subscription *mwi_stasis = obj;
302 struct ast_sip_message_accumulator *counter = arg;
303 struct ast_mwi_state *mwi_state;
305 msg = stasis_cache_get(ast_mwi_state_cache(), ast_mwi_state_type(), mwi_stasis->mailbox);
310 mwi_state = stasis_message_data(msg);
311 counter->old_msgs += mwi_state->old_msgs;
312 counter->new_msgs += mwi_state->new_msgs;
319 struct unsolicited_mwi_data {
320 struct mwi_subscription *sub;
321 struct ast_sip_endpoint *endpoint;
322 pjsip_evsub_state state;
323 const struct ast_sip_body *body;
326 static int send_unsolicited_mwi_notify_to_contact(void *obj, void *arg, int flags)
328 struct unsolicited_mwi_data *mwi_data = arg;
329 struct mwi_subscription *sub = mwi_data->sub;
330 struct ast_sip_endpoint *endpoint = mwi_data->endpoint;
331 pjsip_evsub_state state = mwi_data->state;
332 const struct ast_sip_body *body = mwi_data->body;
333 struct ast_sip_contact *contact = obj;
334 const char *state_name;
335 pjsip_tx_data *tdata;
336 pjsip_sub_state_hdr *sub_state;
337 pjsip_event_hdr *event;
338 const pjsip_hdr *allow_events = pjsip_evsub_get_allow_events_hdr(NULL);
340 if (ast_sip_create_request("NOTIFY", NULL, endpoint, NULL, contact, &tdata)) {
341 ast_log(LOG_WARNING, "Unable to create unsolicited NOTIFY request to endpoint %s URI %s\n", sub->id, contact->uri);
345 if (!ast_strlen_zero(endpoint->subscription.mwi.fromuser)) {
346 pjsip_fromto_hdr *from = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM, NULL);
347 pjsip_name_addr *from_name_addr = (pjsip_name_addr *) from->uri;
348 pjsip_sip_uri *from_uri = pjsip_uri_get_uri(from_name_addr->uri);
350 pj_strdup2(tdata->pool, &from_uri->user, endpoint->subscription.mwi.fromuser);
354 case PJSIP_EVSUB_STATE_ACTIVE:
355 state_name = "active";
357 case PJSIP_EVSUB_STATE_TERMINATED:
359 state_name = "terminated";
363 sub_state = pjsip_sub_state_hdr_create(tdata->pool);
364 pj_cstr(&sub_state->sub_state, state_name);
365 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) sub_state);
367 event = pjsip_event_hdr_create(tdata->pool);
368 pj_cstr(&event->event_type, "message-summary");
369 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) event);
371 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_shallow_clone(tdata->pool, allow_events));
372 ast_sip_add_body(tdata, body);
373 ast_sip_send_request(tdata, NULL, endpoint, NULL, NULL);
378 static void send_unsolicited_mwi_notify(struct mwi_subscription *sub,
379 struct ast_sip_message_accumulator *counter)
381 RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(),
382 "endpoint", sub->id), ao2_cleanup);
385 struct ast_sip_body body;
386 struct ast_str *body_text;
387 struct ast_sip_body_data body_data = {
388 .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
389 .body_data = counter,
393 ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because endpoint does not exist\n",
397 if (ast_strlen_zero(endpoint->aors)) {
398 ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because the endpoint has no"
399 " configured AORs\n", sub->id);
403 body.type = MWI_TYPE;
404 body.subtype = MWI_SUBTYPE;
406 body_text = ast_str_create(64);
412 if (ast_sip_pubsub_generate_body_content(body.type, body.subtype, &body_data, &body_text)) {
413 ast_log(LOG_WARNING, "Unable to generate SIP MWI NOTIFY body.\n");
418 body.body_text = ast_str_buffer(body_text);
420 endpoint_aors = ast_strdupa(endpoint->aors);
422 ast_debug(5, "Sending unsolicited MWI NOTIFY to endpoint %s, new messages: %d, old messages: %d\n",
423 sub->id, counter->new_msgs, counter->old_msgs);
425 while ((aor_name = strsep(&endpoint_aors, ","))) {
426 RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
427 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
428 struct unsolicited_mwi_data mwi_data = {
430 .endpoint = endpoint,
435 ast_log(LOG_WARNING, "Unable to locate AOR %s for unsolicited MWI\n", aor_name);
439 contacts = ast_sip_location_retrieve_aor_contacts(aor);
440 if (!contacts || (ao2_container_count(contacts) == 0)) {
441 ast_log(LOG_NOTICE, "No contacts bound to AOR %s. Cannot send unsolicited MWI until a contact registers.\n", aor_name);
445 ao2_callback(contacts, OBJ_NODATA, send_unsolicited_mwi_notify_to_contact, &mwi_data);
451 static void send_mwi_notify(struct mwi_subscription *sub)
453 struct ast_sip_message_accumulator counter = {
457 struct ast_sip_body_data data = {
458 .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
459 .body_data = &counter,
462 ao2_callback(sub->stasis_subs, OBJ_NODATA, get_message_count, &counter);
464 if (sub->is_solicited) {
465 ast_sip_subscription_notify(sub->sip_sub, &data, 0);
469 send_unsolicited_mwi_notify(sub, &counter);
472 static int unsubscribe_stasis(void *obj, void *arg, int flags)
474 struct mwi_stasis_subscription *mwi_stasis = obj;
475 if (mwi_stasis->stasis_sub) {
476 ast_debug(3, "Removing stasis subscription to mailbox %s\n", mwi_stasis->mailbox);
477 mwi_stasis->stasis_sub = stasis_unsubscribe_and_join(mwi_stasis->stasis_sub);
482 static void mwi_subscription_shutdown(struct ast_sip_subscription *sub)
484 struct mwi_subscription *mwi_sub;
485 struct ast_datastore *mwi_datastore;
487 mwi_datastore = ast_sip_subscription_get_datastore(sub, MWI_DATASTORE);
488 if (!mwi_datastore) {
492 mwi_sub = mwi_datastore->data;
493 ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
495 ao2_ref(mwi_datastore, -1);
498 static struct ast_datastore_info mwi_ds_info = { };
500 static int add_mwi_datastore(struct mwi_subscription *sub)
502 struct ast_datastore *mwi_datastore;
504 mwi_datastore = ast_sip_subscription_alloc_datastore(&mwi_ds_info, MWI_DATASTORE);
505 if (!mwi_datastore) {
508 mwi_datastore->data = sub;
510 ast_sip_subscription_add_datastore(sub->sip_sub, mwi_datastore);
511 ao2_ref(mwi_datastore, -1);
516 * \brief Determines if an endpoint is receiving unsolicited MWI for a particular mailbox.
518 * \param endpoint The endpoint to check
519 * \param mailbox The candidate mailbox
520 * \retval 0 The endpoint does not receive unsolicited MWI for this mailbox
521 * \retval 1 The endpoint receives unsolicited MWI for this mailbox
523 static int endpoint_receives_unsolicited_mwi_for_mailbox(struct ast_sip_endpoint *endpoint,
526 struct ao2_iterator *mwi_subs;
527 struct mwi_subscription *mwi_sub;
528 const char *endpoint_id = ast_sorcery_object_get_id(endpoint);
531 mwi_subs = ao2_find(unsolicited_mwi, endpoint_id, OBJ_SEARCH_KEY | OBJ_MULTIPLE);
537 for (; (mwi_sub = ao2_iterator_next(mwi_subs)) && !ret; ao2_cleanup(mwi_sub)) {
538 struct mwi_stasis_subscription *mwi_stasis;
540 mwi_stasis = ao2_find(mwi_sub->stasis_subs, mailbox, OBJ_SEARCH_KEY);
543 ao2_cleanup(mwi_stasis);
547 ao2_iterator_destroy(mwi_subs);
552 * \brief Determine if an endpoint is a candidate to be able to subscribe for MWI
554 * Currently, this just makes sure that the endpoint is not already receiving unsolicted
555 * MWI for any of an AOR's configured mailboxes.
557 * \param obj The AOR to which the endpoint is subscribing.
558 * \param arg The endpoint that is attempting to subscribe.
559 * \param flags Unused.
560 * \retval 0 Endpoint is a candidate to subscribe to MWI on the AOR.
561 * \retval -1 The endpoint cannot subscribe to MWI on the AOR.
563 static int mwi_validate_for_aor(void *obj, void *arg, int flags)
565 struct ast_sip_aor *aor = obj;
566 struct ast_sip_endpoint *endpoint = arg;
570 if (ast_strlen_zero(aor->mailboxes)) {
574 mailboxes = ast_strdupa(aor->mailboxes);
575 while ((mailbox = strsep(&mailboxes, ","))) {
576 if (endpoint_receives_unsolicited_mwi_for_mailbox(endpoint, mailbox)) {
577 ast_log(LOG_NOTICE, "Endpoint '%s' already configured for unsolicited MWI for mailbox '%s'. "
578 "Denying MWI subscription to %s\n", ast_sorcery_object_get_id(endpoint), mailbox,
579 ast_sorcery_object_get_id(aor));
587 static int mwi_on_aor(void *obj, void *arg, int flags)
589 struct ast_sip_aor *aor = obj;
590 struct mwi_subscription *sub = arg;
594 if (ast_strlen_zero(aor->mailboxes)) {
598 mailboxes = ast_strdupa(aor->mailboxes);
599 while ((mailbox = strsep(&mailboxes, ","))) {
600 struct mwi_stasis_subscription *mwi_stasis_sub;
602 mwi_stasis_sub = mwi_stasis_subscription_alloc(mailbox, sub);
603 if (!mwi_stasis_sub) {
607 ao2_link(sub->stasis_subs, mwi_stasis_sub);
608 ao2_ref(mwi_stasis_sub, -1);
614 static struct mwi_subscription *mwi_create_subscription(
615 struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub)
617 struct mwi_subscription *sub = mwi_subscription_alloc(endpoint, 1, sip_sub);
623 if (add_mwi_datastore(sub)) {
624 ast_log(LOG_WARNING, "Unable to allocate datastore on MWI "
625 "subscription from %s\n", sub->id);
633 static struct mwi_subscription *mwi_subscribe_single(
634 struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub, const char *name)
636 RAII_VAR(struct ast_sip_aor *, aor,
637 ast_sip_location_retrieve_aor(name), ao2_cleanup);
638 struct mwi_subscription *sub;
641 /*! I suppose it's possible for the AOR to disappear on us
642 * between accepting the subscription and sending the first
645 ast_log(LOG_WARNING, "Unable to locate aor %s. MWI subscription failed.\n",
650 if (!(sub = mwi_create_subscription(endpoint, sip_sub))) {
654 mwi_on_aor(aor, sub, 0);
658 static struct mwi_subscription *mwi_subscribe_all(
659 struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub)
661 struct mwi_subscription *sub;
663 sub = mwi_create_subscription(endpoint, sip_sub);
668 ast_sip_for_each_aor(endpoint->aors, mwi_on_aor, sub);
672 static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
673 const char *resource)
675 RAII_VAR(struct ast_sip_aor *, aor, NULL, ao2_cleanup);
677 if (ast_strlen_zero(resource)) {
678 if (ast_sip_for_each_aor(endpoint->aors, mwi_validate_for_aor, endpoint)) {
684 aor = ast_sip_location_retrieve_aor(resource);
686 ast_log(LOG_WARNING, "Unable to locate aor %s. MWI subscription failed.\n",
691 if (ast_strlen_zero(aor->mailboxes)) {
692 ast_log(LOG_NOTICE, "AOR %s has no configured mailboxes. MWI subscription failed.\n",
697 if (mwi_validate_for_aor(aor, endpoint, 0)) {
704 static int mwi_subscription_established(struct ast_sip_subscription *sip_sub)
706 const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
707 struct mwi_subscription *sub;
708 struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
710 /* no aor in uri? subscribe to all on endpoint */
711 if (ast_strlen_zero(resource)) {
712 sub = mwi_subscribe_all(endpoint, sip_sub);
714 sub = mwi_subscribe_single(endpoint, sip_sub, resource);
718 ao2_cleanup(endpoint);
723 ao2_cleanup(endpoint);
727 static void *mwi_get_notify_data(struct ast_sip_subscription *sub)
729 struct ast_sip_message_accumulator *counter;
730 struct mwi_subscription *mwi_sub;
731 struct ast_datastore *mwi_datastore;
733 mwi_datastore = ast_sip_subscription_get_datastore(sub, MWI_DATASTORE);
734 if (!mwi_datastore) {
737 mwi_sub = mwi_datastore->data;
739 counter = ao2_alloc(sizeof(*counter), NULL);
741 ao2_cleanup(mwi_datastore);
745 ao2_callback(mwi_sub->stasis_subs, OBJ_NODATA, get_message_count, counter);
746 ao2_cleanup(mwi_datastore);
750 static void mwi_subscription_mailboxes_str(struct ao2_container *stasis_subs,
751 struct ast_str **str)
753 int num = ao2_container_count(stasis_subs);
755 struct mwi_stasis_subscription *node;
756 struct ao2_iterator i = ao2_iterator_init(stasis_subs, 0);
758 while ((node = ao2_iterator_next(&i))) {
760 ast_str_append(str, 0, "%s,", node->mailbox);
762 ast_str_append(str, 0, "%s", node->mailbox);
766 ao2_iterator_destroy(&i);
769 static void mwi_to_ami(struct ast_sip_subscription *sub,
770 struct ast_str **buf)
772 struct mwi_subscription *mwi_sub;
773 struct ast_datastore *mwi_datastore;
775 mwi_datastore = ast_sip_subscription_get_datastore(sub, MWI_DATASTORE);
776 if (!mwi_datastore) {
780 mwi_sub = mwi_datastore->data;
782 ast_str_append(buf, 0, "SubscriptionType: mwi\r\n");
783 ast_str_append(buf, 0, "Mailboxes: ");
784 mwi_subscription_mailboxes_str(mwi_sub->stasis_subs, buf);
785 ast_str_append(buf, 0, "\r\n");
787 ao2_ref(mwi_datastore, -1);
790 static int serialized_notify(void *userdata)
792 struct mwi_subscription *mwi_sub = userdata;
794 send_mwi_notify(mwi_sub);
795 ao2_ref(mwi_sub, -1);
799 static int serialized_cleanup(void *userdata)
801 struct mwi_subscription *mwi_sub = userdata;
803 /* This is getting rid of the reference that was added
804 * just before this serialized task was pushed.
806 ao2_cleanup(mwi_sub);
807 /* This is getting rid of the reference held by the
808 * stasis subscription
810 ao2_cleanup(mwi_sub);
814 static int send_notify(void *obj, void *arg, int flags)
816 struct mwi_subscription *mwi_sub = obj;
817 struct ast_taskprocessor *serializer = mwi_sub->is_solicited ? ast_sip_subscription_get_serializer(mwi_sub->sip_sub) : NULL;
819 if (ast_sip_push_task(serializer, serialized_notify, ao2_bump(mwi_sub))) {
820 ao2_ref(mwi_sub, -1);
826 static void mwi_stasis_cb(void *userdata, struct stasis_subscription *sub,
827 struct stasis_message *msg)
829 struct mwi_subscription *mwi_sub = userdata;
831 if (stasis_subscription_final_message(sub, msg)) {
832 if (ast_sip_push_task(NULL, serialized_cleanup, ao2_bump(mwi_sub))) {
833 ao2_ref(mwi_sub, -1);
838 if (ast_mwi_state_type() == stasis_message_type(msg)) {
839 send_notify(mwi_sub, NULL, 0);
843 /*! \note Called with the unsolicited_mwi conainer lock held. */
844 static int create_mwi_subscriptions_for_endpoint(void *obj, void *arg, int flags)
846 RAII_VAR(struct mwi_subscription *, aggregate_sub, NULL, ao2_cleanup);
847 struct ast_sip_endpoint *endpoint = obj;
848 char *endpoint_aors, *aor_name, *mailboxes, *mailbox;
849 struct ao2_container *contacts = NULL;
851 if (ast_strlen_zero(endpoint->subscription.mwi.mailboxes)) {
855 endpoint_aors = ast_strdupa(endpoint->aors);
857 while ((aor_name = strsep(&endpoint_aors, ","))) {
858 RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
864 contacts = ast_sip_location_retrieve_aor_contacts(aor);
865 if (!contacts || (ao2_container_count(contacts) == 0)) {
866 ao2_cleanup(contacts);
878 ao2_ref(contacts, -1);
880 if (endpoint->subscription.mwi.aggregate) {
881 aggregate_sub = mwi_subscription_alloc(endpoint, 0, NULL);
882 if (!aggregate_sub) {
887 mailboxes = ast_strdupa(endpoint->subscription.mwi.mailboxes);
888 while ((mailbox = strsep(&mailboxes, ","))) {
889 struct mwi_subscription *sub = aggregate_sub ?:
890 mwi_subscription_alloc(endpoint, 0, NULL);
891 struct mwi_stasis_subscription *mwi_stasis_sub;
893 mwi_stasis_sub = mwi_stasis_subscription_alloc(mailbox, sub);
894 if (mwi_stasis_sub) {
895 ao2_link(sub->stasis_subs, mwi_stasis_sub);
896 ao2_ref(mwi_stasis_sub, -1);
898 if (!aggregate_sub && sub) {
899 ao2_link_flags(unsolicited_mwi, sub, OBJ_NOLOCK);
904 ao2_link_flags(unsolicited_mwi, aggregate_sub, OBJ_NOLOCK);
909 static int unsubscribe(void *obj, void *arg, int flags)
911 struct mwi_subscription *mwi_sub = obj;
913 ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
918 static void create_mwi_subscriptions(void)
920 struct ao2_container *endpoints;
922 endpoints = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "endpoint",
923 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
928 /* We remove all the old stasis subscriptions first before applying the new configuration. This
929 * prevents a situation where there might be multiple overlapping stasis subscriptions for an
930 * endpoint for mailboxes. Though there may be mailbox changes during the gap between unsubscribing
931 * and resubscribing, up-to-date mailbox state will be sent out to the endpoint when the
932 * new stasis subscription is established
934 ao2_lock(unsolicited_mwi);
935 ao2_callback(unsolicited_mwi, OBJ_NOLOCK | OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
936 ao2_callback(endpoints, OBJ_NODATA, create_mwi_subscriptions_for_endpoint, NULL);
937 ao2_unlock(unsolicited_mwi);
939 ao2_ref(endpoints, -1);
942 /*! \brief Function called to send MWI NOTIFY on any unsolicited mailboxes relating to this AOR */
943 static int send_contact_notify(void *obj, void *arg, int flags)
945 struct mwi_subscription *mwi_sub = obj;
946 const char *aor = arg;
948 if (!mwi_sub->aors || !strstr(mwi_sub->aors, aor)) {
952 if (ast_sip_push_task(NULL, serialized_notify, ao2_bump(mwi_sub))) {
953 ao2_ref(mwi_sub, -1);
959 /*! \brief Function called when a contact is updated */
960 static void mwi_contact_updated(const void *object)
962 char *id = ast_strdupa(ast_sorcery_object_get_id(object)), *aor = NULL;
964 aor = strsep(&id, ";@");
966 ao2_callback(unsolicited_mwi, OBJ_NODATA, send_contact_notify, aor);
969 /*! \brief Function called when a contact is added */
970 static void mwi_contact_added(const void *object)
972 const struct ast_sip_contact *contact = object;
973 struct ao2_iterator *mwi_subs;
974 struct mwi_subscription *mwi_sub;
975 const char *endpoint_id = ast_sorcery_object_get_id(contact->endpoint);
977 if (ast_strlen_zero(contact->endpoint->subscription.mwi.mailboxes)) {
981 ao2_lock(unsolicited_mwi);
983 mwi_subs = ao2_find(unsolicited_mwi, endpoint_id, OBJ_SEARCH_KEY | OBJ_MULTIPLE | OBJ_NOLOCK | OBJ_UNLINK);
985 for (; (mwi_sub = ao2_iterator_next(mwi_subs)); ao2_cleanup(mwi_sub)) {
986 unsubscribe(mwi_sub, NULL, 0);
988 ao2_iterator_destroy(mwi_subs);
991 create_mwi_subscriptions_for_endpoint(contact->endpoint, NULL, 0);
993 ao2_unlock(unsolicited_mwi);
995 mwi_contact_updated(object);
998 /*! \brief Observer for contacts so unsolicited MWI is sent when a contact changes */
999 static const struct ast_sorcery_observer mwi_contact_observer = {
1000 .created = mwi_contact_added,
1001 .updated = mwi_contact_updated,
1004 /*! \brief Task invoked to send initial MWI NOTIFY for unsolicited */
1005 static int send_initial_notify_all(void *obj)
1007 ao2_callback(unsolicited_mwi, OBJ_NODATA, send_notify, NULL);
1012 /*! \brief Event callback which fires initial unsolicited MWI NOTIFY messages when we're fully booted */
1013 static void mwi_startup_event_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
1015 struct ast_json_payload *payload;
1018 if (stasis_message_type(message) != ast_manager_get_generic_type()) {
1022 payload = stasis_message_data(message);
1023 type = ast_json_string_get(ast_json_object_get(payload->json, "type"));
1025 if (strcmp(type, "FullyBooted")) {
1029 ast_sip_push_task(NULL, send_initial_notify_all, NULL);
1031 stasis_unsubscribe(sub);
1034 static int reload(void)
1036 create_mwi_subscriptions();
1040 static int load_module(void)
1042 CHECK_PJSIP_MODULE_LOADED();
1044 if (ast_sip_register_subscription_handler(&mwi_handler)) {
1045 return AST_MODULE_LOAD_DECLINE;
1048 unsolicited_mwi = ao2_container_alloc(MWI_BUCKETS, mwi_sub_hash, mwi_sub_cmp);
1049 if (!unsolicited_mwi) {
1050 ast_sip_unregister_subscription_handler(&mwi_handler);
1051 return AST_MODULE_LOAD_DECLINE;
1054 create_mwi_subscriptions();
1055 ast_sorcery_observer_add(ast_sip_get_sorcery(), "contact", &mwi_contact_observer);
1057 if (ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)) {
1058 ast_sip_push_task(NULL, send_initial_notify_all, NULL);
1060 stasis_subscribe_pool(ast_manager_get_topic(), mwi_startup_event_cb, NULL);
1063 return AST_MODULE_LOAD_SUCCESS;
1066 static int unload_module(void)
1068 ao2_callback(unsolicited_mwi, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
1069 ao2_ref(unsolicited_mwi, -1);
1070 ast_sorcery_observer_remove(ast_sip_get_sorcery(), "contact", &mwi_contact_observer);
1071 ast_sip_unregister_subscription_handler(&mwi_handler);
1075 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP MWI resource",
1076 .support_level = AST_MODULE_SUPPORT_CORE,
1077 .load = load_module,
1078 .unload = unload_module,
1080 .load_pri = AST_MODPRI_CHANNEL_DEPEND,