sal_eXosip2_presence.c 23 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
linphone
Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


#include "sal_eXosip2.h"

23 24 25 26 27 28 29 30 31 32 33
typedef enum {
	PIDF = 0,
	RFCxxxx = 1,
	MSOLDPRES = 2
} presence_type_t;

/*
 * REVISIT: this static variable forces every dialog to use the same presence description type depending 
 * on what is received on a single dialog...
 */
static presence_type_t presence_style = PIDF;
Simon Morlat's avatar
Simon Morlat committed
34

35
SalOp * sal_find_out_subscribe(Sal *sal, int sid){
Simon Morlat's avatar
Simon Morlat committed
36 37 38 39 40 41
	const MSList *elem;
	SalOp *op;
	for(elem=sal->out_subscribes;elem!=NULL;elem=elem->next){
		op=(SalOp*)elem->data;
		if (op->sid==sid) return op;
	}
42
	ms_message("No op for sid %i",sid);
Simon Morlat's avatar
Simon Morlat committed
43 44 45 46 47 48 49
	return NULL;
}

static void sal_add_out_subscribe(Sal *sal, SalOp *op){
	sal->out_subscribes=ms_list_append(sal->out_subscribes,op);
}

50 51
void sal_remove_out_subscribe(Sal *sal, SalOp *op){
	sal->out_subscribes=ms_list_remove(sal->out_subscribes,op);
Simon Morlat's avatar
Simon Morlat committed
52 53
}

Simon Morlat's avatar
Simon Morlat committed
54
SalOp * sal_find_in_subscribe(Sal *sal, int nid){
Simon Morlat's avatar
Simon Morlat committed
55 56 57 58 59 60 61 62 63
	const MSList *elem;
	SalOp *op;
	for(elem=sal->in_subscribes;elem!=NULL;elem=elem->next){
		op=(SalOp*)elem->data;
		if (op->nid==nid) return op;
	}
	return NULL;
}

64 65 66 67 68 69 70 71 72 73 74 75 76
static SalOp * sal_find_in_subscribe_by_call_id(Sal *sal, osip_call_id_t *call_id){
	const MSList *elem;
	SalOp *op;
	for(elem=sal->in_subscribes;elem!=NULL;elem=elem->next){
		op=(SalOp*)elem->data;
		if (op->call_id && osip_call_id_match(op->call_id,call_id)==0)
			return op;
	}
	return NULL;
}

static void sal_add_in_subscribe(Sal *sal, SalOp *op, osip_message_t *subs){
	osip_call_id_clone(subs->call_id,&op->call_id);
Simon Morlat's avatar
Simon Morlat committed
77 78 79
	sal->in_subscribes=ms_list_append(sal->in_subscribes,op);
}

80 81
void sal_remove_in_subscribe(Sal *sal, SalOp *op){
	sal->in_subscribes=ms_list_remove(sal->in_subscribes,op);
Simon Morlat's avatar
Simon Morlat committed
82 83 84 85 86
}

int sal_text_send(SalOp *op, const char *from, const char *to, const char *msg){
	osip_message_t *sip=NULL;

Simon Morlat's avatar
Simon Morlat committed
87 88 89 90 91 92 93 94
	if(op->cid == -1)
	{
		/* we are not currently in communication with the destination */
		if (from)
			sal_op_set_from(op,from);
		if (to)
			sal_op_set_to(op,to);

95
		sal_exosip_fix_route(op);
Simon Morlat's avatar
Simon Morlat committed
96 97 98
		eXosip_lock();
		eXosip_message_build_request(&sip,"MESSAGE",sal_op_get_to(op),
			sal_op_get_from(op),sal_op_get_route(op));
99 100 101 102 103 104 105 106
		if (sip!=NULL){
			osip_message_set_content_type(sip,"text/plain");
			osip_message_set_body(sip,msg,strlen(msg));
			sal_add_other(op->base.root,op,sip);
			eXosip_message_send_request(sip);
		}else{
			ms_error("Could not build MESSAGE request !");
		}
Simon Morlat's avatar
Simon Morlat committed
107 108 109 110 111 112 113
		eXosip_unlock();
	}
	else
	{
		/* we are currently in communication with the destination */
		eXosip_lock();
		//First we generate an INFO message to get the current call_id and a good cseq
114
		eXosip_call_build_request(op->did,"MESSAGE",&sip);
Simon Morlat's avatar
Simon Morlat committed
115 116 117 118 119 120 121 122
		if(sip == NULL)
		{
			ms_warning("could not get a build info to send MESSAGE, maybe no previous call established ?");
			eXosip_unlock();
			return -1;
		}
		osip_message_set_content_type(sip,"text/plain");
		osip_message_set_body(sip,msg,strlen(msg));
123
		eXosip_call_send_request(op->did,sip);
Simon Morlat's avatar
Simon Morlat committed
124 125
		eXosip_unlock();
	}
Simon Morlat's avatar
Simon Morlat committed
126 127 128 129 130
	return 0;
}

