From 405c18d21388f1cc6f8b7349186efb3fa44eef87 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 19 Nov 2020 10:50:52 -0500 Subject: Convert HTML to plaintext for forwarding & replies --- plugins/base/routes.go | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) (limited to 'plugins/base') diff --git a/plugins/base/routes.go b/plugins/base/routes.go index 2107abb..0345189 100644 --- a/plugins/base/routes.go +++ b/plugins/base/routes.go @@ -14,12 +14,13 @@ import ( "git.sr.ht/~emersion/alps" "github.com/emersion/go-imap" - imapmove "github.com/emersion/go-imap-move" - imapclient "github.com/emersion/go-imap/client" "github.com/emersion/go-message" "github.com/emersion/go-message/mail" "github.com/emersion/go-smtp" "github.com/labstack/echo/v4" + "jaytaylor.com/html2text" + imapclient "github.com/emersion/go-imap/client" + imapmove "github.com/emersion/go-imap-move" ) func registerRoutes(p *alps.GoPlugin) { @@ -759,17 +760,25 @@ func handleReply(ctx *alps.Context) error { return fmt.Errorf("failed to parse part Content-Type: %v", err) } - if !strings.EqualFold(mimeType, "text/plain") { - err := fmt.Errorf("cannot reply to %q part", mimeType) + if mimeType == "text/plain" { + msg.Text, err = quote(part.Body) + if err != nil { + return err + } + } else if mimeType == "text/html" { + text, err := html2text.FromReader(part.Body, html2text.Options{}) + if err != nil { + return err + } + msg.Text, err = quote(strings.NewReader(text)) + if err != nil { + return nil + } + } else { + err := fmt.Errorf("cannot forward %q part", mimeType) return echo.NewHTTPError(http.StatusBadRequest, err) } - // TODO: strip HTML tags if text/html - msg.Text, err = quote(part.Body) - if err != nil { - return err - } - msg.MessageID = mail.GenerateMessageID() msg.InReplyTo = inReplyTo.Envelope.MessageId // TODO: populate From from known user addresses and inReplyTo.Envelope.To @@ -819,16 +828,21 @@ func handleForward(ctx *alps.Context) error { return fmt.Errorf("failed to parse part Content-Type: %v", err) } - if !strings.EqualFold(mimeType, "text/plain") { + if mimeType == "text/plain" { + msg.Text, err = quote(part.Body) + if err != nil { + return err + } + } else if mimeType == "text/html" { + msg.Text, err = html2text.FromReader(part.Body, html2text.Options{}) + if err != nil { + return err + } + } else { err := fmt.Errorf("cannot forward %q part", mimeType) return echo.NewHTTPError(http.StatusBadRequest, err) } - msg.Text, err = quote(part.Body) - if err != nil { - return err - } - msg.MessageID = mail.GenerateMessageID() msg.Subject = source.Envelope.Subject if !strings.HasPrefix(strings.ToLower(msg.Subject), "fwd:") && -- cgit v1.2.3