]> sjero.net Git - linphone/blob - coreapi/sal_eXosip2_presence.c
Merge branch 'master' of git.savannah.nongnu.org:/srv/git/linphone
[linphone] / coreapi / sal_eXosip2_presence.c
1 /*
2 linphone
3 Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20
21 #include "sal_eXosip2.h"
22
23 typedef enum {
24         PIDF = 0,
25         RFCxxxx = 1,
26         MSOLDPRES = 2
27 } presence_type_t;
28
29 /*
30  * REVISIT: this static variable forces every dialog to use the same presence description type depending 
31  * on what is received on a single dialog...
32  */
33 static presence_type_t presence_style = PIDF;
34
35 SalOp * sal_find_out_subscribe(Sal *sal, int sid){
36         const MSList *elem;
37         SalOp *op;
38         for(elem=sal->out_subscribes;elem!=NULL;elem=elem->next){
39                 op=(SalOp*)elem->data;
40                 if (op->sid==sid) return op;
41         }
42         ms_message("No op for sid %i",sid);
43         return NULL;
44 }
45
46 static void sal_add_out_subscribe(Sal *sal, SalOp *op){
47         sal->out_subscribes=ms_list_append(sal->out_subscribes,op);
48 }
49
50 void sal_remove_out_subscribe(Sal *sal, SalOp *op){
51         sal->out_subscribes=ms_list_remove(sal->out_subscribes,op);
52 }
53
54 SalOp * sal_find_in_subscribe(Sal *sal, int nid){
55         const MSList *elem;
56         SalOp *op;
57         for(elem=sal->in_subscribes;elem!=NULL;elem=elem->next){
58                 op=(SalOp*)elem->data;
59                 if (op->nid==nid) return op;
60         }
61         return NULL;
62 }
63
64 static SalOp * sal_find_in_subscribe_by_call_id(Sal *sal, osip_call_id_t *call_id){
65         const MSList *elem;
66         SalOp *op;
67         for(elem=sal->in_subscribes;elem!=NULL;elem=elem->next){
68                 op=(SalOp*)elem->data;
69                 if (op->call_id && osip_call_id_match(op->call_id,call_id)==0)
70                         return op;
71         }
72         return NULL;
73 }
74
75 static void sal_add_in_subscribe(Sal *sal, SalOp *op, osip_message_t *subs){
76         osip_call_id_clone(subs->call_id,&op->call_id);
77         sal->in_subscribes=ms_list_append(sal->in_subscribes,op);
78 }
79
80 void sal_remove_in_subscribe(Sal *sal, SalOp *op){
81         sal->in_subscribes=ms_list_remove(sal->in_subscribes,op);
82 }
83
84 int sal_text_send(SalOp *op, const char *from, const char *to, const char *msg){
85         osip_message_t *sip=NULL;
86
87         if(op->cid == -1)
88         {
89                 /* we are not currently in communication with the destination */
90                 if (from)
91                         sal_op_set_from(op,from);
92                 if (to)
93                         sal_op_set_to(op,to);
94
95                 eXosip_lock();
96                 eXosip_message_build_request(&sip,"MESSAGE",sal_op_get_to(op),
97                         sal_op_get_from(op),sal_op_get_route(op));
98                 if (sip!=NULL){
99                         osip_message_set_content_type(sip,"text/plain");
100                         osip_message_set_body(sip,msg,strlen(msg));
101                         sal_add_other(op->base.root,op,sip);
102                         eXosip_message_send_request(sip);
103                 }else{
104                         ms_error("Could not build MESSAGE request !");
105                 }
106                 eXosip_unlock();
107         }
108         else
109         {
110                 /* we are currently in communication with the destination */
111                 eXosip_lock();
112                 //First we generate an INFO message to get the current call_id and a good cseq
113                 eXosip_call_build_request(op->did,"MESSAGE",&sip);
114                 if(sip == NULL)
115                 {
116                         ms_warning("could not get a build info to send MESSAGE, maybe no previous call established ?");
117                         eXosip_unlock();
118                         return -1;
119                 }
120                 osip_message_set_content_type(sip,"text/plain");
121                 osip_message_set_body(sip,msg,strlen(msg));
122                 eXosip_call_send_request(op->did,sip);
123                 eXosip_unlock();
124         }
125         return 0;
126 }
127
128 /*presence Subscribe/notify*/
129 int sal_subscribe_presence(SalOp *op, const char *from, const char *to){
130         osip_message_t *msg=NULL;
131         if (from)
132                 sal_op_set_from(op,from);
133         if (to)
134                 sal_op_set_to(op,to);
135         sal_exosip_fix_route(op);
136         eXosip_lock();
137         eXosip_subscribe_build_initial_request(&msg,sal_op_get_to(op),sal_op_get_from(op),
138                 sal_op_get_route(op),"presence",600);
139         if (msg==NULL){
140                 ms_error("Could not build subscribe request to %s",to);
141                 eXosip_unlock();
142                 return -1;
143         }
144         if (op->base.contact){
145                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
146                 osip_message_set_contact(msg,op->base.contact);
147         }
148         op->sid=eXosip_subscribe_send_initial_request(msg);
149         eXosip_unlock();
150         if (op->sid==-1){
151                 osip_message_free(msg);
152                 return -1;
153         }
154         sal_add_out_subscribe(op->base.root,op);
155         return 0;
156 }
157
158 int sal_unsubscribe(SalOp *op){
159         osip_message_t *msg=NULL;
160         if (op->did==-1){
161                 ms_error("cannot unsubscribe, no dialog !");
162                 return -1;
163         }
164         eXosip_lock();
165         eXosip_subscribe_build_refresh_request(op->did,&msg);
166         if (msg){
167                 osip_message_set_expires(msg,"0");
168                 eXosip_subscribe_send_refresh_request(op->did,msg);
169         }else ms_error("Could not build subscribe refresh request ! op->sid=%i, op->did=%i",
170                 op->sid,op->did);
171         eXosip_unlock();
172         return 0;
173 }
174
175 int sal_subscribe_accept(SalOp *op){
176         osip_message_t *msg=NULL;
177         eXosip_lock();
178         eXosip_insubscription_build_answer(op->tid,202,&msg);
179         if (msg==NULL){
180                 ms_error("Fail to build answer to subscribe.");
181                 eXosip_unlock();
182                 return -1;
183         }
184         if (op->base.contact){
185                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
186                 osip_message_set_contact(msg,op->base.contact);
187         }
188         eXosip_insubscription_send_answer(op->tid,202,msg);
189         eXosip_unlock();
190         return 0;
191 }
192
193 int sal_subscribe_decline(SalOp *op){
194         eXosip_lock();
195         eXosip_insubscription_send_answer(op->tid,401,NULL);
196         eXosip_unlock();
197         return 0;
198 }
199
200 static void mk_presence_body (const SalPresenceStatus online_status, const char *contact_info,
201                 char *buf, size_t buflen, presence_type_t ptype) {
202   switch (ptype) {
203     case RFCxxxx: {
204           /* definition from http://msdn.microsoft.com/en-us/library/cc246202%28PROT.10%29.aspx */
205           int atom_id = 1000;
206
207           if (online_status==SalPresenceOnline)
208           {
209                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
210 <!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
211 <presence>\n\
212 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
213 <atom id=\"%i\">\n\
214 <address uri=\"%s\" priority=\"0.800000\">\n\
215 <status status=\"open\" />\n\
216 <msnsubstatus substatus=\"online\" />\n\
217 </address>\n\
218 </atom>\n\
219 </presence>", contact_info, atom_id, contact_info);
220
221           }
222           else if (online_status == SalPresenceBusy ||
223                           online_status == SalPresenceDonotdisturb)
224           {
225                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
226 <!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
227 <presence>\n\
228 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
229 <atom id=\"%i\">\n\
230 <address uri=\"%s\" priority=\"0.800000\">\n\
231 <status status=\"inuse\" />\n\
232 <msnsubstatus substatus=\"busy\" />\n\
233 </address>\n\
234 </atom>\n</presence>", contact_info, atom_id, contact_info);
235
236           }
237           else if (online_status==SalPresenceBerightback)
238           {
239                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
240 <!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
241 <presence>\n\
242 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
243 <atom id=\"%i\">\n\
244 <address uri=\"%s\" priority=\"0.800000\">\n\
245 <status status=\"open\" />\n\
246 <msnsubstatus substatus=\"berightback\" />\n\
247 </address>\n\
248 </atom>\n\
249 </presence>", contact_info, atom_id, contact_info);
250
251           }
252           else if (online_status == SalPresenceAway ||
253                           online_status == SalPresenceMoved)
254           {
255                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
256 <!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
257 <presence>\n\
258 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
259 <atom id=\"%i\">\n\
260 <address uri=\"%s\" priority=\"0.800000\">\n\
261 <status status=\"open\" />\n\
262 <msnsubstatus substatus=\"away\" />\n\
263 </address>\n\
264 </atom>\n\
265 </presence>", contact_info, atom_id, contact_info);
266
267           }
268           else if (online_status==SalPresenceOnthephone)
269           {
270                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
271 <!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
272 <presence>\n\
273 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
274 <atom id=\"%i\">\n\
275 <address uri=\"%s\" priority=\"0.800000\">\n\
276 <status status=\"inuse\" />\n\
277 <msnsubstatus substatus=\"onthephone\" />\n\
278 </address>\n\
279 </atom>\n\
280 </presence>", contact_info, atom_id, contact_info);
281
282           }
283           else if (online_status==SalPresenceOuttolunch)
284           {
285                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
286 <!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
287 <presence>\n\
288 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
289 <atom id=\"%i\">\n\
290 <address uri=\"%s\" priority=\"0.800000\">\n\
291 <status status=\"open\" />\n\
292 <msnsubstatus substatus=\"outtolunch\" />\n\
293 </address>\n\
294 </atom>\n\
295 </presence>", contact_info, atom_id, contact_info);
296
297           }
298           else
299           {
300                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
301 <!DOCTYPE presence PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
302 <presence>\n\
303 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
304 <atom id=\"%i\">\n\
305 <address uri=\"%s\" priority=\"0.800000\">\n\
306 <status status=\"closed\" />\n\
307 <msnsubstatus substatus=\"away\" />\n\
308 </address>\n\
309 </atom>\n\
310 </presence>", contact_info, atom_id, contact_info);
311           }
312           break;
313     } 
314     case MSOLDPRES: {
315         /* Couldn't find schema http://schemas.microsoft.com/2002/09/sip/presence
316         *  so messages format has been taken from Communigate that can send notify
317         *  requests with this schema
318         */
319           int atom_id = 1000;
320
321           if (online_status==SalPresenceOnline)
322           {
323                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
324 <!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
325 <presence>\n\
326 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
327 <atom id=\"%i\">\n\
328 <address uri=\"%s\">\n\
329 <status status=\"open\" />\n\
330 <msnsubstatus substatus=\"online\" />\n\
331 </address>\n\
332 </atom>\n\
333 </presence>", contact_info, atom_id, contact_info);
334
335           }
336           else if (online_status == SalPresenceBusy ||
337                           online_status == SalPresenceDonotdisturb)
338           {
339                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
340 <!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
341 <presence>\n\
342 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
343 <atom id=\"%i\">\n\
344 <address uri=\"%s\">\n\
345 <status status=\"inuse\" />\n\
346 <msnsubstatus substatus=\"busy\" />\n\
347 </address>\n\
348 </atom>\n</presence>", contact_info, atom_id, contact_info);
349
350           }
351           else if (online_status==SalPresenceBerightback)
352           {
353                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
354 <!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
355 <presence>\n\
356 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
357 <atom id=\"%i\">\n\
358 <address uri=\"%s\">\n\
359 <status status=\"inactive\" />\n\
360 <msnsubstatus substatus=\"berightback\" />\n\
361 </address>\n\
362 </atom>\n\
363 </presence>", contact_info, atom_id, contact_info);
364
365           }
366           else if (online_status == SalPresenceAway ||
367                           online_status == SalPresenceMoved)
368           {
369                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
370 <!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
371 <presence>\n\
372 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
373 <atom id=\"%i\">\n\
374 <address uri=\"%s\">\n\
375 <status status=\"inactive\" />\n\
376 <msnsubstatus substatus=\"idle\" />\n\
377 </address>\n\
378 </atom>\n\
379 </presence>", contact_info, atom_id, contact_info);
380
381           }
382           else if (online_status==SalPresenceOnthephone)
383           {
384                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
385 <!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
386 <presence>\n\
387 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
388 <atom id=\"%i\">\n\
389 <address uri=\"%s\">\n\
390 <status status=\"inuse\" />\n\
391 <msnsubstatus substatus=\"onthephone\" />\n\
392 </address>\n\
393 </atom>\n\
394 </presence>", contact_info, atom_id, contact_info);
395
396           }
397           else if (online_status==SalPresenceOuttolunch)
398           {
399                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
400 <!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
401 <presence>\n\
402 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
403 <atom id=\"%i\">\n\
404 <address uri=\"%s\">\n\
405 <status status=\"inactive\" />\n\
406 <msnsubstatus substatus=\"outtolunch\" />\n\
407 </address>\n\
408 </atom>\n\
409 </presence>", contact_info, atom_id, contact_info);
410
411           }
412           else
413           {
414                   snprintf(buf, buflen, "<?xml version=\"1.0\"?>\n\
415 <!DOCTYPE presence SYSTEM \"http://schemas.microsoft.com/2002/09/sip/presence\">\n\
416 <presence>\n\
417 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
418 <atom id=\"%i\">\n\
419 <address uri=\"%s\">\n\
420 <status status=\"closed\" />\n\
421 <msnsubstatus substatus=\"offline\" />\n\
422 </address>\n\
423 </atom>\n\
424 </presence>", contact_info, atom_id, contact_info);
425           }
426         break;
427         }
428     default: { /* use pidf+xml as default format, rfc4479, rfc4480, rfc3863 */
429
430         if (online_status==SalPresenceOnline)
431         {
432                 snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
433 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \
434 xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" \
435 xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" \
436 entity=\"%s\">\n\
437 <tuple id=\"sg89ae\">\n\
438 <status><basic>open</basic></status>\n\
439 <contact priority=\"0.8\">%s</contact>\n\
440 </tuple>\n\
441 </presence>",
442 contact_info, contact_info);
443         }
444         else if (online_status == SalPresenceBusy ||
445                         online_status == SalPresenceDonotdisturb)
446         {
447                 snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
448 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \
449 xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" \
450 xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" \
451 entity=\"%s\">\n\
452 <tuple id=\"sg89ae\">\n\
453 <status><basic>open</basic></status>\n\
454 <contact priority=\"0.8\">%s</contact>\n\
455 </tuple>\n\
456 <dm:person id=\"sg89aep\">\n\
457 <rpid:activities><rpid:busy/></rpid:activities>\n\
458 </dm:person>\n\
459 </presence>",
460 contact_info, contact_info);
461         }
462         else if (online_status==SalPresenceBerightback)
463         {
464                 snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
465 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \
466 xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" \
467 xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" \
468 entity=\"%s\">\n\
469 <tuple id=\"sg89ae\">\n\
470 <status><basic>open</basic></status>\n\
471 <contact priority=\"0.8\">%s</contact>\n\
472 </tuple>\n\
473 <dm:person id=\"sg89aep\">\n\
474 <rpid:activities><rpid:in-transit/></rpid:activities>\n\
475 </dm:person>\n\
476 </presence>",
477 contact_info, contact_info);
478         }
479         else if (online_status == SalPresenceAway ||
480                         online_status == SalPresenceMoved)
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:away/></rpid:activities>\n\
493 </dm:person>\n\
494 </presence>",
495 contact_info, contact_info);
496         }
497         else if (online_status==SalPresenceOnthephone)
498         {
499                 snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
500 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \
501 xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" \
502 xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" \
503 entity=\"%s\">\n\
504 <tuple id=\"sg89ae\">\n\
505 <status><basic>open</basic></status>\n\
506 <contact priority=\"0.8\">%s</contact>\n\
507 </tuple>\n\
508 <dm:person id=\"sg89aep\">\n\
509 <rpid:activities><rpid:on-the-phone/></rpid:activities>\n\
510 </dm:person>\n\
511 </presence>",
512 contact_info, contact_info);
513         }
514         else if (online_status==SalPresenceOuttolunch)
515         {
516                 snprintf(buf, buflen, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
517 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\" \
518 xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" \
519 xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" \
520 entity=\"%s\">\n\
521 <tuple id=\"7777\">\n\
522 <status><basic>open</basic></status>\n\
523 <contact priority=\"0.8\">%s</contact>\n\
524 </tuple>\n\
525 <dm:person id=\"78787878\">\n\
526 <rpid:activities><rpid:meal/></rpid:activities>\n\
527 <rpid:note>Out to lunch</rpid:note> \n\
528 </dm:person>\n\
529 </presence>",
530 contact_info, contact_info);
531         }
532         else
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=\"sg89ae\">\n\
540 <status><basic>closed</basic></status>\n\
541 <contact priority=\"0.8\">%s</contact>\n\
542 </tuple>\n\
543 </presence>\n", contact_info, contact_info);
544         }
545         break;
546     }
547  } // switch
548
549 }
550
551 static void add_presence_body(osip_message_t *notify, SalPresenceStatus online_status)
552 {
553         char buf[1000];
554         char *contact_info;
555
556         osip_from_t *from=NULL;
557         from=osip_message_get_from(notify);
558         osip_uri_to_str(from->url,&contact_info);
559
560         mk_presence_body (online_status, contact_info, buf, sizeof (buf), presence_style);
561
562         osip_message_set_body(notify, buf, strlen(buf));
563         osip_message_set_content_type(notify,
564                 presence_style ? "application/xpidf+xml" : "application/pidf+xml");
565
566         osip_free(contact_info);
567 }
568
569
570 int sal_notify_presence(SalOp *op, SalPresenceStatus status, const char *status_message){
571         osip_message_t *msg=NULL;
572         eXosip_ss_t ss=EXOSIP_SUBCRSTATE_ACTIVE;
573         if (op->nid==-1){
574                 ms_warning("Cannot notify, subscription was closed.");
575                 return -1;
576         }
577         
578         eXosip_lock();
579         eXosip_insubscription_build_notify(op->did,ss,DEACTIVATED,&msg);
580         if (msg!=NULL){
581                 const char *identity=sal_op_get_contact(op);
582                 if (identity==NULL) identity=sal_op_get_to(op);
583                 _osip_list_set_empty(&msg->contacts,(void (*)(void*))osip_contact_free);
584                 osip_message_set_contact(msg,identity);
585                 add_presence_body(msg,status);
586                 eXosip_insubscription_send_request(op->did,msg);
587         }else ms_error("could not create notify for incoming subscription.");
588         eXosip_unlock();
589         return 0;
590 }
591
592 int sal_notify_close(SalOp *op){
593         osip_message_t *msg=NULL;
594         eXosip_lock();
595         eXosip_insubscription_build_notify(op->did,EXOSIP_SUBCRSTATE_TERMINATED,DEACTIVATED,&msg);
596         if (msg!=NULL){
597                 const char *identity=sal_op_get_contact(op);
598                 if (identity==NULL) identity=sal_op_get_to(op);
599                 osip_message_set_contact(msg,identity);
600                 add_presence_body(msg,SalPresenceOffline);
601                 eXosip_insubscription_send_request(op->did,msg);
602         }else ms_error("sal_notify_close(): could not create notify for incoming subscription"
603             " did=%i, nid=%i",op->did,op->nid);
604         eXosip_unlock();
605         return 0;
606 }
607
608 int sal_publish(SalOp *op, const char *from, const char *to, SalPresenceStatus presence_mode){
609         osip_message_t *pub;
610         int i;
611         char buf[1024];
612
613         mk_presence_body (presence_mode, from, buf, sizeof (buf), presence_style);
614
615         i = eXosip_build_publish(&pub,from, to, NULL, "presence", "300", 
616                 presence_style ? "application/xpidf+xml" : "application/pidf+xml", buf);
617         if (i<0){
618                 ms_warning("Failed to build publish request.");
619                 return -1;
620         }
621
622         eXosip_lock();
623         i = eXosip_publish(pub, to); /* should update the sip-if-match parameter
624                                     from sip-etag  from last 200ok of PUBLISH */
625         eXosip_unlock();
626         if (i<0){
627           ms_message("Failed to send publish request.");
628           return -1;
629         }
630         sal_add_other(sal_op_get_sal(op),op,pub);
631         return 0;
632 }
633
634 static void _sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){        
635         SalOp *op=sal_op_new(sal);
636         char *tmp;
637         op->did=ev->did;
638         op->tid=ev->tid;
639         op->nid=ev->nid;
640         osip_from_to_str(ev->request->from,&tmp);
641         sal_op_set_from(op,tmp);
642         ms_free(tmp);
643         osip_from_to_str(ev->request->to,&tmp);
644         sal_op_set_to(op,tmp);
645         ms_free(tmp);
646         sal_add_in_subscribe(sal,op,ev->request);
647         sal->callbacks.subscribe_received(op,sal_op_get_from(op));
648 }
649
650 void sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){        
651         /*workaround a bug in eXosip: incoming SUBSCRIBES within dialog with expires: 0 are
652          recognized as new incoming subscribes*/
653         SalOp *op=sal_find_in_subscribe_by_call_id(sal,ev->request->call_id);
654         if (op){
655                 osip_header_t *h;
656                 osip_message_header_get_byname(ev->request,"expires",0,&h);
657                 if (h && h->hvalue && atoi(h->hvalue)==0){
658                         ms_warning("This susbscribe is not a new one but terminates an old one.");
659                         ev->did=op->did;
660                         ev->nid=op->nid;
661                         sal_exosip_subscription_closed(sal,ev);
662                 }else {
663                         osip_message_t *msg=NULL;
664                         ms_warning("Probably a refresh subscribe");
665                         eXosip_lock();
666                         eXosip_insubscription_build_answer(ev->tid,202,&msg);
667                         eXosip_insubscription_send_answer(ev->tid,202,msg);
668                         eXosip_unlock();
669                 }
670         }else _sal_exosip_subscription_recv(sal,ev);
671 }
672
673 void sal_exosip_notify_recv(Sal *sal, eXosip_event_t *ev){
674         SalOp *op=sal_find_out_subscribe(sal,ev->sid);
675         char *tmp;
676         osip_from_t *from=NULL;
677         osip_body_t *body=NULL;
678         SalPresenceStatus estatus=SalPresenceOffline;
679         
680         ms_message("Receiving notify with sid=%i,nid=%i",ev->sid,ev->nid);
681
682         if (op==NULL){
683                 ms_error("No operation related to this notify !");
684                 return;
685         }
686         if (ev->request==NULL) return;
687
688         from=ev->request->from;
689         osip_message_get_body(ev->request,0,&body);
690         if (body==NULL){
691                 ms_error("No body in NOTIFY");
692                 return;
693         }
694         osip_from_to_str(from,&tmp);
695         if (strstr(body->body,"pending")!=NULL){
696                 estatus=SalPresenceOffline;
697         }else if (strstr(body->body,"busy")!=NULL){
698                 estatus=SalPresenceBusy;
699         }else if (strstr(body->body,"berightback")!=NULL
700                         || strstr(body->body,"in-transit")!=NULL ){
701                 estatus=SalPresenceBerightback;
702         }else if (strstr(body->body,"away")!=NULL
703                         || strstr(body->body,"idle")){
704                 estatus=SalPresenceAway;
705         }else if (strstr(body->body,"onthephone")!=NULL
706                 || strstr(body->body,"on-the-phone")!=NULL){
707                 estatus=SalPresenceOnthephone;
708         }else if (strstr(body->body,"outtolunch")!=NULL
709                         || strstr(body->body,"meal")!=NULL){
710                 estatus=SalPresenceOuttolunch;
711         }else if (strstr(body->body,"closed")!=NULL){
712                 estatus=SalPresenceOffline;
713         }else if ((strstr(body->body,"online")!=NULL) || (strstr(body->body,"open")!=NULL)) {
714                 estatus=SalPresenceOnline;
715         }else{
716                 estatus=SalPresenceOffline;
717         }
718         ms_message("We are notified that %s has online status %i",tmp,estatus);
719         if (ev->ss_status==EXOSIP_SUBCRSTATE_TERMINATED) {
720                 sal_remove_out_subscribe(sal,op);
721                 op->sid=-1;
722                 op->did=-1;
723                 ms_message("And outgoing subscription terminated by remote.");
724         }
725         sal->callbacks.notify_presence(op,op->sid!=-1 ? SalSubscribeActive : SalSubscribeTerminated, estatus,NULL);
726
727         /* try to detect presence message style used by server,
728          * and switch our presence messages to servers style */
729         if (strstr (body->body, "//IETF//DTD RFCxxxx XPIDF 1.0//EN") != NULL) {
730                 presence_style = RFCxxxx;
731         } else if (strstr(body->body,"http://schemas.microsoft.com/2002/09/sip/presence")!=NULL) {
732                 presence_style = MSOLDPRES;
733         }
734         
735         osip_free(tmp);
736 }
737
738 void sal_exosip_subscription_answered(Sal *sal,eXosip_event_t *ev){
739         SalOp *op=sal_find_out_subscribe(sal,ev->sid);
740         if (op==NULL){
741                 ms_error("Subscription answered but no associated op !");
742                 return;
743         }
744         op->did=ev->did;
745 }
746
747 void sal_exosip_in_subscription_closed(Sal *sal, eXosip_event_t *ev){
748         SalOp *op=sal_find_in_subscribe(sal,ev->nid);
749         char *tmp;
750         if (op==NULL){
751                 ms_error("Incoming subscription closed but no associated op !");
752                 return;
753         }
754         
755         
756         sal_remove_in_subscribe(sal,op);
757         op->nid=-1;
758         op->did=-1;
759         if (ev->request){
760                 osip_from_to_str(ev->request->from,&tmp);
761                 sal->callbacks.subscribe_closed(op,tmp);
762                 osip_free(tmp);
763         }
764 }
765
766 void sal_exosip_subscription_closed(Sal *sal,eXosip_event_t *ev){
767         SalOp *op=sal_find_out_subscribe(sal,ev->sid);
768         if (op==NULL){
769                 ms_error("Subscription closed but no associated op !");
770                 return;
771         }
772         sal_remove_out_subscribe(sal,op);
773         op->sid=-1;
774         op->did=-1;
775         sal->callbacks.notify_presence(op,SalSubscribeTerminated, SalPresenceOffline,NULL);
776 }
777
778