~ubuntu-branches/ubuntu/trusty/sflphone/trusty

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.0.1/pjsip/src/pjsua-lib/pjsua_pres.c

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (4.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20140128182336-jrsv0k9u6cawc068
Tags: 1.3.0-1
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: pjsua_pres.c 4186 2012-06-29 01:37:50Z ming $ */
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 <pjsua-lib/pjsua.h>
21
 
#include <pjsua-lib/pjsua_internal.h>
22
 
 
23
 
 
24
 
#define THIS_FILE   "pjsua_pres.c"
25
 
 
26
 
 
27
 
static void subscribe_buddy_presence(pjsua_buddy_id buddy_id);
28
 
static void unsubscribe_buddy_presence(pjsua_buddy_id buddy_id);
29
 
 
30
 
 
31
 
/*
32
 
 * Find buddy.
33
 
 */
34
 
static pjsua_buddy_id find_buddy(const pjsip_uri *uri)
35
 
{
36
 
    const pjsip_sip_uri *sip_uri;
37
 
    unsigned i;
38
 
 
39
 
    uri = (const pjsip_uri*) pjsip_uri_get_uri((pjsip_uri*)uri);
40
 
 
41
 
    if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
42
 
        return PJSUA_INVALID_ID;
43
 
 
44
 
    sip_uri = (const pjsip_sip_uri*) uri;
45
 
 
46
 
    for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
47
 
        const pjsua_buddy *b = &pjsua_var.buddy[i];
48
 
 
49
 
        if (!pjsua_buddy_is_valid(i))
50
 
            continue;
51
 
 
52
 
        if (pj_stricmp(&sip_uri->user, &b->name)==0 &&
53
 
            pj_stricmp(&sip_uri->host, &b->host)==0 &&
54
 
            (sip_uri->port==(int)b->port || (sip_uri->port==0 && b->port==5060)))
55
 
        {
56
 
            /* Match */
57
 
            return i;
58
 
        }
59
 
    }
60
 
 
61
 
    return PJSUA_INVALID_ID;
62
 
}
63
 
 
64
 
#define LOCK_DIALOG     1
65
 
#define LOCK_PJSUA      2
66
 
#define LOCK_ALL        (LOCK_DIALOG | LOCK_PJSUA)
67
 
 
68
 
/* Buddy lock object */
69
 
struct buddy_lock
70
 
{
71
 
    pjsua_buddy     *buddy;
72
 
    pjsip_dialog    *dlg;
73
 
    pj_uint8_t       flag;
74
 
};
75
 
 
76
 
/* Acquire lock to the specified buddy_id */
77
 
pj_status_t lock_buddy(const char *title,
78
 
                       pjsua_buddy_id buddy_id,
79
 
                       struct buddy_lock *lck,
80
 
                       unsigned _unused_)
81
 
{
82
 
    enum { MAX_RETRY=50 };
83
 
    pj_bool_t has_pjsua_lock = PJ_FALSE;
84
 
    unsigned retry;
85
 
 
86
 
    PJ_UNUSED_ARG(_unused_);
87
 
 
88
 
    pj_bzero(lck, sizeof(*lck));
89
 
 
90
 
    for (retry=0; retry<MAX_RETRY; ++retry) {
91
 
 
92
 
        if (PJSUA_TRY_LOCK() != PJ_SUCCESS) {
93
 
            pj_thread_sleep(retry/10);
94
 
            continue;
95
 
        }
96
 
 
97
 
        has_pjsua_lock = PJ_TRUE;
98
 
        lck->flag = LOCK_PJSUA;
99
 
        lck->buddy = &pjsua_var.buddy[buddy_id];
100
 
 
101
 
        if (lck->buddy->dlg == NULL)
102
 
            return PJ_SUCCESS;
103
 
 
104
 
        if (pjsip_dlg_try_inc_lock(lck->buddy->dlg) != PJ_SUCCESS) {
105
 
            lck->flag = 0;
106
 
            lck->buddy = NULL;
107
 
            has_pjsua_lock = PJ_FALSE;
108
 
            PJSUA_UNLOCK();
109
 
            pj_thread_sleep(retry/10);
110
 
            continue;
111
 
        }
112
 
 
113
 
        lck->dlg = lck->buddy->dlg;
114
 
        lck->flag = LOCK_DIALOG;
115
 
        PJSUA_UNLOCK();
116
 
 
117
 
        break;
118
 
    }
119
 
 
120
 
    if (lck->flag == 0) {
121
 
        if (has_pjsua_lock == PJ_FALSE)
122
 
            PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire PJSUA mutex "
123
 
                                 "(possibly system has deadlocked) in %s",
124
 
                                 title));
125
 
        else
126
 
            PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire dialog mutex "
127
 
                                 "(possibly system has deadlocked) in %s",
128
 
                                 title));
129
 
        return PJ_ETIMEDOUT;
130
 
    }
131
 
 
132
 
    return PJ_SUCCESS;
133
 
}
134
 
 
135
 
/* Release buddy lock */
136
 
static void unlock_buddy(struct buddy_lock *lck)
137
 
{
138
 
    if (lck->flag & LOCK_DIALOG)
139
 
        pjsip_dlg_dec_lock(lck->dlg);
140
 
 
141
 
    if (lck->flag & LOCK_PJSUA)
142
 
        PJSUA_UNLOCK();
143
 
}
144
 
 
145
 
 
146
 
/*
147
 
 * Get total number of buddies.
148
 
 */
149
 
PJ_DEF(unsigned) pjsua_get_buddy_count(void)
150
 
{
151
 
    return pjsua_var.buddy_cnt;
152
 
}
153
 
 
154
 
 
155
 
/*
156
 
 * Find buddy.
157
 
 */
158
 
PJ_DEF(pjsua_buddy_id) pjsua_buddy_find(const pj_str_t *uri_str)
159
 
{
160
 
    pj_str_t input;
161
 
    pj_pool_t *pool;
162
 
    pjsip_uri *uri;
163
 
    pjsua_buddy_id buddy_id;
164
 
 
165
 
    pool = pjsua_pool_create("buddyfind", 512, 512);
166
 
    pj_strdup_with_null(pool, &input, uri_str);
167
 
 
168
 
    uri = pjsip_parse_uri(pool, input.ptr, input.slen, 0);
169
 
    if (!uri)
170
 
        buddy_id = PJSUA_INVALID_ID;
171
 
    else {
172
 
        PJSUA_LOCK();
173
 
        buddy_id = find_buddy(uri);
174
 
        PJSUA_UNLOCK();
175
 
    }
176
 
 
177
 
    pj_pool_release(pool);
178
 
 
179
 
    return buddy_id;
180
 
}
181
 
 
182
 
 
183
 
/*
184
 
 * Check if buddy ID is valid.
185
 
 */
186
 
PJ_DEF(pj_bool_t) pjsua_buddy_is_valid(pjsua_buddy_id buddy_id)
187
 
{
188
 
    return buddy_id>=0 && buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy) &&
189
 
           pjsua_var.buddy[buddy_id].uri.slen != 0;
190
 
}
191
 
 
192
 
 
193
 
/*
194
 
 * Enum buddy IDs.
195
 
 */
196
 
PJ_DEF(pj_status_t) pjsua_enum_buddies( pjsua_buddy_id ids[],
197
 
                                        unsigned *count)
198
 
{
199
 
    unsigned i, c;
200
 
 
201
 
    PJ_ASSERT_RETURN(ids && count, PJ_EINVAL);
202
 
 
203
 
    PJSUA_LOCK();
204
 
 
205
 
    for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
206
 
        if (!pjsua_var.buddy[i].uri.slen)
207
 
            continue;
208
 
        ids[c] = i;
209
 
        ++c;
210
 
    }
211
 
 
212
 
    *count = c;
213
 
 
214
 
    PJSUA_UNLOCK();
215
 
 
216
 
    return PJ_SUCCESS;
217
 
}
218
 
 
219
 
 
220
 
/*
221
 
 * Get detailed buddy info.
222
 
 */
223
 
PJ_DEF(pj_status_t) pjsua_buddy_get_info( pjsua_buddy_id buddy_id,
224
 
                                          pjsua_buddy_info *info)
225
 
{
226
 
    unsigned total=0;
227
 
    struct buddy_lock lck;
228
 
    pjsua_buddy *buddy;
229
 
    pj_status_t status;
230
 
 
231
 
    PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id),  PJ_EINVAL);
232
 
 
233
 
    pj_bzero(info, sizeof(pjsua_buddy_info));
234
 
 
235
 
    status = lock_buddy("pjsua_buddy_get_info()", buddy_id, &lck, 0);
236
 
    if (status != PJ_SUCCESS)
237
 
        return status;
238
 
 
239
 
    buddy = lck.buddy;
240
 
    info->id = buddy->index;
241
 
    if (pjsua_var.buddy[buddy_id].uri.slen == 0) {
242
 
        unlock_buddy(&lck);
243
 
        return PJ_SUCCESS;
244
 
    }
245
 
 
246
 
    /* uri */
247
 
    info->uri.ptr = info->buf_ + total;
248
 
    pj_strncpy(&info->uri, &buddy->uri, sizeof(info->buf_)-total);
249
 
    total += info->uri.slen;
250
 
 
251
 
    /* contact */
252
 
    info->contact.ptr = info->buf_ + total;
253
 
    pj_strncpy(&info->contact, &buddy->contact, sizeof(info->buf_)-total);
254
 
    total += info->contact.slen;
255
 
 
256
 
    /* Presence status */
257
 
    pj_memcpy(&info->pres_status, &buddy->status, sizeof(pjsip_pres_status));
258
 
 
259
 
    /* status and status text */
260
 
    if (buddy->sub == NULL || buddy->status.info_cnt==0) {
261
 
        info->status = PJSUA_BUDDY_STATUS_UNKNOWN;
262
 
        info->status_text = pj_str("?");
263
 
    } else if (pjsua_var.buddy[buddy_id].status.info[0].basic_open) {
264
 
        info->status = PJSUA_BUDDY_STATUS_ONLINE;
265
 
 
266
 
        /* copy RPID information */
267
 
        info->rpid = buddy->status.info[0].rpid;
268
 
 
269
 
        if (info->rpid.note.slen)
270
 
            info->status_text = info->rpid.note;
271
 
        else
272
 
            info->status_text = pj_str("Online");
273
 
 
274
 
    } else {
275
 
        info->status = PJSUA_BUDDY_STATUS_OFFLINE;
276
 
        info->rpid = buddy->status.info[0].rpid;
277
 
 
278
 
        if (info->rpid.note.slen)
279
 
            info->status_text = info->rpid.note;
280
 
        else
281
 
            info->status_text = pj_str("Offline");
282
 
    }
283
 
 
284
 
    /* monitor pres */
285
 
    info->monitor_pres = buddy->monitor;
286
 
 
287
 
    /* subscription state and termination reason */
288
 
    info->sub_term_code = buddy->term_code;
289
 
    if (buddy->sub) {
290
 
        info->sub_state = pjsip_evsub_get_state(buddy->sub);
291
 
        info->sub_state_name = pjsip_evsub_get_state_name(buddy->sub);
292
 
        if (info->sub_state == PJSIP_EVSUB_STATE_TERMINATED &&
293
 
            total < sizeof(info->buf_))
294
 
        {
295
 
            info->sub_term_reason.ptr = info->buf_ + total;
296
 
            pj_strncpy(&info->sub_term_reason,
297
 
                       pjsip_evsub_get_termination_reason(buddy->sub),
298
 
                       sizeof(info->buf_) - total);
299
 
            total += info->sub_term_reason.slen;
300
 
        } else {
301
 
            info->sub_term_reason = pj_str("");
302
 
        }
303
 
    } else if (total < sizeof(info->buf_)) {
304
 
        info->sub_state_name = "NULL";
305
 
        info->sub_term_reason.ptr = info->buf_ + total;
306
 
        pj_strncpy(&info->sub_term_reason, &buddy->term_reason,
307
 
                   sizeof(info->buf_) - total);
308
 
        total += info->sub_term_reason.slen;
309
 
    } else {
310
 
        info->sub_state_name = "NULL";
311
 
        info->sub_term_reason = pj_str("");
312
 
    }
313
 
 
314
 
    unlock_buddy(&lck);
315
 
    return PJ_SUCCESS;
316
 
}
317
 
 
318
 
/*
319
 
 * Set the user data associated with the buddy object.
320
 
 */
321
 
PJ_DEF(pj_status_t) pjsua_buddy_set_user_data( pjsua_buddy_id buddy_id,
322
 
                                               void *user_data)
323
 
{
324
 
    struct buddy_lock lck;
325
 
    pj_status_t status;
326
 
 
327
 
    PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
328
 
 
329
 
    status = lock_buddy("pjsua_buddy_set_user_data()", buddy_id, &lck, 0);
330
 
    if (status != PJ_SUCCESS)
331
 
        return status;
332
 
 
333
 
    pjsua_var.buddy[buddy_id].user_data = user_data;
334
 
 
335
 
    unlock_buddy(&lck);
336
 
 
337
 
    return PJ_SUCCESS;
338
 
}
339
 
 
340
 
 
341
 
