From 25e151f9580add2ff8e77facbda85dd70de29709 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 13 Dec 2016 14:26:02 -0500 Subject: [PATCH] Go back to using pop3.Message rather than IDs for most pop3.Mailbox methods. But also add a Mailbox.GetMessage method to make it easier to convert. --- pop3.go | 20 ++++++++++---------- pop3/conn.go | 33 ++++++++++++++++++++++----------- pop3/server.go | 5 +++-- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/pop3.go b/pop3.go index ad4d1a4..6d8dfd2 100644 --- a/pop3.go +++ b/pop3.go @@ -123,20 +123,20 @@ func (mb *mailbox) ListMessages() ([]pop3.Message, error) { return msgs, nil } -func (mb *mailbox) Retrieve(idx int) (io.ReadCloser, error) { - if idx > len(mb.messages) { - return nil, errors.New("no such message") +func (mb *mailbox) GetMessage(id int) pop3.Message { + if id > len(mb.messages) { + return nil } - filename := mb.messages[idx-1].filename + return &mb.messages[id-1] +} + +func (mb *mailbox) Retrieve(msg pop3.Message) (io.ReadCloser, error) { + filename := msg.(*message).filename return os.Open(filename) } -func (mb *mailbox) Delete(idx int) error { - message := &mb.messages[idx-1] - if message.deleted { - return errors.New("already deleted") - } - message.deleted = true +func (mb *mailbox) Delete(msg pop3.Message) error { + msg.(*message).deleted = true return nil } diff --git a/pop3/conn.go b/pop3/conn.go index 5341bcb..91a2543 100644 --- a/pop3/conn.go +++ b/pop3/conn.go @@ -196,12 +196,12 @@ func (conn *connection) doRETR() { return } - idx, ok := conn.intParam() - if !ok { + msg := conn.getRequestedMessage() + if msg == nil { return } - rc, err := conn.mb.Retrieve(idx) + rc, err := conn.mb.Retrieve(msg) if err != nil { conn.err(err.Error()) return @@ -218,12 +218,12 @@ func (conn *connection) doDELE() { return } - idx, ok := conn.intParam() - if !ok { + msg := conn.getRequestedMessage() + if msg == nil { return } - if err := conn.mb.Delete(idx); err != nil { + if err := conn.mb.Delete(msg); err != nil { conn.err(err.Error()) } else { conn.ok("") @@ -239,12 +239,23 @@ func (conn *connection) doRSET() { conn.ok("") } -func (conn *connection) intParam() (int, bool) { +func (conn *connection) getRequestedMessage() Message { var cmd string - var param int - if _, err := fmt.Sscanf(conn.line, "%s %d", &cmd, ¶m); err != nil { + var idx int + if _, err := fmt.Sscanf(conn.line, "%s %d", &cmd, &idx); err != nil { conn.err(errSyntax) - return 0, false + return nil } - return param, true + + if idx < 1 { + conn.err("invalid message-number") + return nil + } + + msg := conn.mb.GetMessage(idx) + if msg == nil { + conn.err("no such message") + return nil + } + return msg } diff --git a/pop3/server.go b/pop3/server.go index f0377b5..6e5dc36 100644 --- a/pop3/server.go +++ b/pop3/server.go @@ -12,8 +12,9 @@ type Message interface { type Mailbox interface { ListMessages() ([]Message, error) - Retrieve(int) (io.ReadCloser, error) - Delete(int) error + GetMessage(int) Message + Retrieve(Message) (io.ReadCloser, error) + Delete(Message) error Close() error Reset() } -- 2.43.5