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