2001-02-13 20:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / nntp.el
index 5ced1d7..3ba900a 100644 (file)
@@ -1,6 +1,6 @@
 ;;; nntp.el --- nntp access for Gnus
 ;; Copyright (C) 1987, 1988, 1989, 1990, 1992, 1993, 1994, 1995, 1996,
-;;        1997, 1998, 2000
+;;        1997, 1998, 2000, 2001
 ;;        Free Software Foundation, Inc.
 
 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
@@ -180,7 +180,8 @@ server there that you can connect to.  See also
 
 (defvoo nntp-connection-timeout nil
   "*Number of seconds to wait before an nntp connection times out.
-If this variable is nil, which is the default, no timers are set.")
+If this variable is nil, which is the default, no timers are set.
+NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
 
 ;;; Internal variables.
 
@@ -289,8 +290,8 @@ noticing asynchronous data.")
              ;; Nix out "nntp reading...." message.
              (when nntp-have-messaged
                (setq nntp-have-messaged nil)
-               (nnheader-message 5 ""))
-             t))))
+               (nnheader-message 5 ""))))
+         t))
       (unless discard
        (erase-buffer)))))
 
@@ -304,7 +305,7 @@ noticing asynchronous data.")
   (let ((alist nntp-connection-alist)
        (buffer (if (stringp buffer) (get-buffer buffer) buffer))
        process entry)
-    (while (setq entry (pop alist))
+    (while (and alist (setq entry (pop alist)))
       (when (eq buffer (cadr entry))
        (setq process (car entry)
              alist nil)))
@@ -349,10 +350,13 @@ noticing asynchronous data.")
             (wait-for
              (nntp-wait-for process wait-for buffer decode))
             (t t)))
-       (error 
-        (nnheader-report 'nntp "Couldn't open connection to %s: %s" 
+       (error
+        (nnheader-report 'nntp "Couldn't open connection to %s: %s"
                          address err))
-       (quit nil)))))
+       (quit
+        (message "Quit retrieving data from nntp")
+        (signal 'quit nil)
+        nil)))))
 
 (defsubst nntp-send-command (wait-for &rest strings)
   "Send STRINGS to server and wait until WAIT-FOR returns."
@@ -514,32 +518,41 @@ noticing asynchronous data.")
          (when (or (null groups)       ;All requests have been sent.
                    (zerop (% count nntp-maximum-request)))
            (nntp-accept-response)
-           (while (progn
-                    ;; Search `blue moon' in this file for the
-                    ;; reason why set-buffer here.
-                    (set-buffer buf)
-                    (goto-char last-point)
-                    ;; Count replies.
-                    (while (re-search-forward "^[0-9]" nil t)
-                      (incf received))
-                    (setq last-point (point))
-                    (< received count))
+           (while (and (gnus-buffer-live-p buf)
+                       (progn
+                         ;; Search `blue moon' in this file for the
+                         ;; reason why set-buffer here.
+                         (set-buffer buf)
+                         (goto-char last-point)
+                         ;; Count replies.
+                         (while (re-search-forward "^[0-9]" nil t)
+                           (incf received))
+                         (setq last-point (point))
+                         (< received count)))
              (nntp-accept-response))))
 
        ;; Wait for the reply from the final command.
