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