/*
342
 
 * Get the user data associated with the budy object.
343
 
 */
344
 
PJ_DEF(void*) pjsua_buddy_get_user_data(pjsua_buddy_id buddy_id)
345
 
{
346
 
    struct buddy_lock lck;
347
 
    pj_status_t status;
348
 
    void *user_data;
349
 
 
350
 
    PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), NULL);
351
 
 
352
 
    status = lock_buddy("pjsua_buddy_get_user_data()", buddy_id, &lck, 0);
353
 
    if (status != PJ_SUCCESS)
354
 
        return NULL;
355
 
 
356
 
    user_data = pjsua_var.buddy[buddy_id].user_data;
357
 
 
358
 
    unlock_buddy(&lck);
359
 
 
360
 
    return user_data;
361
 
}
362
 
 
363
 
 
364
 
/*
365
 
 * Reset buddy descriptor.
366
 
 */
367
 
static void reset_buddy(pjsua_buddy_id id)
368
 
{
369
 
    pj_pool_t *pool = pjsua_var.buddy[id].pool;
370
 
    pj_bzero(&pjsua_var.buddy[id], sizeof(pjsua_var.buddy[id]));
371
 
    pjsua_var.buddy[id].pool = pool;
372
 
    pjsua_var.buddy[id].index = id;
373
 
}
374
 
 
375
 
 
376
 
/*
377
 
 * Add new buddy.
378
 
 */
379
 
PJ_DEF(pj_status_t) pjsua_buddy_add( const pjsua_buddy_config *cfg,
380
 
                                     pjsua_buddy_id *p_buddy_id)
381
 
{
382
 
    pjsip_name_addr *url;
383
 
    pjsua_buddy *buddy;
384
 
    pjsip_sip_uri *sip_uri;
385
 
    int index;
386
 
    pj_str_t tmp;
387
 
 
388
 
    PJ_ASSERT_RETURN(pjsua_var.buddy_cnt <=
389
 
                        PJ_ARRAY_SIZE(pjsua_var.buddy),
390
 
                     PJ_ETOOMANY);
391
 
 
392
 
    PJ_LOG(4,(THIS_FILE, "Adding buddy: %.*s",
393
 
              (int)cfg->uri.slen, cfg->uri.ptr));
394
 
    pj_log_push_indent();
395
 
 
396
 
    PJSUA_LOCK();
397
 
 
398
 
    /* Find empty slot */
399
 
    for (index=0; index<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++index) {
400
 
        if (pjsua_var.buddy[index].uri.slen == 0)
401
 
            break;
402
 
    }
403
 
 
404
 
    /* Expect to find an empty slot */
405
 
    if (index == PJ_ARRAY_SIZE(pjsua_var.buddy)) {
406
 
        PJSUA_UNLOCK();
407
 
        /* This shouldn't happen */
408
 
        pj_assert(!"index < PJ_ARRAY_SIZE(pjsua_var.buddy)");
409
 
        pj_log_pop_indent();
410
 
        return PJ_ETOOMANY;
411
 
    }
412
 
 
413
 
    buddy = &pjsua_var.buddy[index];
414
 
 
415
 
    /* Create pool for this buddy */
416
 
    if (buddy->pool) {
417
 
        pj_pool_reset(buddy->pool);
418
 
    } else {
419
 
        char name[PJ_MAX_OBJ_NAME];
420
 
        pj_ansi_snprintf(name, sizeof(name), "buddy%03d", index);
421
 
        buddy->pool = pjsua_pool_create(name, 512, 256);
422
 
    }
423
 
 
424
 
    /* Init buffers for presence subscription status */
425
 
    buddy->term_reason.ptr = (char*)
426
 
                             pj_pool_alloc(buddy->pool,
427
 
                                           PJSUA_BUDDY_SUB_TERM_REASON_LEN);
428
 
 
429
 
    /* Get name and display name for buddy */
430
 
    pj_strdup_with_null(buddy->pool, &tmp, &cfg->uri);
431
 
    url = (pjsip_name_addr*)pjsip_parse_uri(buddy->pool, tmp.ptr, tmp.slen,
432
 
                                            PJSIP_PARSE_URI_AS_NAMEADDR);
433
 
 
434
 
    if (url == NULL) {
435
 
        pjsua_perror(THIS_FILE, "Unable to add buddy", PJSIP_EINVALIDURI);
436
 
        pj_pool_release(buddy->pool);
437
 
        buddy->pool = NULL;
438
 
        PJSUA_UNLOCK();
439
 
        pj_log_pop_indent();
440
 
        return PJSIP_EINVALIDURI;
441
 
    }
442
 
 
443
 
    /* Only support SIP schemes */
444
 
    if (!PJSIP_URI_SCHEME_IS_SIP(url) && !PJSIP_URI_SCHEME_IS_SIPS(url)) {
445
 
        pj_pool_release(buddy->pool);
446
 
        buddy->pool = NULL;
447
 
        PJSUA_UNLOCK();
448
 
        pj_log_pop_indent();
449
 
        return PJSIP_EINVALIDSCHEME;
450
 
    }
451
 
 
452
 
    /* Reset buddy, to make sure everything is cleared with default
453
 
     * values
454
 
     */
455
 
    reset_buddy(index);
456
 
 
457
 
    /* Save URI */
458
 
    pjsua_var.buddy[index].uri = tmp;
459
 
 
460
 
    sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(url->uri);
461
 
    pjsua_var.buddy[index].name = sip_uri->user;
462
 
    pjsua_var.buddy[index].display = url->display;
463
 
    pjsua_var.buddy[index].host = sip_uri->host;
464
 
    pjsua_var.buddy[index].port = sip_uri->port;
465
 
    pjsua_var.buddy[index].monitor = cfg->subscribe;
466
 
    if (pjsua_var.buddy[index].port == 0)
467
 
        pjsua_var.buddy[index].port = 5060;
468
 
 
469
 
    /* Save user data */
470
 
    pjsua_var.buddy[index].user_data = (void*)cfg->user_data;
471
 
 
472
 
    if (p_buddy_id)
473
 
        *p_buddy_id = index;
474
 
 
475
 
    pjsua_var.buddy_cnt++;
476
 
 
477
 
    PJSUA_UNLOCK();
478
 
 
479
 
    PJ_LOG(4,(THIS_FILE, "Buddy %d added.", index));
480
 
 
481
 
    pjsua_buddy_subscribe_pres(index, cfg->subscribe);
482
 
 
483
 
    pj_log_pop_indent();
484
 
    return PJ_SUCCESS;
485
 
}
486
 
 
487
 
 
488
 
/*
489
 
 * Delete buddy.
490
 
 */
491
 
PJ_DEF(pj_status_t) pjsua_buddy_del(pjsua_buddy_id buddy_id)
492
 
{
493
 
    struct buddy_lock lck;
494
 
    pj_status_t status;
495
 
 
496
 
    PJ_ASSERT_RETURN(buddy_id>=0 &&
497
 
                        buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy),
498
 
                     PJ_EINVAL);
499
 
 
500
 
    if (pjsua_var.buddy[buddy_id].uri.slen == 0) {
501
 
        return PJ_SUCCESS;
502
 
    }
503
 
 
504
 
    status = lock_buddy("pjsua_buddy_del()", buddy_id, &lck, 0);
505
 
    if (status != PJ_SUCCESS)
506
 
        return status;
507
 
 
508
 
    PJ_LOG(4,(THIS_FILE, "Buddy %d: deleting..", buddy_id));
509
 
    pj_log_push_indent();
510
 
 
511
 
    /* Unsubscribe presence */
512
 
    pjsua_buddy_subscribe_pres(buddy_id, PJ_FALSE);
513
 
 
514
 
    /* Not interested with further events for this buddy */
515
 
    if (pjsua_var.buddy[buddy_id].sub) {
516
 
        pjsip_evsub_set_mod_data(pjsua_var.buddy[buddy_id].sub,
517
 
                                 pjsua_var.mod.id, NULL);
518
 
    }
519
 
 
520
 
    /* Remove buddy */
521
 
    pjsua_var.buddy[buddy_id].uri.slen = 0;
522
 
    pjsua_var.buddy_cnt--;
523
 
 
524
 
    /* Clear timer */
525
 
    if (pjsua_var.buddy[buddy_id].timer.id) {
526
 
        pjsua_cancel_timer(&pjsua_var.buddy[buddy_id].timer);
527
 
        pjsua_var.buddy[buddy_id].timer.id = PJ_FALSE;
528
 
    }
529
 
 
530
 
    /* Reset buddy struct */
531
 
    reset_buddy(buddy_id);
532
 
 
533
 
    unlock_buddy(&lck);
534
 
    pj_log_pop_indent();
535
 
    return PJ_SUCCESS;
536
 
}
537
 
 
538
 
 
539
 
/*
540
 
 * Enable/disable buddy's presence monitoring.
541
 
 */
542
 
PJ_DEF(pj_status_t) pjsua_buddy_subscribe_pres( pjsua_buddy_id buddy_id,
543
 
                                                pj_bool_t subscribe)
544
 
{
545
 
    struct buddy_lock lck;
546
 
    pj_status_t status;
547
 
 
548
 
    PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
549
 
 
550
 
    status = lock_buddy("pjsua_buddy_subscribe_pres()", buddy_id, &lck, 0);
551
 
    if (status != PJ_SUCCESS)
552
 
        return status;
553
 
 
554
 
    PJ_LOG(4,(THIS_FILE, "Buddy %d: unsubscribing presence..", buddy_id));
555
 
    pj_log_push_indent();
556
 
 
557
 
    lck.buddy->monitor = subscribe;
558
 
 
559
 
    pjsua_buddy_update_pres(buddy_id);
560
 
 
561
 
    unlock_buddy(&lck);
562
 
    pj_log_pop_indent();
563
 
    return PJ_SUCCESS;
564
 
}
565
 
 
566
 
 
567
 
/*
568
 
 * Update buddy's presence.
569
 
 */
570
 
PJ_DEF(pj_status_t) pjsua_buddy_update_pres(pjsua_buddy_id buddy_id)
571
 
{
572
 
    struct buddy_lock lck;
573
 
    pj_status_t status;
574
 
 
575
 
    PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
576
 
 
577
 
    status = lock_buddy("pjsua_buddy_update_pres()", buddy_id, &lck, 0);
578
 
    if (status != PJ_SUCCESS)
579
 
        return status;
580
 
 
581
 
    PJ_LOG(4,(THIS_FILE, "Buddy %d: updating presence..", buddy_id));
582
 
    pj_log_push_indent();
583
 
 
584
 
    /* Is this an unsubscribe request? */
585
 
    if (!lck.buddy->monitor) {
586
 
        unsubscribe_buddy_presence(buddy_id);
587
 
        unlock_buddy(&lck);
588
 
        pj_log_pop_indent();
589
 
        return PJ_SUCCESS;
590
 
    }
591
 
 
592
 
    /* Ignore if presence is already active for the buddy */
593
 
    if (lck.buddy->sub) {
594
 
        unlock_buddy(&lck);
595
 
        pj_log_pop_indent();
596
 
        return PJ_SUCCESS;
597
 
    }
598
 
 
599
 
    /* Initiate presence subscription */
600
 
    subscribe_buddy_presence(buddy_id);
601
 
 
602
 
    unlock_buddy(&lck);
603
 
    pj_log_pop_indent();
604
 
    return PJ_SUCCESS;
605
 
}
606
 
 
607
 
 
608
 
/*
609
 
 * Dump presence subscriptions to log file.
610
 
 */
611
 
PJ_DEF(void) pjsua_pres_dump(pj_bool_t verbose)
612
 
{
613
 
    unsigned acc_id;
614
 
    unsigned i;
615
 
 
616
 
 
617
 
    PJSUA_LOCK();
618
 
 
619
 
    /*
620
 
     * When no detail is required, just dump number of server and client
621
 
     * subscriptions.
622
 
     */
623
 
    if (verbose == PJ_FALSE) {
624
 
 
625
 
        int count = 0;
626
 
 
627
 
        for (acc_id=0; acc_id<PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
628
 
 
629
 
            if (!pjsua_var.acc[acc_id].valid)
630
 
                continue;
631
 
 
632
 
            if (!pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) {
633
 
                struct pjsua_srv_pres *uapres;
634
 
 
635
 
                uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
636
 
                while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) {
637
 
                    ++count;
638
 
                    uapres = uapres->next;
639
 
                }
640
 
            }
641
 
        }
642
 
 
643
 
        PJ_LOG(3,(THIS_FILE, "Number of server/UAS subscriptions: %d",
644
 
                  count));
645
 
 
646
 
        count = 0;
647
 
 
648
 
        for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
649
 
            if (pjsua_var.buddy[i].uri.slen == 0)
650
 
                continue;
651
 
            if (pjsua_var.buddy[i].sub) {
652
 
                ++count;
653
 
            }
654
 
        }
655
 
 
656
 
        PJ_LOG(3,(THIS_FILE, "Number of client/UAC subscriptions: %d",
657
 
                  count));
658
 
        PJSUA_UNLOCK();
659
 
        return;
660
 
    }
