~ubuntu-branches/ubuntu/raring/sflphone/raring

« back to all changes in this revision

Viewing changes to sflphone-common/libs/pjproject/pjsip/src/pjsip-simple/presence_body.c

  • Committer: Package Import Robot
  • Author(s): Francois Marier
  • Date: 2011-11-25 13:24:12 UTC
  • mfrom: (4.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20111125132412-dc4qvhyosk74cd42
Tags: 1.0.1-4
Don't assume that arch:all packages will get built (closes: #649726)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: presence_body.c 3045 2010-01-05 15:23:43Z bennylp $ */
2
 
/* 
3
 
 * Copyright (C) 2008-2009 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
 
 *  Additional permission under GNU GPL version 3 section 7:
21
 
 *
22
 
 *  If you modify this program, or any covered work, by linking or
23
 
 *  combining it with the OpenSSL project's OpenSSL library (or a
24
 
 *  modified version of that library), containing parts covered by the
25
 
 *  terms of the OpenSSL or SSLeay licenses, Teluu Inc. (http://www.teluu.com)
26
 
 *  grants you additional permission to convey the resulting work.
27
 
 *  Corresponding Source for a non-source form of such a combination
28
 
 *  shall include the source code for the parts of OpenSSL used as well
29
 
 *  as that of the covered work.
30
 
 */
31
 
#include <pjsip-simple/presence.h>
32
 
#include <pjsip-simple/errno.h>
33
 
#include <pjsip/sip_msg.h>
34
 
#include <pjsip/sip_transport.h>
35
 
#include <pj/guid.h>
36
 
#include <pj/log.h>
37
 
#include <pj/os.h>
38
 
#include <pj/pool.h>
39
 
#include <pj/string.h>
40
 
 
41
 
 
42
 
#define THIS_FILE   "presence_body.c"
43
 
 
44
 
 
45
 
static const pj_str_t STR_APPLICATION = { "application", 11 };
46
 
static const pj_str_t STR_PIDF_XML =    { "pidf+xml", 8 };
47
 
static const pj_str_t STR_XPIDF_XML =   { "xpidf+xml", 9 };
48
 
 
49
 
 
50
 
 
51
 
 
52
 
/*
53
 
 * Function to print XML message body.
54
 
 */
55
 
static int pres_print_body(struct pjsip_msg_body *msg_body, 
56
 
                           char *buf, pj_size_t size)
57
 
{
58
 
    return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size, 
59
 
                        PJ_TRUE);
60
 
}
61
 
 
62
 
 
63
 
/*
64
 
 * Function to clone XML document.
65
 
 */
66
 
static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
67
 
{
68
 
    PJ_UNUSED_ARG(len);
69
 
    return pj_xml_clone( pool, (const pj_xml_node*) data);
70
 
}
71
 
 
72
 
 
73
 
/*
74
 
 * This is a utility function to create PIDF message body from PJSIP
75
 
 * presence status (pjsip_pres_status).
76
 
 */
77
 
PJ_DEF(pj_status_t) pjsip_pres_create_pidf( pj_pool_t *pool,
78
 
                                            const pjsip_pres_status *status,
79
 
                                            const pj_str_t *entity,
80
 
                                            pjsip_msg_body **p_body )
81
 
