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