*** empty log message ***
[gnus] / lisp / nntp.el
index dbf1866..d54b4a4 100644 (file)
@@ -1,8 +1,6 @@
-;;; nntp.el --- nntp access for Gnus
-;; Copyright (C) 1987,88,89,90,92,93,94,95 Free Software Foundation, Inc.
+;; Copyright (C) 1987,88,89,90,92,93,94,95,96 Free Software Foundation, Inc.
 
 
-;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
-;;     Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
+;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
 ;; Keywords: news
 
 ;; This file is part of GNU Emacs.
 ;; Keywords: news
 
 ;; This file is part of GNU Emacs.
 
 ;;; Code:
 
 
 ;;; Code:
 
-(require 'rnews)
-(require 'sendmail)
 (require 'nnheader)
 (require 'nnheader)
+(require 'nnoo)
+(require 'gnus-util)
+
+(nnoo-declare nntp)
 
 (eval-and-compile
 
 (eval-and-compile
-  (autoload 'news-setup "rnewspost")
-  (autoload 'news-reply-mode "rnewspost")
-  (autoload 'nnmail-request-post-buffer "nnmail")
-  (autoload 'cancel-timer "timer")
-  (autoload 'telnet "telnet" nil t)
-  (autoload 'telnet-send-input "telnet" nil t)
-  (autoload 'timezone-parse-date "timezone"))
-
-(defvar nntp-server-hook nil
-  "*Hooks for the NNTP server.
-If the kanji code of the NNTP server is different from the local kanji
-code, the correct kanji code of the buffer associated with the NNTP
-server must be specified as follows:
-
-\(setq nntp-server-hook
-      (function
-       (lambda ()
-        ;; Server's Kanji code is EUC (NEmacs hack).
-        (make-local-variable 'kanji-fileio-code)
-        (setq kanji-fileio-code 0))))
-
-If you'd like to change something depending on the server in this
-hook, use the variable `nntp-address'.")
-
-(defvar nntp-server-opened-hook nil
+  (unless (fboundp 'open-network-stream)
+    (require 'tcp)))
+
+(eval-when-compile (require 'cl))
+
+(defvoo nntp-address nil
+  "Address of the physical nntp server.")
+
+(defvoo nntp-port-number "nntp"
+  "Port number on the physical nntp server.")
+
+(defvoo nntp-server-opened-hook nil
   "*Hook used for sending commands to the server at startup.  
 The default value is `nntp-send-mode-reader', which makes an innd
 server spawn an nnrpd server.  Another useful function to put in this
   "*Hook used for sending commands to the server at startup.  
 The default value is `nntp-send-mode-reader', which makes an innd
 server spawn an nnrpd server.  Another useful function to put in this
@@ -63,619 +50,369 @@ to allow posting from the server.  Note that this is only necessary to
 do on servers that use strict access control.")  
 (add-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)
 
 do on servers that use strict access control.")  
 (add-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)
 
-(defvar nntp-open-server-function 'nntp-open-network-stream
+(defvoo nntp-authinfo-function 'nntp-send-authinfo
+  "Function used to send AUTHINFO to the server.")
+
+(defvoo nntp-server-action-alist 
+  '(("nntpd 1\\.5\\.11t" 
+     (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)))
+  "Alist of regexps to match on server types and actions to be taken.
+For instance, if you want Gnus to beep every time you connect
+to innd, you could say something like:
+
+\(setq nntp-server-action-alist
+       '((\"innd\" (ding))))
+
+You probably don't want to do that, though.")
+
+(defvoo nntp-open-connection-function 'nntp-open-network-stream
   "*Function used for connecting to a remote system.
   "*Function used for connecting to a remote system.
-It will be called with the address of the remote system.
+It will be called with the buffer to output in.
 
 Two pre-made functions are `nntp-open-network-stream', which is the
 default, and simply connects to some port or other on the remote
 
 Two pre-made functions are `nntp-open-network-stream', which is the
 default, and simply connects to some port or other on the remote
-system (see nntp-port-number).  The other is `nntp-open-rlogin', which
+system (see nntp-port-number).  The other are `nntp-open-rlogin', which
 does an rlogin on the remote system, and then does a telnet to the
 does an rlogin on the remote system, and then does a telnet to the
-NNTP server available there (see nntp-rlogin-parameters).")
+NNTP server available there (see nntp-rlogin-parameters) and `nntp-open-telnet' which
+telnets to a remote system, logs in and does the same")
 
 
-(defvar nntp-rlogin-parameters '("telnet" "${NNTPSERVER:=localhost}" "nntp")
+(defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
   "*Parameters to `nntp-open-login'.
 That function may be used as `nntp-open-server-function'.  In that
 case, this list will be used as the parameter list given to rsh.")
 
   "*Parameters to `nntp-open-login'.
 That function may be used as `nntp-open-server-function'.  In that
 case, this list will be used as the parameter list given to rsh.")
 
-(defvar nntp-rlogin-user-name nil
+(defvoo nntp-rlogin-user-name nil
   "*User name on remote system when using the rlogin connect method.")
 
   "*User name on remote system when using the rlogin connect method.")
 
-(defvar nntp-address nil
-  "*The name of the NNTP server.")
+(defvoo nntp-telnet-parameters '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
+  "*Parameters to `nntp-open-telnet'.
+That function may be used as `nntp-open-server-function'.  In that
+case, this list will be executed as a command after logging in
+via telnet.")
+
+(defvoo nntp-telnet-user-name nil
+  "User name to log in via telnet with.")
+
+(defvoo nntp-telnet-passwd nil
+  "Password to use to log in via telnet with.")
 
 
-(defvar nntp-port-number "nntp"
-  "*Port number to connect to.")
+(defvoo nntp-end-of-line "\r\n"
+  "String to use on the end of lines when talking to the NNTP server.
+This is \"\\r\\n\" by default, but should be \"\\n\" when
+using rlogin or telnet to communicate with the server.")
 
 
-(defvar nntp-large-newsgroup 50
+(defvoo nntp-large-newsgroup 50
   "*The number of the articles which indicates a large newsgroup.
 If the number of the articles is greater than the value, verbose
 messages will be shown to indicate the current status.")
 
   "*The number of the articles which indicates a large newsgroup.
 If the number of the articles is greater than the value, verbose
 messages will be shown to indicate the current status.")
 
-(defvar nntp-buggy-select (memq system-type '(fujitsu-uts))
-  "*t if your select routine is buggy.
-If the select routine signals error or fall into infinite loop while
-waiting for the server response, the variable must be set to t.  In
-case of Fujitsu UTS, it is set to T since `accept-process-output'
-doesn't work properly.")
-
-(defvar nntp-maximum-request 400
+(defvoo nntp-maximum-request 400
   "*The maximum number of the requests sent to the NNTP server at one time.
 If Emacs hangs up while retrieving headers, set the variable to a
 lower value.")
 
   "*The maximum number of the requests sent to the NNTP server at one time.
 If Emacs hangs up while retrieving headers, set the variable to a
 lower value.")
 
-(defvar nntp-debug-read 10000
-  "*Display '...' every 10Kbytes of a message being received if it is non-nil.
-If it is a number, dots are displayed per the number.")
-
-(defvar nntp-nov-is-evil nil
+(defvoo nntp-nov-is-evil nil
   "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
 
   "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
 
-(defvar nntp-xover-commands '("XOVER" "XOVERVIEW")
+(defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
   "*List of strings that are used as commands to fetch NOV lines from a server.
 The strings are tried in turn until a positive response is gotten. If
 none of the commands are successful, nntp will just grab headers one
 by one.")
 
   "*List of strings that are used as commands to fetch NOV lines from a server.
 The strings are tried in turn until a positive response is gotten. If
 none of the commands are successful, nntp will just grab headers one
 by one.")
 
-(defvar nntp-nov-gap 20
+(defvoo nntp-nov-gap 20
   "*Maximum allowed gap between two articles.
 If the gap between two consecutive articles is bigger than this
 variable, split the XOVER request into two requests.")
 
   "*Maximum allowed gap between two articles.
 If the gap between two consecutive articles is bigger than this
 variable, split the XOVER request into two requests.")
 
-(defvar nntp-connection-timeout nil
+(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.")
 
   "*Number of seconds to wait before an nntp connection times out.
 If this variable is nil, which is the default, no timers are set.")
 
-(defvar nntp-news-default-headers nil
-  "*If non-nil, override `mail-default-headers' when posting news.")
-
-(defvar nntp-prepare-server-hook nil
+(defvoo nntp-prepare-server-hook nil
   "*Hook run before a server is opened.
 If can be used to set up a server remotely, for instance.  Say you
 have an account at the machine \"other.machine\".  This machine has
 access to an NNTP server that you can't access locally.  You could
 then use this hook to rsh to the remote machine and start a proxy NNTP
   "*Hook run before a server is opened.
 If can be used to set up a server remotely, for instance.  Say you
 have an account at the machine \"other.machine\".  This machine has
 access to an NNTP server that you can't access locally.  You could
 then use this hook to rsh to the remote machine and start a proxy NNTP
-server there that you can connect to.")
-
-(defvar nntp-async-number 5
-  "*How many articles should be prefetched when in asynchronous mode.")
+server there that you can connect to. See also `nntp-open-connection-function'")
 
 
+(defvoo nntp-warn-about-losing-connection t
+  "*If non-nil, beep when a server closes connection.")
 
 \f
 
 
 \f
 
-(defconst nntp-version "nntp 4.0"
-  "Version numbers of this version of NNTP.")
-
-(defvar nntp-server-buffer nil
-  "Buffer associated with the NNTP server process.")
-
-(defvar nntp-server-process nil
-  "The NNTP server process.
-You'd better not use this variable in NNTP front-end program, but
-instead use `nntp-server-buffer'.")
+;;; Internal variables.
 
 
-(defvar nntp-status-string nil
-  "Save the server response message.
-You'd better not use this variable in NNTP front-end program but
-instead call function `nntp-status-message' to get status message.")
+(defvar nntp-process-wait-for nil)
+(defvar nntp-process-to-buffer nil)
+(defvar nntp-process-callback nil)
+(defvar nntp-process-decode nil)
+(defvar nntp-process-start-point nil)
+(defvar nntp-inside-change-function nil)
 
 
-(defvar nntp-server-xover 'try)
-(defvar nntp-server-list-active-group 'try)
-(defvar nntp-current-group "")
-(defvar nntp-timeout-servers nil)
+(defvoo nntp-server-type nil)
+(defvoo nntp-connection-alist nil)
+(defvoo nntp-status-string "")
+(defconst nntp-version "nntp 5.0")
+(defvoo nntp-inhibit-erase nil)
 
 
-(defvar nntp-async-process nil)
-(defvar nntp-async-buffer nil)
-(defvar nntp-async-articles nil)
-(defvar nntp-async-fetched nil)
-(defvar nntp-async-group-alist nil)
+(defvoo nntp-server-xover 'try)
+(defvoo nntp-server-list-active-group 'try)
 
 
+(eval-and-compile
+  (autoload 'nnmail-read-passwd "nnmail"))
 
 \f
 
 \f
-(defvar nntp-current-server nil)
-(defvar nntp-server-alist nil)
-(defvar nntp-server-variables 
-  (list
-   (list 'nntp-server-hook nntp-server-hook)
-   (list 'nntp-server-opened-hook nntp-server-opened-hook)
-   (list 'nntp-port-number nntp-port-number)
-   (list 'nntp-address nntp-address)
-   (list 'nntp-large-newsgroup nntp-large-newsgroup)
-   (list 'nntp-buggy-select nntp-buggy-select)
-   (list 'nntp-maximum-request nntp-maximum-request)
-   (list 'nntp-debug-read nntp-debug-read)
-   (list 'nntp-nov-is-evil nntp-nov-is-evil)
-   (list 'nntp-xover-commands nntp-xover-commands)
-   (list 'nntp-connection-timeout nntp-connection-timeout)
-   (list 'nntp-news-default-headers nntp-news-default-headers)
-   (list 'nntp-prepare-server-hook nntp-prepare-server-hook) 
-   (list 'nntp-async-number nntp-async-number)
-   '(nntp-async-process nil)
-   '(nntp-async-buffer nil)
-   '(nntp-async-articles nil)
-   '(nntp-async-fetched nil)
-   '(nntp-async-group-alist nil)
-   '(nntp-server-process nil)
-   '(nntp-status-string nil)
-   '(nntp-server-xover try)
-   '(nntp-server-list-active-group try)
-   '(nntp-current-group "")))
 
 
-\f
 ;;; Interface functions.
 
 ;;; Interface functions.
 
-(defun nntp-retrieve-headers (sequence &optional newsgroup server)
-  "Retrieve the headers to the articles in SEQUENCE."
-  (nntp-possibly-change-server newsgroup server)
+(nnoo-define-basics nntp)
+
+(deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
+  "Retrieve the headers of ARTICLES."
+  (nntp-possibly-change-group group server)
   (save-excursion
   (save-excursion
-    (set-buffer nntp-server-buffer)
+    (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
     (erase-buffer)
     (if (and (not gnus-nov-is-evil) 
             (not nntp-nov-is-evil)
     (erase-buffer)
     (if (and (not gnus-nov-is-evil) 
             (not nntp-nov-is-evil)
-            (nntp-retrieve-headers-with-xover sequence))
+            (nntp-retrieve-headers-with-xover articles fetch-old))
+       ;; We successfully retrieved the headers via XOVER.
         'nov
         'nov
-      (let ((number (length sequence))
+      ;; XOVER didn't work, so we do it the hard, slow and inefficient
+      ;; way.  
+      (let ((number (length articles))
            (count 0)
            (received 0)
            (count 0)
            (received 0)
-           (last-point (point-min)))
+           (last-point (point-min))
+           (nntp-inhibit-erase t))
        ;; Send HEAD command.
        ;; Send HEAD command.
-       (while sequence
-         (nntp-send-strings-to-server 
-          "HEAD" (if (numberp (car sequence)) (int-to-string (car sequence))
-                   (car sequence)))
-         (setq sequence (cdr sequence)
+       (while articles
+         (nntp-send-command 
+          nil
+          "HEAD" (if (numberp (car articles)) 
+                     (int-to-string (car articles))
+                   ;; `articles' is either a list of article numbers
+                   ;; or a list of article IDs.
+                   (car articles)))
+         (setq articles (cdr articles)
                count (1+ count))
                count (1+ count))
-         ;; Every 400 header requests we have to read stream in order
-         ;;  to avoid deadlock.
-         (if (or (null sequence)       ;All requests have been sent.
-                 (zerop (% count nntp-maximum-request)))
-             (progn
-               (nntp-accept-response)
-               (while (progn
-                        (goto-char last-point)
-                        ;; Count replies.
-                        (while (re-search-forward "^[0-9]" nil t)
-                          (setq received (1+ received)))
-                        (setq last-point (point))
-                        (< received count))
-                 ;; If number of headers is greater than 100, give
-                 ;;  informative messages.
-                 (and (numberp nntp-large-newsgroup)
-                      (> number nntp-large-newsgroup)
-                      (zerop (% received 20))
-                      (message "NNTP: Receiving headers... %d%%"
-                               (/ (* received 100) number)))
-                 (nntp-accept-response)))))
+         ;; Every 400 header requests we have to read the stream in
+         ;; order to avoid deadlocks.
+         (when (or (null articles)     ;All requests have been sent.
+                   (zerop (% count nntp-maximum-request)))
+           (nntp-accept-response)
+           (while (progn
+                    (goto-char last-point)
+                    ;; Count replies.
+                    (while (re-search-forward "^[0-9]" nil t)
+                      (setq received (1+ received)))
+                    (setq last-point (point))
+                    (< received count))
+             ;; If number of headers is greater than 100, give
+             ;;  informative messages.
+             (and (numberp nntp-large-newsgroup)
+                  (> number nntp-large-newsgroup)
+                  (zerop (% received 20))
+                  (message "NNTP: Receiving headers... %d%%"
+                           (/ (* received 100) number)))
+             (nntp-accept-response))))
        ;; Wait for text of last command.
        (goto-char (point-max))
        (re-search-backward "^[0-9]" nil t)
        ;; Wait for text of last command.
        (goto-char (point-max))
        (re-search-backward "^[0-9]" nil t)
-       (if (looking-at "^[23]")
-           (while (progn
-                    (goto-char (- (point-max) 3))
-                    (not (looking-at "^\\.\r?\n")))
-             (nntp-accept-response)))
+       (when (looking-at "^[23]")
+         (while (progn
+                  (goto-char (- (point-max) 3))
+                  (not (looking-at "^\\.\r?\n")))
+           (nntp-accept-response)))
        (and (numberp nntp-large-newsgroup)
             (> number nntp-large-newsgroup)
             (message "NNTP: Receiving headers...done"))
 
        (and (numberp nntp-large-newsgroup)
             (> number nntp-large-newsgroup)
             (message "NNTP: Receiving headers...done"))
 
-       ;; Now all of replies are received.
-       (setq received number)
-       ;; First, fold continuation lines.
-       (goto-char (point-min))
-       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
-         (replace-match " "))
-       ;; Remove all "\r"'s
+       ;; Now all of replies are received.  Fold continuation lines.
+       (nnheader-fold-continuation-lines)
+       ;; Remove all "\r"'s.
        (goto-char (point-min))
        (while (search-forward "\r" nil t)
        (goto-char (point-min))
        (while (search-forward "\r" nil t)
-         (replace-match ""))
+         (replace-match "" t t))
+       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
        'headers))))
 
        'headers))))
 
-
-(defun nntp-retrieve-groups (groups &optional server)
-  (nntp-possibly-change-server nil server)
+(deffoo nntp-retrieve-groups (groups &optional server)
+  "Retrieve group info on GROUPS."
+  (nntp-possibly-change-group nil server)
   (save-excursion
     (set-buffer nntp-server-buffer)
   (save-excursion
     (set-buffer nntp-server-buffer)
-    (and (eq nntp-server-list-active-group 'try)
-        (nntp-try-list-active (car groups)))
+    ;; The first time this is run, this variable is `try'.  So we
+    ;; try.   
+    (when (eq nntp-server-list-active-group 'try)
+      (nntp-try-list-active (car groups)))
     (erase-buffer)
     (let ((count 0)
          (received 0)
          (last-point (point-min))
     (erase-buffer)
     (let ((count 0)
          (received 0)
          (last-point (point-min))
-         (command (if nntp-server-list-active-group
-                      "LIST ACTIVE" "GROUP")))
+         (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
       (while groups
       (while groups
-       (nntp-send-strings-to-server command (car groups))
+       ;; Send the command to the server.
+       (nntp-send-command nil command (car groups))
        (setq groups (cdr groups))
        (setq count (1+ count))
        ;; Every 400 requests we have to read the stream in
        ;; order to avoid deadlocks.
        (setq groups (cdr groups))
        (setq count (1+ count))
        ;; Every 400 requests we have to read the stream in
        ;; order to avoid deadlocks.
-       (if (or (null groups)           ;All requests have been sent.
-               (zerop (% count nntp-maximum-request)))
-           (progn
-             (nntp-accept-response)
-             (while (progn
-                      (goto-char last-point)
-                      ;; Count replies.
-                      (while (re-search-forward "^[0-9]" nil t)
-                        (setq received (1+ received)))
-                      (setq last-point (point))
-                      (< received count))
-               (nntp-accept-response)))))
+       (when (or (null groups)         ;All requests have been sent.
+                 (zerop (% count nntp-maximum-request)))
+         (nntp-accept-response)
+         (while (progn
+                  (goto-char last-point)
+                  ;; Count replies.
+                  (while (re-search-forward "^[0-9]" nil t)
+                    (setq received (1+ received)))
+                  (setq last-point (point))
+                  (< received count))
+           (nntp-accept-response))))
 
       ;; Wait for the reply from the final command.
 
       ;; Wait for the reply from the final command.
-      (if nntp-server-list-active-group
-         (progn
-           (goto-char (point-max))
-           (re-search-backward "^[0-9]" nil t)
-           (if (looking-at "^[23]")
-               (while (progn
-                        (goto-char (- (point-max) 3))
-                        (not (looking-at "^\\.\r?\n")))
-                 (nntp-accept-response)))))
+      (when nntp-server-list-active-group
+       (goto-char (point-max))
+       (re-search-backward "^[0-9]" nil t)
+       (when (looking-at "^[23]")
+         (while (progn
+                  (goto-char (- (point-max) 3))
+                  (not (looking-at "^\\.\r?\n")))
+           (nntp-accept-response))))
 
       ;; Now all replies are received. We remove CRs.
       (goto-char (point-min))
       (while (search-forward "\r" nil t)
        (replace-match "" t t))
 
 
       ;; Now all replies are received. We remove CRs.
       (goto-char (point-min))
       (while (search-forward "\r" nil t)
        (replace-match "" t t))
 
-      (if nntp-server-list-active-group
-         (progn
-           ;; We have read active entries, so we just delete the
-           ;; superfluos gunk.
-           (goto-char (point-min))
-           (while (re-search-forward "^[.2-5]" nil t)
-             (delete-region (match-beginning 0) 
-                            (progn (forward-line 1) (point))))
-           'active)
-       'group))))
+      (if (not nntp-server-list-active-group)
+         'group
+       ;; We have read active entries, so we just delete the
+       ;; superfluos gunk.
+       (goto-char (point-min))
+       (while (re-search-forward "^[.2-5]" nil t)
+         (delete-region (match-beginning 0) 
+                        (progn (forward-line 1) (point))))
+       'active))))
 
 
-(defun nntp-open-server (server &optional defs)
-  (nnheader-init-server-buffer)
-  (if (nntp-server-opened server)
-      t
-    (if (or (stringp (car defs))
-           (numberp (car defs)))
-       (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
-    (or (assq 'nntp-address defs)
-       (setq defs (append defs (list (list 'nntp-address server)))))
-    (if (and nntp-current-server
-            (not (equal server nntp-current-server)))
-       (setq nntp-server-alist 
-             (cons (list nntp-current-server
-                         (nnheader-save-variables nntp-server-variables))
-                   nntp-server-alist)))
-    (let ((state (assoc server nntp-server-alist)))
-      (if state 
-         (progn
-           (nnheader-restore-variables (nth 1 state))
-           (setq nntp-server-alist (delq state nntp-server-alist)))
-       (nnheader-set-init-variables nntp-server-variables defs)))
-    (setq nntp-current-server server)
-    (or (nntp-server-opened server)
-       (progn
-         (if (member nntp-address nntp-timeout-servers)
-             nil
-           (run-hooks 'nntp-prepare-server-hook)
-           (nntp-open-server-semi-internal nntp-address nntp-port-number))))))
-
-(defun nntp-close-server (&optional server)
-  "Close connection to SERVER."
-  (nntp-possibly-change-server nil server)
-  (unwind-protect
-      (progn
-       ;; Un-set default sentinel function before closing connection.
-       (and nntp-server-process
-            (eq 'nntp-default-sentinel
-                (process-sentinel nntp-server-process))
-            (set-process-sentinel nntp-server-process nil))
-       ;; We cannot send QUIT command unless the process is running.
-       (if (nntp-server-opened)
-           (nntp-send-command nil "QUIT")))
-    (nntp-close-server-internal server)
-    (setq nntp-timeout-servers (delete server nntp-timeout-servers))))
-
-(defalias 'nntp-request-quit (symbol-function 'nntp-close-server))
-
-(defun nntp-request-close ()
-  "Close all server connections."
-  (let (proc)
-    (and nntp-async-process
-        (progn
-          (delete-process nntp-async-process)
-          (and (get-buffer nntp-async-buffer)
-               (kill-buffer nntp-async-buffer))))
-    (while nntp-async-group-alist
-      (and (nth 3 (car nntp-async-group-alist))
-          (delete-process (nth 3 (car nntp-async-group-alist))))
-      (setq nntp-async-group-alist (cdr nntp-async-group-alist)))
-    (while nntp-server-alist
-      (and 
-       (setq proc (nth 1 (assq 'nntp-server-process (car nntp-server-alist))))
-       (delete-process proc))
-      (and 
-       (setq proc (nth 1 (assq 'nntp-async-process (car nntp-server-alist))))
-       (delete-process proc))
-      (and (setq proc (nth 1 (assq 'nntp-async-buffer
-                                  (car nntp-server-alist))))
-          (buffer-name proc)
-          (kill-buffer proc))
-      (setq nntp-server-alist (cdr nntp-server-alist)))
-    (setq nntp-current-server nil
-         nntp-timeout-servers nil
-         nntp-async-group-alist nil)))
-
-(defun nntp-server-opened (&optional server)
-  "Say whether a connection to SERVER has been opened."
-  (and (equal server nntp-current-server)
-       nntp-server-buffer
-       (buffer-name nntp-server-buffer)
-       nntp-server-process
-       (memq (process-status nntp-server-process) '(open run))))
-
-(defun nntp-status-message (&optional server)
-  "Return server status as a string."
-  (if (and nntp-status-string
-          ;; NNN MESSAGE
-          (string-match "[0-9][0-9][0-9][ \t]+\\([^\r]*\\).*$"
-                        nntp-status-string))
-      (substring nntp-status-string (match-beginning 1) (match-end 1))
-    ;; Empty message if nothing.
-    (or nntp-status-string "")))
-
-(defun nntp-request-article (id &optional newsgroup server buffer)
-  "Request article ID (message-id or number)."
-  (nntp-possibly-change-server newsgroup server)
-
-  (let (found)
-
-    ;; First we see whether we can get the article from the async buffer. 
-    (if (and (numberp id)
-            nntp-async-articles
-            (memq id nntp-async-fetched))
-       (save-excursion
-         (set-buffer nntp-async-buffer)
-         (let ((opoint (point))
-               (art (if (numberp id) (int-to-string id) id))
-               beg end)
-           (if (and (or (re-search-forward (concat "^2.. +" art) nil t)
-                        (progn
-                          (goto-char (point-min))
-                          (re-search-forward (concat "^2.. +" art) opoint t)))
-                    (progn
-                      (beginning-of-line)
-                      (setq beg (point)
-                            end (re-search-forward "^\\.\r?\n" nil t))))
-               (progn
-                 (setq found t)
-                 (save-excursion
-                   (set-buffer (or buffer nntp-server-buffer))
-                   (erase-buffer)
-                   (insert-buffer-substring nntp-async-buffer beg end)
-                   (let ((nntp-server-buffer (current-buffer)))
-                     (nntp-decode-text)))
-                 (delete-region beg end)
-                 (and nntp-async-articles
-                      (nntp-async-fetch-articles id)))))))
-
-    (if found 
-       t
-      ;; The article was not in the async buffer, so we fetch it now.
-      (unwind-protect
-         (progn
-           (if buffer (set-process-buffer nntp-server-process buffer))
-           (let ((nntp-server-buffer (or buffer nntp-server-buffer))
-                 (art (or (and (numberp id) (int-to-string id)) id)))
-             ;; If NEmacs, end of message may look like: "\256\215" (".^M")
-             (prog1
-                 (nntp-send-command "^\\.\r?\n" "ARTICLE" art)
-               (nntp-decode-text)
-               (and nntp-async-articles (nntp-async-fetch-articles id)))))
-       (if buffer (set-process-buffer 
-                   nntp-server-process nntp-server-buffer))))))
-
-(defun nntp-request-body (id &optional newsgroup server)
-  "Request body of article ID (message-id or number)."
-  (nntp-possibly-change-server newsgroup server)
-  (prog1
-      ;; If NEmacs, end of message may look like: "\256\215" (".^M")
-      (nntp-send-command
-       "^\\.\r?\n" "BODY" (or (and (numberp id) (int-to-string id)) id))
-    (nntp-decode-text)))
-
-(defun nntp-request-head (id &optional newsgroup server)
-  "Request head of article ID (message-id or number)."
-  (nntp-possibly-change-server newsgroup server)
-  (prog1
-      (nntp-send-command 
-       "^\\.\r?\n" "HEAD" (or (and (numberp id) (int-to-string id)) id))
-    (nntp-decode-text)))
-
-(defun nntp-request-stat (id &optional newsgroup server)
-  "Request STAT of article ID (message-id or number)."
-  (nntp-possibly-change-server newsgroup server)
-  (nntp-send-command 
-   "^[23].*\r?\n" "STAT" (or (and (numberp id) (int-to-string id)) id)))
-
-(defun nntp-request-group (group &optional server dont-check)
-  "Select GROUP."
-  (nntp-send-command "^.*\r?\n" "GROUP" group)
-  (setq nntp-current-group group)
+ (defun nntp-try-list-active (group)
+  (nntp-list-active-group group)
   (save-excursion
     (set-buffer nntp-server-buffer)
     (goto-char (point-min))
   (save-excursion
     (set-buffer nntp-server-buffer)
     (goto-char (point-min))
-    (looking-at "[23]")))
-
-(defun nntp-request-asynchronous (group &optional server articles)
-  (and nntp-async-articles (nntp-async-request-group group))
-  (and 
-   nntp-async-number
-   (if (not (or (nntp-async-server-opened)
-               (nntp-async-open-server)))
-       (progn
-        (message "Can't open second connection to %s" nntp-address)
-        (ding)
-        (setq nntp-async-articles nil)
-        (sit-for 2))
-     (setq nntp-async-articles articles)
-     (setq nntp-async-fetched nil)
-     (save-excursion
-       (set-buffer nntp-async-buffer)
-       (erase-buffer))
-     (nntp-async-send-strings "GROUP" group)
-     t)))
-
-(defun nntp-list-active-group (group &optional server)
-  (nntp-send-command "^.*\r?\n" "LIST ACTIVE" group))
+    (cond ((looking-at "5[0-9]+")
+          (setq nntp-server-list-active-group nil))
+         (t
+          (setq nntp-server-list-active-group t)))))
 
 
-(defun nntp-request-group-description (group &optional server)
-  "Get description of GROUP."
-  (if (nntp-possibly-change-server nil server)
-      (prog1
-         (nntp-send-command "^.*\r?\n" "XGTITLE" group)
-       (nntp-decode-text))))
+(deffoo nntp-list-active-group (group &optional server)
+  "Return the active info on GROUP (which can be a regexp."
+  (nntp-possibly-change-group group server)
+  (nntp-send-command "^.*\r?\n" "LIST ACTIVE" group))
 
 
-(defun nntp-close-group (group &optional server)
-  (setq nntp-current-group nil)
+(deffoo nntp-request-article (article &optional group server buffer command)
+  (nntp-possibly-change-group group server)
+  (when (nntp-send-command-and-decode
+        "\r?\n\\.\r?\n" "ARTICLE"
+        (if (numberp article) (int-to-string article) article))
+    (when buffer
+      (save-excursion
+       (set-buffer nntp-server-buffer)
+       (copy-to-buffer buffer (point-min) (point-max))
+       (nntp-find-group-and-number)))
+    (nntp-find-group-and-number)))
+
+(deffoo nntp-request-head (article &optional group server)
+  (nntp-possibly-change-group group server)
+  (when (nntp-send-command-and-decode
+        "\r\n\\.\r\n" "HEAD"
+        (if (numberp article) (int-to-string article) article))
+    (nntp-find-group-and-number)))
+
+(deffoo nntp-request-body (article &optional group server)
+  (nntp-possibly-change-group group server)
+  (nntp-send-command-and-decode
+   "\r?\n\\.\r?\n" "BODY"
+   (if (numberp article) (int-to-string article) article)))
+
+(deffoo nntp-request-group (group &optional server dont-check)
+  (nntp-possibly-change-group nil server)
+  (when (nntp-send-command "^2.*\n" "GROUP" group)
+    (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
+      (setcar (cddr entry) group))))
+
+(deffoo nntp-close-group (group &optional server)
   t)
 
   t)
 
-(defun nntp-request-list (&optional server)
-  "List active groups."
-  (nntp-possibly-change-server nil server)
-  (prog1
-      (nntp-send-command "^\\.\r?\n" "LIST")
-    (nntp-decode-text)))
-
-(defun nntp-request-list-newsgroups (&optional server)
-  "List groups."
-  (nntp-possibly-change-server nil server)
-  (prog1
-      (nntp-send-command "^\\.\r?\n" "LIST NEWSGROUPS")
-    (nntp-decode-text)))
-
-(defun nntp-request-newgroups (date &optional server)
-  "List new groups."
-  (nntp-possibly-change-server nil server)
-  (let* ((date (timezone-parse-date date))
-        (time-string
-         (format "%s%02d%02d %s%s%s"
-                 (substring (aref date 0) 2) (string-to-int (aref date 1)) 
-                 (string-to-int (aref date 2)) (substring (aref date 3) 0 2)
-                 (substring 
-                  (aref date 3) 3 5) (substring (aref date 3) 6 8))))
-    (prog1
-       (nntp-send-command "^\\.\r?\n" "NEWGROUPS" time-string)
-      (nntp-decode-text))))
-
-(defun nntp-request-list-distributions (&optional server)
-  "List distributions."
-  (nntp-possibly-change-server nil server)
-  (prog1
-      (nntp-send-command "^\\.\r?\n" "LIST DISTRIBUTIONS")
-    (nntp-decode-text)))
-
-(defun nntp-request-last (&optional newsgroup server)
-  "Decrease the current article pointer."
-  (nntp-possibly-change-server newsgroup server)
-  (nntp-send-command "^[23].*\r?\n" "LAST"))
-
-(defun nntp-request-next (&optional newsgroup server)
-  "Advance the current article pointer."
-  (nntp-possibly-change-server newsgroup server)
-  (nntp-send-command "^[23].*\r?\n" "NEXT"))
-
-(defun nntp-request-post (&optional server)
-  "Post the current buffer."
-  (nntp-possibly-change-server nil server)
-  (if (nntp-send-command "^[23].*\r?\n" "POST")
-      (progn
-       (nntp-encode-text)
-       (nntp-send-region-to-server (point-min) (point-max))
-       ;; 1.2a NNTP's post command is buggy. "^M" (\r) is not
-       ;;  appended to end of the status message.
-       (nntp-wait-for-response "^[23].*\n"))))
-
-(defun nntp-request-post-buffer 
-  (post group subject header article-buffer info follow-to respect-poster)
-  "Request a buffer suitable for composing an article.
-If POST, this is an original article; otherwise it's a followup.
-GROUP is the group to be posted to, the article should have subject
-SUBJECT.  HEADER is a Gnus header vector.  ARTICLE-BUFFER contains the
-article being followed up.  INFO is a Gnus info list.  If FOLLOW-TO,
-post to this group instead.  If RESPECT-POSTER, heed the special
-\"poster\" value of the Followup-to header."
-  (if (assq 'to-address (nth 5 info))
-      (nnmail-request-post-buffer 
-       post group subject header article-buffer info follow-to respect-poster)
-    (let ((mail-default-headers 
-          (or nntp-news-default-headers mail-default-headers))
-         from date to followup-to newsgroups message-of
-         references distribution message-id)
-      (save-excursion
-       (set-buffer (get-buffer-create "*post-news*"))
-       (news-reply-mode)
-       (if (and (buffer-modified-p)
-                (> (buffer-size) 0)
-                (not (y-or-n-p "Unsent article being composed; erase it? ")))
-           ()
-         (erase-buffer)
-         (if post
-             (news-setup nil subject nil group nil)
-           (save-excursion
-             (set-buffer article-buffer)
-             (goto-char (point-min))
-             (narrow-to-region (point-min)
-                               (progn (search-forward "\n\n") (point)))
-             (setq from (mail-header-from header))
-             (setq date (mail-header-date header))
-             (and from
-                  (let ((stop-pos 
-                         (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
-                    (setq 
-                     message-of
-                     (concat (if stop-pos (substring from 0 stop-pos) from) 
-                             "'s message of " date))))
-             (setq subject (or subject (mail-header-subject header)))
-             (or (string-match "^[Rr][Ee]:" subject)
-                 (setq subject (concat "Re: " subject)))
-             (setq followup-to (mail-fetch-field "followup-to"))
-             (if (or (null respect-poster) ;Ignore followup-to: field.
-                     (string-equal "" followup-to) ;Bogus header.
-                     (string-equal "poster" followup-to);Poster
-                     (and (eq respect-poster 'ask)
-                          followup-to
-                          (y-or-n-p (concat "Followup to " 
-                                            followup-to "? "))))
-                 (setq followup-to nil))
-             (setq newsgroups
-                   (or follow-to followup-to (mail-fetch-field "newsgroups")))
-             (setq references (mail-header-references header))
-             (setq distribution (mail-fetch-field "distribution"))
-             ;; Remove bogus distribution.
-             (and (stringp distribution)
-                  (string-match "world" distribution)
-                  (setq distribution nil))
-             (setq message-id (mail-header-id header))
-             (widen))
-           (setq news-reply-yank-from from)
-           (setq news-reply-yank-message-id message-id)
-           (news-setup to subject message-of 
-                       (if (stringp newsgroups) newsgroups "") 
-                       article-buffer)
-           (if (and newsgroups (listp newsgroups))
-               (progn
-                 (goto-char (point-min))
-                 (while newsgroups
-                   (insert (car (car newsgroups)) ": " 
-                           (cdr (car newsgroups)) "\n")
-                   (setq newsgroups (cdr newsgroups)))))
-           (nnheader-insert-references references message-id)
-           (if distribution
-               (progn
-                 (mail-position-on-field "Distribution")
-                 (insert distribution)))))
-       (current-buffer)))))
+(deffoo nntp-server-opened (&optional server)
+  "Say whether a connection to SERVER has been opened."
+  (and (nnoo-current-server-p 'nntp server)
+       nntp-server-buffer
+       (gnus-buffer-live-p nntp-server-buffer)
+       (nntp-find-connection nntp-server-buffer)))
 
 
-;;; Internal functions.
+(deffoo nntp-open-server (server &optional defs connectionless)
+  (nnheader-init-server-buffer)
+  (if (nntp-server-opened server)
+      t
+    (when (or (stringp (car defs))
+             (numberp (car defs)))
+      (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
+    (unless (assq 'nntp-address defs)
+      (setq defs (append defs (list (list 'nntp-address server)))))
+    (nnoo-change-server 'nntp server defs)
+    (unless connectionless
+      (or (nntp-find-connection nntp-server-buffer)
+         (nntp-open-connection nntp-server-buffer)))))
+
+(deffoo nntp-close-server (&optional server)
+  (nntp-possibly-change-group nil server t)
+  (let (process)
+    (while (setq process (car (pop nntp-connection-alist)))
+      (when (memq (process-status process) '(open run))
+       (set-process-sentinel process nil)
+       (nntp-send-string process "QUIT"))
+      (when (buffer-name (process-buffer process))
+       (kill-buffer (process-buffer process))))
+    (nnoo-close-server 'nntp)))