blob: c57e427a403d225b392a0aaad3ac673b62a73993 [file] [log] [blame]
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono3a5e1ab2006-08-15 20:26:34 +00004 *
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{
Benny Prijono9d4469d2007-05-02 05:14:29 +000045 return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size,
46 PJ_TRUE);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +000047}
48
49
50/*
51 * Function to clone XML document.
52 */
53static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
54{
55 PJ_UNUSED_ARG(len);
Benny Prijono9d4469d2007-05-02 05:14:29 +000056 return pj_xml_clone( pool, (const pj_xml_node*) data);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +000057}
58
59
60/*
61 * This is a utility function to create PIDF message body from PJSIP
62 * presence status (pjsip_pres_status).
63 */
64PJ_DEF(pj_status_t) pjsip_pres_create_pidf( pj_pool_t *pool,
65 const pjsip_pres_status *status,
66 const pj_str_t *entity,
67 pjsip_msg_body **p_body )
68{
69 pjpidf_pres *pidf;
70 pjsip_msg_body *body;
71 unsigned i;
72
73 /* Create <presence>. */
74 pidf = pjpidf_create(pool, entity);
75
76 /* Create <tuple> */
77 for (i=0; i<status->info_cnt; ++i) {
78
79 pjpidf_tuple *pidf_tuple;
80 pjpidf_status *pidf_status;
81 pj_str_t id;
82
83 /* Add tuple id. */
84 if (status->info[i].id.slen == 0) {
85 pj_create_unique_string(pool, &id);
86 } else {
87 id = status->info[i].id;
88 }
89
90 pidf_tuple = pjpidf_pres_add_tuple(pool, pidf, &id);
91
92 /* Set <contact> */
93 if (status->info[i].contact.slen)
94 pjpidf_tuple_set_contact(pool, pidf_tuple,
95 &status->info[i].contact);
96
97
98 /* Set basic status */
99 pidf_status = pjpidf_tuple_get_status(pidf_tuple);
100 pjpidf_status_set_basic_open(pidf_status,
101 status->info[i].basic_open);
102 }
103
Benny Prijono9d4469d2007-05-02 05:14:29 +0000104 body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000105 body->data = pidf;
106 body->content_type.type = STR_APPLICATION;
107 body->content_type.subtype = STR_PIDF_XML;
108 body->print_body = &pres_print_body;
109 body->clone_data = &xml_clone_data;
110
111 *p_body = body;
112
113 return PJ_SUCCESS;
114}
115
116
117/*
118 * This is a utility function to create X-PIDF message body from PJSIP
119 * presence status (pjsip_pres_status).
120 */
121PJ_DEF(pj_status_t) pjsip_pres_create_xpidf( pj_pool_t *pool,
122 const pjsip_pres_status *status,
123 const pj_str_t *entity,
124 pjsip_msg_body **p_body )
125{
126 /* Note: PJSIP implementation of XPIDF is not complete!
127 */
128 pjxpidf_pres *xpidf;
129 pjsip_msg_body *body;
130
131 PJ_LOG(4,(THIS_FILE, "Warning: XPIDF format is not fully supported "
132 "by PJSIP"));
133
134 /* Create XPIDF document. */
135 xpidf = pjxpidf_create(pool, entity);
136
137 /* Set basic status. */
138 if (status->info_cnt > 0)
139 pjxpidf_set_status( xpidf, status->info[0].basic_open);
140 else
141 pjxpidf_set_status( xpidf, PJ_FALSE);
142
Benny Prijono9d4469d2007-05-02 05:14:29 +0000143 body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000144 body->data = xpidf;
145 body->content_type.type = STR_APPLICATION;
146 body->content_type.subtype = STR_XPIDF_XML;
147 body->print_body = &pres_print_body;
148 body->clone_data = &xml_clone_data;
149
150 *p_body = body;
151
152 return PJ_SUCCESS;
153}
154
155
156
157/*
158 * This is a utility function to parse PIDF body into PJSIP presence status.
159 */
160PJ_DEF(pj_status_t) pjsip_pres_parse_pidf( pjsip_rx_data *rdata,
161 pj_pool_t *pool,
162 pjsip_pres_status *pres_status)
163{
164 pjpidf_pres *pidf;
165 pjpidf_tuple *pidf_tuple;
166
167 pidf = pjpidf_parse(rdata->tp_info.pool,
Benny Prijono9d4469d2007-05-02 05:14:29 +0000168 (char*)rdata->msg_info.msg->body->data,
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000169 rdata->msg_info.msg->body->len);
170 if (pidf == NULL)
171 return PJSIP_SIMPLE_EBADPIDF;
172
173 pres_status->info_cnt = 0;
174
175 pidf_tuple = pjpidf_pres_get_first_tuple(pidf);
176 while (pidf_tuple) {
177 pjpidf_status *pidf_status;
178
179 pj_strdup(pool,
180 &pres_status->info[pres_status->info_cnt].id,
181 pjpidf_tuple_get_id(pidf_tuple));
182
183 pj_strdup(pool,
184 &pres_status->info[pres_status->info_cnt].contact,
185 pjpidf_tuple_get_contact(pidf_tuple));
186
187 pidf_status = pjpidf_tuple_get_status(pidf_tuple);
188 if (pidf_status) {
189 pres_status->info[pres_status->info_cnt].basic_open =
190 pjpidf_status_is_basic_open(pidf_status);
191 } else {
192 pres_status->info[pres_status->info_cnt].basic_open = PJ_FALSE;
193 }
194
195 pidf_tuple = pjpidf_pres_get_next_tuple( pidf, pidf_tuple );
196 pres_status->info_cnt++;
197 }
198
199 return PJ_SUCCESS;
200}
201
202
203/*
204 * This is a utility function to parse X-PIDF body into PJSIP presence status.
205 */
206PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf(pjsip_rx_data *rdata,
207 pj_pool_t *pool,
208 pjsip_pres_status *pres_status)
209{
210 pjxpidf_pres *xpidf;
211
212 xpidf = pjxpidf_parse(rdata->tp_info.pool,
Benny Prijono9d4469d2007-05-02 05:14:29 +0000213 (char*)rdata->msg_info.msg->body->data,
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000214 rdata->msg_info.msg->body->len);
215 if (xpidf == NULL)
216 return PJSIP_SIMPLE_EBADXPIDF;
217
218 pres_status->info_cnt = 1;
219
220 pj_strdup(pool,
221 &pres_status->info[0].contact,
222 pjxpidf_get_uri(xpidf));
223 pres_status->info[0].basic_open = pjxpidf_get_status(xpidf);
224 pres_status->info[0].id.slen = 0;
225
226 return PJ_SUCCESS;
227}
228
229