Convert from BoltDB to Maildir storage for emails
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package imap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
@@ -14,23 +15,29 @@ var (
|
||||
)
|
||||
|
||||
type Mailbox struct {
|
||||
stored store.Mailbox
|
||||
stored store.Maildir
|
||||
session *Session
|
||||
}
|
||||
|
||||
// Name returns this mailbox name.
|
||||
func (m *Mailbox) Name() string {
|
||||
if m.stored.Name == "" {
|
||||
return InboxName
|
||||
}
|
||||
|
||||
return m.stored.Name
|
||||
}
|
||||
|
||||
// Info returns this mailbox info.
|
||||
func (m *Mailbox) Info() (*imap.MailboxInfo, error) {
|
||||
log.Printf("Mailbox(%v).Info()", m.stored.ID)
|
||||
return &imap.MailboxInfo{
|
||||
info := &imap.MailboxInfo{
|
||||
Attributes: []string{}, // FIXME: what do we need here?
|
||||
Delimiter: ".",
|
||||
Name: m.Name(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
log.Printf("Mailbox(%v).Info(): %#v", m.stored.Rel(), info)
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// Status returns this mailbox status. The fields Name, Flags, PermanentFlags
|
||||
@@ -38,7 +45,7 @@ func (m *Mailbox) Info() (*imap.MailboxInfo, error) {
|
||||
// This function does not affect the state of any messages in the mailbox. See
|
||||
// RFC 3501 section 6.3.10 for a list of items that can be requested.
|
||||
func (m *Mailbox) Status(items []string) (*imap.MailboxStatus, error) {
|
||||
log.Printf("Mailbox(%v).Status(%#v)", m.stored.ID, items)
|
||||
log.Printf("Mailbox(%v).Status(%#v)", m.stored.Rel(), items)
|
||||
|
||||
return &imap.MailboxStatus{
|
||||
Name: m.Name(),
|
||||
@@ -52,7 +59,7 @@ func (m *Mailbox) Status(items []string) (*imap.MailboxStatus, error) {
|
||||
// SetSubscribed adds or removes the mailbox to the server's set of "active"
|
||||
// or "subscribed" mailboxes.
|
||||
func (m *Mailbox) SetSubscribed(subscribed bool) error {
|
||||
log.Printf("Mailbox(%v).SetSubscribed(%#v): not implemented", m.stored.ID, subscribed)
|
||||
log.Printf("Mailbox(%v).SetSubscribed(%#v): not implemented", m.stored.Rel(), subscribed)
|
||||
return ErrNotImplemented
|
||||
}
|
||||
|
||||
@@ -63,7 +70,7 @@ func (m *Mailbox) SetSubscribed(subscribed bool) error {
|
||||
// real time to complete. If a server implementation has no such housekeeping
|
||||
// considerations, CHECK is equivalent to NOOP.
|
||||
func (m *Mailbox) Check() error {
|
||||
log.Printf("Mailbox(%v).Check()", m.stored.ID)
|
||||
log.Printf("Mailbox(%v).Check()", m.stored.Rel())
|
||||
return nil // FIXME: do we need to do anything here?
|
||||
}
|
||||
|
||||
@@ -73,7 +80,7 @@ func (m *Mailbox) Check() error {
|
||||
//
|
||||
// Messages must be sent to ch. When the function returns, ch must be closed.
|
||||
func (m *Mailbox) ListMessages(uid bool, seqset *imap.SeqSet, items []string, ch chan<- *imap.Message) error {
|
||||
log.Printf("Mailbox(%v).ListMessages(%#v, %#v, %#v, ch)", m.stored.ID, uid, *seqset, items)
|
||||
log.Printf("Mailbox(%v).ListMessages(%#v, %#v, %#v, ch)", m.stored.Rel(), uid, *seqset, items)
|
||||
defer close(ch)
|
||||
|
||||
var messages []store.Message
|
||||
@@ -92,27 +99,35 @@ func (m *Mailbox) ListMessages(uid bool, seqset *imap.SeqSet, items []string, ch
|
||||
|
||||
log.Printf(" %v messages: %#v", len(messages), messages)
|
||||
for _, message := range messages {
|
||||
header, err := message.Header()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
envelope := &imap.Envelope{
|
||||
// Date:
|
||||
Subject: message.Header.Get("Subject"),
|
||||
Subject: header.Get("Subject"),
|
||||
// From:
|
||||
// Sender:
|
||||
// ReplyTo:
|
||||
// To:
|
||||
// Cc:
|
||||
// Bcc:
|
||||
InReplyTo: message.Header.Get("In-Reply-To"),
|
||||
MessageId: message.Header.Get("Message-Id"),
|
||||
InReplyTo: header.Get("In-Reply-To"),
|
||||
MessageId: header.Get("Message-Id"),
|
||||
}
|
||||
|
||||
body := map[*imap.BodySectionName]imap.Literal{}
|
||||
|
||||
imapMsg := imap.NewMessage(uint32(message.ID), items)
|
||||
fillItems(imapMsg, message)
|
||||
imapMsg := imap.NewMessage(uint32(message.UID), items)
|
||||
if err := fillItems(imapMsg, message); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
imapMsg.Envelope = envelope
|
||||
imapMsg.Body = body
|
||||
imapMsg.Uid = uint32(message.ID)
|
||||
imapMsg.Size = uint32(len(message.EML))
|
||||
imapMsg.Uid = uint32(message.UID)
|
||||
imapMsg.Size = uint32(message.Len())
|
||||
|
||||
log.Printf(" Sending message %#v", imapMsg)
|
||||
ch <- imapMsg
|
||||
@@ -122,28 +137,34 @@ func (m *Mailbox) ListMessages(uid bool, seqset *imap.SeqSet, items []string, ch
|
||||
return nil
|
||||
}
|
||||
|
||||
func fillItems(imapMsg *imap.Message, msg store.Message) {
|
||||
func fillItems(imapMsg *imap.Message, msg store.Message) error {
|
||||
for k, _ := range imapMsg.Items {
|
||||
switch k {
|
||||
case "UID":
|
||||
imapMsg.Items[k] = msg.ID
|
||||
imapMsg.Items[k] = msg.UID
|
||||
case "FLAGS":
|
||||
imapMsg.Items[k] = []string{}
|
||||
case "RFC822.SIZE":
|
||||
imapMsg.Items[k] = uint32(len(msg.EML))
|
||||
// FIXME: Actually send *just* the header...
|
||||
imapMsg.Items[k] = uint32(msg.Len())
|
||||
case "RFC822.HEADER":
|
||||
imapMsg.Items[k] = string(msg.EML)
|
||||
str, err := msg.HeaderString()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
imapMsg.Items[k] = str
|
||||
default:
|
||||
log.Printf("Unknown item: %v", k)
|
||||
return fmt.Errorf("Unknown item: %v", k)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SearchMessages searches messages. The returned list must contain UIDs if
|
||||
// uid is set to true, or sequence numbers otherwise.
|
||||
func (m *Mailbox) SearchMessages(uid bool, criteria *imap.SearchCriteria) ([]uint32, error) {
|
||||
log.Printf("Mailbox(%v).SearchMessages(): not implemented", m.stored.ID)
|
||||
log.Printf("Mailbox(%v).SearchMessages(): not implemented", m.stored.Rel())
|
||||
return nil, ErrNotImplemented
|
||||
}
|
||||
|
||||
@@ -154,7 +175,7 @@ func (m *Mailbox) SearchMessages(uid bool, criteria *imap.SearchCriteria) ([]uin
|
||||
// If the Backend implements Updater, it must notify the client immediately
|
||||
// via a mailbox update.
|
||||
func (m *Mailbox) CreateMessage(flags []string, date time.Time, body imap.Literal) error {
|
||||
log.Printf("Mailbox(%v).CreateMessage(%#v %#v %#v): not implemented", m.stored.ID, flags, date, body)
|
||||
log.Printf("Mailbox(%v).CreateMessage(%#v %#v %#v): not implemented", m.stored.Rel(), flags, date, body)
|
||||
return ErrNotImplemented
|
||||
}
|
||||
|
||||
@@ -163,7 +184,7 @@ func (m *Mailbox) CreateMessage(flags []string, date time.Time, body imap.Litera
|
||||
// If the Backend implements Updater, it must notify the client immediately
|
||||
// via a message update.
|
||||
func (m *Mailbox) UpdateMessagesFlags(uid bool, seqset *imap.SeqSet, operation imap.FlagsOp, flags []string) error {
|
||||
log.Printf("Mailbox(%v).UpdateMessagesFlags(...): not implemented", m.stored.ID)
|
||||
log.Printf("Mailbox(%v).UpdateMessagesFlags(...): not implemented", m.stored.Rel())
|
||||
return ErrNotImplemented
|
||||
}
|
||||
|
||||
@@ -177,7 +198,7 @@ func (m *Mailbox) UpdateMessagesFlags(uid bool, seqset *imap.SeqSet, operation i
|
||||
// If the Backend implements Updater, it must notify the client immediately
|
||||
// via a mailbox update.
|
||||
func (m *Mailbox) CopyMessages(uid bool, seqset *imap.SeqSet, dest string) error {
|
||||
log.Printf("Mailbox(%v).CopyMessages(...): not implemented", m.stored.ID)
|
||||
log.Printf("Mailbox(%v).CopyMessages(...): not implemented", m.stored.Rel())
|
||||
return ErrNotImplemented
|
||||
}
|
||||
|
||||
@@ -187,6 +208,6 @@ func (m *Mailbox) CopyMessages(uid bool, seqset *imap.SeqSet, dest string) error
|
||||
// If the Backend implements Updater, it must notify the client immediately
|
||||
// via an expunge update.
|
||||
func (m *Mailbox) Expunge() error {
|
||||
log.Printf("Mailbox(%v).CopyMessages(): not implemented", m.stored.ID)
|
||||
log.Printf("Mailbox(%v).CopyMessages(): not implemented", m.stored.Rel())
|
||||
return ErrNotImplemented
|
||||
}
|
||||
|
Reference in New Issue
Block a user