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 void sal_remove_out_subscribe(Sal *sal, SalOp *op){
39 sal->out_subscribes=ms_list_remove(sal->out_subscribes,op);
42 static SalOp * sal_find_in_subscribe(Sal *sal, int nid){
45 for(elem=sal->in_subscribes;elem!=NULL;elem=elem->next){
46 op=(SalOp*)elem->data;
47 if (op->nid==nid) return op;
52 static SalOp * sal_find_in_subscribe_by_call_id(Sal *sal, osip_call_id_t *call_id){
55 for(elem=sal->in_subscribes;elem!=NULL;elem=elem->next){
56 op=(SalOp*)elem->data;
57 if (op->call_id && osip_call_id_match(op->call_id,call_id)==0)
63 static void sal_add_in_subscribe(Sal *sal, SalOp *op, osip_message_t *subs){
64 osip_call_id_clone(subs->call_id,&op->call_id);
65 sal->in_subscribes=ms_list_append(sal->in_subscribes,op);
68 void sal_remove_in_subscribe(Sal *sal, SalOp *op){
69 sal->in_subscribes=ms_list_remove(sal->in_subscribes,op);
72 int sal_text_send(SalOp *op, const char *from, const char *to, const char *msg){
73 osip_message_t *sip=NULL;
77 /* we are not currently in communication with the destination */
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 /* we are currently in communication with the destination */
95 //First we generate an INFO message to get the current call_id and a good cseq
96 eXosip_call_build_info(op->did,&sip);
99 ms_warning("could not get a build info to send MESSAGE, maybe no previous call established ?");
100 osip_message_free(sip);
104 osip_free(sip->sip_method);
105 //change the sip_message to be a MESSAGE ...
106 osip_message_set_method(sip,osip_strdup("MESSAGE"));
107 osip_message_set_content_type(sip,"text/plain");
108 osip_message_set_body(sip,msg,strlen(msg));
109 eXosip_message_send_request(sip);
115 /*presence Subscribe/notify*/
116 int sal_subscribe_presence(SalOp *op, const char *from, const char *to){
119 sal_op_set_from(op,from);
121 sal_op_set_to(op,to);
122 sal_exosip_fix_route(op);
124 eXosip_subscribe_build_initial_request(&msg,sal_op_get_to(op),sal_op_get_from(op),
125 sal_op_get_route(op),"presence",600);
126 op->sid=eXosip_subscribe_send_initial_request(msg);
129 osip_message_free(msg);
132 sal_add_out_subscribe(op->base.root,op);
136 int sal_unsubscribe(SalOp *op){
137 osip_message_t *msg=NULL;
139 ms_error("cannot unsubscribe, no dialog !");
143 eXosip_subscribe_build_refresh_request(op->did,&msg);
145 osip_message_set_expires(msg,"0");
146 eXosip_subscribe_send_refresh_request(op->did,msg);
147 }else ms_error("Could not build subscribe refresh request ! op->sid=%i, op->did=%i",
153 int sal_subscribe_accept(SalOp *op){
156 eXosip_insubscription_build_answer(op->tid,202,&msg);
157 eXosip_insubscription_send_answer(op->tid,202,msg);
162 int sal_subscribe_decline(SalOp *op){
164 eXosip_insubscription_send_answer(op->tid,401,NULL);
169 static void add_presence_body(osip_message_t *notify, SalPresenceStatus online_status)
177 osip_contact_t *ct=NULL;
178 osip_message_get_contact(notify,0,&ct);
179 osip_contact_to_str(ct,&contact_info);
183 if (online_status==SalPresenceOnline)
185 sprintf(buf, "<?xml version=\"1.0\"?>\n\
186 <!DOCTYPE presence\n\
187 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
189 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
191 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
192 <status status=\"open\" />\n\
193 <msnsubstatus substatus=\"online\" />\n\
196 </presence>", contact_info, atom_id, contact_info);
199 else if (online_status==SalPresenceBusy)
201 sprintf(buf, "<?xml version=\"1.0\"?>\n\
202 <!DOCTYPE presence\n\
203 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
205 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
207 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
208 <status status=\"inuse\" />\n\
209 <msnsubstatus substatus=\"busy\" />\n\
212 </presence>", contact_info, atom_id, contact_info);
215 else if (online_status==SalPresenceBerightback)
217 sprintf(buf, "<?xml version=\"1.0\"?>\n\
218 <!DOCTYPE presence\n\
219 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
221 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
223 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
224 <status status=\"inactive\" />\n\
225 <msnsubstatus substatus=\"berightback\" />\n\
228 </presence>", contact_info, atom_id, contact_info);
231 else if (online_status==SalPresenceAway)
233 sprintf(buf, "<?xml version=\"1.0\"?>\n\
234 <!DOCTYPE presence\n\
235 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
237 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
239 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
240 <status status=\"inactive\" />\n\
241 <msnsubstatus substatus=\"away\" />\n\
244 </presence>", contact_info, atom_id, contact_info);
247 else if (online_status==SalPresenceOnthephone)
249 sprintf(buf, "<?xml version=\"1.0\"?>\n\
250 <!DOCTYPE presence\n\
251 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
253 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
255 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
256 <status status=\"inuse\" />\n\
257 <msnsubstatus substatus=\"onthephone\" />\n\
260 </presence>", contact_info, atom_id, contact_info);
263 else if (online_status==SalPresenceOuttolunch)
265 sprintf(buf, "<?xml version=\"1.0\"?>\n\
266 <!DOCTYPE presence\n\
267 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
269 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
271 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
272 <status status=\"inactive\" />\n\
273 <msnsubstatus substatus=\"outtolunch\" />\n\
276 </presence>", contact_info, atom_id, contact_info);
281 sprintf(buf, "<?xml version=\"1.0\"?>\n\
282 <!DOCTYPE presence\n\
283 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
285 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
287 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
288 <status status=\"inactive\" />\n\
289 <msnsubstatus substatus=\"away\" />\n\
292 </presence>", contact_info, atom_id, contact_info);
295 osip_message_set_body(notify, buf, strlen(buf));
296 osip_message_set_content_type(notify, "application/xpidf+xml");
299 if (online_status==SalPresenceOnline)
301 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
302 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
304 <tuple id=\"sg89ae\">\n\
306 <basic>open</basic>\n\
308 <contact priority=\"0.8\">%s</contact>\n\
309 <note>online</note>\n\
312 contact_info, contact_info);
314 else if (online_status==SalPresenceBusy)
316 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
317 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
318 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
320 <tuple id=\"sg89ae\">\n\
322 <basic>open</basic>\n\
324 <es:activity>busy</es:activity>\n\
327 <contact priority=\"0.8\">%s</contact>\n\
331 contact_info, contact_info);
333 else if (online_status==SalPresenceBerightback)
335 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
336 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
337 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
339 <tuple id=\"sg89ae\">\n\
341 <basic>open</basic>\n\
343 <es:activity>in-transit</es:activity>\n\
346 <contact priority=\"0.8\">%s</contact>\n\
347 <note>be right back</note>\n\
350 contact_info, contact_info);
352 else if (online_status==SalPresenceAway)
354 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
355 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
356 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
358 <tuple id=\"sg89ae\">\n\
360 <basic>open</basic>\n\
362 <es:activity>away</es:activity>\n\
365 <contact priority=\"0.8\">%s</contact>\n\
369 contact_info, contact_info);
371 else if (online_status==SalPresenceOnthephone)
373 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
374 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
375 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
377 <tuple id=\"sg89ae\">\n\
379 <basic>open</basic>\n\
381 <es:activity>on-the-phone</es:activity>\n\
384 <contact priority=\"0.8\">%s</contact>\n\
385 <note>on the phone</note>\n\
388 contact_info, contact_info);
390 else if (online_status==SalPresenceOuttolunch)
392 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
393 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
394 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
396 <tuple id=\"sg89ae\">\n\
398 <basic>open</basic>\n\
400 <es:activity>meal</es:activity>\n\
403 <contact priority=\"0.8\">%s</contact>\n\
404 <note>out to lunch</note>\n\
407 contact_info, contact_info);
412 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
413 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
414 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
417 "<tuple id=\"sg89ae\">\n\
419 <basic>closed</basic>\n\
421 <es:activity>permanent-absence</es:activity>\n\
427 osip_message_set_body(notify, buf, strlen(buf));
428 osip_message_set_content_type(notify, "application/pidf+xml");
431 osip_free(contact_info);
435 int sal_notify_presence(SalOp *op, SalPresenceStatus status, const char *status_message){
437 eXosip_ss_t ss=EXOSIP_SUBCRSTATE_ACTIVE;
439 ms_warning("Cannot notify, subscription was closed.");
444 eXosip_insubscription_build_notify(op->did,ss,DEACTIVATED,&msg);
446 const char *identity=sal_op_get_contact(op);
447 if (identity==NULL) identity=sal_op_get_to(op);
448 osip_message_set_contact(msg,identity);
449 add_presence_body(msg,status);
450 eXosip_insubscription_send_request(op->did,msg);
451 }else ms_error("could not create notify for incoming subscription.");
456 int sal_notify_close(SalOp *op){
457 osip_message_t *msg=NULL;
459 eXosip_insubscription_build_notify(op->did,EXOSIP_SUBCRSTATE_TERMINATED,DEACTIVATED,&msg);
461 const char *identity=sal_op_get_contact(op);
462 if (identity==NULL) identity=sal_op_get_to(op);
463 osip_message_set_contact(msg,identity);
464 add_presence_body(msg,SalPresenceOffline);
465 eXosip_insubscription_send_request(op->did,msg);
466 }else ms_error("sal_notify_close(): could not create notify for incoming subscription"
467 " did=%i, nid=%i",op->did,op->nid);
472 int sal_publish(SalOp *op, const char *from, const char *to, SalPresenceStatus presence_mode){
477 if (presence_mode==SalPresenceOnline)
479 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
480 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
482 <tuple id=\"sg89ae\">\n\
484 <basic>open</basic>\n\
486 <contact priority=\"0.8\">%s</contact>\n\
487 <note>online</note>\n\
492 else if (presence_mode==SalPresenceBusy
493 ||presence_mode==SalPresenceDonotdisturb)
495 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
496 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
497 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
499 <tuple id=\"sg89ae\">\n\
501 <basic>open</basic>\n\
503 <es:activity>busy</es:activity>\n\
506 <contact priority=\"0.8\">%s</contact>\n\
512 else if (presence_mode==SalPresenceBerightback)
514 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
515 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
516 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
518 <tuple id=\"sg89ae\">\n\
520 <basic>open</basic>\n\
522 <es:activity>in-transit</es:activity>\n\
525 <contact priority=\"0.8\">%s</contact>\n\
526 <note>be right back</note>\n\
531 else if (presence_mode==SalPresenceAway
532 ||presence_mode==SalPresenceMoved)
534 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
535 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
536 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
538 <tuple id=\"sg89ae\">\n\
540 <basic>open</basic>\n\
542 <es:activity>away</es:activity>\n\
545 <contact priority=\"0.8\">%s</contact>\n\
551 else if (presence_mode==SalPresenceOnthephone)
553 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
554 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
555 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
557 <tuple id=\"sg89ae\">\n\
559 <basic>open</basic>\n\
561 <es:activity>on-the-phone</es:activity>\n\
564 <contact priority=\"0.8\">%s</contact>\n\
565 <note>on the phone</note>\n\
570 else if (presence_mode==SalPresenceOuttolunch)
572 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
573 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
574 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
576 <tuple id=\"sg89ae\">\n\
578 <basic>open</basic>\n\
580 <es:activity>meal</es:activity>\n\
583 <contact priority=\"0.8\">%s</contact>\n\
584 <note>out to lunch</note>\n\
591 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
592 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
593 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
596 "<tuple id=\"sg89ae\">\n\
598 <basic>closed</basic>\n\
600 <es:activity>permanent-absence</e:activity>\n\
607 i = eXosip_build_publish(&pub,from, to, NULL, "presence", "1800", "application/pidf+xml", buf);
609 ms_warning("Failed to build publish request.");
614 i = eXosip_publish(pub, to); /* should update the sip-if-match parameter
615 from sip-etag from last 200ok of PUBLISH */
618 ms_message("Failed to send publish request.");
624 static void _sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){
625 SalOp *op=sal_op_new(sal);
630 osip_from_to_str(ev->request->from,&tmp);
631 sal_op_set_from(op,tmp);
633 osip_from_to_str(ev->request->to,&tmp);
634 sal_op_set_to(op,tmp);
636 sal_add_in_subscribe(sal,op,ev->request);
637 sal->callbacks.subscribe_received(op,sal_op_get_from(op));
640 void sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){
641 /*workaround a bug in eXosip: incoming SUBSCRIBES within dialog with expires: 0 are
642 recognized as new incoming subscribes*/
643 SalOp *op=sal_find_in_subscribe_by_call_id(sal,ev->request->call_id);
646 osip_message_header_get_byname(ev->request,"expires",0,&h);
647 if (h && h->hvalue && atoi(h->hvalue)==0){
648 ms_warning("This susbscribe is not a new one but terminates an old one.");
651 sal_exosip_subscription_closed(sal,ev);
653 osip_message_t *msg=NULL;
654 ms_warning("Probably a refresh subscribe");
656 eXosip_insubscription_build_answer(ev->tid,202,&msg);
657 eXosip_insubscription_send_answer(ev->tid,202,msg);
660 }else _sal_exosip_subscription_recv(sal,ev);
663 void sal_exosip_notify_recv(Sal *sal, eXosip_event_t *ev){
664 SalOp *op=sal_find_out_subscribe(sal,ev->sid);
666 osip_from_t *from=NULL;
667 osip_body_t *body=NULL;
668 SalPresenceStatus estatus=SalPresenceOffline;
670 ms_message("Receiving notify with sid=%i,nid=%i",ev->sid,ev->nid);
673 ms_error("No operation related to this notify !");
676 if (ev->request==NULL) return;
678 from=ev->request->from;
679 osip_message_get_body(ev->request,0,&body);
681 ms_error("No body in NOTIFY");
684 osip_from_to_str(from,&tmp);
685 if (strstr(body->body,"pending")!=NULL){
686 estatus=SalPresenceOffline;
687 }else if (strstr(body->body,"busy")!=NULL){
688 estatus=SalPresenceBusy;
689 }else if (strstr(body->body,"berightback")!=NULL
690 || strstr(body->body,"in-transit")!=NULL ){
691 estatus=SalPresenceBerightback;
692 }else if (strstr(body->body,"away")!=NULL){
693 estatus=SalPresenceAway;
694 }else if (strstr(body->body,"onthephone")!=NULL
695 || strstr(body->body,"on-the-phone")!=NULL){
696 estatus=SalPresenceOnthephone;
697 }else if (strstr(body->body,"outtolunch")!=NULL
698 || strstr(body->body,"meal")!=NULL){
699 estatus=SalPresenceOuttolunch;
700 }else if (strstr(body->body,"closed")!=NULL){
701 estatus=SalPresenceOffline;
702 }else if ((strstr(body->body,"online")!=NULL) || (strstr(body->body,"open")!=NULL)) {
703 estatus=SalPresenceOnline;
705 estatus=SalPresenceOffline;
707 ms_message("We are notified that %s has online status %i",tmp,estatus);
708 if (ev->ss_status==EXOSIP_SUBCRSTATE_TERMINATED) {
709 sal_remove_out_subscribe(sal,op);
712 ms_message("And outgoing subscription terminated by remote.");
714 sal->callbacks.notify(op,op->sid!=-1 ? SalSubscribeActive : SalSubscribeTerminated, estatus,NULL);
718 void sal_exosip_subscription_answered(Sal *sal,eXosip_event_t *ev){
719 SalOp *op=sal_find_out_subscribe(sal,ev->sid);
721 ms_error("Subscription answered but no associated op !");
727 void sal_exosip_in_subscription_closed(Sal *sal, eXosip_event_t *ev){
728 SalOp *op=sal_find_in_subscribe(sal,ev->nid);
731 ms_error("Incoming subscription closed but no associated op !");
736 sal_remove_in_subscribe(sal,op);
740 osip_from_to_str(ev->request->from,&tmp);
741 sal->callbacks.subscribe_closed(op,tmp);
746 void sal_exosip_subscription_closed(Sal *sal,eXosip_event_t *ev){
747 SalOp *op=sal_find_out_subscribe(sal,ev->sid);
749 ms_error("Subscription closed but no associated op !");
752 sal_remove_out_subscribe(sal,op);
755 sal->callbacks.notify(op,SalSubscribeTerminated, SalPresenceOffline,NULL);