8000 优化别名管理操作与邮件摘要展示 by hl128k · Pull Request #3 · xiaozhou26/icloud-hme · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 2 additions & 30 deletions internal/mail/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"mime"
"mime/quotedprintable"
"net/mail"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -472,8 +471,6 @@ func decodeHeader(s string) string {
return out
}

var htmlTag = regexp.MustCompile(`<[^>]+>`)

// readBody 读取邮件正文,优先 text/plain,其次从 HTML 提取纯文本。
func readBody(msg *mail.Message) (string, error) {
ct := msg.Header.Get("Content-Type")
Expand All @@ -484,7 +481,7 @@ func readBody(msg *mail.Message) (string, error) {
r := quotedprintable.NewReader(strings.NewReader(string(raw)))
raw, _ = io.ReadAll(r)
}
return stripHTML(string(raw)), nil
return sanitizePreview(string(raw)), nil
}
// 默认当 text/plain
raw, err := io.ReadAll(msg.Body)
Expand All @@ -495,30 +492,5 @@ func readBody(msg *mail.Message) (string, error) {
r := quotedprintable.NewReader(strings.NewReader(string(raw)))
raw, _ = io.ReadAll(r)
}
return string(raw), nil
}

// stripHTML 粗略剥离 HTML 标签,保留可读文本。
func stripHTML(html string) string {
// 换行标签转换行
html = strings.ReplaceAll(html, "<br>", "\n")
html = strings.ReplaceAll(html, "<br/>", "\n")
html = strings.ReplaceAll(html, "<br />", "\n")
html = strings.ReplaceAll(html, "</p>", "\n")
html = strings.ReplaceAll(html, "</div>", "\n")
html = strings.ReplaceAll(html, "</tr>", "\n")
html = strings.ReplaceAll(html, "<li>", "\n- ")
// 去掉所有标签
html = htmlTag.ReplaceAllString(html, "")
// 反转义常见实体
html = strings.ReplaceAll(html, "&nbsp;", " ")
html = strings.ReplaceAll(html, "&amp;", "&")
html = strings.ReplaceAll(html, "&lt;", "<")
html = strings.ReplaceAll(html, "&gt;", ">")
// 压缩多余空白
lines := strings.Split(html, "\n")
for i, l := range lines {
lines[i] = strings.TrimSpace(l)
}
return strings.TrimSpace(strings.Join(lines, "\n"))
return sanitizePlainPreview(string(raw)), nil
}
154 changes: 154 additions & 0 deletions internal/mail/preview.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package mail

import (
stdhtml "html"
"regexp"
"strings"
)

var (
htmlTagRE = regexp.MustCompile(`(?s)<[^>]+>`)
htmlNoiseBlockRE = regexp.MustCompile(`(?is)<(style|script|head|title|noscript)\b[^>]*>.*?</(style|script|head|title|noscript)\s*>`)
htmlCommentRE = regexp.MustCompile(`(?s)<!--.*?-->`)
htmlBreakRE = regexp.MustCompile(`(?i)<br\s*/?>`)
htmlBlockEndRE = regexp.MustCompile(`(?i)</(p|div|tr|h[1-6])\s*>`)
htmlListItemRE = regexp.MustCompile(`(?i)<li\b[^>]*>`)

cssSignalRE = regexp.MustCompile(`(?i)(@font-face|@media|@import|@supports|@keyframes|(^|[\s;{])(-webkit-|-moz-|-ms-|mso-)[\w-]*|(^|[\s;{])(font-family|text-size-adjust|border-collapse|mso-table-[\w-]+)\s*:)`)
cssDeclarationRE = regexp.MustCompile(`(?i)(^|[;{])\s*[-a-z_][\w-]*\s*:\s*[^;{}]+`)
)

// sanitizePreview converts an email preview to readable text and removes
// presentation-only HTML/CSS that mail clients commonly include.
func sanitizePreview(raw string) string {
text := stripHTML(raw)
return sanitizePlainPreview(text)
}

func sanitizePlainPreview(raw string) string {
text := normalizePreview(raw)
if text == "" || !looksLikeCSS(text) {
return text
}

// Some upstream APIs return the contents of a <style> block without the
// surrounding tags. Remove balanced CSS rules while preserving any text
// that follows the stylesheet.
text = normalizePreview(stripCSSRules(text))
if text == "" || looksLikeCSS(text) {
return ""
}
return text
}