661
 
 
662
 
 
663
 
    /*
664
 
     * Dumping all server (UAS) subscriptions
665
 
     */
666
 
    PJ_LOG(3,(THIS_FILE, "Dumping pjsua server subscriptions:"));
667
 
 
668
 
    for (acc_id=0; acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
669
 
 
670
 
        if (!pjsua_var.acc[acc_id].valid)
671
 
            continue;
672
 
 
673
 
        PJ_LOG(3,(THIS_FILE, "  %.*s",
674
 
                  (int)pjsua_var.acc[acc_id].cfg.id.slen,
675
 
                  pjsua_var.acc[acc_id].cfg.id.ptr));
676
 
 
677
 
        if (pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) {
678
 
 
679
 
            PJ_LOG(3,(THIS_FILE, "  - none - "));
680
 
 
681
 
        } else {
682
 
            struct pjsua_srv_pres *uapres;
683
 
 
684
 
            uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
685
 
            while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) {
686
 
 
687
 
                PJ_LOG(3,(THIS_FILE, "    %10s %s",
688
 
                          pjsip_evsub_get_state_name(uapres->sub),
689
 
                          uapres->remote));
690
 
 
691
 
                uapres = uapres->next;
692
 
            }
693
 
        }
694
 
    }
695
 
 
696
 
    /*
697
 
     * Dumping all client (UAC) subscriptions
698
 
     */
699
 
    PJ_LOG(3,(THIS_FILE, "Dumping pjsua client subscriptions:"));
700
 
 
701
 
    if (pjsua_var.buddy_cnt == 0) {
702
 
 
703
 
        PJ_LOG(3,(THIS_FILE, "  - no buddy list - "));
704
 
 
705
 
    } else {
706
 
        for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
707
 
 
708
 
            if (pjsua_var.buddy[i].uri.slen == 0)
709
 
                continue;
710
 
 
711
 
            if (pjsua_var.buddy[i].sub) {
712
 
                PJ_LOG(3,(THIS_FILE, "  %10s %.*s",
713
 
                          pjsip_evsub_get_state_name(pjsua_var.buddy[i].sub),
714
 
                          (int)pjsua_var.buddy[i].uri.slen,
715
 
                          pjsua_var.buddy[i].uri.ptr));
716
 
            } else {
717
 
                PJ_LOG(3,(THIS_FILE, "  %10s %.*s",
718
 
                          "(null)",
719
 
                          (int)pjsua_var.buddy[i].uri.slen,
720
 
                          pjsua_var.buddy[i].uri.ptr));
721
 
            }
722
 
        }
723
 
    }
724
 
 
725
 
    PJSUA_UNLOCK();
726
 
}
727
 
 
728
 
 
729
 
/***************************************************************************
730
 
 * Server subscription.
731
 
 */
732
 
 
733
 
/* Proto */
734
 
static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata);
735
 
 
736
 
/* The module instance. */
737
 
static pjsip_module mod_pjsua_pres =
738
 
{
739
 
    NULL, NULL,                         /* prev, next.          */
740
 
    { "mod-pjsua-pres", 14 },           /* Name.                */
741
 
    -1,                                 /* Id                   */
742
 
    PJSIP_MOD_PRIORITY_APPLICATION,     /* Priority             */
743
 
    NULL,                               /* load()               */
744
 
    NULL,                               /* start()              */
745
 
    NULL,                               /* stop()               */
746
 
    NULL,                               /* unload()             */
747
 
    &pres_on_rx_request,                /* on_rx_request()      */
748
 
    NULL,                               /* on_rx_response()     */
749
 
    NULL,                               /* on_tx_request.       */
750
 
    NULL,                               /* on_tx_response()     */
751
 
    NULL,                               /* on_tsx_state()       */
752
 
 
753
 
};
754
 
 
755
 
 
756
 
/* Callback called when *server* subscription state has changed. */
757
 
static void pres_evsub_on_srv_state( pjsip_evsub *sub, pjsip_event *event)
758
 
{
759
 
    pjsua_srv_pres *uapres;
760
 
 
761
 
    PJ_UNUSED_ARG(event);
762
 
 
763
 
    PJSUA_LOCK();
764
 
 
765
 
    uapres = (pjsua_srv_pres*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
766
 
    if (uapres) {
767
 
        pjsip_evsub_state state;
768
 
 
769
 
        PJ_LOG(4,(THIS_FILE, "Server subscription to %s is %s",
770
 
                  uapres->remote, pjsip_evsub_get_state_name(sub)));
771
 
        pj_log_push_indent();
772
 
 
773
 
        state = pjsip_evsub_get_state(sub);
774
 
 
775
 
        if (pjsua_var.ua_cfg.cb.on_srv_subscribe_state) {
776
 
            pj_str_t from;
777
 
 
778
 
            from = uapres->dlg->remote.info_str;
779
 
            (*pjsua_var.ua_cfg.cb.on_srv_subscribe_state)(uapres->acc_id,
780
 
                                                          uapres, &from,
781
 
                                                          state, event);
782
 
        }
783
 
 
784
 
        if (state == PJSIP_EVSUB_STATE_TERMINATED) {
785
 
            pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
786
 
            pj_list_erase(uapres);
787
 
        }
788
 
        pj_log_pop_indent();
789
 
    }
790
 
 
791
 
    PJSUA_UNLOCK();
792
 
}
793
 
 
794
 
/* This is called when request is received.
795
 
 * We need to check for incoming SUBSCRIBE request.
796
 
 */
797
 
static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata)
798
 
{
799
 
    int acc_id;
800
 
    pjsua_acc *acc;
801
 
    pj_str_t contact;
802
 
    pjsip_method *req_method = &rdata->msg_info.msg->line.req.method;
803
 
    pjsua_srv_pres *uapres;
804
 
    pjsip_evsub *sub;
805
 
    pjsip_evsub_user pres_cb;
806
 
    pjsip_dialog *dlg;
807
 
    pjsip_status_code st_code;
808
 
    pj_str_t reason;
809
 
    pjsip_expires_hdr *expires_hdr;
810
 
    pjsua_msg_data msg_data;
811
 
    pj_status_t status;
812
 
 
813
 
    if (pjsip_method_cmp(req_method, pjsip_get_subscribe_method()) != 0)
814
 
        return PJ_FALSE;
815
 
 
816
 
    /* Incoming SUBSCRIBE: */
817
 
 
818
 
    /* Don't want to accept the request if shutdown is in progress */
819
 
    if (pjsua_var.thread_quit_flag) {
820
 
        pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
821
 
                                      PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL,
822
 
                                      NULL, NULL);
823
 
        return PJ_TRUE;
824
 
    }
825
 
 
826
 
    PJSUA_LOCK();
827
 
 
828
 
    /* Find which account for the incoming request. */
829
 
    acc_id = pjsua_acc_find_for_incoming(rdata);
830
 
    acc = &pjsua_var.acc[acc_id];
831
 
 
832
 
    PJ_LOG(4,(THIS_FILE, "Creating server subscription, using account %d",
833
 
              acc_id));
834
 
    pj_log_push_indent();
835
 
 
836
 
    /* Create suitable Contact header */
837
 
    if (acc->contact.slen) {
838
 
        contact = acc->contact;
839
 
    } else {
840
 
        status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact,
841
 
                                              acc_id, rdata);
842
 
        if (status != PJ_SUCCESS) {
843
 
            pjsua_perror(THIS_FILE, "Unable to generate Contact header",
844
 
                         status);
845
 
            PJSUA_UNLOCK();
846
 
            pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL,
847
 
                                          NULL, NULL);
848
 
            pj_log_pop_indent();
849
 
            return PJ_TRUE;
850
 
        }
851
 
    }
852
 
 
853
 
    /* Create UAS dialog: */
854
 
    status = pjsip_dlg_create_uas(pjsip_ua_instance(), rdata,
855
 
                                  &contact, &dlg);
856
 
    if (status != PJ_SUCCESS) {
857
 
        pjsua_perror(THIS_FILE,
858
 
                     "Unable to create UAS dialog for subscription",
859
 
                     status);
860
 
        PJSUA_UNLOCK();
861
 
        pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL,
862
 
                                      NULL, NULL);
863
 
        pj_log_pop_indent();
864
 
        return PJ_TRUE;
865
 
    }
866
 
 
867
 
    if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0)
868
 
        pjsip_dlg_set_via_sent_by(dlg, &acc->via_addr, acc->via_tp);
869
 
 
870
 
    /* Set credentials and preference. */
871
 
    pjsip_auth_clt_set_credentials(&dlg->auth_sess, acc->cred_cnt, acc->cred);
872
 
    pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref);
873
 
 
874
 
    /* Init callback: */
875
 
    pj_bzero(&pres_cb, sizeof(pres_cb));
876
 
    pres_cb.on_evsub_state = &pres_evsub_on_srv_state;
877
 
 
878
 
    /* Create server presence subscription: */
879
 
    status = pjsip_pres_create_uas( dlg, &pres_cb, rdata, &sub);
880
 
    if (status != PJ_SUCCESS) {
881
 
        int code = PJSIP_ERRNO_TO_SIP_STATUS(status);
882
 
        pjsip_tx_data *tdata;
883
 
 
884
 
        pjsua_perror(THIS_FILE, "Unable to create server subscription",
885
 
                     status);
886
 
 
887
 
        if (code==599 || code > 699 || code < 300) {
888
 
            code = 400;
889
 
        }
890
 
 
891
 
        status = pjsip_dlg_create_response(dlg, rdata, code, NULL, &tdata);
892
 
        if (status == PJ_SUCCESS) {
893
 
            status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata),
894
 
                                             tdata);
895
 
        }
896
 
 
897
 
        PJSUA_UNLOCK();
898
 
        pj_log_pop_indent();
899
 
        return PJ_TRUE;
900
 
    }
901
 
 
902
 
    /* If account is locked to specific transport, then lock dialog
903
 
     * to this transport too.
904
 
     */
905
 
    if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
906
 
        pjsip_tpselector tp_sel;
907
 
 
908
 
        pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
909
 
        pjsip_dlg_set_transport(dlg, &tp_sel);
910
 
    }
911
 
 
912
 
    /* Attach our data to the subscription: */
913
 
    uapres = PJ_POOL_ALLOC_T(dlg->pool, pjsua_srv_pres);
914
 
    uapres->sub = sub;
915
 
    uapres->remote = (char*) pj_pool_alloc(dlg->pool, PJSIP_MAX_URL_SIZE);
916
 
    uapres->acc_id = acc_id;
917
 
    uapres->dlg = dlg;
918
 
    status = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, dlg->remote.info->uri,
919
 
                             uapres->remote, PJSIP_MAX_URL_SIZE);
920
 
    if (status < 1)
921
 
        pj_ansi_strcpy(uapres->remote, "<-- url is too long-->");
922
 
    else
923
 
        uapres->remote[status] = '\0';
924
 
 
925
 
    pjsip_evsub_add_header(sub, &acc->cfg.sub_hdr_list);
926
 
    pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, uapres);
927
 
 
928
 
    /* Add server subscription to the list: */
929
 
    pj_list_push_back(&pjsua_var.acc[acc_id].pres_srv_list, uapres);
930
 
 
931
 
 
932
 
    /* Capture the value of Expires header. */
933
 
    expires_hdr = (pjsip_expires_hdr*)
934
 
                  pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES,
935
 
                                     NULL);
936
 
    if (expires_hdr)
937
 
        uapres->expires = expires_hdr->ivalue;
938
 
    else
939
 
        uapres->expires = -1;
940
 
 
941
 
    st_code = (pjsip_status_code)200;
942
 
    reason = pj_str("OK");
943
 
    pjsua_msg_data_init(&msg_data);
944
 
 
945
 
    /* Notify application callback, if any */
946
 
    if (pjsua_var.ua_cfg.cb.on_incoming_subscribe) {
947
 
        pjsua_buddy_id buddy_id;
948
 
 
949
 
        buddy_id = find_buddy(rdata->msg_info.from->uri);
950
 
 
951
 
        (*pjsua_var.ua_cfg.cb.on_incoming_subscribe)(acc_id, uapres, buddy_id,
952
 
                                                     &dlg->remote.info_str,
953
 
                                                     rdata, &st_code, &reason,
954
 
                                                     &msg_data);
955
 
    }
956
 
 
957
 
    /* Handle rejection case */
958
 
    if (st_code >= 300) {
959
 
        pjsip_tx_data *tdata;
960
 
 
961
 
        /* Create response */
962
 
        status = pjsip_dlg_create_response(dlg, rdata, st_code,
963
 
                                           &reason, &tdata);
964
 
        if (status != PJ_SUCCESS) {
965
 
            pjsua_perror(THIS_FILE, "Error creating response",  status);
966
 
            pj_list_erase(uapres);
967
 
            pjsip_pres_terminate(sub, PJ_FALSE);
968
 
            PJSUA_UNLOCK();
969
 
            pj_log_pop_indent();
970
 
            return PJ_FALSE;
971
 
        }
972
 
 
973
 
        /* Add header list, if any */
974
 
        pjsua_process_msg_data(tdata, &msg_data);
975
 
 
976
 
        /* Send the response */
977
 
        status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata),
