From a22412f65e737cb0dc182098990a07a1fd9b8503 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sat, 17 Mar 2018 15:17:44 -0700 Subject: [PATCH] manager: Add MailboxForAddress(), calls policy pkg #84 --- pkg/message/manager.go | 7 +++++++ pkg/rest/apiv1_controller.go | 11 +++++------ pkg/rest/socketv1_controller.go | 3 +-- pkg/test/manager.go | 6 ++++++ pkg/webui/mailbox_controller.go | 17 ++++++++--------- pkg/webui/root_controller.go | 3 +-- 6 files changed, 28 insertions(+), 19 deletions(-) diff --git a/pkg/message/manager.go b/pkg/message/manager.go index 5782273..1c4e04e 100644 --- a/pkg/message/manager.go +++ b/pkg/message/manager.go @@ -4,6 +4,7 @@ import ( "io" "github.com/jhillyerd/enmime" + "github.com/jhillyerd/inbucket/pkg/policy" "github.com/jhillyerd/inbucket/pkg/storage" ) @@ -14,6 +15,7 @@ type Manager interface { PurgeMessages(mailbox string) error RemoveMessage(mailbox, id string) error SourceReader(mailbox, id string) (io.ReadCloser, error) + MailboxForAddress(address string) (string, error) } // StoreManager is a message Manager backed by the storage.Store. @@ -72,6 +74,11 @@ func (s *StoreManager) SourceReader(mailbox, id string) (io.ReadCloser, error) { return sm.RawReader() } +// MailboxForAddress parses an email address to return the canonical mailbox name. +func (s *StoreManager) MailboxForAddress(mailbox string) (string, error) { + return policy.ParseMailboxName(mailbox) +} + // makeMetadata populates Metadata from a StoreMessage. func makeMetadata(m storage.StoreMessage) *Metadata { return &Metadata{ diff --git a/pkg/rest/apiv1_controller.go b/pkg/rest/apiv1_controller.go index 2b10eb0..c463ecf 100644 --- a/pkg/rest/apiv1_controller.go +++ b/pkg/rest/apiv1_controller.go @@ -11,7 +11,6 @@ import ( "strconv" "github.com/jhillyerd/inbucket/pkg/log" - "github.com/jhillyerd/inbucket/pkg/policy" "github.com/jhillyerd/inbucket/pkg/rest/model" "github.com/jhillyerd/inbucket/pkg/server/web" "github.com/jhillyerd/inbucket/pkg/storage" @@ -21,7 +20,7 @@ import ( // MailboxListV1 renders a list of messages in a mailbox func MailboxListV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { // Don't have to validate these aren't empty, Gorilla returns 404 - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { return err } @@ -51,7 +50,7 @@ func MailboxListV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) ( func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { // Don't have to validate these aren't empty, Gorilla returns 404 id := ctx.Vars["id"] - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { return err } @@ -101,7 +100,7 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) ( // MailboxPurgeV1 deletes all messages from a mailbox func MailboxPurgeV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { // Don't have to validate these aren't empty, Gorilla returns 404 - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { return err } @@ -119,7 +118,7 @@ func MailboxPurgeV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) func MailboxSourceV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { // Don't have to validate these aren't empty, Gorilla returns 404 id := ctx.Vars["id"] - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { return err } @@ -143,7 +142,7 @@ func MailboxSourceV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) func MailboxDeleteV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { // Don't have to validate these aren't empty, Gorilla returns 404 id := ctx.Vars["id"] - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { return err } diff --git a/pkg/rest/socketv1_controller.go b/pkg/rest/socketv1_controller.go index 7614ac1..d0ceddd 100644 --- a/pkg/rest/socketv1_controller.go +++ b/pkg/rest/socketv1_controller.go @@ -7,7 +7,6 @@ import ( "github.com/gorilla/websocket" "github.com/jhillyerd/inbucket/pkg/log" "github.com/jhillyerd/inbucket/pkg/msghub" - "github.com/jhillyerd/inbucket/pkg/policy" "github.com/jhillyerd/inbucket/pkg/rest/model" "github.com/jhillyerd/inbucket/pkg/server/web" ) @@ -173,7 +172,7 @@ func MonitorAllMessagesV1( // notifies the client of messages received by a particular mailbox. func MonitorMailboxMessagesV1( w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { return err } diff --git a/pkg/test/manager.go b/pkg/test/manager.go index 47c87a8..bf8d9ad 100644 --- a/pkg/test/manager.go +++ b/pkg/test/manager.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/jhillyerd/inbucket/pkg/message" + "github.com/jhillyerd/inbucket/pkg/policy" "github.com/jhillyerd/inbucket/pkg/storage" ) @@ -51,3 +52,8 @@ func (m *ManagerStub) GetMetadata(mailbox string) ([]*message.Metadata, error) { } return metas, nil } + +// MailboxForAddress invokes policy.ParseMailboxName. +func (m *ManagerStub) MailboxForAddress(address string) (string, error) { + return policy.ParseMailboxName(address) +} diff --git a/pkg/webui/mailbox_controller.go b/pkg/webui/mailbox_controller.go index 2af9ef1..d876b3a 100644 --- a/pkg/webui/mailbox_controller.go +++ b/pkg/webui/mailbox_controller.go @@ -8,7 +8,6 @@ import ( "strconv" "github.com/jhillyerd/inbucket/pkg/log" - "github.com/jhillyerd/inbucket/pkg/policy" "github.com/jhillyerd/inbucket/pkg/server/web" "github.com/jhillyerd/inbucket/pkg/storage" "github.com/jhillyerd/inbucket/pkg/webui/sanitize" @@ -25,7 +24,7 @@ func MailboxIndex(w http.ResponseWriter, req *http.Request, ctx *web.Context) (e http.Redirect(w, req, web.Reverse("RootIndex"), http.StatusSeeOther) return nil } - name, err = policy.ParseMailboxName(name) + name, err = ctx.Manager.MailboxForAddress(name) if err != nil { ctx.Session.AddFlash(err.Error(), "errors") _ = ctx.Session.Save(req, w) @@ -52,7 +51,7 @@ func MailboxIndex(w http.ResponseWriter, req *http.Request, ctx *web.Context) (e func MailboxLink(w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { // Don't have to validate these aren't empty, Gorilla returns 404 id := ctx.Vars["id"] - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { ctx.Session.AddFlash(err.Error(), "errors") _ = ctx.Session.Save(req, w) @@ -68,7 +67,7 @@ func MailboxLink(w http.ResponseWriter, req *http.Request, ctx *web.Context) (er // MailboxList renders a list of messages in a mailbox. Renders a partial func MailboxList(w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { // Don't have to validate these aren't empty, Gorilla returns 404 - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { return err } @@ -90,7 +89,7 @@ func MailboxList(w http.ResponseWriter, req *http.Request, ctx *web.Context) (er func MailboxShow(w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { // Don't have to validate these aren't empty, Gorilla returns 404 id := ctx.Vars["id"] - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { return err } @@ -131,7 +130,7 @@ func MailboxShow(w http.ResponseWriter, req *http.Request, ctx *web.Context) (er func MailboxHTML(w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { // Don't have to validate these aren't empty, Gorilla returns 404 id := ctx.Vars["id"] - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { return err } @@ -159,7 +158,7 @@ func MailboxHTML(w http.ResponseWriter, req *http.Request, ctx *web.Context) (er func MailboxSource(w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { // Don't have to validate these aren't empty, Gorilla returns 404 id := ctx.Vars["id"] - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { return err } @@ -183,7 +182,7 @@ func MailboxSource(w http.ResponseWriter, req *http.Request, ctx *web.Context) ( func MailboxDownloadAttach(w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { // Don't have to validate these aren't empty, Gorilla returns 404 id := ctx.Vars["id"] - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { ctx.Session.AddFlash(err.Error(), "errors") _ = ctx.Session.Save(req, w) @@ -225,7 +224,7 @@ func MailboxDownloadAttach(w http.ResponseWriter, req *http.Request, ctx *web.Co // MailboxViewAttach sends the attachment to the client for online viewing func MailboxViewAttach(w http.ResponseWriter, req *http.Request, ctx *web.Context) (err error) { // Don't have to validate these aren't empty, Gorilla returns 404 - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { ctx.Session.AddFlash(err.Error(), "errors") _ = ctx.Session.Save(req, w) diff --git a/pkg/webui/root_controller.go b/pkg/webui/root_controller.go index 21d4dd3..0ea5780 100644 --- a/pkg/webui/root_controller.go +++ b/pkg/webui/root_controller.go @@ -7,7 +7,6 @@ import ( "net/http" "github.com/jhillyerd/inbucket/pkg/config" - "github.com/jhillyerd/inbucket/pkg/policy" "github.com/jhillyerd/inbucket/pkg/server/web" ) @@ -58,7 +57,7 @@ func RootMonitorMailbox(w http.ResponseWriter, req *http.Request, ctx *web.Conte http.Redirect(w, req, web.Reverse("RootIndex"), http.StatusSeeOther) return nil } - name, err := policy.ParseMailboxName(ctx.Vars["name"]) + name, err := ctx.Manager.MailboxForAddress(ctx.Vars["name"]) if err != nil { ctx.Session.AddFlash(err.Error(), "errors") _ = ctx.Session.Save(req, w)