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_ONLINE:
37 case LINPHONE_STATUS_BUSY:
40 case LINPHONE_STATUS_BERIGHTBACK:
41 str=_("Be right back");
43 case LINPHONE_STATUS_AWAY:
46 case LINPHONE_STATUS_ONTHEPHONE:
47 str=_("On the phone");
49 case LINPHONE_STATUS_OUTTOLUNCH:
50 str=_("Out to lunch");
52 case LINPHONE_STATUS_NOT_DISTURB:
53 str=_("Do not disturb");
55 case LINPHONE_STATUS_MOVED:
58 case LINPHONE_STATUS_ALT_SERVICE:
59 str=_("Using another messaging service");
61 case LINPHONE_STATUS_OFFLINE:
64 case LINPHONE_STATUS_PENDING:
73 static int friend_data_compare(const void * a, const void * b, void * data){
74 LinphoneAddress *fa=((LinphoneFriend*)a)->uri;
75 LinphoneAddress *fb=((LinphoneFriend*)b)->uri;
77 ua=linphone_address_get_username(fa);
78 ub=linphone_address_get_username(fb);
79 if (ua!=NULL && ub!=NULL) {
80 //printf("Comparing usernames %s,%s\n",ua,ub);
81 return strcasecmp(ua,ub);
85 ua=linphone_address_get_domain(fa);
86 ub=linphone_address_get_domain(fb);
87 if (ua!=NULL && ub!=NULL){
88 int ret=strcasecmp(ua,ub);
89 //printf("Comparing hostnames %s,%s,res=%i\n",ua,ub,ret);
96 static int friend_compare(const void * a, const void * b){
97 return friend_data_compare(a,b,NULL);
101 MSList *linphone_find_friend(MSList *fl, const LinphoneAddress *friend, LinphoneFriend **lf){
103 LinphoneFriend dummy;
104 if (lf!=NULL) *lf=NULL;
105 dummy.uri=(LinphoneAddress*)friend;
106 res=ms_list_find_custom(fl,friend_compare,&dummy);
107 if (lf!=NULL && res!=NULL) *lf=(LinphoneFriend*)res->data;
111 LinphoneFriend *linphone_find_friend_by_inc_subscribe(MSList *l, SalOp *op){
113 for (elem=l;elem!=NULL;elem=elem->next){
114 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
115 if (lf->insub==op) return lf;
120 LinphoneFriend *linphone_find_friend_by_out_subscribe(MSList *l, SalOp *op){
122 for (elem=l;elem!=NULL;elem=elem->next){
123 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
124 if (lf->outsub==op) return lf;
129 void __linphone_friend_do_subscribe(LinphoneFriend *fr){
131 const char *route=NULL;
132 const char *from=NULL;
133 LinphoneProxyConfig *cfg;
135 friend=linphone_address_as_string(fr->uri);
136 cfg=linphone_core_lookup_known_proxy(fr->lc,linphone_friend_get_address(fr));
138 route=linphone_proxy_config_get_route(cfg);
139 from=linphone_proxy_config_get_identity(cfg);
140 }else from=linphone_core_get_primary_contact(fr->lc);
141 if (fr->outsub==NULL){
142 /* people for which we don't have yet an answer should appear as offline */
143 fr->status=LINPHONE_STATUS_OFFLINE;
145 if (fr->lc->vtable.notify_recv)
146 fr->lc->vtable.notify_recv(fr->lc,(LinphoneFriend*)fr);
149 sal_op_release(fr->outsub);
152 fr->outsub=sal_op_new(fr->lc->sal);
153 sal_op_set_route(fr->outsub,route);
154 sal_subscribe_presence(fr->outsub,from,friend);
158 LinphoneFriend * linphone_friend_new(){
159 LinphoneFriend *obj=ms_new0(LinphoneFriend,1);
160 obj->pol=LinphoneSPAccept;
161 obj->status=LINPHONE_STATUS_OFFLINE;
166 LinphoneFriend *linphone_friend_new_with_addr(const char *addr){
167 LinphoneFriend *fr=linphone_friend_new();
168 if (linphone_friend_set_sip_addr(fr,addr)<0){
169 linphone_friend_destroy(fr);
175 bool_t linphone_friend_in_list(const LinphoneFriend *lf){
179 void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char **result){
180 LinphoneAddress *fr=NULL;
182 fr=linphone_address_new(uri);
185 if (strchr(uri,'@')!=NULL){
188 tmp=ms_strdup_printf("sip:%s",uri);
189 u=linphone_address_new(tmp);
193 }else if (lc->default_proxy!=NULL){
194 /*try adding domain part from default current proxy*/
195 LinphoneAddress * id=linphone_address_new(linphone_core_get_identity(lc));
197 linphone_address_set_username(id,uri);
198 *result=linphone_address_as_string(id);
199 linphone_address_destroy(id);
204 ms_message("%s interpreted as %s",uri,*result);
206 ms_warning("Fail to interpret friend uri %s",uri);
208 }else *result=linphone_address_as_string(fr);
209 linphone_address_destroy(fr);
212 int linphone_friend_set_sip_addr(LinphoneFriend *lf, const char *addr){
213 LinphoneAddress *fr=linphone_address_new(addr);
215 ms_warning("Invalid friend sip uri: %s",addr);
218 linphone_address_clean(fr);
219 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
224 int linphone_friend_set_name(LinphoneFriend *lf, const char *name){
225 LinphoneAddress *fr=lf->uri;
227 ms_error("linphone_friend_set_sip_addr() must be called before linphone_friend_set_name().");
230 linphone_address_set_display_name(fr,name);
234 int linphone_friend_send_subscribe(LinphoneFriend *fr, bool_t val){
239 int linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol)
245 SalPresenceStatus linphone_online_status_to_sal(LinphoneOnlineStatus os){
247 case LINPHONE_STATUS_OFFLINE:
248 return SalPresenceOffline;
250 case LINPHONE_STATUS_ONLINE:
251 return SalPresenceOnline;
253 case LINPHONE_STATUS_BUSY:
254 return SalPresenceBusy;
256 case LINPHONE_STATUS_BERIGHTBACK:
257 return SalPresenceBerightback;
259 case LINPHONE_STATUS_AWAY:
260 return SalPresenceAway;
262 case LINPHONE_STATUS_ONTHEPHONE:
263 return SalPresenceOnthephone;
265 case LINPHONE_STATUS_OUTTOLUNCH:
266 return SalPresenceOuttolunch;
268 case LINPHONE_STATUS_NOT_DISTURB:
269 return SalPresenceDonotdisturb;
271 case LINPHONE_STATUS_MOVED:
272 return SalPresenceMoved;
274 case LINPHONE_STATUS_ALT_SERVICE:
275 return SalPresenceAltService;
277 case LINPHONE_STATUS_PENDING:
278 return SalPresenceOffline;
281 return SalPresenceOffline;
284 return SalPresenceOffline;
287 void linphone_friend_notify(LinphoneFriend *lf, LinphoneOnlineStatus os){
288 //printf("Wish to notify %p, lf->nid=%i\n",lf,lf->nid);
289 if (lf->insub!=NULL){
290 sal_notify_presence(lf->insub,linphone_online_status_to_sal(os),NULL);
294 static void linphone_friend_unsubscribe(LinphoneFriend *lf){
295 if (lf->outsub!=NULL) {
296 sal_unsubscribe(lf->outsub);
297 sal_op_release(lf->outsub);
302 void linphone_friend_close_subscriptions(LinphoneFriend *lf){
303 linphone_friend_notify(lf,LINPHONE_STATUS_OFFLINE);
304 linphone_friend_unsubscribe(lf);
306 sal_notify_close(lf->insub);
307 sal_op_release(lf->insub);
312 void linphone_friend_destroy(LinphoneFriend *lf){
314 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
315 if (lf->info!=NULL) buddy_info_free(lf->info);
319 const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf){
323 bool_t linphone_friend_get_send_subscribe(const LinphoneFriend *lf){
324 return lf->subscribe;
327 LinphoneSubscribePolicy linphone_friend_get_inc_subscribe_policy(const LinphoneFriend *lf){
331 LinphoneOnlineStatus linphone_friend_get_status(const LinphoneFriend *lf){
335 BuddyInfo * linphone_friend_get_info(const LinphoneFriend *lf){
339 void linphone_friend_apply(LinphoneFriend *fr, LinphoneCore *lc){
341 ms_warning("No sip url defined.");
346 linphone_core_write_friends_config(lc);
348 if (fr->inc_subscribe_pending){
351 linphone_friend_notify(fr,LINPHONE_STATUS_PENDING);
353 case LinphoneSPAccept:
356 linphone_friend_notify(fr,fr->lc->presence_mode);
360 linphone_friend_notify(fr,LINPHONE_STATUS_OFFLINE);
363 fr->inc_subscribe_pending=FALSE;
365 if (fr->subscribe && fr->outsub==NULL){
367 __linphone_friend_do_subscribe(fr);
369 ms_message("linphone_friend_apply() done.");
373 void linphone_friend_edit(LinphoneFriend *fr){
376 void linphone_friend_done(LinphoneFriend *fr){
377 ms_return_if_fail(fr!=NULL);
378 if (fr->lc==NULL) return;
379 linphone_friend_apply(fr,fr->lc);
382 void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf)
384 ms_return_if_fail(lf->lc==NULL);
385 ms_return_if_fail(lf->uri!=NULL);
386 if (ms_list_find(lc->friends,lf)!=NULL){
388 const LinphoneAddress *addr=linphone_friend_get_address(lf);
389 if (addr) tmp=linphone_address_as_string(addr);
390 ms_warning("Friend %s already in list, ignored.", tmp ? tmp : "unknown");
391 if (tmp) ms_free(tmp);
394 lc->friends=ms_list_append(lc->friends,lf);
395 linphone_friend_apply(lf,lc);
399 void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend* fl){
400 MSList *el=ms_list_find(lc->friends,(void *)fl);
402 lc->friends=ms_list_remove_link(lc->friends,el);
403 linphone_friend_destroy((LinphoneFriend*)el->data);
404 linphone_core_write_friends_config(lc);
408 void linphone_friend_set_ref_key(LinphoneFriend *lf, const char *key){
409 if (lf->refkey!=NULL){
414 lf->refkey=ms_strdup(key);
416 linphone_core_write_friends_config(lf->lc);
419 const char *linphone_friend_get_ref_key(const LinphoneFriend *lf){
423 static bool_t username_match(const char *u1, const char *u2){
424 if (u1==NULL && u2==NULL) return TRUE;
425 if (u1 && u2 && strcasecmp(u1,u2)==0) return TRUE;
429 LinphoneFriend *linphone_core_get_friend_by_address(const LinphoneCore *lc, const char *uri){
430 LinphoneAddress *puri=linphone_address_new(uri);
432 const char *username=linphone_address_get_username(puri);
433 const char *domain=linphone_address_get_domain(puri);
434 LinphoneFriend *lf=NULL;
439 for(elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
440 lf=(LinphoneFriend*)elem->data;
441 const char *it_username=linphone_address_get_username(lf->uri);
442 const char *it_host=linphone_address_get_domain(lf->uri);;
443 if (strcasecmp(domain,it_host)==0 && username_match(username,it_username)){
448 linphone_address_destroy(puri);
452 LinphoneFriend *linphone_core_get_friend_by_ref_key(const LinphoneCore *lc, const char *key){
454 if (key==NULL) return NULL;
455 for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
456 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
457 if (lf->refkey!=NULL && strcmp(lf->refkey,key)==0){
464 #define key_compare(s1,s2) strcmp(s1,s2)
466 LinphoneSubscribePolicy __policy_str_to_enum(const char* pol){
467 if (key_compare("accept",pol)==0){
468 return LinphoneSPAccept;
470 if (key_compare("deny",pol)==0){
471 return LinphoneSPDeny;
473 if (key_compare("wait",pol)==0){
474 return LinphoneSPWait;
476 ms_warning("Unrecognized subscribe policy: %s",pol);
477 return LinphoneSPWait;
480 LinphoneProxyConfig *__index_to_proxy(LinphoneCore *lc, int index){
481 if (index>=0) return (LinphoneProxyConfig*)ms_list_nth_data(lc->sip_conf.proxies,index);
485 LinphoneFriend * linphone_friend_new_from_config_file(LinphoneCore *lc, int index){
490 LpConfig *config=lc->config;
492 sprintf(item,"friend_%i",index);
494 if (!lp_config_has_section(config,item)){
498 tmp=lp_config_get_string(config,item,"url",NULL);
502 lf=linphone_friend_new_with_addr(tmp);
506 tmp=lp_config_get_string(config,item,"pol",NULL);
507 if (tmp==NULL) linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPWait);
509 linphone_friend_set_inc_subscribe_policy(lf,__policy_str_to_enum(tmp));
511 a=lp_config_get_int(config,item,"subscribe",0);
512 linphone_friend_send_subscribe(lf,a);
514 linphone_friend_set_ref_key(lf,lp_config_get_string(config,item,"refkey",NULL));
518 const char *__policy_enum_to_str(LinphoneSubscribePolicy pol){
520 case LinphoneSPAccept:
530 ms_warning("Invalid policy enum value.");
534 void linphone_friend_write_to_config_file(LpConfig *config, LinphoneFriend *lf, int index){
539 sprintf(key,"friend_%i",index);
542 lp_config_clean_section(config,key);
546 tmp=linphone_address_as_string(lf->uri);
550 lp_config_set_string(config,key,"url",tmp);
553 lp_config_set_string(config,key,"pol",__policy_enum_to_str(lf->pol));
554 lp_config_set_int(config,key,"subscribe",lf->subscribe);
556 refkey=linphone_friend_get_ref_key(lf);
558 lp_config_set_string(config,key,"refkey",refkey);
562 void linphone_core_write_friends_config(LinphoneCore* lc)
566 if (!lc->ready) return; /*dont write config when reading it !*/
567 for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
568 linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
570 linphone_friend_write_to_config_file(lc->config,NULL,i); /* set the end */