Mail relay: review round 2 — the store is keyed by host, a copied database does not enrol by itself

- Q-1 one entry per host (iapl:mail-relay:site:<host>); a read never deletes or writes; another host's
  entry is neither used nor touched (a developer's process on the live database, www and apex, a copy)
- a database that holds another host's connection and none of this site's does not enrol by itself:
  "Connect again" does, once; mail.relay.otherHosts ('wait' | 'enrol' | the site's own other hosts);
  panel text and doctor finding; a site with an empty store enrols by itself as before
- Q-2 the file limit answers from what is stored and never asks the service
- Q-3 a file whose mail was not confirmed is not reported as a wrong file, and no notice mail follows
- Q-4 its own text when the access-key field is off
- D-1 docs for open enrolment; staging, copies and several hosts

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 23:14:11 +01:00
co-authored by Claude Fable 5.2
parent edee3a321f
commit 85ed92bb89
31 changed files with 467 additions and 211 deletions
+12 -11
View File
@@ -10,8 +10,8 @@ import { RELAY_DEFAULTS, type RelayStatusCode, relayStatusFor } from './constant
import { currentMailKind, type MailKind, mailKindOf, takeExpectedMail } from './kinds.js'
import { bareAddresses, buildRelayMessage, type RelaySendRequest } from './message.js'
import { relayHost } from './options.js'
import { LOCAL_CODES, readSiteStore, refreshRelayStatus, rotateRelayToken, type TokenUse, withRelayToken } from './session.js'
import { readRelayStore, sha256Hex, updateRelayStore } from './store.js'
import { LOCAL_CODES, readSiteStore, refreshRelayStatus, rotateRelayToken, type TokenUse, updateSiteStore, withRelayToken } from './session.js'
import { sha256Hex } from './store.js'
export type RelaySendResult =
| {
@@ -85,7 +85,7 @@ function describeRefusal(answer: RelayRefusal): { sent: false } & RelaySendResul
const extra = [answer.limit ? `limit: ${answer.limit}` : '', answer.reason ? `reason: ${answer.reason}` : '', answer.field ? `field: ${answer.field}` : ''].filter(Boolean).join(', ')
const said = `${answer.code}${extra ? ` (${extra})` : ''}: ${answer.message}`
if (answer.code === LOCAL_CODES.disconnected) {return failure('relay-disconnected', answer.code, answer.message)}
if (([LOCAL_CODES.foreignToken, LOCAL_CODES.noHost, LOCAL_CODES.noRoute, LOCAL_CODES.noToken] as string[]).includes(answer.code)) {return failure('relay-refused', answer.code, answer.message)}
if (([LOCAL_CODES.foreignToken, LOCAL_CODES.noHost, LOCAL_CODES.noRoute, LOCAL_CODES.noToken, LOCAL_CODES.otherHost] as string[]).includes(answer.code)) {return failure('relay-refused', answer.code, answer.message)}
// No answer at all, or a send the service cannot report on yet: it may be out.
if (answer.noAnswer || answer.code === 'in-progress') {return failure('relay-not-confirmed', answer.code, `Not confirmed — the message may have been sent. ${said}`, false)}
if (answer.code === 'callback-failed' && answer.reason === 'status') {
@@ -227,11 +227,11 @@ export async function sendThroughRelay(payload: BasePayload, options: ResolvedRe
const used = typeof answer.body.senderName === 'string' ? answer.body.senderName : ''
const replaced = warnings.includes('sender-name-replaced')
try {
const now = await readRelayStore(payload)
const now = await readSiteStore(payload, options)
const known = now.nameReplaced
const nameChanged = replaced ? known?.used !== used || known?.asked !== (request.senderName ?? '') : Boolean(known)
if (nameChanged || now.state !== 'active' || now.refusal) {
await updateRelayStore(payload, (current) => ({
await updateSiteStore(payload, options, (current) => ({
...current,
nameReplaced: replaced ? { asked: request.senderName ?? '', at: new Date().toISOString(), used } : null,
refusal: null,
@@ -269,19 +269,20 @@ export async function sendThroughRelay(payload: BasePayload, options: ResolvedRe
else {unconfirmed.delete(hash)}
if (answer.code === 'site-pending' || answer.code === 'site-suspended') {
const state = answer.code === 'site-pending' ? 'pending' : 'suspended'
if (stored.state !== state) {await updateRelayStore(payload, (current) => ({ ...current, state })).catch(() => undefined)}
if (stored.state !== state) {await updateSiteStore(payload, options, (current) => ({ ...current, state })).catch(() => undefined)}
}
// The panel's status is the LAST send's; a later good mail must not hide this one: kept with its time
// (written when the code changes, or every ten minutes while it lasts).
try {
const now = await readRelayStore(payload)
// (written when the code changes, or every ten minutes while it lasts). Not for a refusal of the site's own
// making that never reached the service (a local host, a copied database): such a process writes nothing.
if (!answer.code.startsWith('client-') && !(answer.code === 'host-not-allowed' && answer.status === 0)) {try {
const now = await readSiteStore(payload, options)
const last = now.lastFailure
if (!last || last.code !== result.code || Date.now() - Date.parse(last.at) > 10 * 60_000) {
await updateRelayStore(payload, (current) => ({ ...current, lastFailure: { at: new Date().toISOString(), code: result.code, kind } }))
await updateSiteStore(payload, options, (current) => ({ ...current, lastFailure: { at: new Date().toISOString(), code: result.code, kind } }))
}
} catch {
/* bookkeeping only */
}
}}
payload.logger.error(`[iapl] mail service: e-mail ${result.confirmed ? 'not sent' : 'not confirmed'} (${result.code}; ${answer.code}${answer.limit ? `, ${answer.limit}` : ''}${answer.reason ? `, ${answer.reason}` : ''}).`)
return result
}