-
Notifications
You must be signed in to change notification settings - Fork 1
/
message_store.go
167 lines (133 loc) · 3.38 KB
/
message_store.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
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
package main
import (
"bytes"
"fmt"
"strings"
"sync"
"time"
"github.com/flashmob/go-guerrilla/mail"
"github.com/jhillyerd/enmime"
)
type Message struct {
From string
To []string
Headers map[string][]string
Text string
HTML string
ExpireTime time.Time
}
type MessageStore struct {
expireInterval time.Duration
messages map[string][]*Message
messagesMutex *sync.RWMutex
stopRunning bool
}
func NewMessageStore(expireInterval time.Duration) *MessageStore {
return &MessageStore{
expireInterval: expireInterval,
messages: make(map[string][]*Message),
messagesMutex: &sync.RWMutex{},
}
}
func (ms *MessageStore) Run() error {
go ms.expireWorker()
return nil
}
func (ms *MessageStore) Stop() {
ms.stopRunning = true
}
func (ms *MessageStore) SaveMessage(incoming *mail.Envelope) error {
ms.messagesMutex.Lock()
defer ms.messagesMutex.Unlock()
reader := incoming.NewReader()
env, err := enmime.ReadEnvelope(reader)
if err != nil {
return fmt.Errorf("error occurred during email parsing: %v", err)
}
text := env.Text
for _, part := range env.Inlines {
if bytes.Equal(part.Content, []byte(env.Text)) {
continue
}
if text == "" && part.ContentType == "text/plain" && part.FileName == "" {
text = string(part.Content)
continue
}
}
for _, part := range env.Attachments {
if bytes.Equal(part.Content, []byte(env.Text)) {
continue
}
if text == "" && part.ContentType == "text/plain" && part.FileName == "" {
text = string(part.Content)
continue
}
}
if text == "" {
text = incoming.Data.String()
}
var toAddrs []string
for _, addr := range incoming.RcptTo {
toAddrs = append(toAddrs, addr.String())
}
headers := make(map[string][]string)
for _, key := range env.GetHeaderKeys() {
headers[key] = env.GetHeaderValues(key)
}
mailItem := &Message{
From: incoming.MailFrom.String(),
To: toAddrs,
Headers: headers,
Text: text,
HTML: env.HTML,
ExpireTime: time.Now().Add(ms.expireInterval),
}
for _, target := range incoming.RcptTo {
targetAddress := strings.ToLower(target.String())
ms.messages[targetAddress] = append(ms.messages[targetAddress], mailItem)
}
return nil
}
func (ms *MessageStore) FetchMessages(email string) []*Message {
ms.messagesMutex.RLock()
defer ms.messagesMutex.RUnlock()
return ms.messages[email]
}
func (ms *MessageStore) RemoveMessages(email string) {
ms.messagesMutex.Lock()
defer ms.messagesMutex.Unlock()
delete(ms.messages, email)
}
func (ms *MessageStore) expireWorker() {
for !ms.stopRunning {
ms.messagesMutex.Lock()
for email, messageList := range ms.messages {
// remove expired items from messageList without allocating a new array
// in-place deletion taken from here: https://stackoverflow.com/a/20551116
i := 0
for _, message := range messageList {
// if this message is expired, skip it
if time.Now().After(message.ExpireTime) {
continue
}
// copy and increment index
messageList[i] = message
i++
}
// if the message list is empty, just delete this entire map entry
if i == 0 {
delete(ms.messages, email)
continue
}
// clear dead values
for j := i; j < len(messageList); j++ {
messageList[j] = nil
}
// update slice reference
ms.messages[email] = messageList[:i]
}
ms.messagesMutex.Unlock()
// TODO: Make this configurable
time.Sleep(30 * time.Second)
}
}