-
Notifications
You must be signed in to change notification settings - Fork 40
/
maildir.go
184 lines (161 loc) · 3.97 KB
/
maildir.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package storage
import (
"errors"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/mailhog/data"
)
// Maildir is a maildir storage backend
type Maildir struct {
Path string
}
// CreateMaildir creates a new maildir storage backend
func CreateMaildir(path string) *Maildir {
if len(path) == 0 {
dir, err := ioutil.TempDir("", "mailhog")
if err != nil {
panic(err)
}
path = dir
}
if _, err := os.Stat(path); err != nil {
err := os.MkdirAll(path, 0770)
if err != nil {
panic(err)
}
}
log.Println("Maildir path is", path)
return &Maildir{
Path: path,
}
}
// Store stores a message and returns its storage ID
func (maildir *Maildir) Store(m *data.Message) (string, error) {
b, err := ioutil.ReadAll(m.Raw.Bytes())
if err != nil {
return "", err
}
err = ioutil.WriteFile(filepath.Join(maildir.Path, string(m.ID)), b, 0660)
return string(m.ID), err
}
// Count returns the number of stored messages
func (maildir *Maildir) Count() int {
// FIXME may be wrong, ../. ?
// and handle error?
dir, err := os.Open(maildir.Path)
if err != nil {
panic(err)
}
defer dir.Close()
n, _ := dir.Readdirnames(0)
return len(n)
}
// Search finds messages matching the query
func (maildir *Maildir) Search(kind, query string, start, limit int) (*data.Messages, int, error) {
query = strings.ToLower(query)
var filteredMessages = make([]data.Message, 0)
var matched int
err := filepath.Walk(maildir.Path, func(path string, info os.FileInfo, err error) error {
if limit > 0 && len(filteredMessages) >= limit {
return errors.New("reached limit")
}
if info.IsDir() {
return nil
}
msg, err := maildir.Load(info.Name())
if err != nil {
log.Println(err)
return nil
}
switch kind {
case "to":
for _, t := range msg.To {
if strings.Contains(strings.ToLower(t.Mailbox+"@"+t.Domain), query) {
if start > matched {
matched++
break
}
filteredMessages = append(filteredMessages, *msg)
break
}
}
case "from":
if strings.Contains(strings.ToLower(msg.From.Mailbox+"@"+msg.From.Domain), query) {
if start > matched {
matched++
break
}
filteredMessages = append(filteredMessages, *msg)
}
case "containing":
if strings.Contains(strings.ToLower(msg.Raw.Data), query) {
if start > matched {
matched++
break
}
filteredMessages = append(filteredMessages, *msg)
}
}
return nil
})
if err != nil {
log.Println(err)
}
msgs := data.Messages(filteredMessages)
return &msgs, len(filteredMessages), nil
}
// List lists stored messages by index
func (maildir *Maildir) List(start, limit int) (*data.Messages, error) {
log.Println("Listing messages in", maildir.Path)
messages := make([]data.Message, 0)
dir, err := os.Open(maildir.Path)
if err != nil {
return nil, err
}
defer dir.Close()
n, err := dir.Readdir(0)
if err != nil {
return nil, err
}
for _, fileinfo := range n {
b, err := ioutil.ReadFile(filepath.Join(maildir.Path, fileinfo.Name()))
if err != nil {
return nil, err
}
msg := data.FromBytes(b)
// FIXME domain
m := *msg.Parse("mailhog.example")
m.ID = data.MessageID(fileinfo.Name())
m.Created = fileinfo.ModTime()
messages = append(messages, m)
}
log.Printf("Found %d messages", len(messages))
msgs := data.Messages(messages)
return &msgs, nil
}
// DeleteOne deletes an individual message by storage ID
func (maildir *Maildir) DeleteOne(id string) error {
return os.Remove(filepath.Join(maildir.Path, id))
}
// DeleteAll deletes all in memory messages
func (maildir *Maildir) DeleteAll() error {
err := os.RemoveAll(maildir.Path)
if err != nil {
return err
}
return os.Mkdir(maildir.Path, 0770)
}
// Load returns an individual message by storage ID
func (maildir *Maildir) Load(id string) (*data.Message, error) {
b, err := ioutil.ReadFile(filepath.Join(maildir.Path, id))
if err != nil {
return nil, err
}
// FIXME domain
m := data.FromBytes(b).Parse("mailhog.example")
m.ID = data.MessageID(id)
return m, nil
}