*** empty log message ***
[gnus] / lisp / nntp.el
index 71fb502..e942036 100644 (file)
@@ -1,9 +1,8 @@
 ;;; nntp.el --- NNTP (RFC977) Interface for GNU Emacs
-
 ;; Copyright (C) 1987,88,89,90,92,93,94,95 Free Software Foundation, Inc.
 
 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
-;;     Lars Ingebrigtsen <larsi@ifi.uio.no>
+;;     Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
 ;; Keywords: news
 
 ;; This file is part of GNU Emacs.
@@ -31,7 +30,9 @@
 
 (eval-and-compile
   (autoload 'news-setup "rnewspost")
-  (autoload 'news-reply-mode "rnewspost"))
+  (autoload 'news-reply-mode "rnewspost")
+  (autoload 'nnmail-request-post-buffer "nnmail")
+  (autoload 'cancel-timer "timer"))
 
 (defvar nntp-server-hook nil
   "Hooks for the NNTP server.
@@ -49,28 +50,28 @@ server must be specified as follows:
 If you'd like to change something depending on the server in this
 hook, use the variable `nntp-server-name'.")
 
-(defvar nntp-server-opened-hook 
-  (list
-   (lambda ()
-     (nntp-send-command "MODE" "READER")))
-  "Hook used for sending commands to the server at startup.
-It is used by default to send the \"MODE READER\" command to the
-server. This makes innd servers spawn an nnrpd server.
-Other useful commands might be \"AUTHINFO\".")
+(defvar nntp-server-opened-hook nil
+  "Hook used for sending commands to the server at startup.  The
+default value is `nntp-send-mode-reader', whick makes an innd server
+spawn an nnrpd server.  Another useful function to put in this hook
+might be `nntp-send-authinfo', which will prompt for a password 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)
 
 (defvar 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.")
 
-(defvar nntp-buggy-select (memq system-type '(usg-unix-v fujitsu-uts))
+(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 1
+(defvar 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.")
@@ -88,19 +89,23 @@ 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-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.")
+
 \f
 (defconst nntp-version "nntp 4.0"
   "Version numbers of this version of NNTP.")
 
 (defvar nntp-server-name nil
-  "The name of the host running NNTP server.")
+  "The name of the NNTP server.")
 
 (defvar nntp-server-buffer nil
-  "Buffer associated with NNTP server process.")
+  "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
+You'd better not use this variable in NNTP front-end program, but
 instead use `nntp-server-buffer'.")
 
 (defvar nntp-status-string nil
@@ -109,11 +114,9 @@ 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-current-server "")
-
 (defvar nntp-server-alist nil)
-
 (defvar nntp-server-xover t)
-
+(defvar nntp-server-list-active-group t)
 (defvar nntp-current-group "")
 
 ;;; Interface funtions.
@@ -157,9 +160,7 @@ instead call function `nntp-status-message' to get status message.")
                       (zerop (% received 20))
                       (message "NNTP: Receiving headers... %d%%"
                                (/ (* received 100) number)))
-                 (nntp-accept-response))
-               ))
-         )
+                 (nntp-accept-response)))))
        ;; Wait for text of last command.
        (goto-char (point-max))
        (re-search-backward "^[0-9]" nil t)
@@ -171,6 +172,7 @@ instead call function `nntp-status-message' to get status message.")
        (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.
@@ -184,38 +186,40 @@ instead call function `nntp-status-message' to get status message.")
        'headers))))
 
 (defun nntp-open-server (server &optional service)
-  "Open news server on SERVER.
+  "Open SERVER.
 If SERVER is nil, use value of environment variable `NNTPSERVER'.
-If optional argument SERVICE is non-nil, open by the service name."
+If SERVICE, this this as the port number."
   (let ((server (or server (getenv "NNTPSERVER")))
-       (status nil))
+       (status nil)
+       (timer 
+        (and nntp-connection-timeout 
+             (run-at-time nntp-connection-timeout
+                          nil 'nntp-kill-connection server))))
     (setq nntp-status-string "")
     (message "nntp: Connecting to server on %s..." server)
     (cond ((and server (nntp-open-server-internal server service))
           (setq nntp-current-server server)
-          (setq status (nntp-wait-for-response "^[23].*\r$"))
-          ;; Do check unexpected close of connection.
-          ;; Suggested by feldmark@hanako.stars.flab.fujitsu.junet.
-          (if status
-              (progn
-                (set-process-sentinel nntp-server-process
-                                      'nntp-default-sentinel)
-                ;; You can send commands at startup like AUTHINFO here.
-                ;; Added by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>
-                (run-hooks 'nntp-server-opened-hook))
-            ;; We have to close connection here, since function
-            ;;  `nntp-server-opened' may return incorrect status.
-            (nntp-close-server-internal server)
-            ))
+          (setq status
+                (condition-case nil
+                    (nntp-wait-for-response "^[23].*\r$")
+                  (error nil)
+                  (quit nil)))
+          (or status (nntp-close-server-internal server))
+          (and nntp-server-process
+               (progn
+                 (set-process-sentinel 
+                  nntp-server-process 'nntp-default-sentinel)
+                 ;; You can send commands at startup like AUTHINFO here.
+                 ;; Added by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>
+                 (run-hooks 'nntp-server-opened-hook))))
          ((null server)
-          (setq nntp-status-string "NNTP server is not specified."))
-         )
+          (setq nntp-status-string "NNTP server is not specified.")))
+    (and timer (cancel-timer timer))
     (message "")
-    status
-    ))
+    status))
 
 (defun nntp-close-server (&optional server)
-  "Close news server."
+  "Close connection to SERVER."
   (nntp-possibly-change-server nil server)
   (unwind-protect
       (progn
@@ -226,16 +230,21 @@ If optional argument SERVICE is non-nil, open by the service name."
             (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)
-    ))
+           (nntp-send-command nil "QUIT")))
+    (nntp-close-server-internal server)))
 
 (fset 'nntp-request-quit (symbol-function 'nntp-close-server))
 
+(defun nntp-request-close ()
+  "Close all server connections."
+  (while nntp-server-alist
+    (delete-process (car (cdr (car nntp-server-alist))))
+    (setq nntp-server-alist (cdr nntp-server-alist)))
+  (setq nntp-current-server "")
+  (setq nntp-server-process nil))
+
 (defun nntp-server-opened (&optional server)
-  "Return server process status.
-If the stream is opened, return non-nil, otherwise return nil."
+  "Say whether a connection to SERVER has been opened."
   (if (or server nntp-current-server)
       (let ((process (nth 1 (assoc (or server nntp-current-server)
                                   nntp-server-alist))))
@@ -243,23 +252,23 @@ If the stream is opened, return non-nil, otherwise return nil."
             (memq (process-status process) '(open run))))))
 
 (defun nntp-status-message (&optional server)
-  "Return server status response as string."
+  "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.
-    ""
-    ))
+    nntp-status-string))
 
 (defun nntp-request-article (id &optional newsgroup server buffer)
-  "Select article by message ID (or number)."
+  "Request article ID (message-id or number)."
   (nntp-possibly-change-server newsgroup server)
   (unwind-protect
       (progn
        (if buffer (set-process-buffer nntp-server-process buffer))
-       (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
+       (let ((nntp-server-buffer (or buffer nntp-server-buffer))
+             (id (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$" "ARTICLE" id)
@@ -267,81 +276,129 @@ If the stream is opened, return non-nil, otherwise return nil."
     (if buffer (set-process-buffer nntp-server-process nntp-server-buffer))))
 
 (defun nntp-request-body (id &optional newsgroup server)
-  "Select article body by message ID (or number)."
+  "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$" "BODY" id)
-    (nntp-decode-text)
-    ))
+      (nntp-send-command
+       "^\\.\r$" "BODY" (or (and (numberp id) (int-to-string id)) id))
+    (nntp-decode-text)))
 
 (defun nntp-request-head (id &optional newsgroup server)
-  "Select article head by message ID (or number)."
+  "Request head of article ID (message-id or number)."
   (nntp-possibly-change-server newsgroup server)
   (prog1
-      (nntp-send-command "^\\.\r$" "HEAD" id)
-    (nntp-decode-text)
-    ))
+      (nntp-send-command 
+       "^\\.\r$" "HEAD" (or (and (numberp id) (int-to-string id)) id))
+    (nntp-decode-text)))
 
 (defun nntp-request-stat (id &optional newsgroup server)
-  "Select article by message ID (or number)."
+  "Request STAT of article ID (message-id or number)."
   (nntp-possibly-change-server newsgroup server)
-  (nntp-send-command "^[23].*\r$" "STAT" id))
+  (nntp-send-command 
+   "^[23].*\r$" "STAT" (or (and (numberp id) (int-to-string id)) id)))
 
 (defun nntp-request-group (group &optional server dont-check)
-  "Select news GROUP."
-  (if (nntp-possibly-change-server nil server)
-      (progn
+  "Select GROUP."
+  (if (not (nntp-possibly-change-server nil server))
+      ()
+    (if dont-check
        (nntp-send-command "^.*\r$" "GROUP" group)
-       )))
+      (if nntp-server-list-active-group
+         (save-excursion
+           (nntp-list-active-group group server)
+           (set-buffer nntp-server-buffer)
+           (goto-char (point-min))
+           ;; We look at the output from `nntp-list-active-group' to
+           ;; see whether the server supports this command.  If it
+           ;; does, we transform the output.  
+           (cond ((looking-at "2[0-9]+")
+                  (forward-line 1)
+                  (if (looking-at "[^ ] +\\([0-9]\\) +\\([0-9]\\)")
+                      (let ((end (progn (goto-char (match-beginning 1))
+                                        (read (current-buffer))))
+                            (beg (read (current-buffer))))
+                        (and (> beg end)
+                             (setq end 0
+                                   beg 0))
+                        (erase-buffer)
+                        (insert (format "211 %s %d %d %d\n"
+                                        group (max (- (1+ end) beg) 0)
+                                        beg end)))))
+                 ;; The server does not support the command.
+                 ((looking-at "5[0-9]+")
+                  (setq nntp-server-list-active-group nil)
+                  (setcar (nthcdr 
+                           3 (assoc nntp-current-server nntp-server-alist))
+                          nntp-server-xover)
+                  (nntp-send-command "^.*\r$" "GROUP" group))
+                 ;; The server supports it, but the group doesn't
+                 ;; exist. 
+                 ((looking-at "4[0-9]+")
+                  (erase-buffer)
+                  nil)))
+       (nntp-send-command "^.*\r$" "GROUP" group)))))
+
+(defun nntp-list-active-group (group &optional server)
+  (nntp-send-command "^.*\r$" "LIST ACTIVE" group))
+
+(defun nntp-request-group-description (group &optional server)
+  "Get description of GROUP."
+  (if (nntp-possibly-change-server nil server)
+      (prog1
+         (nntp-send-command "^.*\r$" "XGTITLE" group)
+       (nntp-decode-text))))
 
 (defun nntp-close-group (group &optional server)
   t)
 
 (defun nntp-request-list (&optional server)
-  "List active newsgroups."
+  "List active groups."
   (nntp-possibly-change-server nil server)
   (prog1
       (nntp-send-command "^\\.\r$" "LIST")
-    (nntp-decode-text)
-    ))
+    (nntp-decode-text)))
 
 (defun nntp-request-list-newsgroups (&optional server)
-  "List newsgroups (defined in NNTP2)."
+  "List groups."
   (nntp-possibly-change-server nil server)
   (prog1
       (nntp-send-command "^\\.\r$" "LIST NEWSGROUPS")
-    (nntp-decode-text)
-    ))
+    (nntp-decode-text)))
 
 (defun nntp-request-newgroups (date &optional server)
-  "List new groups (defined in NNTP2)."
+  "List new groups."
   (nntp-possibly-change-server nil server)
-  (prog1
-      (nntp-send-command "^\\.\r$" "NEWGROUPS" date)
-    (nntp-decode-text)))
+  (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$" "NEWGROUPS" time-string)
+      (nntp-decode-text))))
 
 (defun nntp-request-list-distributions (&optional server)
-  "List distributions (defined in NNTP2)."
+  "List distributions."
   (nntp-possibly-change-server nil server)
   (prog1
       (nntp-send-command "^\\.\r$" "LIST DISTRIBUTIONS")
-    (nntp-decode-text)
-    ))
+    (nntp-decode-text)))
 
 (defun nntp-request-last (&optional newsgroup server)
-  "Set current article pointer to the previous article
-in the current news group."
+  "Decrease the current article pointer."
   (nntp-possibly-change-server newsgroup server)
   (nntp-send-command "^[23].*\r$" "LAST"))
 
 (defun nntp-request-next (&optional newsgroup server)
-  "Advance current article pointer."
+  "Advance the current article pointer."
   (nntp-possibly-change-server newsgroup server)
   (nntp-send-command "^[23].*\r$" "NEXT"))
 
 (defun nntp-request-post (&optional server)
-  "Post a new news in current buffer."
+  "Post the current buffer."
   (nntp-possibly-change-server nil server)
   (if (nntp-send-command "^[23].*\r$" "POST")
       (progn
@@ -349,86 +406,115 @@ in the current news group."
        (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].*$")
-       )))
-
-(defun nntp-request-post-buffer (method header article-buffer group info)
-  (let (from subject date to followup-to newsgroups message-of
-            references distribution message-id follow-to)
-    (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 (eq method 'post)
-           (news-setup nil nil nil header article-buffer)
-         (save-excursion
-           (set-buffer article-buffer)
-           (goto-char (point-min))
-           (narrow-to-region (point-min)
-                             (progn (search-forward "\n\n") (point)))
-           (if (and (boundp 'gnus-followup-to-function)
-                    gnus-followup-to-function)
-               (setq follow-to (funcall gnus-followup-to-function group)))
-           (setq from (header-from header))
-           (setq date (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 (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 gnus-use-followup-to) ;Ignore followup-to: field.
-                   (string-equal "" followup-to) ;Bogus header.
-                   (string-equal "poster" followup-to)) ;Poster
-               (setq followup-to nil))
-           (setq newsgroups (or followup-to (mail-fetch-field "newsgroups")))
-           (setq references (header-references header))
-           (setq distribution (mail-fetch-field "distribution"))
-           ;; Remove bogus distribution.
-           (and (string= distribution "world")
-                (setq distribution nil))
-           (setq message-id (header-id header))
-           (widen))
-         (setq news-reply-yank-from from)
-         (setq news-reply-yank-message-id message-id)
-         ;; Prevent getting BCC or FCC fields inserted for both mail
-         ;; and news.  
-         (let ((mail-self-blind
-                (and (not gnus-mail-self-blind) mail-self-blind))
-               (mail-archive-file-name
-                (and (not gnus-author-copy) mail-archive-file-name)))
-           (news-setup (and gnus-auto-mail-to-author from)
-                       subject message-of newsgroups article-buffer))
-         ;; Fold long references line to follow RFC1036.
-         (mail-position-on-field "References")
-         (let ((begin (- (point) (length "References: ")))
-               (fill-column 79)
-               (fill-prefix "\t"))
-           (if references (insert references))
-           (if (and references message-id) (insert " "))
-           (if message-id (insert message-id))
-           ;; The region must end with a newline to fill the region
-           ;; without inserting extra newline.
-           (fill-region-as-paragraph begin (1+ (point))))
-         (if distribution
-             (progn
-               (mail-position-on-field "Distribution")
-               (insert distribution)))))
-      (current-buffer))))
+       (nntp-wait-for-response "^[23].*$"))))
+
+(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 4 info))
+      (nnmail-request-post-buffer 
+       post group subject header article-buffer info follow-to respect-poster)
+    (let (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 (header-from header))
+             (setq date (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 (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
+                 (setq followup-to nil))
+             (setq newsgroups
+                   (or follow-to followup-to (mail-fetch-field "newsgroups")))
+             (setq references (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 (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)))))
+           ;; Fold long references line to follow RFC1036.
+           (mail-position-on-field "References")
+           (let ((begin (- (point) (length "References: ")))
+                 (fill-column 79)
+                 (fill-prefix "\t"))
+             (if references (insert references))
+             (if (and references message-id) (insert " "))
+             (if message-id (insert message-id))
+             ;; The region must end with a newline to fill the region
+             ;; without inserting extra newline.
+             (fill-region-as-paragraph begin (1+ (point))))
+           (if distribution
+               (progn
+                 (mail-position-on-field "Distribution")
+                 (insert distribution)))))
+       (current-buffer)))))
 
 ;;; Internal functions.
 
