Mercurial > emacs
annotate lisp/gnus/auth-source.el @ 94739:58cf3761a803
(eshell-review-quick-commands): Don't use `iff' in docstring.
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Thu, 08 May 2008 03:39:26 +0000 |
parents | f42ef85caf91 |
children | 55eb2a3c59b4 |
rev | line source |
---|---|
92694 | 1 ;;; auth-source.el --- authentication sources for Gnus and Emacs |
2 | |
93386 | 3 ;; Copyright (C) 2008 Free Software Foundation, Inc. |
92694 | 4 |
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com> | |
6 ;; Keywords: news | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
94662
f42ef85caf91
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94369
diff
changeset
|
10 ;; GNU Emacs is free software: you can redistribute it and/or modify |
92694 | 11 ;; it under the terms of the GNU General Public License as published by |
94662
f42ef85caf91
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94369
diff
changeset
|
12 ;; the Free Software Foundation, either version 3 of the License, or |
f42ef85caf91
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94369
diff
changeset
|
13 ;; (at your option) any later version. |
92694 | 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 | |
94662
f42ef85caf91
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94369
diff
changeset
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
92694 | 18 ;; GNU General Public License for more details. |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
94662
f42ef85caf91
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94369
diff
changeset
|
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
92694 | 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. | |
94209 | 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 | |
92694 | 42 ;;; Code: |
43 | |
44 (eval-when-compile (require 'cl)) | |
93386 | 45 (eval-when-compile (require 'netrc)) |
92694 | 46 |
47 (defgroup auth-source nil | |
48 "Authentication sources." | |
93386 | 49 :version "23.1" ;; No Gnus |
92694 | 50 :group 'gnus) |
51 | |
93386 | 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)) | |
92694 | 78 "List of authentication sources. |
79 | |
80 Each entry is the authentication type with optional properties." | |
81 :group 'auth-source | |
93386 | 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) | |
94209 | 88 (choice :tag "Host (machine) choice" |
93386 | 89 (const :tag "Any" t) |
94209 | 90 (regexp :tag "Host (machine) regular expression (TODO)") |
93386 | 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)))) | |
92694 | 97 |
98 ;; temp for debugging | |
93386 | 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 | |
94369 | 114 (defun auth-source-pick (host protocol &optional fallback) |
115 "Parse `auth-sources' for HOST, and PROTOCOL matches. | |
93386 | 116 |
94369 | 117 Returns fallback choices (where PROTOCOL or HOST are nil) with FALLBACK t." |
93386 | 118 (interactive "sHost: \nsProtocol: \n") ;for testing |
119 (let (choices) | |
120 (dolist (choice auth-sources) | |
94369 | 121 (let ((h (plist-get choice :host)) |
93386 | 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 | |
94369 | 134 (auth-source-pick host protocol t))))) |
93386 | 135 |
94369 | 136 (defun auth-source-user-or-password (mode host protocol) |
137 "Find user or password (from the string MODE) matching HOST and PROTOCOL." | |
93386 | 138 (let (found) |
94369 | 139 (dolist (choice (auth-source-pick host protocol)) |
93386 | 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 | |
94369 | 153 (defun auth-source-user-or-password-imap (mode host) |
154 (auth-source-user-or-password mode host 'imap)) | |
93386 | 155 |
94369 | 156 (defun auth-source-user-or-password-pop3 (mode host) |
157 (auth-source-user-or-password mode host 'pop3)) | |
93386 | 158 |
94369 | 159 (defun auth-source-user-or-password-ssh (mode host) |
160 (auth-source-user-or-password mode host 'ssh)) | |
93386 | 161 |
94369 | 162 (defun auth-source-user-or-password-sftp (mode host) |
163 (auth-source-user-or-password mode host 'sftp)) | |
93386 | 164 |
94369 | 165 (defun auth-source-user-or-password-smtp (mode host) |
166 (auth-source-user-or-password mode host 'smtp)) | |
92694 | 167 |
168 (provide 'auth-source) | |
169 | |
170 ;; arch-tag: ff1afe78-06e9-42c2-b693-e9f922cbe4ab | |
171 ;;; auth-source.el ends here |