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 void sal_add_in_subscribe(Sal *sal, SalOp *op){
53 sal->in_subscribes=ms_list_append(sal->in_subscribes,op);
56 void sal_remove_in_subscribe(Sal *sal, SalOp *op){
57 sal->in_subscribes=ms_list_remove(sal->in_subscribes,op);
60 int sal_text_send(SalOp *op, const char *from, const char *to, const char *msg){
61 osip_message_t *sip=NULL;
63 sal_op_set_from(op,from);
68 eXosip_message_build_request(&sip,"MESSAGE",sal_op_get_to(op),
69 sal_op_get_from(op),sal_op_get_route(op));
70 osip_message_set_content_type(sip,"text/plain");
71 osip_message_set_body(sip,msg,strlen(msg));
72 eXosip_message_send_request(sip);
77 /*presence Subscribe/notify*/
78 int sal_subscribe_presence(SalOp *op, const char *from, const char *to){
81 sal_op_set_from(op,from);
84 sal_exosip_fix_route(op);
86 eXosip_subscribe_build_initial_request(&msg,sal_op_get_to(op),sal_op_get_from(op),
87 sal_op_get_route(op),"presence",600);
88 op->sid=eXosip_subscribe_send_initial_request(msg);
91 osip_message_free(msg);
94 sal_add_out_subscribe(op->base.root,op);
98 int sal_unsubscribe(SalOp *op){
99 osip_message_t *msg=NULL;
101 ms_error("cannot unsubscribe, no dialog !");
105 eXosip_subscribe_build_refresh_request(op->did,&msg);
107 osip_message_set_expires(msg,"0");
108 eXosip_subscribe_send_refresh_request(op->did,msg);
109 }else ms_error("Could not build subscribe refresh request ! op->sid=%i, op->did=%i",
115 int sal_subscribe_accept(SalOp *op){
118 eXosip_insubscription_build_answer(op->tid,200,&msg);
119 eXosip_insubscription_send_answer(op->tid,200,msg);
124 int sal_subscribe_decline(SalOp *op){
126 eXosip_insubscription_send_answer(op->tid,401,NULL);
131 static void add_presence_body(osip_message_t *notify, SalPresenceStatus online_status)
139 osip_contact_t *ct=NULL;
140 osip_message_get_contact(notify,0,&ct);
141 osip_contact_to_str(ct,&contact_info);
145 if (online_status==SalPresenceOnline)
147 sprintf(buf, "<?xml version=\"1.0\"?>\n\
148 <!DOCTYPE presence\n\
149 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
151 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
153 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
154 <status status=\"open\" />\n\
155 <msnsubstatus substatus=\"online\" />\n\
158 </presence>", contact_info, atom_id, contact_info);
161 else if (online_status==SalPresenceBusy)
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=\"inuse\" />\n\
171 <msnsubstatus substatus=\"busy\" />\n\
174 </presence>", contact_info, atom_id, contact_info);
177 else if (online_status==SalPresenceBerightback)
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=\"inactive\" />\n\
187 <msnsubstatus substatus=\"berightback\" />\n\
190 </presence>", contact_info, atom_id, contact_info);
193 else if (online_status==SalPresenceAway)
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=\"away\" />\n\
206 </presence>", contact_info, atom_id, contact_info);
209 else if (online_status==SalPresenceOnthephone)
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=\"inuse\" />\n\
219 <msnsubstatus substatus=\"onthephone\" />\n\
222 </presence>", contact_info, atom_id, contact_info);
225 else if (online_status==SalPresenceOuttolunch)
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=\"inactive\" />\n\
235 <msnsubstatus substatus=\"outtolunch\" />\n\
238 </presence>", contact_info, atom_id, contact_info);
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=\"away\" />\n\
254 </presence>", contact_info, atom_id, contact_info);
257 osip_message_set_body(notify, buf, strlen(buf));
258 osip_message_set_content_type(notify, "application/xpidf+xml");
261 if (online_status==SalPresenceOnline)
263 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
264 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
266 <tuple id=\"sg89ae\">\n\
268 <basic>open</basic>\n\
270 <contact priority=\"0.8\">%s</contact>\n\
271 <note>online</note>\n\
274 contact_info, contact_info);
276 else if (online_status==SalPresenceBusy)
278 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
279 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
280 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
282 <tuple id=\"sg89ae\">\n\
284 <basic>open</basic>\n\
286 <es:activity>busy</es:activity>\n\
289 <contact priority=\"0.8\">%s</contact>\n\
293 contact_info, contact_info);
295 else if (online_status==SalPresenceBerightback)
297 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
298 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
299 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
301 <tuple id=\"sg89ae\">\n\
303 <basic>open</basic>\n\
305 <es:activity>in-transit</es:activity>\n\
308 <contact priority=\"0.8\">%s</contact>\n\
309 <note>be right back</note>\n\
312 contact_info, contact_info);
314 else if (online_status==SalPresenceAway)
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>away</es:activity>\n\
327 <contact priority=\"0.8\">%s</contact>\n\
331 contact_info, contact_info);
333 else if (online_status==SalPresenceOnthephone)
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>on-the-phone</es:activity>\n\
346 <contact priority=\"0.8\">%s</contact>\n\
347 <note>on the phone</note>\n\
350 contact_info, contact_info);
352 else if (online_status==SalPresenceOuttolunch)
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>meal</es:activity>\n\
365 <contact priority=\"0.8\">%s</contact>\n\
366 <note>out to lunch</note>\n\
369 contact_info, contact_info);
374 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
375 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
376 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
379 "<tuple id=\"sg89ae\">\n\
381 <basic>closed</basic>\n\
383 <es:activity>permanent-absence</es:activity>\n\
389 osip_message_set_body(notify, buf, strlen(buf));
390 osip_message_set_content_type(notify, "application/pidf+xml");
393 osip_free(contact_info);
397 int sal_notify_presence(SalOp *op, SalPresenceStatus status, const char *status_message){
399 eXosip_ss_t ss=EXOSIP_SUBCRSTATE_ACTIVE;
401 ms_warning("Cannot notify, subscription was closed.");
406 eXosip_insubscription_build_notify(op->did,ss,DEACTIVATED,&msg);
408 const char *identity=sal_op_get_contact(op);
409 if (identity==NULL) identity=sal_op_get_to(op);
410 osip_message_set_contact(msg,identity);
411 add_presence_body(msg,status);
412 eXosip_insubscription_send_request(op->did,msg);
413 }else ms_error("could not create notify for incoming subscription.");
418 int sal_notify_close(SalOp *op){
419 osip_message_t *msg=NULL;
421 eXosip_insubscription_build_notify(op->did,EXOSIP_SUBCRSTATE_TERMINATED,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 eXosip_insubscription_send_request(op->did,msg);
427 }else ms_error("sal_notify_close(): could not create notify for incoming subscription"
428 " did=%i, nid=%i",op->did,op->nid);
433 int sal_publish(SalOp *op, const char *from, const char *to, SalPresenceStatus presence_mode){
438 if (presence_mode==SalPresenceOnline)
440 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
441 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
443 <tuple id=\"sg89ae\">\n\
445 <basic>open</basic>\n\
447 <contact priority=\"0.8\">%s</contact>\n\
448 <note>online</note>\n\
453 else if (presence_mode==SalPresenceBusy
454 ||presence_mode==SalPresenceDonotdisturb)
456 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
457 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
458 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
460 <tuple id=\"sg89ae\">\n\
462 <basic>open</basic>\n\
464 <es:activity>busy</es:activity>\n\
467 <contact priority=\"0.8\">%s</contact>\n\
473 else if (presence_mode==SalPresenceBerightback)
475 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
476 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
477 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
479 <tuple id=\"sg89ae\">\n\
481 <basic>open</basic>\n\
483 <es:activity>in-transit</es:activity>\n\
486 <contact priority=\"0.8\">%s</contact>\n\
487 <note>be right back</note>\n\
492 else if (presence_mode==SalPresenceAway
493 ||presence_mode==SalPresenceMoved)
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>away</es:activity>\n\
506 <contact priority=\"0.8\">%s</contact>\n\
512 else if (presence_mode==SalPresenceOnthephone)
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>on-the-phone</es:activity>\n\
525 <contact priority=\"0.8\">%s</contact>\n\
526 <note>on the phone</note>\n\
531 else if (presence_mode==SalPresenceOuttolunch)
533 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
534 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
535 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
537 <tuple id=\"sg89ae\">\n\
539 <basic>open</basic>\n\
541 <es:activity>meal</es:activity>\n\
544 <contact priority=\"0.8\">%s</contact>\n\
545 <note>out to lunch</note>\n\
552 snprintf(buf, sizeof(buf), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
553 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
554 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
557 "<tuple id=\"sg89ae\">\n\
559 <basic>closed</basic>\n\
561 <es:activity>permanent-absence</e:activity>\n\
568 i = eXosip_build_publish(&pub,from, to, NULL, "presence", "1800", "application/pidf+xml", buf);
570 ms_warning("Failed to build publish request.");
575 i = eXosip_publish(pub, to); /* should update the sip-if-match parameter
576 from sip-etag from last 200ok of PUBLISH */
579 ms_message("Failed to send publish request.");
585 void sal_exosip_subscription_recv(Sal *sal, eXosip_event_t *ev){
586 SalOp *op=sal_op_new(sal);
591 osip_from_to_str(ev->request->from,&tmp);
592 sal_op_set_from(op,tmp);
594 osip_from_to_str(ev->request->to,&tmp);
595 sal_op_set_to(op,tmp);
597 sal_add_in_subscribe(sal,op);
598 sal->callbacks.subscribe_received(op,sal_op_get_from(op));
601 void sal_exosip_notify_recv(Sal *sal, eXosip_event_t *ev){
602 SalOp *op=sal_find_out_subscribe(sal,ev->sid);
604 osip_from_t *from=NULL;
605 osip_body_t *body=NULL;
606 SalPresenceStatus estatus=SalPresenceOffline;
608 ms_message("Receiving notify with sid=%i,nid=%i",ev->sid,ev->nid);
611 ms_error("No operation related to this notify !");
614 if (ev->request==NULL) return;
616 from=ev->request->from;
617 osip_message_get_body(ev->request,0,&body);
619 ms_error("No body in NOTIFY");
622 osip_from_to_str(from,&tmp);
623 if (strstr(body->body,"pending")!=NULL){
624 estatus=SalPresenceOffline;
625 }else if (strstr(body->body,"busy")!=NULL){
626 estatus=SalPresenceBusy;
627 }else if (strstr(body->body,"berightback")!=NULL
628 || strstr(body->body,"in-transit")!=NULL ){
629 estatus=SalPresenceBerightback;
630 }else if (strstr(body->body,"away")!=NULL){
631 estatus=SalPresenceAway;
632 }else if (strstr(body->body,"onthephone")!=NULL
633 || strstr(body->body,"on-the-phone")!=NULL){
634 estatus=SalPresenceOnthephone;
635 }else if (strstr(body->body,"outtolunch")!=NULL
636 || strstr(body->body,"meal")!=NULL){
637 estatus=SalPresenceOuttolunch;
638 }else if (strstr(body->body,"closed")!=NULL){
639 estatus=SalPresenceOffline;
640 }else if ((strstr(body->body,"online")!=NULL) || (strstr(body->body,"open")!=NULL)) {
641 estatus=SalPresenceOnline;
643 estatus=SalPresenceOffline;
645 ms_message("We are notified that %s has online status %i",tmp,estatus);
646 if (ev->ss_status==EXOSIP_SUBCRSTATE_TERMINATED) {
647 sal_remove_out_subscribe(sal,op);
650 ms_message("And outgoing subscription terminated by remote.");
652 sal->callbacks.notify(op,op->sid!=-1 ? SalSubscribeActive : SalSubscribeTerminated, estatus,NULL);
656 void sal_exosip_subscription_answered(Sal *sal,eXosip_event_t *ev){
657 SalOp *op=sal_find_out_subscribe(sal,ev->sid);
659 ms_error("Subscription answered but no associated op !");
665 void sal_exosip_in_subscription_closed(Sal *sal, eXosip_event_t *ev){
666 SalOp *op=sal_find_in_subscribe(sal,ev->nid);
669 ms_error("Incoming subscription closed but no associated op !");
674 sal_remove_in_subscribe(sal,op);
678 osip_from_to_str(ev->request->from,&tmp);
679 sal->callbacks.subscribe_closed(op,tmp);
684 void sal_exosip_subscription_closed(Sal *sal,eXosip_event_t *ev){
685 SalOp *op=sal_find_out_subscribe(sal,ev->sid);
687 ms_error("Subscription closed but no associated op !");
690 sal_remove_out_subscribe(sal,op);
693 sal->callbacks.notify(op,SalSubscribeTerminated, SalPresenceOffline,NULL);