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_compare(const void * a, const void * b){
72 LinphoneAddress *fa=((LinphoneFriend*)a)->uri;
73 LinphoneAddress *fb=((LinphoneFriend*)b)->uri;
74 if (linphone_address_weak_equal (fa,fb)) return 0;
79 MSList *linphone_find_friend(MSList *fl, const LinphoneAddress *friend, LinphoneFriend **lf){
82 if (lf!=NULL) *lf=NULL;
83 dummy.uri=(LinphoneAddress*)friend;
84 res=ms_list_find_custom(fl,friend_compare,&dummy);
85 if (lf!=NULL && res!=NULL) *lf=(LinphoneFriend*)res->data;
89 LinphoneFriend *linphone_find_friend_by_inc_subscribe(MSList *l, SalOp *op){
91 for (elem=l;elem!=NULL;elem=elem->next){
92 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
93 if (lf->insub==op) return lf;
98 LinphoneFriend *linphone_find_friend_by_out_subscribe(MSList *l, SalOp *op){
100 for (elem=l;elem!=NULL;elem=elem->next){
101 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
102 if (lf->outsub==op) return lf;
107 void __linphone_friend_do_subscribe(LinphoneFriend *fr){
109 const char *route=NULL;
110 const char *from=NULL;
111 const char *fixed_contact=NULL;
112 LinphoneProxyConfig *cfg;
114 friend=linphone_address_as_string(fr->uri);
115 cfg=linphone_core_lookup_known_proxy(fr->lc,linphone_friend_get_address(fr));
117 route=linphone_proxy_config_get_route(cfg);
118 from=linphone_proxy_config_get_identity(cfg);
120 fixed_contact=sal_op_get_contact(cfg->op);
122 ms_message("Contact for subscribe has been fixed using proxy to %s",fixed_contact);
125 }else from=linphone_core_get_primary_contact(fr->lc);
126 if (fr->outsub==NULL){
127 /* people for which we don't have yet an answer should appear as offline */
128 fr->status=LinphoneStatusOffline;
130 if (fr->lc->vtable.notify_recv)
131 fr->lc->vtable.notify_recv(fr->lc,(LinphoneFriend*)fr);
134 sal_op_release(fr->outsub);
137 fr->outsub=sal_op_new(fr->lc->sal);
138 sal_op_set_route(fr->outsub,route);
139 sal_op_set_contact(fr->outsub,fixed_contact);
140 sal_subscribe_presence(fr->outsub,from,friend);
141 fr->subscribe_active=TRUE;
145 LinphoneFriend * linphone_friend_new(){
146 LinphoneFriend *obj=ms_new0(LinphoneFriend,1);
147 obj->pol=LinphoneSPAccept;
148 obj->status=LinphoneStatusOffline;
153 LinphoneFriend *linphone_friend_new_with_addr(const char *addr){
154 LinphoneAddress* linphone_address = linphone_address_new(addr);
155 if (linphone_address == NULL) {
156 ms_error("Cannot create friend for address [%s]",addr?addr:"null");
159 LinphoneFriend *fr=linphone_friend_new();
160 if (linphone_friend_set_addr(fr,linphone_address)<0){
161 linphone_friend_destroy(fr);
167 bool_t linphone_friend_in_list(const LinphoneFriend *lf){
171 void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char **result){
172 LinphoneAddress *fr=NULL;
174 fr=linphone_address_new(uri);
177 if (strchr(uri,'@')!=NULL){
180 tmp=ms_strdup_printf("sip:%s",uri);
181 u=linphone_address_new(tmp);
185 }else if (lc->default_proxy!=NULL){
186 /*try adding domain part from default current proxy*/
187 LinphoneAddress * id=linphone_address_new(linphone_core_get_identity(lc));
189 linphone_address_set_display_name(id,NULL);
190 linphone_address_set_username(id,uri);
191 *result=linphone_address_as_string(id);
192 linphone_address_destroy(id);
197 ms_message("%s interpreted as %s",uri,*result);
199 ms_warning("Fail to interpret friend uri %s",uri);
201 }else *result=linphone_address_as_string(fr);
202 linphone_address_destroy(fr);
205 int linphone_friend_set_addr(LinphoneFriend *lf, const LinphoneAddress *addr){
206 LinphoneAddress *fr=linphone_address_clone(addr);
207 linphone_address_clean(fr);
208 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
213 int linphone_friend_set_name(LinphoneFriend *lf, const char *name){
214 LinphoneAddress *fr=lf->uri;
216 ms_error("linphone_friend_set_sip_addr() must be called before linphone_friend_set_name().");
219 linphone_address_set_display_name(fr,name);
223 int linphone_friend_enable_subscribes(LinphoneFriend *fr, bool_t val){
228 int linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol)
234 SalPresenceStatus linphone_online_status_to_sal(LinphoneOnlineStatus os){
236 case LinphoneStatusOffline:
237 return SalPresenceOffline;
239 case LinphoneStatusOnline:
240 return SalPresenceOnline;
242 case LinphoneStatusBusy:
243 return SalPresenceBusy;
245 case LinphoneStatusBeRightBack:
246 return SalPresenceBerightback;
248 case LinphoneStatusAway:
249 return SalPresenceAway;
251 case LinphoneStatusOnThePhone:
252 return SalPresenceOnthephone;
254 case LinphoneStatusOutToLunch:
255 return SalPresenceOuttolunch;
257 case LinphoneStatusDoNotDisturb:
258 return SalPresenceDonotdisturb;
260 case LinphoneStatusMoved:
261 return SalPresenceMoved;
263 case LinphoneStatusAltService:
264 return SalPresenceAltService;
266 case LinphoneStatusPending:
267 return SalPresenceOffline;
270 return SalPresenceOffline;
273 return SalPresenceOffline;
276 void linphone_friend_notify(LinphoneFriend *lf, LinphoneOnlineStatus os){
277 char *addr=linphone_address_as_string(linphone_friend_get_address(lf));
278 ms_message("Want to notify %s, insub=%p",addr,lf->insub);
280 if (lf->insub!=NULL){
281 sal_notify_presence(lf->insub,linphone_online_status_to_sal(os),NULL);
285 static void linphone_friend_unsubscribe(LinphoneFriend *lf){
286 if (lf->outsub!=NULL) {
287 sal_unsubscribe(lf->outsub);
288 lf->subscribe_active=FALSE;
292 void linphone_friend_close_subscriptions(LinphoneFriend *lf){
293 linphone_friend_unsubscribe(lf);
295 sal_notify_close(lf->insub);
300 void linphone_friend_destroy(LinphoneFriend *lf){
302 sal_op_release(lf->insub);
306 sal_op_release(lf->outsub);
309 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
310 if (lf->info!=NULL) buddy_info_free(lf->info);
314 const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf){
318 bool_t linphone_friend_get_send_subscribe(const LinphoneFriend *lf){
319 return lf->subscribe;
322 LinphoneSubscribePolicy linphone_friend_get_inc_subscribe_policy(const LinphoneFriend *lf){
326 LinphoneOnlineStatus linphone_friend_get_status(const LinphoneFriend *lf){
330 BuddyInfo * linphone_friend_get_info(const LinphoneFriend *lf){
334 void linphone_friend_apply(LinphoneFriend *fr, LinphoneCore *lc){
336 ms_warning("No sip url defined.");
341 linphone_core_write_friends_config(lc);
343 if (fr->inc_subscribe_pending){
346 linphone_friend_notify(fr,LinphoneStatusPending);
348 case LinphoneSPAccept:
351 linphone_friend_notify(fr,fr->lc->presence_mode);
355 linphone_friend_notify(fr,LinphoneStatusOffline);
358 fr->inc_subscribe_pending=FALSE;
360 if (fr->subscribe && fr->subscribe_active==FALSE){
361 ms_message("Sending a new SUBSCRIBE");
362 __linphone_friend_do_subscribe(fr);
364 ms_message("linphone_friend_apply() done.");
369 void linphone_friend_edit(LinphoneFriend *fr){
372 void linphone_friend_done(LinphoneFriend *fr){
373 ms_return_if_fail(fr!=NULL);
374 if (fr->lc==NULL) return;
375 linphone_friend_apply(fr,fr->lc);
378 void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf)
380 ms_return_if_fail(lf->lc==NULL);
381 ms_return_if_fail(lf->uri!=NULL);
382 if (ms_list_find(lc->friends,lf)!=NULL){
384 const LinphoneAddress *addr=linphone_friend_get_address(lf);
385 if (addr) tmp=linphone_address_as_string(addr);
386 ms_warning("Friend %s already in list, ignored.", tmp ? tmp : "unknown");
387 if (tmp) ms_free(tmp);
390 lc->friends=ms_list_append(lc->friends,lf);
391 if ( linphone_core_ready(lc)) linphone_friend_apply(lf,lc);
392 else lf->commit=TRUE;
396 void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend* fl){
397 MSList *el=ms_list_find(lc->friends,(void *)fl);
399 linphone_friend_destroy((LinphoneFriend*)el->data);
400 lc->friends=ms_list_remove_link(lc->friends,el);
401 linphone_core_write_friends_config(lc);
405 void linphone_core_send_initial_subscribes(LinphoneCore *lc){
407 for(elem=lc->friends;elem!=NULL;elem=elem->next){
408 LinphoneFriend *f=(LinphoneFriend*)elem->data;
410 linphone_friend_apply(f,lc);
414 void linphone_friend_set_ref_key(LinphoneFriend *lf, const char *key){
415 if (lf->refkey!=NULL){
420 lf->refkey=ms_strdup(key);
422 linphone_core_write_friends_config(lf->lc);
425 const char *linphone_friend_get_ref_key(const LinphoneFriend *lf){
429 static bool_t username_match(const char *u1, const char *u2){
430 if (u1==NULL && u2==NULL) return TRUE;
431 if (u1 && u2 && strcasecmp(u1,u2)==0) return TRUE;
435 LinphoneFriend *linphone_core_get_friend_by_address(const LinphoneCore *lc, const char *uri){
436 LinphoneAddress *puri=linphone_address_new(uri);
438 const char *username;
440 LinphoneFriend *lf=NULL;
445 username=linphone_address_get_username(puri);
446 domain=linphone_address_get_domain(puri);
448 linphone_address_destroy(puri);
451 for(elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
452 lf=(LinphoneFriend*)elem->data;
453 const char *it_username=linphone_address_get_username(lf->uri);
454 const char *it_host=linphone_address_get_domain(lf->uri);;
455 if (strcasecmp(domain,it_host)==0 && username_match(username,it_username)){
460 linphone_address_destroy(puri);
464 LinphoneFriend *linphone_core_get_friend_by_ref_key(const LinphoneCore *lc, const char *key){
466 if (key==NULL) return NULL;
467 for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
468 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
469 if (lf->refkey!=NULL && strcmp(lf->refkey,key)==0){
476 #define key_compare(s1,s2) strcmp(s1,s2)
478 LinphoneSubscribePolicy __policy_str_to_enum(const char* pol){
479 if (key_compare("accept",pol)==0){
480 return LinphoneSPAccept;
482 if (key_compare("deny",pol)==0){
483 return LinphoneSPDeny;
485 if (key_compare("wait",pol)==0){
486 return LinphoneSPWait;
488 ms_warning("Unrecognized subscribe policy: %s",pol);
489 return LinphoneSPWait;
492 LinphoneProxyConfig *__index_to_proxy(LinphoneCore *lc, int index){
493 if (index>=0) return (LinphoneProxyConfig*)ms_list_nth_data(lc->sip_conf.proxies,index);
497 LinphoneFriend * linphone_friend_new_from_config_file(LinphoneCore *lc, int index){
502 LpConfig *config=lc->config;
504 sprintf(item,"friend_%i",index);
506 if (!lp_config_has_section(config,item)){
510 tmp=lp_config_get_string(config,item,"url",NULL);
514 lf=linphone_friend_new_with_addr(tmp);
518 tmp=lp_config_get_string(config,item,"pol",NULL);
519 if (tmp==NULL) linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPWait);
521 linphone_friend_set_inc_subscribe_policy(lf,__policy_str_to_enum(tmp));
523 a=lp_config_get_int(config,item,"subscribe",0);
524 linphone_friend_send_subscribe(lf,a);
526 linphone_friend_set_ref_key(lf,lp_config_get_string(config,item,"refkey",NULL));
530 const char *__policy_enum_to_str(LinphoneSubscribePolicy pol){
532 case LinphoneSPAccept:
542 ms_warning("Invalid policy enum value.");
546 void linphone_friend_write_to_config_file(LpConfig *config, LinphoneFriend *lf, int index){
551 sprintf(key,"friend_%i",index);
554 lp_config_clean_section(config,key);
558 tmp=linphone_address_as_string(lf->uri);
562 lp_config_set_string(config,key,"url",tmp);
565 lp_config_set_string(config,key,"pol",__policy_enum_to_str(lf->pol));
566 lp_config_set_int(config,key,"subscribe",lf->subscribe);
568 refkey=linphone_friend_get_ref_key(lf);
570 lp_config_set_string(config,key,"refkey",refkey);
574 void linphone_core_write_friends_config(LinphoneCore* lc)
578 if (! linphone_core_ready(lc)) return; /*dont write config when reading it !*/
579 for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
580 linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
582 linphone_friend_write_to_config_file(lc->config,NULL,i); /* set the end */