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