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.
 
 
 
 

50 lines
1.0 KiB

  1. package koushinviewtext
  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. // TODO: dim quotes and "On xxx, xxx wrote:" lines
  13. // TODO: turn URLs into links
  14. const tpl = `<pre>{{.}}</pre>`
  15. type viewer struct{}
  16. func (viewer) ViewMessagePart(ctx *koushin.Context, msg *koushinbase.IMAPMessage, part *message.Entity) (interface{}, error) {
  17. mimeType, _, err := part.Header.ContentType()
  18. if err != nil {
  19. return nil, err
  20. }
  21. if !strings.EqualFold(mimeType, "text/plain") {
  22. return nil, koushinbase.ErrViewUnsupported
  23. }
  24. body, err := ioutil.ReadAll(part.Body)
  25. if err != nil {
  26. return nil, fmt.Errorf("failed to read part body: %v", err)
  27. }
  28. t := template.Must(template.New("view-text.html").Parse(tpl))
  29. var buf bytes.Buffer
  30. err = t.Execute(&buf, string(body))
  31. if err != nil {
  32. return nil, err
  33. }
  34. return template.HTML(buf.String()), nil
  35. }
  36. func init() {
  37. koushinbase.RegisterViewer(viewer{})
  38. }