]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2_presence.c
start to fix bugs for presence support.
[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
24 static SalOp * sal_find_out_subscribe(Sal *sal, int sid){
25         const MSList *elem;
26         SalOp *op;
27         for(elem=sal->out_subscribes;elem!=NULL;elem=elem->next){
28                 op=(SalOp*)elem->data;
29                 if (op->sid==sid) return op;
30         }
31         return NULL;
32 }
33
34 static void sal_add_out_subscribe(Sal *sal, SalOp *op){
35         sal->out_subscribes=ms_list_append(sal->out_subscribes,op);
36 }
37
38 static void sal_remove_out_subscribe(Sal *sal, int sid){
39         MSList *elem;
40         SalOp *op;
41         for(elem=sal->out_subscribes;elem!=NULL;elem=elem->next){
42                 op=(SalOp*)elem->data;
43                 if (op->sid==sid) {
44                         sal->out_subscribes=ms_list_remove_link(sal->out_subscribes,elem);
45                         return;
46                 }
47         }
48 }
49
50 static SalOp * sal_find_in_subscribe(Sal *sal, int nid){
51         const MSList *elem;
52         SalOp *op;
53         for(elem=sal->in_subscribes;elem!=NULL;elem=elem->next){
54                 op=(SalOp*)elem->data;
55                 if (op->nid==nid) return op;
56         }
57         return NULL;
58 }
59
60 static void sal_add_in_subscribe(Sal *sal, SalOp *op){
61         sal->in_subscribes=ms_list_append(sal->in_subscribes,op);
62 }
63
64 static void sal_remove_in_subscribe(Sal *sal, int nid){
65         MSList *elem;
66         SalOp *op;
67         for(elem=sal->in_subscribes;elem!=NULL;elem=elem->next){
68                 op=(SalOp*)elem->data;
69                 if (op->nid==nid) {
70                         sal->in_subscribes=ms_list_remove_link(sal->in_subscribes,elem);
71                         return;
72                 }
73         }
74 }
75
76 int sal_text_send(SalOp *op, const char *from, const char *to, const char *msg){
77         osip_message_t *sip=NULL;
78         if (from)
79                 sal_op_set_from(op,from);
80         if (to)
81                 sal_op_set_to(op,to);
82
83         eXosip_lock();
84         eXosip_message_build_request(&sip,"MESSAGE",sal_op_get_to(op),
85             sal_op_get_from(op),sal_op_get_route(op));
86         osip_message_set_content_type(sip,"text/plain");
87         osip_message_set_body(sip,msg,strlen(msg));
88         eXosip_message_send_request(sip);
89         eXosip_unlock();
90         return 0;
91 }
92
93 /*presence Subscribe/notify*/
94 int sal_subscribe_presence(SalOp *op, const char *from, const char *to){
95         osip_message_t *msg;
96         if (from)
97                 sal_op_set_from(op,from);
98         if (to)
99                 sal_op_set_to(op,to);
100         sal_exosip_fix_route(op);
101         eXosip_lock();
102         eXosip_subscribe_build_initial_request(&msg,sal_op_get_to(op),sal_op_get_from(op),
103                 sal_op_get_route(op),"presence",600);
104         op->sid=eXosip_subscribe_send_initial_request(msg);
105         eXosip_unlock();
106         if (op->sid==-1){
107                 osip_message_free(msg);
108                 return -1;
109         }
110         sal_add_out_subscribe(op->base.root,op);
111         return 0;
112 }
113
114 int sal_unsubscribe(SalOp *op){
115         osip_message_t *msg=NULL;
116         if (op->did==-1){
117                 ms_error("cannot unsubscribe, no dialog !");
118                 return -1;
119         }
120         eXosip_lock();
121         eXosip_subscribe_build_refresh_request(op->did,&msg);
122         if (msg){
123                 osip_message_set_expires(msg,"0");
124                 eXosip_subscribe_send_refresh_request(op->did,msg);
125         }else ms_error("Could not build subscribe refresh request ! op->sid=%i, op->did=%i",
126                 op->sid,op->did);
127         eXosip_unlock();
128         return 0;
129 }
130
131 int sal_subscribe_accept(SalOp *op){
132         osip_message_t *msg;
133         eXosip_lock();
134         eXosip_insubscription_build_answer(op->tid,200,&msg);
135         eXosip_insubscription_send_answer(op->tid,200,msg);
136         eXosip_unlock();
137         return 0;
138 }
139
140 int sal_subscribe_decline(SalOp *op){
141         eXosip_lock();
142         eXosip_insubscription_send_answer(op->tid,401,NULL);
143         eXosip_unlock();
144         return 0;
145 }
146
147 static void add_presence_body(osip_message_t *notify, SalPresenceStatus online_status)
148 {
149         char buf[1000];
150 #ifdef SUPPORT_MSN
151         int atom_id = 1000;
152 #endif
153         char *contact_info;
154
155         osip_contact_t *ct=NULL;
156         osip_message_get_contact(notify,0,&ct);
157         osip_contact_to_str(ct,&contact_info);
158
159 #ifdef SUPPORT_MSN
160
161   if (online_status==SalPresenceOnline)
162     {
163       sprintf(buf, "<?xml version=\"1.0\"?>\n\
164 <!DOCTYPE presence\n\
165 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
166 <presence>\n\
167 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
168 <atom id=\"%i\">\n\
169 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
170 <status status=\"open\" />\n\
171 <msnsubstatus substatus=\"online\" />\n\
172 </address>\n\
173 </atom>\n\
174 </presence>", contact_info, atom_id, contact_info);
175
176     }
177   else if (online_status==SalPresenceBusy)
178     {
179       sprintf(buf, "<?xml version=\"1.0\"?>\n\
180 <!DOCTYPE presence\n\
181 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
182 <presence>\n\
183 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
184 <atom id=\"%i\">\n\
185 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
186 <status status=\"inuse\" />\n\
187 <msnsubstatus substatus=\"busy\" />\n\
188 </address>\n\
189 </atom>\n\
190 </presence>", contact_info, atom_id, contact_info);
191
192     }
193   else if (online_status==SalPresenceBerightback)
194     {
195       sprintf(buf, "<?xml version=\"1.0\"?>\n\
196 <!DOCTYPE presence\n\
197 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
198 <presence>\n\
199 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
200 <atom id=\"%i\">\n\
201 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
202 <status status=\"inactive\" />\n\
203 <msnsubstatus substatus=\"berightback\" />\n\
204 </address>\n\
205 </atom>\n\
206 </presence>", contact_info, atom_id, contact_info);
207
208     }
209   else if (online_status==SalPresenceAway)
210     {
211       sprintf(buf, "<?xml version=\"1.0\"?>\n\
212 <!DOCTYPE presence\n\
213 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;user=ip\" priority=\"0.800000\">\n\
218 <status status=\"inactive\" />\n\
219 <msnsubstatus substatus=\"away\" />\n\
220 </address>\n\
221 </atom>\n\
222 </presence>", contact_info, atom_id, contact_info);
223
224     }
225   else if (online_status==SalPresenceOnthephone)
226     {
227       sprintf(buf, "<?xml version=\"1.0\"?>\n\
228 <!DOCTYPE presence\n\
229 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;user=ip\" priority=\"0.800000\">\n\
234 <status status=\"inuse\" />\n\
235 <msnsubstatus substatus=\"onthephone\" />\n\
236 </address>\n\
237 </atom>\n\
238 </presence>", contact_info, atom_id, contact_info);
239
240     }
241   else if (online_status==SalPresenceOuttolunch)
242     {
243       sprintf(buf, "<?xml version=\"1.0\"?>\n\
244 <!DOCTYPE presence\n\
245 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
246 <presence>\n\
247 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
248 <atom id=\"%i\">\n\
249 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
250 <status status=\"inactive\" />\n\
251 <msnsubstatus substatus=\"outtolunch\" />\n\
252 </address>\n\
253 </atom>\n\
254 </presence>", contact_info, atom_id, contact_info);
255
256     }
257   else
258     {
259       sprintf(buf, "<?xml version=\"1.0\"?>\n\
260 <!DOCTYPE presence\n\
261 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;user=ip\" priority=\"0.800000\">\n\
266 <status status=\"inactive\" />\n\
267 <msnsubstatus substatus=\"away\" />\n\
268 </address>\n\
269 </atom>\n\
270 </presence>", contact_info, atom_id, contact_info);
271     }
272
273   osip_message_set_body(notify, buf, strlen(buf));
274   osip_message_set_content_type(notify, "application/xpidf+xml");
275 #else
276
277   if (online_status==SalPresenceOnline)
278     {
279       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
280 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
281           entity=\"%s\">\n\
282 <tuple id=\"sg89ae\">\n\
283 <status>\n\
284 <basic>open</basic>\n\
285 </status>\n\
286 <contact priority=\"0.8\">%s</contact>\n\
287 <note>online</note>\n\
288 </tuple>\n\
289 </presence>",
290               contact_info, contact_info);
291     }
292   else if (online_status==SalPresenceBusy)
293     {
294       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
295 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
296           xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
297           entity=\"%s\">\n\
298 <tuple id=\"sg89ae\">\n\
299 <status>\n\
300 <basic>open</basic>\n\
301 <es:activities>\n\
302   <es:activity>busy</es:activity>\n\
303 </es:activities>\n\
304 </status>\n\
305 <contact priority=\"0.8\">%s</contact>\n\
306 <note>busy</note>\n\
307 </tuple>\n\
308 </presence>",
309               contact_info, contact_info);
310     }
311   else if (online_status==SalPresenceBerightback)
312     {
313       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
314 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
315           xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
316           entity=\"%s\">\n\
317 <tuple id=\"sg89ae\">\n\
318 <status>\n\
319 <basic>open</basic>\n\
320 <es:activities>\n\
321   <es:activity>in-transit</es:activity>\n\
322 </es:activities>\n\
323 </status>\n\
324 <contact priority=\"0.8\">%s</contact>\n\
325 <note>be right back</note>\n\
326 </tuple>\n\
327 </presence>",
328               contact_info, contact_info);
329     }
330   else if (online_status==SalPresenceAway)
331     {
332       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
333 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
334           xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
335           entity=\"%s\">\n\
336 <tuple id=\"sg89ae\">\n\
337 <status>\n\
338 <basic>open</basic>\n\
339 <es:activities>\n\
340   <es:activity>away</es:activity>\n\
341 </es:activities>\n\
342 </status>\n\
343 <contact priority=\"0.8\">%s</contact>\n\
344 <note>away</note>\n\
345 </tuple>\n\
346 </presence>",
347               contact_info, contact_info);
348     }
349   else if (online_status==SalPresenceOnthephone)
350     {
351       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
352 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
353           xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
354           entity=\"%s\">\n\
355 <tuple id=\"sg89ae\">\n\
356 <status>\n\
357 <basic>open</basic>\n\
358 <es:activities>\n\
359   <es:activity>on-the-phone</es:activity>\n\
360 </es:activities>\n\
361 </status>\n\
362 <contact priority=\"0.8\">%s</contact>\n\
363 <note>on the phone</note>\n\
364 </tuple>\n\
365 </presence>",
366               contact_info, contact_info);
367     }
368   else if (online_status==SalPresenceOuttolunch)
369     {
370       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
371 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
372           xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
373           entity=\"%s\">\n\
374 <tuple id=\"sg89ae\">\n\
375 <status>\n\
376 <basic>open</basic>\n\
377 <es:activities>\n\
378   <es:activity>meal</es:activity>\n\
379 </es:activities>\n\
380 </status>\n\
381 <contact priority=\"0.8\">%s</contact>\n\
382 <note>out to lunch</note>\n\
383 </tuple>\n\
384 </presence>",
385               contact_info, contact_info);
386     }
387   else
388     {
389       /* */
390       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
391 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
392 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
393 entity=\"%s\">\n%s",
394               contact_info,
395 "<tuple id=\"sg89ae\">\n\
396 <status>\n\
397 <basic>closed</basic>\n\
398 <es:activities>\n\
399   <es:activity>permanent-absence</es:activity>\n\
400 </es:activities>\n\
401 </status>\n\
402 </tuple>\n\
403 \n</presence>\n");
404     }
405   osip_message_set_body(notify, buf, strlen(buf));
406   osip_message_set_content_type(notify, "application/pidf+xml");
407
408 #endif
409         osip_free(contact_info);
410 }
411
412
413 int sal_notify_presence(SalOp *op, SalPresenceStatus status, const char *status_message){
414         osip_message_t *msg;
415         eXosip_ss_t ss=EXOSIP_SUBCRSTATE_ACTIVE;
416         if (op->nid==-1){
417                 ms_warning("Cannot notify, subscription was closed.");
418                 return -1;
419         }
420         
421         eXosip_lock();
422         eXosip_insubscription_build_notify(op->did,ss,DEACTIVATED,&msg);
423         if (msg!=NULL){
424                 const char *identity=sal_op_get_contact(op);
425                 if (identity==NULL) identity=sal_op_get_to(op);
426                 osip_message_set_contact(msg,identity);
427                 add_presence_body(msg,status);
428                 eXosip_insubscription_send_request(op->did,msg);
429         }else ms_error("could not create notify for incoming subscription.");
430         eXosip_unlock();
431         return 0;
432 }
433
434 int sal_notify_close(SalOp *op){
435         osip_message_t *msg;
436         eXosip_lock();
437         eXosip_insubscription_build_notify(op->did,EXOSIP_SUBCRSTATE_TERMINATED,DEACTIVATED,&msg);
438         if (msg!=NULL){
439                 const char *identity=sal_op_get_contact(op);
440                 if (identity==NULL) identity=sal_op_get_to(op);
441                 osip_message_set_contact(msg,identity);
442                 eXosip_insubscription_send_request(op->did,msg);
443         }else ms_error("could not create notify for incoming subscription.");
444         eXosip_unlock();
445         return 0;
446 }
447
448 int sal_publish(SalOp *op, const char *from, const char *to, SalPresenceStatus presence_mode){
449         osip_message_t *pub;
450         int i;
451         char buf[1024];
452
453         if (presence_mode==SalPresenceOnline)
454         {
455           snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
456         <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
457                   entity=\"%s\">\n\
458         <tuple id=\"sg89ae\">\n\
459         <status>\n\
460         <basic>open</basic>\n\
461         </status>\n\
462         <contact priority=\"0.8\">%s</contact>\n\
463         <note>online</note>\n\
464         </tuple>\n\
465         </presence>",
466                    from, from);
467         }
468         else if (presence_mode==SalPresenceBusy
469            ||presence_mode==SalPresenceDonotdisturb)
470         {
471           snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
472         <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
473                   xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
474                   entity=\"%s\">\n\
475         <tuple id=\"sg89ae\">\n\
476         <status>\n\
477         <basic>open</basic>\n\
478         <es:activities>\n\
479         <es:activity>busy</es:activity>\n\
480         </es:activities>\n\
481         </status>\n\
482         <contact priority=\"0.8\">%s</contact>\n\
483         <note>busy</note>\n\
484         </tuple>\n\
485         </presence>",
486                   from, from);
487         }
488         else if (presence_mode==SalPresenceBerightback)
489         {
490                 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
491         <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
492                   xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
493                   entity=\"%s\">\n\
494         <tuple id=\"sg89ae\">\n\
495         <status>\n\
496         <basic>open</basic>\n\
497         <es:activities>\n\
498         <es:activity>in-transit</es:activity>\n\
499         </es:activities>\n\
500         </status>\n\
501         <contact priority=\"0.8\">%s</contact>\n\
502         <note>be right back</note>\n\
503         </tuple>\n\
504         </presence>",
505                   from,from);
506         }
507         else if (presence_mode==SalPresenceAway
508            ||presence_mode==SalPresenceMoved)
509         {
510                 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
511         <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
512                   xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
513                   entity=\"%s\">\n\
514         <tuple id=\"sg89ae\">\n\
515         <status>\n\
516         <basic>open</basic>\n\
517         <es:activities>\n\
518         <es:activity>away</es:activity>\n\
519         </es:activities>\n\
520         </status>\n\
521         <contact priority=\"0.8\">%s</contact>\n\
522         <note>away</note>\n\
523         </tuple>\n\
524         </presence>",
525                   from, from);
526         }
527         else if (presence_mode==SalPresenceOnthephone)
528         {
529           snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
530         <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
531                   xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
532                   entity=\"%s\">\n\
533         <tuple id=\"sg89ae\">\n\
534         <status>\n\
535         <basic>open</basic>\n\
536         <es:activities>\n\
537         <es:activity>on-the-phone</es:activity>\n\
538         </es:activities>\n\
539         </status>\n\
540         <contact priority=\"0.8\">%s</contact>\n\
541         <note>on the phone</note>\n\
542         </tuple>\n\
543         </presence>",
544                   from, from);
545         }
546         else if (presence_mode==SalPresenceOuttolunch)
547         {
548           snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
549         <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
550                   xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
551                   entity=\"%s\">\n\
552         <tuple id=\"sg89ae\">\n\
553         <status>\n\
554         <basic>open</basic>\n\
555         <es:activities>\n\
556         <es:activity>meal</es:activity>\n\
557         </es:activities>\n\
558         </status>\n\
559         <contact priority=\"0.8\">%s</contact>\n\
560         <note>out to lunch</note>\n\
561         </tuple>\n\
562         </presence>",
563                   from, from);
564         }
565         else{ 
566           /* offline */
567           snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
568         <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
569         xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
570         entity=\"%s\">\n%s",
571                   from,
572         "<tuple id=\"sg89ae\">\n\
573         <status>\n\
574         <basic>closed</basic>\n\
575         <es:activities>\n\
576         <es:activity>permanent-absence</e:activity>\n\
577         </es:activities>\n\
578         </status>\n\
579         </tuple>\n\
580         \n</presence>\n");
581         }
582
583         i = eXosip_build_publish(&pub,from, to, NULL, "presence", "1800", "application/pidf+xml", buf);
584         if (i<0){
585                 ms_warning("Failed to build publish request.");
586                 return -1;
587         }
588
589         eXosip_lock();
590         i = eXosip_publish(pub, to); /* should update the sip-if-match parameter
591                                     from sip-etag  from last 200ok of PUBLISH */
592         eXosip_unlock();
593         if (i<0){
594           ms_message("Failed to send publish request.");
595           return -1;
596         }
597         return 0;
598 }
599
600 void sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){
601         SalOp *op=sal_op_new(sal);
602         char *tmp;
603         op->did=ev->did;
604         op->tid=ev->tid;
605         op->nid=ev->nid;
606         osip_from_to_str(ev->request->from,&tmp);
607         sal_op_set_from(op,tmp);
608         ms_free(tmp);
609         osip_from_to_str(ev->request->to,&tmp);
610         sal_op_set_to(op,tmp);
611         ms_free(tmp);
612         sal_add_in_subscribe(sal,op);
613         sal->callbacks.subscribe_received(op,sal_op_get_from(op));
614 }
615
616 void sal_exosip_notify_recv(Sal *sal, eXosip_event_t *ev){
617         SalOp *op=sal_find_out_subscribe(sal,ev->sid);
618         char *tmp;
619         osip_from_t *from=NULL;
620         osip_body_t *body=NULL;
621         SalPresenceStatus estatus=SalPresenceOffline;
622         
623         ms_message("Receiving notify with sid=%i,nid=%i",ev->sid,ev->nid);
624
625         if (op==NULL){
626                 ms_error("No operation related to this notify !");
627                 return;
628         }
629         if (ev->request==NULL) return;
630
631         from=ev->request->from;
632         osip_message_get_body(ev->request,0,&body);
633         if (body==NULL){
634                 ms_error("No body in NOTIFY");
635                 return;
636         }
637         osip_from_to_str(from,&tmp);
638         if (strstr(body->body,"pending")!=NULL){
639                 estatus=SalPresenceOffline;
640         }else if (strstr(body->body,"busy")!=NULL){
641                 estatus=SalPresenceBusy;
642         }else if (strstr(body->body,"berightback")!=NULL
643                         || strstr(body->body,"in-transit")!=NULL ){
644                 estatus=SalPresenceBerightback;
645         }else if (strstr(body->body,"away")!=NULL){
646                 estatus=SalPresenceAway;
647         }else if (strstr(body->body,"onthephone")!=NULL
648                 || strstr(body->body,"on-the-phone")!=NULL){
649                 estatus=SalPresenceOnthephone;
650         }else if (strstr(body->body,"outtolunch")!=NULL
651                         || strstr(body->body,"meal")!=NULL){
652                 estatus=SalPresenceOuttolunch;
653         }else if (strstr(body->body,"closed")!=NULL){
654                 estatus=SalPresenceOffline;
655         }else if ((strstr(body->body,"online")!=NULL) || (strstr(body->body,"open")!=NULL)) {
656                 estatus=SalPresenceOnline;
657         }else{
658                 estatus=SalPresenceOffline;
659         }
660         ms_message("We are notified that %s has online status %i",tmp,estatus);
661         if (ev->ss_status==EXOSIP_SUBCRSTATE_TERMINATED) {
662                 sal_remove_out_subscribe(sal,op->sid);
663                 op->sid=-1;
664                 op->did=-1;
665                 ms_message("And outgoing subscription terminated by remote.");
666         }
667         sal->callbacks.notify(op,op->sid!=-1 ? SalSubscribeActive : SalSubscribeTerminated, estatus,NULL);
668         osip_free(tmp);
669 }
670
671 void sal_exosip_subscription_answered(Sal *sal,eXosip_event_t *ev){
672         SalOp *op=sal_find_out_subscribe(sal,ev->sid);
673         if (op==NULL){
674                 ms_error("Subscription answered but no associated op !");
675                 return;
676         }
677         op->did=ev->did;
678 }
679
680 void sal_exosip_subscription_closed(Sal *sal,eXosip_event_t *ev){
681         SalOp *op=sal_find_in_subscribe(sal,ev->sid);
682         if (op==NULL){
683                 ms_error("Subscription closed but no associated op !");
684                 return;
685         }
686         sal_remove_in_subscribe(sal,op->nid);
687         op->nid=-1;
688         op->did=0;
689 }
690
691