Debian Codegraph @

Xpjproject/pjsip/src/pjsip-simple/presence.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 */
19 #include <pjsip-simple/presence.h>
20 #include <pjsip-simple/errno.h>
21 #include <pjsip-simple/evsub_msg.h>
22 #include <pjsip/sip_module.h>
23 #include <pjsip/sip_multipart.h>
24 #include <pjsip/sip_endpoint.h>
25 #include <pjsip/sip_dialog.h>
26 #include <pj/assert.h>
27 #include <pj/guid.h>
28 #include <pj/log.h>
29 #include <pj/os.h>
30 #include <pj/pool.h>
31 #include <pj/string.h>
32
33
34 #define THIS_FILE "presence.c"
35 #define PRES_DEFAULT_EXPIRES PJSIP_PRES_DEFAULT_EXPIRES
36
37 #if PJSIP_PRES_BAD_CONTENT_RESPONSE < 200 || \
38 PJSIP_PRES_BAD_CONTENT_RESPONSE > 699 || \
39 PJSIP_PRES_BAD_CONTENT_RESPONSE/100 == 3
40 # error Invalid PJSIP_PRES_BAD_CONTENT_RESPONSE value
41 #endif
42
43 /*
44 * Presence module (mod-presence)
45 */
46 static struct pjsip_module mod_presence =
47 {
48 NULL, NULL, /* prev, next. */
49 { "mod-presence", 12 }, /* Name. */
50 -1, /* Id */
51 PJSIP_MOD_PRIORITY_DIALOG_USAGE,/* Priority */
52 NULL, /* load() */
53 NULL, /* start() */
54 NULL, /* stop() */
55 NULL, /* unload() */
56 NULL, /* on_rx_request() */
57 NULL, /* on_rx_response() */
58 NULL, /* on_tx_request. */
59 NULL, /* on_tx_response() */
60 NULL, /* on_tsx_state() */
61 };
62
63
64 /*
65 * Presence message body type.
66 */
67 typedef enum content_type_e
68 {
69 CONTENT_TYPE_NONE,
70 CONTENT_TYPE_PIDF,
71 CONTENT_TYPE_XPIDF,
72 } content_type_e;
73
74 /*
75 * This structure describe a presentity, for both subscriber and notifier.
76 */
77 struct pjsip_pres
78 {
79 pjsip_evsub *sub; /**< Event subscribtion record. */
80 pjsip_dialog *dlg; /**< The dialog. */
81 content_type_e content_type; /**< Content-Type. */
82 pj_pool_t *status_pool; /**< Pool for pres_status */
83 pjsip_pres_status status; /**< Presence status. */
84 pj_pool_t *tmp_pool; /**< Pool for tmp_status */
85 pjsip_pres_status tmp_status; /**< Temp, before NOTIFY is answred.*/
86 pj_bool_t is_ts_valid; /**< Is tmp_status valid? */
87 pjsip_evsub_user user_cb; /**< The user callback. */
88 pj_mutex_t *mutex; /**< Mutex. */
89 };
90
91
92 typedef struct pjsip_pres pjsip_pres;
93
94
95 /*
96 * Forward decl for evsub callback.
97 */
98 static void pres_on_evsub_state( pjsip_evsub *sub, pjsip_event *event);
99 static void pres_on_evsub_tsx_state( pjsip_evsub *sub, pjsip_transaction *tsx,
100 pjsip_event *event);
101 static void pres_on_evsub_rx_refresh( pjsip_evsub *sub,
102 pjsip_rx_data *rdata,
103 int *p_st_code,
104 pj_str_t **p_st_text,
105 pjsip_hdr *res_hdr,
106 pjsip_msg_body **p_body);
107 static void pres_on_evsub_rx_notify( pjsip_evsub *sub,
108 pjsip_rx_data *rdata,
109 int *p_st_code,
110 pj_str_t **p_st_text,
111 pjsip_hdr *res_hdr,
112 pjsip_msg_body **p_body);
113 static void pres_on_evsub_client_refresh(pjsip_evsub *sub);
114 static void pres_on_evsub_server_timeout(pjsip_evsub *sub);
115
116
117 /*
118 * Event subscription callback for presence.
119 */
120 static pjsip_evsub_user pres_user =
121 {
122 &pres_on_evsub_state,
123 &pres_on_evsub_tsx_state,
124 &pres_on_evsub_rx_refresh,
125 &pres_on_evsub_rx_notify,
126 &pres_on_evsub_client_refresh,
127 &pres_on_evsub_server_timeout,
128 };
129
130
131 /*
132 * Some static constants.
133 */
134 static const pj_str_t STR_EVENT = { "Event", 5 };
135 static const pj_str_t STR_PRESENCE = { "presence", 8 };
136 static const pj_str_t STR_APPLICATION = { "application", 11 };
137 static const pj_str_t STR_PIDF_XML = { "pidf+xml", 8};
138 static const pj_str_t STR_XPIDF_XML = { "xpidf+xml", 9};
139 static const pj_str_t STR_APP_PIDF_XML = { "application/pidf+xml", 20 };
140 static const pj_str_t STR_APP_XPIDF_XML = { "application/xpidf+xml", 21 };
141
142
143 /*
144 * Init presence module.
145 */
146 PJ_DEF(pj_status_t) pjsip_pres_init_module( pjsip_endpoint *endpt,
147 pjsip_module *mod_evsub)
148 {
149 pj_status_t status;
150 pj_str_t accept[2];
151
152 /* Check arguments. */
153 PJ_ASSERT_RETURN(endpt && mod_evsub, PJ_EINVAL);
154
155 /* Must have not been registered */
156 PJ_ASSERT_RETURN(mod_presence.id == -1, PJ_EINVALIDOP);
157
158 /* Register to endpoint */
159 status = pjsip_endpt_register_module(endpt, &mod_presence);
160 if (status != PJ_SUCCESS)
161 return status;
162
163 accept[0] = STR_APP_PIDF_XML;
164 accept[1] = STR_APP_XPIDF_XML;
165
166 /* Register event package to event module. */
167 status = pjsip_evsub_register_pkg( &mod_presence, &STR_PRESENCE,
168 PRES_DEFAULT_EXPIRES,
169 PJ_ARRAY_SIZE(accept), accept);
170 if (status != PJ_SUCCESS) {
171 pjsip_endpt_unregister_module(endpt, &mod_presence);
172 return status;
173 }
174
175 return PJ_SUCCESS;
176 }
177
178
179 /*
180 * Get presence module instance.
181 */
182 PJ_DEF(pjsip_module*) pjsip_pres_instance(void)
183 {
184 return &mod_presence;
185 }
186
187
188 /*
189 * Create client subscription.
190 */
191 PJ_DEF(pj_status_t) pjsip_pres_create_uac( pjsip_dialog *dlg,
192 const pjsip_evsub_user *user_cb,
193 unsigned options,
194 pjsip_evsub **p_evsub )
195 {
196 pj_status_t status;
197 pjsip_pres *pres;
198 char obj_name[PJ_MAX_OBJ_NAME];
199 pjsip_evsub *sub;
200
201 PJ_ASSERT_RETURN(dlg && p_evsub, PJ_EINVAL);
202
203 pjsip_dlg_inc_lock(dlg);
204
205 /* Create event subscription */
206 status = pjsip_evsub_create_uac( dlg, &pres_user, &STR_PRESENCE,
207 options, &sub);
208 if (status != PJ_SUCCESS)
209 goto on_return;
210
211 /* Create presence */
212 pres = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_pres);
213 pres->dlg = dlg;
214 pres->sub = sub;
215 if (user_cb)
216 pj_memcpy(&pres->user_cb, user_cb, sizeof(pjsip_evsub_user));
217
218 status = pj_mutex_create_recursive(dlg->pool, "pres_mutex",
219 &pres->mutex);
220 if (status != PJ_SUCCESS)
221 goto on_return;
222
223 pj_ansi_snprintf(obj_name, PJ_MAX_OBJ_NAME, "pres%p", dlg->pool);
224 pres->status_pool = pj_pool_create(dlg->pool->factory, obj_name,
225 512, 512, NULL);
226 pj_ansi_snprintf(obj_name, PJ_MAX_OBJ_NAME, "tmpres%p", dlg->pool);
227 pres->tmp_pool = pj_pool_create(dlg->pool->factory, obj_name,
228 512, 512, NULL);
229
230 /* Attach to evsub */
231 pjsip_evsub_set_mod_data(sub, mod_presence.id, pres);
232
233 *p_evsub = sub;
234
235 on_return:
236 pjsip_dlg_dec_lock(dlg);
237 return status;
238 }
239
240
241 /*
242 * Create server subscription.
243 */
244 PJ_DEF(pj_status_t) pjsip_pres_create_uas( pjsip_dialog *dlg,
245 const pjsip_evsub_user *user_cb,
246 pjsip_rx_data *rdata,
247 pjsip_evsub **p_evsub )
248 {
249 pjsip_accept_hdr *accept;
250 pjsip_event_hdr *event;
251 content_type_e content_type = CONTENT_TYPE_NONE;
252 pjsip_evsub *sub;
253 pjsip_pres *pres;
254 char obj_name[PJ_MAX_OBJ_NAME];
255 pj_status_t status;
256
257 /* Check arguments */
258 PJ_ASSERT_RETURN(dlg && rdata && p_evsub, PJ_EINVAL);
259
260 /* Must be request message */
261 PJ_ASSERT_RETURN(rdata->msg_info.msg->type == PJSIP_REQUEST_MSG,
262 PJSIP_ENOTREQUESTMSG);
263
264 /* Check that request is SUBSCRIBE */
265 PJ_ASSERT_RETURN(pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
266 &pjsip_subscribe_method)==0,
267 PJSIP_SIMPLE_ENOTSUBSCRIBE);
268
269 /* Check that Event header contains "presence" */
270 event = (pjsip_event_hdr*)
271 pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &STR_EVENT, NULL);
272 if (!event) {
273 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_BAD_REQUEST);
274 }
275 if (pj_stricmp(&event->event_type, &STR_PRESENCE) != 0) {
276 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_BAD_EVENT);
277 }
278
279 /* Check that request contains compatible Accept header. */
280 accept = (pjsip_accept_hdr*)
281 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, NULL);
282 if (accept) {
283 unsigned i;
284 for (i=0; i<accept->count; ++i) {
285 if (pj_stricmp(&accept->values[i], &STR_APP_PIDF_XML)==0) {
286 content_type = CONTENT_TYPE_PIDF;
287 break;
288 } else
289 if (pj_stricmp(&accept->values[i], &STR_APP_XPIDF_XML)==0) {
290 content_type = CONTENT_TYPE_XPIDF;
291 break;
292 }
293 }
294
295 if (i==accept->count) {
296 /* Nothing is acceptable */
297 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE);
298 }
299
300 } else {
301 /* No Accept header.
302 * Treat as "application/pidf+xml"
303 */
304 content_type = CONTENT_TYPE_PIDF;
305 }
306
307 /* Lock dialog */
308 pjsip_dlg_inc_lock(dlg);
309
310
311 /* Create server subscription */
312 status = pjsip_evsub_create_uas( dlg, &pres_user, rdata, 0, &sub);
313 if (status != PJ_SUCCESS)
314 goto on_return;
315
316 /* Create server presence subscription */
317 pres = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_pres);
318 pres->dlg = dlg;
319 pres->sub = sub;
320 pres->content_type = content_type;
321 if (user_cb)
322 pj_memcpy(&pres->user_cb, user_cb, sizeof(pjsip_evsub_user));
323
324 pj_ansi_snprintf(obj_name, PJ_MAX_OBJ_NAME, "pres%p", dlg->pool);
325 pres->status_pool = pj_pool_create(dlg->pool->factory, obj_name,
326 512, 512, NULL);
327 pj_ansi_snprintf(obj_name, PJ_MAX_OBJ_NAME, "tmpres%p", dlg->pool);
328 pres->tmp_pool = pj_pool_create(dlg->pool->factory, obj_name,
329 512, 512, NULL);
330
331 /* Attach to evsub */
332 pjsip_evsub_set_mod_data(sub, mod_presence.id, pres);
333
334 /* Done: */
335 *p_evsub = sub;
336
337 on_return:
338 pjsip_dlg_dec_lock(dlg);
339 return status;
340 }
341
342
343 /*
344 * Forcefully terminate presence.
345 */
346 PJ_DEF(pj_status_t) pjsip_pres_terminate( pjsip_evsub *sub,
347 pj_bool_t notify )
348 {
349 return pjsip_evsub_terminate(sub, notify);
350 }
351
352 /*
353 * Create SUBSCRIBE
354 */
355 PJ_DEF(pj_status_t) pjsip_pres_initiate( pjsip_evsub *sub,
356 pj_uint32_t expires,
357 pjsip_tx_data **p_tdata)
358 {
359 return pjsip_evsub_initiate(sub, &pjsip_subscribe_method, expires,
360 p_tdata);
361 }
362
363
364 /*
365 * Add custom headers.
366 */
367 PJ_DEF(pj_status_t) pjsip_pres_add_header( pjsip_evsub *sub,
368 const pjsip_hdr *hdr_list )
369 {
370 return pjsip_evsub_add_header( sub, hdr_list );
371 }
372
373
374 /*
375 * Accept incoming subscription.
376 */
377 PJ_DEF(pj_status_t) pjsip_pres_accept( pjsip_evsub *sub,
378 pjsip_rx_data *rdata,
379 int st_code,
380 const pjsip_hdr *hdr_list )
381 {
382 return pjsip_evsub_accept( sub, rdata, st_code, hdr_list );
383 }
384
385
386 /*
387 * Get presence status.
388 */
389 PJ_DEF(pj_status_t) pjsip_pres_get_status( pjsip_evsub *sub,
390 pjsip_pres_status *status )
391 {
392 pjsip_pres *pres;
393
394 PJ_ASSERT_RETURN(sub && status, PJ_EINVAL);
395
396 pres = (pjsip_pres*) pjsip_evsub_get_mod_data(sub, mod_presence.id);
397 PJ_ASSERT_RETURN(pres!=NULL, PJSIP_SIMPLE_ENOPRESENCE);
398
399 if (pres->mutex)
400 pj_mutex_lock(pres->mutex);
401
402 if (pres->is_ts_valid) {
403 PJ_ASSERT_ON_FAIL(pres->tmp_pool!=NULL,
404 {if (pres->mutex) pj_mutex_unlock(pres->mutex);
405 return PJSIP_SIMPLE_ENOPRESENCE;});
406 pj_memcpy(status, &pres->tmp_status, sizeof(pjsip_pres_status));
407 } else {
408 PJ_ASSERT_ON_FAIL(pres->status_pool!=NULL,
409 {if (pres->mutex) pj_mutex_unlock(pres->mutex);
410 return PJSIP_SIMPLE_ENOPRESENCE;});
411 pj_memcpy(status, &pres->status, sizeof(pjsip_pres_status));
412 }
413
414 if (pres->mutex)
415 pj_mutex_unlock(pres->mutex);
416
417 return PJ_SUCCESS;
418 }
419
420
421 /*
422 * Set presence status.
423 */
424 PJ_DEF(pj_status_t) pjsip_pres_set_status( pjsip_evsub *sub,
425 const pjsip_pres_status *status )
426 {
427 unsigned i;
428 pj_pool_t *tmp;
429 pjsip_pres *pres;
430
431 PJ_ASSERT_RETURN(sub && status, PJ_EINVAL);
432
433 pres = (pjsip_pres*) pjsip_evsub_get_mod_data(sub, mod_presence.id);
434 PJ_ASSERT_RETURN(pres!=NULL, PJSIP_SIMPLE_ENOPRESENCE);
435
436 for (i=0; i<status->info_cnt; ++i) {
437 pres->status.info[i].basic_open = status->info[i].basic_open;
438 if (pres->status.info[i].id.slen) {
439 /* Id already set */
440 } else if (status->info[i].id.slen == 0) {
441 pj_create_unique_string(pres->dlg->pool,
442 &pres->status.info[i].id);
443 } else {
444 pj_strdup(pres->dlg->pool,
445 &pres->status.info[i].id,
446 &status->info[i].id);
447 }
448 pj_strdup(pres->tmp_pool,
449 &pres->status.info[i].contact,
450 &status->info[i].contact);
451
452 /* Duplicate <person> */
453 pres->status.info[i].rpid.activity =
454 status->info[i].rpid.activity;
455 pj_strdup(pres->tmp_pool,
456 &pres->status.info[i].rpid.id,
457 &status->info[i].rpid.id);
458 pj_strdup(pres->tmp_pool,
459 &pres->status.info[i].rpid.note,
460 &status->info[i].rpid.note);
461
462 }
463
464 pres->status.info_cnt = status->info_cnt;
465
466 /* Swap pools */
467 tmp = pres->tmp_pool;
468 pres->tmp_pool = pres->status_pool;
469 pres->status_pool = tmp;
470 pj_pool_reset(pres->tmp_pool);
471
472 return PJ_SUCCESS;
473 }
474
475
476 /*
477 * Create message body.
478 */
479 static pj_status_t pres_create_msg_body( pjsip_pres *pres,
480 pjsip_tx_data *tdata)
481 {
482 pj_str_t entity;
483
484 /* Get publisher URI */
485 entity.ptr = (char*) pj_pool_alloc(tdata->pool, PJSIP_MAX_URL_SIZE);
486 entity.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
487 pres->dlg->local.info->uri,
488 entity.ptr, PJSIP_MAX_URL_SIZE);
489 if (entity.slen < 1)
490 return PJ_ENOMEM;
491
492 if (pres->content_type == CONTENT_TYPE_PIDF) {
493
494 return pjsip_pres_create_pidf(tdata->pool, &pres->status,
495 &entity, &tdata->msg->body);
496
497 } else if (pres->content_type == CONTENT_TYPE_XPIDF) {
498
499 return pjsip_pres_create_xpidf(tdata->pool, &pres->status,
500 &entity, &tdata->msg->body);
501
502 } else {
503 return PJSIP_SIMPLE_EBADCONTENT;
504 }
505 }
506
507
508 /*
509 * Create NOTIFY
510 */
511 PJ_DEF(pj_status_t) pjsip_pres_notify( pjsip_evsub *sub,
512 pjsip_evsub_state state,
513 const pj_str_t *state_str,
514 const pj_str_t *reason,
515 pjsip_tx_data **p_tdata)
516 {
517 pjsip_pres *pres;
518 pjsip_tx_data *tdata;
519 pj_status_t status;
520
521 /* Check arguments. */
522 PJ_ASSERT_RETURN(sub, PJ_EINVAL);
523
524 /* Get the presence object. */
525 pres = (pjsip_pres*) pjsip_evsub_get_mod_data(sub, mod_presence.id);
526 PJ_ASSERT_RETURN(pres != NULL, PJSIP_SIMPLE_ENOPRESENCE);
527
528 /* Must have at least one presence info, unless state is
529 * PJSIP_EVSUB_STATE_TERMINATED. This could happen if subscription
530 * has not been active (e.g. we're waiting for user authorization)
531 * and remote cancels the subscription.
532 */
533 PJ_ASSERT_RETURN(state==PJSIP_EVSUB_STATE_TERMINATED ||
534 pres->status.info_cnt > 0, PJSIP_SIMPLE_ENOPRESENCEINFO);
535
536
537 /* Lock object. */
538 pjsip_dlg_inc_lock(pres->dlg);
539
540 /* Create the NOTIFY request. */
541 status = pjsip_evsub_notify( sub, state, state_str, reason, &tdata);
542 if (status != PJ_SUCCESS)
543 goto on_return;
544
545
546 /* Create message body to reflect the presence status.
547 * Only do this if we have presence status info to send (see above).
548 */
549 if (pres->status.info_cnt > 0) {
550 status = pres_create_msg_body( pres, tdata );
551 if (status != PJ_SUCCESS)
552 goto on_return;
553 }
554
555 /* Done. */
556 *p_tdata = tdata;
557
558
559 on_return:
560 pjsip_dlg_dec_lock(pres->dlg);
561 return status;
562 }
563
564
565 /*
566 * Create NOTIFY that reflect current state.
567 */
568 PJ_DEF(pj_status_t) pjsip_pres_current_notify( pjsip_evsub *sub,
569 pjsip_tx_data **p_tdata )
570 {
571 pjsip_pres *pres;
572 pjsip_tx_data *tdata;
573 pj_status_t status;
574
575 /* Check arguments. */
576 PJ_ASSERT_RETURN(sub, PJ_EINVAL);
577
578 /* Get the presence object. */
579 pres = (pjsip_pres*) pjsip_evsub_get_mod_data(sub, mod_presence.id);
580 PJ_ASSERT_RETURN(pres != NULL, PJSIP_SIMPLE_ENOPRESENCE);
581
582 /* We may not have a presence info yet, e.g. when we receive SUBSCRIBE
583 * to refresh subscription while we're waiting for user authorization.
584 */
585 //PJ_ASSERT_RETURN(pres->status.info_cnt > 0,
586 // PJSIP_SIMPLE_ENOPRESENCEINFO);
587
588
589 /* Lock object. */
590 pjsip_dlg_inc_lock(pres->dlg);
591
592 /* Create the NOTIFY request. */
593 status = pjsip_evsub_current_notify( sub, &tdata);
594 if (status != PJ_SUCCESS)
595 goto on_return;
596
597
598 /* Create message body to reflect the presence status. */
599 if (pres->status.info_cnt > 0) {
600 status = pres_create_msg_body( pres, tdata );
601 if (status != PJ_SUCCESS)
602 goto on_return;
603 }
604
605 /* Done. */
606 *p_tdata = tdata;
607
608
609 on_return:
610 pjsip_dlg_dec_lock(pres->dlg);
611 return status;
612 }
613
614
615 /*
616 * Send request.
617 */
618 PJ_DEF(pj_status_t) pjsip_pres_send_request( pjsip_evsub *sub,
619 pjsip_tx_data *tdata )
620 {
621 return pjsip_evsub_send_request(sub, tdata);
622 }
623
624
625 /*
626 * This callback is called by event subscription when subscription
627 * state has changed.
628 */
629 static void pres_on_evsub_state( pjsip_evsub *sub, pjsip_event *event)
630 {
631 pjsip_pres *pres;
632
633 pres = (pjsip_pres*) pjsip_evsub_get_mod_data(sub, mod_presence.id);
634 PJ_ASSERT_ON_FAIL(pres!=NULL, {return;});
635
636 if (pres->user_cb.on_evsub_state)
637 (*pres->user_cb.on_evsub_state)(sub, event);
638
639 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
640 if (pres->status_pool) {
641 pj_pool_release(pres->status_pool);
642 pres->status_pool = NULL;
643 }
644 if (pres->tmp_pool) {
645 pj_pool_release(pres->tmp_pool);
646 pres->tmp_pool = NULL;
647 }
648 if (pres->mutex) {
649 pj_mutex_destroy(pres->mutex);
650 pres->mutex = NULL;
651 }
652 }
653 }
654
655 /*
656 * Called when transaction state has changed.
657 */
658 static void pres_on_evsub_tsx_state( pjsip_evsub *sub, pjsip_transaction *tsx,
659 pjsip_event *event)
660 {
661 pjsip_pres *pres;
662
663 pres = (pjsip_pres*) pjsip_evsub_get_mod_data(sub, mod_presence.id);
664 PJ_ASSERT_ON_FAIL(pres!=NULL, {return;});
665
666 if (pres->user_cb.on_tsx_state)
667 (*pres->user_cb.on_tsx_state)(sub, tsx, event);
668 }
669
670
671 /*
672 * Called when SUBSCRIBE is received.
673 */
674 static void pres_on_evsub_rx_refresh( pjsip_evsub *sub,
675 pjsip_rx_data *rdata,
676 int *p_st_code,
677 pj_str_t **p_st_text,
678 pjsip_hdr *res_hdr,
679 pjsip_msg_body **p_body)
680 {
681 pjsip_pres *pres;
682
683 pres = (pjsip_pres*) pjsip_evsub_get_mod_data(sub, mod_presence.id);
684 PJ_ASSERT_ON_FAIL(pres!=NULL, {return;});
685
686 if (pres->user_cb.on_rx_refresh) {
687 (*pres->user_cb.on_rx_refresh)(sub, rdata, p_st_code, p_st_text,
688 res_hdr, p_body);
689
690 } else {
691 /* Implementors MUST send NOTIFY if it implements on_rx_refresh */
692 pjsip_tx_data *tdata;
693 pj_str_t timeout = { "timeout", 7};
694 pj_status_t status;
695
696 if (pjsip_evsub_get_state(sub)==PJSIP_EVSUB_STATE_TERMINATED) {
697 status = pjsip_pres_notify( sub, PJSIP_EVSUB_STATE_TERMINATED,
698 NULL, &timeout, &tdata);
699 } else {
700 status = pjsip_pres_current_notify(sub, &tdata);
701 }
702
703 if (status == PJ_SUCCESS)
704 pjsip_pres_send_request(sub, tdata);
705 }
706 }
707
708
709 /*
710 * Process the content of incoming NOTIFY request and update temporary
711 * status.
712 *
713 * return PJ_SUCCESS if incoming request is acceptable. If return value
714 * is not PJ_SUCCESS, res_hdr may be added with Warning header.
715 */
716 static pj_status_t pres_process_rx_notify( pjsip_pres *pres,
717 pjsip_rx_data *rdata,
718 int *p_st_code,
719 pj_str_t **p_st_text,
720 pjsip_hdr *res_hdr)
721 {
722 const pj_str_t STR_MULTIPART = { "multipart", 9 };
723 pjsip_ctype_hdr *ctype_hdr;
724 pj_status_t status = PJ_SUCCESS;
725
726 *p_st_text = NULL;
727
728 /* Check Content-Type and msg body are present. */
729 ctype_hdr = rdata->msg_info.ctype;
730
731 if (ctype_hdr==NULL || rdata->msg_info.msg->body==NULL) {
732
733 pjsip_warning_hdr *warn_hdr;
734 pj_str_t warn_text;
735
736 *p_st_code = PJSIP_SC_BAD_REQUEST;
737
738 warn_text = pj_str("Message body is not present");
739 warn_hdr = pjsip_warning_hdr_create(rdata->tp_info.pool, 399,
740 pjsip_endpt_name(pres->dlg->endpt),
741 &warn_text);
742 pj_list_push_back(res_hdr, warn_hdr);
743
744 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_BAD_REQUEST);
745 }
746
747 /* Parse content. */
748 if (pj_stricmp(&ctype_hdr->media.type, &STR_MULTIPART)==0) {
749 pjsip_multipart_part *mpart;
750 pjsip_media_type ctype;
751
752 pjsip_media_type_init(&ctype, (pj_str_t*)&STR_APPLICATION,
753 (pj_str_t*)&STR_PIDF_XML);
754 mpart = pjsip_multipart_find_part(rdata->msg_info.msg->body,
755 &ctype, NULL);
756 if (mpart) {
757 status = pjsip_pres_parse_pidf2((char*)mpart->body->data,
758 mpart->body->len, pres->tmp_pool,
759 &pres->tmp_status);
760 }
761
762 if (mpart==NULL) {
763 pjsip_media_type_init(&ctype, (pj_str_t*)&STR_APPLICATION,
764 (pj_str_t*)&STR_XPIDF_XML);
765 mpart = pjsip_multipart_find_part(rdata->msg_info.msg->body,
766 &ctype, NULL);
767 if (mpart) {
768 status = pjsip_pres_parse_xpidf2((char*)mpart->body->data,
769 mpart->body->len,
770 pres->tmp_pool,
771 &pres->tmp_status);
772 } else {
773 status = PJSIP_SIMPLE_EBADCONTENT;
774 }
775 }
776 }
777 else
778 if (pj_stricmp(&ctype_hdr->media.type, &STR_APPLICATION)==0 &&
779 pj_stricmp(&ctype_hdr->media.subtype, &STR_PIDF_XML)==0)
780 {
781 status = pjsip_pres_parse_pidf( rdata, pres->tmp_pool,
782 &pres->tmp_status);
783 }
784 else
785 if (pj_stricmp(&ctype_hdr->media.type, &STR_APPLICATION)==0 &&
786 pj_stricmp(&ctype_hdr->media.subtype, &STR_XPIDF_XML)==0)
787 {
788 status = pjsip_pres_parse_xpidf( rdata, pres->tmp_pool,
789 &pres->tmp_status);
790 }
791 else
792 {
793 status = PJSIP_SIMPLE_EBADCONTENT;
794 }
795
796 /* If application calls pres_get_status(), redirect the call to
797 * retrieve the temporary status.
798 */
799 pres->is_ts_valid = (status == PJ_SUCCESS? PJ_TRUE: PJ_FALSE);
800
801 if (status != PJ_SUCCESS) {
802 /* Unsupported or bad Content-Type */
803 if (PJSIP_PRES_BAD_CONTENT_RESPONSE >= 300) {
804 pjsip_accept_hdr *accept_hdr;
805 pjsip_warning_hdr *warn_hdr;
806
807 *p_st_code = PJSIP_PRES_BAD_CONTENT_RESPONSE;
808
809 /* Add Accept header */
810 accept_hdr = pjsip_accept_hdr_create(rdata->tp_info.pool);
811 accept_hdr->values[accept_hdr->count++] = STR_APP_PIDF_XML;
812 accept_hdr->values[accept_hdr->count++] = STR_APP_XPIDF_XML;
813 pj_list_push_back(res_hdr, accept_hdr);
814
815 /* Add Warning header */
816 warn_hdr = pjsip_warning_hdr_create_from_status(
817 rdata->tp_info.pool,
818 pjsip_endpt_name(pres->dlg->endpt),
819 status);
820 pj_list_push_back(res_hdr, warn_hdr);
821
822 return status;
823 } else {
824 pj_assert(PJSIP_PRES_BAD_CONTENT_RESPONSE/100 == 2);
825 PJ_PERROR(4,(THIS_FILE, status,
826 "Ignoring presence error due to "
827 "PJSIP_PRES_BAD_CONTENT_RESPONSE setting [%d]",
828 PJSIP_PRES_BAD_CONTENT_RESPONSE));
829 *p_st_code = PJSIP_PRES_BAD_CONTENT_RESPONSE;
830 status = PJ_SUCCESS;
831 }
832 }
833
834 return PJ_SUCCESS;
835 }
836
837
838 /*
839 * Called when NOTIFY is received.
840 */
841 static void pres_on_evsub_rx_notify( pjsip_evsub *sub,
842 pjsip_rx_data *rdata,
843 int *p_st_code,
844 pj_str_t **p_st_text,
845 pjsip_hdr *res_hdr,
846 pjsip_msg_body **p_body)
847 {
848 pjsip_pres *pres;
849 pj_status_t status;
850
851 pres = (pjsip_pres*) pjsip_evsub_get_mod_data(sub, mod_presence.id);
852 PJ_ASSERT_ON_FAIL(pres!=NULL, {return;});
853
854 if (rdata->msg_info.msg->body) {
855 status = pres_process_rx_notify( pres, rdata, p_st_code, p_st_text,
856 res_hdr );
857 if (status != PJ_SUCCESS)
858 return;
859
860 } else {
861 #if 1
862 /* This is the newest change, https://github.com/pjsip/pjproject/issues/873
863 * Some app want to be notified about the empty NOTIFY, e.g. to
864 * decide whether it should consider the buddy as offline.
865 * In this case, leave the buddy state unchanged, but set the
866 * "tuple_node" in pjsip_pres_status to NULL.
867 */
868 unsigned i;
869 pj_mutex_lock(pres->mutex);
870 for (i=0; i<pres->status.info_cnt; ++i) {
871 pres->status.info[i].tuple_node = NULL;
872 }
873 pj_mutex_unlock(pres->mutex);
874 #elif 0
875 /* This has just been changed. Previously, we treat incoming NOTIFY
876 * with no message body as having the presence subscription closed.
877 * Now we treat it as no change in presence status (ref: EyeBeam).
878 */
879 *p_st_code = 200;
880 return;
881 #else
882 unsigned i;
883 /* Subscription is terminated. Consider contact is offline */
884 pres->is_ts_valid = PJ_TRUE;
885 for (i=0; i<pres->tmp_status.info_cnt; ++i)
886 pres->tmp_status.info[i].basic_open = PJ_FALSE;
887 #endif
888 }
889
890 /* Notify application. */
891 if (pres->user_cb.on_rx_notify) {
892 (*pres->user_cb.on_rx_notify)(sub, rdata, p_st_code, p_st_text,
893 res_hdr, p_body);
894 }
895
896
897 /* If application responded NOTIFY with 2xx, copy temporary status
898 * to main status, and mark the temporary status as invalid.
899 */
900 pj_mutex_lock(pres->mutex);
901
902 if ((*p_st_code)/100 == 2) {
903 pj_pool_t *tmp;
904
905 pj_memcpy(&pres->status, &pres->tmp_status, sizeof(pjsip_pres_status));
906
907 /* Swap the pool */
908 tmp = pres->tmp_pool;
909 pres->tmp_pool = pres->status_pool;
910 pres->status_pool = tmp;
911 }
912
913 pres->is_ts_valid = PJ_FALSE;
914 pj_pool_reset(pres->tmp_pool);
915
916 pj_mutex_unlock(pres->mutex);
917
918 /* Done */
919 }
920
921 /*
922 * Called when it's time to send SUBSCRIBE.
923 */
924 static void pres_on_evsub_client_refresh(pjsip_evsub *sub)
925 {
926 pjsip_pres *pres;
927
928 pres = (pjsip_pres*) pjsip_evsub_get_mod_data(sub, mod_presence.id);
929 PJ_ASSERT_ON_FAIL(pres!=NULL, {return;});
930
931 if (pres->user_cb.on_client_refresh) {
932 (*pres->user_cb.on_client_refresh)(sub);
933 } else {
934 pj_status_t status;
935 pjsip_tx_data *tdata;
936
937 status = pjsip_pres_initiate(sub, PJSIP_EXPIRES_NOT_SPECIFIED,
938 &tdata);
939 if (status == PJ_SUCCESS)
940 pjsip_pres_send_request(sub, tdata);
941 }
942 }
943
944 /*
945 * Called when no refresh is received after the interval.
946 */
947 static void pres_on_evsub_server_timeout(pjsip_evsub *sub)
948 {
949 pjsip_pres *pres;
950
951 pres = (pjsip_pres*) pjsip_evsub_get_mod_data(sub, mod_presence.id);
952 PJ_ASSERT_ON_FAIL(pres!=NULL, {return;});
953
954 if (pres->user_cb.on_server_timeout) {
955 (*pres->user_cb.on_server_timeout)(sub);
956 } else {
957 pj_status_t status;
958 pjsip_tx_data *tdata;
959 pj_str_t reason = { "timeout", 7 };
960
961 status = pjsip_pres_notify(sub, PJSIP_EVSUB_STATE_TERMINATED,
962 NULL, &reason, &tdata);
963 if (status == PJ_SUCCESS)
964 pjsip_pres_send_request(sub, tdata);
965 }
966 }
967