Fix a gdb-mi process filtering issue arising in ansi-color.el.
[bpt/emacs.git] / lisp / epa-mail.el
index e6f6c0e..be5b849 100644 (file)
@@ -1,5 +1,5 @@
 ;;; epa-mail.el --- the EasyPG Assistant, minor-mode for mail composer -*- lexical-binding: t -*-
-;; Copyright (C) 2006-2011 Free Software Foundation, Inc.
+;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
 
 ;; Author: Daiki Ueno <ueno@unixuser.org>
 ;; Keywords: PGP, GnuPG, mail, message
 
 ;;;###autoload
 (define-minor-mode epa-mail-mode
-  "A minor-mode for composing encrypted/clearsigned mails."
+  "A minor-mode for composing encrypted/clearsigned mails.
+With a prefix argument ARG, enable the mode if ARG is positive,
+and disable it otherwise.  If called from Lisp, enable the mode
+if ARG is omitted or nil."
   nil " epa-mail" epa-mail-mode-map)
 
-;;; ??? Could someone please clarify this doc string?
-;;; In particular, what does USAGE look like
-;;; and what does it mean?  -- rms
 (defun epa-mail--find-usable-key (keys usage)
-  "Find a usable key from KEYS for USAGE."
+  "Find a usable key from KEYS for USAGE.
+USAGE would be `sign' or `encrypt'."
   (catch 'found
     (while keys
       (let ((pointer (epg-key-sub-key-list (car keys))))
          (setq pointer (cdr pointer))))
       (setq keys (cdr keys)))))
 
-(defvar epa-mail-group-alist nil
-  "Alist of GnuPG mail groups (`group' commands in `.gnupg/gpg.conf').
-Each element has the form (GROUPNAME ADDRESSES...).
-t means the list is not yet read in.")
-
-(defvar epa-mail-group-modtime nil
-  "The modification time of `~/.gnupg/gpg.conf' file when last examined.")
-
-(defvar epa-mail-gnupg-conf-file "~/.gnupg/gpg.conf"
-  "File name of GnuPG configuration file that specifies recipient groups.")
-
-(defun epa-mail-parse-groups ()
-  "Parse `~/.gnupg/gpg.conf' and set `epa-mail-group-alist' from it."
-  (let (aliases)
-    (with-temp-buffer
-      (insert-file-contents-literally epa-mail-gnupg-conf-file)
-
-      (while (re-search-forward "^[ \t]*group[ \t]*" nil t)
-       (if (looking-at "\\([^= \t]+\\)[ \t]*=[ \t]*\\([^ \t\n]+\\)")
-           (push (cons (match-string-no-properties 1)
-                       (split-string (match-string-no-properties 2)))
-                 aliases))))
-    (setq epa-mail-group-alist aliases)))
-
-(defun epa-mail-sync-groups ()
-  "Update GnuPG groups from file if necessary."
-  (if (file-exists-p epa-mail-gnupg-conf-file)
-      (let ((modtime (nth 5 (file-attributes epa-mail-gnupg-conf-file))))
-       (if (not (equal epa-mail-group-modtime modtime))
-           (progn
-             (setq epa-mail-group-modtime modtime)
-             (epa-mail-parse-groups))))
-    (setq epa-mail-group-alist nil)))
-
-(defun epa-mail-expand-recipient-1 (recipient)
-  "Expand RECIPIENT once thru `epa-mail-group-alist'.
-Returns the list of names it stands for, or nil if it isn't a group."
-  ;; Load the alias list if not loaded before.
-  (let (alist-elt)
-    (setq alist-elt (assoc recipient epa-mail-group-alist))
-    (cdr alist-elt)))
-
-(defun epa-mail-expand-recipients-2 (recipients)
-  "Expand list RECIPIENTS once thru `epa-mail-group-alist'.
-Returns the list of names they stand for."
-  ;; Load the alias list if not loaded before.
-  (let (output)
-    (dolist (r recipients)
-      (let ((expanded (epa-mail-expand-recipient-1 r)))
-       (if expanded
-           (dolist (xr expanded)
-             (unless (member xr output)
-               (push xr output)))
-         (unless (member r output)
-           (push r output)))))
-    (nreverse output)))
-
-(defun epa-mail-expand-recipients (recipients)
-  "Expand RECIPIENTS thru `epa-mail-group-alist' until it stops changing."
-  (epa-mail-sync-groups)
-  (while (not (equal recipients
-                    (setq recipients
-                          (epa-mail-expand-recipients-2 recipients)))))
-  recipients)
-
 ;;;###autoload
 (defun epa-mail-decrypt ()
   "Decrypt OpenPGP armors in the current buffer.
@@ -184,6 +120,7 @@ Don't use this command in Lisp programs!"
   (interactive
    (save-excursion
      (let ((verbose current-prefix-arg)
+          (config (epg-configuration))
           (context (epg-make-context epa-protocol))
           recipients-string recipients recipient-key sign)
        (goto-char (point-min))
@@ -211,9 +148,13 @@ Don't use this command in Lisp programs!"
 
        ;; Process all the recipients thru the list of GnuPG groups.
        ;; Expand GnuPG group names to what they stand for.
-       ;; The code below, and elsewhere, that checks that names have keys
-       ;; does not know about these group names.
-       (setq recipients (epa-mail-expand-recipients recipients))
+       (setq recipients
+            (apply #'nconc
+                   (mapcar
+                    (lambda (recipient)
+                      (or (epg-expand-group config recipient)
+                          (list recipient)))
+                    recipients)))
 
        (goto-char (point-min))
        (if (search-forward mail-header-separator nil t)
@@ -251,7 +192,9 @@ If no one is selected, symmetric encryption will be performed.  "
             (if sign
                 (epa-select-keys context
                                  "Select keys for signing.  "))))))
-  (epa-encrypt-region start end recipients sign signers))
+  ;; Don't let some read-only text stop us from encrypting.
+  (let ((inhibit-read-only t))
+    (epa-encrypt-region start end recipients sign signers)))
 
 ;;;###autoload
 (defun epa-mail-import-keys ()
@@ -264,7 +207,10 @@ Don't use this command in Lisp programs!"
 
 ;;;###autoload
 (define-minor-mode epa-global-mail-mode
-  "Minor mode to hook EasyPG into Mail mode."
+  "Minor mode to hook EasyPG into Mail mode.
+With a prefix argument ARG, enable the mode if ARG is positive,
+and disable it otherwise.  If called from Lisp, enable the mode
+if ARG is omitted or nil."
   :global t :init-value nil :group 'epa-mail :version "23.1"
   (remove-hook 'mail-mode-hook 'epa-mail-mode)
   (if epa-global-mail-mode