Skip to content

Commit

Permalink
Allow LMTP servers to listen with TCP
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Ser <[email protected]>
  • Loading branch information
ikedas and emersion committed Nov 9, 2023
1 parent d506ef1 commit 53bb8bb
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
)

var (
errTCPAndLMTP = errors.New("smtp: cannot start LMTP server listening on a TCP socket")
ErrServerClosed = errors.New("smtp: server already closed")
)

Expand All @@ -30,12 +29,13 @@ type Logger interface {

// A SMTP server.
type Server struct {
// The type of network, "tcp" or "unix".
Network string
// TCP or Unix address to listen on.
Addr string
// The server TLS configuration.
TLSConfig *tls.Config
// Enable LMTP mode, as defined in RFC 2033. LMTP mode cannot be used with a
// TCP listener.
// Enable LMTP mode, as defined in RFC 2033.
LMTP bool

Domain string
Expand Down Expand Up @@ -214,15 +214,22 @@ func (s *Server) handleConn(c *Conn) error {
}
}

func (s *Server) network() string {
if s.Network != "" {
return s.Network
}
if s.LMTP {
return "unix"
}
return "tcp"
}

// ListenAndServe listens on the network address s.Addr and then calls Serve
// to handle requests on incoming connections.
//
// If s.Addr is blank and LMTP is disabled, ":smtp" is used.
func (s *Server) ListenAndServe() error {
network := "tcp"
if s.LMTP {
network = "unix"
}
network := s.network()

addr := s.Addr
if !s.LMTP && addr == "" {
Expand All @@ -240,18 +247,16 @@ func (s *Server) ListenAndServe() error {
// ListenAndServeTLS listens on the TCP network address s.Addr and then calls
// Serve to handle requests on incoming TLS connections.
//
// If s.Addr is blank, ":smtps" is used.
// If s.Addr is blank and LMTP is disabled, ":smtps" is used.
func (s *Server) ListenAndServeTLS() error {
if s.LMTP {
return errTCPAndLMTP
}
network := s.network()

addr := s.Addr
if addr == "" {
if !s.LMTP && addr == "" {
addr = ":smtps"
}

l, err := tls.Listen("tcp", addr, s.TLSConfig)
l, err := tls.Listen(network, addr, s.TLSConfig)
if err != nil {
return err
}
Expand Down

0 comments on commit 53bb8bb

Please sign in to comment.