978
 
                                         tdata);
979
 
        if (status != PJ_SUCCESS) {
980
 
            pjsua_perror(THIS_FILE, "Error sending response",  status);
981
 
            /* This is not fatal */
982
 
        }
983
 
 
984
 
        /* Terminate presence subscription */
985
 
        pj_list_erase(uapres);
986
 
        pjsip_pres_terminate(sub, PJ_FALSE);
987
 
        PJSUA_UNLOCK();
988
 
        pj_log_pop_indent();
989
 
        return PJ_TRUE;
990
 
    }
991
 
 
992
 
    /* Create and send 2xx response to the SUBSCRIBE request: */
993
 
    status = pjsip_pres_accept(sub, rdata, st_code, &msg_data.hdr_list);
994
 
    if (status != PJ_SUCCESS) {
995
 
        pjsua_perror(THIS_FILE, "Unable to accept presence subscription",
996
 
                     status);
997
 
        pj_list_erase(uapres);
998
 
        pjsip_pres_terminate(sub, PJ_FALSE);
999
 
        PJSUA_UNLOCK();
1000
 
        pj_log_pop_indent();
1001
 
        return PJ_FALSE;
1002
 
    }
1003
 
 
1004
 
    /* If code is 200, send NOTIFY now */
1005
 
    if (st_code == 200) {
1006
 
        pjsua_pres_notify(acc_id, uapres, PJSIP_EVSUB_STATE_ACTIVE,
1007
 
                          NULL, NULL, PJ_TRUE, &msg_data);
1008
 
    }
1009
 
 
1010
 
    /* Done: */
1011
 
 
1012
 
    PJSUA_UNLOCK();
1013
 
    pj_log_pop_indent();
1014
 
    return PJ_TRUE;
1015
 
}
1016
 
 
1017
 
 
1018
 
/*
1019
 
 * Send NOTIFY.
1020
 
 */
1021
 
PJ_DEF(pj_status_t) pjsua_pres_notify( pjsua_acc_id acc_id,
1022
 
                                       pjsua_srv_pres *srv_pres,
1023
 
                                       pjsip_evsub_state ev_state,
1024
 
                                       const pj_str_t *state_str,
1025
 
                                       const pj_str_t *reason,
1026
 
                                       pj_bool_t with_body,
1027
 
                                       const pjsua_msg_data *msg_data)
1028
 
{
1029
 
    pjsua_acc *acc;
1030
 
    pjsip_pres_status pres_status;
1031
 
    pjsua_buddy_id buddy_id;
1032
 
    pjsip_tx_data *tdata;
1033
 
    pj_status_t status;
1034
 
 
1035
 
    /* Check parameters */
1036
 
    PJ_ASSERT_RETURN(acc_id!=-1 && srv_pres, PJ_EINVAL);
1037
 
 
1038
 
    /* Check that account ID is valid */
1039
 
    PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1040
 
                     PJ_EINVAL);
1041
 
    /* Check that account is valid */
1042
 
    PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1043
 
 
1044
 
    PJ_LOG(4,(THIS_FILE, "Acc %d: sending NOTIFY for srv_pres=0x%p..",
1045
 
              acc_id, (int)(long)srv_pres));
1046
 
    pj_log_push_indent();
1047
 
 
1048
 
    PJSUA_LOCK();
1049
 
 
1050
 
    acc = &pjsua_var.acc[acc_id];
1051
 
 
1052
 
    /* Check that the server presence subscription is still valid */
1053
 
    if (pj_list_find_node(&acc->pres_srv_list, srv_pres) == NULL) {
1054
 
        /* Subscription has been terminated */
1055
 
        PJSUA_UNLOCK();
1056
 
        pj_log_pop_indent();
1057
 
        return PJ_EINVALIDOP;
1058
 
    }
1059
 
 
1060
 
    /* Set our online status: */
1061
 
    pj_bzero(&pres_status, sizeof(pres_status));
1062
 
    pres_status.info_cnt = 1;
1063
 
    pres_status.info[0].basic_open = acc->online_status;
1064
 
    pres_status.info[0].id = acc->cfg.pidf_tuple_id;
1065
 
    //Both pjsua_var.local_uri and pjsua_var.contact_uri are enclosed in "<" and ">"
1066
 
    //causing XML parsing to fail.
1067
 
    //pres_status.info[0].contact = pjsua_var.local_uri;
1068
 
    /* add RPID information */
1069
 
    pj_memcpy(&pres_status.info[0].rpid, &acc->rpid,
1070
 
              sizeof(pjrpid_element));
1071
 
 
1072
 
    pjsip_pres_set_status(srv_pres->sub, &pres_status);
1073
 
 
1074
 
    /* Check expires value. If it's zero, send our presense state but
1075
 
     * set subscription state to TERMINATED.
1076
 
     */
1077
 
    if (srv_pres->expires == 0)
1078
 
        ev_state = PJSIP_EVSUB_STATE_TERMINATED;
1079
 
 
1080
 
    /* Create and send the NOTIFY to active subscription: */
1081
 
    status = pjsip_pres_notify(srv_pres->sub, ev_state, state_str,
1082
 
                               reason, &tdata);
1083
 
    if (status == PJ_SUCCESS) {
1084
 
        /* Force removal of message body if msg_body==FALSE */
1085
 
        if (!with_body) {
1086
 
            tdata->msg->body = NULL;
1087
 
        }
1088
 
        pjsua_process_msg_data(tdata, msg_data);
1089
 
        status = pjsip_pres_send_request( srv_pres->sub, tdata);
1090
 
    }
1091
 
 
1092
 
    if (status != PJ_SUCCESS) {
1093
 
        pjsua_perror(THIS_FILE, "Unable to create/send NOTIFY",
1094
 
                     status);
1095
 
        pj_list_erase(srv_pres);
1096
 
        pjsip_pres_terminate(srv_pres->sub, PJ_FALSE);
1097
 
        PJSUA_UNLOCK();
1098
 
        pj_log_pop_indent();
1099
 
        return status;
1100
 
    }
1101
 
 
1102
 
 
1103
 
    /* Subscribe to buddy's presence if we're not subscribed */
1104
 
    buddy_id = find_buddy(srv_pres->dlg->remote.info->uri);
1105
 
    if (buddy_id != PJSUA_INVALID_ID) {
1106
 
        pjsua_buddy *b = &pjsua_var.buddy[buddy_id];
1107
 
        if (b->monitor && b->sub == NULL) {
1108
 
            PJ_LOG(4,(THIS_FILE, "Received SUBSCRIBE from buddy %d, "
1109
 
                      "activating outgoing subscription", buddy_id));
1110
 
            subscribe_buddy_presence(buddy_id);
1111
 
        }
1112
 
    }
1113
 
 
1114
 
    PJSUA_UNLOCK();
1115
 
    pj_log_pop_indent();
1116
 
    return PJ_SUCCESS;
1117
 
}
1118
 
 
1119
 
 
1120
 
/*
1121
 
 * Client presence publication callback.
1122
 
 */
1123
 
static void publish_cb(struct pjsip_publishc_cbparam *param)
1124
 
{
1125
 
    pjsua_acc *acc = (pjsua_acc*) param->token;
1126
 
 
1127
 
    if (param->code/100 != 2 || param->status != PJ_SUCCESS) {
1128
 
 
1129
 
        pjsip_publishc_destroy(param->pubc);
1130
 
        acc->publish_sess = NULL;
1131
 
 
1132
 
        if (param->status != PJ_SUCCESS) {
1133
 
            char errmsg[PJ_ERR_MSG_SIZE];
1134
 
 
1135
 
            pj_strerror(param->status, errmsg, sizeof(errmsg));
1136
 
            PJ_LOG(1,(THIS_FILE,
1137
 
                      "Client publication (PUBLISH) failed, status=%d, msg=%s",
1138
 
                       param->status, errmsg));
1139
 
        } else if (param->code == 412) {
1140
 
            /* 412 (Conditional Request Failed)
1141
 
             * The PUBLISH refresh has failed, retry with new one.
1142
 
             */
1143
 
            pjsua_pres_init_publish_acc(acc->index);
1144
 
 
1145
 
        } else {
1146
 
            PJ_LOG(1,(THIS_FILE,
1147
 
                      "Client publication (PUBLISH) failed (%d/%.*s)",
1148
 
                       param->code, (int)param->reason.slen,
1149
 
                       param->reason.ptr));
1150
 
        }
1151
 
 
1152
 
    } else {
1153
 
        if (param->expiration < 1) {
1154
 
            /* Could happen if server "forgot" to include Expires header
1155
 
             * in the response. We will not renew, so destroy the pubc.
1156
 
             */
1157
 
            pjsip_publishc_destroy(param->pubc);
1158
 
            acc->publish_sess = NULL;
1159
 
        }
1160
 
    }
1161
 
}
1162
 
 
1163
 
 
1164
 
/*
1165
 
 * Send PUBLISH request.
1166
 
 */
1167
 
static pj_status_t send_publish(int acc_id, pj_bool_t active)
1168
 
{
1169
 
    pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1170
 
    pjsua_acc *acc = &pjsua_var.acc[acc_id];
1171
 
    pjsip_pres_status pres_status;
1172
 
    pjsip_tx_data *tdata;
1173
 
    pj_status_t status;
1174
 
 
1175
 
    PJ_LOG(5,(THIS_FILE, "Acc %d: sending %sPUBLISH..",
1176
 
              acc_id, (active ? "" : "un-")));
1177
 
    pj_log_push_indent();
1178
 
 
1179
 
    /* Create PUBLISH request */
1180
 
    if (active) {
1181
 
        char *bpos;
1182
 
        pj_str_t entity;
1183
 
 
1184
 
        status = pjsip_publishc_publish(acc->publish_sess, PJ_TRUE, &tdata);
1185
 
        if (status != PJ_SUCCESS) {
1186
 
            pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status);
1187
 
            goto on_error;
1188
 
        }
1189
 
 
1190
 
        /* Set our online status: */
1191
 
        pj_bzero(&pres_status, sizeof(pres_status));
1192
 
        pres_status.info_cnt = 1;
1193
 
        pres_status.info[0].basic_open = acc->online_status;
1194
 
        pres_status.info[0].id = acc->cfg.pidf_tuple_id;
1195
 
        /* .. including RPID information */
1196
 
        pj_memcpy(&pres_status.info[0].rpid, &acc->rpid,
1197
 
                  sizeof(pjrpid_element));
1198
 
 
1199
 
        /* Be careful not to send PIDF with presence entity ID containing
1200
 
         * "<" character.
1201
 
         */
1202
 
        if ((bpos=pj_strchr(&acc_cfg->id, '<')) != NULL) {
1203
 
            char *epos = pj_strchr(&acc_cfg->id, '>');
1204
 
            if (epos - bpos < 2) {
1205
 
                pj_assert(!"Unexpected invalid URI");
1206
 
                status = PJSIP_EINVALIDURI;
1207
 
                goto on_error;
1208
 
            }
1209
 
            entity.ptr = bpos+1;
1210
 
            entity.slen = epos - bpos - 1;
1211
 
        } else {
1212
 
            entity = acc_cfg->id;
1213
 
        }
1214
 
 
1215
 
        /* Create and add PIDF message body */
1216
 
        status = pjsip_pres_create_pidf(tdata->pool, &pres_status,
1217
 
                                        &entity, &tdata->msg->body);
1218
 
        if (status != PJ_SUCCESS) {
1219
 
            pjsua_perror(THIS_FILE, "Error creating PIDF for PUBLISH request",
1220
 
                         status);
1221
 
            pjsip_tx_data_dec_ref(tdata);
1222
 
            goto on_error;
1223
 
        }
1224
 
    } else {
1225
 
        status = pjsip_publishc_unpublish(acc->publish_sess, &tdata);
1226
 
        if (status != PJ_SUCCESS) {
1227
 
            pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status);
1228
 
            goto on_error;
1229
 
        }
1230
 
    }
1231
 
 
1232
 
    /* Add headers etc */
1233
 
    pjsua_process_msg_data(tdata, NULL);
1234
 
 
1235
 
    /* Set Via sent-by */
1236
 
    if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
1237
 
        pjsip_publishc_set_via_sent_by(acc->publish_sess, &acc->via_addr,
1238
 
                                       acc->via_tp);
1239
 
    }
1240
 
 
1241
 
    /* Send the PUBLISH request */
1242
 
    status = pjsip_publishc_send(acc->publish_sess, tdata);
1243
 
    if (status == PJ_EPENDING) {
1244
 
        PJ_LOG(3,(THIS_FILE, "Previous request is in progress, "
1245
 
                  "PUBLISH request is queued"));
1246
 
    } else if (status != PJ_SUCCESS) {
1247
 
        pjsua_perror(THIS_FILE, "Error sending PUBLISH request", status);
1248
 
        goto on_error;
1249
 
    }
1250
 
 
1251
 
    acc->publish_state = acc->online_status;
1252
 
    pj_log_pop_indent();
1253
 
    return PJ_SUCCESS;
1254
 
 
1255
 
