3 Copyright (C) 2000 Simon MORLAT (simon.morlat@linphone.org)
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.
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.
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.
21 #include "linphonecore.h"
23 #include <eXosip2/eXosip.h>
24 #include <osipparser2/osip_message.h>
32 void linphone_proxy_config_write_all_to_config_file(LinphoneCore *lc){
35 for(elem=lc->sip_conf.proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
36 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
37 linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
41 void linphone_proxy_config_init(LinphoneProxyConfig *obj){
42 memset(obj,0,sizeof(LinphoneProxyConfig));
53 * Creates an empty proxy config.
55 LinphoneProxyConfig *linphone_proxy_config_new(){
56 LinphoneProxyConfig *obj=NULL;
57 obj=ms_new(LinphoneProxyConfig,1);
58 linphone_proxy_config_init(obj);
63 * Destroys a proxy config.
65 * @note: LinphoneProxyConfig that have been removed from LinphoneCore with
66 * linphone_core_remove_proxy_config() must not be freed.
68 void linphone_proxy_config_destroy(LinphoneProxyConfig *obj){
69 if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
70 if (obj->reg_identity!=NULL) ms_free(obj->reg_identity);
71 if (obj->reg_route!=NULL) ms_free(obj->reg_route);
72 if (obj->ssctx!=NULL) sip_setup_context_free(obj->ssctx);
73 if (obj->realm!=NULL) ms_free(obj->realm);
74 if (obj->type!=NULL) ms_free(obj->type);
75 if (obj->contact_addr!=NULL) ms_free(obj->contact_addr);
76 if (obj->dial_prefix!=NULL) ms_free(obj->dial_prefix);
80 * Returns a boolean indicating that the user is sucessfully registered on the proxy.
82 bool_t linphone_proxy_config_is_registered(const LinphoneProxyConfig *obj){
83 return obj->registered;
86 void linphone_proxy_config_get_contact(LinphoneProxyConfig *cfg, const char **ip, int *port){
88 *ip=cfg->contact_addr;
89 *port=cfg->contact_port;
96 static void update_contact(LinphoneProxyConfig *cfg, const char *ip, const char *port){
97 if (cfg->contact_addr){
98 ms_free(cfg->contact_addr);
100 cfg->contact_addr=ms_strdup(ip);
102 cfg->contact_port=atoi(port);
103 else cfg->contact_port=5060;
106 bool_t linphone_proxy_config_register_again_with_updated_contact(LinphoneProxyConfig *obj, osip_message_t *orig_request, osip_message_t *last_answer){
108 const char *rport,*received;
109 osip_via_t *via=NULL;
110 osip_generic_param_t *param=NULL;
111 osip_contact_t *ctt=NULL;
112 osip_message_get_via(last_answer,0,&via);
113 if (!via) return FALSE;
114 osip_via_param_get_byname(via,"rport",¶m);
115 if (param) rport=param->gvalue;
118 osip_via_param_get_byname(via,"received",¶m);
119 if (param) received=param->gvalue;
121 osip_message_get_contact(orig_request,0,&ctt);
122 if (strcmp(ctt->url->host,received)==0){
123 /*ip address matches, check ports*/
124 const char *contact_port=ctt->url->port;
125 const char *via_rport=rport;
126 if (via_rport==NULL || strlen(via_rport)>0)
128 if (contact_port==NULL || strlen(contact_port)>0)
130 if (strcmp(contact_port,via_rport)==0){
131 ms_message("Register has up to date contact, doing nothing.");
133 }else ms_message("ports do not match, need to update the register (%s <> %s)", contact_port,via_rport);
137 eXosip_register_build_register(obj->rid,obj->expires,&msg);
140 ms_warning("Fail to create a contact updated register.");
143 osip_message_get_contact(msg,0,&ctt);
144 if (ctt->url->host!=NULL){
145 osip_free(ctt->url->host);
147 ctt->url->host=osip_strdup(received);
148 if (ctt->url->port!=NULL){
149 osip_free(ctt->url->port);
151 ctt->url->port=osip_strdup(rport);
152 eXosip_register_send_register(obj->rid,msg);
154 update_contact(obj,received,rport);
155 ms_message("Resending new register with updated contact %s:%s",received,rport);
160 * Sets the proxy address
162 * Examples of valid sip proxy address are:
163 * - IP address: sip:87.98.157.38
164 * - IP address with port: sip:87.98.157.38:5062
165 * - hostnames : sip:sip.example.net
167 int linphone_proxy_config_set_server_addr(LinphoneProxyConfig *obj, const char *server_addr){
170 if (obj->reg_proxy!=NULL) ms_free(obj->reg_proxy);
172 if (server_addr!=NULL && strlen(server_addr)>0){
173 osip_from_init(&url);
174 err=osip_from_parse(url,server_addr);
175 if (err==0 && url->url->host!=NULL){
176 obj->reg_proxy=ms_strdup(server_addr);
178 ms_warning("Could not parse %s",server_addr);
186 * Sets the user identity as a SIP address.
188 * This identity is normally formed with display name, username and domain, such
190 * Alice <sip:alice@example.net>
191 * The REGISTER messages will have from and to set to this identity.
194 void linphone_proxy_config_set_identity(LinphoneProxyConfig *obj, const char *identity){
196 osip_from_t *url=NULL;
197 if (identity!=NULL && strlen(identity)>0){
198 osip_from_init(&url);
199 err=osip_from_parse(url,identity);
200 if (err<0 || url->url->host==NULL || url->url->username==NULL){
201 ms_warning("Could not parse %s",identity);
206 if (obj->reg_identity!=NULL) {
207 ms_free(obj->reg_identity);
208 obj->reg_identity=NULL;
210 if (err==-2) obj->reg_identity=NULL;
212 obj->reg_identity=ms_strdup(identity);
215 obj->realm=ms_strdup(url->url->host);
217 if (url) osip_from_free(url);
220 const char *linphone_proxy_config_get_domain(const LinphoneProxyConfig *cfg){
226 * When a route is set, all outgoing calls will go to the route's destination if this proxy
227 * is the default one (see linphone_core_set_default_proxy() ).
229 void linphone_proxy_config_set_route(LinphoneProxyConfig *obj, const char *route)
232 osip_uri_param_t *lr_param=NULL;
233 osip_route_t *rt=NULL;
235 if (route!=NULL && strlen(route)>0){
236 osip_route_init(&rt);
237 err=osip_route_parse(rt,route);
239 ms_warning("Could not parse %s",route);
243 if (obj->reg_route!=NULL) {
244 ms_free(obj->reg_route);
248 /* check if the lr parameter is set , if not add it */
249 osip_uri_uparam_get_byname(rt->url, "lr", &lr_param);
251 osip_uri_uparam_add(rt->url,osip_strdup("lr"),NULL);
252 osip_route_to_str(rt,&tmproute);
253 obj->reg_route=ms_strdup(tmproute);
255 }else obj->reg_route=ms_strdup(route);
257 if (obj->reg_route!=NULL) ms_free(obj->reg_route);
262 bool_t linphone_proxy_config_check(LinphoneCore *lc, LinphoneProxyConfig *obj){
263 if (obj->reg_proxy==NULL){
264 if (lc->vtable.display_warning)
265 lc->vtable.display_warning(lc,_("The sip proxy address you entered is invalid, it must start with \"sip:\""
266 " followed by a hostname."));
269 if (obj->reg_identity==NULL){
270 if (lc->vtable.display_warning)
271 lc->vtable.display_warning(lc,_("The sip identity you entered is invalid.\nIt should look like "
272 "sip:username@proxydomain, such as sip:alice@example.net"));
279 * Indicates whether a REGISTER request must be sent to the proxy.
281 void linphone_proxy_config_enableregister(LinphoneProxyConfig *obj, bool_t val){
282 obj->reg_sendregister=val;
286 * Sets the registration expiration time in seconds.
288 void linphone_proxy_config_expires(LinphoneProxyConfig *obj, int val){
293 void linphone_proxy_config_enable_publish(LinphoneProxyConfig *obj, bool_t val){
298 * Starts editing a proxy configuration.
300 * Because proxy configuration must be consistent, applications MUST
301 * call linphone_proxy_config_edit() before doing any attempts to modify
302 * proxy configuration (such as identity, proxy address and so on).
303 * Once the modifications are done, then the application must call
304 * linphone_proxy_config_done() to commit the changes.
306 void linphone_proxy_config_edit(LinphoneProxyConfig *obj){
307 obj->auth_failures=0;
308 if (obj->reg_sendregister){
310 if (obj->registered) {
313 eXosip_register_build_register(obj->rid,0,&msg);
314 eXosip_register_send_register(obj->rid,msg);
316 obj->registered=FALSE;
321 void linphone_proxy_config_apply(LinphoneProxyConfig *obj,LinphoneCore *lc)
324 linphone_proxy_config_done(obj);
327 static void linphone_proxy_config_register(LinphoneProxyConfig *obj){
329 if (obj->reg_identity!=NULL) id_str=obj->reg_identity;
330 else id_str=linphone_core_get_primary_contact(obj->lc);
331 if (obj->reg_sendregister){
333 osip_message_t *msg=NULL;
335 obj->rid=eXosip_register_build_initial_register(id_str,obj->reg_proxy,NULL,obj->expires,&msg);
336 eXosip_register_send_register(obj->rid,msg);
338 if (ct!=NULL) osip_free(ct);
344 * Sets a dialing prefix to be automatically prepended when inviting a number with
345 * #linphone_core_invite.
348 void linphone_proxy_config_set_dial_prefix(LinphoneProxyConfig *cfg, const char *prefix){
349 if (cfg->dial_prefix!=NULL){
350 ms_free(cfg->dial_prefix);
351 cfg->dial_prefix=NULL;
353 if (prefix) cfg->dial_prefix=ms_strdup(prefix);
357 * Returns dialing prefix.
361 const char *linphone_proxy_config_get_dial_prefix(const LinphoneProxyConfig *cfg){
362 return cfg->dial_prefix;
366 * Sets whether liblinphone should replace "+" by "00" in dialed numbers (passed to
367 * #linphone_core_invite ).
370 void linphone_proxy_config_set_dial_escape_plus(LinphoneProxyConfig *cfg, bool_t val){
371 cfg->dial_escape_plus=val;
375 * Returns whether liblinphone should replace "+" by "00" in dialed numbers (passed to
376 * #linphone_core_invite ).
379 bool_t linphone_proxy_config_get_dial_escape_plus(const LinphoneProxyConfig *cfg){
380 return cfg->dial_escape_plus;
384 static bool_t is_a_phone_number(const char *username){
386 for(p=username;*p!='\0';++p){
399 static char *flatten_number(const char *number){
400 char *result=ms_malloc0(strlen(number)+1);
403 for(r=number;*r!='\0';++r){
404 if (*r=='+' || isdigit(*r)){
412 static void copy_result(const char *src, char *dest, size_t destlen, bool_t escape_plus){
415 if (escape_plus && src[0]=='+' && destlen>2){
422 for(;i<destlen-1;++i){
430 static char *append_prefix(const char *number, const char *prefix){
431 char *res=ms_malloc(strlen(number)+strlen(prefix)+1);
433 return strcat(res,number);
436 int linphone_proxy_config_normalize_number(LinphoneProxyConfig *proxy, const char *username, char *result, size_t result_len){
439 if (is_a_phone_number(username)){
440 flatten=flatten_number(username);
441 ms_message("Flattened number is '%s'",flatten);
442 numlen=strlen(flatten);
443 if (numlen>10 || flatten[0]=='+' || proxy->dial_prefix==NULL){
444 ms_message("No need to add a prefix");
445 /* prefix is already there */
446 copy_result(flatten,result,result_len,proxy->dial_escape_plus);
449 }else if (proxy->dial_prefix){
452 ms_message("Need to prefix with %s",proxy->dial_prefix);
454 /*remove initial number before prepending prefix*/
457 prefixed=append_prefix(flatten+skipped,proxy->dial_prefix);
459 copy_result(prefixed,result,result_len,proxy->dial_escape_plus);
462 }else strncpy(result,username,result_len);
467 * Commits modification made to the proxy configuration.
469 int linphone_proxy_config_done(LinphoneProxyConfig *obj)
471 if (!linphone_proxy_config_check(obj->lc,obj)) return -1;
473 linphone_proxy_config_write_all_to_config_file(obj->lc);
477 void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm)
479 if (cfg->realm!=NULL) {
483 if (realm!=NULL) cfg->realm=ms_strdup(realm);
486 int linphone_proxy_config_send_publish(LinphoneProxyConfig *proxy,
487 LinphoneOnlineStatus presence_mode)
491 const char *from=NULL;
494 if (proxy->publish==FALSE) return 0;
497 from=linphone_proxy_config_get_identity(proxy);
499 if (from==NULL) from=linphone_core_get_primary_contact(proxy->lc);
501 if (presence_mode==LINPHONE_STATUS_ONLINE)
503 snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
504 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
506 <tuple id=\"sg89ae\">\n\
508 <basic>open</basic>\n\
510 <contact priority=\"0.8\">%s</contact>\n\
511 <note>online</note>\n\
516 else if (presence_mode==LINPHONE_STATUS_BUSY
517 ||presence_mode==LINPHONE_STATUS_NOT_DISTURB)
519 snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
520 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
521 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
523 <tuple id=\"sg89ae\">\n\
525 <basic>open</basic>\n\
527 <es:activity>busy</es:activity>\n\
530 <contact priority=\"0.8\">%s</contact>\n\
536 else if (presence_mode==LINPHONE_STATUS_BERIGHTBACK)
538 snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
539 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
540 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
542 <tuple id=\"sg89ae\">\n\
544 <basic>open</basic>\n\
546 <es:activity>in-transit</es:activity>\n\
549 <contact priority=\"0.8\">%s</contact>\n\
550 <note>be right back</note>\n\
555 else if (presence_mode==LINPHONE_STATUS_AWAY
556 ||presence_mode==LINPHONE_STATUS_MOVED
557 ||presence_mode==LINPHONE_STATUS_ALT_SERVICE)
559 snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
560 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
561 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
563 <tuple id=\"sg89ae\">\n\
565 <basic>open</basic>\n\
567 <es:activity>away</es:activity>\n\
570 <contact priority=\"0.8\">%s</contact>\n\
576 else if (presence_mode==LINPHONE_STATUS_ONTHEPHONE)
578 snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
579 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
580 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
582 <tuple id=\"sg89ae\">\n\
584 <basic>open</basic>\n\
586 <es:activity>on-the-phone</es:activity>\n\
589 <contact priority=\"0.8\">%s</contact>\n\
590 <note>on the phone</note>\n\
595 else if (presence_mode==LINPHONE_STATUS_OUTTOLUNCH)
597 snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
598 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
599 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
601 <tuple id=\"sg89ae\">\n\
603 <basic>open</basic>\n\
605 <es:activity>meal</es:activity>\n\
608 <contact priority=\"0.8\">%s</contact>\n\
609 <note>out to lunch</note>\n\
614 else if (presence_mode==LINPHONE_STATUS_OFFLINE)
617 snprintf(buf, 5000, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
618 <presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n\
619 xmlns:es=\"urn:ietf:params:xml:ns:pidf:status:rpid-status\"\n\
622 "<tuple id=\"sg89ae\">\n\
624 <basic>closed</basic>\n\
626 <es:activity>permanent-absence</e:activity>\n\
633 i = eXosip_build_publish(&pub, (char *)from, (char *)from, NULL, "presence", "1800", "application/pidf+xml", buf);
637 ms_message("Failed to build publish request.");
642 i = eXosip_publish(pub, from); /* should update the sip-if-match parameter
643 from sip-etag from last 200ok of PUBLISH */
647 ms_message("Failed to send publish request.");
655 * Add a proxy configuration.
656 * This will start registration on the proxy, if registration is enabled.
658 int linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
659 if (!linphone_proxy_config_check(lc,cfg)) return -1;
660 lc->sip_conf.proxies=ms_list_append(lc->sip_conf.proxies,(void *)cfg);
661 linphone_proxy_config_apply(cfg,lc);
665 extern void linphone_friend_check_for_removed_proxy(LinphoneFriend *lf, LinphoneProxyConfig *cfg);
668 * Removes a proxy configuration.
670 * LinphoneCore will then automatically unregister and place the proxy configuration
671 * on a deleted list. For that reason, a removed proxy does NOT need to be freed.
673 void linphone_core_remove_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *cfg){
675 lc->sip_conf.proxies=ms_list_remove(lc->sip_conf.proxies,(void *)cfg);
676 /* add to the list of destroyed proxies, so that the possible unREGISTER request can succeed authentication */
677 lc->sip_conf.deleted_proxies=ms_list_append(lc->sip_conf.deleted_proxies,(void *)cfg);
678 /* this will unREGISTER */
679 linphone_proxy_config_edit(cfg);
680 if (lc->default_proxy==cfg){
681 lc->default_proxy=NULL;
683 /* invalidate all references to this proxy in our friend list */
684 for (elem=lc->friends;elem!=NULL;elem=ms_list_next(elem)){
685 linphone_friend_check_for_removed_proxy((LinphoneFriend*)elem->data,cfg);
690 * Erase all proxies from config.
694 void linphone_core_clear_proxy_config(LinphoneCore *lc){
695 MSList* list=ms_list_copy(linphone_core_get_proxy_config_list((const LinphoneCore*)lc));
696 for(;list!=NULL;list=list->next){
697 linphone_core_remove_proxy_config(lc,(LinphoneProxyConfig *)list->data);
702 * Sets the default proxy.
704 * This default proxy must be part of the list of already entered LinphoneProxyConfig.
705 * Toggling it as default will make LinphoneCore use the identity associated with
706 * the proxy configuration in all incoming and outgoing calls.
708 void linphone_core_set_default_proxy(LinphoneCore *lc, LinphoneProxyConfig *config){
709 /* check if this proxy is in our list */
711 if (ms_list_find(lc->sip_conf.proxies,config)==NULL){
712 ms_warning("Bad proxy address: it is not in the list !");
713 lc->default_proxy=NULL;
717 lc->default_proxy=config;
721 void linphone_core_set_default_proxy_index(LinphoneCore *lc, int index){
722 if (index<0) linphone_core_set_default_proxy(lc,NULL);
723 else linphone_core_set_default_proxy(lc,ms_list_nth_data(lc->sip_conf.proxies,index));
727 * Returns the default proxy configuration, that is the one used to determine the current identity.
729 int linphone_core_get_default_proxy(LinphoneCore *lc, LinphoneProxyConfig **config){
731 if (config!=NULL) *config=lc->default_proxy;
732 if (lc->default_proxy!=NULL){
733 pos=ms_list_position(lc->sip_conf.proxies,ms_list_find(lc->sip_conf.proxies,(void *)lc->default_proxy));
738 static int rid_compare(const void *pcfg,const void *prid){
739 const LinphoneProxyConfig *cfg=(const LinphoneProxyConfig*)pcfg;
740 const int *rid=(const int*)prid;
741 ms_message("cfg= %s, cfg->rid=%i, rid=%i",cfg->reg_proxy, cfg->rid, *rid);
742 return cfg->rid-(*rid);
745 LinphoneProxyConfig *linphone_core_get_proxy_config_from_rid(LinphoneCore *lc, int rid){
746 MSList *elem=ms_list_find_custom(lc->sip_conf.proxies,rid_compare, &rid);
748 ms_message("linphone_core_get_proxy_config_from_rid: searching in deleted proxies...");
749 elem=ms_list_find_custom(lc->sip_conf.deleted_proxies,rid_compare, &rid);
751 if (elem==NULL) return NULL;
752 else return (LinphoneProxyConfig*)elem->data;
756 * Returns an unmodifiable list of entered proxy configurations.
758 const MSList *linphone_core_get_proxy_config_list(const LinphoneCore *lc){
759 return lc->sip_conf.proxies;
763 void linphone_proxy_config_process_authentication_failure(LinphoneCore *lc, eXosip_event_t *ev){
764 LinphoneProxyConfig *cfg=linphone_core_get_proxy_config_from_rid(lc, ev->rid);
766 cfg->auth_failures++;
767 if (strcmp(ev->request->sip_method,"REGISTER")==0) {
768 gstate_new_state(lc, GSTATE_REG_FAILED, "Authentication failed.");
770 /*restart a new register so that the user gets a chance to be prompted for a password*/
771 if (cfg->auth_failures==1){
772 linphone_proxy_config_register(cfg);
777 void linphone_proxy_config_write_to_config_file(LpConfig *config, LinphoneProxyConfig *obj, int index)
781 sprintf(key,"proxy_%i",index);
782 lp_config_clean_section(config,key);
786 if (obj->type!=NULL){
787 lp_config_set_string(config,key,"type",obj->type);
789 if (obj->reg_proxy!=NULL){
790 lp_config_set_string(config,key,"reg_proxy",obj->reg_proxy);
792 if (obj->reg_route!=NULL){
793 lp_config_set_string(config,key,"reg_route",obj->reg_route);
795 if (obj->reg_identity!=NULL){
796 lp_config_set_string(config,key,"reg_identity",obj->reg_identity);
798 lp_config_set_int(config,key,"reg_expires",obj->expires);
799 lp_config_set_int(config,key,"reg_sendregister",obj->reg_sendregister);
800 lp_config_set_int(config,key,"publish",obj->publish);
801 lp_config_set_int(config,key,"dial_escape_plus",obj->dial_escape_plus);
802 lp_config_set_string(config,key,"dial_prefix",obj->dial_prefix);
807 LinphoneProxyConfig *linphone_proxy_config_new_from_config_file(LpConfig *config, int index)
810 const char *identity;
812 LinphoneProxyConfig *cfg;
815 sprintf(key,"proxy_%i",index);
817 if (!lp_config_has_section(config,key)){
821 cfg=linphone_proxy_config_new();
823 identity=lp_config_get_string(config,key,"reg_identity",NULL);
824 proxy=lp_config_get_string(config,key,"reg_proxy",NULL);
826 linphone_proxy_config_set_identity(cfg,identity);
827 linphone_proxy_config_set_server_addr(cfg,proxy);
829 tmp=lp_config_get_string(config,key,"reg_route",NULL);
830 if (tmp!=NULL) linphone_proxy_config_set_route(cfg,tmp);
832 linphone_proxy_config_expires(cfg,lp_config_get_int(config,key,"reg_expires",600));
833 linphone_proxy_config_enableregister(cfg,lp_config_get_int(config,key,"reg_sendregister",0));
835 linphone_proxy_config_enable_publish(cfg,lp_config_get_int(config,key,"publish",0));
837 linphone_proxy_config_set_dial_escape_plus(cfg,lp_config_get_int(config,key,"dial_escape_plus",0));
838 linphone_proxy_config_set_dial_prefix(cfg,lp_config_get_string(config,key,"dial_prefix",NULL));
840 tmp=lp_config_get_string(config,key,"type",NULL);
841 if (tmp!=NULL && strlen(tmp)>0)
842 linphone_proxy_config_set_sip_setup(cfg,tmp);
847 static void linphone_proxy_config_activate_sip_setup(LinphoneProxyConfig *cfg){
848 SipSetupContext *ssc;
849 SipSetup *ss=sip_setup_lookup(cfg->type);
850 LinphoneCore *lc=linphone_proxy_config_get_core(cfg);
853 ssc=sip_setup_context_new(ss,cfg);
855 if (cfg->reg_identity==NULL){
856 ms_error("Invalid identity for this proxy configuration.");
859 caps=sip_setup_context_get_capabilities(ssc);
860 if (caps & SIP_SETUP_CAP_ACCOUNT_MANAGER){
861 if (sip_setup_context_login_account(ssc,cfg->reg_identity,NULL)!=0){
862 if (lc->vtable.display_warning){
863 char *tmp=ms_strdup_printf(_("Could not login as %s"),cfg->reg_identity);
864 lc->vtable.display_warning(lc,tmp);
870 if (caps & SIP_SETUP_CAP_PROXY_PROVIDER){
872 if (sip_setup_context_get_proxy(ssc,NULL,proxy,sizeof(proxy))==0){
873 linphone_proxy_config_set_server_addr(cfg,proxy);
875 ms_error("Could not retrieve proxy uri !");
881 SipSetup *linphone_proxy_config_get_sip_setup(LinphoneProxyConfig *cfg){
882 if (cfg->ssctx!=NULL) return cfg->ssctx->funcs;
883 if (cfg->type!=NULL){
884 return sip_setup_lookup(cfg->type);
889 void linphone_proxy_config_update(LinphoneProxyConfig *cfg){
891 if (cfg->type && cfg->ssctx==NULL){
892 linphone_proxy_config_activate_sip_setup(cfg);
894 linphone_proxy_config_register(cfg);
899 void linphone_proxy_config_set_sip_setup(LinphoneProxyConfig *cfg, const char *type){
902 cfg->type=ms_strdup(type);
903 if (linphone_proxy_config_get_addr(cfg)==NULL){
904 /*put a placeholder so that the sip setup gets saved into the config */
905 linphone_proxy_config_set_server_addr(cfg,"sip:undefined");
909 SipSetupContext *linphone_proxy_config_get_sip_setup_context(LinphoneProxyConfig *cfg){
917 LinphoneAccountCreator *linphone_account_creator_new(struct _LinphoneCore *core, const char *type){
918 LinphoneAccountCreator *obj;
919 LinphoneProxyConfig *cfg;
920 SipSetup *ss=sip_setup_lookup(type);
921 SipSetupContext *ssctx;
925 if (!(sip_setup_get_capabilities(ss) & SIP_SETUP_CAP_ACCOUNT_MANAGER)){
926 ms_error("%s cannot manage accounts.");
929 obj=ms_new0(LinphoneAccountCreator,1);
930 cfg=linphone_proxy_config_new();
931 ssctx=sip_setup_context_new(ss,cfg);
934 set_string(&obj->domain,sip_setup_context_get_domains(ssctx)[0]);
939 void linphone_account_creator_set_username(LinphoneAccountCreator *obj, const char *username){
940 set_string(&obj->username,username);
943 void linphone_account_creator_set_password(LinphoneAccountCreator *obj, const char *password){
944 set_string(&obj->password,password);
947 void linphone_account_creator_set_domain(LinphoneAccountCreator *obj, const char *domain){
948 set_string(&obj->domain,domain);
951 const char * linphone_account_creator_get_username(LinphoneAccountCreator *obj){
952 return obj->username;
955 const char * linphone_account_creator_get_domain(LinphoneAccountCreator *obj){
959 int linphone_account_creator_test_existence(LinphoneAccountCreator *obj){
960 SipSetupContext *ssctx=obj->ssctx;
961 char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
962 int err=sip_setup_context_account_exists(ssctx,uri);
967 LinphoneProxyConfig * linphone_account_creator_validate(LinphoneAccountCreator *obj){
968 SipSetupContext *ssctx=obj->ssctx;
969 char *uri=ms_strdup_printf("%s@%s",obj->username,obj->domain);
970 int err=sip_setup_context_create_account(ssctx,uri,obj->password);
974 return sip_setup_context_get_proxy_config(ssctx);
979 void linphone_account_creator_destroy(LinphoneAccountCreator *obj){
981 ms_free(obj->username);
983 ms_free(obj->password);
985 ms_free(obj->domain);
986 if (!obj->succeeded){
987 linphone_proxy_config_destroy(sip_setup_context_get_proxy_config(obj->ssctx));