* message.el (message-setup-1): Don't bind the constant -forbidden-properties.
[gnus] / lisp / mml2015.el
index 55ebf8c..1271168 100644 (file)
@@ -1,7 +1,6 @@
 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
 
-;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
-;;   2008, 2009, 2010 Free Software Foundation, Inc.
+;; Copyright (C) 2000-2011 Free Software Foundation, Inc.
 
 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
 ;; Keywords: PGP MIME MML
@@ -117,10 +116,17 @@ Whether the passphrase is cached at all is controlled by
   :type 'integer)
 
 (defcustom mml2015-signers nil
-  "A list of your own key ID which will be used to sign a message."
+  "A list of your own key ID which will be used to sign a message.
+If set, it overrides the setting of `mml2015-sign-with-sender'."
   :group 'mime-security
   :type '(repeat (string :tag "Key ID")))
 
+(defcustom mml2015-sign-with-sender nil
+  "If t, use message sender so find a key to sign with."
+  :group 'mime-security
+  :type 'boolean
+  :version "24.1")
+
 (defcustom mml2015-encrypt-to-self nil
   "If t, add your own key ID to recipient list when encryption."
   :group 'mime-security
@@ -742,6 +748,7 @@ Whether the passphrase is cached at all is controlled by
 (autoload 'epg-key-sub-key-list "epg")
 (autoload 'epg-sub-key-capability "epg")
 (autoload 'epg-sub-key-validity "epg")
+(autoload 'epg-sub-key-fingerprint "epg")
 (autoload 'epg-configuration "epg-config")
 (autoload 'epg-expand-group "epg-config")
 (autoload 'epa-select-keys "epa")
@@ -784,6 +791,24 @@ Whether the passphrase is cached at all is controlled by
          (setq pointer (cdr pointer))))
       (setq keys (cdr keys)))))
 
+;; XXX: since gpg --list-secret-keys does not return validity of each
+;; key, `mml2015-epg-find-usable-key' defined above is not enough for
+;; secret keys.  The function `mml2015-epg-find-usable-secret-key'
+;; below looks at appropriate public keys to check usability.
+(defun mml2015-epg-find-usable-secret-key (context name usage)
+  (let ((secret-keys (epg-list-keys context name t))
+       secret-key)
+    (while (and (not secret-key) secret-keys)
+      (if (mml2015-epg-find-usable-key
+          (epg-list-keys context (epg-sub-key-fingerprint
+                                  (car (epg-key-sub-key-list
+                                        (car secret-keys)))))
+          usage)
+         (setq secret-key (car secret-keys)
+               secret-keys nil)
+       (setq secret-keys (cdr secret-keys))))
+    secret-key))
+
 (defun mml2015-epg-decrypt (handle ctl)
   (catch 'error
     (let ((inhibit-redisplay t)
@@ -941,6 +966,10 @@ Whether the passphrase is cached at all is controlled by
   (let* ((inhibit-redisplay t)
         (context (epg-make-context))
         (boundary (mml-compute-boundary cont))
+        (sender (message-options-get 'message-sender))
+        (signer-names (or mml2015-signers
+                          (if (and mml2015-sign-with-sender sender)
+                              (list (concat "<" sender ">")))))
         signer-key
         (signers
          (or (message-options-get 'mml2015-epg-signers)
@@ -950,14 +979,15 @@ Whether the passphrase is cached at all is controlled by
                   (epa-select-keys context "\
 Select keys for signing.
 If no one is selected, default secret key is used.  "
-                                   mml2015-signers t)
-                (if mml2015-signers
+                                   signer-names
+                                   t)
+                (if (or sender mml2015-signers)
                     (delq nil
                           (mapcar
                            (lambda (signer)
-                             (setq signer-key (mml2015-epg-find-usable-key
-                                               (epg-list-keys context signer t)
-                                               'sign))
+                             (setq signer-key
+                                   (mml2015-epg-find-usable-secret-key
+                                    context signer 'sign))
                              (unless (or signer-key
                                          (y-or-n-p
                                           (format
@@ -965,7 +995,7 @@ If no one is selected, default secret key is used.  "
                                            signer)))
                                (error "No secret key for %s" signer))
                              signer-key)
-                           mml2015-signers)))))))
+                           signer-names)))))))
         signature micalg)
     (epg-context-set-armor context t)
     (epg-context-set-textmode context t)
@@ -1005,13 +1035,18 @@ If no one is selected, default secret key is used.  "
     (goto-char (point-max))))
 
 (defun mml2015-epg-encrypt (cont &optional sign)
-  (let ((inhibit-redisplay t)
-       (context (epg-make-context))
-       (config (epg-configuration))
-       (recipients (message-options-get 'mml2015-epg-recipients))
-       cipher signers
-       (boundary (mml-compute-boundary cont))
-       recipient-key signer-key)
+  (let* ((inhibit-redisplay t)
+        (context (epg-make-context))
+        (boundary (mml-compute-boundary cont))
+        (config (epg-configuration))
+        (recipients (message-options-get 'mml2015-epg-recipients))
+        cipher
+        (sender (message-options-get 'message-sender))
+        (signer-names (or mml2015-signers
+                          (if (and mml2015-sign-with-sender sender)
+                              (list (concat "<" sender ">")))))
+        signers
+        recipient-key signer-key)
     (unless recipients
       (setq recipients
            (apply #'nconc
@@ -1025,9 +1060,9 @@ If no one is selected, default secret key is used.  "
                                              (read-string "Recipients: ")))
                     "[ \f\t\n\r\v,]+"))))
       (when mml2015-encrypt-to-self
-       (unless mml2015-signers
-         (error "mml2015-signers not set"))
-       (setq recipients (nconc recipients mml2015-signers)))
+       (unless signer-names
+         (error "Neither message sender nor mml2015-signers are set"))
+       (setq recipients (nconc recipients signer-names)))
       (if (eq mm-encrypt-option 'guided)
          (setq recipients
                (epa-select-keys context "\
@@ -1060,14 +1095,15 @@ If no one is selected, symmetric encryption will be performed.  "
                     (epa-select-keys context "\
 Select keys for signing.
 If no one is selected, default secret key is used.  "
-                                     mml2015-signers t)
-                  (if mml2015-signers
+                                     signer-names
+                                     t)
+                  (if (or sender mml2015-signers)
                       (delq nil
                             (mapcar
                              (lambda (signer)
-                               (setq signer-key (mml2015-epg-find-usable-key
-                                                 (epg-list-keys context signer t)
-                                                 'sign))
+                               (setq signer-key
+                                     (mml2015-epg-find-usable-secret-key
+                                      context signer 'sign))
                                (unless (or signer-key
                                            (y-or-n-p
                                             (format
@@ -1075,7 +1111,7 @@ If no one is selected, default secret key is used.  "
                                              signer)))
                                  (error "No secret key for %s" signer))
                                signer-key)
-                             mml2015-signers)))))))
+                             signer-names)))))))
       (epg-context-set-signers context signers))
     (epg-context-set-armor context t)
     (epg-context-set-textmode context t)