Extremely WIP, but now messages appear in the evolution list pane
This commit is contained in:
9
Makefile
Normal file
9
Makefile
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
SOURCES := $(shell find $(SOURCEDIR) -iname *.go)
|
||||||
|
|
||||||
|
crockery: $(SOURCES)
|
||||||
|
go build ur.gs/crockery/cmd/crockery && sudo setcap 'cap_net_bind_service=+ep' crockery
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f crockery
|
||||||
|
|
||||||
|
.PHONY: init
|
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"gopkg.in/urfave/cli.v1"
|
"gopkg.in/urfave/cli.v1"
|
||||||
|
|
||||||
|
"ur.gs/crockery/internal/imap"
|
||||||
"ur.gs/crockery/internal/store"
|
"ur.gs/crockery/internal/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -69,13 +70,24 @@ func crockeryInit(c *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
postmaster := &store.Account{
|
username := "postmaster@" + domain
|
||||||
Username: "postmaster@" + domain,
|
mailbox := store.Mailbox{
|
||||||
Admin: true,
|
Account: username,
|
||||||
PasswordHash: hash,
|
Name: imap.InboxName,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := datastore.CreateAccount(postmaster); err != nil {
|
if err := datastore.CreateMailbox(&mailbox); err != nil {
|
||||||
|
return fmt.Errorf("Failed to create admin mailbox %s: %v", mailbox.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
postmaster := store.Account{
|
||||||
|
Username: username,
|
||||||
|
Admin: true,
|
||||||
|
PasswordHash: hash,
|
||||||
|
DefaultMailbox: mailbox.ID, // Automatically filled in by the store
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := datastore.CreateAccount(&postmaster); err != nil {
|
||||||
return fmt.Errorf("Failed to create admin account %s: %v", postmaster.Username, err)
|
return fmt.Errorf("Failed to create admin account %s: %v", postmaster.Username, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,27 +1,34 @@
|
|||||||
package imap
|
package imap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/emersion/go-imap"
|
"github.com/emersion/go-imap"
|
||||||
|
|
||||||
|
"ur.gs/crockery/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
InboxName = imap.InboxName
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mailbox struct {
|
type Mailbox struct {
|
||||||
name string
|
stored store.Mailbox
|
||||||
session *Session
|
session *Session
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns this mailbox name.
|
// Name returns this mailbox name.
|
||||||
func (m *Mailbox) Name() string {
|
func (m *Mailbox) Name() string {
|
||||||
return m.name
|
return m.stored.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info returns this mailbox info.
|
// Info returns this mailbox info.
|
||||||
func (m *Mailbox) Info() (*imap.MailboxInfo, error) {
|
func (m *Mailbox) Info() (*imap.MailboxInfo, error) {
|
||||||
|
log.Printf("Mailbox(%v).Info()", m.stored.ID)
|
||||||
return &imap.MailboxInfo{
|
return &imap.MailboxInfo{
|
||||||
Attributes: []string{}, // FIXME: what do we need here?
|
Attributes: []string{}, // FIXME: what do we need here?
|
||||||
Delimiter: "/",
|
Delimiter: ".",
|
||||||
Name: m.Name(),
|
Name: m.Name(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@@ -31,6 +38,8 @@ func (m *Mailbox) Info() (*imap.MailboxInfo, error) {
|
|||||||
// This function does not affect the state of any messages in the mailbox. See
|
// 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.
|
// RFC 3501 section 6.3.10 for a list of items that can be requested.
|
||||||
func (m *Mailbox) Status(items []string) (*imap.MailboxStatus, error) {
|
func (m *Mailbox) Status(items []string) (*imap.MailboxStatus, error) {
|
||||||
|
log.Printf("Mailbox(%v).Status(%#v)", m.stored.ID, items)
|
||||||
|
|
||||||
return &imap.MailboxStatus{
|
return &imap.MailboxStatus{
|
||||||
Name: m.Name(),
|
Name: m.Name(),
|
||||||
Flags: []string{}, // FIXME: what do we need here?
|
Flags: []string{}, // FIXME: what do we need here?
|
||||||
@@ -43,6 +52,7 @@ func (m *Mailbox) Status(items []string) (*imap.MailboxStatus, error) {
|
|||||||
// SetSubscribed adds or removes the mailbox to the server's set of "active"
|
// SetSubscribed adds or removes the mailbox to the server's set of "active"
|
||||||
// or "subscribed" mailboxes.
|
// or "subscribed" mailboxes.
|
||||||
func (m *Mailbox) SetSubscribed(subscribed bool) error {
|
func (m *Mailbox) SetSubscribed(subscribed bool) error {
|
||||||
|
log.Printf("Mailbox(%v).SetSubscribed(%#v): not implemented", m.stored.ID, subscribed)
|
||||||
return ErrNotImplemented
|
return ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,6 +63,7 @@ func (m *Mailbox) SetSubscribed(subscribed bool) error {
|
|||||||
// real time to complete. If a server implementation has no such housekeeping
|
// real time to complete. If a server implementation has no such housekeeping
|
||||||
// considerations, CHECK is equivalent to NOOP.
|
// considerations, CHECK is equivalent to NOOP.
|
||||||
func (m *Mailbox) Check() error {
|
func (m *Mailbox) Check() error {
|
||||||
|
log.Printf("Mailbox(%v).Check()", m.stored.ID)
|
||||||
return nil // FIXME: do we need to do anything here?
|
return nil // FIXME: do we need to do anything here?
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,38 +73,77 @@ func (m *Mailbox) Check() error {
|
|||||||
//
|
//
|
||||||
// Messages must be sent to ch. When the function returns, ch must be closed.
|
// 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 {
|
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)
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
|
|
||||||
// FIXME: TODO
|
var messages []store.Message
|
||||||
|
for _, set := range seqset.Set {
|
||||||
|
msgs, err := m.session.store.FindMessagesInRange(
|
||||||
|
m.stored, uint64(set.Start), uint64(set.Stop),
|
||||||
|
)
|
||||||
|
|
||||||
messages, err := m.session.store.FindMessages(m.session.Username(), m.name)
|
if err != nil && !store.IsNotFound(err) {
|
||||||
if err != nil {
|
log.Printf(" error: %#v", err)
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
messages = append(messages, msgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, _ := range messages {
|
log.Printf(" %v messages: %#v", len(messages), messages)
|
||||||
envelope := &imap.Envelope{}
|
for _, message := range messages {
|
||||||
|
envelope := &imap.Envelope{
|
||||||
|
// Date:
|
||||||
|
Subject: message.Header.Get("Subject"),
|
||||||
|
// From:
|
||||||
|
// Sender:
|
||||||
|
// ReplyTo:
|
||||||
|
// To:
|
||||||
|
// Cc:
|
||||||
|
// Bcc:
|
||||||
|
InReplyTo: message.Header.Get("In-Reply-To"),
|
||||||
|
MessageId: message.Header.Get("Message-Id"),
|
||||||
|
}
|
||||||
|
|
||||||
body := map[*imap.BodySectionName]imap.Literal{}
|
body := map[*imap.BodySectionName]imap.Literal{}
|
||||||
|
|
||||||
imapMsg := &imap.Message{
|
imapMsg := imap.NewMessage(uint32(message.ID), items)
|
||||||
SeqNum: uint32(i + 1),
|
fillItems(imapMsg, message)
|
||||||
Envelope: envelope,
|
imapMsg.Envelope = envelope
|
||||||
Uid: uint32(i + 1),
|
imapMsg.Body = body
|
||||||
Body: body,
|
imapMsg.Uid = uint32(message.ID)
|
||||||
}
|
imapMsg.Size = uint32(len(message.EML))
|
||||||
|
|
||||||
fmt.Printf("Sending message %#v", imapMsg)
|
|
||||||
|
|
||||||
|
log.Printf(" Sending message %#v", imapMsg)
|
||||||
ch <- imapMsg
|
ch <- imapMsg
|
||||||
|
log.Printf(" Sent message")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fillItems(imapMsg *imap.Message, msg store.Message) {
|
||||||
|
for k, _ := range imapMsg.Items {
|
||||||
|
switch k {
|
||||||
|
case "UID":
|
||||||
|
imapMsg.Items[k] = msg.ID
|
||||||
|
case "FLAGS":
|
||||||
|
imapMsg.Items[k] = []string{}
|
||||||
|
case "RFC822.SIZE":
|
||||||
|
imapMsg.Items[k] = uint32(len(msg.EML))
|
||||||
|
// FIXME: Actually send *just* the header...
|
||||||
|
case "RFC822.HEADER":
|
||||||
|
imapMsg.Items[k] = string(msg.EML)
|
||||||
|
default:
|
||||||
|
log.Printf("Unknown item: %v", k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SearchMessages searches messages. The returned list must contain UIDs if
|
// SearchMessages searches messages. The returned list must contain UIDs if
|
||||||
// uid is set to true, or sequence numbers otherwise.
|
// uid is set to true, or sequence numbers otherwise.
|
||||||
func (m *Mailbox) SearchMessages(uid bool, criteria *imap.SearchCriteria) ([]uint32, error) {
|
func (m *Mailbox) SearchMessages(uid bool, criteria *imap.SearchCriteria) ([]uint32, error) {
|
||||||
|
log.Printf("Mailbox(%v).SearchMessages(): not implemented", m.stored.ID)
|
||||||
return nil, ErrNotImplemented
|
return nil, ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,6 +154,7 @@ func (m *Mailbox) SearchMessages(uid bool, criteria *imap.SearchCriteria) ([]uin
|
|||||||
// If the Backend implements Updater, it must notify the client immediately
|
// If the Backend implements Updater, it must notify the client immediately
|
||||||
// via a mailbox update.
|
// via a mailbox update.
|
||||||
func (m *Mailbox) CreateMessage(flags []string, date time.Time, body imap.Literal) error {
|
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)
|
||||||
return ErrNotImplemented
|
return ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,6 +163,7 @@ func (m *Mailbox) CreateMessage(flags []string, date time.Time, body imap.Litera
|
|||||||
// If the Backend implements Updater, it must notify the client immediately
|
// If the Backend implements Updater, it must notify the client immediately
|
||||||
// via a message update.
|
// via a message update.
|
||||||
func (m *Mailbox) UpdateMessagesFlags(uid bool, seqset *imap.SeqSet, operation imap.FlagsOp, flags []string) error {
|
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)
|
||||||
return ErrNotImplemented
|
return ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,6 +177,7 @@ func (m *Mailbox) UpdateMessagesFlags(uid bool, seqset *imap.SeqSet, operation i
|
|||||||
// If the Backend implements Updater, it must notify the client immediately
|
// If the Backend implements Updater, it must notify the client immediately
|
||||||
// via a mailbox update.
|
// via a mailbox update.
|
||||||
func (m *Mailbox) CopyMessages(uid bool, seqset *imap.SeqSet, dest string) error {
|
func (m *Mailbox) CopyMessages(uid bool, seqset *imap.SeqSet, dest string) error {
|
||||||
|
log.Printf("Mailbox(%v).CopyMessages(...): not implemented", m.stored.ID)
|
||||||
return ErrNotImplemented
|
return ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,5 +187,6 @@ func (m *Mailbox) CopyMessages(uid bool, seqset *imap.SeqSet, dest string) error
|
|||||||
// If the Backend implements Updater, it must notify the client immediately
|
// If the Backend implements Updater, it must notify the client immediately
|
||||||
// via an expunge update.
|
// via an expunge update.
|
||||||
func (m *Mailbox) Expunge() error {
|
func (m *Mailbox) Expunge() error {
|
||||||
|
log.Printf("Mailbox(%v).CopyMessages(): not implemented", m.stored.ID)
|
||||||
return ErrNotImplemented
|
return ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
@@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/emersion/go-imap"
|
|
||||||
imapbackend "github.com/emersion/go-imap/backend"
|
imapbackend "github.com/emersion/go-imap/backend"
|
||||||
|
|
||||||
"ur.gs/crockery/internal/store"
|
"ur.gs/crockery/internal/store"
|
||||||
@@ -28,28 +27,43 @@ func (s *Session) Username() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) ListMailboxes(subscribed bool) ([]imapbackend.Mailbox, error) {
|
func (s *Session) ListMailboxes(subscribed bool) ([]imapbackend.Mailbox, error) {
|
||||||
return []imapbackend.Mailbox{ // FIXME: hardcoded!
|
mailboxes, err := s.store.FindMailboxes(s.Account)
|
||||||
&Mailbox{name: imap.InboxName, session: s},
|
log.Printf("Session(%v).ListMailboxes(%v): %#v %#v", s.ID, subscribed, mailboxes, err)
|
||||||
}, nil
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
imapped := make([]imapbackend.Mailbox, len(mailboxes))
|
||||||
|
for i, mailbox := range mailboxes {
|
||||||
|
imapped[i] = &Mailbox{stored: mailbox, session: s}
|
||||||
|
}
|
||||||
|
|
||||||
|
return imapped, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) GetMailbox(name string) (imapbackend.Mailbox, error) {
|
func (s *Session) GetMailbox(name string) (imapbackend.Mailbox, error) {
|
||||||
if name == imap.InboxName { // FIXME: hardcoded!
|
mailbox, err := s.store.FindMailbox(s.Account, name)
|
||||||
return &Mailbox{name: name, session: s}, nil
|
log.Printf("Session(%v).GetMailbox(%v): %#v %#v", s.ID, name, mailbox, err)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, ErrNotImplemented
|
return &Mailbox{stored: mailbox, session: s}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) CreateMailbox(name string) error {
|
func (s *Session) CreateMailbox(name string) error {
|
||||||
|
log.Printf("Session(%v).CreateMailbox(%v): not implemented", s.ID, name)
|
||||||
return ErrNotImplemented
|
return ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) DeleteMailbox(name string) error {
|
func (s *Session) DeleteMailbox(name string) error {
|
||||||
|
log.Printf("Session(%v).DeleteMailbox(%v): not implemented", s.ID, name)
|
||||||
return ErrNotImplemented
|
return ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) RenameMailbox(existingName, newName string) error {
|
func (s *Session) RenameMailbox(existingName, newName string) error {
|
||||||
|
log.Printf("Session(%v).DeleteMailbox(%v, %v): not implemented", s.ID, existingName, newName)
|
||||||
return ErrNotImplemented
|
return ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
package smtp
|
package smtp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,16 +15,10 @@ type Session struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) Send(from string, to []string, r io.Reader) error {
|
func (s *Session) Send(from string, to []string, r io.Reader) error {
|
||||||
// FIXME: TODO: don't keep this lot forever, it's for debugging purposes
|
log.Printf("session=%d from=%s to=%s", s.ID, from, to)
|
||||||
data, err := ioutil.ReadAll(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("session=%d from=%s to=%s r=<<EOF\n%s\nEOF", s.ID, from, to, string(data))
|
|
||||||
|
|
||||||
// TODO: a chain of middlewares here
|
// TODO: a chain of middlewares here
|
||||||
return s.Handler.ServeSMTP(from, to, bytes.NewReader(data))
|
return s.Handler.ServeSMTP(from, to, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) Logout() error {
|
func (s *Session) Logout() error {
|
||||||
|
@@ -29,6 +29,9 @@ type Account struct {
|
|||||||
|
|
||||||
// As generated by HashPassword
|
// As generated by HashPassword
|
||||||
PasswordHash string
|
PasswordHash string
|
||||||
|
|
||||||
|
// Where to put messages by default. FK: mailbox.id
|
||||||
|
DefaultMailbox uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
// HashPassword turns a plaintext password into a crypt()ed string, using bcrypt
|
// HashPassword turns a plaintext password into a crypt()ed string, using bcrypt
|
||||||
|
43
internal/store/mailbox.go
Normal file
43
internal/store/mailbox.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MailboxInterface interface {
|
||||||
|
CreateMailbox(*Mailbox) error
|
||||||
|
FindMailbox(account Account, name string) (Mailbox, error)
|
||||||
|
FindMailboxes(account Account) ([]Mailbox, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Mailbox struct {
|
||||||
|
ID uint64 `storm:"id,increment"`
|
||||||
|
Account string `storm:"index"` // FK accounts.username
|
||||||
|
Name string `storm:"index"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *concrete) CreateMailbox(mailbox *Mailbox) error {
|
||||||
|
return c.storm.Save(mailbox)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *concrete) FindMailbox(account Account, name string) (Mailbox, error) {
|
||||||
|
// FIXME I don't know how to storm
|
||||||
|
candidates, err := c.FindMailboxes(account)
|
||||||
|
if err != nil {
|
||||||
|
return Mailbox{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, mbox := range candidates {
|
||||||
|
if mbox.Name == name {
|
||||||
|
return mbox, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Mailbox{}, errors.New("not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *concrete) FindMailboxes(account Account) ([]Mailbox, error) {
|
||||||
|
var out []Mailbox
|
||||||
|
|
||||||
|
return out, c.storm.Find("Account", account.Username, &out)
|
||||||
|
}
|
@@ -2,41 +2,49 @@ package store
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/mail"
|
"net/mail"
|
||||||
|
|
||||||
|
"github.com/asdine/storm/q"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MessageInterface interface {
|
type MessageInterface interface {
|
||||||
CreateMessage(Message) error
|
CreateMessage(Message) error
|
||||||
FindMessages(string, string) ([]Message, error)
|
FindMessages(Mailbox) ([]Message, error)
|
||||||
|
|
||||||
|
// if end == 0, it means "from start to most recent"
|
||||||
|
FindMessagesInRange(mailbox Mailbox, start, end uint64) ([]Message, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
ID string
|
// FIXME: this should be per-mailbox, IMAP "only" allows 32-bit message UIDs?
|
||||||
Username string `storm:"index"` // FK accounts.username
|
ID uint64 `storm:"id,increment"`
|
||||||
Mailbox string `storm:"index"` // The mailbox, e.g. `INBOX` or `Foo/Bar`
|
|
||||||
|
|
||||||
Header mail.Header
|
Mailbox uint64 `storm:"index"` // FK mailbox.id
|
||||||
Body []byte
|
|
||||||
|
Header mail.Header // The parsed headers
|
||||||
|
EML []byte // The full email, unaltered
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *concrete) CreateMessage(message Message) error {
|
func (c *concrete) CreateMessage(message Message) error {
|
||||||
return c.storm.Save(&message)
|
return c.storm.Save(&message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *concrete) FindMessages(username string, mailbox string) ([]Message, error) {
|
func (c *concrete) FindMessages(mailbox Mailbox) ([]Message, error) {
|
||||||
var messages []Message
|
|
||||||
var out []Message
|
var out []Message
|
||||||
|
|
||||||
err := c.storm.Find("Username", username, &messages)
|
return out, c.storm.Find("Mailbox", mailbox.ID, &out)
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
|
||||||
}
|
func (c *concrete) FindMessagesInRange(mailbox Mailbox, start, end uint64) ([]Message, error) {
|
||||||
|
var out []Message
|
||||||
// FIXME: I cannot storm, so I just filter out all non-mailbox messages
|
|
||||||
for _, message := range messages {
|
matchers := []q.Matcher{
|
||||||
if message.Mailbox == mailbox {
|
q.Eq("Mailbox", mailbox.ID),
|
||||||
out = append(out, message)
|
q.Gte("ID", start),
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
if end > 0 {
|
||||||
return out, nil
|
matchers = append(matchers, q.Lte("ID", end))
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, c.storm.Select(matchers...).Find(&out)
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,10 @@
|
|||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,31 +15,26 @@ type SpoolInterface interface {
|
|||||||
// FIXME: we don't actually spool for now, we just store it to each user's
|
// FIXME: we don't actually spool for now, we just store it to each user's
|
||||||
// mailbox. This might lead to oddness in the partial delivery case.
|
// mailbox. This might lead to oddness in the partial delivery case.
|
||||||
func (c *concrete) SpoolMessage(recipients []Account, r io.Reader) error {
|
func (c *concrete) SpoolMessage(recipients []Account, r io.Reader) error {
|
||||||
message, err := mail.ReadMessage(r)
|
data, err := ioutil.ReadAll(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: we can assign our own ID in this case
|
message, err := mail.ReadMessage(bytes.NewReader(data))
|
||||||
mid := message.Header.Get("Message-ID")
|
|
||||||
if mid == "" {
|
|
||||||
return fmt.Errorf("No Message-ID for this email")
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(message.Body)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, recipient := range recipients {
|
for _, recipient := range recipients {
|
||||||
message := Message{
|
message := Message{
|
||||||
ID: mid,
|
Mailbox: recipient.DefaultMailbox,
|
||||||
Username: recipient.Username,
|
Header: message.Header,
|
||||||
Mailbox: "INBOX", // FIXME: at some point this will be changeable
|
EML: data,
|
||||||
Header: message.Header,
|
|
||||||
Body: body,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Creating email: %#v", message)
|
||||||
|
log.Print(string(data))
|
||||||
|
|
||||||
if err := c.CreateMessage(message); err != nil {
|
if err := c.CreateMessage(message); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@@ -19,12 +19,17 @@ type Interface interface {
|
|||||||
SetTLS([]byte, []byte) error
|
SetTLS([]byte, []byte) error
|
||||||
|
|
||||||
AccountInterface
|
AccountInterface
|
||||||
|
MailboxInterface
|
||||||
MessageInterface
|
MessageInterface
|
||||||
SpoolInterface
|
SpoolInterface
|
||||||
|
|
||||||
io.Closer
|
io.Closer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsNotFound(err error) bool {
|
||||||
|
return err.Error() == "not found" // Magic hardcoded value in storm
|
||||||
|
}
|
||||||
|
|
||||||
func New(ctx context.Context, filename string) (Interface, error) {
|
func New(ctx context.Context, filename string) (Interface, error) {
|
||||||
db, err := storm.Open(filename, storm.BoltOptions(0600, &bolt.Options{}))
|
db, err := storm.Open(filename, storm.BoltOptions(0600, &bolt.Options{}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Reference in New Issue
Block a user