Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 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 Prijono | 4f9f64e | 2006-02-27 00:00:30 +0000 | [diff] [blame] | 19 | #include <pjsua-lib/pjsua.h> |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 20 | #include <pjsua-lib/pjsua_internal.h> |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 21 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 22 | |
| 23 | #define THIS_FILE "pjsua_pres.c" |
| 24 | |
| 25 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 26 | /* |
| 27 | * Get total number of buddies. |
| 28 | */ |
| 29 | PJ_DEF(unsigned) pjsua_get_buddy_count(void) |
| 30 | { |
| 31 | return pjsua_var.buddy_cnt; |
| 32 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 33 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * Check if buddy ID is valid. |
| 37 | */ |
| 38 | PJ_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 | */ |
| 48 | PJ_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 | */ |
| 76 | PJ_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 Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 88 | pj_bzero(info, sizeof(pjsua_buddy_info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 89 | |
| 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 | */ |
| 130 | static void reset_buddy(pjsua_buddy_id id) |
| 131 | { |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 132 | pj_bzero(&pjsua_var.buddy[id], sizeof(pjsua_var.buddy[id])); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 133 | pjsua_var.buddy[id].index = id; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /* |
| 138 | * Add new buddy. |
| 139 | */ |
| 140 | PJ_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 Prijono | fc49359 | 2007-02-18 20:56:32 +0000 | [diff] [blame] | 180 | /* Only support SIP schemes */ |
| 181 | if (!PJSIP_URI_SCHEME_IS_SIP(url) && !PJSIP_URI_SCHEME_IS_SIPS(url)) |
| 182 | return PJSIP_EINVALIDSCHEME; |
| 183 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 184 | /* 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 Prijono | 9c1528f | 2007-02-10 19:22:25 +0000 | [diff] [blame] | 192 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(url->uri); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 193 | 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 | */ |
| 217 | PJ_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 | */ |
| 248 | PJ_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 | */ |
| 272 | PJ_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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 392 | */ |
| 393 | |
| 394 | /* Proto */ |
| 395 | static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata); |
| 396 | |
| 397 | /* The module instance. */ |
| 398 | static 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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 404 | 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. */ |
| 418 | static void pres_evsub_on_srv_state( pjsip_evsub *sub, pjsip_event *event) |
| 419 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 420 | pjsua_srv_pres *uapres; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 421 | |
| 422 | PJ_UNUSED_ARG(event); |
| 423 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 424 | PJSUA_LOCK(); |
| 425 | |
| 426 | uapres = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 427 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 432 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 433 | pj_list_erase(uapres); |
| 434 | } |
| 435 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 436 | |
| 437 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /* This is called when request is received. |
| 441 | * We need to check for incoming SUBSCRIBE request. |
| 442 | */ |
| 443 | static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata) |
| 444 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 445 | int acc_id; |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 446 | pjsua_acc *acc; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 447 | pj_str_t contact; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 448 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 462 | PJSUA_LOCK(); |
| 463 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 464 | /* Find which account for the incoming request. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 465 | acc_id = pjsua_acc_find_for_incoming(rdata); |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 466 | acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 467 | |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 468 | PJ_LOG(4,(THIS_FILE, "Creating server subscription, using account %d", |
| 469 | acc_id)); |
| 470 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 471 | /* 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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 480 | /* Create UAS dialog: */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 481 | status = pjsip_dlg_create_uas(pjsip_ua_instance(), rdata, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 482 | &contact, &dlg); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 483 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 484 | pjsua_perror(THIS_FILE, |
| 485 | "Unable to create UAS dialog for subscription", |
| 486 | status); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 487 | PJSUA_UNLOCK(); |
| 488 | return PJ_TRUE; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Benny Prijono | 6f97941 | 2006-06-15 12:25:46 +0000 | [diff] [blame] | 491 | /* Set credentials. */ |
| 492 | pjsip_auth_clt_set_credentials(&dlg->auth_sess, acc->cred_cnt, acc->cred); |
| 493 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 494 | /* Init callback: */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 495 | pj_bzero(&pres_cb, sizeof(pres_cb)); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 496 | 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 Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 501 | pjsip_dlg_terminate(dlg); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 502 | pjsua_perror(THIS_FILE, "Unable to create server subscription", |
| 503 | status); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 504 | PJSUA_UNLOCK(); |
| 505 | return PJ_TRUE; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 508 | /* 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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 518 | /* 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 529 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, uapres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 530 | |
| 531 | /* Add server subscription to the list: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 532 | pj_list_push_back(&pjsua_var.acc[acc_id].pres_srv_list, uapres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 533 | |
| 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 Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 538 | pjsua_perror(THIS_FILE, "Unable to accept presence subscription", |
| 539 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 540 | pj_list_erase(uapres); |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 541 | pjsip_pres_terminate(sub, PJ_FALSE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 542 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 543 | return PJ_FALSE; |
| 544 | } |
| 545 | |
| 546 | |
| 547 | /* Set our online status: */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 548 | pj_bzero(&pres_status, sizeof(pres_status)); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 549 | pres_status.info_cnt = 1; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 550 | 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 Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 552 | //causing XML parsing to fail. |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 553 | //pres_status.info[0].contact = pjsua_var.local_uri; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 554 | |
| 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 Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 560 | if (status == PJ_SUCCESS) { |
| 561 | pjsua_process_msg_data(tdata, NULL); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 562 | status = pjsip_pres_send_request( sub, tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 563 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 564 | |
| 565 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 566 | pjsua_perror(THIS_FILE, "Unable to create/send NOTIFY", |
| 567 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 568 | pj_list_erase(uapres); |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 569 | pjsip_pres_terminate(sub, PJ_FALSE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 570 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 571 | return PJ_FALSE; |
| 572 | } |
| 573 | |
| 574 | |
| 575 | /* Done: */ |
| 576 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 577 | PJSUA_UNLOCK(); |
| 578 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 579 | return PJ_TRUE; |
| 580 | } |
| 581 | |
| 582 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 583 | /* |
| 584 | * Client presence publication callback. |
| 585 | */ |
| 586 | static 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 | */ |
| 614 | static 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 Prijono | 8c6e884 | 2007-02-24 15:33:54 +0000 | [diff] [blame] | 625 | char *bpos; |
| 626 | pj_str_t entity; |
| 627 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 628 | 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 Prijono | 8c6e884 | 2007-02-24 15:33:54 +0000 | [diff] [blame] | 639 | /* 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 Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 655 | /* Create and add PIDF message body */ |
| 656 | status = pjsip_pres_create_pidf(tdata->pool, &pres_status, |
Benny Prijono | 8c6e884 | 2007-02-24 15:33:54 +0000 | [diff] [blame] | 657 | &entity, &tdata->msg->body); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 658 | 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 | |
| 685 | on_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 Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 693 | pj_status_t pjsua_pres_init_publish_acc(int acc_id) |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 694 | { |
| 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, |
Benny Prijono | 703b7d7 | 2007-03-20 09:13:24 +0000 | [diff] [blame] | 715 | 60); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 716 | if (status != PJ_SUCCESS) { |
| 717 | acc->publish_sess = NULL; |
| 718 | return status; |
| 719 | } |
| 720 | |
Benny Prijono | 703b7d7 | 2007-03-20 09:13:24 +0000 | [diff] [blame] | 721 | /* Add credential for authentication */ |
| 722 | pjsip_publishc_set_credentials(acc->publish_sess, acc->cred_cnt, |
| 723 | acc->cred); |
| 724 | |
| 725 | /* Set route-set */ |
| 726 | pjsip_publishc_set_route_set(acc->publish_sess, &acc->route_set); |
| 727 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 728 | /* Send initial PUBLISH request */ |
| 729 | if (acc->online_status != 0) { |
| 730 | status = send_publish(acc_id, PJ_TRUE); |
| 731 | if (status != PJ_SUCCESS) |
| 732 | return status; |
| 733 | } |
| 734 | |
| 735 | } else { |
| 736 | acc->publish_sess = NULL; |
| 737 | } |
| 738 | |
| 739 | return PJ_SUCCESS; |
| 740 | } |
| 741 | |
| 742 | |
| 743 | /* Init presence for account */ |
| 744 | pj_status_t pjsua_pres_init_acc(int acc_id) |
| 745 | { |
| 746 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 747 | |
| 748 | /* Init presence subscription */ |
| 749 | pj_list_init(&acc->pres_srv_list); |
| 750 | |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 751 | return PJ_SUCCESS; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 755 | /* Terminate server subscription for the account */ |
| 756 | void pjsua_pres_delete_acc(int acc_id) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 757 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 758 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 759 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 760 | pjsua_srv_pres *uapres; |
| 761 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 762 | uapres = pjsua_var.acc[acc_id].pres_srv_list.next; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 763 | |
Benny Prijono | 922933b | 2007-01-21 16:23:56 +0000 | [diff] [blame] | 764 | /* Notify all subscribers that we're no longer available */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 765 | while (uapres != &acc->pres_srv_list) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 766 | |
| 767 | pjsip_pres_status pres_status; |
| 768 | pj_str_t reason = { "noresource", 10 }; |
| 769 | pjsip_tx_data *tdata; |
| 770 | |
| 771 | pjsip_pres_get_status(uapres->sub, &pres_status); |
| 772 | |
| 773 | pres_status.info[0].basic_open = pjsua_var.acc[acc_id].online_status; |
| 774 | pjsip_pres_set_status(uapres->sub, &pres_status); |
| 775 | |
| 776 | if (pjsip_pres_notify(uapres->sub, |
| 777 | PJSIP_EVSUB_STATE_TERMINATED, NULL, |
| 778 | &reason, &tdata)==PJ_SUCCESS) |
| 779 | { |
| 780 | pjsip_pres_send_request(uapres->sub, tdata); |
| 781 | } |
| 782 | |
| 783 | uapres = uapres->next; |
| 784 | } |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 785 | |
Benny Prijono | 922933b | 2007-01-21 16:23:56 +0000 | [diff] [blame] | 786 | /* Clear server presence subscription list because account might be reused |
| 787 | * later. */ |
| 788 | pj_list_init(&acc->pres_srv_list); |
| 789 | |
| 790 | /* Terminate presence publication, if any */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 791 | if (acc->publish_sess) { |
| 792 | acc->online_status = PJ_FALSE; |
| 793 | send_publish(acc_id, PJ_FALSE); |
| 794 | if (acc->publish_sess) { |
| 795 | pjsip_publishc_destroy(acc->publish_sess); |
| 796 | acc->publish_sess = NULL; |
| 797 | } |
| 798 | acc_cfg->publish_enabled = PJ_FALSE; |
| 799 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | |
| 803 | /* Refresh subscription (e.g. when our online status has changed) */ |
| 804 | static void refresh_server_subscription(int acc_id) |
| 805 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 806 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
| 807 | pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 808 | pjsua_srv_pres *uapres; |
| 809 | |
| 810 | uapres = pjsua_var.acc[acc_id].pres_srv_list.next; |
| 811 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 812 | while (uapres != &acc->pres_srv_list) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 813 | |
| 814 | pjsip_pres_status pres_status; |
| 815 | pjsip_tx_data *tdata; |
| 816 | |
| 817 | pjsip_pres_get_status(uapres->sub, &pres_status); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 818 | if (pres_status.info[0].basic_open != acc->online_status) { |
| 819 | pres_status.info[0].basic_open = acc->online_status; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 820 | pjsip_pres_set_status(uapres->sub, &pres_status); |
| 821 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 822 | if (pjsip_pres_current_notify(uapres->sub, &tdata)==PJ_SUCCESS) { |
| 823 | pjsua_process_msg_data(tdata, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 824 | pjsip_pres_send_request(uapres->sub, tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 825 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | uapres = uapres->next; |
| 829 | } |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 830 | |
Benny Prijono | 8b6834f | 2007-02-24 13:29:22 +0000 | [diff] [blame] | 831 | /* Send PUBLISH if required. We only do this when we have a PUBLISH |
| 832 | * session. If we don't have a PUBLISH session, then it could be |
| 833 | * that we're waiting until registration has completed before we |
| 834 | * send the first PUBLISH. |
| 835 | */ |
| 836 | if (acc_cfg->publish_enabled && acc->publish_sess) { |
| 837 | if (acc->publish_state != acc->online_status) { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 838 | send_publish(acc_id, PJ_TRUE); |
| 839 | } |
| 840 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | |
| 844 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 845 | /*************************************************************************** |
| 846 | * Client subscription. |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 847 | */ |
| 848 | |
| 849 | /* Callback called when *client* subscription state has changed. */ |
| 850 | static void pjsua_evsub_on_state( pjsip_evsub *sub, pjsip_event *event) |
| 851 | { |
| 852 | pjsua_buddy *buddy; |
| 853 | |
| 854 | PJ_UNUSED_ARG(event); |
| 855 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 856 | PJSUA_LOCK(); |
| 857 | |
| 858 | buddy = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 859 | if (buddy) { |
| 860 | PJ_LOG(3,(THIS_FILE, |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 861 | "Presence subscription to %.*s is %s", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 862 | (int)pjsua_var.buddy[buddy->index].uri.slen, |
| 863 | pjsua_var.buddy[buddy->index].uri.ptr, |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 864 | pjsip_evsub_get_state_name(sub))); |
| 865 | |
| 866 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
| 867 | buddy->sub = NULL; |
| 868 | buddy->status.info_cnt = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 869 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 870 | } |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 871 | |
| 872 | /* Call callback */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 873 | if (pjsua_var.ua_cfg.cb.on_buddy_state) |
| 874 | (*pjsua_var.ua_cfg.cb.on_buddy_state)(buddy->index); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 875 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 876 | |
| 877 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 880 | |
| 881 | /* Callback when transaction state has changed. */ |
| 882 | static void pjsua_evsub_on_tsx_state(pjsip_evsub *sub, |
| 883 | pjsip_transaction *tsx, |
| 884 | pjsip_event *event) |
| 885 | { |
| 886 | pjsua_buddy *buddy; |
| 887 | pjsip_contact_hdr *contact_hdr; |
| 888 | |
| 889 | PJSUA_LOCK(); |
| 890 | |
| 891 | buddy = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
| 892 | if (!buddy) { |
| 893 | PJSUA_UNLOCK(); |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | /* We only use this to update buddy's Contact, when it's not |
| 898 | * set. |
| 899 | */ |
| 900 | if (buddy->contact.slen != 0) { |
| 901 | /* Contact already set */ |
| 902 | PJSUA_UNLOCK(); |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | /* Only care about 2xx response to outgoing SUBSCRIBE */ |
| 907 | if (tsx->status_code/100 != 2 || |
| 908 | tsx->role != PJSIP_UAC_ROLE || |
| 909 | event->type != PJSIP_EVENT_RX_MSG || |
| 910 | pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method)!=0) |
| 911 | { |
| 912 | PJSUA_UNLOCK(); |
| 913 | return; |
| 914 | } |
| 915 | |
| 916 | /* Find contact header. */ |
| 917 | contact_hdr = pjsip_msg_find_hdr(event->body.rx_msg.rdata->msg_info.msg, |
| 918 | PJSIP_H_CONTACT, NULL); |
| 919 | if (!contact_hdr) { |
| 920 | PJSUA_UNLOCK(); |
| 921 | return; |
| 922 | } |
| 923 | |
| 924 | buddy->contact.ptr = pj_pool_alloc(pjsua_var.pool, PJSIP_MAX_URL_SIZE); |
| 925 | buddy->contact.slen = pjsip_uri_print( PJSIP_URI_IN_CONTACT_HDR, |
| 926 | contact_hdr->uri, |
| 927 | buddy->contact.ptr, |
| 928 | PJSIP_MAX_URL_SIZE); |
| 929 | if (buddy->contact.slen < 0) |
| 930 | buddy->contact.slen = 0; |
| 931 | |
| 932 | PJSUA_UNLOCK(); |
| 933 | } |
| 934 | |
| 935 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 936 | /* Callback called when we receive NOTIFY */ |
| 937 | static void pjsua_evsub_on_rx_notify(pjsip_evsub *sub, |
| 938 | pjsip_rx_data *rdata, |
| 939 | int *p_st_code, |
| 940 | pj_str_t **p_st_text, |
| 941 | pjsip_hdr *res_hdr, |
| 942 | pjsip_msg_body **p_body) |
| 943 | { |
| 944 | pjsua_buddy *buddy; |
| 945 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 946 | PJSUA_LOCK(); |
| 947 | |
| 948 | buddy = pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 949 | if (buddy) { |
| 950 | /* Update our info. */ |
| 951 | pjsip_pres_get_status(sub, &buddy->status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* The default is to send 200 response to NOTIFY. |
| 955 | * Just leave it there.. |
| 956 | */ |
| 957 | PJ_UNUSED_ARG(rdata); |
| 958 | PJ_UNUSED_ARG(p_st_code); |
| 959 | PJ_UNUSED_ARG(p_st_text); |
| 960 | PJ_UNUSED_ARG(res_hdr); |
| 961 | PJ_UNUSED_ARG(p_body); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 962 | |
| 963 | PJSUA_UNLOCK(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | |
| 967 | /* Event subscription callback. */ |
| 968 | static pjsip_evsub_user pres_callback = |
| 969 | { |
| 970 | &pjsua_evsub_on_state, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 971 | &pjsua_evsub_on_tsx_state, |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 972 | |
| 973 | NULL, /* on_rx_refresh: don't care about SUBSCRIBE refresh, unless |
| 974 | * we want to authenticate |
| 975 | */ |
| 976 | |
| 977 | &pjsua_evsub_on_rx_notify, |
| 978 | |
| 979 | NULL, /* on_client_refresh: Use default behaviour, which is to |
| 980 | * refresh client subscription. */ |
| 981 | |
| 982 | NULL, /* on_server_timeout: Use default behaviour, which is to send |
| 983 | * NOTIFY to terminate. |
| 984 | */ |
| 985 | }; |
| 986 | |
| 987 | |
| 988 | /* It does what it says.. */ |
| 989 | static void subscribe_buddy_presence(unsigned index) |
| 990 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 991 | pjsua_buddy *buddy; |
| 992 | int acc_id; |
| 993 | pjsua_acc *acc; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 994 | pj_str_t contact; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 995 | pjsip_dialog *dlg; |
| 996 | pjsip_tx_data *tdata; |
| 997 | pj_status_t status; |
| 998 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 999 | buddy = &pjsua_var.buddy[index]; |
| 1000 | acc_id = pjsua_acc_find_for_outgoing(&buddy->uri); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1001 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1002 | acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1003 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 1004 | PJ_LOG(4,(THIS_FILE, "Using account %d for buddy %d subscription", |
| 1005 | acc_id, index)); |
| 1006 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1007 | /* Generate suitable Contact header */ |
| 1008 | status = pjsua_acc_create_uac_contact(pjsua_var.pool, &contact, |
| 1009 | acc_id, &buddy->uri); |
| 1010 | if (status != PJ_SUCCESS) { |
| 1011 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", status); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1015 | /* Create UAC dialog */ |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1016 | status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1017 | &acc->cfg.id, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1018 | &contact, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1019 | &buddy->uri, |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1020 | NULL, &dlg); |
| 1021 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1022 | pjsua_perror(THIS_FILE, "Unable to create dialog", |
| 1023 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1024 | return; |
| 1025 | } |
| 1026 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1027 | status = pjsip_pres_create_uac( dlg, &pres_callback, |
| 1028 | PJSIP_EVSUB_NO_EVENT_ID, &buddy->sub); |
| 1029 | if (status != PJ_SUCCESS) { |
| 1030 | pjsua_var.buddy[index].sub = NULL; |
| 1031 | pjsua_perror(THIS_FILE, "Unable to create presence client", |
| 1032 | status); |
| 1033 | pjsip_dlg_terminate(dlg); |
| 1034 | return; |
| 1035 | } |
| 1036 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1037 | /* If account is locked to specific transport, then lock dialog |
| 1038 | * to this transport too. |
| 1039 | */ |
| 1040 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
| 1041 | pjsip_tpselector tp_sel; |
| 1042 | |
| 1043 | pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); |
| 1044 | pjsip_dlg_set_transport(dlg, &tp_sel); |
| 1045 | } |
| 1046 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1047 | /* Set route-set */ |
| 1048 | if (!pj_list_empty(&acc->route_set)) { |
| 1049 | pjsip_dlg_set_route_set(dlg, &acc->route_set); |
| 1050 | } |
| 1051 | |
| 1052 | /* Set credentials */ |
| 1053 | if (acc->cred_cnt) { |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1054 | pjsip_auth_clt_set_credentials( &dlg->auth_sess, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1055 | acc->cred_cnt, acc->cred); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1056 | } |
Benny Prijono | 1d8d608 | 2006-04-29 12:38:25 +0000 | [diff] [blame] | 1057 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1058 | pjsip_evsub_set_mod_data(buddy->sub, pjsua_var.mod.id, buddy); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1059 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1060 | status = pjsip_pres_initiate(buddy->sub, -1, &tdata); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1061 | if (status != PJ_SUCCESS) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1062 | pjsip_pres_terminate(buddy->sub, PJ_FALSE); |
| 1063 | buddy->sub = NULL; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1064 | pjsua_perror(THIS_FILE, "Unable to create initial SUBSCRIBE", |
| 1065 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1066 | return; |
| 1067 | } |
| 1068 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1069 | pjsua_process_msg_data(tdata, NULL); |
| 1070 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1071 | status = pjsip_pres_send_request(buddy->sub, tdata); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1072 | if (status != PJ_SUCCESS) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1073 | pjsip_pres_terminate(buddy->sub, PJ_FALSE); |
| 1074 | buddy->sub = NULL; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1075 | pjsua_perror(THIS_FILE, "Unable to send initial SUBSCRIBE", |
| 1076 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1077 | return; |
| 1078 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | |
| 1082 | /* It does what it says... */ |
| 1083 | static void unsubscribe_buddy_presence(unsigned index) |
| 1084 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1085 | pjsua_buddy *buddy; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1086 | pjsip_tx_data *tdata; |
| 1087 | pj_status_t status; |
| 1088 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1089 | buddy = &pjsua_var.buddy[index]; |
| 1090 | |
| 1091 | if (buddy->sub == NULL) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1092 | return; |
| 1093 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1094 | if (pjsip_evsub_get_state(buddy->sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
| 1095 | pjsua_var.buddy[index].sub = NULL; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1096 | return; |
| 1097 | } |
| 1098 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1099 | status = pjsip_pres_initiate( buddy->sub, 0, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1100 | if (status == PJ_SUCCESS) { |
| 1101 | pjsua_process_msg_data(tdata, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1102 | status = pjsip_pres_send_request( buddy->sub, tdata ); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1103 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1104 | |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 1105 | if (status != PJ_SUCCESS) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1106 | pjsip_pres_terminate(buddy->sub, PJ_FALSE); |
| 1107 | buddy->sub = NULL; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1108 | pjsua_perror(THIS_FILE, "Unable to unsubscribe presence", |
| 1109 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | |
| 1114 | /* It does what it says.. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1115 | static void refresh_client_subscriptions(void) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1116 | { |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1117 | unsigned i; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1118 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1119 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1120 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1121 | if (!pjsua_var.buddy[i].uri.slen) |
| 1122 | continue; |
| 1123 | |
| 1124 | if (pjsua_var.buddy[i].monitor && !pjsua_var.buddy[i].sub) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1125 | subscribe_buddy_presence(i); |
| 1126 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1127 | } else if (!pjsua_var.buddy[i].monitor && pjsua_var.buddy[i].sub) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1128 | unsubscribe_buddy_presence(i); |
| 1129 | |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | |
| 1135 | /* |
| 1136 | * Init presence |
| 1137 | */ |
| 1138 | pj_status_t pjsua_pres_init() |
| 1139 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1140 | unsigned i; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1141 | pj_status_t status; |
| 1142 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1143 | status = pjsip_endpt_register_module( pjsua_var.endpt, &mod_pjsua_pres); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1144 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1145 | pjsua_perror(THIS_FILE, "Unable to register pjsua presence module", |
| 1146 | status); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1149 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 1150 | reset_buddy(i); |
| 1151 | } |
| 1152 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1153 | return status; |
| 1154 | } |
| 1155 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1156 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1157 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1158 | * Start presence subsystem. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1159 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1160 | pj_status_t pjsua_pres_start(void) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1161 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1162 | /* Nothing to do (is it?) */ |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1163 | return PJ_SUCCESS; |
| 1164 | } |
| 1165 | |
| 1166 | |
| 1167 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1168 | * Refresh presence subscriptions |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1169 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1170 | void pjsua_pres_refresh() |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1171 | { |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 1172 | unsigned i; |
| 1173 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1174 | refresh_client_subscriptions(); |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 1175 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1176 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 1177 | if (pjsua_var.acc[i].valid) |
| 1178 | refresh_server_subscription(i); |
| 1179 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | /* |
| 1184 | * Shutdown presence. |
| 1185 | */ |
| 1186 | void pjsua_pres_shutdown(void) |
| 1187 | { |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1188 | unsigned i; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1189 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1190 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 1191 | if (!pjsua_var.acc[i].valid) |
| 1192 | continue; |
| 1193 | pjsua_pres_delete_acc(i); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1196 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) { |
| 1197 | pjsua_var.buddy[i].monitor = 0; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1198 | } |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1199 | |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 1200 | pjsua_pres_refresh(); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1201 | } |