Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
526 lines (483 loc) · 12.8 KB
/
Copy pathclient.go
File metadata and controls
526 lines (483 loc) · 12.8 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Package mail 实现 iCloud 邮件 IMAP 读取客户端。
//
// 通过 Apple 应用专用密码连接 imap.mail.me.com:993,
// 拉取隐私邮箱别名收到的邮件。对应原 Python 项目 icloud_mail.py。
package mail
import (
"fmt"
"io"
"mime"
"mime/quotedprintable"
"net/mail"
"sort"
"strings"
"time"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-message/charset"
)
const (
IMAPServer = "imap.mail.me.com"
IMAPPort = 993
)
// Message 是一封邮件的摘要信息。
type Message struct {
ID string `json:"id"`
From string `json:"from"`
To string `json:"to"`
Subject string `json:"subject"`
Date string `json:"date"`
Preview string `json:"preview"`
}
// FullMessage 是一封邮件的完整内容(含正文)。
type FullMessage struct {
Message
Body string `json:"body"`
ContentType string `json:"content_type"`
}
// Client 是 iCloud 邮件 IMAP 客户端。
type Client struct {
username string
password string
server string
port int
cli *client.Client
}
// NewClient 创建 IMAP 客户端。需在调用其它方法前先 Connect。
func NewClient(appleID, appPassword string) *Client {
return NewClientWithServer(appleID, appPassword, IMAPServer, IMAPPort)
}
// NewClientWithServer creates an IMAP client for a custom server.
func NewClientWithServer(username, password, server string, port int) *Client {
return &Client{username: username, password: password, server: server, port: port}
}
// Connect 连接并登录 IMAP 服务器。已连接且存活时直接复用。
func (c *Client) Connect() error {
if c.cli != nil {
if err := c.cli.Noop(); err == nil {
return nil
}
c.forceClose()
}
addr := fmt.Sprintf("%s:%d", c.server, c.port)
cli, err := client.DialTLS(addr, nil)
if err != nil {
return fmt.Errorf("IMAP 连接失败: %w", err)
}
if err := cli.Login(c.username, c.password); err != nil {
_ = cli.Logout()
return fmt.Errorf("IMAP 登录失败 — 请检查邮箱账号、授权码和服务器地址: %w", err)
}
c.cli = cli
return nil
}
// Ping 探测连接是否仍可用(NOOP)。
func (c *Client) Ping() error {
if c.cli == nil {
return fmt.Errorf("未连接")
}
return c.cli.Noop()
}
// Disconnect 登出并关闭连接。
func (c *Client) Disconnect() {
if c.cli != nil {
_ = c.cli.Logout()
c.cli = nil
}
}
// forceClose 不发 LOGOUT, 直接掐断(坏连接/池丢弃时用)。
func (c *Client) forceClose() {
if c.cli != nil {
_ = c.cli.Terminate()
c.cli = nil
}
}
// InboxCount 返回收件箱邮件总数。
func (c *Client) InboxCount() (int, error) {
if c.cli == nil {
return 0, fmt.Errorf("未连接")
}
mbox, err := c.cli.Select("INBOX", false)
if err != nil {
return 0, err
}
return int(mbox.Messages), nil
}
// ListInbox 拉取收件箱最近 limit 封邮件摘要。
//
// days 用于过滤只看近 N 天的邮件(0 表示不限制)。
// 返回按时间倒序排列。
func (c *Client) ListInbox(limit int, days int) ([]Message, error) {
if c.cli == nil {
return nil, fmt.Errorf("未连接")
}
if limit <= 0 {
limit = 50
}
mbox, err := c.cli.Select("INBOX", true)
if err != nil {
return nil, err
}
total := int(mbox.Messages)
if total == 0 {
return []Message{}, nil
}
// 计算起始序号(只取最近 limit 封)
from := uint32(1)
if uint32(limit) < mbox.Messages {
from = mbox.Messages - uint32(limit) + 1
}
seqset := new(imap.SeqSet)
seqset.AddRange(from, mbox.Messages)
// 拉取完整正文,以便填充 Preview(OTP 验证码在正文中); PEEK 不标已读
section := &imap.BodySectionName{Peek: true}
items := []imap.FetchItem{
imap.FetchUid,
imap.FetchEnvelope,
imap.FetchInternalDate,
section.FetchItem(),
}
messages := make(chan *imap.Message, limit)
done := make(chan error, 1)
go func() {
done <- c.cli.Fetch(seqset, items, messages)
}()
var out []Message
for msg := range messages {
m := toMessageWithBody(msg)
// days 过滤
if days > 0 {
if t, err := time.Parse(time.RFC1123Z, m.Date); err == nil {
if time.Since(t) > time.Duration(days)*24*time.Hour {
continue
}
}
}
out = append(out, m)
}
if err := <-done; err != nil {
return nil, err
}
sort.SliceStable(out, func(i, j int) bool { return out[i].Date > out[j].Date })
return out, nil
}
// FindByRecipient 查找发给指定隐私邮箱别名的最近 limit 封邮件(新→旧)。
//
// 先尝试 IMAP TO 搜索; 失败则只扫收件箱最近若干封本地过滤。
func (c *Client) FindByRecipient(recipient string, limit int, days int) ([]Message, error) {
var out []Message
err := c.ForEachByRecipient(recipient, limit, days, func(m Message) bool {
out = append(out, m)
return true // 收满 limit 为止
})
return out, err
}
// ForEachByRecipient 按新→旧遍历发给 recipient 的最近 limit 封邮件。
// onMsg 返回 false 时立即停止(用于 OTP 命中即返回)。
func (c *Client) ForEachByRecipient(recipient string, limit int, days int, onMsg func(Message) bool) error {
if c.cli == nil {
return fmt.Errorf("未连接")
}
if nil {
return fmt.Errorf("onMsg 不能为空")
}
if limit <= 0 {
limit = 5
}
if _, err := c.cli.Select("INBOX", true); err != nil {
return err
}
// 1) 服务端按 To 搜索
criteria := imap.NewSearchCriteria()
criteria.Header.Add("To", recipient)
if days > 0 {
criteria.Since = time.Now().AddDate(0, 0, -days)
}
uids, err := c.cli.UidSearch(criteria)
if err == nil && len(uids) > 0 {
// UID 升序 → 取最后 limit 个(最新) → 倒序遍历
uids = newestUIDs(uids, limit)
for i := len(uids) - 1; i >= 0; i-- {
m, ferr := c.fetchOneUID(uids[i])
if ferr != nil {
return ferr
}
if !onMsg(m) {
return nil
}
}
return nil
}
// 2) fallback: 只扫最近 N 封信封, 命中 To 再拉 body
return c.forEachRecentMatching(recipient, limit, days, onMsg)
}
// newestUIDs 保留 UID 列表中最新的 limit 个(假定 UID 升序)。
func newestUIDs(uids []uint32, limit int) []uint32 {
if limit <= 0 || len(uids) <= limit {
return uids
}
return uids[len(uids)-limit:]
}
// forEachRecentMatching 拉取收件箱最近 scan 封(仅 envelope), 本地按 To 过滤后再取 body。
func (c *Client) forEachRecentMatching(recipient string, limit int, days int, onMsg func(Message) bool) error {
mbox, err := c.cli.Select("INBOX", true)
if err != nil {
return err
}
total := int(mbox.Messages)
if total == 0 {
return nil
}
// 只扫最近 scan 封, 避免全箱
scan := limit * 4
if scan < 20 {
scan = 20
}
if scan > 80 {
scan = 80
}
if scan > total {
scan = total
}
from := mbox.Messages - uint32(scan) + 1
seqset := new(imap.SeqSet)
seqset.AddRange(from, mbox.Messages)
// 仅 envelope + date, 不拉 body
items := []imap.FetchItem{imap.FetchUid, imap.FetchEnvelope, imap.FetchInternalDate}
messages := make(chan *imap.Message, scan)
done := make(chan error, 1)
go func() {
done <- c.cli.Fetch(seqset, items, messages)
}()
type cand struct {
uid uint32
date time.Time
to string
}
var cands []cand
recipient = strings.ToLower(recipient)
for msg := range messages {
if msg == nil || msg.Envelope == nil {
continue
}
to := ""
if len(msg.Envelope.To) > 0 {
parts := make([]string, 0, len(msg.Envelope.To))
for _, a := range msg.Envelope.To {
parts = append(parts, a.Address())
}
to = strings.Join(parts, ", ")
}
if !strings.Contains(strings.ToLower(to), recipient) {
continue
}
if days > 0 && !msg.Envelope.Date.IsZero() {
if time.Since(msg.Envelope.Date) > time.Duration(days)*24*time.Hour {
continue
}
}
cands = append(cands, cand{uid: msg.Uid, date: msg.Envelope.Date, to: to})
}
if err := <-done; err != nil {
return err
}
// 新→旧
for i := 0; i < len(cands); i++ {
for j := i + 1; j < len(cands); j++ {
if cands[j].date.After(cands[i].date) {
cands[i], cands[j] = cands[j], cands[i]
}
}
}
n := 0
for _, cd := range cands {
if n >= limit {
break
}
m, ferr := c.fetchOneUID(cd.uid)
if ferr != nil {
return ferr
}
n++
if !onMsg(m) {
return nil
}
}
return nil
}
// fetchOneUID 拉取单封邮件(含 body preview), 使用 BODY.PEEK 不标已读。
func (c *Client) fetchOneUID(uid uint32) (Message, error) {
seqset := new(imap.SeqSet)
seqset.AddNum(uid)
section := &imap.BodySectionName{Peek: true}
items := []imap.FetchItem{imap.FetchUid, imap.FetchEnvelope, imap.FetchInternalDate, section.FetchItem()}
messages := make(chan *imap.Message, 1)
done := make(chan error, 1)
go func() {
done <- c.cli.UidFetch(seqset, items, messages)
}()
msg := <-messages
if err := <-done; err != nil {
return Message{}, err
}
if msg == nil {
return Message{}, fmt.Errorf("邮件不存在 (uid=%d)", uid)
}
return toMessageWithBody(msg), nil
}
func (c *Client) fetchByUIDs(uids []uint32, limit int) ([]Message, error) {
if len(uids) == 0 {
return []Message{}, nil
}
uids = newestUIDs(uids, limit)
var out []Message
// 新→旧
for i := len(uids) - 1; i >= 0; i-- {
m, err := c.fetchOneUID(uids[i])
if err != nil {
return out, err
}
out = append(out, m)
}
return out, nil
}
// GetFull 获取单封邮件的完整内容(含正文)。
func (c *Client) GetFull(uid uint32) (*FullMessage, error) {
if c.cli == nil {
return nil, fmt.Errorf("未连接")
}
if _, err := c.cli.Select("INBOX", true); err != nil {
return nil, err
}
seqset := new(imap.SeqSet)
seqset.AddNum(uid)
items := []imap.FetchItem{imap.FetchUid, imap.FetchEnvelope, imap.FetchInternalDate, imap.FetchRFC822}
messages := make(chan *imap.Message, 1)
done := make(chan error, 1)
go func() {
done <- c.cli.UidFetch(seqset, items, messages)
}()
msg := <-messages
if err := <-done; err != nil {
return nil, err
}
if msg == nil {
return nil, fmt.Errorf("邮件不存在 (uid=%d)", uid)
}
full := &FullMessage{Message: toMessage(msg)}
// 解析正文
if r := msg.GetBody(&imap.BodySectionName{}); r != nil {
if em, err := mail.ReadMessage(r); err == nil {
body, _ := readBody(em)
full.Body = body
full.ContentType = em.Header.Get("Content-Type")
}
}
return full, nil
}
// Delete 删除收件箱中指定 UID 的邮件。
func (c *Client) Delete(uid uint32) error {
if c.cli == nil {
return fmt.Errorf("未连接")
}
if uid == 0 {
return fmt.Errorf("邮件 UID 无效")
}
if _, err := c.cli.Select("INBOX", false); err != nil {
return err
}
seqset := new(imap.SeqSet)
seqset.AddNum(uid)
item := imap.FormatFlagsOp(imap.AddFlags, true)
if err := c.cli.UidStore(seqset, item, []interface{}{imap.DeletedFlag}, nil); err != nil {
return err
}
return c.cli.Expunge(nil)
}
// ---- 解析工具 ----
func toMessage(msg *imap.Message) Message {
m := Message{}
if msg.Uid > 0 {
m.ID = fmt.Sprintf("%d", msg.Uid)
}
if msg.Envelope != nil {
if len(msg.Envelope.From) > 0 {
m.From = msg.Envelope.From[0].Address()
}
if len(msg.Envelope.To) > 0 {
addrs := make([]string, 0, len(msg.Envelope.To))
for _, a := range msg.Envelope.To {
addrs = append(addrs, a.Address())
}
m.To = strings.Join(addrs, ", ")
}
m.Subject = decodeHeader(msg.Envelope.Subject)
if !msg.Envelope.Date.IsZero() {
m.Date = msg.Envelope.Date.Format(time.RFC3339)
}
}
if m.From != "" {
m.From = decodeHeader(m.From)
}
if m.To != "" {
m.To = decodeHeader(m.To)
}
return m
}
// toMessageWithBody 在 toMessage 基础上解析正文填充 Preview(供 OTP 提取)。
func toMessageWithBody(msg *imap.Message) Message {
m := toMessage(msg)
// Fetch 可能用 BODY[] 或 BODY.PEEK[], 两种 section 都试
for _, section := range []*imap.BodySectionName{{Peek: true}, {}} {
r := msg.GetBody(section)
if r == nil {
continue
}
em, err := mail.ReadMessage(r)
if err != nil {
continue
}
body, err := readBody(em)
if err != nil {
continue
}
m.Preview = strings.TrimSpace(body)
break
}
return m
}
// decodeHeader 解码 RFC 2047 编码的邮件头(如 =?UTF-8?B?xxx?=)。
func decodeHeader(s string) string {
if s == "" {
return ""
}
dec := mime.WordDecoder{CharsetReader: charset.Reader}
out, err := dec.DecodeHeader(s)
if err != nil {
return s
}
return out
}
// readBody 读取邮件正文,优先 text/plain,其次从 HTML 提取纯文本。
func readBody(msg *mail.Message) (string, error) {
ct := msg.Header.Get("Content-Type")
if strings.HasPrefix(ct, "text/html") {
raw, _ := io.ReadAll(msg.Body)
// quoted-printable 解码
if strings.Contains(msg.Header.Get("Content-Transfer-Encoding"), "quoted-printable") {
r := quotedprintable.NewReader(strings.NewReader(string(raw)))
raw, _ = io.ReadAll(r)
}
return sanitizePreview(string(raw)), nil
}
// 默认当 text/plain
raw, err := io.ReadAll(msg.Body)
if err != nil {
return "", err
}
if strings.Contains(msg.Header.Get("Content-Transfer-Encoding"), "quoted-printable") {
r := quotedprintable.NewReader(strings.NewReader(string(raw)))
raw, _ = io.ReadAll(r)
}
return sanitizePlainPreview(string(raw)), nil
}