import { simpleParser } from 'mailparser'
import { richEmailBody } from './rich-body.js'
import type { AddressEntry, EmailAttachmentMeta, ReadMessageBody } from './types.js'
export function flattenAddresses(input: unknown): AddressEntry[] {
if (input === null || input === undefined) return []
const list = Array.isArray(input) ? input : (input as { value?: unknown }).value
if (!Array.isArray(list)) return []
return list
.filter(entry => entry !== null && typeof entry === 'object')
.map(entry => {
const { name, address } = entry as { name?: string; address?: string }
const out: AddressEntry = {}
if (typeof name === 'string' && name !== '') out.name = name
if (typeof address === 'string' && address !== '') out.address = address
return out
})
.filter(entry => entry.address !== undefined || entry.name !== undefined)
}
/** Minimal, dependency-free HTML-to-text: block tags become newlines, tags are dropped, common entities decoded. */
export function stripHtml(html: string): string {
let out = html
.replace(/<(script|style|head|title)[\s\S]*?<\/\1\s*>/gi, ' ')
.replace(/<\/(p|div|tr|li|h[1-6]|table|blockquote|ul|ol|section|article|header|footer)[^>]*>/gi, '\n')
.replace(/
/gi, '\n')
.replace(/<[^>]+>/g, ' ')
.replace(/ /gi, ' ')
.replace(/&/gi, '&')
.replace(/</gi, '<')
.replace(/>/gi, '>')
.replace(/"/gi, '"')
.replace(/'|'/gi, "'")
out = out.replace(/[ \t]+/g, ' ').replace(/ *\n */g, '\n').replace(/\n{3,}/g, '\n\n')
return out.trim()
}
export function truncateText(text: string, maxChars: number): { text: string; truncated: boolean } {
if (text.length <= maxChars) return { text, truncated: false }
const cut = text.slice(0, maxChars)
const lastBreak = Math.max(cut.lastIndexOf('\n'), cut.lastIndexOf(' '))
// 窗口内没有空格/换行时必须硬切:否则 lastBreak 为 0,正文会被整段丢成空串。
const head = lastBreak > 0 ? cut.slice(0, lastBreak) : cut
return { text: head + '\n\n…[正文过长,已截断,共 ' + text.length + ' 字符]', truncated: true }
}
/**
* Turn an untrusted attachment filename into a safe basename: no directory
* separators, no traversal, no control characters, bounded length.
*/
export function sanitizeFilename(raw: unknown, fallback = 'attachment.bin'): string {
let name = String(raw ?? '').replace(/\\/g, '/').split('/').pop() ?? ''
name = name.replace(/[\u0000-\u001f\u007f]/g, '').replace(/[<>:"|?*]/g, '_').trim().replace(/[. ]+$/g, '')
if (name === '' || name === '.' || name === '..' || /^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(name)) name = fallback
if (name.length > 120) {
const dot = name.lastIndexOf('.')
const ext = dot > 0 && dot >= name.length - 12 ? name.slice(dot) : ''
name = name.slice(0, 120 - ext.length) + ext
}
return name
}
/** Parse a raw RFC822 message source into the read-result body. */
export async function parseRawMessage(source: Buffer, maxBodyChars: number): Promise {
const parsed = await simpleParser(source, { skipImageLinks: true, keepCidLinks: true, skipHtmlToText: false })
let text = parsed.text ?? ''
if (text.trim() === '' && typeof parsed.html === 'string' && parsed.html.trim() !== '') {
text = stripHtml(parsed.html)
}
const limited = truncateText(text, maxBodyChars)
const attachments: EmailAttachmentMeta[] = (parsed.attachments ?? []).map((att, index) => ({
filename: att.filename ?? '(unnamed)',
contentType: typeof att.contentType === 'string' ? att.contentType : 'application/octet-stream',
size: Number.isFinite(att.size) && att.size >= 0 ? att.size : 0,
part: 'attachment-' + index,
...(att.contentId ? { contentId: att.contentId } : {}),
}))
const rich = richEmailBody(parsed.html, attachments)
return {
date: parsed.date instanceof Date && Number.isFinite(parsed.date.getTime()) ? parsed.date.toISOString() : '',
from: flattenAddresses(parsed.from),
to: flattenAddresses(parsed.to),
cc: flattenAddresses(parsed.cc),
subject: parsed.subject ?? '',
text: limited.text,
attachments,
...(rich ? { rich } : {}),
truncated: limited.truncated,
}
}