3 Copyright (C) 2010 Simon MORLAT (simon.morlat@free.fr)
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.
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.
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.
21 #include "sal_eXosip2.h"
24 static SalOp * sal_find_out_subscribe(Sal *sal, int sid){
27 for(elem=sal->out_subscribes;elem!=NULL;elem=elem->next){
28 op=(SalOp*)elem->data;
29 if (op->sid==sid) return op;
34 static void sal_add_out_subscribe(Sal *sal, SalOp *op){
35 sal->out_subscribes=ms_list_append(sal->out_subscribes,op);
38 static void sal_remove_out_subscribe(Sal *sal, int sid){
41 for(elem=sal->out_subscribes;elem!=NULL;elem=elem->next){
42 op=(SalOp*)elem->data;
44 sal->out_subscribes=ms_list_remove_link(sal->out_subscribes,elem);
50 static SalOp * sal_find_in_subscribe(Sal *sal, int nid){
53 for(elem=sal->in_subscribes;elem!=NULL;elem=elem->next){
54 op=(SalOp*)elem->data;
55 if (op->nid==nid) return op;
60 static void sal_add_in_subscribe(Sal *sal, SalOp *op){
61 sal->in_subscribes=ms_list_append(sal->in_subscribes,op);
64 static void sal_remove_in_subscribe(Sal *sal, int nid){
67 for(elem=sal->in_subscribes;elem!=NULL;elem=elem->next){
68 op=(SalOp*)elem->data;
70 sal->in_subscribes=ms_list_remove_link(sal->in_subscribes,elem);
76 int sal_text_send(SalOp *op, const char *from, const char *to, const char *msg){
77 osip_message_t *sip=NULL;
79 sal_op_set_from(op,from);
84 eXosip_message_build_request(&sip,"MESSAGE",sal_op_get_to(op),
85 sal_op_get_from(op),sal_op_get_route(op));
86 osip_message_set_content_type(sip,"text/plain");
87 osip_message_set_body(sip,msg,strlen(msg));
88 eXosip_message_send_request(sip);
93 /*presence Subscribe/notify*/
94 int sal_subscribe_presence(SalOp *op, const char *from, const char *to){
97 sal_op_set_from(op,from);
100 sal_exosip_fix_route(op);
102 eXosip_subscribe_build_initial_request(&msg,sal_op_get_to(op),sal_op_get_from(op),
103 sal_op_get_route(op),"presence",600);
104 op->sid=eXosip_subscribe_send_initial_request(msg);
107 osip_message_free(msg);
110 sal_add_out_subscribe(op->base.root,op);
114 int sal_unsubscribe(SalOp *op){
115 osip_message_t *msg=NULL;
117 ms_error("cannot unsubscribe, no dialog !");
121 eXosip_subscribe_build_refresh_request(op->did,&msg);
123 osip_message_set_expires(msg,"0");
124 eXosip_subscribe_send_refresh_request(op->did,msg);
125 }else ms_error("Could not build subscribe refresh request !");
130 int sal_subscribe_accept(SalOp *op){
133 eXosip_insubscription_build_answer(op->tid,202,&msg);
134 eXosip_insubscription_send_answer(op->tid,202,msg);
139 int sal_subscribe_decline(SalOp *op){
141 eXosip_insubscription_send_answer(op->tid,401,NULL);
146 static void add_presence_body(osip_message_t *notify, SalPresenceStatus online_status)
154 osip_contact_t *ct=NULL;
155 osip_message_get_contact(notify,0,&ct);
156 osip_contact_to_str(ct,&contact_info);
160 if (online_status==SalPresenceOnline)
162 sprintf(buf, "<?xml version=\"1.0\"?>\n\
163 <!DOCTYPE presence\n\
164 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
166 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
168 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
169 <status status=\"open\" />\n\
170 <msnsubstatus substatus=\"online\" />\n\
173 </presence>", contact_info, atom_id, contact_info);
176 else if (online_status==SalPresenceBusy)
178 sprintf(buf, "<?xml version=\"1.0\"?>\n\
179 <!DOCTYPE presence\n\
180 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
182 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
184 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
185 <status status=\"inuse\" />\n\
186 <msnsubstatus substatus=\"busy\" />\n\
189 </presence>", contact_info, atom_id, contact_info);
192 else if (online_status==SalPresenceBerightback)
194 sprintf(buf, "<?xml version=\"1.0\"?>\n\
195 <!DOCTYPE presence\n\
196 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
198 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
200 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
201 <status status=\"inactive\" />\n\
202 <msnsubstatus substatus=\"berightback\" />\n\
205 </presence>", contact_info, atom_id, contact_info);
208 else if (online_status==SalPresenceAway)
210 sprintf(buf, "<?xml version=\"1.0\"?>\n\
211 <!DOCTYPE presence\n\
212 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
214 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
216 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
217 <status status=\"inactive\" />\n\
218 <msnsubstatus substatus=\"away\" />\n\
221 </presence>", contact_info, atom_id, contact_info);
224 else if (online_status==SalPresenceOnthephone)
226 sprintf(buf, "<?xml version=\"1.0\"?>\n\
227 <!DOCTYPE presence\n\
228 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
230 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
232 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
233 <status status=\"inuse\" />\n\
234 <msnsubstatus substatus=\"onthephone\" />\n\
237 </presence>", contact_info, atom_id, contact_info);
240 else if (online_status==SalPresenceOuttolunch)
242 sprintf(buf, "<?xml version=\"1.0\"?>\n\
243 <!DOCTYPE presence\n\
244 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
246 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
248 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
249 <status status=\"inactive\" />\n\
250 <msnsubstatus substatus=\"outtolunch\" />\n\
253 </presence>", contact_info, atom_id, contact_info);
258 sprintf(buf, "<?xml version=\"1.0\"?>\n\
259 <!DOCTYPE presence\n\
260 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
262 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
264 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
265 <status status=\"inactive\" />\n\
266 <msnsubstatus substatus=\"away\" />\n\
269 </presence>", contact_info, atom_id, contact_info);
272 osip_message_set_body(notify, buf, strlen(buf));
273 osip_message_set_content_type(notify, "application/xpidf+xml");
276 if (online_status==SalPresenceOnline)
278 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
279 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
281 <tuple id=\"sg89ae\">\n\
283 <basic>open</basic>\n\
285 <contact priority=\"0.8\">%s</contact>\n\
286 <note>online</note>\n\
289 contact_info, contact_info);
291 else if (online_status==SalPresenceBusy)
293 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
294 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
295 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
297 <tuple id=\"sg89ae\">\n\
299 <basic>open</basic>\n\
301 <es:activity>busy</es:activity>\n\
304 <contact priority=\"0.8\">%s</contact>\n\
308 contact_info, contact_info);
310 else if (online_status==SalPresenceBerightback)
312 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
313 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
314 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
316 <tuple id=\"sg89ae\">\n\
318 <basic>open</basic>\n\
320 <es:activity>in-transit</es:activity>\n\
323 <contact priority=\"0.8\">%s</contact>\n\
324 <note>be right back</note>\n\
327 contact_info, contact_info);
329 else if (online_status==SalPresenceAway)
331 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
332 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
333 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
335 <tuple id=\"sg89ae\">\n\
337 <basic>open</basic>\n\
339 <es:activity>away</es:activity>\n\
342 <contact priority=\"0.8\">%s</contact>\n\
346 contact_info, contact_info);
348 else if (online_status==SalPresenceOnthephone)
350 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
351 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
352 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
354 <tuple id=\"sg89ae\">\n\
356 <basic>open</basic>\n\
358 <es:activity>on-the-phone</es:activity>\n\
361 <contact priority=\"0.8\">%s</contact>\n\
362 <note>on the phone</note>\n\
365 contact_info, contact_info);
367 else if (online_status==SalPresenceOuttolunch)
369 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
370 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
371 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
373 <tuple id=\"sg89ae\">\n\
375 <basic>open</basic>\n\
377 <es:activity>meal</es:activity>\n\
380 <contact priority=\"0.8\">%s</contact>\n\
381 <note>out to lunch</note>\n\
384 contact_info, contact_info);
389 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
390 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
391 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
394 "<tuple id=\"sg89ae\">\n\
396 <basic>closed</basic>\n\
398 <es:activity>permanent-absence</es:activity>\n\
404 osip_message_set_body(notify, buf, strlen(buf));
405 osip_message_set_content_type(notify, "application/pidf+xml");
408 osip_free(contact_info);
412 int sal_notify_presence(SalOp *op, SalPresenceStatus status, const char *status_message){
414 eXosip_ss_t ss=EXOSIP_SUBCRSTATE_ACTIVE;
416 ms_warning("Cannot notify, subscription was closed.");
421 eXosip_insubscription_build_notify(op->did,ss,DEACTIVATED,&msg);
423 const char *identity=sal_op_get_contact(op);
424 if (identity==NULL) identity=sal_op_get_to(op);
425 osip_message_set_contact(msg,identity);
426 add_presence_body(msg,status);
427 eXosip_insubscription_send_request(op->did,msg);
428 }else ms_error("could not create notify for incoming subscription.");
433 int sal_notify_close(SalOp *op){
436 eXosip_insubscription_build_notify(op->did,EXOSIP_SUBCRSTATE_TERMINATED,DEACTIVATED,&msg);
438 const char *identity=sal_op_get_contact(op);
439 if (identity==NULL) identity=sal_op_get_to(op);
440 osip_message_set_contact(msg,identity);
441 eXosip_insubscription_send_request(op->did,msg);
442 }else ms_error("could not create notify for incoming subscription.");
447 int sal_publish(SalOp *op, const char *from, const char *to, SalPresenceStatus presence_mode){
452 if (presence_mode==SalPresenceOnline)
454 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
455 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
457 <tuple id=\"sg89ae\">\n\
459 <basic>open</basic>\n\
461 <contact priority=\"0.8\">%s</contact>\n\
462 <note>online</note>\n\
467 else if (presence_mode==SalPresenceBusy
468 ||presence_mode==SalPresenceDonotdisturb)
470 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
471 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
472 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
474 <tuple id=\"sg89ae\">\n\
476 <basic>open</basic>\n\
478 <es:activity>busy</es:activity>\n\
481 <contact priority=\"0.8\">%s</contact>\n\
487 else if (presence_mode==SalPresenceBerightback)
489 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
490 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
491 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
493 <tuple id=\"sg89ae\">\n\
495 <basic>open</basic>\n\
497 <es:activity>in-transit</es:activity>\n\
500 <contact priority=\"0.8\">%s</contact>\n\
501 <note>be right back</note>\n\
506 else if (presence_mode==SalPresenceAway
507 ||presence_mode==SalPresenceMoved)
509 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
510 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
511 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
513 <tuple id=\"sg89ae\">\n\
515 <basic>open</basic>\n\
517 <es:activity>away</es:activity>\n\
520 <contact priority=\"0.8\">%s</contact>\n\
526 else if (presence_mode==SalPresenceOnthephone)
528 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
529 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
530 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
532 <tuple id=\"sg89ae\">\n\
534 <basic>open</basic>\n\
536 <es:activity>on-the-phone</es:activity>\n\
539 <contact priority=\"0.8\">%s</contact>\n\
540 <note>on the phone</note>\n\
545 else if (presence_mode==SalPresenceOuttolunch)
547 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
548 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
549 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
551 <tuple id=\"sg89ae\">\n\
553 <basic>open</basic>\n\
555 <es:activity>meal</es:activity>\n\
558 <contact priority=\"0.8\">%s</contact>\n\
559 <note>out to lunch</note>\n\
566 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
567 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
568 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
571 "<tuple id=\"sg89ae\">\n\
573 <basic>closed</basic>\n\
575 <es:activity>permanent-absence</e:activity>\n\
582 i = eXosip_build_publish(&pub,from, to, NULL, "presence", "1800", "application/pidf+xml", buf);
584 ms_warning("Failed to build publish request.");
589 i = eXosip_publish(pub, to); /* should update the sip-if-match parameter
590 from sip-etag from last 200ok of PUBLISH */
593 ms_message("Failed to send publish request.");
599 void sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){
600 SalOp *op=sal_op_new(sal);
605 osip_from_to_str(ev->request->from,&tmp);
606 sal_op_set_from(op,tmp);
608 osip_from_to_str(ev->request->to,&tmp);
609 sal_op_set_to(op,tmp);
611 sal_add_in_subscribe(sal,op);
612 sal->callbacks.subscribe_received(op,sal_op_get_from(op));
615 void sal_exosip_notify_recv(Sal *sal, eXosip_event_t *ev){
616 SalOp *op=sal_find_out_subscribe(sal,ev->sid);
618 osip_from_t *from=NULL;
619 osip_body_t *body=NULL;
620 SalPresenceStatus estatus=SalPresenceOffline;
622 ms_message("Receiving notify with sid=%i,nid=%i",ev->sid,ev->nid);
625 ms_error("No operation related to this notify !");
628 if (ev->request==NULL) return;
630 from=ev->request->from;
631 osip_message_get_body(ev->request,0,&body);
633 ms_error("No body in NOTIFY");
636 osip_from_to_str(from,&tmp);
637 if (strstr(body->body,"pending")!=NULL){
638 estatus=SalPresenceOffline;
639 }else if ((strstr(body->body,"online")!=NULL) || (strstr(body->body,"open")!=NULL)) {
640 estatus=SalPresenceOnline;
641 }else if (strstr(body->body,"busy")!=NULL){
642 estatus=SalPresenceBusy;
643 }else if (strstr(body->body,"berightback")!=NULL
644 || strstr(body->body,"in-transit")!=NULL ){
645 estatus=SalPresenceBerightback;
646 }else if (strstr(body->body,"away")!=NULL){
647 estatus=SalPresenceAway;
648 }else if (strstr(body->body,"onthephone")!=NULL
649 || strstr(body->body,"on-the-phone")!=NULL){
650 estatus=SalPresenceOnthephone;
651 }else if (strstr(body->body,"outtolunch")!=NULL
652 || strstr(body->body,"meal")!=NULL){
653 estatus=SalPresenceOuttolunch;
654 }else if (strstr(body->body,"closed")!=NULL){
655 estatus=SalPresenceOffline;
657 estatus=SalPresenceOffline;
659 ms_message("We are notified that %s has online status %i",tmp,estatus);
660 if (ev->ss_status==EXOSIP_SUBCRSTATE_TERMINATED) {
661 sal_remove_out_subscribe(sal,op->sid);
664 ms_message("And outgoing subscription terminated by remote.");
666 sal->callbacks.notify(op,op->sid!=-1 ? SalSubscribeActive : SalSubscribeTerminated, estatus,NULL);
670 void sal_exosip_subscription_answered(Sal *sal,eXosip_event_t *ev){
671 SalOp *op=sal_find_out_subscribe(sal,ev->sid);
673 ms_error("Subscription answered but no associated op !");
679 void sal_exosip_subscription_closed(Sal *sal,eXosip_event_t *ev){
680 SalOp *op=sal_find_in_subscribe(sal,ev->sid);
682 ms_error("Subscription closed but no associated op !");
685 sal_remove_in_subscribe(sal,op->nid);