From 2c8167557861692d84f8b3b8c19e294ed55fe85d Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Mon, 5 Mar 2018 23:37:54 +0000 Subject: [PATCH] Add the beginnings of SMTP session management --- internal/smtp/server.go | 20 ++++++++++++++++++-- internal/smtp/session.go | 32 ++++++++++++++++++++++++++++++++ internal/store/store.go | 4 ++-- 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 internal/smtp/session.go diff --git a/internal/smtp/server.go b/internal/smtp/server.go index fdb419a..539d2fd 100644 --- a/internal/smtp/server.go +++ b/internal/smtp/server.go @@ -4,6 +4,7 @@ import ( "context" "io" "log" + "sync/atomic" "github.com/emersion/go-smtp" @@ -27,8 +28,10 @@ func NewServer(cancel context.CancelFunc, datastore store.Interface, submission out.server.TLSConfig = datastore.TLSConfig() if submission { + out.name = "submission" out.server.Addr = ":587" } else { + out.name = "SMTP" out.server.Addr = ":25" } @@ -36,9 +39,13 @@ func NewServer(cancel context.CancelFunc, datastore store.Interface, submission } type concrete struct { + name string cancel context.CancelFunc store store.Interface server *smtp.Server + + // Session IDs + sid uint64 } func (c *concrete) Run() { @@ -52,8 +59,17 @@ func (c *concrete) Run() { } // backend implementation for go-smtp -func (c *concrete) Login(string, string) (smtp.User, error) { - return nil, nil +func (c *concrete) Login(user, pass string) (smtp.User, error) { + // FIXME: TODO: Check for account existence and correct password(!) + session := &Session{ + ID: atomic.AddUint64(&c.sid, uint64(1)), + AccountName: user, + ServiceName: c.name, + } + + log.Printf("Beginning %s session %d for %s", c.name, session.ID, user) + + return session, nil } func (c *concrete) Close() error { diff --git a/internal/smtp/session.go b/internal/smtp/session.go new file mode 100644 index 0000000..a4989cb --- /dev/null +++ b/internal/smtp/session.go @@ -0,0 +1,32 @@ +package smtp + +import ( + "fmt" + "io" + "io/ioutil" + "log" +) + +// type Session implements the User interface for emersion/go-smtp +type Session struct { + ID uint64 + AccountName string + ServiceName string +} + +func (s *Session) Send(from string, to []string, r io.Reader) error { + data, err := ioutil.ReadAll(r) + if err != nil { + return err + } + + log.Printf("%s session %d for %s: from=%s, to=%s, r=<