blob: d34116260b92afda8780ecf15aa2d6415781038b [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Benny Prijono834aee32006-02-19 01:38:06 +000019#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_endpoint.h>
24#include <pjsip/sip_dialog.h>
25#include <pj/assert.h>
26#include <pj/guid.h>
27#include <pj/log.h>
28#include <pj/os.h>
Benny Prijono5dcb38d2005-11-21 01:55:47 +000029#include <pj/pool.h>
30#include <pj/string.h>
Benny Prijono5dcb38d2005-11-21 01:55:47 +000031
Benny Prijono5dcb38d2005-11-21 01:55:47 +000032
Benny Prijono834aee32006-02-19 01:38:06 +000033#define THIS_FILE "presence.c"
34#define PRES_DEFAULT_EXPIRES 600
Benny Prijono5dcb38d2005-11-21 01:55:47 +000035
36/*
Benny Prijono834aee32006-02-19 01:38:06 +000037 * Presence module (mod-presence)
Benny Prijono5dcb38d2005-11-21 01:55:47 +000038 */
Benny Prijono834aee32006-02-19 01:38:06 +000039static struct pjsip_module mod_presence =
Benny Prijono5dcb38d2005-11-21 01:55:47 +000040{
Benny Prijono834aee32006-02-19 01:38:06 +000041 NULL, NULL, /* prev, next. */
42 { "mod-presence", 12 }, /* Name. */
43 -1, /* Id */
Benny Prijono2f8992b2006-02-25 21:16:36 +000044 PJSIP_MOD_PRIORITY_DIALOG_USAGE,/* Priority */
Benny Prijono834aee32006-02-19 01:38:06 +000045 NULL, /* load() */
46 NULL, /* start() */
47 NULL, /* stop() */
48 NULL, /* unload() */
49 NULL, /* on_rx_request() */
50 NULL, /* on_rx_response() */
51 NULL, /* on_tx_request. */
52 NULL, /* on_tx_response() */
53 NULL, /* on_tsx_state() */
54};
55
56
57/*
58 * Presence message body type.
59 */
60typedef enum content_type
61{
62 CONTENT_TYPE_NONE,
63 CONTENT_TYPE_PIDF,
64 CONTENT_TYPE_XPIDF,
65} content_type;
66
67/*
68 * This structure describe a presentity, for both subscriber and notifier.
69 */
70struct pjsip_pres
71{
72 pjsip_evsub *sub; /**< Event subscribtion record. */
73 pjsip_dialog *dlg; /**< The dialog. */
74 content_type content_type; /**< Content-Type. */
75 pjsip_pres_status status; /**< Presence status. */
76 pjsip_pres_status tmp_status; /**< Temp, before NOTIFY is answred.*/
77 pjsip_evsub_user user_cb; /**< The user callback. */
78};
79
80
81typedef struct pjsip_pres pjsip_pres;
82
83
84/*
85 * Forward decl for evsub callback.
86 */
87static void pres_on_evsub_state( pjsip_evsub *sub, pjsip_event *event);
88static void pres_on_evsub_tsx_state( pjsip_evsub *sub, pjsip_transaction *tsx,
89 pjsip_event *event);
90static void pres_on_evsub_rx_refresh( pjsip_evsub *sub,
91 pjsip_rx_data *rdata,
92 int *p_st_code,
93 pj_str_t **p_st_text,
94 pjsip_hdr *res_hdr,
95 pjsip_msg_body **p_body);
96static void pres_on_evsub_rx_notify( pjsip_evsub *sub,
97 pjsip_rx_data *rdata,
98 int *p_st_code,
99 pj_str_t **p_st_text,
100 pjsip_hdr *res_hdr,
101 pjsip_msg_body **p_body);
102static void pres_on_evsub_client_refresh(pjsip_evsub *sub);
103static void pres_on_evsub_server_timeout(pjsip_evsub *sub);
104
105
106/*
107 * Event subscription callback for presence.
108 */
109static pjsip_evsub_user pres_user =
110{
111 &pres_on_evsub_state,
112 &pres_on_evsub_tsx_state,
113 &pres_on_evsub_rx_refresh,
114 &pres_on_evsub_rx_notify,
115 &pres_on_evsub_client_refresh,
116 &pres_on_evsub_server_timeout,
117};
118
119
120/*
121 * Some static constants.
122 */
123const pj_str_t STR_EVENT = { "Event", 5 };
124const pj_str_t STR_PRESENCE = { "presence", 8 };
125const pj_str_t STR_APPLICATION = { "application", 11 };
126const pj_str_t STR_PIDF_XML = { "pidf+xml", 8};
127const pj_str_t STR_XPIDF_XML = { "xpidf+xml", 9};
128const pj_str_t STR_APP_PIDF_XML = { "application/pidf+xml", 20 };
129const pj_str_t STR_APP_XPIDF_XML = { "application/xpidf+xml", 21 };
130
131
132/*
133 * Init presence module.
134 */
135PJ_DEF(pj_status_t) pjsip_pres_init_module( pjsip_endpoint *endpt,
136 pjsip_module *mod_evsub)
137{
138 pj_status_t status;
139 pj_str_t accept[2];
140
141 /* Check arguments. */
142 PJ_ASSERT_RETURN(endpt && mod_evsub, PJ_EINVAL);
143
144 /* Must have not been registered */
145 PJ_ASSERT_RETURN(mod_presence.id == -1, PJ_EINVALIDOP);
146
147 /* Register to endpoint */
148 status = pjsip_endpt_register_module(endpt, &mod_presence);
149 if (status != PJ_SUCCESS)
150 return status;
151
152 accept[0] = STR_APP_PIDF_XML;
153 accept[1] = STR_APP_XPIDF_XML;
154
155 /* Register event package to event module. */
156 status = pjsip_evsub_register_pkg( &mod_presence, &STR_PRESENCE,
157 PRES_DEFAULT_EXPIRES,
158 PJ_ARRAY_SIZE(accept), accept);
159 if (status != PJ_SUCCESS) {
160 pjsip_endpt_unregister_module(endpt, &mod_presence);
161 return status;
162 }
163
164 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000165}
166
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000167
Benny Prijono834aee32006-02-19 01:38:06 +0000168/*
169 * Get presence module instance.
170 */
171PJ_DEF(pjsip_module*) pjsip_pres_instance(void)
172{
173 return &mod_presence;
174}
175
176
177/*
178 * Create client subscription.
179 */
180PJ_DEF(pj_status_t) pjsip_pres_create_uac( pjsip_dialog *dlg,
181 const pjsip_evsub_user *user_cb,
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000182 unsigned options,
Benny Prijono834aee32006-02-19 01:38:06 +0000183 pjsip_evsub **p_evsub )
184{
185 pj_status_t status;
186 pjsip_pres *pres;
187 pjsip_evsub *sub;
188
189 PJ_ASSERT_RETURN(dlg && p_evsub, PJ_EINVAL);
190
191 pjsip_dlg_inc_lock(dlg);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000192
193 /* Create event subscription */
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000194 status = pjsip_evsub_create_uac( dlg, &pres_user, &STR_PRESENCE,
195 options, &sub);
Benny Prijono834aee32006-02-19 01:38:06 +0000196 if (status != PJ_SUCCESS)
197 goto on_return;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000198
Benny Prijono834aee32006-02-19 01:38:06 +0000199 /* Create presence */
200 pres = pj_pool_zalloc(dlg->pool, sizeof(pjsip_pres));
201 pres->dlg = dlg;
Benny Prijono26ff9062006-02-21 23:47:00 +0000202 pres->sub = sub;
Benny Prijono834aee32006-02-19 01:38:06 +0000203 if (user_cb)
204 pj_memcpy(&pres->user_cb, user_cb, sizeof(pjsip_evsub_user));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000205
Benny Prijono834aee32006-02-19 01:38:06 +0000206 /* Attach to evsub */
207 pjsip_evsub_set_mod_data(sub, mod_presence.id, pres);
208
209 *p_evsub = sub;
210
211on_return:
212 pjsip_dlg_dec_lock(dlg);
213 return status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000214}
215
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000216
217/*
Benny Prijono834aee32006-02-19 01:38:06 +0000218 * Create server subscription.
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000219 */
Benny Prijono834aee32006-02-19 01:38:06 +0000220PJ_DEF(pj_status_t) pjsip_pres_create_uas( pjsip_dialog *dlg,
221 const pjsip_evsub_user *user_cb,
222 pjsip_rx_data *rdata,
223 pjsip_evsub **p_evsub )
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000224{
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000225 pjsip_accept_hdr *accept;
Benny Prijono834aee32006-02-19 01:38:06 +0000226 pjsip_event_hdr *event;
227 pjsip_expires_hdr *expires_hdr;
228 unsigned expires;
229 content_type content_type = CONTENT_TYPE_NONE;
230 pjsip_evsub *sub;
231 pjsip_pres *pres;
232 pj_status_t status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000233
Benny Prijono834aee32006-02-19 01:38:06 +0000234 /* Check arguments */
235 PJ_ASSERT_RETURN(dlg && rdata && p_evsub, PJ_EINVAL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000236
Benny Prijono834aee32006-02-19 01:38:06 +0000237 /* Must be request message */
238 PJ_ASSERT_RETURN(rdata->msg_info.msg->type == PJSIP_REQUEST_MSG,
239 PJSIP_ENOTREQUESTMSG);
240
241 /* Check that request is SUBSCRIBE */
242 PJ_ASSERT_RETURN(pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
243 &pjsip_subscribe_method)==0,
244 PJSIP_SIMPLE_ENOTSUBSCRIBE);
245
246 /* Check that Event header contains "presence" */
247 event = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &STR_EVENT, NULL);
248 if (!event) {
249 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_BAD_REQUEST);
250 }
251 if (pj_stricmp(&event->event_type, &STR_PRESENCE) != 0) {
252 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_BAD_EVENT);
253 }
254
255 /* Check that request contains compatible Accept header. */
256 accept = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, NULL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000257 if (accept) {
258 unsigned i;
Benny Prijono834aee32006-02-19 01:38:06 +0000259 for (i=0; i<accept->count; ++i) {
260 if (pj_stricmp(&accept->values[i], &STR_APP_PIDF_XML)==0) {
261 content_type = CONTENT_TYPE_PIDF;
262 break;
263 } else
264 if (pj_stricmp(&accept->values[i], &STR_APP_XPIDF_XML)==0) {
265 content_type = CONTENT_TYPE_XPIDF;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000266 break;
267 }
268 }
269
Benny Prijono834aee32006-02-19 01:38:06 +0000270 if (i==accept->count) {
271 /* Nothing is acceptable */
272 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000273 }
274
Benny Prijono834aee32006-02-19 01:38:06 +0000275 } else {
276 /* No Accept header.
277 * Treat as "application/pidf+xml"
278 */
279 content_type = CONTENT_TYPE_PIDF;
280 }
281
282 /* Check that expires is not too short. */
283 expires_hdr=pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
284 if (expires_hdr) {
285 if (expires_hdr->ivalue < 5) {
286 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_INTERVAL_TOO_BRIEF);
287 }
288
289 expires = expires_hdr->ivalue;
290 if (expires > PRES_DEFAULT_EXPIRES)
291 expires = PRES_DEFAULT_EXPIRES;
292
293 } else {
294 expires = PRES_DEFAULT_EXPIRES;
295 }
296
297 /* Lock dialog */
298 pjsip_dlg_inc_lock(dlg);
299
300
301 /* Create server subscription */
Benny Prijono26ff9062006-02-21 23:47:00 +0000302 status = pjsip_evsub_create_uas( dlg, &pres_user, rdata, 0, &sub);
Benny Prijono834aee32006-02-19 01:38:06 +0000303 if (status != PJ_SUCCESS)
304 goto on_return;
305
306 /* Create server presence subscription */
307 pres = pj_pool_zalloc(dlg->pool, sizeof(pjsip_pres));
308 pres->dlg = dlg;
309 pres->sub = sub;
310 pres->content_type = content_type;
311 if (user_cb)
312 pj_memcpy(&pres->user_cb, user_cb, sizeof(pjsip_evsub_user));
313
314 /* Attach to evsub */
315 pjsip_evsub_set_mod_data(sub, mod_presence.id, pres);
316
317 /* Done: */
318 *p_evsub = sub;
319
320on_return:
321 pjsip_dlg_dec_lock(dlg);
322 return status;
323}
324
325
326/*
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000327 * Forcefully terminate presence.
328 */
329PJ_DEF(pj_status_t) pjsip_pres_terminate( pjsip_evsub *sub,
330 pj_bool_t notify )
331{
332 return pjsip_evsub_terminate(sub, notify);
333}
334
335/*
Benny Prijono834aee32006-02-19 01:38:06 +0000336 * Create SUBSCRIBE
337 */
338PJ_DEF(pj_status_t) pjsip_pres_initiate( pjsip_evsub *sub,
339 pj_int32_t expires,
340 pjsip_tx_data **p_tdata)
341{
342 return pjsip_evsub_initiate(sub, &pjsip_subscribe_method, expires,
343 p_tdata);
344}
345
346
347/*
348 * Accept incoming subscription.
349 */
350PJ_DEF(pj_status_t) pjsip_pres_accept( pjsip_evsub *sub,
351 pjsip_rx_data *rdata,
352 int st_code,
353 const pjsip_hdr *hdr_list )
354{
355 return pjsip_evsub_accept( sub, rdata, st_code, hdr_list );
356}
357
358
359/*
360 * Get presence status.
361 */
362PJ_DEF(pj_status_t) pjsip_pres_get_status( pjsip_evsub *sub,
363 pjsip_pres_status *status )
364{
365 pjsip_pres *pres;
366
367 PJ_ASSERT_RETURN(sub && status, PJ_EINVAL);
368
369 pres = pjsip_evsub_get_mod_data(sub, mod_presence.id);
370 PJ_ASSERT_RETURN(pres!=NULL, PJSIP_SIMPLE_ENOPRESENCE);
371
372 if (pres->tmp_status._is_valid)
373 pj_memcpy(status, &pres->tmp_status, sizeof(pjsip_pres_status));
374 else
375 pj_memcpy(status, &pres->status, sizeof(pjsip_pres_status));
376
377 return PJ_SUCCESS;
378}
379
380
381/*
382 * Set presence status.
383 */
384PJ_DEF(pj_status_t) pjsip_pres_set_status( pjsip_evsub *sub,
385 const pjsip_pres_status *status )
386{
387 unsigned i;
388 pjsip_pres *pres;
389
390 PJ_ASSERT_RETURN(sub && status, PJ_EINVAL);
391
392 pres = pjsip_evsub_get_mod_data(sub, mod_presence.id);
393 PJ_ASSERT_RETURN(pres!=NULL, PJSIP_SIMPLE_ENOPRESENCE);
394
395 for (i=0; i<status->info_cnt; ++i) {
396 pres->status.info[i].basic_open = status->info[i].basic_open;
397 if (status->info[i].id.slen == 0) {
398 pj_create_unique_string(pres->dlg->pool,
399 &pres->status.info[i].id);
400 } else {
401 pj_strdup(pres->dlg->pool,
402 &pres->status.info[i].id,
403 &status->info[i].id);
404 }
405 pj_strdup(pres->dlg->pool,
406 &pres->status.info[i].contact,
407 &status->info[i].contact);
408 }
409
410 pres->status.info_cnt = status->info_cnt;
411
412 return PJ_SUCCESS;
413}
414
415
416/*
Benny Prijono834aee32006-02-19 01:38:06 +0000417 * Create message body.
418 */
419static pj_status_t pres_create_msg_body( pjsip_pres *pres,
420 pjsip_tx_data *tdata)
421{
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000422 pj_str_t entity;
Benny Prijono834aee32006-02-19 01:38:06 +0000423
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000424 /* Get publisher URI */
425 entity.ptr = pj_pool_alloc(tdata->pool, PJSIP_MAX_URL_SIZE);
426 entity.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
427 pres->dlg->local.info->uri,
428 entity.ptr, PJSIP_MAX_URL_SIZE);
429 if (entity.slen < 1)
430 return PJ_ENOMEM;
431
Benny Prijono834aee32006-02-19 01:38:06 +0000432 if (pres->content_type == CONTENT_TYPE_PIDF) {
433
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000434 return pjsip_pres_create_pidf(tdata->pool, &pres->status,
435 &entity, &tdata->msg->body);
Benny Prijono834aee32006-02-19 01:38:06 +0000436
437 } else if (pres->content_type == CONTENT_TYPE_XPIDF) {
438
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000439 return pjsip_pres_create_xpidf(tdata->pool, &pres->status,
440 &entity, &tdata->msg->body);
Benny Prijono834aee32006-02-19 01:38:06 +0000441
442 } else {
443 return PJSIP_SIMPLE_EBADCONTENT;
444 }
Benny Prijono834aee32006-02-19 01:38:06 +0000445}
446
447
448/*
449 * Create NOTIFY
450 */
451PJ_DEF(pj_status_t) pjsip_pres_notify( pjsip_evsub *sub,
452 pjsip_evsub_state state,
453 const pj_str_t *state_str,
454 const pj_str_t *reason,
455 pjsip_tx_data **p_tdata)
456{
457 pjsip_pres *pres;
458 pjsip_tx_data *tdata;
459 pj_status_t status;
460
461 /* Check arguments. */
462 PJ_ASSERT_RETURN(sub, PJ_EINVAL);
463
464 /* Get the presence object. */
465 pres = pjsip_evsub_get_mod_data(sub, mod_presence.id);
466 PJ_ASSERT_RETURN(pres != NULL, PJSIP_SIMPLE_ENOPRESENCE);
467
468 /* Must have at least one presence info. */
469 PJ_ASSERT_RETURN(pres->status.info_cnt > 0, PJSIP_SIMPLE_ENOPRESENCEINFO);
470
471
472 /* Lock object. */
473 pjsip_dlg_inc_lock(pres->dlg);
474
475 /* Create the NOTIFY request. */
476 status = pjsip_evsub_notify( sub, state, state_str, reason, &tdata);
477 if (status != PJ_SUCCESS)
478 goto on_return;
479
480
481 /* Create message body to reflect the presence status. */
482 status = pres_create_msg_body( pres, tdata );
483 if (status != PJ_SUCCESS)
484 goto on_return;
485
486
487 /* Done. */
488 *p_tdata = tdata;
489
490
491on_return:
492 pjsip_dlg_dec_lock(pres->dlg);
493 return status;
494}
495
496
497/*
498 * Create NOTIFY that reflect current state.
499 */
500PJ_DEF(pj_status_t) pjsip_pres_current_notify( pjsip_evsub *sub,
501 pjsip_tx_data **p_tdata )
502{
503 pjsip_pres *pres;
504 pjsip_tx_data *tdata;
505 pj_status_t status;
506
507 /* Check arguments. */
508 PJ_ASSERT_RETURN(sub, PJ_EINVAL);
509
510 /* Get the presence object. */
511 pres = pjsip_evsub_get_mod_data(sub, mod_presence.id);
512 PJ_ASSERT_RETURN(pres != NULL, PJSIP_SIMPLE_ENOPRESENCE);
513
514 /* Must have at least one presence info. */
515 PJ_ASSERT_RETURN(pres->status.info_cnt > 0, PJSIP_SIMPLE_ENOPRESENCEINFO);
516
517
518 /* Lock object. */
519 pjsip_dlg_inc_lock(pres->dlg);
520
521 /* Create the NOTIFY request. */
522 status = pjsip_evsub_current_notify( sub, &tdata);
523 if (status != PJ_SUCCESS)
524 goto on_return;
525
526
527 /* Create message body to reflect the presence status. */
528 status = pres_create_msg_body( pres, tdata );
529 if (status != PJ_SUCCESS)
530 goto on_return;
531
532
533 /* Done. */
534 *p_tdata = tdata;
535
536
537on_return:
538 pjsip_dlg_dec_lock(pres->dlg);
539 return status;
540}
541
542
543/*
544 * Send request.
545 */
546PJ_DEF(pj_status_t) pjsip_pres_send_request( pjsip_evsub *sub,
547 pjsip_tx_data *tdata )
548{
549 return pjsip_evsub_send_request(sub, tdata);
550}
551
552
553/*
554 * This callback is called by event subscription when subscription
555 * state has changed.
556 */
557static void pres_on_evsub_state( pjsip_evsub *sub, pjsip_event *event)
558{
559 pjsip_pres *pres;
560
561 pres = pjsip_evsub_get_mod_data(sub, mod_presence.id);
562 PJ_ASSERT_ON_FAIL(pres!=NULL, {return;});
563
564 if (pres->user_cb.on_evsub_state)
565 (*pres->user_cb.on_evsub_state)(sub, event);
566}
567
568/*
569 * Called when transaction state has changed.
570 */
571static void pres_on_evsub_tsx_state( pjsip_evsub *sub, pjsip_transaction *tsx,
572 pjsip_event *event)
573{
574 pjsip_pres *pres;
575
576 pres = pjsip_evsub_get_mod_data(sub, mod_presence.id);
577 PJ_ASSERT_ON_FAIL(pres!=NULL, {return;});
578
579 if (pres->user_cb.on_tsx_state)
580 (*pres->user_cb.on_tsx_state)(sub, tsx, event);
581}
582
583
584/*
585 * Called when SUBSCRIBE is received.
586 */
587static void pres_on_evsub_rx_refresh( pjsip_evsub *sub,
588 pjsip_rx_data *rdata,
589 int *p_st_code,
590 pj_str_t **p_st_text,
591 pjsip_hdr *res_hdr,
592 pjsip_msg_body **p_body)
593{
594 pjsip_pres *pres;
595
596 pres = pjsip_evsub_get_mod_data(sub, mod_presence.id);
597 PJ_ASSERT_ON_FAIL(pres!=NULL, {return;});
598
599 if (pres->user_cb.on_rx_refresh) {
600 (*pres->user_cb.on_rx_refresh)(sub, rdata, p_st_code, p_st_text,
601 res_hdr, p_body);
602
603 } else {
604 /* Implementors MUST send NOTIFY if it implements on_rx_refresh */
605 pjsip_tx_data *tdata;
606 pj_str_t timeout = { "timeout", 7};
607 pj_status_t status;
608
609 if (pjsip_evsub_get_state(sub)==PJSIP_EVSUB_STATE_TERMINATED) {
610 status = pjsip_pres_notify( sub, PJSIP_EVSUB_STATE_TERMINATED,
611 NULL, &timeout, &tdata);
612 } else {
613 status = pjsip_pres_current_notify(sub, &tdata);
614 }
615
616 if (status == PJ_SUCCESS)
617 pjsip_pres_send_request(sub, tdata);
618 }
619}
620
Benny Prijono834aee32006-02-19 01:38:06 +0000621
622/*
Benny Prijonob0808372006-03-02 21:18:58 +0000623 * Process the content of incoming NOTIFY request and update temporary
624 * status.
625 *
626 * return PJ_SUCCESS if incoming request is acceptable. If return value
627 * is not PJ_SUCCESS, res_hdr may be added with Warning header.
Benny Prijono834aee32006-02-19 01:38:06 +0000628 */
Benny Prijonob0808372006-03-02 21:18:58 +0000629static pj_status_t pres_process_rx_notify( pjsip_pres *pres,
630 pjsip_rx_data *rdata,
631 int *p_st_code,
632 pj_str_t **p_st_text,
633 pjsip_hdr *res_hdr)
Benny Prijono834aee32006-02-19 01:38:06 +0000634{
635 pjsip_ctype_hdr *ctype_hdr;
Benny Prijono834aee32006-02-19 01:38:06 +0000636 pj_status_t status;
637
Benny Prijonob0808372006-03-02 21:18:58 +0000638 *p_st_text = NULL;
Benny Prijono834aee32006-02-19 01:38:06 +0000639
640 /* Check Content-Type and msg body are present. */
641 ctype_hdr = rdata->msg_info.ctype;
642
643 if (ctype_hdr==NULL || rdata->msg_info.msg->body==NULL) {
644
645 pjsip_warning_hdr *warn_hdr;
646 pj_str_t warn_text;
647
648 *p_st_code = PJSIP_SC_BAD_REQUEST;
649
650 warn_text = pj_str("Message body is not present");
651 warn_hdr = pjsip_warning_hdr_create(rdata->tp_info.pool, 399,
652 pjsip_endpt_name(pres->dlg->endpt),
653 &warn_text);
654 pj_list_push_back(res_hdr, warn_hdr);
655
Benny Prijonob0808372006-03-02 21:18:58 +0000656 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_BAD_REQUEST);
Benny Prijono834aee32006-02-19 01:38:06 +0000657 }
658
659 /* Parse content. */
660
661 if (pj_stricmp(&ctype_hdr->media.type, &STR_APPLICATION)==0 &&
662 pj_stricmp(&ctype_hdr->media.subtype, &STR_PIDF_XML)==0)
663 {
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000664 status = pjsip_pres_parse_pidf( rdata, pres->dlg->pool,
665 &pres->tmp_status);
Benny Prijono834aee32006-02-19 01:38:06 +0000666 }
667 else
668 if (pj_stricmp(&ctype_hdr->media.type, &STR_APPLICATION)==0 &&
669 pj_stricmp(&ctype_hdr->media.subtype, &STR_XPIDF_XML)==0)
670 {
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000671 status = pjsip_pres_parse_xpidf( rdata, pres->dlg->pool,
672 &pres->tmp_status);
Benny Prijono834aee32006-02-19 01:38:06 +0000673 }
674 else
675 {
676 status = PJSIP_SIMPLE_EBADCONTENT;
677 }
678
679 if (status != PJ_SUCCESS) {
680 /* Unsupported or bad Content-Type */
681 pjsip_accept_hdr *accept_hdr;
682 pjsip_warning_hdr *warn_hdr;
683
684 *p_st_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
685
686 /* Add Accept header */
687 accept_hdr = pjsip_accept_hdr_create(rdata->tp_info.pool);
688 accept_hdr->values[accept_hdr->count++] = STR_APP_PIDF_XML;
689 accept_hdr->values[accept_hdr->count++] = STR_APP_XPIDF_XML;
690 pj_list_push_back(res_hdr, accept_hdr);
691
692 /* Add Warning header */
693 warn_hdr = pjsip_warning_hdr_create_from_status(
694 rdata->tp_info.pool,
695 pjsip_endpt_name(pres->dlg->endpt),
696 status);
697 pj_list_push_back(res_hdr, warn_hdr);
698
Benny Prijonob0808372006-03-02 21:18:58 +0000699 return status;
Benny Prijono834aee32006-02-19 01:38:06 +0000700 }
701
702 /* If application calls pres_get_status(), redirect the call to
703 * retrieve the temporary status.
704 */
705 pres->tmp_status._is_valid = PJ_TRUE;
706
Benny Prijonob0808372006-03-02 21:18:58 +0000707 return PJ_SUCCESS;
708}
709
710
711/*
712 * Called when NOTIFY is received.
713 */
714static void pres_on_evsub_rx_notify( pjsip_evsub *sub,
715 pjsip_rx_data *rdata,
716 int *p_st_code,
717 pj_str_t **p_st_text,
718 pjsip_hdr *res_hdr,
719 pjsip_msg_body **p_body)
720{
721 pjsip_pres *pres;
722 pj_status_t status;
723
724 pres = pjsip_evsub_get_mod_data(sub, mod_presence.id);
725 PJ_ASSERT_ON_FAIL(pres!=NULL, {return;});
726
Benny Prijonob0808372006-03-02 21:18:58 +0000727 if (rdata->msg_info.msg->body) {
728 status = pres_process_rx_notify( pres, rdata, p_st_code, p_st_text,
729 res_hdr );
730 if (status != PJ_SUCCESS)
731 return;
732
733 } else {
Benny Prijonoe6821552006-06-19 12:03:35 +0000734 /* This has just been changed. Previously, we treat incoming NOTIFY
735 * with no message body as having the presence subscription closed.
736 * Now we treat it as no change in presence status (ref: EyeBeam).
737 */
738#if 1
739 *p_st_code = 200;
740 return;
741#else
Benny Prijonob0808372006-03-02 21:18:58 +0000742 unsigned i;
Benny Prijonob0808372006-03-02 21:18:58 +0000743 /* Subscription is terminated. Consider contact is offline */
744 pres->tmp_status._is_valid = PJ_TRUE;
745 for (i=0; i<pres->tmp_status.info_cnt; ++i)
746 pres->tmp_status.info[i].basic_open = PJ_FALSE;
Benny Prijonoe6821552006-06-19 12:03:35 +0000747#endif
Benny Prijonob0808372006-03-02 21:18:58 +0000748 }
749
Benny Prijono834aee32006-02-19 01:38:06 +0000750 /* Notify application. */
751 if (pres->user_cb.on_rx_notify) {
752 (*pres->user_cb.on_rx_notify)(sub, rdata, p_st_code, p_st_text,
753 res_hdr, p_body);
754 }
755
756
757 /* If application responded NOTIFY with 2xx, copy temporary status
758 * to main status, and mark the temporary status as invalid.
759 */
760 if ((*p_st_code)/100 == 2) {
761 pj_memcpy(&pres->status, &pres->tmp_status, sizeof(pjsip_pres_status));
762 }
763
764 pres->tmp_status._is_valid = PJ_FALSE;
765
766 /* Done */
767}
768
769/*
770 * Called when it's time to send SUBSCRIBE.
771 */
772static void pres_on_evsub_client_refresh(pjsip_evsub *sub)
773{
774 pjsip_pres *pres;
775
776 pres = pjsip_evsub_get_mod_data(sub, mod_presence.id);
777 PJ_ASSERT_ON_FAIL(pres!=NULL, {return;});
778
779 if (pres->user_cb.on_client_refresh) {
780 (*pres->user_cb.on_client_refresh)(sub);
781 } else {
782 pj_status_t status;
783 pjsip_tx_data *tdata;
784
785 status = pjsip_pres_initiate(sub, -1, &tdata);
786 if (status == PJ_SUCCESS)
787 pjsip_pres_send_request(sub, tdata);
788 }
789}
790
791/*
792 * Called when no refresh is received after the interval.
793 */
794static void pres_on_evsub_server_timeout(pjsip_evsub *sub)
795{
796 pjsip_pres *pres;
797
798 pres = pjsip_evsub_get_mod_data(sub, mod_presence.id);
799 PJ_ASSERT_ON_FAIL(pres!=NULL, {return;});
800
801 if (pres->user_cb.on_server_timeout) {
802 (*pres->user_cb.on_server_timeout)(sub);
803 } else {
804 pj_status_t status;
805 pjsip_tx_data *tdata;
806 pj_str_t reason = { "timeout", 7 };
807
808 status = pjsip_pres_notify(sub, PJSIP_EVSUB_STATE_TERMINATED,
809 NULL, &reason, &tdata);
810 if (status == PJ_SUCCESS)
811 pjsip_pres_send_request(sub, tdata);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000812 }
813}
814