Merge "res_pjsip: New endpoint option "refer_blind_progress""
[asterisk/asterisk.git] / res / res_pjsip_dialog_info_body_generator.c
1 /*
2  * asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*** MODULEINFO
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>
25  ***/
26
27 #include "asterisk.h"
28
29 #include <pjsip.h>
30 #include <pjsip_simple.h>
31 #include <pjlib.h>
32
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"
38
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 */
42         unsigned int version;
43 };
44
45 /*! \brief Destructor for dialog-info+xml information */
46 static void dialog_info_xml_state_destroy(void *obj)
47 {
48         ast_free(obj);
49 }
50
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,
55 };
56
57 static void *dialog_info_allocate_body(void *data)
58 {
59         struct ast_sip_exten_state_data *state_data = data;
60
61         return ast_sip_presence_xml_create_node(state_data->pool, NULL, "dialog-info");
62 }
63
64 static struct ast_datastore *dialog_info_xml_state_find_or_create(struct ao2_container *datastores)
65 {
66         struct ast_datastore *datastore = ast_datastores_find(datastores, "dialog-info+xml");
67
68         if (datastore) {
69                 return datastore;
70         }
71
72         datastore = ast_datastores_alloc_datastore(&dialog_info_xml_datastore, "dialog-info+xml");
73         if (!datastore) {
74                 return NULL;
75         }
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);
79                 return NULL;
80         }
81
82         return datastore;
83 }
84
85 static unsigned int dialog_info_xml_get_version(struct ao2_container *datastores, unsigned int *version)
86 {
87         struct ast_datastore *datastore = dialog_info_xml_state_find_or_create(datastores);
88         struct dialog_info_xml_state *state;
89
90         if (!datastore) {
91                 return -1;
92         }
93
94         state = datastore->data;
95         *version = state->version++;
96         ao2_ref(datastore, -1);
97
98         return 0;
99 }
100
101 static int dialog_info_generate_body_content(void *body, void *data)
102 {
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
111         if (!local || !state_data->datastores) {
112                 return -1;
113         }
114
115         if (dialog_info_xml_get_version(state_data->datastores, &version)) {
116                 ast_log(LOG_WARNING, "dialog-info+xml version could not be retrieved from datastore\n");
117                 return -1;
118         }
119
120         stripped = ast_strip_quoted(local, "<", ">");
121         ast_sip_sanitize_xml(stripped, sanitized, sizeof(sanitized));
122
123         ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
124                         &pidfstate, &pidfnote, &local_state);
125
126         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "xmlns", "urn:ietf:params:xml:ns:dialog-info");
127
128         snprintf(version_str, sizeof(version_str), "%u", version);
129         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "version", version_str);
130
131         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "state", "full");
132         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "entity", sanitized);
133
134         dialog = ast_sip_presence_xml_create_node(state_data->pool, dialog_info, "dialog");
135         ast_sip_presence_xml_create_attr(state_data->pool, dialog, "id", state_data->exten);
136         if (state_data->exten_state == AST_EXTENSION_RINGING) {
137                 ast_sip_presence_xml_create_attr(state_data->pool, dialog, "direction", "recipient");
138         }
139
140         state = ast_sip_presence_xml_create_node(state_data->pool, dialog, "state");
141         pj_strdup2(state_data->pool, &state->content, statestring);
142
143         if (state_data->exten_state == AST_EXTENSION_ONHOLD) {
144                 pj_xml_node *local_node, *target, *param;
145
146                 local_node = ast_sip_presence_xml_create_node(state_data->pool, dialog, "local");
147                 target = ast_sip_presence_xml_create_node(state_data->pool, local_node, "target");
148                 ast_sip_presence_xml_create_attr(state_data->pool, target, "uri", sanitized);
149                 param = ast_sip_presence_xml_create_node(state_data->pool, target, "param");
150                 ast_sip_presence_xml_create_attr(state_data->pool, param, "pname", "+sip.rendering");
151                 ast_sip_presence_xml_create_attr(state_data->pool, param, "pvalue", "no");
152         }
153
154         return 0;
155 }
156
157 /* The maximum number of times the ast_str() for the body text can grow before we declare an XML body
158  * too large to send.
159  */
160 #define MAX_STRING_GROWTHS 6
161
162 static void dialog_info_to_string(void *body, struct ast_str **str)
163 {
164         pj_xml_node *dialog_info = body;
165         int growths = 0;
166         int size;
167
168         do {
169                 size = pj_xml_print(dialog_info, ast_str_buffer(*str), ast_str_size(*str) - 1, PJ_TRUE);
170                 if (size <= AST_PJSIP_XML_PROLOG_LEN) {
171                         ast_str_make_space(str, ast_str_size(*str) * 2);
172                         ++growths;
173                 }
174         } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
175         if (size <= AST_PJSIP_XML_PROLOG_LEN) {
176                 ast_log(LOG_WARNING, "dialog-info+xml body text too large\n");
177                 return;
178         }
179
180         *(ast_str_buffer(*str) + size) = '\0';
181         ast_str_update(*str);
182 }
183
184 static struct ast_sip_pubsub_body_generator dialog_info_body_generator = {
185         .type = "application",
186         .subtype = "dialog-info+xml",
187         .body_type = AST_SIP_EXTEN_STATE_DATA,
188         .allocate_body = dialog_info_allocate_body,
189         .generate_body_content = dialog_info_generate_body_content,
190         .to_string = dialog_info_to_string,
191         /* No need for a destroy_body callback since we use a pool */
192 };
193
194 static int load_module(void)
195 {
196         CHECK_PJSIP_PUBSUB_MODULE_LOADED();
197
198         if (ast_sip_pubsub_register_body_generator(&dialog_info_body_generator)) {
199                 return AST_MODULE_LOAD_DECLINE;
200         }
201
202         return AST_MODULE_LOAD_SUCCESS;
203 }
204
205 static int unload_module(void)
206 {
207         ast_sip_pubsub_unregister_body_generator(&dialog_info_body_generator);
208         return 0;
209 }
210
211 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State Dialog Info+XML Provider",
212         .support_level = AST_MODULE_SUPPORT_CORE,
213         .load = load_module,
214         .unload = unload_module,
215         .load_pri = AST_MODPRI_CHANNEL_DEPEND,
216 );