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