Add support for RFC 4662 resource list subscriptions.
authorMark Michelson <mmichelson@digium.com>
Thu, 7 Aug 2014 19:26:32 +0000 (19:26 +0000)
committerMark Michelson <mmichelson@digium.com>
Thu, 7 Aug 2014 19:26:32 +0000 (19:26 +0000)
This commit adds the ability for a user to configure
a resource list in pjsip.conf. Subscribing to this
list simultaneously subscribes the subscriber to all
resources listed. This has the potential to reduce
the amount of SIP traffic when loads of subscribers
on a system attempt to subscribe to each others' states.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@420384 65c4cc65-6c06-0410-ace0-fbb531ad65f3

include/asterisk/res_pjsip_presence_xml.h
include/asterisk/res_pjsip_pubsub.h
include/asterisk/strings.h
main/strings.c
res/res_pjsip_dialog_info_body_generator.c
res/res_pjsip_exten_state.c
res/res_pjsip_mwi.c
res/res_pjsip_pidf_body_generator.c
res/res_pjsip_pubsub.c
res/res_pjsip_xpidf_body_generator.c

index 8318067..add5f89 100644 (file)
  */
 
 /*!
+ * \brief The length of the XML prolog when printing
+ * presence or other XML in PJSIP.
+ *
+ * When calling any variant of pj_xml_print(), the documentation
+ * claims that it will return -1 if the provided buffer is not
+ * large enough. However, if the XML prolog is requested to be
+ * printed, then the length of the XML prolog is returned upon
+ * failure instead of -1.
+ *
+ * This constant is useful to check against when trying to determine
+ * if printing XML succeeded or failed.
+ */
+#define AST_PJSIP_XML_PROLOG_LEN 39
+
+/*!
  * PIDF state
  */
 enum ast_sip_pidf_state {
index 8ad1334..73b9874 100644 (file)
@@ -240,23 +240,29 @@ struct ast_sip_notifier {
         */
        int (*new_subscribe)(struct ast_sip_endpoint *endpoint, const char *resource);
        /*!
-        * \brief The subscription is in need of a NOTIFY request.
+        * \brief Called when an inbound subscription has been accepted.
         *
-        * A reason of AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED is given immediately
-        * after a SUBSCRIBE is accepted. This is a good opportunity for the notifier to
-        * perform setup duties such as establishing Stasis subscriptions or adding
-        * datastores to the subscription.
+        * This is a prime opportunity for notifiers to add any notifier-specific
+        * data to the subscription (such as datastores) that it needs to.
         *
-        * A reason of AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED is given when the
-        * subscriber has terminated the subscription. If there are any duties that the
+        * \note There is no need to send a NOTIFY request when this callback
+        * is called
         *
-        *
-        * \param sub The subscription to send the NOTIFY on.
-        * \param reason The reason why the NOTIFY is being sent.
+        * \param sub The new subscription
         * \retval 0 Success
         * \retval -1 Failure
         */
-       int (*notify_required)(struct ast_sip_subscription *sub, enum ast_sip_subscription_notify_reason reason);
+       int (*subscription_established)(struct ast_sip_subscription *sub);
+       /*!
+        * \brief Supply data needed to create a NOTIFY body.
+        *
+        * The returned data must be an ao2 object. The caller of this function
+        * will be responsible for decrementing the refcount of the returned object
+        *
+        * \param sub The subscription
+        * \return An ao2 object that can be used to create a NOTIFY body.
+        */
+       void *(*get_notify_data)(struct ast_sip_subscription *sub);
 };
 
 struct ast_sip_subscriber {
@@ -343,10 +349,9 @@ struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_sub
 /*!
  * \brief Notify a SIP subscription of a state change.
  *
- * This will create a NOTIFY body to be sent out for the subscribed resource.
- * On real subscriptions, a NOTIFY request will be generated and sent.
- * On virtual subscriptions, the NOTIFY is saved on the virtual subscription and the
- * parent subscription is alerted.
+ * This tells the pubsub core that the state of a subscribed resource has changed.
+ * The pubsub core will generate an appropriate NOTIFY request to send to the
+ * subscriber.
  *
  * \param sub The subscription on which a state change is occurring.
  * \param notify_data Event package-specific data used to create the NOTIFY body.
@@ -359,7 +364,7 @@ int ast_sip_subscription_notify(struct ast_sip_subscription *sub, void *notify_d
 /*!
  * \brief Retrieve the local URI for this subscription
  *
- * This is the local URI as determined by the underlying SIP dialog.
+ * This is the local URI of the subscribed resource.
  *
  * \param sub The subscription
  * \param[out] buf The buffer into which to store the URI.
index 5dbebba..0b98a2b 100644 (file)
@@ -1196,4 +1196,20 @@ int ast_str_container_add(struct ao2_container *str_container, const char *add);
  */
 void ast_str_container_remove(struct ao2_container *str_container, const char *remove);
 
+/*!
+ * \brief Create a pseudo-random string of a fixed length.
+ *
+ * This function is useful for generating a string whose randomness
+ * does not need to be across all time and space, does not need to
+ * be cryptographically secure, and needs to fit in a limited space.
+ *
+ * This function will write a null byte at the final position
+ * in the buffer (buf[size - 1]). So if you pass in a size of
+ * 10, then this will generate a random 9-character string.
+ *
+ * \param buf Buffer to write random string into.
+ * \param size The size of the buffer.
+ * \return A pointer to buf
+ */
+char *ast_generate_random_string(char *buf, size_t size);
 #endif /* _ASTERISK_STRINGS_H */
index a65df39..73892eb 100644 (file)
@@ -195,3 +195,15 @@ void ast_str_container_remove(struct ao2_container *str_container, const char *r
 {
        ao2_find(str_container, remove, OBJ_SEARCH_KEY | OBJ_NODATA | OBJ_UNLINK);
 }
+
+char *ast_generate_random_string(char *buf, size_t size)
+{
+       int i;
+
+       for (i = 0; i < size - 1; ++i) {
+               buf[i] = 'a' + (ast_random() % 26);
+       }
+       buf[i] = '\0';
+
+       return buf;
+}
index d4ad216..848ec10 100644 (file)
@@ -156,11 +156,6 @@ static int dialog_info_generate_body_content(void *body, void *data)
  */
 #define MAX_STRING_GROWTHS 3
 
-/* When having pj_xml_print add the XML prolog to the output body the function will return 39
- * instead of -1 if the rest of the document can not be printed into the body.
- */
-#define XML_PROLOG 39
-
 static void dialog_info_to_string(void *body, struct ast_str **str)
 {
        pj_xml_node *dialog_info = body;
@@ -169,13 +164,13 @@ static void dialog_info_to_string(void *body, struct ast_str **str)
 
        do {
                size = pj_xml_print(dialog_info, ast_str_buffer(*str), ast_str_size(*str), PJ_TRUE);
-               if (size == XML_PROLOG) {
+               if (size == AST_PJSIP_XML_PROLOG_LEN) {
                        ast_str_make_space(str, ast_str_size(*str) * 2);
                        ++growths;
                }
-       } while (size == XML_PROLOG && growths < MAX_STRING_GROWTHS);
+       } while (size == AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
 
-       if (size == XML_PROLOG) {
+       if (size == AST_PJSIP_XML_PROLOG_LEN) {
                ast_log(LOG_WARNING, "dialog-info+xml body text too large\n");
                return;
        }
index fb6f72b..29b26cc 100644 (file)
@@ -70,15 +70,23 @@ struct exten_state_subscription {
 
 static void subscription_shutdown(struct ast_sip_subscription *sub);
 static int new_subscribe(struct ast_sip_endpoint *endpoint, const char *resource);
-static int notify_required(struct ast_sip_subscription *sub,
-               enum ast_sip_subscription_notify_reason reason);
+static int subscription_established(struct ast_sip_subscription *sub);
+static void *get_notify_data(struct ast_sip_subscription *sub);
 static void to_ami(struct ast_sip_subscription *sub,
                   struct ast_str **buf);
 
 struct ast_sip_notifier presence_notifier = {
        .default_accept = DEFAULT_PRESENCE_BODY,
        .new_subscribe = new_subscribe,
-       .notify_required = notify_required,
+       .subscription_established = subscription_established,
+       .get_notify_data = get_notify_data,
+};
+
+struct ast_sip_notifier dialog_notifier = {
+       .default_accept = DEFAULT_DIALOG_BODY,
+       .new_subscribe = new_subscribe,
+       .subscription_established = subscription_established,
+       .get_notify_data = get_notify_data,
 };
 
 struct ast_sip_subscription_handler presence_handler = {
@@ -94,7 +102,7 @@ struct ast_sip_subscription_handler dialog_handler = {
        .accept = { DEFAULT_DIALOG_BODY, },
        .subscription_shutdown = subscription_shutdown,
        .to_ami = to_ami,
-       .notifier = &presence_notifier,
+       .notifier = &dialog_notifier,
 };
 
 static void exten_state_subscription_destructor(void *obj)
@@ -153,45 +161,6 @@ static struct exten_state_subscription *exten_state_subscription_alloc(
        return exten_state_sub;
 }
 
-/*!
- * \internal
- * \brief Get device state information and send notification to the subscriber.
- */
-static void send_notify(struct exten_state_subscription *exten_state_sub)
-{
-       RAII_VAR(struct ao2_container*, info, NULL, ao2_cleanup);
-       char *subtype = NULL, *message = NULL;
-       struct ast_sip_exten_state_data exten_state_data = {
-               .exten = exten_state_sub->exten,
-               .presence_state = ast_hint_presence_state(NULL, exten_state_sub->context,
-                                                         exten_state_sub->exten, &subtype, &message),
-               .presence_subtype = subtype,
-               .presence_message = message,
-               .sub = exten_state_sub->sip_sub,
-               .user_agent = exten_state_sub->user_agent
-       };
-
-       ast_sip_subscription_get_local_uri(exten_state_sub->sip_sub,
-                       exten_state_data.local, sizeof(exten_state_data.local));
-       ast_sip_subscription_get_remote_uri(exten_state_sub->sip_sub,
-                       exten_state_data.remote, sizeof(exten_state_data.remote));
-
-       if ((exten_state_data.exten_state = ast_extension_state_extended(
-                    NULL, exten_state_sub->context, exten_state_sub->exten, &info)) < 0) {
-
-               ast_log(LOG_WARNING, "Unable to get device hint/info for extension %s\n",
-                       exten_state_sub->exten);
-               return;
-       }
-
-       exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
-                       "exten_state", 1024, 1024);
-
-       exten_state_data.device_state_info = info;
-       ast_sip_subscription_notify(exten_state_sub->sip_sub, &exten_state_data, 0);
-       pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), exten_state_data.pool);
-}
-
 struct notify_task_data {
        struct ast_sip_exten_state_data exten_state_data;
        struct exten_state_subscription *exten_state_sub;
@@ -231,11 +200,8 @@ static struct notify_task_data *alloc_notify_task_data(char *exten, struct exten
        task_data->exten_state_data.presence_subtype = ast_strdup(info->presence_subtype);
        task_data->exten_state_data.presence_message = ast_strdup(info->presence_message);
        task_data->exten_state_data.user_agent = ast_strdup(exten_state_sub->user_agent);
-       task_data->exten_state_data.device_state_info = info->device_state_info;
-
-       if (task_data->exten_state_data.device_state_info) {
-               ao2_ref(task_data->exten_state_data.device_state_info, +1);
-       }
+       task_data->exten_state_data.device_state_info = ao2_bump(info->device_state_info);
+       task_data->exten_state_data.sub = exten_state_sub->sip_sub;
 
        ast_sip_subscription_get_local_uri(exten_state_sub->sip_sub,
                        task_data->exten_state_data.local, sizeof(task_data->exten_state_data.local));
@@ -259,6 +225,9 @@ static int notify_task(void *obj)
        /* Pool allocation has to happen here so that we allocate within a PJLIB thread */
        task_data->exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
                        "exten_state", 1024, 1024);
+       if (!task_data->exten_state_data.pool) {
+               return -1;
+       }
 
        task_data->exten_state_data.sub = task_data->exten_state_sub->sip_sub;
 
@@ -366,7 +335,7 @@ static int new_subscribe(struct ast_sip_endpoint *endpoint,
        return 200;
 }
 
-static int initial_subscribe(struct ast_sip_subscription *sip_sub)
+static int subscription_established(struct ast_sip_subscription *sip_sub)
 {
        struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
        const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
@@ -403,33 +372,77 @@ static int initial_subscribe(struct ast_sip_subscription *sip_sub)
                return -1;
        }
 
-       send_notify(exten_state_sub);
        ao2_cleanup(exten_state_sub);
        return 0;
 }
 
-static int notify_required(struct ast_sip_subscription *sub,
-               enum ast_sip_subscription_notify_reason reason)
+static void exten_state_data_destructor(void *obj)
 {
-       struct exten_state_subscription *exten_state_sub;
+       struct ast_sip_exten_state_data *exten_state_data = obj;
 
-       switch (reason) {
-       case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED:
-               return initial_subscribe(sub);
-       case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_RENEWED:
-       case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED:
-       case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_OTHER:
-               exten_state_sub = get_exten_state_sub(sub);
+       ao2_cleanup(exten_state_data->device_state_info);
+       ast_free(exten_state_data->presence_subtype);
+       ast_free(exten_state_data->presence_message);
+       if (exten_state_data->pool) {
+               pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), exten_state_data->pool);
+       }
+}
 
-               if (!exten_state_sub) {
-                       return -1;
-               }
+static struct ast_sip_exten_state_data *exten_state_data_alloc(struct ast_sip_subscription *sip_sub,
+               struct exten_state_subscription *exten_state_sub)
+{
+       struct ast_sip_exten_state_data *exten_state_data;
+       char *subtype = NULL;
+       char *message = NULL;
 
-               send_notify(exten_state_sub);
-               break;
+       exten_state_data = ao2_alloc(sizeof(*exten_state_data), exten_state_data_destructor);
+       if (!exten_state_data) {
+               return NULL;
        }
 
-       return 0;
+       exten_state_data->exten = exten_state_sub->exten;
+       if ((exten_state_data->presence_state = ast_hint_presence_state(NULL, exten_state_sub->context,
+                       exten_state_sub->exten, &subtype, &message)) == -1) {
+               ao2_cleanup(exten_state_data);
+               return NULL;
+       }
+       exten_state_data->presence_subtype = subtype;
+       exten_state_data->presence_message = message;
+       exten_state_data->user_agent = exten_state_sub->user_agent;
+       ast_sip_subscription_get_local_uri(sip_sub, exten_state_data->local,
+                       sizeof(exten_state_data->local));
+       ast_sip_subscription_get_remote_uri(sip_sub, exten_state_data->remote,
+                       sizeof(exten_state_data->remote));
+       exten_state_data->sub = sip_sub;
+
+       exten_state_data->exten_state = ast_extension_state_extended(
+                       NULL, exten_state_sub->context, exten_state_sub->exten,
+                       &exten_state_data->device_state_info);
+       if (exten_state_data->exten_state < 0) {
+               ao2_cleanup(exten_state_data);
+               return NULL;
+       }
+
+       exten_state_data->pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
+                       "exten_state", 1024, 1024);
+       if (!exten_state_data->pool) {
+               ao2_cleanup(exten_state_data);
+               return NULL;
+       }
+
+       return exten_state_data;
+}
+
+static void *get_notify_data(struct ast_sip_subscription *sub)
+{
+       struct exten_state_subscription *exten_state_sub;
+
+       exten_state_sub = get_exten_state_sub(sub);
+       if (!exten_state_sub) {
+               return NULL;
+       }
+
+       return exten_state_data_alloc(sub, exten_state_sub);
 }
 
 static void to_ami(struct ast_sip_subscription *sub,
index 55ef300..37e1da0 100644 (file)
@@ -48,17 +48,20 @@ AO2_GLOBAL_OBJ_STATIC(unsolicited_mwi);
 #define MWI_TYPE "application"
 #define MWI_SUBTYPE "simple-message-summary"
 
+#define MWI_DATASTORE "MWI datastore"
+
 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_notify_required(struct ast_sip_subscription *sip_sub,
-               enum ast_sip_subscription_notify_reason reason);
+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,
-       .notify_required = mwi_notify_required,
+       .subscription_established = mwi_subscription_established,
+       .get_notify_data = mwi_get_notify_data,
 };
 
 static struct ast_sip_subscription_handler mwi_handler = {
@@ -457,7 +460,7 @@ static void mwi_subscription_shutdown(struct ast_sip_subscription *sub)
 {
        struct mwi_subscription *mwi_sub;
        RAII_VAR(struct ast_datastore *, mwi_datastore,
-                       ast_sip_subscription_get_datastore(sub, "MWI datastore"), ao2_cleanup);
+                       ast_sip_subscription_get_datastore(sub, MWI_DATASTORE), ao2_cleanup);
 
        if (!mwi_datastore) {
                return;
@@ -473,7 +476,7 @@ static int add_mwi_datastore(struct mwi_subscription *sub)
 {
        RAII_VAR(struct ast_datastore *, mwi_datastore, NULL, ao2_cleanup);
 
-       mwi_datastore = ast_sip_subscription_alloc_datastore(&mwi_ds_info, "MWI datastore");
+       mwi_datastore = ast_sip_subscription_alloc_datastore(&mwi_ds_info, MWI_DATASTORE);
        if (!mwi_datastore) {
                return -1;
        }
@@ -676,7 +679,7 @@ static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
        return 200;
 }
 
-static int mwi_initial_subscription(struct ast_sip_subscription *sip_sub)
+static int mwi_subscription_established(struct ast_sip_subscription *sip_sub)
 {
        const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
        struct mwi_subscription *sub;
@@ -694,39 +697,32 @@ static int mwi_initial_subscription(struct ast_sip_subscription *sip_sub)
                return -1;
        }
 
-       send_mwi_notify(sub);
-
        ao2_cleanup(sub);
        ao2_cleanup(endpoint);
        return 0;
 }
 
-static int mwi_notify_required(struct ast_sip_subscription *sip_sub,
-               enum ast_sip_subscription_notify_reason reason)
+static void *mwi_get_notify_data(struct ast_sip_subscription *sub)
 {
+       struct ast_sip_message_accumulator *counter;
        struct mwi_subscription *mwi_sub;
        struct ast_datastore *mwi_datastore;
 
-       switch (reason) {
-       case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED:
-               return mwi_initial_subscription(sip_sub);
-       case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_RENEWED:
-       case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED:
-       case AST_SIP_SUBSCRIPTION_NOTIFY_REASON_OTHER:
-               mwi_datastore = ast_sip_subscription_get_datastore(sip_sub, "MWI datastore");
-
-               if (!mwi_datastore) {
-                       return -1;
-               }
-
-               mwi_sub = mwi_datastore->data;
+       mwi_datastore = ast_sip_subscription_get_datastore(sub, MWI_DATASTORE);
+       if (!mwi_datastore) {
+               return NULL;
+       }
+       mwi_sub = mwi_datastore->data;
 
-               send_mwi_notify(mwi_sub);
+       counter = ao2_alloc(sizeof(*counter), NULL);
+       if (!counter) {
                ao2_cleanup(mwi_datastore);
-               break;
+               return NULL;
        }
 
-       return 0;
+       ao2_callback(mwi_sub->stasis_subs, OBJ_NODATA, get_message_count, counter);
+       ao2_cleanup(mwi_datastore);
+       return counter;
 }
 
 static void mwi_subscription_mailboxes_str(struct ao2_container *stasis_subs,
@@ -753,7 +749,7 @@ static void mwi_to_ami(struct ast_sip_subscription *sub,
 {
        struct mwi_subscription *mwi_sub;
        RAII_VAR(struct ast_datastore *, mwi_datastore,
-                       ast_sip_subscription_get_datastore(sub, "MWI datastore"), ao2_cleanup);
+                       ast_sip_subscription_get_datastore(sub, MWI_DATASTORE), ao2_cleanup);
 
        if (!mwi_datastore) {
                return;
index b3164a2..3181b29 100644 (file)
@@ -81,7 +81,6 @@ static int pidf_generate_body_content(void *body, void *data)
 }
 
 #define MAX_STRING_GROWTHS 5
-#define XML_PROLOG 39
 
 static void pidf_to_string(void *body, struct ast_str **str)
 {
@@ -91,13 +90,13 @@ static void pidf_to_string(void *body, struct ast_str **str)
 
        do {
                size = pjpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str) - 1);
-               if (size == XML_PROLOG) {
+               if (size == AST_PJSIP_XML_PROLOG_LEN) {
                        ast_str_make_space(str, ast_str_size(*str) * 2);
                        ++growths;
                }
-       } while (size == XML_PROLOG && growths < MAX_STRING_GROWTHS);
+       } while (size == AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
 
-       if (size == XML_PROLOG) {
+       if (size == AST_PJSIP_XML_PROLOG_LEN) {
                ast_log(LOG_WARNING, "PIDF body text too large\n");
                return;
        }
index 6a8ec12..09fd629 100644 (file)
@@ -44,6 +44,7 @@
 #include "asterisk/manager.h"
 #include "asterisk/test.h"
 #include "res_pjsip/include/res_pjsip_private.h"
+#include "asterisk/res_pjsip_presence_xml.h"
 
 /*** DOCUMENTATION
        <manager name="PJSIPShowSubscriptionsInbound" language="en_US">
                         </para>
                </description>
        </manager>
+       <manager name="PJSIPShowResourceLists" language="en_US">
+               <synopsis>
+                       Displays settings for configured resource lists.
+               </synopsis>
+               <syntax />
+               <description>
+                       <para>
+                       Provides a listing of all resource lists.  An event <literal>ResourceListDetail</literal>
+                       is issued for each resource list object.  Once all detail events are completed a
+                       <literal>ResourceListDetailComplete</literal> event is issued.
+                        </para>
+               </description>
+       </manager>
+
        <configInfo name="res_pjsip_pubsub" language="en_US">
                <synopsis>Module that implements publish and subscribe support.</synopsis>
                <configFile name="pjsip.conf">
                                        <synopsis>The time at which the subscription expires</synopsis>
                                </configOption>
                        </configObject>
+                       <configObject name="resource_list">
+                               <synopsis>Resource list configuration parameters.</synopsis>
+                               <configOption name="type">
+                                       <synopsis>Must be of type 'resource_list'</synopsis>
+                               </configOption>
+                               <configOption name="event">
+                                       <synopsis>The SIP event package that the list resource belong to.</synopsis>
+                                       <description><para>
+                                               The SIP event package describes the types of resources that Asterisk reports
+                                               the state of.
+                                       </para>
+                                               <enumlist>
+                                                       <enum name="presence"><para>
+                                                               Device state and presence reporting.
+                                                       </para></enum>
+                                                       <enum name="message-summary"><para>
+                                                               Message-waiting indication (MWI) reporting.
+                                                       </para></enum>
+                                               </enumlist>
+                                       </description>
+                               </configOption>
+                               <configOption name="list_item">
+                                       <synopsis>The name of a resource to report state on</synopsis>
+                                       <description>
+                                               <para>In general Asterisk looks up list items in the following way:</para>
+                                               <para>1. Check if the list item refers to another configured resource list.</para>
+                                               <para>2. Pass the name of the resource off to event-package-specific handlers
+                                                  to find the specified resource.</para>
+                                               <para>The second part means that the way the list item is specified depends
+                                               on what type of list this is. For instance, if you have the <replaceable>event</replaceable>
+                                               set to <literal>presence</literal>, then list items should be in the form of
+                                               dialplan_extension@dialplan_context. For <literal>message-summary</literal> mailbox
+                                               names should be listed.</para>
+                                       </description>
+                               </configOption>
+                               <configOption name="full_state" default="no">
+                                       <synopsis>Indicates if the entire list's state should be sent out.</synopsis>
+                                       <description>
+                                               <para>If this option is enabled, and a resource changes state, then Asterisk will construct
+                                               a notification that contains the state of all resources in the list. If the option is
+                                               disabled, Asterisk will construct a notification that only contains the states of
+                                               resources that have changed.</para>
+                                               <note>
+                                                       <para>Even with this option disabled, there are certain situations where Asterisk is forced
+                                                       to send a notification with the states of all resources in the list. When a subscriber
+                                                       renews or terminates its subscription to the list, Asterisk MUST send a full state
+                                                       notification.</para>
+                                               </note>
+                                       </description>
+                               </configOption>
+                               <configOption name="notification_batch_interval" default="0">
+                                       <synopsis>Time Asterisk should wait, in milliseconds, before sending notifications.</synopsis>
+                                       <description>
+                                               <para>When a resource's state changes, it may be desired to wait a certain amount before Asterisk
+                                               sends a notification to subscribers. This allows for other state changes to accumulate, so that
+                                               Asterisk can communicate multiple state changes in a single notification instead of rapidly sending
+                                               many notifications.</para>
+                                       </description>
+                               </configOption>
+                       </configObject>
                        <configObject name="inbound-publication">
                                <synopsis>The configuration for inbound publications</synopsis>
                                <configOption name="endpoint" default="">
@@ -143,6 +218,12 @@ static struct ast_sched_context *sched;
 /*! \brief Default expiration time for PUBLISH if one is not specified */
 #define DEFAULT_PUBLISH_EXPIRES 3600
 
+/*! \brief Number of buckets for subscription datastore */
+#define DATASTORE_BUCKETS 53
+
+/*! \brief Default expiration for subscriptions */
+#define DEFAULT_EXPIRES 3600
+
 /*! \brief Defined method for PUBLISH */
 const pjsip_method pjsip_publish_method =
 {
@@ -206,6 +287,26 @@ enum sip_publish_type {
 };
 
 /*!
+ * \brief A vector of strings commonly used throughout this module
+ */
+AST_VECTOR(resources, const char *);
+
+/*!
+ * \brief Resource list configuration item
+ */
+struct resource_list {
+       SORCERY_OBJECT(details);
+       /*! SIP event package the list uses. */
+       char event[32];
+       /*! Strings representing resources in the list. */
+       struct resources items;
+       /*! Indicates if Asterisk sends full or partial state on notifications. */
+       unsigned int full_state;
+       /*! Time, in milliseconds Asterisk waits before sending a batched notification.*/
+       unsigned int notification_batch_interval;
+};
+
+/*!
  * Used to create new entity IDs by ESCs.
  */
 static int esc_etag_counter;
@@ -264,83 +365,72 @@ struct subscription_persistence {
 };
 
 /*!
- * \brief Real subscription details
+ * \brief A tree of SIP subscriptions
  *
- * A real subscription is one that has a direct link to a
- * PJSIP subscription and dialog.
+ * Because of the ability to subscribe to resource lists, a SIP
+ * subscription can result in a tree of subscriptions being created.
+ * This structure represents the information relevant to the subscription
+ * as a whole, to include the underlying PJSIP structure for the
+ * subscription.
  */
-struct ast_sip_real_subscription {
+struct sip_subscription_tree {
+       /*! The endpoint with which the subscription is communicating */
+       struct ast_sip_endpoint *endpoint;
+       /*! Serializer on which to place operations for this subscription */
+       struct ast_taskprocessor *serializer;
+       /*! The role for this subscription */
+       enum ast_sip_subscription_role role;
+       /*! Persistence information */
+       struct subscription_persistence *persistence;
        /*! The underlying PJSIP event subscription structure */
        pjsip_evsub *evsub;
        /*! The underlying PJSIP dialog */
        pjsip_dialog *dlg;
+       /*! Interval to use for batching notifications */
+       unsigned int notification_batch_interval;
+       /*! Scheduler ID for batched notification */
+       int notify_sched_id;
+       /*! Indicator if scheduled batched notification should be sent */
+       unsigned int send_scheduled_notify;
+       /*! The root of the subscription tree */
+       struct ast_sip_subscription *root;
+       /*! Is this subscription to a list? */
+       int is_list;
+       /*! Next item in the list */
+       AST_LIST_ENTRY(sip_subscription_tree) next;
 };
 
 /*!
- * \brief Virtual subscription details
+ * \brief Structure representing a "virtual" SIP subscription.
  *
- * A virtual subscription is one that does not have a direct
- * link to a PJSIP subscription. Instead, it is a descendent
- * of an ast_sip_subscription. Following the ancestry will
- * eventually lead to a real subscription.
- */
-struct ast_sip_virtual_subscription {
-       struct ast_sip_subscription *parent;
-};
-
-/*!
- * \brief Discriminator between real and virtual subscriptions
- */
-enum sip_subscription_type {
-       /*!
-        * \brief a "real" subscription.
-        *
-        * Real subscriptions are at the root of a tree of subscriptions.
-        * A real subscription has a corresponding SIP subscription in the
-        * PJSIP stack.
-        */
-       SIP_SUBSCRIPTION_REAL,
-       /*!
-        * \brief a "virtual" subscription.
-        *
-        * Virtual subscriptions are the descendents of real subscriptions
-        * in a tree of subscriptions. Virtual subscriptions do not have
-        * a corresponding SIP subscription in the PJSIP stack. Instead,
-        * when a state change happens on a virtual subscription, the
-        * state change is indicated to the virtual subscription's parent.
-        */
-       SIP_SUBSCRIPTION_VIRTUAL,
-};
-
-/*!
- * \brief Structure representing a SIP subscription
+ * This structure serves a dual purpose. Structurally, it is
+ * the constructed tree of subscriptions based on the resources
+ * being subscribed to. API-wise, this serves as the handle that
+ * subscription handlers use in order to interact with the pubsub API.
  */
 struct ast_sip_subscription {
        /*! Subscription datastores set up by handlers */
        struct ao2_container *datastores;
-       /*! The endpoint with which the subscription is communicating */
-       struct ast_sip_endpoint *endpoint;
-       /*! Serializer on which to place operations for this subscription */
-       struct ast_taskprocessor *serializer;
        /*! The handler for this subscription */
        const struct ast_sip_subscription_handler *handler;
-       /*! The role for this subscription */
-       enum ast_sip_subscription_role role;
-       /*! Indicator of real or virtual subscription */
-       enum sip_subscription_type type;
-       /*! Real and virtual components of the subscription */
-       union {
-               struct ast_sip_real_subscription real;
-               struct ast_sip_virtual_subscription virtual;
-       } reality;
+       /*! Pointer to the base of the tree */
+       struct sip_subscription_tree *tree;
        /*! Body generaator for NOTIFYs */
        struct ast_sip_pubsub_body_generator *body_generator;
-       /*! Persistence information */
-       struct subscription_persistence *persistence;
-       /*! Next item in the list */
-       AST_LIST_ENTRY(ast_sip_subscription) next;
-       /*! List of child subscriptions */
-       AST_LIST_HEAD_NOLOCK(,ast_sip_subscription) children;
+       /*! Vector of child subscriptions */
+       AST_VECTOR(, struct ast_sip_subscription *) children;
+       /*! Saved NOTIFY body text for this subscription */
+       struct ast_str *body_text;
+       /*! Indicator that the body text has changed since the last notification */
+       int body_changed;
+       /*! The current state of the subscription */
+       pjsip_evsub_state subscription_state;
+       /*! For lists, the current version to place in the RLMI body */
+       unsigned int version;
+       /*! For lists, indicates if full state should always be communicated. */
+       unsigned int full_state;
+       /*! URI associated with the subscription */
+       pjsip_sip_uri *uri;
        /*! Name of resource being subscribed to */
        char resource[0];
 };
@@ -362,20 +452,26 @@ static const char *sip_subscription_roles_map[] = {
        [AST_SIP_NOTIFIER] = "Notifier"
 };
 
-AST_RWLIST_HEAD_STATIC(subscriptions, ast_sip_subscription);
+AST_RWLIST_HEAD_STATIC(subscriptions, sip_subscription_tree);
 
 AST_RWLIST_HEAD_STATIC(body_generators, ast_sip_pubsub_body_generator);
 AST_RWLIST_HEAD_STATIC(body_supplements, ast_sip_pubsub_body_supplement);
 
-static pjsip_evsub *sip_subscription_get_evsub(const struct ast_sip_subscription *sub)
-{
-       return sub->reality.real.evsub;
-}
-
-static pjsip_dialog *sip_subscription_get_dlg(const struct ast_sip_subscription *sub)
-{
-       return sub->reality.real.dlg;
-}
+static void pubsub_on_evsub_state(pjsip_evsub *sub, pjsip_event *event);
+static void pubsub_on_rx_refresh(pjsip_evsub *sub, pjsip_rx_data *rdata,
+               int *p_st_code, pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body);
+static void pubsub_on_rx_notify(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code,
+               pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body);
+static void pubsub_on_client_refresh(pjsip_evsub *sub);
+static void pubsub_on_server_timeout(pjsip_evsub *sub);
+static pjsip_evsub_user pubsub_cb = {
+       .on_evsub_state = pubsub_on_evsub_state,
+       .on_rx_refresh = pubsub_on_rx_refresh,
+       .on_rx_notify = pubsub_on_rx_notify,
+       .on_client_refresh = pubsub_on_client_refresh,
+       .on_server_timeout = pubsub_on_server_timeout,
+};
 
 /*! \brief Destructor for publication resource */
 static void publication_resource_destroy(void *obj)
@@ -408,7 +504,7 @@ static void *subscription_persistence_alloc(const char *name)
 }
 
 /*! \brief Function which creates initial persistence information of a subscription in sorcery */
-static struct subscription_persistence *subscription_persistence_create(struct ast_sip_subscription *sub)
+static struct subscription_persistence *subscription_persistence_create(struct sip_subscription_tree *sub_tree)
 {
        char tag[PJ_GUID_STRING_LENGTH + 1];
 
@@ -418,13 +514,13 @@ static struct subscription_persistence *subscription_persistence_create(struct a
        struct subscription_persistence *persistence = ast_sorcery_alloc(ast_sip_get_sorcery(),
                "subscription_persistence", NULL);
 
-       pjsip_dialog *dlg = sip_subscription_get_dlg(sub);
+       pjsip_dialog *dlg = sub_tree->dlg;
 
        if (!persistence) {
                return NULL;
        }
 
-       persistence->endpoint = ast_strdup(ast_sorcery_object_get_id(sub->endpoint));
+       persistence->endpoint = ast_strdup(ast_sorcery_object_get_id(sub_tree->endpoint));
        ast_copy_pj_str(tag, &dlg->local.info->tag, sizeof(tag));
        persistence->tag = ast_strdup(tag);
 
@@ -433,47 +529,49 @@ static struct subscription_persistence *subscription_persistence_create(struct a
 }
 
 /*! \brief Function which updates persistence information of a subscription in sorcery */
-static void subscription_persistence_update(struct ast_sip_subscription *sub,
+static void subscription_persistence_update(struct sip_subscription_tree *sub_tree,
        pjsip_rx_data *rdata)
 {
        pjsip_dialog *dlg;
 
-       if (!sub->persistence) {
+       if (!sub_tree->persistence) {
                return;
        }
 
-       dlg = sip_subscription_get_dlg(sub);
-       sub->persistence->cseq = dlg->local.cseq;
+       dlg = sub_tree->dlg;
+       sub_tree->persistence->cseq = dlg->local.cseq;
 
        if (rdata) {
                int expires;
                pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
 
                expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES;
-               sub->persistence->expires = ast_tvadd(ast_tvnow(), ast_samp2tv(expires, 1));
+               sub_tree->persistence->expires = ast_tvadd(ast_tvnow(), ast_samp2tv(expires, 1));
 
-               ast_copy_string(sub->persistence->packet, rdata->pkt_info.packet, sizeof(sub->persistence->packet));
-               ast_copy_string(sub->persistence->src_name, rdata->pkt_info.src_name, sizeof(sub->persistence->src_name));
-               sub->persistence->src_port = rdata->pkt_info.src_port;
-               ast_copy_string(sub->persistence->transport_key, rdata->tp_info.transport->type_name,
-                       sizeof(sub->persistence->transport_key));
-               ast_copy_pj_str(sub->persistence->local_name, &rdata->tp_info.transport->local_name.host,
-                       sizeof(sub->persistence->local_name));
-               sub->persistence->local_port = rdata->tp_info.transport->local_name.port;
+               ast_copy_string(sub_tree->persistence->packet, rdata->pkt_info.packet,
+                               sizeof(sub_tree->persistence->packet));
+               ast_copy_string(sub_tree->persistence->src_name, rdata->pkt_info.src_name,
+                               sizeof(sub_tree->persistence->src_name));
+               sub_tree->persistence->src_port = rdata->pkt_info.src_port;
+               ast_copy_string(sub_tree->persistence->transport_key, rdata->tp_info.transport->type_name,
+                       sizeof(sub_tree->persistence->transport_key));
+               ast_copy_pj_str(sub_tree->persistence->local_name, &rdata->tp_info.transport->local_name.host,
+                       sizeof(sub_tree->persistence->local_name));
+               sub_tree->persistence->local_port = rdata->tp_info.transport->local_name.port;
        }
 
-       ast_sorcery_update(ast_sip_get_sorcery(), sub->persistence);
+       ast_sorcery_update(ast_sip_get_sorcery(), sub_tree->persistence);
 }
 
 /*! \brief Function which removes persistence of a subscription from sorcery */
-static void subscription_persistence_remove(struct ast_sip_subscription *sub)
+static void subscription_persistence_remove(struct sip_subscription_tree *sub_tree)
 {
-       if (!sub->persistence) {
+       if (!sub_tree->persistence) {
                return;
        }
 
-       ast_sorcery_delete(ast_sip_get_sorcery(), sub->persistence);
-       ao2_ref(sub->persistence, -1);
+       ast_sorcery_delete(ast_sip_get_sorcery(), sub_tree->persistence);
+       ao2_ref(sub_tree->persistence, -1);
 }
 
 
@@ -503,23 +601,62 @@ static struct ast_sip_subscription_handler *subscription_get_handler_from_rdata(
        return handler;
 }
 
+/*!
+ * \brief Accept headers that are exceptions to the rule
+ *
+ * Typically, when a SUBSCRIBE arrives, we attempt to find a
+ * body generator that matches one of the Accept headers in
+ * the request. When subscribing to a single resource, this works
+ * great. However, when subscribing to a list, things work
+ * differently. Most Accept header values are fine, but there
+ * are a couple that are endemic to resource lists that need
+ * to be ignored when searching for a body generator to use
+ * for the individual resources of the subscription.
+ */
+const char *accept_exceptions[] =  {
+       "multipart/related",
+       "application/rlmi+xml",
+};
+
+/*!
+ * \brief Is the Accept header from the SUBSCRIBE in the list of exceptions?
+ *
+ * \retval 1 This Accept header value is an exception to the rule.
+ * \retval 0 This Accept header is not an exception to the rule.
+ */
+static int exceptional_accept(const pj_str_t *accept)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_LEN(accept_exceptions); ++i) {
+               if (!pj_strcmp2(accept, accept_exceptions[i])) {
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
 /*! \brief Retrieve a body generator using the Accept header of an rdata message */
 static struct ast_sip_pubsub_body_generator *subscription_get_generator_from_rdata(pjsip_rx_data *rdata,
        const struct ast_sip_subscription_handler *handler)
 {
-       pjsip_accept_hdr *accept_header;
+       pjsip_accept_hdr *accept_header = (pjsip_accept_hdr *) &rdata->msg_info.msg->hdr;
        char accept[AST_SIP_MAX_ACCEPT][64];
-       size_t num_accept_headers;
+       size_t num_accept_headers = 0;
 
-       accept_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, rdata->msg_info.msg->hdr.next);
-       if (accept_header) {
+       while ((accept_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, accept_header->next))) {
                int i;
 
                for (i = 0; i < accept_header->count; ++i) {
-                       ast_copy_pj_str(accept[i], &accept_header->values[i], sizeof(accept[i]));
+                       if (!exceptional_accept(&accept_header->values[i])) {
+                               ast_copy_pj_str(accept[num_accept_headers], &accept_header->values[i], sizeof(accept[num_accept_headers]));
+                               ++num_accept_headers;
+                       }
                }
-               num_accept_headers = accept_header->count;
-       } else {
+       }
+
+       if (num_accept_headers == 0) {
                /* If a SUBSCRIBE contains no Accept headers, then we must assume that
                 * the default accept type for the event package is to be used.
                 */
@@ -530,219 +667,291 @@ static struct ast_sip_pubsub_body_generator *subscription_get_generator_from_rda
        return find_body_generator(accept, num_accept_headers);
 }
 
-static struct ast_sip_subscription *notifier_create_subscription(const struct ast_sip_subscription_handler *handler,
-               struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *resource,
-               struct ast_sip_pubsub_body_generator *generator);
+struct resource_tree;
 
-/*! \brief Callback function to perform the actual recreation of a subscription */
-static int subscription_persistence_recreate(void *obj, void *arg, int flags)
+/*!
+ * \brief A node for a resource tree.
+ */
+struct tree_node {
+       AST_VECTOR(, struct tree_node *) children;
+       unsigned int full_state;
+       char resource[0];
+};
+
+/*!
+ * \brief Helper function for retrieving a resource list for a given event.
+ *
+ * This will retrieve a resource list that corresponds to the resource and event provided.
+ *
+ * \param resource The name of the resource list to retrieve
+ * \param event The expected event name on the resource list
+ */
+static struct resource_list *retrieve_resource_list(const char *resource, const char *event)
 {
-       struct subscription_persistence *persistence = obj;
-       pj_pool_t *pool = arg;
-       pjsip_rx_data rdata = { { 0, }, };
-       pjsip_expires_hdr *expires_header;
-       struct ast_sip_subscription_handler *handler;
-       RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
-       struct ast_sip_subscription *sub;
-       struct ast_sip_pubsub_body_generator *generator;
-       int resp;
-       char *resource;
-       size_t resource_size;
-       pjsip_sip_uri *request_uri;
+       struct resource_list *list;
 
-       /* If this subscription has already expired remove it */
-       if (ast_tvdiff_ms(persistence->expires, ast_tvnow()) <= 0) {
-               ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
-               return 0;
+       list = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "resource_list", resource);
+       if (!list) {
+               return NULL;
        }
 
-       endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", persistence->endpoint);
-       if (!endpoint) {
-               ast_log(LOG_WARNING, "A subscription for '%s' could not be recreated as the endpoint was not found\n",
-                       persistence->endpoint);
-               ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
-               return 0;
+       if (strcmp(list->event, event)) {
+               ast_log(LOG_WARNING, "Found resource list %s, but its event type (%s) does not match SUBSCRIBE's (%s)\n",
+                               resource, list->event, event);
+               ao2_cleanup(list);
+               return NULL;
        }
 
-       pj_pool_reset(pool);
-       rdata.tp_info.pool = pool;
-
-       if (ast_sip_create_rdata(&rdata, persistence->packet, persistence->src_name, persistence->src_port,
-               persistence->transport_key, persistence->local_name, persistence->local_port)) {
-               ast_log(LOG_WARNING, "A subscription for '%s' could not be recreated as the message could not be parsed\n",
-                       persistence->endpoint);
-               ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
-               return 0;
-       }
+       return list;
+}
 
-       request_uri = pjsip_uri_get_uri(rdata.msg_info.msg->line.req.uri);
-       resource_size = pj_strlen(&request_uri->user) + 1;
-       resource = alloca(resource_size);
-       ast_copy_pj_str(resource, &request_uri->user, resource_size);
+/*!
+ * \brief Allocate a tree node
+ *
+ * In addition to allocating and initializing the tree node, the node is also added
+ * to the vector of visited resources. See \ref build_resource_tree for more information
+ * on the visited resources.
+ *
+ * \param resource The name of the resource for this tree node.
+ * \param visited The vector of resources that have been visited.
+ * \param if allocating a list, indicate whether full state is requested in notifications.
+ * \retval NULL Allocation failure.
+ * \retval non-NULL The newly-allocated tree_node
+ */
+static struct tree_node *tree_node_alloc(const char *resource, struct resources *visited, unsigned int full_state)
+{
+       struct tree_node *node;
 
-       /* Update the expiration header with the new expiration */
-       expires_header = pjsip_msg_find_hdr(rdata.msg_info.msg, PJSIP_H_EXPIRES, rdata.msg_info.msg->hdr.next);
-       if (!expires_header) {
-               expires_header = pjsip_expires_hdr_create(pool, 0);
-               if (!expires_header) {
-                       ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
-                       return 0;
-               }
-               pjsip_msg_add_hdr(rdata.msg_info.msg, (pjsip_hdr*)expires_header);
+       node = ast_calloc(1, sizeof(*node) + strlen(resource) + 1);
+       if (!node) {
+               return NULL;
        }
-       expires_header->ivalue = (ast_tvdiff_ms(persistence->expires, ast_tvnow()) / 1000);
 
-       handler = subscription_get_handler_from_rdata(&rdata);
-       if (!handler || !handler->notifier) {
-               ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
-               return 0;
+       strcpy(node->resource, resource);
+       if (AST_VECTOR_INIT(&node->children, 4)) {
+               ast_free(node);
+               return NULL;
        }
+       node->full_state = full_state;
 
-       generator = subscription_get_generator_from_rdata(&rdata, handler);
-       if (!generator) {
-               ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
-               return 0;
+       if (visited) {
+               AST_VECTOR_APPEND(visited, resource);
        }
+       return node;
+}
 
-       ast_sip_mod_data_set(rdata.tp_info.pool, rdata.endpt_info.mod_data,
-                       pubsub_module.id, MOD_DATA_PERSISTENCE, persistence);
-
-       resp = handler->notifier->new_subscribe(endpoint, resource);
-       if (PJSIP_IS_STATUS_IN_CLASS(resp, 200)) {
-               sub = notifier_create_subscription(handler, endpoint, &rdata, resource, generator);
-               sub->persistence = ao2_bump(persistence);
-               subscription_persistence_update(sub, &rdata);
-       } else {
-               ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
+/*!
+ * \brief Destructor for a tree node
+ *
+ * This function calls recursively in order to destroy
+ * all nodes lower in the tree from the given node in
+ * addition to the node itself.
+ *
+ * \param node The node to destroy.
+ */
+static void tree_node_destroy(struct tree_node *node)
+{
+       int i;
+       if (!node) {
+               return;
        }
 
-       return 0;
+       for (i = 0; i < AST_VECTOR_SIZE(&node->children); ++i) {
+               tree_node_destroy(AST_VECTOR_GET(&node->children, i));
+       }
+       AST_VECTOR_FREE(&node->children);
+       ast_free(node);
 }
 
-/*! \brief Function which loads and recreates persisted subscriptions upon startup when the system is fully booted */
-static int subscription_persistence_load(void *data)
+/*!
+ * \brief Determine if this resource has been visited already
+ *
+ * See \ref build_resource_tree for more information
+ *
+ * \param resource The resource currently being visited
+ * \param visited The resources that have previously been visited
+ */
+static int have_visited(const char *resource, struct resources *visited)
 {
-       struct ao2_container *persisted_subscriptions = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(),
-               "subscription_persistence", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
-       pj_pool_t *pool;
+       int i;
 
-       pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "rtd%p", PJSIP_POOL_RDATA_LEN,
-               PJSIP_POOL_RDATA_INC);
-       if (!pool) {
-               ast_log(LOG_WARNING, "Could not create a memory pool for recreating SIP subscriptions\n");
-               return 0;
+       for (i = 0; i < AST_VECTOR_SIZE(visited); ++i) {
+               if (!strcmp(resource, AST_VECTOR_GET(visited, i))) {
+                       return 1;
+               }
        }
 
-       ao2_callback(persisted_subscriptions, OBJ_NODATA, subscription_persistence_recreate, pool);
-
-       pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
-
-       ao2_ref(persisted_subscriptions, -1);
        return 0;
 }
 
-/*! \brief Event callback which fires subscription persistence recreation when the system is fully booted */
-static void subscription_persistence_event_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
+/*!
+ * \brief Build child nodes for a given parent.
+ *
+ * This iterates through the items on a resource list and creates tree nodes for each one. The
+ * tree nodes created are children of the supplied parent node. If an item in the resource
+ * list is itself a list, then this function is called recursively to provide children for
+ * the the new node.
+ *
+ * If an item in a resource list is not a list, then the supplied subscription handler is
+ * called into as if a new SUBSCRIBE for the list item were presented. The handler's response
+ * is used to determine if the node can be added to the tree or not.
+ *
+ * If a parent node ends up having no child nodes added under it, then the parent node is
+ * pruned from the tree.
+ *
+ * \param endpoint The endpoint that sent the inbound SUBSCRIBE.
+ * \param handler The subscription handler for leaf nodes in the tree.
+ * \param list The configured resource list from which the child node is being built.
+ * \param parent The parent node for these children.
+ * \param visited The resources that have already been visited.
+ */
+static void build_node_children(struct ast_sip_endpoint *endpoint, const struct ast_sip_subscription_handler *handler,
+               struct resource_list *list, struct tree_node *parent, struct resources *visited)
 {
-       struct ast_json_payload *payload;
-       const char *type;
+       int i;
 
-       if (stasis_message_type(message) != ast_manager_get_generic_type()) {
-               return;
-       }
+       for (i = 0; i < AST_VECTOR_SIZE(&list->items); ++i) {
+               struct tree_node *current;
+               struct resource_list *child_list;
+               const char *resource = AST_VECTOR_GET(&list->items, i);
 
-       payload = stasis_message_data(message);
-       type = ast_json_string_get(ast_json_object_get(payload->json, "type"));
+               if (have_visited(resource, visited)) {
+                       ast_debug(1, "Already visited resource %s. Avoiding duplicate resource or potential loop.\n", resource);
+                       continue;
+               }
 
-       /* This subscription only responds to the FullyBooted event so that all modules have been loaded when we
-        * recreate SIP subscriptions.
-        */
-       if (strcmp(type, "FullyBooted")) {
-               return;
+               child_list = retrieve_resource_list(resource, list->event);
+               if (!child_list) {
+                       int resp = handler->notifier->new_subscribe(endpoint, resource);
+                       if (PJSIP_IS_STATUS_IN_CLASS(resp, 200)) {
+                               current = tree_node_alloc(resource, visited, 0);
+                               if (!current) {
+                                       ast_debug(1, "Subscription to leaf resource %s was successful, but encountered"
+                                                       "allocation error afterwards\n", resource);
+                                       continue;
+                               }
+                               ast_debug(1, "Subscription to leaf resource %s resulted in success. Adding to parent %s\n",
+                                               resource, parent->resource);
+                               AST_VECTOR_APPEND(&parent->children, current);
+                       } else {
+                               ast_debug(1, "Subscription to leaf resource %s resulted in error response %d\n",
+                                               resource, resp);
+                       }
+               } else {
+                       ast_debug(1, "Resource %s (child of %s) is a list\n", resource, parent->resource);
+                       current = tree_node_alloc(resource, visited, child_list->full_state);
+                       if (!current) {
+                               ast_debug(1, "Cannot build children of resource %s due to allocation failure\n", resource);
+                               continue;
+                       }
+                       build_node_children(endpoint, handler, child_list, current, visited);
+                       if (AST_VECTOR_SIZE(&current->children) > 0) {
+                               ast_debug(1, "List %s had no successful children.\n", resource);
+                               AST_VECTOR_APPEND(&parent->children, current);
+                       } else {
+                               ast_debug(1, "List %s had successful children. Adding to parent %s\n",
+                                               resource, parent->resource);
+                               tree_node_destroy(current);
+                       }
+                       ao2_cleanup(child_list);
+               }
        }
-
-       /* This has to be here so the subscription is recreated when the body generator is available */
-       ast_sip_push_task(NULL, subscription_persistence_load, NULL);
-
-       /* Once the system is fully booted we don't care anymore */
-       stasis_unsubscribe(sub);
 }
 
-static void add_subscription(struct ast_sip_subscription *obj)
-{
-       SCOPED_LOCK(lock, &subscriptions, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
-       AST_RWLIST_INSERT_TAIL(&subscriptions, obj, next);
-       ast_module_ref(ast_module_info->self);
-}
-
-static void remove_subscription(struct ast_sip_subscription *obj)
-{
-       struct ast_sip_subscription *i;
-       SCOPED_LOCK(lock, &subscriptions, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
-       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&subscriptions, i, next) {
-               if (i == obj) {
-                       AST_RWLIST_REMOVE_CURRENT(next);
-                       ast_module_unref(ast_module_info->self);
-                       break;
-               }
-       }
-       AST_RWLIST_TRAVERSE_SAFE_END;
-}
-
-typedef int (*on_subscription_t)(struct ast_sip_subscription *sub, void *arg);
+/*!
+ * \brief A resource tree
+ *
+ * When an inbound SUBSCRIBE arrives, the resource being subscribed to may
+ * be a resource list. If this is the case, the resource list may contain resources
+ * that are themselves lists. The structure needed to hold the resources is
+ * a tree.
+ *
+ * Upon receipt of the SUBSCRIBE, the tree is built by determining if subscriptions
+ * to the individual resources in the tree would be successful or not. Any successful
+ * subscriptions result in a node in the tree being created. Any unsuccessful subscriptions
+ * result in no node being created.
+ *
+ * This tree can be seen as a bare-bones analog of the tree of ast_sip_subscriptions that
+ * will end up being created to actually carry out the duties of a SIP SUBSCRIBE dialog.
+ */
+struct resource_tree {
+       struct tree_node *root;
+       unsigned int notification_batch_interval;
+};
 
-static int for_each_subscription(on_subscription_t on_subscription, void *arg)
+/*!
+ * \brief Destroy a resource tree.
+ *
+ * This function makes no assumptions about how the tree itself was
+ * allocated and does not attempt to free the tree itself. Callers
+ * of this function are responsible for freeing the tree.
+ *
+ * \param tree The tree to destroy.
+ */
+static void resource_tree_destroy(struct resource_tree *tree)
 {
-       int num = 0;
-       struct ast_sip_subscription *i;
-       SCOPED_LOCK(lock, &subscriptions, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
-
-       if (!on_subscription) {
-               return num;
+       if (tree) {
+               tree_node_destroy(tree->root);
        }
+}
 
-       AST_RWLIST_TRAVERSE(&subscriptions, i, next) {
-               if (on_subscription(i, arg)) {
-                       break;
+/*!
+ * \brief Build a resource tree
+ *
+ * This function builds a resource tree based on the requested resource in a SUBSCRIBE request.
+ *
+ * This function also creates a container that has all resources that have been visited during
+ * creation of the tree, whether those resources resulted in a tree node being created or not.
+ * Keeping this container of visited resources allows for misconfigurations such as loops in
+ * the tree or duplicated resources to be detected.
+ *
+ * \param endpoint The endpoint that sent the SUBSCRIBE request.
+ * \param handler The subscription handler for leaf nodes in the tree.
+ * \param resource The resource requested in the SUBSCRIBE request.
+ * \param tree The tree that is to be built.
+ *
+ * \retval 200-299 Successfully subscribed to at least one resource.
+ * \retval 300-699 Failure to subscribe to requested resource.
+ */
+static int build_resource_tree(struct ast_sip_endpoint *endpoint, const struct ast_sip_subscription_handler *handler,
+               const char *resource, struct resource_tree *tree)
+{
+       struct resource_list *list;
+       struct resources visited;
+
+       list = retrieve_resource_list(resource, handler->event_name);
+       if (!list) {
+               ast_debug(1, "Subscription to resource %s is not to a list\n", resource);
+               tree->root = tree_node_alloc(resource, NULL, 0);
+               if (!tree->root) {
+                       return 500;
                }
-               ++num;
+               return handler->notifier->new_subscribe(endpoint, resource);
        }
-       return num;
-}
-
-static void sip_subscription_to_ami(struct ast_sip_subscription *sub,
-                                   struct ast_str **buf)
-{
-       char str[256];
-       struct ast_sip_endpoint_id_configuration *id = &sub->endpoint->id;
-
-       ast_str_append(buf, 0, "Role: %s\r\n",
-                      sip_subscription_roles_map[sub->role]);
-       ast_str_append(buf, 0, "Endpoint: %s\r\n",
-                      ast_sorcery_object_get_id(sub->endpoint));
 
-       ast_copy_pj_str(str, &sip_subscription_get_dlg(sub)->call_id->id, sizeof(str));
-       ast_str_append(buf, 0, "Callid: %s\r\n", str);
+       ast_debug(1, "Subscription to resource %s is a list\n", resource);
+       if (AST_VECTOR_INIT(&visited, AST_VECTOR_SIZE(&list->items))) {
+               return 500;
+       }
 
-       ast_str_append(buf, 0, "State: %s\r\n", pjsip_evsub_get_state_name(
-                              sip_subscription_get_evsub(sub)));
+       tree->root = tree_node_alloc(resource, &visited, list->full_state);
+       if (!tree->root) {
+               return 500;
+       }
 
-       ast_callerid_merge(str, sizeof(str),
-                          S_COR(id->self.name.valid, id->self.name.str, NULL),
-                          S_COR(id->self.number.valid, id->self.number.str, NULL),
-                          "Unknown");
+       tree->notification_batch_interval = list->notification_batch_interval;
 
-       ast_str_append(buf, 0, "Callerid: %s\r\n", str);
+       build_node_children(endpoint, handler, list, tree->root, &visited);
+       AST_VECTOR_FREE(&visited);
+       ao2_cleanup(list);
 
-       if (sub->handler->to_ami) {
-               sub->handler->to_ami(sub, buf);
+       if (AST_VECTOR_SIZE(&tree->root->children) > 0) {
+               return 200;
+       } else {
+               return 500;
        }
 }
 
-#define DATASTORE_BUCKETS 53
-
-#define DEFAULT_EXPIRES 3600
-
 static int datastore_hash(const void *obj, int flags)
 {
        const struct ast_datastore *datastore = obj;
@@ -767,7 +976,7 @@ static int datastore_cmp(void *obj, void *arg, int flags)
 
 static int subscription_remove_serializer(void *obj)
 {
-       struct ast_sip_subscription *sub = obj;
+       struct sip_subscription_tree *sub_tree = obj;
 
        /* This is why we keep the dialog on the subscription. When the subscription
         * is destroyed, there is no guarantee that the underlying dialog is ready
@@ -778,53 +987,48 @@ static int subscription_remove_serializer(void *obj)
         * subscription is destroyed so that we can guarantee that our attempt to
         * remove the serializer will be successful.
         */
-       ast_sip_dialog_set_serializer(sip_subscription_get_dlg(sub), NULL);
-       pjsip_dlg_dec_session(sip_subscription_get_dlg(sub), &pubsub_module);
+       ast_sip_dialog_set_serializer(sub_tree->dlg, NULL);
+       pjsip_dlg_dec_session(sub_tree->dlg, &pubsub_module);
 
        return 0;
 }
 
-static void subscription_destructor(void *obj)
+static void add_subscription(struct sip_subscription_tree *obj)
 {
-       struct ast_sip_subscription *sub = obj;
-
-       ast_debug(3, "Destroying SIP subscription\n");
-
-       subscription_persistence_remove(sub);
-
-       remove_subscription(sub);
-
-       ao2_cleanup(sub->datastores);
-       ao2_cleanup(sub->endpoint);
+       SCOPED_LOCK(lock, &subscriptions, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
+       AST_RWLIST_INSERT_TAIL(&subscriptions, obj, next);
+}
 
-       if (sip_subscription_get_dlg(sub)) {
-               ast_sip_push_task_synchronous(NULL, subscription_remove_serializer, sub);
+static void remove_subscription(struct sip_subscription_tree *obj)
+{
+       struct sip_subscription_tree *i;
+       SCOPED_LOCK(lock, &subscriptions, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
+       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&subscriptions, i, next) {
+               if (i == obj) {
+                       AST_RWLIST_REMOVE_CURRENT(next);
+                       ast_debug(1, "Removing subscription to resource %s from list of subscriptions\n",
+                                       ast_sip_subscription_get_resource_name(i->root));
+                       break;
+               }
        }
-       ast_taskprocessor_unreference(sub->serializer);
+       AST_RWLIST_TRAVERSE_SAFE_END;
 }
 
+static void subscription_destructor(void *obj)
+{
+       struct ast_sip_subscription *sub = obj;
 
-static void pubsub_on_evsub_state(pjsip_evsub *sub, pjsip_event *event);
-static void pubsub_on_rx_refresh(pjsip_evsub *sub, pjsip_rx_data *rdata,
-               int *p_st_code, pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body);
-static void pubsub_on_rx_notify(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code,
-               pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body);
-static void pubsub_on_client_refresh(pjsip_evsub *sub);
-static void pubsub_on_server_timeout(pjsip_evsub *sub);
-
+       ast_debug(3, "Destroying SIP subscription to resource %s\n", sub->resource);
+       ast_free(sub->body_text);
 
-static pjsip_evsub_user pubsub_cb = {
-       .on_evsub_state = pubsub_on_evsub_state,
-       .on_rx_refresh = pubsub_on_rx_refresh,
-       .on_rx_notify = pubsub_on_rx_notify,
-       .on_client_refresh = pubsub_on_client_refresh,
-       .on_server_timeout = pubsub_on_server_timeout,
-};
+       ao2_cleanup(sub->datastores);
+}
 
 static struct ast_sip_subscription *allocate_subscription(const struct ast_sip_subscription_handler *handler,
-               struct ast_sip_endpoint *endpoint, const char *resource, enum ast_sip_subscription_role role)
+               const char *resource, struct sip_subscription_tree *tree)
 {
        struct ast_sip_subscription *sub;
+       pjsip_sip_uri *contact_uri;
 
        sub = ao2_alloc(sizeof(*sub) + strlen(resource) + 1, subscription_destructor);
        if (!sub) {
@@ -837,1303 +1041,2895 @@ static struct ast_sip_subscription *allocate_subscription(const struct ast_sip_s
                ao2_ref(sub, -1);
                return NULL;
        }
-       sub->serializer = ast_sip_create_serializer();
-       if (!sub->serializer) {
+
+       sub->body_text = ast_str_create(128);
+       if (!sub->body_text) {
                ao2_ref(sub, -1);
                return NULL;
        }
-       sub->role = role;
-       sub->type = SIP_SUBSCRIPTION_REAL;
-       sub->endpoint = ao2_bump(endpoint);
+
+       sub->uri = pjsip_sip_uri_create(tree->dlg->pool, PJ_FALSE);
+       contact_uri = pjsip_uri_get_uri(tree->dlg->local.contact->uri);
+       pjsip_sip_uri_assign(tree->dlg->pool, sub->uri, contact_uri);
+       pj_strdup2(tree->dlg->pool, &sub->uri->user, resource);
+
        sub->handler = handler;
+       sub->subscription_state = PJSIP_EVSUB_STATE_ACTIVE;
+       sub->tree = tree;
 
        return sub;
 }
 
-static void subscription_setup_dialog(struct ast_sip_subscription *sub, pjsip_dialog *dlg)
-{
-       /* We keep a reference to the dialog until our subscription is destroyed. See
-        * the subscription_destructor for more details
-        */
-       pjsip_dlg_inc_session(dlg, &pubsub_module);
-       sub->reality.real.dlg = dlg;
-       ast_sip_dialog_set_serializer(dlg, sub->serializer);
-       pjsip_evsub_set_mod_data(sip_subscription_get_evsub(sub), pubsub_module.id, sub);
-}
-
-static struct ast_sip_subscription *notifier_create_subscription(const struct ast_sip_subscription_handler *handler,
-               struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *resource,
-               struct ast_sip_pubsub_body_generator *generator)
+/*!
+ * \brief Create a tree of virtual subscriptions based on a resource tree node.
+ *
+ * \param handler The handler to supply to leaf subscriptions.
+ * \param resource The requested resource for this subscription.
+ * \param generator Body generator to use for leaf subscriptions.
+ * \param tree The root of the subscription tree.
+ * \param current The tree node that corresponds to the subscription being created.
+ */
+static struct ast_sip_subscription *create_virtual_subscriptions(const struct ast_sip_subscription_handler *handler,
+               const char *resource, struct ast_sip_pubsub_body_generator *generator,
+               struct sip_subscription_tree *tree, struct tree_node *current)
 {
+       int i;
        struct ast_sip_subscription *sub;
-       pjsip_dialog *dlg;
-       struct subscription_persistence *persistence;
 
-       sub = allocate_subscription(handler, endpoint, resource, AST_SIP_NOTIFIER);
+       sub = allocate_subscription(handler, resource, tree);
        if (!sub) {
                return NULL;
        }
 
+       sub->full_state = current->full_state;
        sub->body_generator = generator;
-       dlg = ast_sip_create_dialog_uas(endpoint, rdata);
-       if (!dlg) {
-               ast_log(LOG_WARNING, "Unable to create dialog for SIP subscription\n");
-               ao2_ref(sub, -1);
-               return NULL;
-       }
 
-       persistence = ast_sip_mod_data_get(rdata->endpt_info.mod_data,
-                       pubsub_module.id, MOD_DATA_PERSISTENCE);
-       if (persistence) {
-               /* Update the created dialog with the persisted information */
-               pjsip_ua_unregister_dlg(pjsip_ua_instance(), dlg);
-               pj_strdup2(dlg->pool, &dlg->local.info->tag, persistence->tag);
-               dlg->local.tag_hval = pj_hash_calc_tolower(0, NULL, &dlg->local.info->tag);
-               pjsip_ua_register_dlg(pjsip_ua_instance(), dlg);
-               dlg->local.cseq = persistence->cseq;
-               dlg->remote.cseq = persistence->cseq;
-       }
+       for (i = 0; i < AST_VECTOR_SIZE(&current->children); ++i) {
+               struct ast_sip_subscription *child;
+               struct tree_node *child_node = AST_VECTOR_GET(&current->children, i);
 
-       pjsip_evsub_create_uas(dlg, &pubsub_cb, rdata, 0, &sub->reality.real.evsub);
-       subscription_setup_dialog(sub, dlg);
+               child = create_virtual_subscriptions(handler, child_node->resource, generator,
+                               tree, child_node);
 
-       ast_sip_mod_data_set(dlg->pool, dlg->mod_data, pubsub_module.id, MOD_DATA_MSG,
-                       pjsip_msg_clone(dlg->pool, rdata->msg_info.msg));
+               if (!child) {
+                       ast_debug(1, "Child subscription to resource %s could not be created\n",
+                                       child_node->resource);
+                       continue;
+               }
+
+               if (AST_VECTOR_APPEND(&sub->children, child)) {
+                       ast_debug(1, "Child subscription to resource %s could not be appended\n",
+                                       child_node->resource);
+               }
+       }
 
-       add_subscription(sub);
        return sub;
 }
 
-void *ast_sip_subscription_get_header(const struct ast_sip_subscription *sub, const char *header)
+static void shutdown_subscriptions(struct ast_sip_subscription *sub)
 {
-       pjsip_dialog *dlg = sip_subscription_get_dlg(sub);
-       pjsip_msg *msg = ast_sip_mod_data_get(dlg->mod_data, pubsub_module.id, MOD_DATA_MSG);
-       pj_str_t name;
+       int i;
 
-       pj_cstr(&name, header);
+       if (AST_VECTOR_SIZE(&sub->children) > 0) {
+               for (i = 0; i < AST_VECTOR_SIZE(&sub->children); ++i) {
+                       shutdown_subscriptions(AST_VECTOR_GET(&sub->children, i));
+                       ao2_cleanup(AST_VECTOR_GET(&sub->children, i));
+               }
+               return;
+       }
 
-       return pjsip_msg_find_hdr_by_name(msg, &name, NULL);
+       if (sub->handler->subscription_shutdown) {
+               sub->handler->subscription_shutdown(sub);
+       }
 }
 
-struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler,
-               struct ast_sip_endpoint *endpoint, const char *resource)
+static void subscription_tree_destructor(void *obj)
 {
-       struct ast_sip_subscription *sub = ao2_alloc(sizeof(*sub) + strlen(resource) + 1, subscription_destructor);
-       pjsip_dialog *dlg;
-       struct ast_sip_contact *contact;
-       pj_str_t event;
-       pjsip_tx_data *tdata;
-       pjsip_evsub *evsub;
+       struct sip_subscription_tree *sub_tree = obj;
 
-       sub = allocate_subscription(handler, endpoint, resource, AST_SIP_SUBSCRIBER);
-       if (!sub) {
-               return NULL;
-       }
+       remove_subscription(sub_tree);
 
-       contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
-       if (!contact || ast_strlen_zero(contact->uri)) {
-               ast_log(LOG_WARNING, "No contacts configured for endpoint %s. Unable to create SIP subsription\n",
-                               ast_sorcery_object_get_id(endpoint));
-               ao2_ref(sub, -1);
-               ao2_cleanup(contact);
-               return NULL;
-       }
+       subscription_persistence_remove(sub_tree);
+       ao2_cleanup(sub_tree->endpoint);
 
-       dlg = ast_sip_create_dialog_uac(endpoint, contact->uri, NULL);
-       ao2_cleanup(contact);
-       if (!dlg) {
-               ast_log(LOG_WARNING, "Unable to create dialog for SIP subscription\n");
-               ao2_ref(sub, -1);
-               return NULL;
+       if (sub_tree->dlg) {
+               ast_sip_push_task_synchronous(NULL, subscription_remove_serializer, sub_tree);
        }
 
-       pj_cstr(&event, handler->event_name);
-       pjsip_evsub_create_uac(dlg, &pubsub_cb, &event, 0, &sub->reality.real.evsub);
-       subscription_setup_dialog(sub, dlg);
-
-       add_subscription(sub);
-
-       evsub = sip_subscription_get_evsub(sub);
-
-       if (pjsip_evsub_initiate(evsub, NULL, -1, &tdata) == PJ_SUCCESS) {
-               pjsip_evsub_send_request(evsub, tdata);
-       } else {
-               /* pjsip_evsub_terminate will result in pubsub_on_evsub_state,
-                * being called and terminating the subscription. Therefore, we don't
-                * need to decrease the reference count of sub here.
-                */
-               pjsip_evsub_terminate(evsub, PJ_TRUE);
-               return NULL;
-       }
+       shutdown_subscriptions(sub_tree->root);
+       ao2_cleanup(sub_tree->root);
 
-       return sub;
+       ast_taskprocessor_unreference(sub_tree->serializer);
+       ast_module_unref(ast_module_info->self);
 }
 
-struct ast_sip_endpoint *ast_sip_subscription_get_endpoint(struct ast_sip_subscription *sub)
+static void subscription_setup_dialog(struct sip_subscription_tree *sub_tree, pjsip_dialog *dlg)
 {
-       ast_assert(sub->endpoint != NULL);
-       ao2_ref(sub->endpoint, +1);
-       return sub->endpoint;
+       /* We keep a reference to the dialog until our subscription is destroyed. See
+        * the subscription_destructor for more details
+        */
+       pjsip_dlg_inc_session(dlg, &pubsub_module);
+       sub_tree->dlg = dlg;
+       ast_sip_dialog_set_serializer(dlg, sub_tree->serializer);
+       pjsip_evsub_set_mod_data(sub_tree->evsub, pubsub_module.id, sub_tree);
 }
 
-struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_subscription *sub)
+static struct sip_subscription_tree *allocate_subscription_tree(struct ast_sip_endpoint *endpoint)
 {
-       ast_assert(sub->serializer != NULL);
-       return sub->serializer;
-}
+       struct sip_subscription_tree *sub_tree;
 
-static int sip_subscription_send_request(struct ast_sip_subscription *sub, pjsip_tx_data *tdata)
-{
-       struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sub);
-       int res;
+       sub_tree = ao2_alloc(sizeof *sub_tree, subscription_tree_destructor);
+       if (!sub_tree) {
+               return NULL;
+       }
 
-       ao2_ref(sub, +1);
-       res = pjsip_evsub_send_request(sip_subscription_get_evsub(sub),
-                       tdata) == PJ_SUCCESS ? 0 : -1;
+       ast_module_ref(ast_module_info->self);
 
-       subscription_persistence_update(sub, NULL);
+       sub_tree->serializer = ast_sip_create_serializer();
+       if (!sub_tree->serializer) {
+               ao2_ref(sub_tree, -1);
+               return NULL;
+       }
 
-       ast_test_suite_event_notify("SUBSCRIPTION_STATE_SET",
-               "StateText: %s\r\n"
-               "Endpoint: %s\r\n",
-               pjsip_evsub_get_state_name(sip_subscription_get_evsub(sub)),
-               ast_sorcery_object_get_id(endpoint));
-       ao2_cleanup(sub);
-       ao2_cleanup(endpoint);
+       sub_tree->endpoint = ao2_bump(endpoint);
+       sub_tree->notify_sched_id = -1;
 
-       return res;
+       add_subscription(sub_tree);
+       return sub_tree;
 }
 
-int ast_sip_subscription_notify(struct ast_sip_subscription *sub, void *notify_data,
-               int terminate)
+/*!
+ * \brief Create a subscription tree based on a resource tree.
+ *
+ * Using the previously-determined valid resources in the provided resource tree,
+ * a corresponding tree of ast_sip_subscriptions are created. The root of the
+ * subscription tree is a real subscription, and the rest in the tree are
+ * virtual subscriptions.
+ *
+ * \param handler The handler to use for leaf subscriptions
+ * \param endpoint The endpoint that sent the SUBSCRIBE request
+ * \param rdata The SUBSCRIBE content
+ * \param resource The requested resource in the SUBSCRIBE request
+ * \param generator The body generator to use in leaf subscriptions
+ * \param tree The resource tree on which the subscription tree is based
+ *
+ * \retval NULL Could not create the subscription tree
+ * \retval non-NULL The root of the created subscription tree
+ */
+
+static struct sip_subscription_tree *create_subscription_tree(const struct ast_sip_subscription_handler *handler,
+               struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *resource,
+               struct ast_sip_pubsub_body_generator *generator, struct resource_tree *tree)
 {
-       struct ast_sip_body body = {
-               .type = ast_sip_subscription_get_body_type(sub),
-               .subtype = ast_sip_subscription_get_body_subtype(sub),
-       };
-       struct ast_str *body_text = ast_str_create(64);
-       pjsip_evsub *evsub = sip_subscription_get_evsub(sub);
-       pjsip_tx_data *tdata;
-       pjsip_evsub_state state;
+       struct sip_subscription_tree *sub_tree;
+       pjsip_dialog *dlg;
+       struct subscription_persistence *persistence;
 
-       if (!body_text) {
-               return -1;
+       sub_tree = allocate_subscription_tree(endpoint);
+       if (!sub_tree) {
+               return NULL;
        }
 
-       if (ast_sip_pubsub_generate_body_content(body.type, body.subtype, notify_data, &body_text)) {
-               ast_free(body_text);
-               return -1;
+       dlg = ast_sip_create_dialog_uas(endpoint, rdata);
+       if (!dlg) {
+               ast_log(LOG_WARNING, "Unable to create dialog for SIP subscription\n");
+               ao2_ref(sub_tree, -1);
+               return NULL;
        }
 
-       body.body_text = ast_str_buffer(body_text);
-
-       if (terminate) {
-               state = PJSIP_EVSUB_STATE_TERMINATED;
-       } else {
-               state = pjsip_evsub_get_state(evsub) <= PJSIP_EVSUB_STATE_ACTIVE ?
-                       PJSIP_EVSUB_STATE_ACTIVE : PJSIP_EVSUB_STATE_TERMINATED;
+       persistence = ast_sip_mod_data_get(rdata->endpt_info.mod_data,
+                       pubsub_module.id, MOD_DATA_PERSISTENCE);
+       if (persistence) {
+               /* Update the created dialog with the persisted information */
+               pjsip_ua_unregister_dlg(pjsip_ua_instance(), dlg);
+               pj_strdup2(dlg->pool, &dlg->local.info->tag, persistence->tag);
+               dlg->local.tag_hval = pj_hash_calc_tolower(0, NULL, &dlg->local.info->tag);
+               pjsip_ua_register_dlg(pjsip_ua_instance(), dlg);
+               dlg->local.cseq = persistence->cseq;
+               dlg->remote.cseq = persistence->cseq;
        }
 
-       if (pjsip_evsub_notify(evsub, state, NULL, NULL, &tdata) != PJ_SUCCESS) {
-               ast_free(body_text);
-               return -1;
-       }
-       if (ast_sip_add_body(tdata, &body)) {
-               ast_free(body_text);
-               pjsip_tx_data_dec_ref(tdata);
-               return -1;
-       }
-       if (sip_subscription_send_request(sub, tdata)) {
-               ast_free(body_text);
-               pjsip_tx_data_dec_ref(tdata);
-               return -1;
+       pjsip_evsub_create_uas(dlg, &pubsub_cb, rdata, 0, &sub_tree->evsub);
+       subscription_setup_dialog(sub_tree, dlg);
+
+       ast_sip_mod_data_set(dlg->pool, dlg->mod_data, pubsub_module.id, MOD_DATA_MSG,
+                       pjsip_msg_clone(dlg->pool, rdata->msg_info.msg));
+
+       sub_tree->notification_batch_interval = tree->notification_batch_interval;
+
+       sub_tree->root = create_virtual_subscriptions(handler, resource, generator, sub_tree, tree->root);
+       if (AST_VECTOR_SIZE(&sub_tree->root->children) > 0) {
+               sub_tree->is_list = 1;
        }
 
-       return 0;
+       return sub_tree;
 }
 
-void ast_sip_subscription_get_local_uri(struct ast_sip_subscription *sub, char *buf, size_t size)
+/*! \brief Callback function to perform the actual recreation of a subscription */
+static int subscription_persistence_recreate(void *obj, void *arg, int flags)
 {
-       pjsip_dialog *dlg = sip_subscription_get_dlg(sub);
-       ast_copy_pj_str(buf, &dlg->local.info_str, size);
+       struct subscription_persistence *persistence = obj;
+       pj_pool_t *pool = arg;
+       pjsip_rx_data rdata = { { 0, }, };
+       RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
+       struct sip_subscription_tree *sub_tree;
+       struct ast_sip_pubsub_body_generator *generator;
+       int resp;
+       char *resource;
+       size_t resource_size;
+       pjsip_sip_uri *request_uri;
+       struct resource_tree tree;
+       pjsip_expires_hdr *expires_header;
+       struct ast_sip_subscription_handler *handler;
+
+       /* If this subscription has already expired remove it */
+       if (ast_tvdiff_ms(persistence->expires, ast_tvnow()) <= 0) {
+               ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
+               return 0;
+       }
+
+       endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", persistence->endpoint);
+       if (!endpoint) {
+               ast_log(LOG_WARNING, "A subscription for '%s' could not be recreated as the endpoint was not found\n",
+                       persistence->endpoint);
+               ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
+               return 0;
+       }
+
+       pj_pool_reset(pool);
+       rdata.tp_info.pool = pool;
+
+       if (ast_sip_create_rdata(&rdata, persistence->packet, persistence->src_name, persistence->src_port,
+               persistence->transport_key, persistence->local_name, persistence->local_port)) {
+               ast_log(LOG_WARNING, "A subscription for '%s' could not be recreated as the message could not be parsed\n",
+                       persistence->endpoint);
+               ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
+               return 0;
+       }
+
+       request_uri = pjsip_uri_get_uri(rdata.msg_info.msg->line.req.uri);
+       resource_size = pj_strlen(&request_uri->user) + 1;
+       resource = alloca(resource_size);
+       ast_copy_pj_str(resource, &request_uri->user, resource_size);
+
+       /* Update the expiration header with the new expiration */
+       expires_header = pjsip_msg_find_hdr(rdata.msg_info.msg, PJSIP_H_EXPIRES, rdata.msg_info.msg->hdr.next);
+       if (!expires_header) {
+               expires_header = pjsip_expires_hdr_create(pool, 0);
+               if (!expires_header) {
+                       ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
+                       return 0;
+               }
+               pjsip_msg_add_hdr(rdata.msg_info.msg, (pjsip_hdr*)expires_header);
+       }
+       expires_header->ivalue = (ast_tvdiff_ms(persistence->expires, ast_tvnow()) / 1000);
+
+       handler = subscription_get_handler_from_rdata(&rdata);
+       if (!handler || !handler->notifier) {
+               ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
+               return 0;
+       }
+
+       generator = subscription_get_generator_from_rdata(&rdata, handler);
+       if (!generator) {
+               ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
+               return 0;
+       }
+
+       ast_sip_mod_data_set(rdata.tp_info.pool, rdata.endpt_info.mod_data,
+                       pubsub_module.id, MOD_DATA_PERSISTENCE, persistence);
+
+       memset(&tree, 0, sizeof(tree));
+       resp = build_resource_tree(endpoint, handler, resource, &tree);
+       if (PJSIP_IS_STATUS_IN_CLASS(resp, 200)) {
+               sub_tree = create_subscription_tree(handler, endpoint, &rdata, resource, generator, &tree);
+               sub_tree->persistence = ao2_bump(persistence);
+               subscription_persistence_update(sub_tree, &rdata);
+       } else {
+               ast_sorcery_delete(ast_sip_get_sorcery(), persistence);
+       }
+       resource_tree_destroy(&tree);
+
+       return 0;
 }
 
-void ast_sip_subscription_get_remote_uri(struct ast_sip_subscription *sub, char *buf, size_t size)
+/*! \brief Function which loads and recreates persisted subscriptions upon startup when the system is fully booted */
+static int subscription_persistence_load(void *data)
 {
-       pjsip_dialog *dlg = sip_subscription_get_dlg(sub);
-       ast_copy_pj_str(buf, &dlg->remote.info_str, size);
+       struct ao2_container *persisted_subscriptions = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(),
+               "subscription_persistence", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
+       pj_pool_t *pool;
+
+       pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "rtd%p", PJSIP_POOL_RDATA_LEN,
+               PJSIP_POOL_RDATA_INC);
+       if (!pool) {
+               ast_log(LOG_WARNING, "Could not create a memory pool for recreating SIP subscriptions\n");
+               return 0;
+       }
+
+       ao2_callback(persisted_subscriptions, OBJ_NODATA, subscription_persistence_recreate, pool);
+
+       pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
+
+       ao2_ref(persisted_subscriptions, -1);
+       return 0;
 }
 
-const char *ast_sip_subscription_get_resource_name(struct ast_sip_subscription *sub)
+/*! \brief Event callback which fires subscription persistence recreation when the system is fully booted */
+static void subscription_persistence_event_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
 {
-       return sub->resource;
+       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"));
+
+       /* This subscription only responds to the FullyBooted event so that all modules have been loaded when we
+        * recreate SIP subscriptions.
+        */
+       if (strcmp(type, "FullyBooted")) {
+               return;
+       }
+
+       /* This has to be here so the subscription is recreated when the body generator is available */
+       ast_sip_push_task(NULL, subscription_persistence_load, NULL);
+
+       /* Once the system is fully booted we don't care anymore */
+       stasis_unsubscribe(sub);
 }
 
-static int sip_subscription_accept(struct ast_sip_subscription *sub, pjsip_rx_data *rdata, int response)
+typedef int (*on_subscription_t)(struct sip_subscription_tree *sub, void *arg);
+
+static int for_each_subscription(on_subscription_t on_subscription, void *arg)
 {
-       /* If this is a persistence recreation the subscription has already been accepted */
-       if (ast_sip_mod_data_get(rdata->endpt_info.mod_data, pubsub_module.id, MOD_DATA_PERSISTENCE)) {
-               return 0;
+       int num = 0;
+       struct sip_subscription_tree *i;
+       SCOPED_LOCK(lock, &subscriptions, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
+
+       if (!on_subscription) {
+               return num;
        }
 
-       return pjsip_evsub_accept(sip_subscription_get_evsub(sub), rdata, response, NULL) == PJ_SUCCESS ? 0 : -1;
+       AST_RWLIST_TRAVERSE(&subscriptions, i, next) {
+               if (on_subscription(i, arg)) {
+                       break;
+               }
+               ++num;
+       }
+       return num;
 }
 
-static void subscription_datastore_destroy(void *obj)
+static void sip_subscription_to_ami(struct sip_subscription_tree *sub_tree,
+                                   struct ast_str **buf)
 {
-       struct ast_datastore *datastore = obj;
+       char str[256];
+       struct ast_sip_endpoint_id_configuration *id = &sub_tree->endpoint->id;
 
-       /* Using the destroy function (if present) destroy the data */
-       if (datastore->info->destroy != NULL && datastore->data != NULL) {
-               datastore->info->destroy(datastore->data);
-               datastore->data = NULL;
+       ast_str_append(buf, 0, "Role: %s\r\n",
+                      sip_subscription_roles_map[sub_tree->role]);
+       ast_str_append(buf, 0, "Endpoint: %s\r\n",
+                      ast_sorcery_object_get_id(sub_tree->endpoint));
+
+       ast_copy_pj_str(str, &sub_tree->dlg->call_id->id, sizeof(str));
+       ast_str_append(buf, 0, "Callid: %s\r\n", str);
+
+       ast_str_append(buf, 0, "State: %s\r\n", pjsip_evsub_get_state_name(sub_tree->evsub));
+
+       ast_callerid_merge(str, sizeof(str),
+                          S_COR(id->self.name.valid, id->self.name.str, NULL),
+                          S_COR(id->self.number.valid, id->self.number.str, NULL),
+                          "Unknown");
+
+       ast_str_append(buf, 0, "Callerid: %s\r\n", str);
+
+       /* XXX This needs to be done recursively for lists */
+       if (sub_tree->root->handler->to_ami) {
+               sub_tree->root->handler->to_ami(sub_tree->root, buf);
        }
+}
 
-       ast_free((void *) datastore->uid);
-       datastore->uid = NULL;
+
+void *ast_sip_subscription_get_header(const struct ast_sip_subscription *sub, const char *header)
+{
+       pjsip_dialog *dlg = sub->tree->dlg;
+       pjsip_msg *msg = ast_sip_mod_data_get(dlg->mod_data, pubsub_module.id, MOD_DATA_MSG);
+       pj_str_t name;
+
+       pj_cstr(&name, header);
+
+       return pjsip_msg_find_hdr_by_name(msg, &name, NULL);
 }
 
-struct ast_datastore *ast_sip_subscription_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
+struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler,
+               struct ast_sip_endpoint *endpoint, const char *resource)
 {
-       RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
-       const char *uid_ptr = uid;
+       struct ast_sip_subscription *sub;
+       pjsip_dialog *dlg;
+       struct ast_sip_contact *contact;
+       pj_str_t event;
+       pjsip_tx_data *tdata;
+       pjsip_evsub *evsub;
+       struct sip_subscription_tree *sub_tree = NULL;
 
-       if (!info) {
+       sub_tree = allocate_subscription_tree(endpoint);
+       if (!sub_tree) {
                return NULL;
        }
 
-       datastore = ao2_alloc(sizeof(*datastore), subscription_datastore_destroy);
-       if (!datastore) {
+       sub = allocate_subscription(handler, resource, sub_tree);
+       if (!sub) {
+               ao2_cleanup(sub_tree);
                return NULL;
        }
 
-       datastore->info = info;
-       if (ast_strlen_zero(uid)) {
-               /* They didn't provide an ID so we'll provide one ourself */
-               struct ast_uuid *uuid = ast_uuid_generate();
-               char uuid_buf[AST_UUID_STR_LEN];
-               if (!uuid) {
-                       return NULL;
-               }
-               uid_ptr = ast_uuid_to_str(uuid, uuid_buf, sizeof(uuid_buf));
-               ast_free(uuid);
+       contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
+       if (!contact || ast_strlen_zero(contact->uri)) {
+               ast_log(LOG_WARNING, "No contacts configured for endpoint %s. Unable to create SIP subsription\n",
+                               ast_sorcery_object_get_id(endpoint));
+               ao2_ref(sub_tree, -1);
+               ao2_cleanup(contact);
+               return NULL;
        }
 
-       datastore->uid = ast_strdup(uid_ptr);
-       if (!datastore->uid) {
+       dlg = ast_sip_create_dialog_uac(endpoint, contact->uri, NULL);
+       ao2_cleanup(contact);
+       if (!dlg) {
+               ast_log(LOG_WARNING, "Unable to create dialog for SIP subscription\n");
+               ao2_ref(sub_tree, -1);
                return NULL;
        }
 
-       ao2_ref(datastore, +1);
-       return datastore;
-}
+       pj_cstr(&event, handler->event_name);
+       pjsip_evsub_create_uac(dlg, &pubsub_cb, &event, 0, &sub_tree->evsub);
+       subscription_setup_dialog(sub_tree, dlg);
 
-int ast_sip_subscription_add_datastore(struct ast_sip_subscription *subscription, struct ast_datastore *datastore)
-{
-       ast_assert(datastore != NULL);
-       ast_assert(datastore->info != NULL);
-       ast_assert(!ast_strlen_zero(datastore->uid));
+       evsub = sub_tree->evsub;
 
-       if (!ao2_link(subscription->datastores, datastore)) {
-               return -1;
+       if (pjsip_evsub_initiate(evsub, NULL, -1, &tdata) == PJ_SUCCESS) {
+               pjsip_evsub_send_request(evsub, tdata);
+       } else {
+               /* pjsip_evsub_terminate will result in pubsub_on_evsub_state,
+                * being called and terminating the subscription. Therefore, we don't
+                * need to decrease the reference count of sub here.
+                */
+               pjsip_evsub_terminate(evsub, PJ_TRUE);
+               ao2_ref(sub_tree, -1);
+               return NULL;
        }
-       return 0;
+
+       return sub;
 }
 
-struct ast_datastore *ast_sip_subscription_get_datastore(struct ast_sip_subscription *subscription, const char *name)
+struct ast_sip_endpoint *ast_sip_subscription_get_endpoint(struct ast_sip_subscription *sub)
 {
-       return ao2_find(subscription->datastores, name, OBJ_KEY);
+       ast_assert(sub->tree->endpoint != NULL);
+       return ao2_bump(sub->tree->endpoint);
 }
 
-void ast_sip_subscription_remove_datastore(struct ast_sip_subscription *subscription, const char *name)
+struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_subscription *sub)
 {
-       ao2_find(subscription->datastores, name, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
+       ast_assert(sub->tree->serializer != NULL);
+       return sub->tree->serializer;
 }
 
-int ast_sip_publication_add_datastore(struct ast_sip_publication *publication, struct ast_datastore *datastore)
+static int sip_subscription_send_request(struct sip_subscription_tree *sub_tree, pjsip_tx_data *tdata)
 {
-       ast_assert(datastore != NULL);
-       ast_assert(datastore->info != NULL);
-       ast_assert(!ast_strlen_zero(datastore->uid));
+#ifdef TEST_FRAMEWORK
+       struct ast_sip_endpoint *endpoint = sub_tree->endpoint;
+#endif
+       int res;
 
-       if (!ao2_link(publication->datastores, datastore)) {
-               return -1;
-       }
-       return 0;
-}
+       res = pjsip_evsub_send_request(sub_tree->evsub, tdata) == PJ_SUCCESS ? 0 : -1;
+       subscription_persistence_update(sub_tree, NULL);
 
-struct ast_datastore *ast_sip_publication_get_datastore(struct ast_sip_publication *publication, const char *name)
-{
-       return ao2_find(publication->datastores, name, OBJ_KEY);
-}
+       ast_test_suite_event_notify("SUBSCRIPTION_STATE_SET",
+               "StateText: %s\r\n"
+               "Endpoint: %s\r\n",
+               pjsip_evsub_get_state_name(sub_tree->evsub),
+               ast_sorcery_object_get_id(endpoint));
+
+       return res;
+}
+
+/*!
+ * \brief Add a resource XML element to an RLMI body
+ *
+ * Each resource element represents a subscribed resource in the list. This function currently
+ * will unconditionally add an instance element to each created resource element. Instance
+ * elements refer to later parts in the multipart body.
+ *
+ * \param pool PJLIB allocation pool
+ * \param cid Content-ID header of the resource
+ * \param resource_name Name of the resource
+ * \param resource_uri URI of the resource
+ * \param state State of the subscribed resource
+ */
+static void add_rlmi_resource(pj_pool_t *pool, pj_xml_node *rlmi, const pjsip_generic_string_hdr *cid,
+               const char *resource_name, const pjsip_sip_uri *resource_uri, pjsip_evsub_state state)
+{
+       static pj_str_t cid_name = { "cid", 3 };
+       pj_xml_node *resource;
+       pj_xml_node *name;
+       pj_xml_node *instance;
+       pj_xml_attr *cid_attr;
+       char id[6];
+       char uri[PJSIP_MAX_URL_SIZE];
+
+       /* This creates a string representing the Content-ID without the enclosing < > */
+       const pj_str_t cid_stripped = {
+               .ptr = cid->hvalue.ptr + 1,
+               .slen = cid->hvalue.slen - 2,
+       };
+
+       resource = ast_sip_presence_xml_create_node(pool, rlmi, "resource");
+       name = ast_sip_presence_xml_create_node(pool, resource, "name");
+       instance = ast_sip_presence_xml_create_node(pool, resource, "instance");
+
+       pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, resource_uri, uri, sizeof(uri));
+       ast_sip_presence_xml_create_attr(pool, resource, "uri", uri);
+
+       pj_strdup2(pool, &name->content, resource_name);
+
+       ast_generate_random_string(id, sizeof(id));
+
+       ast_sip_presence_xml_create_attr(pool, instance, "id", id);
+       ast_sip_presence_xml_create_attr(pool, instance, "state",
+                       state == PJSIP_EVSUB_STATE_TERMINATED ? "terminated" : "active");
+
+       /* Use the PJLIB-util XML library directly here since we are using a
+        * pj_str_t
+        */
+
+       cid_attr = pj_xml_attr_new(pool, &cid_name, &cid_stripped);
+       pj_xml_add_attr(instance, cid_attr);
+}
+
+/*!
+ * \brief A multipart body part and meta-information
+ *
+ * When creating a multipart body part, the end result (the
+ * pjsip_multipart_part) is hard to inspect without undoing
+ * a lot of what was done to create it. Therefore, we use this
+ * structure to store meta-information about the body part.
+ *
+ * The main consumer of this is the creator of the RLMI body
+ * part of a multipart resource list body.
+ */
+struct body_part {
+       /*! Content-ID header for the body part */
+       pjsip_generic_string_hdr *cid;
+       /*! Subscribed resource represented in the body part */
+       const char *resource;
+       /*! URI for the subscribed body part */
+       pjsip_sip_uri *uri;
+       /*! Subscription state of the resource represented in the body part */
+       pjsip_evsub_state state;
+       /*! The actual body part that will be present in the multipart body */
+       pjsip_multipart_part *part;
+};
+
+/*!
+ * \brief Type declaration for container of body part structures
+ */
+AST_VECTOR(body_part_list, struct body_part *);
+
+/*!
+ * \brief Create a Content-ID header
+ *
+ * Content-ID headers are required by RFC2387 for multipart/related
+ * bodies. They serve as identifiers for each part of the multipart body.
+ *
+ * \param pool PJLIB allocation pool
+ * \param sub Subscription to a resource
+ */
+static pjsip_generic_string_hdr *generate_content_id_hdr(pj_pool_t *pool,
+               const struct ast_sip_subscription *sub)
+{
+       static const pj_str_t cid_name = { "Content-ID", 10 };
+       pjsip_generic_string_hdr *cid;
+       char id[6];
+       size_t alloc_size;
+       pj_str_t cid_value;
+
+       /* '<' + '@' + '>' = 3. pj_str_t does not require a null-terminator */
+       alloc_size = sizeof(id) + pj_strlen(&sub->uri->host) + 3;
+       cid_value.ptr = pj_pool_alloc(pool, alloc_size);
+       cid_value.slen = sprintf(cid_value.ptr, "<%s@%.*s>",
+                       ast_generate_random_string(id, sizeof(id)),
+                       (int) pj_strlen(&sub->uri->host), pj_strbuf(&sub->uri->host));
+       cid = pjsip_generic_string_hdr_create(pool, &cid_name, &cid_value);
+
+       return cid;
+}
+
+static int rlmi_print_body(struct pjsip_msg_body *msg_body, char *buf, pj_size_t size)
+{
+       int num_printed;
+       pj_xml_node *rlmi = msg_body->data;
+
+       num_printed = pj_xml_print(rlmi, buf, size, PJ_TRUE);
+       if (num_printed == AST_PJSIP_XML_PROLOG_LEN) {
+               return -1;
+       }
+
+       return num_printed;
+}
+
+static void *rlmi_clone_data(pj_pool_t *pool, const void *data, unsigned len)
+{
+       const pj_xml_node *rlmi = data;
+
+       return pj_xml_clone(pool, rlmi);
+}
+
+/*!
+ * \brief Create an RLMI body part for a multipart resource list body
+ *
+ * RLMI (Resource list meta information) is a special body type that lists
+ * the subscribed resources and tells subscribers the number of subscribed
+ * resources and what other body parts are in the multipart body. The
+ * RLMI body also has a version number that a subscriber can use to ensure
+ * that the locally-stored state corresponds to server state.
+ *
+ * \param pool The allocation pool
+ * \param sub The subscription representing the subscribed resource list
+ * \param body_parts A container of body parts that RLMI will refer to
+ * \param full_state Indicates whether this is a full or partial state notification
+ * \return The multipart part representing the RLMI body
+ */
+static pjsip_multipart_part *build_rlmi_body(pj_pool_t *pool, struct ast_sip_subscription *sub,
+               struct body_part_list *body_parts, unsigned int full_state)
+{
+       static const pj_str_t rlmi_type = { "application", 11 };
+       static const pj_str_t rlmi_subtype = { "rlmi+xml", 8 };
+       pj_xml_node *rlmi;
+       pj_xml_node *name;
+       pjsip_multipart_part *rlmi_part;
+       char version_str[32];
+       char uri[PJSIP_MAX_URL_SIZE];
+       pjsip_generic_string_hdr *cid;
+       int i;
+
+       rlmi = ast_sip_presence_xml_create_node(pool, NULL, "list");
+       ast_sip_presence_xml_create_attr(pool, rlmi, "xmlns", "urn:ietf:params:xml:ns:rlmi");
+
+       ast_sip_subscription_get_local_uri(sub, uri, sizeof(uri));
+       ast_sip_presence_xml_create_attr(pool, rlmi, "uri", uri);
+
+       snprintf(version_str, sizeof(version_str), "%u", sub->version++);
+       ast_sip_presence_xml_create_attr(pool, rlmi, "version", version_str);
+       ast_sip_presence_xml_create_attr(pool, rlmi, "fullState", full_state ? "true" : "false");
+
+       name = ast_sip_presence_xml_create_node(pool, rlmi, "name");
+       pj_strdup2(pool, &name->content, ast_sip_subscription_get_resource_name(sub));
+
+       for (i = 0; i < AST_VECTOR_SIZE(body_parts); ++i) {
+               const struct body_part *part = AST_VECTOR_GET(body_parts, i);
+
+               add_rlmi_resource(pool, rlmi, part->cid, part->resource, part->uri, part->state);
+       }
+
+       rlmi_part = pjsip_multipart_create_part(pool);
+
+       rlmi_part->body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
+       pj_strdup(pool, &rlmi_part->body->content_type.type, &rlmi_type);
+       pj_strdup(pool, &rlmi_part->body->content_type.subtype, &rlmi_subtype);
+       pj_list_init(&rlmi_part->body->content_type.param);
+
+       rlmi_part->body->data = pj_xml_clone(pool, rlmi);
+       rlmi_part->body->clone_data = rlmi_clone_data;
+       rlmi_part->body->print_body = rlmi_print_body;
+
+       cid = generate_content_id_hdr(pool, sub);
+       pj_list_insert_before(&rlmi_part->hdr, cid);
+
+       return rlmi_part;
+}
+
+static pjsip_msg_body *generate_notify_body(pj_pool_t *pool, struct ast_sip_subscription *root,
+               unsigned int force_full_state);
+
+/*!
+ * \brief Destroy a list of body parts
+ *
+ * \param parts The container of parts to destroy
+ */
+static void free_body_parts(struct body_part_list *parts)
+{
+       int i;
+
+       for (i = 0; i < AST_VECTOR_SIZE(parts); ++i) {
+               struct body_part *part = AST_VECTOR_GET(parts, i);
+               ast_free(part);
+       }
+
+       AST_VECTOR_FREE(parts);
+}
+
+/*!
+ * \brief Allocate and initialize a body part structure
+ *
+ * \param pool PJLIB allocation pool
+ * \param sub Subscription representing a subscribed resource
+ */
+static struct body_part *allocate_body_part(pj_pool_t *pool, const struct ast_sip_subscription *sub)
+{
+       struct body_part *bp;
+
+       bp = ast_calloc(1, sizeof(*bp));
+       if (!bp) {
+               return NULL;
+       }
+
+       bp->cid = generate_content_id_hdr(pool, sub);
+       bp->resource = sub->resource;
+       bp->state = sub->subscription_state;
+       bp->uri = sub->uri;
+
+       return bp;
+}
+
+/*!
+ * \brief Create a multipart body part for a subscribed resource
+ *
+ * \param pool PJLIB allocation pool
+ * \param sub The subscription representing a subscribed resource
+ * \param parts A vector of parts to append the created part to.
+ * \param use_full_state Unused locally, but may be passed to other functions
+ */
+static void build_body_part(pj_pool_t *pool, struct ast_sip_subscription *sub,
+               struct body_part_list *parts, unsigned int use_full_state)
+{
+       struct body_part *bp;
+       pjsip_msg_body *body;
+
+       bp = allocate_body_part(pool, sub);
+       if (!bp) {
+               return;
+       }
+
+       body = generate_notify_body(pool, sub, use_full_state);
+       if (!body) {
+               /* Partial state was requested and the resource has not changed state */
+               ast_free(bp);
+               return;
+       }
+
+       bp->part = pjsip_multipart_create_part(pool);
+       bp->part->body = body;
+       pj_list_insert_before(&bp->part->hdr, bp->cid);
+
+       AST_VECTOR_APPEND(parts, bp);
+}
+
+/*!
+ * \brief Create and initialize the PJSIP multipart body structure for a resource list subscription
+ *
+ * \param pool
+ * \return The multipart message body
+ */
+static pjsip_msg_body *create_multipart_body(pj_pool_t *pool)
+{
+       pjsip_media_type media_type;
+       pjsip_param *media_type_param;
+       char boundary[6];
+       pj_str_t pj_boundary;
+
+       pjsip_media_type_init2(&media_type, "multipart", "related");
+
+       media_type_param = pj_pool_alloc(pool, sizeof(*media_type_param));
+       pj_list_init(media_type_param);
+
+       pj_strdup2(pool, &media_type_param->name, "type");
+       pj_strdup2(pool, &media_type_param->value, "\"application/rlmi+xml\"");
+
+       pj_list_insert_before(&media_type.param, media_type_param);
+
+       pj_cstr(&pj_boundary, ast_generate_random_string(boundary, sizeof(boundary)));
+       return pjsip_multipart_create(pool, &media_type, &pj_boundary);
+}
+
+/*!
+ * \brief Create a resource list body for NOTIFY requests
+ *
+ * Resource list bodies are multipart/related bodies. The first part of the multipart body
+ * is an RLMI body that describes the rest of the parts to come. The other parts of the body
+ * convey state of individual subscribed resources.
+ *
+ * \param pool PJLIB allocation pool
+ * \param sub Subscription details from which to generate body
+ * \param force_full_state If true, ignore resource list settings and send a full state notification
+ * \return The generated multipart/related body
+ */
+static pjsip_msg_body *generate_list_body(pj_pool_t *pool, struct ast_sip_subscription *sub,
+               unsigned int force_full_state)
+{
+       int i;
+       pjsip_multipart_part *rlmi_part;
+       pjsip_msg_body *multipart;
+       struct body_part_list body_parts;
+       unsigned int use_full_state = force_full_state ? 1 : sub->full_state;
+
+       if (AST_VECTOR_INIT(&body_parts, AST_VECTOR_SIZE(&sub->children))) {
+               return NULL;
+       }
+
+       for (i = 0; i < AST_VECTOR_SIZE(&sub->children); ++i) {
+               build_body_part(pool, AST_VECTOR_GET(&sub->children, i), &body_parts, use_full_state);
+       }
+
+       /* This can happen if issuing partial state and no children of the list have changed state */
+       if (AST_VECTOR_SIZE(&body_parts) == 0) {
+               return NULL;
+       }
+
+       multipart = create_multipart_body(pool);
+
+       rlmi_part = build_rlmi_body(pool, sub, &body_parts, use_full_state);
+       if (!rlmi_part) {
+               return NULL;
+       }
+       pjsip_multipart_add_part(pool, multipart, rlmi_part);
+
+       for (i = 0; i < AST_VECTOR_SIZE(&body_parts); ++i) {
+               pjsip_multipart_add_part(pool, multipart, AST_VECTOR_GET(&body_parts, i)->part);
+       }
+
+       free_body_parts(&body_parts);
+       return multipart;
+}
+
+/*!
+ * \brief Create the body for a NOTIFY request.
+ *
+ * \param pool The pool used for allocations
+ * \param root The root of the subscription tree
+ * \param force_full_state If true, ignore resource list settings and send a full state notification
+ */
+static pjsip_msg_body *generate_notify_body(pj_pool_t *pool, struct ast_sip_subscription *root,
+               unsigned int force_full_state)
+{
+       pjsip_msg_body *body;
+
+       if (AST_VECTOR_SIZE(&root->children) == 0) {
+               if (force_full_state || root->body_changed) {
+                       /* Not a list. We've already generated the body and saved it on the subscription.
+                        * Use that directly.
+                        */
+                       pj_str_t type;
+                       pj_str_t subtype;
+                       pj_str_t text;
+
+                       pj_cstr(&type, ast_sip_subscription_get_body_type(root));
+                       pj_cstr(&subtype, ast_sip_subscription_get_body_subtype(root));
+                       pj_cstr(&text, ast_str_buffer(root->body_text));
+
+                       body = pjsip_msg_body_create(pool, &type, &subtype, &text);
+                       root->body_changed = 0;
+               } else {
+                       body = NULL;
+               }
+       } else {
+               body = generate_list_body(pool, root, force_full_state);
+       }
+
+       return body;
+}
+
+/*!
+ * \brief Shortcut method to create a Require: eventlist header
+ */
+static pjsip_require_hdr *create_require_eventlist(pj_pool_t *pool)
+{
+       pjsip_require_hdr *require;
+
+       require = pjsip_require_hdr_create(pool);
+       pj_strdup2(pool, &require->values[0], "eventlist");
+       require->count = 1;
+
+       return require;
+}
+
+/*!
+ * \brief Send a NOTIFY request to a subscriber
+ *
+ * \param sub_tree The subscription tree representing the subscription
+ * \param force_full_state If true, ignore resource list settings and send full resource list state.
+ * \retval 0 Success
+ * \retval non-zero Failure
+ */
+static int send_notify(struct sip_subscription_tree *sub_tree, unsigned int force_full_state)
+{
+       pjsip_evsub *evsub = sub_tree->evsub;
+       pjsip_tx_data *tdata;
+
+       if (pjsip_evsub_notify(evsub, sub_tree->root->subscription_state,
+                               NULL, NULL, &tdata) != PJ_SUCCESS) {
+               return -1;
+       }
+
+       tdata->msg->body = generate_notify_body(tdata->pool, sub_tree->root, force_full_state);
+       if (!tdata->msg->body) {
+               pjsip_tx_data_dec_ref(tdata);
+               return -1;
+       }
+
+       if (sub_tree->is_list) {
+               pjsip_require_hdr *require = create_require_eventlist(tdata->pool);
+               pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) require);
+       }
+
+       if (sip_subscription_send_request(sub_tree, tdata)) {
+               return -1;
+       }
+
+       sub_tree->send_scheduled_notify = 0;
+
+       return 0;
+}
+
+static int serialized_send_notify(void *userdata)
+{
+       struct sip_subscription_tree *sub_tree = userdata;
+
+       /* It's possible that between when the notification was scheduled
+        * and now, that a new SUBSCRIBE arrived, requiring full state to be
+        * sent out in an immediate NOTIFY. If that has happened, we need to
+        * bail out here instead of sending the batched NOTIFY.
+        */
+       if (!sub_tree->send_scheduled_notify) {
+               ao2_cleanup(sub_tree);
+               return 0;
+       }
+
+       send_notify(sub_tree, 0);
+       ao2_cleanup(sub_tree);
+       return 0;
+}
+
+static int sched_cb(const void *data)
+{
+       struct sip_subscription_tree *sub_tree = (struct sip_subscription_tree *) data;
+
+       /* We don't need to bump the refcount of sub_tree since we bumped it when scheduling this task */
+       ast_sip_push_task(sub_tree->serializer, serialized_send_notify, sub_tree);
+       return 0;
+}
+
+static int schedule_notification(struct sip_subscription_tree *sub_tree)
+{
+       /* There's already a notification scheduled */
+       if (sub_tree->notify_sched_id > -1) {
+               return 0;
+       }
+
+       sub_tree->notify_sched_id = ast_sched_add(sched, sub_tree->notification_batch_interval, sched_cb, ao2_bump(sub_tree));
+       if (sub_tree->notify_sched_id < 0) {
+               return -1;
+       }
+
+       sub_tree->send_scheduled_notify = 1;
+       return 0;
+}
+
+int ast_sip_subscription_notify(struct ast_sip_subscription *sub, void *notify_data,
+               int terminate)
+{
+       if (ast_sip_pubsub_generate_body_content(ast_sip_subscription_get_body_type(sub),
+                               ast_sip_subscription_get_body_subtype(sub), notify_data, &sub->body_text)) {
+               return -1;
+       }
+
+       sub->body_changed = 1;
+       if (terminate) {
+               sub->subscription_state = PJSIP_EVSUB_STATE_TERMINATED;
+       }
+
+       if (sub->tree->notification_batch_interval) {
+               return schedule_notification(sub->tree);
+       } else {
+               return send_notify(sub->tree, 0);
+       }
+}
+
+void ast_sip_subscription_get_local_uri(struct ast_sip_subscription *sub, char *buf, size_t size)
+{
+       pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, sub->uri, buf, size);
+}
+
+void ast_sip_subscription_get_remote_uri(struct ast_sip_subscription *sub, char *buf, size_t size)
+{
+       pjsip_dialog *dlg = sub->tree->dlg;
+       ast_copy_pj_str(buf, &dlg->remote.info_str, size);
+}
+
+const char *ast_sip_subscription_get_resource_name(struct ast_sip_subscription *sub)
+{
+       return sub->resource;
+}
+
+static int sip_subscription_accept(struct sip_subscription_tree *sub_tree, pjsip_rx_data *rdata, int response)
+{
+       pjsip_hdr res_hdr;
+
+       /* If this is a persistence recreation the subscription has already been accepted */
+       if (ast_sip_mod_data_get(rdata->endpt_info.mod_data, pubsub_module.id, MOD_DATA_PERSISTENCE)) {
+               return 0;
+       }
+
+       pj_list_init(&res_hdr);
+       if (sub_tree->is_list) {
+               /* If subscribing to a list, our response has to have a Require: eventlist header in it */
+               pj_list_insert_before(&res_hdr, create_require_eventlist(rdata->tp_info.pool));
+       }
+
+       return pjsip_evsub_accept(sub_tree->evsub, rdata, response, &res_hdr) == PJ_SUCCESS ? 0 : -1;
+}
+
+static void subscription_datastore_destroy(void *obj)
+{
+       struct ast_datastore *datastore = obj;
+
+       /* Using the destroy function (if present) destroy the data */
+       if (datastore->info->destroy != NULL && datastore->data != NULL) {
+               datastore->info->destroy(datastore->data);
+               datastore->data = NULL;
+       }
+
+       ast_free((void *) datastore->uid);
+       datastore->uid = NULL;
+}
+
+struct ast_datastore *ast_sip_subscription_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
+{
+       RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
+       const char *uid_ptr = uid;
+
+       if (!info) {
+               return NULL;
+       }
+
+       datastore = ao2_alloc(sizeof(*datastore), subscription_datastore_destroy);
+       if (!datastore) {
+               return NULL;
+       }
+
+       datastore->info = info;
+       if (ast_strlen_zero(uid)) {
+               /* They didn't provide an ID so we'll provide one ourself */
+               struct ast_uuid *uuid = ast_uuid_generate();
+               char uuid_buf[AST_UUID_STR_LEN];
+               if (!uuid) {
+                       return NULL;
+               }
+               uid_ptr = ast_uuid_to_str(uuid, uuid_buf, sizeof(uuid_buf));
+               ast_free(uuid);
+       }
+
+       datastore->uid = ast_strdup(uid_ptr);
+       if (!datastore->uid) {
+               return NULL;
+       }
+
+       ao2_ref(datastore, +1);
+       return datastore;
+}
+
+int ast_sip_subscription_add_datastore(struct ast_sip_subscription *subscription, struct ast_datastore *datastore)
+{
+       ast_assert(datastore != NULL);
+       ast_assert(datastore->info != NULL);
+       ast_assert(!ast_strlen_zero(datastore->uid));
+
+       if (!ao2_link(subscription->datastores, datastore)) {
+               return -1;
+       }
+       return 0;
+}
+
+struct ast_datastore *ast_sip_subscription_get_datastore(struct ast_sip_subscription *subscription, const char *name)
+{
+       return ao2_find(subscription->datastores, name, OBJ_KEY);
+}
+
+void ast_sip_subscription_remove_datastore(struct ast_sip_subscription *subscription, const char *name)
+{
+       ao2_find(subscription->datastores, name, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
+}
+
+int ast_sip_publication_add_datastore(struct ast_sip_publication *publication, struct ast_datastore *datastore)
+{
+       ast_assert(datastore != NULL);
+       ast_assert(datastore->info != NULL);
+       ast_assert(!ast_strlen_zero(datastore->uid));
+
+       if (!ao2_link(publication->datastores, datastore)) {
+               return -1;
+       }
+       return 0;
+}
+
+struct ast_datastore *ast_sip_publication_get_datastore(struct ast_sip_publication *publication, const char *name)
+{
+       return ao2_find(publication->datastores, name, OBJ_KEY);
+}
+
+void ast_sip_publication_remove_datastore(struct ast_sip_publication *publication, const char *name)
+{
+       ao2_callback(publication->datastores, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, NULL, (void *) name);
+}
+
+AST_RWLIST_HEAD_STATIC(publish_handlers, ast_sip_publish_handler);
+
+static int publication_hash_fn(const void *obj, const int flags)
+{
+       const struct ast_sip_publication *publication = obj;
+       const int *entity_tag = obj;
+
+       return flags & OBJ_KEY ? *entity_tag : publication->entity_tag;
+}
+
+static int publication_cmp_fn(void *obj, void *arg, int flags)
+{
+       const struct ast_sip_publication *publication1 = obj;
+       const struct ast_sip_publication *publication2 = arg;
+       const int *entity_tag = arg;
+
+       return (publication1->entity_tag == (flags & OBJ_KEY ? *entity_tag : publication2->entity_tag) ?
+               CMP_MATCH | CMP_STOP : 0);
+}
+
+static void publish_add_handler(struct ast_sip_publish_handler *handler)
+{
+       SCOPED_LOCK(lock, &publish_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
+       AST_RWLIST_INSERT_TAIL(&publish_handlers, handler, next);
+}
+
+int ast_sip_register_publish_handler(struct ast_sip_publish_handler *handler)
+{
+       if (ast_strlen_zero(handler->event_name)) {
+               ast_log(LOG_ERROR, "No event package specified for publish handler. Cannot register\n");
+               return -1;
+       }
+
+       if (!(handler->publications = ao2_container_alloc(PUBLICATIONS_BUCKETS,
+               publication_hash_fn, publication_cmp_fn))) {
+               ast_log(LOG_ERROR, "Could not allocate publications container for event '%s'\n",
+                       handler->event_name);
+               return -1;
+       }
+
+       publish_add_handler(handler);
+
+       ast_module_ref(ast_module_info->self);
+
+       return 0;
+}
+
+void ast_sip_unregister_publish_handler(struct ast_sip_publish_handler *handler)
+{
+       struct ast_sip_publish_handler *iter;
+       SCOPED_LOCK(lock, &publish_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
+       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&publish_handlers, iter, next) {
+               if (handler == iter) {
+                       AST_RWLIST_REMOVE_CURRENT(next);
+                       ao2_cleanup(handler->publications);
+                       ast_module_unref(ast_module_info->self);
+                       break;
+               }
+       }
+       AST_RWLIST_TRAVERSE_SAFE_END;
+}
+
+AST_RWLIST_HEAD_STATIC(subscription_handlers, ast_sip_subscription_handler);
+
+static void sub_add_handler(struct ast_sip_subscription_handler *handler)
+{
+       SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
+       AST_RWLIST_INSERT_TAIL(&subscription_handlers, handler, next);
+       ast_module_ref(ast_module_info->self);
+}
+
+static struct ast_sip_subscription_handler *find_sub_handler_for_event_name(const char *event_name)
+{
+       struct ast_sip_subscription_handler *iter;
+       SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
+
+       AST_RWLIST_TRAVERSE(&subscription_handlers, iter, next) {
+               if (!strcmp(iter->event_name, event_name)) {
+                       break;
+               }
+       }
+       return iter;
+}
+
+int ast_sip_register_subscription_handler(struct ast_sip_subscription_handler *handler)
+{
+       pj_str_t event;
+       pj_str_t accept[AST_SIP_MAX_ACCEPT] = { {0, }, };
+       struct ast_sip_subscription_handler *existing;
+       int i = 0;
+
+       if (ast_strlen_zero(handler->event_name)) {
+               ast_log(LOG_ERROR, "No event package specified for subscription handler. Cannot register\n");
+               return -1;
+       }
+
+       existing = find_sub_handler_for_event_name(handler->event_name);
+       if (existing) {
+               ast_log(LOG_ERROR, "Unable to register subscription handler for event %s."
+                               "A handler is already registered\n", handler->event_name);
+               return -1;
+       }
+
+       for (i = 0; i < AST_SIP_MAX_ACCEPT && !ast_strlen_zero(handler->accept[i]); ++i) {
+               pj_cstr(&accept[i], handler->accept[i]);
+       }
+
+       pj_cstr(&event, handler->event_name);
+
+       pjsip_evsub_register_pkg(&pubsub_module, &event, DEFAULT_EXPIRES, i, accept);
+
+       sub_add_handler(handler);
+
+       return 0;
+}
+
+void ast_sip_unregister_subscription_handler(struct ast_sip_subscription_handler *handler)
+{
+       struct ast_sip_subscription_handler *iter;
+       SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
+       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&subscription_handlers, iter, next) {
+               if (handler == iter) {
+                       AST_RWLIST_REMOVE_CURRENT(next);
+                       ast_module_unref(ast_module_info->self);
+                       break;
+               }
+       }
+       AST_RWLIST_TRAVERSE_SAFE_END;
+}
+
+static struct ast_sip_pubsub_body_generator *find_body_generator_type_subtype(const char *content_type,
+               const char *content_subtype)
+{
+       struct ast_sip_pubsub_body_generator *iter;
+       SCOPED_LOCK(lock, &body_generators, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
+
+       AST_LIST_TRAVERSE(&body_generators, iter, list) {
+               if (!strcmp(iter->type, content_type) &&
+                               !strcmp(iter->subtype, content_subtype)) {
+                       break;
+               }
+       };
+
+       return iter;
+}
+
+static struct ast_sip_pubsub_body_generator *find_body_generator_accept(const char *accept)
+{
+       char *accept_copy = ast_strdupa(accept);
+       char *subtype = accept_copy;
+       char *type = strsep(&subtype, "/");
+
+       if (ast_strlen_zero(type) || ast_strlen_zero(subtype)) {
+               return NULL;
+       }
+
+       return find_body_generator_type_subtype(type, subtype);
+}
+
+static struct ast_sip_pubsub_body_generator *find_body_generator(char accept[AST_SIP_MAX_ACCEPT][64],
+               size_t num_accept)
+{
+       int i;
+       struct ast_sip_pubsub_body_generator *generator = NULL;
+
+       for (i = 0; i < num_accept; ++i) {
+               generator = find_body_generator_accept(accept[i]);
+               if (generator) {
+                       ast_debug(3, "Body generator %p found for accept type %s\n", generator, accept[i]);
+                       break;
+               } else {
+                       ast_debug(3, "No body generator found for accept type %s\n", accept[i]);
+               }
+       }
+
+       return generator;
+}
+
+static int generate_initial_notify(struct ast_sip_subscription *sub)
+{
+       void *notify_data;
+       int res;
+
+       if (AST_VECTOR_SIZE(&sub->children) > 0) {
+               int i;
+
+               for (i = 0; i < AST_VECTOR_SIZE(&sub->children); ++i) {
+                       if (generate_initial_notify(AST_VECTOR_GET(&sub->children, i))) {
+                               return -1;
+                       }
+               }
+
+               return 0;
+       }
+
+       if (sub->handler->notifier->subscription_established(sub)) {
+               return -1;
+       }
+
+       notify_data = sub->handler->notifier->get_notify_data(sub);
+       if (!notify_data) {
+               return -1;
+       }
+
+       res = ast_sip_pubsub_generate_body_content(ast_sip_subscription_get_body_type(sub),
+                       ast_sip_subscription_get_body_subtype(sub), notify_data, &sub->body_text);
+
+       ao2_cleanup(notify_data);
+
+       return res;
+}
+
+static pj_bool_t pubsub_on_rx_subscribe_request(pjsip_rx_data *rdata)
+{
+       pjsip_expires_hdr *expires_header;
+       struct ast_sip_subscription_handler *handler;
+       RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
+       struct sip_subscription_tree *sub_tree;
+       struct ast_sip_pubsub_body_generator *generator;
+       char *resource;
+       pjsip_uri *request_uri;
+       pjsip_sip_uri *request_uri_sip;
+       size_t resource_size;
+       int resp;
+       struct resource_tree tree;
+
+       endpoint = ast_pjsip_rdata_get_endpoint(rdata);
+       ast_assert(endpoint != NULL);
+
+       if (!endpoint->subscription.allow) {
+               ast_log(LOG_WARNING, "Subscriptions not permitted for endpoint %s.\n", ast_sorcery_object_get_id(endpoint));
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 603, NULL, NULL, NULL);
+               return PJ_TRUE;
+       }
+
+       request_uri = rdata->msg_info.msg->line.req.uri;
+
+       if (!PJSIP_URI_SCHEME_IS_SIP(request_uri) && !PJSIP_URI_SCHEME_IS_SIPS(request_uri)) {
+               char uri_str[PJSIP_MAX_URL_SIZE];
+
+               pjsip_uri_print(PJSIP_URI_IN_REQ_URI, request_uri, uri_str, sizeof(uri_str));
+               ast_log(LOG_WARNING, "Request URI '%s' is not a sip: or sips: URI.\n", uri_str);
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 416, NULL, NULL, NULL);
+               return PJ_TRUE;
+       }
+
+       request_uri_sip = pjsip_uri_get_uri(request_uri);
+       resource_size = pj_strlen(&request_uri_sip->user) + 1;
+       resource = alloca(resource_size);
+       ast_copy_pj_str(resource, &request_uri_sip->user, resource_size);
+
+       expires_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, rdata->msg_info.msg->hdr.next);
+
+       if (expires_header) {
+               if (expires_header->ivalue == 0) {
+                       ast_log(LOG_WARNING, "Susbscription request from endpoint %s rejected. Expiration of 0 is invalid\n",
+                               ast_sorcery_object_get_id(endpoint));
+                       pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 400, NULL, NULL, NULL);
+                               return PJ_TRUE;
+               }
+               if (expires_header->ivalue < endpoint->subscription.minexpiry) {
+                       ast_log(LOG_WARNING, "Subscription expiration %d is too brief for endpoint %s. Minimum is %u\n",
+                               expires_header->ivalue, ast_sorcery_object_get_id(endpoint), endpoint->subscription.minexpiry);
+                       pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 423, NULL, NULL, NULL);
+                       return PJ_TRUE;
+               }
+       }
+
+       handler = subscription_get_handler_from_rdata(rdata);
+       if (!handler) {
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
+               return PJ_TRUE;
+       }
+
+       generator = subscription_get_generator_from_rdata(rdata, handler);
+       if (!generator) {
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
+               return PJ_TRUE;
+       }
+
+       memset(&tree, 0, sizeof(tree));
+       resp = build_resource_tree(endpoint, handler, resource, &tree);
+       if (!PJSIP_IS_STATUS_IN_CLASS(resp, 200)) {
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, resp, NULL, NULL, NULL);
+               resource_tree_destroy(&tree);
+               return PJ_TRUE;
+       }
+
+       sub_tree = create_subscription_tree(handler, endpoint, rdata, resource, generator, &tree);
+       if (!sub_tree) {
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
+       } else {
+               sub_tree->persistence = subscription_persistence_create(sub_tree);
+               subscription_persistence_update(sub_tree, rdata);
+               sip_subscription_accept(sub_tree, rdata, resp);
+               if (generate_initial_notify(sub_tree->root)) {
+                       pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE);
+               }
+               send_notify(sub_tree, 1);
+       }
+
+       resource_tree_destroy(&tree);
+       return PJ_TRUE;
+}
+
+static struct ast_sip_publish_handler *find_pub_handler(const char *event)
+{
+       struct ast_sip_publish_handler *iter = NULL;
+       SCOPED_LOCK(lock, &publish_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
+
+       AST_RWLIST_TRAVERSE(&publish_handlers, iter, next) {
+               if (strcmp(event, iter->event_name)) {
+                       ast_debug(3, "Event %s does not match %s\n", event, iter->event_name);
+                       continue;
+               }
+               ast_debug(3, "Event name match: %s = %s\n", event, iter->event_name);
+               break;
+       }
+
+       return iter;
+}
+
+static enum sip_publish_type determine_sip_publish_type(pjsip_rx_data *rdata,
+       pjsip_generic_string_hdr *etag_hdr, int *expires, int *entity_id)
+{
+       pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
+
+       if (etag_hdr) {
+               char etag[pj_strlen(&etag_hdr->hvalue) + 1];
+
+               ast_copy_pj_str(etag, &etag_hdr->hvalue, sizeof(etag));
+
+               if (sscanf(etag, "%30d", entity_id) != 1) {
+                       return SIP_PUBLISH_UNKNOWN;
+               }
+       }
+
+       *expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES;
+
+       if (!(*expires)) {
+               return SIP_PUBLISH_REMOVE;
+       } else if (!etag_hdr && rdata->msg_info.msg->body) {
+               return SIP_PUBLISH_INITIAL;
+       } else if (etag_hdr && !rdata->msg_info.msg->body) {
+               return SIP_PUBLISH_REFRESH;
+       } else if (etag_hdr && rdata->msg_info.msg->body) {
+               return SIP_PUBLISH_MODIFY;
+       }
+
+       return SIP_PUBLISH_UNKNOWN;
+}
+
+/*! \brief Internal destructor for publications */
+static void publication_destroy_fn(void *obj)
+{
+       struct ast_sip_publication *publication = obj;
+
+       ast_debug(3, "Destroying SIP publication\n");
+
+       ao2_cleanup(publication->datastores);
+       ao2_cleanup(publication->endpoint);
+}
+
+static struct ast_sip_publication *sip_create_publication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata,
+       const char *resource, const char *event_configuration_name)
+{
+       struct ast_sip_publication *publication;
+       pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
+       size_t resource_len = strlen(resource) + 1, event_configuration_name_len = strlen(event_configuration_name) + 1;
+       char *dst;
+
+       ast_assert(endpoint != NULL);
+
+       if (!(publication = ao2_alloc(sizeof(*publication) + resource_len + event_configuration_name_len, publication_destroy_fn))) {
+               return NULL;
+       }
+
+       if (!(publication->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp))) {
+               ao2_ref(publication, -1);
+               return NULL;
+       }
+
+       publication->entity_tag = ast_atomic_fetchadd_int(&esc_etag_counter, +1);
+       ao2_ref(endpoint, +1);
+       publication->endpoint = endpoint;
+       publication->expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES;
+       publication->sched_id = -1;
+       dst = publication->data;
+       publication->resource = strcpy(dst, resource);
+       dst += resource_len;
+       publication->event_configuration_name = strcpy(dst, event_configuration_name);
+
+       return publication;
+}
+
+static int sip_publication_respond(struct ast_sip_publication *pub, int status_code,
+               pjsip_rx_data *rdata)
+{
+       pj_status_t status;
+       pjsip_tx_data *tdata;
+       pjsip_transaction *tsx;
+
+       if (pjsip_endpt_create_response(ast_sip_get_pjsip_endpoint(), rdata, status_code, NULL, &tdata) != PJ_SUCCESS) {
+               return -1;
+       }
+
+       if (PJSIP_IS_STATUS_IN_CLASS(status_code, 200)) {
+               RAII_VAR(char *, entity_tag, NULL, ast_free_ptr);
+               RAII_VAR(char *, expires, NULL, ast_free_ptr);
+
+               if ((ast_asprintf(&entity_tag, "%d", pub->entity_tag) < 0) ||
+                       (ast_asprintf(&expires, "%d", pub->expires) < 0)) {
+                       pjsip_tx_data_dec_ref(tdata);
+                       return -1;
+               }
+
+               ast_sip_add_header(tdata, "SIP-ETag", entity_tag);
+               ast_sip_add_header(tdata, "Expires", expires);
+       }
+
+       if ((status = pjsip_tsx_create_uas(&pubsub_module, rdata, &tsx)) != PJ_SUCCESS) {
+               return -1;
+       }
+
+       pjsip_tsx_recv_msg(tsx, rdata);
+
+       if (pjsip_tsx_send_msg(tsx, tdata) != PJ_SUCCESS) {
+               return -1;
+       }
+
+       return 0;
+}
+
+static struct ast_sip_publication *publish_request_initial(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata,
+       struct ast_sip_publish_handler *handler)
+{
+       struct ast_sip_publication *publication;
+       char *resource_name;
+       size_t resource_size;
+       RAII_VAR(struct ast_sip_publication_resource *, resource, NULL, ao2_cleanup);
+       struct ast_variable *event_configuration_name = NULL;
+       pjsip_uri *request_uri;
+       pjsip_sip_uri *request_uri_sip;
+       int resp;
+
+       request_uri = rdata->msg_info.msg->line.req.uri;
+
+       if (!PJSIP_URI_SCHEME_IS_SIP(request_uri) && !PJSIP_URI_SCHEME_IS_SIPS(request_uri)) {
+               char uri_str[PJSIP_MAX_URL_SIZE];
+
+               pjsip_uri_print(PJSIP_URI_IN_REQ_URI, request_uri, uri_str, sizeof(uri_str));
+               ast_log(LOG_WARNING, "Request URI '%s' is not a sip: or sips: URI.\n", uri_str);
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 416, NULL, NULL, NULL);
+               return NULL;
+       }
+
+       request_uri_sip = pjsip_uri_get_uri(request_uri);
+       resource_size = pj_strlen(&request_uri_sip->user) + 1;
+       resource_name = alloca(resource_size);
+       ast_copy_pj_str(resource_name, &request_uri_sip->user, resource_size);
+
+       resource = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "inbound-publication", resource_name);
+       if (!resource) {
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 404, NULL, NULL, NULL);
+               return NULL;
+       }
+
+       if (!ast_strlen_zero(resource->endpoint) && strcmp(resource->endpoint, ast_sorcery_object_get_id(endpoint))) {
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
+               return NULL;
+       }
+
+       for (event_configuration_name = resource->events; event_configuration_name; event_configuration_name = event_configuration_name->next) {
+               if (!strcmp(event_configuration_name->name, handler->event_name)) {
+                       break;
+               }
+       }
+
+       if (!event_configuration_name) {
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 404, NULL, NULL, NULL);
+               return NULL;
+       }
+
+       resp = handler->new_publication(endpoint, resource_name, event_configuration_name->value);
+
+       if (!PJSIP_IS_STATUS_IN_CLASS(resp, 200)) {
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, resp, NULL, NULL, NULL);
+               return NULL;
+       }
+
+       publication = sip_create_publication(endpoint, rdata, S_OR(resource_name, ""), event_configuration_name->value);
+
+       if (!publication) {
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 503, NULL, NULL, NULL);
+               return NULL;
+       }
+
+       publication->handler = handler;
+       if (publication->handler->publication_state_change(publication, rdata->msg_info.msg->body,
+                       AST_SIP_PUBLISH_STATE_INITIALIZED)) {
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
+               ao2_cleanup(publication);
+               return NULL;
+       }
+
+       sip_publication_respond(publication, resp, rdata);
+
+       return publication;
+}
+
+static int publish_expire_callback(void *data)
+{
+       RAII_VAR(struct ast_sip_publication *, publication, data, ao2_cleanup);
+
+       if (publication->handler->publish_expire) {
+               publication->handler->publish_expire(publication);
+       }
+
+       return 0;
+}
+
+static int publish_expire(const void *data)
+{
+       struct ast_sip_publication *publication = (struct ast_sip_publication*)data;
+
+       ao2_unlink(publication->handler->publications, publication);
+       publication->sched_id = -1;
+
+       if (ast_sip_push_task(NULL, publish_expire_callback, publication)) {
+               ao2_cleanup(publication);
+       }
+
+       return 0;
+}
+
+static pj_bool_t pubsub_on_rx_publish_request(pjsip_rx_data *rdata)
+{
+       pjsip_event_hdr *event_header;
+       struct ast_sip_publish_handler *handler;
+       RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
+       char event[32];
+       static const pj_str_t str_sip_if_match = { "SIP-If-Match", 12 };
+       pjsip_generic_string_hdr *etag_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_sip_if_match, NULL);
+       enum sip_publish_type publish_type;
+       RAII_VAR(struct ast_sip_publication *, publication, NULL, ao2_cleanup);
+       int expires = 0, entity_id, response = 0;
+
+       endpoint = ast_pjsip_rdata_get_endpoint(rdata);
+       ast_assert(endpoint != NULL);
+
+       event_header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_event_name, rdata->msg_info.msg->hdr.next);
+       if (!event_header) {
+               ast_log(LOG_WARNING, "Incoming PUBLISH request with no Event header\n");
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
+               return PJ_TRUE;
+       }
+       ast_copy_pj_str(event, &event_header->event_type, sizeof(event));
+
+       handler = find_pub_handler(event);
+       if (!handler) {
+               ast_log(LOG_WARNING, "No registered publish handler for event %s\n", event);
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
+               return PJ_TRUE;
+       }
+
+       publish_type = determine_sip_publish_type(rdata, etag_hdr, &expires, &entity_id);
+
+       /* If this is not an initial publish ensure that a publication is present */
+       if ((publish_type != SIP_PUBLISH_INITIAL) && (publish_type != SIP_PUBLISH_UNKNOWN)) {
+               if (!(publication = ao2_find(handler->publications, &entity_id, OBJ_KEY | OBJ_UNLINK))) {
+                       static const pj_str_t str_conditional_request_failed = { "Conditional Request Failed", 26 };
+
+                       pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 412, &str_conditional_request_failed,
+                               NULL, NULL);
+                       return PJ_TRUE;
+               }
+
+               /* Per the RFC every response has to have a new entity tag */
+               publication->entity_tag = ast_atomic_fetchadd_int(&esc_etag_counter, +1);
+
+               /* Update the expires here so that the created responses will contain the correct value */
+               publication->expires = expires;
+       }
+
+       switch (publish_type) {
+               case SIP_PUBLISH_INITIAL:
+                       publication = publish_request_initial(endpoint, rdata, handler);
+                       break;
+               case SIP_PUBLISH_REFRESH:
+               case SIP_PUBLISH_MODIFY:
+                       if (handler->publication_state_change(publication, rdata->msg_info.msg->body,
+                                               AST_SIP_PUBLISH_STATE_ACTIVE)) {
+                               /* If an error occurs we want to terminate the publication */
+                               expires = 0;
+                       }
+                       response = 200;
+                       break;
+               case SIP_PUBLISH_REMOVE:
+                       handler->publication_state_change(publication, rdata->msg_info.msg->body,
+                                       AST_SIP_PUBLISH_STATE_TERMINATED);
+                       response = 200;
+                       break;
+               case SIP_PUBLISH_UNKNOWN:
+               default:
+                       pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 400, NULL, NULL, NULL);
+                       break;
+       }
+
+       if (publication) {
+               if (expires) {
+                       ao2_link(handler->publications, publication);
+
+                       AST_SCHED_REPLACE_UNREF(publication->sched_id, sched, expires * 1000, publish_expire, publication,
+                                               ao2_ref(publication, -1), ao2_ref(publication, -1), ao2_ref(publication, +1));
+               } else {
+                       AST_SCHED_DEL_UNREF(sched, publication->sched_id, ao2_ref(publication, -1));
+               }
+       }
+
+       if (response) {
+               sip_publication_respond(publication, response, rdata);
+       }
 
-void ast_sip_publication_remove_datastore(struct ast_sip_publication *publication, const char *name)
-{
-       ao2_callback(publication->datastores, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, NULL, (void *) name);
+       return PJ_TRUE;
 }
 
-AST_RWLIST_HEAD_STATIC(publish_handlers, ast_sip_publish_handler);
-
-static int publication_hash_fn(const void *obj, const int flags)
+struct ast_sip_endpoint *ast_sip_publication_get_endpoint(struct ast_sip_publication *pub)
 {
-       const struct ast_sip_publication *publication = obj;
-       const int *entity_tag = obj;
-
-       return flags & OBJ_KEY ? *entity_tag : publication->entity_tag;
+       return pub->endpoint;
 }
 
-static int publication_cmp_fn(void *obj, void *arg, int flags)
+const char *ast_sip_publication_get_resource(const struct ast_sip_publication *pub)
 {
-       const struct ast_sip_publication *publication1 = obj;
-       const struct ast_sip_publication *publication2 = arg;
-       const int *entity_tag = arg;
-
-       return (publication1->entity_tag == (flags & OBJ_KEY ? *entity_tag : publication2->entity_tag) ?
-               CMP_MATCH | CMP_STOP : 0);
+       return pub->resource;
 }
 
-static void publish_add_handler(struct ast_sip_publish_handler *handler)
+const char *ast_sip_publication_get_event_configuration(const struct ast_sip_publication *pub)
 {
-       SCOPED_LOCK(lock, &publish_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
-       AST_RWLIST_INSERT_TAIL(&publish_handlers, handler, next);
+       return pub->event_configuration_name;
 }
 
-int ast_sip_register_publish_handler(struct ast_sip_publish_handler *handler)
+int ast_sip_pubsub_register_body_generator(struct ast_sip_pubsub_body_generator *generator)
 {
-       if (ast_strlen_zero(handler->event_name)) {
-               ast_log(LOG_ERROR, "No event package specified for publish handler. Cannot register\n");
-               return -1;
-       }
+       struct ast_sip_pubsub_body_generator *existing;
+       pj_str_t accept;
+       pj_size_t accept_len;
 
-       if (!(handler->publications = ao2_container_alloc(PUBLICATIONS_BUCKETS,
-               publication_hash_fn, publication_cmp_fn))) {
-               ast_log(LOG_ERROR, "Could not allocate publications container for event '%s'\n",
-                       handler->event_name);
+       existing = find_body_generator_type_subtype(generator->type, generator->subtype);
+       if (existing) {
+               ast_log(LOG_WARNING, "Cannot register body generator of %s/%s."
+                               "One is already registered.\n", generator->type, generator->subtype);
                return -1;
        }
 
-       publish_add_handler(handler);
+       AST_RWLIST_WRLOCK(&body_generators);
+       AST_LIST_INSERT_HEAD(&body_generators, generator, list);
+       AST_RWLIST_UNLOCK(&body_generators);
 
-       ast_module_ref(ast_module_info->self);
+       /* Lengths of type and subtype plus space for a slash. pj_str_t is not
+        * null-terminated, so there is no need to allocate for the extra null
+        * byte
+        */
+       accept_len = strlen(generator->type) + strlen(generator->subtype) + 1;
+
+       accept.ptr = alloca(accept_len);
+       accept.slen = accept_len;
+       /* Safe use of sprintf */
+       sprintf(accept.ptr, "%s/%s", generator->type, generator->subtype);
+       pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), &pubsub_module,
+                       PJSIP_H_ACCEPT, NULL, 1, &accept);
 
        return 0;
 }
 
-void ast_sip_unregister_publish_handler(struct ast_sip_publish_handler *handler)
+void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator)
 {
-       struct ast_sip_publish_handler *iter;
-       SCOPED_LOCK(lock, &publish_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
-       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&publish_handlers, iter, next) {
-               if (handler == iter) {
-                       AST_RWLIST_REMOVE_CURRENT(next);
-                       ao2_cleanup(handler->publications);
-                       ast_module_unref(ast_module_info->self);
+       struct ast_sip_pubsub_body_generator *iter;
+       SCOPED_LOCK(lock, &body_generators, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
+
+       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&body_generators, iter, list) {
+               if (iter == generator) {
+                       AST_LIST_REMOVE_CURRENT(list);
                        break;
                }
        }
        AST_RWLIST_TRAVERSE_SAFE_END;
 }
 
-AST_RWLIST_HEAD_STATIC(subscription_handlers, ast_sip_subscription_handler);
-
-static void sub_add_handler(struct ast_sip_subscription_handler *handler)
+int ast_sip_pubsub_register_body_supplement(struct ast_sip_pubsub_body_supplement *supplement)
 {
-       SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
-       AST_RWLIST_INSERT_TAIL(&subscription_handlers, handler, next);
-       ast_module_ref(ast_module_info->self);
+       AST_RWLIST_WRLOCK(&body_supplements);
+       AST_RWLIST_INSERT_TAIL(&body_supplements, supplement, list);
+       AST_RWLIST_UNLOCK(&body_supplements);
+
+       return 0;
 }
 
-static struct ast_sip_subscription_handler *find_sub_handler_for_event_name(const char *event_name)
+void ast_sip_pubsub_unregister_body_supplement(struct ast_sip_pubsub_body_supplement *supplement)
 {
-       struct ast_sip_subscription_handler *iter;
-       SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
+       struct ast_sip_pubsub_body_supplement *iter;
+       SCOPED_LOCK(lock, &body_supplements, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
 
-       AST_RWLIST_TRAVERSE(&subscription_handlers, iter, next) {
-               if (!strcmp(iter->event_name, event_name)) {
+       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&body_supplements, iter, list) {
+               if (iter == supplement) {
+                       AST_LIST_REMOVE_CURRENT(list);
                        break;
                }
        }
-       return iter;
+       AST_RWLIST_TRAVERSE_SAFE_END;
 }
 
-int ast_sip_register_subscription_handler(struct ast_sip_subscription_handler *handler)
+const char *ast_sip_subscription_get_body_type(struct ast_sip_subscription *sub)
 {
-       pj_str_t event;
-       pj_str_t accept[AST_SIP_MAX_ACCEPT] = { {0, }, };
-       struct ast_sip_subscription_handler *existing;
-       int i = 0;
+       return sub->body_generator->type;
+}
 
-       if (ast_strlen_zero(handler->event_name)) {
-               ast_log(LOG_ERROR, "No event package specified for subscription handler. Cannot register\n");
+const char *ast_sip_subscription_get_body_subtype(struct ast_sip_subscription *sub)
+{
+       return sub->body_generator->subtype;
+}
+
+int ast_sip_pubsub_generate_body_content(const char *type, const char *subtype,
+               void *data, struct ast_str **str)
+{
+       struct ast_sip_pubsub_body_supplement *supplement;
+       struct ast_sip_pubsub_body_generator *generator;
+       int res = 0;
+       void *body;
+
+       generator = find_body_generator_type_subtype(type, subtype);
+       if (!generator) {
+               ast_log(LOG_WARNING, "Unable to find a body generator for %s/%s\n",
+                               type, subtype);
                return -1;
        }
 
-       existing = find_sub_handler_for_event_name(handler->event_name);
-       if (existing) {
-               ast_log(LOG_ERROR, "Unable to register subscription handler for event %s."
-                               "A handler is already registered\n", handler->event_name);
+       body = generator->allocate_body(data);
+       if (!body) {
+               ast_log(LOG_WARNING, "Unable to allocate a NOTIFY body of type %s/%s\n",
+                               type, subtype);
                return -1;
        }
 
-       for (i = 0; i < AST_SIP_MAX_ACCEPT && !ast_strlen_zero(handler->accept[i]); ++i) {
-               pj_cstr(&accept[i], handler->accept[i]);
+       if (generator->generate_body_content(body, data)) {
+               res = -1;
+               goto end;
        }
 
-       pj_cstr(&event, handler->event_name);
-
-       pjsip_evsub_register_pkg(&pubsub_module, &event, DEFAULT_EXPIRES, i, accept);
-
-       sub_add_handler(handler);
-
-       return 0;
-}
-
-void ast_sip_unregister_subscription_handler(struct ast_sip_subscription_handler *handler)
-{
-       struct ast_sip_subscription_handler *iter;
-       SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
-       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&subscription_handlers, iter, next) {
-               if (handler == iter) {
-                       AST_RWLIST_REMOVE_CURRENT(next);
-                       ast_module_unref(ast_module_info->self);
-                       break;
+       AST_RWLIST_RDLOCK(&body_supplements);
+       AST_RWLIST_TRAVERSE(&body_supplements, supplement, list) {
+               if (!strcmp(generator->type, supplement->type) &&
+                               !strcmp(generator->subtype, supplement->subtype)) {
+                       res = supplement->supplement_body(body, data);
+                       if (res) {
+                               break;
+                       }
                }
        }
-       AST_RWLIST_TRAVERSE_SAFE_END;
-}
-
-static struct ast_sip_pubsub_body_generator *find_body_generator_type_subtype(const char *content_type,
-               const char *content_subtype)
-{
-       struct ast_sip_pubsub_body_generator *iter;
-       SCOPED_LOCK(lock, &body_generators, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
-
-       AST_LIST_TRAVERSE(&body_generators, iter, list) {
-               if (!strcmp(iter->type, content_type) &&
-                               !strcmp(iter->subtype, content_subtype)) {
-                       break;
-               }
-       };
-
-       return iter;
-}
+       AST_RWLIST_UNLOCK(&body_supplements);
 
-static struct ast_sip_pubsub_body_generator *find_body_generator_accept(const char *accept)
-{
-       char *accept_copy = ast_strdupa(accept);
-       char *subtype = accept_copy;
-       char *type = strsep(&subtype, "/");
+       if (!res) {
+               generator->to_string(body, str);
+       }
 
-       if (ast_strlen_zero(type) || ast_strlen_zero(subtype)) {
-               return NULL;
+end:
+       if (generator->destroy_body) {
+               generator->destroy_body(body);
        }
 
-       return find_body_generator_type_subtype(type, subtype);
+       return res;
 }
 
-static struct ast_sip_pubsub_body_generator *find_body_generator(char accept[AST_SIP_MAX_ACCEPT][64],
-               size_t num_accept)
+static pj_bool_t pubsub_on_rx_request(pjsip_rx_data *rdata)
 {
-       int i;
-       struct ast_sip_pubsub_body_generator *generator = NULL;
-
-       for (i = 0; i < num_accept; ++i) {
-               generator = find_body_generator_accept(accept[i]);
-               if (generator) {
-                       ast_debug(3, "Body generator %p found for accept type %s\n", generator, accept[i]);
-                       break;
-               } else {
-                       ast_debug(3, "No body generator found for accept type %s\n", accept[i]);
-               }
+       if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, pjsip_get_subscribe_method())) {
+               return pubsub_on_rx_subscribe_request(rdata);
+       } else if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_publish_method)) {
+               return pubsub_on_rx_publish_request(rdata);
        }
 
-       return generator;
+       return PJ_FALSE;
 }
 
-static pj_bool_t pubsub_on_rx_subscribe_request(pjsip_rx_data *rdata)
+static void pubsub_on_evsub_state(pjsip_evsub *evsub, pjsip_event *event)
 {
-       pjsip_expires_hdr *expires_header;
-       struct ast_sip_subscription_handler *handler;
-       RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
-       struct ast_sip_subscription *sub;
-       struct ast_sip_pubsub_body_generator *generator;
-       char *resource;
-       pjsip_uri *request_uri;
-       pjsip_sip_uri *request_uri_sip;
-       size_t resource_size;
-       int resp;
+       struct sip_subscription_tree *sub_tree;
 
-       endpoint = ast_pjsip_rdata_get_endpoint(rdata);
-       ast_assert(endpoint != NULL);
+       if (pjsip_evsub_get_state(evsub) != PJSIP_EVSUB_STATE_TERMINATED) {
+               return;
+       }
 
-       if (!endpoint->subscription.allow) {
-               ast_log(LOG_WARNING, "Subscriptions not permitted for endpoint %s.\n", ast_sorcery_object_get_id(endpoint));
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 603, NULL, NULL, NULL);
-               return PJ_TRUE;
+       sub_tree = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
+       if (!sub_tree) {
+               return;
        }
 
-       request_uri = rdata->msg_info.msg->line.req.uri;
+       ao2_cleanup(sub_tree);
 
-       if (!PJSIP_URI_SCHEME_IS_SIP(request_uri) && !PJSIP_URI_SCHEME_IS_SIPS(request_uri)) {
-               char uri_str[PJSIP_MAX_URL_SIZE];
+       pjsip_evsub_set_mod_data(evsub, pubsub_module.id, NULL);
+}
 
-               pjsip_uri_print(PJSIP_URI_IN_REQ_URI, request_uri, uri_str, sizeof(uri_str));
-               ast_log(LOG_WARNING, "Request URI '%s' is not a sip: or sips: URI.\n", uri_str);
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 416, NULL, NULL, NULL);
-               return PJ_TRUE;
-       }
+static void set_state_terminated(struct ast_sip_subscription *sub)
+{
+       int i;
 
-       request_uri_sip = pjsip_uri_get_uri(request_uri);
-       resource_size = pj_strlen(&request_uri_sip->user) + 1;
-       resource = alloca(resource_size);
-       ast_copy_pj_str(resource, &request_uri_sip->user, resource_size);
+       sub->subscription_state = PJSIP_EVSUB_STATE_TERMINATED;
+       for (i = 0; i < AST_VECTOR_SIZE(&sub->children); ++i) {
+               set_state_terminated(AST_VECTOR_GET(&sub->children, i));
+       }
+}
 
-       expires_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, rdata->msg_info.msg->hdr.next);
+static void pubsub_on_rx_refresh(pjsip_evsub *evsub, pjsip_rx_data *rdata,
+               int *p_st_code, pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body)
+{
+       struct sip_subscription_tree *sub_tree;
 
-       if (expires_header) {
-               if (expires_header->ivalue == 0) {
-                       ast_log(LOG_WARNING, "Susbscription request from endpoint %s rejected. Expiration of 0 is invalid\n",
-                               ast_sorcery_object_get_id(endpoint));
-                       pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 400, NULL, NULL, NULL);
-                               return PJ_TRUE;
-               }
-               if (expires_header->ivalue < endpoint->subscription.minexpiry) {
-                       ast_log(LOG_WARNING, "Subscription expiration %d is too brief for endpoint %s. Minimum is %u\n",
-                               expires_header->ivalue, ast_sorcery_object_get_id(endpoint), endpoint->subscription.minexpiry);
-                       pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 423, NULL, NULL, NULL);
-                       return PJ_TRUE;
-               }
+       sub_tree = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
+       if (!sub_tree) {
+               return;
        }
 
-       handler = subscription_get_handler_from_rdata(rdata);
-       if (!handler) {
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
-               return PJ_TRUE;
+       /* If sending a NOTIFY to terminate a subscription, then pubsub_on_evsub_state()
+        * will be called when we send the NOTIFY, and that will result in dropping the
+        * refcount of sub_tree by one, and possibly destroying the sub_tree. We need to
+        * hold a reference to the sub_tree until this function returns so that we don't
+        * try to read from or write to freed memory by accident
+        */
+       ao2_ref(sub_tree, +1);
+
+       if (pjsip_evsub_get_state(evsub) == PJSIP_EVSUB_STATE_TERMINATED) {
+               set_state_terminated(sub_tree->root);
        }
 
-       generator = subscription_get_generator_from_rdata(rdata, handler);
-       if (!generator) {
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
-               return PJ_TRUE;
+       if (send_notify(sub_tree, 1)) {
+               *p_st_code = 500;
        }
 
-       resp = handler->notifier->new_subscribe(endpoint, resource);
-       if (!PJSIP_IS_STATUS_IN_CLASS(resp, 200)) {
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, resp, NULL, NULL, NULL);
-               return PJ_TRUE;
+       if (sub_tree->is_list) {
+               pj_list_insert_before(res_hdr, create_require_eventlist(rdata->tp_info.pool));
        }
 
-       sub = notifier_create_subscription(handler, endpoint, rdata, resource, generator);
+       ao2_ref(sub_tree, -1);
+}
+
+static void pubsub_on_rx_notify(pjsip_evsub *evsub, pjsip_rx_data *rdata, int *p_st_code,
+               pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body)
+{
+       struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
+
        if (!sub) {
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
-       } else {
-               sub->persistence = subscription_persistence_create(sub);
-               subscription_persistence_update(sub, rdata);
-               sip_subscription_accept(sub, rdata, resp);
-               if (handler->notifier->notify_required(sub, AST_SIP_SUBSCRIPTION_NOTIFY_REASON_STARTED)) {
-                       pjsip_evsub_terminate(sip_subscription_get_evsub(sub), PJ_TRUE);
-               }
+               return;
        }
 
-       return PJ_TRUE;
+       sub->handler->subscriber->state_change(sub, rdata->msg_info.msg->body,
+                       pjsip_evsub_get_state(evsub));
 }
 
-static struct ast_sip_publish_handler *find_pub_handler(const char *event)
+static int serialized_pubsub_on_client_refresh(void *userdata)
 {
-       struct ast_sip_publish_handler *iter = NULL;
-       SCOPED_LOCK(lock, &publish_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
+       struct sip_subscription_tree *sub_tree = userdata;
+       pjsip_tx_data *tdata;
 
-       AST_RWLIST_TRAVERSE(&publish_handlers, iter, next) {
-               if (strcmp(event, iter->event_name)) {
-                       ast_debug(3, "Event %s does not match %s\n", event, iter->event_name);
-                       continue;
-               }
-               ast_debug(3, "Event name match: %s = %s\n", event, iter->event_name);
-               break;
+       if (pjsip_evsub_initiate(sub_tree->evsub, NULL, -1, &tdata) == PJ_SUCCESS) {
+               pjsip_evsub_send_request(sub_tree->evsub, tdata);
+       } else {
+               pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE);
+               return 0;
        }
+       ao2_cleanup(sub_tree);
+       return 0;
+}
 
-       return iter;
+static void pubsub_on_client_refresh(pjsip_evsub *evsub)
+{
+       struct sip_subscription_tree *sub_tree = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
+
+       ao2_ref(sub_tree, +1);
+       ast_sip_push_task(sub_tree->serializer, serialized_pubsub_on_client_refresh, sub_tree);
 }
 
-static enum sip_publish_type determine_sip_publish_type(pjsip_rx_data *rdata,
-       pjsip_generic_string_hdr *etag_hdr, int *expires, int *entity_id)
+static int serialized_pubsub_on_server_timeout(void *userdata)
 {
-       pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
+       struct sip_subscription_tree *sub_tree = userdata;
 
-       if (etag_hdr) {
-               char etag[pj_strlen(&etag_hdr->hvalue) + 1];
+       set_state_terminated(sub_tree->root);
+       send_notify(sub_tree, 1);
 
-               ast_copy_pj_str(etag, &etag_hdr->hvalue, sizeof(etag));
+       ao2_cleanup(sub_tree);
+       return 0;
+}
 
-               if (sscanf(etag, "%30d", entity_id) != 1) {
-                       return SIP_PUBLISH_UNKNOWN;
-               }
+static void pubsub_on_server_timeout(pjsip_evsub *evsub)
+{
+       struct sip_subscription_tree *sub_tree = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
+
+       if (!sub_tree) {
+               /* if a subscription has been terminated and the subscription
+                  timeout/expires is less than the time it takes for all pending
+                  transactions to end then the subscription timer will not have
+                  been canceled yet and sub will be null, so do nothing since
+                  the subscription has already been terminated. */
+               return;
        }
 
-       *expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES;
+       ao2_ref(sub_tree, +1);
+       ast_sip_push_task(sub_tree->serializer, serialized_pubsub_on_server_timeout, sub_tree);
+}
 
-       if (!(*expires)) {
-               return SIP_PUBLISH_REMOVE;
-       } else if (!etag_hdr && rdata->msg_info.msg->body) {
-               return SIP_PUBLISH_INITIAL;
-       } else if (etag_hdr && !rdata->msg_info.msg->body) {
-               return SIP_PUBLISH_REFRESH;
-       } else if (etag_hdr && rdata->msg_info.msg->body) {
-               return SIP_PUBLISH_MODIFY;
+static int ami_subscription_detail(struct sip_subscription_tree *sub_tree,
+                                  struct ast_sip_ami *ami,
+                                  const char *event)
+{
+       RAII_VAR(struct ast_str *, buf,
+                ast_sip_create_ami_event(event, ami), ast_free);
+
+       if (!buf) {
+               return -1;
        }
 
-       return SIP_PUBLISH_UNKNOWN;
+       sip_subscription_to_ami(sub_tree, &buf);
+       astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
+       return 0;
 }
 
-/*! \brief Internal destructor for publications */
-static void publication_destroy_fn(void *obj)
+static int ami_subscription_detail_inbound(struct sip_subscription_tree *sub_tree, void *arg)
 {
-       struct ast_sip_publication *publication = obj;
+       return sub_tree->role == AST_SIP_NOTIFIER ? ami_subscription_detail(
+               sub_tree, arg, "InboundSubscriptionDetail") : 0;
+}
 
-       ast_debug(3, "Destroying SIP publication\n");
+static int ami_subscription_detail_outbound(struct sip_subscription_tree *sub_tree, void *arg)
+{
+       return sub_tree->role == AST_SIP_SUBSCRIBER ? ami_subscription_detail(
+               sub_tree, arg, "OutboundSubscriptionDetail") : 0;
+}
 
-       ao2_cleanup(publication->datastores);
-       ao2_cleanup(publication->endpoint);
+static int ami_show_subscriptions_inbound(struct mansession *s, const struct message *m)
+{
+       struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
+       int num;
+
+       astman_send_listack(s, m, "Following are Events for "
+                           "each inbound Subscription", "start");
+
+       num = for_each_subscription(ami_subscription_detail_inbound, &ami);
+
+       astman_append(s, "Event: InboundSubscriptionDetailComplete\r\n");
+       if (!ast_strlen_zero(ami.action_id)) {
+               astman_append(s, "ActionID: %s\r\n", ami.action_id);
+       }
+       astman_append(s, "EventList: Complete\r\n"
+                     "ListItems: %d\r\n\r\n", num);
+       return 0;
 }
 
-static struct ast_sip_publication *sip_create_publication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata,
-       const char *resource, const char *event_configuration_name)
+static int ami_show_subscriptions_outbound(struct mansession *s, const struct message *m)
 {
-       struct ast_sip_publication *publication;
-       pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
-       size_t resource_len = strlen(resource) + 1, event_configuration_name_len = strlen(event_configuration_name) + 1;
-       char *dst;
+       struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
+       int num;
 
-       ast_assert(endpoint != NULL);
+       astman_send_listack(s, m, "Following are Events for "
+                           "each outbound Subscription", "start");
 
-       if (!(publication = ao2_alloc(sizeof(*publication) + resource_len + event_configuration_name_len, publication_destroy_fn))) {
-               return NULL;
+       num = for_each_subscription(ami_subscription_detail_outbound, &ami);
+
+       astman_append(s, "Event: OutboundSubscriptionDetailComplete\r\n");
+       if (!ast_strlen_zero(ami.action_id)) {
+               astman_append(s, "ActionID: %s\r\n", ami.action_id);
        }
+       astman_append(s, "EventList: Complete\r\n"
+                     "ListItems: %d\r\n\r\n", num);
+       return 0;
+}
 
-       if (!(publication->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp))) {
-               ao2_ref(publication, -1);
-               return NULL;
+static int format_ami_resource_lists(void *obj, void *arg, int flags)
+{
+       struct resource_list *list = obj;
+       struct ast_sip_ami *ami = arg;
+       struct ast_str *buf;
+
+       buf = ast_sip_create_ami_event("ResourceListDetail", ami);
+       if (!buf) {
+               return CMP_STOP;
        }
 
-       publication->entity_tag = ast_atomic_fetchadd_int(&esc_etag_counter, +1);
-       ao2_ref(endpoint, +1);
-       publication->endpoint = endpoint;
-       publication->expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES;
-       publication->sched_id = -1;
-       dst = publication->data;
-       publication->resource = strcpy(dst, resource);
-       dst += resource_len;
-       publication->event_configuration_name = strcpy(dst, event_configuration_name);
+       if (ast_sip_sorcery_object_to_ami(list, &buf)) {
+               ast_free(buf);
+               return CMP_STOP;
+       }
+       astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
 
-       return publication;
+       ast_free(buf);
+       return 0;
 }
 
-static int sip_publication_respond(struct ast_sip_publication *pub, int status_code,
-               pjsip_rx_data *rdata)
+static int ami_show_resource_lists(struct mansession *s, const struct message *m)
 {
-       pj_status_t status;
-       pjsip_tx_data *tdata;
-       pjsip_transaction *tsx;
+       struct ast_sip_ami ami = { .s = s, .m = m };
+       int num;
+       struct ao2_container *lists;
 
-       if (pjsip_endpt_create_response(ast_sip_get_pjsip_endpoint(), rdata, status_code, NULL, &tdata) != PJ_SUCCESS) {
-               return -1;
+       lists = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "resource_list",
+                       AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
+
+       if (!lists || !(num = ao2_container_count(lists))) {
+               astman_send_error(s, m, "No resource lists found\n");
+               return 0;
        }
 
-       if (PJSIP_IS_STATUS_IN_CLASS(status_code, 200)) {
-               RAII_VAR(char *, entity_tag, NULL, ast_free_ptr);
-               RAII_VAR(char *, expires, NULL, ast_free_ptr);
+       astman_send_listack(s, m, "A listing of resource lists follows, "
+                           "presented as ResourceListDetail events", "start");
 
-               if ((ast_asprintf(&entity_tag, "%d", pub->entity_tag) < 0) ||
-                       (ast_asprintf(&expires, "%d", pub->expires) < 0)) {
-                       pjsip_tx_data_dec_ref(tdata);
-                       return -1;
-               }
+       ao2_callback(lists, OBJ_NODATA, format_ami_resource_lists, &ami);
 
-               ast_sip_add_header(tdata, "SIP-ETag", entity_tag);
-               ast_sip_add_header(tdata, "Expires", expires);
-       }
+       astman_append(s,
+                     "Event: ResourceListDetailComplete\r\n"
+                     "EventList: Complete\r\n"
+                     "ListItems: %d\r\n\r\n", num);
+       return 0;
+}
 
-       if ((status = pjsip_tsx_create_uas(&pubsub_module, rdata, &tsx)) != PJ_SUCCESS) {
-               return -1;
-       }
+#define AMI_SHOW_SUBSCRIPTIONS_INBOUND "PJSIPShowSubscriptionsInbound"
+#define AMI_SHOW_SUBSCRIPTIONS_OUTBOUND "PJSIPShowSubscriptionsOutbound"
 
-       pjsip_tsx_recv_msg(tsx, rdata);
+static int persistence_endpoint_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
+{
+       struct subscription_persistence *persistence = obj;
 
-       if (pjsip_tsx_send_msg(tsx, tdata) != PJ_SUCCESS) {
-               return -1;
-       }
+       persistence->endpoint = ast_strdup(var->value);
+       return 0;
+}
+
+static int persistence_endpoint_struct2str(const void *obj, const intptr_t *args, char **buf)
+{
+       const struct subscription_persistence *persistence = obj;
 
+       *buf = ast_strdup(persistence->endpoint);
        return 0;
 }
 
-static struct ast_sip_publication *publish_request_initial(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata,
-       struct ast_sip_publish_handler *handler)
+static int persistence_tag_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
 {
-       struct ast_sip_publication *publication;
-       char *resource_name;
-       size_t resource_size;
-       RAII_VAR(struct ast_sip_publication_resource *, resource, NULL, ao2_cleanup);
-       struct ast_variable *event_configuration_name = NULL;
-       pjsip_uri *request_uri;
-       pjsip_sip_uri *request_uri_sip;
-       int resp;
+       struct subscription_persistence *persistence = obj;
 
-       request_uri = rdata->msg_info.msg->line.req.uri;
+       persistence->tag = ast_strdup(var->value);
+       return 0;
+}
+
+static int persistence_tag_struct2str(const void *obj, const intptr_t *args, char **buf)
+{
+       const struct subscription_persistence *persistence = obj;
+
+       *buf = ast_strdup(persistence->tag);
+       return 0;
+}
+
+static int persistence_expires_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
+{
+       struct subscription_persistence *persistence = obj;
+       return ast_get_timeval(var->value, &persistence->expires, ast_tv(0, 0), NULL);
+}
 
-       if (!PJSIP_URI_SCHEME_IS_SIP(request_uri) && !PJSIP_URI_SCHEME_IS_SIPS(request_uri)) {
-               char uri_str[PJSIP_MAX_URL_SIZE];
+static int persistence_expires_struct2str(const void *obj, const intptr_t *args, char **buf)
+{
+       const struct subscription_persistence *persistence = obj;
+       return (ast_asprintf(buf, "%ld", persistence->expires.tv_sec) < 0) ? -1 : 0;
+}
 
-               pjsip_uri_print(PJSIP_URI_IN_REQ_URI, request_uri, uri_str, sizeof(uri_str));
-               ast_log(LOG_WARNING, "Request URI '%s' is not a sip: or sips: URI.\n", uri_str);
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 416, NULL, NULL, NULL);
-               return NULL;
-       }
+#define RESOURCE_LIST_INIT_SIZE 4
 
-       request_uri_sip = pjsip_uri_get_uri(request_uri);
-       resource_size = pj_strlen(&request_uri_sip->user) + 1;
-       resource_name = alloca(resource_size);
-       ast_copy_pj_str(resource_name, &request_uri_sip->user, resource_size);
+static void resource_list_destructor(void *obj)
+{
+       struct resource_list *list = obj;
+       int i;
 
-       resource = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "inbound-publication", resource_name);
-       if (!resource) {
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 404, NULL, NULL, NULL);
-               return NULL;
+       for (i = 0; i < AST_VECTOR_SIZE(&list->items); ++i) {
+               ast_free((char *) AST_VECTOR_GET(&list->items, i));
        }
 
-       if (!ast_strlen_zero(resource->endpoint) && strcmp(resource->endpoint, ast_sorcery_object_get_id(endpoint))) {
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
-               return NULL;
-       }
+       AST_VECTOR_FREE(&list->items);
+}
 
-       for (event_configuration_name = resource->events; event_configuration_name; event_configuration_name = event_configuration_name->next) {
-               if (!strcmp(event_configuration_name->name, handler->event_name)) {
-                       break;
-               }
-       }
+static void *resource_list_alloc(const char *name)
+{
+       struct resource_list *list;
 
-       if (!event_configuration_name) {
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 404, NULL, NULL, NULL);
+       list = ast_sorcery_generic_alloc(sizeof(*list), resource_list_destructor);
+       if (!list) {
                return NULL;
        }
 
-       resp = handler->new_publication(endpoint, resource_name, event_configuration_name->value);
-
-       if (!PJSIP_IS_STATUS_IN_CLASS(resp, 200)) {
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, resp, NULL, NULL, NULL);
+       if (AST_VECTOR_INIT(&list->items, RESOURCE_LIST_INIT_SIZE)) {
+               ao2_cleanup(list);
                return NULL;
        }
 
-       publication = sip_create_publication(endpoint, rdata, S_OR(resource_name, ""), event_configuration_name->value);
+       return list;
+}
 
-       if (!publication) {
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 503, NULL, NULL, NULL);
-               return NULL;
-       }
+static int item_in_vector(const struct resource_list *list, const char *item)
+{
+       int i;
 
-       publication->handler = handler;
-       if (publication->handler->publication_state_change(publication, rdata->msg_info.msg->body,
-                       AST_SIP_PUBLISH_STATE_INITIALIZED)) {
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
-               ao2_cleanup(publication);
-               return NULL;
+       for (i = 0; i < AST_VECTOR_SIZE(&list->items); ++i) {
+               if (!strcmp(item, AST_VECTOR_GET(&list->items, i))) {
+                       return 1;
+               }
        }
 
-       sip_publication_respond(publication, resp, rdata);
-
-       return publication;
+       return 0;
 }
 
-static int publish_expire_callback(void *data)
+static int list_item_handler(const struct aco_option *opt,
+               struct ast_variable *var, void *obj)
 {
-       RAII_VAR(struct ast_sip_publication *, publication, data, ao2_cleanup);
+       struct resource_list *list = obj;
+       char *items = ast_strdupa(var->value);
+       char *item;
 
-       if (publication->handler->publish_expire) {
-               publication->handler->publish_expire(publication);
+       while ((item = strsep(&items, ","))) {
+               if (item_in_vector(list, item)) {
+                       ast_log(LOG_WARNING, "Ignoring duplicated list item '%s'\n", item);
+                       continue;
+               }
+               if (AST_VECTOR_APPEND(&list->items, ast_strdup(item))) {
+                       return -1;
+               }
        }
 
        return 0;
 }
 
-static int publish_expire(const void *data)
+static int list_item_to_str(const void *obj, const intptr_t *args, char **buf)
 {
-       struct ast_sip_publication *publication = (struct ast_sip_publication*)data;
-
-       ao2_unlink(publication->handler->publications, publication);
-       publication->sched_id = -1;
+       const struct resource_list *list = obj;
+       int i;
+       struct ast_str *str = ast_str_create(32);
 
-       if (ast_sip_push_task(NULL, publish_expire_callback, publication)) {
-               ao2_cleanup(publication);
+       for (i = 0; i < AST_VECTOR_SIZE(&list->items); ++i) {
+               ast_str_append(&str, 0, "%s,", AST_VECTOR_GET(&list->items, i));
        }
 
+       /* Chop off trailing comma */
+       ast_str_truncate(str, -1);
+       *buf = ast_strdup(ast_str_buffer(str));
+       ast_free(str);
        return 0;
 }
 
-static pj_bool_t pubsub_on_rx_publish_request(pjsip_rx_data *rdata)
+static int resource_list_apply_handler(const struct ast_sorcery *sorcery, void *obj)
 {
-       pjsip_event_hdr *event_header;
-       struct ast_sip_publish_handler *handler;
-       RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
-       char event[32];
-       static const pj_str_t str_sip_if_match = { "SIP-If-Match", 12 };
-       pjsip_generic_string_hdr *etag_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_sip_if_match, NULL);
-       enum sip_publish_type publish_type;
-       RAII_VAR(struct ast_sip_publication *, publication, NULL, ao2_cleanup);
-       int expires = 0, entity_id, response = 0;
+       struct resource_list *list = obj;
 
-       endpoint = ast_pjsip_rdata_get_endpoint(rdata);
-       ast_assert(endpoint != NULL);
-
-       event_header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_event_name, rdata->msg_info.msg->hdr.next);
-       if (!event_header) {
-               ast_log(LOG_WARNING, "Incoming PUBLISH request with no Event header\n");
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
-               return PJ_TRUE;
+       if (ast_strlen_zero(list->event)) {
+               ast_log(LOG_WARNING, "Resource list '%s' has no event set\n",
+                               ast_sorcery_object_get_id(list));
+               return -1;
        }
-       ast_copy_pj_str(event, &event_header->event_type, sizeof(event));
 
-       handler = find_pub_handler(event);
-       if (!handler) {
-               ast_log(LOG_WARNING, "No registered publish handler for event %s\n", event);
-               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
-               return PJ_TRUE;
+       if (AST_VECTOR_SIZE(&list->items) == 0) {
+               ast_log(LOG_WARNING, "Resource list '%s' has no list items\n",
+                               ast_sorcery_object_get_id(list));
+               return -1;
        }
 
-       publish_type = determine_sip_publish_type(rdata, etag_hdr, &expires, &entity_id);
+       return 0;
+}
 
-       /* If this is not an initial publish ensure that a publication is present */
-       if ((publish_type != SIP_PUBLISH_INITIAL) && (publish_type != SIP_PUBLISH_UNKNOWN)) {
-               if (!(publication = ao2_find(handler->publications, &entity_id, OBJ_KEY | OBJ_UNLINK))) {
-                       static const pj_str_t str_conditional_request_failed = { "Conditional Request Failed", 26 };
+static int apply_list_configuration(struct ast_sorcery *sorcery)
+{
+       ast_sorcery_apply_default(sorcery, "resource_list", "config",
+                       "pjsip.conf,criteria=type=resource_list");
+       if (ast_sorcery_object_register(sorcery, "resource_list", resource_list_alloc,
+                               NULL, resource_list_apply_handler)) {
+               return -1;
+       }
 
-                       pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 412, &str_conditional_request_failed,
-                               NULL, NULL);
-                       return PJ_TRUE;
-               }
+       ast_sorcery_object_field_register(sorcery, "resource_list", "type", "",
+                       OPT_NOOP_T, 0, 0);
+       ast_sorcery_object_field_register(sorcery, "resource_list", "event", "",
+                       OPT_CHAR_ARRAY_T, 1, CHARFLDSET(struct resource_list, event));
+       ast_sorcery_object_field_register(sorcery, "resource_list", "full_state", "no",
+                       OPT_BOOL_T, 1, FLDSET(struct resource_list, full_state));
+       ast_sorcery_object_field_register(sorcery, "resource_list", "notification_batch_interval",
+                       "0", OPT_UINT_T, 0, FLDSET(struct resource_list, notification_batch_interval));
+       ast_sorcery_object_field_register_custom(sorcery, "resource_list", "list_item",
+                       "", list_item_handler, list_item_to_str, NULL, 0, 0);
 
-               /* Per the RFC every response has to have a new entity tag */
-               publication->entity_tag = ast_atomic_fetchadd_int(&esc_etag_counter, +1);
+       ast_sorcery_reload_object(sorcery, "resource_list");
 
-               /* Update the expires here so that the created responses will contain the correct value */
-               publication->expires = expires;
-       }
+       return 0;
+}
 
-       switch (publish_type) {
-               case SIP_PUBLISH_INITIAL:
-                       publication = publish_request_initial(endpoint, rdata, handler);
-                       break;
-               case SIP_PUBLISH_REFRESH:
-               case SIP_PUBLISH_MODIFY:
-                       if (handler->publication_state_change(publication, rdata->msg_info.msg->body,
-                                               AST_SIP_PUBLISH_STATE_ACTIVE)) {
-                               /* If an error occurs we want to terminate the publication */
-                               expires = 0;
-                       }
-                       response = 200;
-                       break;
-               case SIP_PUBLISH_REMOVE:
-                       handler->publication_state_change(publication, rdata->msg_info.msg->body,
-                                       AST_SIP_PUBLISH_STATE_TERMINATED);
-                       response = 200;
-                       break;
-               case SIP_PUBLISH_UNKNOWN:
-               default:
-                       pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 400, NULL, NULL, NULL);
-                       break;
-       }
+#ifdef TEST_FRAMEWORK
 
-       if (publication) {
-               if (expires) {
-                       ao2_link(handler->publications, publication);
+/*!
+ * \brief "bad" resources
+ *
+ * These are resources that the test handler will reject subscriptions to.
+ */
+const char *bad_resources[] = {
+       "coconut",
+       "cilantro",
+       "olive",
+       "cheese",
+};
 
-                       AST_SCHED_REPLACE_UNREF(publication->sched_id, sched, expires * 1000, publish_expire, publication,
-                                               ao2_ref(publication, -1), ao2_ref(publication, -1), ao2_ref(publication, +1));
-               } else {
-                       AST_SCHED_DEL_UNREF(sched, publication->sched_id, ao2_ref(publication, -1));
-               }
-       }
+/*!
+ * \brief new_subscribe callback for unit tests
+ *
+ * Will give a 200 OK response to any resource except the "bad" ones.
+ */
+static int test_new_subscribe(struct ast_sip_endpoint *endpoint, const char *resource)
+{
+       int i;
 
-       if (response) {
-               sip_publication_respond(publication, response, rdata);
+       for (i = 0; i < ARRAY_LEN(bad_resources); ++i) {
+               if (!strcmp(resource, bad_resources[i])) {
+                       return 400;
+               }
        }
 
-       return PJ_TRUE;
+       return 200;
 }
 
-struct ast_sip_endpoint *ast_sip_publication_get_endpoint(struct ast_sip_publication *pub)
-{
-       return pub->endpoint;
-}
+/*!
+ * \brief Subscription notifier for unit tests.
+ *
+ * Since unit tests are only concerned with building a resource tree,
+ * only the new_subscribe callback needs to be defined.
+ */
+struct ast_sip_notifier test_notifier = {
+       .new_subscribe = test_new_subscribe,
+};
 
-const char *ast_sip_publication_get_resource(const struct ast_sip_publication *pub)
-{
-       return pub->resource;
-}
+/*!
+ * \brief Subscription handler for unit tests.
+ */
+struct ast_sip_subscription_handler test_handler = {
+       .event_name = "test",
+       .notifier = &test_notifier,
+};
 
-const char *ast_sip_publication_get_event_configuration(const struct ast_sip_publication *pub)
+/*!
+ * \brief Set properties on an allocated resource list
+ *
+ * \param list The list to set details on.
+ * \param event The list's event.
+ * \param resources Array of resources to add to the list.
+ * \param num_resources Number of resources in the array.
+ * \retval 0 Success
+ * \retval non-zero Failure
+ */
+static int populate_list(struct resource_list *list, const char *event, const char **resources, size_t num_resources)
 {
-       return pub->event_configuration_name;
-}
+       int i;
 
-int ast_sip_pubsub_register_body_generator(struct ast_sip_pubsub_body_generator *generator)
-{
-       struct ast_sip_pubsub_body_generator *existing;
-       pj_str_t accept;
-       pj_size_t accept_len;
+       ast_copy_string(list->event, event, sizeof(list->event));
 
-       existing = find_body_generator_type_subtype(generator->type, generator->subtype);
-       if (existing) {
-               ast_log(LOG_WARNING, "Cannot register body generator of %s/%s."
-                               "One is already registered.\n", generator->type, generator->subtype);
-               return -1;
+       for (i = 0; i < num_resources; ++i) {
+               if (AST_VECTOR_APPEND(&list->items, ast_strdup(resources[i]))) {
+                       return -1;
+               }
        }
-
-       AST_RWLIST_WRLOCK(&body_generators);
-       AST_LIST_INSERT_HEAD(&body_generators, generator, list);
-       AST_RWLIST_UNLOCK(&body_generators);
-
-       /* Lengths of type and subtype plus space for a slash. pj_str_t is not
-        * null-terminated, so there is no need to allocate for the extra null
-        * byte
-        */
-       accept_len = strlen(generator->type) + strlen(generator->subtype) + 1;
-
-       accept.ptr = alloca(accept_len);
-       accept.slen = accept_len;
-       /* Safe use of sprintf */
-       sprintf(accept.ptr, "%s/%s", generator->type, generator->subtype);
-       pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), &pubsub_module,
-                       PJSIP_H_ACCEPT, NULL, 1, &accept);
-
        return 0;
 }
 
-void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator)
+/*!
+ * \brief RAII callback to destroy a resource list
+ */
+static void cleanup_resource_list(struct resource_list *list)
 {
-       struct ast_sip_pubsub_body_generator *iter;
-       SCOPED_LOCK(lock, &body_generators, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
-
-       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&body_generators, iter, list) {
-               if (iter == generator) {
-                       AST_LIST_REMOVE_CURRENT(list);
-                       break;
-               }
+       if (!list) {
+               return;
        }
-       AST_RWLIST_TRAVERSE_SAFE_END;
+
+       ast_sorcery_delete(ast_sip_get_sorcery(), list);
+       ao2_cleanup(list);
 }
 
-int ast_sip_pubsub_register_body_supplement(struct ast_sip_pubsub_body_supplement *supplement)
+/*!
+ * \brief allocate a resource list, store it in sorcery, and set its details
+ *
+ * \param test The unit test. Used for logging status messages.
+ * \param list_name The name of the list to create.
+ * \param event The event the list services
+ * \param resources Array of resources to apply to the list
+ * \param num_resources The number of resources in the array
+ * \retval NULL Failed to allocate or populate list
+ * \retval non-NULL The created list
+ */
+static struct resource_list *create_resource_list(struct ast_test *test,
+               const char *list_name, const char *event, const char **resources, size_t num_resources)
 {
-       AST_RWLIST_WRLOCK(&body_supplements);
-       AST_RWLIST_INSERT_TAIL(&body_supplements, supplement, list);
-       AST_RWLIST_UNLOCK(&body_supplements);
+       struct resource_list *list;
 
-       return 0;
+       list = ast_sorcery_alloc(ast_sip_get_sorcery(), "resource_list", list_name);
+       if (!list) {
+               ast_test_status_update(test, "Could not allocate resource list in sorcery\n");
+               return NULL;
+       }
+
+       if (ast_sorcery_create(ast_sip_get_sorcery(), list)) {
+               ast_test_status_update(test, "Could not store the resource list in sorcery\n");
+               ao2_cleanup(list);
+               return NULL;
+       }
+
+       if (populate_list(list, event, resources, num_resources)) {
+               ast_test_status_update(test, "Could not add resources to the resource list\n");
+               cleanup_resource_list(list);
+               return NULL;
+       }
+
+       return list;
 }
 
-void ast_sip_pubsub_unregister_body_supplement(struct ast_sip_pubsub_body_supplement *supplement)
+/*!
+ * \brief Check the integrity of a tree node against a set of resources.
+ *
+ * The tree node's resources must be in the same order as the resources in
+ * the supplied resources array. Because of this constraint, tests can misrepresent
+ * the size of the resources array as being smaller than it really is if resources
+ * at the end of the array should not be present in the tree node.
+ *
+ * \param test The unit test. Used for printing status messages.
+ * \param node The constructed tree node whose integrity is under question.
+ * \param resources Array of expected resource values
+ * \param num_resources The number of resources to check in the array.
+ */
+static int check_node(struct ast_test *test, struct tree_node *node,
+               const char **resources, size_t num_resources)
 {
-       struct ast_sip_pubsub_body_supplement *iter;
-       SCOPED_LOCK(lock, &body_supplements, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
+       int i;
 
-       AST_RWLIST_TRAVERSE_SAFE_BEGIN(&body_supplements, iter, list) {
-               if (iter == supplement) {
-                       AST_LIST_REMOVE_CURRENT(list);
-                       break;
+       if (AST_VECTOR_SIZE(&node->children) != num_resources) {
+               ast_test_status_update(test, "Unexpected number of resources in tree. Expected %d, got %d\n",
+                               num_resources, AST_VECTOR_SIZE(&node->children));
+               return -1;
+       }
+
+       for (i = 0; i < num_resources; ++i) {
+               if (strcmp(resources[i], AST_VECTOR_GET(&node->children, i)->resource)) {
+                       ast_test_status_update(test, "Mismatched resources. Expected '%s' but got '%s'\n",
+                                       resources[i], AST_VECTOR_GET(&node->children, i)->resource);
+                       return -1;
                }
        }
-       AST_RWLIST_TRAVERSE_SAFE_END;
-}
 
-const char *ast_sip_subscription_get_body_type(struct ast_sip_subscription *sub)
-{
-       return sub->body_generator->type;
+       return 0;
 }
 
-const char *ast_sip_subscription_get_body_subtype(struct ast_sip_subscription *sub)
+/*!
+ * \brief RAII_VAR callback to destroy an allocated resource tree
+ */
+static void test_resource_tree_destroy(struct resource_tree *tree)
 {
-       return sub->body_generator->subtype;
+       resource_tree_destroy(tree);
+       ast_free(tree);
 }
 
-int ast_sip_pubsub_generate_body_content(const char *type, const char *subtype,
-               void *data, struct ast_str **str)
+AST_TEST_DEFINE(resource_tree)
 {
-       struct ast_sip_pubsub_body_supplement *supplement;
-       struct ast_sip_pubsub_body_generator *generator;
-       int res = 0;
-       void *body;
-
-       generator = find_body_generator_type_subtype(type, subtype);
-       if (!generator) {
-               ast_log(LOG_WARNING, "Unable to find a body generator for %s/%s\n",
-                               type, subtype);
-               return -1;
-       }
+       RAII_VAR(struct resource_list *, list, NULL, cleanup_resource_list);
+       RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
+       const char *resources[] = {
+               "huey",
+               "dewey",
+               "louie",
+       };
+       int resp;
 
-       body = generator->allocate_body(data);
-       if (!body) {
-               ast_log(LOG_WARNING, "Unable to allocate a NOTIFY body of type %s/%s\n",
-                               type, subtype);
-               return -1;
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "resource_tree";
+               info->category = "/res/res_pjsip_pubsub/";
+               info->summary = "Basic resource tree integrity check";
+               info->description =
+                       "Create a resource list and ensure that our attempt to build a tree works as expected.";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
        }
 
-       if (generator->generate_body_content(body, data)) {
-               res = -1;
-               goto end;
+       list = create_resource_list(test, "foo", "test", resources, ARRAY_LEN(resources));
+       if (!list) {
+               return AST_TEST_FAIL;
        }
 
-       AST_RWLIST_RDLOCK(&body_supplements);
-       AST_RWLIST_TRAVERSE(&body_supplements, supplement, list) {
-               if (!strcmp(generator->type, supplement->type) &&
-                               !strcmp(generator->subtype, supplement->subtype)) {
-                       res = supplement->supplement_body(body, data);
-                       if (res) {
-                               break;
-                       }
-               }
+       tree = ast_calloc(1, sizeof(*tree));
+       resp = build_resource_tree(NULL, &test_handler, "foo", tree);
+       if (resp != 200) {
+               ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
+               return AST_TEST_FAIL;
        }
-       AST_RWLIST_UNLOCK(&body_supplements);
 
-       if (!res) {
-               generator->to_string(body, str);
+       if (!tree->root) {
+               ast_test_status_update(test, "Resource tree has no root\n");
+               return AST_TEST_FAIL;
        }
 
-end:
-       if (generator->destroy_body) {
-               generator->destroy_body(body);
+       if (check_node(test, tree->root, resources, ARRAY_LEN(resources))) {
+               return AST_TEST_FAIL;
        }
 
-       return res;
+       return AST_TEST_PASS;
 }
 
-static pj_bool_t pubsub_on_rx_request(pjsip_rx_data *rdata)
+AST_TEST_DEFINE(complex_resource_tree)
 {
-       if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, pjsip_get_subscribe_method())) {
-               return pubsub_on_rx_subscribe_request(rdata);
-       } else if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_publish_method)) {
-               return pubsub_on_rx_publish_request(rdata);
+       RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list);
+       RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list);
+       RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
+       const char *resources_1[] = {
+               "huey",
+               "dewey",
+               "louie",
+               "dwarves",
+       };
+       const char *resources_2[] = {
+               "happy",
+               "grumpy",
+               "doc",
+               "bashful",
+               "dopey",
+               "sneezy",
+               "sleepy",
+       };
+       int resp;
+       struct tree_node *node;
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "complex_resource_tree";
+               info->category = "/res/res_pjsip_pubsub/";
+               info->summary = "Complex resource tree integrity check";
+               info->description =
+                       "Create a complex resource list and ensure that our attempt to build a tree works as expected.";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
        }
 
-       return PJ_FALSE;
-}
-
-static void pubsub_on_evsub_state(pjsip_evsub *evsub, pjsip_event *event)
-{
-       struct ast_sip_subscription *sub;
-       if (pjsip_evsub_get_state(evsub) != PJSIP_EVSUB_STATE_TERMINATED) {
-               return;
+       list_1 = create_resource_list(test, "foo", "test", resources_1, ARRAY_LEN(resources_1));
+       if (!list_1) {
+               return AST_TEST_FAIL;
        }
 
-       sub = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
-       if (!sub) {
-               return;
+       list_2 = create_resource_list(test, "dwarves", "test", resources_2, ARRAY_LEN(resources_2));
+       if (!list_2) {
+               return AST_TEST_FAIL;
        }
 
-       if (sub->handler->subscription_shutdown) {
-               sub->handler->subscription_shutdown(sub);
+       tree = ast_calloc(1, sizeof(*tree));
+       resp = build_resource_tree(NULL, &test_handler, "foo", tree);
+       if (resp != 200) {
+               ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
+               return AST_TEST_FAIL;
        }
-       pjsip_evsub_set_mod_data(evsub, pubsub_module.id, NULL);
-}
-
-static void pubsub_on_rx_refresh(pjsip_evsub *evsub, pjsip_rx_data *rdata,
-               int *p_st_code, pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body)
-{
-       struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
-       enum ast_sip_subscription_notify_reason reason;
 
-       if (!sub) {
-               return;
+       if (!tree->root) {
+               ast_test_status_update(test, "Resource tree has no root\n");
+               return AST_TEST_FAIL;
        }
 
-       if (pjsip_evsub_get_state(sip_subscription_get_evsub(sub)) == PJSIP_EVSUB_STATE_TERMINATED) {
-               reason = AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED;
-       } else {
-               reason = AST_SIP_SUBSCRIPTION_NOTIFY_REASON_RENEWED;
-       }
-       if (sub->handler->notifier->notify_required(sub, reason)) {
-               *p_st_code = 500;
+       node = tree->root;
+       if (check_node(test, node, resources_1, ARRAY_LEN(resources_1))) {
+               return AST_TEST_FAIL;
        }
-}
-
-static void pubsub_on_rx_notify(pjsip_evsub *evsub, pjsip_rx_data *rdata, int *p_st_code,
-               pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body)
-{
-       struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
 
-       if (!sub) {
-               return;
+       /* The embedded list is at index 3 in the root node's children */
+       node = AST_VECTOR_GET(&node->children, 3);
+       if (check_node(test, node, resources_2, ARRAY_LEN(resources_2))) {
+               return AST_TEST_FAIL;
        }
 
-       sub->handler->subscriber->state_change(sub, rdata->msg_info.msg->body,
-                       pjsip_evsub_get_state(evsub));
+       return AST_TEST_PASS;
 }
 
-static int serialized_pubsub_on_client_refresh(void *userdata)
+AST_TEST_DEFINE(bad_resource)
 {
-       struct ast_sip_subscription *sub = userdata;
-       pjsip_evsub *evsub;
-       pjsip_tx_data *tdata;
+       RAII_VAR(struct resource_list *, list, NULL, cleanup_resource_list);
+       RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
+       const char *resources[] = {
+               "huey",
+               "dewey",
+               "louie",
+               "coconut", /* A "bad" resource */
+       };
+       int resp;
 
-       evsub = sip_subscription_get_evsub(sub);
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "bad_resource";
+               info->category = "/res/res_pjsip_pubsub/";
+               info->summary = "Ensure bad resources do not end up in the tree";
+               info->description =
+                       "Create a resource list with a single bad resource. Ensure the bad resource does not end up in the tree.";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
 
-       if (pjsip_evsub_initiate(evsub, NULL, -1, &tdata) == PJ_SUCCESS) {
-               pjsip_evsub_send_request(evsub, tdata);
-       } else {
-               pjsip_evsub_terminate(evsub, PJ_TRUE);
-               return 0;
+       list = create_resource_list(test, "foo", "test", resources, ARRAY_LEN(resources));
+       if (!list) {
+               return AST_TEST_FAIL;
        }
-       ao2_cleanup(sub);
-       return 0;
-}
 
-static void pubsub_on_client_refresh(pjsip_evsub *evsub)
-{
-       struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
+       tree = ast_calloc(1, sizeof(*tree));
+       resp = build_resource_tree(NULL, &test_handler, "foo", tree);
+       if (resp != 200) {
+               ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
+               return AST_TEST_FAIL;
+       }
 
-       ao2_ref(sub, +1);
-       ast_sip_push_task(sub->serializer, serialized_pubsub_on_client_refresh, sub);
-}
+       if (!tree->root) {
+               ast_test_status_update(test, "Resource tree has no root\n");
+               return AST_TEST_FAIL;
+       }
 
-static int serialized_pubsub_on_server_timeout(void *userdata)
-{
-       struct ast_sip_subscription *sub = userdata;
+       /* We check against all but the final resource since we expect it not to be in the tree */
+       if (check_node(test, tree->root, resources, ARRAY_LEN(resources) - 1)) {
+               return AST_TEST_FAIL;
+       }
 
-       sub->handler->notifier->notify_required(sub,
-                       AST_SIP_SUBSCRIPTION_NOTIFY_REASON_TERMINATED);
+       return AST_TEST_PASS;
 
-       ao2_cleanup(sub);
-       return 0;
 }
 
-static void pubsub_on_server_timeout(pjsip_evsub *evsub)
+AST_TEST_DEFINE(bad_branch)
 {
-       struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, pubsub_module.id);
+       RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list);
+       RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list);
+       RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
+       const char *resources_1[] = {
+               "huey",
+               "dewey",
+               "louie",
+               "gross",
+       };
+       /* This list has nothing but bad resources */
+       const char *resources_2[] = {
+               "coconut",
+               "cilantro",
+               "olive",
+               "cheese",
+       };
+       int resp;
 
-       if (!sub) {
-               /* if a subscription has been terminated and the subscription
-                  timeout/expires is less than the time it takes for all pending
-                  transactions to end then the subscription timer will not have
-                  been canceled yet and sub will be null, so do nothing since
-                  the subscription has already been terminated. */
-               return;
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "bad_branch";
+               info->category = "/res/res_pjsip_pubsub/";
+               info->summary = "Ensure bad branches are pruned from the tree";
+               info->description =
+                       "Create a resource list that makes a tree with an entire branch of bad resources.\n"
+                       "Ensure the bad branch is pruned from the tree.";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
        }
 
-       ao2_ref(sub, +1);
-       ast_sip_push_task(sub->serializer, serialized_pubsub_on_server_timeout, sub);
-}
+       list_1 = create_resource_list(test, "foo", "test", resources_1, ARRAY_LEN(resources_1));
+       if (!list_1) {
+               return AST_TEST_FAIL;
+       }
+       list_2 = create_resource_list(test, "gross", "test", resources_2, ARRAY_LEN(resources_2));
+       if (!list_2) {
+               return AST_TEST_FAIL;
+       }
 
-static int ami_subscription_detail(struct ast_sip_subscription *sub,
-                                  struct ast_sip_ami *ami,
-                                  const char *event)
-{
-       RAII_VAR(struct ast_str *, buf,
-                ast_sip_create_ami_event(event, ami), ast_free);
+       tree = ast_calloc(1, sizeof(*tree));
+       resp = build_resource_tree(NULL, &test_handler, "foo", tree);
+       if (resp != 200) {
+               ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
+               return AST_TEST_FAIL;
+       }
 
-       if (!buf) {
-               return -1;
+       if (!tree->root) {
+               ast_test_status_update(test, "Resource tree has no root\n");
+               return AST_TEST_FAIL;
        }
 
-       sip_subscription_to_ami(sub, &buf);
-       astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
-       return 0;
-}
+       /* We check against all but the final resource of the list since the entire branch should
+        * be pruned from the tree
+        */
+       if (check_node(test, tree->root, resources_1, ARRAY_LEN(resources_1) - 1)) {
+               return AST_TEST_FAIL;
+       }
 
-static int ami_subscription_detail_inbound(struct ast_sip_subscription *sub, void *arg)
-{
-       return sub->role == AST_SIP_NOTIFIER ? ami_subscription_detail(
-               sub, arg, "InboundSubscriptionDetail") : 0;
-}
+       return AST_TEST_PASS;
 
-static int ami_subscription_detail_outbound(struct ast_sip_subscription *sub, void *arg)
-{
-       return sub->role == AST_SIP_SUBSCRIBER ? ami_subscription_detail(
-               sub, arg, "OutboundSubscriptionDetail") : 0;
 }
 
-static int ami_show_subscriptions_inbound(struct mansession *s, const struct message *m)
+AST_TEST_DEFINE(duplicate_resource)
 {
-       struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
-       int num;
-
-       astman_send_listack(s, m, "Following are Events for "
-                           "each inbound Subscription", "start");
+       RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list);
+       RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list);
+       RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
+       const char *resources_1[] = {
+               "huey",
+               "ducks",
+               "dewey",
+               "louie",
+       };
+       const char *resources_2[] = {
+               "donald",
+               "daisy",
+               "scrooge",
+               "dewey",
+               "louie",
+               "huey",
+       };
+       int resp;
+       struct tree_node *node;
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "duplicate_resource";
+               info->category = "/res/res_pjsip_pubsub/";
+               info->summary = "Ensure duplicated resources do not end up in the tree";
+               info->description =
+                       "Create a resource list with a single duplicated resource. Ensure the duplicated resource does not end up in the tree.";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
 
-       num = for_each_subscription(ami_subscription_detail_inbound, &ami);
+       list_1 = create_resource_list(test, "foo", "test", resources_1, ARRAY_LEN(resources_1));
+       if (!list_1) {
+               return AST_TEST_FAIL;
+       }
 
-       astman_append(s, "Event: InboundSubscriptionDetailComplete\r\n");
-       if (!ast_strlen_zero(ami.action_id)) {
-               astman_append(s, "ActionID: %s\r\n", ami.action_id);
+       list_2 = create_resource_list(test, "ducks", "test", resources_2, ARRAY_LEN(resources_2));
+       if (!list_2) {
+               return AST_TEST_FAIL;
        }
-       astman_append(s, "EventList: Complete\r\n"
-                     "ListItems: %d\r\n\r\n", num);
-       return 0;
-}
 
-static int ami_show_subscriptions_outbound(struct mansession *s, const struct message *m)
-{
-       struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
-       int num;
+       tree = ast_calloc(1, sizeof(*tree));
+       resp = build_resource_tree(NULL, &test_handler, "foo", tree);
+       if (resp != 200) {
+               ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
+               return AST_TEST_FAIL;
+       }
 
-       astman_send_listack(s, m, "Following are Events for "
-                           "each outbound Subscription", "start");
+       if (!tree->root) {
+               ast_test_status_update(test, "Resource tree has no root\n");
+               return AST_TEST_FAIL;
+       }
 
-       num = for_each_subscription(ami_subscription_detail_outbound, &ami);
+       node = tree->root;
+       /* This node should have "huey" and "ducks". "dewey" and "louie" should not
+        * be present since they were found in the "ducks" list.
+        */
+       if (check_node(test, node, resources_1, ARRAY_LEN(resources_1) - 2)) {
+               return AST_TEST_FAIL;
+       }
 
-       astman_append(s, "Event: OutboundSubscriptionDetailComplete\r\n");
-       if (!ast_strlen_zero(ami.action_id)) {
-               astman_append(s, "ActionID: %s\r\n", ami.action_id);
+       /* This node should have "donald", "daisy", "scrooge", "dewey", and "louie".
+        * "huey" is not here since that was already encountered in the parent list
+        */
+       node = AST_VECTOR_GET(&node->children, 1);
+       if (check_node(test, node, resources_2, ARRAY_LEN(resources_2) - 1)) {
+               return AST_TEST_FAIL;
        }
-       astman_append(s, "EventList: Complete\r\n"
-                     "ListItems: %d\r\n\r\n", num);
-       return 0;
-}
 
-#define AMI_SHOW_SUBSCRIPTIONS_INBOUND "PJSIPShowSubscriptionsInbound"
-#define AMI_SHOW_SUBSCRIPTIONS_OUTBOUND "PJSIPShowSubscriptionsOutbound"
+       return AST_TEST_PASS;
+}
 
-static int persistence_endpoint_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
+AST_TEST_DEFINE(loop)
 {
-       struct subscription_persistence *persistence = obj;
+       RAII_VAR(struct resource_list *, list_1, NULL, cleanup_resource_list);
+       RAII_VAR(struct resource_list *, list_2, NULL, cleanup_resource_list);
+       RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
+       const char *resources_1[] = {
+               "derp",
+       };
+       const char *resources_2[] = {
+               "herp",
+       };
+       int resp;
 
-       persistence->endpoint = ast_strdup(var->value);
-       return 0;
-}
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "loop";
+               info->category = "/res/res_pjsip_pubsub/";
+               info->summary = "Test that loops are properly detected.";
+               info->description =
+                       "Create two resource lists that refer to each other. Ensure that attempting to build a tree\n"
+                       "results in an empty tree.";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
 
-static int persistence_endpoint_struct2str(const void *obj, const intptr_t *args, char **buf)
-{
-       const struct subscription_persistence *persistence = obj;
+       list_1 = create_resource_list(test, "herp", "test", resources_1, ARRAY_LEN(resources_1));
+       if (!list_1) {
+               return AST_TEST_FAIL;
+       }
+       list_2 = create_resource_list(test, "derp", "test", resources_2, ARRAY_LEN(resources_2));
+       if (!list_2) {
+               return AST_TEST_FAIL;
+       }
 
-       *buf = ast_strdup(persistence->endpoint);
-       return 0;
+       tree = ast_calloc(1, sizeof(*tree));
+       resp = build_resource_tree(NULL, &test_handler, "herp", tree);
+       if (resp == 200) {
+               ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
+               return AST_TEST_FAIL;
+       }
+
+       return AST_TEST_PASS;
 }
 
-static int persistence_tag_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
+AST_TEST_DEFINE(bad_event)
 {
-       struct subscription_persistence *persistence = obj;
+       RAII_VAR(struct resource_list *, list, NULL, cleanup_resource_list);
+       RAII_VAR(struct resource_tree *, tree, NULL, test_resource_tree_destroy);
+       const char *resources[] = {
+               "huey",
+               "dewey",
+               "louie",
+       };
+       int resp;
 
-       persistence->tag = ast_strdup(var->value);
-       return 0;
-}
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "bad_event";
+               info->category = "/res/res_pjsip_pubsub/";
+               info->summary = "Ensure that list with wrong event specified is not retrieved";
+               info->description =
+                       "Create a simple resource list for event 'tsetse'. Ensure that trying to retrieve the list for event 'test' fails.";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
 
-static int persistence_tag_struct2str(const void *obj, const intptr_t *args, char **buf)
-{
-       const struct subscription_persistence *persistence = obj;
+       list = create_resource_list(test, "foo", "tsetse", resources, ARRAY_LEN(resources));
+       if (!list) {
+               return AST_TEST_FAIL;
+       }
 
-       *buf = ast_strdup(persistence->tag);
-       return 0;
-}
+       tree = ast_calloc(1, sizeof(*tree));
+       /* Since the test_handler is for event "test", this should not build a list, but
+        * instead result in a single resource being created, called "foo"
+        */
+       resp = build_resource_tree(NULL, &test_handler, "foo", tree);
+       if (resp != 200) {
+               ast_test_status_update(test, "Unexpected response %d when building resource tree\n", resp);
+               return AST_TEST_FAIL;
+       }
 
-static int persistence_expires_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
-{
-       struct subscription_persistence *persistence = obj;
-       return ast_get_timeval(var->value, &persistence->expires, ast_tv(0, 0), NULL);
-}
+       if (!tree->root) {
+               ast_test_status_update(test, "Resource tree has no root\n");
+               return AST_TEST_FAIL;
+       }
 
-static int persistence_expires_struct2str(const void *obj, const intptr_t *args, char **buf)
-{
-       const struct subscription_persistence *persistence = obj;
-       return (ast_asprintf(buf, "%ld", persistence->expires.tv_sec) < 0) ? -1 : 0;
+       if (strcmp(tree->root->resource, "foo")) {
+               ast_test_status_update(test, "Unexpected resource %s found in tree\n", tree->root->resource);
+               return AST_TEST_FAIL;
+       }
+
+       return AST_TEST_PASS;
 }
 
+#endif
+
 static int resource_endpoint_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
 {
        struct ast_sip_publication_resource *resource = obj;
@@ -2224,6 +4020,11 @@ static int load_module(void)
        ast_sorcery_object_field_register_custom(sorcery, "subscription_persistence", "expires", "",
                persistence_expires_str2struct, persistence_expires_struct2str, NULL, 0, 0);
 
+       if (apply_list_configuration(sorcery)) {
+               ast_sip_unregister_service(&pubsub_module);
+               ast_sched_context_destroy(sched);
+       }
+
        ast_sorcery_apply_default(sorcery, "inbound-publication", "config", "pjsip.conf,criteria=type=inbound-publication");
        if (ast_sorcery_object_register(sorcery, "inbound-publication", publication_resource_alloc,
                NULL, NULL)) {
@@ -2248,6 +4049,16 @@ static int load_module(void)
                                 ami_show_subscriptions_inbound);
        ast_manager_register_xml(AMI_SHOW_SUBSCRIPTIONS_OUTBOUND, EVENT_FLAG_SYSTEM,
                                 ami_show_subscriptions_outbound);
+       ast_manager_register_xml("PJSIPShowResourceLists", EVENT_FLAG_SYSTEM,
+                       ami_show_resource_lists);
+
+       AST_TEST_REGISTER(resource_tree);
+       AST_TEST_REGISTER(complex_resource_tree);
+       AST_TEST_REGISTER(bad_resource);
+       AST_TEST_REGISTER(bad_branch);
+       AST_TEST_REGISTER(duplicate_resource);
+       AST_TEST_REGISTER(loop);
+       AST_TEST_REGISTER(bad_event);
 
        return AST_MODULE_LOAD_SUCCESS;
 }
@@ -2256,11 +4067,20 @@ static int unload_module(void)
 {
        ast_manager_unregister(AMI_SHOW_SUBSCRIPTIONS_OUTBOUND);
        ast_manager_unregister(AMI_SHOW_SUBSCRIPTIONS_INBOUND);
+       ast_manager_unregister("PJSIPShowResourceLists");
 
        if (sched) {
                ast_sched_context_destroy(sched);
        }
 
+       AST_TEST_UNREGISTER(resource_tree);
+       AST_TEST_UNREGISTER(complex_resource_tree);
+       AST_TEST_UNREGISTER(bad_resource);
+       AST_TEST_UNREGISTER(bad_branch);
+       AST_TEST_UNREGISTER(duplicate_resource);
+       AST_TEST_UNREGISTER(loop);
+       AST_TEST_UNREGISTER(bad_event);
+
        return 0;
 }
 
index 4e0587d..38e036a 100644 (file)
@@ -98,7 +98,6 @@ static int xpidf_generate_body_content(void *body, void *data)
 }
 
 #define MAX_STRING_GROWTHS 5
-#define XML_PROLOG 39
 
 static void xpidf_to_string(void *body, struct ast_str **str)
 {
@@ -108,13 +107,13 @@ static void xpidf_to_string(void *body, struct ast_str **str)
 
        do {
                size = pjxpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str));
-               if (size == XML_PROLOG) {
+               if (size == AST_PJSIP_XML_PROLOG_LEN) {
                        ast_str_make_space(str, ast_str_size(*str) * 2);
                        ++growths;
                }
-       } while (size == XML_PROLOG && growths < MAX_STRING_GROWTHS);
+       } while (size == AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
 
-       if (size == XML_PROLOG) {
+       if (size == AST_PJSIP_XML_PROLOG_LEN) {
                ast_log(LOG_WARNING, "XPIDF body text too large\n");
                return;
        }