blob: eb0ccc219f2caecf68f811c077cc12bd3dd09507 [file] [log] [blame]
Benny Prijono834aee32006-02-19 01:38:06 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
4 *
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) {
625 status = pjsip_publishc_publish(acc->publish_sess, PJ_TRUE, &tdata);
626 if (status != PJ_SUCCESS) {
627 pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status);
628 goto on_error;
629 }
630
631 /* Set our online status: */
632 pj_bzero(&pres_status, sizeof(pres_status));
633 pres_status.info_cnt = 1;
634 pres_status.info[0].basic_open = acc->online_status;
635
636 /* Create and add PIDF message body */
637 status = pjsip_pres_create_pidf(tdata->pool, &pres_status,
638 &acc_cfg->id, &tdata->msg->body);
639 if (status != PJ_SUCCESS) {
640 pjsua_perror(THIS_FILE, "Error creating PIDF for PUBLISH request",
641 status);
642 pjsip_tx_data_dec_ref(tdata);
643 goto on_error;
644 }
645 } else {
646 status = pjsip_publishc_unpublish(acc->publish_sess, &tdata);
647 if (status != PJ_SUCCESS) {
648 pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status);
649 goto on_error;
650 }
651 }
652
653 /* Add headers etc */
654 pjsua_process_msg_data(tdata, NULL);
655
656 /* Send the PUBLISH request */
657 status = pjsip_publishc_send(acc->publish_sess, tdata);
658 if (status != PJ_SUCCESS) {
659 pjsua_perror(THIS_FILE, "Error sending PUBLISH request", status);
660 goto on_error;
661 }
662
663 acc->publish_state = acc->online_status;
664 return PJ_SUCCESS;
665
666on_error:
667 pjsip_publishc_destroy(acc->publish_sess);
668 acc->publish_sess = NULL;
669 return status;
670}
671
672
673/* Create client publish session */
674static pj_status_t create_publish(int acc_id)
675{
676 const pj_str_t STR_PRESENCE = { "presence", 8 };
677 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
678 pjsua_acc *acc = &pjsua_var.acc[acc_id];
679 pj_status_t status;
680
681 /* Create and init client publication session */
682 if (acc_cfg->publish_enabled) {
683
684 /* Create client publication */
685 status = pjsip_publishc_create(pjsua_var.endpt, 0, acc, &publish_cb,
686 &acc->publish_sess);
687 if (status != PJ_SUCCESS) {
688 acc->publish_sess = NULL;
689 return status;
690 }
691
692 /* Initialize client publication */
693 status = pjsip_publishc_init(acc->publish_sess, &STR_PRESENCE,
694 &acc_cfg->id, &acc_cfg->id,
695 &acc_cfg->id,
696 PJSUA_PUBLISH_EXPIRATION);
697 if (status != PJ_SUCCESS) {
698 acc->publish_sess = NULL;
699 return status;
700 }
701
702 /* Send initial PUBLISH request */
703 if (acc->online_status != 0) {
704 status = send_publish(acc_id, PJ_TRUE);
705 if (status != PJ_SUCCESS)
706 return status;
707 }
708
709 } else {
710 acc->publish_sess = NULL;
711 }
712
713 return PJ_SUCCESS;
714}
715
716
717/* Init presence for account */
718pj_status_t pjsua_pres_init_acc(int acc_id)
719{
720 pjsua_acc *acc = &pjsua_var.acc[acc_id];
721
722 /* Init presence subscription */
723 pj_list_init(&acc->pres_srv_list);
724
725
726 return create_publish(acc_id);
727}
728
729
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000730/* Terminate server subscription for the account */
731void pjsua_pres_delete_acc(int acc_id)
Benny Prijono834aee32006-02-19 01:38:06 +0000732{
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000733 pjsua_acc *acc = &pjsua_var.acc[acc_id];
734 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
Benny Prijono834aee32006-02-19 01:38:06 +0000735 pjsua_srv_pres *uapres;
736
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000737 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
Benny Prijono834aee32006-02-19 01:38:06 +0000738
Benny Prijono922933b2007-01-21 16:23:56 +0000739 /* Notify all subscribers that we're no longer available */
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000740 while (uapres != &acc->pres_srv_list) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000741
742 pjsip_pres_status pres_status;
743 pj_str_t reason = { "noresource", 10 };
744 pjsip_tx_data *tdata;
745
746 pjsip_pres_get_status(uapres->sub, &pres_status);
747
748 pres_status.info[0].basic_open = pjsua_var.acc[acc_id].online_status;
749 pjsip_pres_set_status(uapres->sub, &pres_status);
750
751 if (pjsip_pres_notify(uapres->sub,
752 PJSIP_EVSUB_STATE_TERMINATED, NULL,
753 &reason, &tdata)==PJ_SUCCESS)
754 {
755 pjsip_pres_send_request(uapres->sub, tdata);
756 }
757
758 uapres = uapres->next;
759 }
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000760
Benny Prijono922933b2007-01-21 16:23:56 +0000761 /* Clear server presence subscription list because account might be reused
762 * later. */
763 pj_list_init(&acc->pres_srv_list);
764
765 /* Terminate presence publication, if any */
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000766 if (acc->publish_sess) {
767 acc->online_status = PJ_FALSE;
768 send_publish(acc_id, PJ_FALSE);
769 if (acc->publish_sess) {
770 pjsip_publishc_destroy(acc->publish_sess);
771 acc->publish_sess = NULL;
772 }
773 acc_cfg->publish_enabled = PJ_FALSE;
774 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000775}
776
777
778/* Refresh subscription (e.g. when our online status has changed) */
779static void refresh_server_subscription(int acc_id)
780{
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000781 pjsua_acc *acc = &pjsua_var.acc[acc_id];
782 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000783 pjsua_srv_pres *uapres;
784
785 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
786
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000787 while (uapres != &acc->pres_srv_list) {
Benny Prijono834aee32006-02-19 01:38:06 +0000788
789 pjsip_pres_status pres_status;
790 pjsip_tx_data *tdata;
791
792 pjsip_pres_get_status(uapres->sub, &pres_status);
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000793 if (pres_status.info[0].basic_open != acc->online_status) {
794 pres_status.info[0].basic_open = acc->online_status;
Benny Prijono834aee32006-02-19 01:38:06 +0000795 pjsip_pres_set_status(uapres->sub, &pres_status);
796
Benny Prijono21b9ad92006-08-15 13:11:22 +0000797 if (pjsip_pres_current_notify(uapres->sub, &tdata)==PJ_SUCCESS) {
798 pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000799 pjsip_pres_send_request(uapres->sub, tdata);
Benny Prijono21b9ad92006-08-15 13:11:22 +0000800 }
Benny Prijono834aee32006-02-19 01:38:06 +0000801 }
802
803 uapres = uapres->next;
804 }
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000805
806 /* Send PUBLISH if required */
807 if (acc_cfg->publish_enabled) {
808 if (acc->publish_sess == NULL)
809 create_publish(acc_id);
810
811 if (acc->publish_sess && acc->publish_state != acc->online_status) {
812 send_publish(acc_id, PJ_TRUE);
813 }
814 }
Benny Prijono834aee32006-02-19 01:38:06 +0000815}
816
817
818
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000819/***************************************************************************
820 * Client subscription.
Benny Prijono834aee32006-02-19 01:38:06 +0000821 */
822
823/* Callback called when *client* subscription state has changed. */
824static void pjsua_evsub_on_state( pjsip_evsub *sub, pjsip_event *event)
825{
826 pjsua_buddy *buddy;
827
828 PJ_UNUSED_ARG(event);
829
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000830 PJSUA_LOCK();
831
832 buddy = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
Benny Prijono834aee32006-02-19 01:38:06 +0000833 if (buddy) {
834 PJ_LOG(3,(THIS_FILE,
Benny Prijono9fc735d2006-05-28 14:58:12 +0000835 "Presence subscription to %.*s is %s",
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000836 (int)pjsua_var.buddy[buddy->index].uri.slen,
837 pjsua_var.buddy[buddy->index].uri.ptr,
Benny Prijono834aee32006-02-19 01:38:06 +0000838 pjsip_evsub_get_state_name(sub)));
839
840 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
841 buddy->sub = NULL;
842 buddy->status.info_cnt = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000843 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +0000844 }
Benny Prijono9fc735d2006-05-28 14:58:12 +0000845
846 /* Call callback */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000847 if (pjsua_var.ua_cfg.cb.on_buddy_state)
848 (*pjsua_var.ua_cfg.cb.on_buddy_state)(buddy->index);
Benny Prijono834aee32006-02-19 01:38:06 +0000849 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000850
851 PJSUA_UNLOCK();
Benny Prijono834aee32006-02-19 01:38:06 +0000852}
853
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000854
855/* Callback when transaction state has changed. */
856static void pjsua_evsub_on_tsx_state(pjsip_evsub *sub,
857 pjsip_transaction *tsx,
858 pjsip_event *event)
859{
860 pjsua_buddy *buddy;
861 pjsip_contact_hdr *contact_hdr;
862
863 PJSUA_LOCK();
864
865 buddy = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
866 if (!buddy) {
867 PJSUA_UNLOCK();
868 return;
869 }
870
871 /* We only use this to update buddy's Contact, when it's not
872 * set.
873 */
874 if (buddy->contact.slen != 0) {
875 /* Contact already set */
876 PJSUA_UNLOCK();
877 return;
878 }
879
880 /* Only care about 2xx response to outgoing SUBSCRIBE */
881 if (tsx->status_code/100 != 2 ||
882 tsx->role != PJSIP_UAC_ROLE ||
883 event->type != PJSIP_EVENT_RX_MSG ||
884 pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method)!=0)
885 {
886 PJSUA_UNLOCK();
887 return;
888 }
889
890 /* Find contact header. */
891 contact_hdr = pjsip_msg_find_hdr(event->body.rx_msg.rdata->msg_info.msg,
892 PJSIP_H_CONTACT, NULL);
893 if (!contact_hdr) {
894 PJSUA_UNLOCK();
895 return;
896 }
897
898 buddy->contact.ptr = pj_pool_alloc(pjsua_var.pool, PJSIP_MAX_URL_SIZE);
899 buddy->contact.slen = pjsip_uri_print( PJSIP_URI_IN_CONTACT_HDR,
900 contact_hdr->uri,
901 buddy->contact.ptr,
902 PJSIP_MAX_URL_SIZE);
903 if (buddy->contact.slen < 0)
904 buddy->contact.slen = 0;
905
906 PJSUA_UNLOCK();
907}
908
909
Benny Prijono834aee32006-02-19 01:38:06 +0000910/* Callback called when we receive NOTIFY */
911static void pjsua_evsub_on_rx_notify(pjsip_evsub *sub,
912 pjsip_rx_data *rdata,
913 int *p_st_code,
914 pj_str_t **p_st_text,
915 pjsip_hdr *res_hdr,
916 pjsip_msg_body **p_body)
917{
918 pjsua_buddy *buddy;
919
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000920 PJSUA_LOCK();
921
922 buddy = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
Benny Prijono834aee32006-02-19 01:38:06 +0000923 if (buddy) {
924 /* Update our info. */
925 pjsip_pres_get_status(sub, &buddy->status);
Benny Prijono834aee32006-02-19 01:38:06 +0000926 }
927
928 /* The default is to send 200 response to NOTIFY.
929 * Just leave it there..
930 */
931 PJ_UNUSED_ARG(rdata);
932 PJ_UNUSED_ARG(p_st_code);
933 PJ_UNUSED_ARG(p_st_text);
934 PJ_UNUSED_ARG(res_hdr);
935 PJ_UNUSED_ARG(p_body);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000936
937 PJSUA_UNLOCK();
Benny Prijono834aee32006-02-19 01:38:06 +0000938}
939
940
941/* Event subscription callback. */
942static pjsip_evsub_user pres_callback =
943{
944 &pjsua_evsub_on_state,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000945 &pjsua_evsub_on_tsx_state,
Benny Prijono834aee32006-02-19 01:38:06 +0000946
947 NULL, /* on_rx_refresh: don't care about SUBSCRIBE refresh, unless
948 * we want to authenticate
949 */
950
951 &pjsua_evsub_on_rx_notify,
952
953 NULL, /* on_client_refresh: Use default behaviour, which is to
954 * refresh client subscription. */
955
956 NULL, /* on_server_timeout: Use default behaviour, which is to send
957 * NOTIFY to terminate.
958 */
959};
960
961
962/* It does what it says.. */
963static void subscribe_buddy_presence(unsigned index)
964{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000965 pjsua_buddy *buddy;
966 int acc_id;
967 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000968 pj_str_t contact;
Benny Prijono834aee32006-02-19 01:38:06 +0000969 pjsip_dialog *dlg;
970 pjsip_tx_data *tdata;
971 pj_status_t status;
972
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000973 buddy = &pjsua_var.buddy[index];
974 acc_id = pjsua_acc_find_for_outgoing(&buddy->uri);
Benny Prijono8b1889b2006-06-06 18:40:40 +0000975
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000976 acc = &pjsua_var.acc[acc_id];
Benny Prijonoa91a0032006-02-26 21:23:45 +0000977
Benny Prijonob4a17c92006-07-10 14:40:21 +0000978 PJ_LOG(4,(THIS_FILE, "Using account %d for buddy %d subscription",
979 acc_id, index));
980
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000981 /* Generate suitable Contact header */
982 status = pjsua_acc_create_uac_contact(pjsua_var.pool, &contact,
983 acc_id, &buddy->uri);
984 if (status != PJ_SUCCESS) {
985 pjsua_perror(THIS_FILE, "Unable to generate Contact header", status);
986 return;
987 }
988
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000989 /* Create UAC dialog */
Benny Prijono834aee32006-02-19 01:38:06 +0000990 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000991 &acc->cfg.id,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000992 &contact,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000993 &buddy->uri,
Benny Prijono834aee32006-02-19 01:38:06 +0000994 NULL, &dlg);
995 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000996 pjsua_perror(THIS_FILE, "Unable to create dialog",
997 status);
Benny Prijono834aee32006-02-19 01:38:06 +0000998 return;
999 }
1000
Benny Prijonodc752ca2006-09-22 16:55:42 +00001001 status = pjsip_pres_create_uac( dlg, &pres_callback,
1002 PJSIP_EVSUB_NO_EVENT_ID, &buddy->sub);
1003 if (status != PJ_SUCCESS) {
1004 pjsua_var.buddy[index].sub = NULL;
1005 pjsua_perror(THIS_FILE, "Unable to create presence client",
1006 status);
1007 pjsip_dlg_terminate(dlg);
1008 return;
1009 }
1010
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001011 /* If account is locked to specific transport, then lock dialog
1012 * to this transport too.
1013 */
1014 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
1015 pjsip_tpselector tp_sel;
1016
1017 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
1018 pjsip_dlg_set_transport(dlg, &tp_sel);
1019 }
1020
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001021 /* Set route-set */
1022 if (!pj_list_empty(&acc->route_set)) {
1023 pjsip_dlg_set_route_set(dlg, &acc->route_set);
1024 }
1025
1026 /* Set credentials */
1027 if (acc->cred_cnt) {
Benny Prijonodc39fe82006-05-26 12:17:46 +00001028 pjsip_auth_clt_set_credentials( &dlg->auth_sess,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001029 acc->cred_cnt, acc->cred);
Benny Prijonodc39fe82006-05-26 12:17:46 +00001030 }
Benny Prijono1d8d6082006-04-29 12:38:25 +00001031
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001032 pjsip_evsub_set_mod_data(buddy->sub, pjsua_var.mod.id, buddy);
Benny Prijono834aee32006-02-19 01:38:06 +00001033
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001034 status = pjsip_pres_initiate(buddy->sub, -1, &tdata);
Benny Prijono834aee32006-02-19 01:38:06 +00001035 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001036 pjsip_pres_terminate(buddy->sub, PJ_FALSE);
1037 buddy->sub = NULL;
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001038 pjsua_perror(THIS_FILE, "Unable to create initial SUBSCRIBE",
1039 status);
Benny Prijono834aee32006-02-19 01:38:06 +00001040 return;
1041 }
1042
Benny Prijono21b9ad92006-08-15 13:11:22 +00001043 pjsua_process_msg_data(tdata, NULL);
1044
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001045 status = pjsip_pres_send_request(buddy->sub, tdata);
Benny Prijono834aee32006-02-19 01:38:06 +00001046 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001047 pjsip_pres_terminate(buddy->sub, PJ_FALSE);
1048 buddy->sub = NULL;
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001049 pjsua_perror(THIS_FILE, "Unable to send initial SUBSCRIBE",
1050 status);
Benny Prijono834aee32006-02-19 01:38:06 +00001051 return;
1052 }
Benny Prijono834aee32006-02-19 01:38:06 +00001053}
1054
1055
1056/* It does what it says... */
1057static void unsubscribe_buddy_presence(unsigned index)
1058{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001059 pjsua_buddy *buddy;
Benny Prijono834aee32006-02-19 01:38:06 +00001060 pjsip_tx_data *tdata;
1061 pj_status_t status;
1062
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001063 buddy = &pjsua_var.buddy[index];
1064
1065 if (buddy->sub == NULL)
Benny Prijono834aee32006-02-19 01:38:06 +00001066 return;
1067
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001068 if (pjsip_evsub_get_state(buddy->sub) == PJSIP_EVSUB_STATE_TERMINATED) {
1069 pjsua_var.buddy[index].sub = NULL;
Benny Prijono834aee32006-02-19 01:38:06 +00001070 return;
1071 }
1072
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001073 status = pjsip_pres_initiate( buddy->sub, 0, &tdata);
Benny Prijono21b9ad92006-08-15 13:11:22 +00001074 if (status == PJ_SUCCESS) {
1075 pjsua_process_msg_data(tdata, NULL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001076 status = pjsip_pres_send_request( buddy->sub, tdata );
Benny Prijono21b9ad92006-08-15 13:11:22 +00001077 }
Benny Prijono834aee32006-02-19 01:38:06 +00001078
Benny Prijono1c2bf462006-03-05 11:54:02 +00001079 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001080 pjsip_pres_terminate(buddy->sub, PJ_FALSE);
1081 buddy->sub = NULL;
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001082 pjsua_perror(THIS_FILE, "Unable to unsubscribe presence",
1083 status);
Benny Prijono834aee32006-02-19 01:38:06 +00001084 }
1085}
1086
1087
1088/* It does what it says.. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001089static void refresh_client_subscriptions(void)
Benny Prijono834aee32006-02-19 01:38:06 +00001090{
Benny Prijono9fc735d2006-05-28 14:58:12 +00001091 unsigned i;
Benny Prijono834aee32006-02-19 01:38:06 +00001092
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001093 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
Benny Prijono834aee32006-02-19 01:38:06 +00001094
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001095 if (!pjsua_var.buddy[i].uri.slen)
1096 continue;
1097
1098 if (pjsua_var.buddy[i].monitor && !pjsua_var.buddy[i].sub) {
Benny Prijono834aee32006-02-19 01:38:06 +00001099 subscribe_buddy_presence(i);
1100
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001101 } else if (!pjsua_var.buddy[i].monitor && pjsua_var.buddy[i].sub) {
Benny Prijono834aee32006-02-19 01:38:06 +00001102 unsubscribe_buddy_presence(i);
1103
1104 }
1105 }
1106}
1107
1108
1109/*
1110 * Init presence
1111 */
1112pj_status_t pjsua_pres_init()
1113{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001114 unsigned i;
Benny Prijono834aee32006-02-19 01:38:06 +00001115 pj_status_t status;
1116
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001117 status = pjsip_endpt_register_module( pjsua_var.endpt, &mod_pjsua_pres);
Benny Prijono834aee32006-02-19 01:38:06 +00001118 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +00001119 pjsua_perror(THIS_FILE, "Unable to register pjsua presence module",
1120 status);
Benny Prijono834aee32006-02-19 01:38:06 +00001121 }
1122
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001123 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
1124 reset_buddy(i);
1125 }
1126
Benny Prijono834aee32006-02-19 01:38:06 +00001127 return status;
1128}
1129
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001130
Benny Prijono834aee32006-02-19 01:38:06 +00001131/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001132 * Start presence subsystem.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001133 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001134pj_status_t pjsua_pres_start(void)
Benny Prijono9fc735d2006-05-28 14:58:12 +00001135{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001136 /* Nothing to do (is it?) */
Benny Prijono9fc735d2006-05-28 14:58:12 +00001137 return PJ_SUCCESS;
1138}
1139
1140
1141/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001142 * Refresh presence subscriptions
Benny Prijono834aee32006-02-19 01:38:06 +00001143 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001144void pjsua_pres_refresh()
Benny Prijono834aee32006-02-19 01:38:06 +00001145{
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001146 unsigned i;
1147
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001148 refresh_client_subscriptions();
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001149
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001150 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1151 if (pjsua_var.acc[i].valid)
1152 refresh_server_subscription(i);
1153 }
Benny Prijono834aee32006-02-19 01:38:06 +00001154}
1155
1156
1157/*
1158 * Shutdown presence.
1159 */
1160void pjsua_pres_shutdown(void)
1161{
Benny Prijono9fc735d2006-05-28 14:58:12 +00001162 unsigned i;
Benny Prijono834aee32006-02-19 01:38:06 +00001163
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001164 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1165 if (!pjsua_var.acc[i].valid)
1166 continue;
1167 pjsua_pres_delete_acc(i);
Benny Prijonoa91a0032006-02-26 21:23:45 +00001168 }
1169
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001170 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
1171 pjsua_var.buddy[i].monitor = 0;
Benny Prijono834aee32006-02-19 01:38:06 +00001172 }
Benny Prijonoa91a0032006-02-26 21:23:45 +00001173
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001174 pjsua_pres_refresh();
Benny Prijono834aee32006-02-19 01:38:06 +00001175}