X-Git-Url: http://cgit.sxemacs.org/?p=gnus;a=blobdiff_plain;f=lisp%2Fnetrc.el;h=80ae1b57ba378deb6fb443623df3aa72cf294efd;hp=0f8f4d33291be3a625fb11064251ef75bd01f3c5;hb=a1e20e76b6028842122275df0c5620cfd986790a;hpb=786d05e27f23ae1e1254d90a50f61487e168c616 diff --git a/lisp/netrc.el b/lisp/netrc.el index 0f8f4d332..80ae1b57b 100644 --- a/lisp/netrc.el +++ b/lisp/netrc.el @@ -1,27 +1,26 @@ ;;; netrc.el --- .netrc parsing functionality ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, -;; 2005 Free Software Foundation, Inc. +;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. ;; Author: Lars Magne Ingebrigtsen -;; Modularizer: Ted Zlatanov ;; Keywords: news +;; Modularized by Ted Zlatanov +;; when it was part of Gnus. ;; This file is part of GNU Emacs. -;; GNU Emacs is free software; you can redistribute it and/or modify +;; GNU Emacs is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by -;; the Free Software Foundation; either version 2, or (at your option) -;; any later version. +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. ;; GNU Emacs is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License -;; along with GNU Emacs; see the file COPYING. If not, write to the -;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, -;; Boston, MA 02110-1301, USA. +;; along with GNU Emacs. If not, see . ;;; Commentary: @@ -34,10 +33,21 @@ ;;; .netrc and .authinfo rc parsing ;;; -;; autoload encrypt -(eval-and-compile - (autoload 'encrypt-find-model "encrypt") - (autoload 'encrypt-insert-file-contents "encrypt")) +;; use encrypt if loaded (encrypt-file-alist has to be set as well) +(autoload 'encrypt-find-model "encrypt") +(autoload 'encrypt-insert-file-contents "encrypt") +(defalias 'netrc-point-at-eol + (if (fboundp 'point-at-eol) + 'point-at-eol + 'line-end-position)) +(defvar encrypt-file-alist) +(eval-when-compile + ;; 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)))) (defgroup netrc nil "Netrc configuration." @@ -48,63 +58,64 @@ (defun netrc-parse (file) (interactive "fFile to Parse: ") - "Parse FILE and return an list of all entries in the file." - (when (file-exists-p file) - (with-temp-buffer - (let ((tokens '("machine" "default" "login" - "password" "account" "macdef" "force" - "port")) - (encryption-model (encrypt-find-model file)) - alist elem result pair) - - (if encryption-model - (encrypt-insert-file-contents file encryption-model) - (insert-file-contents file)) - - (goto-char (point-min)) - ;; Go through the file, line by line. - (while (not (eobp)) - (narrow-to-region (point) (point-at-eol)) - ;; For each line, get the tokens and values. + "Parse FILE and return a list of all entries in the file." + (if (listp file) + file + (when (file-exists-p file) + (with-temp-buffer + (let ((tokens '("machine" "default" "login" + "password" "account" "macdef" "force" + "port")) + (encryption-model (when (netrc-bound-and-true-p encrypt-file-alist) + (encrypt-find-model file))) + alist elem result pair) + (if encryption-model + (encrypt-insert-file-contents file encryption-model) + (insert-file-contents file)) + (goto-char (point-min)) + ;; Go through the file, line by line. (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))))) + (narrow-to-region (point) (point-at-eol)) + ;; 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. @@ -127,8 +138,10 @@ Entries without port tokens default to DEFAULTPORT." (while (and result (not (netrc-port-equal (or port defaultport "nntp") + ;; when port is not given in the netrc file, + ;; it should mean "any port" (or (netrc-get (car result) "port") - defaultport "nntp")))) + defaultport port)))) (pop result)) (car result)))) @@ -145,11 +158,22 @@ MODE can be \"login\" or \"password\", suitable for passing to (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))) - (setq info (or (netrc-get alist mode) info)))))) + (if (listp mode) + (setq info + (mapcar + (lambda (mode-element) + (netrc-machine-user-or-password + mode-element + authinfo-list + machines + ports + defaults)) + mode)) + (dolist (machine machines) + (dolist (default defaults) + (dolist (port ports) + (let ((alist (netrc-machine authinfo-list machine port default))) + (setq info (or (netrc-get alist mode) info))))))) info)) (defun netrc-get (alist type) @@ -169,7 +193,7 @@ MODE can be \"login\" or \"password\", suitable for passing to (with-temp-buffer (insert-file-contents netrc-services-file) (while (search-forward "#" nil t) - (delete-region (1- (point)) (line-end-position))) + (delete-region (1- (point)) (point-at-eol))) (goto-char (point-min)) (while (re-search-forward "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t) @@ -184,8 +208,7 @@ MODE can be \"login\" or \"password\", suitable for passing to (setq type (or type 'tcp)) (while (and (setq service (pop services)) (not (and (= number (cadr service)) - (eq type (caddr service))))) - ) + (eq type (car (cddr service))))))) (car service))) (defun netrc-find-service-number (name &optional type) @@ -194,11 +217,10 @@ MODE can be \"login\" or \"password\", suitable for passing to (setq type (or type 'tcp)) (while (and (setq service (pop services)) (not (and (string= name (car service)) - (eq type (caddr service))))) - ) + (eq type (car (cddr service))))))) (cadr service))) (provide 'netrc) -;;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55 +;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55 ;;; netrc.el ends here