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