Skip to content

Commit c09e0e2

Browse files
committed
refactor: move AEAP and peerstate save from mimeparser to receive_imf()
Ideally mimeparser should be functional and have no side effects such as modifying a peerstate in the database.
1 parent 0c8f967 commit c09e0e2

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/mimeparser.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -461,20 +461,6 @@ impl MimeMessage {
461461
parser.decoded_data = mail_raw;
462462
}
463463

464-
crate::peerstate::maybe_do_aeap_transition(context, &mut parser).await?;
465-
if let Some(peerstate) = &parser.decryption_info.peerstate {
466-
peerstate
467-
.handle_fingerprint_change(context, message_time)
468-
.await?;
469-
// When peerstate is set to Mutual, it's saved immediately to not lose that fact in case
470-
// of an error. Otherwise we don't save peerstate until get here to reduce the number of
471-
// calls to save_to_db() and not to degrade encryption if a mail wasn't parsed
472-
// successfully.
473-
if peerstate.prefer_encrypt != EncryptPreference::Mutual {
474-
peerstate.save_to_db(&context.sql).await?;
475-
}
476-
}
477-
478464
Ok(parser)
479465
}
480466

src/receive_imf.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use num_traits::FromPrimitive;
1010
use once_cell::sync::Lazy;
1111
use regex::Regex;
1212

13+
use crate::aheader::EncryptPreference;
1314
use crate::chat::{self, Chat, ChatId, ChatIdBlocked, ProtectionStatus};
1415
use crate::config::Config;
1516
use crate::constants::{Blocked, Chattype, ShowEmails, DC_CHAT_ID_TRASH};
@@ -171,6 +172,29 @@ pub(crate) async fn receive_imf_inner(
171172
Ok(mime_parser) => mime_parser,
172173
};
173174

175+
let rcvd_timestamp = smeared_time(context);
176+
177+
// Sender timestamp is allowed to be a bit in the future due to
178+
// unsynchronized clocks, but not too much.
179+
let sent_timestamp = mime_parser
180+
.get_header(HeaderDef::Date)
181+
.and_then(|value| mailparse::dateparse(value).ok())
182+
.map_or(rcvd_timestamp, |value| min(value, rcvd_timestamp + 60));
183+
184+
crate::peerstate::maybe_do_aeap_transition(context, &mut mime_parser).await?;
185+
if let Some(peerstate) = &mime_parser.decryption_info.peerstate {
186+
peerstate
187+
.handle_fingerprint_change(context, sent_timestamp)
188+
.await?;
189+
// When peerstate is set to Mutual, it's saved immediately to not lose that fact in case
190+
// of an error. Otherwise we don't save peerstate until get here to reduce the number of
191+
// calls to save_to_db() and not to degrade encryption if a mail wasn't parsed
192+
// successfully.
193+
if peerstate.prefer_encrypt != EncryptPreference::Mutual {
194+
peerstate.save_to_db(&context.sql).await?;
195+
}
196+
}
197+
174198
info!(context, "Receiving message {rfc724_mid:?}, seen={seen}...");
175199

176200
// check, if the mail is already in our database.
@@ -232,15 +256,6 @@ pub(crate) async fn receive_imf_inner(
232256
)
233257
.await?;
234258

235-
let rcvd_timestamp = smeared_time(context);
236-
237-
// Sender timestamp is allowed to be a bit in the future due to
238-
// unsynchronized clocks, but not too much.
239-
let sent_timestamp = mime_parser
240-
.get_header(HeaderDef::Date)
241-
.and_then(|value| mailparse::dateparse(value).ok())
242-
.map_or(rcvd_timestamp, |value| min(value, rcvd_timestamp + 60));
243-
244259
update_verified_keys(context, &mut mime_parser, from_id).await?;
245260

246261
let received_msg;

0 commit comments

Comments
 (0)