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