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