]> sjero.net Git - linphone/blob - linphone/coreapi/friend.c
08f072de31b8309b6f86750d0273360f7fd4241b
[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 <eXosip2/eXosip.h>
28 #include <osipparser2/osip_message.h>
29 #include "lpconfig.h"
30
31 const char *linphone_online_status_to_string(LinphoneOnlineStatus ss){
32         const char *str=NULL;
33         switch(ss){
34                 case LINPHONE_STATUS_UNKNOWN:
35                 str=_("Unknown");
36                 break;
37                 case LINPHONE_STATUS_ONLINE:
38                 str=_("Online");
39                 break;
40                 case LINPHONE_STATUS_BUSY:
41                 str=_("Busy");
42                 break;
43                 case LINPHONE_STATUS_BERIGHTBACK:
44                 str=_("Be right back");
45                 break;
46                 case LINPHONE_STATUS_AWAY:
47                 str=_("Away");
48                 break;
49                 case LINPHONE_STATUS_ONTHEPHONE:
50                 str=_("On the phone");
51                 break;
52                 case LINPHONE_STATUS_OUTTOLUNCH:
53                 str=_("Out to lunch");
54                 break;
55                 case LINPHONE_STATUS_NOT_DISTURB:
56                 str=_("Do not disturb");
57                 break;
58                 case LINPHONE_STATUS_MOVED:
59                 str=_("Moved");
60                 break;
61                 case LINPHONE_STATUS_ALT_SERVICE:
62                 str=_("Using another messaging service");
63                 break;
64                 case LINPHONE_STATUS_OFFLINE:
65                 str=_("Offline");
66                 break;
67                 case LINPHONE_STATUS_PENDING:
68                 str=_("Pending");
69                 break;
70                 case LINPHONE_STATUS_CLOSED:
71                 str=_("Closed");
72                 break;
73                 default:
74                 str=_("Unknown-bug");
75         }
76         return str;
77 }
78
79 static int friend_data_compare(const void * a, const void * b, void * data){
80         LinphoneAddress *fa=((LinphoneFriend*)a)->uri;
81         LinphoneAddress *fb=((LinphoneFriend*)b)->uri;
82         const char *ua,*ub;
83         ua=linphone_address_get_username(fa);
84         ub=linphone_address_get_username(fb);
85         if (ua!=NULL && ub!=NULL) {
86                 //printf("Comparing usernames %s,%s\n",ua,ub);
87                 return strcasecmp(ua,ub);
88         }
89         else {
90                 /* compare hosts*/
91                 ua=linphone_address_get_domain(fa);
92                 ub=linphone_address_get_domain(fb);
93                 if (ua!=NULL && ub!=NULL){
94                         int ret=strcasecmp(ua,ub);
95                         //printf("Comparing hostnames %s,%s,res=%i\n",ua,ub,ret);
96                         return ret;
97                 }
98                 else return -1;
99         }
100 }
101
102 static int friend_compare(const void * a, const void * b){
103         return friend_data_compare(a,b,NULL);
104 }
105
106
107 MSList *linphone_find_friend(MSList *fl, const LinphoneAddress *friend, LinphoneFriend **lf){
108         MSList *res=NULL;
109         LinphoneFriend dummy;
110         if (lf!=NULL) *lf=NULL;
111         dummy.uri=(LinphoneAddress*)friend;
112         res=ms_list_find_custom(fl,friend_compare,&dummy);
113         if (lf!=NULL && res!=NULL) *lf=(LinphoneFriend*)res->data;
114         return res;
115 }
116
117 LinphoneFriend *linphone_find_friend_by_nid(MSList *l, int nid){
118         MSList *elem;
119         for (elem=l;elem!=NULL;elem=elem->next){
120                 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
121                 if (lf->nid==nid) return lf;
122         }
123         return NULL;
124 }
125
126 LinphoneFriend *linphone_find_friend_by_sid(MSList *l, int sid){
127         MSList *elem;
128         for (elem=l;elem!=NULL;elem=elem->next){
129                 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
130                 if (lf->sid==sid) return lf;
131         }
132         return NULL;
133 }
134
135 void __linphone_friend_do_subscribe(LinphoneFriend *fr){
136         char *friend=NULL;
137         const char *route=NULL;
138         const char *from=NULL;
139         osip_message_t *msg=NULL;
140         friend=linphone_address_as_string(fr->uri);
141         if (fr->proxy!=NULL){
142                 route=fr->proxy->reg_route;
143                 from=fr->proxy->reg_identity;
144         }else from=linphone_core_get_primary_contact(fr->lc);
145         if (fr->sid<0){
146                 /* people for which we don't have yet an answer should appear as offline */
147                 fr->lc->vtable.notify_recv(fr->lc,(LinphoneFriend*)fr,friend,_("Gone"),"sip-closed.png");
148         }
149         eXosip_lock();
150         eXosip_subscribe_build_initial_request(&msg,friend,from,route,"presence",600);
151         eXosip_subscribe_send_initial_request(msg);
152         eXosip_unlock();
153         ms_free(friend);
154 }
155
156
157 LinphoneFriend * linphone_friend_new(){
158         LinphoneFriend *obj=ms_new0(LinphoneFriend,1);
159         obj->out_did=-1;
160         obj->in_did=-1;
161         obj->nid=-1;
162         obj->sid=-1;
163         obj->pol=LinphoneSPAccept;
164         obj->status=LINPHONE_STATUS_OFFLINE;
165         obj->subscribe=TRUE;
166         return obj;     
167 }
168
169 LinphoneFriend *linphone_friend_new_with_addr(const char *addr){
170         LinphoneFriend *fr=linphone_friend_new();
171         if (linphone_friend_set_sip_addr(fr,addr)<0){
172                 linphone_friend_destroy(fr);
173                 return NULL;
174         }
175         return fr;
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         if (lf->uri!=NULL) linphone_address_destroy(lf->uri);   
218         lf->uri=fr;
219         return 0;
220 }
221
222 int linphone_friend_set_name(LinphoneFriend *lf, const char *name){
223         LinphoneAddress *fr=lf->uri;
224         if (fr==NULL){
225                 ms_error("linphone_friend_set_sip_addr() must be called before linphone_friend_set_name().");
226                 return -1;
227         }
228         linphone_address_set_display_name(fr,name);
229         return 0;
230 }
231
232 int linphone_friend_send_subscribe(LinphoneFriend *fr, bool_t val){
233         fr->subscribe=val;
234         return 0;
235 }
236
237 int linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol)
238 {
239         fr->pol=pol;
240         return 0;
241 }
242
243 int linphone_friend_set_proxy(LinphoneFriend *fr, struct _LinphoneProxyConfig *cfg){
244         fr->proxy=cfg;
245         return 0;
246 }
247
248 void linphone_friend_set_sid(LinphoneFriend *lf, int sid){
249         lf->sid=sid;
250 }
251 void linphone_friend_set_nid(LinphoneFriend *lf, int nid){
252         lf->nid=nid;
253         lf->inc_subscribe_pending=TRUE;
254 }
255
256 void add_presence_body(osip_message_t *notify, LinphoneOnlineStatus online_status)
257 {
258         char buf[1000];
259 #ifdef SUPPORT_MSN
260         int atom_id = 1000;
261 #endif
262         char *contact_info;
263
264         osip_contact_t *ct=NULL;
265         osip_message_get_contact(notify,0,&ct);
266         osip_contact_to_str(ct,&contact_info);
267
268 #ifdef SUPPORT_MSN
269
270   if (online_status==LINPHONE_STATUS_ONLINE)
271     {
272       sprintf(buf, "<?xml version=\"1.0\"?>\n\
273 <!DOCTYPE presence\n\
274 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
275 <presence>\n\
276 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
277 <atom id=\"%i\">\n\
278 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
279 <status status=\"open\" />\n\
280 <msnsubstatus substatus=\"online\" />\n\
281 </address>\n\
282 </atom>\n\
283 </presence>", contact_info, atom_id, contact_info);
284
285     }
286   else if (online_status==LINPHONE_STATUS_BUSY)
287     {
288       sprintf(buf, "<?xml version=\"1.0\"?>\n\
289 <!DOCTYPE presence\n\
290 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
291 <presence>\n\
292 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
293 <atom id=\"%i\">\n\
294 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
295 <status status=\"inuse\" />\n\
296 <msnsubstatus substatus=\"busy\" />\n\
297 </address>\n\
298 </atom>\n\
299 </presence>", contact_info, atom_id, contact_info);
300
301     }
302   else if (online_status==LINPHONE_STATUS_BERIGHTBACK)
303     {
304       sprintf(buf, "<?xml version=\"1.0\"?>\n\
305 <!DOCTYPE presence\n\
306 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
307 <presence>\n\
308 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
309 <atom id=\"%i\">\n\
310 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
311 <status status=\"inactive\" />\n\
312 <msnsubstatus substatus=\"berightback\" />\n\
313 </address>\n\
314 </atom>\n\
315 </presence>", contact_info, atom_id, contact_info);
316
317     }
318   else if (online_status==LINPHONE_STATUS_AWAY)
319     {
320       sprintf(buf, "<?xml version=\"1.0\"?>\n\
321 <!DOCTYPE presence\n\
322 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
323 <presence>\n\
324 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
325 <atom id=\"%i\">\n\
326 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
327 <status status=\"inactive\" />\n\
328 <msnsubstatus substatus=\"away\" />\n\
329 </address>\n\
330 </atom>\n\
331 </presence>", contact_info, atom_id, contact_info);
332
333     }
334   else if (online_status==LINPHONE_STATUS_ONTHEPHONE)
335     {
336       sprintf(buf, "<?xml version=\"1.0\"?>\n\
337 <!DOCTYPE presence\n\
338 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
339 <presence>\n\
340 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
341 <atom id=\"%i\">\n\
342 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
343 <status status=\"inuse\" />\n\
344 <msnsubstatus substatus=\"onthephone\" />\n\
345 </address>\n\
346 </atom>\n\
347 </presence>", contact_info, atom_id, contact_info);
348
349     }
350   else if (online_status==LINPHONE_STATUS_OUTTOLUNCH)
351     {
352       sprintf(buf, "<?xml version=\"1.0\"?>\n\
353 <!DOCTYPE presence\n\
354 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
355 <presence>\n\
356 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
357 <atom id=\"%i\">\n\
358 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
359 <status status=\"inactive\" />\n\
360 <msnsubstatus substatus=\"outtolunch\" />\n\
361 </address>\n\
362 </atom>\n\
363 </presence>", contact_info, atom_id, contact_info);
364
365     }
366   else
367     {
368       sprintf(buf, "<?xml version=\"1.0\"?>\n\
369 <!DOCTYPE presence\n\
370 PUBLIC \"-//IETF//DTD RFCxxxx XPIDF 1.0//EN\" \"xpidf.dtd\">\n\
371 <presence>\n\
372 <presentity uri=\"%s;method=SUBSCRIBE\" />\n\
373 <atom id=\"%i\">\n\
374 <address uri=\"%s;user=ip\" priority=\"0.800000\">\n\
375 <status status=\"inactive\" />\n\
376 <msnsubstatus substatus=\"away\" />\n\
377 </address>\n\
378 </atom>\n\
379 </presence>", contact_info, atom_id, contact_info);
380     }
381
382   osip_message_set_body(notify, buf, strlen(buf));
383   osip_message_set_content_type(notify, "application/xpidf+xml");
384 #else
385
386   if (online_status==LINPHONE_STATUS_ONLINE)
387     {
388       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
389 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
390           entity=\"%s\">\n\
391 <tuple id=\"sg89ae\">\n\
392 <status>\n\
393 <basic>open</basic>\n\
394 </status>\n\
395 <contact priority=\"0.8\">%s</contact>\n\
396 <note>online</note>\n\
397 </tuple>\n\
398 </presence>",
399               contact_info, contact_info);
400     }
401   else if (online_status==LINPHONE_STATUS_BUSY)
402     {
403       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
404 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
405           xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
406           entity=\"%s\">\n\
407 <tuple id=\"sg89ae\">\n\
408 <status>\n\
409 <basic>open</basic>\n\
410 <es:activities>\n\
411   <es:activity>busy</es:activity>\n\
412 </es:activities>\n\
413 </status>\n\
414 <contact priority=\"0.8\">%s</contact>\n\
415 <note>busy</note>\n\
416 </tuple>\n\
417 </presence>",
418               contact_info, contact_info);
419     }
420   else if (online_status==LINPHONE_STATUS_BERIGHTBACK)
421     {
422       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
423 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
424           xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
425           entity=\"%s\">\n\
426 <tuple id=\"sg89ae\">\n\
427 <status>\n\
428 <basic>open</basic>\n\
429 <es:activities>\n\
430   <es:activity>in-transit</es:activity>\n\
431 </es:activities>\n\
432 </status>\n\
433 <contact priority=\"0.8\">%s</contact>\n\
434 <note>be right back</note>\n\
435 </tuple>\n\
436 </presence>",
437               contact_info, contact_info);
438     }
439   else if (online_status==LINPHONE_STATUS_AWAY)
440     {
441       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
442 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
443           xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
444           entity=\"%s\">\n\
445 <tuple id=\"sg89ae\">\n\
446 <status>\n\
447 <basic>open</basic>\n\
448 <es:activities>\n\
449   <es:activity>away</es:activity>\n\
450 </es:activities>\n\
451 </status>\n\
452 <contact priority=\"0.8\">%s</contact>\n\
453 <note>away</note>\n\
454 </tuple>\n\
455 </presence>",
456               contact_info, contact_info);
457     }
458   else if (online_status==LINPHONE_STATUS_ONTHEPHONE)
459     {
460       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
461 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
462           xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
463           entity=\"%s\">\n\
464 <tuple id=\"sg89ae\">\n\
465 <status>\n\
466 <basic>open</basic>\n\
467 <es:activities>\n\
468   <es:activity>on-the-phone</es:activity>\n\
469 </es:activities>\n\
470 </status>\n\
471 <contact priority=\"0.8\">%s</contact>\n\
472 <note>on the phone</note>\n\
473 </tuple>\n\
474 </presence>",
475               contact_info, contact_info);
476     }
477   else if (online_status==LINPHONE_STATUS_OUTTOLUNCH)
478     {
479       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
480 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
481           xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
482           entity=\"%s\">\n\
483 <tuple id=\"sg89ae\">\n\
484 <status>\n\
485 <basic>open</basic>\n\
486 <es:activities>\n\
487   <es:activity>meal</es:activity>\n\
488 </es:activities>\n\
489 </status>\n\
490 <contact priority=\"0.8\">%s</contact>\n\
491 <note>out to lunch</note>\n\
492 </tuple>\n\
493 </presence>",
494               contact_info, contact_info);
495     }
496   else
497     {
498       /* */
499       sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
500 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
501 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
502 entity=\"%s\">\n%s",
503               contact_info,
504 "<tuple id=\"sg89ae\">\n\
505 <status>\n\
506 <basic>closed</basic>\n\
507 <es:activities>\n\
508   <es:activity>permanent-absence</es:activity>\n\
509 </es:activities>\n\
510 </status>\n\
511 </tuple>\n\
512 \n</presence>\n");
513     }
514   osip_message_set_body(notify, buf, strlen(buf));
515   osip_message_set_content_type(notify, "application/pidf+xml");
516
517 #endif
518         osip_free(contact_info);
519 }
520
521
522 void linphone_friend_notify(LinphoneFriend *lf, int ss, LinphoneOnlineStatus os){
523         //printf("Wish to notify %p, lf->nid=%i\n",lf,lf->nid);
524         if (lf->in_did!=-1){
525                 osip_message_t *msg=NULL;
526                 const char *identity;
527                 if (lf->proxy!=NULL) identity=lf->proxy->reg_identity;
528                 else identity=linphone_core_get_primary_contact(lf->lc);
529                 eXosip_lock();
530                 eXosip_insubscription_build_notify(lf->in_did,ss,0,&msg);
531                 if (msg!=NULL){
532                         osip_message_set_contact(msg,identity);
533                         add_presence_body(msg,os);
534                         eXosip_insubscription_send_request(lf->in_did,msg);
535                 }else ms_error("could not create notify for incoming subscription.");
536                 eXosip_unlock();
537         }
538 }
539
540 static void linphone_friend_unsubscribe(LinphoneFriend *lf){
541         if (lf->out_did!=-1) {
542                 osip_message_t *msg=NULL;
543                 eXosip_lock();
544                 eXosip_subscribe_build_refresh_request(lf->out_did,&msg);
545                 if (msg){
546                         osip_message_set_expires(msg,"0");
547                         eXosip_subscribe_send_refresh_request(lf->out_did,msg);
548                 }else ms_error("Could not build subscribe refresh request !");
549                 eXosip_unlock();
550         }
551 }
552
553 void linphone_friend_destroy(LinphoneFriend *lf){
554         linphone_friend_notify(lf,EXOSIP_SUBCRSTATE_TERMINATED,LINPHONE_STATUS_CLOSED);
555         linphone_friend_unsubscribe(lf);
556         if (lf->uri!=NULL) linphone_address_destroy(lf->uri);
557         if (lf->info!=NULL) buddy_info_free(lf->info);
558         ms_free(lf);
559 }
560
561 void linphone_friend_check_for_removed_proxy(LinphoneFriend *lf, LinphoneProxyConfig *cfg){
562         if (lf->proxy==cfg){
563                 lf->proxy=NULL;
564         }
565 }
566
567 const LinphoneAddress *linphone_friend_get_uri(const LinphoneFriend *lf){
568         return lf->uri;
569 }
570
571
572 bool_t linphone_friend_get_send_subscribe(const LinphoneFriend *lf){
573         return lf->subscribe;
574 }
575
576 LinphoneSubscribePolicy linphone_friend_get_inc_subscribe_policy(const LinphoneFriend *lf){
577         return lf->pol;
578 }
579
580 LinphoneOnlineStatus linphone_friend_get_status(const LinphoneFriend *lf){
581         return lf->status;
582 }
583
584 BuddyInfo * linphone_friend_get_info(const LinphoneFriend *lf){
585         return lf->info;
586 }
587
588 void linphone_friend_apply(LinphoneFriend *fr, LinphoneCore *lc){
589         if (fr->uri==NULL) {
590                 ms_warning("No sip url defined.");
591                 return;
592         }
593         fr->lc=lc;
594         
595         linphone_core_write_friends_config(lc);
596
597         if (fr->inc_subscribe_pending){
598                 switch(fr->pol){
599                         case LinphoneSPWait:
600                                 linphone_friend_notify(fr,EXOSIP_SUBCRSTATE_PENDING,LINPHONE_STATUS_PENDING);
601                                 break;
602                         case LinphoneSPAccept:
603                                 if (fr->lc!=NULL)
604                                   {
605                                         linphone_friend_notify(fr,EXOSIP_SUBCRSTATE_ACTIVE,fr->lc->presence_mode);
606                                   }
607                                 break;
608                         case LinphoneSPDeny:
609                                 linphone_friend_notify(fr,EXOSIP_SUBCRSTATE_TERMINATED,LINPHONE_STATUS_CLOSED);
610                                 break;
611                 }
612                 fr->inc_subscribe_pending=FALSE;
613         }
614         if (fr->subscribe && fr->out_did==-1){
615                 
616                 __linphone_friend_do_subscribe(fr);
617         }
618         ms_message("linphone_friend_apply() done.");
619         lc->bl_refresh=TRUE;
620 }
621
622 void linphone_friend_edit(LinphoneFriend *fr){
623 }
624
625 void linphone_friend_done(LinphoneFriend *fr){
626         ms_return_if_fail(fr!=NULL);
627         if (fr->lc==NULL) return;
628         linphone_friend_apply(fr,fr->lc);
629 }
630
631 void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf)
632 {
633         ms_return_if_fail(lf->lc==NULL);
634         ms_return_if_fail(lf->uri!=NULL);
635         lc->friends=ms_list_append(lc->friends,lf);
636         linphone_friend_apply(lf,lc);
637         return ;
638 }
639
640 void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend* fl){
641         MSList *el=ms_list_find(lc->friends,(void *)fl);
642         if (el!=NULL){
643                 lc->friends=ms_list_remove_link(lc->friends,el);
644                 linphone_friend_destroy((LinphoneFriend*)el->data);
645                 linphone_core_write_friends_config(lc);
646         }
647 }
648
649 void linphone_friend_set_ref_key(LinphoneFriend *lf, const char *key){
650         if (lf->refkey!=NULL){
651                 ms_free(lf->refkey);
652                 lf->refkey=NULL;
653         }
654         if (key)
655                 lf->refkey=ms_strdup(key);
656         if (lf->lc)
657                 linphone_core_write_friends_config(lf->lc);
658 }
659
660 const char *linphone_friend_get_ref_key(const LinphoneFriend *lf){
661         return lf->refkey;
662 }
663
664 static bool_t username_match(const char *u1, const char *u2){
665         if (u1==NULL && u2==NULL) return TRUE;
666         if (u1 && u2 && strcasecmp(u1,u2)==0) return TRUE;
667         return FALSE;
668 }
669
670 LinphoneFriend *linphone_core_get_friend_by_uri(const LinphoneCore *lc, const char *uri){
671         LinphoneAddress *puri=linphone_address_new(uri);
672         const MSList *elem;
673         const char *username=linphone_address_get_username(puri);
674         const char *domain=linphone_address_get_domain(puri);
675         LinphoneFriend *lf=NULL;
676                 
677         if (puri==NULL){
678                 return NULL;
679         }
680         for(elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
681                 lf=(LinphoneFriend*)elem->data;
682                 const char *it_username=linphone_address_get_username(lf->uri);
683                 const char *it_host=linphone_address_get_domain(lf->uri);;
684                 if (strcasecmp(domain,it_host)==0 && username_match(username,it_username)){
685                         break;
686                 }
687                 lf=NULL;
688         }
689         linphone_address_destroy(puri);
690         return lf;
691 }
692
693 LinphoneFriend *linphone_core_get_friend_by_ref_key(const LinphoneCore *lc, const char *key){
694         const MSList *elem;
695         if (key==NULL) return NULL;
696         for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
697                 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
698                 if (lf->refkey!=NULL && strcmp(lf->refkey,key)==0){
699                         return lf;
700                 }
701         }
702         return NULL;
703 }
704
705 #define key_compare(s1,s2)      strcmp(s1,s2)
706
707 LinphoneSubscribePolicy __policy_str_to_enum(const char* pol){
708         if (key_compare("accept",pol)==0){
709                 return LinphoneSPAccept;
710         }
711         if (key_compare("deny",pol)==0){
712                 return LinphoneSPDeny;
713         }
714         if (key_compare("wait",pol)==0){
715                 return LinphoneSPWait;
716         }
717         ms_warning("Unrecognized subscribe policy: %s",pol);
718         return LinphoneSPWait;
719 }
720
721 LinphoneProxyConfig *__index_to_proxy(LinphoneCore *lc, int index){
722         if (index>=0) return (LinphoneProxyConfig*)ms_list_nth_data(lc->sip_conf.proxies,index);
723         else return NULL;
724 }
725
726 LinphoneFriend * linphone_friend_new_from_config_file(LinphoneCore *lc, int index){
727         const char *tmp;
728         char item[50];
729         int a;
730         LinphoneFriend *lf;
731         LpConfig *config=lc->config;
732         
733         sprintf(item,"friend_%i",index);
734         
735         if (!lp_config_has_section(config,item)){
736                 return NULL;
737         }
738         
739         tmp=lp_config_get_string(config,item,"url",NULL);
740         if (tmp==NULL) {
741                 return NULL;
742         }
743         lf=linphone_friend_new_with_addr(tmp);
744         if (lf==NULL) {
745                 return NULL;
746         }
747         tmp=lp_config_get_string(config,item,"pol",NULL);
748         if (tmp==NULL) linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPWait);
749         else{
750                 linphone_friend_set_inc_subscribe_policy(lf,__policy_str_to_enum(tmp));
751         }
752         a=lp_config_get_int(config,item,"subscribe",0);
753         linphone_friend_send_subscribe(lf,a);
754                 
755         a=lp_config_get_int(config,item,"proxy",-1);
756         if (a!=-1) {
757                 linphone_friend_set_proxy(lf,__index_to_proxy(lc,a));
758         }
759         linphone_friend_set_ref_key(lf,lp_config_get_string(config,item,"refkey",NULL));
760         return lf;
761 }
762
763 const char *__policy_enum_to_str(LinphoneSubscribePolicy pol){
764         switch(pol){
765                 case LinphoneSPAccept:
766                         return "accept";
767                         break;
768                 case LinphoneSPDeny:
769                         return "deny";
770                         break;
771                 case LinphoneSPWait:
772                         return "wait";
773                         break;
774         }
775         ms_warning("Invalid policy enum value.");
776         return "wait";
777 }
778
779 void linphone_friend_write_to_config_file(LpConfig *config, LinphoneFriend *lf, int index){
780         char key[50];
781         char *tmp;
782         int a;
783         const char *refkey;
784         
785         sprintf(key,"friend_%i",index);
786         
787         if (lf==NULL){
788                 lp_config_clean_section(config,key);
789                 return;
790         }
791         if (lf->uri!=NULL){
792                 tmp=linphone_address_as_string(lf->uri);
793                 if (tmp==NULL) {
794                         return;
795                 }
796                 lp_config_set_string(config,key,"url",tmp);
797                 osip_free(tmp);
798         }
799         lp_config_set_string(config,key,"pol",__policy_enum_to_str(lf->pol));
800         lp_config_set_int(config,key,"subscribe",lf->subscribe);
801         if (lf->proxy!=NULL){
802                 a=ms_list_index(lf->lc->sip_conf.proxies,lf->proxy);
803                 lp_config_set_int(config,key,"proxy",a);
804         }else lp_config_set_int(config,key,"proxy",-1);
805
806         refkey=linphone_friend_get_ref_key(lf);
807         if (refkey){
808                 lp_config_set_string(config,key,"refkey",refkey);
809         }
810 }
811
812 void linphone_core_write_friends_config(LinphoneCore* lc)
813 {
814         MSList *elem;
815         int i;
816         if (!lc->ready) return; /*dont write config when reading it !*/
817         for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
818                 linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
819         }
820         linphone_friend_write_to_config_file(lc->config,NULL,i);        /* set the end */
821 }
822