]> sjero.net Git - linphone/blob - coreapi/proxy.c
major redesign of liblinphone in progress
[linphone] / coreapi / proxy.c
1 /*
2 linphone
3 Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)
4 */
5 /*
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20  
21 #include "linphonecore.h"
22 #include "sipsetup.h"
23 #include "lpconfig.h"
24 #include "private.h"
25 #include "mediastreamer2/mediastream.h"
26
27
28 #include <ctype.h>
29
30
31 void linphone_proxy_config_write_all_to_config_file(LinphoneCore *lc){
32         MSList *elem;
33         int i;
34         for(elem=lc->sip_conf.proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
35                 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
36                 linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
37         }
38 }
39
40 void linphone_proxy_config_init(LinphoneProxyConfig *obj){
41         memset(obj,0,sizeof(LinphoneProxyConfig));
42         obj->expires=3600;
43 }
44
45 /**
46  * @addtogroup proxies
47  * @{
48 **/
49
50 /**
51  * Creates an empty proxy config.
52 **/
53 LinphoneProxyConfig *linphone_proxy_config_new(){
54         LinphoneProxyConfig *obj=NULL;
55         obj=ms_new(LinphoneProxyConfig,1);
56         linphone_proxy_config_init(obj);
57         return obj;
58 }
59
60 /**
61  * Destroys a proxy config.
62  * 
63  * @note: LinphoneProxyConfig that have been removed from LinphoneCore with
64  * linphone_core_remove_proxy_config() must not be freed.
65 **/
66 void linphone_proxy_config_destroy(LinphoneProxyConfig *obj){
67         if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
68         if (obj->reg_identity!=NULL) ms_free(obj->reg_identity);
69         if (obj->reg_route!=NULL) ms_free(obj->reg_route);
70         if (obj->ssctx!=NULL) sip_setup_context_free(obj->ssctx);
71         if (obj->realm!=NULL) ms_free(obj->realm);
72         if (obj->type!=NULL) ms_free(obj->type);
73         if (obj->dial_prefix!=NULL) ms_free(obj->dial_prefix);
74         if (obj->op) sal_op_release(obj->op);
75 }
76
77 /**
78  * Returns a boolean indicating that the user is sucessfully registered on the proxy.
79 **/
80 bool_t linphone_proxy_config_is_registered(const LinphoneProxyConfig *obj){
81         return obj->registered;
82 }
83
84 /**
85  * Sets the proxy address
86  *
87  * Examples of valid sip proxy address are:
88  * - IP address: sip:87.98.157.38
89  * - IP address with port: sip:87.98.157.38:5062
90  * - hostnames : sip:sip.example.net
91 **/
92 int linphone_proxy_config_set_server_addr(LinphoneProxyConfig *obj, const char *server_addr){
93         LinphoneAddress *addr=NULL;
94         char *modified=NULL;
95         
96         if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
97         obj->reg_proxy=NULL;
98         
99         if (server_addr!=NULL && strlen(server_addr)>0){
100                 if (strstr(server_addr,"sip:")==NULL){
101                         modified=ms_strdup_printf("sip:%s",server_addr);
102                         addr=linphone_address_new(modified);
103                         ms_free(modified);
104                 }
105                 if (addr==NULL)
106                         addr=linphone_address_new(server_addr);
107                 if (addr){
108                         obj->reg_proxy=linphone_address_as_string_uri_only(addr);
109                         linphone_address_destroy(addr);
110                 }else{
111                         ms_warning("Could not parse %s",server_addr);
112                         return -1;
113                 }
114         }
115         return 0;
116 }
117
118 /**
119  * Sets the user identity as a SIP address.
120  *
121  * This identity is normally formed with display name, username and domain, such 
122  * as:
123  * Alice <sip:alice@example.net>
124  * The REGISTER messages will have from and to set to this identity.
125  *
126 **/
127 int linphone_proxy_config_set_identity(LinphoneProxyConfig *obj, const char *identity){
128         LinphoneAddress *addr;
129         if (identity!=NULL && strlen(identity)>0){
130                 addr=linphone_address_new(identity);
131                 if (!addr || linphone_address_get_username(addr)==NULL){
132                         ms_warning("Invalid sip identity: %s",identity);
133                         if (addr)
134                                 linphone_address_destroy(addr);
135                         return -1;
136                 }else{
137                         if (obj->reg_identity!=NULL) {
138                                 ms_free(obj->reg_identity);
139                                 obj->reg_identity=NULL;
140                         }
141                         obj->reg_identity=ms_strdup(identity);
142                         if (obj->realm){
143                                 ms_free(obj->realm);
144                         }
145                         obj->realm=ms_strdup(linphone_address_get_domain(addr));
146                         linphone_address_destroy(addr);
147                         return 0;
148                 }
149         }
150         return -1;
151 }
152
153 const char *linphone_proxy_config_get_domain(const LinphoneProxyConfig *cfg){
154         return cfg->realm;
155 }
156
157 /**
158  * Sets a SIP route.
159  * When a route is set, all outgoing calls will go to the route's destination if this proxy
160  * is the default one (see linphone_core_set_default_proxy() ).
161 **/
162 int linphone_proxy_config_set_route(LinphoneProxyConfig *obj, const char *route)
163 {
164         if (obj->reg_route!=NULL){
165                 ms_free(obj->reg_route);
166                 obj->reg_route=NULL;
167         }
168         if (route!=NULL){
169                 /*try to prepend 'sip:' */
170                 if (strstr(route,"sip:")==NULL){
171                         obj->reg_route=ms_strdup_printf("sip:%s",route);
172                 }else obj->reg_route=ms_strdup(route);
173         }
174         return 0;
175 }
176
177 bool_t linphone_proxy_config_check(LinphoneCore *lc, LinphoneProxyConfig *obj){
178         if (obj->reg_proxy==NULL){
179                 if (lc->vtable.display_warning)
180                         lc->vtable.display_warning(lc,_("The sip proxy address you entered is invalid, it must start with \"sip:\""
181                                                 " followed by a hostname."));
182                 return FALSE;
183         }
184         if (obj->reg_identity==NULL){
185                 if (lc->vtable.display_warning)
186                         lc->vtable.display_warning(lc,_("The sip identity you entered is invalid.\nIt should look like "
187                                         "sip:username@proxydomain, such as sip:alice@example.net"));
188                 return FALSE;
189         }
190         return TRUE;
191 }
192
193 /**
194  * Indicates whether a REGISTER request must be sent to the proxy.
195 **/
196 void linphone_proxy_config_enableregister(LinphoneProxyConfig *obj, bool_t val){
197         obj->reg_sendregister=val;
198 }
199
200 /**
201  * Sets the registration expiration time in seconds.
202 **/
203 void linphone_proxy_config_expires(LinphoneProxyConfig *obj, int val){
204         if (val<=0) val=600;
205         obj->expires=val;
206 }
207
208 void linphone_proxy_config_enable_publish(LinphoneProxyConfig *obj, bool_t val){
209         obj->publish=val;
210 }
211
212 /**
213  * Starts editing a proxy configuration.
214  *
215  * Because proxy configuration must be consistent, applications MUST
216  * call linphone_proxy_config_edit() before doing any attempts to modify
217  * proxy configuration (such as identity, proxy address and so on).
218  * Once the modifications are done, then the application must call
219  * linphone_proxy_config_done() to commit the changes.
220 **/
221 void linphone_proxy_config_edit(LinphoneProxyConfig *obj){
222         if (obj->reg_sendregister){
223                 /* unregister */
224                 if (obj->registered) {
225                         sal_unregister(obj->op);
226                         obj->registered=FALSE;
227                 }
228         }
229 }
230
231 void linphone_proxy_config_apply(LinphoneProxyConfig *obj,LinphoneCore *lc)
232 {
233         obj->lc=lc;
234         linphone_proxy_config_done(obj);
235 }
236
237 static char *guess_contact_for_register(LinphoneProxyConfig *obj){
238         LinphoneAddress *proxy=linphone_address_new(obj->reg_proxy);
239         char *ret=NULL;
240         const char *host;
241         if (proxy==NULL) return NULL;
242         host=linphone_address_get_domain (proxy);
243         if (host!=NULL){
244                 LinphoneAddress *contact;
245                 char localip[LINPHONE_IPADDR_SIZE];
246                 
247                 linphone_core_get_local_ip(obj->lc,host,localip);
248                 contact=linphone_address_new(obj->reg_identity);
249                 linphone_address_set_domain (contact,localip);
250                 linphone_address_set_port_int(contact,linphone_core_get_sip_port(obj->lc));
251                 linphone_address_set_display_name(contact,NULL);
252                 LCSipTransports tr;
253                 linphone_core_get_sip_transports(obj->lc,&tr);
254                 if (tr.udp_port <= 0 && tr.tcp_port>0) {
255                         sal_address_add_param(contact,"transport","tcp");
256                 }
257                 ret=linphone_address_as_string(contact);
258                 linphone_address_destroy(contact);
259         }
260         linphone_address_destroy (proxy);
261         return ret;
262 }
263
264 static void linphone_proxy_config_register(LinphoneProxyConfig *obj){
265         const char *id_str;
266
267         if (obj->reg_identity!=NULL) id_str=obj->reg_identity;
268         else id_str=linphone_core_get_primary_contact(obj->lc);
269         if (obj->reg_sendregister){
270                 char *contact;
271                 if (obj->op)
272                         sal_op_release(obj->op);
273                 obj->op=sal_op_new(obj->lc->sal);
274                 contact=guess_contact_for_register(obj);
275                 sal_op_set_contact(obj->op,contact);
276                 ms_free(contact);
277                 sal_op_set_user_pointer(obj->op,obj);
278                 if (sal_register(obj->op,obj->reg_proxy,obj->reg_identity,obj->expires)==0) {
279                         linphone_proxy_config_set_state(obj,LinphoneRegistrationProgress,"Registration in progress");
280                 } else {
281                         linphone_proxy_config_set_state(obj,LinphoneRegistrationFailed,"Registration failed");
282                 }
283         }
284 }
285
286
287 /**
288  * Sets a dialing prefix to be automatically prepended when inviting a number with 
289  * #linphone_core_invite.
290  *
291 **/
292 void linphone_proxy_config_set_dial_prefix(LinphoneProxyConfig *cfg, const char *prefix){
293         if (cfg->dial_prefix!=NULL){
294                 ms_free(cfg->dial_prefix);
295                 cfg->dial_prefix=NULL;
296         }
297         if (prefix && prefix[0]!='\0') cfg->dial_prefix=ms_strdup(prefix);
298 }
299
300 /**
301  * Returns dialing prefix.
302  *
303  * 
304 **/
305 const char *linphone_proxy_config_get_dial_prefix(const LinphoneProxyConfig *cfg){
306         return cfg->dial_prefix;
307 }
308
309 /**
310  * Sets whether liblinphone should replace "+" by "00" in dialed numbers (passed to
311  * #linphone_core_invite ).
312  *
313 **/
314 void linphone_proxy_config_set_dial_escape_plus(LinphoneProxyConfig *cfg, bool_t val){
315         cfg->dial_escape_plus=val;
316 }
317
318 /**
319  * Returns whether liblinphone should replace "+" by "00" in dialed numbers (passed to
320  * #linphone_core_invite ).
321  *
322 **/
323 bool_t linphone_proxy_config_get_dial_escape_plus(const LinphoneProxyConfig *cfg){
324         return cfg->dial_escape_plus;
325 }
326
327
328 static bool_t is_a_phone_number(const char *username){
329         const char *p;
330         for(p=username;*p!='\0';++p){
331                 if (isdigit(*p) || 
332                     *p==' ' ||
333                     *p=='-' ||
334                     *p==')' ||
335                         *p=='(' ||
336                         *p=='/' ||
337                         *p=='+') continue;
338                 else return FALSE;
339         }
340         return TRUE;
341 }
342
343 static char *flatten_number(const char *number){
344         char *result=ms_malloc0(strlen(number)+1);
345         char *w=result;
346         const char *r;
347         for(r=number;*r!='\0';++r){
348                 if (*r=='+' || isdigit(*r)){
349                         *w++=*r;
350                 }
351         }
352         *w++='\0';
353         return result;
354 }
355
356 static void copy_result(const char *src, char *dest, size_t destlen, bool_t escape_plus){
357         int i=0;
358         
359         if (escape_plus && src[0]=='+' && destlen>2){
360                 dest[0]='0';
361                 dest[1]='0';
362                 src++;
363                 i=2;
364         }
365         
366         for(;(i<destlen-1) && *src!='\0';++i){
367                 dest[i]=*src;
368                 src++;
369         }
370         dest[i]='\0';
371 }
372
373
374 static char *append_prefix(const char *number, const char *prefix){
375         char *res=ms_malloc(strlen(number)+strlen(prefix)+1);
376         strcpy(res,prefix);
377         return strcat(res,number);
378 }
379
380 int linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){
381         char *flatten;
382         int numlen;
383         if (is_a_phone_number(username)){
384                 flatten=flatten_number(username);
385                 ms_message("Flattened number is '%s'",flatten);
386                 numlen=strlen(flatten);
387                 if (numlen>10 || flatten[0]=='+' || proxy->dial_prefix==NULL || proxy->dial_prefix[0]=='\0'){
388                         ms_message("No need to add a prefix");
389                         /* prefix is already there */
390                         copy_result(flatten,result,result_len,proxy->dial_escape_plus);
391                         ms_free(flatten);
392                         return 0;
393                 }else if (proxy->dial_prefix && proxy->dial_prefix[0]!='\0'){
394                         char *prefixed;
395                         int skipped=0;
396                         ms_message("Need to prefix with %s",proxy->dial_prefix);
397                         if (numlen==10){
398                                 /*remove initial number before prepending prefix*/
399                                 skipped=1;
400                         }
401                         prefixed=append_prefix(flatten+skipped,proxy->dial_prefix);
402                         ms_free(flatten);
403                         copy_result(prefixed,result,result_len,proxy->dial_escape_plus);
404                         ms_free(prefixed);
405                 }
406         }else strncpy(result,username,result_len);
407         return 0;
408 }
409
410 /**
411  * Commits modification made to the proxy configuration.
412 **/
413 int linphone_proxy_config_done(LinphoneProxyConfig *obj)
414 {
415         if (!linphone_proxy_config_check(obj->lc,obj)) return -1;
416         obj->commit=TRUE;
417         linphone_proxy_config_write_all_to_config_file(obj->lc);
418         return 0;
419 }
420
421 void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
422 {
423         if (cfg->realm!=NULL) {
424                 ms_free(cfg->realm);
425                 cfg->realm=NULL;
426         }
427         if (realm!=NULL) cfg->realm=ms_strdup(realm);
428 }
429
430 int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
431                                LinphoneOnlineStatus presence_mode){
432         int err;
433         SalOp *op=sal_op_new(proxy->lc->sal);
434         err=sal_publish(op,linphone_proxy_config_get_identity(proxy),
435             linphone_proxy_config_get_identity(proxy),linphone_online_status_to_sal(presence_mode));
436         sal_op_release(op);
437         return err;
438 }
439
440 /**
441  * Returns the route set for this proxy configuration.
442 **/
443 const char *linphone_proxy_config_get_route(const LinphoneProxyConfig *obj){
444         return obj->reg_route;
445 }
446
447 /**
448  * Returns the SIP identity that belongs to this proxy configuration.
449  *
450  * The SIP identity is a SIP address (Display Name <sip:username@domain> )
451 **/
452 const char *linphone_proxy_config_get_identity(const LinphoneProxyConfig *obj){
453         return obj->reg_identity;
454 }
455
456 /**
457  * Returns TRUE if PUBLISH request is enabled for this proxy.
458 **/
459 bool_t linphone_proxy_config_publish_enabled(const LinphoneProxyConfig *obj){
460         return obj->publish;
461 }
462
463 /**
464  * Returns the proxy's SIP address.
465 **/
466 const char *linphone_proxy_config_get_addr(const LinphoneProxyConfig *obj){
467         return obj->reg_proxy;
468 }
469
470 /**
471  * Returns the duration of registration.
472 **/
473 int linphone_proxy_config_get_expires(const LinphoneProxyConfig *obj){
474         return obj->expires;
475 }
476
477 /**
478  * Returns TRUE if registration to the proxy is enabled.
479 **/
480 bool_t linphone_proxy_config_register_enabled(const LinphoneProxyConfig *obj){
481         return obj->reg_sendregister;
482 }
483
484 struct _LinphoneCore * linphone_proxy_config_get_core(const LinphoneProxyConfig *obj){
485         return obj->lc;
486 }
487
488 /**
489  * Add a proxy configuration.
490  * This will start registration on the proxy, if registration is enabled.
491 **/
492 int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
493         if (!linphone_proxy_config_check(lc,cfg)) return -1;
494         if (ms_list_find(lc->sip_conf.proxies,cfg)!=NULL){
495                 ms_warning("ProxyConfig already entered, ignored.");
496                 return 0;
497         }
498         lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)cfg);
499         linphone_proxy_config_apply(cfg,lc);
500         return 0;
501 }
502
503 /**
504  * Removes a proxy configuration.
505  *
506  * LinphoneCore will then automatically unregister and place the proxy configuration
507  * on a deleted list. For that reason, a removed proxy does NOT need to be freed.
508 **/
509 void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
510         lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
511         /* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
512         lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
513         cfg->deletion_date=ms_time(NULL);
514         /* this will unREGISTER */
515         linphone_proxy_config_edit(cfg);
516         if (lc->default_proxy==cfg){
517                 lc->default_proxy=NULL;
518         }
519 }
520 /**
521  * Erase all proxies from config.
522  *
523  * @ingroup proxy
524 **/
525 void linphone_core_clear_proxy_config(LinphoneCore *lc){
526         MSList* list=ms_list_copy(linphone_core_get_proxy_config_list((const LinphoneCore*)lc));
527         for(;list!=NULL;list=list->next){
528                 linphone_core_remove_proxy_config(lc,(LinphoneProxyConfig *)list->data);
529         }
530         ms_list_free(list);
531 }
532 /**
533  * Sets the default proxy.
534  *
535  * This default proxy must be part of the list of already entered LinphoneProxyConfig.
536  * Toggling it as default will make LinphoneCore use the identity associated with
537  * the proxy configuration in all incoming and outgoing calls.
538 **/
539 void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *config){
540         /* check if this proxy is in our list */
541         if (config!=NULL){
542                 if (ms_list_find(lc->sip_conf.proxies,config)==NULL){
543                         ms_warning("Bad proxy address: it is not in the list !");
544                         lc->default_proxy=NULL;
545                         return ;
546                 }
547         }
548         lc->default_proxy=config;
549         
550 }       
551
552 void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
553         if (index<0) linphone_core_set_default_proxy(lc,NULL);
554         else linphone_core_set_default_proxy(lc,ms_list_nth_data(lc->sip_conf.proxies,index));
555 }
556
557 /**
558  * Returns the default proxy configuration, that is the one used to determine the current identity.
559 **/
560 int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
561         int pos=-1;
562         if (config!=NULL) *config=lc->default_proxy;
563         if (lc->default_proxy!=NULL){
564                 pos=ms_list_position(lc->sip_conf.proxies,ms_list_find(lc->sip_conf.proxies,(void *)lc->default_proxy));
565         }
566         return pos;
567 }
568
569 /**
570  * Returns an unmodifiable list of entered proxy configurations.
571 **/
572 const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
573         return lc->sip_conf.proxies;
574 }
575
576 void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *obj, int index)
577 {
578         char key[50];
579
580         sprintf(key,"proxy_%i",index);
581         lp_config_clean_section(config,key);
582         if (obj==NULL){
583                 return;
584         }
585         if (obj->type!=NULL){
586                 lp_config_set_string(config,key,"type",obj->type);
587         }
588         if (obj->reg_proxy!=NULL){
589                 lp_config_set_string(config,key,"reg_proxy",obj->reg_proxy);
590         }
591         if (obj->reg_route!=NULL){
592                 lp_config_set_string(config,key,"reg_route",obj->reg_route);
593         }
594         if (obj->reg_identity!=NULL){
595                 lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
596         }
597         lp_config_set_int(config,key,"reg_expires",obj->expires);
598         lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
599         lp_config_set_int(config,key,"publish",obj->publish);
600         lp_config_set_int(config,key,"dial_escape_plus",obj->dial_escape_plus);
601         lp_config_set_string(config,key,"dial_prefix",obj->dial_prefix);
602 }
603
604
605
606 LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config, int index)
607 {
608         const char *tmp;
609         const char *identity;
610         const char *proxy;
611         LinphoneProxyConfig *cfg;
612         char key[50];
613         
614         sprintf(key,"proxy_%i",index);
615
616         if (!lp_config_has_section(config,key)){
617                 return NULL;
618         }
619
620         cfg=linphone_proxy_config_new();
621
622         identity=lp_config_get_string(config,key,"reg_identity",NULL);  
623         proxy=lp_config_get_string(config,key,"reg_proxy",NULL);
624         
625         linphone_proxy_config_set_identity(cfg,identity);
626         linphone_proxy_config_set_server_addr(cfg,proxy);
627         
628         tmp=lp_config_get_string(config,key,"reg_route",NULL);
629         if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
630
631         linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",600));
632         linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
633         
634         linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
635
636         linphone_proxy_config_set_dial_escape_plus(cfg,lp_config_get_int(config,key,"dial_escape_plus",0));
637         linphone_proxy_config_set_dial_prefix(cfg,lp_config_get_string(config,key,"dial_prefix",NULL));
638         
639         tmp=lp_config_get_string(config,key,"type",NULL);
640         if (tmp!=NULL && strlen(tmp)>0) 
641                 linphone_proxy_config_set_sip_setup(cfg,tmp);
642
643         return cfg;
644 }
645
646 static void linphone_proxy_config_activate_sip_setup(LinphoneProxyConfig *cfg){
647         SipSetupContext *ssc;
648         SipSetup *ss=sip_setup_lookup(cfg->type);
649         LinphoneCore *lc=linphone_proxy_config_get_core(cfg);
650         unsigned int caps;
651         if (!ss) return ;
652         ssc=sip_setup_context_new(ss,cfg);
653         cfg->ssctx=ssc;
654         if (cfg->reg_identity==NULL){
655                 ms_error("Invalid identity for this proxy configuration.");
656                 return;
657         }
658         caps=sip_setup_context_get_capabilities(ssc);
659         if (caps & SIP_SETUP_CAP_ACCOUNT_MANAGER){
660                 if (sip_setup_context_login_account(ssc,cfg->reg_identity,NULL)!=0){
661                         if (lc->vtable.display_warning){
662                                 char *tmp=ms_strdup_printf(_("Could not login as %s"),cfg->reg_identity);
663                                 lc->vtable.display_warning(lc,tmp);
664                                 ms_free(tmp);
665                         }
666                         return;
667                 }
668         }
669         if (caps & SIP_SETUP_CAP_PROXY_PROVIDER){
670                 char proxy[256];
671                 if (sip_setup_context_get_proxy(ssc,NULL,proxy,sizeof(proxy))==0){
672                         linphone_proxy_config_set_server_addr(cfg,proxy);
673                 }else{
674                         ms_error("Could not retrieve proxy uri !");
675                 }
676         }
677         
678 }
679
680 SipSetup *linphone_proxy_config_get_sip_setup(LinphoneProxyConfig *cfg){
681         if (cfg->ssctx!=NULL) return cfg->ssctx->funcs;
682         if (cfg->type!=NULL){
683                 return sip_setup_lookup(cfg->type);
684         }
685         return NULL;
686 }
687
688 void linphone_proxy_config_update(LinphoneProxyConfig *cfg){
689         LinphoneCore *lc=cfg->lc;
690         if (cfg->commit){
691                 if (cfg->type && cfg->ssctx==NULL){
692                         linphone_proxy_config_activate_sip_setup(cfg);
693                 }
694                 if (lc->sip_conf.register_only_when_network_is_up || lc->network_reachable)
695                         linphone_proxy_config_register(cfg);
696                 cfg->commit=FALSE;
697         }
698 }
699
700 void linphone_proxy_config_set_sip_setup(LinphoneProxyConfig *cfg, const char *type){
701         if (cfg->type)
702                 ms_free(cfg->type);
703         cfg->type=ms_strdup(type);
704         if (linphone_proxy_config_get_addr(cfg)==NULL){
705                 /*put a placeholder so that the sip setup gets saved into the config */
706                 linphone_proxy_config_set_server_addr(cfg,"sip:undefined");
707         }
708 }
709
710 SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig *cfg){
711         return cfg->ssctx;
712 }
713
714 /**
715  * @}
716 **/
717
718 LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core, const char *type){
719         LinphoneAccountCreator *obj;
720         LinphoneProxyConfig *cfg;
721         SipSetup *ss=sip_setup_lookup(type);
722         SipSetupContext *ssctx;
723         if (!ss){
724                 return NULL;
725         }
726         if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
727                 ms_error("%s cannot manage accounts.");
728                 return NULL;
729         }
730         obj=ms_new0(LinphoneAccountCreator,1);
731         cfg=linphone_proxy_config_new();
732         ssctx=sip_setup_context_new(ss,cfg);
733         obj->lc=core;
734         obj->ssctx=ssctx;
735         set_string(&obj->domain,sip_setup_context_get_domains(ssctx)[0]);
736         cfg->lc=core;
737         return obj;
738 }
739
740 void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username){
741         set_string(&obj->username,username);
742 }
743
744 void linphone_account_creator_set_password(LinphoneAccountCreator *obj, const char *password){
745         set_string(&obj->password,password);
746 }
747
748 void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char *domain){
749         set_string(&obj->domain,domain);
750 }
751
752 const char * linphone_account_creator_get_username(LinphoneAccountCreator *obj){
753         return obj->username;
754 }
755
756 const char * linphone_account_creator_get_domain(LinphoneAccountCreator *obj){
757         return obj->domain;
758 }
759
760 int linphone_account_creator_test_existence(LinphoneAccountCreator *obj){
761         SipSetupContext *ssctx=obj->ssctx;
762         char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
763         int err=sip_setup_context_account_exists(ssctx,uri);
764         ms_free(uri);
765         return err;
766 }
767
768 LinphoneProxyConfig * linphone_account_creator_validate(LinphoneAccountCreator *obj){
769         SipSetupContext *ssctx=obj->ssctx;
770         char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
771         int err=sip_setup_context_create_account(ssctx,uri,obj->password);
772         ms_free(uri);
773         if (err==0) {
774                 obj->succeeded=TRUE;
775                 return sip_setup_context_get_proxy_config(ssctx);
776         }
777         return NULL;
778 }
779
780 void linphone_account_creator_destroy(LinphoneAccountCreator *obj){
781         if (obj->username)
782                 ms_free(obj->username);
783         if (obj->password)
784                 ms_free(obj->password);
785         if (obj->domain)
786                 ms_free(obj->domain);
787         if (!obj->succeeded){
788                 linphone_proxy_config_destroy(sip_setup_context_get_proxy_config(obj->ssctx));
789         }
790 }
791
792 void linphone_proxy_config_set_user_data(LinphoneProxyConfig *cr, void * ud) {
793         cr->user_data=ud;
794 }
795
796 void * linphone_proxy_config_get_user_data(LinphoneProxyConfig *cr) {
797         return cr->user_data;
798 }
799
800 void linphone_proxy_config_set_state(LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const char *message){
801         LinphoneCore *lc=cfg->lc;
802         if (lc && lc->vtable.registration_state_changed){
803                 lc->vtable.registration_state_changed(lc,cfg,state,message);
804         }
805 }
806
807
808