1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 09:37:02 +00:00

storage: Store addresses as mail.Address for #69

This commit is contained in:
James Hillyerd
2018-03-11 16:56:09 -07:00
parent 487e491d6f
commit 3bc66d2788
9 changed files with 49 additions and 33 deletions

View File

@@ -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(),

View File

@@ -67,16 +67,16 @@ func TestRestMailboxList(t *testing.T) {
data1 := &InputMessageData{
Mailbox: "good",
ID: "0001",
From: "from1",
To: []string{"to1"},
From: "<from1@host>",
To: []string{"<to1@host>"},
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: "<from2@host>",
To: []string{"<to1@host>"},
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: "<from1@host>",
Subject: "subject 1",
Date: time.Date(2012, 2, 1, 10, 11, 12, 253, time.FixedZone("PST", -800)),
Header: mail.Header{

View File

@@ -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)

View File

@@ -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(),

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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
}