blob: 221228ef42bc9ec91df3fc3e7ab6cf8e795793e1 [file] [log] [blame]
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001/* $Id$ */
2/*
Nanang Izzuddina62ffc92011-05-05 06:14:19 +00003 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00005 *
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.
10 *
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.
15 *
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
19 */
20#include <pjsip-simple/presence.h>
21#include <pjsip-simple/errno.h>
22#include <pjsip/sip_msg.h>
23#include <pjsip/sip_transport.h>
24#include <pj/guid.h>
25#include <pj/log.h>
Nanang Izzuddin560e2862009-06-17 10:37:18 +000026#include <pj/os.h>
Benny Prijono3a5e1ab2006-08-15 20:26:34 +000027#include <pj/pool.h>
28#include <pj/string.h>
29
30
31#define THIS_FILE "presence_body.c"
32
33
34static const pj_str_t STR_APPLICATION = { "application", 11 };
35static const pj_str_t STR_PIDF_XML = { "pidf+xml", 8 };
36static const pj_str_t STR_XPIDF_XML = { "xpidf+xml", 9 };
37
38
39
40
41/*
42 * Function to print XML message body.
43 */
44static int pres_print_body(struct pjsip_msg_body *msg_body,
45 char *buf, pj_size_t size)
46{
Benny Prijono9d4469d2007-05-02 05:14:29 +000047 return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size,
48 PJ_TRUE);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +000049}
50
51
52/*
53 * Function to clone XML document.
54 */
55static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
56{
57 PJ_UNUSED_ARG(len);
Benny Prijono9d4469d2007-05-02 05:14:29 +000058 return pj_xml_clone( pool, (const pj_xml_node*) data);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +000059}
60
61
62/*
63 * This is a utility function to create PIDF message body from PJSIP
64 * presence status (pjsip_pres_status).
65 */
66PJ_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 )
70{
71 pjpidf_pres *pidf;
72 pjsip_msg_body *body;
73 unsigned i;
74
75 /* Create <presence>. */
76 pidf = pjpidf_create(pool, entity);
77
78 /* Create <tuple> */
79 for (i=0; i<status->info_cnt; ++i) {
80
81 pjpidf_tuple *pidf_tuple;
82 pjpidf_status *pidf_status;
83 pj_str_t id;
84
85 /* Add tuple id. */
86 if (status->info[i].id.slen == 0) {
Benny Prijono6f54e8d2009-08-13 16:05:55 +000087 /* xs:ID must start with letter */
Benny Prijono840c3d22009-08-13 16:17:51 +000088 //pj_create_unique_string(pool, &id);
89 id.ptr = (char*)pj_pool_alloc(pool, PJ_GUID_STRING_LENGTH+2);
90 id.ptr += 2;
91 pj_generate_unique_string(&id);
92 id.ptr -= 2;
Benny Prijono6f54e8d2009-08-13 16:05:55 +000093 id.ptr[0] = 'p';
Benny Prijono840c3d22009-08-13 16:17:51 +000094 id.ptr[1] = 'j';
95 id.slen += 2;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +000096 } else {
97 id = status->info[i].id;
98 }
99
100 pidf_tuple = pjpidf_pres_add_tuple(pool, pidf, &id);
101
102 /* Set <contact> */
103 if (status->info[i].contact.slen)
104 pjpidf_tuple_set_contact(pool, pidf_tuple,
105 &status->info[i].contact);
106
107
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);
Benny Prijono28add7e2009-06-15 16:03:40 +0000112
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) {
116 char buf[50];
117 int tslen = 0;
118 pj_time_val tv;
119 pj_parsed_time pt;
120
121 pj_gettimeofday(&tv);
122 /* TODO: convert time to GMT! (unsupported by pjlib) */
123 pj_time_decode( &tv, &pt);
124
125 tslen = pj_ansi_snprintf(buf, sizeof(buf),
126 "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
Benny Prijono98301da2010-01-05 15:23:43 +0000127 pt.year, pt.mon+1, pt.day,
Benny Prijono28add7e2009-06-15 16:03:40 +0000128 pt.hour, pt.min, pt.sec, pt.msec);
Nanang Izzuddind1f054e2010-08-06 07:18:08 +0000129 if (tslen > 0 && tslen < (int)sizeof(buf)) {
Benny Prijono28add7e2009-06-15 16:03:40 +0000130 pj_str_t time = pj_str(buf);
131 pjpidf_tuple_set_timestamp(pool, pidf_tuple, &time);
132 }
133 }
134#endif
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000135 }
136
Benny Prijono4461c7d2007-08-25 13:36:15 +0000137 /* Create <person> (RPID) */
138 if (status->info_cnt) {
139 pjrpid_add_element(pidf, pool, 0, &status->info[0].rpid);
140 }
141
Benny Prijono9d4469d2007-05-02 05:14:29 +0000142 body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000143 body->data = pidf;
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;
148
149 *p_body = body;
150
151 return PJ_SUCCESS;
152}
153
154
155/*
156 * This is a utility function to create X-PIDF message body from PJSIP
157 * presence status (pjsip_pres_status).
158 */
159PJ_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 )
163{
164 /* Note: PJSIP implementation of XPIDF is not complete!
165 */
166 pjxpidf_pres *xpidf;
167 pjsip_msg_body *body;
168
169 PJ_LOG(4,(THIS_FILE, "Warning: XPIDF format is not fully supported "
170 "by PJSIP"));
171
172 /* Create XPIDF document. */
173 xpidf = pjxpidf_create(pool, entity);
174
175 /* Set basic status. */
176 if (status->info_cnt > 0)
177 pjxpidf_set_status( xpidf, status->info[0].basic_open);
178 else
179 pjxpidf_set_status( xpidf, PJ_FALSE);
180
Benny Prijono9d4469d2007-05-02 05:14:29 +0000181 body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000182 body->data = xpidf;
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;
187
188 *p_body = body;
189
190 return PJ_SUCCESS;
191}
192
193
194
195/*
196 * This is a utility function to parse PIDF body into PJSIP presence status.
197 */
198PJ_DEF(pj_status_t) pjsip_pres_parse_pidf( pjsip_rx_data *rdata,
199 pj_pool_t *pool,
200 pjsip_pres_status *pres_status)
201{
Benny Prijonof279c092010-10-12 11:35:55 +0000202 return pjsip_pres_parse_pidf2((char*)rdata->msg_info.msg->body->data,
203 rdata->msg_info.msg->body->len,
204 pool, pres_status);
205}
206
207PJ_DEF(pj_status_t) pjsip_pres_parse_pidf2(char *body, unsigned body_len,
208 pj_pool_t *pool,
209 pjsip_pres_status *pres_status)
210{
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000211 pjpidf_pres *pidf;
212 pjpidf_tuple *pidf_tuple;
213
Benny Prijonof279c092010-10-12 11:35:55 +0000214 pidf = pjpidf_parse(pool, body, body_len);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000215 if (pidf == NULL)
216 return PJSIP_SIMPLE_EBADPIDF;
217
218 pres_status->info_cnt = 0;
219
220 pidf_tuple = pjpidf_pres_get_first_tuple(pidf);
Benny Prijono28add7e2009-06-15 16:03:40 +0000221 while (pidf_tuple && pres_status->info_cnt < PJSIP_PRES_STATUS_MAX_INFO) {
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000222 pjpidf_status *pidf_status;
223
Benny Prijono28add7e2009-06-15 16:03:40 +0000224 pres_status->info[pres_status->info_cnt].tuple_node =
225 pj_xml_clone(pool, pidf_tuple);
226
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000227 pj_strdup(pool,
228 &pres_status->info[pres_status->info_cnt].id,
229 pjpidf_tuple_get_id(pidf_tuple));
230
231 pj_strdup(pool,
232 &pres_status->info[pres_status->info_cnt].contact,
233 pjpidf_tuple_get_contact(pidf_tuple));
234
235 pidf_status = pjpidf_tuple_get_status(pidf_tuple);
236 if (pidf_status) {
237 pres_status->info[pres_status->info_cnt].basic_open =
238 pjpidf_status_is_basic_open(pidf_status);
239 } else {
240 pres_status->info[pres_status->info_cnt].basic_open = PJ_FALSE;
241 }
242
243 pidf_tuple = pjpidf_pres_get_next_tuple( pidf, pidf_tuple );
244 pres_status->info_cnt++;
245 }
246
Benny Prijono4461c7d2007-08-25 13:36:15 +0000247 /* Parse <person> (RPID) */
248 pjrpid_get_element(pidf, pool, &pres_status->info[0].rpid);
249
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000250 return PJ_SUCCESS;
251}
252
253
254/*
255 * This is a utility function to parse X-PIDF body into PJSIP presence status.
256 */
257PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf(pjsip_rx_data *rdata,
258 pj_pool_t *pool,
259 pjsip_pres_status *pres_status)
260{
Benny Prijonof279c092010-10-12 11:35:55 +0000261 return pjsip_pres_parse_xpidf2((char*)rdata->msg_info.msg->body->data,
262 rdata->msg_info.msg->body->len,
263 pool, pres_status);
264}
265
266PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf2(char *body, unsigned body_len,
267 pj_pool_t *pool,
268 pjsip_pres_status *pres_status)
269{
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000270 pjxpidf_pres *xpidf;
271
Benny Prijonof279c092010-10-12 11:35:55 +0000272 xpidf = pjxpidf_parse(pool, body, body_len);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000273 if (xpidf == NULL)
274 return PJSIP_SIMPLE_EBADXPIDF;
275
276 pres_status->info_cnt = 1;
277
278 pj_strdup(pool,
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;
Benny Prijono28add7e2009-06-15 16:03:40 +0000283 pres_status->info[0].tuple_node = NULL;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000284
285 return PJ_SUCCESS;
286}
287
288