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