From 31ae099e19e224cb485d8ddaa9e113c4f1e6737c Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sun, 3 Jan 2021 21:50:46 +0300 Subject: [PATCH] configure: simplify get_autoconfig() --- src/configure/auto_mozilla.rs | 4 +- src/configure/auto_outlook.rs | 3 +- src/configure/mod.rs | 132 ++++++++++++++++------------------ 3 files changed, 65 insertions(+), 74 deletions(-) diff --git a/src/configure/auto_mozilla.rs b/src/configure/auto_mozilla.rs index f434cc1a16..9b96d208b6 100644 --- a/src/configure/auto_mozilla.rs +++ b/src/configure/auto_mozilla.rs @@ -251,10 +251,10 @@ fn parse_serverparams(in_emailaddr: &str, xml_raw: &str) -> Result, param_in: &LoginParam, ) -> Result, Error> { - let xml_raw = read_url(context, url).await?; + let xml_raw = read_url(context, url.as_ref()).await?; let res = parse_serverparams(¶m_in.addr, &xml_raw); if let Err(err) = &res { diff --git a/src/configure/auto_outlook.rs b/src/configure/auto_outlook.rs index 091901e638..7da487d759 100644 --- a/src/configure/auto_outlook.rs +++ b/src/configure/auto_outlook.rs @@ -187,9 +187,8 @@ fn protocols_to_serverparams(protocols: Vec) -> Vec { pub(crate) async fn outlk_autodiscover( context: &Context, - url: &str, + mut url: String, ) -> Result, Error> { - let mut url = url.to_string(); /* Follow up to 10 xml-redirects (http-redirects are followed in read_url() */ for _i in 0..10 { let xml_raw = read_url(context, &url).await?; diff --git a/src/configure/mod.rs b/src/configure/mod.rs index ff9e66b236..b9e29d9dbc 100644 --- a/src/configure/mod.rs +++ b/src/configure/mod.rs @@ -369,66 +369,6 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> { 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> { - let params = match self.provider { - AutoconfigProvider::Mozilla => moz_autoconfigure(ctx, &self.url, ¶m).await?, - AutoconfigProvider::Outlook => outlk_autodiscover(ctx, &self.url).await?, - }; - - Ok(params) - } -} - /// Retrieve available autoconfigurations. /// /// 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_addr_urlencoded: &str, ) -> Option> { - let sources = AutoconfigSource::all(param_domain, param_addr_urlencoded); - - let mut progress = 300; - for source in &sources { - let res = source.fetch(ctx, param).await; - progress!(ctx, progress); - progress += 10; - if let Ok(res) = res { - return Some(res); - } + if let Ok(res) = moz_autoconfigure( + ctx, + format!( + "https://autoconfig.{}/mail/config-v1.1.xml?emailaddress={}", + param_domain, param_addr_urlencoded + ), + ¶m, + ) + .await + { + return Some(res); + } + progress!(ctx, 300); + + if let Ok(res) = moz_autoconfigure( + ctx, + // the doc does not mention `emailaddress=`, however, Thunderbird adds it, see https://releases.mozilla.org/pub/thunderbird/ , which makes some sense + format!( + "https://{}/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress={}", + ¶m_domain, ¶m_addr_urlencoded + ), + ¶m, + ) + .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", ¶m_domain), + ) + .await + { + return Some(res); + } + progress!(ctx, 320); + + if let Ok(res) = outlk_autodiscover( + ctx, + format!( + "https://autodiscover.{}/autodiscover/autodiscover.xml", + ¶m_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/{}", ¶m_domain), + ¶m, + ) + .await + { + return Some(res); } None