// stripHTML removes tags and invisible document sections while keeping common
// line breaks so the preview remains readable.
func stripHTML(raw string) string {
raw = htmlNoiseBlockRE.ReplaceAllString(raw, "")
raw = htmlCommentRE.ReplaceAllString(raw, "")
raw = htmlBreakRE.ReplaceAllString(raw, "\n")
raw = htmlBlockEndRE.ReplaceAllString(raw, "\n")
raw = htmlListItemRE.ReplaceAllString(raw, "\n- ")
raw = htmlTagRE.ReplaceAllString(raw, "")
return normalizePreview(stdhtml.UnescapeString(raw))
}

func normalizePreview(raw string) string {
raw = strings.ReplaceAll(raw, "\r\n", "\n")
raw = strings.ReplaceAll(raw, "\r", "\n")
lines := strings.Split(raw, "\n")
out := make([]string, 0, len(lines))
blank := false
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
if !blank {
out = append(out, "")
}
blank = true
continue
}
out = append(out, line)
blank = false
}
return strings.TrimSpace(strings.Join(out, "\n"))
}

func look 8000 sLikeCSS(text string) bool {
braces := strings.Count(text, "{")
semicolons := strings.Count(text, ";")
signals := len(cssSignalRE.FindAllStringIndex(text, -1))
declarations := len(cssDeclarationRE.FindAllStringIndex(text, -1))
trimmed := strings.TrimSpace(text)
selectorStart := strings.HasPrefix(trimmed, ".") ||
strings.HasPrefix(trimmed, "#") ||
strings.HasPrefix(trimmed, "@")

if braces == 0 {
return signals >= 2 && semicolons >= 3
}
if selectorStart && declarations >= 1 {
return true
}
return (signals >= 2 && semicolons >= 2) ||
(braces >= 3 && declarations >= 3 && semicolons >= 3)
}

func stripCSSRules(text string) string {
for {
open := strings.IndexByte(text, '{')
if open < 0 {
return text
}
close := matchingBrace(text, open)
if close < 0 {
return text
}

start := open
for start > 0 {
if text[start-1] == '}' || text[start-1] == '\n' || text[start-1] == '\r' {
break
}
start--
}
text = text[:start] + text[close+1:]
}
}

func matchingBrace(text string, open int) int {
depth := 0
var quote byte
escaped := false
for i := open; i < len(text); i++ {
ch := text[i]
if quote != 0 {
if escaped {
escaped = false
continue
}
if ch == '\\' {
escaped = true
continue
}
if ch == quote {
quote = 0
}
continue
}
if ch == '\'' || ch == '"' {
quote = ch
continue
}
switch ch {
case '{':
depth++
case '}':
depth--
if depth == 0 {
return i
}
}
}
return -1
}
31 changes: 31 additions & 0 deletions internal/mail/preview_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package mail

import "testing"

func TestSanitizePreviewRemovesInvisibleHTMLBlocks(t *testing.T) {
raw := `<html><head><style>@font-face { font-family: Söhne; } body { color: red; }</style></head><body><p>验证码:123456</p><script>alert(1)</script></body></html>`
if got := sanitizePreview(raw); got != "验证码:123456" {
t.Fatalf("sanitizePreview() = %q, want readable body", got)
}
}

func TestSanitizePreviewDropsCSSOnlyContent(t *testing.T) {
raw := `@font-face { font-family: Söhne; } .ExternalClass { line-height: 100%; } #bodyTable { width: 560px; } body { font-family: Helvetica, Arial, sans-serif; }`
if got := sanitizePreview(raw); got != "" {
t.Fatalf("sanitizePreview() = %q, want empty CSS preview", got)
}
}

func TestSanitizePreviewRemovesCSSPrefixAndKeepsBody(t *testing.T) {
raw := `@font-face { font-family: Söhne; } .ExternalClass { line-height: 100%; } 正文内容`
if got := sanitizePreview(raw); got != "正文内容" {
t.Fatalf("sanitizePreview() = %q, want body text", got)
}
}

func TestSanitizePreviewKeepsNormalText(t *testing.T) {
raw := `订单号:A-123; 请在 10:00 前完成验证。`
if got := sanitizePreview(raw); got != raw {
t.Fatalf("sanitizePreview() = %q, want %q", got, raw)
}
}
2 changes: 1 addition & 1 deletion internal/mail/web_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (c *WebClient) search(payload string) ([]Message, error) {
ID: t.ThreadID,
From: from,
Subject: t.Subject,
Preview: t.Preview,
Preview: sanitizePreview(t.Preview),
Date: date,
})
}
Expand Down
Loading
Loading
0