+(defun mml1991-pgg-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))))))
+ (unless (let ((pgg-text-mode t))
+ (pgg-encrypt-region
+ (point-min) (point-max)
+ (split-string
+ (or
+ (message-options-get 'message-recipients)
+ (message-options-set 'message-recipients
+ (read-string "Recipients: ")))
+ "[ \f\t\n\r\v,]+")
+ sign))
+ (pop-to-buffer pgg-errors-buffer)
+ (error "Encrypt error"))
+ (delete-region (point-min) (point-max))
+ (insert "\n")
+ (insert-buffer-substring pgg-output-buffer)
+ t)
+
+;; epg wrapper
+
+(defvar epg-user-id-alist)
+
+(autoload 'epg-make-context "epg")
+(autoload 'epg-passphrase-callback-function "epg")
+(autoload 'epa-select-keys "epa")
+(autoload 'epg-list-keys "epg")
+(autoload 'epg-context-set-armor "epg")
+(autoload 'epg-context-set-textmode "epg")
+(autoload 'epg-context-set-signers "epg")
+(autoload 'epg-context-set-passphrase-callback "epg")
+(autoload 'epg-sign-string "epg")
+(autoload 'epg-encrypt-string "epg")
+(autoload 'epg-configuration "epg-config")
+(autoload 'epg-expand-group "epg-config")
+
+(defvar mml1991-epg-secret-key-id-list nil)
+
+(defun mml1991-epg-passphrase-callback (context key-id ignore)
+ (if (eq key-id 'SYM)
+ (epg-passphrase-callback-function context key-id nil)
+ (let* ((entry (assoc key-id epg-user-id-alist))
+ (passphrase
+ (password-read
+ (format "GnuPG passphrase for %s: "
+ (if entry
+ (cdr entry)
+ key-id))
+ (if (eq key-id 'PIN)
+ "PIN"
+ key-id))))
+ (when passphrase
+ (let ((password-cache-expiry mml1991-passphrase-cache-expiry))
+ (password-cache-add key-id passphrase))
+ (setq mml1991-epg-secret-key-id-list
+ (cons key-id mml1991-epg-secret-key-id-list))
+ (copy-sequence passphrase)))))
+
+(defun mml1991-epg-sign (cont)
+ (let ((context (epg-make-context))
+ headers cte signers signature)
+ (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-armor context t)
+ (epg-context-set-textmode context t)
+ (epg-context-set-signers context signers)
+ (if mml1991-cache-passphrase
+ (epg-context-set-passphrase-callback
+ context
+ #'mml1991-epg-passphrase-callback))