Debian Codegraph @

server/backend/message.go

raw
1 package backend
2
3 import (
4 "net/mail"
5 "strings"
6 "time"
7
8 "github.com/ProtonMail/gluon/rfc822"
9 "github.com/bradenaw/juniper/xslices"
10 "github.com/google/uuid"
11 "github.com/henrybear327/go-proton-api"
12 "golang.org/x/exp/slices"
13 )
14
15 type message struct {
16 messageID string
17 externalID string
18 addrID string
19 labelIDs []string
20 attIDs []string
21 inReplyTo string
22
23 // sysLabel is the system label for the message.
24 // If nil, the message's flags are used to determine the system label (inbox, sent, drafts).
25 // If "", the message has no system label (e.g. is in a custom folder or all mail).
26 // If non-nil and non-empty, the message has the system label with the given ID (e.g. spam, trash).
27 sysLabel *string
28
29 subject string
30 sender *mail.Address
31 toList []*mail.Address
32 ccList []*mail.Address
33 bccList []*mail.Address
34 replytos []*mail.Address
35 date time.Time
36
37 armBody string
38 mimeType rfc822.MIMEType
39
40 flags proton.MessageFlag
41 unread bool
42 starred bool
43 }
44
45 func newMessage(
46 addrID string,
47 subject string,
48 sender *mail.Address,
49 toList, ccList, bccList, replytos []*mail.Address,
50 armBody string,
51 mimeType rfc822.MIMEType,
52 externalID string,
53 date time.Time,
54 ) *message {
55 return &message{
56 messageID: uuid.NewString(),
57 externalID: externalID,
58 addrID: addrID,
59 sysLabel: pointer(""),
60
61 subject: subject,
62 sender: sender,
63 toList: toList,
64 ccList: ccList,
65 bccList: bccList,
66 replytos: replytos,
67 date: date,
68
69 armBody: armBody,
70 mimeType: mimeType,
71 }
72 }
73
74 func newMessageFromSent(addrID, armBody string, msg *message) *message {
75 return &message{
76 messageID: uuid.NewString(),
77 externalID: msg.externalID,
78 addrID: addrID,
79 sysLabel: pointer(""),
80
81 subject: msg.subject,
82 sender: msg.sender,
83 toList: msg.toList,
84 ccList: msg.ccList,
85 bccList: nil, // BCC is not sent to the recipient
86 replytos: msg.replytos,
87 date: time.Now(),
88
89 armBody: armBody,
90 mimeType: msg.mimeType,
91 inReplyTo: msg.inReplyTo,
92 }
93 }
94
95 func newMessageFromTemplate(addrID string, template proton.DraftTemplate, parentRef string) *message {
96 return &message{
97 messageID: uuid.NewString(),
98 externalID: template.ExternalID,
99 addrID: addrID,
100 sysLabel: pointer(""),
101 inReplyTo: parentRef,
102
103 subject: template.Subject,
104 sender: template.Sender,
105 toList: template.ToList,
106 ccList: template.CCList,
107 bccList: template.BCCList,
108 unread: bool(template.Unread),
109
110 armBody: template.Body,
111 mimeType: template.MIMEType,
112 }
113 }
114
115 func (msg *message) toMessage(attData map[string][]byte, att map[string]*attachment) proton.Message {
116 return proton.Message{
117 MessageMetadata: msg.toMetadata(attData, att),
118
119 Header: msg.getHeader(),
120 ParsedHeaders: msg.getParsedHeaders(),
121 Body: msg.armBody,
122 MIMEType: msg.mimeType,
123 Attachments: xslices.Map(msg.attIDs, func(attID string) proton.Attachment {
124 return att[attID].toAttachment()
125 }),
126 }
127 }
128
129 func (msg *message) getLabelIDs() []string {
130 labelIDs := []string{proton.AllMailLabel}
131
132 if msg.flags.HasAny(proton.MessageFlagSent, proton.MessageFlagScheduledSend) {
133 labelIDs = append(labelIDs, proton.AllSentLabel)
134 }
135
136 if !msg.flags.HasAny(proton.MessageFlagSent, proton.MessageFlagScheduledSend, proton.MessageFlagReceived) {
137 labelIDs = append(labelIDs, proton.AllDraftsLabel)
138 }
139
140 if msg.starred {
141 labelIDs = append(labelIDs, proton.StarredLabel)
142 }
143
144 if msg.sysLabel != nil {
145 if *msg.sysLabel != "" {
146 labelIDs = append(labelIDs, *msg.sysLabel)
147 }
148 } else {
149 switch {
150 case msg.flags.Has(proton.MessageFlagReceived):
151 labelIDs = append(labelIDs, proton.InboxLabel)
152
153 case msg.flags.Has(proton.MessageFlagSent):
154 labelIDs = append(labelIDs, proton.SentLabel)
155
156 case msg.flags.Has(proton.MessageFlagScheduledSend):
157 labelIDs = append(labelIDs, proton.AllScheduledLabel)
158
159 default:
160 labelIDs = append(labelIDs, proton.DraftsLabel)
161 }
162 }
163
164 return labelIDs
165 }
166
167 func (msg *message) toMetadata(attData map[string][]byte, att map[string]*attachment) proton.MessageMetadata {
168 labelIDs := msg.getLabelIDs()
169
170 messageSize := len(msg.armBody)
171 for _, a := range msg.attIDs {
172 messageSize += len(attData[att[a].attDataID])
173 }
174
175 return proton.MessageMetadata{
176 ID: msg.messageID,
177 ExternalID: msg.externalID,
178 AddressID: msg.addrID,
179 LabelIDs: append(msg.labelIDs, labelIDs...),
180
181 Subject: msg.subject,
182 Sender: msg.sender,
183 ToList: msg.toList,
184 CCList: msg.ccList,
185 BCCList: msg.bccList,
186 ReplyTos: msg.replytos,
187 Size: messageSize,
188
189 Flags: msg.flags,
190 Unread: proton.Bool(msg.unread),
191
192 NumAttachments: len(attData),
193 }
194 }
195
196 func (msg *message) getHeader() string {
197 builder := new(strings.Builder)
198
199 builder.WriteString("Subject: " + msg.subject + "\r\n")
200
201 if msg.sender != nil && (msg.sender.Name != "" || msg.sender.Address != "") {
202 builder.WriteString("From: " + msg.sender.String() + "\r\n")
203 }
204
205 if len(msg.toList) > 0 {
206 builder.WriteString("To: " + toAddressList(msg.toList) + "\r\n")
207 }
208
209 if len(msg.ccList) > 0 {
210 builder.WriteString("Cc: " + toAddressList(msg.ccList) + "\r\n")
211 }
212
213 if len(msg.bccList) > 0 {
214 builder.WriteString("Bcc: " + toAddressList(msg.bccList) + "\r\n")
215 }
216
217 if msg.mimeType != "" {
218 builder.WriteString("Content-Type: " + string(msg.mimeType) + "\r\n")
219 }
220
221 if len(msg.inReplyTo) > 0 {
222 builder.WriteString("References: " + msg.inReplyTo + "\r\n")
223 }
224
225 if msg.inReplyTo != "" {
226 builder.WriteString("In-Reply-To: " + msg.inReplyTo + "\r\n")
227 }
228
229 builder.WriteString("Date: " + msg.date.Format(time.RFC822) + "\r\n")
230
231 return builder.String()
232 }
233
234 func (msg *message) getParsedHeaders() proton.Headers {
235 header, err := rfc822.NewHeader([]byte(msg.getHeader()))
236 if err != nil {
237 panic(err)
238 }
239
240 parsed := make(proton.Headers)
241
242 header.Entries(func(key, value string) {
243 parsed[key] = append(parsed[key], value)
244 })
245
246 return parsed
247 }
248
249 // applyChanges will apply non-nil field from passed message.
250 //
251 // NOTE: This is not feature complete. It might panic on non-implemented changes.
252 func (msg *message) applyChanges(changes proton.DraftTemplate) {
253 if changes.Subject != "" {
254 msg.subject = changes.Subject
255 }
256
257 if changes.Sender != nil {
258 msg.sender = changes.Sender
259 }
260
261 if changes.ToList != nil {
262 msg.toList = append([]*mail.Address{}, changes.ToList...)
263 }
264
265 if changes.CCList != nil {
266 msg.ccList = append([]*mail.Address{}, changes.CCList...)
267 }
268
269 if changes.BCCList != nil {
270 msg.bccList = append([]*mail.Address{}, changes.BCCList...)
271 }
272
273 if changes.Body != "" {
274 msg.armBody = changes.Body
275 }
276
277 if changes.MIMEType != "" {
278 msg.mimeType = changes.MIMEType
279 }
280
281 if changes.ExternalID != "" {
282 msg.externalID = changes.ExternalID
283 }
284 }
285
286 func (msg *message) addLabel(labelID string, labels map[string]*label) {
287 switch labelID {
288 case proton.InboxLabel, proton.SentLabel, proton.DraftsLabel, proton.AllScheduledLabel:
289 msg.addFlagLabel(labelID, labels)
290
291 case proton.TrashLabel, proton.SpamLabel, proton.ArchiveLabel:
292 msg.addSystemLabel(labelID, labels)
293
294 case proton.StarredLabel:
295 msg.starred = true
296
297 default:
298 if label, ok := labels[labelID]; ok {
299 msg.addUserLabel(label, labels)
300 }
301 }
302 }
303
304 func (msg *message) addFlagLabel(labelID string, labels map[string]*label) {
305 msg.labelIDs = xslices.Filter(msg.labelIDs, func(otherLabelID string) bool {
306 return labels[otherLabelID].labelType == proton.LabelTypeLabel
307 })
308
309 msg.sysLabel = nil
310 }
311
312 func (msg *message) addSystemLabel(labelID string, labels map[string]*label) {
313 msg.labelIDs = xslices.Filter(msg.labelIDs, func(otherLabelID string) bool {
314 return labels[otherLabelID].labelType == proton.LabelTypeLabel
315 })
316
317 msg.sysLabel = &labelID
318 }
319
320 func (msg *message) addUserLabel(label *label, labels map[string]*label) {
321 if label.labelType != proton.LabelTypeLabel {
322 msg.labelIDs = xslices.Filter(msg.labelIDs, func(otherLabelID string) bool {
323 return labels[otherLabelID].labelType == proton.LabelTypeLabel
324 })
325
326 msg.sysLabel = pointer("")
327 }
328
329 if !slices.Contains(msg.labelIDs, label.labelID) {
330 msg.labelIDs = append(msg.labelIDs, label.labelID)
331 }
332 }
333
334 func (msg *message) remLabel(labelID string, labels map[string]*label) {
335 switch labelID {
336 case proton.InboxLabel, proton.SentLabel, proton.DraftsLabel, proton.AllScheduledLabel:
337 msg.remFlagLabel(labelID, labels)
338
339 case proton.TrashLabel, proton.SpamLabel, proton.ArchiveLabel:
340 msg.remSystemLabel(labelID, labels)
341
342 case proton.StarredLabel:
343 msg.starred = false
344
345 default:
346 if label, ok := labels[labelID]; ok {
347 msg.remUserLabel(label, labels)
348 }
349 }
350 }
351
352 func (msg *message) remFlagLabel(labelID string, labels map[string]*label) {
353 if msg.sysLabel == nil {
354 msg.sysLabel = pointer("")
355 }
356 }
357
358 func (msg *message) remSystemLabel(labelID string, labels map[string]*label) {
359 if msg.sysLabel != nil && *msg.sysLabel == labelID {
360 msg.sysLabel = pointer("")
361 }
362 }
363
364 func (msg *message) remUserLabel(label *label, labels map[string]*label) {
365 msg.labelIDs = xslices.Filter(msg.labelIDs, func(otherLabelID string) bool {
366 return otherLabelID != label.labelID
367 })
368 }
369
370 func toAddressList(addrs []*mail.Address) string {
371 res := make([]string, len(addrs))
372
373 for i, addr := range addrs {
374 res[i] = addr.String()
375 }
376
377 return strings.Join(res, ", ")
378 }
379
380 func pointer[T any](v T) *T {
381 return &v
382 }