Update todo.
[bpt/emacs.git] / lisp / gnus / auth-source.el
CommitLineData
8f7abae3
MB
1;;; auth-source.el --- authentication sources for Gnus and Emacs
2
9b3ebcb6 3;; Copyright (C) 2008 Free Software Foundation, Inc.
8f7abae3
MB
4
5;; Author: Ted Zlatanov <tzz@lifelogs.com>
6;; Keywords: news
7
8;; This file is part of GNU Emacs.
9
5e809f55 10;; GNU Emacs is free software: you can redistribute it and/or modify
8f7abae3 11;; it under the terms of the GNU General Public License as published by
5e809f55
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
8f7abae3
MB
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
5e809f55 17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8f7abae3
MB
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
5e809f55 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
8f7abae3
MB
22
23;;; Commentary:
24
25;; This is the auth-source.el package. It lets users tell Gnus how to
26;; authenticate in a single place. Simplicity is the goal. Instead
27;; of providing 5000 options, we'll stick to simple, easy to
28;; understand options.
d55fe5bb
MB
29
30;; Easy setup:
31;; (require 'auth-source)
32;; (customize-variable 'auth-sources) ;; optional
33
34;; now, whatever sources you've defined for password have to be available
35
36;; if you want encrypted sources, which is strongly recommended, do
37;; (require 'epa-file)
38;; (epa-file-mode)
39
40;; before you put some data in ~/.authinfo.gpg (the default place)
41
8f7abae3
MB
42;;; Code:
43
44(eval-when-compile (require 'cl))
9b3ebcb6 45(eval-when-compile (require 'netrc))
8f7abae3
MB
46
47(defgroup auth-source nil
48 "Authentication sources."
9b3ebcb6 49 :version "23.1" ;; No Gnus
8f7abae3
MB
50 :group 'gnus)
51
9b3ebcb6
MB
52(defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993")
53 (pop3 "pop3" "pop" "pop3s" "110" "995")
54 (ssh "ssh" "22")
55 (sftp "sftp" "115")
56 (smtp "smtp" "25"))
57 "List of authentication protocols and their names"
58
59 :group 'auth-source
60 :version "23.1" ;; No Gnus
61 :type '(repeat :tag "Authentication Protocols"
62 (cons :tag "Protocol Entry"
63 (symbol :tag "Protocol")
64 (repeat :tag "Names"
65 (string :tag "Name")))))
66
67;;; generate all the protocols in a format Customize can use
68(defconst auth-source-protocols-customize
69 (mapcar (lambda (a)
70 (let ((p (car-safe a)))
71 (list 'const
72 :tag (upcase (symbol-name p))
73 p)))
74 auth-source-protocols))
75
76;;; this default will be changed to ~/.authinfo.gpg
77(defcustom auth-sources '((:source "~/.authinfo.enc" :host t :protocol t))
8f7abae3
MB
78 "List of authentication sources.
79
80Each entry is the authentication type with optional properties."
81 :group 'auth-source
9b3ebcb6
MB
82 :version "23.1" ;; No Gnus
83 :type `(repeat :tag "Authentication Sources"
84 (list :tag "Source definition"
85 (const :format "" :value :source)
86 (string :tag "Authentication Source")
87 (const :format "" :value :host)
d55fe5bb 88 (choice :tag "Host (machine) choice"
9b3ebcb6 89 (const :tag "Any" t)
d55fe5bb 90 (regexp :tag "Host (machine) regular expression (TODO)")
9b3ebcb6
MB
91 (const :tag "Fallback" nil))
92 (const :format "" :value :protocol)
93 (choice :tag "Protocol"
94 (const :tag "Any" t)
95 (const :tag "Fallback" nil)
96 ,@auth-source-protocols-customize))))
8f7abae3
MB
97
98;; temp for debugging
9b3ebcb6
MB
99;; (unintern 'auth-source-protocols)
100;; (unintern 'auth-sources)
101;; (customize-variable 'auth-sources)
102;; (setq auth-sources nil)
103;; (format "%S" auth-sources)
104;; (customize-variable 'auth-source-protocols)
105;; (setq auth-source-protocols nil)
106;; (format "%S" auth-source-protocols)
107;; (auth-source-pick "a" 'imap)
108;; (auth-source-user-or-password "login" "imap.myhost.com" 'imap)
109;; (auth-source-user-or-password "password" "imap.myhost.com" 'imap)
110;; (auth-source-user-or-password-imap "login" "imap.myhost.com")
111;; (auth-source-user-or-password-imap "password" "imap.myhost.com")
112;; (auth-source-protocol-defaults 'imap)
113
58a67d68
MB
114(defun auth-source-pick (host protocol &optional fallback)
115 "Parse `auth-sources' for HOST, and PROTOCOL matches.
9b3ebcb6 116
58a67d68 117Returns fallback choices (where PROTOCOL or HOST are nil) with FALLBACK t."
9b3ebcb6
MB
118 (interactive "sHost: \nsProtocol: \n") ;for testing
119 (let (choices)
120 (dolist (choice auth-sources)
58a67d68 121 (let ((h (plist-get choice :host))
9b3ebcb6
MB
122 (p (plist-get choice :protocol)))
123 (when (and
124 (or (equal t h)
125 (and (stringp h) (string-match h host))
126 (and fallback (equal h nil)))
127 (or (equal t p)
128 (and (symbolp p) (equal p protocol))
129 (and fallback (equal p nil))))
130 (push choice choices))))
131 (if choices
132 choices
133 (unless fallback
58a67d68 134 (auth-source-pick host protocol t)))))
9b3ebcb6 135
58a67d68
MB
136(defun auth-source-user-or-password (mode host protocol)
137 "Find user or password (from the string MODE) matching HOST and PROTOCOL."
9b3ebcb6 138 (let (found)
58a67d68 139 (dolist (choice (auth-source-pick host protocol))
9b3ebcb6
MB
140 (setq found (netrc-machine-user-or-password
141 mode
142 (plist-get choice :source)
143 (list host)
144 (list (format "%s" protocol))
145 (auth-source-protocol-defaults protocol)))
146 (when found
147 (return found)))))
148
149(defun auth-source-protocol-defaults (protocol)
150 "Return a list of default ports and names for PROTOCOL."
151 (cdr-safe (assoc protocol auth-source-protocols)))
152
58a67d68
MB
153(defun auth-source-user-or-password-imap (mode host)
154 (auth-source-user-or-password mode host 'imap))
9b3ebcb6 155
58a67d68
MB
156(defun auth-source-user-or-password-pop3 (mode host)
157 (auth-source-user-or-password mode host 'pop3))
9b3ebcb6 158
58a67d68
MB
159(defun auth-source-user-or-password-ssh (mode host)
160 (auth-source-user-or-password mode host 'ssh))
9b3ebcb6 161
58a67d68
MB
162(defun auth-source-user-or-password-sftp (mode host)
163 (auth-source-user-or-password mode host 'sftp))
9b3ebcb6 164
58a67d68
MB
165(defun auth-source-user-or-password-smtp (mode host)
166 (auth-source-user-or-password mode host 'smtp))
8f7abae3
MB
167
168(provide 'auth-source)
169
170;; arch-tag: ff1afe78-06e9-42c2-b693-e9f922cbe4ab
171;;; auth-source.el ends here