From 56c9f940ce07fd8d93e08a562e1f5cf7e1a58c6a Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Sat, 12 Dec 2020 18:05:57 +0100 Subject: [PATCH] Fix line-ending issues with MIME and Outlook Outlook seems to expect CRLF in S/MIME signed+encrypted mails, so we add those somewhere in the process of encoding the mail. Furthermore, Outlook is sending MIME messages with CRLF line endings, and we have to take care of that when looking for the end headers. The changes proposed here are preliminary and subject to further testing. --- init.el | 31 ++++++++++++++++++++++++++----- site-lisp/db-utils.el | 20 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/init.el b/init.el index 04d2596..2950318 100644 --- a/init.el +++ b/init.el @@ -613,7 +613,9 @@ db/system-open db/switch-to-dark-theme db/switch-to-light-theme - keyboard-quit-context+)) + keyboard-quit-context+ + db/convert-lf-to-crlf-in-buffer + db/convert-crlf-to-lf-in-buffer)) (use-package db-hydras :commands (hydra-toggle/body @@ -1661,7 +1663,17 @@ (add-to-list 'mm-inlined-types "application/pgp$") (add-to-list 'mm-inline-media-tests '("application/pgp$" mm-inline-text identity)) - (add-to-list 'mm-automatic-display "application/pgp$"))) + (add-to-list 'mm-automatic-display "application/pgp$") + + ;; When copying MIME buffers, we are looking for the start of the + ;; header by searching ^\n; however, if we received mail from + ;; Outlook, there's an ^\r\n seperating header and body, which is + ;; not found by `mm-copy-to-buffer', leaving the target buffer empty + ;; and the mail as well. Replacing all \r\n with \n before copying + ;; buffers seems to help. + + (advice-add 'mm-copy-to-buffer + :before #'db/convert-crlf-to-lf-in-buffer))) (setq message-forward-as-mime t) @@ -1687,9 +1699,7 @@ :init (setq mm-encrypt-option nil mm-sign-option nil)) -(setq mml-smime-use 'epg - ;;mml2015-encrypt-to-self t - mml2015-display-key-image nil +(setq mml2015-display-key-image nil gnus-message-replysign t gnus-message-replyencrypt t gnus-message-replysignencrypted t @@ -1697,6 +1707,17 @@ mml-secure-openpgp-sign-with-sender t mml-secure-smime-sign-with-sender t) +(use-package mml-smime + :init (setq mml-smime-use 'epg) + :config (progn + ;; Outlook seems to expect \r\n in PKCS#7 encrypted containers, but + ;; Gnus is only sending \n; so let's artificially replace \n by \r\n + ;; before, well, signing? Seems to work at least in the case where + ;; we are sending S/MIME encrypted and signed messages + + (advice-add 'mml-smime-epg-sign + :after #'db/convert-lf-to-crlf-in-buffer) )) + ;; Archiving ;; We store messages in the current group, so there is diff --git a/site-lisp/db-utils.el b/site-lisp/db-utils.el index 099c006..5e1cfc8 100644 --- a/site-lisp/db-utils.el +++ b/site-lisp/db-utils.el @@ -404,6 +404,26 @@ regardless of the currently selected window." (let ((debug-on-quit nil)) (signal 'quit nil))))) +(defun db/convert-lf-to-crlf-in-buffer (&rest _stuff) + "Convert all LF to CRLF in current buffer. +Does not replace CRLF with CRCRLF, and so on." + (save-mark-and-excursion + (save-restriction + (widen) + (goto-char (point-min)) + (while (re-search-forward "\n" nil 'noerror) + (unless (looking-back "\r\n" 2) + (replace-match "\r\n")))))) + +(defun db/convert-crlf-to-lf-in-buffer (&rest _stuff) + "Convert all CRLF to LF in current buffer." + (save-mark-and-excursion + (save-restriction + (widen) + (goto-char (point-min)) + (while (re-search-forward "\r\n" nil 'noerror) + (replace-match "\n"))))) + ;;; Extend Input Methods