res_pjsip: New endpoint option "notify_early_inuse_ringing"
[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         struct ast_sip_endpoint *endpoint = NULL;
111         unsigned int notify_early_inuse_ringing = 0;
112
113         if (!local || !state_data->datastores) {
114                 return -1;
115         }
116
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");
119                 return -1;
120         }
121
122         stripped = ast_strip_quoted(local, "<", ">");
123         ast_sip_sanitize_xml(stripped, sanitized, sizeof(sanitized));
124
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);
128         }
129         ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
130                         &pidfstate, &pidfnote, &local_state, notify_early_inuse_ringing);
131
132         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "xmlns", "urn:ietf:params:xml:ns:dialog-info");
133
134         snprintf(version_str, sizeof(version_str), "%u", version);
135         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "version", version_str);
136
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);
139
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");
144         }
145
146         state = ast_sip_presence_xml_create_node(state_data->pool, dialog, "state");
147         pj_strdup2(state_data->pool, &state->content, statestring);
148
149         if (state_data->exten_state == AST_EXTENSION_ONHOLD) {
150                 pj_xml_node *local_node, *target, *param;
151
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");
158         }
159
160         return 0;
161 }
162
163 /* The maximum number of times the ast_str() for the body text can grow before we declare an XML body
164  * too large to send.
165  */
166 #define MAX_STRING_GROWTHS 6
167
168 static void dialog_info_to_string(void *body, struct ast_str **str)
169 {
170         pj_xml_node *dialog_info = body;
171         int growths = 0;
172         int size;
173
174         do {
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);
178                         ++growths;
179                 }
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");
183                 return;
184         }
185
186         *(ast_str_buffer(*str) + size) = '\0';
187         ast_str_update(*str);
188 }
189
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 */
198 };
199
200 static int load_module(void)
201 {
202         CHECK_PJSIP_PUBSUB_MODULE_LOADED();
203
204         if (ast_sip_pubsub_register_body_generator(&dialog_info_body_generator)) {
205                 return AST_MODULE_LOAD_DECLINE;
206         }
207
208         return AST_MODULE_LOAD_SUCCESS;
209 }
210
211 static int unload_module(void)
212 {
213         ast_sip_pubsub_unregister_body_generator(&dialog_info_body_generator);
214         return 0;
215 }
216
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,
219         .load = load_module,
220         .unload = unload_module,
221         .load_pri = AST_MODPRI_CHANNEL_DEPEND,
222 );