aboutsummaryrefslogtreecommitdiffstats
path: root/worker/jmap/worker.go
blob: 0b1c57aeb06c7b3af834d726b018d65b828e20dc (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package jmap

import (
	"errors"
	"net/url"
	"time"

	"git.sr.ht/~rjarry/aerc/config"
	"git.sr.ht/~rjarry/aerc/lib/uidstore"
	"git.sr.ht/~rjarry/aerc/models"
	"git.sr.ht/~rjarry/aerc/worker/handlers"
	"git.sr.ht/~rjarry/aerc/worker/jmap/cache"
	"git.sr.ht/~rjarry/aerc/worker/types"
	"git.sr.ht/~rockorager/go-jmap"
	"git.sr.ht/~rockorager/go-jmap/mail/identity"
	"git.sr.ht/~rockorager/go-jmap/mail/mailbox"
)

func init() {
	handlers.RegisterWorkerFactory("jmap", NewJMAPWorker)
}

var (
	errNoop        error = errors.New("noop")
	errUnsupported error = errors.New("unsupported")
)

type JMAPWorker struct {
	config struct {
		account    *config.AccountConfig
		endpoint   string
		oauth      bool
		user       *url.Userinfo
		cacheState bool
		cacheBlobs bool
		serverPing time.Duration
		useLabels  bool
		allMail    string
	}

	w         *types.Worker
	client    *jmap.Client
	cache     *cache.JMAPCache
	accountId jmap.ID

	selectedMbox jmap.ID
	dir2mbox     map[string]jmap.ID
	mbox2dir     map[jmap.ID]string
	roles        map[mailbox.Role]jmap.ID
	identities   map[string]*identity.Identity
	uidStore     *uidstore.Store

	changes chan jmap.TypeState
	stop    chan struct{}
}

func NewJMAPWorker(worker *types.Worker) (types.Backend, error) {
	return &JMAPWorker{
		w:          worker,
		uidStore:   uidstore.NewStore(),
		roles:      make(map[mailbox.Role]jmap.ID),
		dir2mbox:   make(map[string]jmap.ID),
		mbox2dir:   make(map[jmap.ID]string),
		identities: make(map[string]*identity.Identity),
		changes:    make(chan jmap.TypeState),
	}, nil
}

func (w *JMAPWorker) addMbox(mbox *mailbox.Mailbox, dir string) {
	w.mbox2dir[mbox.ID] = dir
	w.dir2mbox[dir] = mbox.ID
	w.roles[mbox.Role] = mbox.ID
}

func (w *JMAPWorker) deleteMbox(id jmap.ID) {
	var dir string
	var role mailbox.Role

	delete(w.mbox2dir, id)
	for d, i := range w.dir2mbox {
		if i == id {
			dir = d
			break
		}
	}
	delete(w.dir2mbox, dir)
	for r, i := range w.roles {
		if i == id {
			role = r
			break
		}
	}
	delete(w.roles, role)
}

var capas = models.Capabilities{Sort: true, Thread: false}

func (w *JMAPWorker) Capabilities() *models.Capabilities {
	return &capas
}

func (w *JMAPWorker) PathSeparator() string {
	return "/"
}

func (w *JMAPWorker) handleMessage(msg types.WorkerMessage) error {
	switch msg := msg.(type) {
	case *types.Configure:
		return w.handleConfigure(msg)
	case *types.Connect:
		if w.stop != nil {
			return errors.New("already connected")
		}
		return w.handleConnect(msg)
	case *types.Reconnect:
		if w.stop == nil {
			return errors.New("not connected")
		}
		close(w.stop)
		return w.handleConnect(&types.Connect{Message: msg.Message})
	case *types.Disconnect:
		if w.stop == nil {
			return errors.New("not connected")
		}
		close(w.stop)
		return nil
	case *types.ListDirectories:
		return w.handleListDirectories(msg)
	case *types.OpenDirectory:
		return w.handleOpenDirectory(msg)
	case *types.FetchDirectoryContents:
		return w.handleFetchDirectoryContents(msg)
	case *types.SearchDirectory:
		return w.handleSearchDirectory(msg)
	case *types.CreateDirectory:
		return w.handleCreateDirectory(msg)
	case *types.RemoveDirectory:
		return w.handleRemoveDirectory(msg)
	case *types.FetchMessageHeaders:
		return w.handleFetchMessageHeaders(msg)
	case *types.FetchMessageBodyPart:
		return w.handleFetchMessageBodyPart(msg)
	case *types.FetchFullMessages:
		return w.handleFetchFullMessages(msg)
	case *types.FlagMessages:
		return w.updateFlags(msg.Uids, msg.Flags, msg.Enable)
	case *types.AnsweredMessages:
		return w.updateFlags(msg.Uids, models.AnsweredFlag, msg.Answered)
	case *types.DeleteMessages:
		return w.moveCopy(msg.Uids, "", true)
	case *types.CopyMessages:
		return w.moveCopy(msg.Uids, msg.Destination, false)
	case *types.MoveMessages:
		return w.moveCopy(msg.Uids, msg.Destination, true)
	case *types.ModifyLabels:
		if w.config.useLabels {
			return w.handleModifyLabels(msg)
		}
	case *types.AppendMessage:
		return w.handleAppendMessage(msg)
	case *types.StartSendingMessage:
		return w.handleStartSend(msg)
	}
	return errUnsupported
}

func (w *JMAPWorker) Run() {
	for {
		select {
		case change := <-w.changes:
			err := w.refresh(change)
			if err != nil {
				w.w.Errorf("refresh: %s", err)
			}
		case msg := <-w.w.Actions():
			msg = w.w.ProcessAction(msg)
			err := w.handleMessage(msg)
			switch {
			case errors.Is(err, errNoop):
				// Operation did not have any effect.
				// Do *NOT* send a Done message.
				break
			case errors.Is(err, errUnsupported):
				w.w.PostMessage(&types.Unsupported{
					Message: types.RespondTo(msg),
				}, nil)
			case err != nil:
				w.w.PostMessage(&types.Error{
					Message: types.RespondTo(msg),
					Error:   err,
				}, nil)
			default: // err == nil
				// Operation is finished.
				// Send a Done message.
				w.w.PostMessage(&types.Done{
					Message: types.RespondTo(msg),
				}, nil)
			}
		}
	}
}