* gnus-sum.el (gnus-summary-highlight): Add uncached to docstring.
[gnus] / lisp / nntp.el
1 ;;; nntp.el --- nntp access for Gnus
2
3 ;; Copyright (C) 1987, 1988, 1989, 1990, 1992, 1993, 1994, 1995, 1996,
4 ;; 1997, 1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published
13 ;; by the Free Software Foundation; either version 2, or (at your
14 ;; option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'nnheader)
30 (require 'nnoo)
31 (require 'gnus-util)
32
33 (nnoo-declare nntp)
34
35 (eval-when-compile (require 'cl))
36
37 (defvoo nntp-address nil
38   "Address of the physical nntp server.")
39
40 (defvoo nntp-port-number "nntp"
41   "Port number on the physical nntp server.")
42
43 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
44   "*Hook used for sending commands to the server at startup.
45 The default value is `nntp-send-mode-reader', which makes an innd
46 server spawn an nnrpd server.")
47
48 (defvoo nntp-authinfo-function 'nntp-send-authinfo
49   "Function used to send AUTHINFO to the server.
50 It is called with no parameters.")
51
52 (defvoo nntp-server-action-alist
53     '(("nntpd 1\\.5\\.11t"
54        (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
55       ("NNRP server Netscape"
56        (setq nntp-server-list-active-group nil)))
57   "Alist of regexps to match on server types and actions to be taken.
58 For instance, if you want Gnus to beep every time you connect
59 to innd, you could say something like:
60
61 \(setq nntp-server-action-alist
62        '((\"innd\" (ding))))
63
64 You probably don't want to do that, though.")
65
66 (defvoo nntp-open-connection-function 'nntp-open-network-stream
67   "*Function used for connecting to a remote system.
68 It will be called with the buffer to output in as argument.
69
70 Currently, five such functions are provided (please refer to their
71 respective doc string for more information), three of them establishing
72 direct connections to the nntp server, and two of them using an indirect
73 host.
74
75 Direct connections:
76 - `nntp-open-network-stream' (the default),
77 - `nntp-open-ssl-stream',
78 - `nntp-open-tls-stream',
79 - `nntp-open-telnet-stream'.
80
81 Indirect connections:
82 - `nntp-open-via-rlogin-and-telnet',
83 - `nntp-open-via-telnet-and-telnet'.")
84
85 (defvoo nntp-pre-command nil
86   "*Pre-command to use with the various nntp-open-via-* methods.
87 This is where you would put \"runsocks\" or stuff like that.")
88
89 (defvoo nntp-telnet-command "telnet"
90   "*Telnet command used to connect to the nntp server.
91 This command is used by the various nntp-open-via-* methods.")
92
93 (defvoo nntp-telnet-switches '("-8")
94   "*Switches given to the telnet command `nntp-telnet-command'.")
95
96 (defvoo nntp-end-of-line "\r\n"
97   "*String to use on the end of lines when talking to the NNTP server.
98 This is \"\\r\\n\" by default, but should be \"\\n\" when
99 using and indirect connection method (nntp-open-via-*).")
100
101 (defvoo nntp-via-rlogin-command "rsh"
102   "*Rlogin command used to connect to an intermediate host.
103 This command is used by the `nntp-open-via-rlogin-and-telnet' method.
104 The default is \"rsh\", but \"ssh\" is a popular alternative.")
105
106 (defvoo nntp-via-rlogin-command-switches nil
107   "*Switches given to the rlogin command `nntp-via-rlogin-command'.
108 If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
109 \(\"-C\") in order to compress all data connections, otherwise set this
110 to \(\"-t\" \"-e\" \"none\") or (\"-C\" \"-t\" \"-e\" \"none\") if the telnet
111 command requires a pseudo-tty allocation on an intermediate host.")
112
113 (defvoo nntp-via-telnet-command "telnet"
114   "*Telnet command used to connect to an intermediate host.
115 This command is used by the `nntp-open-via-telnet-and-telnet' method.")
116
117 (defvoo nntp-via-telnet-switches '("-8")
118   "*Switches given to the telnet command `nntp-via-telnet-command'.")
119
120 (defvoo nntp-via-user-name nil
121   "*User name to log in on an intermediate host with.
122 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
123
124 (defvoo nntp-via-user-password nil
125   "*Password to use to log in on an intermediate host with.
126 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
127
128 (defvoo nntp-via-address nil
129   "*Address of an intermediate host to connect to.
130 This variable is used by the `nntp-open-via-rlogin-and-telnet' and
131 `nntp-open-via-telnet-and-telnet' methods.")
132
133 (defvoo nntp-via-envuser nil
134   "*Whether both telnet client and server support the ENVIRON option.
135 If non-nil, there will be no prompt for a login name.")
136
137 (defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
138   "*Regular expression to match the shell prompt on an intermediate host.
139 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
140
141 (defvoo nntp-large-newsgroup 50
142   "*The number of articles which indicates a large newsgroup.
143 If the number of articles is greater than the value, verbose
144 messages will be shown to indicate the current status.")
145
146 (defvoo nntp-maximum-request 400
147   "*The maximum number of the requests sent to the NNTP server at one time.
148 If Emacs hangs up while retrieving headers, set the variable to a
149 lower value.")
150
151 (defvoo nntp-nov-is-evil nil
152   "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
153
154 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
155   "*List of strings that are used as commands to fetch NOV lines from a server.
156 The strings are tried in turn until a positive response is gotten.  If
157 none of the commands are successful, nntp will just grab headers one
158 by one.")
159
160 (defvoo nntp-nov-gap 5
161   "*Maximum allowed gap between two articles.
162 If the gap between two consecutive articles is bigger than this
163 variable, split the XOVER request into two requests.")
164
165 (defvoo nntp-prepare-server-hook nil
166   "*Hook run before a server is opened.
167 If can be used to set up a server remotely, for instance.  Say you
168 have an account at the machine \"other.machine\".  This machine has
169 access to an NNTP server that you can't access locally.  You could
170 then use this hook to rsh to the remote machine and start a proxy NNTP
171 server there that you can connect to.  See also
172 `nntp-open-connection-function'")
173
174 (defvoo nntp-warn-about-losing-connection t
175   "*If non-nil, beep when a server closes connection.")
176
177 (defvoo nntp-coding-system-for-read 'binary
178   "*Coding system to read from NNTP.")
179
180 (defvoo nntp-coding-system-for-write 'binary
181   "*Coding system to write to NNTP.")
182
183 (defcustom nntp-authinfo-file "~/.authinfo"
184   ".netrc-like file that holds nntp authinfo passwords."
185   :type
186   '(choice file
187            (repeat :tag "Entries"
188                    :menu-tag "Inline"
189                    (list :format "%v"
190                          :value ("" ("login" . "") ("password" . ""))
191                          (string :tag "Host")
192                          (checklist :inline t
193                                     (cons :format "%v"
194                                           (const :format "" "login")
195                                           (string :format "Login: %v"))
196                                     (cons :format "%v"
197                                           (const :format "" "password")
198                                           (string :format "Password: %v")))))))
199
200 \f
201
202 (defvoo nntp-connection-timeout nil
203   "*Number of seconds to wait before an nntp connection times out.
204 If this variable is nil, which is the default, no timers are set.
205 NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
206
207 (defvoo nntp-prepare-post-hook nil
208   "*Hook run just before posting an article.  It is supposed to be used
209 to insert Cancel-Lock headers.")
210
211 ;;; Internal variables.
212
213 (defvar nntp-record-commands nil
214   "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
215
216 (defvar nntp-have-messaged nil)
217
218 (defvar nntp-process-wait-for nil)
219 (defvar nntp-process-to-buffer nil)
220 (defvar nntp-process-callback nil)
221 (defvar nntp-process-decode nil)
222 (defvar nntp-process-start-point nil)
223 (defvar nntp-inside-change-function nil)
224 (defvoo nntp-last-command-time nil)
225 (defvoo nntp-last-command nil)
226 (defvoo nntp-authinfo-password nil)
227 (defvoo nntp-authinfo-user nil)
228
229 (defvar nntp-connection-list nil)
230
231 (defvoo nntp-server-type nil)
232 (defvoo nntp-connection-alist nil)
233 (defvoo nntp-status-string "")
234 (defconst nntp-version "nntp 5.0")
235 (defvoo nntp-inhibit-erase nil)
236 (defvoo nntp-inhibit-output nil)
237
238 (defvoo nntp-server-xover 'try)
239 (defvoo nntp-server-list-active-group 'try)
240
241 (defvar nntp-async-needs-kluge
242   (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
243   "*When non-nil, nntp will poll asynchronous connections
244 once a second.  By default, this is turned on only for Emacs
245 20.3, which has a bug that breaks nntp's normal method of
246 noticing asynchronous data.")
247
248 (defvar nntp-async-timer nil)
249 (defvar nntp-async-process-list nil)
250
251 (defvar nntp-ssl-program 
252   "openssl s_client -quiet -ssl3 -connect %s:%p"
253 "A string containing commands for SSL connections.
254 Within a string, %s is replaced with the server address and %p with
255 port number on server.  The program should accept IMAP commands on
256 stdin and return responses to stdout.")
257
258 \f
259
260 ;;; Internal functions.
261
262 (defsubst nntp-send-string (process string)
263   "Send STRING to PROCESS."
264   ;; We need to store the time to provide timeouts, and
265   ;; to store the command so the we can replay the command
266   ;; if the server gives us an AUTHINFO challenge.
267   (setq nntp-last-command-time (current-time)
268         nntp-last-command string)
269   (when nntp-record-commands
270     (nntp-record-command string))
271   (process-send-string process (concat string nntp-end-of-line))
272   (or (memq (process-status process) '(open run))
273       (nntp-report "Server closed connection")))
274
275 (defun nntp-record-command (string)
276   "Record the command STRING."
277   (save-excursion
278     (set-buffer (get-buffer-create "*nntp-log*"))
279     (goto-char (point-max))
280     (let ((time (current-time)))
281       (insert (format-time-string "%Y%m%dT%H%M%S" time)
282               "." (format "%03d" (/ (nth 2 time) 1000))
283               " " nntp-address " " string "\n"))))
284
285 (defun nntp-report (&rest args)
286   "Report an error from the nntp backend.  The first string in ARGS
287 can be a format string.  For some commands, the failed command may be
288 retried once before actually displaying the error report."
289
290   (when nntp-record-commands
291     (nntp-record-command "*** CALLED nntp-report ***"))
292
293   (nnheader-report 'nntp args)
294
295   (apply 'error args))
296
297 (defun nntp-report-1 (&rest args)
298   "Throws out to nntp-with-open-group-error so that the connection may
299 be restored and the command retried."
300
301   (when nntp-record-commands
302     (nntp-record-command "*** CONNECTION LOST ***"))
303
304   (throw 'nntp-with-open-group-error t))
305
306 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
307   "Wait for WAIT-FOR to arrive from PROCESS."
308   (save-excursion
309     (set-buffer (process-buffer process))
310     (goto-char (point-min))
311     (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
312                     (looking-at "480"))
313                 (memq (process-status process) '(open run)))
314       (when (looking-at "480")
315         (nntp-handle-authinfo process))
316       (when (looking-at "^.*\n")
317         (delete-region (point) (progn (forward-line 1) (point))))
318       (nntp-accept-process-output process)
319       (goto-char (point-min)))
320     (prog1
321         (cond
322          ((looking-at "[45]")
323           (progn
324             (nntp-snarf-error-message)
325             nil))
326          ((not (memq (process-status process) '(open run)))
327           (nntp-report "Server closed connection"))
328          (t
329           (goto-char (point-max))
330           (let ((limit (point-min))
331                 response)
332             (while (not (re-search-backward wait-for limit t))
333               (nntp-accept-process-output process)
334               ;; We assume that whatever we wait for is less than 1000
335               ;; characters long.
336               (setq limit (max (- (point-max) 1000) (point-min)))
337               (goto-char (point-max)))
338             (setq response (match-string 0))
339             (with-current-buffer nntp-server-buffer
340               (setq nntp-process-response response)))
341           (nntp-decode-text (not decode))
342           (unless discard
343             (save-excursion
344               (set-buffer buffer)
345               (goto-char (point-max))
346               (insert-buffer-substring (process-buffer process))
347               ;; Nix out "nntp reading...." message.
348               (when nntp-have-messaged
349                 (setq nntp-have-messaged nil)
350                 (nnheader-message 5 ""))))
351           t))
352       (unless discard
353         (erase-buffer)))))
354
355 (defun nntp-kill-buffer (buffer)
356   (when (buffer-name buffer)
357     (kill-buffer buffer)
358     (nnheader-init-server-buffer)))
359
360 (defsubst nntp-find-connection (buffer)
361   "Find the connection delivering to BUFFER."
362   (let ((alist nntp-connection-alist)
363         (buffer (if (stringp buffer) (get-buffer buffer) buffer))
364         process entry)
365     (while (and alist (setq entry (pop alist)))
366       (when (eq buffer (cadr entry))
367         (setq process (car entry)
368               alist nil)))
369     (when process
370       (if (memq (process-status process) '(open run))
371           process
372         (nntp-kill-buffer (process-buffer process))
373         (setq nntp-connection-alist (delq entry nntp-connection-alist))
374         nil))))
375
376 (defsubst nntp-find-connection-entry (buffer)
377   "Return the entry for the connection to BUFFER."
378   (assq (nntp-find-connection buffer) nntp-connection-alist))
379
380 (defun nntp-find-connection-buffer (buffer)
381   "Return the process connection buffer tied to BUFFER."
382   (let ((process (nntp-find-connection buffer)))
383     (when process
384       (process-buffer process))))
385
386 (defsubst nntp-retrieve-data (command address port buffer
387                                       &optional wait-for callback decode)
388   "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
389   (let ((process (or (nntp-find-connection buffer)
390                      (nntp-open-connection buffer))))
391     (if process
392         (progn
393           (unless (or nntp-inhibit-erase nnheader-callback-function)
394             (save-excursion
395               (set-buffer (process-buffer process))
396               (erase-buffer)))
397           (condition-case err
398               (progn
399                 (when command
400                   (nntp-send-string process command))
401                 (cond
402                  ((eq callback 'ignore)
403                   t)
404                  ((and callback wait-for)
405                   (nntp-async-wait process wait-for buffer decode callback)
406                   t)
407                  (wait-for
408                   (nntp-wait-for process wait-for buffer decode))
409                  (t t)))
410             (error
411              (nnheader-report 'nntp "Couldn't open connection to %s: %s"
412                               address err))
413             (quit
414              (message "Quit retrieving data from nntp")
415              (signal 'quit nil)
416              nil)))
417       (nnheader-report 'nntp "Couldn't open connection to %s" address))))
418
419 (defsubst nntp-send-command (wait-for &rest strings)
420   "Send STRINGS to server and wait until WAIT-FOR returns."
421   (when (and (not nnheader-callback-function)
422              (not nntp-inhibit-output))
423     (save-excursion
424       (set-buffer nntp-server-buffer)
425       (erase-buffer)))
426   (let* ((command (mapconcat 'identity strings " "))
427          (process (nntp-find-connection nntp-server-buffer))
428          (buffer (and process (process-buffer process)))
429          (pos (and buffer (with-current-buffer buffer (point)))))
430     (if process
431         (prog1
432             (nntp-retrieve-data command
433                                 nntp-address nntp-port-number
434                                 nntp-server-buffer
435                                 wait-for nnheader-callback-function)
436           ;; If nothing to wait for, still remove possibly echo'ed commands.
437           ;; We don't have echos if nntp-open-connection-function
438           ;; is `nntp-open-network-stream', so we skip this in that case.
439           (unless (or wait-for
440                       (equal nntp-open-connection-function
441                              'nntp-open-network-stream))
442             (nntp-accept-response)
443             (save-excursion
444               (set-buffer buffer)
445               (goto-char pos)
446               (if (looking-at (regexp-quote command))
447                   (delete-region pos (progn (forward-line 1)
448                                             (gnus-point-at-bol))))
449               )))
450       (nnheader-report 'nntp "Couldn't open connection to %s."
451                        nntp-address))))
452
453 (defun nntp-send-command-nodelete (wait-for &rest strings)
454   "Send STRINGS to server and wait until WAIT-FOR returns."
455   (let* ((command (mapconcat 'identity strings " "))
456          (process (nntp-find-connection nntp-server-buffer))
457          (buffer (and process (process-buffer process)))
458          (pos (and buffer (with-current-buffer buffer (point)))))
459     (if process
460         (prog1
461             (nntp-retrieve-data command
462                                 nntp-address nntp-port-number
463                                 nntp-server-buffer
464                                 wait-for nnheader-callback-function)
465           ;; If nothing to wait for, still remove possibly echo'ed commands
466           (unless wait-for
467             (nntp-accept-response)
468             (save-excursion
469               (set-buffer buffer)
470               (goto-char pos)
471               (if (looking-at (regexp-quote command))
472                   (delete-region pos (progn (forward-line 1)
473                                             (gnus-point-at-bol))))
474               )))
475       (nnheader-report 'nntp "Couldn't open connection to %s."
476                        nntp-address))))
477
478 (defun nntp-send-command-and-decode (wait-for &rest strings)
479   "Send STRINGS to server and wait until WAIT-FOR returns."
480   (when (and (not nnheader-callback-function)
481              (not nntp-inhibit-output))
482     (save-excursion
483       (set-buffer nntp-server-buffer)
484       (erase-buffer)))
485   (let* ((command (mapconcat 'identity strings " "))
486          (process (nntp-find-connection nntp-server-buffer))
487          (buffer (and process (process-buffer process)))
488          (pos (and buffer (with-current-buffer buffer (point)))))
489     (if process
490         (prog1
491             (nntp-retrieve-data command
492                                 nntp-address nntp-port-number
493                                 nntp-server-buffer
494                                 wait-for nnheader-callback-function t)
495           ;; If nothing to wait for, still remove possibly echo'ed commands
496           (unless wait-for
497             (nntp-accept-response)
498             (save-excursion
499           (set-buffer buffer)
500           (goto-char pos)
501           (if (looking-at (regexp-quote command))
502               (delete-region pos (progn (forward-line 1) (gnus-point-at-bol))))
503           )))
504       (nnheader-report 'nntp "Couldn't open connection to %s."
505                        nntp-address))))
506
507
508 (defun nntp-send-buffer (wait-for)
509   "Send the current buffer to server and wait until WAIT-FOR returns."
510   (when (and (not nnheader-callback-function)
511              (not nntp-inhibit-output))
512     (save-excursion
513       (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
514       (erase-buffer)))
515   (nntp-encode-text)
516   (mm-with-unibyte-current-buffer
517     ;; Some encoded unicode text contains character 0x80-0x9f e.g. Euro.
518     (process-send-region (nntp-find-connection nntp-server-buffer)
519                          (point-min) (point-max)))
520   (nntp-retrieve-data
521    nil nntp-address nntp-port-number nntp-server-buffer
522    wait-for nnheader-callback-function))
523
524 \f
525
526 ;;; Interface functions.
527
528 (nnoo-define-basics nntp)
529
530 (defsubst nntp-next-result-arrived-p ()
531   (cond
532    ;; A result that starts with a 2xx code is terminated by
533    ;; a line with only a "." on it.
534    ((eq (char-after) ?2)
535     (if (re-search-forward "\n\\.\r?\n" nil t)
536         t
537       nil))
538    ;; A result that starts with a 3xx or 4xx code is terminated
539    ;; by a newline.
540    ((looking-at "[34]")
541     (if (search-forward "\n" nil t)
542         t
543       nil))
544    ;; No result here.
545    (t
546     nil)))
547
548 (eval-when-compile
549   (defvar nntp-with-open-group-internal nil)
550   (defvar nntp-report-n nil))
551
552 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
553   "Protect against servers that don't like clients that keep idle connections opens.
554 The problem being that these servers may either close a connection or
555 simply ignore any further requests on a connection.  Closed
556 connections are not detected until accept-process-output has updated
557 the process-status.  Dropped connections are not detected until the
558 connection timeouts (which may be several minutes) or
559 nntp-connection-timeout has expired.  When these occur
560 nntp-with-open-group, opens a new connection then re-issues the NNTP
561 command whose response triggered the error."
562   (when (and (listp connectionless)
563              (not (eq connectionless nil)))
564     (setq forms (cons connectionless forms)
565           connectionless nil))
566   `(letf ((nntp-report-n (symbol-function 'nntp-report))
567           ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1))
568           (nntp-with-open-group-internal nil))
569      (while (catch 'nntp-with-open-group-error
570               ;; Open the connection to the server
571               ;; NOTE: Existing connections are NOT tested.
572               (nntp-possibly-change-group ,group ,server ,connectionless)
573
574               (let ((timer
575                      (and nntp-connection-timeout
576                           (nnheader-run-at-time
577                            nntp-connection-timeout nil
578                            '(lambda ()
579                               (let ((process (nntp-find-connection
580                                               nntp-server-buffer))
581                                     (buffer  (and process
582                                                   (process-buffer process))))
583                                 ;; When I an able to identify the
584                                 ;; connection to the server AND I've
585                                 ;; received NO reponse for
586                                 ;; nntp-connection-timeout seconds.
587                                 (when (and buffer (eq 0 (buffer-size buffer)))
588                                   ;; Close the connection.  Take no
589                                   ;; other action as the accept input
590                                   ;; code will handle the closed
591                                   ;; connection.
592                                   (nntp-kill-buffer buffer))))))))
593                 (unwind-protect
594                     (setq nntp-with-open-group-internal
595                           (condition-case nil
596                               (progn ,@forms)
597                             (quit
598                              (nntp-close-server)
599                              (signal 'quit nil))))
600                   (when timer
601                     (nnheader-cancel-timer timer)))
602                 nil))
603        (setf (symbol-function 'nntp-report) nntp-report-n))
604      nntp-with-open-group-internal))
605
606 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
607   "Retrieve the headers of ARTICLES."
608   (nntp-with-open-group
609    group server
610    (save-excursion
611      (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
612      (erase-buffer)
613      (if (and (not gnus-nov-is-evil)
614               (not nntp-nov-is-evil)
615               (nntp-retrieve-headers-with-xover articles fetch-old))
616          ;; We successfully retrieved the headers via XOVER.
617          'nov
618        ;; XOVER didn't work, so we do it the hard, slow and inefficient
619        ;; way.
620        (let ((number (length articles))
621              (articles articles)
622              (count 0)
623              (received 0)
624              (last-point (point-min))
625              (buf (nntp-find-connection-buffer nntp-server-buffer))
626              (nntp-inhibit-erase t)
627              article)
628          ;; Send HEAD commands.
629          (while (setq article (pop articles))
630            (nntp-send-command
631             nil
632             "HEAD" (if (numberp article)
633                        (int-to-string article)
634                      ;; `articles' is either a list of article numbers
635                      ;; or a list of article IDs.
636                      article))
637            (incf count)
638            ;; Every 400 requests we have to read the stream in
639            ;; order to avoid deadlocks.
640            (when (or (null articles)    ;All requests have been sent.
641                      (zerop (% count nntp-maximum-request)))
642              (nntp-accept-response)
643              (while (progn
644                       (set-buffer buf)
645                       (goto-char last-point)
646                       ;; Count replies.
647                       (while (nntp-next-result-arrived-p)
648                         (setq last-point (point))
649                         (incf received))
650                       (< received count))
651                ;; If number of headers is greater than 100, give
652                ;;  informative messages.
653                (and (numberp nntp-large-newsgroup)
654                     (> number nntp-large-newsgroup)
655                     (zerop (% received 20))
656                     (nnheader-message 6 "NNTP: Receiving headers... %d%%"
657                                       (/ (* received 100) number)))
658                (nntp-accept-response))))
659          (and (numberp nntp-large-newsgroup)
660               (> number nntp-large-newsgroup)
661               (nnheader-message 6 "NNTP: Receiving headers...done"))
662
663          ;; Now all of replies are received.  Fold continuation lines.
664          (nnheader-fold-continuation-lines)
665          ;; Remove all "\r"'s.
666          (nnheader-strip-cr)
667          (copy-to-buffer nntp-server-buffer (point-min) (point-max))
668          'headers)))))
669
670 (deffoo nntp-retrieve-groups (groups &optional server)
671   "Retrieve group info on GROUPS."
672   (nntp-with-open-group
673    nil server
674    (when (nntp-find-connection-buffer nntp-server-buffer)
675      (catch 'done
676        (save-excursion
677          ;; Erase nntp-server-buffer before nntp-inhibit-erase.
678          (set-buffer nntp-server-buffer)
679          (erase-buffer)
680          (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
681          ;; The first time this is run, this variable is `try'.  So we
682          ;; try.
683          (when (eq nntp-server-list-active-group 'try)
684            (nntp-try-list-active (car groups)))
685          (erase-buffer)
686          (let ((count 0)
687                (groups groups)
688                (received 0)
689                (last-point (point-min))
690                (nntp-inhibit-erase t)
691                (buf (nntp-find-connection-buffer nntp-server-buffer))
692                (command (if nntp-server-list-active-group
693                             "LIST ACTIVE" "GROUP")))
694            (while groups
695              ;; Timeout may have killed the buffer.
696              (unless (gnus-buffer-live-p buf)
697                (nnheader-report 'nntp "Connection to %s is closed." server)
698                (throw 'done nil))
699              ;; Send the command to the server.
700              (nntp-send-command nil command (pop groups))
701              (incf count)
702              ;; Every 400 requests we have to read the stream in
703              ;; order to avoid deadlocks.
704              (when (or (null groups)    ;All requests have been sent.
705                        (zerop (% count nntp-maximum-request)))
706                (nntp-accept-response)
707                (while (and (gnus-buffer-live-p buf)
708                            (progn
709                              ;; Search `blue moon' in this file for the
710                              ;; reason why set-buffer here.
711                              (set-buffer buf)
712                              (goto-char last-point)
713                              ;; Count replies.
714                              (while (re-search-forward "^[0-9]" nil t)
715                                (incf received))
716                              (setq last-point (point))
717                              (< received count)))
718                  (nntp-accept-response))))
719
720            ;; Wait for the reply from the final command.
721            (unless (gnus-buffer-live-p buf)
722              (nnheader-report 'nntp "Connection to %s is closed." server)
723              (throw 'done nil))
724            (set-buffer buf)
725            (goto-char (point-max))
726            (re-search-backward "^[0-9]" nil t)
727            (when (looking-at "^[23]")
728              (while (and (gnus-buffer-live-p buf)
729                          (progn
730                            (set-buffer buf)
731                            (goto-char (point-max))
732                            (if (not nntp-server-list-active-group)
733                                (not (re-search-backward "\r?\n"
734                                                         (- (point) 3) t))
735                              (not (re-search-backward "^\\.\r?\n"
736                                                       (- (point) 4) t)))))
737                (nntp-accept-response)))
738
739            ;; Now all replies are received.  We remove CRs.
740            (unless (gnus-buffer-live-p buf)
741              (nnheader-report 'nntp "Connection to %s is closed." server)
742              (throw 'done nil))
743            (set-buffer buf)
744            (goto-char (point-min))
745            (while (search-forward "\r" nil t)
746              (replace-match "" t t))
747
748            (if (not nntp-server-list-active-group)
749                (progn
750                  (copy-to-buffer nntp-server-buffer (point-min) (point-max))
751                  'group)
752              ;; We have read active entries, so we just delete the
753              ;; superfluous gunk.
754              (goto-char (point-min))
755              (while (re-search-forward "^[.2-5]" nil t)
756                (delete-region (match-beginning 0)
757                               (progn (forward-line 1) (point))))
758              (copy-to-buffer nntp-server-buffer (point-min) (point-max))
759              'active)))))))
760
761 (deffoo nntp-retrieve-articles (articles &optional group server)
762   (nntp-with-open-group
763     group server
764    (save-excursion
765      (let ((number (length articles))
766            (articles articles)
767            (count 0)
768            (received 0)
769            (last-point (point-min))
770            (buf (nntp-find-connection-buffer nntp-server-buffer))
771            (nntp-inhibit-erase t)
772            (map (apply 'vector articles))
773            (point 1)
774            article)
775        (set-buffer buf)
776        (erase-buffer)
777        ;; Send ARTICLE command.
778        (while (setq article (pop articles))
779          (nntp-send-command
780           nil
781           "ARTICLE" (if (numberp article)
782                         (int-to-string article)
783                       ;; `articles' is either a list of article numbers
784                       ;; or a list of article IDs.
785                       article))
786          (incf count)
787          ;; Every 400 requests we have to read the stream in
788          ;; order to avoid deadlocks.
789          (when (or (null articles)      ;All requests have been sent.
790                    (zerop (% count nntp-maximum-request)))
791            (nntp-accept-response)
792            (while (progn
793                     (set-buffer buf)
794                     (goto-char last-point)
795                     ;; Count replies.
796                     (while (nntp-next-result-arrived-p)
797                       (aset map received (cons (aref map received) (point)))
798                       (setq last-point (point))
799                       (incf received))
800                     (< received count))
801              ;; If number of headers is greater than 100, give
802              ;;  informative messages.
803              (and (numberp nntp-large-newsgroup)
804                   (> number nntp-large-newsgroup)
805                   (zerop (% received 20))
806                   (nnheader-message 6 "NNTP: Receiving articles... %d%%"
807                                     (/ (* received 100) number)))
808              (nntp-accept-response))))
809        (and (numberp nntp-large-newsgroup)
810             (> number nntp-large-newsgroup)
811             (nnheader-message 6 "NNTP: Receiving articles...done"))
812
813        ;; Now we have all the responses.  We go through the results,
814        ;; wash it and copy it over to the server buffer.
815        (set-buffer nntp-server-buffer)
816        (erase-buffer)
817        (setq last-point (point-min))
818        (mapcar
819         (lambda (entry)
820           (narrow-to-region
821            (setq point (goto-char (point-max)))
822            (progn
823              (insert-buffer-substring buf last-point (cdr entry))
824              (point-max)))
825           (setq last-point (cdr entry))
826           (nntp-decode-text)
827           (widen)
828           (cons (car entry) point))
829         map)))))
830
831 (defun nntp-try-list-active (group)
832   (nntp-list-active-group group)
833   (save-excursion
834     (set-buffer nntp-server-buffer)
835     (goto-char (point-min))
836     (cond ((or (eobp)
837                (looking-at "5[0-9]+"))
838            (setq nntp-server-list-active-group nil))
839           (t
840            (setq nntp-server-list-active-group t)))))
841
842 (deffoo nntp-list-active-group (group &optional server)
843   "Return the active info on GROUP (which can be a regexp)."
844   (nntp-with-open-group
845    nil server
846    (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
847
848 (deffoo nntp-request-group-articles (group &optional server)
849   "Return the list of existing articles in GROUP."
850   (nntp-with-open-group
851    nil server
852    (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
853
854 (deffoo nntp-request-article (article &optional group server buffer command)
855   (nntp-with-open-group
856     group server
857     (when (nntp-send-command-and-decode
858            "\r?\n\\.\r?\n" "ARTICLE"
859            (if (numberp article) (int-to-string article) article))
860       (if (and buffer
861                (not (equal buffer nntp-server-buffer)))
862           (save-excursion
863             (set-buffer nntp-server-buffer)
864             (copy-to-buffer buffer (point-min) (point-max))
865             (nntp-find-group-and-number group))
866         (nntp-find-group-and-number group)))))
867
868 (deffoo nntp-request-head (article &optional group server)
869   (nntp-with-open-group
870    group server
871    (when (nntp-send-command
872           "\r?\n\\.\r?\n" "HEAD"
873           (if (numberp article) (int-to-string article) article))
874      (prog1
875          (nntp-find-group-and-number group)
876        (nntp-decode-text)))))
877
878 (deffoo nntp-request-body (article &optional group server)
879   (nntp-with-open-group
880    group server
881    (nntp-send-command-and-decode
882     "\r?\n\\.\r?\n" "BODY"
883     (if (numberp article) (int-to-string article) article))))
884
885 (deffoo nntp-request-group (group &optional server dont-check)
886   (nntp-with-open-group 
887     nil server
888     (when (nntp-send-command "^[245].*\n" "GROUP" group)
889       (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
890         (setcar (cddr entry) group)))))
891
892 (deffoo nntp-close-group (group &optional server)
893   t)
894
895 (deffoo nntp-server-opened (&optional server)
896   "Say whether a connection to SERVER has been opened."
897   (and (nnoo-current-server-p 'nntp server)
898        nntp-server-buffer
899        (gnus-buffer-live-p nntp-server-buffer)
900        (nntp-find-connection nntp-server-buffer)))
901
902 (deffoo nntp-open-server (server &optional defs connectionless)
903   (nnheader-init-server-buffer)
904   (if (nntp-server-opened server)
905       t
906     (when (or (stringp (car defs))
907               (numberp (car defs)))
908       (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
909     (unless (assq 'nntp-address defs)
910       (setq defs (append defs (list (list 'nntp-address server)))))
911     (nnoo-change-server 'nntp server defs)
912     (unless connectionless
913       (or (nntp-find-connection nntp-server-buffer)
914           (nntp-open-connection nntp-server-buffer)))))
915
916 (deffoo nntp-close-server (&optional server)
917   (nntp-possibly-change-group nil server t)
918   (let ((process (nntp-find-connection nntp-server-buffer)))
919     (while process
920       (when (memq (process-status process) '(open run))
921         (ignore-errors
922           (nntp-send-string process "QUIT")
923           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
924             ;; Ok, this is evil, but when using telnet and stuff
925             ;; as the connection method, it's important that the
926             ;; QUIT command actually is sent out before we kill
927             ;; the process.
928             (sleep-for 1))))
929       (nntp-kill-buffer (process-buffer process))
930       (setq process (car (pop nntp-connection-alist))))
931     (nnoo-close-server 'nntp)))
932
933 (deffoo nntp-request-close ()
934   (let (process)
935     (while (setq process (pop nntp-connection-list))
936       (when (memq (process-status process) '(open run))
937         (ignore-errors
938           (nntp-send-string process "QUIT")
939           (unle