on_error:
1256
 
    if (acc->publish_sess) {
1257
 
        pjsip_publishc_destroy(acc->publish_sess);
1258
 
        acc->publish_sess = NULL;
1259
 
    }
1260
 
    pj_log_pop_indent();
1261
 
    return status;
1262
 
}
1263
 
 
1264
 
 
1265
 
/* Create client publish session */
1266
 
pj_status_t pjsua_pres_init_publish_acc(int acc_id)
1267
 
{
1268
 
    const pj_str_t STR_PRESENCE = { "presence", 8 };
1269
 
    pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1270
 
    pjsua_acc *acc = &pjsua_var.acc[acc_id];
1271
 
    pj_status_t status;
1272
 
 
1273
 
    /* Create and init client publication session */
1274
 
    if (acc_cfg->publish_enabled) {
1275
 
 
1276
 
        /* Create client publication */
1277
 
        status = pjsip_publishc_create(pjsua_var.endpt, &acc_cfg->publish_opt,
1278
 
                                       acc, &publish_cb,
1279
 
                                       &acc->publish_sess);
1280
 
        if (status != PJ_SUCCESS) {
1281
 
            acc->publish_sess = NULL;
1282
 
            return status;
1283
 
        }
1284
 
 
1285
 
        /* Initialize client publication */
1286
 
        status = pjsip_publishc_init(acc->publish_sess, &STR_PRESENCE,
1287
 
                                     &acc_cfg->id, &acc_cfg->id,
1288
 
                                     &acc_cfg->id,
1289
 
                                     PJSUA_PUBLISH_EXPIRATION);
1290
 
        if (status != PJ_SUCCESS) {
1291
 
            acc->publish_sess = NULL;
1292
 
            return status;
1293
 
        }
1294
 
 
1295
 
        /* Add credential for authentication */
1296
 
        if (acc->cred_cnt) {
1297
 
            pjsip_publishc_set_credentials(acc->publish_sess, acc->cred_cnt,
1298
 
                                           acc->cred);
1299
 
        }
1300
 
 
1301
 
        /* Set route-set */
1302
 
        pjsip_publishc_set_route_set(acc->publish_sess, &acc->route_set);
1303
 
 
1304
 
        /* Send initial PUBLISH request */
1305
 
        if (acc->online_status != 0) {
1306
 
            status = send_publish(acc_id, PJ_TRUE);
1307
 
            if (status != PJ_SUCCESS)
1308
 
                return status;
1309
 
        }
1310
 
 
1311
 
    } else {
1312
 
        acc->publish_sess = NULL;
1313
 
    }
1314
 
 
1315
 
    return PJ_SUCCESS;
1316
 
}
1317
 
 
1318
 
 
1319
 
/* Init presence for account */
1320
 
pj_status_t pjsua_pres_init_acc(int acc_id)
1321
 
{
1322
 
    pjsua_acc *acc = &pjsua_var.acc[acc_id];
1323
 
 
1324
 
    /* Init presence subscription */
1325
 
    pj_list_init(&acc->pres_srv_list);
1326
 
 
1327
 
    return PJ_SUCCESS;
1328
 
}
1329
 
 
1330
 
 
1331
 
/* Unpublish presence publication */
1332
 
void pjsua_pres_unpublish(pjsua_acc *acc, unsigned flags)
1333
 
{
1334
 
    if (acc->publish_sess) {
1335
 
        pjsua_acc_config *acc_cfg = &acc->cfg;
1336
 
 
1337
 
        acc->online_status = PJ_FALSE;
1338
 
 
1339
 
        if ((flags & PJSUA_DESTROY_NO_TX_MSG) == 0) {
1340
 
            send_publish(acc->index, PJ_FALSE);
1341
 
        }
1342
 
 
1343
 
        /* By ticket #364, don't destroy the session yet (let the callback
1344
 
           destroy it)
1345
 
        if (acc->publish_sess) {
1346
 
            pjsip_publishc_destroy(acc->publish_sess);
1347
 
            acc->publish_sess = NULL;
1348
 
        }
1349
 
        */
1350
 
        acc_cfg->publish_enabled = PJ_FALSE;
1351
 
    }
1352
 
}
1353
 
 
1354
 
/* Terminate server subscription for the account */
1355
 
void pjsua_pres_delete_acc(int acc_id, unsigned flags)
1356
 
{
1357
 
    pjsua_acc *acc = &pjsua_var.acc[acc_id];
1358
 
    pjsua_srv_pres *uapres;
1359
 
 
1360
 
    uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
1361
 
 
1362
 
    /* Notify all subscribers that we're no longer available */
1363
 
    while (uapres != &acc->pres_srv_list) {
1364
 
 
1365
 
        pjsip_pres_status pres_status;
1366
 
        pj_str_t reason = { "noresource", 10 };
1367
 
        pjsua_srv_pres *next;
1368
 
        pjsip_tx_data *tdata;
1369
 
 
1370
 
        next = uapres->next;
1371
 
 
1372
 
        pjsip_pres_get_status(uapres->sub, &pres_status);
1373
 
 
1374
 
        pres_status.info[0].basic_open = pjsua_var.acc[acc_id].online_status;
1375
 
        pjsip_pres_set_status(uapres->sub, &pres_status);
1376
 
 
1377
 
        if ((flags & PJSUA_DESTROY_NO_TX_MSG) == 0) {
1378
 
            if (pjsip_pres_notify(uapres->sub,
1379
 
                                  PJSIP_EVSUB_STATE_TERMINATED, NULL,
1380
 
                                  &reason, &tdata)==PJ_SUCCESS)
1381
 
            {
1382
 
                pjsip_pres_send_request(uapres->sub, tdata);
1383
 
            }
1384
 
        } else {
1385
 
            pjsip_pres_terminate(uapres->sub, PJ_FALSE);
1386
 
        }
1387
 
 
1388
 
        uapres = next;
1389
 
    }
1390
 
 
1391
 
    /* Clear server presence subscription list because account might be reused
1392
 
     * later. */
1393
 
    pj_list_init(&acc->pres_srv_list);
1394
 
 
1395
 
    /* Terminate presence publication, if any */
1396
 
    pjsua_pres_unpublish(acc, flags);
1397
 
}
1398
 
 
1399
 
 
1400
 
/* Update server subscription (e.g. when our online status has changed) */
1401
 
void pjsua_pres_update_acc(int acc_id, pj_bool_t force)
1402
 
{
1403
 
    pjsua_acc *acc = &pjsua_var.acc[acc_id];
1404
 
    pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1405
 
    pjsua_srv_pres *uapres;
1406
 
 
1407
 
    uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
1408
 
 
1409
 
    while (uapres != &acc->pres_srv_list) {
1410
 
 
1411
 
        pjsip_pres_status pres_status;
1412
 
        pjsip_tx_data *tdata;
1413
 
 
1414
 
        pjsip_pres_get_status(uapres->sub, &pres_status);
1415
 
 
1416
 
        /* Only send NOTIFY once subscription is active. Some subscriptions
1417
 
         * may still be in NULL (when app is adding a new buddy while in the
1418
 
         * on_incoming_subscribe() callback) or PENDING (when user approval is
1419
 
         * being requested) state and we don't send NOTIFY to these subs until
1420
 
         * the user accepted the request.
1421
 
         */
1422
 
        if (pjsip_evsub_get_state(uapres->sub)==PJSIP_EVSUB_STATE_ACTIVE &&
1423
 
            (force || pres_status.info[0].basic_open != acc->online_status))
1424
 
        {
1425
 
 
1426
 
            pres_status.info[0].basic_open = acc->online_status;
1427
 
            pj_memcpy(&pres_status.info[0].rpid, &acc->rpid,
1428
 
                      sizeof(pjrpid_element));
1429
 
 
1430
 
            pjsip_pres_set_status(uapres->sub, &pres_status);
1431
 
 
1432
 
            if (pjsip_pres_current_notify(uapres->sub, &tdata)==PJ_SUCCESS) {
1433
 
                pjsua_process_msg_data(tdata, NULL);
1434
 
                pjsip_pres_send_request(uapres->sub, tdata);
1435
 
            }
1436
 
        }
1437
 
 
1438
 
        uapres = uapres->next;
1439
 
    }
1440
 
 
1441
 
    /* Send PUBLISH if required. We only do this when we have a PUBLISH
1442
 
     * session. If we don't have a PUBLISH session, then it could be
1443
 
     * that we're waiting until registration has completed before we
1444
 
     * send the first PUBLISH.
1445
 
     */
1446
 
    if (acc_cfg->publish_enabled && acc->publish_sess) {
1447
 
        if (force || acc->publish_state != acc->online_status) {
1448
 
            send_publish(acc_id, PJ_TRUE);
1449
 
        }
1450
 
    }
1451
 
}
1452
 
 
1453
 
 
1454
 
 
1455
 
/***************************************************************************
1456
 
 * Client subscription.
1457
 
 */
1458
 
 
1459
 
static void buddy_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry)
1460
 
{
1461
 
    pjsua_buddy *buddy = (pjsua_buddy*)entry->user_data;
1462
 
 
1463
 
    PJ_UNUSED_ARG(th);
1464
 
 
1465
 
    entry->id = PJ_FALSE;
1466
 
    pjsua_buddy_update_pres(buddy->index);
1467
 
}
1468
 
 
1469
 
/* Reschedule subscription refresh timer or terminate the subscription
1470
 
 * refresh timer for the specified buddy.
1471
 
 */
1472
 
static void buddy_resubscribe(pjsua_buddy *buddy, pj_bool_t resched,
1473
 
                              unsigned msec_interval)
1474
 
{
1475
 
    if (buddy->timer.id) {
1476
 
        pjsua_cancel_timer(&buddy->timer);
1477
 
        buddy->timer.id = PJ_FALSE;
1478
 
    }
1479
 
 
1480
 
    if (resched) {
1481
 
        pj_time_val delay;
1482
 
 
1483
 
        PJ_LOG(4,(THIS_FILE,
1484
 
                  "Resubscribing buddy id %u in %u ms (reason: %.*s)",
1485
 
                  buddy->index, msec_interval,
1486
 
                  (int)buddy->term_reason.slen,
1487
 
                  buddy->term_reason.ptr));
1488
 
 
1489
 
        pj_timer_entry_init(&buddy->timer, 0, buddy, &buddy_timer_cb);
1490
 
        delay.sec = 0;
1491
 
        delay.msec = msec_interval;
1492
 
        pj_time_val_normalize(&delay);
1493
 
 
1494
 
        if (pjsua_schedule_timer(&buddy->timer, &delay)==PJ_SUCCESS)
1495
 
            buddy->timer.id = PJ_TRUE;
1496
 
    }
1497
 
}
1498
 
 
1499
 
/* Callback called when *client* subscription state has changed. */
1500
 
static void pjsua_evsub_on_state( pjsip_evsub *sub, pjsip_event *event)
1501
 