/*presence Subscribe/notify*/
int sal_subscribe_presence(SalOp *op, const char *from, const char *to){
131
	osip_message_t *msg=NULL;
Simon Morlat's avatar
Simon Morlat committed
132 133 134 135
	if (from)
		sal_op_set_from(op,from);
	if (to)
		sal_op_set_to(op,to);
136
	sal_exosip_fix_route(op);
Simon Morlat's avatar
Simon Morlat committed
137 138 139
	eXosip_lock();
	eXosip_subscribe_build_initial_request(&msg,sal_op_get_to(op),sal_op_get_from(op),
	    	sal_op_get_route(op),"presence",600);
140
	if (msg==NULL){
141 142 143 144
		ms_error("Could not build subscribe request to %s",to);
		eXosip_unlock();
		return -1;
	}
145 146 147 148
	if (op->base.contact){
		_osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
		osip_message_set_contact(msg,op->base.contact);
	}
Simon Morlat's avatar
Simon Morlat committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	op->sid=eXosip_subscribe_send_initial_request(msg);
	eXosip_unlock();
	if (op->sid==-1){
		osip_message_free(msg);
		return -1;
	}
	sal_add_out_subscribe(op->base.root,op);
	return 0;
}

int sal_unsubscribe(SalOp *op){
	osip_message_t *msg=NULL;
	if (op->did==-1){
		ms_error("cannot unsubscribe, no dialog !");
		return -1;
	}
	eXosip_lock();
	eXosip_subscribe_build_refresh_request(op->did,&msg);
	if (msg){
		osip_message_set_expires(msg,"0");
		eXosip_subscribe_send_refresh_request(op->did,msg);
170 171
	}else ms_error("Could not build subscribe refresh request ! op->sid=%i, op->did=%i",
	    	op->sid,op->did);
Simon Morlat's avatar
Simon Morlat committed
172 173 174 175 176
	eXosip_unlock();
	return 0;
}

int sal_subscribe_accept(SalOp *op){
177
	osip_message_t *msg=NULL;
Simon Morlat's avatar
Simon Morlat committed
178
	eXosip_lock();
179
	eXosip_insubscription_build_answer(op->tid,202,&msg);
180 181 182 183 184
	if (msg==NULL){
		ms_error("Fail to build answer to subscribe.");
		eXosip_unlock();
		return -1;
	}
185 186 187 188
	if (op->base.contact){
		_osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
		osip_message_set_contact(msg,op->base.contact);
	}
189
	eXosip_insubscription_send_answer(op->tid,202,msg);
Simon Morlat's avatar
Simon Morlat committed
190 191 192 193 194 195 196 197 198 199 200
	eXosip_unlock();
	return 0;
}

int sal_subscribe_decline(SalOp *op){
	eXosip_lock();
	eXosip_insubscription_send_answer(op->tid,401,NULL);
	eXosip_unlock();
	return 0;
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static void mk_presence_body (const SalPresenceStatus online_status, const char *contact_info,
		char *buf, size_t buflen, presence_type_t ptype) {
  switch (ptype) {
    case RFCxxxx: {
	  /* definition from http://msdn.microsoft.com/en-us/library/cc246202%28PROT.10%29.aspx */
	  int atom_id = 1000;

	  if (online_status==SalPresenceOnline)
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
<address uri=\"%s\" priority=\"0.800000\">\n\
<status status=\"open\" />\n\
<msnsubstatus substatus=\"online\" />\n\
</address>\n\
</atom>\n\
</presence>", contact_info, atom_id, contact_info);
Simon Morlat's avatar
Simon Morlat committed
221

222 223 224 225 226 227 228 229 230 231 232 233 234 235
	  }
	  else if (online_status == SalPresenceBusy ||
			  online_status == SalPresenceDonotdisturb)
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
<address uri=\"%s\" priority=\"0.800000\">\n\
<status status=\"inuse\" />\n\
<msnsubstatus substatus=\"busy\" />\n\
</address>\n\
</atom>\n</presence>", contact_info, atom_id, contact_info);
Simon Morlat's avatar
Simon Morlat committed
236

237 238 239 240 241 242 243 244 245 246 247 248 249 250
	  }
	  else if (online_status==SalPresenceBerightback)
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
<address uri=\"%s\" priority=\"0.800000\">\n\
<status status=\"open\" />\n\
<msnsubstatus substatus=\"berightback\" />\n\
</address>\n\
</atom>\n\
</presence>", contact_info, atom_id, contact_info);
Simon Morlat's avatar
Simon Morlat committed
251

