Skip to content

Commit

Permalink
fix: do not allow quotes with "... wrote:" headers in chat messages
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Aug 28, 2024
1 parent 273158a commit 84c1ffd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
19 changes: 19 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2357,6 +2357,25 @@ mod tests {
assert_eq!(quoted_msg.get_text(), msg2.quoted_text().unwrap());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_no_quote() {
let mut tcm = TestContextManager::new();
let alice = &tcm.alice().await;
let bob = &tcm.bob().await;

tcm.send_recv_accept(alice, bob, "Hi!").await;
let msg = tcm
.send_recv(
alice,
bob,
"On 2024-08-28, Alice wrote:\n> A quote.\nNot really.",
)
.await;

assert!(msg.quoted_text().is_none());
assert!(msg.quoted_message(bob).await.unwrap().is_none());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_unencrypted_quote_encrypted_message() -> Result<()> {
let mut tcm = TestContextManager::new();
Expand Down
36 changes: 30 additions & 6 deletions src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub(crate) fn simplify(mut input: String, is_chat_message: bool) -> SimplifiedTe
let lines = split_lines(&input);
let (lines, is_forwarded) = skip_forward_header(&lines);

let (lines, mut top_quote) = remove_top_quote(lines);
let (lines, mut top_quote) = remove_top_quote(lines, is_chat_message);
let original_lines = &lines;
let (lines, footer_lines) = remove_message_footer(lines);
let footer = footer_lines.map(|footer_lines| render_message(footer_lines, false));
Expand Down Expand Up @@ -210,7 +210,10 @@ fn remove_bottom_quote<'a>(lines: &'a [&str]) -> (&'a [&'a str], Option<String>)
}

#[allow(clippy::indexing_slicing)]
fn remove_top_quote<'a>(lines: &'a [&str]) -> (&'a [&'a str], Option<String>) {
fn remove_top_quote<'a>(
lines: &'a [&str],
is_chat_message: bool,
) -> (&'a [&'a str], Option<String>) {
let mut first_quoted_line = 0;
let mut last_quoted_line = None;
let mut has_quoted_headline = false;
Expand All @@ -220,7 +223,11 @@ fn remove_top_quote<'a>(lines: &'a [&str]) -> (&'a [&'a str], Option<String>) {
first_quoted_line = l;
}
last_quoted_line = Some(l)
} else if is_quoted_headline(line) && !has_quoted_headline && last_quoted_line.is_none() {
} else if !is_chat_message
&& is_quoted_headline(line)
&& !has_quoted_headline
&& last_quoted_line.is_none()
{
has_quoted_headline = true
} else {
/* non-quoting line found */
Expand Down Expand Up @@ -396,17 +403,34 @@ mod tests {

#[test]
fn test_remove_top_quote() {
let (lines, top_quote) = remove_top_quote(&["> first", "> second"]);
let (lines, top_quote) = remove_top_quote(&["> first", "> second"], true);
assert!(lines.is_empty());
assert_eq!(top_quote.unwrap(), "first\nsecond");

let (lines, top_quote) = remove_top_quote(&["> first", "> second", "not a quote"]);
let (lines, top_quote) = remove_top_quote(&["> first", "> second", "not a quote"], true);
assert_eq!(lines, &["not a quote"]);
assert_eq!(top_quote.unwrap(), "first\nsecond");

let (lines, top_quote) = remove_top_quote(&["not a quote", "> first", "> second"]);
let (lines, top_quote) = remove_top_quote(&["not a quote", "> first", "> second"], true);
assert_eq!(lines, &["not a quote", "> first", "> second"]);
assert!(top_quote.is_none());

let (lines, top_quote) = remove_top_quote(
&["On 2024-08-28, Bob wrote:", "> quote", "not a quote"],
false,
);
assert_eq!(lines, &["not a quote"]);
assert_eq!(top_quote.unwrap(), "quote");

let (lines, top_quote) = remove_top_quote(
&["On 2024-08-28, Bob wrote:", "> quote", "not a quote"],
true,
);
assert_eq!(
lines,
&["On 2024-08-28, Bob wrote:", "> quote", "not a quote"]
);
assert!(top_quote.is_none());
}

#[test]
Expand Down

0 comments on commit 84c1ffd

Please sign in to comment.