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