]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2_presence.c
add message storage
[linphone] / coreapi / sal_eXosip2_presence.c
1 /*
2 linphone
3 Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (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
20
21 #include "sal_eXosip2.h"
22
23 typedef enum {
24         PIDF = 0,
25         RFCxxxx = 1,
26         MSOLDPRES = 2
27 } presence_type_t;
28
29 /*
30  * REVISIT: this static variable forces every dialog to use the same presence description type depending 
31  * on what is received on a single dialog...
32  */
33 static presence_type_t presence_style = PIDF;
34
35 SalOp * sal_find_out_subscribe(Sal *sal, int sid){
36         const MSList *elem;
37         SalOp *op;
38         for(elem=sal->out_subscribes;elem!=NULL;elem=elem->next){
39                 op=(SalOp*)elem->data;
40                 if (op->sid==sid) return op;
41         }
42         ms_message("No op for sid %i",sid);
43         return NULL;
44 }
45
46 static void sal_add_out_subscribe(Sal *sal, SalOp *op){
47         sal->out_subscribes=ms_list_append(sal->out_subscribes,op);
48 }
49
50 void sal_remove_out_subscribe(Sal *sal, SalOp *op){
51         sal->out_subscribes=ms_list_remove(sal->out_subscribes,op);
52 }
53
54 SalOp * sal_find_in_subscribe(Sal *sal, int nid){
55         const MSList *elem;
56         SalOp *op;
57         for(elem=sal->in_subscribes;elem!=NULL;elem=elem->next){
58                 op=(SalOp*)elem->data;
59                 if (op->nid==nid) return op;
60         }
61         return NULL;
62 }
63
64 static SalOp * sal_find_in_subscribe_by_call_id(Sal *sal, osip_call_id_t *call_id){
65         const MSList *elem;
66         SalOp *op;
67         for(elem=sal->in_subscribes;elem!=NULL;elem=elem->next){
68                 op=(SalOp*)elem->data;
69                 if (op->call_id && osip_call_id_match(op->call_id,call_id)==0)
70                         return op;
71         }
72         return NULL;
73 }
74
75 static void sal_add_in_subscribe(Sal *sal, SalOp *op, osip_message_t *subs){
76         osip_call_id_clone(subs->call_id,&op->call_id);
77         sal->in_subscribes=ms_list_append(sal->in_subscribes,op);
78 }
79
80 void sal_remove_in_subscribe(Sal *sal, SalOp *op){
81         sal->in_subscribes=ms_list_remove(sal->in_subscribes,op);
82 }
83
84 int sal_message_send(SalOp *op, const char *from, const char *to, const char* content_type, const char *msg, const char *t){
85         osip_message_t *sip=NULL;
86
87         if(op->cid == -1)
88         {
89                 /* we are not currently in communication with the destination */
90                 if (from)
91                         sal_op_set_from(op,from);
92                 if (to)
93                         sal_op_set_to(op,to);
94
95                 sal_exosip_fix_route(op);
96                 eXosip_lock();
97                 eXosip_message_build_request(&sip,"MESSAGE",sal_op_get_to(op),
98                         sal_op_get_from(op),sal_op_get_route(op));
99                 if (sip!=NULL){
100                         sal_exosip_add_custom_headers(sip,op->base.custom_headers);
101                         osip_message_set_date(sip,t);
102                         osip_message_set_content_type(sip,content_type);
103                         if (msg) osip_message_set_body(sip,msg,strlen(msg));
104                         sal_add_other(op->base.root,op,sip);
105                         eXosip_message_send_request(sip);
106                 }else{
107                         ms_error("Could not build MESSAGE request !");
108                 }
109                 eXosip_unlock();
110         }
111         else
112         {
113                 /* we are currently in communication with the destination */
114                 eXosip_lock();
115                 //First we generate an INFO message to get the current call_id and a good cseq
116                 eXosip_call_build_request(op->did,"MESSAGE",&sip);
117                 if(sip == NULL)
118                 {
119                         ms_warning("could not get a build info to send MESSAGE, maybe no previous call established ?");
120                         eXosip_unlock();
121                         return -1;
122                 }
123                 osip_message_set_content_type(sip,content_type);
124                 if (msg) osip_message_set_body(sip,msg,strlen(msg));
125                 eXosip_call_send_request(op->did,sip);
126                 eXosip_unlock();
127         }
128         return 0;
129 }
130
131 int sal_text_send(SalOp *op, const char *from, const char *to, const char *msg,const char *t) {
132         return sal_message_send(op,from,to,"text/plain",msg,t);
133 }
134 /*presence Subscribe/notify*/
135 int sal_subscribe_presence(SalOp *op, const char *from, const char *to){
136         osip_message_t *msg=NULL;
137         if (from)
138                 sal_op_set_from(op,from);
139         if (to)
140                 sal_op_set_to(op,to);
141         sal_exosip_fix_route(op);
142         eXosip_lock();
143         eXosip_subscribe_build_initial_request(&msg,sal_op_get_to(op),sal_op_get_from(op),
144                 sal_op_get_route(op),"presence",600);
145         if (msg==NULL){
146                 ms_error("Could not build subscribe request to %s",to);
147                 eXosip_unlock();
148                 return -1;
149         }
150         if (op->base.contact){
151                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
152                 osip_message_set_contact(msg,op->base.contact);
153         }
154         op->sid=eXosip_subscribe_send_initial_request(msg);
155         eXosip_unlock();
156         if (op->sid==-1){
157                 osip_message_free(msg);
158                 return -1;
159         }
160         sal_add_out_subscribe(op->base.root,op);
161         return 0;
162 }
163
164 int sal_unsubscribe(SalOp *op){
165         osip_message_t *msg=NULL;
166         if (op->did==-1){
167                 ms_error("cannot unsubscribe, no dialog !");
168                 return -1;
169         }
170         eXosip_lock();
171         eXosip_subscribe_build_refresh_request(op->did,&msg);
172         if (msg){
173                 osip_message_set_expires(msg,"0");
174                 eXosip_subscribe_send_refresh_request(op->did,msg);
175         }else ms_error("Could not build subscribe refresh request ! op->sid=%i, op->did=%i",
176                 op->sid,op->did);
177         eXosip_unlock();
178         return 0;
179 }
180
181 int sal_subscribe_accept(SalOp *op){
182         osip_message_t *msg=NULL;
183         eXosip_lock();
184         eXosip_insubscription_build_answer(op->tid,202,&msg);
185         if (msg==NULL){
186                 ms_error("Fail to build answer to subscribe.");
187                 eXosip_unlock();
188                 return -1;
189         }
190         if (op->base.contact){
191                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
192                 osip_message_set_contact(msg,op->base.contact);
193         }
194         eXosip_insubscription_send_answer(op->tid,202,msg);
195         eXosip_unlock();
196         return 0;
197 }
198
199 int sal_subscribe_decline(SalOp *op){
200         eXosip_lock();
201         eXosip_insubscription_send_answer(op->tid,401,NULL);
202         eXosip_unlock();
203         return 0;
204 }
205
206 static void mk_presence_body (const SalPresenceStatus online_status, const char *contact_info,
207                 char *buf, size_t buflen, presence_type_t ptype) {
208   switch (ptype) {
209     case RFCxxxx: {
210           /* definition from http://msdn.microsoft.com/en-us/library/cc246202%28PROT.10%29.aspx */
211           int atom_id = 1000;
212
213           if (online_status==SalPresenceOnline)
214           {
215                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
216 "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
217 "<presence>\n"
218 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
219 "<atom id=\"%i\">\n"
220 "<address uri=\"%s\" priority=\"0.800000\">\n"
221 "<status status=\"open\" />\n"
222 "<msnsubstatus substatus=\"online\" />\n"
223 "</address>\n"
224 "</atom>\n"
225 "</presence>", contact_info, atom_id, contact_info);
226
227           }
228           else if (online_status == SalPresenceBusy ||
229                           online_status == SalPresenceDonotdisturb)
230           {
231                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
232 "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
233 "<presence>\n"
234 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
235 "<atom id=\"%i\">\n"
236 "<address uri=\"%s\" priority=\"0.800000\">\n"
237 "<status status=\"inuse\" />\n"
238 "<msnsubstatus substatus=\"busy\" />\n"
239 "</address>\n"
240 "</atom>\n</presence>", contact_info, atom_id, contact_info);
241
242           }
243           else if (online_status==SalPresenceBerightback)
244           {
245                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
246 "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
247 "<presence>\n"
248 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
249 "<atom id=\"%i\">\n"
250 "<address uri=\"%s\" priority=\"0.800000\">\n"
251 "<status status=\"open\" />\n"
252 "<msnsubstatus substatus=\"berightback\" />\n"
253 "</address>\n"
254 "</atom>\n"
255 "</presence>", contact_info, atom_id, contact_info);
256
257           }
258           else if (online_status == SalPresenceAway ||
259                           online_status == SalPresenceMoved)
260           {
261                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
262 "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
263 "<presence>\n"
264 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
265 "<atom id=\"%i\">\n"
266 "<address uri=\"%s\" priority=\"0.800000\">\n"
267 "<status status=\"open\" />\n"
268 "<msnsubstatus substatus=\"away\" />\n"
269 "</address>\n"
270 "</atom>\n"
271 "</presence>", contact_info, atom_id, contact_info);
272
273           }
274           else if (online_status==SalPresenceOnthephone)
275           {
276                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
277 "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
278 "<presence>\n"
279 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
280 "<atom id=\"%i\">\n"
281 "<address uri=\"%s\" priority=\"0.800000\">\n"
282 "<status status=\"inuse\" />\n"
283 "<msnsubstatus substatus=\"onthephone\" />\n"
284 "</address>\n"
285 "</atom>\n"
286 "</presence>", contact_info, atom_id, contact_info);
287
288           }
289           else if (online_status==SalPresenceOuttolunch)
290           {
291                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
292 "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
293 "<presence>\n"
294 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
295 "<atom id=\"%i\">\n"
296 "<address uri=\"%s\" priority=\"0.800000\">\n"
297 "<status status=\"open\" />\n"
298 "<msnsubstatus substatus=\"outtolunch\" />\n"
299 "</address>\n"
300 "</atom>\n"
301 "</presence>", contact_info, atom_id, contact_info);
302
303           }
304           else
305           {
306                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
307 "<!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n"
308 "<presence>\n"
309 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
310 "<atom id=\"%i\">\n"
311 "<address uri=\"%s\" priority=\"0.800000\">\n"
312 "<status status=\"closed\" />\n"
313 "<msnsubstatus substatus=\"away\" />\n"
314 "</address>\n"
315 "</atom>\n"
316 "</presence>", contact_info, atom_id, contact_info);
317           }
318           break;
319     } 
320     case MSOLDPRES: {
321         /* Couldn't find schema http://schemas.microsoft.com/2002/09/sip/presence
322         *  so messages format has been taken from Communigate that can send notify
323         *  requests with this schema
324         */
325           int atom_id = 1000;
326
327           if (online_status==SalPresenceOnline)
328           {
329                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
330 "<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n"
331 "<presence>\n"
332 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
333 "<atom id=\"%i\">\n"
334 "<address uri=\"%s\">\n"
335 "<status status=\"open\" />\n"
336 "<msnsubstatus substatus=\"online\" />\n"
337 "</address>\n"
338 "</atom>\n"
339 "</presence>", contact_info, atom_id, contact_info);
340
341           }
342           else if (online_status == SalPresenceBusy ||
343                           online_status == SalPresenceDonotdisturb)
344           {
345                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
346 "<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n"
347 "<presence>\n"
348 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
349 "<atom id=\"%i\">\n"
350 "<address uri=\"%s\">\n"
351 "<status status=\"inuse\" />\n"
352 "<msnsubstatus substatus=\"busy\" />\n"
353 "</address>\n"
354 "</atom>\n</presence>", contact_info, atom_id, contact_info);
355
356           }
357           else if (online_status==SalPresenceBerightback)
358           {
359                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
360 "<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n"
361 "<presence>\n"
362 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
363 "<atom id=\"%i\">\n"
364 "<address uri=\"%s\">\n"
365 "<status status=\"inactive\" />\n"
366 "<msnsubstatus substatus=\"berightback\" />\n"
367 "</address>\n"
368 "</atom>\n"
369 "</presence>", contact_info, atom_id, contact_info);
370
371           }
372           else if (online_status == SalPresenceAway ||
373                           online_status == SalPresenceMoved)
374           {
375                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
376 "<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n"
377 "<presence>\n"
378 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
379 "<atom id=\"%i\">\n"
380 "<address uri=\"%s\">\n"
381 "<status status=\"inactive\" />\n"
382 "<msnsubstatus substatus=\"idle\" />\n"
383 "</address>\n"
384 "</atom>\n"
385 "</presence>", contact_info, atom_id, contact_info);
386
387           }
388           else if (online_status==SalPresenceOnthephone)
389           {
390                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
391 "<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n"
392 "<presence>\n"
393 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
394 "<atom id=\"%i\">\n"
395 "<address uri=\"%s\">\n"
396 "<status status=\"inuse\" />\n"
397 "<msnsubstatus substatus=\"onthephone\" />\n"
398 "</address>\n"
399 "</atom>\n"
400 "</presence>", contact_info, atom_id, contact_info);
401
402           }
403           else if (online_status==SalPresenceOuttolunch)
404           {
405                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
406 "<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n"
407 "<presence>\n"
408 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
409 "<atom id=\"%i\">\n"
410 "<address uri=\"%s\">\n"
411 "<status status=\"inactive\" />\n"
412 "<msnsubstatus substatus=\"outtolunch\" />\n"
413 "</address>\n"
414 "</atom>\n"
415 "</presence>", contact_info, atom_id, contact_info);
416
417           }
418           else
419           {
420                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n"
421 "<!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n"
422 "<presence>\n"
423 "<presentity uri=\"%s;method=SUBSCRIBE\" />\n"
424 "<atom id=\"%i\">\n"
425 "<address uri=\"%s\">\n"
426 "<status status=\"closed\" />\n"
427 "<msnsubstatus substatus=\"offline\" />\n"
428 "</address>\n"
429 "</atom>\n"
430 "</presence>", contact_info, atom_id, contact_info);
431           }
432         break;
433         }
434     default: { /* use pidf+xml as default format, rfc4479, rfc4480, rfc3863 */
435
436         if (online_status==SalPresenceOnline)
437         {
438                 snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
439 "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" "
440 "xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" "
441 "xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" "
442 "entity=\"%s\">\n"
443 "<tuple id=\"sg89ae\">\n"
444 "<status><basic>open</basic></status>\n"
445 "<contact priority=\"0.8\">%s</contact>\n"
446 "</tuple>\n"
447 "</presence>",
448 contact_info, contact_info);
449         }
450         else if (online_status == SalPresenceBusy ||
451                         online_status == SalPresenceDonotdisturb)
452         {
453                 snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
454 "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" "
455 "xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" "
456 "xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" "
457 "entity=\"%s\">\n"
458 "<tuple id=\"sg89ae\">\n"
459 "<status><basic>open</basic></status>\n"
460 "<contact priority=\"0.8\">%s</contact>\n"
461 "</tuple>\n"
462 "<dm:person id=\"sg89aep\">\n"
463 "<rpid:activities><rpid:busy/></rpid:activities>\n"
464 "</dm:person>\n"
465 "</presence>",
466 contact_info, contact_info);
467         }
468         else if (online_status==SalPresenceBerightback)
469         {
470                 snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
471 "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" "
472 "xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" "
473 "xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" "
474 "entity=\"%s\">\n"
475 "<tuple id=\"sg89ae\">\n"
476 "<status><basic>open</basic></status>\n"
477 "<contact priority=\"0.8\">%s</contact>\n"
478 "</tuple>\n"
479 "<dm:person id=\"sg89aep\">\n"
480 "<rpid:activities><rpid:in-transit/></rpid:activities>\n"
481 "</dm:person>\n"
482 "</presence>",
483 contact_info, contact_info);
484         }
485         else if (online_status == SalPresenceAway ||
486                         online_status == SalPresenceMoved)
487         {
488                 snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
489 "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" "
490 "xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" "
491 "xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" "
492 "entity=\"%s\">\n"
493 "<tuple id=\"sg89ae\">\n"
494 "<status><basic>open</basic></status>\n"
495 "<contact priority=\"0.8\">%s</contact>\n"
496 "</tuple>\n"
497 "<dm:person id=\"sg89aep\">\n"
498 "<rpid:activities><rpid:away/></rpid:activities>\n"
499 "</dm:person>\n"
500 "</presence>",
501 contact_info, contact_info);
502         }
503         else if (online_status==SalPresenceOnthephone)
504         {
505                 snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
506 "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" "
507 "xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" "
508 "xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" "
509 "entity=\"%s\">\n"
510 "<tuple id=\"sg89ae\">\n"
511 "<status><basic>open</basic></status>\n"
512 "<contact priority=\"0.8\">%s</contact>\n"
513 "</tuple>\n"
514 "<dm:person id=\"sg89aep\">\n"
515 "<rpid:activities><rpid:on-the-phone/></rpid:activities>\n"
516 "</dm:person>\n"
517 "</presence>",
518 contact_info, contact_info);
519         }
520         else if (online_status==SalPresenceOuttolunch)
521         {
522                 snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
523 "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" "
524 "xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" "
525 "xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" "
526 "entity=\"%s\">\n"
527 "<tuple id=\"7777\">\n"
528 "<status><basic>open</basic></status>\n"
529 "<contact priority=\"0.8\">%s</contact>\n"
530 "</tuple>\n"
531 "<dm:person id=\"78787878\">\n"
532 "<rpid:activities><rpid:meal/></rpid:activities>\n"
533 "<rpid:note>Out to lunch</rpid:note> \n"
534 "</dm:person>\n"
535 "</presence>",
536 contact_info, contact_info);
537         }
538         else
539         {
540                 snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
541 "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" "
542 "xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" "
543 "xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" "
544 "entity=\"%s\">\n"
545 "<tuple id=\"sg89ae\">\n"
546 "<status><basic>closed</basic></status>\n"
547 "<contact priority=\"0.8\">%s</contact>\n"
548 "</tuple>\n"
549 "</presence>\n", contact_info, contact_info);
550         }
551         break;
552     }
553  } // switch
554
555 }
556
557 static void add_presence_body(osip_message_t *notify, SalPresenceStatus online_status)
558 {
559         char buf[1000];
560         char *contact_info;
561
562         osip_from_t *from=NULL;
563         from=osip_message_get_from(notify);
564         osip_uri_to_str(from->url,&contact_info);
565
566         mk_presence_body (online_status, contact_info, buf, sizeof (buf), presence_style);
567
568         osip_message_set_body(notify, buf, strlen(buf));
569         osip_message_set_content_type(notify,
570                 presence_style ? "application/xpidf+xml" : "application/pidf+xml");
571
572         osip_free(contact_info);
573 }
574
575
576 int sal_notify_presence(SalOp *op, SalPresenceStatus status, const char *status_message){
577         osip_message_t *msg=NULL;
578         eXosip_ss_t ss=EXOSIP_SUBCRSTATE_ACTIVE;
579         if (op->nid==-1){
580                 ms_warning("Cannot notify, subscription was closed.");
581                 return -1;
582         }
583         
584         eXosip_lock();
585         eXosip_insubscription_build_notify(op->did,ss,DEACTIVATED,&msg);
586         if (msg!=NULL){
587                 const char *identity=sal_op_get_contact(op);
588                 if (identity==NULL) identity=sal_op_get_to(op);
589                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
590                 osip_message_set_contact(msg,identity);
591                 add_presence_body(msg,status);
592                 eXosip_insubscription_send_request(op->did,msg);
593         }else ms_error("could not create notify for incoming subscription.");
594         eXosip_unlock();
595         return 0;
596 }
597
598 int sal_notify_close(SalOp *op){
599         osip_message_t *msg=NULL;
600         eXosip_lock();
601         eXosip_insubscription_build_notify(op->did,EXOSIP_SUBCRSTATE_TERMINATED,DEACTIVATED,&msg);
602         if (msg!=NULL){
603                 const char *identity=sal_op_get_contact(op);
604                 if (identity==NULL) identity=sal_op_get_to(op);
605                 osip_message_set_contact(msg,identity);
606                 add_presence_body(msg,SalPresenceOffline);
607                 eXosip_insubscription_send_request(op->did,msg);
608         }else ms_error("sal_notify_close(): could not create notify for incoming subscription"
609             " did=%i, nid=%i",op->did,op->nid);
610         eXosip_unlock();
611         return 0;
612 }
613
614 int sal_publish(SalOp *op, const char *from, const char *to, SalPresenceStatus presence_mode){
615         osip_message_t *pub;
616         int i;
617         char buf[1024];
618
619         mk_presence_body (presence_mode, from, buf, sizeof (buf), presence_style);
620
621         i = eXosip_build_publish(&pub,from, to, NULL, "presence", "300", 
622                 presence_style ? "application/xpidf+xml" : "application/pidf+xml", buf);
623         if (i<0){
624                 ms_warning("Failed to build publish request.");
625                 return -1;
626         }
627
628         eXosip_lock();
629         i = eXosip_publish(pub, to); /* should update the sip-if-match parameter
630                                     from sip-etag  from last 200ok of PUBLISH */
631         eXosip_unlock();
632         if (i<0){
633           ms_message("Failed to send publish request.");
634           return -1;
635         }
636         sal_add_other(sal_op_get_sal(op),op,pub);
637         return 0;
638 }
639
640 static void _sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){        
641         SalOp *op=sal_op_new(sal);
642         char *tmp;
643         op->did=ev->did;
644         op->tid=ev->tid;
645         op->nid=ev->nid;
646         osip_from_to_str(ev->request->from,&tmp);
647         sal_op_set_from(op,tmp);
648         ms_free(tmp);
649         osip_from_to_str(ev->request->to,&tmp);
650         sal_op_set_to(op,tmp);
651         ms_free(tmp);
652         sal_add_in_subscribe(sal,op,ev->request);
653         sal->callbacks.subscribe_received(op,sal_op_get_from(op));
654 }
655
656 void sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){        
657         /*workaround a bug in eXosip: incoming SUBSCRIBES within dialog with expires: 0 are
658          recognized as new incoming subscribes*/
659         SalOp *op=sal_find_in_subscribe_by_call_id(sal,ev->request->call_id);
660         if (op){
661                 osip_header_t *h;
662                 osip_message_header_get_byname(ev->request,"expires",0,&h);
663                 if (h && h->hvalue && atoi(h->hvalue)==0){
664                         ms_warning("This susbscribe is not a new one but terminates an old one.");
665                         ev->did=op->did;
666                         ev->nid=op->nid;
667                         sal_exosip_subscription_closed(sal,ev);
668                 }else {
669                         osip_message_t *msg=NULL;
670                         ms_warning("Probably a refresh subscribe");
671                         eXosip_lock();
672                         eXosip_insubscription_build_answer(ev->tid,202,&msg);
673                         eXosip_insubscription_send_answer(ev->tid,202,msg);
674                         eXosip_unlock();
675                 }
676         }else _sal_exosip_subscription_recv(sal,ev);
677 }
678
679 void sal_exosip_notify_recv(Sal *sal, eXosip_event_t *ev){
680         SalOp *op=sal_find_out_subscribe(sal,ev->sid);
681         char *tmp;
682         osip_from_t *from=NULL;
683         osip_body_t *body=NULL;
684         SalPresenceStatus estatus=SalPresenceOffline;
685         
686         ms_message("Receiving notify with sid=%i,nid=%i",ev->sid,ev->nid);
687
688         if (op==NULL){
689                 ms_error("No operation related to this notify !");
690                 return;
691         }
692         if (ev->request==NULL) return;
693
694         from=ev->request->from;
695         osip_message_get_body(ev->request,0,&body);
696         if (body==NULL){
697                 ms_error("No body in NOTIFY");
698                 return;
699         }
700         osip_from_to_str(from,&tmp);
701         if (strstr(body->body,"pending")!=NULL){
702                 estatus=SalPresenceOffline;
703         }else if (strstr(body->body,"busy")!=NULL){
704                 estatus=SalPresenceBusy;
705         }else if (strstr(body->body,"berightback")!=NULL
706                         || strstr(body->body,"in-transit")!=NULL ){
707                 estatus=SalPresenceBerightback;
708         }else if (strstr(body->body,"away")!=NULL
709                         || strstr(body->body,"idle")){
710                 estatus=SalPresenceAway;
711         }else if (strstr(body->body,"onthephone")!=NULL
712                 || strstr(body->body,"on-the-phone")!=NULL){
713                 estatus=SalPresenceOnthephone;
714         }else if (strstr(body->body,"outtolunch")!=NULL
715                         || strstr(body->body,"meal")!=NULL){
716                 estatus=SalPresenceOuttolunch;
717         }else if (strstr(body->body,"closed")!=NULL){
718                 estatus=SalPresenceOffline;
719         }else if ((strstr(body->body,"online")!=NULL) || (strstr(body->body,"open")!=NULL)) {
720                 estatus=SalPresenceOnline;
721         }else{
722                 estatus=SalPresenceOffline;
723         }
724         ms_message("We are notified that %s has online status %i",tmp,estatus);
725         if (ev->ss_status==EXOSIP_SUBCRSTATE_TERMINATED) {
726                 sal_remove_out_subscribe(sal,op);
727                 op->sid=-1;
728                 op->did=-1;
729                 ms_message("And outgoing subscription terminated by remote.");
730         }
731         sal->callbacks.notify_presence(op,op->sid!=-1 ? SalSubscribeActive : SalSubscribeTerminated, estatus,NULL);
732
733         /* try to detect presence message style used by server,
734          * and switch our presence messages to servers style */
735         if (strstr (body->body, "//IETF//DTD RFCxxxx XPIDF 1.0//EN") != NULL) {
736                 presence_style = RFCxxxx;
737         } else if (strstr(body->body,"http://schemas.microsoft.com/2002/09/sip/presence")!=NULL) {
738                 presence_style = MSOLDPRES;
739         }
740         
741         osip_free(tmp);
742 }
743
744 void sal_exosip_subscription_answered(Sal *sal,eXosip_event_t *ev){
745         SalOp *op=sal_find_out_subscribe(sal,ev->sid);
746         if (op==NULL){
747                 ms_error("Subscription answered but no associated op !");
748                 return;
749         }
750         op->did=ev->did;
751 }
752
753 void sal_exosip_in_subscription_closed(Sal *sal, eXosip_event_t *ev){
754         SalOp *op=sal_find_in_subscribe(sal,ev->nid);
755         char *tmp;
756         if (op==NULL){
757                 ms_error("Incoming subscription closed but no associated op !");
758                 return;
759         }
760         
761         
762         sal_remove_in_subscribe(sal,op);
763         op->nid=-1;
764         op->did=-1;
765         if (ev->request){
766                 osip_from_to_str(ev->request->from,&tmp);
767                 sal->callbacks.subscribe_closed(op,tmp);
768                 osip_free(tmp);
769         }
770 }
771
772 void sal_exosip_subscription_closed(Sal *sal,eXosip_event_t *ev){
773         SalOp *op=sal_find_out_subscribe(sal,ev->sid);
774         if (op==NULL){
775                 ms_error("Subscription closed but no associated op !");
776                 return;
777         }
778         sal_remove_out_subscribe(sal,op);
779         op->sid=-1;
780         op->did=-1;
781         sal->callbacks.notify_presence(op,SalSubscribeTerminated, SalPresenceOffline,NULL);
782 }
783
784