1 /***************************************************************************
4 * Sat May 15 15:25:16 2004
5 * Copyright 2004-2009 Simon Morlat
7 ****************************************************************************/
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include "linphonecore.h"
27 #include <eXosip2/eXosip.h>
28 #include <osipparser2/osip_message.h>
31 const char *linphone_online_status_to_string(LinphoneOnlineStatus ss){
34 case LINPHONE_STATUS_UNKNOWN:
37 case LINPHONE_STATUS_ONLINE:
40 case LINPHONE_STATUS_BUSY:
43 case LINPHONE_STATUS_BERIGHTBACK:
44 str=_("Be right back");
46 case LINPHONE_STATUS_AWAY:
49 case LINPHONE_STATUS_ONTHEPHONE:
50 str=_("On the phone");
52 case LINPHONE_STATUS_OUTTOLUNCH:
53 str=_("Out to lunch");
55 case LINPHONE_STATUS_NOT_DISTURB:
56 str=_("Do not disturb");
58 case LINPHONE_STATUS_MOVED:
61 case LINPHONE_STATUS_ALT_SERVICE:
62 str=_("Using another messaging service");
64 case LINPHONE_STATUS_OFFLINE:
67 case LINPHONE_STATUS_PENDING:
70 case LINPHONE_STATUS_CLOSED:
79 static int friend_data_compare(const void * a, const void * b, void * data){
80 LinphoneAddress *fa=((LinphoneFriend*)a)->uri;
81 LinphoneAddress *fb=((LinphoneFriend*)b)->uri;
83 ua=linphone_address_get_username(fa);
84 ub=linphone_address_get_username(fb);
85 if (ua!=NULL && ub!=NULL) {
86 //printf("Comparing usernames %s,%s\n",ua,ub);
87 return strcasecmp(ua,ub);
91 ua=linphone_address_get_domain(fa);
92 ub=linphone_address_get_domain(fb);
93 if (ua!=NULL && ub!=NULL){
94 int ret=strcasecmp(ua,ub);
95 //printf("Comparing hostnames %s,%s,res=%i\n",ua,ub,ret);
102 static int friend_compare(const void * a, const void * b){
103 return friend_data_compare(a,b,NULL);
107 MSList *linphone_find_friend(MSList *fl, const LinphoneAddress *friend, LinphoneFriend **lf){
109 LinphoneFriend dummy;
110 if (lf!=NULL) *lf=NULL;
111 dummy.uri=(LinphoneAddress*)friend;
112 res=ms_list_find_custom(fl,friend_compare,&dummy);
113 if (lf!=NULL && res!=NULL) *lf=(LinphoneFriend*)res->data;
117 LinphoneFriend *linphone_find_friend_by_nid(MSList *l, int nid){
119 for (elem=l;elem!=NULL;elem=elem->next){
120 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
121 if (lf->nid==nid) return lf;
126 LinphoneFriend *linphone_find_friend_by_sid(MSList *l, int sid){
128 for (elem=l;elem!=NULL;elem=elem->next){
129 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
130 if (lf->sid==sid) return lf;
135 void __linphone_friend_do_subscribe(LinphoneFriend *fr){
137 const char *route=NULL;
138 const char *from=NULL;
139 osip_message_t *msg=NULL;
140 friend=linphone_address_as_string(fr->uri);
141 if (fr->proxy!=NULL){
142 route=fr->proxy->reg_route;
143 from=fr->proxy->reg_identity;
144 }else from=linphone_core_get_primary_contact(fr->lc);
146 /* people for which we don't have yet an answer should appear as offline */
147 fr->lc->vtable.notify_recv(fr->lc,(LinphoneFriend*)fr,friend,_("Gone"),"sip-closed.png");
150 eXosip_subscribe_build_initial_request(&msg,friend,from,route,"presence",600);
151 eXosip_subscribe_send_initial_request(msg);
157 LinphoneFriend * linphone_friend_new(){
158 LinphoneFriend *obj=ms_new0(LinphoneFriend,1);
163 obj->pol=LinphoneSPAccept;
164 obj->status=LINPHONE_STATUS_OFFLINE;
169 LinphoneFriend *linphone_friend_new_with_addr(const char *addr){
170 LinphoneFriend *fr=linphone_friend_new();
171 if (linphone_friend_set_sip_addr(fr,addr)<0){
172 linphone_friend_destroy(fr);
178 void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char **result){
179 LinphoneAddress *fr=NULL;
181 fr=linphone_address_new(uri);
184 if (strchr(uri,'@')!=NULL){
187 tmp=ms_strdup_printf("sip:%s",uri);
188 u=linphone_address_new(tmp);
192 }else if (lc->default_proxy!=NULL){
193 /*try adding domain part from default current proxy*/
194 LinphoneAddress * id=linphone_address_new(linphone_core_get_identity(lc));
196 linphone_address_set_username(id,uri);
197 *result=linphone_address_as_string(id);
198 linphone_address_destroy(id);
203 ms_message("%s interpreted as %s",uri,*result);
205 ms_warning("Fail to interpret friend uri %s",uri);
207 }else *result=linphone_address_as_string(fr);
208 linphone_address_destroy(fr);
211 int linphone_friend_set_sip_addr(LinphoneFriend *lf, const char *addr){
212 LinphoneAddress *fr=linphone_address_new(addr);
214 ms_warning("Invalid friend sip uri: %s",addr);
217 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
222 int linphone_friend_set_name(LinphoneFriend *lf, const char *name){
223 LinphoneAddress *fr=lf->uri;
225 ms_error("linphone_friend_set_sip_addr() must be called before linphone_friend_set_name().");
228 linphone_address_set_display_name(fr,name);
232 int linphone_friend_send_subscribe(LinphoneFriend *fr, bool_t val){
237 int linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol)
243 int linphone_friend_set_proxy(LinphoneFriend *fr, struct _LinphoneProxyConfig *cfg){
248 void linphone_friend_set_sid(LinphoneFriend *lf, int sid){
251 void linphone_friend_set_nid(LinphoneFriend *lf, int nid){
253 lf->inc_subscribe_pending=TRUE;
256 void add_presence_body(osip_message_t *notify, LinphoneOnlineStatus online_status)
264 osip_contact_t *ct=NULL;
265 osip_message_get_contact(notify,0,&ct);
266 osip_contact_to_str(ct,&contact_info);
270 if (online_status==LINPHONE_STATUS_ONLINE)
272 sprintf(buf, "<?xml version=\"1.0\"?>\n\
273 <!DOCTYPE presence\n\
274 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
276 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
278 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
279 <status status=\"open\" />\n\
280 <msnsubstatus substatus=\"online\" />\n\
283 </presence>", contact_info, atom_id, contact_info);
286 else if (online_status==LINPHONE_STATUS_BUSY)
288 sprintf(buf, "<?xml version=\"1.0\"?>\n\
289 <!DOCTYPE presence\n\
290 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
292 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
294 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
295 <status status=\"inuse\" />\n\
296 <msnsubstatus substatus=\"busy\" />\n\
299 </presence>", contact_info, atom_id, contact_info);
302 else if (online_status==LINPHONE_STATUS_BERIGHTBACK)
304 sprintf(buf, "<?xml version=\"1.0\"?>\n\
305 <!DOCTYPE presence\n\
306 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
308 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
310 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
311 <status status=\"inactive\" />\n\
312 <msnsubstatus substatus=\"berightback\" />\n\
315 </presence>", contact_info, atom_id, contact_info);
318 else if (online_status==LINPHONE_STATUS_AWAY)
320 sprintf(buf, "<?xml version=\"1.0\"?>\n\
321 <!DOCTYPE presence\n\
322 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
324 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
326 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
327 <status status=\"inactive\" />\n\
328 <msnsubstatus substatus=\"away\" />\n\
331 </presence>", contact_info, atom_id, contact_info);
334 else if (online_status==LINPHONE_STATUS_ONTHEPHONE)
336 sprintf(buf, "<?xml version=\"1.0\"?>\n\
337 <!DOCTYPE presence\n\
338 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
340 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
342 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
343 <status status=\"inuse\" />\n\
344 <msnsubstatus substatus=\"onthephone\" />\n\
347 </presence>", contact_info, atom_id, contact_info);
350 else if (online_status==LINPHONE_STATUS_OUTTOLUNCH)
352 sprintf(buf, "<?xml version=\"1.0\"?>\n\
353 <!DOCTYPE presence\n\
354 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
356 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
358 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
359 <status status=\"inactive\" />\n\
360 <msnsubstatus substatus=\"outtolunch\" />\n\
363 </presence>", contact_info, atom_id, contact_info);
368 sprintf(buf, "<?xml version=\"1.0\"?>\n\
369 <!DOCTYPE presence\n\
370 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
372 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
374 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
375 <status status=\"inactive\" />\n\
376 <msnsubstatus substatus=\"away\" />\n\
379 </presence>", contact_info, atom_id, contact_info);
382 osip_message_set_body(notify, buf, strlen(buf));
383 osip_message_set_content_type(notify, "application/xpidf+xml");
386 if (online_status==LINPHONE_STATUS_ONLINE)
388 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
389 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
391 <tuple id=\"sg89ae\">\n\
393 <basic>open</basic>\n\
395 <contact priority=\"0.8\">%s</contact>\n\
396 <note>online</note>\n\
399 contact_info, contact_info);
401 else if (online_status==LINPHONE_STATUS_BUSY)
403 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
404 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
405 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
407 <tuple id=\"sg89ae\">\n\
409 <basic>open</basic>\n\
411 <es:activity>busy</es:activity>\n\
414 <contact priority=\"0.8\">%s</contact>\n\
418 contact_info, contact_info);
420 else if (online_status==LINPHONE_STATUS_BERIGHTBACK)
422 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
423 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
424 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
426 <tuple id=\"sg89ae\">\n\
428 <basic>open</basic>\n\
430 <es:activity>in-transit</es:activity>\n\
433 <contact priority=\"0.8\">%s</contact>\n\
434 <note>be right back</note>\n\
437 contact_info, contact_info);
439 else if (online_status==LINPHONE_STATUS_AWAY)
441 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
442 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
443 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
445 <tuple id=\"sg89ae\">\n\
447 <basic>open</basic>\n\
449 <es:activity>away</es:activity>\n\
452 <contact priority=\"0.8\">%s</contact>\n\
456 contact_info, contact_info);
458 else if (online_status==LINPHONE_STATUS_ONTHEPHONE)
460 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
461 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
462 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
464 <tuple id=\"sg89ae\">\n\
466 <basic>open</basic>\n\
468 <es:activity>on-the-phone</es:activity>\n\
471 <contact priority=\"0.8\">%s</contact>\n\
472 <note>on the phone</note>\n\
475 contact_info, contact_info);
477 else if (online_status==LINPHONE_STATUS_OUTTOLUNCH)
479 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
480 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
481 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
483 <tuple id=\"sg89ae\">\n\
485 <basic>open</basic>\n\
487 <es:activity>meal</es:activity>\n\
490 <contact priority=\"0.8\">%s</contact>\n\
491 <note>out to lunch</note>\n\
494 contact_info, contact_info);
499 sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
500 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
501 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
504 "<tuple id=\"sg89ae\">\n\
506 <basic>closed</basic>\n\
508 <es:activity>permanent-absence</es:activity>\n\
514 osip_message_set_body(notify, buf, strlen(buf));
515 osip_message_set_content_type(notify, "application/pidf+xml");
518 osip_free(contact_info);
522 void linphone_friend_notify(LinphoneFriend *lf, int ss, LinphoneOnlineStatus os){
523 //printf("Wish to notify %p, lf->nid=%i\n",lf,lf->nid);
525 osip_message_t *msg=NULL;
526 const char *identity;
527 if (lf->proxy!=NULL) identity=lf->proxy->reg_identity;
528 else identity=linphone_core_get_primary_contact(lf->lc);
530 eXosip_insubscription_build_notify(lf->in_did,ss,0,&msg);
532 osip_message_set_contact(msg,identity);
533 add_presence_body(msg,os);
534 eXosip_insubscription_send_request(lf->in_did,msg);
535 }else ms_error("could not create notify for incoming subscription.");
540 static void linphone_friend_unsubscribe(LinphoneFriend *lf){
541 if (lf->out_did!=-1) {
542 osip_message_t *msg=NULL;
544 eXosip_subscribe_build_refresh_request(lf->out_did,&msg);
546 osip_message_set_expires(msg,"0");
547 eXosip_subscribe_send_refresh_request(lf->out_did,msg);
548 }else ms_error("Could not build subscribe refresh request !");
553 void linphone_friend_destroy(LinphoneFriend *lf){
554 linphone_friend_notify(lf,EXOSIP_SUBCRSTATE_TERMINATED,LINPHONE_STATUS_CLOSED);
555 linphone_friend_unsubscribe(lf);
556 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
557 if (lf->info!=NULL) buddy_info_free(lf->info);
561 void linphone_friend_check_for_removed_proxy(LinphoneFriend *lf, LinphoneProxyConfig *cfg){
567 const LinphoneAddress *linphone_friend_get_uri(const LinphoneFriend *lf){
572 bool_t linphone_friend_get_send_subscribe(const LinphoneFriend *lf){
573 return lf->subscribe;
576 LinphoneSubscribePolicy linphone_friend_get_inc_subscribe_policy(const LinphoneFriend *lf){
580 LinphoneOnlineStatus linphone_friend_get_status(const LinphoneFriend *lf){
584 BuddyInfo * linphone_friend_get_info(const LinphoneFriend *lf){
588 void linphone_friend_apply(LinphoneFriend *fr, LinphoneCore *lc){
590 ms_warning("No sip url defined.");
595 linphone_core_write_friends_config(lc);
597 if (fr->inc_subscribe_pending){
600 linphone_friend_notify(fr,EXOSIP_SUBCRSTATE_PENDING,LINPHONE_STATUS_PENDING);
602 case LinphoneSPAccept:
605 linphone_friend_notify(fr,EXOSIP_SUBCRSTATE_ACTIVE,fr->lc->presence_mode);
609 linphone_friend_notify(fr,EXOSIP_SUBCRSTATE_TERMINATED,LINPHONE_STATUS_CLOSED);
612 fr->inc_subscribe_pending=FALSE;
614 if (fr->subscribe && fr->out_did==-1){
616 __linphone_friend_do_subscribe(fr);
618 ms_message("linphone_friend_apply() done.");
622 void linphone_friend_edit(LinphoneFriend *fr){
625 void linphone_friend_done(LinphoneFriend *fr){
626 ms_return_if_fail(fr!=NULL);
627 if (fr->lc==NULL) return;
628 linphone_friend_apply(fr,fr->lc);
631 void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf)
633 ms_return_if_fail(lf->lc==NULL);
634 ms_return_if_fail(lf->uri!=NULL);
635 lc->friends=ms_list_append(lc->friends,lf);
636 linphone_friend_apply(lf,lc);
640 void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend* fl){
641 MSList *el=ms_list_find(lc->friends,(void *)fl);
643 lc->friends=ms_list_remove_link(lc->friends,el);
644 linphone_friend_destroy((LinphoneFriend*)el->data);
645 linphone_core_write_friends_config(lc);
649 void linphone_friend_set_ref_key(LinphoneFriend *lf, const char *key){
650 if (lf->refkey!=NULL){
655 lf->refkey=ms_strdup(key);
657 linphone_core_write_friends_config(lf->lc);
660 const char *linphone_friend_get_ref_key(const LinphoneFriend *lf){
664 static bool_t username_match(const char *u1, const char *u2){
665 if (u1==NULL && u2==NULL) return TRUE;
666 if (u1 && u2 && strcasecmp(u1,u2)==0) return TRUE;
670 LinphoneFriend *linphone_core_get_friend_by_uri(const LinphoneCore *lc, const char *uri){
671 LinphoneAddress *puri=linphone_address_new(uri);
673 const char *username=linphone_address_get_username(puri);
674 const char *domain=linphone_address_get_domain(puri);
675 LinphoneFriend *lf=NULL;
680 for(elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
681 lf=(LinphoneFriend*)elem->data;
682 const char *it_username=linphone_address_get_username(lf->uri);
683 const char *it_host=linphone_address_get_domain(lf->uri);;
684 if (strcasecmp(domain,it_host)==0 && username_match(username,it_username)){
689 linphone_address_destroy(puri);
693 LinphoneFriend *linphone_core_get_friend_by_ref_key(const LinphoneCore *lc, const char *key){
695 if (key==NULL) return NULL;
696 for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
697 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
698 if (lf->refkey!=NULL && strcmp(lf->refkey,key)==0){
705 #define key_compare(s1,s2) strcmp(s1,s2)
707 LinphoneSubscribePolicy __policy_str_to_enum(const char* pol){
708 if (key_compare("accept",pol)==0){
709 return LinphoneSPAccept;
711 if (key_compare("deny",pol)==0){
712 return LinphoneSPDeny;
714 if (key_compare("wait",pol)==0){
715 return LinphoneSPWait;
717 ms_warning("Unrecognized subscribe policy: %s",pol);
718 return LinphoneSPWait;
721 LinphoneProxyConfig *__index_to_proxy(LinphoneCore *lc, int index){
722 if (index>=0) return (LinphoneProxyConfig*)ms_list_nth_data(lc->sip_conf.proxies,index);
726 LinphoneFriend * linphone_friend_new_from_config_file(LinphoneCore *lc, int index){
731 LpConfig *config=lc->config;
733 sprintf(item,"friend_%i",index);
735 if (!lp_config_has_section(config,item)){
739 tmp=lp_config_get_string(config,item,"url",NULL);
743 lf=linphone_friend_new_with_addr(tmp);
747 tmp=lp_config_get_string(config,item,"pol",NULL);
748 if (tmp==NULL) linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPWait);
750 linphone_friend_set_inc_subscribe_policy(lf,__policy_str_to_enum(tmp));
752 a=lp_config_get_int(config,item,"subscribe",0);
753 linphone_friend_send_subscribe(lf,a);
755 a=lp_config_get_int(config,item,"proxy",-1);
757 linphone_friend_set_proxy(lf,__index_to_proxy(lc,a));
759 linphone_friend_set_ref_key(lf,lp_config_get_string(config,item,"refkey",NULL));
763 const char *__policy_enum_to_str(LinphoneSubscribePolicy pol){
765 case LinphoneSPAccept:
775 ms_warning("Invalid policy enum value.");
779 void linphone_friend_write_to_config_file(LpConfig *config, LinphoneFriend *lf, int index){
785 sprintf(key,"friend_%i",index);
788 lp_config_clean_section(config,key);
792 tmp=linphone_address_as_string(lf->uri);
796 lp_config_set_string(config,key,"url",tmp);
799 lp_config_set_string(config,key,"pol",__policy_enum_to_str(lf->pol));
800 lp_config_set_int(config,key,"subscribe",lf->subscribe);
801 if (lf->proxy!=NULL){
802 a=ms_list_index(lf->lc->sip_conf.proxies,lf->proxy);
803 lp_config_set_int(config,key,"proxy",a);
804 }else lp_config_set_int(config,key,"proxy",-1);
806 refkey=linphone_friend_get_ref_key(lf);
808 lp_config_set_string(config,key,"refkey",refkey);
812 void linphone_core_write_friends_config(LinphoneCore* lc)
816 if (!lc->ready) return; /*dont write config when reading it !*/
817 for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
818 linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
820 linphone_friend_write_to_config_file(lc->config,NULL,i); /* set the end */