diff --git a/pkg/message/parser.go b/pkg/message/parser.go index e992da11..67df6ae7 100644 --- a/pkg/message/parser.go +++ b/pkg/message/parser.go @@ -45,6 +45,7 @@ func Parse(r io.Reader, key, keyName string) (m *pmapi.Message, mimeBody, plainB } if key != "" { + // TODO: This is currently broken! if err = attachPublicKey(p.Root(), key, keyName); err != nil { return } diff --git a/pkg/message/parser/part.go b/pkg/message/parser/part.go index 141a2885..3f8c1036 100644 --- a/pkg/message/parser/part.go +++ b/pkg/message/parser/part.go @@ -97,3 +97,13 @@ func selectDecoderFromParams(params map[string]string) *encoding.Decoder { return decoder } + +func (p *Part) is7BitClean() bool { + for _, b := range p.Body { + if b > 1<<7 { + return false + } + } + + return true +} diff --git a/pkg/message/parser/writer.go b/pkg/message/parser/writer.go index a6dfade5..e1e4e3d5 100644 --- a/pkg/message/parser/writer.go +++ b/pkg/message/parser/writer.go @@ -25,28 +25,16 @@ import ( type Writer struct { root *Part - cond []Condition } -type Condition func(p *Part) bool - func newWriter(root *Part) *Writer { return &Writer{ root: root, } } -// WithCondition allows setting a condition when parts should be written. -// Parts are passed to each condition set and if any condition returns false, -// the part is not written. -// This initially seemed like a good idea but is now kinda useless. -func (w *Writer) WithCondition(cond Condition) *Writer { - w.cond = append(w.cond, cond) - return w -} - func (w *Writer) Write(ww io.Writer) error { - if w.shouldFilter(w.root) { + if !w.root.is7BitClean() { w.root.Header.Add("Content-Transfer-Encoding", "base64") } @@ -62,32 +50,6 @@ func (w *Writer) Write(ww io.Writer) error { return msgWriter.Close() } -func (w *Writer) shouldWrite(p *Part) bool { - for _, cond := range w.cond { - if !cond(p) { - return false - } - } - - return true -} - -func (w *Writer) shouldFilter(p *Part) bool { - encoding := p.Header.Get("Content-Transfer-Encoding") - - if encoding != "" && encoding == "quoted-printable" || encoding == "base64" { - return false - } - - for _, b := range p.Body { - if b > 1<<7 { - return true - } - } - - return false -} - func (w *Writer) write(writer *message.Writer, p *Part) error { if len(p.children) > 0 { for _, child := range p.children { @@ -105,11 +67,7 @@ func (w *Writer) write(writer *message.Writer, p *Part) error { } func (w *Writer) writeAsChild(writer *message.Writer, p *Part) error { - if !w.shouldWrite(p) { - return nil - } - - if w.shouldFilter(p) { + if !p.is7BitClean() { p.Header.Add("Content-Transfer-Encoding", "base64") } diff --git a/pkg/message/parser/writer_test.go b/pkg/message/parser/writer_test.go index e8e8ce8d..c64ea774 100644 --- a/pkg/message/parser/writer_test.go +++ b/pkg/message/parser/writer_test.go @@ -36,26 +36,6 @@ func TestParserWrite(t *testing.T) { assert.Equal(t, s("text_html_octet_attachment.eml"), crlf(buf.String())) } -func TestParserWriteNoAttachments(t *testing.T) { - p := newTestParser(t, "text_html_octet_attachment.eml") - - w := p. - NewWriter(). - WithCondition(func(p *Part) bool { - // We don't write if the content disposition says it's an attachment. - if disp, _, err := p.Header.ContentDisposition(); err == nil && disp == "attachment" { - return false - } - - return true - }) - - buf := new(bytes.Buffer) - - assert.NoError(t, w.Write(buf)) - assert.Equal(t, s("text_html.eml"), crlf(buf.String())) -} - func crlf(s string) string { return strings.ReplaceAll(s, "\r\n", "\n") }