From 95d79341bff3c66c6d32b373994d3a1be26295b9 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Mon, 15 Feb 2021 01:28:07 +0100 Subject: Issue #46: Make the resulting email body not to include single \r or \n. This now pleases Cyrus IMAP. --- internal/feed/mail.go | 5 ++-- pkg/util/fixWriter.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 pkg/util/fixWriter.go 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} +} -- cgit v1.2.3-70-g09d2