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