- Release removes the held message and queues its deliveries in one transaction - Of several simultaneous approvals one wins, the others get 404 - Extract the thread-root computation and test its linearity without the database (fast under -race)
274 lines
8.9 KiB
Go
274 lines
8.9 KiB
Go
// Package router decides what happens to an incoming message.
|
|
package router
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"log"
|
|
"net/mail"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"codeberg.org/squidd/parsimony/internal/archive"
|
|
"codeberg.org/squidd/parsimony/internal/store"
|
|
)
|
|
|
|
var (
|
|
// ErrUnknownList means the recipient address is not a list or a list command.
|
|
ErrUnknownList = errors.New("unknown list")
|
|
// ErrDiscarded means the message was dropped on purpose (a loop or an automatic reply). It is
|
|
// acknowledged to the MTA: refusing it would make the sender's server bounce it, starting a new loop.
|
|
ErrDiscarded = errors.New("discarded")
|
|
// ErrHeldFull means the list's moderation queue is full: refuse for now, the sender may retry.
|
|
ErrHeldFull = store.ErrHeldFull
|
|
// ErrNotMember means the sender may not post to the list.
|
|
ErrNotMember = errors.New("sender is not a member")
|
|
// ErrHeld means the message was queued for moderation. It is not a failure.
|
|
ErrHeld = errors.New("held for moderation")
|
|
// ErrBadToken means a confirmation or bounce token is invalid, used or expired.
|
|
ErrBadToken = errors.New("invalid or expired token")
|
|
// ErrGone means the member behind a valid token is already unsubscribed.
|
|
ErrGone = errors.New("already unsubscribed")
|
|
// ErrJoinClosed means the list does not accept self-service subscriptions.
|
|
ErrJoinClosed = errors.New("list is closed")
|
|
)
|
|
|
|
// Recipient is a subscriber that receives a message.
|
|
type Recipient struct {
|
|
ID int64 // member id, used for the VERP bounce address
|
|
Address string
|
|
}
|
|
|
|
// Result describes how to deliver an accepted message.
|
|
type Result struct {
|
|
List store.List
|
|
Recipients []Recipient
|
|
Data []byte // message to send, with list headers
|
|
}
|
|
|
|
// Router applies list policy to incoming messages.
|
|
type Router struct {
|
|
store *store.Store
|
|
// BaseURL is the public web URL; enables one-click unsubscribe when set.
|
|
BaseURL string
|
|
// OnEnqueue, when set, is called after mail is queued (to wake the sender).
|
|
OnEnqueue func()
|
|
|
|
secretOnce sync.Once
|
|
secretVal []byte
|
|
secretErr error
|
|
}
|
|
|
|
// New creates a Router backed by s.
|
|
func New(s *store.Store) *Router { return &Router{store: s} }
|
|
|
|
// Route validates a message from sender to the list address rcpt.
|
|
// Moderated lists queue the message and return ErrHeld.
|
|
func (r *Router) Route(sender, rcpt string, data []byte) (Result, error) {
|
|
l, err := r.store.FindList(rcpt)
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
return Result{}, ErrUnknownList
|
|
}
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
if why, drop := discardReason(l, data); drop {
|
|
log.Printf("discard post to %s from %s: %s", l.Address(), sender, why)
|
|
return Result{}, ErrDiscarded
|
|
}
|
|
if l.PostPolicy == store.PostModerated {
|
|
return Result{}, r.hold(l, sender, data, "the list is moderated")
|
|
}
|
|
ok, err := r.store.IsMember(l.ID, sender)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
if !ok {
|
|
return Result{}, ErrNotMember
|
|
}
|
|
// Membership is judged on the envelope sender, which anyone can forge, while the archive, the
|
|
// rewritten From and Reply-To come from the From header. Only trust a post whose header names
|
|
// the same address: otherwise a member could pass as someone else. An admin decides on the rest.
|
|
if from, ok := fromHeaderAddress(data); !ok || !strings.EqualFold(strings.TrimSpace(sender), from) {
|
|
return Result{}, r.hold(l, sender, data, "the From header does not match the sender of the message ("+displayFrom(data, from, ok)+")")
|
|
}
|
|
return r.prepare(l, data)
|
|
}
|
|
|
|
// hold queues a message for the admin and reports it as held.
|
|
func (r *Router) hold(l store.List, sender string, data []byte, reason string) error {
|
|
if _, err := r.store.HoldMessage(l.ID, sender, data, reason); err != nil {
|
|
return err
|
|
}
|
|
return ErrHeld
|
|
}
|
|
|
|
// prepare builds the delivery plan for a list.
|
|
func (r *Router) prepare(l store.List, data []byte) (Result, error) {
|
|
members, err := r.store.ActiveMembers(l.ID)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
rcpts := make([]Recipient, len(members))
|
|
for i, m := range members {
|
|
rcpts[i] = Recipient{ID: m.ID, Address: m.Address}
|
|
}
|
|
r.archive(l, data)
|
|
out, err := rewrite(l, data)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
return Result{List: l, Recipients: rcpts, Data: out}, nil
|
|
}
|
|
|
|
// Deliver queues the message for every recipient, each with its own signed
|
|
// bounce address (VERP) and unsubscribe headers. The queue write is atomic: on
|
|
// error nothing was queued and the caller may safely retry.
|
|
func (r *Router) Deliver(res Result) error {
|
|
items, err := r.outbound(res)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return r.enqueue(res.Data, items)
|
|
}
|
|
|
|
// outbound builds one queue entry per recipient.
|
|
func (r *Router) outbound(res Result) ([]store.NewOutbound, error) {
|
|
items := make([]store.NewOutbound, 0, len(res.Recipients))
|
|
for _, rc := range res.Recipients {
|
|
from := r.bounceAddress(res.List, rc.ID)
|
|
if from == "" {
|
|
return nil, errors.New("cannot create bounce address")
|
|
}
|
|
items = append(items, store.NewOutbound{
|
|
From: from, Rcpt: rc.Address, MemberID: rc.ID, Prefix: r.unsubscribeHeaders(res.List, rc.ID),
|
|
})
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
// enqueue stores deliveries and wakes the sender.
|
|
func (r *Router) enqueue(data []byte, items []store.NewOutbound) error {
|
|
if err := r.store.Enqueue(data, items); err != nil {
|
|
return err
|
|
}
|
|
if r.OnEnqueue != nil && len(items) > 0 {
|
|
r.OnEnqueue()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// unsubscribeHeaders builds the per-recipient List-Unsubscribe headers: a signed
|
|
// one-click link (RFC 8058) when BaseURL is set, and the +leave address.
|
|
func (r *Router) unsubscribeHeaders(l store.List, memberID int64) []byte {
|
|
value := "<mailto:" + l.Name + "+leave@" + l.Domain + ">"
|
|
var b strings.Builder
|
|
if tok := r.UnsubscribeToken(memberID); r.BaseURL != "" && tok != "" {
|
|
value = "<" + strings.TrimRight(r.BaseURL, "/") + "/unsubscribe/" + tok + ">, " + value
|
|
b.WriteString("List-Unsubscribe-Post: List-Unsubscribe=One-Click\r\n")
|
|
}
|
|
b.WriteString("List-Unsubscribe: " + value + "\r\n")
|
|
return []byte(b.String())
|
|
}
|
|
|
|
// archive stores the original message. Failures are logged, never fatal: the
|
|
// post must be delivered even if the archive is unavailable.
|
|
func (r *Router) archive(l store.List, data []byte) {
|
|
if l.ArchivePolicy == store.ArchiveOff {
|
|
return
|
|
}
|
|
en, err := archive.Parse(data, time.Now())
|
|
if err != nil {
|
|
log.Printf("archive %s: %v", l.Address(), err)
|
|
return
|
|
}
|
|
_, _, err = r.store.ArchiveMessage(l.ID, store.Message{
|
|
MessageID: en.MessageID, InReplyTo: en.InReplyTo, Subject: en.Subject,
|
|
Sender: en.Sender, Date: en.Date, Body: en.Body,
|
|
})
|
|
if err != nil {
|
|
log.Printf("archive %s: %v", l.Address(), err)
|
|
}
|
|
}
|
|
|
|
// Release approves a held message: it leaves the moderation queue and is queued for delivery in
|
|
// one transaction, so simultaneous approvals deliver it once (the others get store.ErrNotFound).
|
|
func (r *Router) Release(heldID int64) error {
|
|
h, err := r.store.GetHeld(heldID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
l, err := r.store.GetList(h.ListID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
res, err := r.prepare(l, h.Data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
items, err := r.outbound(res)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := r.store.EnqueueFromHeld(heldID, res.Data, items); err != nil {
|
|
return err
|
|
}
|
|
if r.OnEnqueue != nil && len(items) > 0 {
|
|
r.OnEnqueue()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// fromHeaderAddress returns the lowercase address of the From header when there is exactly one.
|
|
func fromHeaderAddress(data []byte) (string, bool) {
|
|
m, err := mail.ReadMessage(bytes.NewReader(data))
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
addrs, err := m.Header.AddressList("From")
|
|
if err != nil || len(addrs) != 1 || addrs[0].Address == "" {
|
|
return "", false
|
|
}
|
|
return strings.ToLower(addrs[0].Address), true
|
|
}
|
|
|
|
// displayFrom describes the From header for the held-message reason.
|
|
func displayFrom(data []byte, addr string, ok bool) string {
|
|
if ok {
|
|
return "From: " + addr
|
|
}
|
|
if m, err := mail.ReadMessage(bytes.NewReader(data)); err == nil {
|
|
if raw := strings.TrimSpace(m.Header.Get("From")); raw != "" {
|
|
if len(raw) > 80 {
|
|
raw = raw[:80] + "…"
|
|
}
|
|
return "From: " + raw
|
|
}
|
|
}
|
|
return "no usable From header"
|
|
}
|
|
|
|
// discardReason spots mail that must not be distributed: our own list mail coming back (a
|
|
// loop), and automatic replies (vacation messages and the like, RFC 3834).
|
|
func discardReason(l store.List, data []byte) (string, bool) {
|
|
m, err := mail.ReadMessage(bytes.NewReader(data))
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
own := l.Name + "." + l.Domain
|
|
for _, v := range m.Header["List-Id"] {
|
|
if i, j := strings.LastIndex(v, "<"), strings.LastIndex(v, ">"); i >= 0 && j > i && strings.EqualFold(strings.TrimSpace(v[i+1:j]), own) {
|
|
return "it carries this list's own List-Id: a mail loop", true
|
|
}
|
|
}
|
|
if v := strings.TrimSpace(m.Header.Get("Auto-Submitted")); v != "" {
|
|
if kind := strings.ToLower(strings.Fields(v)[0]); kind != "no" {
|
|
return "Auto-Submitted: " + kind, true
|
|
}
|
|
}
|
|
if strings.EqualFold(strings.TrimSpace(m.Header.Get("Precedence")), "auto_reply") {
|
|
return "Precedence: auto_reply", true
|
|
}
|
|
return "", false
|
|
}
|