252 253 254 255 256 257
	  }
	  else if (online_status == SalPresenceAway ||
			  online_status == SalPresenceMoved)
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
Simon Morlat's avatar
Simon Morlat committed
258 259 260
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
261
<address uri=\"%s\" priority=\"0.800000\">\n\
Simon Morlat's avatar
Simon Morlat committed
262
<status status=\"open\" />\n\
263
<msnsubstatus substatus=\"away\" />\n\
Simon Morlat's avatar
Simon Morlat committed
264 265 266 267
</address>\n\
</atom>\n\
</presence>", contact_info, atom_id, contact_info);

268 269 270 271 272
	  }
	  else if (online_status==SalPresenceOnthephone)
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
Simon Morlat's avatar
Simon Morlat committed
273 274 275
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
276
<address uri=\"%s\" priority=\"0.800000\">\n\
Simon Morlat's avatar
Simon Morlat committed
277
<status status=\"inuse\" />\n\
278
<msnsubstatus substatus=\"onthephone\" />\n\
Simon Morlat's avatar
Simon Morlat committed
279 280 281 282
</address>\n\
</atom>\n\
</presence>", contact_info, atom_id, contact_info);

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	  }
	  else if (online_status==SalPresenceOuttolunch)
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
<address uri=\"%s\" priority=\"0.800000\">\n\
<status status=\"open\" />\n\
<msnsubstatus substatus=\"outtolunch\" />\n\
</address>\n\
</atom>\n\
</presence>", contact_info, atom_id, contact_info);

	  }
	  else
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
<address uri=\"%s\" priority=\"0.800000\">\n\
<status status=\"closed\" />\n\
<msnsubstatus substatus=\"away\" />\n\
</address>\n\
</atom>\n\
</presence>", contact_info, atom_id, contact_info);
	  }
	  break;
    } 
    case MSOLDPRES: {
	/* Couldn't find schema http://schemas.microsoft.com/2002/09/sip/presence
 	*  so messages format has been taken from Communigate that can send notify
 	*  requests with this schema
 	*/
	  int atom_id = 1000;

	  if (online_status==SalPresenceOnline)
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
<address uri=\"%s\">\n\
<status status=\"open\" />\n\
<msnsubstatus substatus=\"online\" />\n\
</address>\n\
</atom>\n\
</presence>", contact_info, atom_id, contact_info);

	  }
	  else if (online_status == SalPresenceBusy ||
			  online_status == SalPresenceDonotdisturb)
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
<address uri=\"%s\">\n\
<status status=\"inuse\" />\n\
<msnsubstatus substatus=\"busy\" />\n\
</address>\n\
</atom>\n</presence>", contact_info, atom_id, contact_info);

	  }
	  else if (online_status==SalPresenceBerightback)
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
Simon Morlat's avatar
Simon Morlat committed
356 357 358
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
359
<address uri=\"%s\">\n\
Simon Morlat's avatar
Simon Morlat committed
360 361 362 363 364 365
<status status=\"inactive\" />\n\
<msnsubstatus substatus=\"berightback\" />\n\
</address>\n\
</atom>\n\
</presence>", contact_info, atom_id, contact_info);

366 367 368 369 370 371
	  }
	  else if (online_status == SalPresenceAway ||
			  online_status == SalPresenceMoved)
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
Simon Morlat's avatar
Simon Morlat committed
372 373 374
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
375
<address uri=\"%s\">\n\
Simon Morlat's avatar
Simon Morlat committed
376
<status status=\"inactive\" />\n\
377
<msnsubstatus substatus=\"idle\" />\n\
Simon Morlat's avatar
Simon Morlat committed
378 379 380 381
</address>\n\
</atom>\n\
</presence>", contact_info, atom_id, contact_info);

