aboutsummaryrefslogtreecommitdiff
path: root/pkg/util
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2021-02-15 01:28:07 +0100
committerRené 'Necoro' Neumann <necoro@necoro.eu>2021-02-15 01:28:07 +0100
commit95d79341bff3c66c6d32b373994d3a1be26295b9 (patch)
tree5c70d17f098c129fd1316b647aa57e3477a2f025 /pkg/util
parent725a6dcd20abac7a629085bc4495d6e29ee3ed06 (diff)
downloadfeed2imap-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.
Diffstat (limited to '')
-rw-r--r--pkg/util/fixWriter.go63
1 files changed, 63 insertions, 0 deletions
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}
+}