{
1502
 
    pjsua_buddy *buddy;
1503
 
 
1504
 
    PJ_UNUSED_ARG(event);
1505
 
 
1506
 
    /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
1507
 
     *   a dialog attached to it, lock_buddy() will use the dialog
1508
 
     *   lock, which we are currently holding!
1509
 
     */
1510
 
    buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
1511
 
    if (buddy) {
1512
 
        PJ_LOG(4,(THIS_FILE,
1513
 
                  "Presence subscription to %.*s is %s",
1514
 
                  (int)pjsua_var.buddy[buddy->index].uri.slen,
1515
 
                  pjsua_var.buddy[buddy->index].uri.ptr,
1516
 
                  pjsip_evsub_get_state_name(sub)));
1517
 
        pj_log_push_indent();
1518
 
 
1519
 
        if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
1520
 
            int resub_delay = -1;
1521
 
 
1522
 
            if (buddy->term_reason.ptr == NULL) {
1523
 
                buddy->term_reason.ptr = (char*)
1524
 
                                         pj_pool_alloc(buddy->pool,
1525
 
                                           PJSUA_BUDDY_SUB_TERM_REASON_LEN);
1526
 
            }
1527
 
            pj_strncpy(&buddy->term_reason,
1528
 
                       pjsip_evsub_get_termination_reason(sub),
1529
 
                       PJSUA_BUDDY_SUB_TERM_REASON_LEN);
1530
 
 
1531
 
            buddy->term_code = 200;
1532
 
 
1533
 
            /* Determine whether to resubscribe automatically */
1534
 
            if (event && event->type==PJSIP_EVENT_TSX_STATE) {
1535
 
                const pjsip_transaction *tsx = event->body.tsx_state.tsx;
1536
 
                if (pjsip_method_cmp(&tsx->method,
1537
 
                                     &pjsip_subscribe_method)==0)
1538
 
                {
1539
 
                    buddy->term_code = tsx->status_code;
1540
 
                    switch (tsx->status_code) {
1541
 
                    case PJSIP_SC_CALL_TSX_DOES_NOT_EXIST:
1542
 
                        /* 481: we refreshed too late? resubscribe
1543
 
                         * immediately.
1544
 
                         */
1545
 
                        /* But this must only happen when the 481 is received
1546
 
                         * on subscription refresh request. We MUST NOT try to
1547
 
                         * resubscribe automatically if the 481 is received
1548
 
                         * on the initial SUBSCRIBE (if server returns this
1549
 
                         * response for some reason).
1550
 
                         */
1551
 
                        if (buddy->dlg->remote.contact)
1552
 
                            resub_delay = 500;
1553
 
                        break;
1554
 
                    }
1555
 
                } else if (pjsip_method_cmp(&tsx->method,
1556
 
                                            &pjsip_notify_method)==0)
1557
 
                {
1558
 
                    if (pj_stricmp2(&buddy->term_reason, "deactivated")==0 ||
1559
 
                        pj_stricmp2(&buddy->term_reason, "timeout")==0) {
1560
 
                        /* deactivated: The subscription has been terminated,
1561
 
                         * but the subscriber SHOULD retry immediately with
1562
 
                         * a new subscription.
1563
 
                         */
1564
 
                        /* timeout: The subscription has been terminated
1565
 
                         * because it was not refreshed before it expired.
1566
 
                         * Clients MAY re-subscribe immediately. The
1567
 
                         * "retry-after" parameter has no semantics for
1568
 
                         * "timeout".
1569
 
                         */
1570
 
                        resub_delay = 500;
1571
 
                    }
1572
 
                    else if (pj_stricmp2(&buddy->term_reason, "probation")==0||
1573
 
                             pj_stricmp2(&buddy->term_reason, "giveup")==0) {
1574
 
                        /* probation: The subscription has been terminated,
1575
 
                         * but the client SHOULD retry at some later time.
1576
 
                         * If a "retry-after" parameter is also present, the
1577
 
                         * client SHOULD wait at least the number of seconds
1578
 
                         * specified by that parameter before attempting to re-
1579
 
                         * subscribe.
1580
 
                         */
1581
 
                        /* giveup: The subscription has been terminated because
1582
 
                         * the notifier could not obtain authorization in a
1583
 
                         * timely fashion.  If a "retry-after" parameter is
1584
 
                         * also present, the client SHOULD wait at least the
1585
 
                         * number of seconds specified by that parameter before
1586
 
                         * attempting to re-subscribe; otherwise, the client
1587
 
                         * MAY retry immediately, but will likely get put back
1588
 
                         * into pending state.
1589
 
                         */
1590
 
                        const pjsip_sub_state_hdr *sub_hdr;
1591
 
                        pj_str_t sub_state = { "Subscription-State", 18 };
1592
 
                        const pjsip_msg *msg;
1593
 
 
1594
 
                        msg = event->body.tsx_state.src.rdata->msg_info.msg;
1595
 
                        sub_hdr = (const pjsip_sub_state_hdr*)
1596
 
                                  pjsip_msg_find_hdr_by_name(msg, &sub_state,
1597
 
                                                             NULL);
1598
 
                        if (sub_hdr && sub_hdr->retry_after > 0)
1599
 
                            resub_delay = sub_hdr->retry_after * 1000;
1600
 
                    }
1601
 
 
1602
 
                }
1603
 
            }
1604
 
 
1605
 
            /* For other cases of subscription termination, if resubscribe
1606
 
             * timer is not set, schedule with default expiration (plus minus
1607
 
             * some random value, to avoid sending SUBSCRIBEs all at once)
1608
 
             */
1609
 
            if (resub_delay == -1) {
1610
 
                pj_assert(PJSUA_PRES_TIMER >= 3);
1611
 
                resub_delay = PJSUA_PRES_TIMER*1000 - 2500 + (pj_rand()%5000);
1612
 
            }
1613
 
 
1614
 
            buddy_resubscribe(buddy, PJ_TRUE, resub_delay);
1615
 
 
1616
 
        } else {
1617
 
            /* This will clear the last termination code/reason */
1618
 
            buddy->term_code = 0;
1619
 
            buddy->term_reason.slen = 0;
1620
 
        }
1621
 
 
1622
 
        /* Call callbacks */
1623
 
        if (pjsua_var.ua_cfg.cb.on_buddy_evsub_state)
1624
 
            (*pjsua_var.ua_cfg.cb.on_buddy_evsub_state)(buddy->index, sub,
1625
 
                                                        event);
1626
 
 
1627
 
        if (pjsua_var.ua_cfg.cb.on_buddy_state)
1628
 
            (*pjsua_var.ua_cfg.cb.on_buddy_state)(buddy->index);
1629
 
 
1630
 
        /* Clear subscription */
1631
 
        if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
1632
 
            buddy->sub = NULL;
1633
 
            buddy->status.info_cnt = 0;
1634
 
            buddy->dlg = NULL;
1635
 
            pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
1636
 
        }
1637
 
 
1638
 
        pj_log_pop_indent();
1639
 
    }
1640
 
}
1641
 
 
1642
 
 
1643
 
/* Callback when transaction state has changed. */
1644
 
static void pjsua_evsub_on_tsx_state(pjsip_evsub *sub,
1645
 
                                     pjsip_transaction *tsx,
1646
 
                                     pjsip_event *event)
1647
 
{
1648
 
    pjsua_buddy *buddy;
1649
 
    pjsip_contact_hdr *contact_hdr;
1650
 
 
1651
 
    /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
1652
 
     *   a dialog attached to it, lock_buddy() will use the dialog
1653
 
     *   lock, which we are currently holding!
1654
 
     */
1655
 
    buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
1656
 
    if (!buddy) {
1657
 
        return;
1658
 
    }
1659
 
 
1660
 
    /* We only use this to update buddy's Contact, when it's not
1661
 
     * set.
1662
 
     */
1663
 
    if (buddy->contact.slen != 0) {
1664
 
        /* Contact already set */
1665
 
        return;
1666
 
    }
1667
 
 
1668
 
    /* Only care about 2xx response to outgoing SUBSCRIBE */
1669
 
    if (tsx->status_code/100 != 2 ||
1670
 
        tsx->role != PJSIP_UAC_ROLE ||
1671
 
        event->type != PJSIP_EVENT_RX_MSG ||
1672
 
        pjsip_method_cmp(&tsx->method, pjsip_get_subscribe_method())!=0)
1673
 
    {
1674
 
        return;
1675
 
    }
1676
 
 
1677
 
    /* Find contact header. */
1678
 
    contact_hdr = (pjsip_contact_hdr*)
1679
 
                  pjsip_msg_find_hdr(event->body.rx_msg.rdata->msg_info.msg,
1680
 
                                     PJSIP_H_CONTACT, NULL);
1681
 
    if (!contact_hdr || !contact_hdr->uri) {
1682
 
        return;
1683
 
    }
1684
 
 
1685
 
    buddy->contact.ptr = (char*)
1686
 
                         pj_pool_alloc(buddy->pool, PJSIP_MAX_URL_SIZE);
1687
 
    buddy->contact.slen = pjsip_uri_print( PJSIP_URI_IN_CONTACT_HDR,
1688
 
                                           contact_hdr->uri,
1689
 
                                           buddy->contact.ptr,
1690
 
                                           PJSIP_MAX_URL_SIZE);
1691
 
    if (buddy->contact.slen < 0)
1692
 
        buddy->contact.slen = 0;
1693
 
}
1694
 
 
1695
 
 
1696
 
/* Callback called when we receive NOTIFY */
1697
 
static void pjsua_evsub_on_rx_notify(pjsip_evsub *sub,
1698
 
                                     pjsip_rx_data *rdata,
1699
 
                                     int *p_st_code,
1700
 
                                     pj_str_t **p_st_text,
1701
 
                                     pjsip_hdr *res_hdr,
1702
 
                                     pjsip_msg_body **p_body)
1703
 
{
1704
 
    pjsua_buddy *buddy;
1705
 
 
1706
 
    /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
1707
 
     *   a dialog attached to it, lock_buddy() will use the dialog
1708
 
     *   lock, which we are currently holding!
1709
 
     */
1710
 
    buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
1711
 
    if (buddy) {
1712
 
        /* Update our info. */
1713
 
        pjsip_pres_get_status(sub, &buddy->status);
1714
 
    }
1715
 
 
1716
 
    /* The default is to send 200 response to NOTIFY.
1717
 
     * Just leave it there..
1718
 
     */
1719
 
    PJ_UNUSED_ARG(rdata);
1720
 
    PJ_UNUSED_ARG(p_st_code);
1721
 
    PJ_UNUSED_ARG(p_st_text);
1722
 
    PJ_UNUSED_ARG(res_hdr);
1723
 
    PJ_UNUSED_ARG(p_body);
1724
 
}
1725
 
 
1726
 
 
1727
 
/* It does what it says.. */
1728
 
static void subscribe_buddy_presence(pjsua_buddy_id buddy_id)
1729
 
{
1730
 
    pjsip_evsub_user pres_callback;
1731
 
    pj_pool_t *tmp_pool = NULL;
1732
 
    pjsua_buddy *buddy;
1733
 
    int acc_id;
1734
 
    pjsua_acc *acc;
1735
 
    pj_str_t contact;
1736
 
    pjsip_tx_data *tdata;
1737
 
    pj_status_t status;
1738
 
 
1739
 
    /* Event subscription callback. */
1740
 
    pj_bzero(&pres_callback, sizeof(pres_callback));
1741
 
    pres_callback.on_evsub_state = &pjsua_evsub_on_state;
1742
 
    pres_callback.on_tsx_state = &pjsua_evsub_on_tsx_state;
1743
 
    pres_callback.on_rx_notify = &pjsua_evsub_on_rx_notify;
1744
 
 
1745
 
    buddy = &pjsua_var.buddy[buddy_id];
1746
 
    acc_id = pjsua_acc_find_for_outgoing(&buddy->uri);
1747
 
 
1748
 
    acc = &pjsua_var.acc[acc_id];
1749
 
 
1750
 
    PJ_LOG(4,(THIS_FILE, "Buddy %d: subscribing presence,using account %d..",
1751
 
              buddy_id, acc_id));
1752
 
    pj_log_push_indent();
1753
 
 
1754
 
    /* Generate suitable Contact header unless one is already set in
1755
 
     * the account
1756
 
     */
1757
 
    if (acc->contact.slen) {
1758
 
        contact = acc->contact;
1759
 
    } else {
1760
 
        tmp_pool = pjsua_pool_create("tmpbuddy", 512, 256);
1761
 
 
1762
 
        status = pjsua_acc_create_uac_contact(tmp_pool, &contact,
1763
 
                                              acc_id, &buddy->uri);
1764
 
        if (status != PJ_SUCCESS) {
1765
 
            pjsua_perror(THIS_FILE, "Unable to generate Contact header",
1766
 
                         status);
1767
 
            pj_pool_release(tmp_pool);
1768
 
            pj_log_pop_indent();
1769
 
            return;
1770
 
        }
1771
 
    }
1772
 
 
1773
 
    /* Create UAC dialog */
1774
 
    status = pjsip_dlg_create_uac( pjsip_ua_instance(),
1775
 
                                   &acc->cfg.id,
1776
 
                                   &contact,
1777
 
                                   &buddy->uri,
1778
 
                                   NULL, &buddy->dlg);
1779
 
    if (status != PJ_SUCCESS) {
1780
 
        pjsua_perror(THIS_FILE, "Unable to create dialog",
1781
 
                     status);
1782
 
        if (tmp_pool) pj_pool_release(tmp_pool);
1783
 
        pj_log_pop_indent();
1784
 
        return;
1785
 
    }
1786
 
 
1787
 
    /* Increment the dialog's lock otherwise when presence session creation
1788
 
     * fails the dialog will be destroyed prematurely.
1789
 
     */
1790
 
    pjsip_dlg_inc_lock(buddy->dlg);
1791
 
 
1792
 
    if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0)
1793
 
        pjsip_dlg_set_via_sent_by(buddy->dlg, &acc->via_addr, acc->via_tp);
1794
 
 
1795
 
    status = pjsip_pres_create_uac( buddy->dlg, &pres_callback,
1796
 
                                    PJSIP_EVSUB_NO_EVENT_ID, &buddy->sub);
1797
 
    if (status != PJ_SUCCESS) {
1798
 
        buddy->sub = NULL;
1799
 
        pjsua_perror(THIS_FILE, "Unable to create presence client",
1800
 
                     status);
1801
 
        /* This should destroy the dialog since there's no session
1802
 
         * referencing it
1803
 
         */
1804
 
        if (buddy->dlg) pjsip_dlg_dec_lock(buddy->dlg);
1805
 
        if (tmp_pool) pj_pool_release(tmp_pool);
1806
 
        pj_log_pop_indent();
1807
 
        return;
1808
 
    }
1809
 
 
1810
 
    /* If account is locked to specific transport, then lock dialog
1811
 
     * to this transport too.
1812
 
     */
1813
 
    if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1814
 
        pjsip_tpselector tp_sel;
1815
 
 
1816
 
        pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
1817
 
        pjsip_dlg_set_transport(buddy->dlg, &tp_sel);
1818
 
    }
1819
 
 
1820
 
    /* Set route-set */
1821
 
    if (!pj_list_empty(&acc->route_set)) {
1822
 
        pjsip_dlg_set_route_set(buddy->dlg, &acc->route_set);
1823
 
    }
1824
 
 
1825
 
    /* Set credentials */
