blob: 8754735adcdb228f243692fa0ea9cfe7f827f825 [file] [log] [blame]
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 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) {
87 pj_create_unique_string(pool, &id);
88 } else {
89 id = status->info[i].id;
90 }
91
92 pidf_tuple = pjpidf_pres_add_tuple(pool, pidf, &id);
93
94 /* Set <contact> */
95 if (status->info[i].contact.slen)
96 pjpidf_tuple_set_contact(pool, pidf_tuple,
97 &status->info[i].contact);
98
99
100 /* Set basic status */
101 pidf_status = pjpidf_tuple_get_status(pidf_tuple);
102 pjpidf_status_set_basic_open(pidf_status,
103 status->info[i].basic_open);
Benny Prijono28add7e2009-06-15 16:03:40 +0000104
105 /* Add <timestamp> if configured */
106#if defined(PJSIP_PRES_PIDF_ADD_TIMESTAMP) && PJSIP_PRES_PIDF_ADD_TIMESTAMP
107 if (PJSIP_PRES_PIDF_ADD_TIMESTAMP) {
108 char buf[50];
109 int tslen = 0;
110 pj_time_val tv;
111 pj_parsed_time pt;
112
113 pj_gettimeofday(&tv);
114 /* TODO: convert time to GMT! (unsupported by pjlib) */
115 pj_time_decode( &tv, &pt);
116
117 tslen = pj_ansi_snprintf(buf, sizeof(buf),
118 "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
119 pt.year, pt.mon, pt.day,
120 pt.hour, pt.min, pt.sec, pt.msec);
121 if (tslen > 0 && tslen < sizeof(buf)) {
122 pj_str_t time = pj_str(buf);
123 pjpidf_tuple_set_timestamp(pool, pidf_tuple, &time);
124 }
125 }
126#endif
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000127 }
128
Benny Prijono4461c7d2007-08-25 13:36:15 +0000129 /* Create <person> (RPID) */
130 if (status->info_cnt) {
131 pjrpid_add_element(pidf, pool, 0, &status->info[0].rpid);
132 }
133
Benny Prijono9d4469d2007-05-02 05:14:29 +0000134 body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000135 body->data = pidf;
136 body->content_type.type = STR_APPLICATION;
137 body->content_type.subtype = STR_PIDF_XML;
138 body->print_body = &pres_print_body;
139 body->clone_data = &xml_clone_data;
140
141 *p_body = body;
142
143 return PJ_SUCCESS;
144}
145
146
147/*
148 * This is a utility function to create X-PIDF message body from PJSIP
149 * presence status (pjsip_pres_status).
150 */
151PJ_DEF(pj_status_t) pjsip_pres_create_xpidf( pj_pool_t *pool,
152 const pjsip_pres_status *status,
153 const pj_str_t *entity,
154 pjsip_msg_body **p_body )
155{
156 /* Note: PJSIP implementation of XPIDF is not complete!
157 */
158 pjxpidf_pres *xpidf;
159 pjsip_msg_body *body;
160
161 PJ_LOG(4,(THIS_FILE, "Warning: XPIDF format is not fully supported "
162 "by PJSIP"));
163
164 /* Create XPIDF document. */
165 xpidf = pjxpidf_create(pool, entity);
166
167 /* Set basic status. */
168 if (status->info_cnt > 0)
169 pjxpidf_set_status( xpidf, status->info[0].basic_open);
170 else
171 pjxpidf_set_status( xpidf, PJ_FALSE);
172
Benny Prijono9d4469d2007-05-02 05:14:29 +0000173 body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000174 body->data = xpidf;
175 body->content_type.type = STR_APPLICATION;
176 body->content_type.subtype = STR_XPIDF_XML;
177 body->print_body = &pres_print_body;
178 body->clone_data = &xml_clone_data;
179
180 *p_body = body;
181
182 return PJ_SUCCESS;
183}
184
185
186
187/*
188 * This is a utility function to parse PIDF body into PJSIP presence status.
189 */
190PJ_DEF(pj_status_t) pjsip_pres_parse_pidf( pjsip_rx_data *rdata,
191 pj_pool_t *pool,
192 pjsip_pres_status *pres_status)
193{
194 pjpidf_pres *pidf;
195 pjpidf_tuple *pidf_tuple;
196
197 pidf = pjpidf_parse(rdata->tp_info.pool,
Benny Prijono9d4469d2007-05-02 05:14:29 +0000198 (char*)rdata->msg_info.msg->body->data,
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000199 rdata->msg_info.msg->body->len);
200 if (pidf == NULL)
201 return PJSIP_SIMPLE_EBADPIDF;
202
203 pres_status->info_cnt = 0;
204
205 pidf_tuple = pjpidf_pres_get_first_tuple(pidf);
Benny Prijono28add7e2009-06-15 16:03:40 +0000206 while (pidf_tuple && pres_status->info_cnt < PJSIP_PRES_STATUS_MAX_INFO) {
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000207 pjpidf_status *pidf_status;
208
Benny Prijono28add7e2009-06-15 16:03:40 +0000209 pres_status->info[pres_status->info_cnt].tuple_node =
210 pj_xml_clone(pool, pidf_tuple);
211
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000212 pj_strdup(pool,
213 &pres_status->info[pres_status->info_cnt].id,
214 pjpidf_tuple_get_id(pidf_tuple));
215
216 pj_strdup(pool,
217 &pres_status->info[pres_status->info_cnt].contact,
218 pjpidf_tuple_get_contact(pidf_tuple));
219
220 pidf_status = pjpidf_tuple_get_status(pidf_tuple);
221 if (pidf_status) {
222 pres_status->info[pres_status->info_cnt].basic_open =
223 pjpidf_status_is_basic_open(pidf_status);
224 } else {
225 pres_status->info[pres_status->info_cnt].basic_open = PJ_FALSE;
226 }
227
228 pidf_tuple = pjpidf_pres_get_next_tuple( pidf, pidf_tuple );
229 pres_status->info_cnt++;
230 }
231
Benny Prijono4461c7d2007-08-25 13:36:15 +0000232 /* Parse <person> (RPID) */
233 pjrpid_get_element(pidf, pool, &pres_status->info[0].rpid);
234
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000235 return PJ_SUCCESS;
236}
237
238
239/*
240 * This is a utility function to parse X-PIDF body into PJSIP presence status.
241 */
242PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf(pjsip_rx_data *rdata,
243 pj_pool_t *pool,
244 pjsip_pres_status *pres_status)
245{
246 pjxpidf_pres *xpidf;
247
248 xpidf = pjxpidf_parse(rdata->tp_info.pool,
Benny Prijono9d4469d2007-05-02 05:14:29 +0000249 (char*)rdata->msg_info.msg->body->data,
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000250 rdata->msg_info.msg->body->len);
251 if (xpidf == NULL)
252 return PJSIP_SIMPLE_EBADXPIDF;
253
254 pres_status->info_cnt = 1;
255
256 pj_strdup(pool,
257 &pres_status->info[0].contact,
258 pjxpidf_get_uri(xpidf));
259 pres_status->info[0].basic_open = pjxpidf_get_status(xpidf);
260 pres_status->info[0].id.slen = 0;
Benny Prijono28add7e2009-06-15 16:03:40 +0000261 pres_status->info[0].tuple_node = NULL;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000262
263 return PJ_SUCCESS;
264}
265
266