From dfdadc5da235b0270152c20351c2cacf9491222e Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 4 Oct 2024 22:38:18 +0200 Subject: [PATCH] Refactor client.go: Move functions to new locations Reordered several functions within client.go for better code organization and readability. This change involves moving `setDefaultHelo`, `checkConn`, `serverFallbackAddr`, and `tls` functions to new locations without altering their implementations. --- client.go | 150 +++++++++++++++++++++++++++--------------------------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/client.go b/client.go index 5332cda..48d6731 100644 --- a/client.go +++ b/client.go @@ -648,16 +648,6 @@ func (c *Client) SetSMTPAuthCustom(smtpAuth smtp.Auth) { c.smtpAuthType = SMTPAuthCustom } -// setDefaultHelo retrieves the current hostname and sets it as HELO/EHLO hostname -func (c *Client) setDefaultHelo() error { - hostname, err := os.Hostname() - if err != nil { - return fmt.Errorf("failed to read local hostname: %w", err) - } - c.helo = hostname - return nil -} - // DialWithContext establishes a connection to the SMTP server with a given context.Context func (c *Client) DialWithContext(dialCtx context.Context) error { c.mutex.Lock() @@ -761,71 +751,6 @@ func (c *Client) DialAndSendWithContext(ctx context.Context, messages ...*Msg) e return nil } -// checkConn makes sure that a required server connection is available and extends the -// connection deadline -func (c *Client) checkConn() error { - if !c.smtpClient.HasConnection() { - return ErrNoActiveConnection - } - - if !c.noNoop { - if err := c.smtpClient.Noop(); err != nil { - return ErrNoActiveConnection - } - } - - if err := c.smtpClient.UpdateDeadline(c.connTimeout); err != nil { - return ErrDeadlineExtendFailed - } - return nil -} - -// serverFallbackAddr returns the currently set combination of hostname -// and fallback port. -func (c *Client) serverFallbackAddr() string { - return fmt.Sprintf("%s:%d", c.host, c.fallbackPort) -} - -// tls tries to make sure that the STARTTLS requirements are satisfied -func (c *Client) tls() error { - if !c.smtpClient.HasConnection() { - return ErrNoActiveConnection - } - if !c.useSSL && c.tlspolicy != NoTLS { - hasStartTLS := false - extension, _ := c.smtpClient.Extension("STARTTLS") - if c.tlspolicy == TLSMandatory { - hasStartTLS = true - if !extension { - return fmt.Errorf("STARTTLS mode set to: %q, but target host does not support STARTTLS", - c.tlspolicy) - } - } - if c.tlspolicy == TLSOpportunistic { - if extension { - hasStartTLS = true - } - } - if hasStartTLS { - if err := c.smtpClient.StartTLS(c.tlsconfig); err != nil { - return err - } - } - tlsConnState, err := c.smtpClient.GetTLSConnectionState() - if err != nil { - switch { - case errors.Is(err, smtp.ErrNonTLSConnection): - c.isEncrypted = false - return nil - default: - return fmt.Errorf("failed to get TLS connection state: %w", err) - } - } - c.isEncrypted = tlsConnState.HandshakeComplete - } - return nil -} - // auth will try to perform SMTP AUTH if requested func (c *Client) auth() error { if err := c.checkConn(); err != nil { @@ -998,3 +923,78 @@ func (c *Client) sendSingleMsg(message *Msg) error { } return nil } + +// checkConn makes sure that a required server connection is available and extends the +// connection deadline +func (c *Client) checkConn() error { + if !c.smtpClient.HasConnection() { + return ErrNoActiveConnection + } + + if !c.noNoop { + if err := c.smtpClient.Noop(); err != nil { + return ErrNoActiveConnection + } + } + + if err := c.smtpClient.UpdateDeadline(c.connTimeout); err != nil { + return ErrDeadlineExtendFailed + } + return nil +} + +// serverFallbackAddr returns the currently set combination of hostname +// and fallback port. +func (c *Client) serverFallbackAddr() string { + return fmt.Sprintf("%s:%d", c.host, c.fallbackPort) +} + +// setDefaultHelo retrieves the current hostname and sets it as HELO/EHLO hostname +func (c *Client) setDefaultHelo() error { + hostname, err := os.Hostname() + if err != nil { + return fmt.Errorf("failed to read local hostname: %w", err) + } + c.helo = hostname + return nil +} + +// tls tries to make sure that the STARTTLS requirements are satisfied +func (c *Client) tls() error { + if !c.smtpClient.HasConnection() { + return ErrNoActiveConnection + } + if !c.useSSL && c.tlspolicy != NoTLS { + hasStartTLS := false + extension, _ := c.smtpClient.Extension("STARTTLS") + if c.tlspolicy == TLSMandatory { + hasStartTLS = true + if !extension { + return fmt.Errorf("STARTTLS mode set to: %q, but target host does not support STARTTLS", + c.tlspolicy) + } + } + if c.tlspolicy == TLSOpportunistic { + if extension { + hasStartTLS = true + } + } + if hasStartTLS { + if err := c.smtpClient.StartTLS(c.tlsconfig); err != nil { + return err + } + } + tlsConnState, err := c.smtpClient.GetTLSConnectionState() + if err != nil { + switch { + case errors.Is(err, smtp.ErrNonTLSConnection): + c.isEncrypted = false + return nil + default: + return fmt.Errorf("failed to get TLS connection state: %w", err) + } + } + c.isEncrypted = tlsConnState.HandshakeComplete + } + return nil +}