1826
 
    if (acc->cred_cnt) {
1827
 
        pjsip_auth_clt_set_credentials( &buddy->dlg->auth_sess,
1828
 
                                        acc->cred_cnt, acc->cred);
1829
 
    }
1830
 
 
1831
 
    /* Set authentication preference */
1832
 
    pjsip_auth_clt_set_prefs(&buddy->dlg->auth_sess, &acc->cfg.auth_pref);
1833
 
 
1834
 
    pjsip_evsub_set_mod_data(buddy->sub, pjsua_var.mod.id, buddy);
1835
 
 
1836
 
    status = pjsip_pres_initiate(buddy->sub, -1, &tdata);
1837
 
    if (status != PJ_SUCCESS) {
1838
 
        if (buddy->dlg) pjsip_dlg_dec_lock(buddy->dlg);
1839
 
        if (buddy->sub) {
1840
 
            pjsip_pres_terminate(buddy->sub, PJ_FALSE);
1841
 
        }
1842
 
        buddy->sub = NULL;
1843
 
        pjsua_perror(THIS_FILE, "Unable to create initial SUBSCRIBE",
1844
 
                     status);
1845
 
        if (tmp_pool) pj_pool_release(tmp_pool);
1846
 
        pj_log_pop_indent();
1847
 
        return;
1848
 
    }
1849
 
 
1850
 
    pjsua_process_msg_data(tdata, NULL);
1851
 
 
1852
 
    status = pjsip_pres_send_request(buddy->sub, tdata);
1853
 
    if (status != PJ_SUCCESS) {
1854
 
        if (buddy->dlg) pjsip_dlg_dec_lock(buddy->dlg);
1855
 
        if (buddy->sub) {
1856
 
            pjsip_pres_terminate(buddy->sub, PJ_FALSE);
1857
 
        }
1858
 
        buddy->sub = NULL;
1859
 
        pjsua_perror(THIS_FILE, "Unable to send initial SUBSCRIBE",
1860
 
                     status);
1861
 
        if (tmp_pool) pj_pool_release(tmp_pool);
1862
 
        pj_log_pop_indent();
1863
 
        return;
1864
 
    }
1865
 
 
1866
 
    pjsip_dlg_dec_lock(buddy->dlg);
1867
 
    if (tmp_pool) pj_pool_release(tmp_pool);
1868
 
    pj_log_pop_indent();
1869
 
}
1870
 
 
1871
 
 
1872
 
/* It does what it says... */
1873
 
static void unsubscribe_buddy_presence(pjsua_buddy_id buddy_id)
1874
 
{
1875
 
    pjsua_buddy *buddy;
1876
 
    pjsip_tx_data *tdata;
1877
 
    pj_status_t status;
1878
 
 
1879
 
    buddy = &pjsua_var.buddy[buddy_id];
1880
 
 
1881
 
    if (buddy->sub == NULL)
1882
 
        return;
1883
 
 
1884
 
    if (pjsip_evsub_get_state(buddy->sub) == PJSIP_EVSUB_STATE_TERMINATED) {
1885
 
        buddy->sub = NULL;
1886
 
        return;
1887
 
    }
1888
 
 
1889
 
    PJ_LOG(5,(THIS_FILE, "Buddy %d: unsubscribing..", buddy_id));
1890
 
    pj_log_push_indent();
1891
 
 
1892
 
    status = pjsip_pres_initiate( buddy->sub, 0, &tdata);
1893
 
    if (status == PJ_SUCCESS) {
1894
 
        pjsua_process_msg_data(tdata, NULL);
1895
 
        status = pjsip_pres_send_request( buddy->sub, tdata );
1896
 
    }
1897
 
 
1898
 
    if (status != PJ_SUCCESS && buddy->sub) {
1899
 
        pjsip_pres_terminate(buddy->sub, PJ_FALSE);
1900
 
        buddy->sub = NULL;
1901
 
        pjsua_perror(THIS_FILE, "Unable to unsubscribe presence",
1902
 
                     status);
1903
 
    }
1904
 
 
1905
 
    pj_log_pop_indent();
1906
 
}
1907
 
 
1908
 
/* It does what it says.. */
1909
 
static pj_status_t refresh_client_subscriptions(void)
1910
 
{
1911
 
    unsigned i;
1912
 
    pj_status_t status;
1913
 
 
1914
 
    for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
1915
 
        struct buddy_lock lck;
1916
 
 
1917
 
        if (!pjsua_buddy_is_valid(i))
1918
 
            continue;
1919
 
 
1920
 
        status = lock_buddy("refresh_client_subscriptions()", i, &lck, 0);
1921
 
        if (status != PJ_SUCCESS)
1922
 
            return status;
1923
 
 
1924
 
        if (pjsua_var.buddy[i].monitor && !pjsua_var.buddy[i].sub) {
1925
 
            subscribe_buddy_presence(i);
1926
 
 
1927
 
        } else if (!pjsua_var.buddy[i].monitor && pjsua_var.buddy[i].sub) {
1928
 
            unsubscribe_buddy_presence(i);
1929
 
 
1930
 
        }
1931
 
 
1932
 
        unlock_buddy(&lck);
1933
 
    }
1934
 
 
1935
 
    return PJ_SUCCESS;
1936
 
}
1937
 
 
1938
 
/***************************************************************************
1939
 
 * MWI
1940
 
 */
1941
 
/* Callback called when *client* subscription state has changed. */
1942
 
static void mwi_evsub_on_state( pjsip_evsub *sub, pjsip_event *event)
1943
 
{
1944
 
    pjsua_acc *acc;
1945
 
 
1946
 
    PJ_UNUSED_ARG(event);
1947
 
 
1948
 
    /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
1949
 
     *   a dialog attached to it, lock_buddy() will use the dialog
1950
 
     *   lock, which we are currently holding!
1951
 
     */
1952
 
    acc = (pjsua_acc*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
1953
 
    if (!acc)
1954
 
        return;
1955
 
 
1956
 
    PJ_LOG(4,(THIS_FILE,
1957
 
              "MWI subscription for %.*s is %s",
1958
 
              (int)acc->cfg.id.slen, acc->cfg.id.ptr,
1959
 
              pjsip_evsub_get_state_name(sub)));
1960
 
 
1961
 
    /* Call callback */
1962
 
    if (pjsua_var.ua_cfg.cb.on_mwi_state) {
1963
 
        (*pjsua_var.ua_cfg.cb.on_mwi_state)(acc->index, sub);
1964
 
    }
1965
 
 
1966
 
    if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
1967
 
        /* Clear subscription */
1968
 
        acc->mwi_dlg = NULL;
1969
 
        acc->mwi_sub = NULL;
1970
 
        pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
1971
 
 
1972
 
    }
1973
 
}
1974
 
 
1975
 
/* Callback called when we receive NOTIFY */
1976
 
static void mwi_evsub_on_rx_notify(pjsip_evsub *sub,
1977
 
                                   pjsip_rx_data *rdata,
1978
 
                                   int *p_st_code,
1979
 
                                   pj_str_t **p_st_text,
1980
 
                                   pjsip_hdr *res_hdr,
1981
 
                                   pjsip_msg_body **p_body)
1982
 
{
1983
 
    pjsua_mwi_info mwi_info;
1984
 
    pjsua_acc *acc;
1985
 
 
1986
 
    PJ_UNUSED_ARG(p_st_code);
1987
 
    PJ_UNUSED_ARG(p_st_text);
1988
 
    PJ_UNUSED_ARG(res_hdr);
1989
 
    PJ_UNUSED_ARG(p_body);
1990
 
 
1991
 
    acc = (pjsua_acc*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
1992
 
    if (!acc)
1993
 
        return;
1994
 
 
1995
 
    /* Construct mwi_info */
1996
 
    pj_bzero(&mwi_info, sizeof(mwi_info));
1997
 
    mwi_info.evsub = sub;
1998
 
    mwi_info.rdata = rdata;
1999
 
 
2000
 
    PJ_LOG(4,(THIS_FILE, "MWI got NOTIFY.."));
2001
 
    pj_log_push_indent();
2002
 
 
2003
 
    /* Call callback */
2004
 
    if (pjsua_var.ua_cfg.cb.on_mwi_info) {
2005
 
        (*pjsua_var.ua_cfg.cb.on_mwi_info)(acc->index, &mwi_info);
2006
 
    }
2007
 
 
2008
 
    pj_log_pop_indent();
2009
 
}
2010
 
 
2011
 
 
2012
 
/* Event subscription callback. */
2013
 
static pjsip_evsub_user mwi_cb =
2014
 
{
2015
 
    &mwi_evsub_on_state,
2016
 
    NULL,   /* on_tsx_state: not interested */
2017
 
    NULL,   /* on_rx_refresh: don't care about SUBSCRIBE refresh, unless
2018
 
             * we want to authenticate
2019
 
             */
2020
 
 
2021
 
    &mwi_evsub_on_rx_notify,
2022
 
 
2023
 
    NULL,   /* on_client_refresh: Use default behaviour, which is to
2024
 
             * refresh client subscription. */
2025
 
 
2026
 
    NULL,   /* on_server_timeout: Use default behaviour, which is to send
2027
 
             * NOTIFY to terminate.
2028
 
             */
2029
 
};
2030
 
 
2031
 
pj_status_t pjsua_start_mwi(pjsua_acc_id acc_id, pj_bool_t force_renew)
2032
 
{
2033
 
    pjsua_acc *acc;
2034
 
    pj_pool_t *tmp_pool = NULL;
2035
 
    pj_str_t contact;
2036
 
    pjsip_tx_data *tdata;
2037
 
    pj_status_t status = PJ_SUCCESS;
2038
 
 
2039
 
    PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc)
2040
 
                     && pjsua_var.acc[acc_id].valid, PJ_EINVAL);
2041
 
 
2042
 
    acc = &pjsua_var.acc[acc_id];
2043
 
 
2044
 
    if (!acc->cfg.mwi_enabled) {
2045
 
        if (acc->mwi_sub) {
2046
 
            /* Terminate MWI subscription */
2047
 
            pjsip_evsub *sub = acc->mwi_sub;
2048
 
 
2049
 
            /* Detach sub from this account */
2050
 
            acc->mwi_sub = NULL;
2051
 
            acc->mwi_dlg = NULL;
2052
 
            pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
2053
 
 
2054
 
            /* Unsubscribe */
2055
 
            status = pjsip_mwi_initiate(sub, 0, &tdata);
2056
 
            if (status == PJ_SUCCESS) {
2057
 
                status = pjsip_mwi_send_request(sub, tdata);
2058
 
            }
2059
 
        }
2060
 
        return status;
2061
 
    }
2062
 
 
2063
 
    /* Subscription is already active */
2064
 
    if (acc->mwi_sub) {
2065
 
        if (!force_renew)
2066
 
            return PJ_SUCCESS;
2067
 
 
2068
 
        /* Update MWI subscription */
2069
 
        pj_assert(acc->mwi_dlg);
2070
 
        pjsip_dlg_inc_lock(acc->mwi_dlg);
2071
 
 
2072
 
        status = pjsip_mwi_initiate(acc->mwi_sub, acc->cfg.mwi_expires, &tdata);
2073
 
        if (status == PJ_SUCCESS) {
2074
 
            pjsua_process_msg_data(tdata, NULL);
2075
 
            status = pjsip_pres_send_request(acc->mwi_sub, tdata);
2076
 
        }
2077
 
 
2078
 
        pjsip_dlg_dec_lock(acc->mwi_dlg);
2079
 
        return status;
2080
 
    }
2081
 
 
2082
 
    PJ_LOG(4,(THIS_FILE, "Starting MWI subscription.."));
2083
 
    pj_log_push_indent();
2084
 
 
2085
 
    /* Generate suitable Contact header unless one is already set in
2086
 
     * the account
2087
 
     */
2088
 
    if (acc->contact.slen) {
2089
 
        contact = acc->contact;
2090
 
    } else {
2091
 
        tmp_pool = pjsua_pool_create("tmpmwi", 512, 256);
2092
 
        status = pjsua_acc_create_uac_contact(tmp_pool, &contact,
2093
 
                                              acc->index, &acc->cfg.id);
2094
 
        if (status != PJ_SUCCESS) {
2095
 
            pjsua_perror(THIS_FILE, "Unable to generate Contact header",
2096
 
                         status);
2097
 
            goto on_return;
2098
 
        }
2099
 
    }
2100
 
 
2101
 
    /* Create UAC dialog */
2102
 
    status = pjsip_dlg_create_uac( pjsip_ua_instance(),
2103
 
                                   &acc->cfg.id,
2104
 
                                   &contact,
2105
 
                                   &acc->cfg.id,
2106
 
                                   NULL, &acc->mwi_dlg);
2107
 
    if (status != PJ_SUCCESS) {
2108
 
        pjsua_perror(THIS_FILE, "Unable to create dialog", status);
2109
 
        goto on_return;
2110
 
    }
2111
 
 
2112
 
    /* Increment the dialog's lock otherwise when presence session creation
2113
 
     * fails the dialog will be destroyed prematurely.
2114
 
     */
2115
 
    pjsip_dlg_inc_lock(acc->mwi_dlg);
