- (while (looking-at "^Content[^ ]+:") (forward-line))
- (if (> (point) (point-min))
- (progn
- (kill-region (point-min) (point))))
- (mm-with-unibyte-current-buffer-mule4
- (with-temp-buffer
- (unless (gpg-sign-encrypt
- text (setq cipher (current-buffer))
- result-buffer
- (split-string
- (or
- (message-options-get 'message-recipients)
- (message-options-set 'message-recipients
- (read-string "Recipients: ")))
- "[ \f\t\n\r\v,]+")
- nil
- (message-options-get 'message-sender)
- t t) ; armor & textmode
- (unless (> (point-max) (point-min))
- (pop-to-buffer result-buffer)
- (error "Encrypt error")))
- (goto-char (point-min))
- (while (re-search-forward "\r+$" nil t)
- (replace-match "" t t))
- (set-buffer text)
- (kill-region (point-min) (point-max))
- ;;(insert "Content-Type: application/pgp-encrypted\n\n")
- ;;(insert "Version: 1\n\n")
- (insert "\n")
- (insert-buffer cipher)
- (goto-char (point-max))))))
+ (when (re-search-forward "^$" nil t)
+ (setq headers (buffer-substring (point-min) (point)))
+ (save-restriction
+ (narrow-to-region (point-min) (point))
+ (setq cte (mail-fetch-field "content-transfer-encoding")))
+ (forward-line 1)
+ (delete-region (point-min) (point))
+ (when cte
+ (setq cte (intern (downcase cte)))
+ (mm-decode-content-transfer-encoding cte)))
+ (condition-case error
+ (setq signature (epg-sign-string context (buffer-string) 'clear)
+ mml1991-epg-secret-key-id-list nil)
+ (error
+ (while mml1991-epg-secret-key-id-list
+ (password-cache-remove (car mml1991-epg-secret-key-id-list))
+ (setq mml1991-epg-secret-key-id-list
+ (cdr mml1991-epg-secret-key-id-list)))
+ (signal (car error) (cdr error))))
+ (delete-region (point-min) (point-max))
+ (mm-with-unibyte-current-buffer
+ (insert signature)
+ (goto-char (point-min))
+ (while (re-search-forward "\r+$" nil t)
+ (replace-match "" t t))
+ (when cte
+ (mm-encode-content-transfer-encoding cte))
+ (goto-char (point-min))
+ (when headers
+ (insert headers))
+ (insert "\n"))
+ t))
+
+(defun mml1991-epg-encrypt (cont &optional sign)
+ (goto-char (point-min))
+ (when (re-search-forward "^$" nil t)
+ (let ((cte (save-restriction
+ (narrow-to-region (point-min) (point))
+ (mail-fetch-field "content-transfer-encoding"))))
+ ;; Strip MIME headers since it will be ASCII armored.
+ (forward-line 1)
+ (delete-region (point-min) (point))
+ (when cte
+ (mm-decode-content-transfer-encoding (intern (downcase cte))))))
+ (let ((context (epg-make-context))
+ (recipients
+ (if (message-options-get 'message-recipients)
+ (split-string
+ (message-options-get 'message-recipients)
+ "[ \f\t\n\r\v,]+")))
+ cipher signers config)
+ ;; We should remove this check if epg-0.0.6 is released.
+ (if (and (condition-case nil
+ (require 'epg-config)
+ (error))
+ (functionp #'epg-expand-group))
+ (setq config (epg-configuration)
+ recipients
+ (apply #'nconc
+ (mapcar (lambda (recipient)
+ (or (epg-expand-group config recipient)
+ (list recipient)))
+ recipients))))
+ (if (eq mm-encrypt-option 'guided)
+ (setq recipients
+ (epa-select-keys context "Select recipients for encryption.
+If no one is selected, symmetric encryption will be performed. "
+ recipients))
+ (setq recipients
+ (delq nil (mapcar (lambda (name)
+ (car (epg-list-keys context name)))
+ recipients))))
+ (if mml1991-encrypt-to-self
+ (if mml1991-signers
+ (setq recipients
+ (nconc recipients
+ (mapcar (lambda (name)
+ (car (epg-list-keys context name)))
+ mml1991-signers)))
+ (error "mml1991-signers not set")))
+ (when sign
+ (if (eq mm-sign-option 'guided)
+ (setq signers (epa-select-keys context "Select keys for signing.
+If no one is selected, default secret key is used. "
+ mml1991-signers t))
+ (if mml1991-signers
+ (setq signers (mapcar (lambda (name)
+ (car (epg-list-keys context name t)))
+ mml1991-signers))))
+ (epg-context-set-signers context signers))
+ (epg-context-set-armor context t)
+ (epg-context-set-textmode context t)
+ (if mml1991-cache-passphrase
+ (epg-context-set-passphrase-callback
+ context
+ #'mml1991-epg-passphrase-callback))
+ (condition-case error
+ (setq cipher
+ (epg-encrypt-string context (buffer-string) recipients sign)
+ mml1991-epg-secret-key-id-list nil)
+ (error
+ (while mml1991-epg-secret-key-id-list
+ (password-cache-remove (car mml1991-epg-secret-key-id-list))
+ (setq mml1991-epg-secret-key-id-list
+ (cdr mml1991-epg-secret-key-id-list)))
+ (signal (car error) (cdr error))))
+ (delete-region (point-min) (point-max))
+ (insert "\n" cipher))
+ t)