Add smtp.DomainForAddress.
authorRobert Sesek <rsesek@bluestatic.org>
Mon, 3 Sep 2018 04:16:22 +0000 (00:16 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Mon, 3 Sep 2018 04:16:22 +0000 (00:16 -0400)
smtp.go
smtp/server.go
smtp/server_test.go [new file with mode: 0644]

diff --git a/smtp.go b/smtp.go
index ef71dabb17459416d6394c8bb85f72993a8871f0..81aae4404124dde452faeec973c197121ff28db4 100644 (file)
--- 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
index d9432aacdabd63722593311c84087b6387627c66..dbd0dc3d76dca41d89b8b35604afa5d0b96a599f 100644 (file)
@@ -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 (file)
index 0000000..09cb306
--- /dev/null
@@ -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)
+               }
+       }
+}