oxedyne/fe2o3/fe2o3_mail/tests/mime_corpus/old_message.rs
13.4 KiB, 1 run
created by r1870400018:74948, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The reader of `fe2o3_mail::message` as it stood at d81121d5 (and a5c60dfe), before it used |
| 2 | //! `fe2o3_text::mime`'s walker, kept as the baseline `parser_compare` holds the new one to. |
| 3 | //! Only its reading half is kept; the draft builder never changed. |
| 4 | #![allow(dead_code, unused_imports)] |
| 5 | |
| 6 | //! text with the odd replacement character rather than failing. |
| 7 | |
| 8 | use oxedyne_fe2o3_core::prelude::*; |
| 9 | use oxedyne_fe2o3_text::{ |
| 10 | base64, |
| 11 | mime::{ |
| 12 | decode_base64_lenient, |
| 13 | decode_qp, |
| 14 | decode_words, |
| 15 | }, |
| 16 | }; |
| 17 | |
| 18 | |
| 19 | /// A parsed message, reduced to what a reader needs: the headers that identify it and the |
| 20 | /// readable text of its body. |
| 21 | /// |
| 22 | /// The address and subject fields are decoded for display -- RFC 2047 encoded-words are |
| 23 | /// turned back into the characters they stand for -- so what a reader sees is the name and |
| 24 | /// the subject rather than `=?utf-8?B?...?=`. The raw header block is not kept; a caller |
| 25 | /// that wants a header this struct does not name can call [`headers`] on the bytes itself. |
| 26 | #[derive(Clone, Debug, Default)] |
| 27 | pub struct ParsedMessage { |
| 28 | pub from: String, // decoded display, e.g. "Ada <ada@x.example>" |
| 29 | pub to: String, |
| 30 | pub cc: String, |
| 31 | pub subject: String, // RFC 2047 decoded |
| 32 | pub date: String, // the Date header, verbatim |
| 33 | pub message_id: String, |
| 34 | pub in_reply_to: String, |
| 35 | pub references: String, |
| 36 | pub body: String, // readable text of the message |
| 37 | pub attachments: Vec<String>, // filenames of the non-text parts |
| 38 | } |
| 39 | |
| 40 | impl ParsedMessage { |
| 41 | /// Read a whole message from its RFC 5322 bytes. |
| 42 | /// |
| 43 | /// Infallible on purpose: a mailbox holds whatever a sender put in it, and a parser that |
| 44 | /// refused a malformed message would leave a reader unable to see the very message they |
| 45 | /// most need to. A field that cannot be found is empty, and a body that cannot be |
| 46 | /// decoded degrades rather than failing. |
| 47 | pub fn parse(raw: &[u8]) -> Self { |
| 48 | let text = String::from_utf8_lossy(raw); |
| 49 | let hs = headers(&text); |
| 50 | Self { |
| 51 | from: decode_words(&hget(&hs, "from")), |
| 52 | to: decode_words(&hget(&hs, "to")), |
| 53 | cc: decode_words(&hget(&hs, "cc")), |
| 54 | subject: decode_words(&hget(&hs, "subject")), |
| 55 | date: hget(&hs, "date"), |
| 56 | message_id: hget(&hs, "message-id"), |
| 57 | in_reply_to: hget(&hs, "in-reply-to"), |
| 58 | references: hget(&hs, "references"), |
| 59 | body: readable_text(&text), |
| 60 | attachments: attachment_names(&text), |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | // ── Reading ───────────────────────────────────────────────────────────── |
| 67 | |
| 68 | /// Split the header block from the body at the first blank line, unfold continuation lines, |
| 69 | /// and return the headers as an ordered list of `(lowercased-name, value)`. |
| 70 | /// |
| 71 | /// The name is lowercased because a header is looked up by a caller who did not write it and |
| 72 | /// cannot know whether the sender wrote `Message-ID` or `Message-Id`; the value is left as it |
| 73 | /// was, since its case may matter. |
| 74 | pub fn headers(text: &str) -> Vec<(String, String)> { |
| 75 | let block = match find_blank_line(text) { |
| 76 | Some(i) => &text[..i], |
| 77 | None => text, |
| 78 | }; |
| 79 | let mut out: Vec<(String, String)> = Vec::new(); |
| 80 | for line in block.split('\n') { |
| 81 | let line = line.strip_suffix('\r').unwrap_or(line); |
| 82 | // A line that begins with a space or tab continues the header above it. |
| 83 | if line.starts_with(' ') || line.starts_with('\t') { |
| 84 | if let Some(last) = out.last_mut() { |
| 85 | last.1.push(' '); |
| 86 | last.1.push_str(line.trim()); |
| 87 | continue; |
| 88 | } |
| 89 | } |
| 90 | if let Some(i) = line.find(':') { |
| 91 | let name = line[..i].trim().to_lowercase(); |
| 92 | let val = line[i + 1..].trim().to_string(); |
| 93 | if !name.is_empty() { |
| 94 | out.push((name, val)); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | out |
| 99 | } |
| 100 | |
| 101 | /// The first value of a header, or the empty string when it is absent. |
| 102 | fn hget(hs: &[(String, String)], name: &str) -> String { |
| 103 | hs.iter() |
| 104 | .find(|(n, _)| n == name) |
| 105 | .map(|(_, v)| v.clone()) |
| 106 | .unwrap_or_default() |
| 107 | } |
| 108 | |
| 109 | /// The byte offset of the blank line that ends the header block, `\r\n\r\n` or `\n\n`. |
| 110 | fn find_blank_line(text: &str) -> Option<usize> { |
| 111 | let b = text.as_bytes(); |
| 112 | let mut i = 0; |
| 113 | while i + 1 < b.len() { |
| 114 | if b[i] == b'\n' && b[i + 1] == b'\n' { |
| 115 | return Some(i); |
| 116 | } |
| 117 | if i + 3 < b.len() && b[i] == b'\r' && b[i + 1] == b'\n' |
| 118 | && b[i + 2] == b'\r' && b[i + 3] == b'\n' |
| 119 | { |
| 120 | return Some(i); |
| 121 | } |
| 122 | i += 1; |
| 123 | } |
| 124 | None |
| 125 | } |
| 126 | |
| 127 | /// The body, which is everything after the blank line that ends the headers. |
| 128 | fn body_of(text: &str) -> &str { |
| 129 | match find_blank_line(text) { |
| 130 | Some(i) => { |
| 131 | // Step over the separator itself, whichever spelling it was. |
| 132 | let b = text.as_bytes(); |
| 133 | if i + 3 < b.len() && b[i] == b'\r' { |
| 134 | &text[i + 4..] |
| 135 | } else { |
| 136 | &text[i + 2..] |
| 137 | } |
| 138 | }, |
| 139 | None => "", |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /// The readable text of a message: the `text/plain` part of a multipart, or the decoded |
| 144 | /// body, or `text/html` reduced to text where that is all there is. |
| 145 | /// |
| 146 | /// One level of multipart nesting is followed, which is the shape of a message with an |
| 147 | /// attachment -- a `multipart/mixed` whose first part is the `multipart/alternative` that |
| 148 | /// holds the text. |
| 149 | pub fn readable_text(raw: &str) -> String { |
| 150 | let hs = headers(raw); |
| 151 | let ctype = { |
| 152 | let c = hget(&hs, "content-type"); |
| 153 | if c.is_empty() { fmt!("text/plain") } else { c } |
| 154 | }; |
| 155 | let body = body_of(raw); |
| 156 | |
| 157 | if ctype.to_lowercase().contains("multipart") { |
| 158 | if let Some(boundary) = param(&ctype, "boundary") { |
| 159 | let marker = fmt!("--{}", boundary); |
| 160 | let mut plain: Option<String> = None; |
| 161 | let mut html: Option<String> = None; |
| 162 | for part in body.split(marker.as_str()) { |
| 163 | let part = part.strip_prefix('\r').unwrap_or(part); |
| 164 | let part = part.strip_prefix('\n').unwrap_or(part); |
| 165 | let phs = headers(part); |
| 166 | let pct = hget(&phs, "content-type"); |
| 167 | let pcte = hget(&phs, "content-transfer-encoding").to_lowercase(); |
| 168 | let pbody = body_of(part); |
| 169 | if pbody.trim().is_empty() { |
| 170 | continue; |
| 171 | } |
| 172 | let pctl = pct.to_lowercase(); |
| 173 | if pctl.contains("multipart") && plain.is_none() { |
| 174 | // A nested multipart: read one level down and take its text. |
| 175 | let inner = readable_text(&fmt!("Content-Type: {}\r\n\r\n{}", pct, pbody)); |
| 176 | if !inner.trim().is_empty() { |
| 177 | plain = Some(inner); |
| 178 | } |
| 179 | continue; |
| 180 | } |
| 181 | let decoded = decode_transfer(pbody, &pcte, param(&pct, "charset").as_deref()); |
| 182 | if pctl.contains("text/plain") && plain.is_none() { |
| 183 | plain = Some(decoded); |
| 184 | } else if pctl.contains("text/html") && html.is_none() { |
| 185 | html = Some(decoded); |
| 186 | } |
| 187 | } |
| 188 | if let Some(p) = plain { |
| 189 | return norm_lines(p.trim()); |
| 190 | } |
| 191 | if let Some(h) = html { |
| 192 | return norm_lines(strip_html(&h).trim()); |
| 193 | } |
| 194 | return String::new(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | let cte = hget(&hs, "content-transfer-encoding").to_lowercase(); |
| 199 | let decoded = decode_transfer(body, &cte, param(&ctype, "charset").as_deref()); |
| 200 | if ctype.to_lowercase().contains("text/html") { |
| 201 | return norm_lines(strip_html(&decoded).trim()); |
| 202 | } |
| 203 | norm_lines(decoded.trim()) |
| 204 | } |
| 205 | |
| 206 | /// Line endings a reader wants: a message's `\r\n` and bare `\r` reduced to `\n`, so the |
| 207 | /// text a model is handed is the text and not the wire's carriage returns. |
| 208 | fn norm_lines(s: &str) -> String { |
| 209 | s.replace("\r\n", "\n").replace('\r', "\n") |
| 210 | } |
| 211 | |
| 212 | /// Decode one part's body out of its transfer encoding, then read the bytes as text. |
| 213 | fn decode_transfer(body: &str, cte: &str, charset: Option<&str>) -> String { |
| 214 | let bytes = match cte.trim() { |
| 215 | "base64" => decode_base64_lenient(body), |
| 216 | "quoted-printable" => decode_qp(body), |
| 217 | _ => body.as_bytes().to_vec(), |
| 218 | }; |
| 219 | // Every supported charset shares ASCII, and UTF-8 is the one this crate decodes exactly; |
| 220 | // anything else degrades (see the module header). `charset` is accepted so a future |
| 221 | // transcoder has the name to hand. |
| 222 | let _ = charset; |
| 223 | String::from_utf8_lossy(&bytes).into_owned() |
| 224 | } |
| 225 | |
| 226 | /// The filenames of the parts that are not the readable text -- what a reader means by "the |
| 227 | /// attachments". A part with a `filename` or a `name` parameter, or a `Content-Disposition` |
| 228 | /// of `attachment`, counts. |
| 229 | fn attachment_names(raw: &str) -> Vec<String> { |
| 230 | let hs = headers(raw); |
| 231 | let ctype = hget(&hs, "content-type"); |
| 232 | let mut out: Vec<String> = Vec::new(); |
| 233 | if let Some(boundary) = param(&ctype, "boundary") { |
| 234 | let marker = fmt!("--{}", boundary); |
| 235 | for part in body_of(raw).split(marker.as_str()) { |
| 236 | let phs = headers(part); |
| 237 | let disp = hget(&phs, "content-disposition"); |
| 238 | let pct = hget(&phs, "content-type"); |
| 239 | let name = param(&pct, "name") |
| 240 | .or_else(|| param(&disp, "filename")); |
| 241 | if let Some(n) = name { |
| 242 | if !n.trim().is_empty() { |
| 243 | out.push(decode_words(n.trim())); |
| 244 | } |
| 245 | } else if disp.to_lowercase().contains("attachment") { |
| 246 | out.push(fmt!("attachment")); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | out |
| 251 | } |
| 252 | |
| 253 | /// The value of a `name="value"` parameter on a header, unquoted. |
| 254 | fn param(header: &str, name: &str) -> Option<String> { |
| 255 | let lower = header.to_lowercase(); |
| 256 | let key = fmt!("{}=", name.to_lowercase()); |
| 257 | let at = lower.find(&key)?; |
| 258 | let after = &header[at + key.len()..]; |
| 259 | let after = after.trim_start(); |
| 260 | if let Some(stripped) = after.strip_prefix('"') { |
| 261 | let end = stripped.find('"')?; |
| 262 | Some(stripped[..end].to_string()) |
| 263 | } else { |
| 264 | let end = after.find(|c: char| c == ';' || c == '\r' || c == '\n' || c == ' ') |
| 265 | .unwrap_or(after.len()); |
| 266 | let v = after[..end].trim(); |
| 267 | if v.is_empty() { None } else { Some(v.to_string()) } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /// Reduce HTML to its readable text. A mail body is the least trustworthy string a reader |
| 272 | /// meets, so nothing here is ever treated as markup to render -- the tags are removed and |
| 273 | /// only the text between them is kept. |
| 274 | pub fn strip_html(html: &str) -> String { |
| 275 | let s0 = remove_blocks(html, "<style", "</style>"); |
| 276 | let s = remove_blocks(&s0, "<script", "</script>"); |
| 277 | |
| 278 | let mut out = String::with_capacity(s.len()); |
| 279 | let mut i = 0; |
| 280 | while i < s.len() { |
| 281 | if s.as_bytes()[i] == b'<' { |
| 282 | // A tag: skip to the matching '>'. A block-level close or a break becomes a |
| 283 | // newline so the text does not run together. |
| 284 | let tag_end = s[i..].find('>').map(|j| i + j + 1).unwrap_or(s.len()); |
| 285 | let tag = s[i..tag_end].to_lowercase(); |
| 286 | if is_break_tag(&tag) { |
| 287 | out.push('\n'); |
| 288 | } |
| 289 | i = tag_end; |
| 290 | } else { |
| 291 | let ch = s[i..].chars().next().unwrap_or('\u{fffd}'); |
| 292 | out.push(ch); |
| 293 | i += ch.len_utf8(); |
| 294 | } |
| 295 | } |
| 296 | collapse_blank_lines(&decode_entities(&out)) |
| 297 | } |
| 298 | |
| 299 | /// Does this tag end a block, so its removal should leave a line break behind? |
| 300 | fn is_break_tag(tag: &str) -> bool { |
| 301 | const BREAKERS: [&str; 12] = [ |
| 302 | "</p", "</div", "</tr", "</li", "</h1", "</h2", "</h3", "</h4", "</h5", "</h6", |
| 303 | "<br", "</table", |
| 304 | ]; |
| 305 | BREAKERS.iter().any(|b| tag.starts_with(b)) |
| 306 | } |
| 307 | |
| 308 | /// Remove every `<open ... close>` block, tag and content, case-insensitively. |
| 309 | fn remove_blocks(s: &str, open: &str, close: &str) -> String { |
| 310 | let lower = s.to_lowercase(); |
| 311 | let mut out = String::with_capacity(s.len()); |
| 312 | let mut i = 0; |
| 313 | while i < s.len() { |
| 314 | if lower[i..].starts_with(open) { |
| 315 | if let Some(rel) = lower[i..].find(close) { |
| 316 | i += rel + close.len(); |
| 317 | continue; |
| 318 | } |
| 319 | // An unterminated block: drop the rest. |
| 320 | break; |
| 321 | } |
| 322 | let ch = s[i..].chars().next().unwrap_or('\u{fffd}'); |
| 323 | out.push(ch); |
| 324 | i += ch.len_utf8(); |
| 325 | } |
| 326 | out |
| 327 | } |
| 328 | |
| 329 | /// Decode the handful of HTML entities that carry meaning in plain text. |
| 330 | fn decode_entities(s: &str) -> String { |
| 331 | s.replace("&", "&") |
| 332 | .replace("<", "<") |
| 333 | .replace(">", ">") |
| 334 | .replace(""", "\"") |
| 335 | .replace("'", "'") |
| 336 | .replace("'", "'") |
| 337 | .replace(" ", " ") |
| 338 | } |
| 339 | |
| 340 | /// Collapse three or more newlines to two, so the text keeps its paragraphs without the |
| 341 | /// gaps a stripped layout leaves. |
| 342 | fn collapse_blank_lines(s: &str) -> String { |
| 343 | let mut out = String::with_capacity(s.len()); |
| 344 | let mut runs = 0; |
| 345 | for ch in s.chars() { |
| 346 | if ch == '\n' { |
| 347 | runs += 1; |
| 348 | if runs <= 2 { |
| 349 | out.push('\n'); |
| 350 | } |
| 351 | } else { |
| 352 | runs = 0; |
| 353 | out.push(ch); |
| 354 | } |
| 355 | } |
| 356 | out |
| 357 | } |
| 358 |