Start saving SMTP messages in the maildrop.
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 17 Dec 2016 23:22:14 +0000 (18:22 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 17 Dec 2016 23:22:14 +0000 (18:22 -0500)
smtp.go
smtp/server.go

diff --git a/smtp.go b/smtp.go
index fd071fd551b6df4d3c4cb65c80a57f96b95f7d96..24f635ff565c17fd45976668b6343d0a80c84702 100644 (file)
--- a/smtp.go
+++ b/smtp.go
@@ -5,6 +5,9 @@ import (
        "fmt"
        "net"
        "net/mail"
+       "os"
+       "path"
+       "strings"
 
        "src.bluestatic.org/mailpopbox/smtp"
 )
@@ -50,6 +53,9 @@ func (server *smtpServer) TLSConfig() *tls.Config {
 }
 
 func (server *smtpServer) VerifyAddress(addr mail.Address) smtp.ReplyLine {
+       if server.maildropForAddress(addr) == "" {
+               return smtp.ReplyBadMailbox
+       }
        return smtp.ReplyOK
 }
 
@@ -58,6 +64,36 @@ func (server *smtpServer) OnEHLO() *smtp.ReplyLine {
 }
 
 func (server *smtpServer) OnMessageDelivered(en smtp.Envelope) *smtp.ReplyLine {
-       fmt.Printf("MSG: %#v\n%s\n", en, string(en.Data))
+       maildrop := server.maildropForAddress(en.RcptTo[0])
+       if maildrop == "" {
+               // TODO: log error
+               return &smtp.ReplyBadMailbox
+       }
+
+       f, err := os.Create(path.Join(maildrop, en.ID+".msg"))
+       if err != nil {
+               // TODO: log error
+               return &smtp.ReplyBadMailbox
+       }
+
+       smtp.WriteEnvelopeForDelivery(f, en)
+       f.Close()
        return nil
 }
+
+func (server *smtpServer) maildropForAddress(addr mail.Address) string {
+       domainIdx := strings.LastIndex(addr.Address, "@")
+       if domainIdx == -1 {
+               return ""
+       }
+
+       domain := addr.Address[domainIdx+1:]
+
+       for _, s := range server.config.Servers {
+               if domain == s.Domain {
+                       return s.MaildropPath
+               }
+       }
+
+       return ""
+}
index a574ef9906639c6e263c987ccf5d236dc0074001..395a03dd72558799dcc4f2e7f352ab9aeb24b4d1 100644 (file)
@@ -2,6 +2,8 @@ package smtp
 
 import (
        "crypto/tls"
+       "fmt"
+       "io"
        "net"
        "net/mail"
        "time"
@@ -29,6 +31,12 @@ type Envelope struct {
        ID         string
 }
 
+func WriteEnvelopeForDelivery(w io.Writer, e Envelope) {
+       fmt.Fprintf(w, "Delivered-To: <%s>\r\n", e.RcptTo[0].Address)
+       fmt.Fprintf(w, "Return-Path: <%s>\r\n", e.MailFrom.Address)
+       w.Write(e.Data)
+}
+
 type Server interface {
        Name() string
        TLSConfig() *tls.Config