feat(bichonctl): add support for decoding MIME-encoded X-Gmail-Labels in mbox #182

This commit is contained in:
rustmailer
2026-03-13 13:19:49 +08:00
parent 638a93f184
commit 2b10d201ee
2 changed files with 14 additions and 8 deletions
+10 -5
View File
@@ -27,6 +27,7 @@ use crate::modules::cli::BichonCtlConfig;
use console::style; use console::style;
use dialoguer::{theme::ColorfulTheme, Input}; use dialoguer::{theme::ColorfulTheme, Input};
use dialoguer::{Confirm, Select}; use dialoguer::{Confirm, Select};
use mail_parser::parsers::MessageStream;
use mail_parser::MessageParser; use mail_parser::MessageParser;
use reqwest::Client; use reqwest::Client;
@@ -152,11 +153,15 @@ pub async fn run_import(
let folder_name = match target_folder { let folder_name = match target_folder {
Some(ref folder_name) => folder_name.clone(), Some(ref folder_name) => folder_name.clone(),
None => { None => {
let labels = message let gmail_labels = message.header_raw("X-Gmail-Labels").unwrap_or("INBOX");
.header("X-Gmail-Labels") let text_cow = MessageStream::new(gmail_labels.as_bytes())
.and_then(|h| h.as_text()) .parse_unstructured()
.unwrap_or("Inbox"); .into_text();
determine_folder(labels) let data: &str = match &text_cow {
Some(c) => c.as_ref(),
None => "INBOX",
};
determine_folder(data)
} }
}; };
let b64_eml = base64_encode_url_safe!(&body); let b64_eml = base64_encode_url_safe!(&body);
+4 -3
View File
@@ -52,6 +52,7 @@ async fn test1() {
#[tokio::test] #[tokio::test]
async fn test2() { async fn test2() {
const MESSAGE: &str = r#"From: Art Vandelay <art@vandelay.com> (Vandelay Industries) const MESSAGE: &str = r#"From: Art Vandelay <art@vandelay.com> (Vandelay Industries)
X-Gmail-Labels: =?UTF-8?Q?Archiv=C3=A9s,Envoy=C3=A9?=
To: "Colleagues": "James Smythe" <james@vandelay.com>; Friends: To: "Colleagues": "James Smythe" <james@vandelay.com>; Friends:
jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= <john@example.com>; jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= <john@example.com>;
Date: Sat, 20 Nov 2021 14:22:01 -0800 Date: Sat, 20 Nov 2021 14:22:01 -0800
@@ -93,16 +94,16 @@ R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
"#; "#;
let message = MessageParser::default().parse(MESSAGE).unwrap(); let message = MessageParser::default().parse(MESSAGE).unwrap();
let raw_subject = message.header_raw("Subject").unwrap().as_bytes(); let header = message.header_raw("X-Gmail-Labels").unwrap().as_bytes();
let data = MessageStream::new(raw_subject) let data = MessageStream::new(header)
.parse_unstructured() .parse_unstructured()
.unwrap_text() .unwrap_text()
.to_string(); .to_string();
println!("{}", data); println!("{}", data);
// RFC2047 support for encoded text in message readers // RFC2047 support for encoded text in message readers
println!("{}", message.subject().unwrap()); //println!("{}", message.subject().unwrap());
} }
#[tokio::test] #[tokio::test]