diff options
author | Drew DeVault <sir@cmpwn.com> | 2020-11-19 10:50:52 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2020-11-19 10:52:18 -0500 |
commit | 405c18d21388f1cc6f8b7349186efb3fa44eef87 (patch) | |
tree | 9015835a85d94e0347dd7f36b7de2ae4a6120560 /plugins | |
parent | 2fb78cad973765e3471884b8cb404948d40ab2aa (diff) | |
download | alps-405c18d21388f1cc6f8b7349186efb3fa44eef87.tar.gz alps-405c18d21388f1cc6f8b7349186efb3fa44eef87.zip |
Convert HTML to plaintext for forwarding & replies
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/base/routes.go | 46 |
1 files changed, 30 insertions, 16 deletions
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:") && |