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