1
/* $Id: presence_body.c 3045 2010-01-05 15:23:43Z bennylp $ */
3
* Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
* Additional permission under GNU GPL version 3 section 7:
22
* If you modify this program, or any covered work, by linking or
23
* combining it with the OpenSSL project's OpenSSL library (or a
24
* modified version of that library), containing parts covered by the
25
* terms of the OpenSSL or SSLeay licenses, Teluu Inc. (http://www.teluu.com)
26
* grants you additional permission to convey the resulting work.
27
* Corresponding Source for a non-source form of such a combination
28
* shall include the source code for the parts of OpenSSL used as well
29
* as that of the covered work.
31
#include <pjsip-simple/presence.h>
32
#include <pjsip-simple/errno.h>
33
#include <pjsip/sip_msg.h>
34
#include <pjsip/sip_transport.h>
39
#include <pj/string.h>
42
#define THIS_FILE "presence_body.c"
45
static const pj_str_t STR_APPLICATION = { "application", 11 };
46
static const pj_str_t STR_PIDF_XML = { "pidf+xml", 8 };
47
static const pj_str_t STR_XPIDF_XML = { "xpidf+xml", 9 };
53
* Function to print XML message body.
55
static int pres_print_body(struct pjsip_msg_body *msg_body,
56
char *buf, pj_size_t size)
58
return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size,
64
* Function to clone XML document.
66
static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
69
return pj_xml_clone( pool, (const pj_xml_node*) data);
74
* This is a utility function to create PIDF message body from PJSIP
75
* presence status (pjsip_pres_status).
77
PJ_DEF(pj_status_t) pjsip_pres_create_pidf( pj_pool_t *pool,
78
const pjsip_pres_status *status,
79
const pj_str_t *entity,
80
pjsip_msg_body **p_body )
86
/* Create <presence>. */
87
pidf = pjpidf_create(pool, entity);
90
for (i=0; i<status->info_cnt; ++i) {
92
pjpidf_tuple *pidf_tuple;
93
pjpidf_status *pidf_status;
97
if (status->info[i].id.slen == 0) {
98
/* xs:ID must start with letter */
99
//pj_create_unique_string(pool, &id);
100
id.ptr = (char*)pj_pool_alloc(pool, PJ_GUID_STRING_LENGTH+2);
102
pj_generate_unique_string(&id);
108
id = status->info[i].id;
111
pidf_tuple = pjpidf_pres_add_tuple(pool, pidf, &id);
114
if (status->info[i].contact.slen)
115
pjpidf_tuple_set_contact(pool, pidf_tuple,
116
&status->info[i].contact);
119
/* Set basic status */
120
pidf_status = pjpidf_tuple_get_status(pidf_tuple);
121
pjpidf_status_set_basic_open(pidf_status,
122
status->info[i].basic_open);
124
/* Add <timestamp> if configured */
125
#if defined(PJSIP_PRES_PIDF_ADD_TIMESTAMP) && PJSIP_PRES_PIDF_ADD_TIMESTAMP
126
if (PJSIP_PRES_PIDF_ADD_TIMESTAMP) {
132
pj_gettimeofday(&tv);
133
/* TODO: convert time to GMT! (unsupported by pjlib) */
134
pj_time_decode( &tv, &pt);
136
tslen = pj_ansi_snprintf(buf, sizeof(buf),
137
"%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
138
pt.year, pt.mon+1, pt.day,
139
pt.hour, pt.min, pt.sec, pt.msec);
140
if (tslen > 0 && tslen < sizeof(buf)) {
141
pj_str_t time = pj_str(buf);
142
pjpidf_tuple_set_timestamp(pool, pidf_tuple, &time);
148
/* Create <person> (RPID) */
149
if (status->info_cnt) {
150
pjrpid_add_element(pidf, pool, 0, &status->info[0].rpid);
153
body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
155
body->content_type.type = STR_APPLICATION;
156
body->content_type.subtype = STR_PIDF_XML;
157
body->print_body = &pres_print_body;
158
body->clone_data = &xml_clone_data;
167
* This is a utility function to create X-PIDF message body from PJSIP
168
* presence status (pjsip_pres_status).
170
PJ_DEF(pj_status_t) pjsip_pres_create_xpidf( pj_pool_t *pool,
171
const pjsip_pres_status *status,
172
const pj_str_t *entity,
173
pjsip_msg_body **p_body )
175
/* Note: PJSIP implementation of XPIDF is not complete!
178
pjsip_msg_body *body;
180
PJ_LOG(4,(THIS_FILE, "Warning: XPIDF format is not fully supported "
183
/* Create XPIDF document. */
184
xpidf = pjxpidf_create(pool, entity);
186
/* Set basic status. */
187
if (status->info_cnt > 0)
188
pjxpidf_set_status( xpidf, status->info[0].basic_open);
190
pjxpidf_set_status( xpidf, PJ_FALSE);
192
body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
194
body->content_type.type = STR_APPLICATION;
195
body->content_type.subtype = STR_XPIDF_XML;
196
body->print_body = &pres_print_body;
197
body->clone_data = &xml_clone_data;
207
* This is a utility function to parse PIDF body into PJSIP presence status.
209
PJ_DEF(pj_status_t) pjsip_pres_parse_pidf( pjsip_rx_data *rdata,
211
pjsip_pres_status *pres_status)
214
pjpidf_tuple *pidf_tuple;
216
pidf = pjpidf_parse(rdata->tp_info.pool,
217
(char*)rdata->msg_info.msg->body->data,
218
rdata->msg_info.msg->body->len);
220
return PJSIP_SIMPLE_EBADPIDF;
222
pres_status->info_cnt = 0;
224
pidf_tuple = pjpidf_pres_get_first_tuple(pidf);
225
while (pidf_tuple && pres_status->info_cnt < PJSIP_PRES_STATUS_MAX_INFO) {
226
pjpidf_status *pidf_status;
228
pres_status->info[pres_status->info_cnt].tuple_node =
229
pj_xml_clone(pool, pidf_tuple);
232
&pres_status->info[pres_status->info_cnt].id,
233
pjpidf_tuple_get_id(pidf_tuple));
236
&pres_status->info[pres_status->info_cnt].contact,
237
pjpidf_tuple_get_contact(pidf_tuple));
239
pidf_status = pjpidf_tuple_get_status(pidf_tuple);
241
pres_status->info[pres_status->info_cnt].basic_open =
242
pjpidf_status_is_basic_open(pidf_status);
244
pres_status->info[pres_status->info_cnt].basic_open = PJ_FALSE;
247
pidf_tuple = pjpidf_pres_get_next_tuple( pidf, pidf_tuple );
248
pres_status->info_cnt++;
251
/* Parse <person> (RPID) */
252
pjrpid_get_element(pidf, pool, &pres_status->info[0].rpid);
259
* This is a utility function to parse X-PIDF body into PJSIP presence status.
261
PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf(pjsip_rx_data *rdata,
263
pjsip_pres_status *pres_status)
267
xpidf = pjxpidf_parse(rdata->tp_info.pool,
268
(char*)rdata->msg_info.msg->body->data,
269
rdata->msg_info.msg->body->len);
271
return PJSIP_SIMPLE_EBADXPIDF;
273
pres_status->info_cnt = 1;
276
&pres_status->info[0].contact,
277
pjxpidf_get_uri(xpidf));
278
pres_status->info[0].basic_open = pjxpidf_get_status(xpidf);
279
pres_status->info[0].id.slen = 0;
280
pres_status->info[0].tuple_node = NULL;