From 4bc90701cc735cdff16fc9260ad055548537bcae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Kl=C3=A4hn?= <39526136+Septias@users.noreply.github.com> Date: Sat, 20 Jan 2024 12:47:23 +0100 Subject: [PATCH] feat: Add system message when provider does not allow unencrypted messages (#5161) (#5195) close #5161 ![Screenshot from 2024-01-19 19-56-09](https://github.com/deltachat/deltachat-core-rust/assets/39526136/27ecdd9b-1739-410b-bb26-80d5bdbbc39a) --------- Co-authored-by: bjoern --- deltachat-jsonrpc/src/api/types/message.rs | 2 + src/mimeparser.rs | 4 ++ src/smtp.rs | 43 +++++++++++++++++++++- src/stock_str.rs | 12 ++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/deltachat-jsonrpc/src/api/types/message.rs b/deltachat-jsonrpc/src/api/types/message.rs index 414461e20..210c06e39 100644 --- a/deltachat-jsonrpc/src/api/types/message.rs +++ b/deltachat-jsonrpc/src/api/types/message.rs @@ -345,6 +345,7 @@ pub enum SystemMessageType { SecurejoinMessage, LocationStreamingEnabled, LocationOnly, + InvalidUnencryptedMail, /// Chat ephemeral message timer is changed. EphemeralTimerChanged, @@ -385,6 +386,7 @@ impl From for SystemMessageType { SystemMessage::MultiDeviceSync => SystemMessageType::MultiDeviceSync, SystemMessage::WebxdcStatusUpdate => SystemMessageType::WebxdcStatusUpdate, SystemMessage::WebxdcInfoMessage => SystemMessageType::WebxdcInfoMessage, + SystemMessage::InvalidUnencryptedMail => SystemMessageType::InvalidUnencryptedMail, } } } diff --git a/src/mimeparser.rs b/src/mimeparser.rs index 90c2384c9..f826e44c7 100644 --- a/src/mimeparser.rs +++ b/src/mimeparser.rs @@ -176,6 +176,10 @@ pub enum SystemMessage { /// "%1$s sent a message from another device." ChatProtectionDisabled = 12, + /// Message can't be sent because of `Invalid unencrypted mail to <>` + /// which is sent by chatmail servers. + InvalidUnencryptedMail = 13, + /// Self-sent-message that contains only json used for multi-device-sync; /// if possible, we attach that to other messages as for locations. MultiDeviceSync = 20, diff --git a/src/smtp.rs b/src/smtp.rs index d1e576919..a503df045 100644 --- a/src/smtp.rs +++ b/src/smtp.rs @@ -10,6 +10,7 @@ use async_smtp::{self as smtp, EmailAddress, SmtpTransport}; use tokio::io::BufStream; use tokio::task; +use crate::chat::{add_info_msg_with_cmd, ChatId}; use crate::config::Config; use crate::contact::{Contact, ContactId}; use crate::context::Context; @@ -26,6 +27,7 @@ use crate::provider::Socket; use crate::scheduler::connectivity::ConnectivityStore; use crate::socks::Socks5Config; use crate::sql; +use crate::stock_str::unencrypted_email; /// SMTP connection, write and read timeout. const SMTP_TIMEOUT: Duration = Duration::from_secs(60); @@ -584,7 +586,46 @@ pub(crate) async fn send_msg_to_smtp( match status { SendResult::Retry => {} - SendResult::Success | SendResult::Failure(_) => { + SendResult::Success => { + context + .sql + .execute("DELETE FROM smtp WHERE id=?", (rowid,)) + .await?; + } + SendResult::Failure(ref err) => { + if err.to_string().contains("Invalid unencrypted mail") { + let res = context + .sql + .query_row_optional( + "SELECT chat_id, timestamp FROM msgs WHERE id=?;", + (msg_id,), + |row| Ok((row.get::<_, ChatId>(0)?, row.get::<_, i64>(1)?)), + ) + .await?; + + if let Some((chat_id, timestamp_sort)) = res { + let addr = context.get_config(Config::ConfiguredAddr).await?; + let text = unencrypted_email( + context, + addr.unwrap_or_default() + .split('@') + .nth(1) + .unwrap_or_default(), + ) + .await; + add_info_msg_with_cmd( + context, + chat_id, + &text, + crate::mimeparser::SystemMessage::InvalidUnencryptedMail, + timestamp_sort, + None, + None, + None, + ) + .await?; + }; + } context .sql .execute("DELETE FROM smtp WHERE id=?", (rowid,)) diff --git a/src/stock_str.rs b/src/stock_str.rs index 0ba3747ca..f64a3685c 100644 --- a/src/stock_str.rs +++ b/src/stock_str.rs @@ -419,6 +419,11 @@ pub enum StockMessage { #[strum(props(fallback = "Member %1$s added."))] MsgAddMember = 173, + + #[strum(props( + fallback = "⚠️ Your email provider %1$s requires end-to-end encryption which is not setup yet." + ))] + InvalidUnencryptedMail = 174, } impl StockMessage { @@ -1285,6 +1290,13 @@ pub(crate) async fn aeap_addr_changed( .replace3(new_addr) } +/// Stock string: `⚠️ Your email provider %1$s requires end-to-end encryption which is not setup yet. Tap to learn more.`. +pub(crate) async fn unencrypted_email(context: &Context, provider: &str) -> String { + translated(context, StockMessage::InvalidUnencryptedMail) + .await + .replace1(provider) +} + pub(crate) async fn aeap_explanation_and_link( context: &Context, old_addr: &str,