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