]> sjero.net Git - linphone/blob - coreapi/friend.c
Merge branch 'master' of git.savannah.nongnu.org:/srv/git/linphone
[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         LinphoneFriend *fr=linphone_friend_new();
175         if (linphone_friend_set_sip_addr(fr,addr)<0){
176                 linphone_friend_destroy(fr);
177                 return NULL;
178         }
179         return fr;
180 }
181
182 bool_t linphone_friend_in_list(const LinphoneFriend *lf){
183         return lf->lc!=NULL;
184 }
185
186 void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char **result){
187         LinphoneAddress *fr=NULL;
188         *result=NULL;
189         fr=linphone_address_new(uri);
190         if (fr==NULL){
191                 char *tmp=NULL;
192                 if (strchr(uri,'@')!=NULL){
193                         LinphoneAddress *u;
194                         /*try adding sip:*/
195                         tmp=ms_strdup_printf("sip:%s",uri);
196                         u=linphone_address_new(tmp);
197                         if (u!=NULL){
198                                 *result=tmp;
199                         }
200                 }else if (lc->default_proxy!=NULL){
201                         /*try adding domain part from default current proxy*/
202                         LinphoneAddress * id=linphone_address_new(linphone_core_get_identity(lc));
203                         if (id!=NULL){
204                                 linphone_address_set_username(id,uri);
205                                 *result=linphone_address_as_string(id);
206                                 linphone_address_destroy(id);
207                         }
208                 }
209                 if (*result){
210                         /*looks good */
211                         ms_message("%s interpreted as %s",uri,*result);
212                 }else{
213                         ms_warning("Fail to interpret friend uri %s",uri);
214                 }
215         }else *result=linphone_address_as_string(fr);
216         linphone_address_destroy(fr);
217 }
218
219 int linphone_friend_set_sip_addr(LinphoneFriend *lf, const char *addr){
220         LinphoneAddress *fr=linphone_address_new(addr);
221         if (fr==NULL) {
222                 ms_warning("Invalid friend sip uri: %s",addr);
223                 return -1;
224         }
225         linphone_address_clean(fr);
226         if (lf->uri!=NULL) linphone_address_destroy(lf->uri);   
227         lf->uri=fr;
228         return 0;
229 }
230
231 int linphone_friend_set_name(LinphoneFriend *lf, const char *name){
232         LinphoneAddress *fr=lf->uri;
233         if (fr==NULL){
234                 ms_error("linphone_friend_set_sip_addr() must be called before linphone_friend_set_name().");
235                 return -1;
236         }
237         linphone_address_set_display_name(fr,name);
238         return 0;
239 }
240
241 int linphone_friend_enable_subscribes(LinphoneFriend *fr, bool_t val){
242         fr->subscribe=val;
243         return 0;
244 }
245
246 int linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol)
247 {
248         fr->pol=pol;
249         return 0;
250 }
251
252 SalPresenceStatus linphone_online_status_to_sal(LinphoneOnlineStatus os){
253         switch(os){
254                 case LinphoneStatusOffline:
255                         return SalPresenceOffline;
256                 break;
257                 case LinphoneStatusOnline:
258                         return SalPresenceOnline;
259                 break;
260                 case LinphoneStatusBusy:
261                         return SalPresenceBusy;
262                 break;
263                 case LinphoneStatusBeRightBack:
264                         return SalPresenceBerightback;
265                 break;
266                 case LinphoneStatusAway:
267                         return SalPresenceAway;
268                 break;
269                 case LinphoneStatusOnThePhone:
270                         return SalPresenceOnthephone;
271                 break;
272                 case LinphoneStatusOutToLunch:
273                         return SalPresenceOuttolunch;
274                 break;
275                 case LinphoneStatusDoNotDisturb:
276                         return SalPresenceDonotdisturb;
277                 break;
278                 case LinphoneStatusMoved:
279                         return SalPresenceMoved;
280                 break;
281                 case LinphoneStatusAltService:
282                         return SalPresenceAltService;
283                 break;
284                 case LinphoneStatusPending:
285                         return SalPresenceOffline;
286                 break;
287                 default:
288                         return SalPresenceOffline;
289                 break;
290         }
291         return SalPresenceOffline;
292 }
293
294 void linphone_friend_notify(LinphoneFriend *lf, LinphoneOnlineStatus os){
295         //printf("Wish to notify %p, lf->nid=%i\n",lf,lf->nid);
296         if (lf->insub!=NULL){
297                 sal_notify_presence(lf->insub,linphone_online_status_to_sal(os),NULL);
298         }
299 }
300
301 static void linphone_friend_unsubscribe(LinphoneFriend *lf){
302         if (lf->outsub!=NULL) {
303                 sal_unsubscribe(lf->outsub);
304                 sal_op_release(lf->outsub);
305                 lf->outsub=NULL;
306                 lf->subscribe_active=FALSE;
307         }
308 }
309
310 void linphone_friend_close_subscriptions(LinphoneFriend *lf){
311         linphone_friend_unsubscribe(lf);
312         if (lf->insub){
313                 sal_notify_close(lf->insub);
314                 sal_op_release(lf->insub);
315                 lf->insub=NULL;
316         }
317 }
318
319 void linphone_friend_destroy(LinphoneFriend *lf){
320         
321         if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
322         if (lf->info!=NULL) buddy_info_free(lf->info);
323         ms_free(lf);
324 }
325
326 const LinphoneAddress *linphone_friend_get_address(const LinphoneFriend *lf){
327         return lf->uri;
328 }
329
330 bool_t linphone_friend_get_send_subscribe(const LinphoneFriend *lf){
331         return lf->subscribe;
332 }
333
334 LinphoneSubscribePolicy linphone_friend_get_inc_subscribe_policy(const LinphoneFriend *lf){
335         return lf->pol;
336 }
337
338 LinphoneOnlineStatus linphone_friend_get_status(const LinphoneFriend *lf){
339         return lf->status;
340 }
341
342 BuddyInfo * linphone_friend_get_info(const LinphoneFriend *lf){
343         return lf->info;
344 }
345
346 void linphone_friend_apply(LinphoneFriend *fr, LinphoneCore *lc){
347         if (fr->uri==NULL) {
348                 ms_warning("No sip url defined.");
349                 return;
350         }
351         fr->lc=lc;
352         
353         linphone_core_write_friends_config(lc);
354
355         if (fr->inc_subscribe_pending){
356                 switch(fr->pol){
357                         case LinphoneSPWait:
358                                 linphone_friend_notify(fr,LinphoneStatusPending);
359                                 break;
360                         case LinphoneSPAccept:
361                                 if (fr->lc!=NULL)
362                                   {
363                                         linphone_friend_notify(fr,fr->lc->presence_mode);
364                                   }
365                                 break;
366                         case LinphoneSPDeny:
367                                 linphone_friend_notify(fr,LinphoneStatusOffline);
368                                 break;
369                 }
370                 fr->inc_subscribe_pending=FALSE;
371         }
372         if (fr->subscribe && fr->subscribe_active==FALSE){
373                 ms_message("Sending a new SUBSCRIBE");
374                 __linphone_friend_do_subscribe(fr);
375         }
376         ms_message("linphone_friend_apply() done.");
377         lc->bl_refresh=TRUE;
378         fr->commit=FALSE;
379 }
380
381 void linphone_friend_edit(LinphoneFriend *fr){
382 }
383
384 void linphone_friend_done(LinphoneFriend *fr){
385         ms_return_if_fail(fr!=NULL);
386         if (fr->lc==NULL) return;
387         linphone_friend_apply(fr,fr->lc);
388 }
389
390 void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf)
391 {
392         ms_return_if_fail(lf->lc==NULL);
393         ms_return_if_fail(lf->uri!=NULL);
394         if (ms_list_find(lc->friends,lf)!=NULL){
395                 char *tmp=NULL;
396                 const LinphoneAddress *addr=linphone_friend_get_address(lf);
397                 if (addr) tmp=linphone_address_as_string(addr);
398                 ms_warning("Friend %s already in list, ignored.", tmp ? tmp : "unknown");
399                 if (tmp) ms_free(tmp);
400                 return ;
401         }
402         lc->friends=ms_list_append(lc->friends,lf);
403         if ( linphone_core_ready(lc)) linphone_friend_apply(lf,lc);
404         else lf->commit=TRUE;
405         return ;
406 }
407
408 void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend* fl){
409         MSList *el=ms_list_find(lc->friends,(void *)fl);
410         if (el!=NULL){
411                 lc->friends=ms_list_remove_link(lc->friends,el);
412                 linphone_friend_destroy((LinphoneFriend*)el->data);
413                 linphone_core_write_friends_config(lc);
414         }
415 }
416
417 void linphone_core_send_initial_subscribes(LinphoneCore *lc){
418         const MSList *elem;
419         for(elem=lc->friends;elem!=NULL;elem=elem->next){
420                 LinphoneFriend *f=(LinphoneFriend*)elem->data;
421                 if (f->commit)
422                         linphone_friend_apply(f,lc);
423         }
424 }
425
426 void linphone_friend_set_ref_key(LinphoneFriend *lf, const char *key){
427         if (lf->refkey!=NULL){
428                 ms_free(lf->refkey);
429                 lf->refkey=NULL;
430         }
431         if (key)
432                 lf->refkey=ms_strdup(key);
433         if (lf->lc)
434                 linphone_core_write_friends_config(lf->lc);
435 }
436
437 const char *linphone_friend_get_ref_key(const LinphoneFriend *lf){
438         return lf->refkey;
439 }
440
441 static bool_t username_match(const char *u1, const char *u2){
442         if (u1==NULL && u2==NULL) return TRUE;
443         if (u1 && u2 && strcasecmp(u1,u2)==0) return TRUE;
444         return FALSE;
445 }
446
447 LinphoneFriend *linphone_core_get_friend_by_address(const LinphoneCore *lc, const char *uri){
448         LinphoneAddress *puri=linphone_address_new(uri);
449         const MSList *elem;
450         const char *username=linphone_address_get_username(puri);
451         const char *domain=linphone_address_get_domain(puri);
452         LinphoneFriend *lf=NULL;
453                 
454         if (puri==NULL){
455                 return NULL;
456         }
457         for(elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
458                 lf=(LinphoneFriend*)elem->data;
459                 const char *it_username=linphone_address_get_username(lf->uri);
460                 const char *it_host=linphone_address_get_domain(lf->uri);;
461                 if (strcasecmp(domain,it_host)==0 && username_match(username,it_username)){
462                         break;
463                 }
464                 lf=NULL;
465         }
466         linphone_address_destroy(puri);
467         return lf;
468 }
469
470 LinphoneFriend *linphone_core_get_friend_by_ref_key(const LinphoneCore *lc, const char *key){
471         const MSList *elem;
472         if (key==NULL) return NULL;
473         for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
474                 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
475                 if (lf->refkey!=NULL && strcmp(lf->refkey,key)==0){
476                         return lf;
477                 }
478         }
479         return NULL;
480 }
481
482 #define key_compare(s1,s2)      strcmp(s1,s2)
483
484 LinphoneSubscribePolicy __policy_str_to_enum(const char* pol){
485         if (key_compare("accept",pol)==0){
486                 return LinphoneSPAccept;
487         }
488         if (key_compare("deny",pol)==0){
489                 return LinphoneSPDeny;
490         }
491         if (key_compare("wait",pol)==0){
492                 return LinphoneSPWait;
493         }
494         ms_warning("Unrecognized subscribe policy: %s",pol);
495         return LinphoneSPWait;
496 }
497
498 LinphoneProxyConfig *__index_to_proxy(LinphoneCore *lc, int index){
499         if (index>=0) return (LinphoneProxyConfig*)ms_list_nth_data(lc->sip_conf.proxies,index);
500         else return NULL;
501 }
502
503 LinphoneFriend * linphone_friend_new_from_config_file(LinphoneCore *lc, int index){
504         const char *tmp;
505         char item[50];
506         int a;
507         LinphoneFriend *lf;
508         LpConfig *config=lc->config;
509         
510         sprintf(item,"friend_%i",index);
511         
512         if (!lp_config_has_section(config,item)){
513                 return NULL;
514         }
515         
516         tmp=lp_config_get_string(config,item,"url",NULL);
517         if (tmp==NULL) {
518                 return NULL;
519         }
520         lf=linphone_friend_new_with_addr(tmp);
521         if (lf==NULL) {
522                 return NULL;
523         }
524         tmp=lp_config_get_string(config,item,"pol",NULL);
525         if (tmp==NULL) linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPWait);
526         else{
527                 linphone_friend_set_inc_subscribe_policy(lf,__policy_str_to_enum(tmp));
528         }
529         a=lp_config_get_int(config,item,"subscribe",0);
530         linphone_friend_send_subscribe(lf,a);
531                 
532         linphone_friend_set_ref_key(lf,lp_config_get_string(config,item,"refkey",NULL));
533         return lf;
534 }
535
536 const char *__policy_enum_to_str(LinphoneSubscribePolicy pol){
537         switch(pol){
538                 case LinphoneSPAccept:
539                         return "accept";
540                         break;
541                 case LinphoneSPDeny:
542                         return "deny";
543                         break;
544                 case LinphoneSPWait:
545                         return "wait";
546                         break;
547         }
548         ms_warning("Invalid policy enum value.");
549         return "wait";
550 }
551
552 void linphone_friend_write_to_config_file(LpConfig *config, LinphoneFriend *lf, int index){
553         char key[50];
554         char *tmp;
555         const char *refkey;
556         
557         sprintf(key,"friend_%i",index);
558         
559         if (lf==NULL){
560                 lp_config_clean_section(config,key);
561                 return;
562         }
563         if (lf->uri!=NULL){
564                 tmp=linphone_address_as_string(lf->uri);
565                 if (tmp==NULL) {
566                         return;
567                 }
568                 lp_config_set_string(config,key,"url",tmp);
569                 ms_free(tmp);
570         }
571         lp_config_set_string(config,key,"pol",__policy_enum_to_str(lf->pol));
572         lp_config_set_int(config,key,"subscribe",lf->subscribe);
573
574         refkey=linphone_friend_get_ref_key(lf);
575         if (refkey){
576                 lp_config_set_string(config,key,"refkey",refkey);
577         }
578 }
579
580 void linphone_core_write_friends_config(LinphoneCore* lc)
581 {
582         MSList *elem;
583         int i;
584         if (! linphone_core_ready(lc)) return; /*dont write config when reading it !*/
585         for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
586                 linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
587         }
588         linphone_friend_write_to_config_file(lc->config,NULL,i);        /* set the end */
589 }
590