- Route on a publishers list lets through a member flagged publisher, after the unchanged discard and loop checks, and applies the usual From header check - anyone else gets the new ErrNotPublisher: never held, archived or queued - ingest answers ErrNotPublisher with 550 5.7.1 only the publishers of this list may post - tests: publisher ok, plain member and stranger refused, disabled publisher posts, identity mismatch held and released, discard rules first, other policies unchanged
112 lines
3.6 KiB
Go
112 lines
3.6 KiB
Go
// Package ingest receives mail from the MTA over LMTP.
|
|
package ingest
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/emersion/go-smtp"
|
|
|
|
"codeberg.org/squidd/parsimony/internal/router"
|
|
)
|
|
|
|
const maxMessageBytes = 25 << 20
|
|
|
|
// NewServer builds an LMTP server that routes incoming mail into the outbound queue.
|
|
// A message is accepted only once it is durably queued.
|
|
// Set Network/Addr on the result (or call Serve) to start it.
|
|
func NewServer(r *router.Router) *smtp.Server {
|
|
srv := smtp.NewServer(&backend{router: r})
|
|
srv.LMTP = true
|
|
srv.Domain = "localhost"
|
|
srv.MaxMessageBytes = maxMessageBytes
|
|
srv.ReadTimeout = 60 * time.Second
|
|
srv.WriteTimeout = 60 * time.Second
|
|
return srv
|
|
}
|
|
|
|
type backend struct {
|
|
router *router.Router
|
|
}
|
|
|
|
func (b *backend) NewSession(*smtp.Conn) (smtp.Session, error) {
|
|
return &session{b: b}, nil
|
|
}
|
|
|
|
type session struct {
|
|
b *backend
|
|
from string
|
|
rcpts []string
|
|
}
|
|
|
|
func (s *session) Reset() { s.from, s.rcpts = "", nil }
|
|
|
|
func (s *session) Logout() error { return nil }
|
|
|
|
func (s *session) Mail(from string, _ *smtp.MailOptions) error {
|
|
s.from = from
|
|
return nil
|
|
}
|
|
|
|
func (s *session) Rcpt(to string, _ *smtp.RcptOptions) error {
|
|
s.rcpts = append(s.rcpts, to)
|
|
return nil
|
|
}
|
|
|
|
// Data is unused: LMTPData handles delivery with per-recipient status.
|
|
func (s *session) Data(io.Reader) error {
|
|
return &smtp.SMTPError{Code: 503, EnhancedCode: smtp.EnhancedCode{5, 5, 1}, Message: "LMTP only"}
|
|
}
|
|
|
|
func (s *session) LMTPData(r io.Reader, status smtp.StatusCollector) error {
|
|
data, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, rcpt := range s.rcpts {
|
|
status.SetStatus(rcpt, s.deliver(rcpt, data))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *session) deliver(rcpt string, data []byte) error {
|
|
rt := s.b.router
|
|
if handled, err := rt.Command(s.from, rcpt, data); handled {
|
|
return s.status(rcpt, err)
|
|
}
|
|
res, err := rt.Route(s.from, rcpt, data)
|
|
if err != nil {
|
|
return s.status(rcpt, err)
|
|
}
|
|
return s.status(rcpt, rt.Deliver(res))
|
|
}
|
|
|
|
// status turns a router result into an LMTP reply.
|
|
func (s *session) status(rcpt string, err error) error {
|
|
switch {
|
|
case err == nil, errors.Is(err, router.ErrHeld), errors.Is(err, router.ErrDiscarded), errors.Is(err, router.ErrDuplicate):
|
|
return nil
|
|
case errors.Is(err, router.ErrUnknownList):
|
|
return &smtp.SMTPError{Code: 550, EnhancedCode: smtp.EnhancedCode{5, 1, 1}, Message: "no such list"}
|
|
case errors.Is(err, router.ErrHeldFull):
|
|
return &smtp.SMTPError{Code: 452, EnhancedCode: smtp.EnhancedCode{4, 2, 2}, Message: "the moderation queue of this list is full, try again later"}
|
|
case errors.Is(err, router.ErrHeldTooLarge):
|
|
return &smtp.SMTPError{Code: 552, EnhancedCode: smtp.EnhancedCode{5, 3, 4}, Message: "message too large for this moderated list"}
|
|
case errors.Is(err, router.ErrNotMember):
|
|
return &smtp.SMTPError{Code: 550, EnhancedCode: smtp.EnhancedCode{5, 7, 1}, Message: "only list members may post"}
|
|
case errors.Is(err, router.ErrNotPublisher):
|
|
return &smtp.SMTPError{Code: 550, EnhancedCode: smtp.EnhancedCode{5, 7, 1}, Message: "only the publishers of this list may post"}
|
|
case errors.Is(err, router.ErrBadToken):
|
|
return &smtp.SMTPError{Code: 550, EnhancedCode: smtp.EnhancedCode{5, 7, 1}, Message: "invalid or expired token"}
|
|
case errors.Is(err, router.ErrJoinClosed):
|
|
return &smtp.SMTPError{Code: 550, EnhancedCode: smtp.EnhancedCode{5, 7, 1}, Message: "this list does not accept subscriptions"}
|
|
}
|
|
log.Printf("deliver %s: %v", rcpt, err)
|
|
return tempFail()
|
|
}
|
|
|
|
func tempFail() error {
|
|
return &smtp.SMTPError{Code: 451, EnhancedCode: smtp.EnhancedCode{4, 3, 0}, Message: "temporary failure, try again later"}
|
|
}
|