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.
 
 
 
 

66 lines
1.6 KiB

  1. package alpsviewhtml
  2. import (
  3. "bytes"
  4. "fmt"
  5. "html/template"
  6. "io/ioutil"
  7. "strings"
  8. "git.sr.ht/~migadu/alps"
  9. alpsbase "git.sr.ht/~migadu/alps/plugins/base"
  10. "github.com/emersion/go-message"
  11. )
  12. const tplSrc = `
  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. var tpl = template.Must(template.New("view-html.html").Parse(tplSrc))
  20. type viewer struct{}
  21. func (viewer) ViewMessagePart(ctx *alps.Context, msg *alpsbase.IMAPMessage, part *message.Entity) (interface{}, error) {
  22. allowRemoteResources := ctx.QueryParam("allow-remote-resources") == "1"
  23. mimeType, _, err := part.Header.ContentType()
  24. if err != nil {
  25. return nil, err
  26. }
  27. if !strings.EqualFold(mimeType, "text/html") {
  28. return nil, alpsbase.ErrViewUnsupported
  29. }
  30. body, err := ioutil.ReadAll(part.Body)
  31. if err != nil {
  32. return nil, fmt.Errorf("failed to read part body: %v", err)
  33. }
  34. san := sanitizer{
  35. msg: msg,
  36. allowRemoteResources: allowRemoteResources,
  37. }
  38. body, err = san.sanitizeHTML(body)
  39. if err != nil {
  40. return nil, fmt.Errorf("failed to sanitize HTML part: %v", err)
  41. }
  42. ctx.Set("viewhtml.hasRemoteResources", san.hasRemoteResources)
  43. var buf bytes.Buffer
  44. err = tpl.Execute(&buf, string(body))
  45. if err != nil {
  46. return nil, err
  47. }
  48. return template.HTML(buf.String()), nil
  49. }
  50. func init() {
  51. alpsbase.RegisterViewer(viewer{})
  52. }