const { randomUUID } = require('node:crypto'); const CUSTOM_FOLDER_PREFIX = 'Folder:'; const RESERVED_FOLDER_NAMES = new Set(['inbox', 'sent', 'drafts', 'archive', 'trash', 'starred', 'snoozed', 'junk', 'spam']); function normalizeAccount(input, { createId = randomUUID } = {}) { const email = String(input?.email || '').trim(); const password = String(input?.password || ''); const imapHost = String(input?.imapHost || '').trim(); const smtpHost = String(input?.smtpHost || '').trim(); const displayName = String(input?.displayName || '').trim().slice(0, 80); if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email) || !password || !imapHost || !smtpHost) { throw new Error('Enter a valid email, password, and both mail server names.'); } if (!displayName) throw new Error('Enter a display name for this mail account.'); const syncIntervalMinutes = Number(input?.syncIntervalMinutes ?? 2); if (![2, 5, 15, 30, 60].includes(syncIntervalMinutes)) { throw new Error('Choose a supported mailbox sync interval.'); } const inputRules = Array.isArray(input?.rules) ? input.rules : []; if (inputRules.length > 50) throw new Error('You can set up to 50 mail rules per account.'); const rules = inputRules.map((rule, index) => { const senderContains = String(rule?.senderContains || '').trim().slice(0, 200); const subjectContains = String(rule?.subjectContains || '').trim().slice(0, 200); const action = String(rule?.action || ''); const value = String(rule?.value || '').trim().slice(0, 200); if ((!senderContains && !subjectContains) || !['archive', 'move', 'label', 'flag'].includes(action)) { throw new Error(`Mail rule ${index + 1} needs a sender or subject condition and a valid action.`); } if ((action === 'move' || action === 'label') && !value) { throw new Error(`Mail rule ${index + 1} needs a destination folder or label.`); } if (action === 'move' && !['Inbox', 'Archive', 'Trash'].includes(value) && !value.startsWith(CUSTOM_FOLDER_PREFIX)) { throw new Error(`Mail rule ${index + 1} must move messages to Inbox, Archive, Trash, or one of your folders.`); } return { id: String(rule.id || createId()), senderContains, subjectContains, action, value }; }); return { email, password, displayName, imapHost, smtpHost, signature: String(input?.signature || '').slice(0, 5000), notificationsEnabled: input?.notificationsEnabled !== false, syncIntervalMinutes, rules, imapPort: 143, smtpPort: 587 }; } function publicAccount(account) { return { id: account.id, email: account.email, displayName: account.displayName || '', imapHost: account.imapHost, smtpHost: account.smtpHost, signature: account.signature || '', notificationsEnabled: account.notificationsEnabled !== false, syncIntervalMinutes: Number(account.syncIntervalMinutes) || 2, rules: Array.isArray(account.rules) ? account.rules : [] }; } function isReservedFolderName(name) { return RESERVED_FOLDER_NAMES.has(String(name).toLowerCase()); } function validFolderName(input) { const name = String(input || '').trim(); if (!name || name.length > 100) throw new Error('Enter a folder name of up to 100 characters.'); if (/[\\/.*%#&\u0000-\u001f]/.test(name)) throw new Error('Folder names cannot contain / \\ . * % # & or control characters.'); if (isReservedFolderName(name)) throw new Error('That folder name is reserved.'); return name; } function filterMailboxMessages(messages, filters = {}) { const text = String(filters.query || '').trim().toLowerCase(); const sender = String(filters.sender || '').trim().toLowerCase(); const subject = String(filters.subject || '').trim().toLowerCase(); const after = filters.after ? new Date(`${filters.after}T00:00:00`).getTime() : null; const before = filters.before ? new Date(`${filters.before}T23:59:59.999`).getTime() : null; return messages.filter(message => { const haystack = `${message.sender || ''} ${message.email || ''} ${message.subject || ''} ${message.snippet || ''} ${(message.body || []).join(' ')}`.toLowerCase(); const date = Number(message.timestamp || 0); return (!text || text.split(/\s+/).every(term => haystack.includes(term))) && (!sender || `${message.sender || ''} ${message.email || ''}`.toLowerCase().includes(sender)) && (!subject || String(message.subject || '').toLowerCase().includes(subject)) && (!after || date >= after) && (!before || date <= before) && (!filters.hasAttachments || (message.attachments || []).length > 0); }); } module.exports = { CUSTOM_FOLDER_PREFIX, filterMailboxMessages, isReservedFolderName, normalizeAccount, publicAccount, validFolderName };