* nntp.el (nntp-accept-process-output): Use new function.
[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\") or (\"-C\" \"-t\") if the telnet command requires a pseudo-tty
111 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 the articles which indicates a large newsgroup.
143 If the number of the 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           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
940             ;; Ok, this is evil, but when using telnet and stuff
941             ;; as the connection method, it's important that the
942             ;; QUIT command actually is sent out before we kill
943             ;; the process.
944             (sleep-for 1))))
945       (nntp-kill-buffer (process-buffer process)))))
946
947 (deffoo nntp-request-list (&optional server)
948   (nntp-with-open-group
949    nil server
950    (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
951
952 (deffoo nntp-request-list-newsgroups (&optional server)
953   (nntp-with-open-group
954    nil server
955    (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
956
957 (deffoo nntp-request-newgroups (date &optional server)
958   (nntp-with-open-group
959    nil server
960    (save-excursion
961      (set-buffer nntp-server-buffer)
962      (let* ((time (date-to-time date))
963             (ls (- (cadr time) (nth 8 (decode-time time)))))
964        (cond ((< ls 0)
965               (setcar time (1- (car time)))
966               (setcar (cdr time) (+ ls 65536)))
967              ((>= ls 65536)
968               (setcar time (1+ (car time)))
969               (setcar (cdr time) (- ls 65536)))
970              (t
971               (setcar (cdr time) ls)))
972        (prog1
973            (nntp-send-command
974             "^\\.\r?\n" "NEWGROUPS"
975             (format-time-string "%y%m%d %H%M%S" time)
976             "GMT")
977          (nntp-decode-text))))))
978
979 (deffoo nntp-request-post (&optional server)
980   (nntp-with-open-group
981    nil server
982    (when (nntp-send-command "^[23].*\r?\n" "POST")
983      (let ((response (with-current-buffer nntp-server-buffer
984                        nntp-process-response))
985            server-id)
986        (when (and response
987                   (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
988                                 response))
989          (setq server-id (match-string 1 response))
990          (narrow-to-region (goto-char (point-min))
991                            (if (search-forward "\n\n" nil t)
992                                (1- (point))
993                              (point-max)))
994          (unless (mail-fetch-field "Message-ID")
995            (goto-char (point-min))
996            (insert "Message-ID: " server-id "\n"))
997          (widen))
998        (run-hooks 'nntp-prepare-post-hook)
999        (nntp-send-buffer "^[23].*\n")))))
1000
1001 (deffoo nntp-request-type (group article)
1002   'news)
1003
1004 (deffoo nntp-asynchronous-p ()
1005   t)
1006
1007 ;;; Hooky functions.
1008
1009 (defun nntp-send-mode-reader ()
1010   "Send the MODE READER command to the nntp server.
1011 This function is supposed to be called from `nntp-server-opened-hook'.
1012 It will make innd servers spawn an nnrpd process to allow actual article
1013 reading."
1014   (nntp-send-command "^.*\n" "MODE READER"))
1015
1016 (defun nntp-send-authinfo (&optional send-if-force)
1017   "Send the AUTHINFO to the nntp server.
1018 It will look in the \"~/.authinfo\" file for matching entries.  If
1019 nothing suitable is found there, it will prompt for a user name
1020 and a password.
1021
1022 If SEND-IF-FORCE, only send authinfo to the server if the
1023 .authinfo file has the FORCE token."
1024   (let* ((list (gnus-parse-netrc nntp-authinfo-file))
1025          (alist (gnus-netrc-machine list nntp-address "nntp"))
1026          (force (gnus-netrc-get alist "force"))
1027          (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
1028          (passwd (gnus-netrc-get alist "password")))
1029     (when (or (not send-if-force)
1030               force)
1031       (unless user
1032         (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1033               nntp-authinfo-user user))
1034       (unless (member user '(nil ""))
1035         (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1036         (when t                         ;???Should check if AUTHINFO succeeded
1037           (nntp-send-command
1038            "^2.*\r?\n" "AUTHINFO PASS"
1039            (or passwd
1040                nntp-authinfo-password
1041                (setq nntp-authinfo-password
1042                      (read-passwd (format "NNTP (%s@%s) password: "
1043                                           user nntp-address))))))))))
1044
1045 (defun nntp-send-nosy-authinfo ()
1046   "Send the AUTHINFO to the nntp server."
1047   (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
1048     (unless (member user '(nil ""))
1049       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1050       (when t                           ;???Should check if AUTHINFO succeeded
1051         (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
1052                            (read-passwd (format "NNTP (%s@%s) password: "
1053                                                 user nntp-address)))))))
1054
1055 (defun nntp-send-authinfo-from-file ()
1056   "Send the AUTHINFO to the nntp server.
1057
1058 The authinfo login name is taken from the user's login name and the
1059 password contained in '~/.nntp-authinfo'."
1060   (when (file-exists-p "~/.nntp-authinfo")
1061     (with-temp-buffer
1062       (insert-file-contents "~/.nntp-authinfo")
1063       (goto-char (point-min))
1064       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
1065       (nntp-send-command
1066        "^2.*\r?\n" "AUTHINFO PASS"
1067        (buffer-substring (point) (gnus-point-at-eol))))))
1068
1069 ;;; Internal functions.
1070
1071 (defun nntp-handle-authinfo (process)
1072   "Take care of an authinfo response from the server."
1073   (let ((last nntp-last-command))
1074     (funcall nntp-authinfo-function)
1075     ;; We have to re-send the function that was interrupted by
1076     ;; the authinfo request.
1077     (save-excursion
1078       (set-buffer nntp-server-buffer)
1079       (erase-buffer))
1080     (nntp-send-string process last)))
1081
1082 (defun nntp-make-process-buffer (buffer)
1083   "Create a new, fresh buffer usable for nntp process connections."
1084   (save-excursion
1085     (set-buffer
1086      (generate-new-buffer
1087       (format " *server %s %s %s*"
1088               nntp-address nntp-port-number
1089               (gnus-buffer-exists-p buffer))))
1090     (mm-enable-multibyte)
1091     (set (make-local-variable 'after-change-functions) nil)
1092     (set (make-local-variable 'nntp-process-wait-for) nil)
1093     (set (make-local-variable 'nntp-process-callback) nil)
1094     (set (make-local-variable 'nntp-process-to-buffer) nil)
1095     (set (make-local-variable 'nntp-process-start-point) nil)
1096     (set (make-local-variable 'nntp-process-decode) nil)
1097     (current-buffer)))
1098
1099 (defun nntp-open-connection (buffer)
1100   "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1101   (run-hooks 'nntp-prepare-server-hook)
1102   (let* ((pbuffer (nntp-make-process-buffer buffer))
1103          (timer
1104           (and nntp-connection-timeout
1105                (nnheader-run-at-time
1106                 nntp-connection-timeout nil
1107                 `(lambda ()
1108                    (nntp-kill-buffer ,pbuffer)))))
1109          (process
1110           (condition-case ()
1111               (let ((coding-system-for-read nntp-coding-system-for-read)
1112                     (coding-system-for-write nntp-coding-system-for-write))
1113                 (funcall nntp-open-connection-function pbuffer))
1114             (error nil)
1115             (quit
1116              (message "Quit opening connection")
1117              (nntp-kill-buffer pbuffer)
1118              (signal 'quit nil)
1119              nil))))
1120     (when timer
1121       (nnheader-cancel-timer timer))
1122     (unless process
1123       (nntp-kill-buffer pbuffer))
1124     (when (and (buffer-name pbuffer)
1125                process)
1126       (process-kill-without-query process)
1127       (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1128                (memq (process-status process) '(open run)))
1129           (prog1
1130               (caar (push (list process buffer nil) nntp-connection-alist))
1131             (push process nntp-connection-list)
1132             (save-excursion
1133               (set-buffer pbuffer)
1134               (nntp-read-server-type)
1135               (erase-buffer)
1136               (set-buffer nntp-server-buffer)
1137               (let ((nnheader-callback-function nil))
1138                 (run-hooks 'nntp-server-opened-hook)
1139                 (nntp-send-authinfo t))))
1140         (nntp-kill-buffer (process-buffer process))
1141         nil))))
1142
1143 (defun nntp-open-network-stream (buffer)
1144   (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
1145
1146 (defun nntp-open-ssl-stream (buffer)
1147   (let* ((process-connection-type nil)
1148          (proc (start-process "nntpd" buffer 
1149                               shell-file-name
1150                               shell-command-switch
1151                               (format-spec nntp-ssl-program 
1152                                            (format-spec-make
1153                                             ?s nntp-address
1154                                             ?p nntp-port-number)))))
1155     (process-kill-without-query proc)
1156     (save-excursion
1157       (set-buffer buffer)
1158       (let ((nntp-connection-alist (list proc buffer nil)))
1159         (nntp-wait-for-string "^\r*20[01]"))
1160       (beginning-of-line)
1161       (delete-region (point-min) (point))
1162       proc)))
1163
1164 (defun nntp-open-tls-stream (buffer)
1165   (let ((proc (open-tls-stream "nntpd" buffer nntp-address nntp-port-number)))
1166     (process-kill-without-query proc)
1167     (save-excursion
1168       (set-buffer buffer)
1169       (let ((nntp-connection-alist (list proc buffer nil)))
1170         (nntp-wait-for-string "^\r*20[01]"))
1171       (beginning-of-line)
1172       (delete-region (point-min) (point))
1173       proc)))
1174
1175 (defun nntp-read-server-type ()
1176   "Find out what the name of the server we have connected to is."
1177   ;; Wait for the status string to arrive.
1178   (setq nntp-server-type (buffer-string))
1179   (let ((alist nntp-server-action-alist)
1180         (case-fold-search t)
1181         entry)
1182     ;; Run server-specific commands.
1183     (while alist
1184       (setq entry (pop alist))
1185       (when (string-match (car entry) nntp-server-type)
1186         (if (and (listp (cadr entry))
1187                  (not (eq 'lambda (caadr entry))))
1188             (eval (cadr entry))
1189           (funcall (cadr entry)))))))
1190
1191 (defun nntp-async-wait (process wait-for buffer decode callback)
1192   (save-excursion
1193     (set-buffer (process-buffer process))
1194     (unless nntp-inside-change-function
1195       (erase-buffer))
1196     (setq nntp-process-wait-for wait-for
1197           nntp-process-to-buffer buffer
1198           nntp-process-decode decode
1199           nntp-process-callback callback
1200           nntp-process-start-point (point-max))
1201     (setq after-change-functions '(nntp-after-change-function))
1202     (if nntp-async-needs-kluge
1203         (nntp-async-kluge process))))
1204
1205 (defun nntp-async-kluge (process)
1206   ;; emacs 20.3 bug: process output with encoding 'binary
1207   ;; doesn't trigger after-change-functions.
1208   (unless nntp-async-timer
1209     (setq nntp-async-timer
1210           (nnheader-run-at-time 1 1 'nntp-async-timer-handler)))
1211   (add-to-list 'nntp-async-process-list process))
1212
1213 (defun nntp-async-timer-handler ()
1214   (mapcar
1215    (lambda (proc)
1216      (if (memq (process-status proc) '(open run))
1217          (nntp-async-trigger proc)
1218        (nntp-async-stop proc)))
1219    nntp-async-process-list))
1220
1221 (defun nntp-async-stop (proc)
1222   (setq nntp-async-process-list (delq proc nntp-async-process-list))
1223   (when (and nntp-async-timer (not nntp-async-process-list))
1224     (nnheader-cancel-timer nntp-async-timer)
1225     (setq nntp-async-timer nil)))
1226
1227 (defun nntp-after-change-function (beg end len)
1228   (unwind-protect
1229       ;; we only care about insertions at eob
1230       (when (and (eq 0 len) (eq (point-max) end))
1231         (save-match-data
1232           (let ((proc (get-buffer-process (current-buffer))))
1233             (when proc
1234               (nntp-async-trigger proc)))))
1235     ;; any throw from after-change-functions will leave it
1236     ;; set to nil.  so we reset it here, if necessary.
1237     (when quit-flag
1238       (setq after-change-functions '(nntp-after-change-function)))))
1239
1240 (defun nntp-async-trigger (process)
1241   (save-excursion
1242     (set-buffer (process-buffer process))
1243     (when nntp-process-callback
1244       ;; do we have an error message?
1245       (goto-char nntp-process-start-point)
1246       (if (memq (following-char) '(?4 ?5))
1247           ;; wants credentials?
1248           (if (looking-at "480")
1249               (nntp-handle-authinfo process)
1250             ;; report error message.
1251             (nntp-snarf-error-message)
1252             (nntp-do-callback nil))
1253
1254         ;; got what we expect?
1255         (goto-char (point-max))
1256         (when (re-search-backward
1257                nntp-process-wait-for nntp-process-start-point t)
1258           (let ((response (match-string 0)))
1259             (with-current-buffer nntp-server-buffer
1260               (setq nntp-process-response response)))
1261           (nntp-async-stop process)
1262           ;; convert it.
1263           (when (gnus-buffer-exists-p nntp-process-to-buffer)
1264             (let ((buf (current-buffer))
1265                   (start nntp-process-start-point)
1266                   (decode nntp-process-decode))
1267               (save-excursion
1268                 (set-buffer nntp-process-to-buffer)
1269                 (goto-char (point-max))
1270                 (save-restriction
1271                   (narrow-to-region (point) (point))
1272                   (insert-buffer-substring buf start)
1273                   (when decode
1274                     (nntp-decode-text))))))
1275           ;; report it.
1276           (goto-char (point-max))
1277           (nntp-do-callback
1278            (buffer-name (get-buffer nntp-process-to-buffer))))))))
1279
1280 (defun nntp-do-callback (arg)
1281   (let ((callback nntp-process-callback)
1282         (nntp-inside-change-function t))
1283     (setq nntp-process-callback nil)
1284     (funcall callback arg)))
1285
1286 (defun nntp-snarf-error-message ()
1287   "Save the error message in the current buffer."
1288   (let ((message (buffer-string)))
1289     (while (string-match "[\r\n]+" message)
1290       (setq message (replace-match " " t t message)))
1291     (nnheader-report 'nntp message)
1292     message))
1293
1294 (defun nntp-accept-process-output (process)
1295   "Wait for output from PROCESS and message some dots."
1296   (save-excursion
1297     (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1298                     nntp-server-buffer))
1299     (let ((len (/ (buffer-size) 1024))
1300           message-log-max)
1301       (unless (< len 10)
1302         (setq nntp-have-messaged t)
1303         (nnheader-message 7 "nntp read: %dk" len)))
1304     (nnheader-accept-process-output process)
1305     ;; accept-process-output may update status of process to indicate
1306     ;; that the server has closed the connection.  This MUST be
1307     ;; handled here as the buffer restored by the save-excursion may
1308     ;; be the process's former output buffer (i.e. now killed)
1309     (or (and process 
1310              (memq (process-status process) '(open run)))
1311         (nntp-report "Server closed connection"))))
1312
1313 (defun nntp-accept-response ()
1314   "Wait for output from the process that outputs to BUFFER."
1315   (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1316
1317 (defun nntp-possibly-change-group (group server &optional connectionless)
1318   (let ((nnheader-callback-function nil))
1319     (when server
1320       (or (nntp-server-opened server)
1321           (nntp-open-server server nil connectionless)))
1322
1323     (unless connectionless
1324       (or (nntp-find-connection nntp-server-buffer)
1325           (nntp-open-connection nntp-server-buffer))))
1326
1327   (when group
1328     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1329       (cond ((not entry)
1330              (nntp-report "Server closed connection"))
1331             ((not (equal group (caddr entry)))
1332              (save-excursion
1333                (set-buffer (process-buffer (car entry)))
1334                (erase-buffer)
1335                (nntp-send-command "^[245].*\n" "GROUP" group)
1336                (setcar (cddr entry) group)
1337                (erase-buffer)
1338                (save-excursion
1339                  (set-buffer nntp-server-buffer)
1340                  (erase-buffer))))))))
1341
1342 (defun nntp-decode-text (&optional cr-only)
1343   "Decode the text in the current buffer."
1344   (goto-char (point-min))
1345   (while (search-forward "\r" nil t)
1346     (delete-char -1))
1347   (unless cr-only
1348     ;; Remove trailing ".\n" end-of-transfer marker.
1349     (goto-char (point-max))
1350     (forward-line -1)
1351     (when (looking-at ".\n")
1352       (delete-char 2))
1353     ;; Delete status line.
1354     (goto-char (point-min))
1355     (while (looking-at "[1-5][0-9][0-9] .*\n")
1356       ;; For some unknown reason, there is more than one status line.
1357       (delete-region (point) (progn (forward-line 1) (point))))
1358     ;; Remove "." -> ".." encoding.
1359     (while (search-forward "\n.." nil t)
1360       (delete-char -1))))
1361
1362 (defun nntp-encode-text ()
1363   "Encode the text in the current buffer."
1364   (save-excursion
1365     ;; Replace "." at beginning of line with "..".
1366     (goto-char (point-min))
1367     (while (re-search-forward "^\\." nil t)
1368       (insert "."))
1369     (goto-char (point-max))
1370     ;; Insert newline at the end of the buffer.
1371     (unless (bolp)
1372       (insert "\n"))
1373     ;; Insert `.' at end of buffer (end of text mark).
1374     (goto-char (point-max))
1375     (insert ".\n")
1376     (goto-char (point-min))
1377     (while (not (eobp))
1378       (end-of-line)
1379       (delete-char 1)
1380       (insert nntp-end-of-line))))
1381
1382 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1383   (set-buffer nntp-server-buffer)
1384   (erase-buffer)
1385   (cond
1386
1387    ;; This server does not talk NOV.
1388    ((not nntp-server-xover)
1389     nil)
1390
1391    ;; We don't care about gaps.
1392    ((or (not nntp-nov-gap)
1393         fetch-old)
1394     (nntp-send-xover-command
1395      (if fetch-old
1396          (if (numberp fetch-old)
1397              (max 1 (- (car articles) fetch-old))
1398            1)
1399        (car articles))
1400      (car (last articles)) 'wait)
1401
1402     (goto-char (point-min))
1403     (when (looking-at "[1-5][0-9][0-9] .*\n")
1404       (delete-region (point) (progn (forward-line 1) (point))))
1405     (while (search-forward "\r" nil t)
1406       (replace-match "" t t))
1407     (goto-char (point-max))
1408     (forward-line -1)
1409     (when (looking-at "\\.")
1410       (delete-region (point) (progn (forward-line 1) (point)))))
1411
1412    ;; We do it the hard way.  For each gap, an XOVER command is sent
1413    ;; to the server.  We do not wait for a reply from the server, we
1414    ;; just send them off as fast as we can.  That means that we have
1415    ;; to count the number of responses we get back to find out when we
1416    ;; have gotten all we asked for.
1417    ((numberp nntp-nov-gap)
1418     (let ((count 0)
1419           (received 0)
1420           last-point
1421           in-process-buffer-p
1422           (buf nntp-server-buffer)
1423           (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1424           first
1425           last)
1426       ;; We have to check `nntp-server-xover'.  If it gets set to nil,
1427       ;; that means that the server does not understand XOVER, but we
1428       ;; won't know that until we try.
1429       (while (and nntp-server-xover articles)
1430         (setq first (car articles))
1431         ;; Search forward until we find a gap, or until we run out of
1432         ;; articles.
1433         (while (and (cdr articles)
1434                     (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1435           (setq articles (cdr articles)))
1436
1437         (setq in-process-buffer-p (stringp nntp-server-xover))
1438         (nntp-send-xover-command first (setq last (car articles)))
1439         (setq articles (cdr articles))
1440
1441         (when (and nntp-server-xover in-process-buffer-p)
1442           ;; Don't count tried request.
1443           (setq count (1+ count))
1444
1445           ;; Every 400 requests we have to read the stream in
1446           ;; order to avoid deadlocks.
1447           (when (or (null articles)     ;All requests have been sent.
1448                     (= 1 (% count nntp-maximum-request)))
1449
1450             (nntp-accept-response)
1451             ;; On some Emacs versions the preceding function has a
1452             ;; tendency to change the buffer.  Perhaps.  It's quite
1453             ;; difficult to reproduce, because it only seems to happen
1454             ;; once in a blue moon.
1455             (set-buffer process-buffer)
1456             (while (progn
1457                      (goto-char (or last-point (point-min)))
1458                      ;; Count replies.
1459                      (while (re-search-forward "^[0-9][0-9][0-9] .*\n" nil t)
1460                        (incf received))
1461                      (setq last-point (point))
1462                      (or (< received count)
1463                          ;; I haven't started reading the final response
1464                          (progn
1465                            (goto-char (point-max))
1466                            (forward-line -1)
1467                            (not (looking-at "^\\.\r?\n")))))
1468               ;; I haven't read the end of the final response
1469               (nntp-accept-response)
1470               (set-buffer process-buffer))))
1471
1472         ;; Some nntp servers seem to have an extension to the XOVER
1473         ;; extension.  On these servers, requesting an article range
1474         ;; preceeding the active range does not return an error as
1475         ;; specified in the RFC.  What we instead get is the NOV entry
1476         ;; for the first available article.  Obviously, a client can
1477         ;; use that entry to avoid making unnecessary requests.  The
1478         ;; only problem is for a client that assumes that the response
1479         ;; will always be within the requested ranage.  For such a
1480         ;; client, we can get N copies of the same entry (one for each
1481         ;; XOVER command sent to the server).
1482
1483         (when (<= count 1)
1484           (goto-char (point-min))
1485           (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1486             (let ((low-limit (string-to-int
1487                               (buffer-substring (match-beginning 1) 
1488                                                 (match-end 1)))))
1489               (while (and articles (<= (car articles) low-limit))
1490                 (setq articles (cdr articles))))))
1491         (set-buffer buf))
1492
1493       (when nntp-server-xover
1494         (when in-process-buffer-p
1495           (set-buffer buf)
1496           (goto-char (point-max))
1497           (insert-buffer-substring process-buffer)
1498           (set-buffer process-buffer)
1499           (erase-buffer)
1500           (set-buffer buf))
1501
1502         ;; We remove any "." lines and status lines.
1503         (goto-char (point-min))
1504         (while (search-forward "\r" nil t)
1505           (delete-char -1))
1506         (goto-char (point-min))
1507         (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1508         t))))
1509
1510   nntp-server-xover)
1511
1512 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1513   "Send the XOVER command to the server."
1514   (let ((range (format "%d-%d" beg end))
1515         (nntp-inhibit-erase t))
1516     (if (stringp nntp-server-xover)
1517         ;; If `nntp-server-xover' is a string, then we just send this
1518         ;; command.
1519         (if wait-for-reply
1520             (nntp-send-command-nodelete
1521              "\r?\n\\.\r?\n" nntp-server-xover range)
1522           ;; We do not wait for the reply.
1523           (nntp-send-command-nodelete nil nntp-server-xover range))
1524       (let ((commands nntp-xover-commands))
1525         ;; `nntp-xover-commands' is a list of possible XOVER commands.
1526         ;; We try them all until we get at positive response.
1527         (while (and commands (eq nntp-server-xover 'try))
1528           (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1529           (save-excursion
1530             (set-buffer nntp-server-buffer)
1531             (goto-char (point-min))
1532             (and (looking-at "[23]")    ; No error message.
1533                  ;; We also have to look at the lines.  Some buggy
1534                  ;; servers give back simple lines with just the
1535                  ;; article number.  How... helpful.
1536                  (progn
1537                    (forward-line 1)
1538                    (looking-at "[0-9]+\t...")) ; More text after number.
1539                  (setq nntp-server-xover (car commands))))
1540           (setq commands (cdr commands)))
1541         ;; If none of the commands worked, we disable XOVER.
1542         (when (eq nntp-server-xover 'try)
1543           (save-excursion
1544             (set-buffer nntp-server-buffer)
1545             (erase-buffer)
1546             (setq nntp-server-xover nil)))
1547         nntp-server-xover))))
1548
1549 (defun nntp-find-group-and-number (&optional group)
1550   (save-excursion
1551     (save-restriction
1552       (set-buffer nntp-server-buffer)
1553       (narrow-to-region (goto-char (point-min))
1554                         (or (search-forward "\n\n" nil t) (point-max)))
1555       (goto-char (point-min))
1556       ;; We first find the number by looking at the status line.
1557       (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1558                          (string-to-int
1559                           (buffer-substring (match-beginning 1)
1560                                             (match-end 1)))))
1561             newsgroups xref)
1562         (and number (zerop number) (setq number nil))
1563         (if number
1564             ;; Then we find the group name.
1565             (setq group
1566                   (cond
1567                    ;; If there is only one group in the Newsgroups
1568                    ;; header, then it seems quite likely that this
1569                    ;; article comes from that group, I'd say.
1570                    ((and (setq newsgroups
1571                                (mail-fetch-field "newsgroups"))
1572                          (not (string-match "," newsgroups)))
1573                     newsgroups)
1574                    ;; If there is more than one group in the
1575                    ;; Newsgroups header, then the Xref header should
1576                    ;; be filled out.  We hazard a guess that the group
1577                    ;; that has this article number in the Xref header
1578                    ;; is the one we are looking for.  This might very
1579                    ;; well be wrong if this article happens to have
1580                    ;; the same number in several groups, but that's
1581                    ;; life.
1582                    ((and (setq xref (mail-fetch-field "xref"))
1583                          number
1584                          (string-match
1585                           (format "\\([^ :]+\\):%d" number) xref))
1586                     (match-string 1 xref))
1587                    (t "")))
1588           (cond
1589            ((and (setq xref (mail-fetch-field "xref"))
1590                  (string-match
1591                   (if group
1592                       (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1593                     "\\([^ :]+\\):\\([0-9]+\\)")
1594                   xref))
1595             (setq group (match-string 1 xref)
1596                   number (string-to-int (match-string 2 xref))))
1597            ((and (setq newsgroups
1598                        (mail-fetch-field "newsgroups"))
1599                  (not (string-match "," newsgroups)))
1600             (setq group newsgroups))
1601            (group)
1602            (t (setq group ""))))
1603         (when (string-match "\r" group)
1604           (setq group (substring group 0 (match-beginning 0))))
1605         (cons group number)))))
1606
1607 (defun nntp-wait-for-string (regexp)
1608   "Wait until string arrives in the buffer."
1609   (let ((buf (current-buffer))
1610         proc)
1611     (goto-char (point-min))
1612     (while (and (setq proc (get-buffer-process buf))
1613                 (memq (process-status proc) '(open run))
1614                 (not (re-search-forward regexp nil t)))
1615       (accept-process-output proc)
1616       (set-buffer buf)
1617       (goto-char (point-min)))))
1618
1619
1620 ;; ==========================================================================
1621 ;; Obsolete nntp-open-* connection methods -- drv
1622 ;; ==========================================================================
1623
1624 (defvoo nntp-open-telnet-envuser nil
1625   "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1626
1627 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
1628   "*Regular expression to match the shell prompt on the remote machine.")
1629
1630 (defvoo nntp-rlogin-program "rsh"
1631   "*Program used to log in on remote machines.
1632 The default is \"rsh\", but \"ssh\" is a popular alternative.")
1633
1634 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1635   "*Parameters to `nntp-open-rlogin'.
1636 That function may be used as `nntp-open-connection-function'.  In that
1637 case, this list will be used as the parameter list given to rsh.")
1638
1639 (defvoo nntp-rlogin-user-name nil
1640   "*User name on remote system when using the rlogin connect method.")
1641
1642 (defvoo nntp-telnet-parameters
1643     '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1644   "*Parameters to `nntp-open-telnet'.
1645 That function may be used as `nntp-open-connection-function'.  In that
1646 case, this list will be executed as a command after logging in
1647 via telnet.")
1648
1649 (defvoo nntp-telnet-user-name nil
1650   "User name to log in via telnet with.")
1651
1652 (defvoo nntp-telnet-passwd nil
1653   "Password to use to log in via telnet with.")
1654
1655 (defun nntp-open-telnet (buffer)
1656   (save-excursion
1657     (set-buffer buffer)
1658     (erase-buffer)
1659     (let ((proc (apply
1660                  'start-process
1661                  "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1662           (case-fold-search t))
1663       (when (memq (process-status proc) '(open run))
1664         (nntp-wait-for-string "^r?telnet")
1665         (process-send-string proc "set escape \^X\n")
1666         (cond
1667          ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1668           (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1669                                             nntp-address "\n")))
1670          (t
1671           (process-send-string proc (concat "open " nntp-address "\n"))))
1672         (cond
1673          ((not nntp-open-telnet-envuser)
1674           (nntp-wait-for-string "^\r*.?login:")
1675           (process-send-string
1676            proc (concat
1677                  (or nntp-telnet-user-name
1678                      (setq nntp-telnet-user-name (read-string "login: ")))
1679                  "\n"))))
1680         (nntp-wait-for-string "^\r*.?password:")
1681         (process-send-string
1682          proc (concat
1683                (or nntp-telnet-passwd
1684                    (setq nntp-telnet-passwd
1685                          (read-passwd "Password: ")))
1686                "\n"))
1687         (nntp-wait-for-string nntp-telnet-shell-prompt)
1688         (process-send-string
1689          proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1690         (nntp-wait-for-string "^\r*20[01]")
1691         (beginning-of-line)
1692         (delete-region (point-min) (point))
1693         (process-send-string proc "\^]")
1694         (nntp-wait-for-string "^r?telnet")
1695         (process-send-string proc "mode character\n")
1696         (accept-process-output proc 1)
1697         (sit-for 1)
1698         (goto-char (point-min))
1699         (forward-line 1)
1700         (delete-region (point) (point-max)))
1701       proc)))
1702
1703 (defun nntp-open-rlogin (buffer)
1704   "Open a connection to SERVER using rsh."
1705   (let ((proc (if nntp-rlogin-user-name
1706                   (apply 'start-process
1707                          "nntpd" buffer nntp-rlogin-program
1708                          nntp-address "-l" nntp-rlogin-user-name
1709                          nntp-rlogin-parameters)
1710                 (apply 'start-process
1711                        "nntpd" buffer nntp-rlogin-program nntp-address
1712                        nntp-rlogin-parameters))))
1713     (save-excursion
1714       (set-buffer buffer)
1715       (nntp-wait-for-string "^\r*20[01]")
1716       (beginning-of-line)
1717       (delete-region (point-min) (point))
1718       proc)))
1719
1720
1721 ;; ==========================================================================
1722 ;; Replacements for the nntp-open-* functions -- drv
1723 ;; ==========================================================================
1724
1725 (defun nntp-open-telnet-stream (buffer)
1726   "Open a nntp connection by telnet'ing the news server.
1727
1728 Please refer to the following variables to customize the connection:
1729 - `nntp-pre-command',
1730 - `nntp-telnet-command',
1731 - `nntp-telnet-switches',
1732 - `nntp-address',
1733 - `nntp-port-number',
1734 - `nntp-end-of-line'."
1735   (let ((command `(,nntp-telnet-command
1736                    ,@nntp-telnet-switches
1737                    ,nntp-address ,nntp-port-number))
1738         proc)
1739     (and nntp-pre-command
1740          (push nntp-pre-command command))
1741     (setq proc (apply 'start-process "nntpd" buffer command))
1742     (save-excursion
1743       (set-buffer buffer)
1744       (nntp-wait-for-string "^\r*20[01]")
1745       (beginning-of-line)
1746       (delete-region (point-min) (point))
1747       proc)))
1748
1749 (defun nntp-open-via-rlogin-and-telnet (buffer)
1750   "Open a connection to an nntp server through an intermediate host.
1751 First rlogin to the remote host, and then telnet the real news server
1752 from there.
1753
1754 Please refer to the following variables to customize the connection:
1755 - `nntp-pre-command',
1756 - `nntp-via-rlogin-command',
1757 - `nntp-via-rlogin-command-switches',
1758 - `nntp-via-user-name',
1759 - `nntp-via-address',
1760 - `nntp-telnet-command',
1761 - `nntp-telnet-switches',
1762 - `nntp-address',
1763 - `nntp-port-number',
1764 - `nntp-end-of-line'."
1765   (let ((command `(,nntp-via-address
1766                    ,nntp-telnet-command
1767                    ,@nntp-telnet-switches))
1768         proc)
1769     (when nntp-via-user-name
1770       (setq command `("-l" ,nntp-via-user-name ,@command)))
1771     (when nntp-via-rlogin-command-switches
1772       (setq command (append nntp-via-rlogin-command-switches command)))
1773     (push nntp-via-rlogin-command command)
1774     (and nntp-pre-command
1775          (push nntp-pre-command command))
1776     (setq proc (apply 'start-process "nntpd" buffer command))
1777     (save-excursion
1778       (set-buffer buffer)
1779       (nntp-wait-for-string "^r?telnet")
1780       (process-send-string proc (concat "open " nntp-address
1781                                         " " nntp-port-number "\n"))
1782       (nntp-wait-for-string "^\r*20[01]")
1783       (beginning-of-line)
1784       (delete-region (point-min) (point))
1785       proc)))
1786
1787 (defun nntp-open-via-telnet-and-telnet (buffer)
1788   "Open a connection to an nntp server through an intermediate host.
1789 First telnet the remote host, and then telnet the real news server
1790 from there.
1791
1792 Please refer to the following variables to customize the connection:
1793 - `nntp-pre-command',
1794 - `nntp-via-telnet-command',
1795 - `nntp-via-telnet-switches',
1796 - `nntp-via-address',
1797 - `nntp-via-envuser',
1798 - `nntp-via-user-name',
1799 - `nntp-via-user-password',
1800 - `nntp-via-shell-prompt',
1801 - `nntp-telnet-command',
1802 - `nntp-telnet-switches',
1803 - `nntp-address',
1804 - `nntp-port-number',
1805 - `nntp-end-of-line'."
1806   (save-excursion
1807     (set-buffer buffer)
1808     (erase-buffer)
1809     (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
1810           (case-fold-search t)
1811           proc)
1812       (and nntp-pre-command (push nntp-pre-command command))
1813       (setq proc (apply 'start-process "nntpd" buffer command))
1814       (when (memq (process-status proc) '(open run))
1815         (nntp-wait-for-string "^r?telnet")
1816         (process-send-string proc "set escape \^X\n")
1817         (cond
1818          ((and nntp-via-envuser nntp-via-user-name)
1819           (process-send-string proc (concat "open " "-l" nntp-via-user-name
1820                                             nntp-via-address "\n")))
1821          (t
1822           (process-send-string proc (concat "open " nntp-via-address
1823                                             "\n"))))
1824         (when (not nntp-via-envuser)
1825           (nntp-wait-for-string "^\r*.?login:")
1826           (process-send-string proc
1827                                (concat
1828                                 (or nntp-via-user-name
1829                                     (setq nntp-via-user-name
1830                                           (read-string "login: ")))
1831                                 "\n")))
1832         (nntp-wait-for-string "^\r*.?password:")
1833         (process-send-string proc
1834                              (concat
1835                               (or nntp-via-user-password
1836                                   (setq nntp-via-user-password
1837                                         (read-passwd "Password: ")))
1838                               "\n"))
1839         (nntp-wait-for-string nntp-via-shell-prompt)
1840         (let ((real-telnet-command `("exec"
1841                                      ,nntp-telnet-command
1842                                      ,@nntp-telnet-switches
1843                                      ,nntp-address
1844                                      ,nntp-port-number)))
1845           (process-send-string proc
1846                                (concat (mapconcat 'identity
1847                                                   real-telnet-command " ")
1848                                        "\n")))
1849         (nntp-wait-for-string "^\r*20[01]")
1850         (beginning-of-line)
1851         (delete-region (point-min) (point))
1852         (process-send-string proc "\^]")
1853         (nntp-wait-for-string "^r?telnet")
1854         (process-send-string proc "mode character\n")
1855         (accept-process-output proc 1)
1856         (sit-for 1)
1857         (goto-char (point-min))
1858         (forward-line 1)
1859         (delete-region (point) (point-max)))
1860       proc)))
1861
1862 (provide 'nntp)
1863
1864 ;;; nntp.el ends here