mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
Refactor datastore: DataStore -> Mailbox -> Message (was MailObject)
This commit is contained in:
@@ -2,15 +2,19 @@ package inbucket
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/gob"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/robfig/revel"
|
||||
"io/ioutil"
|
||||
"net/mail"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrNotWritable = errors.New("MailObject not writable")
|
||||
var ErrNotWritable = errors.New("Message not writable")
|
||||
|
||||
// Global because we only want one regardless of the number of DataStore objects
|
||||
var countChannel = make(chan int, 10)
|
||||
@@ -27,11 +31,15 @@ func countGenerator(c chan int) {
|
||||
}
|
||||
}
|
||||
|
||||
// A DataStore is the root of the mail storage hiearchy. It provides access to
|
||||
// Mailbox objects
|
||||
type DataStore struct {
|
||||
path string
|
||||
mailPath string
|
||||
}
|
||||
|
||||
// NewDataStore creates a new DataStore object. It uses the Revel Config object to
|
||||
// construct it's path.
|
||||
func NewDataStore() *DataStore {
|
||||
path, found := rev.Config.String("datastore.path")
|
||||
if found {
|
||||
@@ -42,43 +50,103 @@ func NewDataStore() *DataStore {
|
||||
return nil
|
||||
}
|
||||
|
||||
type MailObject struct {
|
||||
store *DataStore
|
||||
mailbox string
|
||||
rawPath string
|
||||
gobPath string
|
||||
// Retrieves the Mailbox object for a specified email address, if the mailbox
|
||||
// does not exist, it will attempt to create it.
|
||||
func (ds *DataStore) MailboxFor(emailAddress string) (*Mailbox, error) {
|
||||
name := ParseMailboxName(emailAddress)
|
||||
dir := HashMailboxName(name)
|
||||
path := filepath.Join(ds.mailPath, dir)
|
||||
if err := os.MkdirAll(path, 0770); err != nil {
|
||||
rev.ERROR.Printf("Failed to create directory %v, %v", path, err)
|
||||
return nil, err
|
||||
}
|
||||
return &Mailbox{store: ds, name: name, dirName: dir, path: path}, nil
|
||||
}
|
||||
|
||||
// A Mailbox manages the mail for a specific user and correlates to a particular
|
||||
// directory on disk.
|
||||
type Mailbox struct {
|
||||
store *DataStore
|
||||
name string
|
||||
dirName string
|
||||
path string
|
||||
}
|
||||
|
||||
func (mb *Mailbox) String() string {
|
||||
return mb.name + "[" + mb.dirName + "]"
|
||||
}
|
||||
|
||||
func (mb *Mailbox) GetMessages() ([]*Message, error) {
|
||||
files, err := ioutil.ReadDir(mb.path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// This is twice the size it needs to be, oh darn
|
||||
messages := make([]*Message, len(files))
|
||||
for _, f := range files {
|
||||
if (!f.IsDir()) && strings.HasSuffix(strings.ToLower(f.Name()), ".gob") {
|
||||
// TODO: implement
|
||||
}
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
// Message contains a little bit of data about a particular email message, and
|
||||
// methods to retrieve the rest of it from disk.
|
||||
type Message struct {
|
||||
mailbox *Mailbox
|
||||
Id string
|
||||
Date time.Time
|
||||
From string
|
||||
Subject string
|
||||
// These are for creating new messages only
|
||||
writable bool
|
||||
writerFile *os.File
|
||||
writer *bufio.Writer
|
||||
}
|
||||
|
||||
func (ds *DataStore) NewMailObject(emailAddress string) *MailObject {
|
||||
mailbox := ParseMailboxName(emailAddress)
|
||||
maildir := HashMailboxName(mailbox)
|
||||
fileBase := time.Now().Format("20060102T150405") + "-" + fmt.Sprintf("%04d", <-countChannel)
|
||||
boxPath := filepath.Join(ds.mailPath, maildir)
|
||||
if err := os.MkdirAll(boxPath, 0770); err != nil {
|
||||
rev.ERROR.Printf("Failed to create directory %v, %v", boxPath, err)
|
||||
return nil
|
||||
// NewMessage creates a new Message object and sets the Date and Id fields.
|
||||
func (mb *Mailbox) NewMessage() *Message {
|
||||
date := time.Now()
|
||||
id := date.Format("20060102T150405") + "-" + fmt.Sprintf("%04d", <-countChannel)
|
||||
|
||||
return &Message{mailbox: mb, Id: id, Date: date, writable: true}
|
||||
}
|
||||
|
||||
func (m *Message) String() string {
|
||||
return fmt.Sprintf("\"%v\" from %v", m.Subject, m.From)
|
||||
}
|
||||
|
||||
func (m *Message) gobPath() string {
|
||||
return filepath.Join(m.mailbox.path, m.Id+".gob")
|
||||
}
|
||||
|
||||
func (m *Message) rawPath() string {
|
||||
return filepath.Join(m.mailbox.path, m.Id+".raw")
|
||||
}
|
||||
|
||||
// ReadHeader opens the .raw portion of a Message and returns a standard Go mail.Message object
|
||||
func (m *Message) ReadHeader() (msg *mail.Message, err error) {
|
||||
file, err := os.Open(m.rawPath())
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pathBase := filepath.Join(boxPath, fileBase)
|
||||
|
||||
return &MailObject{store: ds, mailbox: mailbox, rawPath: pathBase + ".raw",
|
||||
gobPath: pathBase + ".gob", writable: true}
|
||||
reader := bufio.NewReader(file)
|
||||
msg, err = mail.ReadMessage(reader)
|
||||
return msg, err
|
||||
}
|
||||
|
||||
func (m *MailObject) Mailbox() string {
|
||||
return m.mailbox
|
||||
}
|
||||
|
||||
func (m *MailObject) Append(data []byte) error {
|
||||
// Prevent Appending to a pre-existing MailObject
|
||||
// Append data to a newly opened Message, this will fail on a pre-existing Message and
|
||||
// after Close() is called.
|
||||
func (m *Message) Append(data []byte) error {
|
||||
// Prevent Appending to a pre-existing Message
|
||||
if !m.writable {
|
||||
return ErrNotWritable
|
||||
}
|
||||
// Open file for writing if we haven't yet
|
||||
if m.writer == nil {
|
||||
file, err := os.Create(m.rawPath)
|
||||
file, err := os.Create(m.rawPath())
|
||||
if err != nil {
|
||||
// Set writable false just in case something calls me a million times
|
||||
m.writable = false
|
||||
@@ -91,23 +159,56 @@ func (m *MailObject) Append(data []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *MailObject) Close() error {
|
||||
// nil out the fields so they can't be used
|
||||
// Close this Message for writing - no more data may be Appended. Close() will also
|
||||
// trigger the creation of the .gob file.
|
||||
func (m *Message) Close() error {
|
||||
// nil out the writer fields so they can't be used
|
||||
writer := m.writer
|
||||
writerFile := m.writerFile
|
||||
m.writer = nil
|
||||
m.writerFile = nil
|
||||
|
||||
if (writer != nil) {
|
||||
if writer != nil {
|
||||
if err := writer.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if (writerFile != nil) {
|
||||
if writerFile != nil {
|
||||
if err := writerFile.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return m.createGob()
|
||||
}
|
||||
|
||||
// createGob reads the .raw file to grab the From and Subject header entries,
|
||||
// then creates the .gob file.
|
||||
func (m *Message) createGob() error {
|
||||
// Open gob for writing
|
||||
file, err := os.Create(m.gobPath())
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writer := bufio.NewWriter(file)
|
||||
|
||||
// Fetch headers
|
||||
msg, err := m.ReadHeader()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Only public fields are stored in gob
|
||||
m.From = msg.Header.Get("From")
|
||||
m.Subject = msg.Header.Get("Subject")
|
||||
|
||||
// Write & flush
|
||||
enc := gob.NewEncoder(writer)
|
||||
err = enc.Encode(m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writer.Flush()
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user