]> sjero.net Git - linphone/blob - coreapi/friend.c
fix for fried api dicumentation
[linphone] / coreapi / friend.c
1 /***************************************************************************
2  *            friend.c
3  *
4  *  Sat May 15 15:25:16 2004
5  *  Copyright  2004-2009  Simon Morlat
6  *  Email
7  ****************************************************************************/
8
9 /*
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.
14  *
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.
19  *
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.
23  */
24
25 #include "linphonecore.h"
26 #include "private.h"
27 #include "lpconfig.h"
28
29 const char *linphone_online_status_to_string(LinphoneOnlineStatus ss){
30         const char *str=NULL;
31         switch(ss){
32                 case LinphoneStatusOnline:
33                 str=_("Online");
34                 break;
35                 case LinphoneStatusBusy:
36                 str=_("Busy");
37                 break;
38                 case LinphoneStatusBeRightBack:
39                 str=_("Be right back");
40                 break;
41                 case LinphoneStatusAway:
42                 str=_("Away");
43                 break;
44                 case LinphoneStatusOnThePhone:
45                 str=_("On the phone");
46                 break;
47                 case LinphoneStatusOutToLunch:
48                 str=_("Out to lunch");
49                 break;
50                 case LinphoneStatusDoNotDisturb:
51                 str=_("Do not disturb");
52                 break;
53                 case LinphoneStatusMoved:
54                 str=_("Moved");
55                 break;
56                 case LinphoneStatusAltService:
57                 str=_("Using another messaging service");
58                 break;
59                 case LinphoneStatusOffline:
60                 str=_("Offline");
61                 break;
62                 case LinphoneStatusPending:
63                 str=_("Pending");
64                 break;
65                 default:
66                 str=_("Unknown-bug");
67         }
68         return str;
69 }
70
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;
74         const char *ua,*ub;
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);
80         }
81         else {
82                 /* compare hosts*/
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);
88                         return ret;
89                 }
90                 else return -1;
91         }
92 }
93
94 static int friend_compare(const void * a, const void * b){
95         return friend_data_compare(a,b,NULL);
96 }
97
98
99 MSList *linphone_find_friend(MSList *fl, const LinphoneAddress *friend, LinphoneFriend **lf){
100         MSList *res=NULL;
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;
106         return res;
107 }
108
109 LinphoneFriend *linphone_find_friend_by_inc_subscribe(MSList *l, SalOp *op){
110         MSList *elem;
111         for (elem=l;elem!=NULL;elem=elem->next){
112                 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
113                 if (lf->insub==op) return lf;
114         }
115         return NULL;
116 }
117
118 LinphoneFriend *linphone_find_friend_by_out_subscribe(MSList *l, SalOp *op){
119         MSList *elem;
120         for (elem=l;elem!=NULL;elem=elem->next){
121                 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
122                 if (lf->outsub==op) return lf;
123         }
124         return NULL;
125 }
126
127 void __linphone_friend_do_subscribe(LinphoneFriend *fr){
128         char *friend=NULL;
129         const char *route=NULL;
130         const char *from=NULL;
131         const char *fixed_contact=NULL;
132         LinphoneProxyConfig *cfg;
133         
134         friend=linphone_address_as_string(fr->uri);
135         cfg=linphone_core_lookup_known_proxy(fr->lc,linphone_friend_get_address(fr));
136         if (cfg!=NULL){
137                 route=linphone_proxy_config_get_route(cfg);
138                 from=linphone_proxy_config_get_identity(cfg);
139                 if (cfg->op){
140                         fixed_contact=sal_op_get_contact(cfg->op);
141                         if (fixed_contact) {
142                                 ms_message("Contact for subscribe has been fixed using proxy to %s",fixed_contact);
143                         }
144                 }
145         }else from=linphone_core_get_primary_contact(fr->lc);
146         if (fr->outsub==NULL){
147                 /* people for which we don't have yet an answer should appear as offline */
148                 fr->status=LinphoneStatusOffline;
149                 /*
150                 if (fr->lc->vtable.notify_recv)
151                         fr->lc->vtable.notify_recv(fr->lc,(LinphoneFriend*)fr);
152                  */
153         }else{
154                 sal_op_release(fr->outsub);
155                 fr->outsub=NULL;
156         }
157         fr->outsub=sal_op_new(fr->lc->sal);
158         sal_op_set_route(fr->outsub,route);
159         sal_op_set_contact(fr->outsub,fixed_contact);
160         sal_subscribe_presence(fr->outsub,from,friend);
161         fr->subscribe_active=TRUE;
162         ms_free(friend);
163 }
164
165 LinphoneFriend * linphone_friend_new(){
166         LinphoneFriend *obj=ms_new0(LinphoneFriend,1);
167         obj->pol=LinphoneSPAccept;
168         obj->status=LinphoneStatusOffline;
169         obj->subscribe=TRUE;
170         return obj;     
171 }
172
173 LinphoneFriend *linphone_friend_new_with_addr(const char *addr){
174         LinphoneAddress* linphone_address = linphone_address_new(addr);
175         if (linphone_address == NULL) {
176                 ms_error("Cannot create friend for address [%s]",addr?addr:"null");
177                 return NULL;
178         }
179         LinphoneFriend *fr=linphone_friend_new();
180         if (linphone_friend_set_addr(fr,linphone_address)<0){
181                 linphone_friend_destroy(fr);
182                 return NULL;
183         }
184         return fr;
185 }
186
187 bool_t linphone_friend_in_list(const LinphoneFriend *lf){
188         return lf->lc!=NULL;
189 }
190
191 void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char **result){
192         LinphoneAddress *fr=NULL;
193         *result=NULL;
194         fr=linphone_address_new(uri);
195         if (fr==NULL){
196                 char *tmp=NULL;
197                 if (strchr(uri,'@')!=NULL){
198                         LinphoneAddress *u;
199                         /*try adding sip:*/
200                         tmp=ms_strdup_printf("sip:%s",uri);
201                         u=linphone_address_new(tmp);
202                         if (u!=NULL){
203                                 *result=tmp;
204                         }
205                 }else if (lc->default_proxy!=NULL){
206                         /*try adding domain part from default current proxy*/
207                         LinphoneAddress * id=linphone_address_new(linphone_core_get_identity(lc));
208                         if (id!=NULL){
209                                 linphone_address_set_username(id,uri);
210                                 *result=linphone_address_as_string(id);
211                                 linphone_address_destroy(id);
212                         }
213                 }
214                 if (*result){
215                         /*looks good */
216                         ms_message("%s interpreted as %s",uri,*result);
217                 }else{
218                         ms_warning("Fail to interpret friend uri %s",uri);
219                 }
220         }else *result=linphone_address_as_string(fr);
221         linphone_address_destroy(fr);
222 }
223
224 int linphone_friend_set_addr(LinphoneFriend *lf, const LinphoneAddress *addr){
225         LinphoneAddress *fr=linphone_address_clone(addr);
226         if (fr==NULL) {
227                 ms_warning("Invalid friend sip uri: %s",addr);
228                 return -1;
229         }
230         linphone_address_clean(fr);
231         if (lf->uri!=NULL) linphone_address_destroy(lf->uri);   
232         lf->uri=fr;
233         return 0;
234 }
235
236 int linphone_friend_set_name(LinphoneFriend *lf, const char *name){
237         LinphoneAddress *fr=lf->uri;
238         if (fr==NULL){
239                 ms_error("linphone_friend_set_sip_addr() must be called before linphone_friend_set_name().");
240                 return -1;
241         }
242         linphone_address_set_display_name(fr,name);
243         return 0;
244 }
245
246 int linphone_friend_enable_subscribes(LinphoneFriend *fr, bool_t val){
247         fr->subscribe=val;
248         return 0;
249 }
250
251 int linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol)
252 {
253         fr->pol=pol;
254         return 0;
255 }
256
257 SalPresenceStatus linphone_online_status_to_sal(LinphoneOnlineStatus os){
258         switch(os){
259                 case LinphoneStatusOffline:
260                         return SalPresenceOffline;
261                 break;
262                 case LinphoneStatusOnline:
263                         return SalPresenceOnline;
264                 break;
265                 case LinphoneStatusBusy:
266                         return SalPresenceBusy;
267                 break;
268                 case LinphoneStatusBeRightBack:
269                         return SalPresenceBerightback;
270                 break;
271                 case LinphoneStatusAway:
272                         return SalPresenceAway;
273                 break;
274                 case LinphoneStatusOnThePhone:
275                         return SalPresenceOnthephone;
276                 break;
277                 case LinphoneStatusOutToLunch:
278                         return SalPresenceOuttolunch;
279                 break;
280                 case LinphoneStatusDoNotDisturb:
281                         return SalPresenceDonotdisturb;
282                 break;
283                 case LinphoneStatusMoved:
284                         return SalPresenceMoved;
285                 break;
286                 case LinphoneStatusAltService:
287                         return SalPresenceAltService;
288                 break;
289                 case LinphoneStatusPending:
290                         return SalPresenceOffline;
291                 break;
292                 default:
293                         return SalPresenceOffline;
294                 break;
295         }
296         return SalPresenceOffline;
297 }
298
299 void linphone_friend_notify(LinphoneFriend *lf, LinphoneOnlineStatus os){
300         //printf("Wish to notify %p, lf->nid=%i\n",lf,lf->nid);
301         if (lf->insub!=NULL){
302                 sal_notify_presence(lf->insub,linphone_online_status_to_sal(os),NULL);
303         }
304 }
305
306 static void linphone_friend_unsubscribe(LinphoneFriend *lf){
307         if (lf->outsub!=NULL) {
308                 sal_unsubscribe(lf->outsub);
309                 sal_op_release(lf->outsub);
310                 lf->outsub=NULL;
311                 lf->subscribe_active=FALSE;
312         }
313 }
314
315 void linphone_friend_close_subscriptions(LinphoneFriend *lf){
316         linphone_friend_unsubscribe(lf);
317         if (lf->insub){
318                 sal_notify_close(lf->insub);
319                 sal_op_release(lf->insub);
320                 lf->insub=NULL;
321         }
322 }
323
324 void linphone_friend_destroy(LinphoneFriend *lf){
325         
326         if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
327         if (lf->info!=NULL) buddy_info_free(lf->info);
328         ms_free(lf);
329 }
330
331 const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf){
332         return lf->uri;
333 }
334
335 bool_t linphone_friend_get_send_subscribe(const LinphoneFriend *lf){
336         return lf->subscribe;
337 }
338
339 LinphoneSubscribePolicy linphone_friend_get_inc_subscribe_policy(const LinphoneFriend *lf){
340         return lf->pol;
341 }
342
343 LinphoneOnlineStatus linphone_friend_get_status(const LinphoneFriend *lf){
344         return lf->status;
345 }
346
347 BuddyInfo * linphone_friend_get_info(const LinphoneFriend *lf){
348         return lf->info;
349 }
350
351 void linphone_friend_apply(LinphoneFriend *fr, LinphoneCore *lc){
352         if (fr->uri==NULL) {
353                 ms_warning("No sip url defined.");
354                 return;
355         }
356         fr->lc=lc;
357         
358         linphone_core_write_friends_config(lc);
359
360         if (fr->inc_subscribe_pending){
361                 switch(fr->pol){
362                         case LinphoneSPWait:
363                                 linphone_friend_notify(fr,LinphoneStatusPending);
364                                 break;
365                         case LinphoneSPAccept:
366                                 if (fr->lc!=NULL)
367                                   {
368                                         linphone_friend_notify(fr,fr->lc->presence_mode);
369                                   }
370                                 break;
371                         case LinphoneSPDeny:
372                                 linphone_friend_notify(fr,LinphoneStatusOffline);
373                                 break;
374                 }
375                 fr->inc_subscribe_pending=FALSE;
376         }
377         if (fr->subscribe && fr->subscribe_active==FALSE){
378                 ms_message("Sending a new SUBSCRIBE");
379                 __linphone_friend_do_subscribe(fr);
380         }
381         ms_message("linphone_friend_apply() done.");
382         lc->bl_refresh=TRUE;
383         fr->commit=FALSE;
384 }
385
386 void linphone_friend_edit(LinphoneFriend *fr){
387 }
388
389 void linphone_friend_done(LinphoneFriend *fr){
390         ms_return_if_fail(fr!=NULL);
391         if (fr->lc==NULL) return;
392         linphone_friend_apply(fr,fr->lc);
393 }
394
395 void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf)
396 {
397         ms_return_if_fail(lf->lc==NULL);
398         ms_return_if_fail(lf->uri!=NULL);
399         if (ms_list_find(lc->friends,lf)!=NULL){
400                 char *tmp=NULL;
401                 const LinphoneAddress *addr=linphone_friend_get_address(lf);
402                 if (addr) tmp=linphone_address_as_string(addr);
403                 ms_warning("Friend %s already in list, ignored.", tmp ? tmp : "unknown");
404                 if (tmp) ms_free(tmp);
405                 return ;
406         }
407         lc->friends=ms_list_append(lc->friends,lf);
408         if ( linphone_core_ready(lc)) linphone_friend_apply(lf,lc);
409         else lf->commit=TRUE;
410         return ;
411 }
412
413 void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend* fl){
414         MSList *el=ms_list_find(lc->friends,(void *)fl);
415         if (el!=NULL){
416                 lc->friends=ms_list_remove_link(lc->friends,el);
417                 linphone_friend_destroy((LinphoneFriend*)el->data);
418                 linphone_core_write_friends_config(lc);
419         }
420 }
421
422 void linphone_core_send_initial_subscribes(LinphoneCore *lc){
423         const MSList *elem;
424         for(elem=lc->friends;elem!=NULL;elem=elem->next){
425                 LinphoneFriend *f=(LinphoneFriend*)elem->data;
426                 if (f->commit)
427                         linphone_friend_apply(f,lc);
428         }
429 }
430
431 void linphone_friend_set_ref_key(LinphoneFriend *lf, const char *key){
432         if (lf->refkey!=NULL){
433                 ms_free(lf->refkey);
434                 lf->refkey=NULL;
435         }
436         if (key)
437                 lf->refkey=ms_strdup(key);
438         if (lf->lc)
439                 linphone_core_write_friends_config(lf->lc);
440 }
441
442 const char *linphone_friend_get_ref_key(const LinphoneFriend *lf){
443         return lf->refkey;
444 }
445
446 static bool_t username_match(const char *u1, const char *u2){
447         if (u1==NULL && u2==NULL) return TRUE;
448         if (u1 && u2 && strcasecmp(u1,u2)==0) return TRUE;
449         return FALSE;
450 }
451
452 LinphoneFriend *linphone_core_get_friend_by_address(const LinphoneCore *lc, const char *uri){
453         LinphoneAddress *puri=linphone_address_new(uri);
454         const MSList *elem;
455         const char *username=linphone_address_get_username(puri);
456         const char *domain=linphone_address_get_domain(puri);
457         LinphoneFriend *lf=NULL;
458                 
459         if (puri==NULL){
460                 return NULL;
461         }
462         for(elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
463                 lf=(LinphoneFriend*)elem->data;
464                 const char *it_username=linphone_address_get_username(lf->uri);
465                 const char *it_host=linphone_address_get_domain(lf->uri);;
466                 if (strcasecmp(domain,it_host)==0 && username_match(username,it_username)){
467                         break;
468                 }
469                 lf=NULL;
470         }
471         linphone_address_destroy(puri);
472         return lf;
473 }
474
475 LinphoneFriend *linphone_core_get_friend_by_ref_key(const LinphoneCore *lc, const char *key){
476         const MSList *elem;
477         if (key==NULL) return NULL;
478         for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
479                 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
480                 if (lf->refkey!=NULL && strcmp(lf->refkey,key)==0){
481                         return lf;
482                 }
483         }
484         return NULL;
485 }
486
487 #define key_compare(s1,s2)      strcmp(s1,s2)
488
489 LinphoneSubscribePolicy __policy_str_to_enum(const char* pol){
490         if (key_compare("accept",pol)==0){
491                 return LinphoneSPAccept;
492         }
493         if (key_compare("deny",pol)==0){
494                 return LinphoneSPDeny;
495         }
496         if (key_compare("wait",pol)==0){
497                 return LinphoneSPWait;
498         }
499         ms_warning("Unrecognized subscribe policy: %s",pol);
500         return LinphoneSPWait;
501 }
502
503 LinphoneProxyConfig *__index_to_proxy(LinphoneCore *lc, int index){
504         if (index>=0) return (LinphoneProxyConfig*)ms_list_nth_data(lc->sip_conf.proxies,index);
505         else return NULL;
506 }
507
508 LinphoneFriend * linphone_friend_new_from_config_file(LinphoneCore *lc, int index){
509         const char *tmp;
510         char item[50];
511         int a;
512         LinphoneFriend *lf;
513         LpConfig *config=lc->config;
514         
515         sprintf(item,"friend_%i",index);
516         
517         if (!lp_config_has_section(config,item)){
518                 return NULL;
519         }
520         
521         tmp=lp_config_get_string(config,item,"url",NULL);
522         if (tmp==NULL) {
523                 return NULL;
524         }
525         lf=linphone_friend_new_with_addr(tmp);
526         if (lf==NULL) {
527                 return NULL;
528         }
529         tmp=lp_config_get_string(config,item,"pol",NULL);
530         if (tmp==NULL) linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPWait);
531         else{
532                 linphone_friend_set_inc_subscribe_policy(lf,__policy_str_to_enum(tmp));
533         }
534         a=lp_config_get_int(config,item,"subscribe",0);
535         linphone_friend_send_subscribe(lf,a);
536                 
537         linphone_friend_set_ref_key(lf,lp_config_get_string(config,item,"refkey",NULL));
538         return lf;
539 }
540
541 const char *__policy_enum_to_str(LinphoneSubscribePolicy pol){
542         switch(pol){
543                 case LinphoneSPAccept:
544                         return "accept";
545                         break;
546                 case LinphoneSPDeny:
547                         return "deny";
548                         break;
549                 case LinphoneSPWait:
550                         return "wait";
551                         break;
552         }
553         ms_warning("Invalid policy enum value.");
554         return "wait";
555 }
556
557 void linphone_friend_write_to_config_file(LpConfig *config, LinphoneFriend *lf, int index){
558         char key[50];
559         char *tmp;
560         const char *refkey;
561         
562         sprintf(key,"friend_%i",index);
563         
564         if (lf==NULL){
565                 lp_config_clean_section(config,key);
566                 return;
567         }
568         if (lf->uri!=NULL){
569                 tmp=linphone_address_as_string(lf->uri);
570                 if (tmp==NULL) {
571                         return;
572                 }
573                 lp_config_set_string(config,key,"url",tmp);
574                 ms_free(tmp);
575         }
576         lp_config_set_string(config,key,"pol",__policy_enum_to_str(lf->pol));
577         lp_config_set_int(config,key,"subscribe",lf->subscribe);
578
579         refkey=linphone_friend_get_ref_key(lf);
580         if (refkey){
581                 lp_config_set_string(config,key,"refkey",refkey);
582         }
583 }
584
585 void linphone_core_write_friends_config(LinphoneCore* lc)
586 {
587         MSList *elem;
588         int i;
589         if (! linphone_core_ready(lc)) return; /*dont write config when reading it !*/
590         for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
591                 linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
592         }
593         linphone_friend_write_to_config_file(lc->config,NULL,i);        /* set the end */
594 }
595