{
82
 
    pjpidf_pres *pidf;
83
 
    pjsip_msg_body *body;
84
 
    unsigned i;
85
 
 
86
 
    /* Create <presence>. */
87
 
    pidf = pjpidf_create(pool, entity);
88
 
 
89
 
    /* Create <tuple> */
90
 
    for (i=0; i<status->info_cnt; ++i) {
91
 
 
92
 
        pjpidf_tuple *pidf_tuple;
93
 
        pjpidf_status *pidf_status;
94
 
        pj_str_t id;
95
 
 
96
 
        /* Add tuple id. */
97
 
        if (status->info[i].id.slen == 0) {
98
 
            /* xs:ID must start with letter */
99
 
            //pj_create_unique_string(pool, &id);
100
 
            id.ptr = (char*)pj_pool_alloc(pool, PJ_GUID_STRING_LENGTH+2);
101
 
            id.ptr += 2;
102
 
            pj_generate_unique_string(&id);
103
 
            id.ptr -= 2;
104
 
            id.ptr[0] = 'p';
105
 
            id.ptr[1] = 'j';
106
 
            id.slen += 2;
107
 
        } else {
108
 
            id = status->info[i].id;
109
 
        }
110
 
 
111
 
        pidf_tuple = pjpidf_pres_add_tuple(pool, pidf, &id);
112
 
 
113
 
        /* Set <contact> */
114
 
        if (status->info[i].contact.slen)
115
 
            pjpidf_tuple_set_contact(pool, pidf_tuple, 
116
 
                                     &status->info[i].contact);
117
 
 
118
 
 
119
 
        /* Set basic status */
120
 
        pidf_status = pjpidf_tuple_get_status(pidf_tuple);
121
 
        pjpidf_status_set_basic_open(pidf_status, 
122
 
                                     status->info[i].basic_open);
123
 
 
124
 
        /* Add <timestamp> if configured */
125
 
#if defined(PJSIP_PRES_PIDF_ADD_TIMESTAMP) && PJSIP_PRES_PIDF_ADD_TIMESTAMP
126
 
        if (PJSIP_PRES_PIDF_ADD_TIMESTAMP) {
127
 
          char buf[50];
128
 
          int tslen = 0;
129
 
          pj_time_val tv;
130
 
          pj_parsed_time pt;
131
 
 
132
 
          pj_gettimeofday(&tv);
133
 
          /* TODO: convert time to GMT! (unsupported by pjlib) */
134
 
          pj_time_decode( &tv, &pt);
135
 
 
136
 
          tslen = pj_ansi_snprintf(buf, sizeof(buf),
137
 
                                   "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
138
 
                                   pt.year, pt.mon+1, pt.day, 
139
 
                                   pt.hour, pt.min, pt.sec, pt.msec);
140
 
          if (tslen > 0 && tslen < sizeof(buf)) {
141
 
              pj_str_t time = pj_str(buf);
142
 
              pjpidf_tuple_set_timestamp(pool, pidf_tuple, &time);
143
 
          }
144
 
        }
145
 
#endif
146
 
    }
147
 
 
148
 
    /* Create <person> (RPID) */
149
 
    if (status->info_cnt) {
150
 
        pjrpid_add_element(pidf, pool, 0, &status->info[0].rpid);
151
 
    }
152
 
 
153
 
    body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
154
 
    body->data = pidf;
155
 
    body->content_type.type = STR_APPLICATION;
156
 
    body->content_type.subtype = STR_PIDF_XML;
157
 
    body->print_body = &pres_print_body;
158
 
    body->clone_data = &xml_clone_data;
159
 
 
160
 
    *p_body = body;
161
 
 
162
 
    return PJ_SUCCESS;    
163
 
}
164
 
 
165
 
 
166
 
/*
167
 
 * This is a utility function to create X-PIDF message body from PJSIP
168
 
 * presence status (pjsip_pres_status).
169
 
 */
170
 
PJ_DEF(pj_status_t) pjsip_pres_create_xpidf( pj_pool_t *pool,
171
 
                                             const pjsip_pres_status *status,
172
 
                                             const pj_str_t *entity,
173
 
                                             pjsip_msg_body **p_body )
174
 
{
175
 
    /* Note: PJSIP implementation of XPIDF is not complete!
176
 
     */
177
 
    pjxpidf_pres *xpidf;
178
 
    pjsip_msg_body *body;
179
 
 
180
 
    PJ_LOG(4,(THIS_FILE, "Warning: XPIDF format is not fully supported "
181
 
                         "by PJSIP"));
182
 
 
183
 
    /* Create XPIDF document. */
184
 
    xpidf = pjxpidf_create(pool, entity);
185
 
 
186
 
    /* Set basic status. */
187
 
    if (status->info_cnt > 0)
188
 
        pjxpidf_set_status( xpidf, status->info[0].basic_open);
189
 
    else
190
 
        pjxpidf_set_status( xpidf, PJ_FALSE);
191
 
 
192
 
    body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
193
 
    body->data = xpidf;
194
 
    body->content_type.type = STR_APPLICATION;
195
 
    body->content_type.subtype = STR_XPIDF_XML;
196
 
    body->print_body = &pres_print_body;
197
 
    body->clone_data = &xml_clone_data;
198
 
 
199
 
    *p_body = body;
200
 
 
201
 
    return PJ_SUCCESS;
202
 
}
203
 
 
204
 
 
205
 
 
206
 