382 383 384 385 386
	  }
	  else if (online_status==SalPresenceOnthephone)
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
Simon Morlat's avatar
Simon Morlat committed
387 388 389
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
390
<address uri=\"%s\">\n\
Simon Morlat's avatar
Simon Morlat committed
391 392 393 394 395 396
<status status=\"inuse\" />\n\
<msnsubstatus substatus=\"onthephone\" />\n\
</address>\n\
</atom>\n\
</presence>", contact_info, atom_id, contact_info);

397 398 399 400 401
	  }
	  else if (online_status==SalPresenceOuttolunch)
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
Simon Morlat's avatar
Simon Morlat committed
402 403 404
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
405
<address uri=\"%s\">\n\
Simon Morlat's avatar
Simon Morlat committed
406 407 408 409 410 411
<status status=\"inactive\" />\n\
<msnsubstatus substatus=\"outtolunch\" />\n\
</address>\n\
</atom>\n\
</presence>", contact_info, atom_id, contact_info);

412 413 414 415 416
	  }
	  else
	  {
		  snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
Simon Morlat's avatar
Simon Morlat committed
417 418 419
<presence>\n\
<presentity uri=\"%s;method=SUBSCRIBE\" />\n\
<atom id=\"%i\">\n\
420 421 422
<address uri=\"%s\">\n\
<status status=\"closed\" />\n\
<msnsubstatus substatus=\"offline\" />\n\
Simon Morlat's avatar
Simon Morlat committed
423 424 425
</address>\n\
</atom>\n\
</presence>", contact_info, atom_id, contact_info);
426 427 428 429
	  }
	break;
	}
    default: { /* use pidf+xml as default format, rfc4479, rfc4480, rfc3863 */
Simon Morlat's avatar
Simon Morlat committed
430

431 432 433 434 435 436 437
	if (online_status==SalPresenceOnline)
	{
		snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \
xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" \
xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" \
entity=\"%s\">\n\
Simon Morlat's avatar
Simon Morlat committed
438
<tuple id=\"sg89ae\">\n\
439
<status><basic>open</basic></status>\n\
Simon Morlat's avatar
Simon Morlat committed
440 441 442
<contact priority=\"0.8\">%s</contact>\n\
</tuple>\n\
</presence>",
443 444 445 446 447 448 449 450 451 452
contact_info, contact_info);
	}
	else if (online_status == SalPresenceBusy ||
			online_status == SalPresenceDonotdisturb)
	{
		snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \
xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" \
xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" \
entity=\"%s\">\n\
Simon Morlat's avatar
Simon Morlat committed
453
<tuple id=\"sg89ae\">\n\
454
<status><basic>open</basic></status>\n\
Simon Morlat's avatar
Simon Morlat committed
455 456
<contact priority=\"0.8\">%s</contact>\n\
</tuple>\n\
457 458 459
<dm:person id=\"sg89aep\">\n\
<rpid:activities><rpid:busy/></rpid:activities>\n\
</dm:person>\n\
Simon Morlat's avatar
Simon Morlat committed
460
</presence>",
461 462 463 464 465 466 467 468 469
contact_info, contact_info);
	}
	else if (online_status==SalPresenceBerightback)
	{
		snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \
xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" \
xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" \
entity=\"%s\">\n\
Simon Morlat's avatar
Simon Morlat committed
470
<tuple id=\"sg89ae\">\n\
471
<status><basic>open</basic></status>\n\
Simon Morlat's avatar
Simon Morlat committed
472 473
<contact priority=\"0.8\">%s</contact>\n\
</tuple>\n\
474 475 476
<dm:person id=\"sg89aep\">\n\
<rpid:activities><rpid:in-transit/></rpid:activities>\n\
</dm:person>\n\
Simon Morlat's avatar
Simon Morlat committed
477
</presence>",
478 479 480 481 482 483 484 485 486 487
contact_info, contact_info);
	}
	else if (online_status == SalPresenceAway ||
			online_status == SalPresenceMoved)
	{
		snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \
xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" \
xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" \
entity=\"%s\">\n\
Simon Morlat's avatar
Simon Morlat committed
488
<tuple id=\"sg89ae\">\n\
489
<status><basic>open</basic></status>\n\
Simon Morlat's avatar
Simon Morlat committed
490 491
<contact priority=\"0.8\">%s</contact>\n\
</tuple>\n\
492 493 494
<dm:person id=\"sg89aep\">\n\
<rpid:activities><rpid:away/></rpid:activities>\n\
</dm:person>\n\
Simon Morlat's avatar
Simon Morlat committed
495
</presence>",
496 497 498 499 500 501 502 503 504
contact_info, contact_info);
	}
	else if (online_status==SalPresenceOnthephone)
	{
		snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \
xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" \
xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" \
entity=\"%s\">\n\
Simon Morlat's avatar
Simon Morlat committed
505
<tuple id=\"sg89ae\">\n\
506
<status><basic>open</basic></status>\n\
Simon Morlat's avatar
Simon Morlat committed
507 508
<contact priority=\"0.8\">%s</contact>\n\
</tuple>\n\
509 510 511
<dm:person id=\"sg89aep\">\n\
<rpid:activities><rpid:on-the-phone/></rpid:activities>\n\
</dm:person>\n\
Simon Morlat's avatar
Simon Morlat committed
512
</presence>",
513 514 515 516 517 518 519 520 521 522 523
contact_info, contact_info);
	}
	else if (online_status==SalPresenceOuttolunch)
	{
		snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \
xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" \
xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" \
entity=\"%s\">\n\
<tuple id=\"7777\">\n\
<status><basic>open</basic></status>\n\
Simon Morlat's avatar
Simon Morlat committed
524 525
<contact priority=\"0.8\">%s</contact>\n\
</tuple>\n\
526 527 528 529
<dm:person id=\"78787878\">\n\
<rpid:activities><rpid:meal/></rpid:activities>\n\
<rpid:note>Out to lunch</rpid:note> \n\
</dm:person>\n\
Simon Morlat's avatar
Simon Morlat committed
530
</presence>",
531 532 533 534 535 536 537 538 539 540 541 542
contact_info, contact_info);
	}
	else
	{
		snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \
xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" \
xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" \
entity=\"%s\">\n\
<tuple id=\"sg89ae\">\n\
<status><basic>closed</basic></status>\n\
<contact priority=\"0.8\">%s</contact>\n\
Simon Morlat's avatar
Simon Morlat committed
543
</tuple>\n\
544 545 546
</presence>\n", contact_info, contact_info);
	}
	break;
