1
/* $Id: presence_body.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 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
#include <pjsip-simple/presence.h>
21
#include <pjsip-simple/errno.h>
22
#include <pjsip/sip_msg.h>
23
#include <pjsip/sip_transport.h>
28
#include <pj/string.h>
31
#define THIS_FILE "presence_body.c"
34
static const pj_str_t STR_APPLICATION = { "application", 11 };
35
static const pj_str_t STR_PIDF_XML = { "pidf+xml", 8 };
36
static const pj_str_t STR_XPIDF_XML = { "xpidf+xml", 9 };
42
* Function to print XML message body.
44
static int pres_print_body(struct pjsip_msg_body *msg_body,
45
char *buf, pj_size_t size)
47
return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size,
53
* Function to clone XML document.
55
static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
58
return pj_xml_clone( pool, (const pj_xml_node*) data);
63
* This is a utility function to create PIDF message body from PJSIP
64
* presence status (pjsip_pres_status).
66
PJ_DEF(pj_status_t) pjsip_pres_create_pidf( pj_pool_t *pool,
67
const pjsip_pres_status *status,
68
const pj_str_t *entity,
69
pjsip_msg_body **p_body )
75
/* Create <presence>. */
76
pidf = pjpidf_create(pool, entity);
79
for (i=0; i<status->info_cnt; ++i) {
81
pjpidf_tuple *pidf_tuple;
82
pjpidf_status *pidf_status;
86
if (status->info[i].id.slen == 0) {
87
/* xs:ID must start with letter */
88
//pj_create_unique_string(pool, &id);
89
id.ptr = (char*)pj_pool_alloc(pool, PJ_GUID_STRING_LENGTH+2);
91
pj_generate_unique_string(&id);
97
id = status->info[i].id;
100
pidf_tuple = pjpidf_pres_add_tuple(pool, pidf, &id);
103
if (status->info[i].contact.slen)
104
pjpidf_tuple_set_contact(pool, pidf_tuple,
105
&status->info[i].contact);
108
/* Set basic status */
109
pidf_status = pjpidf_tuple_get_status(pidf_tuple);
110
pjpidf_status_set_basic_open(pidf_status,
111
status->info[i].basic_open);
113
/* Add <timestamp> if configured */
114
#if defined(PJSIP_PRES_PIDF_ADD_TIMESTAMP) && PJSIP_PRES_PIDF_ADD_TIMESTAMP
115
if (PJSIP_PRES_PIDF_ADD_TIMESTAMP) {
121
pj_gettimeofday(&tv);
122
/* TODO: convert time to GMT! (unsupported by pjlib) */
123
pj_time_decode( &tv, &pt);
125
tslen = pj_ansi_snprintf(buf, sizeof(buf),
126
"%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
127
pt.year, pt.mon+1, pt.day,
128
pt.hour, pt.min, pt.sec, pt.msec);
129
if (tslen > 0 && tslen < (int)sizeof(buf)) {
130
pj_str_t time = pj_str(buf);
131
pjpidf_tuple_set_timestamp(pool, pidf_tuple, &time);
137
/* Create <person> (RPID) */
138
if (status->info_cnt) {
139
pjrpid_add_element(pidf, pool, 0, &status->info[0].rpid);
142
body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
144
body->content_type.type = STR_APPLICATION;
145
body->content_type.subtype = STR_PIDF_XML;
146
body->print_body = &pres_print_body;
147
body->clone_data = &xml_clone_data;
156
* This is a utility function to create X-PIDF message body from PJSIP
157
* presence status (pjsip_pres_status).
159
PJ_DEF(pj_status_t) pjsip_pres_create_xpidf( pj_pool_t *pool,
160
const pjsip_pres_status *status,
161
const pj_str_t *entity,
162
pjsip_msg_body **p_body )
164
/* Note: PJSIP implementation of XPIDF is not complete!
167
pjsip_msg_body *body;
169
PJ_LOG(4,(THIS_FILE, "Warning: XPIDF format is not fully supported "
172
/* Create XPIDF document. */
173
xpidf = pjxpidf_create(pool, entity);
175
/* Set basic status. */
176
if (status->info_cnt > 0)
177
pjxpidf_set_status( xpidf, status->info[0].basic_open);
179
pjxpidf_set_status( xpidf, PJ_FALSE);
181
body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
183
body->content_type.type = STR_APPLICATION;
184
body->content_type.subtype = STR_XPIDF_XML;
185
body->print_body = &pres_print_body;
186
body->clone_data = &xml_clone_data;
196
* This is a utility function to parse PIDF body into PJSIP presence status.
198
PJ_DEF(pj_status_t) pjsip_pres_parse_pidf( pjsip_rx_data *rdata,
200
pjsip_pres_status *pres_status)
202
return pjsip_pres_parse_pidf2((char*)rdata->msg_info.msg->body->data,
203
rdata->msg_info.msg->body->len,
207
PJ_DEF(pj_status_t) pjsip_pres_parse_pidf2(char *body, unsigned body_len,
209
pjsip_pres_status *pres_status)
212
pjpidf_tuple *pidf_tuple;
214
pidf = pjpidf_parse(pool, body, body_len);
216
return PJSIP_SIMPLE_EBADPIDF;
218
pres_status->info_cnt = 0;
220
pidf_tuple = pjpidf_pres_get_first_tuple(pidf);
221
while (pidf_tuple && pres_status->info_cnt < PJSIP_PRES_STATUS_MAX_INFO) {
222
pjpidf_status *pidf_status;
224
pres_status->info[pres_status->info_cnt].tuple_node =
225
pj_xml_clone(pool, pidf_tuple);
228
&pres_status->info[pres_status->info_cnt].id,
229
pjpidf_tuple_get_id(pidf_tuple));
232
&pres_status->info[pres_status->info_cnt].contact,
233
pjpidf_tuple_get_contact(pidf_tuple));
235
pidf_status = pjpidf_tuple_get_status(pidf_tuple);
237
pres_status->info[pres_status->info_cnt].basic_open =
238
pjpidf_status_is_basic_open(pidf_status);
240
pres_status->info[pres_status->info_cnt].basic_open = PJ_FALSE;
243
pidf_tuple = pjpidf_pres_get_next_tuple( pidf, pidf_tuple );
244
pres_status->info_cnt++;
247
/* Parse <person> (RPID) */
248
pjrpid_get_element(pidf, pool, &pres_status->info[0].rpid);
255
* This is a utility function to parse X-PIDF body into PJSIP presence status.
257
PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf(pjsip_rx_data *rdata,
259
pjsip_pres_status *pres_status)
261
return pjsip_pres_parse_xpidf2((char*)rdata->msg_info.msg->body->data,
262
rdata->msg_info.msg->body->len,
266
PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf2(char *body, unsigned body_len,
268
pjsip_pres_status *pres_status)
272
xpidf = pjxpidf_parse(pool, body, body_len);
274
return PJSIP_SIMPLE_EBADXPIDF;
276
pres_status->info_cnt = 1;
279
&pres_status->info[0].contact,
280
pjxpidf_get_uri(xpidf));
281
pres_status->info[0].basic_open = pjxpidf_get_status(xpidf);
282
pres_status->info[0].id.slen = 0;
283
pres_status->info[0].tuple_node = NULL;