diff --git a/src/configure/mod.rs b/src/configure/mod.rs index 841553e58..8783fd30e 100644 --- a/src/configure/mod.rs +++ b/src/configure/mod.rs @@ -24,7 +24,7 @@ use crate::{chat, e2ee, provider}; use auto_mozilla::moz_autoconfigure; use auto_outlook::outlk_autodiscover; -use server_params::ServerParams; +use server_params::{expand_param_vector, ServerParams}; macro_rules! progress { ($context:tt, $progress:expr) => { @@ -194,8 +194,8 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> { progress!(ctx, 500); - let servers: Vec = param_autoconfig - .unwrap_or_else(|| { + let servers = expand_param_vector( + param_autoconfig.unwrap_or_else(|| { vec![ ServerParams { protocol: Protocol::IMAP, @@ -212,16 +212,10 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> { username: param.smtp.user.clone(), }, ] - }) - .into_iter() - // The order of expansion is important: ports are expanded the - // last, so they are changed the first. Username is only - // changed if default value (address with domain) didn't work - // for all available hosts and ports. - .flat_map(|params| params.expand_usernames(¶m.addr).into_iter()) - .flat_map(|params| params.expand_hostnames(¶m_domain).into_iter()) - .flat_map(|params| params.expand_ports().into_iter()) - .collect(); + }), + ¶m.addr, + ¶m_domain, + ); progress!(ctx, 600); diff --git a/src/configure/server_params.rs b/src/configure/server_params.rs index 4f533d918..df577de60 100644 --- a/src/configure/server_params.rs +++ b/src/configure/server_params.rs @@ -6,7 +6,7 @@ use crate::provider::{Protocol, Socket}; /// /// Can be loaded from offline provider database, online configuraiton /// or derived from user entered parameters. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub(crate) struct ServerParams { /// Protocol, such as IMAP or SMTP. pub protocol: Protocol, @@ -113,3 +113,52 @@ impl ServerParams { res } } + +/// Expands vector of `ServerParams`, replacing placeholders with +/// variants to try. +pub(crate) fn expand_param_vector( + v: Vec, + addr: &str, + domain: &str, +) -> Vec { + v.into_iter() + // The order of expansion is important: ports are expanded the + // last, so they are changed the first. Username is only + // changed if default value (address with domain) didn't work + // for all available hosts and ports. + .flat_map(|params| params.expand_usernames(addr).into_iter()) + .flat_map(|params| params.expand_hostnames(domain).into_iter()) + .flat_map(|params| params.expand_ports().into_iter()) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_expand_param_vector() { + let v = expand_param_vector( + vec![ServerParams { + protocol: Protocol::IMAP, + hostname: "example.net".to_string(), + port: 0, + socket: Socket::SSL, + username: "foobar".to_string(), + }], + "foobar@example.net", + "example.net", + ); + + assert_eq!( + v, + vec![ServerParams { + protocol: Protocol::IMAP, + hostname: "example.net".to_string(), + port: 993, + socket: Socket::SSL, + username: "foobar".to_string(), + }], + ); + } +}