Simon Morlat's avatar
Simon Morlat committed
547
    }
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
 } // switch

}

static void add_presence_body(osip_message_t *notify, SalPresenceStatus online_status)
{
	char buf[1000];
	char *contact_info;

	osip_from_t *from=NULL;
	from=osip_message_get_from(notify);
	osip_uri_to_str(from->url,&contact_info);

	mk_presence_body (online_status, contact_info, buf, sizeof (buf), presence_style);

	osip_message_set_body(notify, buf, strlen(buf));
	osip_message_set_content_type(notify,
		presence_style ? "application/xpidf+xml" : "application/pidf+xml");
Simon Morlat's avatar
Simon Morlat committed
566 567 568 569 570 571

	osip_free(contact_info);
}


int sal_notify_presence(SalOp *op, SalPresenceStatus status, const char *status_message){
572
	osip_message_t *msg=NULL;
573
	eXosip_ss_t ss=EXOSIP_SUBCRSTATE_ACTIVE;
Simon Morlat's avatar
Simon Morlat committed
574 575 576 577
	if (op->nid==-1){
		ms_warning("Cannot notify, subscription was closed.");
		return -1;
	}
578
	
Simon Morlat's avatar
Simon Morlat committed
579
	eXosip_lock();
580
	eXosip_insubscription_build_notify(op->did,ss,DEACTIVATED,&msg);
Simon Morlat's avatar
Simon Morlat committed
581 582 583
	if (msg!=NULL){
		const char *identity=sal_op_get_contact(op);
		if (identity==NULL) identity=sal_op_get_to(op);
Simon Morlat's avatar
Simon Morlat committed
584
		_osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
Simon Morlat's avatar
Simon Morlat committed
585 586 587 588 589 590 591 592
		osip_message_set_contact(msg,identity);
		add_presence_body(msg,status);
		eXosip_insubscription_send_request(op->did,msg);
	}else ms_error("could not create notify for incoming subscription.");
	eXosip_unlock();
	return 0;
}

