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