/*
207
 
 * This is a utility function to parse PIDF body into PJSIP presence status.
208
 
 */
209
 
PJ_DEF(pj_status_t) pjsip_pres_parse_pidf( pjsip_rx_data *rdata,
210
 
                                           pj_pool_t *pool,
211
 
                                           pjsip_pres_status *pres_status)
212
 
{
213
 
    pjpidf_pres *pidf;
214
 
    pjpidf_tuple *pidf_tuple;
215
 
 
216
 
    pidf = pjpidf_parse(rdata->tp_info.pool, 
217
 
                        (char*)rdata->msg_info.msg->body->data,
218
 
                        rdata->msg_info.msg->body->len);
219
 
    if (pidf == NULL)
220
 
        return PJSIP_SIMPLE_EBADPIDF;
221
 
 
222
 
    pres_status->info_cnt = 0;
223
 
 
224
 
    pidf_tuple = pjpidf_pres_get_first_tuple(pidf);
225
 
    while (pidf_tuple && pres_status->info_cnt < PJSIP_PRES_STATUS_MAX_INFO) {
226
 
        pjpidf_status *pidf_status;
227
 
 
228
 
        pres_status->info[pres_status->info_cnt].tuple_node = 
229
 
            pj_xml_clone(pool, pidf_tuple);
230
 
 
231
 
        pj_strdup(pool, 
232
 
                  &pres_status->info[pres_status->info_cnt].id,
233
 
                  pjpidf_tuple_get_id(pidf_tuple));
234
 
 
235
 
        pj_strdup(pool, 
236
 
                  &pres_status->info[pres_status->info_cnt].contact,
237
 
                  pjpidf_tuple_get_contact(pidf_tuple));
238
 
 
239
 
        pidf_status = pjpidf_tuple_get_status(pidf_tuple);
240
 
        if (pidf_status) {
241
 
            pres_status->info[pres_status->info_cnt].basic_open = 
242
 
                pjpidf_status_is_basic_open(pidf_status);
243
 
        } else {
244
 
            pres_status->info[pres_status->info_cnt].basic_open = PJ_FALSE;
245
 
        }
246
 
 
247
 
        pidf_tuple = pjpidf_pres_get_next_tuple( pidf, pidf_tuple );
248
 
        pres_status->info_cnt++;
249
 
    }
250
 
 
251
 
    /* Parse <person> (RPID) */
252
 
    pjrpid_get_element(pidf, pool, &pres_status->info[0].rpid);
253
 
 
254
 
    return PJ_SUCCESS;
255
 
}
256
 
 
257
 
 
258
 
/*
259
 
 * This is a utility function to parse X-PIDF body into PJSIP presence status.
260
 
 */
261
 
PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf(pjsip_rx_data *rdata,
262
 
                                           pj_pool_t *pool,
263
 
                                           pjsip_pres_status *pres_status)
264
 
{
265
 
    pjxpidf_pres *xpidf;
266
 
 
267
 
    xpidf = pjxpidf_parse(rdata->tp_info.pool, 
268
 
                          (char*)rdata->msg_info.msg->body->data,
269
 
                          rdata->msg_info.msg->body->len);
270
 
    if (xpidf == NULL)
271
 
        return PJSIP_SIMPLE_EBADXPIDF;
272
 
 
273
 
    pres_status->info_cnt = 1;
274
 
    
275
 
    pj_strdup(pool,
276
 
              &pres_status->info[0].contact,
277
 
              pjxpidf_get_uri(xpidf));
278
 
    pres_status->info[0].basic_open = pjxpidf_get_status(xpidf);
279
 
    pres_status->info[0].id.slen = 0;
280
 
    pres_status->info[0].tuple_node = NULL;
281
 
 
282
 
    return PJ_SUCCESS;
283
 
}
284
 
 
285