125 lines
6.8 KiB
JavaScript
125 lines
6.8 KiB
JavaScript
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 authMethod = input?.authMethod === undefined ? 'password' : String(input.authMethod);
|
|
if (!['password', 'oauth2'].includes(authMethod)) throw new Error('Choose a supported sign-in method.');
|
|
const oauthProvider = authMethod === 'oauth2' ? String(input?.oauthProvider || '') : '';
|
|
if (authMethod === 'oauth2' && !['google', 'microsoft'].includes(oauthProvider)) {
|
|
throw new Error('Choose Google or Microsoft OAuth2 sign-in.');
|
|
}
|
|
const providerDefaults = oauthProvider === 'google'
|
|
? { imapHost: 'imap.gmail.com', smtpHost: 'smtp.gmail.com', imapPort: 993, smtpPort: 587, imapSecure: true }
|
|
: oauthProvider === 'microsoft'
|
|
? { imapHost: 'outlook.office365.com', smtpHost: 'smtp.office365.com', imapPort: 993, smtpPort: 587, imapSecure: true }
|
|
: { imapHost: '', smtpHost: '', imapPort: 143, smtpPort: 587, imapSecure: false };
|
|
const password = authMethod === 'password' ? String(input?.password || '') : '';
|
|
const imapHost = String(oauthProvider ? providerDefaults.imapHost : input?.imapHost || providerDefaults.imapHost).trim();
|
|
const smtpHost = String(oauthProvider ? providerDefaults.smtpHost : input?.smtpHost || providerDefaults.smtpHost).trim();
|
|
const displayName = String(input?.displayName || '').trim().slice(0, 80);
|
|
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email) || !imapHost || !smtpHost || (authMethod === 'password' && !password)) {
|
|
throw new Error(authMethod === 'password'
|
|
? 'Enter a valid email, password, and both mail server names.'
|
|
: 'Enter a valid email and supported OAuth mail servers.');
|
|
}
|
|
if (!displayName) throw new Error('Enter a display name for this mail account.');
|
|
const imapPort = Number(oauthProvider ? providerDefaults.imapPort : input?.imapPort ?? providerDefaults.imapPort);
|
|
const smtpPort = Number(oauthProvider ? providerDefaults.smtpPort : input?.smtpPort ?? providerDefaults.smtpPort);
|
|
if (![imapPort, smtpPort].every(port => Number.isInteger(port) && port >= 1 && port <= 65535)) {
|
|
throw new Error('Enter valid IMAP and SMTP ports.');
|
|
}
|
|
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, authMethod, oauthProvider: oauthProvider || null,
|
|
displayName, imapHost, smtpHost, imapPort, smtpPort,
|
|
imapSecure: oauthProvider ? providerDefaults.imapSecure : input?.imapSecure === undefined ? providerDefaults.imapSecure : Boolean(input.imapSecure),
|
|
smtpSecure: oauthProvider ? false : input?.smtpSecure === undefined ? false : Boolean(input.smtpSecure),
|
|
signature: String(input?.signature || '').slice(0, 5000),
|
|
notificationsEnabled: input?.notificationsEnabled !== false,
|
|
syncIntervalMinutes,
|
|
rules
|
|
};
|
|
}
|
|
|
|
function publicAccount(account) {
|
|
return {
|
|
id: account.id,
|
|
email: account.email,
|
|
displayName: account.displayName || '',
|
|
authMethod: account.authMethod === 'oauth2' ? 'oauth2' : 'password',
|
|
oauthProvider: account.authMethod === 'oauth2' ? account.oauthProvider || null : null,
|
|
imapHost: account.imapHost,
|
|
smtpHost: account.smtpHost,
|
|
imapPort: Number(account.imapPort) || (account.authMethod === 'oauth2' ? 993 : 143),
|
|
smtpPort: Number(account.smtpPort) || 587,
|
|
imapSecure: Boolean(account.imapSecure),
|
|
smtpSecure: Boolean(account.smtpSecure),
|
|
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
|
|
};
|