Skip to content

Commit

Permalink
client: add Client.readResponse helper
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Apr 24, 2024
1 parent 52a8bf5 commit 170fe35
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,9 @@ func (c *Client) greet() error {
c.conn.SetDeadline(time.Now().Add(c.CommandTimeout))
defer c.conn.SetDeadline(time.Time{})

_, _, err := c.text.ReadResponse(220)
_, _, err := c.readResponse(220)
if err != nil {
c.text.Close()
if protoErr, ok := err.(*textproto.Error); ok {
return toSMTPErr(protoErr)
}
return err
}

Expand Down Expand Up @@ -237,6 +234,14 @@ func (c *Client) Hello(localName string) error {
return c.hello()
}

func (c *Client) readResponse(expectCode int) (int, string, error) {
code, msg, err := c.text.ReadResponse(expectCode)
if protoErr, ok := err.(*textproto.Error); ok {
err = toSMTPErr(protoErr)
}
return code, msg, err
}

// cmd is a convenience function that sends a command and returns the response
// textproto.Error returned by c.text.ReadResponse is converted into SMTPError.
func (c *Client) cmd(expectCode int, format string, args ...interface{}) (int, string, error) {
Expand All @@ -249,15 +254,8 @@ func (c *Client) cmd(expectCode int, format string, args ...interface{}) (int, s
}
c.text.StartResponse(id)
defer c.text.EndResponse(id)
code, msg, err := c.text.ReadResponse(expectCode)
if err != nil {
if protoErr, ok := err.(*textproto.Error); ok {
smtpErr := toSMTPErr(protoErr)
return code, smtpErr.Message, smtpErr
}
return code, msg, err
}
return code, msg, nil

return c.readResponse(expectCode)
}

// helo sends the HELO greeting to the server. It should be used only when the
Expand Down Expand Up @@ -555,10 +553,10 @@ func (d *dataCloser) Close() error {
if d.c.lmtp {
for expectedResponses > 0 {
rcpt := d.c.rcpts[len(d.c.rcpts)-expectedResponses]
if _, _, err := d.c.text.ReadResponse(250); err != nil {
if protoErr, ok := err.(*textproto.Error); ok {
if _, _, err := d.c.readResponse(250); err != nil {
if smtpErr, ok := err.(*SMTPError); ok {
if d.statusCb != nil {
d.statusCb(rcpt, toSMTPErr(protoErr))
d.statusCb(rcpt, smtpErr)
}
} else {
return err
Expand All @@ -569,11 +567,8 @@ func (d *dataCloser) Close() error {
expectedResponses--
}
} else {
_, _, err := d.c.text.ReadResponse(250)
_, _, err := d.c.readResponse(250)
if err != nil {
if protoErr, ok := err.(*textproto.Error); ok {
return toSMTPErr(protoErr)
}
return err
}
}
Expand Down

0 comments on commit 170fe35

Please sign in to comment.