blob: c67890c1872d830f25011a2f861528ebe0e15e48 [file] [log] [blame]
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjsip-simple/presence.h>
20#include <pjsip-simple/errno.h>
21#include <pjsip/sip_msg.h>
22#include <pjsip/sip_transport.h>
23#include <pj/guid.h>
24#include <pj/log.h>
25#include <pj/pool.h>
26#include <pj/string.h>
27
28
29#define THIS_FILE "presence_body.c"
30
31
32static const pj_str_t STR_APPLICATION = { "application", 11 };
33static const pj_str_t STR_PIDF_XML = { "pidf+xml", 8 };
34static const pj_str_t STR_XPIDF_XML = { "xpidf+xml", 9 };
35
36
37
38
39/*
40 * Function to print XML message body.
41 */
42static int pres_print_body(struct pjsip_msg_body *msg_body,
43 char *buf, pj_size_t size)
44{
45 return pj_xml_print(msg_body->data, buf, size, PJ_TRUE);
46}
47
48
49/*
50 * Function to clone XML document.
51 */
52static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
53{
54 PJ_UNUSED_ARG(len);
55 return pj_xml_clone( pool, data);
56}
57
58
59/*
60 * This is a utility function to create PIDF message body from PJSIP
61 * presence status (pjsip_pres_status).
62 */
63PJ_DEF(pj_status_t) pjsip_pres_create_pidf( pj_pool_t *pool,
64 const pjsip_pres_status *status,
65 const pj_str_t *entity,
66 pjsip_msg_body **p_body )
67{
68 pjpidf_pres *pidf;
69 pjsip_msg_body *body;
70 unsigned i;
71
72 /* Create <presence>. */
73 pidf = pjpidf_create(pool, entity);
74
75 /* Create <tuple> */
76 for (i=0; i<status->info_cnt; ++i) {
77
78 pjpidf_tuple *pidf_tuple;
79 pjpidf_status *pidf_status;
80 pj_str_t id;
81
82 /* Add tuple id. */
83 if (status->info[i].id.slen == 0) {
84 pj_create_unique_string(pool, &id);
85 } else {
86 id = status->info[i].id;
87 }
88
89 pidf_tuple = pjpidf_pres_add_tuple(pool, pidf, &id);
90
91 /* Set <contact> */
92 if (status->info[i].contact.slen)
93 pjpidf_tuple_set_contact(pool, pidf_tuple,
94 &status->info[i].contact);
95
96
97 /* Set basic status */
98 pidf_status = pjpidf_tuple_get_status(pidf_tuple);
99 pjpidf_status_set_basic_open(pidf_status,
100 status->info[i].basic_open);
101 }
102
103 body = pj_pool_zalloc(pool, sizeof(pjsip_msg_body));
104 body->data = pidf;
105 body->content_type.type = STR_APPLICATION;
106 body->content_type.subtype = STR_PIDF_XML;
107 body->print_body = &pres_print_body;
108 body->clone_data = &xml_clone_data;
109
110 *p_body = body;
111
112 return PJ_SUCCESS;
113}
114
115
116/*
117 * This is a utility function to create X-PIDF message body from PJSIP
118 * presence status (pjsip_pres_status).
119 */
120PJ_DEF(pj_status_t) pjsip_pres_create_xpidf( pj_pool_t *pool,
121 const pjsip_pres_status *status,
122 const pj_str_t *entity,
123 pjsip_msg_body **p_body )
124{
125 /* Note: PJSIP implementation of XPIDF is not complete!
126 */
127 pjxpidf_pres *xpidf;
128 pjsip_msg_body *body;
129
130 PJ_LOG(4,(THIS_FILE, "Warning: XPIDF format is not fully supported "
131 "by PJSIP"));
132
133 /* Create XPIDF document. */
134 xpidf = pjxpidf_create(pool, entity);
135
136 /* Set basic status. */
137 if (status->info_cnt > 0)
138 pjxpidf_set_status( xpidf, status->info[0].basic_open);
139 else
140 pjxpidf_set_status( xpidf, PJ_FALSE);
141
142 body = pj_pool_zalloc(pool, sizeof(pjsip_msg_body));
143 body->data = xpidf;
144 body->content_type.type = STR_APPLICATION;
145 body->content_type.subtype = STR_XPIDF_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/*
157 * This is a utility function to parse PIDF body into PJSIP presence status.
158 */
159PJ_DEF(pj_status_t) pjsip_pres_parse_pidf( pjsip_rx_data *rdata,
160 pj_pool_t *pool,
161 pjsip_pres_status *pres_status)
162{
163 pjpidf_pres *pidf;
164 pjpidf_tuple *pidf_tuple;
165
166 pidf = pjpidf_parse(rdata->tp_info.pool,
167 rdata->msg_info.msg->body->data,
168 rdata->msg_info.msg->body->len);
169 if (pidf == NULL)
170 return PJSIP_SIMPLE_EBADPIDF;
171
172 pres_status->info_cnt = 0;
173
174 pidf_tuple = pjpidf_pres_get_first_tuple(pidf);
175 while (pidf_tuple) {
176 pjpidf_status *pidf_status;
177
178 pj_strdup(pool,
179 &pres_status->info[pres_status->info_cnt].id,
180 pjpidf_tuple_get_id(pidf_tuple));
181
182 pj_strdup(pool,
183 &pres_status->info[pres_status->info_cnt].contact,
184 pjpidf_tuple_get_contact(pidf_tuple));
185
186 pidf_status = pjpidf_tuple_get_status(pidf_tuple);
187 if (pidf_status) {
188 pres_status->info[pres_status->info_cnt].basic_open =
189 pjpidf_status_is_basic_open(pidf_status);
190 } else {
191 pres_status->info[pres_status->info_cnt].basic_open = PJ_FALSE;
192 }
193
194 pidf_tuple = pjpidf_pres_get_next_tuple( pidf, pidf_tuple );
195 pres_status->info_cnt++;
196 }
197
198 return PJ_SUCCESS;
199}
200
201
202/*
203 * This is a utility function to parse X-PIDF body into PJSIP presence status.
204 */
205PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf(pjsip_rx_data *rdata,
206 pj_pool_t *pool,
207 pjsip_pres_status *pres_status)
208{
209 pjxpidf_pres *xpidf;
210
211 xpidf = pjxpidf_parse(rdata->tp_info.pool,
212 rdata->msg_info.msg->body->data,
213 rdata->msg_info.msg->body->len);
214 if (xpidf == NULL)
215 return PJSIP_SIMPLE_EBADXPIDF;
216
217 pres_status->info_cnt = 1;
218
219 pj_strdup(pool,
220 &pres_status->info[0].contact,
221 pjxpidf_get_uri(xpidf));
222 pres_status->info[0].basic_open = pjxpidf_get_status(xpidf);
223 pres_status->info[0].id.slen = 0;
224
225 return PJ_SUCCESS;
226}
227
228