Debian Codegraph @

res/res_pjsip_xpidf_body_generator.c

raw symbols SCIP
1 /*
2 * asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Digium, Inc.
5 *
6 * Mark Michelson <[email protected]>
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 static void *xpidf_allocate_body(void *data)
39 {
40 struct ast_sip_exten_state_data *state_data = data;
41 char *local = ast_strdupa(state_data->local);
42 pjxpidf_pres *pres;
43 pj_str_t name;
44
45 pres = pjxpidf_create(state_data->pool, pj_cstr(&name, ast_strip_quoted(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, 0);
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 5
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 size = pjxpidf_print(pres, ast_str_buffer(*str), ast_str_size(*str) - 1);
109 if (size <= AST_PJSIP_XML_PROLOG_LEN) {
110 ast_str_make_space(str, ast_str_size(*str) * 2);
111 ++growths;
112 }
113 } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
114 if (size <= AST_PJSIP_XML_PROLOG_LEN) {
115 ast_log(LOG_WARNING, "XPIDF body text too large\n");
116 return;
117 }
118
119 *(ast_str_buffer(*str) + size) = '\0';
120 ast_str_update(*str);
121 }
122
123 static struct ast_sip_pubsub_body_generator xpidf_body_generator = {
124 .type = "application",
125 .subtype = "xpidf+xml",
126 .body_type = AST_SIP_EXTEN_STATE_DATA,
127 .allocate_body = xpidf_allocate_body,
128 .generate_body_content = xpidf_generate_body_content,
129 .to_string = xpidf_to_string,
130 /* No need for a destroy_body callback since we use a pool */
131 };
132
133 static struct ast_sip_pubsub_body_generator cpim_pidf_body_generator = {
134 .type = "application",
135 .subtype = "cpim-pidf+xml",
136 .body_type = AST_SIP_EXTEN_STATE_DATA,
137 .allocate_body = xpidf_allocate_body,
138 .generate_body_content = xpidf_generate_body_content,
139 .to_string = xpidf_to_string,
140 /* No need for a destroy_body callback since we use a pool */
141 };
142
143 static void unregister_all(void)
144 {
145 ast_sip_pubsub_unregister_body_generator(&cpim_pidf_body_generator);
146 ast_sip_pubsub_unregister_body_generator(&xpidf_body_generator);
147 }
148
149 static int load_module(void)
150 {
151 if (ast_sip_pubsub_register_body_generator(&xpidf_body_generator)) {
152 goto fail;
153 }
154
155 if (ast_sip_pubsub_register_body_generator(&cpim_pidf_body_generator)) {
156 goto fail;
157 }
158
159 return AST_MODULE_LOAD_SUCCESS;
160
161 fail:
162 unregister_all();
163 return AST_MODULE_LOAD_DECLINE;
164 }
165
166 static int unload_module(void)
167 {
168 unregister_all();
169 return 0;
170 }
171
172 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State PIDF Provider",
173 .support_level = AST_MODULE_SUPPORT_CORE,
174 .load = load_module,
175 .unload = unload_module,
176 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
177 .requires = "res_pjsip,res_pjsip_pubsub",
178 );