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 ! op->sid=%i, op->did=%i",
131 int sal_subscribe_accept(SalOp *op){
134 eXosip_insubscription_build_answer(op->tid,200,&msg);
135 eXosip_insubscription_send_answer(op->tid,200,msg);
140 int sal_subscribe_decline(SalOp *op){
142 eXosip_insubscription_send_answer(op->tid,401,NULL);
147 static void add_presence_body(osip_message_t *notify, SalPresenceStatus online_status)
155 osip_contact_t *ct=NULL;
156 osip_message_get_contact(notify,0,&ct);
157 osip_contact_to_str(ct,&contact_info);
161 if (online_status==SalPresenceOnline)
163 sprintf(buf, "<?xml version=\"1.0\"?>\n\
164 <!DOCTYPE presence\n\
165 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
167 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
169 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
170 <status status=\"open\" />\n\
171 <msnsubstatus substatus=\"online\" />\n\
174 </presence>", contact_info, atom_id, contact_info);
177 else if (online_status==SalPresenceBusy)
179 sprintf(buf, "<?xml version=\"1.0\"?>\n\
180 <!DOCTYPE presence\n\
181 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
183 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
185 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
186 <status status=\"inuse\" />\n\
187 <msnsubstatus substatus=\"busy\" />\n\
190 </presence>", contact_info, atom_id, contact_info);
193 else if (online_status==SalPresenceBerightback)
195 sprintf(buf, "<?xml version=\"1.0\"?>\n\
196 <!DOCTYPE presence\n\
197 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
199 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
201 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
202 <status status=\"inactive\" />\n\
203 <msnsubstatus substatus=\"berightback\" />\n\
206 </presence>", contact_info, atom_id, contact_info);
209 else if (online_status==SalPresenceAway)
211 sprintf(buf, "<?xml version=\"1.0\"?>\n\
212 <!DOCTYPE presence\n\
213 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
215 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
217 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
218 <status status=\"inactive\" />\n\
219 <msnsubstatus substatus=\"away\" />\n\
222 </presence>", contact_info, atom_id, contact_info);
225 else if (online_status==SalPresenceOnthephone)
227 sprintf(buf, "<?xml version=\"1.0\"?>\n\
228 <!DOCTYPE presence\n\
229 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
231 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
233 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
234 <status status=\"inuse\" />\n\
235 <msnsubstatus substatus=\"onthephone\" />\n\
238 </presence>", contact_info, atom_id, contact_info);
241 else if (online_status==SalPresenceOuttolunch)
243 sprintf(buf, "<?xml version=\"1.0\"?>\n\
244 <!DOCTYPE presence\n\
245 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
247 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
249 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
250 <status status=\"inactive\" />\n\
251 <msnsubstatus substatus=\"outtolunch\" />\n\
254 </presence>", contact_info, atom_id, contact_info);
259 sprintf(buf, "<?xml version=\"1.0\"?>\n\
260 <!DOCTYPE presence\n\
261 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
263 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
265 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
266 <status status=\"inactive\" />\n\
267 <msnsubstatus substatus=\"away\" />\n\
270 </presence>", contact_info, atom_id, contact_info);
273 osip_message_set_body(notify, buf, strlen(buf));
274 osip_message_set_content_type(notify, "application/xpidf+xml");
277 if (online_status==SalPresenceOnline)
279 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
280 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
282 <tuple id=\"sg89ae\">\n\
284 <basic>open</basic>\n\
286 <contact priority=\"0.8\">%s</contact>\n\
287 <note>online</note>\n\
290 contact_info, contact_info);
292 else if (online_status==SalPresenceBusy)
294 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
295 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
296 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
298 <tuple id=\"sg89ae\">\n\
300 <basic>open</basic>\n\
302 <es:activity>busy</es:activity>\n\
305 <contact priority=\"0.8\">%s</contact>\n\
309 contact_info, contact_info);
311 else if (online_status==SalPresenceBerightback)
313 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
314 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
315 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
317 <tuple id=\"sg89ae\">\n\
319 <basic>open</basic>\n\
321 <es:activity>in-transit</es:activity>\n\
324 <contact priority=\"0.8\">%s</contact>\n\
325 <note>be right back</note>\n\
328 contact_info, contact_info);
330 else if (online_status==SalPresenceAway)
332 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
333 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
334 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
336 <tuple id=\"sg89ae\">\n\
338 <basic>open</basic>\n\
340 <es:activity>away</es:activity>\n\
343 <contact priority=\"0.8\">%s</contact>\n\
347 contact_info, contact_info);
349 else if (online_status==SalPresenceOnthephone)
351 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
352 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
353 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
355 <tuple id=\"sg89ae\">\n\
357 <basic>open</basic>\n\
359 <es:activity>on-the-phone</es:activity>\n\
362 <contact priority=\"0.8\">%s</contact>\n\
363 <note>on the phone</note>\n\
366 contact_info, contact_info);
368 else if (online_status==SalPresenceOuttolunch)
370 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
371 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
372 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
374 <tuple id=\"sg89ae\">\n\
376 <basic>open</basic>\n\
378 <es:activity>meal</es:activity>\n\
381 <contact priority=\"0.8\">%s</contact>\n\
382 <note>out to lunch</note>\n\
385 contact_info, contact_info);
390 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
391 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
392 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
395 "<tuple id=\"sg89ae\">\n\
397 <basic>closed</basic>\n\
399 <es:activity>permanent-absence</es:activity>\n\
405 osip_message_set_body(notify, buf, strlen(buf));
406 osip_message_set_content_type(notify, "application/pidf+xml");
409 osip_free(contact_info);
413 int sal_notify_presence(SalOp *op, SalPresenceStatus status, const char *status_message){
415 eXosip_ss_t ss=EXOSIP_SUBCRSTATE_ACTIVE;
417 ms_warning("Cannot notify, subscription was closed.");
422 eXosip_insubscription_build_notify(op->did,ss,DEACTIVATED,&msg);
424 const char *identity=sal_op_get_contact(op);
425 if (identity==NULL) identity=sal_op_get_to(op);
426 osip_message_set_contact(msg,identity);
427 add_presence_body(msg,status);
428 eXosip_insubscription_send_request(op->did,msg);
429 }else ms_error("could not create notify for incoming subscription.");
434 int sal_notify_close(SalOp *op){
437 eXosip_insubscription_build_notify(op->did,EXOSIP_SUBCRSTATE_TERMINATED,DEACTIVATED,&msg);
439 const char *identity=sal_op_get_contact(op);
440 if (identity==NULL) identity=sal_op_get_to(op);
441 osip_message_set_contact(msg,identity);
442 eXosip_insubscription_send_request(op->did,msg);
443 }else ms_error("could not create notify for incoming subscription.");
448 int sal_publish(SalOp *op, const char *from, const char *to, SalPresenceStatus presence_mode){
453 if (presence_mode==SalPresenceOnline)
455 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
456 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
458 <tuple id=\"sg89ae\">\n\
460 <basic>open</basic>\n\
462 <contact priority=\"0.8\">%s</contact>\n\
463 <note>online</note>\n\
468 else if (presence_mode==SalPresenceBusy
469 ||presence_mode==SalPresenceDonotdisturb)
471 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
472 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
473 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
475 <tuple id=\"sg89ae\">\n\
477 <basic>open</basic>\n\
479 <es:activity>busy</es:activity>\n\
482 <contact priority=\"0.8\">%s</contact>\n\
488 else if (presence_mode==SalPresenceBerightback)
490 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
491 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
492 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
494 <tuple id=\"sg89ae\">\n\
496 <basic>open</basic>\n\
498 <es:activity>in-transit</es:activity>\n\
501 <contact priority=\"0.8\">%s</contact>\n\
502 <note>be right back</note>\n\
507 else if (presence_mode==SalPresenceAway
508 ||presence_mode==SalPresenceMoved)
510 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
511 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
512 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
514 <tuple id=\"sg89ae\">\n\
516 <basic>open</basic>\n\
518 <es:activity>away</es:activity>\n\
521 <contact priority=\"0.8\">%s</contact>\n\
527 else if (presence_mode==SalPresenceOnthephone)
529 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
530 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
531 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
533 <tuple id=\"sg89ae\">\n\
535 <basic>open</basic>\n\
537 <es:activity>on-the-phone</es:activity>\n\
540 <contact priority=\"0.8\">%s</contact>\n\
541 <note>on the phone</note>\n\
546 else if (presence_mode==SalPresenceOuttolunch)
548 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
549 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
550 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
552 <tuple id=\"sg89ae\">\n\
554 <basic>open</basic>\n\
556 <es:activity>meal</es:activity>\n\
559 <contact priority=\"0.8\">%s</contact>\n\
560 <note>out to lunch</note>\n\
567 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
568 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
569 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
572 "<tuple id=\"sg89ae\">\n\
574 <basic>closed</basic>\n\
576 <es:activity>permanent-absence</e:activity>\n\
583 i = eXosip_build_publish(&pub,from, to, NULL, "presence", "1800", "application/pidf+xml", buf);
585 ms_warning("Failed to build publish request.");
590 i = eXosip_publish(pub, to); /* should update the sip-if-match parameter
591 from sip-etag from last 200ok of PUBLISH */
594 ms_message("Failed to send publish request.");
600 void sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){
601 SalOp *op=sal_op_new(sal);
606 osip_from_to_str(ev->request->from,&tmp);
607 sal_op_set_from(op,tmp);
609 osip_from_to_str(ev->request->to,&tmp);
610 sal_op_set_to(op,tmp);
612 sal_add_in_subscribe(sal,op);
613 sal->callbacks.subscribe_received(op,sal_op_get_from(op));
616 void sal_exosip_notify_recv(Sal *sal, eXosip_event_t *ev){
617 SalOp *op=sal_find_out_subscribe(sal,ev->sid);
619 osip_from_t *from=NULL;
620 osip_body_t *body=NULL;
621 SalPresenceStatus estatus=SalPresenceOffline;
623 ms_message("Receiving notify with sid=%i,nid=%i",ev->sid,ev->nid);
626 ms_error("No operation related to this notify !");
629 if (ev->request==NULL) return;
631 from=ev->request->from;
632 osip_message_get_body(ev->request,0,&body);
634 ms_error("No body in NOTIFY");
637 osip_from_to_str(from,&tmp);
638 if (strstr(body->body,"pending")!=NULL){
639 estatus=SalPresenceOffline;
640 }else if (strstr(body->body,"busy")!=NULL){
641 estatus=SalPresenceBusy;
642 }else if (strstr(body->body,"berightback")!=NULL
643 || strstr(body->body,"in-transit")!=NULL ){
644 estatus=SalPresenceBerightback;
645 }else if (strstr(body->body,"away")!=NULL){
646 estatus=SalPresenceAway;
647 }else if (strstr(body->body,"onthephone")!=NULL
648 || strstr(body->body,"on-the-phone")!=NULL){
649 estatus=SalPresenceOnthephone;
650 }else if (strstr(body->body,"outtolunch")!=NULL
651 || strstr(body->body,"meal")!=NULL){
652 estatus=SalPresenceOuttolunch;
653 }else if (strstr(body->body,"closed")!=NULL){
654 estatus=SalPresenceOffline;
655 }else if ((strstr(body->body,"online")!=NULL) || (strstr(body->body,"open")!=NULL)) {
656 estatus=SalPresenceOnline;
658 estatus=SalPresenceOffline;
660 ms_message("We are notified that %s has online status %i",tmp,estatus);
661 if (ev->ss_status==EXOSIP_SUBCRSTATE_TERMINATED) {
662 sal_remove_out_subscribe(sal,op->sid);
665 ms_message("And outgoing subscription terminated by remote.");
667 sal->callbacks.notify(op,op->sid!=-1 ? SalSubscribeActive : SalSubscribeTerminated, estatus,NULL);
671 void sal_exosip_subscription_answered(Sal *sal,eXosip_event_t *ev){
672 SalOp *op=sal_find_out_subscribe(sal,ev->sid);
674 ms_error("Subscription answered but no associated op !");
680 void sal_exosip_subscription_closed(Sal *sal,eXosip_event_t *ev){
681 SalOp *op=sal_find_in_subscribe(sal,ev->sid);
683 ms_error("Subscription closed but no associated op !");
686 sal_remove_in_subscribe(sal,op->nid);