mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 09:26:29 +03:00
configure: simplify get_autoconfig()
This commit is contained in:
committed by
link2xt
parent
0f90d50385
commit
31ae099e19
@@ -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(¶m_in.addr, &xml_raw);
|
let res = parse_serverparams(¶m_in.addr, &xml_raw);
|
||||||
if let Err(err) = &res {
|
if let Err(err) = &res {
|
||||||
|
|||||||
@@ -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?;
|
||||||
|
|||||||
@@ -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, ¶m).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,
|
||||||
let mut progress = 300;
|
format!(
|
||||||
for source in &sources {
|
"https://autoconfig.{}/mail/config-v1.1.xml?emailaddress={}",
|
||||||
let res = source.fetch(ctx, param).await;
|
param_domain, param_addr_urlencoded
|
||||||
progress!(ctx, progress);
|
),
|
||||||
progress += 10;
|
¶m,
|
||||||
if let Ok(res) = res {
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
return Some(res);
|
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
|
None
|
||||||
|
|||||||
Reference in New Issue
Block a user