SPB Git

Repository Public

outbound.ts 17.1 KB 239 lines typescript

1// File d'envoi persistante et suivi de remise. Le vocabulaire d'état ne reflète que des preuves réelles :2//   queued (accepté par SP Mail, pas encore remis à Stalwart) · sent (dans la file de Stalwart) · delivered (2xx du serveur du destinataire)3//   deferred (échec temporaire, nouvelle tentative) · bounced (rejet permanent 5xx) · failed (jamais parti) · scheduled · pending_approval · cancelled.4import { addressById, canSendNow, ensureIdentity, isActive, type AddressRow } from "./addresses.js"5import { config } from "./config.js"6import { sql } from "./db.js"7import type { KeyPrincipal, Principal } from "./keys.js"8import { jmap, jmapOk, mailboxById, roleFolder, type Mbx } from "./mailjmap.js"9import { hostedDomains } from "./stalwart.js"10import { newOutboundId, msgId, threadId } from "./v1/ids.js"11import { emitEvent, type EventType } from "./webhooks.js"12import { bad, HttpError } from "./util.js"1314export type RecipientStatus = { address: string; status: "queued" | "sent" | "delivered" | "deferred" | "bounced" | "failed" | "scheduled" | "pending_approval" | "cancelled"; smtp_reply?: string | null }15export type OutboundRow = {16  id: string; key_id: string | null; user_id: string | null; address_id: string | null; from_address: string; mailbox_id: string; email_id: string | null17  submission_id: string | null; recipients: RecipientStatus[]; status: RecipientStatus["status"]; external: boolean; send_at: Date | null; attempts: number18  last_error: string | null; next_check_at: Date | null; submitted_at: Date | null; final_at: Date | null; created_at: Date; updated_at: Date19  /** SP Mail Next (migration 005) : fin de la fenêtre d'annulation d'envoi, rappel demandé à l'envoi. */20  undo_until?: Date | null; remind_at?: Date | null; remind_if_no_reply?: boolean | null; reminded_at?: Date | null21}22export const FINAL = new Set(["delivered", "bounced", "failed", "cancelled"])2324// ------------------------------------------------------------------ fonctions pures (testées unitairement)25/** « Code: 250, Enhanced code: 2.1.5, Message: OK » ou « 550 5.1.1 Mailbox does not exist. » → { code, text } normalisé. */26export function parseSmtpReply(raw: string | null | undefined): { code: number | null; text: string | null } {27  if (!raw) return { code: null, text: null }28  const s = String(raw).replace(/[\r\n]+/g, " ").trim()29  const m = /^Code:\s*(\d{3}),\s*Enhanced code:\s*([\d.]+),\s*Message:\s*(.*)$/i.exec(s)30  if (m) return { code: Number(m[1]), text: `${m[1]} ${m[2]} ${m[3]}`.trim().slice(0, 500) }31  const c = /^(\d{3})\b/.exec(s)32  return { code: c ? Number(c[1]) : null, text: s.slice(0, 500) }33}34/** État d'un destinataire d'après `deliveryStatus` de JMAP (RFC 8621 §7) tel que Stalwart le renseigne. */35export function mapDelivery(ds: { delivered?: string; smtpReply?: string } | undefined): { status: RecipientStatus["status"]; smtp_reply: string | null } {36  const r = parseSmtpReply(ds?.smtpReply)37  switch (ds?.delivered) {38    case "yes": return { status: "delivered", smtp_reply: r.text }39    case "no": return { status: r.code !== null && r.code >= 500 ? "bounced" : "failed", smtp_reply: r.text }40    case "queued": return { status: r.code !== null && r.code >= 400 && r.code < 500 ? "deferred" : "sent", smtp_reply: r.text }41    default: return { status: "sent", smtp_reply: r.text }42  }43}44/** État global = le pire des états par destinataire (bounced > failed > deferred > sent > delivered). */45export function overallStatus(list: RecipientStatus[]): RecipientStatus["status"] {46  const order = ["bounced", "failed", "deferred", "sent", "queued", "delivered"] as const47  for (const s of order) if (list.some((r) => r.status === s)) return s48  return list[0]?.status ?? "sent"49}5051let extCache: { at: number; set: Set<string> } | undefined52/** Externe = hors des domaines hébergés (ou désigné par le crochet de test SPMAIL_TREAT_AS_EXTERNAL). */53export async function isExternal(address: string): Promise<boolean> {54  const a = address.toLowerCase()55  const domain = a.split("@")[1] ?? ""56  if (config.treatAsExternal.includes(a) || config.treatAsExternal.includes(domain)) return true57  if (!extCache || Date.now() - extCache.at > 60_000) extCache = { at: Date.now(), set: new Set((await hostedDomains()).map((d) => d.name.toLowerCase())) }58  return !extCache.set.has(domain)59}6061export function presentOutbound(o: OutboundRow, extra: Record<string, unknown> = {}) {62  return {63    id: o.id, status: o.status, from: o.from_address,64    message_id: o.email_id ? msgId(o.mailbox_id, o.email_id) : null,65    recipients: o.recipients.map((r) => ({ address: r.address, status: r.status, smtp_reply: r.smtp_reply ?? null })),66    external: o.external, send_at: o.send_at ? new Date(o.send_at).toISOString() : null, attempts: o.attempts, last_error: o.last_error,67    submitted_at: o.submitted_at ? new Date(o.submitted_at).toISOString() : null, final_at: o.final_at ? new Date(o.final_at).toISOString() : null,68    created_at: new Date(o.created_at).toISOString(), updated_at: new Date(o.updated_at).toISOString(),69    ...(o.undo_until ? { undo_until: new Date(o.undo_until).toISOString() } : {}), ...extra,70  }71}7273// ------------------------------------------------------------------ limites par clé74export async function sentToday(keyId: string): Promise<number> {75  const [r] = await sql`select count(*)::int as n from outbound_messages where key_id = ${keyId}76    and created_at >= date_trunc('day', now() at time zone 'utc') at time zone 'utc' and status not in ('cancelled')`77  return r.n as number78}79export async function assertDailyLimit(p: KeyPrincipal, adding = 1) {80  const n = await sentToday(p.keyId)81  if (n + adding > p.dailySendLimit) throw bad("daily_send_limit_exceeded", 429, `Daily send limit of ${p.dailySendLimit} messages reached for this key`, { limit: p.dailySendLimit, used: n })82}8384// ------------------------------------------------------------------ création et soumission85const eventOf: Record<string, EventType | undefined> = { sent: "message.sent", delivered: "message.delivered", deferred: "message.deferred", bounced: "message.bounced", failed: "message.failed" }86function eventData(o: OutboundRow) {87  return { outbound_id: o.id, message_id: o.email_id ? msgId(o.mailbox_id, o.email_id) : null, from: o.from_address, status: o.status, recipients: o.recipients.map((r) => ({ address: r.address, status: r.status, smtp_reply: r.smtp_reply ?? null })) }88}89async function setStatus(o: OutboundRow, patch: Partial<OutboundRow>, emit = true): Promise<OutboundRow> {90  const next = { ...o, ...patch }91  const changed = next.status !== o.status92  await sql.begin(async (tx) => {93    await tx`update outbound_messages set status = ${next.status}, recipients = ${tx.json(next.recipients as never)}, submission_id = ${next.submission_id}, attempts = ${next.attempts},94      last_error = ${next.last_error}, next_check_at = ${next.next_check_at}, submitted_at = ${next.submitted_at}, final_at = ${next.final_at}, send_at = ${next.send_at},95      locked_until = null, updated_at = now() where id = ${o.id}`96    const ev = eventOf[next.status]97    if (emit && changed && ev) await emitEvent({ type: ev, addressId: o.address_id, address: o.from_address, mailboxId: o.mailbox_id, data: eventData(next) }, tx as never)98  })99  return next100}101102export type NewOutbound = {103  principal: Principal; address: AddressRow; mailbox: Mbx; emailId: string; recipients: string[]; external: boolean104  mode: "now" | "scheduled" | "pending_approval"; sendAt?: Date | null; externalRecipients?: string[]105  /** Annulation d'envoi : le message est retenu (programmé) jusqu'à cet instant. Rappel : créé une fois le message soumis. */106  undoUntil?: Date | null; remindAt?: Date | null; remindIfNoReply?: boolean107}108/** Enregistre un message à envoyer (le contenu est déjà un brouillon dans Stalwart) puis, en mode « now », le soumet tout de suite. */109export async function createOutbound(n: NewOutbound): Promise<{ row: OutboundRow; approvalId?: string }> {110  const id = newOutboundId()111  const status = n.mode === "now" ? "queued" : n.mode112  const recipients: RecipientStatus[] = n.recipients.map((address) => ({ address, status, smtp_reply: null }))113  let approvalId: string | undefined114  const row = await sql.begin(async (tx) => {115    // En mode « now », la requête détient le bail (locked_until) : le répartiteur ne soumettra pas ce message en double.116    const [r] = await tx`insert into outbound_messages (id, key_id, user_id, address_id, from_address, mailbox_id, email_id, recipients, status, external, send_at, next_check_at, locked_until, undo_until, remind_at, remind_if_no_reply)117      values (${id}, ${n.principal.type === "key" ? n.principal.keyId : null}, ${n.principal.type === "user" ? n.principal.userId : null}, ${n.address.id}, ${n.address.address},118        ${n.mailbox.id}, ${n.emailId}, ${tx.json(recipients as never)}, ${status}, ${n.external}, ${n.sendAt ?? null}, ${n.mode === "scheduled" ? n.sendAt ?? null : n.mode === "now" ? new Date() : null},119        ${n.mode === "now" ? new Date(Date.now() + 120_000) : null}, ${n.undoUntil ?? null}, ${n.remindAt ?? null}, ${n.remindAt ? !!n.remindIfNoReply : null})120      returning *`121    if (n.mode === "pending_approval") {122      const [a] = await tx`insert into send_approvals (outbound_id, key_id, external_recipients, expires_at)123        values (${id}, ${n.principal.type === "key" ? n.principal.keyId : null}, ${n.externalRecipients ?? []}, now() + interval '7 days') returning id`124      approvalId = a.id125    }126    return r as unknown as OutboundRow127  })128  if (n.mode !== "now") return { row, approvalId }129  return { row: await submitOutbound(row) }130}131132const BACKOFF_SUBMIT = [5, 15, 60, 300, 900, 1800, 3600]133/**134 * Remet un message à Stalwart (EmailSubmission). Revérifie au moment de l'envoi que l'adresse peut toujours envoyer et que la clé135 * n'a pas été révoquée : un message programmé ou approuvé ne part pas si ses droits ont disparu entre-temps.136 */137export async function submitOutbound(o: OutboundRow): Promise<OutboundRow> {138  const fail = (msg: string) => setStatus(o, { status: "failed", last_error: msg.slice(0, 500), final_at: new Date(), next_check_at: null, recipients: o.recipients.map((r) => ({ ...r, status: "failed" })) })139  const mb = await mailboxById(o.mailbox_id)140  const addr = o.address_id ? await addressById(o.address_id) : null141  if (!mb || !addr || !o.email_id) return fail("the sender address no longer exists")142  if (!isActive(addr) || !canSendNow(addr)) return fail("the sender address is suspended, expired or not allowed to send")143  if (o.key_id) {144    const [k] = await sql`select revoked_at, expires_at from api_keys where id = ${o.key_id}`145    if (!k || (k.revoked_at && new Date(k.revoked_at).getTime() <= Date.now()) || (k.expires_at && new Date(k.expires_at).getTime() <= Date.now())) return fail("the API key that created this message was revoked or has expired")146  }147  try {148    const identityId = await ensureIdentity(mb, addr)149    const drafts = await roleFolder(mb, "drafts")150    const sent = await roleFolder(mb, "sent", true)151    const patch: Record<string, unknown> = { "keywords/$draft": null, [`mailboxIds/${sent}`]: true }152    if (drafts) patch[`mailboxIds/${drafts}`] = null153    const res = await jmap(mb, [["EmailSubmission/set", { create: { s: { identityId, emailId: o.email_id } }, onSuccessUpdateEmail: { "#s": patch } }, "s"]])154    const [name, body] = res[0]155    if (name === "error") return fail(`mail server refused the submission: ${body.type}${body.description ? ` (${body.description})` : ""}`)156    const created = body.created?.s157    if (!created) {158      const err = body.notCreated?.s ?? {}159      return fail(`mail server refused the submission: ${err.type ?? "unknown"}${err.description ? ` (${err.description})` : ""}`)160    }161    const done = await setStatus(o, { status: "sent", submission_id: created.id, submitted_at: new Date(), attempts: o.attempts + 1, last_error: null, next_check_at: new Date(Date.now() + 800), recipients: o.recipients.map((r) => ({ ...r, status: "sent" })) })162    await sendReminder(done)163    return done164  } catch (e) {165    // Serveur de courrier injoignable : le message reste « queued » et sera représenté par le répartiteur.166    const attempts = o.attempts + 1167    const delay = BACKOFF_SUBMIT[attempts - 1]168    const msg = e instanceof HttpError ? e.message : String((e as Error).message ?? e)169    if (delay === undefined) return fail(`gave up after ${attempts} attempts: ${msg}`)170    return setStatus(o, { status: "queued", attempts, last_error: msg.slice(0, 500), next_check_at: new Date(Date.now() + delay * 1000), recipients: o.recipients.map((r) => ({ ...r, status: "queued" })) }, false)171  }172}173174type ReminderInput = { mailboxId: string; emailId: string; dueAt: Date; onlyIfNoReply: boolean; note?: string }175/**176 * Rappel demandé à l'envoi (`remind_at`) : créé une seule fois, quand le message a réellement été soumis. Le module des rappels177 * (next/snooze.ts) est chargé à la demande ; s'il est absent ou en panne, le suivi de remise réessaie tant que le message est suivi.178 */179async function sendReminder(o: OutboundRow): Promise<void> {180  if (!o.remind_at || o.reminded_at || !o.user_id || !o.email_id) return181  try {182    const mod = (await import("./next/snooze.js")) as unknown as { createReminder?: (userId: string, r: ReminderInput) => Promise<unknown> }183    if (typeof mod.createReminder !== "function") return184    await mod.createReminder(o.user_id, { mailboxId: o.mailbox_id, emailId: o.email_id, dueAt: new Date(o.remind_at), onlyIfNoReply: !!o.remind_if_no_reply })185    await sql`update outbound_messages set reminded_at = now() where id = ${o.id}`186    o.reminded_at = new Date()187  } catch { /* nouvel essai au prochain passage du suivi de remise */ }188}189190/** Lit `EmailSubmission/get` et met à jour l'état par destinataire. Les événements sont émis sur changement de l'état global. */191export async function trackOutbound(o: OutboundRow): Promise<OutboundRow> {192  await sendReminder(o)193  const mb = await mailboxById(o.mailbox_id)194  if (!mb || !o.submission_id) return setStatus(o, { next_check_at: null }, false)195  const [r] = await jmapOk(mb, [["EmailSubmission/get", { ids: [o.submission_id], properties: ["undoStatus", "deliveryStatus", "sendAt"] }]])196  const sub = (r.list as { undoStatus: string; deliveryStatus: Record<string, { delivered: string; smtpReply: string }> | null }[])[0]197  const age = Date.now() - new Date(o.submitted_at ?? o.created_at).getTime()198  if (!sub) return setStatus(o, { next_check_at: null, last_error: "the mail server no longer holds this submission record; last known status kept" }, false)199  const ds = sub.deliveryStatus ?? {}200  const lower = Object.fromEntries(Object.entries(ds).map(([k, v]) => [k.toLowerCase(), v]))201  const known = new Set(o.recipients.map((x) => x.address))202  // Une liste de diffusion est développée par le serveur : ses membres apparaissent en plus du destinataire demandé.203  const expanded = Object.entries(lower).filter(([k]) => !known.has(k)).map(([, v]) => mapDelivery(v))204  const recipients = o.recipients.map((x) => {205    const d = lower[x.address]206    if (!d) return x207    let m = mapDelivery(d)208    if (d.delivered === "unknown" && expanded.length && expanded.every((e) => FINAL.has(e.status)))209      m = { status: expanded.every((e) => e.status === "delivered") ? "delivered" : overallStatus(expanded.map((e) => ({ address: "", status: e.status }))), smtp_reply: m.smtp_reply }210    return { address: x.address, status: m.status, smtp_reply: m.smtp_reply }211  })212  const status = overallStatus(recipients)213  const done = recipients.every((x) => FINAL.has(x.status))214  const giveUp = age > 5 * 86400_000215  const next = done || giveUp ? null : new Date(Date.now() + (age < 60_000 ? 1500 : age < 600_000 ? 15_000 : 300_000))216  return setStatus(o, { status, recipients, next_check_at: next, final_at: done ? new Date() : null })217}218219/**220 * Annule un message programmé ou en attente d'approbation : le brouillon est détruit, rien ne part.221 * `keepDraft` (annulation d'envoi, SP Mail Next) : le brouillon reste dans « Brouillons », pièces jointes comprises, pour que la rédaction222 * reprenne. Il n'est alors annulé que si le répartiteur ne l'a pas déjà pris en charge (bail) — sinon le message partirait malgré l'annulation.223 */224export async function cancelOutbound(o: OutboundRow, reason: string, keepDraft = false): Promise<OutboundRow> {225  const r = await sql`update outbound_messages set status = 'cancelled', last_error = ${reason}, final_at = now(), next_check_at = null, updated_at = now(),226      recipients = (select coalesce(jsonb_agg(jsonb_set(x, '{status}', '"cancelled"')), '[]'::jsonb) from jsonb_array_elements(recipients) x)227    where id = ${o.id} and status in ('scheduled','pending_approval','queued') ${keepDraft ? sql`and (locked_until is null or locked_until < now())` : sql``} returning *`228  if (!r.length) throw bad("not_cancellable", 409, "This message can no longer be cancelled")229  const mb = await mailboxById(o.mailbox_id)230  if (!keepDraft && mb && o.email_id) await jmap(mb, [["Email/set", { destroy: [o.email_id] }]]).catch(() => {})231  return r[0] as unknown as OutboundRow232}233234export async function outboundById(id: string): Promise<OutboundRow | null> {235  const [r] = await sql`select * from outbound_messages where id = ${id}`236  return (r as OutboundRow | undefined) ?? null237}238export { threadId }239
Files main
Menu

Theme