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