1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 17:47:03 +00:00

smtp: Move delivery into message.Manager for #69

This commit is contained in:
James Hillyerd
2018-03-17 16:54:29 -07:00
parent a22412f65e
commit f953bcf4bb
5 changed files with 102 additions and 93 deletions

View File

@@ -6,18 +6,13 @@ import (
"fmt"
"io"
"net"
"net/mail"
"regexp"
"strconv"
"strings"
"time"
"github.com/jhillyerd/enmime"
"github.com/jhillyerd/inbucket/pkg/log"
"github.com/jhillyerd/inbucket/pkg/message"
"github.com/jhillyerd/inbucket/pkg/msghub"
"github.com/jhillyerd/inbucket/pkg/policy"
"github.com/jhillyerd/inbucket/pkg/stringutil"
)
// State tracks the current mode of our SMTP state machine
@@ -370,9 +365,18 @@ func (ss *Session) dataHandler() {
}
if bytes.Equal(lineBuf, []byte(".\r\n")) || bytes.Equal(lineBuf, []byte(".\n")) {
// Mail data complete.
tstamp := time.Now().Format(timeStampFormat)
for _, recip := range ss.recipients {
if recip.ShouldStore() {
if ok := ss.deliverMessage(recip, msgBuf.Bytes()); !ok {
// Generate Received header.
prefix := fmt.Sprintf("Received: from %s ([%s]) by %s\r\n for <%s>; %s\r\n",
ss.remoteDomain, ss.remoteHost, ss.server.domain, recip.Address.Address,
tstamp)
// Deliver message.
_, err := ss.server.manager.Deliver(
recip, ss.from, ss.recipients, prefix, msgBuf.Bytes())
if err != nil {
ss.logError("delivery for %v: %v", recip.LocalPart, err)
ss.send(fmt.Sprintf("451 Failed to store message for %v", recip.LocalPart))
ss.reset()
return
@@ -385,7 +389,7 @@ func (ss *Session) dataHandler() {
ss.reset()
return
}
// RFC says remove leading periods from input.
// RFC: remove leading periods from DATA.
if len(lineBuf) > 0 && lineBuf[0] == '.' {
lineBuf = lineBuf[1:]
}
@@ -399,59 +403,6 @@ func (ss *Session) dataHandler() {
}
}
// deliverMessage creates and populates a new Message for the specified recipient
func (ss *Session) deliverMessage(recip *policy.Recipient, content []byte) (ok bool) {
// TODO replace with something that only reads header?
env, err := enmime.ReadEnvelope(bytes.NewReader(content))
if err != nil {
ss.logError("Failed to parse message for %q: %v", recip.LocalPart, err)
return false
}
from, err := env.AddressList("From")
if err != nil {
from = []*mail.Address{{Address: ss.from}}
}
to, err := env.AddressList("To")
if err != nil {
to = make([]*mail.Address, len(ss.recipients))
for i, torecip := range ss.recipients {
to[i] = &torecip.Address
}
}
// Generate Received header.
stamp := time.Now().Format(timeStampFormat)
recd := strings.NewReader(fmt.Sprintf("Received: from %s ([%s]) by %s\r\n for <%s>; %s\r\n",
ss.remoteDomain, ss.remoteHost, ss.server.domain, recip.Address, stamp))
delivery := &message.Delivery{
Meta: message.Metadata{
Mailbox: recip.Mailbox,
From: from[0],
To: to,
Date: time.Now(),
Subject: env.GetHeader("Subject"),
},
Reader: io.MultiReader(recd, bytes.NewReader(content)),
}
id, err := ss.server.dataStore.AddMessage(delivery)
if err != nil {
ss.logError("Failed to store message for %q: %s", recip.LocalPart, err)
return false
}
// Broadcast message information.
// TODO this belongs in message pkg.
broadcast := msghub.Message{
Mailbox: recip.Mailbox,
ID: id,
From: delivery.From().String(),
To: stringutil.StringAddressList(delivery.To()),
Subject: delivery.Subject(),
Date: delivery.Date(),
Size: delivery.Size(),
}
ss.server.msgHub.Dispatch(broadcast)
return true
}
func (ss *Session) enterState(state State) {
ss.state = state
ss.logTrace("Entering state %v", state)

View File

@@ -2,7 +2,6 @@ package smtp
import (
"bytes"
"context"
"fmt"
"io"
@@ -14,7 +13,7 @@ import (
"time"
"github.com/jhillyerd/inbucket/pkg/config"
"github.com/jhillyerd/inbucket/pkg/msghub"
"github.com/jhillyerd/inbucket/pkg/message"
"github.com/jhillyerd/inbucket/pkg/policy"
"github.com/jhillyerd/inbucket/pkg/storage"
"github.com/jhillyerd/inbucket/pkg/test"
@@ -379,13 +378,12 @@ func setupSMTPServer(ds storage.Store) (s *Server, buf *bytes.Buffer, teardown f
// Create a server, don't start it
shutdownChan := make(chan bool)
ctx, cancel := context.WithCancel(context.Background())
teardown = func() {
close(shutdownChan)
cancel()
}
apolicy := &policy.Addressing{Config: cfg}
s = NewServer(cfg, shutdownChan, ds, apolicy, msghub.New(ctx, 100))
manager := &message.StoreManager{Store: ds}
s = NewServer(cfg, shutdownChan, manager, apolicy)
return s, buf, teardown
}

View File

@@ -12,9 +12,8 @@ import (
"github.com/jhillyerd/inbucket/pkg/config"
"github.com/jhillyerd/inbucket/pkg/log"
"github.com/jhillyerd/inbucket/pkg/msghub"
"github.com/jhillyerd/inbucket/pkg/message"
"github.com/jhillyerd/inbucket/pkg/policy"
"github.com/jhillyerd/inbucket/pkg/storage"
)
func init() {
@@ -49,10 +48,9 @@ type Server struct {
storeMessages bool
// Dependencies
dataStore storage.Store // Mailbox/message store
apolicy *policy.Addressing // Address policy
globalShutdown chan bool // Shuts down Inbucket
msgHub *msghub.Hub // Pub/sub for message info
apolicy *policy.Addressing // Address policy.
globalShutdown chan bool // Shuts down Inbucket.
manager message.Manager // Used to deliver messages.
// State
listener net.Listener // Incoming network connections
@@ -84,9 +82,9 @@ var (
func NewServer(
cfg config.SMTPConfig,
globalShutdown chan bool,
ds storage.Store,
manager message.Manager,
apolicy *policy.Addressing,
msgHub *msghub.Hub) *Server {
) *Server {
return &Server{
host: fmt.Sprintf("%v:%v", cfg.IP4address, cfg.IP4port),
domain: cfg.Domain,
@@ -96,9 +94,8 @@ func NewServer(
maxMessageBytes: cfg.MaxMessageBytes,
storeMessages: cfg.StoreMessages,
globalShutdown: globalShutdown,
dataStore: ds,
manager: manager,
apolicy: apolicy,
msgHub: msgHub,
waitgroup: new(sync.WaitGroup),
}
}