-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
86 lines (76 loc) · 1.76 KB
/
mail.go
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
package model
import (
"fmt"
"html"
"strings"
"time"
"github.com/microcosm-cc/bluemonday"
)
// Special mail label IDs
const (
MailLabelAll = 1<<31 - 1
MailLabelNone = 0
MailLabelInbox = 1
MailLabelSent = 2
MailLabelCorp = 4
MailLabelAlliance = 8
)
// A mail label for an Eve mail belonging to a character.
type MailLabel struct {
ID int64
MyCharacterID int32
Color string
LabelID int32
Name string
UnreadCount int
}
var bodyPolicy = bluemonday.StrictPolicy()
// An Eve mail belonging to a character.
type Mail struct {
Body string
MyCharacterID int32
From *EveEntity
Labels []*MailLabel
IsRead bool
ID int64
MailID int32
Recipients []*EveEntity
Subject string
Timestamp time.Time
}
// BodyPlain returns a mail's body as plain text.
func (m *Mail) BodyPlain() string {
t := strings.ReplaceAll(m.Body, "<br>", "\n")
b := html.UnescapeString(bodyPolicy.Sanitize(t))
return b
}
// BodyForward returns a mail's body for a mail forward or reply.
func (m *Mail) ToString(format string) string {
s := "\n---\n"
s += m.MakeHeaderText(format)
s += "\n\n"
s += m.BodyPlain()
return s
}
// MakeHeaderText returns the mail's header as formatted text.
func (m *Mail) MakeHeaderText(format string) string {
var names []string
for _, n := range m.Recipients {
names = append(names, n.Name)
}
header := fmt.Sprintf(
"From: %s\nSent: %s\nTo: %s",
m.From.Name,
m.Timestamp.Format(format),
strings.Join(names, ", "),
)
return header
}
// RecipientNames returns the names of the recipients.
func (m *Mail) RecipientNames() []string {
ss := make([]string, len(m.Recipients))
for i, r := range m.Recipients {
ss[i] = r.Name
}
return ss
}