From 442ca833ea5c3d29730b05dea62f7158b816695a Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Thu, 8 Mar 2018 00:17:44 +0000 Subject: [PATCH] Gut the Session interface --- internal/smtp/msa.go | 8 ++++---- internal/smtp/mta.go | 16 ++++++++++++---- internal/smtp/receiver.go | 20 -------------------- internal/smtp/sender.go | 18 ++++++++---------- internal/smtp/session.go | 17 ++++++----------- 5 files changed, 30 insertions(+), 49 deletions(-) delete mode 100644 internal/smtp/receiver.go diff --git a/internal/smtp/msa.go b/internal/smtp/msa.go index 8afd8e2..2ae0ae5 100644 --- a/internal/smtp/msa.go +++ b/internal/smtp/msa.go @@ -60,13 +60,13 @@ func (m *msa) Login(user, pass string) (smtp.User, error) { return nil, fmt.Errorf("Login failed") } + sid := atomic.AddUint64(&m.sid, uint64(1)) session := &Session{ - ID: atomic.AddUint64(&m.sid, uint64(1)), - Account: account, - Handler: &Sender{}, + ID: fmt.Sprintf("submission:%d", sid), + Handler: &sender{msa: m, account: account}, } - log.Printf("Beginning submission session %d for %s", session.ID, user) + log.Printf("Beginning session %d for username=%s", session.ID, account.Username) // FIXME: TODO: Track ongoing sessions for termination or notifications return session, nil diff --git a/internal/smtp/mta.go b/internal/smtp/mta.go index 451b468..87e86f5 100644 --- a/internal/smtp/mta.go +++ b/internal/smtp/mta.go @@ -3,6 +3,7 @@ package smtp import ( "context" "fmt" + "io" "log" "sync/atomic" @@ -58,14 +59,21 @@ func (m *mta) Login(user, pass string) (smtp.User, error) { } func (m *mta) AnonymousLogin() (smtp.User, error) { + sid := atomic.AddUint64(&m.sid, uint64(1)) session := &Session{ - ID: atomic.AddUint64(&m.sid, uint64(1)), - Account: nil, - Handler: &Receiver{}, + ID: fmt.Sprintf("smtp:%d", sid), + Handler: m, } - log.Printf("Beginning SMTP session %d", session.ID) + log.Printf("Beginning session %d", session.ID) // FIXME: TODO: Track ongoing sessions for termination or notifications return session, nil } + +// User implementation for go-smtp after this point +// Since only anonymous login is permitted, there's no need to introduce an +// intermediary to track the logged-in user. +func (m *mta) ServeSMTP(from string, to []string, r io.Reader) error { + return fmt.Errorf("Not yet implemented") +} diff --git a/internal/smtp/receiver.go b/internal/smtp/receiver.go deleted file mode 100644 index 429ca48..0000000 --- a/internal/smtp/receiver.go +++ /dev/null @@ -1,20 +0,0 @@ -package smtp - -import ( - "fmt" - "io" -) - -type Receiver struct{} - -func (r *Receiver) Name() string { - return "smtp-starttls" -} - -// It seems odd to have a Receiver called Send, but what we're looking for is an -// attempt from an arbitrary source to send an email to known accounts. -// -// We might want to clean this interface -func (r *Receiver) Send(from string, to []string, reader io.Reader) error { - return fmt.Errorf("Not yet implemented") -} diff --git a/internal/smtp/sender.go b/internal/smtp/sender.go index c22a5e3..7646eb3 100644 --- a/internal/smtp/sender.go +++ b/internal/smtp/sender.go @@ -3,19 +3,17 @@ package smtp import ( "fmt" "io" + + "ur.gs/crockery/internal/store" ) -type Sender struct{} - -func (s *Sender) Name() string { - return "submission-starttls" +// Submission requires the sender to be logged in, so this struct tracks the +// logged-in account per-session +type sender struct { + msa *msa + account *store.Account } -// We're looking for the email to be from a logged-in account, to anywhere -// on the internet - including locally! -// -// Should we handle local delivery differently to remote delivery, or connect -// to ourselves, i.e., from submission, to smtp? -func (s *Sender) Send(from string, to []string, reader io.Reader) error { +func (s *sender) ServeSMTP(from string, to []string, r io.Reader) error { return fmt.Errorf("Not yet implemented") } diff --git a/internal/smtp/session.go b/internal/smtp/session.go index 0b3da11..bd1477e 100644 --- a/internal/smtp/session.go +++ b/internal/smtp/session.go @@ -5,19 +5,14 @@ import ( "io" "io/ioutil" "log" - - "ur.gs/crockery/internal/store" ) type Handler interface { - Name() string - Send(from string, to []string, r io.Reader) error + ServeSMTP(from string, to []string, r io.Reader) error } -// type Session implements the User interface for emersion/go-smtp type Session struct { - ID uint64 - Account *store.Account + ID string Handler Handler } @@ -28,14 +23,14 @@ func (s *Session) Send(from string, to []string, r io.Reader) error { return err } - log.Printf("%s session %d for %s: from=%s, to=%s, r=<