mirror of
https://gitlab.com/mailcat-devs/mailcat.git
synced 2025-12-20 09:53:26 +01:00
Implement extracting HTML bodies
This commit is contained in:
parent
202736e0bd
commit
b1e9a349c8
3 changed files with 41 additions and 47 deletions
|
|
@ -3,7 +3,7 @@ name = "mailcat"
|
|||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
authors = ["Andrey Golovizin <ag@sologoc.com>"]
|
||||
license = "MIT"
|
||||
license = "GPL-3.0+"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
|
|
|
|||
|
|
@ -29,9 +29,6 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
println!("Date: {}", date);
|
||||
}
|
||||
println!("---");
|
||||
print!(
|
||||
"{}",
|
||||
message.body()?.unwrap_or_default().text.unwrap_or_default()
|
||||
);
|
||||
print!("{}", message.body()?.text.unwrap_or_default());
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,13 +3,33 @@ use mailparse::{DispositionType, MailHeaderMap, ParsedMail};
|
|||
#[derive(PartialEq, Eq, Debug, Default)]
|
||||
pub struct Body {
|
||||
pub text: Option<String>,
|
||||
pub html: Option<String>,
|
||||
}
|
||||
|
||||
pub trait ParsedMailExt {
|
||||
fn is_attachment(&self) -> bool;
|
||||
fn subject(&self) -> Option<String>;
|
||||
fn date(&self) -> Result<Option<chrono::DateTime<chrono::FixedOffset>>, anyhow::Error>;
|
||||
fn body(&self) -> Result<Option<Body>, anyhow::Error>;
|
||||
fn body(&self) -> Result<Body, anyhow::Error>;
|
||||
}
|
||||
|
||||
fn find_body(message: &ParsedMail, body: &mut Body) -> Result<(), anyhow::Error> {
|
||||
if message.is_attachment() {
|
||||
return Ok(());
|
||||
}
|
||||
let mimetype: mime::Mime = message.ctype.mimetype.parse()?;
|
||||
if mimetype == mime::TEXT_PLAIN {
|
||||
body.text.get_or_insert(message.get_body()?);
|
||||
} else if mimetype == mime::TEXT_HTML {
|
||||
body.html.get_or_insert(message.get_body()?);
|
||||
}
|
||||
for subpart in &message.subparts {
|
||||
find_body(subpart, body)?;
|
||||
if body.text.is_some() && body.html.is_some() {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl<'a> ParsedMailExt for ParsedMail<'a> {
|
||||
|
|
@ -29,22 +49,10 @@ impl<'a> ParsedMailExt for ParsedMail<'a> {
|
|||
Ok(date)
|
||||
}
|
||||
|
||||
fn body(&self) -> Result<Option<Body>, anyhow::Error> {
|
||||
let mimetype: mime::Mime = self.ctype.mimetype.parse()?;
|
||||
if self.is_attachment() {
|
||||
return Ok(None);
|
||||
}
|
||||
if mimetype == mime::TEXT_PLAIN {
|
||||
return Ok(Some(Body {
|
||||
text: Some(self.get_body()?),
|
||||
}));
|
||||
}
|
||||
for subpart in &self.subparts {
|
||||
if let Some(body) = subpart.body()? {
|
||||
return Ok(Some(body));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
fn body(&self) -> Result<Body, anyhow::Error> {
|
||||
let mut body = Body::default();
|
||||
find_body(self, &mut body)?;
|
||||
Ok(body)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -85,14 +93,7 @@ Prost=FD text.
|
|||
Content-Transfer-Encoding: quoted-printable
|
||||
Content-Type: text/html; charset="UTF-8"
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv=3D"content-type" content=3D"text/html; charset=3DUTF-8">
|
||||
</head>
|
||||
<body><p style=3D"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0=
|
||||
;"><strong>N=C4=9Bjaky HTML text.</strong></p>
|
||||
</body>
|
||||
</html>
|
||||
<strong>Tu=C4=8Dn=C3=BD HTML text</strong>
|
||||
--nextPart3377186.iIbC2pHGDl--
|
||||
|
||||
"#;
|
||||
|
|
@ -124,14 +125,7 @@ UHJvc3TDvSB0ZXh0Lg==
|
|||
Content-Transfer-Encoding: quoted-printable
|
||||
Content-Type: text/html; charset="UTF-8"
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv=3D"content-type" content=3D"text/html; charset=3DUTF-8">
|
||||
</head>
|
||||
<body><p style=3D"margin-top:0;margin-bottom:0;margin-left:0;margin-right:0=
|
||||
;"><strong>Tu=C4=8Dn=C3=BD HTML text</strong></p>
|
||||
</body>
|
||||
</html>
|
||||
<strong>Tu=C4=8Dn=C3=BD HTML text</strong>
|
||||
--nextPart5630828.MhkbZ0Pkbq--
|
||||
|
||||
--nextPart1698715.VLH7GnMWUR
|
||||
|
|
@ -159,9 +153,10 @@ TsSbamFrw6EgcMWZw61sb2hhLgo=
|
|||
);
|
||||
assert_eq!(
|
||||
message.body()?,
|
||||
Some(Body {
|
||||
text: Some("Prostý text.".to_string())
|
||||
}),
|
||||
Body {
|
||||
text: Some("Prostý text.".to_string()),
|
||||
html: None,
|
||||
},
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -180,9 +175,10 @@ TsSbamFrw6EgcMWZw61sb2hhLgo=
|
|||
);
|
||||
assert_eq!(
|
||||
message.body()?,
|
||||
Some(Body {
|
||||
text: Some("Prostý text.".to_string())
|
||||
}),
|
||||
Body {
|
||||
text: Some("Prostý text.".to_string()),
|
||||
html: Some("<strong>Tučný HTML text</strong>".to_string()),
|
||||
},
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -204,9 +200,10 @@ TsSbamFrw6EgcMWZw61sb2hhLgo=
|
|||
);
|
||||
assert_eq!(
|
||||
message.body()?,
|
||||
Some(Body {
|
||||
text: Some("Prostý text.".to_string())
|
||||
}),
|
||||
Body {
|
||||
text: Some("Prostý text.".to_string()),
|
||||
html: Some("<strong>Tučný HTML text</strong>".to_string()),
|
||||
},
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue