diff options
author | Robin Jarry <robin@jarry.cc> | 2023-07-04 17:09:27 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-07-17 10:23:54 +0200 |
commit | cf061e1b0b536da1cc401c19af456ea373c47e6e (patch) | |
tree | 517611bf2bc11aeb7e3b2c097451616cc0ea30e9 /widgets/compose.go | |
parent | a9fdf417662bd203dbd923126ad6233e965cfc2d (diff) | |
download | aerc-cf061e1b0b536da1cc401c19af456ea373c47e6e.tar.gz |
compose: always use cr-lf line endings
.eml files are universally formatted with DOS line-endings, per RFC5322.
Make sure to replace line endings with CRLF before sending the message.
Always present CRLF files to the text editor. If the editor forces LF
line endings, convert them back to CRLF.
Text editors (vim and nvim at least) detect the .eml extension and force
dos line endings. Emacs seems not to care and will use whatever line
endings are present in the file. Kakoune seems to force unix line
endings and does not care about the contents of the file.
Notes:
* add explicit calls to Truncate() to ensure any trailing content is
stripped from the file when overwriting with shorter content.
* NewCRLFReader cannot be reused since we need buffered and unbuffered
versions of it (for GetBody and {Set,append}Contents, respectively).
Link: https://datatracker.ietf.org/doc/html/rfc5322
Link: https://github.com/vim/vim/blob/v8.2.3583/runtime/ftplugin/mail.vim#L29-L35
Link: https://github.com/neovim/neovim/blob/v0.6.0/runtime/ftplugin/mail.vim#L29-L35
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Bence Ferdinandy <bence@ferdinandy.com>
Tested-by: Koni Marti <koni.marti@gmail.com>
Diffstat (limited to 'widgets/compose.go')
-rw-r--r-- | widgets/compose.go | 48 |
1 files changed, 32 insertions, 16 deletions
diff --git a/widgets/compose.go b/widgets/compose.go index a243ba95..371e0839 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -428,6 +428,21 @@ func (c *Composer) updateCrypto() error { return nil } +func (c *Composer) writeCRLF(reader io.Reader) error { + // .eml files must always use '\r\n' line endings + scanner := bufio.NewScanner(reader) + for scanner.Scan() { + _, err := c.email.WriteString(scanner.Text() + "\r\n") + if err != nil { + return err + } + } + if scanner.Err() != nil { + return scanner.Err() + } + return c.email.Sync() +} + // Note: this does not reload the editor. You must call this before the first // Draw() call. func (c *Composer) setContents(reader io.Reader) error { @@ -435,15 +450,11 @@ func (c *Composer) setContents(reader io.Reader) error { if err != nil { return err } - _, err = io.Copy(c.email, reader) - if err != nil { - return err - } - err = c.email.Sync() + err = c.email.Truncate(0) if err != nil { return err } - return nil + return c.writeCRLF(reader) } func (c *Composer) appendContents(reader io.Reader) error { @@ -451,15 +462,7 @@ func (c *Composer) appendContents(reader io.Reader) error { if err != nil { return err } - _, err = io.Copy(c.email, reader) - if err != nil { - return err - } - err = c.email.Sync() - if err != nil { - return err - } - return nil + return c.writeCRLF(reader) } func (c *Composer) AppendPart(mimetype string, params map[string]string, body io.Reader) error { @@ -609,8 +612,13 @@ func (c *Composer) GetBody() (*bytes.Buffer, error) { if err != nil { return nil, err } + scanner := bufio.NewScanner(c.email) + // .eml files must always use '\r\n' line endings buf := new(bytes.Buffer) - _, err = io.Copy(buf, c.email) + for scanner.Scan() { + buf.WriteString(scanner.Text() + "\r\n") + } + err = scanner.Err() if err != nil { return nil, err } @@ -1068,6 +1076,14 @@ func (c *Composer) ShowTerminal() error { if c.editor != nil { return nil } + body, err := c.GetBody() + if err != nil { + return err + } + err = c.setContents(body) + if err != nil { + return err + } if c.review != nil { c.grid.RemoveChild(c.review) } |