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