-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimap.go
250 lines (214 loc) · 5.31 KB
/
imap.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Meilindex - mail indexing and search tool.
* Copyright (C) 2020 Tero Vierimaa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
*/
package indexer
import (
"crypto/tls"
"fmt"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
_ "github.com/emersion/go-message/charset"
"github.com/emersion/go-message/mail"
"github.com/jaytaylor/html2text"
"github.com/sirupsen/logrus"
"io"
"io/ioutil"
"strings"
"time"
)
type Imap struct {
Url string
Tls bool
TlsSkipVerification bool
Username string
Password string
client *client.Client
mailbox *imap.MailboxStatus
}
func (i *Imap) Connect() error {
var err error
if i.Tls {
if i.TlsSkipVerification {
i.client, err = client.DialTLS(i.Url, &tls.Config{
InsecureSkipVerify: true,
})
} else {
i.client, err = client.DialTLS(i.Url, nil)
}
}
if err != nil {
err = fmt.Errorf("connect server: %v", err)
} else {
err = i.client.Login(i.Username, i.Password)
if err != nil {
err = fmt.Errorf("login: %v", err)
}
}
return err
}
func (i *Imap) Disconnect() error {
if i.client != nil {
return i.client.Logout()
}
return nil
}
func (i *Imap) Mailboxes() []string {
mailboxes := make(chan *imap.MailboxInfo, 10)
done := make(chan error, 1)
go func() {
done <- i.client.List("", "*", mailboxes)
}()
results := make([]string, 0)
num := 0
for m := range mailboxes {
results = append(results, m.Name)
num += 1
}
return results
}
func (i *Imap) SelectMailbox(name string) error {
mbox, err := i.client.Select(name, true)
if err != nil {
return err
}
i.mailbox = mbox
logrus.Infof("Mailbox has %d mails", i.mailbox.Messages)
return nil
}
func (i *Imap) FetchMail() ([]*Mail, error) {
messages := make(chan *imap.Message, i.mailbox.Messages)
done := make(chan error, 1)
sequence := &imap.SeqSet{}
start := 1
stop := i.mailbox.Messages
//stop = 10
sequence.AddRange(stop, uint32(start))
section := &imap.BodySectionName{}
go func() {
done <- i.client.Fetch(sequence, []imap.FetchItem{section.FetchItem(), imap.FetchUid}, messages)
}()
<-done
mails := make([]*Mail, len(messages))
folder := i.mailbox.Name
if folder == "INBOX" {
folder = "Inbox"
}
for i := 0; i < len(mails); i++ {
msg := <-messages
parsed, err := mail.CreateReader(msg.GetBody(section))
if err != nil {
logrus.Errorf("parse mail: %v", err)
}
m, err := mailToMail(parsed)
m.Folder = folder
mails[i] = m
}
return mails, nil
}
func mailToMail(m *mail.Reader) (*Mail, error) {
var err error
h := m.Header
date, err := h.Date()
if err != nil {
logrus.Warningf("(skip) parse date: %v", err)
date = time.Unix(0, 0)
} else {
}
from := h.Get("From")
to := h.Get("To")
cc := h.Get("Cc")
out := &Mail{
To: stripdAddressNames(to),
Cc: stripdAddressNames(cc),
Timestamp: date,
Subject: h.Get("Subject"),
}
if address := stripdAddressNames(from); len(address) >= 1 {
out.From = address[0]
}
out.Uid, err = h.MessageID()
out.Id, err = h.MessageID()
s, err := h.Subject()
if err == nil {
out.Subject = s
}
inlineHeaders := 0
for {
part, err := m.NextPart()
if err != nil {
if err == io.EOF {
break
}
errString := err.Error()
if errString == "multipart: NextPart: EOF" {
break
} else {
logrus.Warningf("(skip mail): parse part: %v", err)
break
}
}
switch part.Header.(type) {
case *mail.InlineHeader:
inlineHeaders += 1
contentType := part.Header.Get("Content-Type")
if strings.Contains(contentType, "text/plain") {
body, err := ioutil.ReadAll(part.Body)
if err != nil {
logrus.Warningf("(skip) read plain body: %v", err)
} else {
out.Body = string(body)
}
} else {
// plain text already exists
if out.Body != "" {
continue
}
// accept only 1st inline header
if inlineHeaders == 1 {
out.Body, err = html2text.FromReader(part.Body, html2text.Options{
PrettyTables: false,
})
} else {
b, err := ioutil.ReadAll(part.Body)
if err != nil {
logrus.Errorf("read message attachment: %v", err)
} else {
out.Attachments = append(out.Attachments, b)
}
}
if err != nil {
errString := err.Error()
if errString != "unexpected EOF" {
logrus.Errorf("Read html body into text: %v", err)
}
}
}
case *mail.AttachmentHeader:
contentType := part.Header.Get("Content-Type")
out.AttachmentNames = append(out.AttachmentNames, ParseAttachments(contentType))
b, err := ioutil.ReadAll(part.Body)
if err != nil {
logrus.Warningf("(skip) read message attachment: %v", err)
} else {
out.Attachments = append(out.Attachments, b)
}
}
}
return out, err
}