From 3bc66d278893447f1b941ca69517670c94088d80 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sun, 11 Mar 2018 16:56:09 -0700 Subject: [PATCH] storage: Store addresses as mail.Address for #69 --- pkg/rest/apiv1_controller.go | 8 ++++---- pkg/rest/apiv1_controller_test.go | 10 +++++----- pkg/rest/testutils_test.go | 9 +++++++-- pkg/server/smtp/handler.go | 4 ++-- pkg/server/smtp/handler_test.go | 9 +++++---- pkg/storage/file/fmessage.go | 18 ++++++++---------- pkg/storage/storage.go | 4 ++-- pkg/storage/testing.go | 8 ++++---- pkg/stringutil/utils.go | 12 ++++++++++++ 9 files changed, 49 insertions(+), 33 deletions(-) diff --git a/pkg/rest/apiv1_controller.go b/pkg/rest/apiv1_controller.go index 6424648..bc03bfd 100644 --- a/pkg/rest/apiv1_controller.go +++ b/pkg/rest/apiv1_controller.go @@ -36,8 +36,8 @@ func MailboxListV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) ( jmessages[i] = &model.JSONMessageHeaderV1{ Mailbox: name, ID: msg.ID(), - From: msg.From(), - To: msg.To(), + From: msg.From().String(), + To: stringutil.StringAddressList(msg.To()), Subject: msg.Subject(), Date: msg.Date(), Size: msg.Size(), @@ -90,8 +90,8 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) ( &model.JSONMessageV1{ Mailbox: name, ID: msg.ID(), - From: msg.From(), - To: msg.To(), + From: msg.From().String(), + To: stringutil.StringAddressList(msg.To()), Subject: msg.Subject(), Date: msg.Date(), Size: msg.Size(), diff --git a/pkg/rest/apiv1_controller_test.go b/pkg/rest/apiv1_controller_test.go index a48fd38..8053a48 100644 --- a/pkg/rest/apiv1_controller_test.go +++ b/pkg/rest/apiv1_controller_test.go @@ -67,16 +67,16 @@ func TestRestMailboxList(t *testing.T) { data1 := &InputMessageData{ Mailbox: "good", ID: "0001", - From: "from1", - To: []string{"to1"}, + From: "", + To: []string{""}, Subject: "subject 1", Date: time.Date(2012, 2, 1, 10, 11, 12, 253, time.FixedZone("PST", -800)), } data2 := &InputMessageData{ Mailbox: "good", ID: "0002", - From: "from2", - To: []string{"to1"}, + From: "", + To: []string{""}, Subject: "subject 2", Date: time.Date(2012, 7, 1, 10, 11, 12, 253, time.FixedZone("PDT", -700)), } @@ -171,7 +171,7 @@ func TestRestMessage(t *testing.T) { data1 := &InputMessageData{ Mailbox: "good", ID: "0001", - From: "from1", + From: "", Subject: "subject 1", Date: time.Date(2012, 2, 1, 10, 11, 12, 253, time.FixedZone("PST", -800)), Header: mail.Header{ diff --git a/pkg/rest/testutils_test.go b/pkg/rest/testutils_test.go index ef294df..4b7d669 100644 --- a/pkg/rest/testutils_test.go +++ b/pkg/rest/testutils_test.go @@ -26,10 +26,15 @@ type InputMessageData struct { } func (d *InputMessageData) MockMessage() *storage.MockMessage { + from, _ := mail.ParseAddress(d.From) + to := make([]*mail.Address, len(d.To)) + for i, a := range d.To { + to[i], _ = mail.ParseAddress(a) + } msg := &storage.MockMessage{} msg.On("ID").Return(d.ID) - msg.On("From").Return(d.From) - msg.On("To").Return(d.To) + msg.On("From").Return(from) + msg.On("To").Return(to) msg.On("Subject").Return(d.Subject) msg.On("Date").Return(d.Date) msg.On("Size").Return(d.Size) diff --git a/pkg/server/smtp/handler.go b/pkg/server/smtp/handler.go index 50bf9ef..b1a6eb3 100644 --- a/pkg/server/smtp/handler.go +++ b/pkg/server/smtp/handler.go @@ -479,8 +479,8 @@ func (ss *Session) deliverMessage(r recipientDetails, msgBuf [][]byte) (ok bool) broadcast := msghub.Message{ Mailbox: name, ID: msg.ID(), - From: msg.From(), - To: msg.To(), + From: msg.From().String(), + To: stringutil.StringAddressList(msg.To()), Subject: msg.Subject(), Date: msg.Date(), Size: msg.Size(), diff --git a/pkg/server/smtp/handler_test.go b/pkg/server/smtp/handler_test.go index d516bf3..ca9ff2e 100644 --- a/pkg/server/smtp/handler_test.go +++ b/pkg/server/smtp/handler_test.go @@ -8,6 +8,7 @@ import ( "log" "net" + "net/mail" "net/textproto" "os" "testing" @@ -145,8 +146,8 @@ func TestMailState(t *testing.T) { msg1 := &storage.MockMessage{} mds.On("NewMessage", "u1").Return(msg1, nil) msg1.On("ID").Return("") - msg1.On("From").Return("") - msg1.On("To").Return(make([]string, 0)) + msg1.On("From").Return(&mail.Address{}) + msg1.On("To").Return(make([]*mail.Address, 0)) msg1.On("Date").Return(time.Time{}) msg1.On("Subject").Return("") msg1.On("Size").Return(0) @@ -257,8 +258,8 @@ func TestDataState(t *testing.T) { msg1 := &storage.MockMessage{} mds.On("NewMessage", "u1").Return(msg1, nil) msg1.On("ID").Return("") - msg1.On("From").Return("") - msg1.On("To").Return(make([]string, 0)) + msg1.On("From").Return(&mail.Address{}) + msg1.On("To").Return(make([]*mail.Address, 0)) msg1.On("Date").Return(time.Time{}) msg1.On("Subject").Return("") msg1.On("Size").Return(0) diff --git a/pkg/storage/file/fmessage.go b/pkg/storage/file/fmessage.go index 26c1612..3f8b0d7 100644 --- a/pkg/storage/file/fmessage.go +++ b/pkg/storage/file/fmessage.go @@ -22,8 +22,8 @@ type Message struct { // Stored in GOB Fid string Fdate time.Time - Ffrom string - Fto []string + Ffrom *mail.Address + Fto []*mail.Address Fsubject string Fsize int64 // These are for creating new messages only @@ -71,12 +71,12 @@ func (m *Message) Date() time.Time { } // From returns the value of the Message From header -func (m *Message) From() string { +func (m *Message) From() *mail.Address { return m.Ffrom } // To returns the value of the Message To header -func (m *Message) To() []string { +func (m *Message) To() []*mail.Address { return m.Fto } @@ -220,19 +220,17 @@ func (m *Message) Close() error { // Only public fields are stored in gob, hence starting with capital F // Parse From address if address, err := mail.ParseAddress(body.GetHeader("From")); err == nil { - m.Ffrom = address.String() + m.Ffrom = address } else { - m.Ffrom = body.GetHeader("From") + m.Ffrom = &mail.Address{Address: body.GetHeader("From")} } m.Fsubject = body.GetHeader("Subject") // Turn the To header into a slice if addresses, err := body.AddressList("To"); err == nil { - for _, a := range addresses { - m.Fto = append(m.Fto, a.String()) - } + m.Fto = addresses } else { - m.Fto = []string{body.GetHeader("To")} + m.Fto = []*mail.Address{{Address: body.GetHeader("To")}} } // Refresh the index before adding our message diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 425a792..c3d2019 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -36,8 +36,8 @@ type Store interface { type StoreMessage interface { Mailbox() string ID() string - From() string - To() []string + From() *mail.Address + To() []*mail.Address Date() time.Time Subject() string RawReader() (reader io.ReadCloser, err error) diff --git a/pkg/storage/testing.go b/pkg/storage/testing.go index 16c40b3..9defa55 100644 --- a/pkg/storage/testing.go +++ b/pkg/storage/testing.go @@ -74,15 +74,15 @@ func (m *MockMessage) ID() string { } // From mock function -func (m *MockMessage) From() string { +func (m *MockMessage) From() *mail.Address { args := m.Called() - return args.String(0) + return args.Get(0).(*mail.Address) } // To mock function -func (m *MockMessage) To() []string { +func (m *MockMessage) To() []*mail.Address { args := m.Called() - return args.Get(0).([]string) + return args.Get(0).([]*mail.Address) } // Date mock function diff --git a/pkg/stringutil/utils.go b/pkg/stringutil/utils.go index 450ae1f..193e552 100644 --- a/pkg/stringutil/utils.go +++ b/pkg/stringutil/utils.go @@ -5,6 +5,7 @@ import ( "crypto/sha1" "fmt" "io" + "net/mail" "strings" ) @@ -224,3 +225,14 @@ LOOP: return buf.String(), domain, nil } + +// StringAddressList converts a list of addresses to a list of strings +func StringAddressList(addrs []*mail.Address) []string { + s := make([]string, len(addrs)) + for i, a := range addrs { + if a != nil { + s[i] = a.String() + } + } + return s +}