Skip to content

Commit

Permalink
server: add Conn.writeError
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Apr 8, 2024
1 parent 53172ad commit bec733d
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,7 @@ func (c *Conn) handleGreet(enhanced bool, arg string) {
sess, err := c.server.Backend.NewSession(c)
if err != nil {
c.helo = ""

if smtpErr, ok := err.(*SMTPError); ok {
c.writeResponse(smtpErr.Code, smtpErr.EnhancedCode, smtpErr.Message)
return
}
c.writeResponse(451, EnhancedCode{4, 0, 0}, err.Error())
c.writeError(451, EnhancedCode{4, 0, 0}, err)
return
}

Expand Down Expand Up @@ -421,11 +416,7 @@ func (c *Conn) handleMail(arg string) {
}

if err := c.Session().Mail(from, opts); err != nil {
if smtpErr, ok := err.(*SMTPError); ok {
c.writeResponse(smtpErr.Code, smtpErr.EnhancedCode, smtpErr.Message)
return
}
c.writeResponse(451, EnhancedCode{4, 0, 0}, err.Error())
c.writeError(451, EnhancedCode{4, 0, 0}, err)
return
}

Expand Down Expand Up @@ -725,11 +716,7 @@ func (c *Conn) handleRcpt(arg string) {
}

if err := c.Session().Rcpt(recipient, opts); err != nil {
if smtpErr, ok := err.(*SMTPError); ok {
c.writeResponse(smtpErr.Code, smtpErr.EnhancedCode, smtpErr.Message)
return
}
c.writeResponse(451, EnhancedCode{4, 0, 0}, err.Error())
c.writeError(451, EnhancedCode{4, 0, 0}, err)
return
}
c.recipients = append(c.recipients, recipient)
Expand Down Expand Up @@ -796,23 +783,15 @@ func (c *Conn) handleAuth(arg string) {

sasl, err := c.auth(mechanism)
if err != nil {
if smtpErr, ok := err.(*SMTPError); ok {
c.writeResponse(smtpErr.Code, smtpErr.EnhancedCode, smtpErr.Message)
} else {
c.writeResponse(454, EnhancedCode{4, 7, 0}, err.Error())
}
c.writeError(454, EnhancedCode{4, 7, 0}, err)
return
}

response := ir
for {
challenge, done, err := sasl.Next(response)
if err != nil {
if smtpErr, ok := err.(*SMTPError); ok {
c.writeResponse(smtpErr.Code, smtpErr.EnhancedCode, smtpErr.Message)
return
}
c.writeResponse(454, EnhancedCode{4, 7, 0}, err.Error())
c.writeError(454, EnhancedCode{4, 7, 0}, err)
return
}

Expand Down Expand Up @@ -1253,6 +1232,14 @@ func (c *Conn) writeResponse(code int, enhCode EnhancedCode, text ...string) {
}
}

func (c *Conn) writeError(code int, enhCode EnhancedCode, err error) {
if smtpErr, ok := err.(*SMTPError); ok {
c.writeResponse(smtpErr.Code, smtpErr.EnhancedCode, smtpErr.Message)
} else {
c.writeResponse(code, enhCode, err.Error())
}
}

// Reads a line of input
func (c *Conn) readLine() (string, error) {
if c.server.ReadTimeout != 0 {
Expand Down

0 comments on commit bec733d

Please sign in to comment.