Function: rmail-epa-decrypt

rmail-epa-decrypt is an interactive and byte-compiled function defined in rmail.el.gz.

Signature

(rmail-epa-decrypt)

Documentation

Decrypt GnuPG or OpenPGP armors in current message.

View in manual

Probably introduced at or before Emacs version 24.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/mail/rmail.el.gz
(defvar rmail-mime-render-html-function) ; defcustom in rmailmm
(defun rmail-epa-decrypt ()
  "Decrypt GnuPG or OpenPGP armors in current message."
  (interactive)

  ;; Save the current buffer here for cleanliness, in case we
  ;; change it in one of the calls to `epa-decrypt-region'.

  (save-excursion
    (let (decrypts
          (mime (and (eq major-mode 'rmail-mode) (rmail-mime-message-p)))
          mime-disabled)
      (goto-char (point-min))

      ;; Turn off mime processing.
      (when (and mime
		 (not (get-text-property (point-min) 'rmail-mime-hidden)))
        (setq mime-disabled t)
	(rmail-mime))

      ;; Now find all armored messages in the buffer
      ;; and decrypt them one by one.
      (goto-char (point-min))
      (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
	(let ((coding-system-for-read coding-system-for-read)
	      (case-fold-search t)
	      (armor-start (match-beginning 0)))
	  ;; Don't decrypt an armor that was copied into
	  ;; the message from a message it is a reply to.
	  (or (equal (buffer-substring (line-beginning-position)
				       armor-start)
		     "> ")
	      (push (rmail-epa-decrypt-1 mime) decrypts))))

      (when (and decrypts (rmail-buffers-swapped-p))
	(if (not (y-or-n-p "Replace the original message? "))
            ;; User wants to decrypt only temporarily.
	    ;; Find, in the view buffer, the armors
	    ;; that we made decrypts for, and replace each one
            ;; with its decrypt.  In a mime part, replace CRLF with NL.
            (dolist (d decrypts)
	      (if (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
		  (let (armor-start armor-end armor-end-regexp)
		    (setq armor-start (match-beginning 0)
			  armor-end-regexp (nth 3 d)
			  armor-end (re-search-forward
				     armor-end-regexp
				     nil t))

		    ;; Found as expected -- now replace it with the decrypt.
		    (when armor-end
                      (if (null (nth 2 d))
                          nil
                        ;; In a mime part --
                        ;; replace CRLF with NL in it.
                        (save-restriction
                          (narrow-to-region armor-start armor-end)
                          (goto-char (point-min))
                          (while (search-forward "\r\n" nil t)
                            (delete-region (- (point) 2) (- (point) 1))))))
		      )))
          ;; User wants to decrypt the message permanently.
          (when (eq major-mode 'rmail-mode)
            (rmail-add-label "decrypt"))
	  (setq decrypts (nreverse decrypts))
	  (let ((beg (rmail-msgbeg rmail-current-message))
		(end (rmail-msgend rmail-current-message)))
	    (with-current-buffer rmail-view-buffer
	      (narrow-to-region beg end)
	      (goto-char (point-min))
	      (dolist (d decrypts)
		;; Find, in the real Rmail buffer, the same armors
		;; that we found and decrypted in the view buffer.
		(if (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
		    (let (armor-start armor-end armor-end-regexp)
		      (setq armor-start (match-beginning 0)
			    armor-end-regexp (nth 3 d)
			    armor-end (re-search-forward
				       armor-end-regexp
				       nil t))

		      ;; Found as expected -- now replace it with the decrypt.
		      (when armor-end
			(delete-region armor-start armor-end)
                        (insert (nth 4 d)))

		      ;; Change the mime type (if this is in a mime part)
		      ;; so this part will display by default
		      ;; when the message is shown later.
		      (when (nth 2 d)
			(goto-char armor-start)
			(when (re-search-backward "^--" nil t)
			  (save-restriction
			    (narrow-to-region (point) armor-start)
			    (when (re-search-forward "^content-type[ \t\n]*:[ \t\n]*" nil t)
			      (when (looking-at "[^\n \t;]+")
				(let ((value (match-string 0)))
				  (unless (member value '("text/plain" "text/html"))
				    (replace-match "text/plain"))))))))
		      )))))))

      (when (and (null decrypts)
                 mime mime-disabled)
        ;; Re-enable mime processing.
	(rmail-mime)
        ;; Find each Show button and show that part.
	(while (search-forward " Show " nil t)
	  (forward-char -2)
	  (let ((rmail-mime-render-html-function nil)
		(entity (get-text-property (point) 'rmail-mime-entity)))
            (unless (and (not (stringp entity))
                         (rmail-mime-entity-truncated entity))
              (push-button))))
        (goto-char (point-min))
        (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
          (let ((coding-system-for-read coding-system-for-read)
                (case-fold-search t))
            (push (rmail-epa-decrypt-1 mime) decrypts)))

        )

      (unless decrypts
	(error "Nothing to decrypt")))))