Compare commits
48 Commits
letterbox-
...
letterbox-
| Author | SHA1 | Date | |
|---|---|---|---|
| 34bda32e30 | |||
| 501ee417c9 | |||
| ecc0a88341 | |||
| d36d508df0 | |||
| b9b12dd717 | |||
| 633e055472 | |||
| 951ee70279 | |||
| 3a41ab1767 | |||
| 5c9955a89e | |||
| 1f75627fd2 | |||
| 5c42d04598 | |||
| 4d888fbea3 | |||
| 8f53678e53 | |||
| 8218fca2ef | |||
| 01164d6afa | |||
| 2f06ae93ae | |||
| 75d4fe49e2 | |||
| 9f2016940b | |||
| ba9cc0127b | |||
| ce17c4a7d8 | |||
| c8850404b8 | |||
| 638e94b4ae | |||
| d0f4716d83 | |||
| 59e35062e7 | |||
| 43827b4d87 | |||
| b29e92cd9c | |||
| 42bea43de9 | |||
| 4048edde11 | |||
| 90768d0d1b | |||
| 70e6271ca3 | |||
| 0bda21e5e9 | |||
| f987b4e4b4 | |||
| a873ec9208 | |||
| d8d26e1f59 | |||
| 1322dde5c5 | |||
| a2147081e8 | |||
| 8c6a24e400 | |||
| 8a08d97930 | |||
| d24a851cd7 | |||
| f6ff597f66 | |||
| 387d133f09 | |||
| a9674e8b7b | |||
| 457f9ac1c2 | |||
| d62759565f | |||
| 4fd97700f7 | |||
| 99b9a88663 | |||
| 56e6036892 | |||
| 232e436378 |
751
Cargo.lock
generated
751
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -8,7 +8,7 @@ authors = ["Bill Thiede <git@xinu.tv>"]
|
||||
edition = "2021"
|
||||
license = "UNLICENSED"
|
||||
publish = ["xinu"]
|
||||
version = "0.17.27"
|
||||
version = "0.17.32"
|
||||
repository = "https://git.z.xinu.tv/wathiede/letterbox"
|
||||
|
||||
[profile.dev]
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\nSELECT\n url\nFROM email_photo ep\nJOIN email_address ea\nON ep.id = ea.email_photo_id\nWHERE\n address = $1\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "url",
|
||||
"type_info": "Text"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "126e16a4675e8d79f330b235f9e1b8614ab1e1526e4e69691c5ebc70d54a42ef"
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n SELECT rule as \"rule: Json<Rule>\"\n FROM email_rule\n ORDER BY sort_order\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "rule: Json<Rule>",
|
||||
"type_info": "Jsonb"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": []
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "6c5b0a96f45f78795732ea428cc01b4eab28b7150aa37387e7439a6b0b58e88c"
|
||||
}
|
||||
@@ -14,6 +14,7 @@ version.workspace = true
|
||||
[dependencies]
|
||||
ammonia = "4.1.0"
|
||||
anyhow = "1.0.98"
|
||||
askama = { version = "0.14.0", features = ["derive"] }
|
||||
async-graphql = { version = "7", features = ["log"] }
|
||||
async-graphql-axum = "7.0.16"
|
||||
async-trait = "0.1.88"
|
||||
@@ -23,17 +24,19 @@ build-info = "0.0.41"
|
||||
cacher = { version = "0.2.0", registry = "xinu" }
|
||||
chrono = "0.4.40"
|
||||
clap = { version = "4.5.37", features = ["derive"] }
|
||||
css-inline = "0.16.0"
|
||||
css-inline = "0.17.0"
|
||||
flate2 = "1.1.2"
|
||||
futures = "0.3.31"
|
||||
headers = "0.4.0"
|
||||
html-escape = "0.2.13"
|
||||
letterbox-notmuch = { path = "../notmuch", version = "0.17.27", registry = "xinu" }
|
||||
letterbox-shared = { path = "../shared", version = "0.17.27", registry = "xinu" }
|
||||
letterbox-notmuch = { path = "../notmuch", version = "0.17.32", registry = "xinu" }
|
||||
letterbox-shared = { path = "../shared", version = "0.17.32", registry = "xinu" }
|
||||
linkify = "0.10.0"
|
||||
lol_html = "2.3.0"
|
||||
mailparse = "0.16.1"
|
||||
maplit = "1.0.2"
|
||||
memmap = "0.7.0"
|
||||
quick-xml = { version = "0.38.1", features = ["serialize"] }
|
||||
regex = "1.11.1"
|
||||
reqwest = { version = "0.12.15", features = ["blocking"] }
|
||||
scraper = "0.23.1"
|
||||
@@ -50,6 +53,8 @@ urlencoding = "2.1.3"
|
||||
#xtracing = { git = "http://git-private.h.xinu.tv/wathiede/xtracing.git" }
|
||||
#xtracing = { path = "../../xtracing" }
|
||||
xtracing = { version = "0.3.2", registry = "xinu" }
|
||||
zip = "4.3.0"
|
||||
|
||||
|
||||
[build-dependencies]
|
||||
build-info-build = "0.0.41"
|
||||
|
||||
@@ -2,4 +2,5 @@ fn main() {
|
||||
// Calling `build_info_build::build_script` collects all data and makes it available to `build_info::build_info!`
|
||||
// and `build_info::format!` in the main program.
|
||||
build_info_build::build_script();
|
||||
println!("cargo:rerun-if-changed=templates");
|
||||
}
|
||||
|
||||
1
server/sql/label_unprocessed.sql
Normal file
1
server/sql/label_unprocessed.sql
Normal file
@@ -0,0 +1 @@
|
||||
SELECT rule as "rule: Json<Rule>" FROM email_rule ORDER BY sort_order
|
||||
1
server/sql/photo_url_for_email_address.sql
Normal file
1
server/sql/photo_url_for_email_address.sql
Normal file
@@ -0,0 +1 @@
|
||||
SELECT url FROM email_photo ep JOIN email_address ea ON ep.id = ea.email_photo_id WHERE address = $1
|
||||
1220
server/src/email_extract.rs
Normal file
1220
server/src/email_extract.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -39,4 +39,10 @@ pub enum ServerError {
|
||||
QueryParseError(#[from] QueryParserError),
|
||||
#[error("impossible: {0}")]
|
||||
InfaillibleError(#[from] Infallible),
|
||||
#[error("askama error: {0}")]
|
||||
AskamaError(#[from] askama::Error),
|
||||
#[error("xml error: {0}")]
|
||||
XmlError(#[from] quick_xml::Error),
|
||||
#[error("xml encoding error: {0}")]
|
||||
XmlEncodingError(#[from] quick_xml::encoding::EncodingError),
|
||||
}
|
||||
|
||||
@@ -237,6 +237,22 @@ impl Body {
|
||||
content_tree: "".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn to_html(&self) -> Option<String> {
|
||||
match self {
|
||||
Body::Html(h) => Some(h.html.clone()),
|
||||
Body::PlainText(p) => Some(format!("<pre>{}</pre>", html_escape::encode_text(&p.text))),
|
||||
Body::UnhandledContentType(u) => Some(format!("<pre>{}</pre>", html_escape::encode_text(&u.text))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_html_content_tree(&self) -> Option<String> {
|
||||
match self {
|
||||
Body::Html(h) => Some(h.content_tree.clone()),
|
||||
Body::PlainText(p) => Some(p.content_tree.clone()),
|
||||
Body::UnhandledContentType(u) => Some(u.content_tree.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, SimpleObject)]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod config;
|
||||
pub mod email_extract;
|
||||
pub mod error;
|
||||
pub mod graphql;
|
||||
pub mod newsreader;
|
||||
|
||||
810
server/src/nm.rs
810
server/src/nm.rs
@@ -1,35 +1,31 @@
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
fs::File,
|
||||
io::{Cursor, Read},
|
||||
};
|
||||
|
||||
use letterbox_notmuch::Notmuch;
|
||||
use letterbox_shared::{compute_color, Rule};
|
||||
use mailparse::{parse_content_type, parse_mail, MailHeader, MailHeaderMap, ParsedMail};
|
||||
use mailparse::{parse_mail, MailHeader, MailHeaderMap};
|
||||
use memmap::MmapOptions;
|
||||
use sqlx::{types::Json, PgPool};
|
||||
use tracing::{error, info, info_span, instrument, warn};
|
||||
use zip::ZipArchive;
|
||||
|
||||
use crate::{
|
||||
compute_offset_limit,
|
||||
email_extract::*,
|
||||
error::ServerError,
|
||||
graphql::{
|
||||
Attachment, Body, Corpus, DispositionType, Email, EmailThread, Header, Html, Message,
|
||||
PlainText, Tag, Thread, ThreadSummary, UnhandledContentType,
|
||||
Attachment, Body, Corpus, EmailThread, Header, Html, Message, PlainText, Tag, Thread,
|
||||
ThreadSummary, UnhandledContentType,
|
||||
},
|
||||
linkify_html, InlineStyle, Query, SanitizeHtml, Transformer,
|
||||
};
|
||||
|
||||
const IMAGE_JPEG: &'static str = "image/jpeg";
|
||||
const IMAGE_PJPEG: &'static str = "image/pjpeg";
|
||||
const IMAGE_PNG: &'static str = "image/png";
|
||||
const MESSAGE_RFC822: &'static str = "message/rfc822";
|
||||
const MULTIPART_ALTERNATIVE: &'static str = "multipart/alternative";
|
||||
const MULTIPART_MIXED: &'static str = "multipart/mixed";
|
||||
const MULTIPART_RELATED: &'static str = "multipart/related";
|
||||
const TEXT_HTML: &'static str = "text/html";
|
||||
const TEXT_PLAIN: &'static str = "text/plain";
|
||||
|
||||
const APPLICATION_GZIP: &'static str = "application/gzip";
|
||||
const APPLICATION_ZIP: &'static str = "application/zip";
|
||||
const MULTIPART_REPORT: &'static str = "multipart/report";
|
||||
const MAX_RAW_MESSAGE_SIZE: usize = 100_000;
|
||||
|
||||
fn is_notmuch_query(query: &Query) -> bool {
|
||||
@@ -169,7 +165,8 @@ pub async fn thread(
|
||||
// display names (that default to the most commonly seen name).
|
||||
let mut messages = Vec::new();
|
||||
for (path, id) in std::iter::zip(nm.files(&thread_id)?, nm.message_ids(&thread_id)?) {
|
||||
let tags = nm.tags_for_query(&format!("id:{id}"))?;
|
||||
let mut html_report_summary: Option<String> = None;
|
||||
let tags = nm.tags_for_query(&format!("id:{}", id))?;
|
||||
let file = File::open(&path)?;
|
||||
let mmap = unsafe { MmapOptions::new().map(&file)? };
|
||||
let m = parse_mail(&mmap)?;
|
||||
@@ -307,8 +304,114 @@ pub async fn thread(
|
||||
.collect();
|
||||
// TODO(wathiede): parse message and fill out attachments
|
||||
let attachments = extract_attachments(&m, &id)?;
|
||||
|
||||
let mut final_body = body;
|
||||
let mut raw_report_content: Option<String> = None;
|
||||
|
||||
// Append TLS report if available
|
||||
if m.ctype.mimetype.as_str() == MULTIPART_REPORT {
|
||||
if let Ok(Body::Html(_html_body)) = extract_report(&m, &mut part_addr) {
|
||||
// Extract raw JSON for pretty printing
|
||||
if let Some(sp) = m
|
||||
.subparts
|
||||
.iter()
|
||||
.find(|sp| sp.ctype.mimetype.as_str() == "application/tlsrpt+gzip")
|
||||
{
|
||||
if let Ok(gz_bytes) = sp.get_body_raw() {
|
||||
let mut decoder = flate2::read::GzDecoder::new(&gz_bytes[..]);
|
||||
let mut buffer = Vec::new();
|
||||
if decoder.read_to_end(&mut buffer).is_ok() {
|
||||
if let Ok(json_str) = String::from_utf8(buffer) {
|
||||
raw_report_content = Some(json_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Append DMARC report if available
|
||||
if m.ctype.mimetype.as_str() == APPLICATION_ZIP {
|
||||
if let Ok(Body::Html(html_body)) = extract_zip(&m) {
|
||||
html_report_summary = Some(html_body.html);
|
||||
// Extract raw XML for pretty printing
|
||||
if let Ok(zip_bytes) = m.get_body_raw() {
|
||||
if let Ok(mut archive) = ZipArchive::new(Cursor::new(&zip_bytes)) {
|
||||
for i in 0..archive.len() {
|
||||
if let Ok(mut file) = archive.by_index(i) {
|
||||
let name = file.name().to_lowercase();
|
||||
if is_dmarc_report_filename(&name) {
|
||||
let mut xml = String::new();
|
||||
use std::io::Read;
|
||||
if file.read_to_string(&mut xml).is_ok() {
|
||||
raw_report_content = Some(xml);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if m.ctype.mimetype.as_str() == APPLICATION_GZIP {
|
||||
// Call extract_gzip to get the HTML summary and also to determine if it's a DMARC report
|
||||
if let Ok((Body::Html(html_body), _)) = extract_gzip(&m) {
|
||||
html_report_summary = Some(html_body.html);
|
||||
// If extract_gzip successfully parsed a DMARC report, then extract the raw content
|
||||
if let Ok(gz_bytes) = m.get_body_raw() {
|
||||
let mut decoder = flate2::read::GzDecoder::new(&gz_bytes[..]);
|
||||
let mut xml = String::new();
|
||||
use std::io::Read;
|
||||
if decoder.read_to_string(&mut xml).is_ok() {
|
||||
raw_report_content = Some(xml);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut current_html = final_body.to_html().unwrap_or_default();
|
||||
|
||||
if let Some(html_summary) = html_report_summary {
|
||||
current_html.push_str(&html_summary);
|
||||
}
|
||||
|
||||
error!(
|
||||
"mimetype {} raw_report_content.is_some() {}",
|
||||
m.ctype.mimetype.as_str(),
|
||||
raw_report_content.is_some()
|
||||
);
|
||||
if let Some(raw_content) = raw_report_content {
|
||||
let pretty_printed_content = if m.ctype.mimetype.as_str() == MULTIPART_REPORT {
|
||||
// Pretty print JSON
|
||||
if let Ok(parsed_json) = serde_json::from_str::<serde_json::Value>(&raw_content) {
|
||||
serde_json::to_string_pretty(&parsed_json).unwrap_or(raw_content)
|
||||
} else {
|
||||
raw_content
|
||||
}
|
||||
} else {
|
||||
// DMARC reports are XML
|
||||
// Pretty print XML
|
||||
match pretty_print_xml_with_trimming(&raw_content) {
|
||||
Ok(pretty_xml) => pretty_xml,
|
||||
Err(e) => {
|
||||
error!("Failed to pretty print XML: {:?}", e);
|
||||
raw_content
|
||||
}
|
||||
}
|
||||
};
|
||||
current_html.push_str(&format!(
|
||||
"\n<pre>{}</pre>",
|
||||
html_escape::encode_text(&pretty_printed_content)
|
||||
));
|
||||
}
|
||||
final_body = Body::Html(Html {
|
||||
html: current_html,
|
||||
content_tree: final_body.to_html_content_tree().unwrap_or_default(),
|
||||
});
|
||||
|
||||
messages.push(Message {
|
||||
id: format!("id:{id}"),
|
||||
id: format!("id:{}", id),
|
||||
from,
|
||||
to,
|
||||
cc,
|
||||
@@ -316,7 +419,7 @@ pub async fn thread(
|
||||
tags,
|
||||
timestamp,
|
||||
headers,
|
||||
body,
|
||||
body: final_body,
|
||||
path,
|
||||
attachments,
|
||||
delivered_to,
|
||||
@@ -339,65 +442,17 @@ pub async fn thread(
|
||||
}))
|
||||
}
|
||||
|
||||
fn email_addresses(
|
||||
_path: &str,
|
||||
m: &ParsedMail,
|
||||
header_name: &str,
|
||||
) -> Result<Vec<Email>, ServerError> {
|
||||
let mut addrs = Vec::new();
|
||||
for header_value in m.headers.get_all_values(header_name) {
|
||||
match mailparse::addrparse(&header_value) {
|
||||
Ok(mal) => {
|
||||
for ma in mal.into_inner() {
|
||||
match ma {
|
||||
mailparse::MailAddr::Group(gi) => {
|
||||
if !gi.group_name.contains("ndisclosed") {}
|
||||
}
|
||||
mailparse::MailAddr::Single(s) => addrs.push(Email {
|
||||
name: s.display_name,
|
||||
addr: Some(s.addr),
|
||||
photo_url: None,
|
||||
}), //println!("Single: {s}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
let v = header_value;
|
||||
if v.matches('@').count() == 1 {
|
||||
if v.matches('<').count() == 1 && v.ends_with('>') {
|
||||
let idx = v.find('<').unwrap();
|
||||
let addr = &v[idx + 1..v.len() - 1].trim();
|
||||
let name = &v[..idx].trim();
|
||||
addrs.push(Email {
|
||||
name: Some(name.to_string()),
|
||||
addr: Some(addr.to_string()),
|
||||
photo_url: None,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
addrs.push(Email {
|
||||
name: Some(v),
|
||||
addr: None,
|
||||
photo_url: None,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(addrs)
|
||||
}
|
||||
|
||||
pub fn cid_attachment_bytes(nm: &Notmuch, id: &str, cid: &str) -> Result<Attachment, ServerError> {
|
||||
let files = nm.files(id)?;
|
||||
let Some(path) = files.first() else {
|
||||
warn!("failed to find files for message {id}");
|
||||
warn!("failed to find files for message {}", id);
|
||||
return Err(ServerError::PartNotFound);
|
||||
};
|
||||
let file = File::open(&path)?;
|
||||
let mmap = unsafe { MmapOptions::new().map(&file)? };
|
||||
let m = parse_mail(&mmap)?;
|
||||
if let Some(attachment) = walk_attachments(&m, |sp, _cur_idx| {
|
||||
info!("{cid} {:?}", get_content_id(&sp.headers));
|
||||
info!("{} {:?}", cid, get_content_id(&sp.headers));
|
||||
if let Some(h_cid) = get_content_id(&sp.headers) {
|
||||
let h_cid = &h_cid[1..h_cid.len() - 1];
|
||||
if h_cid == cid {
|
||||
@@ -418,7 +473,7 @@ pub fn cid_attachment_bytes(nm: &Notmuch, id: &str, cid: &str) -> Result<Attachm
|
||||
pub fn attachment_bytes(nm: &Notmuch, id: &str, idx: &[usize]) -> Result<Attachment, ServerError> {
|
||||
let files = nm.files(id)?;
|
||||
let Some(path) = files.first() else {
|
||||
warn!("failed to find files for message {id}");
|
||||
warn!("failed to find files for message {}", id);
|
||||
return Err(ServerError::PartNotFound);
|
||||
};
|
||||
let file = File::open(&path)?;
|
||||
@@ -439,452 +494,6 @@ pub fn attachment_bytes(nm: &Notmuch, id: &str, idx: &[usize]) -> Result<Attachm
|
||||
Err(ServerError::PartNotFound)
|
||||
}
|
||||
|
||||
fn extract_body(m: &ParsedMail, part_addr: &mut Vec<String>) -> Result<Body, ServerError> {
|
||||
let body = m.get_body()?;
|
||||
let ret = match m.ctype.mimetype.as_str() {
|
||||
TEXT_PLAIN => return Ok(Body::text(body)),
|
||||
TEXT_HTML => return Ok(Body::html(body)),
|
||||
MULTIPART_MIXED => extract_mixed(m, part_addr),
|
||||
MULTIPART_ALTERNATIVE => extract_alternative(m, part_addr),
|
||||
MULTIPART_RELATED => extract_related(m, part_addr),
|
||||
_ => extract_unhandled(m),
|
||||
};
|
||||
if let Err(err) = ret {
|
||||
error!("Failed to extract body: {err:?}");
|
||||
return Ok(extract_unhandled(m)?);
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
fn extract_unhandled(m: &ParsedMail) -> Result<Body, ServerError> {
|
||||
let msg = format!(
|
||||
"Unhandled body content type:\n{}\n{}",
|
||||
render_content_type_tree(m),
|
||||
m.get_body()?,
|
||||
);
|
||||
Ok(Body::UnhandledContentType(UnhandledContentType {
|
||||
text: msg,
|
||||
content_tree: render_content_type_tree(m),
|
||||
}))
|
||||
}
|
||||
|
||||
// multipart/alternative defines multiple representations of the same message, and clients should
|
||||
// show the fanciest they can display. For this program, the priority is text/html, text/plain,
|
||||
// then give up.
|
||||
fn extract_alternative(m: &ParsedMail, part_addr: &mut Vec<String>) -> Result<Body, ServerError> {
|
||||
let handled_types = vec![
|
||||
MULTIPART_ALTERNATIVE,
|
||||
MULTIPART_MIXED,
|
||||
MULTIPART_RELATED,
|
||||
TEXT_HTML,
|
||||
TEXT_PLAIN,
|
||||
];
|
||||
for sp in &m.subparts {
|
||||
if sp.ctype.mimetype.as_str() == MULTIPART_ALTERNATIVE {
|
||||
return extract_alternative(sp, part_addr);
|
||||
}
|
||||
}
|
||||
for sp in &m.subparts {
|
||||
if sp.ctype.mimetype.as_str() == MULTIPART_MIXED {
|
||||
return extract_mixed(sp, part_addr);
|
||||
}
|
||||
}
|
||||
for sp in &m.subparts {
|
||||
if sp.ctype.mimetype.as_str() == MULTIPART_RELATED {
|
||||
return extract_related(sp, part_addr);
|
||||
}
|
||||
}
|
||||
for sp in &m.subparts {
|
||||
if sp.ctype.mimetype.as_str() == TEXT_HTML {
|
||||
let body = sp.get_body()?;
|
||||
return Ok(Body::html(body));
|
||||
}
|
||||
}
|
||||
for sp in &m.subparts {
|
||||
if sp.ctype.mimetype.as_str() == TEXT_PLAIN {
|
||||
let body = sp.get_body()?;
|
||||
return Ok(Body::text(body));
|
||||
}
|
||||
}
|
||||
Err(ServerError::StringError(format!(
|
||||
"extract_alternative failed to find suitable subpart, searched: {:?}",
|
||||
handled_types
|
||||
)))
|
||||
}
|
||||
|
||||
// multipart/mixed defines multiple types of context all of which should be presented to the user
|
||||
// 'serially'.
|
||||
fn extract_mixed(m: &ParsedMail, part_addr: &mut Vec<String>) -> Result<Body, ServerError> {
|
||||
//todo!("add some sort of visual indicator there are unhandled types, i.e. .ics files");
|
||||
let handled_types = vec![
|
||||
IMAGE_JPEG,
|
||||
IMAGE_PJPEG,
|
||||
IMAGE_PNG,
|
||||
MESSAGE_RFC822,
|
||||
MULTIPART_ALTERNATIVE,
|
||||
MULTIPART_RELATED,
|
||||
TEXT_HTML,
|
||||
TEXT_PLAIN,
|
||||
];
|
||||
let mut unhandled_types: Vec<_> = m
|
||||
.subparts
|
||||
.iter()
|
||||
.map(|sp| sp.ctype.mimetype.as_str())
|
||||
.filter(|mt| !handled_types.contains(&mt))
|
||||
.collect();
|
||||
unhandled_types.sort();
|
||||
if !unhandled_types.is_empty() {
|
||||
warn!("{MULTIPART_MIXED} contains the following unhandled mimetypes {unhandled_types:?}");
|
||||
}
|
||||
let mut parts = Vec::new();
|
||||
for (idx, sp) in m.subparts.iter().enumerate() {
|
||||
part_addr.push(idx.to_string());
|
||||
match sp.ctype.mimetype.as_str() {
|
||||
MESSAGE_RFC822 => parts.push(extract_rfc822(&sp, part_addr)?),
|
||||
MULTIPART_RELATED => parts.push(extract_related(sp, part_addr)?),
|
||||
MULTIPART_ALTERNATIVE => parts.push(extract_alternative(sp, part_addr)?),
|
||||
TEXT_PLAIN => parts.push(Body::text(sp.get_body()?)),
|
||||
TEXT_HTML => parts.push(Body::html(sp.get_body()?)),
|
||||
IMAGE_PJPEG | IMAGE_JPEG | IMAGE_PNG => {
|
||||
let pcd = sp.get_content_disposition();
|
||||
let filename = pcd
|
||||
.params
|
||||
.get("filename")
|
||||
.map(|s| s.clone())
|
||||
.unwrap_or("".to_string());
|
||||
// Only add inline images, attachments are handled as an attribute of the top level Message and rendered separate client-side.
|
||||
if pcd.disposition == mailparse::DispositionType::Inline {
|
||||
// TODO: make URL generation more programatic based on what the frontend has
|
||||
// mapped
|
||||
parts.push(Body::html(format!(
|
||||
r#"<img src="/api/view/attachment/{}/{}/{filename}">"#,
|
||||
part_addr[0],
|
||||
part_addr
|
||||
.iter()
|
||||
.skip(1)
|
||||
.map(|i| i.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(".")
|
||||
)));
|
||||
}
|
||||
}
|
||||
mt => parts.push(unhandled_html(MULTIPART_MIXED, mt)),
|
||||
}
|
||||
part_addr.pop();
|
||||
}
|
||||
Ok(flatten_body_parts(&parts))
|
||||
}
|
||||
|
||||
fn unhandled_html(parent_type: &str, child_type: &str) -> Body {
|
||||
Body::Html(Html {
|
||||
html: format!(
|
||||
r#"
|
||||
<div class="p-4 error">
|
||||
Unhandled mimetype {child_type} in a {parent_type} message
|
||||
</div>
|
||||
"#
|
||||
),
|
||||
content_tree: String::new(),
|
||||
})
|
||||
}
|
||||
fn flatten_body_parts(parts: &[Body]) -> Body {
|
||||
let html = parts
|
||||
.iter()
|
||||
.map(|p| match p {
|
||||
Body::PlainText(PlainText { text, .. }) => {
|
||||
format!(
|
||||
r#"<p class="view-part-text-plain font-mono whitespace-pre-line">{}</p>"#,
|
||||
// Trim newlines to prevent excessive white space at the beginning/end of
|
||||
// presenation. Leave tabs and spaces incase plain text attempts to center a
|
||||
// header on the first line.
|
||||
linkify_html(&html_escape::encode_text(text).trim_matches('\n'))
|
||||
)
|
||||
}
|
||||
Body::Html(Html { html, .. }) => html.clone(),
|
||||
Body::UnhandledContentType(UnhandledContentType { text, .. }) => {
|
||||
error!("text len {}", text.len());
|
||||
format!(
|
||||
r#"<p class="view-part-unhandled">{}</p>"#,
|
||||
// Trim newlines to prevent excessive white space at the beginning/end of
|
||||
// presenation. Leave tabs and spaces incase plain text attempts to center a
|
||||
// header on the first line.
|
||||
linkify_html(&html_escape::encode_text(text).trim_matches('\n'))
|
||||
)
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
info!("flatten_body_parts {}", parts.len());
|
||||
Body::html(html)
|
||||
}
|
||||
|
||||
fn extract_related(m: &ParsedMail, part_addr: &mut Vec<String>) -> Result<Body, ServerError> {
|
||||
// TODO(wathiede): collect related things and change return type to new Body arm.
|
||||
let handled_types = vec![
|
||||
MULTIPART_ALTERNATIVE,
|
||||
TEXT_HTML,
|
||||
TEXT_PLAIN,
|
||||
IMAGE_JPEG,
|
||||
IMAGE_PJPEG,
|
||||
IMAGE_PNG,
|
||||
];
|
||||
let mut unhandled_types: Vec<_> = m
|
||||
.subparts
|
||||
.iter()
|
||||
.map(|sp| sp.ctype.mimetype.as_str())
|
||||
.filter(|mt| !handled_types.contains(&mt))
|
||||
.collect();
|
||||
unhandled_types.sort();
|
||||
if !unhandled_types.is_empty() {
|
||||
warn!("{MULTIPART_RELATED} contains the following unhandled mimetypes {unhandled_types:?}");
|
||||
}
|
||||
|
||||
for (i, sp) in m.subparts.iter().enumerate() {
|
||||
if sp.ctype.mimetype == IMAGE_PNG
|
||||
|| sp.ctype.mimetype == IMAGE_JPEG
|
||||
|| sp.ctype.mimetype == IMAGE_PJPEG
|
||||
{
|
||||
info!("sp.ctype {:#?}", sp.ctype);
|
||||
//info!("sp.headers {:#?}", sp.headers);
|
||||
if let Some(cid) = sp.headers.get_first_value("Content-Id") {
|
||||
let mut part_id = part_addr.clone();
|
||||
part_id.push(i.to_string());
|
||||
info!("cid: {cid} part_id {part_id:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
for sp in &m.subparts {
|
||||
if sp.ctype.mimetype == MULTIPART_ALTERNATIVE {
|
||||
return extract_alternative(m, part_addr);
|
||||
}
|
||||
}
|
||||
for sp in &m.subparts {
|
||||
if sp.ctype.mimetype == TEXT_HTML {
|
||||
let body = sp.get_body()?;
|
||||
return Ok(Body::html(body));
|
||||
}
|
||||
}
|
||||
for sp in &m.subparts {
|
||||
if sp.ctype.mimetype == TEXT_PLAIN {
|
||||
let body = sp.get_body()?;
|
||||
return Ok(Body::text(body));
|
||||
}
|
||||
}
|
||||
Err(ServerError::StringError(format!(
|
||||
"extract_related failed to find suitable subpart, searched: {:?}",
|
||||
handled_types
|
||||
)))
|
||||
}
|
||||
|
||||
fn walk_attachments<T, F: Fn(&ParsedMail, &[usize]) -> Option<T> + Copy>(
|
||||
m: &ParsedMail,
|
||||
visitor: F,
|
||||
) -> Option<T> {
|
||||
let mut cur_addr = Vec::new();
|
||||
walk_attachments_inner(m, visitor, &mut cur_addr)
|
||||
}
|
||||
|
||||
fn walk_attachments_inner<T, F: Fn(&ParsedMail, &[usize]) -> Option<T> + Copy>(
|
||||
m: &ParsedMail,
|
||||
visitor: F,
|
||||
cur_addr: &mut Vec<usize>,
|
||||
) -> Option<T> {
|
||||
for (idx, sp) in m.subparts.iter().enumerate() {
|
||||
cur_addr.push(idx);
|
||||
let val = visitor(sp, &cur_addr);
|
||||
if val.is_some() {
|
||||
return val;
|
||||
}
|
||||
let val = walk_attachments_inner(sp, visitor, cur_addr);
|
||||
if val.is_some() {
|
||||
return val;
|
||||
}
|
||||
cur_addr.pop();
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
// TODO(wathiede): make this walk_attachments that takes a closure.
|
||||
// Then implement one closure for building `Attachment` and imlement another that can be used to
|
||||
// get the bytes for serving attachments of HTTP
|
||||
fn extract_attachments(m: &ParsedMail, id: &str) -> Result<Vec<Attachment>, ServerError> {
|
||||
let mut attachments = Vec::new();
|
||||
for (idx, sp) in m.subparts.iter().enumerate() {
|
||||
if let Some(attachment) = extract_attachment(sp, id, &[idx]) {
|
||||
// Filter out inline attachements, they're flattened into the body of the message.
|
||||
if attachment.disposition == DispositionType::Attachment {
|
||||
attachments.push(attachment);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(attachments)
|
||||
}
|
||||
|
||||
fn extract_attachment(m: &ParsedMail, id: &str, idx: &[usize]) -> Option<Attachment> {
|
||||
let pcd = m.get_content_disposition();
|
||||
let pct = m
|
||||
.get_headers()
|
||||
.get_first_value("Content-Type")
|
||||
.map(|s| parse_content_type(&s));
|
||||
let filename = match (
|
||||
pcd.params.get("filename").map(|f| f.clone()),
|
||||
pct.map(|pct| pct.params.get("name").map(|f| f.clone())),
|
||||
) {
|
||||
// Use filename from Content-Disposition
|
||||
(Some(filename), _) => filename,
|
||||
// Use filename from Content-Type
|
||||
(_, Some(Some(name))) => name,
|
||||
// No known filename, assume it's not an attachment
|
||||
_ => return None,
|
||||
};
|
||||
info!("filename {filename}");
|
||||
|
||||
// TODO: grab this from somewhere
|
||||
let content_id = None;
|
||||
let bytes = match m.get_body_raw() {
|
||||
Ok(bytes) => bytes,
|
||||
Err(err) => {
|
||||
error!("failed to get body for attachment: {err}");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
return Some(Attachment {
|
||||
id: id.to_string(),
|
||||
idx: idx
|
||||
.iter()
|
||||
.map(|i| i.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("."),
|
||||
disposition: pcd.disposition.into(),
|
||||
filename: Some(filename),
|
||||
size: bytes.len(),
|
||||
// TODO: what is the default for ctype?
|
||||
// TODO: do we want to use m.ctype.params for anything?
|
||||
content_type: Some(m.ctype.mimetype.clone()),
|
||||
content_id,
|
||||
bytes,
|
||||
});
|
||||
}
|
||||
fn email_address_strings(emails: &[Email]) -> Vec<String> {
|
||||
emails
|
||||
.iter()
|
||||
.map(|e| e.to_string())
|
||||
.inspect(|e| info!("e {e}"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn extract_rfc822(m: &ParsedMail, part_addr: &mut Vec<String>) -> Result<Body, ServerError> {
|
||||
fn extract_headers(m: &ParsedMail) -> Result<Body, ServerError> {
|
||||
let path = "<in-memory>";
|
||||
let from = email_address_strings(&email_addresses(path, &m, "from")?).join(", ");
|
||||
let to = email_address_strings(&email_addresses(path, &m, "to")?).join(", ");
|
||||
let cc = email_address_strings(&email_addresses(path, &m, "cc")?).join(", ");
|
||||
let date = m.headers.get_first_value("date").unwrap_or(String::new());
|
||||
let subject = m
|
||||
.headers
|
||||
.get_first_value("subject")
|
||||
.unwrap_or(String::new());
|
||||
let text = format!(
|
||||
r#"
|
||||
---------- Forwarded message ----------
|
||||
From: {from}
|
||||
To: {to}
|
||||
CC: {cc}
|
||||
Date: {date}
|
||||
Subject: {subject}
|
||||
"#
|
||||
);
|
||||
Ok(Body::text(text))
|
||||
}
|
||||
let inner_body = m.get_body()?;
|
||||
let inner_m = parse_mail(inner_body.as_bytes())?;
|
||||
let headers = extract_headers(&inner_m)?;
|
||||
let body = extract_body(&inner_m, part_addr)?;
|
||||
|
||||
Ok(flatten_body_parts(&[headers, body]))
|
||||
}
|
||||
|
||||
pub fn get_attachment_filename(header_value: &str) -> &str {
|
||||
info!("get_attachment_filename {header_value}");
|
||||
// Strip last "
|
||||
let v = &header_value[..header_value.len() - 1];
|
||||
if let Some(idx) = v.rfind('"') {
|
||||
&v[idx + 1..]
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_content_type<'a>(headers: &[MailHeader<'a>]) -> Option<String> {
|
||||
if let Some(v) = headers.get_first_value("Content-Type") {
|
||||
if let Some(idx) = v.find(';') {
|
||||
return Some(v[..idx].to_string());
|
||||
} else {
|
||||
return Some(v);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn get_content_id<'a>(headers: &[MailHeader<'a>]) -> Option<String> {
|
||||
headers.get_first_value("Content-Id")
|
||||
}
|
||||
|
||||
fn render_content_type_tree(m: &ParsedMail) -> String {
|
||||
const WIDTH: usize = 4;
|
||||
const SKIP_HEADERS: [&str; 4] = [
|
||||
"Authentication-Results",
|
||||
"DKIM-Signature",
|
||||
"Received",
|
||||
"Received-SPF",
|
||||
];
|
||||
fn render_ct_rec(m: &ParsedMail, depth: usize) -> String {
|
||||
let mut parts = Vec::new();
|
||||
let msg = format!("{} {}", "-".repeat(depth * WIDTH), m.ctype.mimetype);
|
||||
parts.push(msg);
|
||||
for sp in &m.subparts {
|
||||
parts.push(render_ct_rec(sp, depth + 1))
|
||||
}
|
||||
parts.join("\n")
|
||||
}
|
||||
fn render_rec(m: &ParsedMail, depth: usize) -> String {
|
||||
let mut parts = Vec::new();
|
||||
let msg = format!("{} {}", "-".repeat(depth * WIDTH), m.ctype.mimetype);
|
||||
parts.push(msg);
|
||||
let indent = " ".repeat(depth * WIDTH);
|
||||
if !m.ctype.charset.is_empty() {
|
||||
parts.push(format!("{indent} Character Set: {}", m.ctype.charset));
|
||||
}
|
||||
for (k, v) in m.ctype.params.iter() {
|
||||
parts.push(format!("{indent} {k}: {v}"));
|
||||
}
|
||||
if !m.headers.is_empty() {
|
||||
parts.push(format!("{indent} == headers =="));
|
||||
for h in &m.headers {
|
||||
if h.get_key().starts_with('X') {
|
||||
continue;
|
||||
}
|
||||
if SKIP_HEADERS.contains(&h.get_key().as_str()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
parts.push(format!("{indent} {}: {}", h.get_key_ref(), h.get_value()));
|
||||
}
|
||||
}
|
||||
for sp in &m.subparts {
|
||||
parts.push(render_rec(sp, depth + 1))
|
||||
}
|
||||
parts.join("\n")
|
||||
}
|
||||
format!(
|
||||
"Outline:\n{}\n\nDetailed:\n{}\n\nNot showing headers:\n {}\n X.*",
|
||||
render_ct_rec(m, 1),
|
||||
render_rec(m, 1),
|
||||
SKIP_HEADERS.join("\n ")
|
||||
)
|
||||
}
|
||||
|
||||
#[instrument(name="nm::set_read_status", skip_all, fields(query=%query, unread=unread))]
|
||||
pub async fn set_read_status<'ctx>(
|
||||
nm: &Notmuch,
|
||||
@@ -896,7 +505,7 @@ pub async fn set_read_status<'ctx>(
|
||||
.iter()
|
||||
.filter(|uid| is_notmuch_thread_or_id(uid))
|
||||
.collect();
|
||||
info!("set_read_status({unread} {uids:?})");
|
||||
info!("set_read_status({} {:?})", unread, uids);
|
||||
for uid in uids {
|
||||
if unread {
|
||||
nm.tag_add("unread", uid)?;
|
||||
@@ -911,21 +520,12 @@ async fn photo_url_for_email_address(
|
||||
pool: &PgPool,
|
||||
addr: &str,
|
||||
) -> Result<Option<String>, ServerError> {
|
||||
let row = sqlx::query!(
|
||||
r#"
|
||||
SELECT
|
||||
url
|
||||
FROM email_photo ep
|
||||
JOIN email_address ea
|
||||
ON ep.id = ea.email_photo_id
|
||||
WHERE
|
||||
address = $1
|
||||
"#,
|
||||
addr
|
||||
)
|
||||
.fetch_optional(pool)
|
||||
.await?;
|
||||
Ok(row.map(|r| r.url))
|
||||
let row =
|
||||
sqlx::query_as::<_, (String,)>(include_str!("../sql/photo_url_for_email_address.sql"))
|
||||
.bind(addr)
|
||||
.fetch_optional(pool)
|
||||
.await?;
|
||||
Ok(row.map(|r| r.0))
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -949,20 +549,17 @@ pub async fn label_unprocessed(
|
||||
use futures::StreamExt;
|
||||
let ids = nm.message_ids(query)?;
|
||||
info!(
|
||||
"Processing {limit:?} of {} messages with '{query}'",
|
||||
ids.len()
|
||||
"Processing {:?} of {} messages with '{}'",
|
||||
limit,
|
||||
ids.len(),
|
||||
query
|
||||
);
|
||||
let rules: Vec<_> = sqlx::query!(
|
||||
r#"
|
||||
SELECT rule as "rule: Json<Rule>"
|
||||
FROM email_rule
|
||||
ORDER BY sort_order
|
||||
"#,
|
||||
)
|
||||
.fetch(pool)
|
||||
.map(|r| r.unwrap().rule.0)
|
||||
.collect()
|
||||
.await;
|
||||
let rules: Vec<_> =
|
||||
sqlx::query_as::<_, (Json<Rule>,)>(include_str!("../sql/label_unprocessed.sql"))
|
||||
.fetch(pool)
|
||||
.map(|r| r.unwrap().0 .0)
|
||||
.collect()
|
||||
.await;
|
||||
/*
|
||||
use letterbox_shared::{Match, MatchType};
|
||||
let rules = vec![Rule {
|
||||
@@ -983,11 +580,11 @@ pub async fn label_unprocessed(
|
||||
let mut add_mutations = HashMap::new();
|
||||
let mut rm_mutations = HashMap::new();
|
||||
for id in ids {
|
||||
let id = format!("id:{id}");
|
||||
let id = format!("id:{}", id);
|
||||
let files = nm.files(&id)?;
|
||||
// Only process the first file path is multiple files have the same id
|
||||
let Some(path) = files.iter().next() else {
|
||||
error!("No files for message-ID {id}");
|
||||
error!("No files for message-ID {}", id);
|
||||
let t = "Letterbox/Bad";
|
||||
nm.tag_add(t, &id)?;
|
||||
let t = "unprocessed";
|
||||
@@ -995,12 +592,12 @@ pub async fn label_unprocessed(
|
||||
continue;
|
||||
};
|
||||
let file = File::open(&path)?;
|
||||
info!("parsing {path}");
|
||||
info!("parsing {}", path);
|
||||
let mmap = unsafe { MmapOptions::new().map(&file)? };
|
||||
let m = match info_span!("parse_mail", path = path).in_scope(|| parse_mail(&mmap)) {
|
||||
Ok(m) => m,
|
||||
Err(err) => {
|
||||
error!("Failed to parse {path}: {err}");
|
||||
error!("Failed to parse {}: {}", path, err);
|
||||
let t = "Letterbox/Bad";
|
||||
nm.tag_add(t, &id)?;
|
||||
let t = "unprocessed";
|
||||
@@ -1012,7 +609,8 @@ pub async fn label_unprocessed(
|
||||
if matched_rule {
|
||||
if dryrun {
|
||||
info!(
|
||||
"\nAdd tags: {add_tags:?}\nTo: {} From: {} Subject: {}\n",
|
||||
"\nAdd tags: {:?}\nTo: {} From: {} Subject: {}\n",
|
||||
add_tags,
|
||||
m.headers.get_first_value("to").expect("no from header"),
|
||||
m.headers.get_first_value("from").expect("no from header"),
|
||||
m.headers
|
||||
@@ -1067,7 +665,7 @@ pub async fn label_unprocessed(
|
||||
}
|
||||
info!("Adding {} distinct labels", add_mutations.len());
|
||||
for (tag, ids) in add_mutations.iter() {
|
||||
info!(" {tag}: {}", ids.len());
|
||||
info!(" {}: {}", tag, ids.len());
|
||||
if !dryrun {
|
||||
let ids: Vec<_> = ids.iter().map(|s| s.as_str()).collect();
|
||||
info_span!("tags_add", tag = tag, count = ids.len())
|
||||
@@ -1076,7 +674,7 @@ pub async fn label_unprocessed(
|
||||
}
|
||||
info!("Removing {} distinct labels", rm_mutations.len());
|
||||
for (tag, ids) in rm_mutations.iter() {
|
||||
info!(" {tag}: {}", ids.len());
|
||||
info!(" {}: {}", tag, ids.len());
|
||||
if !dryrun {
|
||||
let ids: Vec<_> = ids.iter().map(|s| s.as_str()).collect();
|
||||
info_span!("tags_remove", tag = tag, count = ids.len())
|
||||
@@ -1092,7 +690,7 @@ fn find_tags<'a, 'b>(rules: &'a [Rule], headers: &'b [MailHeader]) -> (bool, Has
|
||||
for rule in rules {
|
||||
for hdr in headers {
|
||||
if rule.is_match(&hdr.get_key(), &hdr.get_value()) {
|
||||
//info!("Matched {rule:?}");
|
||||
//info!("Matched {:?}", rule);
|
||||
matched_rule = true;
|
||||
add_tags.insert(rule.tag.as_str());
|
||||
if rule.stop_on_match {
|
||||
@@ -1101,5 +699,115 @@ fn find_tags<'a, 'b>(rules: &'a [Rule], headers: &'b [MailHeader]) -> (bool, Has
|
||||
}
|
||||
}
|
||||
}
|
||||
return (matched_rule, add_tags);
|
||||
(matched_rule, add_tags)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const REPORT_V1: &str = r#"
|
||||
{
|
||||
"organization-name": "Google Inc.",
|
||||
"date-range": {
|
||||
"start-datetime": "2025-08-09T00:00:00Z",
|
||||
"end-datetime": "2025-08-09T23:59:59Z"
|
||||
},
|
||||
"contact-info": "smtp-tls-reporting@google.com",
|
||||
"report-id": "2025-08-09T00:00:00Z_xinu.tv",
|
||||
"policies": [
|
||||
{
|
||||
"policy": {
|
||||
"policy-type": "sts",
|
||||
"policy-string": [
|
||||
"version: STSv1",
|
||||
"mode": "testing",
|
||||
"mx": "mail.xinu.tv",
|
||||
"max_age": "86400"
|
||||
],
|
||||
"policy-domain": "xinu.tv"
|
||||
},
|
||||
"summary": {
|
||||
"total-successful-session-count": 20,
|
||||
"total-failure-session-count": 0
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
"#;
|
||||
|
||||
const REPORT_V2: &str = r#"
|
||||
{
|
||||
"organization-name": "Google Inc.",
|
||||
"date-range": {
|
||||
"start-datetime": "2025-08-09T00:00:00Z",
|
||||
"end-datetime": "2025-08-09T23:59:59Z"
|
||||
},
|
||||
"contact-info": "smtp-tls-reporting@google.com",
|
||||
"report-id": "2025-08-09T00:00:00Z_xinu.tv",
|
||||
"policies": [
|
||||
{
|
||||
"policy": {
|
||||
"policy-type": "sts",
|
||||
"policy-string": [
|
||||
"version: STSv1",
|
||||
"mode": "testing",
|
||||
"mx": "mail.xinu.tv",
|
||||
"max_age": "86400"
|
||||
],
|
||||
"policy-domain": "xinu.tv",
|
||||
"mx-host": [
|
||||
"mail.xinu.tv"
|
||||
]
|
||||
},
|
||||
"summary": {
|
||||
"total-successful-session-count": 3,
|
||||
"total-failure-session-count": 0
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
"#;
|
||||
|
||||
const REPORT_V3: &str = r#"
|
||||
{
|
||||
"organization-name": "Google Inc.",
|
||||
"date-range": {
|
||||
"start-datetime": "2025-08-09T00:00:00Z",
|
||||
"end-datetime": "2025-08-09T23:59:59Z"
|
||||
},
|
||||
"contact-info": "smtp-tls-reporting@google.com",
|
||||
"report-id": "2025-08-09T00:00:00Z_xinu.tv",
|
||||
"policies": [
|
||||
{
|
||||
"policy": {
|
||||
"policy-type": "sts",
|
||||
"policy-string": [
|
||||
"version: STSv1",
|
||||
"mode": "testing",
|
||||
"mx": "mail.xinu.tv",
|
||||
"max_age": "86400"
|
||||
],
|
||||
"policy-domain": "xinu.tv",
|
||||
"mx-host": [
|
||||
{
|
||||
"hostname": "mail.xinu.tv",
|
||||
"failure-count": 0,
|
||||
"result-type": "success"
|
||||
}
|
||||
]
|
||||
},
|
||||
"summary": {
|
||||
"total-successful-session-count": 3,
|
||||
"total-failure-session-count": 0
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
fn test_parse_tls_report_v1() {
|
||||
let report: TlsRpt = serde_json::from_str(REPORT_V1).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
7
server/src/templates.rs
Normal file
7
server/src/templates.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
use askama::Template;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "dmarc_report.html")]
|
||||
pub struct DmarcReportTemplate<'a> {
|
||||
pub feedback: &'a crate::nm::Feedback,
|
||||
}
|
||||
93
server/templates/dmarc_report.html
Normal file
93
server/templates/dmarc_report.html
Normal file
@@ -0,0 +1,93 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>DMARC Report</title>
|
||||
</head>
|
||||
<body>
|
||||
{% if report.report_metadata.is_some() %}
|
||||
{% let meta = report.report_metadata.as_ref().unwrap() %}
|
||||
<b>Reporter:</b> {{ meta.org_name }}<br>
|
||||
<b>Contact:</b> {{ meta.email }}<br>
|
||||
<b>Report ID:</b> {{ meta.report_id }}<br>
|
||||
{% if meta.date_range.is_some() %}
|
||||
{% let dr = meta.date_range.as_ref().unwrap() %}
|
||||
<b>Date range:</b>
|
||||
{{ dr.begin }}
|
||||
to
|
||||
{{ dr.end }}
|
||||
<br>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
{% if report.policy_published.is_some() %}
|
||||
{% let pol = report.policy_published.as_ref().unwrap() %}
|
||||
<b>Policy Published:</b>
|
||||
<ul>
|
||||
<li>Domain: {{ pol.domain }}</li>
|
||||
<li>ADKIM: {{ pol.adkim }}</li>
|
||||
<li>ASPF: {{ pol.aspf }}</li>
|
||||
<li>Policy: {{ pol.p }}</li>
|
||||
<li>Subdomain Policy: {{ pol.sp }}</li>
|
||||
<li>Percent: {{ pol.pct }}</li>
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
{% if report.record.is_some() %}
|
||||
<b>Records:</b>
|
||||
<table style="border-collapse:collapse;width:100%;font-size:0.95em;">
|
||||
<thead>
|
||||
<tr style="background:#f0f0f0;">
|
||||
<th style="border:1px solid #bbb;padding:4px 8px;">Source IP</th>
|
||||
<th style="border:1px solid #bbb;padding:4px 8px;">Count</th>
|
||||
<th style="border:1px solid #bbb;padding:4px 8px;">Header From</th>
|
||||
<th style="border:1px solid #bbb;padding:4px 8px;">Disposition</th>
|
||||
<th style="border:1px solid #bbb;padding:4px 8px;">DKIM</th>
|
||||
<th style="border:1px solid #bbb;padding:4px 8px;">SPF</th>
|
||||
<th style="border:1px solid #bbb;padding:4px 8px;">Auth Results</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for rec in report.record.as_ref().unwrap() %}
|
||||
<tr>
|
||||
<td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.source_ip }}</td>
|
||||
<td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.count }}</td>
|
||||
<td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.header_from }}</td>
|
||||
<td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.disposition }}</td>
|
||||
<td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.dkim }}</td>
|
||||
<td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.spf }}</td>
|
||||
<td style="border:1px solid #bbb;padding:4px 8px;">
|
||||
{% if rec.auth_results.is_some() %}
|
||||
{% let auth = rec.auth_results.as_ref().unwrap() %}
|
||||
{% for dkimres in auth.dkim %}
|
||||
<span style="white-space:nowrap;">
|
||||
DKIM: domain=<b>{{ dkimres.domain }}</b>
|
||||
selector=<b>{{ dkimres.selector }}</b>
|
||||
result=<b>{{ dkimres.result }}</b>
|
||||
</span><br>
|
||||
{% endfor %}
|
||||
|
||||
{% for spfres in auth.spf %}
|
||||
<span style="white-space:nowrap;">
|
||||
SPF: domain=<b>{{ spfres.domain }}</b>
|
||||
scope=<b>{{ spfres.scope }}</b>
|
||||
result=<b>{{ spfres.result }}</b>
|
||||
</span><br>
|
||||
{% endfor %}
|
||||
|
||||
{% for reason in rec.reason %}
|
||||
<span style="white-space:nowrap;">Reason: {{ reason }}</span><br>
|
||||
{% endfor %}
|
||||
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endif %}
|
||||
|
||||
{% if report.report_metadata.is_none() && report.policy_published.is_none() && report.record.is_none() %}
|
||||
<p>No DMARC summary found.</p>
|
||||
{% endif %}
|
||||
</body>
|
||||
</html>
|
||||
43
server/templates/tls_report.html
Normal file
43
server/templates/tls_report.html
Normal file
@@ -0,0 +1,43 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>TLS Report</title>
|
||||
</head>
|
||||
<body>
|
||||
<h3>TLS Report Summary:</h3>
|
||||
<p>Organization: {{ report.organization_name }}</p>
|
||||
<p>Date Range: {{ report.date_range.start_datetime }} to {{ report.date_range.end_datetime }}</p>
|
||||
<p>Contact: {{ report.contact_info }}</p>
|
||||
<p>Report ID: {{ report.report_id }}</p>
|
||||
|
||||
<h4>Policies:</h4>
|
||||
{% for policy in report.policies %}
|
||||
<h5>Policy Domain: {{ policy.policy.policy_domain }}</h5>
|
||||
<ul>
|
||||
<li>Policy Type: {{ policy.policy.policy_type }}</li>
|
||||
<li>Policy String: {{ policy.policy.policy_string | join(", ") }}</li>
|
||||
<li>Successful Sessions: {{ policy.summary.total_successful_session_count }}</li>
|
||||
<li>Failed Sessions: {{ policy.summary.total_failure_session_count }}</li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
{% for mx_host in policy.policy.mx_host %}
|
||||
<li>Hostname: {{ mx_host.hostname }}, Failures: {{ mx_host.failure_count }}, Result: {{ mx_host.result_type }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
{% for detail in policy.failure_details %}
|
||||
<li>Result: {{ detail.result_type }}, Sending IP: {{ detail.sending_mta_ip }}, Failed Sessions: {{ detail.failed_session_count }}
|
||||
{% if detail.failure_reason_code != "" %}
|
||||
(Reason: {{ detail.failure_reason_code }})
|
||||
{% endif %}
|
||||
</li>
|
||||
(Receiving IP: {{ detail.receiving_ip }})
|
||||
(Receiving MX: {{ detail.receiving_mx_hostname }})
|
||||
(Additional Info: {{ detail.additional_info }})
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endfor %}
|
||||
</body>
|
||||
</html>
|
||||
@@ -12,7 +12,7 @@ version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
build-info = "0.0.41"
|
||||
letterbox-notmuch = { path = "../notmuch", version = "0.17.27", registry = "xinu" }
|
||||
letterbox-notmuch = { path = "../notmuch", version = "0.17.32", registry = "xinu" }
|
||||
regex = "1.11.1"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
sqlx = "0.8.5"
|
||||
|
||||
@@ -33,7 +33,7 @@ wasm-bindgen = "=0.2.100"
|
||||
uuid = { version = "1.16.0", features = [
|
||||
"js",
|
||||
] } # direct dep to set js feature, prevents Rng issues
|
||||
letterbox-shared = { path = "../shared/", version = "0.17.27", registry = "xinu" }
|
||||
letterbox-shared = { path = "../shared/", version = "0.17.32", registry = "xinu" }
|
||||
seed_hooks = { version = "0.4.1", registry = "xinu" }
|
||||
strum_macros = "0.27.1"
|
||||
gloo-console = "0.3.0"
|
||||
|
||||
@@ -16,10 +16,11 @@
|
||||
<link data-trunk rel="css" href="static/vars.css" />
|
||||
<link data-trunk rel="tailwind-css" href="./src/tailwind.css" />
|
||||
<link data-trunk rel="css" href="static/overrides.css" />
|
||||
<link data-trunk rel="css" href="static/email-specific.css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<section id="app"></section>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
|
||||
@@ -1025,7 +1025,7 @@ fn message_render(msg: &ShowThreadQueryThreadOnEmailThreadMessages, open: bool)
|
||||
],
|
||||
IF!(open =>
|
||||
div![
|
||||
C!["content", "bg-white", "text-black", "p-4", "min-w-full", "w-0","overflow-x-auto", from],
|
||||
C!["content", "bg-white", "text-black", "p-4", "min-w-full", "w-0","overflow-x-auto", from.map(|f|format!("from-{f}"))],
|
||||
match &msg.body {
|
||||
ShowThreadQueryThreadOnEmailThreadMessagesBody::UnhandledContentType(
|
||||
ShowThreadQueryThreadOnEmailThreadMessagesBodyOnUnhandledContentType { contents ,content_tree},
|
||||
|
||||
11
web/static/email-specific.css
Normal file
11
web/static/email-specific.css
Normal file
@@ -0,0 +1,11 @@
|
||||
.mail-thread .content.from-noreply-news-bloomberg-com a {
|
||||
background-color: initial !important;
|
||||
}
|
||||
|
||||
.mail-thread .content.from-noreply-news-bloomberg-com h2 {
|
||||
margin: 0 !important;
|
||||
padding: 0 !important;
|
||||
}
|
||||
.mail-thread .content.from-dmarcreport-microsoft-com div {
|
||||
font-family: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji" !important;
|
||||
}
|
||||
@@ -57,15 +57,6 @@ html {
|
||||
margin-left: 2em;
|
||||
}
|
||||
|
||||
.mail-thread .content .noreply-news-bloomberg-com a {
|
||||
background-color: initial !important;
|
||||
}
|
||||
|
||||
.mail-thread .content .noreply-news-bloomberg-com h2 {
|
||||
margin: 0 !important;
|
||||
padding: 0 !important;
|
||||
}
|
||||
|
||||
/* Hackaday figures have unreadable black on dark grey */
|
||||
.news-post figcaption.wp-caption-text {
|
||||
background-color: initial !important;
|
||||
|
||||
Reference in New Issue
Block a user