plugins/viewhtml: add support for inline Content-Id images

Closes: https://todo.sr.ht/~sircmpwn/koushin/33
This commit is contained in:
Simon Ser 2020-02-25 10:47:38 +01:00
parent 7af7d73700
commit 62660f8d1d
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48

View file

@ -3,6 +3,7 @@ package koushinviewhtml
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"net/url"
"regexp" "regexp"
"strings" "strings"
@ -73,8 +74,23 @@ type sanitizer struct {
msg *koushinbase.IMAPMessage msg *koushinbase.IMAPMessage
} }
func (san *sanitizer) sanitizeResourceURL(src string) string { func (san *sanitizer) sanitizeImageURL(src string) string {
return "about:blank" u, err := url.Parse(src)
if err != nil {
return "about:blank"
}
// TODO: mid support?
if !strings.EqualFold(u.Scheme, "cid") || san.msg == nil {
return "about:blank"
}
part := san.msg.PartByID(u.Opaque)
if part == nil || !strings.HasPrefix(part.MIMEType, "image/") {
return "about:blank"
}
return part.URL(true).String()
} }
func (san *sanitizer) sanitizeCSSDecls(decls []*css.Declaration) []*css.Declaration { func (san *sanitizer) sanitizeCSSDecls(decls []*css.Declaration) []*css.Declaration {
@ -114,7 +130,7 @@ func (san *sanitizer) sanitizeNode(n *html.Node) {
for i := range n.Attr { for i := range n.Attr {
attr := &n.Attr[i] attr := &n.Attr[i]
if strings.EqualFold(attr.Key, "src") { if strings.EqualFold(attr.Key, "src") {
attr.Val = san.sanitizeResourceURL(attr.Val) attr.Val = san.sanitizeImageURL(attr.Val)
} }
} }
} else if strings.EqualFold(n.Data, "style") { } else if strings.EqualFold(n.Data, "style") {