diff options
author | Simon Ser <contact@emersion.fr> | 2020-02-12 14:42:51 +0100 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2020-02-12 15:41:00 +0100 |
commit | 8299617ebc24a4a5bd1dc03070e17713be7e1e1b (patch) | |
tree | 6f63bf506717b7348f625169885c68bd2c82a23e /plugins/viewhtml/viewer.go | |
parent | 892f1fa581d853f0bc5e83f8b2e66169921330a2 (diff) | |
download | alps-8299617ebc24a4a5bd1dc03070e17713be7e1e1b.tar.gz alps-8299617ebc24a4a5bd1dc03070e17713be7e1e1b.zip |
Turn message part viewers into plugins
Diffstat (limited to 'plugins/viewhtml/viewer.go')
-rw-r--r-- | plugins/viewhtml/viewer.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/plugins/viewhtml/viewer.go b/plugins/viewhtml/viewer.go new file mode 100644 index 0000000..9734c37 --- /dev/null +++ b/plugins/viewhtml/viewer.go @@ -0,0 +1,57 @@ +package koushinviewhtml + +import ( + "bytes" + "fmt" + "html/template" + "io/ioutil" + "strings" + + "git.sr.ht/~emersion/koushin" + koushinbase "git.sr.ht/~emersion/koushin/plugins/base" + "github.com/emersion/go-message" +) + +const tpl = ` +<!-- allow-same-origin is required to resize the frame with its content --> +<!-- allow-popups is required for target="_blank" links --> +<iframe id="email-frame" srcdoc="{{.}}" sandbox="allow-same-origin allow-popups"></iframe> +<script src="/plugins/viewhtml/assets/script.js"></script> +<link rel="stylesheet" href="/plugins/viewhtml/assets/style.css"> +` + +type viewer struct{} + +func (viewer) ViewMessagePart(ctx *koushin.Context, msg *koushinbase.IMAPMessage, part *message.Entity) (interface{}, error) { + mimeType, _, err := part.Header.ContentType() + if err != nil { + return nil, err + } + if !strings.EqualFold(mimeType, "text/html") { + return nil, koushinbase.ErrViewUnsupported + } + + body, err := ioutil.ReadAll(part.Body) + if err != nil { + return nil, fmt.Errorf("failed to read part body: %v", err) + } + + body, err = sanitizeHTML(body) + if err != nil { + return nil, fmt.Errorf("failed to sanitize HTML part: %v", err) + } + + t := template.Must(template.New("view-html.html").Parse(tpl)) + + var buf bytes.Buffer + err = t.Execute(&buf, string(body)) + if err != nil { + return nil, err + } + + return template.HTML(buf.String()), nil +} + +func init() { + koushinbase.RegisterViewer(viewer{}) +} |