Separate the two feed identities so the public read URL never reveals the inbound address and vice-versa: - FeedId becomes an opaque high-entropy token (read id + KV key); MailboxId (noun.noun.NN) owns the inbound address and the untrusted-input boundary via MailboxId.parse. They map only through the inbound:<mailbox> secondary index, resolved solely at reception. - inbound index lifecycle is owned by FeedRepository: written by save/saveConfig, dropped by removeFromList(Bulk) — symmetric, never mirrored by hand (removes the manual delete in feed-service + the cron loop, and a silent empty-catch). - Feed.mailboxId exposes a MailboxId VO (symmetry with Feed.id); the mailbox@domain shape lives on MailboxId.emailAddress(domain). - Distinguish mailbox_unknown (no feed claims the address) from feed_not_found (dangling index) for observability; both forwardable, both 404. - Drop the redundant EmailParser.extractMailbox pass-through so MailboxId.parse is the single parse boundary. Docs (README/INSTALL/CLAUDE.md/landing) and tests updated; 439 tests green, tsc clean, build dry-run OK. Co-Authored-By: Claude Opus 4.7 <[email protected]>
78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import { EmailData } from "../types";
|
|
|
|
export class EmailParser {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
static parseForwardEmailPayload(payload: any): EmailData {
|
|
if (!payload) {
|
|
throw new Error("Missing or invalid webhook payload");
|
|
}
|
|
|
|
const fromAddress =
|
|
payload.from?.text ||
|
|
(payload.from?.value?.[0]?.address
|
|
? `${payload.from.value[0].name || ""} <${payload.from.value[0].address}>`
|
|
: "Unknown Sender");
|
|
|
|
const subject = this.decodeEncodedWords(payload.subject || "No Subject");
|
|
const content = payload.html || payload.text || "";
|
|
|
|
return {
|
|
subject,
|
|
from: fromAddress,
|
|
content,
|
|
receivedAt: payload.date ? new Date(payload.date).getTime() : Date.now(),
|
|
headers: this.extractHeaders(payload),
|
|
};
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
private static extractHeaders(payload: any): Record<string, string> {
|
|
const headers: Record<string, string> = {};
|
|
|
|
if (payload.headerLines && Array.isArray(payload.headerLines)) {
|
|
payload.headerLines.forEach((h: { key: string; line: string }) => {
|
|
const key = h.key.toLowerCase();
|
|
const value = h.line
|
|
.replace(new RegExp(`^${h.key}:\\s*`, "i"), "")
|
|
.trim();
|
|
headers[key] = value;
|
|
});
|
|
} else if (typeof payload.headers === "string") {
|
|
payload.headers.split(/\r?\n/).forEach((line: string) => {
|
|
const match = line.match(/^([^:]+):\s*(.*)$/);
|
|
if (match) {
|
|
headers[match[1].toLowerCase()] = match[2];
|
|
}
|
|
});
|
|
}
|
|
|
|
return headers;
|
|
}
|
|
|
|
static decodeEncodedWords(text: string): string {
|
|
if (!text) return "";
|
|
|
|
return text.replace(
|
|
/=\?([^?]+)\?([BQ])\?([^?]+)\?=/gi,
|
|
(_, charset, encoding, text) => {
|
|
if (encoding.toUpperCase() === "B") {
|
|
try {
|
|
return atob(text);
|
|
} catch {
|
|
return text;
|
|
}
|
|
} else if (encoding.toUpperCase() === "Q") {
|
|
return this.decodeQuotedPrintable(text.replace(/_/g, " "));
|
|
}
|
|
return text;
|
|
},
|
|
);
|
|
}
|
|
|
|
private static decodeQuotedPrintable(text: string): string {
|
|
return text.replace(/=([0-9A-F]{2})/gi, (_, hex) => {
|
|
return String.fromCharCode(parseInt(hex, 16));
|
|
});
|
|
}
|
|
}
|