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"
29 const char *linphone_online_status_to_string(LinphoneOnlineStatus ss){
32 case LinphoneStatusOnline:
35 case LinphoneStatusBusy:
38 case LinphoneStatusBeRightBack:
39 str=_("Be right back");
41 case LinphoneStatusAway:
44 case LinphoneStatusOnThePhone:
45 str=_("On the phone");
47 case LinphoneStatusOutToLunch:
48 str=_("Out to lunch");
50 case LinphoneStatusDoNotDisturb:
51 str=_("Do not disturb");
53 case LinphoneStatusMoved:
56 case LinphoneStatusAltService:
57 str=_("Using another messaging service");
59 case LinphoneStatusOffline:
62 case LinphoneStatusPending:
71 static int friend_data_compare(const void * a, const void * b, void * data){
72 LinphoneAddress *fa=((LinphoneFriend*)a)->uri;
73 LinphoneAddress *fb=((LinphoneFriend*)b)->uri;
75 ua=linphone_address_get_username(fa);
76 ub=linphone_address_get_username(fb);
77 if (ua!=NULL && ub!=NULL) {
78 //printf("Comparing usernames %s,%s\n",ua,ub);
79 return strcasecmp(ua,ub);
83 ua=linphone_address_get_domain(fa);
84 ub=linphone_address_get_domain(fb);
85 if (ua!=NULL && ub!=NULL){
86 int ret=strcasecmp(ua,ub);
87 //printf("Comparing hostnames %s,%s,res=%i\n",ua,ub,ret);
94 static int friend_compare(const void * a, const void * b){
95 return friend_data_compare(a,b,NULL);
99 MSList *linphone_find_friend(MSList *fl, const LinphoneAddress *friend, LinphoneFriend **lf){
101 LinphoneFriend dummy;
102 if (lf!=NULL) *lf=NULL;
103 dummy.uri=(LinphoneAddress*)friend;
104 res=ms_list_find_custom(fl,friend_compare,&dummy);
105 if (lf!=NULL && res!=NULL) *lf=(LinphoneFriend*)res->data;
109 LinphoneFriend *linphone_find_friend_by_inc_subscribe(MSList *l, SalOp *op){
111 for (elem=l;elem!=NULL;elem=elem->next){
112 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
113 if (lf->insub==op) return lf;
118 LinphoneFriend *linphone_find_friend_by_out_subscribe(MSList *l, SalOp *op){
120 for (elem=l;elem!=NULL;elem=elem->next){
121 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
122 if (lf->outsub==op) return lf;
127 void __linphone_friend_do_subscribe(LinphoneFriend *fr){
129 const char *route=NULL;
130 const char *from=NULL;
131 const char *fixed_contact=NULL;
132 LinphoneProxyConfig *cfg;
134 friend=linphone_address_as_string(fr->uri);
135 cfg=linphone_core_lookup_known_proxy(fr->lc,linphone_friend_get_address(fr));
137 route=linphone_proxy_config_get_route(cfg);
138 from=linphone_proxy_config_get_identity(cfg);
140 fixed_contact=sal_op_get_contact(cfg->op);
142 ms_message("Contact for subscribe has been fixed using proxy to %s",fixed_contact);
145 }else from=linphone_core_get_primary_contact(fr->lc);
146 if (fr->outsub==NULL){
147 /* people for which we don't have yet an answer should appear as offline */
148 fr->status=LinphoneStatusOffline;
150 if (fr->lc->vtable.notify_recv)
151 fr->lc->vtable.notify_recv(fr->lc,(LinphoneFriend*)fr);
154 sal_op_release(fr->outsub);
157 fr->outsub=sal_op_new(fr->lc->sal);
158 sal_op_set_route(fr->outsub,route);
159 sal_op_set_contact(fr->outsub,fixed_contact);
160 sal_subscribe_presence(fr->outsub,from,friend);
161 fr->subscribe_active=TRUE;
165 LinphoneFriend * linphone_friend_new(){
166 LinphoneFriend *obj=ms_new0(LinphoneFriend,1);
167 obj->pol=LinphoneSPAccept;
168 obj->status=LinphoneStatusOffline;
173 LinphoneFriend *linphone_friend_new_with_addr(const char *addr){
174 LinphoneAddress* linphone_address = linphone_address_new(addr);
175 if (linphone_address == NULL) {
176 ms_error("Cannot create friend for address [%s]",addr?addr:"null");
179 LinphoneFriend *fr=linphone_friend_new();
180 if (linphone_friend_set_addr(fr,linphone_address)<0){
181 linphone_friend_destroy(fr);
187 bool_t linphone_friend_in_list(const LinphoneFriend *lf){
191 void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char **result){
192 LinphoneAddress *fr=NULL;
194 fr=linphone_address_new(uri);
197 if (strchr(uri,'@')!=NULL){
200 tmp=ms_strdup_printf("sip:%s",uri);
201 u=linphone_address_new(tmp);
205 }else if (lc->default_proxy!=NULL){
206 /*try adding domain part from default current proxy*/
207 LinphoneAddress * id=linphone_address_new(linphone_core_get_identity(lc));
209 linphone_address_set_username(id,uri);
210 *result=linphone_address_as_string(id);
211 linphone_address_destroy(id);
216 ms_message("%s interpreted as %s",uri,*result);
218 ms_warning("Fail to interpret friend uri %s",uri);
220 }else *result=linphone_address_as_string(fr);
221 linphone_address_destroy(fr);
224 int linphone_friend_set_addr(LinphoneFriend *lf, const LinphoneAddress *addr){
225 LinphoneAddress *fr=linphone_address_clone(addr);
227 ms_warning("Invalid friend sip uri: %s",addr);
230 linphone_address_clean(fr);
231 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
236 int linphone_friend_set_name(LinphoneFriend *lf, const char *name){
237 LinphoneAddress *fr=lf->uri;
239 ms_error("linphone_friend_set_sip_addr() must be called before linphone_friend_set_name().");
242 linphone_address_set_display_name(fr,name);
246 int linphone_friend_enable_subscribes(LinphoneFriend *fr, bool_t val){
251 int linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol)
257 SalPresenceStatus linphone_online_status_to_sal(LinphoneOnlineStatus os){
259 case LinphoneStatusOffline:
260 return SalPresenceOffline;
262 case LinphoneStatusOnline:
263 return SalPresenceOnline;
265 case LinphoneStatusBusy:
266 return SalPresenceBusy;
268 case LinphoneStatusBeRightBack:
269 return SalPresenceBerightback;
271 case LinphoneStatusAway:
272 return SalPresenceAway;
274 case LinphoneStatusOnThePhone:
275 return SalPresenceOnthephone;
277 case LinphoneStatusOutToLunch:
278 return SalPresenceOuttolunch;
280 case LinphoneStatusDoNotDisturb:
281 return SalPresenceDonotdisturb;
283 case LinphoneStatusMoved:
284 return SalPresenceMoved;
286 case LinphoneStatusAltService:
287 return SalPresenceAltService;
289 case LinphoneStatusPending:
290 return SalPresenceOffline;
293 return SalPresenceOffline;
296 return SalPresenceOffline;
299 void linphone_friend_notify(LinphoneFriend *lf, LinphoneOnlineStatus os){
300 //printf("Wish to notify %p, lf->nid=%i\n",lf,lf->nid);
301 if (lf->insub!=NULL){
302 sal_notify_presence(lf->insub,linphone_online_status_to_sal(os),NULL);
306 static void linphone_friend_unsubscribe(LinphoneFriend *lf){
307 if (lf->outsub!=NULL) {
308 sal_unsubscribe(lf->outsub);
309 sal_op_release(lf->outsub);
311 lf->subscribe_active=FALSE;
315 void linphone_friend_close_subscriptions(LinphoneFriend *lf){
316 linphone_friend_unsubscribe(lf);
318 sal_notify_close(lf->insub);
319 sal_op_release(lf->insub);
324 void linphone_friend_destroy(LinphoneFriend *lf){
326 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
327 if (lf->info!=NULL) buddy_info_free(lf->info);
331 const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf){
335 bool_t linphone_friend_get_send_subscribe(const LinphoneFriend *lf){
336 return lf->subscribe;
339 LinphoneSubscribePolicy linphone_friend_get_inc_subscribe_policy(const LinphoneFriend *lf){
343 LinphoneOnlineStatus linphone_friend_get_status(const LinphoneFriend *lf){
347 BuddyInfo * linphone_friend_get_info(const LinphoneFriend *lf){
351 void linphone_friend_apply(LinphoneFriend *fr, LinphoneCore *lc){
353 ms_warning("No sip url defined.");
358 linphone_core_write_friends_config(lc);
360 if (fr->inc_subscribe_pending){
363 linphone_friend_notify(fr,LinphoneStatusPending);
365 case LinphoneSPAccept:
368 linphone_friend_notify(fr,fr->lc->presence_mode);
372 linphone_friend_notify(fr,LinphoneStatusOffline);
375 fr->inc_subscribe_pending=FALSE;
377 if (fr->subscribe && fr->subscribe_active==FALSE){
378 ms_message("Sending a new SUBSCRIBE");
379 __linphone_friend_do_subscribe(fr);
381 ms_message("linphone_friend_apply() done.");
386 void linphone_friend_edit(LinphoneFriend *fr){
389 void linphone_friend_done(LinphoneFriend *fr){
390 ms_return_if_fail(fr!=NULL);
391 if (fr->lc==NULL) return;
392 linphone_friend_apply(fr,fr->lc);
395 void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf)
397 ms_return_if_fail(lf->lc==NULL);
398 ms_return_if_fail(lf->uri!=NULL);
399 if (ms_list_find(lc->friends,lf)!=NULL){
401 const LinphoneAddress *addr=linphone_friend_get_address(lf);
402 if (addr) tmp=linphone_address_as_string(addr);
403 ms_warning("Friend %s already in list, ignored.", tmp ? tmp : "unknown");
404 if (tmp) ms_free(tmp);
407 lc->friends=ms_list_append(lc->friends,lf);
408 if ( linphone_core_ready(lc)) linphone_friend_apply(lf,lc);
409 else lf->commit=TRUE;
413 void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend* fl){
414 MSList *el=ms_list_find(lc->friends,(void *)fl);
416 lc->friends=ms_list_remove_link(lc->friends,el);
417 linphone_friend_destroy((LinphoneFriend*)el->data);
418 linphone_core_write_friends_config(lc);
422 void linphone_core_send_initial_subscribes(LinphoneCore *lc){
424 for(elem=lc->friends;elem!=NULL;elem=elem->next){
425 LinphoneFriend *f=(LinphoneFriend*)elem->data;
427 linphone_friend_apply(f,lc);
431 void linphone_friend_set_ref_key(LinphoneFriend *lf, const char *key){
432 if (lf->refkey!=NULL){
437 lf->refkey=ms_strdup(key);
439 linphone_core_write_friends_config(lf->lc);
442 const char *linphone_friend_get_ref_key(const LinphoneFriend *lf){
446 static bool_t username_match(const char *u1, const char *u2){
447 if (u1==NULL && u2==NULL) return TRUE;
448 if (u1 && u2 && strcasecmp(u1,u2)==0) return TRUE;
452 LinphoneFriend *linphone_core_get_friend_by_address(const LinphoneCore *lc, const char *uri){
453 LinphoneAddress *puri=linphone_address_new(uri);
455 const char *username=linphone_address_get_username(puri);
456 const char *domain=linphone_address_get_domain(puri);
457 LinphoneFriend *lf=NULL;
462 for(elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
463 lf=(LinphoneFriend*)elem->data;
464 const char *it_username=linphone_address_get_username(lf->uri);
465 const char *it_host=linphone_address_get_domain(lf->uri);;
466 if (strcasecmp(domain,it_host)==0 && username_match(username,it_username)){
471 linphone_address_destroy(puri);
475 LinphoneFriend *linphone_core_get_friend_by_ref_key(const LinphoneCore *lc, const char *key){
477 if (key==NULL) return NULL;
478 for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
479 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
480 if (lf->refkey!=NULL && strcmp(lf->refkey,key)==0){
487 #define key_compare(s1,s2) strcmp(s1,s2)
489 LinphoneSubscribePolicy __policy_str_to_enum(const char* pol){
490 if (key_compare("accept",pol)==0){
491 return LinphoneSPAccept;
493 if (key_compare("deny",pol)==0){
494 return LinphoneSPDeny;
496 if (key_compare("wait",pol)==0){
497 return LinphoneSPWait;
499 ms_warning("Unrecognized subscribe policy: %s",pol);
500 return LinphoneSPWait;
503 LinphoneProxyConfig *__index_to_proxy(LinphoneCore *lc, int index){
504 if (index>=0) return (LinphoneProxyConfig*)ms_list_nth_data(lc->sip_conf.proxies,index);
508 LinphoneFriend * linphone_friend_new_from_config_file(LinphoneCore *lc, int index){
513 LpConfig *config=lc->config;
515 sprintf(item,"friend_%i",index);
517 if (!lp_config_has_section(config,item)){
521 tmp=lp_config_get_string(config,item,"url",NULL);
525 lf=linphone_friend_new_with_addr(tmp);
529 tmp=lp_config_get_string(config,item,"pol",NULL);
530 if (tmp==NULL) linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPWait);
532 linphone_friend_set_inc_subscribe_policy(lf,__policy_str_to_enum(tmp));
534 a=lp_config_get_int(config,item,"subscribe",0);
535 linphone_friend_send_subscribe(lf,a);
537 linphone_friend_set_ref_key(lf,lp_config_get_string(config,item,"refkey",NULL));
541 const char *__policy_enum_to_str(LinphoneSubscribePolicy pol){
543 case LinphoneSPAccept:
553 ms_warning("Invalid policy enum value.");
557 void linphone_friend_write_to_config_file(LpConfig *config, LinphoneFriend *lf, int index){
562 sprintf(key,"friend_%i",index);
565 lp_config_clean_section(config,key);
569 tmp=linphone_address_as_string(lf->uri);
573 lp_config_set_string(config,key,"url",tmp);
576 lp_config_set_string(config,key,"pol",__policy_enum_to_str(lf->pol));
577 lp_config_set_int(config,key,"subscribe",lf->subscribe);
579 refkey=linphone_friend_get_ref_key(lf);
581 lp_config_set_string(config,key,"refkey",refkey);
585 void linphone_core_write_friends_config(LinphoneCore* lc)
589 if (! linphone_core_ready(lc)) return; /*dont write config when reading it !*/
590 for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
591 linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
593 linphone_friend_write_to_config_file(lc->config,NULL,i); /* set the end */