res_pjsip: New endpoint option "notify_early_inuse_ringing"
[asterisk/asterisk.git] / res / res_pjsip_xpidf_body_generator.c
1 /*
2  * asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@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 static void *xpidf_allocate_body(void *data)
40 {
41         struct ast_sip_exten_state_data *state_data = data;
42         char *local = ast_strdupa(state_data->local);
43         pjxpidf_pres *pres;
44         pj_str_t name;
45
46         pres = pjxpidf_create(state_data->pool, pj_cstr(&name, ast_strip_quoted(local, "<", ">")));
47         return pres;
48 }
49
50 static int xpidf_generate_body_content(void *body, void *data)
51 {
52         pjxpidf_pres *pres = body;
53         struct ast_sip_exten_state_data *state_data = data;
54         static pj_str_t STR_ADDR_PARAM = { ";user=ip", 8 };
55         char *statestring = NULL, *pidfstate = NULL, *pidfnote = NULL;
56         pj_xml_attr *attr;
57         enum ast_sip_pidf_state local_state;
58         pj_str_t uri;
59         char sanitized[PJSIP_MAX_URL_SIZE];
60         pj_xml_node *atom;
61         pj_xml_node *address;
62         pj_xml_node *status;
63         pj_xml_node *msnsubstatus;
64
65         ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
66                         &pidfstate, &pidfnote, &local_state, 0);
67
68         ast_sip_presence_xml_find_node_attr(state_data->pool, pres, "atom", "id",
69                         &atom, &attr);
70         pj_strdup2(state_data->pool, &attr->value, state_data->exten);
71
72         ast_sip_presence_xml_find_node_attr(state_data->pool, atom, "address",
73                         "uri", &address, &attr);
74
75         ast_sip_sanitize_xml(state_data->remote, sanitized, sizeof(sanitized));
76
77         uri.ptr = (char*) pj_pool_alloc(state_data->pool,
78                         strlen(sanitized) + STR_ADDR_PARAM.slen);
79         pj_strcpy2( &uri, sanitized);
80         pj_strcat( &uri, &STR_ADDR_PARAM);
81         pj_strdup(state_data->pool, &attr->value, &uri);
82
83         ast_sip_presence_xml_create_attr(state_data->pool, address, "priority", "0.80000");
84
85         ast_sip_presence_xml_find_node_attr(state_data->pool, address,
86                         "status", "status", &status, &attr);
87         pj_strdup2(state_data->pool, &attr->value,
88                    (local_state ==  NOTIFY_OPEN) ? "open" :
89                    (local_state == NOTIFY_INUSE) ? "inuse" : "closed");
90
91         ast_sip_presence_xml_find_node_attr(state_data->pool, address,
92                         "msnsubstatus", "substatus", &msnsubstatus, &attr);
93         pj_strdup2(state_data->pool, &attr->value,
94                    (local_state == NOTIFY_OPEN) ? "online" :
95                    (local_state == NOTIFY_INUSE) ? "onthephone" : "offline");
96
97         return 0;
98 }
99
100 #define MAX_STRING_GROWTHS 5
101
102 static void xpidf_to_string(void *body, struct ast_str **str)
103 {
104         pjxpidf_pres *pres = body;
105         int growths = 0;
106         int size;
107
108         do {
109                 size = pjxpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str) - 1);
110                 if (size <= AST_PJSIP_XML_PROLOG_LEN) {
111                         ast_str_make_space(str, ast_str_size(*str) * 2);
112                         ++growths;
113                 }
114         } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
115         if (size <= AST_PJSIP_XML_PROLOG_LEN) {
116                 ast_log(LOG_WARNING, "XPIDF body text too large\n");
117                 return;
118         }
119
120         *(ast_str_buffer(*str) + size) = '\0';
121         ast_str_update(*str);
122 }
123
124 static struct ast_sip_pubsub_body_generator xpidf_body_generator = {
125         .type = "application",
126         .subtype = "xpidf+xml",
127         .body_type = AST_SIP_EXTEN_STATE_DATA,
128         .allocate_body = xpidf_allocate_body,
129         .generate_body_content = xpidf_generate_body_content,
130         .to_string = xpidf_to_string,
131         /* No need for a destroy_body callback since we use a pool */
132 };
133
134 static struct ast_sip_pubsub_body_generator cpim_pidf_body_generator = {
135         .type = "application",
136         .subtype = "cpim-pidf+xml",
137         .body_type = AST_SIP_EXTEN_STATE_DATA,
138         .allocate_body = xpidf_allocate_body,
139         .generate_body_content = xpidf_generate_body_content,
140         .to_string = xpidf_to_string,
141         /* No need for a destroy_body callback since we use a pool */
142 };
143
144 static void unregister_all(void)
145 {
146         ast_sip_pubsub_unregister_body_generator(&cpim_pidf_body_generator);
147         ast_sip_pubsub_unregister_body_generator(&xpidf_body_generator);
148 }
149
150 static int load_module(void)
151 {
152         CHECK_PJSIP_PUBSUB_MODULE_LOADED();
153
154         if (ast_sip_pubsub_register_body_generator(&xpidf_body_generator)) {
155                 goto fail;
156         }
157
158         if (ast_sip_pubsub_register_body_generator(&cpim_pidf_body_generator)) {
159                 goto fail;
160         }
161
162         return AST_MODULE_LOAD_SUCCESS;
163
164 fail:
165         unregister_all();
166         return AST_MODULE_LOAD_DECLINE;
167 }
168
169 static int unload_module(void)
170 {
171         unregister_all();
172         return 0;
173 }
174
175 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State PIDF Provider",
176         .support_level = AST_MODULE_SUPPORT_CORE,
177         .load = load_module,
178         .unload = unload_module,
179         .load_pri = AST_MODPRI_CHANNEL_DEPEND,
180 );