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