1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 17:47:03 +00:00
Files
go-inbucket/app/controllers/mailbox.go
2012-10-14 13:09:25 -07:00

108 lines
2.5 KiB
Go

package controllers
import (
"github.com/jhillyerd/inbucket/app/inbucket"
"github.com/robfig/revel"
)
type Mailbox struct {
*rev.Controller
}
func (c Mailbox) Index(name string) rev.Result {
return c.Render(name)
}
func (c Mailbox) List(name string) rev.Result {
ds := inbucket.NewDataStore()
mb, err := ds.MailboxFor(name)
if err != nil {
rev.ERROR.Printf(err.Error())
c.Flash.Error(err.Error())
return c.Redirect(Application.Index)
}
messages, err := mb.GetMessages()
if err != nil {
rev.ERROR.Printf(err.Error())
c.Flash.Error(err.Error())
return c.Redirect(Application.Index)
}
rev.INFO.Printf("Got %v messsages", len(messages))
c.Response.Out.Header().Set("Expires", "-1")
return c.Render(name, messages)
}
func (c Mailbox) Show(name string, id string) rev.Result {
ds := inbucket.NewDataStore()
mb, err := ds.MailboxFor(name)
if err != nil {
rev.ERROR.Printf(err.Error())
c.Flash.Error(err.Error())
return c.Redirect(Application.Index)
}
message, err := mb.GetMessage(id)
if err != nil {
rev.ERROR.Printf(err.Error())
c.Flash.Error(err.Error())
return c.Redirect(Application.Index)
}
_, body, err := message.ReadBody()
if err != nil {
rev.ERROR.Printf(err.Error())
c.Flash.Error(err.Error())
return c.Redirect(Application.Index)
}
c.Response.Out.Header().Set("Expires", "-1")
return c.Render(name, message, body)
}
func (c Mailbox) Delete(name string, id string) rev.Result {
ds := inbucket.NewDataStore()
mb, err := ds.MailboxFor(name)
if err != nil {
rev.ERROR.Printf(err.Error())
c.Flash.Error(err.Error())
return c.Redirect(Application.Index)
}
message, err := mb.GetMessage(id)
if err != nil {
rev.ERROR.Printf(err.Error())
c.Flash.Error(err.Error())
return c.Redirect(Application.Index)
}
err = message.Delete()
if err != nil {
rev.ERROR.Printf(err.Error())
c.Flash.Error(err.Error())
return c.Redirect(Application.Index)
}
return c.RenderText("OK")
}
func (c Mailbox) Source(name string, id string) rev.Result {
ds := inbucket.NewDataStore()
mb, err := ds.MailboxFor(name)
if err != nil {
rev.ERROR.Printf(err.Error())
c.Flash.Error(err.Error())
return c.Redirect(Application.Index)
}
message, err := mb.GetMessage(id)
if err != nil {
rev.ERROR.Printf(err.Error())
c.Flash.Error(err.Error())
return c.Redirect(Application.Index)
}
raw, err := message.ReadRaw()
if err != nil {
rev.ERROR.Printf(err.Error())
c.Flash.Error(err.Error())
return c.Redirect(Application.Index)
}
c.Response.Out.Header().Set("Expires", "-1")
return c.RenderText(*raw)
}