250 lines
7.6 KiB
Go
250 lines
7.6 KiB
Go
package imap
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/emersion/go-imap"
|
|
"github.com/emersion/go-imap/backend/backendutil"
|
|
"github.com/emersion/go-message"
|
|
|
|
"ur.gs/crockery/internal/store"
|
|
)
|
|
|
|
var (
|
|
InboxName = imap.InboxName
|
|
)
|
|
|
|
type Mailbox struct {
|
|
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) {
|
|
info := &imap.MailboxInfo{
|
|
Attributes: []string{}, // FIXME: what do we need here?
|
|
Delimiter: ".",
|
|
Name: m.Name(),
|
|
}
|
|
|
|
log.Printf("Mailbox(%v).Info(): %#v", m.stored.Rel(), info)
|
|
return info, nil
|
|
}
|
|
|
|
// Status returns this mailbox status. The fields Name, Flags, PermanentFlags
|
|
// and UnseenSeqNum in the returned MailboxStatus must be always populated.
|
|
// 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 []imap.StatusItem) (*imap.MailboxStatus, error) {
|
|
status, err := m.buildStatus(items)
|
|
log.Printf("Mailbox(%v).Status(%#v): %#v %#v", m.stored.Rel(), items, status, err)
|
|
return status, err
|
|
}
|
|
|
|
// 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.Rel(), subscribed)
|
|
return ErrNotImplemented
|
|
}
|
|
|
|
// Check requests a checkpoint of the currently selected mailbox. A checkpoint
|
|
// refers to any implementation-dependent housekeeping associated with the
|
|
// mailbox (e.g., resolving the server's in-memory state of the mailbox with
|
|
// the state on its disk). A checkpoint MAY take a non-instantaneous amount of
|
|
// 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.Rel())
|
|
return nil // FIXME: do we need to do anything here?
|
|
}
|
|
|
|
// ListMessages returns a list of messages. seqset must be interpreted as UIDs
|
|
// if uid is set to true and as message sequence numbers otherwise. See RFC
|
|
// 3501 section 6.4.5 for a list of items that can be requested.
|
|
//
|
|
// Messages must be sent to ch. When the function returns, ch must be closed.
|
|
func (m *Mailbox) ListMessages(uid bool, seqset *imap.SeqSet, items []imap.FetchItem, ch chan<- *imap.Message) error {
|
|
log.Printf("Mailbox(%v).ListMessages(%#v, %#v, %#v, ch)", m.stored.Rel(), uid, *seqset, items)
|
|
defer close(ch)
|
|
|
|
var messages []store.Message
|
|
for _, set := range seqset.Set {
|
|
msgs, err := m.session.store.FindMessagesInRangeBySeqNum(
|
|
m.stored, uint64(set.Start), uint64(set.Stop),
|
|
)
|
|
|
|
if err != nil && !store.IsNotFound(err) {
|
|
log.Printf(" error: %#v", err)
|
|
return err
|
|
}
|
|
|
|
messages = append(messages, msgs...)
|
|
}
|
|
|
|
log.Printf(" %v messages: %#v", len(messages), messages)
|
|
for _, message := range messages {
|
|
imapMsg, err := m.buildMessage(message, items)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Printf(" Sending message %#v", imapMsg)
|
|
ch <- imapMsg
|
|
log.Printf(" Sent message")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *Mailbox) buildStatus(items []imap.StatusItem) (*imap.MailboxStatus, error) {
|
|
out := &imap.MailboxStatus{
|
|
Name: m.Name(),
|
|
}
|
|
|
|
for _, item := range items {
|
|
switch item {
|
|
case "FLAGS": // TODO: ???
|
|
out.Flags = []string{}
|
|
case "PERMANENTFLAGS": // TODO: ???
|
|
out.PermanentFlags = []string{}
|
|
// The number of messages in the mailbox.
|
|
case "MESSAGES":
|
|
out.Messages = uint32(m.session.store.CountMessages(m.stored))
|
|
// The number of messages with the \Recent flag set. TODO
|
|
case "RECENT":
|
|
// The next unique identifier value of the mailbox.
|
|
case "UIDNEXT":
|
|
out.UidNext = uint32(m.stored.NextUID())
|
|
// The unique identifier validity value of the mailbox
|
|
case "UIDVALIDITY":
|
|
out.UidValidity = uint32(m.stored.UIDValidity())
|
|
// The number of messages which do not have the \Seen flag set. TODO
|
|
case "UNSEEN":
|
|
default:
|
|
return nil, fmt.Errorf("Unknown status item: %v", item)
|
|
}
|
|
}
|
|
|
|
return out, nil
|
|
}
|
|
|
|
func (m *Mailbox) buildMessage(msg store.Message, items []imap.FetchItem) (*imap.Message, error) {
|
|
out := imap.NewMessage(uint32(msg.SeqNum), items)
|
|
|
|
// FIXME: This might not be necessary on some calls, but is needed in 3
|
|
// different cases below. Tidy it up.
|
|
data, err := m.stored.MessageData(msg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer data.Close()
|
|
|
|
entity, err := message.Read(data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, item := range items {
|
|
switch item {
|
|
case imap.FetchEnvelope:
|
|
env, err := backendutil.FetchEnvelope(entity.Header)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.Envelope = env
|
|
case imap.FetchBody, imap.FetchBodyStructure:
|
|
bs, err := backendutil.FetchBodyStructure(entity, item == imap.FetchBodyStructure)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.BodyStructure = bs
|
|
case imap.FetchInternalDate:
|
|
case imap.FetchUid:
|
|
out.Uid = uint32(msg.UID)
|
|
case imap.FetchFlags:
|
|
out.Flags = []string{}
|
|
case imap.FetchRFC822Size:
|
|
out.Size = uint32(msg.Len())
|
|
default:
|
|
section, err := imap.ParseBodySectionName(item)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
l, err := backendutil.FetchBodySection(entity, section)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out.Body[section] = l
|
|
}
|
|
}
|
|
|
|
return out, 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.Rel())
|
|
return nil, ErrNotImplemented
|
|
}
|
|
|
|
// CreateMessage appends a new message to this mailbox. The \Recent flag will
|
|
// be added no matter flags is empty or not. If date is nil, the current time
|
|
// will be used.
|
|
//
|
|
// 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.Rel(), flags, date, body)
|
|
return ErrNotImplemented
|
|
}
|
|
|
|
// UpdateMessagesFlags alters flags for the specified message(s).
|
|
//
|
|
// 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.Rel())
|
|
return ErrNotImplemented
|
|
}
|
|
|
|
// CopyMessages copies the specified message(s) to the end of the specified
|
|
// destination mailbox. The flags and internal date of the message(s) SHOULD
|
|
// be preserved, and the Recent flag SHOULD be set, in the copy.
|
|
//
|
|
// If the destination mailbox does not exist, a server SHOULD return an error.
|
|
// It SHOULD NOT automatically create the mailbox.
|
|
//
|
|
// 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.Rel())
|
|
return ErrNotImplemented
|
|
}
|
|
|
|
// Expunge permanently removes all messages that have the \Deleted flag set
|
|
// from the currently selected mailbox.
|
|
//
|
|
// 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.Rel())
|
|
return ErrNotImplemented
|
|
}
|