configure: simplify get_autoconfig()

This commit is contained in:
Alexander Krotov
2021-01-03 21:50:46 +03:00
committed by link2xt
parent 0f90d50385
commit 31ae099e19
3 changed files with 64 additions and 73 deletions

View File

@@ -251,10 +251,10 @@ fn parse_serverparams(in_emailaddr: &str, xml_raw: &str) -> Result<Vec<ServerPar
pub(crate) async fn moz_autoconfigure( pub(crate) async fn moz_autoconfigure(
context: &Context, context: &Context,
url: &str, url: impl AsRef<str>,
param_in: &LoginParam, param_in: &LoginParam,
) -> Result<Vec<ServerParams>, Error> { ) -> Result<Vec<ServerParams>, Error> {
let xml_raw = read_url(context, url).await?; let xml_raw = read_url(context, url.as_ref()).await?;
let res = parse_serverparams(&param_in.addr, &xml_raw); let res = parse_serverparams(&param_in.addr, &xml_raw);
if let Err(err) = &res { if let Err(err) = &res {

View File

@@ -187,9 +187,8 @@ fn protocols_to_serverparams(protocols: Vec<ProtocolTag>) -> Vec<ServerParams> {
pub(crate) async fn outlk_autodiscover( pub(crate) async fn outlk_autodiscover(
context: &Context, context: &Context,
url: &str, mut url: String,
) -> Result<Vec<ServerParams>, Error> { ) -> Result<Vec<ServerParams>, Error> {
let mut url = url.to_string();
/* Follow up to 10 xml-redirects (http-redirects are followed in read_url() */ /* Follow up to 10 xml-redirects (http-redirects are followed in read_url() */
for _i in 0..10 { for _i in 0..10 {
let xml_raw = read_url(context, &url).await?; let xml_raw = read_url(context, &url).await?;

View File

@@ -369,66 +369,6 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> {
Ok(()) Ok(())
} }
#[derive(Debug, PartialEq, Eq)]
enum AutoconfigProvider {
Mozilla,
Outlook,
}
#[derive(Debug, PartialEq, Eq)]
struct AutoconfigSource {
provider: AutoconfigProvider,
url: String,
}
impl AutoconfigSource {
fn all(domain: &str, addr: &str) -> [Self; 5] {
[
AutoconfigSource {
provider: AutoconfigProvider::Mozilla,
url: format!(
"https://autoconfig.{}/mail/config-v1.1.xml?emailaddress={}",
domain, addr,
),
},
// the doc does not mention `emailaddress=`, however, Thunderbird adds it, see https://releases.mozilla.org/pub/thunderbird/ , which makes some sense
AutoconfigSource {
provider: AutoconfigProvider::Mozilla,
url: format!(
"https://{}/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress={}",
domain, addr
),
},
AutoconfigSource {
provider: AutoconfigProvider::Outlook,
url: format!("https://{}/autodiscover/autodiscover.xml", domain),
},
// Outlook uses always SSL but different domains (this comment describes the next two steps)
AutoconfigSource {
provider: AutoconfigProvider::Outlook,
url: format!(
"https://autodiscover.{}/autodiscover/autodiscover.xml",
domain
),
},
// always SSL for Thunderbird's database
AutoconfigSource {
provider: AutoconfigProvider::Mozilla,
url: format!("https://autoconfig.thunderbird.net/v1.1/{}", domain),
},
]
}
async fn fetch(&self, ctx: &Context, param: &LoginParam) -> Result<Vec<ServerParams>> {
let params = match self.provider {
AutoconfigProvider::Mozilla => moz_autoconfigure(ctx, &self.url, &param).await?,
AutoconfigProvider::Outlook => outlk_autodiscover(ctx, &self.url).await?,
};
Ok(params)
}
}
/// Retrieve available autoconfigurations. /// Retrieve available autoconfigurations.
/// ///
/// A Search configurations from the domain used in the email-address, prefer encrypted /// A Search configurations from the domain used in the email-address, prefer encrypted
@@ -439,16 +379,68 @@ async fn get_autoconfig(
param_domain: &str, param_domain: &str,
param_addr_urlencoded: &str, param_addr_urlencoded: &str,
) -> Option<Vec<ServerParams>> { ) -> Option<Vec<ServerParams>> {
let sources = AutoconfigSource::all(param_domain, param_addr_urlencoded); if let Ok(res) = moz_autoconfigure(
ctx,
format!(
"https://autoconfig.{}/mail/config-v1.1.xml?emailaddress={}",
param_domain, param_addr_urlencoded
),
&param,
)
.await
{
return Some(res);
}
progress!(ctx, 300);
let mut progress = 300; if let Ok(res) = moz_autoconfigure(
for source in &sources { ctx,
let res = source.fetch(ctx, param).await; // the doc does not mention `emailaddress=`, however, Thunderbird adds it, see https://releases.mozilla.org/pub/thunderbird/ , which makes some sense
progress!(ctx, progress); format!(
progress += 10; "https://{}/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress={}",
if let Ok(res) = res { &param_domain, &param_addr_urlencoded
return Some(res); ),
} &param,
)
.await
{
return Some(res);
}
progress!(ctx, 310);
// Outlook uses always SSL but different domains (this comment describes the next two steps)
if let Ok(res) = outlk_autodiscover(
ctx,
format!("https://{}/autodiscover/autodiscover.xml", &param_domain),
)
.await
{
return Some(res);
}
progress!(ctx, 320);
if let Ok(res) = outlk_autodiscover(
ctx,
format!(
"https://autodiscover.{}/autodiscover/autodiscover.xml",
&param_domain
),
)
.await
{
return Some(res);
}
progress!(ctx, 330);
// always SSL for Thunderbird's database
if let Ok(res) = moz_autoconfigure(
ctx,
format!("https://autoconfig.thunderbird.net/v1.1/{}", &param_domain),
&param,
)
.await
{
return Some(res);
} }
None None