593
int sal_notify_close(SalOp *op){
594
	osip_message_t *msg=NULL;
595 596 597 598 599 600
	eXosip_lock();
	eXosip_insubscription_build_notify(op->did,EXOSIP_SUBCRSTATE_TERMINATED,DEACTIVATED,&msg);
	if (msg!=NULL){
		const char *identity=sal_op_get_contact(op);
		if (identity==NULL) identity=sal_op_get_to(op);
		osip_message_set_contact(msg,identity);
601
		add_presence_body(msg,SalPresenceOffline);
602
		eXosip_insubscription_send_request(op->did,msg);
603 604
	}else ms_error("sal_notify_close(): could not create notify for incoming subscription"
	    " did=%i, nid=%i",op->did,op->nid);
605 606 607 608 609 610 611 612 613
	eXosip_unlock();
	return 0;
}

int sal_publish(SalOp *op, const char *from, const char *to, SalPresenceStatus presence_mode){
	osip_message_t *pub;
	int i;
	char buf[1024];

614
	mk_presence_body (presence_mode, from, buf, sizeof (buf), presence_style);
615

616 617
	i = eXosip_build_publish(&pub,from, to, NULL, "presence", "300", 
		presence_style ? "application/xpidf+xml" : "application/pidf+xml", buf);
618 619 620 621 622 623 624 625 626 627 628 629 630
	if (i<0){
		ms_warning("Failed to build publish request.");
		return -1;
	}

	eXosip_lock();
	i = eXosip_publish(pub, to); /* should update the sip-if-match parameter
				    from sip-etag  from last 200ok of PUBLISH */
	eXosip_unlock();
	if (i<0){
	  ms_message("Failed to send publish request.");
	  return -1;
	}
Simon Morlat's avatar
Simon Morlat committed
631
	sal_add_other(sal_op_get_sal(op),op,pub);
632 633 634
	return 0;
}

635
static void _sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){	
Simon Morlat's avatar
Simon Morlat committed
636 637 638 639 640 641 642 643 644 645 646
	SalOp *op=sal_op_new(sal);
	char *tmp;
	op->did=ev->did;
	op->tid=ev->tid;
	op->nid=ev->nid;
	osip_from_to_str(ev->request->from,&tmp);
	sal_op_set_from(op,tmp);
	ms_free(tmp);
	osip_from_to_str(ev->request->to,&tmp);
	sal_op_set_to(op,tmp);
	ms_free(tmp);
647
	sal_add_in_subscribe(sal,op,ev->request);
Simon Morlat's avatar
Simon Morlat committed
648 649 650
	sal->callbacks.subscribe_received(op,sal_op_get_from(op));
}

651 652 653 654 655 656 657 658 659 660 661 662
void sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){	
	/*workaround a bug in eXosip: incoming SUBSCRIBES within dialog with expires: 0 are
	 recognized as new incoming subscribes*/
	SalOp *op=sal_find_in_subscribe_by_call_id(sal,ev->request->call_id);
	if (op){
		osip_header_t *h;
		osip_message_header_get_byname(ev->request,"expires",0,&h);
		if (h && h->hvalue && atoi(h->hvalue)==0){
			ms_warning("This susbscribe is not a new one but terminates an old one.");
			ev->did=op->did;
			ev->nid=op->nid;
			sal_exosip_subscription_closed(sal,ev);
663 664 665 666 667 668 669 670
		}else {
			osip_message_t *msg=NULL;
			ms_warning("Probably a refresh subscribe");
			eXosip_lock();
			eXosip_insubscription_build_answer(ev->tid,202,&msg);
			eXosip_insubscription_send_answer(ev->tid,202,msg);
			eXosip_unlock();
		}
671 672 673
	}else _sal_exosip_subscription_recv(sal,ev);
}

