(rfc2047-encode): Use uppercase letters to specify encodings of
[gnus] / lisp / netrc.el
1 ;;; netrc.el --- .netrc parsing functionality
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Modularizer: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; Just the .netrc parsing functionality, abstracted so other packages
29 ;; besides Gnus can use it.
30
31 ;;; Code:
32
33 ;;;
34 ;;; .netrc and .authinfo rc parsing
35 ;;;
36
37 ;; autoload password
38 (eval-and-compile
39   (autoload 'password-read "password"))
40
41 (defgroup netrc nil
42  "Netrc configuration.")
43
44 (defcustom netrc-encrypting-method nil
45   "Decoding method used for the netrc file.
46 Use the OpenSSL symmetric ciphers here.  Leave nil for no
47 decoding.  Encrypt the file with netrc-encrypt, but make sure you
48 have set netrc-encrypting-method to a non-nil value."
49   :type '(choice
50           (const :tag "DES-3" "des3")
51           (const :tag "IDEA" "idea")
52           (const :tag "RC4" "rc4")
53           (string :tag "Explicit cipher name")
54           (const :tag "None" nil))
55   :group 'netrc)
56
57 (defcustom netrc-openssl-path (executable-find "openssl")
58   "File path of the OpenSSL shell."
59   :type '(choice (file :tag "Location of openssl")
60                  (const :tag "openssl is not installed" nil))
61   :group 'netrc)
62
63 (defun netrc-encrypt (plain-file encrypted-file)
64   (interactive "fPlain File: \nFEncrypted File: ")
65   "Encrypt FILE to ENCRYPTED-FILE with netrc-encrypting-method cipher."
66   (when (and (file-exists-p plain-file)
67              (stringp encrypted-file)
68              netrc-encrypting-method
69              netrc-openssl-path)
70     (let ((buffer-file-coding-system 'binary)
71           (coding-system-for-read 'binary)
72           (coding-system-for-write 'binary)
73           (password 
74            (password-read
75             (format "OpenSSL Password for cipher %s? "
76                     netrc-encrypting-method)
77             (format "netrc-openssl-password-%s"
78                     netrc-encrypting-method))))
79       (when password
80         (with-temp-buffer
81           (insert-file-contents plain-file)
82           (setenv "NETRC_OPENSSL_PASSWORD" password)
83           (shell-command-on-region 
84            (point-min) 
85            (point-max)
86            (format "%s %s -pass env:NETRC_OPENSSL_PASSWORD -e"
87                    netrc-openssl-path
88                    netrc-encrypting-method)
89            t
90            t)
91           (write-file encrypted-file t))))))
92
93 (defun netrc-parse (file)
94   (interactive "fFile to Parse: ")
95   "Parse FILE and return an list of all entries in the file."
96   (when (file-exists-p file)
97     (with-temp-buffer
98       (let ((tokens '("machine" "default" "login"
99                       "password" "account" "macdef" "force"
100                       "port"))
101             alist elem result pair)
102         (if (and netrc-encrypting-method
103                  netrc-openssl-path)
104             (let ((buffer-file-coding-system 'binary)
105                   (coding-system-for-read 'binary)
106                   (coding-system-for-write 'binary)
107                   (password 
108                    (password-read
109                     (format "OpenSSL Password for cipher %s? "
110                             netrc-encrypting-method)
111                     (format "netrc-openssl-password-%s" 
112                             netrc-encrypting-method))))
113               (when password
114                 (insert-file-contents file)
115                 (setenv "NETRC_OPENSSL_PASSWORD" password)
116                 (shell-command-on-region
117                  (point-min) 
118                  (point-max)
119                  (format "%s %s -pass env:NETRC_OPENSSL_PASSWORD -d"
120                          netrc-openssl-path
121                          netrc-encrypting-method)
122                  t
123                  t)))
124           (insert-file-contents file))
125         (goto-char (point-min))
126         ;; Go through the file, line by line.
127         (while (not (eobp))
128           (narrow-to-region (point) (point-at-eol))
129           ;; For each line, get the tokens and values.
130           (while (not (eobp))
131             (skip-chars-forward "\t ")
132             ;; Skip lines that begin with a "#".
133             (if (eq (char-after) ?#)
134                 (goto-char (point-max))
135               (unless (eobp)
136                 (setq elem
137                       (if (= (following-char) ?\")
138                           (read (current-buffer))
139                         (buffer-substring
140                          (point) (progn (skip-chars-forward "^\t ")
141                                         (point)))))
142                 (cond
143                  ((equal elem "macdef")
144                   ;; We skip past the macro definition.
145                   (widen)
146                   (while (and (zerop (forward-line 1))
147                               (looking-at "$")))
148                   (narrow-to-region (point) (point)))
149                  ((member elem tokens)
150                   ;; Tokens that don't have a following value are ignored,
151                   ;; except "default".
152                   (when (and pair (or (cdr pair)
153                                       (equal (car pair) "default")))
154                     (push pair alist))
155                   (setq pair (list elem)))
156                  (t
157                   ;; Values that haven't got a preceding token are ignored.
158                   (when pair
159                     (setcdr pair elem)
160                     (push pair alist)
161                     (setq pair nil)))))))
162           (when alist
163             (push (nreverse alist) result))
164           (setq alist nil
165                 pair nil)
166           (widen)
167           (forward-line 1))
168         (nreverse result)))))
169
170 (defun netrc-machine (list machine &optional port defaultport)
171   "Return the netrc values from LIST for MACHINE or for the default entry.
172 If PORT specified, only return entries with matching port tokens.
173 Entries without port tokens default to DEFAULTPORT."
174   (let ((rest list)
175         result)
176     (while list
177       (when (equal (cdr (assoc "machine" (car list))) machine)
178         (push (car list) result))
179       (pop list))
180     (unless result
181       ;; No machine name matches, so we look for default entries.
182       (while rest
183         (when (assoc "default" (car rest))
184           (push (car rest) result))
185         (pop rest)))
186     (when result
187       (setq result (nreverse result))
188       (while (and result
189                   (not (equal (or port defaultport "nntp")
190                               (or (netrc-get (car result) "port")
191                                   defaultport "nntp"))))
192         (pop result))
193       (car result))))
194
195 (defun netrc-get (alist type)
196   "Return the value of token TYPE from ALIST."
197   (cdr (assoc type alist)))
198
199 (provide 'netrc)
200
201 ;;; netrc.el ends here