aboutsummaryrefslogtreecommitdiffstats
path: root/worker/mbox/io.go
blob: 22d0d02378a83e12380c40d26bccac4657560e25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package mboxer

import (
	"errors"
	"io"
	"time"

	"git.sr.ht/~rjarry/aerc/lib/rfc822"
	"git.sr.ht/~rjarry/aerc/models"
	"github.com/emersion/go-mbox"
)

func Read(r io.Reader) ([]rfc822.RawMessage, error) {
	mbr := mbox.NewReader(r)
	messages := make([]rfc822.RawMessage, 0)
	for {
		msg, err := mbr.NextMessage()
		if errors.Is(err, io.EOF) {
			break
		} else if err != nil {
			return nil, err
		}

		content, err := io.ReadAll(msg)
		if err != nil {
			return nil, err
		}

		messages = append(messages, &message{
			uid:     uidFromContents(content),
			flags:   models.SeenFlag,
			content: content,
		})
	}
	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()
}