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);
155 fr->subscribe_active=TRUE;
159 LinphoneFriend * linphone_friend_new(){
160 LinphoneFriend *obj=ms_new0(LinphoneFriend,1);
161 obj->pol=LinphoneSPAccept;
162 obj->status=LINPHONE_STATUS_OFFLINE;
167 LinphoneFriend *linphone_friend_new_with_addr(const char *addr){
168 LinphoneFriend *fr=linphone_friend_new();
169 if (linphone_friend_set_sip_addr(fr,addr)<0){
170 linphone_friend_destroy(fr);
176 bool_t linphone_friend_in_list(const LinphoneFriend *lf){
180 void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char **result){
181 LinphoneAddress *fr=NULL;
183 fr=linphone_address_new(uri);
186 if (strchr(uri,'@')!=NULL){
189 tmp=ms_strdup_printf("sip:%s",uri);
190 u=linphone_address_new(tmp);
194 }else if (lc->default_proxy!=NULL){
195 /*try adding domain part from default current proxy*/
196 LinphoneAddress * id=linphone_address_new(linphone_core_get_identity(lc));
198 linphone_address_set_username(id,uri);
199 *result=linphone_address_as_string(id);
200 linphone_address_destroy(id);
205 ms_message("%s interpreted as %s",uri,*result);
207 ms_warning("Fail to interpret friend uri %s",uri);
209 }else *result=linphone_address_as_string(fr);
210 linphone_address_destroy(fr);
213 int linphone_friend_set_sip_addr(LinphoneFriend *lf, const char *addr){
214 LinphoneAddress *fr=linphone_address_new(addr);
216 ms_warning("Invalid friend sip uri: %s",addr);
219 linphone_address_clean(fr);
220 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
225 int linphone_friend_set_name(LinphoneFriend *lf, const char *name){
226 LinphoneAddress *fr=lf->uri;
228 ms_error("linphone_friend_set_sip_addr() must be called before linphone_friend_set_name().");
231 linphone_address_set_display_name(fr,name);
235 int linphone_friend_send_subscribe(LinphoneFriend *fr, bool_t val){
240 int linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol)
246 SalPresenceStatus linphone_online_status_to_sal(LinphoneOnlineStatus os){
248 case LINPHONE_STATUS_OFFLINE:
249 return SalPresenceOffline;
251 case LINPHONE_STATUS_ONLINE:
252 return SalPresenceOnline;
254 case LINPHONE_STATUS_BUSY:
255 return SalPresenceBusy;
257 case LINPHONE_STATUS_BERIGHTBACK:
258 return SalPresenceBerightback;
260 case LINPHONE_STATUS_AWAY:
261 return SalPresenceAway;
263 case LINPHONE_STATUS_ONTHEPHONE:
264 return SalPresenceOnthephone;
266 case LINPHONE_STATUS_OUTTOLUNCH:
267 return SalPresenceOuttolunch;
269 case LINPHONE_STATUS_NOT_DISTURB:
270 return SalPresenceDonotdisturb;
272 case LINPHONE_STATUS_MOVED:
273 return SalPresenceMoved;
275 case LINPHONE_STATUS_ALT_SERVICE:
276 return SalPresenceAltService;
278 case LINPHONE_STATUS_PENDING:
279 return SalPresenceOffline;
282 return SalPresenceOffline;
285 return SalPresenceOffline;
288 void linphone_friend_notify(LinphoneFriend *lf, LinphoneOnlineStatus os){
289 //printf("Wish to notify %p, lf->nid=%i\n",lf,lf->nid);
290 if (lf->insub!=NULL){
291 sal_notify_presence(lf->insub,linphone_online_status_to_sal(os),NULL);
295 static void linphone_friend_unsubscribe(LinphoneFriend *lf){
296 if (lf->outsub!=NULL) {
297 sal_unsubscribe(lf->outsub);
298 sal_op_release(lf->outsub);
300 lf->subscribe_active=FALSE;
304 void linphone_friend_close_subscriptions(LinphoneFriend *lf){
305 linphone_friend_unsubscribe(lf);
307 sal_notify_close(lf->insub);
308 sal_op_release(lf->insub);
313 void linphone_friend_destroy(LinphoneFriend *lf){
315 if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
316 if (lf->info!=NULL) buddy_info_free(lf->info);
320 const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf){
324 bool_t linphone_friend_get_send_subscribe(const LinphoneFriend *lf){
325 return lf->subscribe;
328 LinphoneSubscribePolicy linphone_friend_get_inc_subscribe_policy(const LinphoneFriend *lf){
332 LinphoneOnlineStatus linphone_friend_get_status(const LinphoneFriend *lf){
336 BuddyInfo * linphone_friend_get_info(const LinphoneFriend *lf){
340 void linphone_friend_apply(LinphoneFriend *fr, LinphoneCore *lc){
342 ms_warning("No sip url defined.");
347 linphone_core_write_friends_config(lc);
349 if (fr->inc_subscribe_pending){
352 linphone_friend_notify(fr,LINPHONE_STATUS_PENDING);
354 case LinphoneSPAccept:
357 linphone_friend_notify(fr,fr->lc->presence_mode);
361 linphone_friend_notify(fr,LINPHONE_STATUS_OFFLINE);
364 fr->inc_subscribe_pending=FALSE;
366 if (fr->subscribe && fr->subscribe_active==FALSE){
367 ms_message("Sending a new SUBSCRIBE");
368 __linphone_friend_do_subscribe(fr);
370 ms_message("linphone_friend_apply() done.");
374 void linphone_friend_edit(LinphoneFriend *fr){
377 void linphone_friend_done(LinphoneFriend *fr){
378 ms_return_if_fail(fr!=NULL);
379 if (fr->lc==NULL) return;
380 linphone_friend_apply(fr,fr->lc);
383 void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf)
385 ms_return_if_fail(lf->lc==NULL);
386 ms_return_if_fail(lf->uri!=NULL);
387 if (ms_list_find(lc->friends,lf)!=NULL){
389 const LinphoneAddress *addr=linphone_friend_get_address(lf);
390 if (addr) tmp=linphone_address_as_string(addr);
391 ms_warning("Friend %s already in list, ignored.", tmp ? tmp : "unknown");
392 if (tmp) ms_free(tmp);
395 lc->friends=ms_list_append(lc->friends,lf);
396 linphone_friend_apply(lf,lc);
400 void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend* fl){
401 MSList *el=ms_list_find(lc->friends,(void *)fl);
403 lc->friends=ms_list_remove_link(lc->friends,el);
404 linphone_friend_destroy((LinphoneFriend*)el->data);
405 linphone_core_write_friends_config(lc);
409 void linphone_friend_set_ref_key(LinphoneFriend *lf, const char *key){
410 if (lf->refkey!=NULL){
415 lf->refkey=ms_strdup(key);
417 linphone_core_write_friends_config(lf->lc);
420 const char *linphone_friend_get_ref_key(const LinphoneFriend *lf){
424 static bool_t username_match(const char *u1, const char *u2){
425 if (u1==NULL && u2==NULL) return TRUE;
426 if (u1 && u2 && strcasecmp(u1,u2)==0) return TRUE;
430 LinphoneFriend *linphone_core_get_friend_by_address(const LinphoneCore *lc, const char *uri){
431 LinphoneAddress *puri=linphone_address_new(uri);
433 const char *username=linphone_address_get_username(puri);
434 const char *domain=linphone_address_get_domain(puri);
435 LinphoneFriend *lf=NULL;
440 for(elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
441 lf=(LinphoneFriend*)elem->data;
442 const char *it_username=linphone_address_get_username(lf->uri);
443 const char *it_host=linphone_address_get_domain(lf->uri);;
444 if (strcasecmp(domain,it_host)==0 && username_match(username,it_username)){
449 linphone_address_destroy(puri);
453 LinphoneFriend *linphone_core_get_friend_by_ref_key(const LinphoneCore *lc, const char *key){
455 if (key==NULL) return NULL;
456 for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
457 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
458 if (lf->refkey!=NULL && strcmp(lf->refkey,key)==0){
465 #define key_compare(s1,s2) strcmp(s1,s2)
467 LinphoneSubscribePolicy __policy_str_to_enum(const char* pol){
468 if (key_compare("accept",pol)==0){
469 return LinphoneSPAccept;
471 if (key_compare("deny",pol)==0){
472 return LinphoneSPDeny;
474 if (key_compare("wait",pol)==0){
475 return LinphoneSPWait;
477 ms_warning("Unrecognized subscribe policy: %s",pol);
478 return LinphoneSPWait;
481 LinphoneProxyConfig *__index_to_proxy(LinphoneCore *lc, int index){
482 if (index>=0) return (LinphoneProxyConfig*)ms_list_nth_data(lc->sip_conf.proxies,index);
486 LinphoneFriend * linphone_friend_new_from_config_file(LinphoneCore *lc, int index){
491 LpConfig *config=lc->config;
493 sprintf(item,"friend_%i",index);
495 if (!lp_config_has_section(config,item)){
499 tmp=lp_config_get_string(config,item,"url",NULL);
503 lf=linphone_friend_new_with_addr(tmp);
507 tmp=lp_config_get_string(config,item,"pol",NULL);
508 if (tmp==NULL) linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPWait);
510 linphone_friend_set_inc_subscribe_policy(lf,__policy_str_to_enum(tmp));
512 a=lp_config_get_int(config,item,"subscribe",0);
513 linphone_friend_send_subscribe(lf,a);
515 linphone_friend_set_ref_key(lf,lp_config_get_string(config,item,"refkey",NULL));
519 const char *__policy_enum_to_str(LinphoneSubscribePolicy pol){
521 case LinphoneSPAccept:
531 ms_warning("Invalid policy enum value.");
535 void linphone_friend_write_to_config_file(LpConfig *config, LinphoneFriend *lf, int index){
540 sprintf(key,"friend_%i",index);
543 lp_config_clean_section(config,key);
547 tmp=linphone_address_as_string(lf->uri);
551 lp_config_set_string(config,key,"url",tmp);
554 lp_config_set_string(config,key,"pol",__policy_enum_to_str(lf->pol));
555 lp_config_set_int(config,key,"subscribe",lf->subscribe);
557 refkey=linphone_friend_get_ref_key(lf);
559 lp_config_set_string(config,key,"refkey",refkey);
563 void linphone_core_write_friends_config(LinphoneCore* lc)
567 if (!lc->ready) return; /*dont write config when reading it !*/
568 for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
569 linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
571 linphone_friend_write_to_config_file(lc->config,NULL,i); /* set the end */