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 LINPHONE_STATUS_ONLINE:
35 case LINPHONE_STATUS_BUSY:
38 case LINPHONE_STATUS_BERIGHTBACK:
39 str=_("Be right back");
41 case LINPHONE_STATUS_AWAY:
44 case LINPHONE_STATUS_ONTHEPHONE:
45 str=_("On the phone");
47 case LINPHONE_STATUS_OUTTOLUNCH:
48 str=_("Out to lunch");
50 case LINPHONE_STATUS_NOT_DISTURB:
51 str=_("Do not disturb");
53 case LINPHONE_STATUS_MOVED:
56 case LINPHONE_STATUS_ALT_SERVICE:
57 str=_("Using another messaging service");
59 case LINPHONE_STATUS_OFFLINE:
62 case LINPHONE_STATUS_PENDING:
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 LinphoneProxyConfig *cfg;
133 friend=linphone_address_as_string(fr->uri);
134 cfg=linphone_core_lookup_known_proxy(fr->lc,linphone_friend_get_address(fr));
136 route=linphone_proxy_config_get_route(cfg);
137 from=linphone_proxy_config_get_identity(cfg);
138 }else from=linphone_core_get_primary_contact(fr->lc);
139 if (fr->outsub==NULL){
140 /* people for which we don't have yet an answer should appear as offline */
141 fr->status=LINPHONE_STATUS_OFFLINE;
143 if (fr->lc->vtable.notify_recv)
144 fr->lc->vtable.notify_recv(fr->lc,(LinphoneFriend*)fr);
147 sal_op_release(fr->outsub);
150 fr->outsub=sal_op_new(fr->lc->sal);
151 sal_op_set_route(fr->outsub,route);
152 sal_subscribe_presence(fr->outsub,from,friend);
153 fr->subscribe_active=TRUE;
157 LinphoneFriend * linphone_friend_new(){
158 LinphoneFriend *obj=ms_new0(LinphoneFriend,1);
159 obj->pol=LinphoneSPAccept;
160 obj->status=LINPHONE_STATUS_OFFLINE;
165 LinphoneFriend *linphone_friend_new_with_addr(const char *addr){
166 LinphoneFriend *fr=linphone_friend_new();
167 if (linphone_friend_set_sip_addr(fr,addr)<0){
168 linphone_friend_destroy(fr);
174 bool_t linphone_friend_in_list(const LinphoneFriend *lf){
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 linphone_address_clean(fr);
218 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
223 int linphone_friend_set_name(LinphoneFriend *lf, const char *name){
224 LinphoneAddress *fr=lf->uri;
226 ms_error("linphone_friend_set_sip_addr() must be called before linphone_friend_set_name().");
229 linphone_address_set_display_name(fr,name);
233 int linphone_friend_send_subscribe(LinphoneFriend *fr, bool_t val){
238 int linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol)
244 SalPresenceStatus linphone_online_status_to_sal(LinphoneOnlineStatus os){
246 case LINPHONE_STATUS_OFFLINE:
247 return SalPresenceOffline;
249 case LINPHONE_STATUS_ONLINE:
250 return SalPresenceOnline;
252 case LINPHONE_STATUS_BUSY:
253 return SalPresenceBusy;
255 case LINPHONE_STATUS_BERIGHTBACK:
256 return SalPresenceBerightback;
258 case LINPHONE_STATUS_AWAY:
259 return SalPresenceAway;
261 case LINPHONE_STATUS_ONTHEPHONE:
262 return SalPresenceOnthephone;
264 case LINPHONE_STATUS_OUTTOLUNCH:
265 return SalPresenceOuttolunch;
267 case LINPHONE_STATUS_NOT_DISTURB:
268 return SalPresenceDonotdisturb;
270 case LINPHONE_STATUS_MOVED:
271 return SalPresenceMoved;
273 case LINPHONE_STATUS_ALT_SERVICE:
274 return SalPresenceAltService;
276 case LINPHONE_STATUS_PENDING:
277 return SalPresenceOffline;
280 return SalPresenceOffline;
283 return SalPresenceOffline;
286 void linphone_friend_notify(LinphoneFriend *lf, LinphoneOnlineStatus os){
287 //printf("Wish to notify %p, lf->nid=%i\n",lf,lf->nid);
288 if (lf->insub!=NULL){
289 sal_notify_presence(lf->insub,linphone_online_status_to_sal(os),NULL);
293 static void linphone_friend_unsubscribe(LinphoneFriend *lf){
294 if (lf->outsub!=NULL) {
295 sal_unsubscribe(lf->outsub);
296 sal_op_release(lf->outsub);
298 lf->subscribe_active=FALSE;
302 void linphone_friend_close_subscriptions(LinphoneFriend *lf){
303 linphone_friend_unsubscribe(lf);
305 sal_notify_close(lf->insub);
306 sal_op_release(lf->insub);
311 void linphone_friend_destroy(LinphoneFriend *lf){
313 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
314 if (lf->info!=NULL) buddy_info_free(lf->info);
318 const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf){
322 bool_t linphone_friend_get_send_subscribe(const LinphoneFriend *lf){
323 return lf->subscribe;
326 LinphoneSubscribePolicy linphone_friend_get_inc_subscribe_policy(const LinphoneFriend *lf){
330 LinphoneOnlineStatus linphone_friend_get_status(const LinphoneFriend *lf){
334 BuddyInfo * linphone_friend_get_info(const LinphoneFriend *lf){
338 void linphone_friend_apply(LinphoneFriend *fr, LinphoneCore *lc){
340 ms_warning("No sip url defined.");
345 linphone_core_write_friends_config(lc);
347 if (fr->inc_subscribe_pending){
350 linphone_friend_notify(fr,LINPHONE_STATUS_PENDING);
352 case LinphoneSPAccept:
355 linphone_friend_notify(fr,fr->lc->presence_mode);
359 linphone_friend_notify(fr,LINPHONE_STATUS_OFFLINE);
362 fr->inc_subscribe_pending=FALSE;
364 if (fr->subscribe && fr->subscribe_active==FALSE){
365 ms_message("Sending a new SUBSCRIBE");
366 __linphone_friend_do_subscribe(fr);
368 ms_message("linphone_friend_apply() done.");
372 void linphone_friend_edit(LinphoneFriend *fr){
375 void linphone_friend_done(LinphoneFriend *fr){
376 ms_return_if_fail(fr!=NULL);
377 if (fr->lc==NULL) return;
378 linphone_friend_apply(fr,fr->lc);
381 void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf)
383 ms_return_if_fail(lf->lc==NULL);
384 ms_return_if_fail(lf->uri!=NULL);
385 if (ms_list_find(lc->friends,lf)!=NULL){
387 const LinphoneAddress *addr=linphone_friend_get_address(lf);
388 if (addr) tmp=linphone_address_as_string(addr);
389 ms_warning("Friend %s already in list, ignored.", tmp ? tmp : "unknown");
390 if (tmp) ms_free(tmp);
393 lc->friends=ms_list_append(lc->friends,lf);
394 linphone_friend_apply(lf,lc);
398 void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend* fl){
399 MSList *el=ms_list_find(lc->friends,(void *)fl);
401 lc->friends=ms_list_remove_link(lc->friends,el);
402 linphone_friend_destroy((LinphoneFriend*)el->data);
403 linphone_core_write_friends_config(lc);
407 void linphone_friend_set_ref_key(LinphoneFriend *lf, const char *key){
408 if (lf->refkey!=NULL){
413 lf->refkey=ms_strdup(key);
415 linphone_core_write_friends_config(lf->lc);
418 const char *linphone_friend_get_ref_key(const LinphoneFriend *lf){
422 static bool_t username_match(const char *u1, const char *u2){
423 if (u1==NULL && u2==NULL) return TRUE;
424 if (u1 && u2 && strcasecmp(u1,u2)==0) return TRUE;
428 LinphoneFriend *linphone_core_get_friend_by_address(const LinphoneCore *lc, const char *uri){
429 LinphoneAddress *puri=linphone_address_new(uri);
431 const char *username=linphone_address_get_username(puri);
432 const char *domain=linphone_address_get_domain(puri);
433 LinphoneFriend *lf=NULL;
438 for(elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
439 lf=(LinphoneFriend*)elem->data;
440 const char *it_username=linphone_address_get_username(lf->uri);
441 const char *it_host=linphone_address_get_domain(lf->uri);;
442 if (strcasecmp(domain,it_host)==0 && username_match(username,it_username)){
447 linphone_address_destroy(puri);
451 LinphoneFriend *linphone_core_get_friend_by_ref_key(const LinphoneCore *lc, const char *key){
453 if (key==NULL) return NULL;
454 for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
455 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
456 if (lf->refkey!=NULL && strcmp(lf->refkey,key)==0){
463 #define key_compare(s1,s2) strcmp(s1,s2)
465 LinphoneSubscribePolicy __policy_str_to_enum(const char* pol){
466 if (key_compare("accept",pol)==0){
467 return LinphoneSPAccept;
469 if (key_compare("deny",pol)==0){
470 return LinphoneSPDeny;
472 if (key_compare("wait",pol)==0){
473 return LinphoneSPWait;
475 ms_warning("Unrecognized subscribe policy: %s",pol);
476 return LinphoneSPWait;
479 LinphoneProxyConfig *__index_to_proxy(LinphoneCore *lc, int index){
480 if (index>=0) return (LinphoneProxyConfig*)ms_list_nth_data(lc->sip_conf.proxies,index);
484 LinphoneFriend * linphone_friend_new_from_config_file(LinphoneCore *lc, int index){
489 LpConfig *config=lc->config;
491 sprintf(item,"friend_%i",index);
493 if (!lp_config_has_section(config,item)){
497 tmp=lp_config_get_string(config,item,"url",NULL);
501 lf=linphone_friend_new_with_addr(tmp);
505 tmp=lp_config_get_string(config,item,"pol",NULL);
506 if (tmp==NULL) linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPWait);
508 linphone_friend_set_inc_subscribe_policy(lf,__policy_str_to_enum(tmp));
510 a=lp_config_get_int(config,item,"subscribe",0);
511 linphone_friend_send_subscribe(lf,a);
513 linphone_friend_set_ref_key(lf,lp_config_get_string(config,item,"refkey",NULL));
517 const char *__policy_enum_to_str(LinphoneSubscribePolicy pol){
519 case LinphoneSPAccept:
529 ms_warning("Invalid policy enum value.");
533 void linphone_friend_write_to_config_file(LpConfig *config, LinphoneFriend *lf, int index){
538 sprintf(key,"friend_%i",index);
541 lp_config_clean_section(config,key);
545 tmp=linphone_address_as_string(lf->uri);
549 lp_config_set_string(config,key,"url",tmp);
552 lp_config_set_string(config,key,"pol",__policy_enum_to_str(lf->pol));
553 lp_config_set_int(config,key,"subscribe",lf->subscribe);
555 refkey=linphone_friend_get_ref_key(lf);
557 lp_config_set_string(config,key,"refkey",refkey);
561 void linphone_core_write_friends_config(LinphoneCore* lc)
565 if (!lc->ready) return; /*dont write config when reading it !*/
566 for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
567 linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
569 linphone_friend_write_to_config_file(lc->config,NULL,i); /* set the end */