2022-07-11 20:11:18 +02:00
|
|
|
package mboxer
|
|
|
|
|
|
|
|
import (
|
2022-07-31 15:15:27 +02:00
|
|
|
"errors"
|
2022-07-11 20:11:18 +02:00
|
|
|
"io"
|
|
|
|
"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()
|
2022-07-31 15:15:27 +02:00
|
|
|
if errors.Is(err, io.EOF) {
|
2022-07-11 20:11:18 +02:00
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:19:45 +02:00
|
|
|
content, err := io.ReadAll(msg)
|
2022-07-11 20:11:18 +02:00
|
|
|
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()
|
|
|
|
}
|