(gnus-group-make-rss-group): Strip newlines and excessive whitespace
[gnus] / lisp / auth-source.el
index 863928b..1bec08f 100644 (file)
@@ -1,26 +1,24 @@
 ;;; auth-source.el --- authentication sources for Gnus and Emacs
 
-;; Copyright (C) 2008 Free Software Foundation, Inc.
+;; Copyright (C) 2008, 2009 Free Software Foundation, Inc.
 
 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
 ;; Keywords: news
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -37,7 +35,8 @@
 
 ;; if you want encrypted sources, which is strongly recommended, do
 ;; (require 'epa-file)
-;; (epa-file-mode)
+;; (epa-file-enable)
+;; (setq epa-file-cache-passphrase-for-symmetric-encryption t) ; VERY important
 
 ;; before you put some data in ~/.authinfo.gpg (the default place)
 
 ;;; digest).  If you want finer controls, explore the url-auth source
 ;;; code and variables.
 
+;;; For tramp authentication, use:
+
+;;; machine yourmachine.com port scp login testuser password testpass
+
+;;; Note that the port denotes the Tramp connection method.  When you
+;;; don't use a port entry, you match any Tramp method.
+
 ;;; Code:
 
+(require 'gnus-util)
+
 (eval-when-compile (require 'cl))
 (eval-when-compile (require 'netrc))
 
 (defconst auth-source-protocols-customize
   (mapcar (lambda (a)
            (let ((p (car-safe a)))
-             (list 'const 
+             (list 'const
                    :tag (upcase (symbol-name p))
                    p)))
          auth-source-protocols))
 
-;;; this default will be changed to ~/.authinfo.gpg
-(defcustom auth-sources '((:source "~/.authinfo.enc" :host t :protocol t))
+(defvar auth-source-cache (make-hash-table :test 'equal)
+  "Cache for auth-source data")
+
+(defcustom auth-source-do-cache t
+  "Whether auth-source should cache information."
+  :group 'auth-source
+  :version "23.1" ;; No Gnus
+  :type `boolean)
+
+(defcustom auth-sources '((:source "~/.authinfo.gpg" :host t :protocol t))
   "List of authentication sources.
 
 Each entry is the authentication type with optional properties."
@@ -143,18 +159,51 @@ Returns fallback choices (where PROTOCOL or HOST are nil) with FALLBACK t."
       (unless fallback
        (auth-source-pick host protocol t)))))
 
+(defun auth-source-forget-user-or-password (mode host protocol)
+  (interactive "slogin/password: \nsHost: \nsProtocol: \n") ;for testing
+  (remhash (format "%s %s:%s" mode host protocol) auth-source-cache))
+
+(defun auth-source-forget-all-cached ()
+  "Forget all cached auth-source authentication tokens."
+  (interactive)
+  (setq auth-source-cache (make-hash-table :test 'equal)))
+
 (defun auth-source-user-or-password (mode host protocol)
-  "Find user or password (from the string MODE) matching HOST and PROTOCOL."
-;;; (debug mode host protocol)
-  (let (found)
-    (dolist (choice (auth-source-pick host protocol))
-      (setq found (netrc-machine-user-or-password 
-                  mode
-                  (plist-get choice :source)
-                  (list host)
-                  (list (format "%s" protocol))
-                  (auth-source-protocol-defaults protocol)))
-      (when found
+  "Find MODE (string or list of strings) matching HOST and PROTOCOL.
+MODE can be \"login\" or \"password\" for example."
+  (gnus-message 9
+               "auth-source-user-or-password: get %s for %s (%s)"
+               mode host protocol)
+  (let* ((listy (listp mode))
+        (mode (if listy mode (list mode)))
+        (cname (format "%s %s:%s" mode host protocol))
+        (found (gethash cname auth-source-cache)))
+    (if found
+       (progn
+         (gnus-message 9
+                       "auth-source-user-or-password: cached %s=%s for %s (%s)"
+                       mode
+                       ;; don't show the password
+                       (if (member "password" mode) "SECRET" found)
+                       host protocol)
+         found)
+      (dolist (choice (auth-source-pick host protocol))
+       (setq found (netrc-machine-user-or-password
+                    mode
+                    (plist-get choice :source)
+                    (list host)
+                    (list (format "%s" protocol))
+                    (auth-source-protocol-defaults protocol)))
+       (when found
+         (gnus-message 9
+                       "auth-source-user-or-password: found %s=%s for %s (%s)"
+                       mode
+                       ;; don't show the password
+                       (if (member "password" mode) "SECRET" found)
+                       host protocol)
+         (setq found (if listy found (car-safe found)))
+         (when auth-source-do-cache
+           (puthash cname found auth-source-cache)))
        (return found)))))
 
 (defun auth-source-protocol-defaults (protocol)