1
/* $Id: xpidf.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/xpidf.h>
21
#include <pj/assert.h>
24
#include <pj/string.h>
26
static pj_str_t STR_PRESENCE = { "presence", 8 };
27
static pj_str_t STR_STATUS = { "status", 6 };
28
static pj_str_t STR_OPEN = { "open", 4 };
29
static pj_str_t STR_CLOSED = { "closed", 6 };
30
static pj_str_t STR_URI = { "uri", 3 };
31
static pj_str_t STR_ATOM = { "atom", 4 };
32
static pj_str_t STR_ATOMID = { "atomid", 6 };
33
static pj_str_t STR_ID = { "id", 2 };
34
static pj_str_t STR_ADDRESS = { "address", 7 };
35
static pj_str_t STR_SUBSCRIBE_PARAM = { ";method=SUBSCRIBE", 17 };
36
static pj_str_t STR_PRESENTITY = { "presentity", 10 };
37
static pj_str_t STR_EMPTY_STRING = { NULL, 0 };
39
static pj_xml_node* xml_create_node(pj_pool_t *pool,
40
pj_str_t *name, const pj_str_t *value)
44
node = PJ_POOL_ALLOC_T(pool, pj_xml_node);
45
pj_list_init(&node->attr_head);
46
pj_list_init(&node->node_head);
48
if (value) pj_strdup(pool, &node->content, value);
49
else node->content.ptr=NULL, node->content.slen=0;
54
static pj_xml_attr* xml_create_attr(pj_pool_t *pool, pj_str_t *name,
55
const pj_str_t *value)
57
pj_xml_attr *attr = PJ_POOL_ALLOC_T(pool, pj_xml_attr);
59
pj_strdup(pool, &attr->value, value);
64
PJ_DEF(pjxpidf_pres*) pjxpidf_create(pj_pool_t *pool, const pj_str_t *uri_cstr)
67
pj_xml_node *presentity;
76
pres = xml_create_node(pool, &STR_PRESENCE, NULL);
79
presentity = xml_create_node(pool, &STR_PRESENTITY, NULL);
80
pj_xml_add_node(pres, presentity);
83
uri.ptr = (char*) pj_pool_alloc(pool, uri_cstr->slen +
84
STR_SUBSCRIBE_PARAM.slen);
85
pj_strcpy( &uri, uri_cstr);
86
pj_strcat( &uri, &STR_SUBSCRIBE_PARAM);
87
attr = xml_create_attr(pool, &STR_URI, &uri);
88
pj_xml_add_attr(presentity, attr);
91
atom = xml_create_node(pool, &STR_ATOM, NULL);
92
pj_xml_add_node(pres, atom);
95
pj_create_unique_string(pool, &tmp);
96
attr = xml_create_attr(pool, &STR_ATOMID, &tmp);
97
pj_xml_add_attr(atom, attr);
100
addr = xml_create_node(pool, &STR_ADDRESS, NULL);
101
pj_xml_add_node(atom, addr);
104
attr = xml_create_attr(pool, &STR_URI, uri_cstr);
105
pj_xml_add_attr(addr, attr);
108
status = xml_create_node(pool, &STR_STATUS, NULL);
109
pj_xml_add_node(addr, status);
112
attr = xml_create_attr(pool, &STR_STATUS, &STR_OPEN);
113
pj_xml_add_attr(status, attr);
120
PJ_DEF(pjxpidf_pres*) pjxpidf_parse(pj_pool_t *pool, char *text, pj_size_t len)
125
pres = pj_xml_parse(pool, text, len);
129
/* Validate <presence> */
130
if (pj_stricmp(&pres->name, &STR_PRESENCE) != 0)
133
/* Validate <presentity> */
134
node = pj_xml_find_node(pres, &STR_PRESENTITY);
137
if (pj_xml_find_attr(node, &STR_URI, NULL) == NULL)
140
/* Validate <atom> */
141
node = pj_xml_find_node(pres, &STR_ATOM);
144
if (pj_xml_find_attr(node, &STR_ATOMID, NULL) == NULL &&
145
pj_xml_find_attr(node, &STR_ID, NULL) == NULL)
151
node = pj_xml_find_node(node, &STR_ADDRESS);
154
if (pj_xml_find_attr(node, &STR_URI, NULL) == NULL)
159
node = pj_xml_find_node(node, &STR_STATUS);
162
if (pj_xml_find_attr(node, &STR_STATUS, NULL) == NULL)
169
PJ_DEF(int) pjxpidf_print( pjxpidf_pres *pres, char *text, pj_size_t len)
171
return pj_xml_print(pres, text, len, PJ_TRUE);
175
PJ_DEF(pj_str_t*) pjxpidf_get_uri(pjxpidf_pres *pres)
177
pj_xml_node *presentity;
180
presentity = pj_xml_find_node(pres, &STR_PRESENTITY);
182
return &STR_EMPTY_STRING;
184
attr = pj_xml_find_attr(presentity, &STR_URI, NULL);
186
return &STR_EMPTY_STRING;
192
PJ_DEF(pj_status_t) pjxpidf_set_uri(pj_pool_t *pool, pjxpidf_pres *pres,
195
pj_xml_node *presentity;
201
presentity = pj_xml_find_node(pres, &STR_PRESENTITY);
206
atom = pj_xml_find_node(pres, &STR_ATOM);
211
addr = pj_xml_find_node(atom, &STR_ADDRESS);
217
/* Set uri in presentity */
218
attr = pj_xml_find_attr(presentity, &STR_URI, NULL);
223
pj_strdup(pool, &dup_uri, uri);
224
attr->value = dup_uri;
226
/* Set uri in address. */
227
attr = pj_xml_find_attr(addr, &STR_URI, NULL);
232
attr->value = dup_uri;
238
PJ_DEF(pj_bool_t) pjxpidf_get_status(pjxpidf_pres *pres)
245
atom = pj_xml_find_node(pres, &STR_ATOM);
250
addr = pj_xml_find_node(atom, &STR_ADDRESS);
255
status = pj_xml_find_node(addr, &STR_STATUS);
260
attr = pj_xml_find_attr(status, &STR_STATUS, NULL);
266
return pj_stricmp(&attr->value, &STR_OPEN)==0 ? PJ_TRUE : PJ_FALSE;
270
PJ_DEF(pj_status_t) pjxpidf_set_status(pjxpidf_pres *pres, pj_bool_t online_status)
277
atom = pj_xml_find_node(pres, &STR_ATOM);
282
addr = pj_xml_find_node(atom, &STR_ADDRESS);
287
status = pj_xml_find_node(addr, &STR_STATUS);
292
attr = pj_xml_find_attr(status, &STR_STATUS, NULL);
298
attr->value = ( online_status ? STR_OPEN : STR_CLOSED );