Skip to content

Commit

Permalink
feat: try only the full email address if username is unspecified
Browse files Browse the repository at this point in the history
Previously Delta Chat tried to use local part of email address as well.
This configuration is very uncommon,
but trying it doubled the time of configuration try
in the worst case, e.g. when the password is typed in incorrectly.
  • Loading branch information
link2xt committed Aug 1, 2024
1 parent a30c6ae commit 1d0b880
Showing 1 changed file with 46 additions and 15 deletions.
61 changes: 46 additions & 15 deletions src/configure/server_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,14 @@ pub(crate) struct ServerParams {

impl ServerParams {
fn expand_usernames(self, addr: &str) -> Vec<ServerParams> {
let mut res = Vec::new();

if self.username.is_empty() {
res.push(Self {
vec![Self {
username: addr.to_string(),
..self.clone()
});

if let Some(at) = addr.find('@') {
res.push(Self {
username: addr.split_at(at).0.to_string(),
..self
});
}
}]
} else {
res.push(self)
vec![self]
}
res
}

fn expand_hostnames(self, param_domain: &str) -> Vec<ServerParams> {
Expand Down Expand Up @@ -155,8 +145,7 @@ pub(crate) fn expand_param_vector(
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.
// Ports are expanded the last, so they are changed the first.
.flat_map(|params| params.expand_strict_tls().into_iter())
.flat_map(|params| params.expand_usernames(addr).into_iter())
.flat_map(|params| params.expand_hostnames(domain).into_iter())
Expand Down Expand Up @@ -333,5 +322,47 @@ mod tests {
},
],
);

// Test that email address is used as the default username.
// We do not try other usernames
// such as the local part of the address
// as this is very uncommon configuration
// and not worth doubling the number of candidates to try.
// If such configuration is used, email provider
// should provide XML autoconfig or
// be added to the provider database as an exception.
let v = expand_param_vector(
vec![ServerParams {
protocol: Protocol::Imap,
hostname: "example.net".to_string(),
port: 0,
socket: Socket::Automatic,
username: "".to_string(),
strict_tls: Some(true),
}],
"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@example.net".to_string(),
strict_tls: Some(true)
},
ServerParams {
protocol: Protocol::Imap,
hostname: "example.net".to_string(),
port: 143,
socket: Socket::Starttls,
username: "foobar@example.net".to_string(),
strict_tls: Some(true)
},
],
);
}
}

0 comments on commit 1d0b880

Please sign in to comment.