A webmail client. Forked from https://git.sr.ht/~migadu/alps
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

58 regels
1.4 KiB

  1. package koushinviewhtml
  2. import (
  3. "bytes"
  4. "fmt"
  5. "html/template"
  6. "io/ioutil"
  7. "strings"
  8. "git.sr.ht/~emersion/koushin"
  9. koushinbase "git.sr.ht/~emersion/koushin/plugins/base"
  10. "github.com/emersion/go-message"
  11. )
  12. const tpl = `
  13. <!-- allow-same-origin is required to resize the frame with its content -->
  14. <!-- allow-popups is required for target="_blank" links -->
  15. <iframe id="email-frame" srcdoc="{{.}}" sandbox="allow-same-origin allow-popups"></iframe>
  16. <script src="/plugins/viewhtml/assets/script.js"></script>
  17. <link rel="stylesheet" href="/plugins/viewhtml/assets/style.css">
  18. `
  19. type viewer struct{}
  20. func (viewer) ViewMessagePart(ctx *koushin.Context, msg *koushinbase.IMAPMessage, part *message.Entity) (interface{}, error) {
  21. mimeType, _, err := part.Header.ContentType()
  22. if err != nil {
  23. return nil, err
  24. }
  25. if !strings.EqualFold(mimeType, "text/html") {
  26. return nil, koushinbase.ErrViewUnsupported
  27. }
  28. body, err := ioutil.ReadAll(part.Body)
  29. if err != nil {
  30. return nil, fmt.Errorf("failed to read part body: %v", err)
  31. }
  32. body, err = sanitizeHTML(body)
  33. if err != nil {
  34. return nil, fmt.Errorf("failed to sanitize HTML part: %v", err)
  35. }
  36. t := template.Must(template.New("view-html.html").Parse(tpl))
  37. var buf bytes.Buffer
  38. err = t.Execute(&buf, string(body))
  39. if err != nil {
  40. return nil, err
  41. }
  42. return template.HTML(buf.String()), nil
  43. }
  44. func init() {
  45. koushinbase.RegisterViewer(viewer{})
  46. }