aerc/worker/mbox/io.go
Koni Marti a1a276e002 mbox: implement an mbox backend worker
Implement an mbox backend worker. Worker can be used for testing and
development by mocking a backend for the message store. Worker does not
modify the actual mbox file on disk; all operations are performed in
memory.

To use the mbox backend, create an mbox account in the accounts.conf
where the source uses the "mbox://" scheme, such as

source = mbox://~/mbox/

or

source = mbox://~/mbox/file.mbox

If the mbox source points to a directory, all files in this directory
with the .mbox suffix will be opened as folders.

If an outgoing smtp server is defined for the mbox account, replies can
be sent to emails that are stored in the mbox file.

Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
2022-07-14 23:14:45 +02:00

51 lines
909 B
Go

package mboxer
import (
"io"
"io/ioutil"
"time"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/lib"
"github.com/emersion/go-mbox"
)
func Read(r io.Reader) ([]lib.RawMessage, error) {
mbr := mbox.NewReader(r)
uid := uint32(0)
messages := make([]lib.RawMessage, 0)
for {
msg, err := mbr.NextMessage()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
content, err := ioutil.ReadAll(msg)
if err != nil {
return nil, err
}
messages = append(messages, &message{
uid: uid, flags: []models.Flag{models.SeenFlag}, content: content,
})
uid++
}
return messages, nil
}
func Write(w io.Writer, reader io.Reader, from string, date time.Time) error {
wc := mbox.NewWriter(w)
mw, err := wc.CreateMessage(from, time.Now())
if err != nil {
return err
}
_, err = io.Copy(mw, reader)
if err != nil {
return err
}
return wc.Close()
}