diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2021-02-15 01:28:07 +0100 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2021-02-15 01:28:07 +0100 |
commit | 95d79341bff3c66c6d32b373994d3a1be26295b9 (patch) | |
tree | 5c70d17f098c129fd1316b647aa57e3477a2f025 | |
parent | 725a6dcd20abac7a629085bc4495d6e29ee3ed06 (diff) | |
download | feed2imap-go-95d79341bff3c66c6d32b373994d3a1be26295b9.tar.gz feed2imap-go-95d79341bff3c66c6d32b373994d3a1be26295b9.tar.bz2 feed2imap-go-95d79341bff3c66c6d32b373994d3a1be26295b9.zip |
Issue #46: Make the resulting email body not to include single \r or \n. This now pleases Cyrus IMAP.
-rw-r--r-- | internal/feed/mail.go | 5 | ||||
-rw-r--r-- | pkg/util/fixWriter.go | 63 |
2 files changed, 66 insertions, 2 deletions
diff --git a/internal/feed/mail.go b/internal/feed/mail.go index 2dda3ca..0bc7d37 100644 --- a/internal/feed/mail.go +++ b/internal/feed/mail.go @@ -11,11 +11,11 @@ import ( "strings" "time" - "github.com/jaytaylor/html2text" "github.com/PuerkitoBio/goquery" "github.com/emersion/go-message" "github.com/emersion/go-message/mail" "github.com/gabriel-vasile/mimetype" + "github.com/jaytaylor/html2text" "golang.org/x/net/html" "github.com/Necoro/feed2imap-go/internal/feed/template" @@ -23,6 +23,7 @@ import ( "github.com/Necoro/feed2imap-go/internal/msg" "github.com/Necoro/feed2imap-go/pkg/config" "github.com/Necoro/feed2imap-go/pkg/log" + "github.com/Necoro/feed2imap-go/pkg/util" "github.com/Necoro/feed2imap-go/pkg/version" ) @@ -97,7 +98,7 @@ func (item *item) writeContentPart(w *message.Writer, typ string, tpl template.T } defer partW.Close() - if err = tpl.Execute(w, item); err != nil { + if err = tpl.Execute(util.FixWriter(w), item); err != nil { return fmt.Errorf("writing %s part: %w", typ, err) } diff --git a/pkg/util/fixWriter.go b/pkg/util/fixWriter.go new file mode 100644 index 0000000..51637eb --- /dev/null +++ b/pkg/util/fixWriter.go @@ -0,0 +1,63 @@ +package util + +import "io" + +type fixWriter struct { + w io.Writer +} + +var lf = []byte{'\n'} +var cr = []byte{'\r'} + +func (f fixWriter) Write(p []byte) (n int, err error) { + crFound := false + start := 0 + + write := func(str []byte) { + var j int + j, err = f.w.Write(str) + n = n + j + } + + for idx, b := range p { + if crFound && b != '\n' { + // insert '\n' + if write(p[start:idx]); err != nil { + return + } + if write(lf); err != nil { + return + } + + start = idx + } else if !crFound && b == '\n' { + // insert '\r' + if write(p[start:idx]); err != nil { + return + } + if write(cr); err != nil { + return + } + + start = idx + } + crFound = b == '\r' + } + + // write the remainder + if write(p[start:]); err != nil { + return + } + + if crFound { // dangling \r + write(lf) + } + + return +} + +// Cyrus IMAP really cares about single \r and \n. +// Implement this fixer to change them into \r\n. +func FixWriter(w io.Writer) io.Writer { + return &fixWriter{w} +} |