loader: Add dependency fields to module structures.
[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         <support_level>core</support_level>
24  ***/
25
26 #include "asterisk.h"
27
28 #include <pjsip.h>
29 #include <pjsip_simple.h>
30 #include <pjlib.h>
31
32 #include "asterisk/module.h"
33 #include "asterisk/res_pjsip.h"
34 #include "asterisk/res_pjsip_pubsub.h"
35 #include "asterisk/res_pjsip_presence_xml.h"
36 #include "asterisk/res_pjsip_body_generator_types.h"
37
38 /*! \brief Structure which contains dialog-info+xml state information */
39 struct dialog_info_xml_state {
40         /*! \brief Version to place into the next NOTIFY */
41         unsigned int version;
42 };
43
44 /*! \brief Destructor for dialog-info+xml information */
45 static void dialog_info_xml_state_destroy(void *obj)
46 {
47         ast_free(obj);
48 }
49
50 /*! \brief Datastore for attaching dialog-info+xml state information */
51 static const struct ast_datastore_info dialog_info_xml_datastore = {
52         .type = "dialog-info+xml",
53         .destroy = dialog_info_xml_state_destroy,
54 };
55
56 static void *dialog_info_allocate_body(void *data)
57 {
58         struct ast_sip_exten_state_data *state_data = data;
59
60         return ast_sip_presence_xml_create_node(state_data->pool, NULL, "dialog-info");
61 }
62
63 static struct ast_datastore *dialog_info_xml_state_find_or_create(struct ao2_container *datastores)
64 {
65         struct ast_datastore *datastore = ast_datastores_find(datastores, "dialog-info+xml");
66
67         if (datastore) {
68                 return datastore;
69         }
70
71         datastore = ast_datastores_alloc_datastore(&dialog_info_xml_datastore, "dialog-info+xml");
72         if (!datastore) {
73                 return NULL;
74         }
75         datastore->data = ast_calloc(1, sizeof(struct dialog_info_xml_state));
76         if (!datastore->data || ast_datastores_add(datastores, datastore)) {
77                 ao2_ref(datastore, -1);
78                 return NULL;
79         }
80
81         return datastore;
82 }
83
84 static unsigned int dialog_info_xml_get_version(struct ao2_container *datastores, unsigned int *version)
85 {
86         struct ast_datastore *datastore = dialog_info_xml_state_find_or_create(datastores);
87         struct dialog_info_xml_state *state;
88
89         if (!datastore) {
90                 return -1;
91         }
92
93         state = datastore->data;
94         *version = state->version++;
95         ao2_ref(datastore, -1);
96
97         return 0;
98 }
99
100 static int dialog_info_generate_body_content(void *body, void *data)
101 {
102         pj_xml_node *dialog_info = body, *dialog, *state;
103         struct ast_sip_exten_state_data *state_data = data;
104         char *local = ast_strdupa(state_data->local), *stripped, *statestring = NULL;
105         char *pidfstate = NULL, *pidfnote = NULL;
106         enum ast_sip_pidf_state local_state;
107         unsigned int version;
108         char version_str[32], sanitized[PJSIP_MAX_URL_SIZE];
109         struct ast_sip_endpoint *endpoint = NULL;
110         unsigned int notify_early_inuse_ringing = 0;
111
112         if (!local || !state_data->datastores) {
113                 return -1;
114         }
115
116         if (dialog_info_xml_get_version(state_data->datastores, &version)) {
117                 ast_log(LOG_WARNING, "dialog-info+xml version could not be retrieved from datastore\n");
118                 return -1;
119         }
120
121         stripped = ast_strip_quoted(local, "<", ">");
122         ast_sip_sanitize_xml(stripped, sanitized, sizeof(sanitized));
123
124         if (state_data->sub && (endpoint = ast_sip_subscription_get_endpoint(state_data->sub))) {
125             notify_early_inuse_ringing = endpoint->notify_early_inuse_ringing;
126             ao2_cleanup(endpoint);
127         }
128         ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
129                         &pidfstate, &pidfnote, &local_state, notify_early_inuse_ringing);
130
131         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "xmlns", "urn:ietf:params:xml:ns:dialog-info");
132
133         snprintf(version_str, sizeof(version_str), "%u", version);
134         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "version", version_str);
135
136         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "state", "full");
137         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "entity", sanitized);
138
139         dialog = ast_sip_presence_xml_create_node(state_data->pool, dialog_info, "dialog");
140         ast_sip_presence_xml_create_attr(state_data->pool, dialog, "id", state_data->exten);
141         if (!ast_strlen_zero(statestring) && !strcmp(statestring, "early")) {
142                 ast_sip_presence_xml_create_attr(state_data->pool, dialog, "direction", "recipient");
143         }
144
145         state = ast_sip_presence_xml_create_node(state_data->pool, dialog, "state");
146         pj_strdup2(state_data->pool, &state->content, statestring);
147
148         if (state_data->exten_state == AST_EXTENSION_ONHOLD) {
149                 pj_xml_node *local_node, *target, *param;
150
151                 local_node = ast_sip_presence_xml_create_node(state_data->pool, dialog, "local");
152                 target = ast_sip_presence_xml_create_node(state_data->pool, local_node, "target");
153                 ast_sip_presence_xml_create_attr(state_data->pool, target, "uri", sanitized);
154                 param = ast_sip_presence_xml_create_node(state_data->pool, target, "param");
155                 ast_sip_presence_xml_create_attr(state_data->pool, param, "pname", "+sip.rendering");
156                 ast_sip_presence_xml_create_attr(state_data->pool, param, "pvalue", "no");
157         }
158
159         return 0;
160 }
161
162 /* The maximum number of times the ast_str() for the body text can grow before we declare an XML body
163  * too large to send.
164  */
165 #define MAX_STRING_GROWTHS 6
166
167 static void dialog_info_to_string(void *body, struct ast_str **str)
168 {
169         pj_xml_node *dialog_info = body;
170         int growths = 0;
171         int size;
172
173         do {
174                 size = pj_xml_print(dialog_info, ast_str_buffer(*str), ast_str_size(*str) - 1, PJ_TRUE);
175                 if (size <= AST_PJSIP_XML_PROLOG_LEN) {
176                         ast_str_make_space(str, ast_str_size(*str) * 2);
177                         ++growths;
178                 }
179         } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
180         if (size <= AST_PJSIP_XML_PROLOG_LEN) {
181                 ast_log(LOG_WARNING, "dialog-info+xml body text too large\n");
182                 return;
183         }
184
185         *(ast_str_buffer(*str) + size) = '\0';
186         ast_str_update(*str);
187 }
188
189 static struct ast_sip_pubsub_body_generator dialog_info_body_generator = {
190         .type = "application",
191         .subtype = "dialog-info+xml",
192         .body_type = AST_SIP_EXTEN_STATE_DATA,
193         .allocate_body = dialog_info_allocate_body,
194         .generate_body_content = dialog_info_generate_body_content,
195         .to_string = dialog_info_to_string,
196         /* No need for a destroy_body callback since we use a pool */
197 };
198
199 static int load_module(void)
200 {
201         CHECK_PJSIP_PUBSUB_MODULE_LOADED();
202
203         if (ast_sip_pubsub_register_body_generator(&dialog_info_body_generator)) {
204                 return AST_MODULE_LOAD_DECLINE;
205         }
206
207         return AST_MODULE_LOAD_SUCCESS;
208 }
209
210 static int unload_module(void)
211 {
212         ast_sip_pubsub_unregister_body_generator(&dialog_info_body_generator);
213         return 0;
214 }
215
216 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State Dialog Info+XML Provider",
217         .support_level = AST_MODULE_SUPPORT_CORE,
218         .load = load_module,
219         .unload = unload_module,
220         .load_pri = AST_MODPRI_CHANNEL_DEPEND,
221         .requires = "res_pjsip,res_pjsip_pubsub",
222 );