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