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