|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A forward quoted INLINE in the body text: the `inline` source of the |
| 5 | + * participant set (ruling 6116489638 on #22870, Q3 C, the staged half). |
| 6 | + * |
| 7 | + * Exactly three client formats are read, as each client writes the forward |
| 8 | + * into the text/plain body with English labels: |
| 9 | + * |
| 10 | + * - **Gmail** — the line `---------- Forwarded message ---------`, then |
| 11 | + * `From:`, `Date:`, `Subject:`, `To:` and `Cc:` lines; |
| 12 | + * - **Outlook** (Outlook on the web, Outlook.com, Microsoft 365) — a line of |
| 13 | + * 32 underscores, then `From:`, `Sent:`, `To:`, `Cc:` and `Subject:` lines; |
| 14 | + * - **Apple Mail** — the line `Begin forwarded message:`, an empty line, then |
| 15 | + * `From:`, `Subject:`, `Date:`, `To:`, `Cc:` and `Reply-To:` lines, each |
| 16 | + * line of the block carrying the `>` quote marker the client's plain-text |
| 17 | + * rendering adds (or not). |
| 18 | + * |
| 19 | + * The shapes are those of the client output collected in the MIT-licensed |
| 20 | + * `email-forward-parser` (Crisp IM SAS) — its fixtures `gmail_en_body.txt`, |
| 21 | + * `outlook_live_body.txt` and `apple_mail_en_body.txt`, and the separator |
| 22 | + * patterns its parser documents per client. The fixtures in |
| 23 | + * `messages.fixture.ts` reproduce them. |
| 24 | + * |
| 25 | + * ## What is recognised, and nothing else |
| 26 | + * |
| 27 | + * The FIRST separator line of the three formats in the body decides: the |
| 28 | + * forward the user made sits above any forward quoted inside it, which is not |
| 29 | + * read (as a `message/rfc822` part nested in a forwarded part is not). Its |
| 30 | + * block is recognised only when it is complete: |
| 31 | + * |
| 32 | + * - the header lines follow the separator directly (Apple Mail: after at most |
| 33 | + * one empty line) and run to the first empty line or the end of the text; |
| 34 | + * - every one of them is `Label: value` with a label of THAT format, each |
| 35 | + * label at most once, and `From:` comes first; |
| 36 | + * - the format's date line (`Date:`, or Outlook's `Sent:`) is present; |
| 37 | + * - `From:` names at least one address. |
| 38 | + * |
| 39 | + * Anything else — a separator followed by prose, a label of another format or |
| 40 | + * locale (Gmail's `Von:`, Outlook's `Gesendet:`), a wrapped header line, a |
| 41 | + * block with no date line, a `From:` naming no address — is not recognised, |
| 42 | + * and {@link parseInlineForward} answers `null`: nothing is guessed, and the |
| 43 | + * message is matched on its other participants only, which may pool it. |
| 44 | + * |
| 45 | + * The body read is the text/plain one (`ParsedInboundMessage.text`). A message |
| 46 | + * with an HTML body only has none, so its forward is not read either. |
| 47 | + * |
| 48 | + * Pure: text in, participants out. Whether a recognised forward's participants |
| 49 | + * are USED is the ingest seam's decision (`mail-ingest-service.ts`): only for a |
| 50 | + * forward sent by a user of the mailbox's organization. |
| 51 | + */ |
| 52 | + |
| 53 | +import { addressParser } from 'postal-mime'; |
| 54 | +import type { Address } from 'postal-mime'; |
| 55 | +import { normalizeMailAddress } from './match-addresses.js'; |
| 56 | +import type { MailParticipantHeaders } from './parse-message.js'; |
| 57 | + |
| 58 | +/** The client formats {@link parseInlineForward} reads. */ |
| 59 | +export const INLINE_FORWARD_FORMATS = ['gmail', 'outlook', 'apple_mail'] as const; |
| 60 | + |
| 61 | +/** One of {@link INLINE_FORWARD_FORMATS}. */ |
| 62 | +export type InlineForwardFormat = (typeof INLINE_FORWARD_FORMATS)[number]; |
| 63 | + |
| 64 | +/** A recognised inline forward: its format and the original message's participants. */ |
| 65 | +export interface InlineForward { |
| 66 | + format: InlineForwardFormat; |
| 67 | + /** The addresses the block names, per participant header (`sender` is always empty). */ |
| 68 | + participants: MailParticipantHeaders; |
| 69 | +} |
| 70 | + |
| 71 | +type ParticipantHeader = keyof MailParticipantHeaders; |
| 72 | + |
| 73 | +interface ClientFormat { |
| 74 | + format: InlineForwardFormat; |
| 75 | + /** Matched against one line, trailing whitespace removed (and the quote marker, where the client quotes). */ |
| 76 | + separator: RegExp; |
| 77 | + /** Every label of the block → the participant header it fills, or `null` for a label read for its presence only. */ |
| 78 | + labels: ReadonlyMap<string, ParticipantHeader | null>; |
| 79 | + /** The label the format always writes the original's date under. */ |
| 80 | + dateLabel: string; |
| 81 | + /** Each line of the block may carry one `>` quote marker. */ |
| 82 | + quoted: boolean; |
| 83 | + /** One empty line may sit between the separator and the header lines. */ |
| 84 | + emptyLineBeforeHeaders: boolean; |
| 85 | +} |
| 86 | + |
| 87 | +const FORMATS: ReadonlyArray<ClientFormat> = [ |
| 88 | + { |
| 89 | + format: 'gmail', |
| 90 | + // Ten dashes and nine today; the pattern documented for the client allows 8–10 either side. |
| 91 | + separator: /^-{8,10} Forwarded message -{8,10}$/, |
| 92 | + labels: new Map<string, ParticipantHeader | null>([ |
| 93 | + ['From', 'from'], |
| 94 | + ['Date', null], |
| 95 | + ['Subject', null], |
| 96 | + ['To', 'to'], |
| 97 | + ['Cc', 'cc'], |
| 98 | + ]), |
| 99 | + dateLabel: 'Date', |
| 100 | + quoted: false, |
| 101 | + emptyLineBeforeHeaders: false, |
| 102 | + }, |
| 103 | + { |
| 104 | + format: 'outlook', |
| 105 | + separator: /^_{32}$/, |
| 106 | + labels: new Map<string, ParticipantHeader | null>([ |
| 107 | + ['From', 'from'], |
| 108 | + ['Sent', null], |
| 109 | + ['To', 'to'], |
| 110 | + ['Cc', 'cc'], |
| 111 | + ['Subject', null], |
| 112 | + ]), |
| 113 | + dateLabel: 'Sent', |
| 114 | + quoted: false, |
| 115 | + emptyLineBeforeHeaders: false, |
| 116 | + }, |
| 117 | + { |
| 118 | + format: 'apple_mail', |
| 119 | + separator: /^Begin forwarded message:$/, |
| 120 | + labels: new Map<string, ParticipantHeader | null>([ |
| 121 | + ['From', 'from'], |
| 122 | + ['Subject', null], |
| 123 | + ['Date', null], |
| 124 | + ['To', 'to'], |
| 125 | + ['Cc', 'cc'], |
| 126 | + ['Reply-To', 'replyTo'], |
| 127 | + ]), |
| 128 | + dateLabel: 'Date', |
| 129 | + quoted: true, |
| 130 | + emptyLineBeforeHeaders: true, |
| 131 | + }, |
| 132 | +]; |
| 133 | + |
| 134 | +const HEADER_LINE = /^([^\s:]+):(?: (.*))?$/; |
| 135 | + |
| 136 | +/** One line as a format reads it: trailing whitespace dropped, and one quote marker where the client quotes. */ |
| 137 | +function lineAs(format: ClientFormat, raw: string): string { |
| 138 | + const line = raw.trimEnd(); |
| 139 | + return format.quoted ? line.replace(/^> ?/, '') : line; |
| 140 | +} |
| 141 | + |
| 142 | +/** The addresses of one header value, in order; a name with no address names none. */ |
| 143 | +function addressesOf(value: string): string[] { |
| 144 | + const out: string[] = []; |
| 145 | + const visit = (list: ReadonlyArray<Address>) => { |
| 146 | + for (const entry of list) { |
| 147 | + if (entry.group) visit(entry.group); |
| 148 | + else if (entry.address && normalizeMailAddress(entry.address) !== null) out.push(entry.address); |
| 149 | + } |
| 150 | + }; |
| 151 | + visit(addressParser(value)); |
| 152 | + return out; |
| 153 | +} |
| 154 | + |
| 155 | +/** The block after a separator at `lines[start - 1]`, or `null` when it is not complete. */ |
| 156 | +function readBlock(format: ClientFormat, lines: ReadonlyArray<string>, start: number): InlineForward | null { |
| 157 | + let at = start; |
| 158 | + if (format.emptyLineBeforeHeaders && at < lines.length && lineAs(format, lines[at]!) === '') at++; |
| 159 | + |
| 160 | + const participants: MailParticipantHeaders = { from: [], sender: [], replyTo: [], to: [], cc: [] }; |
| 161 | + const seen = new Set<string>(); |
| 162 | + for (; at < lines.length; at++) { |
| 163 | + const line = lineAs(format, lines[at]!); |
| 164 | + if (line === '') break; |
| 165 | + const header = HEADER_LINE.exec(line); |
| 166 | + if (!header) return null; |
| 167 | + const label = header[1]!; |
| 168 | + if (!format.labels.has(label) || seen.has(label)) return null; |
| 169 | + if (seen.size === 0 && label !== 'From') return null; |
| 170 | + seen.add(label); |
| 171 | + const target = format.labels.get(label); |
| 172 | + if (target) participants[target] = addressesOf(header[2] ?? ''); |
| 173 | + } |
| 174 | + if (!seen.has(format.dateLabel) || participants.from.length === 0) return null; |
| 175 | + return { format: format.format, participants }; |
| 176 | +} |
| 177 | + |
| 178 | +/** |
| 179 | + * The inline forward the body text carries, or `null` when it carries none |
| 180 | + * this module recognises (see the module docblock for what is recognised). |
| 181 | + */ |
| 182 | +export function parseInlineForward(text: string | null | undefined): InlineForward | null { |
| 183 | + if (!text) return null; |
| 184 | + const lines = text.split(/\r?\n/); |
| 185 | + for (let i = 0; i < lines.length; i++) { |
| 186 | + for (const format of FORMATS) { |
| 187 | + if (format.separator.test(lineAs(format, lines[i]!))) return readBlock(format, lines, i + 1); |
| 188 | + } |
| 189 | + } |
| 190 | + return null; |
| 191 | +} |
0 commit comments