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 LinphoneFriend *fr=linphone_friend_new();
175 if (linphone_friend_set_sip_addr(fr,addr)<0){
176 linphone_friend_destroy(fr);
182 bool_t linphone_friend_in_list(const LinphoneFriend *lf){
186 void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char **result){
187 LinphoneAddress *fr=NULL;
189 fr=linphone_address_new(uri);
192 if (strchr(uri,'@')!=NULL){
195 tmp=ms_strdup_printf("sip:%s",uri);
196 u=linphone_address_new(tmp);
200 }else if (lc->default_proxy!=NULL){
201 /*try adding domain part from default current proxy*/
202 LinphoneAddress * id=linphone_address_new(linphone_core_get_identity(lc));
204 linphone_address_set_username(id,uri);
205 *result=linphone_address_as_string(id);
206 linphone_address_destroy(id);
211 ms_message("%s interpreted as %s",uri,*result);
213 ms_warning("Fail to interpret friend uri %s",uri);
215 }else *result=linphone_address_as_string(fr);
216 linphone_address_destroy(fr);
219 int linphone_friend_set_sip_addr(LinphoneFriend *lf, const char *addr){
220 LinphoneAddress *fr=linphone_address_new(addr);
222 ms_warning("Invalid friend sip uri: %s",addr);
225 linphone_address_clean(fr);
226 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
231 int linphone_friend_set_name(LinphoneFriend *lf, const char *name){
232 LinphoneAddress *fr=lf->uri;
234 ms_error("linphone_friend_set_sip_addr() must be called before linphone_friend_set_name().");
237 linphone_address_set_display_name(fr,name);
241 int linphone_friend_enable_subscribes(LinphoneFriend *fr, bool_t val){
246 int linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol)
252 SalPresenceStatus linphone_online_status_to_sal(LinphoneOnlineStatus os){
254 case LinphoneStatusOffline:
255 return SalPresenceOffline;
257 case LinphoneStatusOnline:
258 return SalPresenceOnline;
260 case LinphoneStatusBusy:
261 return SalPresenceBusy;
263 case LinphoneStatusBeRightBack:
264 return SalPresenceBerightback;
266 case LinphoneStatusAway:
267 return SalPresenceAway;
269 case LinphoneStatusOnThePhone:
270 return SalPresenceOnthephone;
272 case LinphoneStatusOutToLunch:
273 return SalPresenceOuttolunch;
275 case LinphoneStatusDoNotDisturb:
276 return SalPresenceDonotdisturb;
278 case LinphoneStatusMoved:
279 return SalPresenceMoved;
281 case LinphoneStatusAltService:
282 return SalPresenceAltService;
284 case LinphoneStatusPending:
285 return SalPresenceOffline;
288 return SalPresenceOffline;
291 return SalPresenceOffline;
294 void linphone_friend_notify(LinphoneFriend *lf, LinphoneOnlineStatus os){
295 //printf("Wish to notify %p, lf->nid=%i\n",lf,lf->nid);
296 if (lf->insub!=NULL){
297 sal_notify_presence(lf->insub,linphone_online_status_to_sal(os),NULL);
301 static void linphone_friend_unsubscribe(LinphoneFriend *lf){
302 if (lf->outsub!=NULL) {
303 sal_unsubscribe(lf->outsub);
304 sal_op_release(lf->outsub);
306 lf->subscribe_active=FALSE;
310 void linphone_friend_close_subscriptions(LinphoneFriend *lf){
311 linphone_friend_unsubscribe(lf);
313 sal_notify_close(lf->insub);
314 sal_op_release(lf->insub);
319 void linphone_friend_destroy(LinphoneFriend *lf){
321 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
322 if (lf->info!=NULL) buddy_info_free(lf->info);
326 const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf){
330 bool_t linphone_friend_get_send_subscribe(const LinphoneFriend *lf){
331 return lf->subscribe;
334 LinphoneSubscribePolicy linphone_friend_get_inc_subscribe_policy(const LinphoneFriend *lf){
338 LinphoneOnlineStatus linphone_friend_get_status(const LinphoneFriend *lf){
342 BuddyInfo * linphone_friend_get_info(const LinphoneFriend *lf){
346 void linphone_friend_apply(LinphoneFriend *fr, LinphoneCore *lc){
348 ms_warning("No sip url defined.");
353 linphone_core_write_friends_config(lc);
355 if (fr->inc_subscribe_pending){
358 linphone_friend_notify(fr,LinphoneStatusPending);
360 case LinphoneSPAccept:
363 linphone_friend_notify(fr,fr->lc->presence_mode);
367 linphone_friend_notify(fr,LinphoneStatusOffline);
370 fr->inc_subscribe_pending=FALSE;
372 if (fr->subscribe && fr->subscribe_active==FALSE){
373 ms_message("Sending a new SUBSCRIBE");
374 __linphone_friend_do_subscribe(fr);
376 ms_message("linphone_friend_apply() done.");
381 void linphone_friend_edit(LinphoneFriend *fr){
384 void linphone_friend_done(LinphoneFriend *fr){
385 ms_return_if_fail(fr!=NULL);
386 if (fr->lc==NULL) return;
387 linphone_friend_apply(fr,fr->lc);
390 void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf)
392 ms_return_if_fail(lf->lc==NULL);
393 ms_return_if_fail(lf->uri!=NULL);
394 if (ms_list_find(lc->friends,lf)!=NULL){
396 const LinphoneAddress *addr=linphone_friend_get_address(lf);
397 if (addr) tmp=linphone_address_as_string(addr);
398 ms_warning("Friend %s already in list, ignored.", tmp ? tmp : "unknown");
399 if (tmp) ms_free(tmp);
402 lc->friends=ms_list_append(lc->friends,lf);
403 if ( linphone_core_ready(lc)) linphone_friend_apply(lf,lc);
404 else lf->commit=TRUE;
408 void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend* fl){
409 MSList *el=ms_list_find(lc->friends,(void *)fl);
411 lc->friends=ms_list_remove_link(lc->friends,el);
412 linphone_friend_destroy((LinphoneFriend*)el->data);
413 linphone_core_write_friends_config(lc);
417 void linphone_core_send_initial_subscribes(LinphoneCore *lc){
419 for(elem=lc->friends;elem!=NULL;elem=elem->next){
420 LinphoneFriend *f=(LinphoneFriend*)elem->data;
422 linphone_friend_apply(f,lc);
426 void linphone_friend_set_ref_key(LinphoneFriend *lf, const char *key){
427 if (lf->refkey!=NULL){
432 lf->refkey=ms_strdup(key);
434 linphone_core_write_friends_config(lf->lc);
437 const char *linphone_friend_get_ref_key(const LinphoneFriend *lf){
441 static bool_t username_match(const char *u1, const char *u2){
442 if (u1==NULL && u2==NULL) return TRUE;
443 if (u1 && u2 && strcasecmp(u1,u2)==0) return TRUE;
447 LinphoneFriend *linphone_core_get_friend_by_address(const LinphoneCore *lc, const char *uri){
448 LinphoneAddress *puri=linphone_address_new(uri);
450 const char *username=linphone_address_get_username(puri);
451 const char *domain=linphone_address_get_domain(puri);
452 LinphoneFriend *lf=NULL;
457 for(elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
458 lf=(LinphoneFriend*)elem->data;
459 const char *it_username=linphone_address_get_username(lf->uri);
460 const char *it_host=linphone_address_get_domain(lf->uri);;
461 if (strcasecmp(domain,it_host)==0 && username_match(username,it_username)){
466 linphone_address_destroy(puri);
470 LinphoneFriend *linphone_core_get_friend_by_ref_key(const LinphoneCore *lc, const char *key){
472 if (key==NULL) return NULL;
473 for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
474 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
475 if (lf->refkey!=NULL && strcmp(lf->refkey,key)==0){
482 #define key_compare(s1,s2) strcmp(s1,s2)
484 LinphoneSubscribePolicy __policy_str_to_enum(const char* pol){
485 if (key_compare("accept",pol)==0){
486 return LinphoneSPAccept;
488 if (key_compare("deny",pol)==0){
489 return LinphoneSPDeny;
491 if (key_compare("wait",pol)==0){
492 return LinphoneSPWait;
494 ms_warning("Unrecognized subscribe policy: %s",pol);
495 return LinphoneSPWait;
498 LinphoneProxyConfig *__index_to_proxy(LinphoneCore *lc, int index){
499 if (index>=0) return (LinphoneProxyConfig*)ms_list_nth_data(lc->sip_conf.proxies,index);
503 LinphoneFriend * linphone_friend_new_from_config_file(LinphoneCore *lc, int index){
508 LpConfig *config=lc->config;
510 sprintf(item,"friend_%i",index);
512 if (!lp_config_has_section(config,item)){
516 tmp=lp_config_get_string(config,item,"url",NULL);
520 lf=linphone_friend_new_with_addr(tmp);
524 tmp=lp_config_get_string(config,item,"pol",NULL);
525 if (tmp==NULL) linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPWait);
527 linphone_friend_set_inc_subscribe_policy(lf,__policy_str_to_enum(tmp));
529 a=lp_config_get_int(config,item,"subscribe",0);
530 linphone_friend_send_subscribe(lf,a);
532 linphone_friend_set_ref_key(lf,lp_config_get_string(config,item,"refkey",NULL));
536 const char *__policy_enum_to_str(LinphoneSubscribePolicy pol){
538 case LinphoneSPAccept:
548 ms_warning("Invalid policy enum value.");
552 void linphone_friend_write_to_config_file(LpConfig *config, LinphoneFriend *lf, int index){
557 sprintf(key,"friend_%i",index);
560 lp_config_clean_section(config,key);
564 tmp=linphone_address_as_string(lf->uri);
568 lp_config_set_string(config,key,"url",tmp);
571 lp_config_set_string(config,key,"pol",__policy_enum_to_str(lf->pol));
572 lp_config_set_int(config,key,"subscribe",lf->subscribe);
574 refkey=linphone_friend_get_ref_key(lf);
576 lp_config_set_string(config,key,"refkey",refkey);
580 void linphone_core_write_friends_config(LinphoneCore* lc)
584 if (! linphone_core_ready(lc)) return; /*dont write config when reading it !*/
585 for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
586 linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
588 linphone_friend_write_to_config_file(lc->config,NULL,i); /* set the end */