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