netrc.el 6.79 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1
;;; netrc.el --- .netrc parsing functionality
2
;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
Glenn Morris's avatar
Glenn Morris committed
3
;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4 5 6 7 8 9 10 11

;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
;; Keywords: news
;;  Modularized by Ted Zlatanov <tzz@lifelogs.com>
;;  when it was part of Gnus.

;; This file is part of GNU Emacs.

12
;; GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
13
;; it under the terms of the GNU General Public License as published by
14 15
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
16 17 18

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Richard M. Stallman's avatar
Richard M. Stallman committed
20 21 22
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
23
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Richard M. Stallman's avatar
Richard M. Stallman committed
24 25 26 27 28 29 30 31 32

;;; Commentary:

;; Just the .netrc parsing functionality, abstracted so other packages
;; besides Gnus can use it.

;;; Code:

;;;
33
;;; .netrc and .authinfo rc parsing
Richard M. Stallman's avatar
Richard M. Stallman committed
34 35
;;;

Miles Bader's avatar
Miles Bader committed
36 37 38 39
;; use encrypt if loaded (encrypt-file-alist has to be set as well)
(eval-and-compile
  (autoload 'encrypt-find-model "encrypt")
  (autoload 'encrypt-insert-file-contents "encrypt"))
Richard M. Stallman's avatar
Richard M. Stallman committed
40 41 42 43
(defalias 'netrc-point-at-eol
  (if (fboundp 'point-at-eol)
      'point-at-eol
    'line-end-position))
Miles Bader's avatar
Miles Bader committed
44 45 46 47 48 49 50 51
(eval-when-compile
  (defvar encrypt-file-alist)
  ;; This is unnecessary in the compiled version as it is a macro.
  (if (fboundp 'bound-and-true-p)
      (defalias 'netrc-bound-and-true-p 'bound-and-true-p)
    (defmacro netrc-bound-and-true-p (var)
      "Return the value of symbol VAR if it is bound, else nil."
      `(and (boundp (quote ,var)) ,var))))
52 53 54 55 56 57 58

(defgroup netrc nil
 "Netrc configuration."
 :group 'comm)

(defvar netrc-services-file "/etc/services"
  "The name of the services file.")
Richard M. Stallman's avatar
Richard M. Stallman committed
59 60

(defun netrc-parse (file)
61
  (interactive "fFile to Parse: ")
Miles Bader's avatar
Miles Bader committed
62
  "Parse FILE and return a list of all entries in the file."
Richard M. Stallman's avatar
Richard M. Stallman committed
63 64 65 66 67
  (when (file-exists-p file)
    (with-temp-buffer
      (let ((tokens '("machine" "default" "login"
		      "password" "account" "macdef" "force"
		      "port"))
Miles Bader's avatar
Miles Bader committed
68 69
	    (encryption-model (when (netrc-bound-and-true-p encrypt-file-alist)
				(encrypt-find-model file)))
Richard M. Stallman's avatar
Richard M. Stallman committed
70
	    alist elem result pair)
Miles Bader's avatar
Miles Bader committed
71 72 73
	(if encryption-model
	    (encrypt-insert-file-contents file encryption-model)
	  (insert-file-contents file))
Richard M. Stallman's avatar
Richard M. Stallman committed
74 75 76
	(goto-char (point-min))
	;; Go through the file, line by line.
	(while (not (eobp))
77
	  (narrow-to-region (point) (point-at-eol))
Richard M. Stallman's avatar
Richard M. Stallman committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	  ;; For each line, get the tokens and values.
	  (while (not (eobp))
	    (skip-chars-forward "\t ")
	    ;; Skip lines that begin with a "#".
	    (if (eq (char-after) ?#)
		(goto-char (point-max))
	      (unless (eobp)
		(setq elem
		      (if (= (following-char) ?\")
			  (read (current-buffer))
			(buffer-substring
			 (point) (progn (skip-chars-forward "^\t ")
					(point)))))
		(cond
		 ((equal elem "macdef")
		  ;; We skip past the macro definition.
		  (widen)
		  (while (and (zerop (forward-line 1))
			      (looking-at "$")))
		  (narrow-to-region (point) (point)))
		 ((member elem tokens)
		  ;; Tokens that don't have a following value are ignored,
		  ;; except "default".
		  (when (and pair (or (cdr pair)
				      (equal (car pair) "default")))
		    (push pair alist))
		  (setq pair (list elem)))
		 (t
		  ;; Values that haven't got a preceding token are ignored.
		  (when pair
		    (setcdr pair elem)
		    (push pair alist)
		    (setq pair nil)))))))
	  (when alist
	    (push (nreverse alist) result))
	  (setq alist nil
		pair nil)
	  (widen)
	  (forward-line 1))
	(nreverse result)))))

(defun netrc-machine (list machine &optional port defaultport)
  "Return the netrc values from LIST for MACHINE or for the default entry.
If PORT specified, only return entries with matching port tokens.
Entries without port tokens default to DEFAULTPORT."
  (let ((rest list)
	result)
    (while list
      (when (equal (cdr (assoc "machine" (car list))) machine)
	(push (car list) result))
      (pop list))
    (unless result
      ;; No machine name matches, so we look for default entries.
      (while rest
	(when (assoc "default" (car rest))
	  (push (car rest) result))
	(pop rest)))
    (when result
      (setq result (nreverse result))
      (while (and result
138 139
		  (not (netrc-port-equal
			(or port defaultport "nntp")
Miles Bader's avatar
Miles Bader committed
140 141
			;; when port is not given in the netrc file,
			;; it should mean "any port"
142
			(or (netrc-get (car result) "port")
Miles Bader's avatar
Miles Bader committed
143
			    defaultport port))))
Richard M. Stallman's avatar
Richard M. Stallman committed
144 145 146
	(pop result))
      (car result))))

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
(defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults)
  "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST.
Matches a machine from MACHINES and a port from PORTS, giving
default ports DEFAULTS to `netrc-machine'.

MODE can be \"login\" or \"password\", suitable for passing to
`netrc-get'."
  (let ((authinfo-list (if (stringp authinfo-file-or-list)
			   (netrc-parse authinfo-file-or-list)
			 authinfo-file-or-list))
	(ports (or ports '(nil)))
	(defaults (or defaults '(nil)))
	info)
    (dolist (machine machines)
      (dolist (default defaults)
	(dolist (port ports)
	  (let ((alist (netrc-machine authinfo-list machine port default)))
Miles Bader's avatar
Miles Bader committed
164
	    (setq info (or (netrc-get alist mode) info))))))
165 166
    info))

Richard M. Stallman's avatar
Richard M. Stallman committed
167 168 169 170
(defun netrc-get (alist type)
  "Return the value of token TYPE from ALIST."
  (cdr (assoc type alist)))

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
(defun netrc-port-equal (port1 port2)
  (when (numberp port1)
    (setq port1 (or (netrc-find-service-name port1) port1)))
  (when (numberp port2)
    (setq port2 (or (netrc-find-service-name port2) port2)))
  (equal port1 port2))

(defun netrc-parse-services ()
  (when (file-exists-p netrc-services-file)
    (let ((services nil))
      (with-temp-buffer
	(insert-file-contents netrc-services-file)
	(while (search-forward "#" nil t)
	  (delete-region (1- (point)) (point-at-eol)))
	(goto-char (point-min))
	(while (re-search-forward
		"^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t)
	  (push (list (match-string 1) (string-to-number (match-string 2))
		      (intern (downcase (match-string 3))))
		services))
	(nreverse services)))))

(defun netrc-find-service-name (number &optional type)
  (let ((services (netrc-parse-services))
	service)
    (setq type (or type 'tcp))
    (while (and (setq service (pop services))
		(not (and (= number (cadr service))
199
			  (eq type (car (cddr service)))))))
200 201 202 203 204 205 206 207
    (car service)))

(defun netrc-find-service-number (name &optional type)
  (let ((services (netrc-parse-services))
	service)
    (setq type (or type 'tcp))
    (while (and (setq service (pop services))
		(not (and (string= name (car service))
208
			  (eq type (car (cddr service)))))))
209 210
    (cadr service)))

Richard M. Stallman's avatar
Richard M. Stallman committed
211 212
(provide 'netrc)

213
;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55
Richard M. Stallman's avatar
Richard M. Stallman committed
214
;;; netrc.el ends here