diff --git a/deltachat-rpc-client/tests/test_something.py b/deltachat-rpc-client/tests/test_something.py index 9e526e459..0e74650b8 100644 --- a/deltachat-rpc-client/tests/test_something.py +++ b/deltachat-rpc-client/tests/test_something.py @@ -93,6 +93,16 @@ def test_configure_alternative_port(acfactory) -> None: account.configure() +def test_configure_username(acfactory) -> None: + account = acfactory.new_preconfigured_account() + + addr = account.get_config("addr") + account.set_config("mail_user", addr) + account.configure() + + assert account.get_config("configured_mail_user") == addr + + def test_account(acfactory) -> None: alice, bob = acfactory.get_online_accounts(2) diff --git a/src/config.rs b/src/config.rs index ab30fbe79..9a21b4bf8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -219,7 +219,7 @@ pub enum Config { /// Configured IMAP server username. /// - /// This is replaced by `configured_imap_servers` for new configurations. + /// This is set if user has configured username manually. ConfiguredMailUser, /// Configured IMAP server password. @@ -253,7 +253,7 @@ pub enum Config { /// Configured SMTP server username. /// - /// This is replaced by `configured_smtp_servers` for new configurations. + /// This is set if user has configured username manually. ConfiguredSendUser, /// Configured SMTP server password. diff --git a/src/configure.rs b/src/configure.rs index f614e2144..c8f8a062e 100644 --- a/src/configure.rs +++ b/src/configure.rs @@ -332,6 +332,7 @@ async fn get_configured_param( } }) .collect(), + imap_user: param.imap.user.clone(), imap_password: param.imap.password.clone(), smtp: servers .iter() @@ -353,6 +354,7 @@ async fn get_configured_param( } }) .collect(), + smtp_user: param.smtp.user.clone(), smtp_password, socks5_config: param.socks5_config.clone(), provider, @@ -610,7 +612,9 @@ pub enum Error { mod tests { #![allow(clippy::indexing_slicing)] + use super::*; use crate::config::Config; + use crate::login_param::EnteredServerLoginParam; use crate::test_utils::TestContext; #[tokio::test(flavor = "multi_thread", worker_threads = 2)] @@ -622,4 +626,24 @@ mod tests { t.set_config(Config::MailPw, Some("123456")).await.unwrap(); assert!(t.configure().await.is_err()); } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn test_get_configured_param() -> Result<()> { + let t = &TestContext::new().await; + let entered_param = EnteredLoginParam { + addr: "alice@example.org".to_string(), + + imap: EnteredServerLoginParam { + user: "alice@example.net".to_string(), + password: "foobar".to_string(), + ..Default::default() + }, + + ..Default::default() + }; + let configured_param = get_configured_param(t, &entered_param).await?; + assert_eq!(configured_param.imap_user, "alice@example.net"); + assert_eq!(configured_param.smtp_user, ""); + Ok(()) + } } diff --git a/src/login_param.rs b/src/login_param.rs index a3694d919..e63047b87 100644 --- a/src/login_param.rs +++ b/src/login_param.rs @@ -76,7 +76,7 @@ pub enum ConfiguredCertificateChecks { } /// Login parameters for a single server, either IMAP or SMTP -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct EnteredServerLoginParam { /// Server hostname or IP address. pub server: String, @@ -99,7 +99,7 @@ pub struct EnteredServerLoginParam { } /// Login parameters entered by the user. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct EnteredLoginParam { /// Email address. pub addr: String, @@ -360,10 +360,22 @@ pub struct ConfiguredLoginParam { pub imap: Vec, + // Custom IMAP user. + // + // This overwrites autoconfig from the provider database + // if non-empty. + pub imap_user: String, + pub imap_password: String, pub smtp: Vec, + // Custom SMTP user. + // + // This overwrites autoconfig from the provider database + // if non-empty. + pub smtp_user: String, + pub smtp_password: String, pub socks5_config: Option, @@ -457,8 +469,14 @@ impl ConfiguredLoginParam { let imap; let smtp; - let legacy_mail_user = context.get_config(Config::ConfiguredMailUser).await?; - let legacy_send_user = context.get_config(Config::ConfiguredSendUser).await?; + let mail_user = context + .get_config(Config::ConfiguredMailUser) + .await? + .unwrap_or_default(); + let send_user = context + .get_config(Config::ConfiguredSendUser) + .await? + .unwrap_or_default(); if let Some(provider) = provider { let addr_localpart = if let Some(at) = addr.find('@') { @@ -484,8 +502,8 @@ impl ConfiguredLoginParam { port: server.port, security, }, - user: if let Some(legacy_mail_user) = &legacy_mail_user { - legacy_mail_user.clone() + user: if !mail_user.is_empty() { + mail_user.clone() } else { match server.username_pattern { UsernamePattern::Email => addr.to_string(), @@ -513,8 +531,8 @@ impl ConfiguredLoginParam { port: server.port, security, }, - user: if let Some(legacy_send_user) = &legacy_send_user { - legacy_send_user.clone() + user: if !send_user.is_empty() { + send_user.clone() } else { match server.username_pattern { UsernamePattern::Email => addr.to_string(), @@ -543,7 +561,6 @@ impl ConfiguredLoginParam { .await? .unwrap_or_default(); - let mail_user = legacy_mail_user.unwrap_or_default(); let mail_security: Socket = context .get_config_parsed::(Config::ConfiguredMailSecurity) .await? @@ -558,7 +575,6 @@ impl ConfiguredLoginParam { .get_config_parsed::(Config::ConfiguredSendPort) .await? .unwrap_or_default(); - let send_user = legacy_send_user.unwrap_or_default(); let send_security: Socket = context .get_config_parsed::(Config::ConfiguredSendSecurity) .await? @@ -571,7 +587,7 @@ impl ConfiguredLoginParam { port: mail_port, security: mail_security.try_into()?, }, - user: mail_user, + user: mail_user.clone(), }]; smtp = vec![ConfiguredServerLoginParam { connection: ConnectionCandidate { @@ -579,7 +595,7 @@ impl ConfiguredLoginParam { port: send_port, security: send_security.try_into()?, }, - user: send_user, + user: send_user.clone(), }]; } @@ -588,8 +604,10 @@ impl ConfiguredLoginParam { Ok(Some(ConfiguredLoginParam { addr, imap, + imap_user: mail_user, imap_password: mail_pw, smtp, + smtp_user: send_user, smtp_password: send_pw, certificate_checks, provider, @@ -615,9 +633,16 @@ impl ConfiguredLoginParam { ) .await?; + context + .set_config(Config::ConfiguredMailUser, Some(&self.imap_user)) + .await?; context .set_config(Config::ConfiguredMailPw, Some(&self.imap_password)) .await?; + + context + .set_config(Config::ConfiguredSendUser, Some(&self.smtp_user)) + .await?; context .set_config(Config::ConfiguredSendPw, Some(&self.smtp_password)) .await?; @@ -643,7 +668,6 @@ impl ConfiguredLoginParam { context .set_config(Config::ConfiguredMailSecurity, None) .await?; - context.set_config(Config::ConfiguredMailUser, None).await?; context .set_config(Config::ConfiguredSendServer, None) .await?; @@ -651,7 +675,6 @@ impl ConfiguredLoginParam { context .set_config(Config::ConfiguredSendSecurity, None) .await?; - context.set_config(Config::ConfiguredSendUser, None).await?; let server_flags = match self.oauth2 { true => DC_LP_AUTH_OAUTH2, @@ -748,6 +771,7 @@ mod tests { }, user: "alice".to_string(), }], + imap_user: "".to_string(), imap_password: "foo".to_string(), smtp: vec![ConfiguredServerLoginParam { connection: ConnectionCandidate { @@ -757,6 +781,7 @@ mod tests { }, user: "alice@example.org".to_string(), }], + smtp_user: "".to_string(), smtp_password: "bar".to_string(), // socks5_config is not saved by `save_to_database`, using default value socks5_config: None, @@ -840,6 +865,7 @@ mod tests { user: user.to_string(), }, ], + imap_user: "alice@posteo.de".to_string(), imap_password: "foobarbaz".to_string(), smtp: vec![ ConfiguredServerLoginParam { @@ -859,6 +885,7 @@ mod tests { user: user.to_string(), }, ], + smtp_user: "alice@posteo.de".to_string(), smtp_password: "foobarbaz".to_string(), socks5_config: None, provider: get_provider_by_id("posteo"),