Skip to content

Commit

Permalink
configure: simplify get_autoconfig()
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Krotov authored and link2xt committed Jan 6, 2021
1 parent 0f90d50 commit 31ae099
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 74 deletions.
4 changes: 2 additions & 2 deletions src/configure/auto_mozilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ fn parse_serverparams(in_emailaddr: &str, xml_raw: &str) -> Result<Vec<ServerPar

pub(crate) async fn moz_autoconfigure(
context: &Context,
url: &str,
url: impl AsRef<str>,
param_in: &LoginParam,
) -> 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);
if let Err(err) = &res {
Expand Down
3 changes: 1 addition & 2 deletions src/configure/auto_outlook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ fn protocols_to_serverparams(protocols: Vec<ProtocolTag>) -> Vec<ServerParams> {

pub(crate) async fn outlk_autodiscover(
context: &Context,
url: &str,
mut url: String,
) -> Result<Vec<ServerParams>, 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?;
Expand Down
132 changes: 62 additions & 70 deletions src/configure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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.
///
/// A Search configurations from the domain used in the email-address, prefer encrypted
Expand All @@ -439,16 +379,68 @@ async fn get_autoconfig(
param_domain: &str,
param_addr_urlencoded: &str,
) -> Option<Vec<ServerParams>> {
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
),
&param,
)
.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={}",
&param_domain, &param_addr_urlencoded
),
&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
Expand Down

0 comments on commit 31ae099

Please sign in to comment.