Skip to content

Commit

Permalink
fix: mark holiday notice messages as bot-generated
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Dec 29, 2024
1 parent 779635d commit 4ce1dd1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
23 changes: 23 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2707,6 +2707,29 @@ mod tests {
async fn test_is_bot() -> Result<()> {
let alice = TestContext::new_alice().await;

// Alice receives an auto-generated non-chat message.
//
// This could be a holiday notice,
// in which case the message should be marked as bot-generated,
// but the contact should not.
receive_imf(
&alice,
b"From: Claire <[email protected]>\n\
To: [email protected]\n\
Message-ID: <[email protected]>\n\
Auto-Submitted: auto-generated\n\
Date: Fri, 29 Jan 2021 21:37:55 +0000\n\
\n\
hello\n",
false,
)
.await?;
let msg = alice.get_last_msg().await;
assert_eq!(msg.get_text(), "hello".to_string());
assert!(msg.is_bot());
let contact = Contact::get_by_id(&alice, msg.from_id).await?;
assert!(!contact.is_bot());

// Alice receives a message from Bob the bot.
receive_imf(
&alice,
Expand Down
16 changes: 11 additions & 5 deletions src/mimeparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,15 @@ pub(crate) struct MimeMessage {
/// Hop info for debugging.
pub(crate) hop_info: String,

/// Whether the contact sending this should be marked as bot or non-bot.
/// Whether the message is auto-generated.
///
/// If chat message (with `Chat-Version` header) is auto-generated,
/// the contact sending this should be marked as bot.
///
/// If non-chat message is auto-generated,
/// it could be a holiday notice auto-reply,
/// in which case the message should be marked as bot-generated,
/// but the contact should not be.
pub(crate) is_bot: Option<bool>,

/// When the message was received, in secs since epoch.
Expand Down Expand Up @@ -563,10 +571,8 @@ impl MimeMessage {
};

if parser.mdn_reports.is_empty() && parser.webxdc_status_update.is_none() {
// "Auto-Submitted" is also set by holiday-notices so we also check "chat-version".
let is_bot = parser.headers.get("auto-submitted")
== Some(&"auto-generated".to_string())
&& parser.headers.contains_key("chat-version");
let is_bot =
parser.headers.get("auto-submitted") == Some(&"auto-generated".to_string());
parser.is_bot = Some(is_bot);
}
parser.maybe_remove_bad_parts();
Expand Down
6 changes: 5 additions & 1 deletion src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,11 @@ pub(crate) async fn receive_imf_inner(
.await;

if let Some(is_bot) = mime_parser.is_bot {
from_id.mark_bot(context, is_bot).await?;
// If the message is auto-generated and was generated by Delta Chat,
// mark the contact as a bot.
if mime_parser.get_header(HeaderDef::ChatVersion).is_some() {
from_id.mark_bot(context, is_bot).await?;
}
}

Ok(Some(received_msg))
Expand Down

0 comments on commit 4ce1dd1

Please sign in to comment.