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