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