refactor: remove dead code

This commit is contained in:
James Houlahan
2020-08-14 15:31:16 +02:00
parent 9821b5bbc2
commit 9ba08e5edb
4 changed files with 13 additions and 64 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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")
}

View File

@ -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")
}