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