+(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))
+ (eq type (car (cddr service)))))))
+ (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))
+ (eq type (car (cddr service)))))))
+ (cadr service)))
+
+(defun netrc-store-data (file host port user password)
+ (with-temp-buffer
+ (when (file-exists-p file)
+ (insert-file-contents file))
+ (goto-char (point-max))
+ (unless (bolp)
+ (insert "\n"))
+ (insert (format "machine %s login %s password %s port %s\n"
+ host user password port))
+ (write-region (point-min) (point-max) file nil 'silent)))
+
+;;;###autoload
+(defun netrc-credentials (machine &rest ports)
+ "Return a user name/password pair.
+Port specifications will be prioritised in the order they are
+listed in the PORTS list."
+ (let ((list (netrc-parse))
+ found)
+ (while (and ports
+ (not found))
+ (setq found (netrc-machine list machine (pop ports))))
+ (when found
+ (list (cdr (assoc "login" found))
+ (cdr (assoc "password" found))))))
+