alps/plugins/viewtext/viewer.go

119 lines
2.4 KiB
Go
Raw Normal View History

2020-05-13 12:07:44 +00:00
package alpsviewtext
2020-02-12 13:42:51 +00:00
import (
"bufio"
2020-02-12 13:42:51 +00:00
"fmt"
"html/template"
"net/url"
2020-02-12 13:42:51 +00:00
"strings"
"git.sr.ht/~migadu/alps"
alpsbase "git.sr.ht/~migadu/alps/plugins/base"
2020-02-12 13:42:51 +00:00
"github.com/emersion/go-message"
"gitlab.com/golang-commonmark/linkify"
2020-02-12 13:42:51 +00:00
)
// TODO: dim quotes and "On xxx, xxx wrote:" lines
const (
tplStr = `<pre>{{range .}}{{.}}{{end}}</pre>`
linkTplStr = `<a href="{{.Href}}" target="_blank" rel="nofollow noopener">{{.Text}}</a>`
)
var tpl *template.Template
func init() {
tpl = template.Must(template.New("view-text.html").Parse(tplStr))
template.Must(tpl.New("view-text-link.html").Parse(linkTplStr))
}
type linkRenderData struct {
Href string
Text string
}
var allowedSchemes = map[string]bool{
"http": true,
"https": true,
"mailto": true,
"ftp": true,
"sftp": true,
"ftps": true,
"tel": true,
}
func executeTemplate(name string, data interface{}) (template.HTML, error) {
var sb strings.Builder
err := tpl.ExecuteTemplate(&sb, name, data)
if err != nil {
return "", err
}
return template.HTML(sb.String()), nil
}
2020-02-12 13:42:51 +00:00
type viewer struct{}
2020-05-13 12:07:44 +00:00
func (viewer) ViewMessagePart(ctx *alps.Context, msg *alpsbase.IMAPMessage, part *message.Entity) (interface{}, error) {
2020-02-12 13:42:51 +00:00
mimeType, _, err := part.Header.ContentType()
if err != nil {
return nil, err
}
if !strings.EqualFold(mimeType, "text/plain") {
2020-05-13 12:07:44 +00:00
return nil, alpsbase.ErrViewUnsupported
2020-02-12 13:42:51 +00:00
}
var tokens []interface{}
scanner := bufio.NewScanner(part.Body)
for scanner.Scan() {
l := scanner.Text()
2020-02-12 13:42:51 +00:00
i := 0
for _, link := range linkify.Links(l) {
href := l[link.Start:link.End]
if link.Scheme == "" {
href = "https://" + href
} else if !strings.HasPrefix(href, link.Scheme) {
href = link.Scheme + href
}
2020-02-12 13:42:51 +00:00
u, err := url.Parse(href)
if err != nil {
continue
}
if !allowedSchemes[u.Scheme] {
continue
}
// TODO: redirect mailto links to the composer
if i < link.Start {
tokens = append(tokens, l[i:link.Start])
}
tok, err := executeTemplate("view-text-link.html", linkRenderData{
Href: href,
Text: l[link.Start:link.End],
})
if err != nil {
return nil, err
}
tokens = append(tokens, tok)
i = link.End
}
if i < len(l) {
tokens = append(tokens, l[i:])
}
tokens = append(tokens, "\n")
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("failed to read part body: %v", err)
2020-02-12 13:42:51 +00:00
}
return executeTemplate("view-text.html", tokens)
2020-02-12 13:42:51 +00:00
}
func init() {
2020-05-13 12:07:44 +00:00
alpsbase.RegisterViewer(viewer{})
2020-02-12 13:42:51 +00:00
}