blob: ae451b19bdfed21dddb2a5bf70df1481befb76d0 [file] [log] [blame]
Benny Prijono834aee32006-02-19 01:38:06 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono834aee32006-02-19 01:38:06 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Benny Prijono4f9f64e2006-02-27 00:00:30 +000019#include <pjsua-lib/pjsua.h>
Benny Prijonoeebe9af2006-06-13 22:57:13 +000020#include <pjsua-lib/pjsua_internal.h>
Benny Prijono834aee32006-02-19 01:38:06 +000021
Benny Prijono834aee32006-02-19 01:38:06 +000022
23#define THIS_FILE "pjsua_pres.c"
24
25
Benny Prijonoeebe9af2006-06-13 22:57:13 +000026/*
27 * Get total number of buddies.
28 */
29PJ_DEF(unsigned) pjsua_get_buddy_count(void)
30{
31 return pjsua_var.buddy_cnt;
32}
Benny Prijono834aee32006-02-19 01:38:06 +000033
Benny Prijonoeebe9af2006-06-13 22:57:13 +000034
35/*
36 * Check if buddy ID is valid.
37 */
38PJ_DEF(pj_bool_t) pjsua_buddy_is_valid(pjsua_buddy_id buddy_id)
39{
40 return buddy_id>=0 && buddy_id<PJ_ARRAY_SIZE(pjsua_var.buddy) &&
41 pjsua_var.buddy[buddy_id].uri.slen != 0;
42}
43
44
45/*
46 * Enum buddy IDs.
47 */
48PJ_DEF(pj_status_t) pjsua_enum_buddies( pjsua_buddy_id ids[],
49 unsigned *count)
50{
51 unsigned i, c;
52
53 PJ_ASSERT_RETURN(ids && count, PJ_EINVAL);
54
55 PJSUA_LOCK();
56
57 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
58 if (!pjsua_var.buddy[i].uri.slen)
59 continue;
60 ids[c] = i;
61 ++c;
62 }
63
64 *count = c;
65
66 PJSUA_UNLOCK();
67
68 return PJ_SUCCESS;
69
70}
71
72
73/*
74 * Get detailed buddy info.
75 */
76PJ_DEF(pj_status_t) pjsua_buddy_get_info( pjsua_buddy_id buddy_id,
77 pjsua_buddy_info *info)
78{
79 int total=0;
80 pjsua_buddy *buddy;
81
82 PJ_ASSERT_RETURN(buddy_id>=0 &&
83 buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy),
84 PJ_EINVAL);
85
86 PJSUA_LOCK();
87
Benny Prijonoac623b32006-07-03 15:19:31 +000088 pj_bzero(info, sizeof(pjsua_buddy_info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +000089
90 buddy = &pjsua_var.buddy[buddy_id];
91 info->id = buddy->index;
92 if (pjsua_var.buddy[buddy_id].uri.slen == 0) {
93 PJSUA_UNLOCK();
94 return PJ_SUCCESS;
95 }
96
97 /* uri */
98 info->uri.ptr = info->buf_ + total;
99 pj_strncpy(&info->uri, &buddy->uri, sizeof(info->buf_)-total);
100 total += info->uri.slen;
101
102 /* contact */
103 info->contact.ptr = info->buf_ + total;
104 pj_strncpy(&info->contact, &buddy->contact, sizeof(info->buf_)-total);
105 total += info->contact.slen;
106
107 /* status and status text */
108 if (buddy->sub == NULL || buddy->status.info_cnt==0) {
109 info->status = PJSUA_BUDDY_STATUS_UNKNOWN;
110 info->status_text = pj_str("?");
111 } else if (pjsua_var.buddy[buddy_id].status.info[0].basic_open) {
112 info->status = PJSUA_BUDDY_STATUS_ONLINE;
113 info->status_text = pj_str("Online");
114 } else {
115 info->status = PJSUA_BUDDY_STATUS_OFFLINE;
116 info->status_text = pj_str("Offline");
117 }
118
119 /* monitor pres */
120 info->monitor_pres = buddy->monitor;
121
122 PJSUA_UNLOCK();
123 return PJ_SUCCESS;
124}
125
126
127/*
128 * Reset buddy descriptor.
129 */
130static void reset_buddy(pjsua_buddy_id id)
131{
Benny Prijonoac623b32006-07-03 15:19:31 +0000132 pj_bzero(&pjsua_var.buddy[id], sizeof(pjsua_var.buddy[id]));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000133 pjsua_var.buddy[id].index = id;
134}
135
136
137/*
138 * Add new buddy.
139 */
140PJ_DEF(pj_status_t) pjsua_buddy_add( const pjsua_buddy_config *cfg,
141 pjsua_buddy_id *p_buddy_id)
142{
143 pjsip_name_addr *url;
144 pjsip_sip_uri *sip_uri;
145 int index;
146 pj_str_t tmp;
147
148 PJ_ASSERT_RETURN(pjsua_var.buddy_cnt <=
149 PJ_ARRAY_SIZE(pjsua_var.buddy),
150 PJ_ETOOMANY);
151
152 PJSUA_LOCK();
153
154 /* Find empty slot */
155 for (index=0; index<PJ_ARRAY_SIZE(pjsua_var.buddy); ++index) {
156 if (pjsua_var.buddy[index].uri.slen == 0)
157 break;
158 }
159
160 /* Expect to find an empty slot */
161 if (index == PJ_ARRAY_SIZE(pjsua_var.buddy)) {
162 PJSUA_UNLOCK();
163 /* This shouldn't happen */
164 pj_assert(!"index < PJ_ARRAY_SIZE(pjsua_var.buddy)");
165 return PJ_ETOOMANY;
166 }
167
168
169 /* Get name and display name for buddy */
170 pj_strdup_with_null(pjsua_var.pool, &tmp, &cfg->uri);
171 url = (pjsip_name_addr*)pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen,
172 PJSIP_PARSE_URI_AS_NAMEADDR);
173
174 if (url == NULL) {
175 pjsua_perror(THIS_FILE, "Unable to add buddy", PJSIP_EINVALIDURI);
176 PJSUA_UNLOCK();
177 return PJSIP_EINVALIDURI;
178 }
179
Benny Prijonofc493592007-02-18 20:56:32 +0000180 /* Only support SIP schemes */
181 if (!PJSIP_URI_SCHEME_IS_SIP(url) && !PJSIP_URI_SCHEME_IS_SIPS(url))
182 return PJSIP_EINVALIDSCHEME;
183
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000184 /* Reset buddy, to make sure everything is cleared with default
185 * values
186 */
187 reset_buddy(index);
188
189 /* Save URI */
190 pjsua_var.buddy[index].uri = tmp;
191
Benny Prijono9c1528f2007-02-10 19:22:25 +0000192 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(url->uri);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000193 pjsua_var.buddy[index].name = sip_uri->user;
194 pjsua_var.buddy[index].display = url->display;
195 pjsua_var.buddy[index].host = sip_uri->host;
196 pjsua_var.buddy[index].port = sip_uri->port;
197 pjsua_var.buddy[index].monitor = cfg->subscribe;
198 if (pjsua_var.buddy[index].port == 0)
199 pjsua_var.buddy[index].port = 5060;
200
201 if (p_buddy_id)
202 *p_buddy_id = index;
203
204 pjsua_var.buddy_cnt++;
205
206 pjsua_buddy_subscribe_pres(index, cfg->subscribe);
207
208 PJSUA_UNLOCK();
209
210 return PJ_SUCCESS;
211}
212
213
214/*
215 * Delete buddy.
216 */
217PJ_DEF(pj_status_t) pjsua_buddy_del(pjsua_buddy_id buddy_id)
218{
219 PJ_ASSERT_RETURN(buddy_id>=0 &&
220 buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy),
221 PJ_EINVAL);
222
223 PJSUA_LOCK();
224
225 if (pjsua_var.buddy[buddy_id].uri.slen == 0) {
226 PJSUA_UNLOCK();
227 return PJ_SUCCESS;
228 }
229
230 /* Unsubscribe presence */
231 pjsua_buddy_subscribe_pres(buddy_id, PJ_FALSE);
232
233 /* Remove buddy */
234 pjsua_var.buddy[buddy_id].uri.slen = 0;
235 pjsua_var.buddy_cnt--;
236
237 /* Reset buddy struct */
238 reset_buddy(buddy_id);
239
240 PJSUA_UNLOCK();
241 return PJ_SUCCESS;
242}
243
244
245/*
246 * Enable/disable buddy's presence monitoring.
247 */
248PJ_DEF(pj_status_t) pjsua_buddy_subscribe_pres( pjsua_buddy_id buddy_id,
249 pj_bool_t subscribe)
250{
251 pjsua_buddy *buddy;
252
253 PJ_ASSERT_RETURN(buddy_id>=0 &&
254 buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy),
255 PJ_EINVAL);
256
257 PJSUA_LOCK();
258
259 buddy = &pjsua_var.buddy[buddy_id];
260 buddy->monitor = subscribe;
261 pjsua_pres_refresh();
262
263 PJSUA_UNLOCK();
264
265 return PJ_SUCCESS;
266}
267
268
269/*
270 * Dump presence subscriptions to log file.
271 */
272PJ_DEF(void) pjsua_pres_dump(pj_bool_t verbose)
273{
274 unsigned acc_id;
275 unsigned i;
276
277
278 PJSUA_LOCK();
279
280 /*
281 * When no detail is required, just dump number of server and client
282 * subscriptions.
283 */
284 if (verbose == PJ_FALSE) {
285
286 int count = 0;
287
288 for (acc_id=0; acc_id<PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
289
290 if (!pjsua_var.acc[acc_id].valid)
291 continue;
292
293 if (!pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) {
294 struct pjsua_srv_pres *uapres;
295
296 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
297 while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) {
298 ++count;
299 uapres = uapres->next;
300 }
301 }
302 }
303
304 PJ_LOG(3,(THIS_FILE, "Number of server/UAS subscriptions: %d",
305 count));
306
307 count = 0;
308
309 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
310 if (pjsua_var.buddy[i].uri.slen == 0)
311 continue;
312 if (pjsua_var.buddy[i].sub) {
313 ++count;
314 }
315 }
316
317 PJ_LOG(3,(THIS_FILE, "Number of client/UAC subscriptions: %d",
318 count));
319 PJSUA_UNLOCK();
320 return;
321 }
322
323
324 /*
325 * Dumping all server (UAS) subscriptions
326 */
327 PJ_LOG(3,(THIS_FILE, "Dumping pjsua server subscriptions:"));
328
329 for (acc_id=0; acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
330
331 if (!pjsua_var.acc[acc_id].valid)
332 continue;
333
334 PJ_LOG(3,(THIS_FILE, " %.*s",
335 (int)pjsua_var.acc[acc_id].cfg.id.slen,
336 pjsua_var.acc[acc_id].cfg.id.ptr));
337
338 if (pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) {
339
340 PJ_LOG(3,(THIS_FILE, " - none - "));
341
342 } else {
343 struct pjsua_srv_pres *uapres;
344
345 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
346 while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) {
347
348 PJ_LOG(3,(THIS_FILE, " %10s %s",
349 pjsip_evsub_get_state_name(uapres->sub),
350 uapres->remote));
351
352 uapres = uapres->next;
353 }
354 }
355 }
356
357 /*
358 * Dumping all client (UAC) subscriptions
359 */
360 PJ_LOG(3,(THIS_FILE, "Dumping pjsua client subscriptions:"));
361
362 if (pjsua_var.buddy_cnt == 0) {
363
364 PJ_LOG(3,(THIS_FILE, " - no buddy list - "));
365
366 } else {
367 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
368
369 if (pjsua_var.buddy[i].uri.slen == 0)
370 continue;
371
372 if (pjsua_var.buddy[i].sub) {
373 PJ_LOG(3,(THIS_FILE, " %10s %.*s",
374 pjsip_evsub_get_state_name(pjsua_var.buddy[i].sub),
375 (int)pjsua_var.buddy[i].uri.slen,
376 pjsua_var.buddy[i].uri.ptr));
377 } else {
378 PJ_LOG(3,(THIS_FILE, " %10s %.*s",
379 "(null)",
380 (int)pjsua_var.buddy[i].uri.slen,
381 pjsua_var.buddy[i].uri.ptr));
382 }
383 }
384 }
385
386 PJSUA_UNLOCK();
387}
388
389
390/***************************************************************************
391 * Server subscription.
Benny Prijono834aee32006-02-19 01:38:06 +0000392 */
393
394/* Proto */
395static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata);
396
397/* The module instance. */
398static pjsip_module mod_pjsua_pres =
399{
400 NULL, NULL, /* prev, next. */
401 { "mod-pjsua-pres", 14 }, /* Name. */
402 -1, /* Id */
403 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
Benny Prijono834aee32006-02-19 01:38:06 +0000404 NULL, /* load() */
405 NULL, /* start() */
406 NULL, /* stop() */
407 NULL, /* unload() */
408 &pres_on_rx_request, /* on_rx_request() */
409 NULL, /* on_rx_response() */
410 NULL, /* on_tx_request. */
411 NULL, /* on_tx_response() */
412 NULL, /* on_tsx_state() */
413
414};
415
416
417/* Callback called when *server* subscription state has changed. */
418static void pres_evsub_on_srv_state( pjsip_evsub *sub, pjsip_event *event)
419{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000420 pjsua_srv_pres *uapres;
Benny Prijono834aee32006-02-19 01:38:06 +0000421
422 PJ_UNUSED_ARG(event);
423
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000424 PJSUA_LOCK();
425
426 uapres = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
Benny Prijono834aee32006-02-19 01:38:06 +0000427 if (uapres) {
428 PJ_LOG(3,(THIS_FILE, "Server subscription to %s is %s",
429 uapres->remote, pjsip_evsub_get_state_name(sub)));
430
431 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000432 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +0000433 pj_list_erase(uapres);
434 }
435 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000436
437 PJSUA_UNLOCK();
Benny Prijono834aee32006-02-19 01:38:06 +0000438}
439
440/* This is called when request is received.
441 * We need to check for incoming SUBSCRIBE request.
442 */
443static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata)
444{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000445 int acc_id;
Benny Prijono6f979412006-06-15 12:25:46 +0000446 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000447 pj_str_t contact;
Benny Prijono834aee32006-02-19 01:38:06 +0000448 pjsip_method *req_method = &rdata->msg_info.msg->line.req.method;
449 pjsua_srv_pres *uapres;
450 pjsip_evsub *sub;
451 pjsip_evsub_user pres_cb;
452 pjsip_tx_data *tdata;
453 pjsip_pres_status pres_status;
454 pjsip_dialog *dlg;
455 pj_status_t status;
456
457 if (pjsip_method_cmp(req_method, &pjsip_subscribe_method) != 0)
458 return PJ_FALSE;
459
460 /* Incoming SUBSCRIBE: */
461
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000462 PJSUA_LOCK();
463
Benny Prijonoa91a0032006-02-26 21:23:45 +0000464 /* Find which account for the incoming request. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000465 acc_id = pjsua_acc_find_for_incoming(rdata);
Benny Prijono6f979412006-06-15 12:25:46 +0000466 acc = &pjsua_var.acc[acc_id];
Benny Prijonoa91a0032006-02-26 21:23:45 +0000467
Benny Prijono6f979412006-06-15 12:25:46 +0000468 PJ_LOG(4,(THIS_FILE, "Creating server subscription, using account %d",
469 acc_id));
470
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000471 /* Create suitable Contact header */
472 status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact,
473 acc_id, rdata);
474 if (status != PJ_SUCCESS) {
475 pjsua_perror(THIS_FILE, "Unable to generate Contact header", status);
476 PJSUA_UNLOCK();
477 return PJ_TRUE;
478 }
479
Benny Prijono834aee32006-02-19 01:38:06 +0000480 /* Create UAS dialog: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000481 status = pjsip_dlg_create_uas(pjsip_ua_instance(), rdata,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000482 &contact, &dlg);
Benny Prijono834aee32006-02-19 01:38:06 +0000483 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000484 pjsua_perror(THIS_FILE,
485 "Unable to create UAS dialog for subscription",
486 status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000487 PJSUA_UNLOCK();
488 return PJ_TRUE;
Benny Prijono834aee32006-02-19 01:38:06 +0000489 }
490
Benny Prijono6f979412006-06-15 12:25:46 +0000491 /* Set credentials. */
492 pjsip_auth_clt_set_credentials(&dlg->auth_sess, acc->cred_cnt, acc->cred);
493
Benny Prijono834aee32006-02-19 01:38:06 +0000494 /* Init callback: */
Benny Prijonoac623b32006-07-03 15:19:31 +0000495 pj_bzero(&pres_cb, sizeof(pres_cb));
Benny Prijono834aee32006-02-19 01:38:06 +0000496 pres_cb.on_evsub_state = &pres_evsub_on_srv_state;
497
498 /* Create server presence subscription: */
499 status = pjsip_pres_create_uas( dlg, &pres_cb, rdata, &sub);
500 if (status != PJ_SUCCESS) {
Benny Prijono1c2bf462006-03-05 11:54:02 +0000501 pjsip_dlg_terminate(dlg);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000502 pjsua_perror(THIS_FILE, "Unable to create server subscription",
503 status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000504 PJSUA_UNLOCK();
505 return PJ_TRUE;
Benny Prijono834aee32006-02-19 01:38:06 +0000506 }
507
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000508 /* If account is locked to specific transport, then lock dialog
509 * to this transport too.
510 */
511 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
512 pjsip_tpselector tp_sel;
513
514 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
515 pjsip_dlg_set_transport(dlg, &tp_sel);
516 }
517
Benny Prijono834aee32006-02-19 01:38:06 +0000518 /* Attach our data to the subscription: */
519 uapres = pj_pool_alloc(dlg->pool, sizeof(pjsua_srv_pres));
520 uapres->sub = sub;
521 uapres->remote = pj_pool_alloc(dlg->pool, PJSIP_MAX_URL_SIZE);
522 status = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, dlg->remote.info->uri,
523 uapres->remote, PJSIP_MAX_URL_SIZE);
524 if (status < 1)
525 pj_ansi_strcpy(uapres->remote, "<-- url is too long-->");
526 else
527 uapres->remote[status] = '\0';
528
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000529 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, uapres);
Benny Prijono834aee32006-02-19 01:38:06 +0000530
531 /* Add server subscription to the list: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000532 pj_list_push_back(&pjsua_var.acc[acc_id].pres_srv_list, uapres);
Benny Prijono834aee32006-02-19 01:38:06 +0000533
534
535 /* Create and send 200 (OK) to the SUBSCRIBE request: */
536 status = pjsip_pres_accept(sub, rdata, 200, NULL);
537 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000538 pjsua_perror(THIS_FILE, "Unable to accept presence subscription",
539 status);
Benny Prijono834aee32006-02-19 01:38:06 +0000540 pj_list_erase(uapres);
Benny Prijono1c2bf462006-03-05 11:54:02 +0000541 pjsip_pres_terminate(sub, PJ_FALSE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000542 PJSUA_UNLOCK();
Benny Prijono834aee32006-02-19 01:38:06 +0000543 return PJ_FALSE;
544 }
545
546
547 /* Set our online status: */
Benny Prijonoac623b32006-07-03 15:19:31 +0000548 pj_bzero(&pres_status, sizeof(pres_status));
Benny Prijono834aee32006-02-19 01:38:06 +0000549 pres_status.info_cnt = 1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000550 pres_status.info[0].basic_open = pjsua_var.acc[acc_id].online_status;
551 //Both pjsua_var.local_uri and pjsua_var.contact_uri are enclosed in "<" and ">"
Benny Prijono834aee32006-02-19 01:38:06 +0000552 //causing XML parsing to fail.
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000553 //pres_status.info[0].contact = pjsua_var.local_uri;
Benny Prijono834aee32006-02-19 01:38:06 +0000554
555 pjsip_pres_set_status(sub, &pres_status);
556
557 /* Create and send the first NOTIFY to active subscription: */
558 status = pjsip_pres_notify( sub, PJSIP_EVSUB_STATE_ACTIVE, NULL,
559 NULL, &tdata);
Benny Prijono21b9ad92006-08-15 13:11:22 +0000560 if (status == PJ_SUCCESS) {
561 pjsua_process_msg_data(tdata, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +0000562 status = pjsip_pres_send_request( sub, tdata);
Benny Prijono21b9ad92006-08-15 13:11:22 +0000563 }
Benny Prijono834aee32006-02-19 01:38:06 +0000564
565 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000566 pjsua_perror(THIS_FILE, "Unable to create/send NOTIFY",
567 status);
Benny Prijono834aee32006-02-19 01:38:06 +0000568 pj_list_erase(uapres);
Benny Prijono1c2bf462006-03-05 11:54:02 +0000569 pjsip_pres_terminate(sub, PJ_FALSE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000570 PJSUA_UNLOCK();
Benny Prijono834aee32006-02-19 01:38:06 +0000571 return PJ_FALSE;
572 }
573
574
575 /* Done: */
576
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000577 PJSUA_UNLOCK();
578
Benny Prijono834aee32006-02-19 01:38:06 +0000579 return PJ_TRUE;
580}
581
582
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000583/*
584 * Client presence publication callback.
585 */
586static void publish_cb(struct pjsip_publishc_cbparam *param)
587{
588 pjsua_acc *acc = param->token;
589
590 if (param->code/100 != 2 || param->status != PJ_SUCCESS) {
591 if (param->status != PJ_SUCCESS) {
592 char errmsg[PJ_ERR_MSG_SIZE];
593
594 pj_strerror(param->status, errmsg, sizeof(errmsg));
595 PJ_LOG(1,(THIS_FILE,
596 "Client publication (PUBLISH) failed, status=%d, msg=%s",
597 param->status, errmsg));
598 } else {
599 PJ_LOG(1,(THIS_FILE,
600 "Client publication (PUBLISH) failed (%d/%.*s)",
601 param->code, (int)param->reason.slen,
602 param->reason.ptr));
603 }
604
605 pjsip_publishc_destroy(param->pubc);
606 acc->publish_sess = NULL;
607 }
608}
609
610
611/*
612 * Send PUBLISH request.
613 */
614static pj_status_t send_publish(int acc_id, pj_bool_t active)
615{
616 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
617 pjsua_acc *acc = &pjsua_var.acc[acc_id];
618 pjsip_pres_status pres_status;
619 pjsip_tx_data *tdata;
620 pj_status_t status;
621
622
623 /* Create PUBLISH request */
624 if (active) {
Benny Prijono8c6e8842007-02-24 15:33:54 +0000625 char *bpos;
626 pj_str_t entity;
627
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000628 status = pjsip_publishc_publish(acc->publish_sess, PJ_TRUE, &tdata);
629 if (status != PJ_SUCCESS) {
630 pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status);
631 goto on_error;
632 }
633
634 /* Set our online status: */
635 pj_bzero(&pres_status, sizeof(pres_status));
636 pres_status.info_cnt = 1;
637 pres_status.info[0].basic_open = acc->online_status;
638
Benny Prijono8c6e8842007-02-24 15:33:54 +0000639 /* Be careful not to send PIDF with presence entity ID containing
640 * "<" character.
641 */
642 if ((bpos=pj_strchr(&acc_cfg->id, '<')) != NULL) {
643 char *epos = pj_strchr(&acc_cfg->id, '>');
644 if (epos - bpos < 2) {
645 pj_assert(!"Unexpected invalid URI");
646 status = PJSIP_EINVALIDURI;
647 goto on_error;
648 }
649 entity.ptr = bpos+1;
650 entity.slen = epos - bpos - 1;
651 } else {
652 entity = acc_cfg->id;
653 }
654
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000655 /* Create and add PIDF message body */
656 status = pjsip_pres_create_pidf(tdata->pool, &pres_status,
Benny Prijono8c6e8842007-02-24 15:33:54 +0000657 &entity, &tdata->msg->body);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000658 if (status != PJ_SUCCESS) {
659 pjsua_perror(THIS_FILE, "Error creating PIDF for PUBLISH request",
660 status);
661 pjsip_tx_data_dec_ref(tdata);
662 goto on_error;
663 }
664 } else {
665 status = pjsip_publishc_unpublish(acc->publish_sess, &tdata);
666 if (status != PJ_SUCCESS) {
667 pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status);
668 goto on_error;
669 }
670 }
671
672 /* Add headers etc */
673 pjsua_process_msg_data(tdata, NULL);
674
675 /* Send the PUBLISH request */
676 status = pjsip_publishc_send(acc->publish_sess, tdata);
677 if (status != PJ_SUCCESS) {
678 pjsua_perror(THIS_FILE, "Error sending PUBLISH request", status);
679 goto on_error;
680 }
681
682 acc->publish_state = acc->online_status;
683 return PJ_SUCCESS;
684
685on_error:
686 pjsip_publishc_destroy(acc->publish_sess);
687 acc->publish_sess = NULL;
688 return status;
689}
690
691
692/* Create client publish session */
Benny Prijono8b6834f2007-02-24 13:29:22 +0000693pj_status_t pjsua_pres_init_publish_acc(int acc_id)
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000694{
695 const pj_str_t STR_PRESENCE = { "presence", 8 };
696 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
697 pjsua_acc *acc = &pjsua_var.acc[acc_id];
698 pj_status_t status;
699
700 /* Create and init client publication session */
701 if (acc_cfg->publish_enabled) {
702
703 /* Create client publication */
704 status = pjsip_publishc_create(pjsua_var.endpt, 0, acc, &publish_cb,
705 &acc->publish_sess);
706 if (status != PJ_SUCCESS) {
707 acc->publish_sess = NULL;
708 return status;
709 }
710
711 /* Initialize client publication */
712 status = pjsip_publishc_init(acc->publish_sess, &STR_PRESENCE,
713 &acc_cfg->id, &acc_cfg->id,
714 &acc_cfg->id,
715 PJSUA_PUBLISH_EXPIRATION);
716 if (status != PJ_SUCCESS) {
717 acc->publish_sess = NULL;
718 return status;
719 }
720
721 /* Send initial PUBLISH request */
722 if (acc->online_status != 0) {
723 status = send_publish(acc_id, PJ_TRUE);
724 if (status != PJ_SUCCESS)
725 return status;
726 }
727
728 } else {
729 acc->publish_sess = NULL;
730 }
731
732 return PJ_SUCCESS;
733}
734
735
736/* Init presence for account */
737pj_status_t pjsua_pres_init_acc(int acc_id)
738{
739 pjsua_acc *acc = &pjsua_var.acc[acc_id];
740
741 /* Init presence subscription */
742 pj_list_init(&acc->pres_srv_list);
743
Benny Prijono8b6834f2007-02-24 13:29:22 +0000744 return PJ_SUCCESS;
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000745}
746
747
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000748/* Terminate server subscription for the account */
749void pjsua_pres_delete_acc(int acc_id)
Benny Prijono834aee32006-02-19 01:38:06 +0000750{
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000751 pjsua_acc *acc = &pjsua_var.acc[acc_id];
752 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
Benny Prijono834aee32006-02-19 01:38:06 +0000753 pjsua_srv_pres *uapres;
754
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000755 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
Benny Prijono834aee32006-02-19 01:38:06 +0000756
Benny Prijono922933b2007-01-21 16:23:56 +0000757 /* Notify all subscribers that we're no longer available */
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000758 while (uapres != &acc->pres_srv_list) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000759
760 pjsip_pres_status pres_status;
761 pj_str_t reason = { "noresource", 10 };
762 pjsip_tx_data *tdata;
763
764 pjsip_pres_get_status(uapres->sub, &pres_status);
765
766 pres_status.info[0].basic_open = pjsua_var.acc[acc_id].online_status;
767 pjsip_pres_set_status(uapres->sub, &pres_status);
768
769 if (pjsip_pres_notify(uapres->sub,
770 PJSIP_EVSUB_STATE_TERMINATED, NULL,
771 &reason, &tdata)==PJ_SUCCESS)
772 {
773 pjsip_pres_send_request(uapres->sub, tdata);
774 }
775
776 uapres = uapres->next;
777 }
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000778
Benny Prijono922933b2007-01-21 16:23:56 +0000779 /* Clear server presence subscription list because account might be reused
780 * later. */
781 pj_list_init(&acc->pres_srv_list);
782
783 /* Terminate presence publication, if any */
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000784 if (acc->publish_sess) {
785 acc->online_status = PJ_FALSE;
786 send_publish(acc_id, PJ_FALSE);
787 if (acc->publish_sess) {
788 pjsip_publishc_destroy(acc->publish_sess);
789 acc->publish_sess = NULL;
790 }
791 acc_cfg->publish_enabled = PJ_FALSE;
792 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000793}
794
795
796/* Refresh subscription (e.g. when our online status has changed) */
797static void refresh_server_subscription(int acc_id)
798{
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000799 pjsua_acc *acc = &pjsua_var.acc[acc_id];
800 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000801 pjsua_srv_pres *uapres;
802
803 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
804
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000805 while (uapres != &acc->pres_srv_list) {
Benny Prijono834aee32006-02-19 01:38:06 +0000806
807 pjsip_pres_status pres_status;
808 pjsip_tx_data *tdata;
809
810 pjsip_pres_get_status(uapres->sub, &pres_status);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000811 if (pres_status.info[0].basic_open != acc->online_status) {
812 pres_status.info[0].basic_open = acc->online_status;
Benny Prijono834aee32006-02-19 01:38:06 +0000813 pjsip_pres_set_status(uapres->sub, &pres_status);
814
Benny Prijono21b9ad92006-08-15 13:11:22 +0000815 if (pjsip_pres_current_notify(uapres->sub, &tdata)==PJ_SUCCESS) {
816 pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000817 pjsip_pres_send_request(uapres->sub, tdata);
Benny Prijono21b9ad92006-08-15 13:11:22 +0000818 }
Benny Prijono834aee32006-02-19 01:38:06 +0000819 }
820
821 uapres = uapres->next;
822 }
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000823
Benny Prijono8b6834f2007-02-24 13:29:22 +0000824 /* Send PUBLISH if required. We only do this when we have a PUBLISH
825 * session. If we don't have a PUBLISH session, then it could be
826 * that we're waiting until registration has completed before we
827 * send the first PUBLISH.
828 */
829 if (acc_cfg->publish_enabled && acc->publish_sess) {
830 if (acc->publish_state != acc->online_status) {
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000831 send_publish(acc_id, PJ_TRUE);
832 }
833 }
Benny Prijono834aee32006-02-19 01:38:06 +0000834}
835
836
837
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000838/***************************************************************************
839 * Client subscription.
Benny Prijono834aee32006-02-19 01:38:06 +0000840 */
841
842/* Callback called when *client* subscription state has changed. */
843static void pjsua_evsub_on_state( pjsip_evsub *sub, pjsip_event *event)
844{
845 pjsua_buddy *buddy;
846
847 PJ_UNUSED_ARG(event);
848
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000849 PJSUA_LOCK();
850
851 buddy = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
Benny Prijono834aee32006-02-19 01:38:06 +0000852 if (buddy) {
853 PJ_LOG(3,(THIS_FILE,
Benny Prijono9fc735d2006-05-28 14:58:12 +0000854 "Presence subscription to %.*s is %s",
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000855 (int)pjsua_var.buddy[buddy->index].uri.slen,
856 pjsua_var.buddy[buddy->index].uri.ptr,
Benny Prijono834aee32006-02-19 01:38:06 +0000857 pjsip_evsub_get_state_name(sub)));
858
859 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
860 buddy->sub = NULL;
861 buddy->status.info_cnt = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000862 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +0000863 }
Benny Prijono9fc735d2006-05-28 14:58:12 +0000864
865 /* Call callback */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000866 if (pjsua_var.ua_cfg.cb.on_buddy_state)
867 (*pjsua_var.ua_cfg.cb.on_buddy_state)(buddy->index);
Benny Prijono834aee32006-02-19 01:38:06 +0000868 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000869
870 PJSUA_UNLOCK();
Benny Prijono834aee32006-02-19 01:38:06 +0000871}
872
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000873
874/* Callback when transaction state has changed. */
875static void pjsua_evsub_on_tsx_state(pjsip_evsub *sub,
876 pjsip_transaction *tsx,
877 pjsip_event *event)
878{
879 pjsua_buddy *buddy;
880 pjsip_contact_hdr *contact_hdr;
881
882 PJSUA_LOCK();
883
884 buddy = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
885 if (!buddy) {
886 PJSUA_UNLOCK();
887 return;
888 }
889
890 /* We only use this to update buddy's Contact, when it's not
891 * set.
892 */
893 if (buddy->contact.slen != 0) {
894 /* Contact already set */
895 PJSUA_UNLOCK();
896 return;
897 }
898
899 /* Only care about 2xx response to outgoing SUBSCRIBE */
900 if (tsx->status_code/100 != 2 ||
901 tsx->role != PJSIP_UAC_ROLE ||
902 event->type != PJSIP_EVENT_RX_MSG ||
903 pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method)!=0)
904 {
905 PJSUA_UNLOCK();
906 return;
907 }
908
909 /* Find contact header. */
910 contact_hdr = pjsip_msg_find_hdr(event->body.rx_msg.rdata->msg_info.msg,
911 PJSIP_H_CONTACT, NULL);
912 if (!contact_hdr) {
913 PJSUA_UNLOCK();
914 return;
915 }
916
917 buddy->contact.ptr = pj_pool_alloc(pjsua_var.pool, PJSIP_MAX_URL_SIZE);
918 buddy->contact.slen = pjsip_uri_print( PJSIP_URI_IN_CONTACT_HDR,
919 contact_hdr->uri,
920 buddy->contact.ptr,
921 PJSIP_MAX_URL_SIZE);
922 if (buddy->contact.slen < 0)
923 buddy->contact.slen = 0;
924
925 PJSUA_UNLOCK();
926}
927
928
Benny Prijono834aee32006-02-19 01:38:06 +0000929/* Callback called when we receive NOTIFY */
930static void pjsua_evsub_on_rx_notify(pjsip_evsub *sub,
931 pjsip_rx_data *rdata,
932 int *p_st_code,
933 pj_str_t **p_st_text,
934 pjsip_hdr *res_hdr,
935 pjsip_msg_body **p_body)
936{
937 pjsua_buddy *buddy;
938
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000939 PJSUA_LOCK();
940
941 buddy = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
Benny Prijono834aee32006-02-19 01:38:06 +0000942 if (buddy) {
943 /* Update our info. */
944 pjsip_pres_get_status(sub, &buddy->status);
Benny Prijono834aee32006-02-19 01:38:06 +0000945 }
946
947 /* The default is to send 200 response to NOTIFY.
948 * Just leave it there..
949 */
950 PJ_UNUSED_ARG(rdata);
951 PJ_UNUSED_ARG(p_st_code);
952 PJ_UNUSED_ARG(p_st_text);
953 PJ_UNUSED_ARG(res_hdr);
954 PJ_UNUSED_ARG(p_body);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000955
956 PJSUA_UNLOCK();
Benny Prijono834aee32006-02-19 01:38:06 +0000957}
958
959
960/* Event subscription callback. */
961static pjsip_evsub_user pres_callback =
962{
963 &pjsua_evsub_on_state,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000964 &pjsua_evsub_on_tsx_state,
Benny Prijono834aee32006-02-19 01:38:06 +0000965
966 NULL, /* on_rx_refresh: don't care about SUBSCRIBE refresh, unless
967 * we want to authenticate
968 */
969
970 &pjsua_evsub_on_rx_notify,
971
972 NULL, /* on_client_refresh: Use default behaviour, which is to
973 * refresh client subscription. */
974
975 NULL, /* on_server_timeout: Use default behaviour, which is to send
976 * NOTIFY to terminate.
977 */
978};
979
980
981/* It does what it says.. */
982static void subscribe_buddy_presence(unsigned index)
983{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000984 pjsua_buddy *buddy;
985 int acc_id;
986 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000987 pj_str_t contact;
Benny Prijono834aee32006-02-19 01:38:06 +0000988 pjsip_dialog *dlg;
989 pjsip_tx_data *tdata;
990 pj_status_t status;
991
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000992 buddy = &pjsua_var.buddy[index];
993 acc_id = pjsua_acc_find_for_outgoing(&buddy->uri);
Benny Prijono8b1889b2006-06-06 18:40:40 +0000994
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000995 acc = &pjsua_var.acc[acc_id];
Benny Prijonoa91a0032006-02-26 21:23:45 +0000996
Benny Prijonob4a17c92006-07-10 14:40:21 +0000997 PJ_LOG(4,(THIS_FILE, "Using account %d for buddy %d subscription",
998 acc_id, index));
999
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001000 /* Generate suitable Contact header */
1001 status = pjsua_acc_create_uac_contact(pjsua_var.pool, &contact,
1002 acc_id, &buddy->uri);
1003 if (status != PJ_SUCCESS) {
1004 pjsua_perror(THIS_FILE, "Unable to generate Contact header", status);
1005 return;
1006 }
1007
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001008 /* Create UAC dialog */
Benny Prijono834aee32006-02-19 01:38:06 +00001009 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001010 &acc->cfg.id,
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001011 &contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001012 &buddy->uri,
Benny Prijono834aee32006-02-19 01:38:06 +00001013 NULL, &dlg);
1014 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001015 pjsua_perror(THIS_FILE, "Unable to create dialog",
1016 status);
Benny Prijono834aee32006-02-19 01:38:06 +00001017 return;
1018 }
1019
Benny Prijonodc752ca2006-09-22 16:55:42 +00001020 status = pjsip_pres_create_uac( dlg, &pres_callback,
1021 PJSIP_EVSUB_NO_EVENT_ID, &buddy->sub);
1022 if (status != PJ_SUCCESS) {
1023 pjsua_var.buddy[index].sub = NULL;
1024 pjsua_perror(THIS_FILE, "Unable to create presence client",
1025 status);
1026 pjsip_dlg_terminate(dlg);
1027 return;
1028 }
1029
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001030 /* If account is locked to specific transport, then lock dialog
1031 * to this transport too.
1032 */
1033 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1034 pjsip_tpselector tp_sel;
1035
1036 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
1037 pjsip_dlg_set_transport(dlg, &tp_sel);
1038 }
1039
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001040 /* Set route-set */
1041 if (!pj_list_empty(&acc->route_set)) {
1042 pjsip_dlg_set_route_set(dlg, &acc->route_set);
1043 }
1044
1045 /* Set credentials */
1046 if (acc->cred_cnt) {
Benny Prijonodc39fe82006-05-26 12:17:46 +00001047 pjsip_auth_clt_set_credentials( &dlg->auth_sess,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001048 acc->cred_cnt, acc->cred);
Benny Prijonodc39fe82006-05-26 12:17:46 +00001049 }
Benny Prijono1d8d6082006-04-29 12:38:25 +00001050
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001051 pjsip_evsub_set_mod_data(buddy->sub, pjsua_var.mod.id, buddy);
Benny Prijono834aee32006-02-19 01:38:06 +00001052
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001053 status = pjsip_pres_initiate(buddy->sub, -1, &tdata);
Benny Prijono834aee32006-02-19 01:38:06 +00001054 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001055 pjsip_pres_terminate(buddy->sub, PJ_FALSE);
1056 buddy->sub = NULL;
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001057 pjsua_perror(THIS_FILE, "Unable to create initial SUBSCRIBE",
1058 status);
Benny Prijono834aee32006-02-19 01:38:06 +00001059 return;
1060 }
1061
Benny Prijono21b9ad92006-08-15 13:11:22 +00001062 pjsua_process_msg_data(tdata, NULL);
1063
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001064 status = pjsip_pres_send_request(buddy->sub, tdata);
Benny Prijono834aee32006-02-19 01:38:06 +00001065 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001066 pjsip_pres_terminate(buddy->sub, PJ_FALSE);
1067 buddy->sub = NULL;
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001068 pjsua_perror(THIS_FILE, "Unable to send initial SUBSCRIBE",
1069 status);
Benny Prijono834aee32006-02-19 01:38:06 +00001070 return;
1071 }
Benny Prijono834aee32006-02-19 01:38:06 +00001072}
1073
1074
1075/* It does what it says... */
1076static void unsubscribe_buddy_presence(unsigned index)
1077{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001078 pjsua_buddy *buddy;
Benny Prijono834aee32006-02-19 01:38:06 +00001079 pjsip_tx_data *tdata;
1080 pj_status_t status;
1081
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001082 buddy = &pjsua_var.buddy[index];
1083
1084 if (buddy->sub == NULL)
Benny Prijono834aee32006-02-19 01:38:06 +00001085 return;
1086
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001087 if (pjsip_evsub_get_state(buddy->sub) == PJSIP_EVSUB_STATE_TERMINATED) {
1088 pjsua_var.buddy[index].sub = NULL;
Benny Prijono834aee32006-02-19 01:38:06 +00001089 return;
1090 }
1091
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001092 status = pjsip_pres_initiate( buddy->sub, 0, &tdata);
Benny Prijono21b9ad92006-08-15 13:11:22 +00001093 if (status == PJ_SUCCESS) {
1094 pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001095 status = pjsip_pres_send_request( buddy->sub, tdata );
Benny Prijono21b9ad92006-08-15 13:11:22 +00001096 }
Benny Prijono834aee32006-02-19 01:38:06 +00001097
Benny Prijono1c2bf462006-03-05 11:54:02 +00001098 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001099 pjsip_pres_terminate(buddy->sub, PJ_FALSE);
1100 buddy->sub = NULL;
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001101 pjsua_perror(THIS_FILE, "Unable to unsubscribe presence",
1102 status);
Benny Prijono834aee32006-02-19 01:38:06 +00001103 }
1104}
1105
1106
1107/* It does what it says.. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001108static void refresh_client_subscriptions(void)
Benny Prijono834aee32006-02-19 01:38:06 +00001109{
Benny Prijono9fc735d2006-05-28 14:58:12 +00001110 unsigned i;
Benny Prijono834aee32006-02-19 01:38:06 +00001111
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001112 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
Benny Prijono834aee32006-02-19 01:38:06 +00001113
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001114 if (!pjsua_var.buddy[i].uri.slen)
1115 continue;
1116
1117 if (pjsua_var.buddy[i].monitor && !pjsua_var.buddy[i].sub) {
Benny Prijono834aee32006-02-19 01:38:06 +00001118 subscribe_buddy_presence(i);
1119
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001120 } else if (!pjsua_var.buddy[i].monitor && pjsua_var.buddy[i].sub) {
Benny Prijono834aee32006-02-19 01:38:06 +00001121 unsubscribe_buddy_presence(i);
1122
1123 }
1124 }
1125}
1126
1127
1128/*
1129 * Init presence
1130 */
1131pj_status_t pjsua_pres_init()
1132{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001133 unsigned i;
Benny Prijono834aee32006-02-19 01:38:06 +00001134 pj_status_t status;
1135
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001136 status = pjsip_endpt_register_module( pjsua_var.endpt, &mod_pjsua_pres);
Benny Prijono834aee32006-02-19 01:38:06 +00001137 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001138 pjsua_perror(THIS_FILE, "Unable to register pjsua presence module",
1139 status);
Benny Prijono834aee32006-02-19 01:38:06 +00001140 }
1141
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001142 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
1143 reset_buddy(i);
1144 }
1145
Benny Prijono834aee32006-02-19 01:38:06 +00001146 return status;
1147}
1148
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001149
Benny Prijono834aee32006-02-19 01:38:06 +00001150/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001151 * Start presence subsystem.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001152 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001153pj_status_t pjsua_pres_start(void)
Benny Prijono9fc735d2006-05-28 14:58:12 +00001154{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001155 /* Nothing to do (is it?) */
Benny Prijono9fc735d2006-05-28 14:58:12 +00001156 return PJ_SUCCESS;
1157}
1158
1159
1160/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001161 * Refresh presence subscriptions
Benny Prijono834aee32006-02-19 01:38:06 +00001162 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001163void pjsua_pres_refresh()
Benny Prijono834aee32006-02-19 01:38:06 +00001164{
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001165 unsigned i;
1166
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001167 refresh_client_subscriptions();
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001168
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001169 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1170 if (pjsua_var.acc[i].valid)
1171 refresh_server_subscription(i);
1172 }
Benny Prijono834aee32006-02-19 01:38:06 +00001173}
1174
1175
1176/*
1177 * Shutdown presence.
1178 */
1179void pjsua_pres_shutdown(void)
1180{
Benny Prijono9fc735d2006-05-28 14:58:12 +00001181 unsigned i;
Benny Prijono834aee32006-02-19 01:38:06 +00001182
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001183 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1184 if (!pjsua_var.acc[i].valid)
1185 continue;
1186 pjsua_pres_delete_acc(i);
Benny Prijonoa91a0032006-02-26 21:23:45 +00001187 }
1188
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001189 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
1190 pjsua_var.buddy[i].monitor = 0;
Benny Prijono834aee32006-02-19 01:38:06 +00001191 }
Benny Prijonoa91a0032006-02-26 21:23:45 +00001192
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001193 pjsua_pres_refresh();
Benny Prijono834aee32006-02-19 01:38:06 +00001194}