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;
144 if (fr->lc->vtable.notify_recv)
145 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);
156 LinphoneFriend * linphone_friend_new(){
157 LinphoneFriend *obj=ms_new0(LinphoneFriend,1);
158 obj->pol=LinphoneSPAccept;
159 obj->status=LINPHONE_STATUS_OFFLINE;
164 LinphoneFriend *linphone_friend_new_with_addr(const char *addr){
165 LinphoneFriend *fr=linphone_friend_new();
166 if (linphone_friend_set_sip_addr(fr,addr)<0){
167 linphone_friend_destroy(fr);
173 bool_t linphone_friend_in_list(const LinphoneFriend *lf){
177 void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char **result){
178 LinphoneAddress *fr=NULL;
180 fr=linphone_address_new(uri);
183 if (strchr(uri,'@')!=NULL){
186 tmp=ms_strdup_printf("sip:%s",uri);
187 u=linphone_address_new(tmp);
191 }else if (lc->default_proxy!=NULL){
192 /*try adding domain part from default current proxy*/
193 LinphoneAddress * id=linphone_address_new(linphone_core_get_identity(lc));
195 linphone_address_set_username(id,uri);
196 *result=linphone_address_as_string(id);
197 linphone_address_destroy(id);
202 ms_message("%s interpreted as %s",uri,*result);
204 ms_warning("Fail to interpret friend uri %s",uri);
206 }else *result=linphone_address_as_string(fr);
207 linphone_address_destroy(fr);
210 int linphone_friend_set_sip_addr(LinphoneFriend *lf, const char *addr){
211 LinphoneAddress *fr=linphone_address_new(addr);
213 ms_warning("Invalid friend sip uri: %s",addr);
216 linphone_address_clean(fr);
217 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
222 int linphone_friend_set_name(LinphoneFriend *lf, const char *name){
223 LinphoneAddress *fr=lf->uri;
225 ms_error("linphone_friend_set_sip_addr() must be called before linphone_friend_set_name().");
228 linphone_address_set_display_name(fr,name);
232 int linphone_friend_send_subscribe(LinphoneFriend *fr, bool_t val){
237 int linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol)
243 SalPresenceStatus linphone_online_status_to_sal(LinphoneOnlineStatus os){
245 case LINPHONE_STATUS_OFFLINE:
246 return SalPresenceOffline;
248 case LINPHONE_STATUS_ONLINE:
249 return SalPresenceOnline;
251 case LINPHONE_STATUS_BUSY:
252 return SalPresenceBusy;
254 case LINPHONE_STATUS_BERIGHTBACK:
255 return SalPresenceBerightback;
257 case LINPHONE_STATUS_AWAY:
258 return SalPresenceAway;
260 case LINPHONE_STATUS_ONTHEPHONE:
261 return SalPresenceOnthephone;
263 case LINPHONE_STATUS_OUTTOLUNCH:
264 return SalPresenceOuttolunch;
266 case LINPHONE_STATUS_NOT_DISTURB:
267 return SalPresenceDonotdisturb;
269 case LINPHONE_STATUS_MOVED:
270 return SalPresenceMoved;
272 case LINPHONE_STATUS_ALT_SERVICE:
273 return SalPresenceAltService;
275 case LINPHONE_STATUS_PENDING:
276 return SalPresenceOffline;
279 return SalPresenceOffline;
282 return SalPresenceOffline;
285 void linphone_friend_notify(LinphoneFriend *lf, LinphoneOnlineStatus os){
286 //printf("Wish to notify %p, lf->nid=%i\n",lf,lf->nid);
287 if (lf->insub!=NULL){
288 sal_notify_presence(lf->insub,linphone_online_status_to_sal(os),NULL);
292 static void linphone_friend_unsubscribe(LinphoneFriend *lf){
293 if (lf->outsub!=NULL) {
294 sal_unsubscribe(lf->outsub);
295 sal_op_release(lf->outsub);
300 void linphone_friend_destroy(LinphoneFriend *lf){
301 linphone_friend_notify(lf,LINPHONE_STATUS_OFFLINE);
302 linphone_friend_unsubscribe(lf);
304 sal_notify_close(lf->insub);
305 sal_op_release(lf->insub);
307 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
308 if (lf->info!=NULL) buddy_info_free(lf->info);
312 const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf){
316 bool_t linphone_friend_get_send_subscribe(const LinphoneFriend *lf){
317 return lf->subscribe;
320 LinphoneSubscribePolicy linphone_friend_get_inc_subscribe_policy(const LinphoneFriend *lf){
324 LinphoneOnlineStatus linphone_friend_get_status(const LinphoneFriend *lf){
328 BuddyInfo * linphone_friend_get_info(const LinphoneFriend *lf){
332 void linphone_friend_apply(LinphoneFriend *fr, LinphoneCore *lc){
334 ms_warning("No sip url defined.");
339 linphone_core_write_friends_config(lc);
341 if (fr->inc_subscribe_pending){
344 linphone_friend_notify(fr,LINPHONE_STATUS_PENDING);
346 case LinphoneSPAccept:
349 linphone_friend_notify(fr,fr->lc->presence_mode);
353 linphone_friend_notify(fr,LINPHONE_STATUS_OFFLINE);
356 fr->inc_subscribe_pending=FALSE;
358 if (fr->subscribe && fr->outsub==NULL){
360 __linphone_friend_do_subscribe(fr);
362 ms_message("linphone_friend_apply() done.");
366 void linphone_friend_edit(LinphoneFriend *fr){
369 void linphone_friend_done(LinphoneFriend *fr){
370 ms_return_if_fail(fr!=NULL);
371 if (fr->lc==NULL) return;
372 linphone_friend_apply(fr,fr->lc);
375 void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf)
377 ms_return_if_fail(lf->lc==NULL);
378 ms_return_if_fail(lf->uri!=NULL);
379 if (ms_list_find(lc->friends,lf)!=NULL){
381 const LinphoneAddress *addr=linphone_friend_get_address(lf);
382 if (addr) tmp=linphone_address_as_string(addr);
383 ms_warning("Friend %s already in list, ignored.", tmp ? tmp : "unknown");
384 if (tmp) ms_free(tmp);
387 lc->friends=ms_list_append(lc->friends,lf);
388 linphone_friend_apply(lf,lc);
392 void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend* fl){
393 MSList *el=ms_list_find(lc->friends,(void *)fl);
395 lc->friends=ms_list_remove_link(lc->friends,el);
396 linphone_friend_destroy((LinphoneFriend*)el->data);
397 linphone_core_write_friends_config(lc);
401 void linphone_friend_set_ref_key(LinphoneFriend *lf, const char *key){
402 if (lf->refkey!=NULL){
407 lf->refkey=ms_strdup(key);
409 linphone_core_write_friends_config(lf->lc);
412 const char *linphone_friend_get_ref_key(const LinphoneFriend *lf){
416 static bool_t username_match(const char *u1, const char *u2){
417 if (u1==NULL && u2==NULL) return TRUE;
418 if (u1 && u2 && strcasecmp(u1,u2)==0) return TRUE;
422 LinphoneFriend *linphone_core_get_friend_by_address(const LinphoneCore *lc, const char *uri){
423 LinphoneAddress *puri=linphone_address_new(uri);
425 const char *username=linphone_address_get_username(puri);
426 const char *domain=linphone_address_get_domain(puri);
427 LinphoneFriend *lf=NULL;
432 for(elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
433 lf=(LinphoneFriend*)elem->data;
434 const char *it_username=linphone_address_get_username(lf->uri);
435 const char *it_host=linphone_address_get_domain(lf->uri);;
436 if (strcasecmp(domain,it_host)==0 && username_match(username,it_username)){
441 linphone_address_destroy(puri);
445 LinphoneFriend *linphone_core_get_friend_by_ref_key(const LinphoneCore *lc, const char *key){
447 if (key==NULL) return NULL;
448 for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
449 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
450 if (lf->refkey!=NULL && strcmp(lf->refkey,key)==0){
457 #define key_compare(s1,s2) strcmp(s1,s2)
459 LinphoneSubscribePolicy __policy_str_to_enum(const char* pol){
460 if (key_compare("accept",pol)==0){
461 return LinphoneSPAccept;
463 if (key_compare("deny",pol)==0){
464 return LinphoneSPDeny;
466 if (key_compare("wait",pol)==0){
467 return LinphoneSPWait;
469 ms_warning("Unrecognized subscribe policy: %s",pol);
470 return LinphoneSPWait;
473 LinphoneProxyConfig *__index_to_proxy(LinphoneCore *lc, int index){
474 if (index>=0) return (LinphoneProxyConfig*)ms_list_nth_data(lc->sip_conf.proxies,index);
478 LinphoneFriend * linphone_friend_new_from_config_file(LinphoneCore *lc, int index){
483 LpConfig *config=lc->config;
485 sprintf(item,"friend_%i",index);
487 if (!lp_config_has_section(config,item)){
491 tmp=lp_config_get_string(config,item,"url",NULL);
495 lf=linphone_friend_new_with_addr(tmp);
499 tmp=lp_config_get_string(config,item,"pol",NULL);
500 if (tmp==NULL) linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPWait);
502 linphone_friend_set_inc_subscribe_policy(lf,__policy_str_to_enum(tmp));
504 a=lp_config_get_int(config,item,"subscribe",0);
505 linphone_friend_send_subscribe(lf,a);
507 linphone_friend_set_ref_key(lf,lp_config_get_string(config,item,"refkey",NULL));
511 const char *__policy_enum_to_str(LinphoneSubscribePolicy pol){
513 case LinphoneSPAccept:
523 ms_warning("Invalid policy enum value.");
527 void linphone_friend_write_to_config_file(LpConfig *config, LinphoneFriend *lf, int index){
532 sprintf(key,"friend_%i",index);
535 lp_config_clean_section(config,key);
539 tmp=linphone_address_as_string(lf->uri);
543 lp_config_set_string(config,key,"url",tmp);
546 lp_config_set_string(config,key,"pol",__policy_enum_to_str(lf->pol));
547 lp_config_set_int(config,key,"subscribe",lf->subscribe);
549 refkey=linphone_friend_get_ref_key(lf);
551 lp_config_set_string(config,key,"refkey",refkey);
555 void linphone_core_write_friends_config(LinphoneCore* lc)
559 if (!lc->ready) return; /*dont write config when reading it !*/
560 for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
561 linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
563 linphone_friend_write_to_config_file(lc->config,NULL,i); /* set the end */