'use strict'; // Email to card (#794, #3948): an email sent to a member's personal board // address becomes a card on that board, the way Trello's email-to-board does. // // Pure (no Meteor, no Mongo), so it is shared by the client (the settings UI) // and the server (the inbound paths), and tested with plain Node // (tests/emailToCard.test.cjs). // // Where it is configured - Notification Settings, at three levels: // - Admin Panel: whether inbound email is on at all, the address domain, and // the mail servers (IMAP accounts) with their passwords - server-side only. // The admin switch is a hard gate: nothing below can turn on what the // administrator has off. // - Board Settings: whether this board accepts email, which list and // swimlane new cards go to, and which parts of an email are used. // - Member Settings: whether the member's own addresses work, and the parts // they want (overriding the board's). // Member over board over admin, as for the other notification settings // (models/lib/notificationSettings.js); an unset value falls through. // What an email can contribute to a card. `title` is always the subject. const EMAIL_PARTS = Object.freeze({ // The card description: 'text' (the plain-text body, or the HTML body as // text) or 'none'. description: Object.freeze(['text', 'none']), // Drop the quoted earlier messages ("On ... wrote:", "> ..."). stripQuoted: 'boolean', // Attach the email's attachments to the card. attachments: 'boolean', // Start the description with "From: Name
" - the card's author is // the address's member, so this keeps who actually wrote a forwarded email. senderLine: 'boolean', }); const DEFAULT_PARTS = Object.freeze({ description: 'text', stripQuoted: true, attachments: true, senderLine: true, }); const MAX_TITLE = 255; const MAX_DESCRIPTION = 100000; function validPart(name, value) { const kind = EMAIL_PARTS[name]; if (Array.isArray(kind)) return kind.includes(value); return kind === 'boolean' && (value === true || value === false); } // Only known parts with valid values; anything else is dropped, so a stored // or submitted object can never add a field the resolver does not know. function cleanParts(parts) { const out = {}; if (!parts || typeof parts !== 'object') return out; for (const name of Object.keys(EMAIL_PARTS)) { if (validPart(name, parts[name])) out[name] = parts[name]; } return out; } const bool = value => (value === true || value === false ? value : undefined); // The effective settings for one member on one board. // admin { enabled, domain } Settings.inboundMail // board { enabled, listId, swimlaneId, parts } board.emailToCard // member { enabled, parts } user.profile.emailToCard function resolveEmailToCard({ admin = {}, board = {}, member = {} } = {}) { const adminOn = bool(admin.enabled) === true && typeof admin.domain === 'string' && admin.domain.trim() !== ''; // Off on a board until its admin turns it on; on for a member unless they // turn their own addresses off. const boardOn = bool(board.enabled) === true; const memberOn = bool(member.enabled) !== false; return { enabled: adminOn && boardOn && memberOn, reason: !adminOn ? 'admin-off' : !boardOn ? 'board-off' : !memberOn ? 'member-off' : null, listId: typeof board.listId === 'string' ? board.listId : null, swimlaneId: typeof board.swimlaneId === 'string' ? board.swimlaneId : null, parts: { ...DEFAULT_PARTS, ...cleanParts(board.parts), ...cleanParts(member.parts) }, }; } // "Name " or "address", for the sender line. function formatSender(from) { if (!from || !from.address) return ''; return from.name ? `${from.name} <${from.address}>` : from.address; } // The HTML body as plain text, when an email has no text part: tags removed, // block ends kept as line breaks, the common entities decoded. Good enough for // a card description; nothing here is ever rendered as HTML. // Tags are removed by ONE pass over the characters, not by regular // expressions: a scanner never puts back together what it took apart, so text // around a removed tag cannot become a tag ("