~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject/pjsip/src/pjsip-simple/xpidf.c

  • Committer: Jackson Doak
  • Date: 2013-07-10 21:04:46 UTC
  • mfrom: (20.1.3 sid)
  • Revision ID: noskcaj@ubuntu.com-20130710210446-y8f587vza807icr9
Properly merged from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: xpidf.c 3553 2011-05-05 06:14:19Z nanang $ */
2
 
/* 
3
 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
 
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5
 
 *
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/xpidf.h>
21
 
#include <pj/assert.h>
22
 
#include <pj/guid.h>
23
 
#include <pj/pool.h>
24
 
#include <pj/string.h>
25
 
 
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 };
38
 
 
39
 
static pj_xml_node* xml_create_node(pj_pool_t *pool, 
40
 
                                    pj_str_t *name, const pj_str_t *value)
41
 
{
42
 
    pj_xml_node *node;
43
 
 
44
 
    node = PJ_POOL_ALLOC_T(pool, pj_xml_node);
45
 
    pj_list_init(&node->attr_head);
46
 
    pj_list_init(&node->node_head);
47
 
    node->name = *name;
48
 
    if (value) pj_strdup(pool, &node->content, value);
49
 
    else node->content.ptr=NULL, node->content.slen=0;
50
 
 
51
 
    return node;
52
 
}
53
 
 
54
 
static pj_xml_attr* xml_create_attr(pj_pool_t *pool, pj_str_t *name,
55
 
                                    const pj_str_t *value)
56
 
{
57
 
    pj_xml_attr *attr = PJ_POOL_ALLOC_T(pool, pj_xml_attr);
58
 
    attr->name = *name;
59
 
    pj_strdup(pool, &attr->value, value);
60
 
    return attr;
61
 
}
62
 
 
63
 
 
64
 
PJ_DEF(pjxpidf_pres*) pjxpidf_create(pj_pool_t *pool, const pj_str_t *uri_cstr)
65
 
{
66
 
    pjxpidf_pres *pres;
67
 
    pj_xml_node *presentity;
68
 
    pj_xml_node *atom;
69
 
    pj_xml_node *addr;
70
 
    pj_xml_node *status;
71
 
    pj_xml_attr *attr;
72
 
    pj_str_t uri;
73
 
    pj_str_t tmp;
74
 
 
75
 
    /* <presence> */
76
 
    pres = xml_create_node(pool, &STR_PRESENCE, NULL);
77
 
 
78
 
    /* <presentity> */
79
 
    presentity = xml_create_node(pool, &STR_PRESENTITY, NULL);
80
 
    pj_xml_add_node(pres, presentity);
81
 
 
82
 
    /* uri attribute */
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);
89
 
 
90
 
    /* <atom> */
91
 
    atom = xml_create_node(pool, &STR_ATOM, NULL);
92
 
    pj_xml_add_node(pres, atom);
93
 
 
94
 
    /* atom id */
95
 
    pj_create_unique_string(pool, &tmp);
96
 
    attr = xml_create_attr(pool, &STR_ATOMID, &tmp);
97
 
    pj_xml_add_attr(atom, attr);
98
 
 
99
 
    /* address */
100
 
    addr = xml_create_node(pool, &STR_ADDRESS, NULL);
101
 
    pj_xml_add_node(atom, addr);
102
 
 
103
 
    /* address'es uri */
104
 
    attr = xml_create_attr(pool, &STR_URI, uri_cstr);
105
 
    pj_xml_add_attr(addr, attr);
106
 
 
107
 
    /* status */
108
 
    status = xml_create_node(pool, &STR_STATUS, NULL);
109
 
    pj_xml_add_node(addr, status);
110
 
 
111
 
    /* status attr */
112
 
    attr = xml_create_attr(pool, &STR_STATUS, &STR_OPEN);
113
 
    pj_xml_add_attr(status, attr);
114
 
 
115
 
    return pres;
116
 
}   
117
 
 
118
 
 
119
 
 
120
 
PJ_DEF(pjxpidf_pres*) pjxpidf_parse(pj_pool_t *pool, char *text, pj_size_t len)
121
 
{
122
 
    pjxpidf_pres *pres;
123
 
    pj_xml_node *node;
124
 
 
125
 
    pres = pj_xml_parse(pool, text, len);
126
 
    if (!pres)
127
 
        return NULL;
128
 
 
129
 
    /* Validate <presence> */
130
 
    if (pj_stricmp(&pres->name, &STR_PRESENCE) != 0)
131
 
        return NULL;
132
 
 
133
 
    /* Validate <presentity> */
134
 
    node = pj_xml_find_node(pres, &STR_PRESENTITY);
135
 
    if (node == NULL)
136
 
        return NULL;
137
 
    if (pj_xml_find_attr(node, &STR_URI, NULL) == NULL)
138
 
        return NULL;
139
 
 
140
 
    /* Validate <atom> */
141
 
    node = pj_xml_find_node(pres, &STR_ATOM);
142
 
    if (node == NULL)
143
 
        return NULL;
144
 
    if (pj_xml_find_attr(node, &STR_ATOMID, NULL) == NULL && 
145
 
        pj_xml_find_attr(node, &STR_ID, NULL) == NULL)
146
 
    {
147
 
        return NULL;
148
 
    }
149
 
 
150
 
    /* Address */
151
 
    node = pj_xml_find_node(node, &STR_ADDRESS);
152
 
    if (node == NULL)
153
 
        return NULL;
154
 
    if (pj_xml_find_attr(node, &STR_URI, NULL) == NULL)
155
 
        return NULL;
156
 
 
157
 
 
158
 
    /* Status */
159
 
    node = pj_xml_find_node(node, &STR_STATUS);
160
 
    if (node == NULL)
161
 
        return NULL;
162
 
    if (pj_xml_find_attr(node, &STR_STATUS, NULL) == NULL)
163
 
        return NULL;
164
 
 
165
 
    return pres;
166
 
}
167
 
 
168
 
 
169
 
