mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 10:07:02 +00:00
Add a controller to view list mailbox contents, view individual
messages.
This commit is contained in:
60
app/controllers/mailbox.go
Normal file
60
app/controllers/mailbox.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"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.Redirect("/mailbox/list/%v", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Mailbox) List(name string) rev.Result {
|
||||||
|
title := fmt.Sprintf("Mailbox for %v", name)
|
||||||
|
|
||||||
|
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))
|
||||||
|
|
||||||
|
return c.Render(title, 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Render(name, message, body)
|
||||||
|
}
|
||||||
@@ -76,21 +76,56 @@ func (mb *Mailbox) String() string {
|
|||||||
return mb.name + "[" + mb.dirName + "]"
|
return mb.name + "[" + mb.dirName + "]"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMessages scans the mailbox directory for .gob files and decodes them into
|
||||||
|
// a slice of Message objects.
|
||||||
func (mb *Mailbox) GetMessages() ([]*Message, error) {
|
func (mb *Mailbox) GetMessages() ([]*Message, error) {
|
||||||
files, err := ioutil.ReadDir(mb.path)
|
files, err := ioutil.ReadDir(mb.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// This is twice the size it needs to be, oh darn
|
rev.TRACE.Printf("Scanning %v files for %v", len(files), mb)
|
||||||
messages := make([]*Message, len(files))
|
|
||||||
|
messages := make([]*Message, 0, len(files))
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
if (!f.IsDir()) && strings.HasSuffix(strings.ToLower(f.Name()), ".gob") {
|
if (!f.IsDir()) && strings.HasSuffix(strings.ToLower(f.Name()), ".gob") {
|
||||||
// TODO: implement
|
// We have a gob file
|
||||||
|
file, err := os.Open(filepath.Join(mb.path, f.Name()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
dec := gob.NewDecoder(bufio.NewReader(file))
|
||||||
|
msg := new(Message)
|
||||||
|
if err = dec.Decode(msg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
file.Close()
|
||||||
|
msg.mailbox = mb
|
||||||
|
rev.TRACE.Printf("Found: %v", msg)
|
||||||
|
messages = append(messages, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return messages, nil
|
return messages, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMessage decodes a single message by Id and returns a Message object
|
||||||
|
func (mb *Mailbox) GetMessage(id string) (*Message, error) {
|
||||||
|
file, err := os.Open(filepath.Join(mb.path, id+".gob"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
dec := gob.NewDecoder(bufio.NewReader(file))
|
||||||
|
msg := new(Message)
|
||||||
|
if err = dec.Decode(msg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
file.Close()
|
||||||
|
msg.mailbox = mb
|
||||||
|
rev.TRACE.Printf("Found: %v", msg)
|
||||||
|
|
||||||
|
return msg, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Message contains a little bit of data about a particular email message, and
|
// Message contains a little bit of data about a particular email message, and
|
||||||
// methods to retrieve the rest of it from disk.
|
// methods to retrieve the rest of it from disk.
|
||||||
type Message struct {
|
type Message struct {
|
||||||
@@ -122,6 +157,8 @@ func (m *Message) gobPath() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Message) rawPath() string {
|
func (m *Message) rawPath() string {
|
||||||
|
rev.TRACE.Println(m.mailbox.path)
|
||||||
|
rev.TRACE.Println(m.Id)
|
||||||
return filepath.Join(m.mailbox.path, m.Id+".raw")
|
return filepath.Join(m.mailbox.path, m.Id+".raw")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,6 +174,26 @@ func (m *Message) ReadHeader() (msg *mail.Message, err error) {
|
|||||||
return msg, err
|
return msg, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadBody opens the .raw portion of a Message and returns a standard Go mail.Message object
|
||||||
|
func (m *Message) ReadBody() (msg *mail.Message, body *string, err error) {
|
||||||
|
file, err := os.Open(m.rawPath())
|
||||||
|
defer file.Close()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
reader := bufio.NewReader(file)
|
||||||
|
msg, err = mail.ReadMessage(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
bodyBytes, err := ioutil.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
bodyString := string(bodyBytes)
|
||||||
|
return msg, &bodyString, err
|
||||||
|
}
|
||||||
|
|
||||||
// Append data to a newly opened Message, this will fail on a pre-existing Message and
|
// Append data to a newly opened Message, this will fail on a pre-existing Message and
|
||||||
// after Close() is called.
|
// after Close() is called.
|
||||||
func (m *Message) Append(data []byte) error {
|
func (m *Message) Append(data []byte) error {
|
||||||
|
|||||||
22
app/views/Mailbox/List.html
Normal file
22
app/views/Mailbox/List.html
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
{{template "header.html" .}}
|
||||||
|
|
||||||
|
<h1>Your Index Is Ready</h1>
|
||||||
|
{{$name := .name}}
|
||||||
|
<p>{{.name}} inbox</p>
|
||||||
|
|
||||||
|
{{if .messages}}
|
||||||
|
<ul>
|
||||||
|
{{range .messages}}
|
||||||
|
<li>
|
||||||
|
<a href="/mailbox/show/{{$name}}/{{.Id}}">{{.Subject}}</a>
|
||||||
|
from {{.From}}
|
||||||
|
({{.Date}})
|
||||||
|
</li>
|
||||||
|
{{end}}
|
||||||
|
</ul>
|
||||||
|
{{else}}
|
||||||
|
<p>No messages!</p>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{template "footer.html" .}}
|
||||||
|
|
||||||
18
app/views/Mailbox/Show.html
Normal file
18
app/views/Mailbox/Show.html
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{{template "header.html" .}}
|
||||||
|
|
||||||
|
<h1>{{.message.Subject}}</h1>
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>From:</th>
|
||||||
|
<td>{{.message.From}}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th>Date:</th>
|
||||||
|
<td>{{.message.Date}}</td>
|
||||||
|
</tr>
|
||||||
|
<table>
|
||||||
|
|
||||||
|
<pre>{{.body}}</pre>
|
||||||
|
|
||||||
|
{{template "footer.html" .}}
|
||||||
|
|
||||||
@@ -3,6 +3,9 @@
|
|||||||
# ~~~~
|
# ~~~~
|
||||||
|
|
||||||
GET / Application.Index
|
GET / Application.Index
|
||||||
|
GET /mailbox/{name} Mailbox.Index
|
||||||
|
GET /mailbox/list/{name} Mailbox.List
|
||||||
|
GET /mailbox/show/{name}/{id} Mailbox.Show
|
||||||
|
|
||||||
# Ignore favicon requests
|
# Ignore favicon requests
|
||||||
GET /favicon.ico 404
|
GET /favicon.ico 404
|
||||||
|
|||||||
Reference in New Issue
Block a user