2116
 
 
2117
 
    if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0)
2118
 
        pjsip_dlg_set_via_sent_by(acc->mwi_dlg, &acc->via_addr, acc->via_tp);
2119
 
 
2120
 
    /* Create UAC subscription */
2121
 
    status = pjsip_mwi_create_uac(acc->mwi_dlg, &mwi_cb,
2122
 
                                  PJSIP_EVSUB_NO_EVENT_ID, &acc->mwi_sub);
2123
 
    if (status != PJ_SUCCESS) {
2124
 
        pjsua_perror(THIS_FILE, "Error creating MWI subscription", status);
2125
 
        if (acc->mwi_dlg) pjsip_dlg_dec_lock(acc->mwi_dlg);
2126
 
        goto on_return;
2127
 
    }
2128
 
 
2129
 
    /* If account is locked to specific transport, then lock dialog
2130
 
     * to this transport too.
2131
 
     */
2132
 
    if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
2133
 
        pjsip_tpselector tp_sel;
2134
 
 
2135
 
        pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
2136
 
        pjsip_dlg_set_transport(acc->mwi_dlg, &tp_sel);
2137
 
    }
2138
 
 
2139
 
    /* Set route-set */
2140
 
    if (!pj_list_empty(&acc->route_set)) {
2141
 
        pjsip_dlg_set_route_set(acc->mwi_dlg, &acc->route_set);
2142
 
    }
2143
 
 
2144
 
    /* Set credentials */
2145
 
    if (acc->cred_cnt) {
2146
 
        pjsip_auth_clt_set_credentials( &acc->mwi_dlg->auth_sess,
2147
 
                                        acc->cred_cnt, acc->cred);
2148
 
    }
2149
 
 
2150
 
    /* Set authentication preference */
2151
 
    pjsip_auth_clt_set_prefs(&acc->mwi_dlg->auth_sess, &acc->cfg.auth_pref);
2152
 
 
2153
 
    pjsip_evsub_set_mod_data(acc->mwi_sub, pjsua_var.mod.id, acc);
2154
 
 
2155
 
    status = pjsip_mwi_initiate(acc->mwi_sub, acc->cfg.mwi_expires, &tdata);
2156
 
    if (status != PJ_SUCCESS) {
2157
 
        if (acc->mwi_dlg) pjsip_dlg_dec_lock(acc->mwi_dlg);
2158
 
        if (acc->mwi_sub) {
2159
 
            pjsip_pres_terminate(acc->mwi_sub, PJ_FALSE);
2160
 
        }
2161
 
        acc->mwi_sub = NULL;
2162
 
        acc->mwi_dlg = NULL;
2163
 
        pjsua_perror(THIS_FILE, "Unable to create initial MWI SUBSCRIBE",
2164
 
                     status);
2165
 
        goto on_return;
2166
 
    }
2167
 
 
2168
 
    pjsua_process_msg_data(tdata, NULL);
2169
 
 
2170
 
    status = pjsip_pres_send_request(acc->mwi_sub, tdata);
2171
 
    if (status != PJ_SUCCESS) {
2172
 
        if (acc->mwi_dlg) pjsip_dlg_dec_lock(acc->mwi_dlg);
2173
 
        if (acc->mwi_sub) {
2174
 
            pjsip_pres_terminate(acc->mwi_sub, PJ_FALSE);
2175
 
        }
2176
 
        acc->mwi_sub = NULL;
2177
 
        acc->mwi_dlg = NULL;
2178
 
        pjsua_perror(THIS_FILE, "Unable to send initial MWI SUBSCRIBE",
2179
 
                     status);
2180
 
        goto on_return;
2181
 
    }
2182
 
 
2183
 
    pjsip_dlg_dec_lock(acc->mwi_dlg);
2184
 
 
2185
 
on_return:
2186
 
    if (tmp_pool) pj_pool_release(tmp_pool);
2187
 
 
2188
 
    pj_log_pop_indent();
2189
 
    return status;
2190
 
}
2191
 
 
2192
 
 
2193
 
/***************************************************************************
2194
 
 * Unsolicited MWI
2195
 
 */
2196
 
static pj_bool_t unsolicited_mwi_on_rx_request(pjsip_rx_data *rdata)
2197
 
{
2198
 
    pjsip_msg *msg = rdata->msg_info.msg;
2199
 
    pj_str_t EVENT_HDR  = { "Event", 5 };
2200
 
    pj_str_t MWI = { "message-summary", 15 };
2201
 
    pjsip_event_hdr *eh;
2202
 
 
2203
 
    if (pjsip_method_cmp(&msg->line.req.method, &pjsip_notify_method)!=0) {
2204
 
        /* Only interested with NOTIFY request */
2205
 
        return PJ_FALSE;
2206
 
    }
2207
 
 
2208
 
    eh = (pjsip_event_hdr*) pjsip_msg_find_hdr_by_name(msg, &EVENT_HDR, NULL);
2209
 
    if (!eh) {
2210
 
        /* Something wrong with the request, it has no Event hdr */
2211
 
        return PJ_FALSE;
2212
 
    }
2213
 
 
2214
 
    if (pj_stricmp(&eh->event_type, &MWI) != 0) {
2215
 
        /* Not MWI event */
2216
 
        return PJ_FALSE;
2217
 
    }
2218
 
 
2219
 
    PJ_LOG(4,(THIS_FILE, "Got unsolicited NOTIFY from %s:%d..",
2220
 
              rdata->pkt_info.src_name, rdata->pkt_info.src_port));
2221
 
    pj_log_push_indent();
2222
 
 
2223
 
    /* Got unsolicited MWI request, respond with 200/OK first */
2224
 
    pjsip_endpt_respond(pjsua_get_pjsip_endpt(), NULL, rdata, 200, NULL,
2225
 
                        NULL, NULL, NULL);
2226
 
 
2227
 
 
2228
 
    /* Call callback */
2229
 
    if (pjsua_var.ua_cfg.cb.on_mwi_info) {
2230
 
        pjsua_acc_id acc_id;
2231
 
        pjsua_mwi_info mwi_info;
2232
 
 
2233
 
        acc_id = pjsua_acc_find_for_incoming(rdata);
2234
 
 
2235
 
        pj_bzero(&mwi_info, sizeof(mwi_info));
2236
 
        mwi_info.rdata = rdata;
2237
 
 
2238
 
        (*pjsua_var.ua_cfg.cb.on_mwi_info)(acc_id, &mwi_info);
2239
 
    }
2240
 
 
2241
 
    pj_log_pop_indent();
2242
 
    return PJ_TRUE;
2243
 
}
2244
 
 
2245
 
/* The module instance. */
2246
 
static pjsip_module pjsua_unsolicited_mwi_mod =
2247
 
{
2248
 
    NULL, NULL,                         /* prev, next.          */
2249
 
    { "mod-unsolicited-mwi", 19 },      /* Name.                */
2250
 
    -1,                                 /* Id                   */
2251
 
    PJSIP_MOD_PRIORITY_APPLICATION,     /* Priority             */
2252
 
    NULL,                               /* load()               */
2253
 
    NULL,                               /* start()              */
2254
 
    NULL,                               /* stop()               */
2255
 
    NULL,                               /* unload()             */
2256
 
    &unsolicited_mwi_on_rx_request,     /* on_rx_request()      */
2257
 
    NULL,                               /* on_rx_response()     */
2258
 
    NULL,                               /* on_tx_request.       */
2259
 
    NULL,                               /* on_tx_response()     */
2260
 
    NULL,                               /* on_tsx_state()       */
2261
 
};
2262
 
 
2263
 
static pj_status_t enable_unsolicited_mwi(void)
2264
 
{
2265
 
    pj_status_t status;
2266
 
 
2267
 
    status = pjsip_endpt_register_module(pjsua_get_pjsip_endpt(),
2268
 
                                         &pjsua_unsolicited_mwi_mod);
2269
 
    if (status != PJ_SUCCESS)
2270
 
        pjsua_perror(THIS_FILE, "Error registering unsolicited MWI module",
2271
 
                     status);
2272
 
 
2273
 
    return status;
2274
 
}
2275
 
 
2276
 
 
2277
 
 
2278
 
/***************************************************************************/
2279
 
 
2280
 
/* Timer callback to re-create client subscription */
2281
 
static void pres_timer_cb(pj_timer_heap_t *th,
2282
 
                          pj_timer_entry *entry)
2283
 
{
2284
 
    unsigned i;
2285
 
    pj_time_val delay = { PJSUA_PRES_TIMER, 0 };
2286
 
 
2287
 
    entry->id = PJ_FALSE;
2288
 
 
2289
 
    /* Retry failed PUBLISH and MWI SUBSCRIBE requests */
2290
 
    for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2291
 
        pjsua_acc *acc = &pjsua_var.acc[i];
2292
 
 
2293
 
        /* Acc may not be ready yet, otherwise assertion will happen */
2294
 
        if (!pjsua_acc_is_valid(i))
2295
 
            continue;
2296
 
 
2297
 
        /* Retry PUBLISH */
2298
 
        if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
2299
 
            pjsua_pres_init_publish_acc(acc->index);
2300
 
 
2301
 
        /* Re-subscribe MWI subscription if it's terminated prematurely */
2302
 
        if (acc->cfg.mwi_enabled && !acc->mwi_sub)
2303
 
            pjsua_start_mwi(acc->index, PJ_FALSE);
2304
 
    }
2305
 
 
2306
 
    /* #937: No need to do bulk client refresh, as buddies have their
2307
 
     *       own individual timer now.
2308
 
     */
2309
 
    //refresh_client_subscriptions();
2310
 
 
2311
 
    pjsip_endpt_schedule_timer(pjsua_var.endpt, entry, &delay);
2312
 
    entry->id = PJ_TRUE;
2313
 
 
2314
 
    PJ_UNUSED_ARG(th);
2315
 
}
2316
 
 
2317
 
 
2318
 
/*
2319
 
 * Init presence
2320
 
 */
2321
 
pj_status_t pjsua_pres_init()
2322
 
{
2323
 
    unsigned i;
2324
 
    pj_status_t status;
2325
 
 
2326
 
    status = pjsip_endpt_register_module( pjsua_var.endpt, &mod_pjsua_pres);
2327
 
    if (status != PJ_SUCCESS) {
2328
 
        pjsua_perror(THIS_FILE, "Unable to register pjsua presence module",
2329
 
                     status);
2330
 
    }
2331
 
 
2332
 
    for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
2333
 
        reset_buddy(i);
2334
 
    }
2335
 
 
2336
 
    return status;
2337
 
}
2338
 
 
2339
 
 
2340
 
/*
2341
 
 * Start presence subsystem.
2342
 
 */
2343
 
pj_status_t pjsua_pres_start(void)
2344
 
{
2345
 
    /* Start presence timer to re-subscribe to buddy's presence when
2346
 
     * subscription has failed.
2347
 
     */
2348
 
    if (pjsua_var.pres_timer.id == PJ_FALSE) {
2349
 
        pj_time_val pres_interval = {PJSUA_PRES_TIMER, 0};
2350
 
 
2351
 
        pjsua_var.pres_timer.cb = &pres_timer_cb;
2352
 
        pjsip_endpt_schedule_timer(pjsua_var.endpt, &pjsua_var.pres_timer,
2353
 
                                   &pres_interval);
2354
 
        pjsua_var.pres_timer.id = PJ_TRUE;
2355
 
    }
2356
 
 
2357
 
    if (pjsua_var.ua_cfg.enable_unsolicited_mwi) {
2358
 
        pj_status_t status = enable_unsolicited_mwi();
2359
 
        if (status != PJ_SUCCESS)
2360
 
            return status;
2361
 
    }
2362
 
 
2363
 
    return PJ_SUCCESS;
2364
 
}
2365
 
 
2366
 
 
2367
 
/*
2368
 
 * Shutdown presence.
2369
 
 */
2370
 
void pjsua_pres_shutdown(unsigned flags)
2371
 
{
2372
 
    unsigned i;
2373
 
 
2374
 
    PJ_LOG(4,(THIS_FILE, "Shutting down presence.."));
2375
 
    pj_log_push_indent();
2376
 
 
2377
 
    if (pjsua_var.pres_timer.id != 0) {
2378
 
        pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.pres_timer);
2379
 
        pjsua_var.pres_timer.id = PJ_FALSE;
2380
 
    }
2381
 
 
2382
 
    for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2383
 
        if (!pjsua_var.acc[i].valid)
2384
 
            continue;
2385
 
        pjsua_pres_delete_acc(i, flags);
2386
 
    }
2387
 
 
2388
 
    for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
2389
 
        pjsua_var.buddy[i].monitor = 0;
2390
 
    }
2391
 
 
2392
 
    if ((flags & PJSUA_DESTROY_NO_TX_MSG) == 0) {
2393
 
        refresh_client_subscriptions();
2394
 
 
2395
 
        for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2396
 
            if (pjsua_var.acc[i].valid)
2397
 
                pjsua_pres_update_acc(i, PJ_FALSE);
2398
 
        }
2399
 
    }
2400
 
 
2401
 
    pj_log_pop_indent();
2402
 
}