+       (unless (gnus-buffer-live-p buf)
+         (error
+          (nnheader-report 'nntp "Connection to %s is closed." server)))
        (set-buffer buf)
        (goto-char (point-max))
        (re-search-backward "^[0-9]" nil t)
        (when (looking-at "^[23]")
-         (while (progn
-                  (set-buffer buf)
-                  (goto-char (point-max))
-                  (if (not nntp-server-list-active-group)
-                      (not (re-search-backward "\r?\n" (- (point) 3) t))
-                    (not (re-search-backward "^\\.\r?\n" (- (point) 4) t))))
-           (nntp-accept-response)))
+         (while (and (gnus-buffer-live-p buf)
+                     (progn
+                       (set-buffer buf)
+                       (goto-char (point-max))
+                       (if (not nntp-server-list-active-group)
+                           (not (re-search-backward "\r?\n" (- (point) 3) t))
+                         (not (re-search-backward "^\\.\r?\n"
+                                                  (- (point) 4) t)))))
+                     (nntp-accept-response)))
 
        ;; Now all replies are received.  We remove CRs.
+       (unless (gnus-buffer-live-p buf)
+         (error
+          (nnheader-report 'nntp "Connection to %s is closed." server)))
        (set-buffer buf)
        (goto-char (point-min))
        (while (search-forward "\r" nil t)
@@ -800,8 +813,9 @@ If SEND-IF-FORCE, only send authinfo to the server if the
           (or passwd
               nntp-authinfo-password
               (setq nntp-authinfo-password
-                    (mail-source-read-passwd (format "NNTP (%s@%s) password: "
-                                                     user nntp-address))))))))))
+                    (mail-source-read-passwd
+                     (format "NNTP (%s@%s) password: "
+                             user nntp-address))))))))))
 
 (defun nntp-send-nosy-authinfo ()
   "Send the AUTHINFO to the nntp server."
@@ -873,14 +887,20 @@ password contained in '~/.nntp-authinfo'."
                     (coding-system-for-write nntp-coding-system-for-write))
                (funcall nntp-open-connection-function pbuffer))
            (error nil)
-           (quit nil))))
+           (quit
+            (message "Quit opening connection")
+            (nntp-kill-buffer pbuffer)
+            (signal 'quit nil)
+            nil))))
     (when timer
       (nnheader-cancel-timer timer))
+    (unless process
+      (nntp-kill-buffer pbuffer))
     (when (and (buffer-name pbuffer)
               process)
       (process-kill-without-query process)
-      (nntp-wait-for process "^.*\n" buffer nil t)
-      (if (memq (process-status process) '(open run))
+      (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
+              (memq (process-status process) '(open run)))
          (prog1
              (caar (push (list process buffer nil) nntp-connection-alist))
            (push process nntp-connection-list)
@@ -982,7 +1002,7 @@ password contained in '~/.nntp-authinfo'."
       (if (memq (following-char) '(?4 ?5))
          ;; wants credentials?
          (if (looking-at "480")
-             (nntp-handle-authinfo nntp-process-to-buffer)
+             (nntp-handle-authinfo process)
            ;; report error message.
            (nntp-snarf-error-message)
            (nntp-do-callback nil))
@@ -1073,7 +1093,9 @@ password contained in '~/.nntp-authinfo'."
       (delete-char 2))
     ;; Delete status line.
     (goto-char (point-min))
-    (delete-region (point) (progn (forward-line 1) (point)))
+    (while (looking-at "[1-5][0-9][0-9] .*\n")
+      ;; For some unknown reason, there is more than one status line.
+      (delete-region (point) (progn (forward-line 1) (point))))
     ;; Remove "." -> ".." encoding.
     (while (search-forward "\n.." nil t)
       (delete-char -1))))
@@ -1151,7 +1173,7 @@ password contained in '~/.nntp-authinfo'."
        (while (and (cdr articles)
                    (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
          (setq articles (cdr articles)))
-
+       
        (setq in-process-buffer-p (stringp nntp-server-xover))
        (nntp-send-xover-command first (car articles))
        (setq articles (cdr articles))
@@ -1159,7 +1181,7 @@ password contained in '~/.nntp-authinfo'."
        (when (and nntp-server-xover in-process-buffer-p)
          ;; Don't count tried request.
          (setq count (1+ count))
-         
+
          ;; Every 400 requests we have to read the stream in
          ;; order to avoid deadlocks.
          (when (or (null articles)     ;All requests have been sent.
@@ -1272,6 +1294,7 @@ password contained in '~/.nntp-authinfo'."
                 "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
          (case-fold-search t))
       (when (memq (process-status proc) '(open run))
+       (nntp-wait-for-string "^r?telnet")
        (process-send-string proc "set escape \^X\n")
        (cond
         ((and nntp-open-telnet-envuser nntp-telnet-user-name)
@@ -1301,7 +1324,7 @@ password contained in '~/.nntp-authinfo'."
        (beginning-of-line)
        (delete-region (point-min) (point))
        (process-send-string proc "\^]")
-       (nntp-wait-for-string "^telnet")
+       (nntp-wait-for-string "^r?telnet")
        (process-send-string proc "mode character\n")
        (accept-process-output proc 1)
        (sit-for 1)