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