Skip to content

Commit 5c44bb3

Browse files
committed
WIP: feat: add option to process unencrypted messages
1 parent 84bc3f8 commit 5c44bb3

16 files changed

Lines changed: 306 additions & 102 deletions

File tree

src/chat/chat_tests.rs

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ async fn chatlist_len(ctx: &Context, listflags: usize) -> usize {
10491049
async fn test_archive() {
10501050
// create two chats
10511051
let t = TestContext::new_alice().await;
1052+
10521053
let mut msg = Message::new_text("foo".to_string());
10531054
let msg_id = add_device_msg(&t, None, Some(&mut msg)).await.unwrap();
10541055
let chat_id1 = message::Message::load_from_db(&t, msg_id)
@@ -1381,6 +1382,9 @@ async fn test_markfresh_chat() -> Result<()> {
13811382
async fn test_archive_fresh_msgs() -> Result<()> {
13821383
let t = TestContext::new_alice().await;
13831384

1385+
// FIXME: use encrypted messages
1386+
t.set_config(Config::ProcessUnencrypted, Some("1")).await?;
1387+
13841388
async fn msg_from(t: &TestContext, name: &str, num: u32) -> Result<()> {
13851389
receive_imf(
13861390
t,
@@ -1873,52 +1877,46 @@ async fn test_lookup_self_by_contact_id() {
18731877

18741878
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
18751879
async fn test_marknoticed_chat() -> Result<()> {
1876-
let t = TestContext::new_alice().await;
1877-
let chat = t.create_chat_with_contact("bob", "bob@example.org").await;
1880+
let mut tcm = TestContextManager::new();
1881+
let alice = &tcm.alice().await;
1882+
let bob = &tcm.bob().await;
1883+
let chat = alice.create_chat(bob).await;
18781884

1879-
receive_imf(
1880-
&t,
1881-
b"From: bob@example.org\n\
1882-
To: alice@example.org\n\
1883-
Message-ID: <1@example.org>\n\
1884-
Chat-Version: 1.0\n\
1885-
Date: Fri, 23 Apr 2021 10:00:57 +0000\n\
1886-
\n\
1887-
hello\n",
1888-
false,
1889-
)
1890-
.await?;
1885+
let bob_chat_id = bob.create_chat_id(alice).await;
1886+
let sent = bob.send_text(bob_chat_id, "hello").await;
1887+
alice.recv_msg(&sent).await;
18911888

1892-
let chats = Chatlist::try_load(&t, 0, None, None).await?;
1889+
let chats = Chatlist::try_load(alice, 0, None, None).await?;
18931890
assert_eq!(chats.len(), 1);
18941891
assert_eq!(chats.get_chat_id(0)?, chat.id);
1895-
assert_eq!(chat.id.get_fresh_msg_cnt(&t).await?, 1);
1896-
assert_eq!(t.get_fresh_msgs().await?.len(), 1);
1892+
assert_eq!(chat.id.get_fresh_msg_cnt(alice).await?, 1);
1893+
assert_eq!(alice.get_fresh_msgs().await?.len(), 1);
18971894

1898-
let msgs = get_chat_msgs(&t, chat.id).await?;
1899-
assert_eq!(msgs.len(), 1);
1900-
let msg_id = match msgs.first().unwrap() {
1895+
let msgs = get_chat_msgs(alice, chat.id).await?;
1896+
assert_eq!(msgs.len(), 2);
1897+
let msg_id = match msgs.last().unwrap() {
19011898
ChatItem::Message { msg_id } => *msg_id,
19021899
_ => MsgId::new_unset(),
19031900
};
1904-
let msg = message::Message::load_from_db(&t, msg_id).await?;
1901+
let msg = message::Message::load_from_db(alice, msg_id).await?;
19051902
assert_eq!(msg.state, MessageState::InFresh);
19061903

1907-
marknoticed_chat(&t, chat.id).await?;
1904+
marknoticed_chat(alice, chat.id).await?;
19081905

1909-
let chats = Chatlist::try_load(&t, 0, None, None).await?;
1906+
let chats = Chatlist::try_load(alice, 0, None, None).await?;
19101907
assert_eq!(chats.len(), 1);
1911-
let msg = message::Message::load_from_db(&t, msg_id).await?;
1908+
let msg = message::Message::load_from_db(alice, msg_id).await?;
19121909
assert_eq!(msg.state, MessageState::InNoticed);
1913-
assert_eq!(chat.id.get_fresh_msg_cnt(&t).await?, 0);
1914-
assert_eq!(t.get_fresh_msgs().await?.len(), 0);
1910+
assert_eq!(chat.id.get_fresh_msg_cnt(alice).await?, 0);
1911+
assert_eq!(alice.get_fresh_msgs().await?.len(), 0);
19151912

19161913
Ok(())
19171914
}
19181915

19191916
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
19201917
async fn test_contact_request_fresh_messages() -> Result<()> {
19211918
let t = TestContext::new_alice().await;
1919+
t.set_config(Config::ProcessUnencrypted, Some("1")).await?;
19221920

19231921
let chats = Chatlist::try_load(&t, 0, None, None).await?;
19241922
assert_eq!(chats.len(), 0);
@@ -1970,47 +1968,53 @@ async fn test_contact_request_fresh_messages() -> Result<()> {
19701968

19711969
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
19721970
async fn test_contact_request_archive() -> Result<()> {
1973-
let t = TestContext::new_alice().await;
1971+
let mut tcm = TestContextManager::new();
1972+
let alice = &tcm.alice().await;
1973+
let bob = &tcm.bob().await;
19741974

1975-
receive_imf(
1976-
&t,
1977-
b"From: bob@example.org\n\
1978-
To: alice@example.org\n\
1979-
Message-ID: <2@example.org>\n\
1980-
Chat-Version: 1.0\n\
1981-
Date: Sun, 22 Mar 2021 19:37:57 +0000\n\
1982-
\n\
1983-
hello\n",
1984-
false,
1985-
)
1986-
.await?;
1975+
let bob_chat_id = bob.create_chat_id(alice).await;
1976+
let bob_sent_text = bob.send_text(bob_chat_id, "hello").await;
1977+
alice.recv_msg(&bob_sent_text).await;
19871978

1988-
let chats = Chatlist::try_load(&t, 0, None, None).await?;
1979+
let chats = Chatlist::try_load(alice, 0, None, None).await?;
19891980
assert_eq!(chats.len(), 1);
19901981
let chat_id = chats.get_chat_id(0)?;
1991-
assert!(Chat::load_from_db(&t, chat_id).await?.is_contact_request());
1992-
assert_eq!(get_archived_cnt(&t).await?, 0);
1982+
assert!(
1983+
Chat::load_from_db(alice, chat_id)
1984+
.await?
1985+
.is_contact_request()
1986+
);
1987+
assert_eq!(get_archived_cnt(alice).await?, 0);
19931988

19941989
// archive request without accepting or blocking
1995-
chat_id.set_visibility(&t, ChatVisibility::Archived).await?;
1990+
chat_id
1991+
.set_visibility(alice, ChatVisibility::Archived)
1992+
.await?;
19961993

1997-
let chats = Chatlist::try_load(&t, 0, None, None).await?;
1994+
let chats = Chatlist::try_load(alice, 0, None, None).await?;
19981995
assert_eq!(chats.len(), 1);
19991996
let chat_id = chats.get_chat_id(0)?;
20001997
assert!(chat_id.is_archived_link());
2001-
assert_eq!(get_archived_cnt(&t).await?, 1);
1998+
assert_eq!(get_archived_cnt(alice).await?, 1);
20021999

2003-
let chats = Chatlist::try_load(&t, DC_GCL_ARCHIVED_ONLY, None, None).await?;
2000+
let chats = Chatlist::try_load(alice, DC_GCL_ARCHIVED_ONLY, None, None).await?;
20042001
assert_eq!(chats.len(), 1);
20052002
let chat_id = chats.get_chat_id(0)?;
2006-
assert!(Chat::load_from_db(&t, chat_id).await?.is_contact_request());
2003+
assert!(
2004+
Chat::load_from_db(alice, chat_id)
2005+
.await?
2006+
.is_contact_request()
2007+
);
20072008

20082009
Ok(())
20092010
}
20102011

20112012
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
20122013
async fn test_classic_email_chat() -> Result<()> {
20132014
let alice = TestContext::new_alice().await;
2015+
alice
2016+
.set_config(Config::ProcessUnencrypted, Some("1"))
2017+
.await?;
20142018

20152019
// Alice receives a classic (non-chat) message from Bob.
20162020
receive_imf(

src/chatlist.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ mod tests {
473473
add_contact_to_chat, create_broadcast, create_group, get_chat_contacts,
474474
remove_contact_from_chat, send_text_msg, set_chat_name,
475475
};
476+
use crate::config::Config;
476477
use crate::receive_imf::receive_imf;
477478
use crate::securejoin::get_securejoin_qr;
478479
use crate::stock_str::StockMessage;
@@ -665,6 +666,7 @@ mod tests {
665666
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
666667
async fn test_search_single_chat() -> anyhow::Result<()> {
667668
let t = TestContext::new_alice().await;
669+
t.set_config(Config::ProcessUnencrypted, Some("1")).await?;
668670

669671
// receive a one-to-one-message
670672
receive_imf(
@@ -725,6 +727,7 @@ mod tests {
725727
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
726728
async fn test_search_single_chat_without_authname() -> anyhow::Result<()> {
727729
let t = TestContext::new_alice().await;
730+
t.set_config(Config::ProcessUnencrypted, Some("1")).await?;
728731

729732
// receive a one-to-one-message without authname set
730733
receive_imf(

src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,11 @@ pub enum Config {
486486
/// Experimental option denoting that the current profile is shared between multiple team members.
487487
/// For now, the only effect of this option is that seen flags are not synchronized.
488488
TeamProfile,
489+
490+
/// Process unencrypted messages.
491+
///
492+
/// Unencrypted messages are fetched and processed only if this setting is explicitly enabled.
493+
ProcessUnencrypted,
489494
}
490495

491496
impl Config {

src/context.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,12 @@ impl Context {
10561056
"team_profile",
10571057
self.get_config_bool(Config::TeamProfile).await?.to_string(),
10581058
);
1059+
res.insert(
1060+
"process_unencrypted",
1061+
self.get_config_bool(Config::ProcessUnencrypted)
1062+
.await?
1063+
.to_string(),
1064+
);
10591065

10601066
let elapsed = time_elapsed(&self.creation_time);
10611067
res.insert("uptime", duration_to_str(elapsed));

src/decrypt.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ mod tests {
370370
use super::*;
371371
use crate::receive_imf::receive_imf;
372372
use crate::test_utils::TestContext;
373+
use crate::config::Config;
373374

374375
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
375376
async fn test_mixed_up_mime() -> Result<()> {
@@ -402,6 +403,7 @@ mod tests {
402403
assert!(get_attachment_mime(&mail).is_some());
403404

404405
let bob = TestContext::new_bob().await;
406+
bob.set_config(Config::ProcessUnencrypted, Some("1")).await?;
405407
receive_imf(&bob, attachment_mime, false).await?;
406408
let msg = bob.get_last_msg().await;
407409
// Subject should be prepended because the attachment doesn't have "Chat-Version".
@@ -416,6 +418,7 @@ mod tests {
416418
// Desktop via MS Exchange (actually made with TB though).
417419
let mixed_up_mime = include_bytes!("../test-data/message/mixed-up-long.eml");
418420
let bob = TestContext::new_bob().await;
421+
bob.set_config(Config::ProcessUnencrypted, Some("1")).await?;
419422
receive_imf(&bob, mixed_up_mime, false).await?;
420423
let msg = bob.get_last_msg().await;
421424
assert!(!msg.get_text().is_empty());

src/e2ee.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ Sent with my Delta Chat Messenger: https://delta.chat";
160160
let mut tcm = TestContextManager::new();
161161
let bob = &tcm.bob().await;
162162
bob.set_config_bool(Config::IsChatmail, true).await?;
163+
bob.set_config_bool(Config::ProcessUnencrypted, true)
164+
.await?;
163165
let bob_chat_id = receive_imf(
164166
bob,
165167
b"From: alice@example.org\n\

src/html.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ impl MsgId {
287287
mod tests {
288288
use super::*;
289289
use crate::chat::{self, Chat, forward_msgs, save_msgs};
290+
use crate::config::Config;
290291
use crate::constants;
291292
use crate::contact::ContactId;
292293
use crate::message::{MessengerMessage, Viewtype};
@@ -450,6 +451,9 @@ test some special html-characters as &lt; &gt; and &amp; but also &quot; and &#x
450451
// alice receives a non-delta html-message
451452
let mut tcm = TestContextManager::new();
452453
let alice = &tcm.alice().await;
454+
alice
455+
.set_config(Config::ProcessUnencrypted, Some("1"))
456+
.await?;
453457
let chat = alice
454458
.create_chat_with_contact("", "sender@testrun.org")
455459
.await;
@@ -483,6 +487,8 @@ test some special html-characters as &lt; &gt; and &amp; but also &quot; and &#x
483487

484488
// bob: check that bob also got the html-part of the forwarded message
485489
let bob = &tcm.bob().await;
490+
bob.set_config(Config::ProcessUnencrypted, Some("1"))
491+
.await?;
486492
let chat_bob = bob.create_chat_with_contact("", "alice@example.org").await;
487493
async fn check_receiver(ctx: &TestContext, chat: &Chat, sender: &TestContext) {
488494
let msg = ctx.recv_msg(&sender.pop_sent_msg().await).await;
@@ -520,8 +526,12 @@ test some special html-characters as &lt; &gt; and &amp; but also &quot; and &#x
520526

521527
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
522528
async fn test_html_save_msg() -> Result<()> {
529+
let mut tcm = TestContextManager::new();
530+
let alice = &tcm.alice().await;
531+
alice
532+
.set_config(Config::ProcessUnencrypted, Some("1"))
533+
.await?;
523534
// Alice receives a non-delta html-message
524-
let alice = TestContext::new_alice().await;
525535
let chat = alice
526536
.create_chat_with_contact("", "sender@testrun.org")
527537
.await;
@@ -555,6 +565,10 @@ test some special html-characters as &lt; &gt; and &amp; but also &quot; and &#x
555565
let mut tcm = TestContextManager::new();
556566
// Alice receives a non-delta html-message
557567
let alice = &tcm.alice().await;
568+
alice
569+
.set_config(Config::ProcessUnencrypted, Some("1"))
570+
.await
571+
.unwrap();
558572
let chat = alice
559573
.create_chat_with_contact("", "sender@testrun.org")
560574
.await;
@@ -618,18 +632,22 @@ test some special html-characters as &lt; &gt; and &amp; but also &quot; and &#x
618632

619633
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
620634
async fn test_cp1252_html() -> Result<()> {
621-
let t = TestContext::new_alice().await;
635+
let mut tcm = TestContextManager::new();
636+
let alice = &tcm.alice().await;
637+
alice
638+
.set_config(Config::ProcessUnencrypted, Some("1"))
639+
.await?;
622640
receive_imf(
623-
&t,
641+
alice,
624642
include_bytes!("../test-data/message/cp1252-html.eml"),
625643
false,
626644
)
627645
.await?;
628-
let msg = t.get_last_msg().await;
646+
let msg = alice.get_last_msg().await;
629647
assert_eq!(msg.viewtype, Viewtype::Text);
630648
assert!(msg.text.contains("foo bar ä ö ü ß"));
631649
assert!(msg.has_html());
632-
let html = msg.get_id().get_html(&t).await?.unwrap();
650+
let html = msg.get_id().get_html(alice).await?.unwrap();
633651
println!("{html}");
634652
assert!(html.contains("foo bar ä ö ü ß"));
635653
Ok(())

src/imap.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1996,12 +1996,21 @@ pub(crate) async fn prefetch_should_download(
19961996
// prevent_rename=true as this might be a mailing list message and in this case it would be bad if we rename the contact.
19971997
// (prevent_rename is the last argument of from_field_to_contact_id())
19981998

1999+
let is_encrypted = if let Some(content_type) = headers.get_header_value(HeaderDef::ContentType)
2000+
{
2001+
mailparse::parse_content_type(&content_type).mimetype == "multipart/encrypted"
2002+
} else {
2003+
false
2004+
};
2005+
19992006
if flags.any(|f| f == Flag::Draft) {
20002007
info!(context, "Ignoring draft message");
20012008
return Ok(false);
20022009
}
20032010

2004-
let should_download = !blocked_contact || maybe_ndn;
2011+
let should_download = maybe_ndn
2012+
|| (!blocked_contact
2013+
&& (is_encrypted || context.get_config_bool(Config::ProcessUnencrypted).await?));
20052014
Ok(should_download)
20062015
}
20072016

src/imap/session.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const PREFETCH_FLAGS: &str = "(UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (\
2121
DATE \
2222
X-MICROSOFT-ORIGINAL-MESSAGE-ID \
2323
FROM \
24+
CONTENT-TYPE \
2425
CHAT-VERSION \
2526
CHAT-IS-POST-MESSAGE \
2627
AUTOCRYPT-SETUP-MESSAGE\

0 commit comments

Comments
 (0)