Adds a real :25 SMTP listener to the Go protocols service, giving F1's "Nucleo SMTP" milestone its first working sub-project: application, infrastructure, and presentation layers on top of F0's domain-only Clean Architecture. - ProcessInboundEmailUseCase resolves recipients against Postgres and persists accepted mail (blob dedup by content hash, transactional UID allocation + outbox event, never sending 250 OK before durable commit per PLAN.md section 6.1). - Postgres repositories via pgx/v5 (replacing lib/pq entirely). - Local-disk blob storage with content-hash dedup, rename-before-commit ordering so a DB row can never reference a file that isn't actually on disk. - A local-file TLS cert provider for STARTTLS, with an ephemeral self-signed fallback so a missing/misconfigured cert degrades gracefully instead of crash-looping the service (PLAN.md section 8.4). - A github.com/emersion/go-smtp Backend/Session adapting real SMTP traffic to the use case: unknown recipients get 550 5.1.1 before DATA, duplicate RCPT TO for the same address is deduplicated, TLS is hardened to the project's required PFS-only cipher/curve policy. - A dev-seed script and Makefile target for local testing. Explicitly out of scope (separate follow-up sub-projects): alias resolution, SPF/DKIM/DMARC/ARC verification, Rspamd/ClamAV, Submission ports with SASL auth, DKIM signing, outbound delivery, and the real Traefik acme.json cert watcher. Verified live end-to-end via WSL Docker: the full compose stack (db, redis, migrate, auth, storage, protocols, web) reaches healthy, and a real swaks SMTP session is accepted and durably persisted with correct sequential UIDs, while an unknown recipient is correctly rejected with 550 5.1.1 and zero rows persisted. Built via 10 sequential tasks under subagent-driven development, each independently reviewed, plus a final whole-branch review that caught and fixed a TLS-crash bug, a blob commit-ordering bug, missing TLS hardening, and missing CI integration coverage before merge. Known follow-up, not blocking: email_messages' partition only covers July 2026 and needs a partition-management job before 2026-08-01.
107 lines
2.7 KiB
Go
107 lines
2.7 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
|
|
}
|
|
}
|
|
|
|
rec, err := s.useCase.ResolveRecipient(context.Background(), to)
|
|
if err != nil {
|
|
if errors.Is(err, usecase.ErrRecipientNotFound) {
|
|
return &gosmtp.SMTPError{
|
|
Code: 550,
|
|
EnhancedCode: gosmtp.EnhancedCode{5, 1, 1},
|
|
Message: "User unknown",
|
|
}
|
|
}
|
|
return &gosmtp.SMTPError{
|
|
Code: 451,
|
|
EnhancedCode: gosmtp.EnhancedCode{4, 3, 0},
|
|
Message: "Temporary error resolving recipient",
|
|
}
|
|
}
|
|
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
|
|
}
|