siplogin.c 3.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
linphone
Copyright (C) 2009  Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 19
*/

20
#include "linphone/core.h"
Simon Morlat's avatar
Simon Morlat committed
21
#include "private.h"
smorlat's avatar
smorlat committed
22
#include <ctype.h>
23 24 25 26 27 28 29

static void sip_login_init_instance(SipSetupContext *ctx){
	LinphoneProxyConfig *cfg=sip_setup_context_get_proxy_config(ctx);
	/*disable registration until the user logs in*/
	linphone_proxy_config_enable_register(cfg,FALSE);
}

30 31
static void guess_display_name(LinphoneAddress *from){
	char *dn=(char*)ms_malloc(strlen(linphone_address_get_username(from))+3);
32
	const char *it;
smorlat's avatar
smorlat committed
33 34 35
	char *wptr=dn;
	bool_t begin=TRUE;
	bool_t surname=0;
36
	for(it=linphone_address_get_username(from);*it!='\0';++it){
37
		if (begin){
38
			*wptr = (char)toupper(*it);
39 40
			begin=FALSE;
		}else if (*it=='.'){
smorlat's avatar
smorlat committed
41 42 43 44 45 46 47
			if (surname) break;
			*wptr=' ';
			begin=TRUE;
			surname=TRUE;
		}else *wptr=*it;
		wptr++;
	}
48
	linphone_address_set_display_name(from,dn);
49
	ms_free(dn);
smorlat's avatar
smorlat committed
50 51
}

52
static int sip_login_do_login(SipSetupContext * ctx, const char *uri, const char *passwd, const char *userid){
53 54
	LinphoneProxyConfig *cfg=sip_setup_context_get_proxy_config(ctx);
	LinphoneCore *lc=linphone_proxy_config_get_core(cfg);
smorlat's avatar
smorlat committed
55
	LinphoneAuthInfo *auth;
56
	LinphoneAddress *parsed_uri;
smorlat's avatar
smorlat committed
57 58
	char *tmp;

59
	parsed_uri=linphone_address_new(uri);
60
	if (parsed_uri==NULL){
61 62
		return -1;
	}
63
	if (linphone_address_get_display_name(parsed_uri)!=NULL){
smorlat's avatar
smorlat committed
64 65
		guess_display_name(parsed_uri);
	}
66
	tmp=linphone_address_as_string(parsed_uri);
smorlat's avatar
smorlat committed
67
	linphone_proxy_config_set_identity(cfg,tmp);
smorlat's avatar
smorlat committed
68
	if (passwd ) {
69 70
		auth=linphone_auth_info_new(linphone_address_get_username(parsed_uri),userid,passwd,NULL,NULL,
			linphone_address_get_domain(parsed_uri));
smorlat's avatar
smorlat committed
71 72
		linphone_core_add_auth_info(lc,auth);
	}
73
	linphone_proxy_config_enable_register(cfg,TRUE);
74
	linphone_proxy_config_done(cfg);
75
	ms_free(tmp);
Simon Morlat's avatar
Simon Morlat committed
76
	linphone_address_unref(parsed_uri);
77
	ms_message("SipLogin: done");
78 79 80
	return 0;
}

smorlat's avatar
smorlat committed
81 82
static int sip_login_do_logout(SipSetupContext * ctx){
	LinphoneProxyConfig *cfg=sip_setup_context_get_proxy_config(ctx);
83
	linphone_proxy_config_edit(cfg);
smorlat's avatar
smorlat committed
84 85 86 87 88
	linphone_proxy_config_enable_register(cfg,FALSE);
	linphone_proxy_config_done(cfg);
	return 0;
}

89
/* a simple SipSetup built-in plugin to allow specify the user/password for proxy config at runtime*/
Simon Morlat's avatar
Simon Morlat committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
SipSetup linphone_sip_login={
	"SipLogin",
	SIP_SETUP_CAP_LOGIN,
	0,
	NULL,
	NULL,
	sip_login_init_instance,
	NULL,
	NULL,
	NULL,
	sip_login_do_login,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
106
	sip_login_do_logout,
Simon Morlat's avatar
Simon Morlat committed
107
	NULL,
108
	NULL
Simon Morlat's avatar
Simon Morlat committed
109
};