aboutsummaryrefslogtreecommitdiffstats
path: root/lib/emlview.go
blob: e0dcb4362b4487c0d4e07c861675248d527fe526 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package lib

import (
	"bytes"
	"io"

	"github.com/ProtonMail/go-crypto/openpgp"
	_ "github.com/emersion/go-message/charset"

	"git.sr.ht/~rjarry/aerc/lib/crypto"
	"git.sr.ht/~rjarry/aerc/models"
	"git.sr.ht/~rjarry/aerc/worker/lib"
)

// EmlMessage implements the RawMessage interface
type EmlMessage []byte

func (fm *EmlMessage) NewReader() (io.ReadCloser, error) {
	return io.NopCloser(bytes.NewReader(*fm)), nil
}

func (fm *EmlMessage) UID() uint32 {
	return 0xFFFFFFF
}

func (fm *EmlMessage) Labels() ([]string, error) {
	return nil, nil
}

func (fm *EmlMessage) ModelFlags() ([]models.Flag, error) {
	return []models.Flag{models.SeenFlag}, nil
}

// NewEmlMessageView provides a MessageView for a full message that is not
// stored in a message store
func NewEmlMessageView(full []byte, pgp crypto.Provider,
	decryptKeys openpgp.PromptFunction, cb func(MessageView, error),
) {
	eml := EmlMessage(full)
	messageInfo, err := lib.MessageInfo(&eml)
	if err != nil {
		cb(nil, err)
		return
	}
	msv := &MessageStoreView{
		messageInfo:   messageInfo,
		messageStore:  nil,
		message:       full,
		details:       nil,
		bodyStructure: nil,
		setSeen:       false,
	}

	if usePGP(messageInfo.BodyStructure) {
		reader := lib.NewCRLFReader(bytes.NewReader(full))
		md, err := pgp.Decrypt(reader, decryptKeys)
		if err != nil {
			cb(nil, err)
			return
		}
		msv.details = md
		msv.message, err = io.ReadAll(md.Body)
		if err != nil {
			cb(nil, err)
			return
		}
	}
	entity, err := lib.ReadMessage(bytes.NewBuffer(msv.message))
	if err != nil {
		cb(nil, err)
		return
	}
	bs, err := lib.ParseEntityStructure(entity)
	if err != nil {
		cb(nil, err)
		return
	}
	msv.bodyStructure = bs
	cb(msv, nil)
}