Skip to content

Commit

Permalink
Special char decoding (rfc2047)
Browse files Browse the repository at this point in the history
  • Loading branch information
UwUDev committed Sep 28, 2024
1 parent 00fdd38 commit 6c59635
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 5 deletions.
111 changes: 109 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ num_cpus = "1.16.0"
percent-encoding = "2.3.1"
mailparse = "0.13"
lazy_static = "1.5.0"
rfc2047-decoder = "1.0.5"

[profile.release]
opt-level = "z"
Expand Down
4 changes: 3 additions & 1 deletion src/smtp/mail.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use mailparse::parse_mail;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use rfc2047_decoder::decode;

#[derive(Default, Serialize, Deserialize)]
pub struct Mail {
Expand Down Expand Up @@ -69,7 +70,8 @@ impl Mail {
pub fn get_subject(data: &str) -> Option<String> {
for line in data.lines() {
if line.to_lowercase().starts_with("subject:") {
return Some(line[8..].trim().to_string());
let subject = line[8..].trim().to_string();
return Some(decode(&subject).unwrap_or(subject));
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/tests/parsing_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod parsing_tester {
fn test_parse_body_multipart() {
let body = std::fs::read_to_string("test/samples/discord_mail.body").unwrap();
let subject = get_subject(&body);
println!("subject: {:?}", subject);
let mail = Mail {
from: Default::default(),
to: Default::default(),
Expand All @@ -20,7 +21,8 @@ mod parsing_tester {
let (from, _) = get_data_from_to(&mail.data);
assert!(from.contains("[email protected]"));

assert_eq!(mail.subject.unwrap(), "Verify Email Address for Discord");
//should've decoded the subject with rfc2047 decoder
assert_eq!(mail.subject.unwrap(), "Vérifie ton adresse e-mail Discord");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion test/samples/discord_mail.body
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Date: Fri, 27 Sep 2024 21:02:51 +0000 (UTC)
From: Discord <[email protected]>
Mime-Version: 1.0
Message-ID: <U5GSwRpZSPWUCqqfA4y23g@geopod-ismtpd-2>
Subject: Verify Email Address for Discord
Subject: =?UTF-8?B?VsOpcmlmaWU=?= ton adresse e-mail Discord
X-SG-EID:
=?us-ascii?Q?u001=2Encph=2Fx3Jw0NdHDwq6twCdiOEjRqduaW+3S=2FWXBRXzv62NXoGt1LlW8ZOp?=
=?us-ascii?Q?GajFHOcRQzla331F6ukAeYmLWbVWcEmwbstknFb?=
Expand Down

0 comments on commit 6c59635

Please sign in to comment.