aboutsummaryrefslogtreecommitdiffstats
path: root/lib/emlview.go
blob: e27edcb02c9284d364b76b7c44b0f9c90e1fd030 (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/lib/rfc822"
	"git.sr.ht/~rjarry/aerc/models"
)

// 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.Flags, error) {
	return 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 := rfc822.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 := rfc822.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 := rfc822.ReadMessage(bytes.NewBuffer(msv.message))
	if err != nil {
		cb(nil, err)
		return
	}
	bs, err := rfc822.ParseEntityStructure(entity)
	if err != nil {
		cb(nil, err)
		return
	}
	msv.bodyStructure = bs
	cb(msv, nil)
}