Simon Morlat's avatar
Simon Morlat committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
void sal_exosip_notify_recv(Sal *sal, eXosip_event_t *ev){
	SalOp *op=sal_find_out_subscribe(sal,ev->sid);
	char *tmp;
	osip_from_t *from=NULL;
	osip_body_t *body=NULL;
	SalPresenceStatus estatus=SalPresenceOffline;
	
	ms_message("Receiving notify with sid=%i,nid=%i",ev->sid,ev->nid);

	if (op==NULL){
		ms_error("No operation related to this notify !");
		return;
	}
	if (ev->request==NULL) return;

	from=ev->request->from;
	osip_message_get_body(ev->request,0,&body);
	if (body==NULL){
		ms_error("No body in NOTIFY");
		return;
	}
	osip_from_to_str(from,&tmp);
	if (strstr(body->body,"pending")!=NULL){
		estatus=SalPresenceOffline;
	}else if (strstr(body->body,"busy")!=NULL){
		estatus=SalPresenceBusy;
	}else if (strstr(body->body,"berightback")!=NULL
			|| strstr(body->body,"in-transit")!=NULL ){
		estatus=SalPresenceBerightback;
703 704
	}else if (strstr(body->body,"away")!=NULL
			|| strstr(body->body,"idle")){
Simon Morlat's avatar
Simon Morlat committed
705 706 707 708 709 710 711 712 713
		estatus=SalPresenceAway;
	}else if (strstr(body->body,"onthephone")!=NULL
		|| strstr(body->body,"on-the-phone")!=NULL){
		estatus=SalPresenceOnthephone;
	}else if (strstr(body->body,"outtolunch")!=NULL
			|| strstr(body->body,"meal")!=NULL){
		estatus=SalPresenceOuttolunch;
	}else if (strstr(body->body,"closed")!=NULL){
		estatus=SalPresenceOffline;
714 715
	}else if ((strstr(body->body,"online")!=NULL) || (strstr(body->body,"open")!=NULL)) {
		estatus=SalPresenceOnline;
Simon Morlat's avatar
Simon Morlat committed
716 717 718 719 720
	}else{
		estatus=SalPresenceOffline;
	}
	ms_message("We are notified that %s has online status %i",tmp,estatus);
	if (ev->ss_status==EXOSIP_SUBCRSTATE_TERMINATED) {
721
		sal_remove_out_subscribe(sal,op);
Simon Morlat's avatar
Simon Morlat committed
722 723 724 725
		op->sid=-1;
		op->did=-1;
		ms_message("And outgoing subscription terminated by remote.");
	}
726
	sal->callbacks.notify_presence(op,op->sid!=-1 ? SalSubscribeActive : SalSubscribeTerminated, estatus,NULL);
727 728 729 730 731 732 733 734 735

	/* try to detect presence message style used by server,
 	 * and switch our presence messages to servers style */
	if (strstr (body->body, "//IETF//DTD RFCxxxx XPIDF 1.0//EN") != NULL) {
		presence_style = RFCxxxx;
	} else if (strstr(body->body,"http://schemas.microsoft.com/2002/09/sip/presence")!=NULL) {
		presence_style = MSOLDPRES;
	}
	
Simon Morlat's avatar
Simon Morlat committed
736 737 738 739 740 741 742 743 744 745 746 747
	osip_free(tmp);
}

void sal_exosip_subscription_answered(Sal *sal,eXosip_event_t *ev){
	SalOp *op=sal_find_out_subscribe(sal,ev->sid);
	if (op==NULL){
		ms_error("Subscription answered but no associated op !");
		return;
	}
	op->did=ev->did;
}

748 749 750
void sal_exosip_in_subscription_closed(Sal *sal, eXosip_event_t *ev){
	SalOp *op=sal_find_in_subscribe(sal,ev->nid);
	char *tmp;
Simon Morlat's avatar
Simon Morlat committed
751
	if (op==NULL){
752
		ms_error("Incoming subscription closed but no associated op !");
Simon Morlat's avatar
Simon Morlat committed
753 754
		return;
	}
755 756 757 758 759
	
	
	sal_remove_in_subscribe(sal,op);
	op->nid=-1;
	op->did=-1;
760 761 762 763 764
	if (ev->request){
		osip_from_to_str(ev->request->from,&tmp);
		sal->callbacks.subscribe_closed(op,tmp);
		osip_free(tmp);
	}
Simon Morlat's avatar
Simon Morlat committed
765 766
}

767 768 769 770 771 772 773 774 775
void sal_exosip_subscription_closed(Sal *sal,eXosip_event_t *ev){
	SalOp *op=sal_find_out_subscribe(sal,ev->sid);
	if (op==NULL){
		ms_error("Subscription closed but no associated op !");
		return;
	}
	sal_remove_out_subscribe(sal,op);
	op->sid=-1;
	op->did=-1;
776
	sal->callbacks.notify_presence(op,SalSubscribeTerminated, SalPresenceOffline,NULL);
777 778
}

779