+(defun nntp-send-mode-reader ()
+  "Send the MODE READER command to the nntp server.
+This function is supposed to be called from `nntp-server-opened-hook'.
+It will make innd servers spawn an nnrpd process to allow actual article
+reading."
+  (nntp-send-command "^.*\r$" "MODE READER"))
+
+(defun nntp-send-authinfo ()
+  "Send the AUTHINFO to the nntp server.
+This function is supposed to be called from `nntp-server-opened-hook'.
+It will prompt for a password."
+  (nntp-send-command "^.*\r$" "AUTHINFO USER" (user-login-name))
+  (nntp-send-command "^.*\r$" "AUTHINFO PASS" (read-string "NNTP password: ")))
+
 (defun nntp-default-sentinel (proc status)
   "Default sentinel function for NNTP server process."
   (let ((servers nntp-server-alist))
+    ;; Go through the alist of server names and find the name of the
+    ;; server that the process that sent the signal is connected to.
+    ;; If you get my drift.
     (while (and servers 
                (not (equal proc (nth 1 (car servers)))))
       (setq servers (cdr servers)))
@@ -436,6 +522,14 @@ in the current news group."
             (or (car (car servers)) "(none)"))
     (ding)))
 
+(defun nntp-kill-connection (server)
+  (let ((proc (nth 1 (assoc server nntp-server-alist))))
+    (if proc (delete-process (process-name proc)))
+    (nntp-close-server server)
+    (setq nntp-status-string 
+         (message "Connection timed out to server %s." server))
+    (ding)))
+
 ;; Encoding and decoding of NNTP text.
 
 (defun nntp-decode-text ()
@@ -448,30 +542,26 @@ in the current news group."
     (set-buffer nntp-server-buffer)
     ;; Insert newline at end of buffer.
     (goto-char (point-max))
-    (if (not (bolp))
-       (insert "\n"))
+    (or (bolp) (insert "\n"))
     ;; Delete status line.
     (goto-char (point-min))
     (delete-region (point) (progn (forward-line 1) (point)))
-    ;; Delete `^M' at end of line.
-    ;; (replace-regexp "\r$" "")
+    ;; Delete `^M' at the end of lines.
     (while (not (eobp))
       (end-of-line)
-      (if (= (preceding-char) ?\r)
-         (delete-char -1))
-      (forward-line 1)
-      )
-    ;; Delete `.' at end of buffer (end of text mark).
+      (and (= (preceding-char) ?\r)
+          (delete-char -1))
+      (forward-line 1))
+    ;; Delete `.' at end of the buffer (end of text mark).
     (goto-char (point-max))
-    (forward-line -1)                  ;(beginning-of-line)
+    (forward-line -1)
     (if (looking-at "^\\.$")
        (delete-region (point) (progn (forward-line 1) (point))))
     ;; Replace `..' at beginning of line with `.'.
     (goto-char (point-min))
     ;; (replace-regexp "^\\.\\." ".")
     (while (search-forward "\n.." nil t)
-      (delete-char -1))
-    ))
+      (delete-char -1))))
 
 (defun nntp-encode-text ()
   "Encode text in current buffer for NNTP transmission.
@@ -480,8 +570,7 @@ in the current news group."
   (save-excursion
     ;; Insert newline at end of buffer.
     (goto-char (point-max))
-    (if (not (bolp))
-       (insert "\n"))
+    (or (bolp) (insert "\n"))
     ;; Replace `.' at beginning of line with `..'.
     (goto-char (point-min))
     ;; (replace-regexp "^\\." "..")
@@ -489,8 +578,7 @@ in the current news group."
       (insert "."))
     ;; Insert `.' at end of buffer (end of text mark).
     (goto-char (point-max))
-    (insert ".\r\n")
-    ))
+    (insert ".\r\n")))
 
 \f
 ;;;
@@ -506,8 +594,7 @@ in the current news group."
     (apply 'nntp-send-strings-to-server cmd args)
     (if response
        (nntp-wait-for-response response)
-      t)
-    ))
+      t)))
 
 (defun nntp-wait-for-response (regexp)
   "Wait for server response which matches REGEXP."
@@ -534,8 +621,7 @@ in the current news group."
              ((looking-at "[45]")
               (setq status nil)
               (setq wait nil))
-             (t (nntp-accept-response))
-             ))
+             (t (nntp-accept-response))))
       ;; Save status message.
       (end-of-line)
       (setq nntp-status-string
@@ -558,16 +644,12 @@ in the current news group."
                            (setq dotnum newnum)
                            (message "NNTP: Reading %s"
                                     (make-string dotnum ?.))))))
-               (nntp-accept-response)
-               ;;(if nntp-debug-read (message ""))
-               ))
+               (nntp-accept-response)))
            ;; Remove "...".
            (if (and nntp-debug-read (> dotnum 0))
                (message ""))
            ;; Successfully received server response.
-           t
-           ))
-      )))
+           t)))))
 
 \f
 ;;;
@@ -596,8 +678,7 @@ in the current news group."
                  (setq nntp-server-xover nil))
              (setcar (nthcdr 2 (assoc nntp-current-server nntp-server-alist))
                      nntp-server-xover)
-             nntp-server-xover)
-           t)
+             nntp-server-xover))
        (if nntp-server-xover (nntp-decode-text) (erase-buffer))))))
 
 (defun nntp-send-strings-to-server (&rest strings)
@@ -617,8 +698,7 @@ in the current news group."
          (save-excursion
            (set-buffer nntp-server-buffer)
            (erase-buffer))))
-    (process-send-string nntp-server-process (concat cmd "\r\n"))
-    ))
+    (process-send-string nntp-server-process (concat cmd "\r\n"))))
 
 (defun nntp-send-region-to-server (begin end)
   "Send current buffer region (from BEGIN to END) to news server."
@@ -648,11 +728,9 @@ in the current news group."
          ;;  problem of communication error of GNU Emacs.
          (accept-process-output)
          ;;(sit-for 0)
-         (goto-char last)
-         )))
+         (goto-char last))))
     ;; We cannot erase buffer, because reply may be received.
-    (delete-region begin end)
-    ))
+    (delete-region begin end)))
 
 (defun nntp-open-server-internal (server &optional service)
   "Open connection to news server on SERVER by SERVICE (default is nntp)."
@@ -662,29 +740,25 @@ in the current news group."
       (or (fboundp 'open-network-stream)
          (require 'tcp))
       ;; Initialize communication buffer.
-      (setq nntp-server-buffer (get-buffer-create " *nntpd*"))
+      (nnheader-init-server-buffer)
       (set-buffer nntp-server-buffer)
-      (buffer-disable-undo (current-buffer))
-      (erase-buffer)
-      (kill-all-local-variables)
-      (setq case-fold-search t)                ;Should ignore case.
       (if (setq proc
                (condition-case nil
-                   (open-network-stream "nntpd" (current-buffer)
-                                        server (or service "nntp"))
+                   (open-network-stream 
+                    "nntpd" (current-buffer) server (or service "nntp"))
                  (error nil)))
          (progn
            (setq nntp-server-process proc)
            ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
            (process-kill-without-query proc)
            (setq nntp-server-xover t)
+           (setq nntp-server-list-active-group t)
            (setq nntp-server-name server)
-           (setq nntp-server-alist (cons (list server nntp-server-process t)
+           (setq nntp-server-alist (cons (list server nntp-server-process t t)
                                          nntp-server-alist))
            ;; It is possible to change kanji-fileio-code in this hook.
            (run-hooks 'nntp-server-hook)
            nntp-server-process)))))
-      
 
 (defun nntp-close-server-internal (&optional server)
   "Close connection to news server."
@@ -692,24 +766,9 @@ in the current news group."
   (if nntp-server-process
       (delete-process nntp-server-process))
   (setq nntp-server-process nil)
-  (let* ((servers nntp-server-alist)
-        (prev servers))
-    (if (and servers (string= (car (car servers)) server))
-       (setq nntp-server-alist (cdr nntp-server-alist))
-      (setq servers (cdr servers))
-      (while servers
-       (if (string= (car (car servers)) server)
-           (setcdr prev (cdr servers)))
-       (setq prev servers)
-       (setq servers (cdr servers))))))
-
-(defun nntp-request-close ()
-  "Close all server connections."
-  (while nntp-server-alist
-    (delete-process (car (cdr (car nntp-server-alist))))
-    (setq nntp-server-alist (cdr nntp-server-alist)))
-  (setq nntp-current-server "")
-  (setq nntp-server-process nil))
+  (setq nntp-server-alist (delq (assoc nntp-current-server nntp-server-alist)
+                               nntp-server-alist))
+  (setq nntp-current-server ""))
 
 (defun nntp-accept-response ()
   "Read response of server.
@@ -719,26 +778,25 @@ defining this function as macro."
   ;;  accept-process-output is called.
   ;; Suggested by Jason Venner <jason@violet.berkeley.edu>.
   ;; This is a copy of `nntp-default-sentinel'.
-  (or (memq (process-status nntp-server-process) '(open run))
-      (error "NNTP: Connection closed."))
-  (if nntp-buggy-select
-      (progn
-       ;; We cannot use `accept-process-output'.
-       ;; Fujitsu UTS requires messages during sleep-for. I don't know why.
-       (message "NNTP: Reading...")
-       (sleep-for 1)
-       (message ""))
-    (condition-case errorcode
-       (accept-process-output nntp-server-process)
-      (error
-       (cond ((string-equal "select error: Invalid argument" (nth 1 errorcode))
-             ;; Ignore select error.
-             nil
-             )
-            (t
-             (signal (car errorcode) (cdr errorcode))
-       ))
-    ))))
+  (if (or (not nntp-server-process)
+         (not (memq (process-status nntp-server-process) '(open run))))
+      (error "nntp: Process connection closed; %s" (nntp-status-message))
+    (if nntp-buggy-select
+       (progn
+         ;; We cannot use `accept-process-output'.
+         ;; Fujitsu UTS requires messages during sleep-for. I don't know why.
+         (message "NNTP: Reading...")
+         (sleep-for 1)
+         (message ""))
+      (condition-case errorcode
+         (accept-process-output nntp-server-process)
+       (error
+        (cond ((string-equal "select error: Invalid argument" 
+                             (nth 1 errorcode))
+               ;; Ignore select error.
+               nil)
+              (t
+               (signal (car errorcode) (cdr errorcode)))))))))
 
 (defun nntp-last-element (list)
   "Return last element of LIST."
@@ -763,15 +821,17 @@ defining this function as macro."
                (setq nntp-server-name server)
                (setq nntp-server-process (nth 1 info))
                (setq nntp-server-xover (nth 2 info))
+               (setq nntp-server-list-active-group (nth 3 info))
                (setq changed-server t)
                (setq result t))))
       (setq result t))
     ;; The we see whether it is necessary to change newsgroup.
-    (if (and newsgroup result (or (not (string= newsgroup nntp-current-group))
-                                 changed-server))
-       (progn
-         (setq result (nntp-request-group newsgroup server))
-         (setq nntp-current-group newsgroup)))
+    (and newsgroup result 
+        (or (not (string= newsgroup nntp-current-group))
+            changed-server)
+        (progn
+          (setq result (nntp-request-group newsgroup server))
+          (setq nntp-current-group newsgroup)))
     result))
 
 (provide 'nntp)