From 12e8517066c5fac10d8dcf7c9e71c51e11b75cb4 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Mon, 28 Oct 2024 20:40:16 +0100 Subject: [PATCH 1/2] fix: Prevent accidental wrong-password-notifications Over the past years, it happend two times that a user came to me worried about the "Cannot login as ***. Please check if the e-mail address and the password are correct." message. I'm not sure why this happened, but I think we should make the logic for showing this notification stricter: - Before: The notification is shown if connection fails two times in a row, and the second error contains the word "authentication". - Now: The notification is shown if the connection fails two times in a row, and _both_ error messages contain the word "authentication". The second commit just renames `login_failed_once` to `authentication_failed_once` in order to reflect this change. --- src/imap.rs | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/imap.rs b/src/imap.rs index afb9858cd1..4fc09e8648 100644 --- a/src/imap.rs +++ b/src/imap.rs @@ -416,35 +416,38 @@ impl Imap { let imap_user = lp.user.to_owned(); let message = stock_str::cannot_login(context, &imap_user).await; - let err_str = err.to_string(); warn!(context, "IMAP failed to login: {err:#}."); first_error.get_or_insert(format_err!("{message} ({err:#})")); + // If it looks like the password is wrong, send a notification: let _lock = context.wrong_pw_warning_mutex.lock().await; - if !configuring - && self.login_failed_once - && err_str.to_lowercase().contains("authentication") - && context.get_config_bool(Config::NotifyAboutWrongPw).await? - { - let mut msg = Message::new_text(message); - if let Err(e) = chat::add_device_msg_with_importance( - context, - None, - Some(&mut msg), - true, - ) - .await + if err.to_string().to_lowercase().contains("authentication") { + if self.login_failed_once + && !configuring + && context.get_config_bool(Config::NotifyAboutWrongPw).await? { - warn!(context, "Failed to add device message: {e:#}."); + let mut msg = Message::new_text(message); + if let Err(e) = chat::add_device_msg_with_importance( + context, + None, + Some(&mut msg), + true, + ) + .await + { + warn!(context, "Failed to add device message: {e:#}."); + } else { + context + .set_config_internal(Config::NotifyAboutWrongPw, None) + .await + .log_err(context) + .ok(); + } } else { - context - .set_config_internal(Config::NotifyAboutWrongPw, None) - .await - .log_err(context) - .ok(); + self.login_failed_once = true; } } else { - self.login_failed_once = true; + self.login_failed_once = false; } } } From b3e196a9f7402ba6d586fe74fb6354b0e02ca223 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Mon, 28 Oct 2024 22:09:12 +0100 Subject: [PATCH 2/2] Rename login_failed_once to authentication_failed_once --- src/imap.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/imap.rs b/src/imap.rs index 4fc09e8648..4e5001fab6 100644 --- a/src/imap.rs +++ b/src/imap.rs @@ -89,7 +89,7 @@ pub(crate) struct Imap { oauth2: bool, - login_failed_once: bool, + authentication_failed_once: bool, pub(crate) connectivity: ConnectivityStore, @@ -254,7 +254,7 @@ impl Imap { proxy_config, strict_tls, oauth2, - login_failed_once: false, + authentication_failed_once: false, connectivity: Default::default(), conn_last_try: UNIX_EPOCH, conn_backoff_ms: 0, @@ -402,7 +402,7 @@ impl Imap { let mut lock = context.server_id.write().await; lock.clone_from(&session.capabilities.server_id); - self.login_failed_once = false; + self.authentication_failed_once = false; context.emit_event(EventType::ImapConnected(format!( "IMAP-LOGIN as {}", lp.user @@ -422,7 +422,7 @@ impl Imap { // If it looks like the password is wrong, send a notification: let _lock = context.wrong_pw_warning_mutex.lock().await; if err.to_string().to_lowercase().contains("authentication") { - if self.login_failed_once + if self.authentication_failed_once && !configuring && context.get_config_bool(Config::NotifyAboutWrongPw).await? { @@ -444,10 +444,10 @@ impl Imap { .ok(); } } else { - self.login_failed_once = true; + self.authentication_failed_once = true; } } else { - self.login_failed_once = false; + self.authentication_failed_once = false; } } }