Files
LambdaMail/services/protocols/internal/presentation/smtp/backend.go
T
Lucas Rayan 2f7389841d #3: close 3 known F1 gaps - folder/mailbox counters, quota-at-RCPT, alias fan-out
Per feature/3-f0-f1-validation's own gap analysis (commit 097fa37):

- InboundMessageRepository.Persist now updates folders.unread_count,
  folders.total_count, and mailboxes.used_bytes in the same transaction
  as the message insert (PLAN.md section 6.1 "++counters" was a no-op
  before this).
- MailboxRecord carries QuotaBytes/UsedBytes; ResolveRecipient rejects
  RCPT with 452 4.2.2 when a target mailbox has already reached its
  quota (used_bytes >= quota_bytes), before DATA - never silently
  accepting mail into a full mailbox.
- MailboxRepository.ResolveDeliveryTargets tries a direct mailbox match
  first, then falls back to the aliases table (exact source_address
  match takes precedence over a domain catch-all). One RCPT TO can now
  fan out to multiple mailboxes; alias destinations that don't resolve
  to an internal active mailbox are silently skipped, since outbound/
  external forwarding doesn't exist yet. postmaster@/abuse@/dmarc@-style
  aliases now actually deliver.

The E2E test's counters_updated subtest flips from a documented KNOWN
GAP (t.Skipf) to a hard assertion now that it passes, and gains two new
subtests (quota_rejected_when_mailbox_full, alias_resolves_to_destination_mailbox)
covering the other two fixes end-to-end. New unit and Postgres
integration test coverage added for all three fixes at every layer.

Verified: full suite green on Windows (unit + E2E via embedded
Postgres, no Docker needed) and via WSL Docker against the real
docker-compose Postgres (all infrastructure/postgres and diskstorage
integration tests, not just E2E). lang-lint clean.
2026-07-23 11:36:03 -03:00

119 lines
3.1 KiB
Go

// Package smtppresentation adapts github.com/emersion/go-smtp's Backend/
// Session interfaces to ProcessInboundEmailUseCase (Clean Architecture
// layer 4 - PLAN.md section 3).
package smtppresentation
import (
"context"
"errors"
"io"
gosmtp "github.com/emersion/go-smtp"
"lambdamail/protocols/internal/application/port"
"lambdamail/protocols/internal/application/usecase"
)
// Backend is the go-smtp entry point: one Session per connection.
type Backend struct {
useCase *usecase.ProcessInboundEmailUseCase
}
func NewBackend(uc *usecase.ProcessInboundEmailUseCase) *Backend {
return &Backend{useCase: uc}
}
func (b *Backend) NewSession(_ *gosmtp.Conn) (gosmtp.Session, error) {
return &session{useCase: b.useCase}, nil
}
// session holds one SMTP transaction's accumulated state (RFC 5321 section
// 4.1.1: MAIL, then one or more RCPT, then DATA, then reset).
type session struct {
useCase *usecase.ProcessInboundEmailUseCase
from string
recipients []port.MailboxRecord
recipientAddresses []string
}
func (s *session) Reset() {
s.from = ""
s.recipients = nil
s.recipientAddresses = nil
}
func (s *session) Logout() error {
return nil
}
func (s *session) Mail(from string, _ *gosmtp.MailOptions) error {
s.from = from
return nil
}
func (s *session) Rcpt(to string, _ *gosmtp.RcptOptions) error {
for _, existing := range s.recipientAddresses {
if existing == to {
// Duplicate RCPT TO for an address already accepted in this
// transaction (some MTAs retry): accept silently without
// persisting a second delivery.
return nil
}
}
targets, err := s.useCase.ResolveRecipient(context.Background(), to)
if err != nil {
switch {
case errors.Is(err, usecase.ErrRecipientNotFound):
return &gosmtp.SMTPError{
Code: 550,
EnhancedCode: gosmtp.EnhancedCode{5, 1, 1},
Message: "User unknown",
}
case errors.Is(err, usecase.ErrMailboxQuotaExceeded):
return &gosmtp.SMTPError{
Code: 452,
EnhancedCode: gosmtp.EnhancedCode{4, 2, 2},
Message: "Mailbox full",
}
default:
return &gosmtp.SMTPError{
Code: 451,
EnhancedCode: gosmtp.EnhancedCode{4, 3, 0},
Message: "Temporary error resolving recipient",
}
}
}
// One RCPT TO can resolve to multiple mailboxes (alias fan-out) - each
// gets its own delivery, all recorded under the same envelope address.
for _, rec := range targets {
s.recipients = append(s.recipients, rec)
s.recipientAddresses = append(s.recipientAddresses, to)
}
return nil
}
func (s *session) Data(r io.Reader) error {
if len(s.recipients) == 0 {
return &gosmtp.SMTPError{
Code: 503,
EnhancedCode: gosmtp.EnhancedCode{5, 5, 1},
Message: "RCPT TO required before DATA",
}
}
err := s.useCase.Handle(context.Background(), usecase.ProcessInboundEmailInput{
Sender: s.from,
Recipients: s.recipients,
RecipientAddresses: s.recipientAddresses,
Body: r,
})
if err != nil {
return &gosmtp.SMTPError{
Code: 451,
EnhancedCode: gosmtp.EnhancedCode{4, 3, 0},
Message: "Temporary error storing message",
}
}
return nil
}