// An SMTP server the server sends its email through (spec/EMAIL.md, // "SMTP"): over `node:net`, on the loopback, plain text, offering no // STARTTLS, and AUTH PLAIN and LOGIN, which it requires, with the user and // password it was made with. It takes EHLO, HELO, AUTH, MAIL FROM, RCPT TO, // DATA, RSET, NOOP and QUIT, and keeps each message whose data it read, // with its envelope and whether it refused it: while `refusing` is set, it // answers the end of each message's data `554`. import { createServer } from 'node:net' /// Each line it answers, as the reply of `code` and `text`. const reply = (code, text) => `${code} ${text}\r\n` /// The SMTP server, once listening: `{url, from, refusing, take(), close()}` /// (see `startSmtp`), or null before. let sink = null export const smtpSink = () => sink /// A command's argument as sent: the socket is read as latin1, a byte to a /// character, and SMTPUTF8 addresses are UTF-8. const utf8 = (s) => Buffer.from(s, 'latin1').toString('utf8') /// Start the SMTP server, requiring `user` and `password` to sign in, for a /// server whose emails are to be from `from`; or answer the one started /// already, so that a server started again sends to the same one. Answers /// `{url, from, refusing, take(), close()}`: `url` is /// `smtp://user:password@127.0.0.1:port`, each percent-encoded; `refusing`, /// false at first, makes it refuse each message at the end of its data /// while set; and `take()` answers what it read since it was last called, /// oldest first: each message `{from, to, data, refused}`, the envelope's /// sender and recipients as given, without `<>`, and the data /// dot-unstuffed, its lines joined by `\r\n`; and each command it refused /// as `{problem, line}`, as a client that signs in wrongly or not at all. export async function startSmtp({ user, password, from }) { if (sink) return sink const got = [] const server = createServer((socket) => { socket.setEncoding('latin1') let buf = '' let authed = false let login = null let mail = null let data = null const send = (code, text) => socket.write(reply(code, text)) const problem = (why, line) => got.push({ problem: why, line }) const credentials = (u, p) => { if (u === user && p === password) { authed = true return send(235, '2.7.0 Authentication successful') } problem('AUTH with other credentials than PDS_EMAIL_SMTP_URL gives', JSON.stringify({ user: u, password: p })) send(535, '5.7.8 Authentication credentials invalid') } const b64 = (s) => Buffer.from(s, 'base64').toString('utf8') const line = (l) => { if (data) { if (l !== '.') return void data.push(l.startsWith('.') ? l.slice(1) : l) const message = { from: mail.from, to: mail.to, data: Buffer.from(data.join('\r\n'), 'latin1').toString('utf8'), refused: sink.refusing } got.push(message) data = null mail = null return message.refused ? send(554, '5.6.0 Message refused') : send(250, '2.0.0 OK') } if (login) { const step = login if (step.user === undefined) { login = { user: b64(l) } return send(334, 'UGFzc3dvcmQ6') } login = null return credentials(step.user, b64(l)) } const [verb, ...rest] = l.split(' ') const arg = rest.join(' ') switch (verb.toUpperCase()) { case 'EHLO': mail = null return socket.write('250-pds-harness.test\r\n250-8BITMIME\r\n250-AUTH PLAIN LOGIN\r\n250 SMTPUTF8\r\n') case 'HELO': mail = null return send(250, 'pds-harness.test') case 'AUTH': { const [mech, initial] = arg.split(' ') if (mech?.toUpperCase() === 'PLAIN') { if (!initial) { login = { plain: true } return send(334, '') } const [, u, p] = b64(initial).split('\0') return credentials(u, p) } if (mech?.toUpperCase() === 'LOGIN') { if (initial) { login = { user: b64(initial) } return send(334, 'UGFzc3dvcmQ6') } login = {} return send(334, 'VXNlcm5hbWU6') } problem('AUTH by a mechanism not offered', l) return send(504, '5.5.4 Unrecognized authentication type') } case 'MAIL': { const m = /^FROM:\s*<([^>]*)>/i.exec(arg) if (!m) return send(501, '5.5.4 Syntax: MAIL FROM:
') if (!authed) { problem('MAIL FROM without signing in', l) return send(530, '5.7.0 Authentication required') } mail = { from: utf8(m[1]), to: [] } return send(250, '2.1.0 OK') } case 'RCPT': { const m = /^TO:\s*<([^>]*)>/i.exec(arg) if (!mail) return send(503, '5.5.1 MAIL first') if (!m) return send(501, '5.5.4 Syntax: RCPT TO:') mail.to.push(utf8(m[1])) return send(250, '2.1.5 OK') } case 'DATA': if (!mail?.to.length) return send(503, '5.5.1 RCPT first') data = [] return send(354, 'End data with