From 8bd74c5edc0dd8caa9efbdeb315a5f5cd6b8ae1a Mon Sep 17 00:00:00 2001 From: James Houlahan Date: Thu, 2 Jul 2020 16:31:12 +0200 Subject: [PATCH] feat: set mime type --- pkg/message/html.go | 76 ------------------------------------ pkg/message/parser.go | 15 +++++-- pkg/message/parser/parser.go | 7 ++++ 3 files changed, 19 insertions(+), 79 deletions(-) delete mode 100644 pkg/message/html.go diff --git a/pkg/message/html.go b/pkg/message/html.go deleted file mode 100644 index 163a2ea8..00000000 --- a/pkg/message/html.go +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (c) 2020 Proton Technologies AG -// -// This file is part of ProtonMail Bridge. -// -// ProtonMail Bridge is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// ProtonMail Bridge is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with ProtonMail Bridge. If not, see . - -package message - -import ( - "bytes" - "errors" - escape "html" - "strings" - - "github.com/andybalholm/cascadia" - "golang.org/x/net/html" -) - -func plaintextToHTML(text string) (output string) { - text = escape.EscapeString(text) - text = strings.Replace(text, "\n\r", "
", -1) - text = strings.Replace(text, "\r\n", "
", -1) - text = strings.Replace(text, "\n", "
", -1) - text = strings.Replace(text, "\r", "
", -1) - - return "
" + text + "
" -} - -func stripHTML(input string) (stripped string, err error) { - reader := strings.NewReader(input) - doc, _ := html.Parse(reader) - body := cascadia.MustCompile("body").MatchFirst(doc) - if body == nil { - err = errors.New("failed to find necessary html element") - return - } - var buf1 bytes.Buffer - if err = html.Render(&buf1, body); err != nil { - stripped = input - return - } - stripped = buf1.String() - // Handle double body tags edge case. - if strings.Index(stripped, "") - if startIndex < 5 { - return - } - stripped = stripped[startIndex+1:] - // Closing body tag is optional. - closingIndex := strings.Index(stripped, "") - if closingIndex > -1 { - stripped = stripped[:closingIndex] - } - } - return -} - -func addOuterHTMLTags(input string) (output string) { - return "" + input + "" -} - -func makeEmbeddedImageHTML(cid, name string) (output string) { - return "\""" -} diff --git a/pkg/message/parser.go b/pkg/message/parser.go index 4b7148db..c3447025 100644 --- a/pkg/message/parser.go +++ b/pkg/message/parser.go @@ -47,10 +47,18 @@ func Parse(r io.Reader, key, keyName string) (m *pmapi.Message, mime, plain stri return } - if m.Body, plain, err = collectBodyParts(p); err != nil { + var isHTML bool + + if m.Body, plain, isHTML, err = collectBodyParts(p); err != nil { return } + if isHTML { + m.MIMEType = "text/html" + } else { + m.MIMEType = "text/plain" + } + if key != "" { attachPublicKey(p.Root(), key, keyName) } @@ -84,7 +92,7 @@ func collectAttachments(p *parser.Parser) (atts []*pmapi.Attachment, data []io.R return } -func collectBodyParts(p *parser.Parser) (body, plain string, err error) { +func collectBodyParts(p *parser.Parser) (body, plain string, isHTML bool, err error) { var parts, plainParts []string w := p. @@ -96,6 +104,7 @@ func collectBodyParts(p *parser.Parser) (body, plain string, err error) { }). WithContentTypeHandler("text/html", func(p *parser.Part) (err error) { parts = append(parts, string(p.Body)) + isHTML = true text, err := html2text.FromString(string(p.Body)) if err != nil { @@ -110,7 +119,7 @@ func collectBodyParts(p *parser.Parser) (body, plain string, err error) { return } - return strings.Join(parts, "\r\n"), strings.Join(plainParts, "\r\n"), nil + return strings.Join(parts, "\r\n"), strings.Join(plainParts, "\r\n"), isHTML, nil } func writeMIMEMessage(p *parser.Parser) (mime string, err error) { diff --git a/pkg/message/parser/parser.go b/pkg/message/parser/parser.go index 01c11657..1ba96f5f 100644 --- a/pkg/message/parser/parser.go +++ b/pkg/message/parser/parser.go @@ -7,6 +7,13 @@ import ( "github.com/emersion/go-message" ) +// TODO: Set this to something that handles charsets. +func init() { // nolint[gochecknoinits] + message.CharsetReader = func(string, io.Reader) (io.Reader, error) { + panic("not implemented") + } +} + type Parser struct { stack []*Part root *Part