PJ_DEF(int) pjxpidf_print( pjxpidf_pres *pres, char *text, pj_size_t len)
170
 
{
171
 
    return pj_xml_print(pres, text, len, PJ_TRUE);
172
 
}
173
 
 
174
 
 
175
 
PJ_DEF(pj_str_t*) pjxpidf_get_uri(pjxpidf_pres *pres)
176
 
{
177
 
    pj_xml_node *presentity;
178
 
    pj_xml_attr *attr;
179
 
 
180
 
    presentity = pj_xml_find_node(pres, &STR_PRESENTITY);
181
 
    if (!presentity)
182
 
        return &STR_EMPTY_STRING;
183
 
 
184
 
    attr = pj_xml_find_attr(presentity, &STR_URI, NULL);
185
 
    if (!attr)
186
 
        return &STR_EMPTY_STRING;
187
 
 
188
 
    return &attr->value;
189
 
}
190
 
 
191
 
 
192
 
PJ_DEF(pj_status_t) pjxpidf_set_uri(pj_pool_t *pool, pjxpidf_pres *pres, 
193
 
                                    const pj_str_t *uri)
194
 
{
195
 
    pj_xml_node *presentity;
196
 
    pj_xml_node *atom;
197
 
    pj_xml_node *addr;
198
 
    pj_xml_attr *attr;
199
 
    pj_str_t dup_uri;
200
 
 
201
 
    presentity = pj_xml_find_node(pres, &STR_PRESENTITY);
202
 
    if (!presentity) {
203
 
        pj_assert(0);
204
 
        return -1;
205
 
    }
206
 
    atom = pj_xml_find_node(pres, &STR_ATOM);
207
 
    if (!atom) {
208
 
        pj_assert(0);
209
 
        return -1;
210
 
    }
211
 
    addr = pj_xml_find_node(atom, &STR_ADDRESS);
212
 
    if (!addr) {
213
 
        pj_assert(0);
214
 
        return -1;
215
 
    }
216
 
 
217
 
    /* Set uri in presentity */
218
 
    attr = pj_xml_find_attr(presentity, &STR_URI, NULL);
219
 
    if (!attr) {
220
 
        pj_assert(0);
221
 
        return -1;
222
 
    }
223
 
    pj_strdup(pool, &dup_uri, uri);
224
 
    attr->value = dup_uri;
225
 
 
226
 
    /* Set uri in address. */
227
 
    attr = pj_xml_find_attr(addr, &STR_URI, NULL);
228
 
    if (!attr) {
229
 
        pj_assert(0);
230
 
        return -1;
231
 
    }
232
 
    attr->value = dup_uri;
233
 
 
234
 
    return 0;
235
 
}
236
 
 
237
 
 
238
 
PJ_DEF(pj_bool_t) pjxpidf_get_status(pjxpidf_pres *pres)
239
 
{
240
 
    pj_xml_node *atom;
241
 
    pj_xml_node *addr;
242
 
    pj_xml_node *status;
243
 
    pj_xml_attr *attr;
244
 
 
245
 
    atom = pj_xml_find_node(pres, &STR_ATOM);
246
 
    if (!atom) {
247
 
        pj_assert(0);
248
 
        return PJ_FALSE;
249
 
    }
250
 
    addr = pj_xml_find_node(atom, &STR_ADDRESS);
251
 
    if (!addr) {
252
 
        pj_assert(0);
253
 
        return PJ_FALSE;
254
 
    }
255
 
    status = pj_xml_find_node(addr, &STR_STATUS);
256
 
    if (!status) {
257
 
        pj_assert(0);
258
 
        return PJ_FALSE;
259
 
    }
260
 
    attr = pj_xml_find_attr(status, &STR_STATUS, NULL);
261
 
    if (!attr) {
262
 
        pj_assert(0);
263
 
        return PJ_FALSE;
264
 
    }
265
 
 
266
 
    return pj_stricmp(&attr->value, &STR_OPEN)==0 ? PJ_TRUE : PJ_FALSE;
267
 
}
268
 
 
269
 
 
270
 
PJ_DEF(pj_status_t) pjxpidf_set_status(pjxpidf_pres *pres, pj_bool_t online_status)
271
 
{
272
 
    pj_xml_node *atom;
273
 
    pj_xml_node *addr;
274
 
    pj_xml_node *status;
275
 
    pj_xml_attr *attr;
276
 
 
277
 
    atom = pj_xml_find_node(pres, &STR_ATOM);
278
 
    if (!atom) {
279
 
        pj_assert(0);
280
 
        return -1;
281
 
    }
282
 
    addr = pj_xml_find_node(atom, &STR_ADDRESS);
283
 
    if (!addr) {
284
 
        pj_assert(0);
285
 
        return -1;
286
 
    }
287
 
    status = pj_xml_find_node(addr, &STR_STATUS);
288
 
    if (!status) {
289
 
        pj_assert(0);
290
 
        return -1;
291
 
    }
292
 
    attr = pj_xml_find_attr(status, &STR_STATUS, NULL);
293
 
    if (!attr) {
294
 
        pj_assert(0);
295
 
        return -1;
296
 
    }
297
 
 
298
 
    attr->value = ( online_status ? STR_OPEN : STR_CLOSED );
299
 
    return 0;
300
 
}
301