]> sjero.net Git - linphone/blob - coreapi/proxy.c
add access to country code from iso name
[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         const char* iso_country_code; /* ISO 3166-1 alpha-2 code, ex: FR for France*/
379         char  ccc[8]; /*country calling code*/
380         int nnl; /*maximum national number length*/
381         const char * icp; /*international call prefix, ex: 00 in europe*/
382         
383 }dial_plan_t;
384
385 /* TODO: fill with information for all countries over the world*/
386 static dial_plan_t const dial_plans[]={
387         {"France"                       ,"FR"           , "33"  , 9             , "00"  },
388         {"United States"        ,"US"           , "1"   , 10    , "011" },
389         {"Turkey"                       ,"TR"           , "90"  , 10    , "00"  },
390         {"Switzerland"          ,"XK"           , "41"  , 9             , "00"  },
391         {NULL           ,NULL,""        , 0     , NULL  }
392 };
393
394 static dial_plan_t most_common_dialplan={ "generic" ,"", "", 10, "00"};
395
396 int linphone_dial_plan_lookup_ccc_from_iso(const char* iso) {
397         dial_plan_t* dial_plan;
398         for (dial_plan=(dial_plan_t*)dial_plans; dial_plan->country!=NULL; dial_plan++) {
399                 if (strcmp(iso, dial_plan->iso_country_code)==0) {
400                         return atoi(dial_plan->ccc);
401                 }
402         }
403         return -1;
404 }
405
406 static void lookup_dial_plan(const char *ccc, dial_plan_t *plan){
407         int i;
408         for(i=0;dial_plans[i].country!=NULL;++i){
409                 if (strcmp(ccc,dial_plans[i].ccc)==0){
410                         *plan=dial_plans[i];
411                         return;
412                 }
413         }
414         /*else return a generic "most common" dial plan*/
415         *plan=most_common_dialplan;
416         strcpy(plan->ccc,ccc);
417 }
418
419 static bool_t is_a_phone_number(const char *username){
420         const char *p;
421         for(p=username;*p!='\0';++p){
422                 if (isdigit(*p) || 
423                     *p==' ' ||
424                     *p=='.' ||
425                     *p=='-' ||
426                     *p==')' ||
427                         *p=='(' ||
428                         *p=='/' ||
429                         *p=='+') continue;
430                 else return FALSE;
431         }
432         return TRUE;
433 }
434
435 static char *flatten_number(const char *number){
436         char *result=ms_malloc0(strlen(number)+1);
437         char *w=result;
438         const char *r;
439         for(r=number;*r!='\0';++r){
440                 if (*r=='+' || isdigit(*r)){
441                         *w++=*r;
442                 }
443         }
444         *w++='\0';
445         return result;
446 }
447
448 static void replace_plus(const char *src, char *dest, size_t destlen, const char *icp){
449         int i=0;
450         
451         if (icp && src[0]=='+' && (destlen>(i=strlen(icp))) ){
452                 src++;
453                 strcpy(dest,icp);
454         }
455         
456         for(;(i<destlen-1) && *src!='\0';++i){
457                 dest[i]=*src;
458                 src++;
459         }
460         dest[i]='\0';
461 }
462
463
464 int linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){
465         int numlen;
466         if (is_a_phone_number(username)){
467                 char *flatten;
468                 flatten=flatten_number(username);
469                 ms_message("Flattened number is '%s'",flatten);
470                 
471                 if (proxy->dial_prefix==NULL || proxy->dial_prefix[0]=='\0'){
472                         /*no prefix configured, nothing else to do*/
473                         strncpy(result,flatten,result_len);
474                         ms_free(flatten);
475                         return 0;
476                 }else{
477                         dial_plan_t dialplan;
478                         lookup_dial_plan(proxy->dial_prefix,&dialplan);
479                         ms_message("Using dialplan '%s'",dialplan.country);
480                         if (flatten[0]=='+' || strstr(flatten,dialplan.icp)==flatten){
481                                 /* the number has international prefix or +, so nothing to do*/
482                                 ms_message("Prefix already present.");
483                                 /*eventually replace the plus*/
484                                 replace_plus(flatten,result,result_len,proxy->dial_escape_plus ? dialplan.icp : NULL);
485                                 ms_free(flatten);
486                                 return 0;
487                         }else{
488                                 int i=0;
489                                 int skip;
490                                 numlen=strlen(flatten);
491                                 /*keep at most national number significant digits */
492                                 skip=numlen-dialplan.nnl;
493                                 if (skip<0) skip=0;
494                                 /*first prepend internation calling prefix or +*/
495                                 if (proxy->dial_escape_plus){
496                                         strncpy(result,dialplan.icp,result_len);
497                                         i+=strlen(dialplan.icp);
498                                 }else{
499                                         strncpy(result,"+",result_len);
500                                         i+=1;
501                                 }
502                                 /*add prefix*/
503                                 if (result_len-i>strlen(dialplan.ccc)){
504                                         strcpy(result+i,dialplan.ccc);
505                                         i+=strlen(dialplan.ccc);
506                                 }
507                                 /*add user digits */
508                                 strncpy(result+i,flatten+skip,result_len-i-1);
509                                 ms_free(flatten);
510                         }
511                 }
512         }else strncpy(result,username,result_len);
513         return 0;
514 }
515
516 /**
517  * Commits modification made to the proxy configuration.
518 **/
519 int linphone_proxy_config_done(LinphoneProxyConfig *obj)
520 {
521         if (!linphone_proxy_config_check(obj->lc,obj)) return -1;
522         obj->commit=TRUE;
523         linphone_proxy_config_write_all_to_config_file(obj->lc);
524         return 0;
525 }
526
527 void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
528 {
529         if (cfg->realm!=NULL) {
530                 ms_free(cfg->realm);
531                 cfg->realm=NULL;
532         }
533         if (realm!=NULL) cfg->realm=ms_strdup(realm);
534 }
535
536 int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
537                                LinphoneOnlineStatus presence_mode){
538         int err;
539         SalOp *op=sal_op_new(proxy->lc->sal);
540         err=sal_publish(op,linphone_proxy_config_get_identity(proxy),
541             linphone_proxy_config_get_identity(proxy),linphone_online_status_to_sal(presence_mode));
542         if (proxy->publish_op!=NULL)
543                 sal_op_release(proxy->publish_op);
544         proxy->publish_op=op;
545         return err;
546 }
547
548 /**
549  * Returns the route set for this proxy configuration.
550 **/
551 const char *linphone_proxy_config_get_route(const LinphoneProxyConfig *obj){
552         return obj->reg_route;
553 }
554
555 /**
556  * Returns the SIP identity that belongs to this proxy configuration.
557  *
558  * The SIP identity is a SIP address (Display Name <sip:username@@domain> )
559 **/
560 const char *linphone_proxy_config_get_identity(const LinphoneProxyConfig *obj){
561         return obj->reg_identity;
562 }
563
564 /**
565  * Returns TRUE if PUBLISH request is enabled for this proxy.
566 **/
567 bool_t linphone_proxy_config_publish_enabled(const LinphoneProxyConfig *obj){
568         return obj->publish;
569 }
570
571 /**
572  * Returns the proxy's SIP address.
573 **/
574 const char *linphone_proxy_config_get_addr(const LinphoneProxyConfig *obj){
575         return obj->reg_proxy;
576 }
577
578 /**
579  * Returns the duration of registration.
580 **/
581 int linphone_proxy_config_get_expires(const LinphoneProxyConfig *obj){
582         return obj->expires;
583 }
584
585 /**
586  * Returns TRUE if registration to the proxy is enabled.
587 **/
588 bool_t linphone_proxy_config_register_enabled(const LinphoneProxyConfig *obj){
589         return obj->reg_sendregister;
590 }
591
592 /**
593  * Set optional contact parameters that will be added to the contact information sent in the registration.
594  * @param obj the proxy config object
595  * @param contact_params a string contaning the additional parameters in text form, like "myparam=something;myparam2=something_else"
596  *
597  * 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.
598  * 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>.
599 **/
600 void linphone_proxy_config_set_contact_parameters(LinphoneProxyConfig *obj, const char *contact_params){
601         if (obj->contact_params) {
602                 ms_free(obj->contact_params);
603                 obj->contact_params=NULL;
604         }
605         if (contact_params){
606                 obj->contact_params=ms_strdup(contact_params);
607         }
608 }
609
610 /**
611  * Returns previously set contact parameters.
612 **/
613 const char *linphone_proxy_config_get_contact_parameters(const LinphoneProxyConfig *obj){
614         return obj->contact_params;
615 }
616
617 struct _LinphoneCore * linphone_proxy_config_get_core(const LinphoneProxyConfig *obj){
618         return obj->lc;
619 }
620
621 /**
622  * Add a proxy configuration.
623  * This will start registration on the proxy, if registration is enabled.
624 **/
625 int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
626         if (!linphone_proxy_config_check(lc,cfg)) {
627                 return -1;
628         }
629         if (ms_list_find(lc->sip_conf.proxies,cfg)!=NULL){
630                 ms_warning("ProxyConfig already entered, ignored.");
631                 return 0;
632         }
633         lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)cfg);
634         linphone_proxy_config_apply(cfg,lc);
635         return 0;
636 }
637
638 /**
639  * Removes a proxy configuration.
640  *
641  * LinphoneCore will then automatically unregister and place the proxy configuration
642  * on a deleted list. For that reason, a removed proxy does NOT need to be freed.
643 **/
644 void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
645         /* check this proxy config is in the list before doing more*/
646         if (ms_list_find(lc->sip_conf.proxies,cfg)==NULL){
647                 ms_error("linphone_core_remove_proxy_config: LinphoneProxyConfig %p is not known by LinphoneCore (programming error?)",cfg);
648                 return;
649         }
650         lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
651         /* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
652         lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
653         cfg->deletion_date=ms_time(NULL);
654         if (cfg->state==LinphoneRegistrationOk){
655                 /* this will unREGISTER */
656                 linphone_proxy_config_edit(cfg);
657         }
658         if (lc->default_proxy==cfg){
659                 lc->default_proxy=NULL;
660         }
661         linphone_proxy_config_write_all_to_config_file(lc);
662 }
663 /**
664  * Erase all proxies from config.
665  *
666  * @ingroup proxy
667 **/
668 void linphone_core_clear_proxy_config(LinphoneCore *lc){
669         MSList* list=ms_list_copy(linphone_core_get_proxy_config_list((const LinphoneCore*)lc));
670         MSList* copy=list;
671         for(;list!=NULL;list=list->next){
672                 linphone_core_remove_proxy_config(lc,(LinphoneProxyConfig *)list->data);
673         }
674         ms_list_free(copy);
675         linphone_proxy_config_write_all_to_config_file(lc);
676 }
677 /**
678  * Sets the default proxy.
679  *
680  * This default proxy must be part of the list of already entered LinphoneProxyConfig.
681  * Toggling it as default will make LinphoneCore use the identity associated with
682  * the proxy configuration in all incoming and outgoing calls.
683 **/
684 void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *config){
685         /* check if this proxy is in our list */
686         if (config!=NULL){
687                 if (ms_list_find(lc->sip_conf.proxies,config)==NULL){
688                         ms_warning("Bad proxy address: it is not in the list !");
689                         lc->default_proxy=NULL;
690                         return ;
691                 }
692         }
693         lc->default_proxy=config;
694         if (linphone_core_ready(lc))
695                 lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
696 }       
697
698 void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
699         if (index<0) linphone_core_set_default_proxy(lc,NULL);
700         else linphone_core_set_default_proxy(lc,ms_list_nth_data(lc->sip_conf.proxies,index));
701 }
702
703 /**
704  * Returns the default proxy configuration, that is the one used to determine the current identity.
705 **/
706 int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
707         int pos=-1;
708         if (config!=NULL) *config=lc->default_proxy;
709         if (lc->default_proxy!=NULL){
710                 pos=ms_list_position(lc->sip_conf.proxies,ms_list_find(lc->sip_conf.proxies,(void *)lc->default_proxy));
711         }
712         return pos;
713 }
714
715 /**
716  * Returns an unmodifiable list of entered proxy configurations.
717 **/
718 const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
719         return lc->sip_conf.proxies;
720 }
721
722 void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *obj, int index)
723 {
724         char key[50];
725
726         sprintf(key,"proxy_%i",index);
727         lp_config_clean_section(config,key);
728         if (obj==NULL){
729                 return;
730         }
731         if (obj->type!=NULL){
732                 lp_config_set_string(config,key,"type",obj->type);
733         }
734         if (obj->reg_proxy!=NULL){
735                 lp_config_set_string(config,key,"reg_proxy",obj->reg_proxy);
736         }
737         if (obj->reg_route!=NULL){
738                 lp_config_set_string(config,key,"reg_route",obj->reg_route);
739         }
740         if (obj->reg_identity!=NULL){
741                 lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
742         }
743         lp_config_set_int(config,key,"reg_expires",obj->expires);
744         lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
745         lp_config_set_int(config,key,"publish",obj->publish);
746         lp_config_set_int(config,key,"dial_escape_plus",obj->dial_escape_plus);
747         lp_config_set_string(config,key,"dial_prefix",obj->dial_prefix);
748 }
749
750
751
752 LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config, int index)
753 {
754         const char *tmp;
755         const char *identity;
756         const char *proxy;
757         LinphoneProxyConfig *cfg;
758         char key[50];
759         
760         sprintf(key,"proxy_%i",index);
761
762         if (!lp_config_has_section(config,key)){
763                 return NULL;
764         }
765
766         cfg=linphone_proxy_config_new();
767
768         identity=lp_config_get_string(config,key,"reg_identity",NULL);  
769         proxy=lp_config_get_string(config,key,"reg_proxy",NULL);
770         
771         linphone_proxy_config_set_identity(cfg,identity);
772         linphone_proxy_config_set_server_addr(cfg,proxy);
773         
774         tmp=lp_config_get_string(config,key,"reg_route",NULL);
775         if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
776
777         linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",DEFAULT_INT(config,reg_expires,600)));
778         linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
779         
780         linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
781
782         linphone_proxy_config_set_dial_escape_plus(cfg,lp_config_get_int(config,key,"dial_escape_plus",DEFAULT_INT(config,dial_escape_plus,0)));
783         linphone_proxy_config_set_dial_prefix(cfg,lp_config_get_string(config,key,"dial_prefix",DEFAULT_STRING(config,dial_prefix,NULL)));
784         
785         tmp=lp_config_get_string(config,key,"type",NULL);
786         if (tmp!=NULL && strlen(tmp)>0) 
787                 linphone_proxy_config_set_sip_setup(cfg,tmp);
788
789         return cfg;
790 }
791
792 static void linphone_proxy_config_activate_sip_setup(LinphoneProxyConfig *cfg){
793         SipSetupContext *ssc;
794         SipSetup *ss=sip_setup_lookup(cfg->type);
795         LinphoneCore *lc=linphone_proxy_config_get_core(cfg);
796         unsigned int caps;
797         if (!ss) return ;
798         ssc=sip_setup_context_new(ss,cfg);
799         cfg->ssctx=ssc;
800         if (cfg->reg_identity==NULL){
801                 ms_error("Invalid identity for this proxy configuration.");
802                 return;
803         }
804         caps=sip_setup_context_get_capabilities(ssc);
805         if (caps & SIP_SETUP_CAP_ACCOUNT_MANAGER){
806                 if (sip_setup_context_login_account(ssc,cfg->reg_identity,NULL)!=0){
807                         if (lc->vtable.display_warning){
808                                 char *tmp=ms_strdup_printf(_("Could not login as %s"),cfg->reg_identity);
809                                 lc->vtable.display_warning(lc,tmp);
810                                 ms_free(tmp);
811                         }
812                         return;
813                 }
814         }
815         if (caps & SIP_SETUP_CAP_PROXY_PROVIDER){
816                 char proxy[256];
817                 if (sip_setup_context_get_proxy(ssc,NULL,proxy,sizeof(proxy))==0){
818                         linphone_proxy_config_set_server_addr(cfg,proxy);
819                 }else{
820                         ms_error("Could not retrieve proxy uri !");
821                 }
822         }
823         
824 }
825
826 SipSetup *linphone_proxy_config_get_sip_setup(LinphoneProxyConfig *cfg){
827         if (cfg->ssctx!=NULL) return cfg->ssctx->funcs;
828         if (cfg->type!=NULL){
829                 return sip_setup_lookup(cfg->type);
830         }
831         return NULL;
832 }
833
834 void linphone_proxy_config_update(LinphoneProxyConfig *cfg){
835         LinphoneCore *lc=cfg->lc;
836         if (cfg->commit){
837                 if (cfg->type && cfg->ssctx==NULL){
838                         linphone_proxy_config_activate_sip_setup(cfg);
839                 }
840                 if (!lc->sip_conf.register_only_when_network_is_up || lc->network_reachable)
841                         linphone_proxy_config_register(cfg);
842                 if (cfg->publish && cfg->publish_op==NULL){
843                         linphone_proxy_config_send_publish(cfg,lc->presence_mode);
844                 }
845                 cfg->commit=FALSE;
846         }
847 }
848
849 void linphone_proxy_config_set_sip_setup(LinphoneProxyConfig *cfg, const char *type){
850         if (cfg->type)
851                 ms_free(cfg->type);
852         cfg->type=ms_strdup(type);
853         if (linphone_proxy_config_get_addr(cfg)==NULL){
854                 /*put a placeholder so that the sip setup gets saved into the config */
855                 linphone_proxy_config_set_server_addr(cfg,"sip:undefined");
856         }
857 }
858
859 SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig *cfg){
860         return cfg->ssctx;
861 }
862
863 /**
864  * @}
865 **/
866
867 LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core, const char *type){
868         LinphoneAccountCreator *obj;
869         LinphoneProxyConfig *cfg;
870         SipSetup *ss=sip_setup_lookup(type);
871         SipSetupContext *ssctx;
872         if (!ss){
873                 return NULL;
874         }
875         if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
876                 ms_error("%s cannot manage accounts.",type);
877                 return NULL;
878         }
879         obj=ms_new0(LinphoneAccountCreator,1);
880         cfg=linphone_proxy_config_new();
881         ssctx=sip_setup_context_new(ss,cfg);
882         obj->lc=core;
883         obj->ssctx=ssctx;
884         set_string(&obj->domain,sip_setup_context_get_domains(ssctx)[0]);
885         cfg->lc=core;
886         return obj;
887 }
888
889 void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username){
890         set_string(&obj->username,username);
891 }
892
893 void linphone_account_creator_set_password(LinphoneAccountCreator *obj, const char *password){
894         set_string(&obj->password,password);
895 }
896
897 void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char *domain){
898         set_string(&obj->domain,domain);
899 }
900
901 void linphone_account_creator_set_route(LinphoneAccountCreator *obj, const char *route) {
902         set_string(&obj->route,route);
903 }
904
905 void linphone_account_creator_set_email(LinphoneAccountCreator *obj, const char *email) {
906         set_string(&obj->email,email);
907 }
908
909 void linphone_account_creator_set_suscribe(LinphoneAccountCreator *obj, int suscribe) {
910         obj->suscribe = suscribe;
911 }
912
913 const char * linphone_account_creator_get_username(LinphoneAccountCreator *obj){
914         return obj->username;
915 }
916
917 const char * linphone_account_creator_get_domain(LinphoneAccountCreator *obj){
918         return obj->domain;
919 }
920
921 int linphone_account_creator_test_existence(LinphoneAccountCreator *obj){
922         SipSetupContext *ssctx=obj->ssctx;
923         char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
924         int err=sip_setup_context_account_exists(ssctx,uri);
925         ms_free(uri);
926         return err;
927 }
928
929 int linphone_account_creator_test_validation(LinphoneAccountCreator *obj) {
930         SipSetupContext *ssctx=obj->ssctx;
931         int err=sip_setup_context_account_validated(ssctx,obj->username);
932         return err;
933 }
934
935 LinphoneProxyConfig * linphone_account_creator_validate(LinphoneAccountCreator *obj){
936         SipSetupContext *ssctx=obj->ssctx;
937         char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
938         int err=sip_setup_context_create_account(ssctx, uri, obj->password, obj->email, obj->suscribe);
939         ms_free(uri);
940         if (err==0) {
941                 obj->succeeded=TRUE;
942                 return sip_setup_context_get_proxy_config(ssctx);
943         }
944         return NULL;
945 }
946
947 void linphone_account_creator_destroy(LinphoneAccountCreator *obj){
948         if (obj->username)
949                 ms_free(obj->username);
950         if (obj->password)
951                 ms_free(obj->password);
952         if (obj->domain)
953                 ms_free(obj->domain);
954         if (!obj->succeeded){
955                 linphone_proxy_config_destroy(sip_setup_context_get_proxy_config(obj->ssctx));
956         }
957 }
958
959 void linphone_proxy_config_set_user_data(LinphoneProxyConfig *cr, void * ud) {
960         cr->user_data=ud;
961 }
962
963 void * linphone_proxy_config_get_user_data(LinphoneProxyConfig *cr) {
964         return cr->user_data;
965 }
966
967 void linphone_proxy_config_set_state(LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const char *message){
968         LinphoneCore *lc=cfg->lc;
969         cfg->state=state;
970         if (lc && lc->vtable.registration_state_changed){
971                 lc->vtable.registration_state_changed(lc,cfg,state,message);
972         }
973 }
974
975 LinphoneRegistrationState linphone_proxy_config_get_state(const LinphoneProxyConfig *cfg){
976         return cfg->state;
977 }
978
979  const char *linphone_registration_state_to_string(LinphoneRegistrationState cs){
980          switch(cs){
981                 case LinphoneRegistrationCleared:
982                          return "LinphoneRegistrationCleared";
983                 break;
984                 case LinphoneRegistrationNone:
985                          return "LinphoneRegistrationNone";
986                 break;
987                 case LinphoneRegistrationProgress:
988                         return "LinphoneRegistrationProgress";
989                 break;
990                 case LinphoneRegistrationOk:
991                          return "LinphoneRegistrationOk";
992                 break;
993                 case LinphoneRegistrationFailed:
994                          return "LinphoneRegistrationFailed";
995                 break;
996          }
997          return NULL;
998  }
999
1000 LinphoneReason linphone_proxy_config_get_error(const LinphoneProxyConfig *cfg) {
1001         return cfg->error;
1002 }
1003
1004 void linphone_proxy_config_set_error(LinphoneProxyConfig *cfg,LinphoneReason error) {
1005         cfg->error = error;
1006 }
1007
1008