From f38137f0e51c4dbad83631ca7a40f59af67cabcc Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 3 Sep 2018 00:16:22 -0400 Subject: [PATCH] Add smtp.DomainForAddress. --- smtp.go | 9 +-------- smtp/server.go | 9 +++++++++ smtp/server_test.go | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 smtp/server_test.go diff --git a/smtp.go b/smtp.go index ef71dab..81aae44 100644 --- a/smtp.go +++ b/smtp.go @@ -7,7 +7,6 @@ import ( "net/mail" "os" "path" - "strings" "github.com/uber-go/zap" @@ -119,13 +118,7 @@ func (server *smtpServer) OnMessageDelivered(en smtp.Envelope) *smtp.ReplyLine { } func (server *smtpServer) maildropForAddress(addr mail.Address) string { - domainIdx := strings.LastIndex(addr.Address, "@") - if domainIdx == -1 { - return "" - } - - domain := addr.Address[domainIdx+1:] - + domain := smtp.DomainForAddress(addr) for _, s := range server.config.Servers { if domain == s.Domain { return s.MaildropPath diff --git a/smtp/server.go b/smtp/server.go index d9432aa..dbd0dc3 100644 --- a/smtp/server.go +++ b/smtp/server.go @@ -6,6 +6,7 @@ import ( "io" "net" "net/mail" + "strings" "time" ) @@ -25,6 +26,14 @@ var ( ReplyBadMailbox = ReplyLine{550, "mailbox unavailable"} ) +func DomainForAddress(addr mail.Address) string { + domainIdx := strings.LastIndex(addr.Address, "@") + if domainIdx == -1 { + return "" + } + return addr.Address[domainIdx+1:] +} + type Envelope struct { RemoteAddr net.Addr EHLO string diff --git a/smtp/server_test.go b/smtp/server_test.go new file mode 100644 index 0000000..09cb306 --- /dev/null +++ b/smtp/server_test.go @@ -0,0 +1,22 @@ +package smtp + +import ( + "net/mail" + "testing" +) + +func TestDomainForAddress(t *testing.T) { + cases := []struct{ + address, domain string + }{ + {"foo@bar.com", "bar.com"}, + {"abc", ""}, + {"abc@one.two.three.four.net", "one.two.three.four.net"}, + } + for i, c := range cases { + actual := DomainForAddress(mail.Address{Address: c.address}) + if actual != c.domain { + t.Errorf("case %d, got %q, expected %q", i, actual, c.domain) + } + } +} -- 2.43.5