2 * asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2014, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <depend>res_pjsip_pubsub</depend>
23 <depend>res_pjsip_exten_state</depend>
24 <support_level>core</support_level>
30 #include <pjsip_simple.h>
33 #include "asterisk/module.h"
34 #include "asterisk/res_pjsip.h"
35 #include "asterisk/res_pjsip_pubsub.h"
36 #include "asterisk/res_pjsip_presence_xml.h"
37 #include "asterisk/res_pjsip_body_generator_types.h"
39 /*! \brief Structure which contains dialog-info+xml state information */
40 struct dialog_info_xml_state {
41 /*! \brief Version to place into the next NOTIFY */
45 /*! \brief Destructor for dialog-info+xml information */
46 static void dialog_info_xml_state_destroy(void *obj)
51 /*! \brief Datastore for attaching dialog-info+xml state information */
52 static const struct ast_datastore_info dialog_info_xml_datastore = {
53 .type = "dialog-info+xml",
54 .destroy = dialog_info_xml_state_destroy,
57 static void *dialog_info_allocate_body(void *data)
59 struct ast_sip_exten_state_data *state_data = data;
61 return ast_sip_presence_xml_create_node(state_data->pool, NULL, "dialog-info");
64 static struct ast_datastore *dialog_info_xml_state_find_or_create(struct ao2_container *datastores)
66 struct ast_datastore *datastore = ast_datastores_find(datastores, "dialog-info+xml");
72 datastore = ast_datastores_alloc_datastore(&dialog_info_xml_datastore, "dialog-info+xml");
76 datastore->data = ast_calloc(1, sizeof(struct dialog_info_xml_state));
77 if (!datastore->data || ast_datastores_add(datastores, datastore)) {
78 ao2_ref(datastore, -1);
85 static unsigned int dialog_info_xml_get_version(struct ao2_container *datastores, unsigned int *version)
87 struct ast_datastore *datastore = dialog_info_xml_state_find_or_create(datastores);
88 struct dialog_info_xml_state *state;
94 state = datastore->data;
95 *version = state->version++;
96 ao2_ref(datastore, -1);
101 static int dialog_info_generate_body_content(void *body, void *data)
103 pj_xml_node *dialog_info = body, *dialog, *state;
104 struct ast_sip_exten_state_data *state_data = data;
105 char *local = ast_strdupa(state_data->local), *stripped, *statestring = NULL;
106 char *pidfstate = NULL, *pidfnote = NULL;
107 enum ast_sip_pidf_state local_state;
108 unsigned int version;
109 char version_str[32], sanitized[PJSIP_MAX_URL_SIZE];
110 struct ast_sip_endpoint *endpoint = NULL;
111 unsigned int notify_early_inuse_ringing = 0;
113 if (!local || !state_data->datastores) {
117 if (dialog_info_xml_get_version(state_data->datastores, &version)) {
118 ast_log(LOG_WARNING, "dialog-info+xml version could not be retrieved from datastore\n");
122 stripped = ast_strip_quoted(local, "<", ">");
123 ast_sip_sanitize_xml(stripped, sanitized, sizeof(sanitized));
125 if (state_data->sub && (endpoint = ast_sip_subscription_get_endpoint(state_data->sub))) {
126 notify_early_inuse_ringing = endpoint->notify_early_inuse_ringing;
127 ao2_cleanup(endpoint);
129 ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
130 &pidfstate, &pidfnote, &local_state, notify_early_inuse_ringing);
132 ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "xmlns", "urn:ietf:params:xml:ns:dialog-info");
134 snprintf(version_str, sizeof(version_str), "%u", version);
135 ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "version", version_str);
137 ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "state", "full");
138 ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "entity", sanitized);
140 dialog = ast_sip_presence_xml_create_node(state_data->pool, dialog_info, "dialog");
141 ast_sip_presence_xml_create_attr(state_data->pool, dialog, "id", state_data->exten);
142 if (!ast_strlen_zero(statestring) && !strcmp(statestring, "early")) {
143 ast_sip_presence_xml_create_attr(state_data->pool, dialog, "direction", "recipient");
146 state = ast_sip_presence_xml_create_node(state_data->pool, dialog, "state");
147 pj_strdup2(state_data->pool, &state->content, statestring);
149 if (state_data->exten_state == AST_EXTENSION_ONHOLD) {
150 pj_xml_node *local_node, *target, *param;
152 local_node = ast_sip_presence_xml_create_node(state_data->pool, dialog, "local");
153 target = ast_sip_presence_xml_create_node(state_data->pool, local_node, "target");
154 ast_sip_presence_xml_create_attr(state_data->pool, target, "uri", sanitized);
155 param = ast_sip_presence_xml_create_node(state_data->pool, target, "param");
156 ast_sip_presence_xml_create_attr(state_data->pool, param, "pname", "+sip.rendering");
157 ast_sip_presence_xml_create_attr(state_data->pool, param, "pvalue", "no");
163 /* The maximum number of times the ast_str() for the body text can grow before we declare an XML body
166 #define MAX_STRING_GROWTHS 6
168 static void dialog_info_to_string(void *body, struct ast_str **str)
170 pj_xml_node *dialog_info = body;
175 size = pj_xml_print(dialog_info, ast_str_buffer(*str), ast_str_size(*str) - 1, PJ_TRUE);
176 if (size <= AST_PJSIP_XML_PROLOG_LEN) {
177 ast_str_make_space(str, ast_str_size(*str) * 2);
180 } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
181 if (size <= AST_PJSIP_XML_PROLOG_LEN) {
182 ast_log(LOG_WARNING, "dialog-info+xml body text too large\n");
186 *(ast_str_buffer(*str) + size) = '\0';
187 ast_str_update(*str);
190 static struct ast_sip_pubsub_body_generator dialog_info_body_generator = {
191 .type = "application",
192 .subtype = "dialog-info+xml",
193 .body_type = AST_SIP_EXTEN_STATE_DATA,
194 .allocate_body = dialog_info_allocate_body,
195 .generate_body_content = dialog_info_generate_body_content,
196 .to_string = dialog_info_to_string,
197 /* No need for a destroy_body callback since we use a pool */
200 static int load_module(void)
202 CHECK_PJSIP_PUBSUB_MODULE_LOADED();
204 if (ast_sip_pubsub_register_body_generator(&dialog_info_body_generator)) {
205 return AST_MODULE_LOAD_DECLINE;
208 return AST_MODULE_LOAD_SUCCESS;
211 static int unload_module(void)
213 ast_sip_pubsub_unregister_body_generator(&dialog_info_body_generator);
217 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State Dialog Info+XML Provider",
218 .support_level = AST_MODULE_SUPPORT_CORE,
220 .unload = unload_module,
221 .load_pri = AST_MODPRI_CHANNEL_DEPEND,