Extremely WIP, but now messages appear in the evolution list pane

This commit is contained in:
2018-03-09 03:20:22 +00:00
parent b661b4a978
commit 3c52b23723
10 changed files with 211 additions and 75 deletions

9
Makefile Normal file
View 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

View File

@@ -9,6 +9,7 @@ import (
"gopkg.in/urfave/cli.v1"
"ur.gs/crockery/internal/imap"
"ur.gs/crockery/internal/store"
)
@@ -69,13 +70,24 @@ func crockeryInit(c *cli.Context) error {
return err
}
postmaster := &store.Account{
Username: "postmaster@" + domain,
Admin: true,
PasswordHash: hash,
username := "postmaster@" + domain
mailbox := store.Mailbox{
Account: username,
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)
}

View File

@@ -1,27 +1,34 @@
package imap
import (
"fmt"
"log"
"time"
"github.com/emersion/go-imap"
"ur.gs/crockery/internal/store"
)
var (
InboxName = imap.InboxName
)
type Mailbox struct {
name string
stored store.Mailbox
session *Session
}
// Name returns this mailbox name.
func (m *Mailbox) Name() string {
return m.name
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{
Attributes: []string{}, // FIXME: what do we need here?
Delimiter: "/",
Delimiter: ".",
Name: m.Name(),
}, 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
// 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)
return &imap.MailboxStatus{
Name: m.Name(),
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"
// or "subscribed" mailboxes.
func (m *Mailbox) SetSubscribed(subscribed bool) error {
log.Printf("Mailbox(%v).SetSubscribed(%#v): not implemented", m.stored.ID, subscribed)
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
// considerations, CHECK is equivalent to NOOP.
func (m *Mailbox) Check() error {
log.Printf("Mailbox(%v).Check()", m.stored.ID)
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.
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)
// 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 {
return err
if err != nil && !store.IsNotFound(err) {
log.Printf(" error: %#v", err)
return err
}
messages = append(messages, msgs...)
}
for i, _ := range messages {
envelope := &imap.Envelope{}
log.Printf(" %v messages: %#v", len(messages), messages)
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{}
imapMsg := &imap.Message{
SeqNum: uint32(i + 1),
Envelope: envelope,
Uid: uint32(i + 1),
Body: body,
}
fmt.Printf("Sending message %#v", imapMsg)
imapMsg := imap.NewMessage(uint32(message.ID), items)
fillItems(imapMsg, message)
imapMsg.Envelope = envelope
imapMsg.Body = body
imapMsg.Uid = uint32(message.ID)
imapMsg.Size = uint32(len(message.EML))
log.Printf(" Sending message %#v", imapMsg)
ch <- imapMsg
log.Printf(" Sent message")
}
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
// 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)
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
// 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)
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
// 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)
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
// 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)
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
// via an expunge update.
func (m *Mailbox) Expunge() error {
log.Printf("Mailbox(%v).CopyMessages(): not implemented", m.stored.ID)
return ErrNotImplemented
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"log"
"github.com/emersion/go-imap"
imapbackend "github.com/emersion/go-imap/backend"
"ur.gs/crockery/internal/store"
@@ -28,28 +27,43 @@ func (s *Session) Username() string {
}
func (s *Session) ListMailboxes(subscribed bool) ([]imapbackend.Mailbox, error) {
return []imapbackend.Mailbox{ // FIXME: hardcoded!
&Mailbox{name: imap.InboxName, session: s},
}, nil
mailboxes, err := s.store.FindMailboxes(s.Account)
log.Printf("Session(%v).ListMailboxes(%v): %#v %#v", s.ID, subscribed, mailboxes, err)
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) {
if name == imap.InboxName { // FIXME: hardcoded!
return &Mailbox{name: name, session: s}, nil
mailbox, err := s.store.FindMailbox(s.Account, name)
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 {
log.Printf("Session(%v).CreateMailbox(%v): not implemented", s.ID, name)
return ErrNotImplemented
}
func (s *Session) DeleteMailbox(name string) error {
log.Printf("Session(%v).DeleteMailbox(%v): not implemented", s.ID, name)
return ErrNotImplemented
}
func (s *Session) RenameMailbox(existingName, newName string) error {
log.Printf("Session(%v).DeleteMailbox(%v, %v): not implemented", s.ID, existingName, newName)
return ErrNotImplemented
}

View File

@@ -1,9 +1,7 @@
package smtp
import (
"bytes"
"io"
"io/ioutil"
"log"
)
@@ -17,16 +15,10 @@ type Session struct {
}
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
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))
log.Printf("session=%d from=%s to=%s", s.ID, from, to)
// 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 {

View File

@@ -29,6 +29,9 @@ type Account struct {
// As generated by HashPassword
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

43
internal/store/mailbox.go Normal file
View 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)
}

View File

@@ -2,41 +2,49 @@ package store
import (
"net/mail"
"github.com/asdine/storm/q"
)
type MessageInterface interface {
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 {
ID string
Username string `storm:"index"` // FK accounts.username
Mailbox string `storm:"index"` // The mailbox, e.g. `INBOX` or `Foo/Bar`
// FIXME: this should be per-mailbox, IMAP "only" allows 32-bit message UIDs?
ID uint64 `storm:"id,increment"`
Header mail.Header
Body []byte
Mailbox uint64 `storm:"index"` // FK mailbox.id
Header mail.Header // The parsed headers
EML []byte // The full email, unaltered
}
func (c *concrete) CreateMessage(message Message) error {
return c.storm.Save(&message)
}
func (c *concrete) FindMessages(username string, mailbox string) ([]Message, error) {
var messages []Message
func (c *concrete) FindMessages(mailbox Mailbox) ([]Message, error) {
var out []Message
err := c.storm.Find("Username", username, &messages)
if err != nil {
return nil, err
}
// FIXME: I cannot storm, so I just filter out all non-mailbox messages
for _, message := range messages {
if message.Mailbox == mailbox {
out = append(out, message)
}
}
return out, nil
return out, c.storm.Find("Mailbox", mailbox.ID, &out)
}
func (c *concrete) FindMessagesInRange(mailbox Mailbox, start, end uint64) ([]Message, error) {
var out []Message
matchers := []q.Matcher{
q.Eq("Mailbox", mailbox.ID),
q.Gte("ID", start),
}
if end > 0 {
matchers = append(matchers, q.Lte("ID", end))
}
return out, c.storm.Select(matchers...).Find(&out)
}

View File

@@ -1,9 +1,10 @@
package store
import (
"fmt"
"bytes"
"io"
"io/ioutil"
"log"
"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
// mailbox. This might lead to oddness in the partial delivery case.
func (c *concrete) SpoolMessage(recipients []Account, r io.Reader) error {
message, err := mail.ReadMessage(r)
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
// FIXME: we can assign our own ID in this case
mid := message.Header.Get("Message-ID")
if mid == "" {
return fmt.Errorf("No Message-ID for this email")
}
body, err := ioutil.ReadAll(message.Body)
message, err := mail.ReadMessage(bytes.NewReader(data))
if err != nil {
return err
}
for _, recipient := range recipients {
message := Message{
ID: mid,
Username: recipient.Username,
Mailbox: "INBOX", // FIXME: at some point this will be changeable
Header: message.Header,
Body: body,
Mailbox: recipient.DefaultMailbox,
Header: message.Header,
EML: data,
}
log.Printf("Creating email: %#v", message)
log.Print(string(data))
if err := c.CreateMessage(message); err != nil {
return err
}

View File

@@ -19,12 +19,17 @@ type Interface interface {
SetTLS([]byte, []byte) error
AccountInterface
MailboxInterface
MessageInterface
SpoolInterface
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) {
db, err := storm.Open(filename, storm.BoltOptions(0600, &bolt.Options{}))
if err != nil {