8ff4d2d4b5d13f2c2b1b829d873685d9502766e3
[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 (defun nntp-erase-buffer (buffer)
361   "Erase contents of BUFFER."
362   (with-current-buffer buffer
363     (erase-buffer)))
364
365 (defsubst nntp-find-connection (buffer)
366   "Find the connection delivering to BUFFER."
367   (let ((alist nntp-connection-alist)
368         (buffer (if (stringp buffer) (get-buffer buffer) buffer))
369         process entry)
370     (while (and alist (setq entry (pop alist)))
371       (when (eq buffer (cadr entry))
372         (setq process (car entry)
373               alist nil)))
374     (when process
375       (if (memq (process-status process) '(open run))
376           process
377         (nntp-kill-buffer (process-buffer process))
378         (setq nntp-connection-alist (delq entry nntp-connection-alist))
379         nil))))
380
381 (defsubst nntp-find-connection-entry (buffer)
382   "Return the entry for the connection to BUFFER."
383   (assq (nntp-find-connection buffer) nntp-connection-alist))
384
385 (defun nntp-find-connection-buffer (buffer)
386   "Return the process connection buffer tied to BUFFER."
387   (let ((process (nntp-find-connection buffer)))
388     (when process
389       (process-buffer process))))
390
391 (defsubst nntp-retrieve-data (command address port buffer
392                                       &optional wait-for callback decode)
393   "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
394   (let ((process (or (nntp-find-connection buffer)
395                      (nntp-open-connection buffer))))
396     (if process
397         (progn
398           (unless (or nntp-inhibit-erase nnheader-callback-function)
399             (nntp-erase-buffer (process-buffer process)))
400           (condition-case err
401               (progn
402                 (when command
403                   (nntp-send-string process command))
404                 (cond
405                  ((eq callback 'ignore)
406                   t)
407                  ((and callback wait-for)
408                   (nntp-async-wait process wait-for buffer decode callback)
409                   t)
410                  (wait-for
411                   (nntp-wait-for process wait-for buffer decode))
412                  (t t)))
413             (error
414              (nnheader-report 'nntp "Couldn't open connection to %s: %s"
415                               address err))
416             (quit
417              (message "Quit retrieving data from nntp")
418              (signal 'quit nil)
419              nil)))
420       (nnheader-report 'nntp "Couldn't open connection to %s" address))))
421
422 (defsubst nntp-send-command (wait-for &rest strings)
423   "Send STRINGS to server and wait until WAIT-FOR returns."
424   (when (and (not nnheader-callback-function)
425              (not nntp-inhibit-output))
426     (nntp-erase-buffer nntp-server-buffer))
427   (let* ((command (mapconcat 'identity strings " "))
428          (process (nntp-find-connection nntp-server-buffer))
429          (buffer (and process (process-buffer process)))
430          (pos (and buffer (with-current-buffer buffer (point)))))
431     (if process
432         (prog1
433             (nntp-retrieve-data command
434                                 nntp-address nntp-port-number
435                                 nntp-server-buffer
436                                 wait-for nnheader-callback-function)
437           ;; If nothing to wait for, still remove possibly echo'ed commands.
438           ;; We don't have echos if nntp-open-connection-function
439           ;; is `nntp-open-network-stream', so we skip this in that case.
440           (unless (or wait-for
441                       (equal nntp-open-connection-function
442                              'nntp-open-network-stream))
443             (nntp-accept-response)
444             (save-excursion
445               (set-buffer buffer)
446               (goto-char pos)
447               (if (looking-at (regexp-quote command))
448                   (delete-region pos (progn (forward-line 1)
449                                             (point-at-bol))))
450               )))
451       (nnheader-report 'nntp "Couldn't open connection to %s."
452                        nntp-address))))
453
454 (defun nntp-send-command-nodelete (wait-for &rest strings)
455   "Send STRINGS to server and wait until WAIT-FOR returns."
456   (let* ((command (mapconcat 'identity strings " "))
457          (process (nntp-find-connection nntp-server-buffer))
458          (buffer (and process (process-buffer process)))
459          (pos (and buffer (with-current-buffer buffer (point)))))
460     (if process
461         (prog1
462             (nntp-retrieve-data command
463                                 nntp-address nntp-port-number
464                                 nntp-server-buffer
465                                 wait-for nnheader-callback-function)
466           ;; If nothing to wait for, still remove possibly echo'ed commands
467           (unless wait-for
468             (nntp-accept-response)
469             (save-excursion
470               (set-buffer buffer)
471               (goto-char pos)
472               (if (looking-at (regexp-quote command))
473                   (delete-region pos (progn (forward-line 1)
474                                             (point-at-bol)))))))
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     (nntp-erase-buffer nntp-server-buffer))
483   (let* ((command (mapconcat 'identity strings " "))
484          (process (nntp-find-connection nntp-server-buffer))
485          (buffer (and process (process-buffer process)))
486          (pos (and buffer (with-current-buffer buffer (point)))))
487     (if process
488         (prog1
489             (nntp-retrieve-data command
490                                 nntp-address nntp-port-number
491                                 nntp-server-buffer
492                                 wait-for nnheader-callback-function t)
493           ;; If nothing to wait for, still remove possibly echo'ed commands
494           (unless wait-for
495             (nntp-accept-response)
496             (save-excursion
497               (set-buffer buffer)
498               (goto-char pos)
499               (if (looking-at (regexp-quote command))
500                   (delete-region pos (progn (forward-line 1) (point-at-bol))))
501               )))
502       (nnheader-report 'nntp "Couldn't open connection to %s."
503                        nntp-address))))
504
505
506 (defun nntp-send-buffer (wait-for)
507   "Send the current buffer to server and wait until WAIT-FOR returns."
508   (when (and (not nnheader-callback-function)
509              (not nntp-inhibit-output))
510     (nntp-erase-buffer
511      (nntp-find-connection-buffer nntp-server-buffer)))
512   (nntp-encode-text)
513   (mm-with-unibyte-current-buffer
514     ;; Some encoded unicode text contains character 0x80-0x9f e.g. Euro.
515     (process-send-region (nntp-find-connection nntp-server-buffer)
516                          (point-min) (point-max)))
517   (nntp-retrieve-data
518    nil nntp-address nntp-port-number nntp-server-buffer
519    wait-for nnheader-callback-function))
520
521 \f
522
523 ;;; Interface functions.
524
525 (nnoo-define-basics nntp)
526
527 (defsubst nntp-next-result-arrived-p ()
528   (cond
529    ;; A result that starts with a 2xx code is terminated by
530    ;; a line with only a "." on it.
531    ((eq (char-after) ?2)
532     (if (re-search-forward "\n\\.\r?\n" nil t)
533         t
534       nil))
535    ;; A result that starts with a 3xx or 4xx code is terminated
536    ;; by a newline.
537    ((looking-at "[34]")
538     (if (search-forward "\n" nil t)
539         t
540       nil))
541    ;; No result here.
542    (t
543     nil)))
544
545 (eval-when-compile
546   (defvar nntp-with-open-group-internal nil)
547   (defvar nntp-report-n nil))
548
549 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
550   "Protect against servers that don't like clients that keep idle connections opens.
551 The problem being that these servers may either close a connection or
552 simply ignore any further requests on a connection.  Closed
553 connections are not detected until accept-process-output has updated
554 the process-status.  Dropped connections are not detected until the
555 connection timeouts (which may be several minutes) or
556 nntp-connection-timeout has expired.  When these occur
557 nntp-with-open-group, opens a new connection then re-issues the NNTP
558 command whose response triggered the error."
559   (when (and (listp connectionless)
560              (not (eq connectionless nil)))
561     (setq forms (cons connectionless forms)
562           connectionless nil))
563   `(letf ((nntp-report-n (symbol-function 'nntp-report))
564           ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1))
565           (nntp-with-open-group-internal nil))
566      (while (catch 'nntp-with-open-group-error
567               ;; Open the connection to the server
568               ;; NOTE: Existing connections are NOT tested.
569               (nntp-possibly-change-group ,group ,server ,connectionless)
570
571               (let ((timer
572                      (and nntp-connection-timeout
573                           (run-at-time
574                            nntp-connection-timeout nil
575                            '(lambda ()
576                               (let ((process (nntp-find-connection
577                                               nntp-server-buffer))
578                                     (buffer  (and process
579                                                   (process-buffer process))))
580                                 ;; When I an able to identify the
581                                 ;; connection to the server AND I've
582                                 ;; received NO reponse for
583                                 ;; nntp-connection-timeout seconds.
584                                 (when (and buffer (eq 0 (buffer-size buffer)))
585                                   ;; Close the connection.  Take no
586                                   ;; other action as the accept input
587                                   ;; code will handle the closed
588                                   ;; connection.
589                                   (nntp-kill-buffer buffer))))))))
590                 (unwind-protect
591                     (setq nntp-with-open-group-internal
592                           (condition-case nil
593                               (progn ,@forms)
594                             (quit
595                              (nntp-close-server)
596                              (signal 'quit nil))))
597                   (when timer
598                     (nnheader-cancel-timer timer)))
599                 nil))
600        (setf (symbol-function 'nntp-report) nntp-report-n))
601      nntp-with-open-group-internal))
602
603 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
604   "Retrieve the headers of ARTICLES."
605   (nntp-with-open-group
606    group server
607    (save-excursion
608      (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
609      (erase-buffer)
610      (if (and (not gnus-nov-is-evil)
611               (not nntp-nov-is-evil)
612               (nntp-retrieve-headers-with-xover articles fetch-old))
613          ;; We successfully retrieved the headers via XOVER.
614          'nov
615        ;; XOVER didn't work, so we do it the hard, slow and inefficient
616        ;; way.
617        (let ((number (length articles))
618              (articles articles)
619              (count 0)
620              (received 0)
621              (last-point (point-min))
622              (buf (nntp-find-connection-buffer nntp-server-buffer))
623              (nntp-inhibit-erase t)
624              article)
625          ;; Send HEAD commands.
626          (while (setq article (pop articles))
627            (nntp-send-command
628             nil
629             "HEAD" (if (numberp article)
630                        (int-to-string article)
631                      ;; `articles' is either a list of article numbers
632                      ;; or a list of article IDs.
633                      article))
634            (incf count)
635            ;; Every 400 requests we have to read the stream in
636            ;; order to avoid deadlocks.
637            (when (or (null articles)    ;All requests have been sent.
638                      (zerop (% count nntp-maximum-request)))
639              (nntp-accept-response)
640              (while (progn
641                       (set-buffer buf)
642                       (goto-char last-point)
643                       ;; Count replies.
644                       (while (nntp-next-result-arrived-p)
645                         (setq last-point (point))
646                         (incf received))
647                       (< received count))
648                ;; If number of headers is greater than 100, give
649                ;;  informative messages.
650                (and (numberp nntp-large-newsgroup)
651                     (> number nntp-large-newsgroup)
652                     (zerop (% received 20))
653                     (nnheader-message 6 "NNTP: Receiving headers... %d%%"
654                                       (/ (* received 100) number)))
655                (nntp-accept-response))))
656          (and (numberp nntp-large-newsgroup)
657               (> number nntp-large-newsgroup)
658               (nnheader-message 6 "NNTP: Receiving headers...done"))
659
660          ;; Now all of replies are received.  Fold continuation lines.
661          (nnheader-fold-continuation-lines)
662          ;; Remove all "\r"'s.
663          (nnheader-strip-cr)
664          (copy-to-buffer nntp-server-buffer (point-min) (point-max))
665          'headers)))))
666
667 (deffoo nntp-retrieve-groups (groups &optional server)
668   "Retrieve group info on GROUPS."
669   (nntp-with-open-group
670    nil server
671    (when (nntp-find-connection-buffer nntp-server-buffer)
672      (catch 'done
673        (save-excursion
674          ;; Erase nntp-server-buffer before nntp-inhibit-erase.
675          (nntp-erase-buffer nntp-server-buffer)
676          (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
677          ;; The first time this is run, this variable is `try'.  So we
678          ;; try.
679          (when (eq nntp-server-list-active-group 'try)
680            (nntp-try-list-active (car groups)))
681          (erase-buffer)
682          (let ((count 0)
683                (groups groups)
684                (received 0)
685                (last-point (point-min))
686                (nntp-inhibit-erase t)
687                (buf (nntp-find-connection-buffer nntp-server-buffer))
688                (command (if nntp-server-list-active-group
689                             "LIST ACTIVE" "GROUP")))
690            (while groups
691              ;; Timeout may have killed the buffer.
692              (unless (gnus-buffer-live-p buf)
693                (nnheader-report 'nntp "Connection to %s is closed." server)
694                (throw 'done nil))
695              ;; Send the command to the server.
696              (nntp-send-command nil command (pop groups))
697              (incf count)
698              ;; Every 400 requests we have to read the stream in
699              ;; order to avoid deadlocks.
700              (when (or (null groups)    ;All requests have been sent.
701                        (zerop (% count nntp-maximum-request)))
702                (nntp-accept-response)
703                (while (and (gnus-buffer-live-p buf)
704                            (progn
705                              ;; Search `blue moon' in this file for the
706                              ;; reason why set-buffer here.
707                              (set-buffer buf)
708                              (goto-char last-point)
709                              ;; Count replies.
710                              (while (re-search-forward "^[0-9]" nil t)
711                                (incf received))
712                              (setq last-point (point))
713                              (< received count)))
714                  (nntp-accept-response))))
715
716            ;; Wait for the reply from the final command.
717            (unless (gnus-buffer-live-p buf)
718              (nnheader-report 'nntp "Connection to %s is closed." server)
719              (throw 'done nil))
720            (set-buffer buf)
721            (goto-char (point-max))
722            (re-search-backward "^[0-9]" nil t)
723            (when (looking-at "^[23]")
724              (while (and (gnus-buffer-live-p buf)
725                          (progn
726                            (set-buffer buf)
727                            (goto-char (point-max))
728                            (if (not nntp-server-list-active-group)
729                                (not (re-search-backward "\r?\n"
730                                                         (- (point) 3) t))
731                              (not (re-search-backward "^\\.\r?\n"
732                                                       (- (point) 4) t)))))
733                (nntp-accept-response)))
734
735            ;; Now all replies are received.  We remove CRs.
736            (unless (gnus-buffer-live-p buf)
737              (nnheader-report 'nntp "Connection to %s is closed." server)
738              (throw 'done nil))
739            (set-buffer buf)
740            (goto-char (point-min))
741            (while (search-forward "\r" nil t)
742              (replace-match "" t t))
743
744            (if (not nntp-server-list-active-group)
745                (progn
746                  (copy-to-buffer nntp-server-buffer (point-min) (point-max))
747                  'group)
748              ;; We have read active entries, so we just delete the
749              ;; superfluous gunk.
750              (goto-char (point-min))
751              (while (re-search-forward "^[.2-5]" nil t)
752                (delete-region (match-beginning 0)
753                               (progn (forward-line 1) (point))))
754              (copy-to-buffer nntp-server-buffer (point-min) (point-max))
755              'active)))))))
756
757 (deffoo nntp-retrieve-articles (articles &optional group server)
758   (nntp-with-open-group
759     group server
760    (save-excursion
761      (let ((number (length articles))
762            (articles articles)
763            (count 0)
764            (received 0)
765            (last-point (point-min))
766            (buf (nntp-find-connection-buffer nntp-server-buffer))
767            (nntp-inhibit-erase t)
768            (map (apply 'vector articles))
769            (point 1)
770            article)
771        (set-buffer buf)
772        (erase-buffer)
773        ;; Send ARTICLE command.
774        (while (setq article (pop articles))
775          (nntp-send-command
776           nil
777           "ARTICLE" (if (numberp article)
778                         (int-to-string article)
779                       ;; `articles' is either a list of article numbers
780                       ;; or a list of article IDs.
781                       article))
782          (incf count)
783          ;; Every 400 requests we have to read the stream in
784          ;; order to avoid deadlocks.
785          (when (or (null articles)      ;All requests have been sent.
786                    (zerop (% count nntp-maximum-request)))
787            (nntp-accept-response)
788            (while (progn
789                     (set-buffer buf)
790                     (goto-char last-point)
791                     ;; Count replies.
792                     (while (nntp-next-result-arrived-p)
793                       (aset map received (cons (aref map received) (point)))
794                       (setq last-point (point))
795                       (incf received))
796                     (< received count))
797              ;; If number of headers is greater than 100, give
798              ;;  informative messages.
799              (and (numberp nntp-large-newsgroup)
800                   (> number nntp-large-newsgroup)
801                   (zerop (% received 20))
802                   (nnheader-message 6 "NNTP: Receiving articles... %d%%"
803                                     (/ (* received 100) number)))
804              (nntp-accept-response))))
805        (and (numberp nntp-large-newsgroup)
806             (> number nntp-large-newsgroup)
807             (nnheader-message 6 "NNTP: Receiving articles...done"))
808
809        ;; Now we have all the responses.  We go through the results,
810        ;; wash it and copy it over to the server buffer.
811        (set-buffer nntp-server-buffer)
812        (erase-buffer)
813        (setq last-point (point-min))
814        (mapcar
815         (lambda (entry)
816           (narrow-to-region
817            (setq point (goto-char (point-max)))
818            (progn
819              (insert-buffer-substring buf last-point (cdr entry))
820              (point-max)))
821           (setq last-point (cdr entry))
822           (nntp-decode-text)
823           (widen)
824           (cons (car entry) point))
825         map)))))
826
827 (defun nntp-try-list-active (group)
828   (nntp-list-active-group group)
829   (save-excursion
830     (set-buffer nntp-server-buffer)
831     (goto-char (point-min))
832     (cond ((or (eobp)
833                (looking-at "5[0-9]+"))
834            (setq nntp-server-list-active-group nil))
835           (t
836            (setq nntp-server-list-active-group t)))))
837
838 (deffoo nntp-list-active-group (group &optional server)
839   "Return the active info on GROUP (which can be a regexp)."
840   (nntp-with-open-group
841    nil server
842    (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
843
844 (deffoo nntp-request-group-articles (group &optional server)
845   "Return the list of existing articles in GROUP."
846   (nntp-with-open-group
847    nil server
848    (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
849
850 (deffoo nntp-request-article (article &optional group server buffer command)
851   (nntp-with-open-group
852     group server
853     (when (nntp-send-command-and-decode
854            "\r?\n\\.\r?\n" "ARTICLE"
855            (if (numberp article) (int-to-string article) article))
856       (if (and buffer
857                (not (equal buffer nntp-server-buffer)))
858           (save-excursion
859             (set-buffer nntp-server-buffer)
860             (copy-to-buffer buffer (point-min) (point-max))
861             (nntp-find-group-and-number group))
862         (nntp-find-group-and-number group)))))
863
864 (deffoo nntp-request-head (article &optional group server)
865   (nntp-with-open-group
866    group server
867    (when (nntp-send-command
868           "\r?\n\\.\r?\n" "HEAD"
869           (if (numberp article) (int-to-string article) article))
870      (prog1
871          (nntp-find-group-and-number group)
872        (nntp-decode-text)))))
873
874 (deffoo nntp-request-body (article &optional group server)
875   (nntp-with-open-group
876    group server
877    (nntp-send-command-and-decode
878     "\r?\n\\.\r?\n" "BODY"
879     (if (numberp article) (int-to-string article) article))))
880
881 (deffoo nntp-request-group (group &optional server dont-check)
882   (nntp-with-open-group 
883     nil server
884     (when (nntp-send-command "^[245].*\n" "GROUP" group)
885       (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
886         (setcar (cddr entry) group)))))
887
888 (deffoo nntp-close-group (group &optional server)
889   t)
890
891 (deffoo nntp-server-opened (&optional server)
892   "Say whether a connection to SERVER has been opened."
893   (and (nnoo-current-server-p 'nntp server)
894        nntp-server-buffer
895        (gnus-buffer-live-p nntp-server-buffer)
896        (nntp-find-connection nntp-server-buffer)))
897
898 (deffoo nntp-open-server (server &optional defs connectionless)
899   (nnheader-init-server-buffer)
900   (if (nntp-server-opened server)
901       t
902     (when (or (stringp (car defs))
903               (numberp (car defs)))
904       (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
905     (unless (assq 'nntp-address defs)
906       (setq defs (append defs (list (list 'nntp-address server)))))
907     (nnoo-change-server 'nntp server defs)
908     (unless connectionless
909       (or (nntp-find-connection nntp-server-buffer)
910           (nntp-open-connection nntp-server-buffer)))))
911
912 (deffoo nntp-close-server (&optional server)
913   (nntp-possibly-change-group nil server t)
914   (let ((process (nntp-find-connection nntp-server-buffer)))
915     (while process
916       (when (memq (process-status process) '(open run))
917         (ignore-errors
918           (nntp-send-string process "QUIT")
919           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
920             ;; Ok, this is evil, but when using telnet and stuff
921             ;; as the connection method, it's important that the
922             ;; QUIT command actually is sent out before we kill
923             ;; the process.
924             (sleep-for 1))))
925       (nntp-kill-buffer (process-buffer process))
926       (setq process (car (pop nntp-connection-alist))))
927     (nnoo-close-server 'nntp)))
928
929 (deffoo nntp-request-close ()
930   (let (process)
931     (while (setq process (pop nntp-connection-list))
932       (when (memq (process-status process) '(open run))
933         (ignore-errors
934           (nntp-send-string process "QUIT")
935           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
936             ;; Ok, this is evil, but when using telnet and stuff
937             ;; as the connection method, it's important that the
938             ;; QUIT command actually is sent out before we kill
939             ;; the process.
940             (sleep-for 1))))
941       (nntp-kill-buffer (process-buffer process)))))
942
943 (deffoo nntp-request-list (&optional server)
944   (nntp-with-open-group
945    nil server
946    (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
947
948 (deffoo nntp-request-list-newsgroups (&optional server)
949   (nntp-with-open-group
950    nil server
951    (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
952
953 (deffoo nntp-request-newgroups (date &optional server)
954   (nntp-with-open-group
955    nil server
956    (save-excursion
957      (set-buffer nntp-server-buffer)
958      (let* ((time (date-to-time date))
959             (ls (- (cadr time) (nth 8 (decode-time time)))))
960        (cond ((< ls 0)
961               (setcar time (1- (car time)))
962               (setcar (cdr time) (+ ls 65536)))
963              ((>= ls 65536)
964               (setcar time (1+ (car time)))
965               (setcar (cdr time) (- ls 65536)))
966              (t
967               (setcar (cdr time) ls)))
968        (prog1
969            (nntp-send-command
970             "^\\.\r?\n" "NEWGROUPS"
971             (format-time-string "%y%m%d %H%M%S" time)
972             "GMT")
973          (nntp-decode-text))))))
974
975 (deffoo nntp-request-post (&optional server)
976   (nntp-with-open-group
977    nil server
978    (when (nntp-send-command "^[23].*\r?\n" "POST")
979      (let ((response (with-current-buffer nntp-server-buffer
980                        nntp-process-response))
981            server-id)
982        (when (and response
983                   (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
984                                 response))
985          (setq server-id (match-string 1 response))
986          (narrow-to-region (goto-char (point-min))
987                            (if (search-forward "\n\n" nil t)
988                                (1- (point))
989                              (point-max)))
990          (unless (mail-fetch-field "Message-ID")
991            (goto-char (point-min))
992            (insert "Message-ID: " server-id "\n"))
993          (widen))
994        (run-hooks 'nntp-prepare-post-hook)
995        (nntp-send-buffer "^[23].*\n")))))
996
997 (deffoo nntp-request-type (group article)
998   'news)
999
1000 (deffoo nntp-asynchronous-p ()
1001   t)
1002
1003 ;;; Hooky functions.
1004
1005 (defun nntp-send-mode-reader ()
1006   "Send the MODE READER command to the nntp server.
1007 This function is supposed to be called from `nntp-server-opened-hook'.
1008 It will make innd servers spawn an nnrpd process to allow actual article
1009 reading."
1010   (nntp-send-command "^.*\n" "MODE READER"))
1011
1012 (defun nntp-send-authinfo (&optional send-if-force)
1013   "Send the AUTHINFO to the nntp server.
1014 It will look in the \"~/.authinfo\" file for matching entries.  If
1015 nothing suitable is found there, it will prompt for a user name
1016 and a password.
1017
1018 If SEND-IF-FORCE, only send authinfo to the server if the
1019 .authinfo file has the FORCE token."
1020   (let* ((list (gnus-parse-netrc nntp-authinfo-file))
1021          (alist (gnus-netrc-machine list nntp-address "nntp"))
1022          (force (gnus-netrc-get alist "force"))
1023          (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
1024          (passwd (gnus-netrc-get alist "password")))
1025     (when (or (not send-if-force)
1026               force)
1027       (unless user
1028         (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1029               nntp-authinfo-user user))
1030       (unless (member user '(nil ""))
1031         (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1032         (when t                         ;???Should check if AUTHINFO succeeded
1033           (nntp-send-command
1034            "^2.*\r?\n" "AUTHINFO PASS"
1035            (or passwd
1036                nntp-authinfo-password
1037                (setq nntp-authinfo-password
1038                      (read-passwd (format "NNTP (%s@%s) password: "
1039                                           user nntp-address))))))))))
1040
1041 (defun nntp-send-nosy-authinfo ()
1042   "Send the AUTHINFO to the nntp server."
1043   (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
1044     (unless (member user '(nil ""))
1045       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1046       (when t                           ;???Should check if AUTHINFO succeeded
1047         (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
1048                            (read-passwd (format "NNTP (%s@%s) password: "
1049                                                 user nntp-address)))))))
1050
1051 (defun nntp-send-authinfo-from-file ()
1052   "Send the AUTHINFO to the nntp server.
1053
1054 The authinfo login name is taken from the user's login name and the
1055 password contained in '~/.nntp-authinfo'."
1056   (when (file-exists-p "~/.nntp-authinfo")
1057     (with-temp-buffer
1058       (insert-file-contents "~/.nntp-authinfo")
1059       (goto-char (point-min))
1060       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
1061       (nntp-send-command
1062        "^2.*\r?\n" "AUTHINFO PASS"
1063        (buffer-substring (point) (point-at-eol))))))
1064
1065 ;;; Internal functions.
1066
1067 (defun nntp-handle-authinfo (process)
1068   "Take care of an authinfo response from the server."
1069   (let ((last nntp-last-command))
1070     (funcall nntp-authinfo-function)
1071     ;; We have to re-send the function that was interrupted by
1072     ;; the authinfo request.
1073     (nntp-erase-buffer nntp-server-buffer)
1074     (nntp-send-string process last)))
1075
1076 (defun nntp-make-process-buffer (buffer)
1077   "Create a new, fresh buffer usable for nntp process connections."
1078   (save-excursion
1079     (set-buffer
1080      (generate-new-buffer
1081       (format " *server %s %s %s*"
1082               nntp-address nntp-port-number
1083               (gnus-buffer-exists-p buffer))))
1084     (mm-enable-multibyte)
1085     (set (make-local-variable 'after-change-functions) nil)
1086     (set (make-local-variable 'nntp-process-wait-for) nil)
1087     (set (make-local-variable 'nntp-process-callback) nil)
1088     (set (make-local-variable 'nntp-process-to-buffer) nil)
1089     (set (make-local-variable 'nntp-process-start-point) nil)
1090     (set (make-local-variable 'nntp-process-decode) nil)
1091     (current-buffer)))
1092
1093 (defun nntp-open-connection (buffer)
1094   "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1095   (run-hooks 'nntp-prepare-server-hook)
1096   (let* ((pbuffer (nntp-make-process-buffer buffer))
1097          (timer
1098           (and nntp-connection-timeout
1099                (run-at-time
1100                 nntp-connection-timeout nil
1101                 `(lambda ()
1102                    (nntp-kill-buffer ,pbuffer)))))
1103          (process
1104           (condition-case ()
1105               (let ((coding-system-for-read nntp-coding-system-for-read)
1106                     (coding-system-for-write nntp-coding-system-for-write))
1107                 (funcall nntp-open-connection-function pbuffer))
1108             (error nil)
1109             (quit
1110              (message "Quit opening connection")
1111              (nntp-kill-buffer pbuffer)
1112              (signal 'quit nil)
1113              nil))))
1114     (when timer
1115       (nnheader-cancel-timer timer))
1116     (unless process
1117       (nntp-kill-buffer pbuffer))
1118     (when (and (buffer-name pbuffer)
1119                process)
1120       (process-kill-without-query process)
1121       (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1122                (memq (process-status process) '(open run)))
1123           (prog1
1124               (caar (push (list process buffer nil) nntp-connection-alist))
1125             (push process nntp-connection-list)
1126             (save-excursion
1127               (set-buffer pbuffer)
1128               (nntp-read-server-type)
1129               (erase-buffer)
1130               (set-buffer nntp-server-buffer)
1131               (let ((nnheader-callback-function nil))
1132                 (run-hooks 'nntp-server-opened-hook)
1133                 (nntp-send-authinfo t))))
1134         (nntp-kill-buffer (process-buffer process))
1135         nil))))
1136
1137 (defun nntp-open-network-stream (buffer)
1138   (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
1139
1140 (autoload 'format-spec "format")
1141 (autoload 'format-spec-make "format")
1142 (autoload 'open-tls-stream "tls")
1143
1144 (defun nntp-open-ssl-stream (buffer)
1145   (let* ((process-connection-type nil)
1146          (proc (start-process "nntpd" buffer 
1147                               shell-file-name
1148                               shell-command-switch
1149                               (format-spec nntp-ssl-program 
1150                                            (format-spec-make
1151                                             ?s nntp-address
1152                                             ?p nntp-port-number)))))
1153     (process-kill-without-query proc)
1154     (save-excursion
1155       (set-buffer buffer)
1156       (let ((nntp-connection-alist (list proc buffer nil)))
1157         (nntp-wait-for-string "^\r*20[01]"))
1158       (beginning-of-line)
1159       (delete-region (point-min) (point))
1160       proc)))
1161
1162 (defun nntp-open-tls-stream (buffer)
1163   (let ((proc (open-tls-stream "nntpd" buffer nntp-address nntp-port-number)))
1164     (process-kill-without-query proc)
1165     (save-excursion
1166       (set-buffer buffer)
1167       (let ((nntp-connection-alist (list proc buffer nil)))
1168         (nntp-wait-for-string "^\r*20[01]"))
1169       (beginning-of-line)
1170       (delete-region (point-min) (point))
1171       proc)))
1172
1173 (defun nntp-read-server-type ()
1174   "Find out what the name of the server we have connected to is."
1175   ;; Wait for the status string to arrive.
1176   (setq nntp-server-type (buffer-string))
1177   (let ((alist nntp-server-action-alist)
1178         (case-fold-search t)
1179         entry)
1180     ;; Run server-specific commands.
1181     (while alist
1182       (setq entry (pop alist))
1183       (when (string-match (car entry) nntp-server-type)
1184         (if (and (listp (cadr entry))
1185                  (not (eq 'lambda (caadr entry))))
1186             (eval (cadr entry))
1187           (funcall (cadr entry)))))))
1188
1189 (defun nntp-async-wait (process wait-for buffer decode callback)
1190   (save-excursion
1191     (set-buffer (process-buffer process))
1192     (unless nntp-inside-change-function
1193       (erase-buffer))
1194     (setq nntp-process-wait-for wait-for
1195           nntp-process-to-buffer buffer
1196           nntp-process-decode decode
1197           nntp-process-callback callback
1198           nntp-process-start-point (point-max))
1199     (setq after-change-functions '(nntp-after-change-function))
1200     (if nntp-async-needs-kluge
1201         (nntp-async-kluge process))))
1202
1203 (defun nntp-async-kluge (process)
1204   ;; emacs 20.3 bug: process output with encoding 'binary
1205   ;; doesn't trigger after-change-functions.
1206   (unless nntp-async-timer
1207     (setq nntp-async-timer
1208           (run-at-time 1 1 'nntp-async-timer-handler)))
1209   (add-to-list 'nntp-async-process-list process))
1210
1211 (defun nntp-async-timer-handler ()
1212   (mapcar
1213    (lambda (proc)
1214      (if (memq (process-status proc) '(open run))
1215          (nntp-async-trigger proc)
1216        (nntp-async-stop proc)))
1217    nntp-async-process-list))
1218
1219 (defun nntp-async-stop (proc)
1220   (setq nntp-async-process-list (delq proc nntp-async-process-list))
1221   (when (and nntp-async-timer (not nntp-async-process-list))
1222     (nnheader-cancel-timer nntp-async-timer)
1223     (setq nntp-async-timer nil)))
1224
1225 (defun nntp-after-change-function (beg end len)
1226   (unwind-protect
1227       ;; we only care about insertions at eob
1228       (when (and (eq 0 len) (eq (point-max) end))
1229         (save-match-data
1230           (let ((proc (get-buffer-process (current-buffer))))
1231             (when proc
1232               (nntp-async-trigger proc)))))
1233     ;; any throw from after-change-functions will leave it
1234     ;; set to nil.  so we reset it here, if necessary.
1235     (when quit-flag
1236       (setq after-change-functions '(nntp-after-change-function)))))
1237
1238 (defun nntp-async-trigger (process)
1239   (save-excursion
1240     (set-buffer (process-buffer process))
1241     (when nntp-process-callback
1242       ;; do we have an error message?
1243       (goto-char nntp-process-start-point)
1244       (if (memq (following-char) '(?4 ?5))
1245           ;; wants credentials?
1246           (if (looking-at "480")
1247               (nntp-handle-authinfo process)
1248             ;; report error message.
1249             (nntp-snarf-error-message)
1250             (nntp-do-callback nil))
1251
1252         ;; got what we expect?
1253         (goto-char (point-max))
1254         (when (re-search-backward
1255                nntp-process-wait-for nntp-process-start-point t)
1256           (let ((response (match-string 0)))
1257             (with-current-buffer nntp-server-buffer
1258               (setq nntp-process-response response)))
1259           (nntp-async-stop process)
1260           ;; convert it.
1261           (when (gnus-buffer-exists-p nntp-process-to-buffer)
1262             (let ((buf (current-buffer))
1263                   (start nntp-process-start-point)
1264                   (decode nntp-process-decode))
1265               (save-excursion
1266                 (set-buffer nntp-process-to-buffer)
1267                 (goto-char (point-max))
1268                 (save-restriction
1269                   (narrow-to-region (point) (point))
1270                   (insert-buffer-substring buf start)
1271                   (when decode
1272                     (nntp-decode-text))))))
1273           ;; report it.
1274           (goto-char (point-max))
1275           (nntp-do-callback
1276            (buffer-name (get-buffer nntp-process-to-buffer))))))))
1277
1278 (defun nntp-do-callback (arg)
1279   (let ((callback nntp-process-callback)
1280         (nntp-inside-change-function t))
1281     (setq nntp-process-callback nil)
1282     (funcall callback arg)))
1283
1284 (defun nntp-snarf-error-message ()
1285   "Save the error message in the current buffer."
1286   (let ((message (buffer-string)))
1287     (while (string-match "[\r\n]+" message)
1288       (setq message (replace-match " " t t message)))
1289     (nnheader-report 'nntp message)
1290     message))
1291
1292 (defun nntp-accept-process-output (process)
1293   "Wait for output from PROCESS and message some dots."
1294   (save-excursion
1295     (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1296                     nntp-server-buffer))
1297     (let ((len (/ (buffer-size) 1024))
1298           message-log-max)
1299       (unless (< len 10)
1300         (setq nntp-have-messaged t)
1301         (nnheader-message 7 "nntp read: %dk" len)))
1302     (nnheader-accept-process-output process)
1303     ;; accept-process-output may update status of process to indicate
1304     ;; that the server has closed the connection.  This MUST be
1305     ;; handled here as the buffer restored by the save-excursion may
1306     ;; be the process's former output buffer (i.e. now killed)
1307     (or (and process 
1308              (memq (process-status process) '(open run)))
1309         (nntp-report "Server closed connection"))))
1310
1311 (defun nntp-accept-response ()
1312   "Wait for output from the process that outputs to BUFFER."
1313   (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1314
1315 (defun nntp-possibly-change-group (group server &optional connectionless)
1316   (let ((nnheader-callback-function nil))
1317     (when server
1318       (or (nntp-server-opened server)
1319           (nntp-open-server server nil connectionless)))
1320
1321     (unless connectionless
1322       (or (nntp-find-connection nntp-server-buffer)
1323           (nntp-open-connection nntp-server-buffer))))
1324
1325   (when group
1326     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1327       (cond ((not entry)
1328              (nntp-report "Server closed connection"))
1329             ((not (equal group (caddr entry)))
1330              (save-excursion
1331                (set-buffer (process-buffer (car entry)))
1332                (erase-buffer)
1333                (nntp-send-command "^[245].*\n" "GROUP" group)
1334                (setcar (cddr entry) group)
1335                (erase-buffer)
1336                (nntp-erase-buffer nntp-server-buffer)))))))
1337
1338 (defun nntp-decode-text (&optional cr-only)
1339   "Decode the text in the current buffer."
1340   (goto-char (point-min))
1341   (while (search-forward "\r" nil t)
1342     (delete-char -1))
1343   (unless cr-only
1344     ;; Remove trailing ".\n" end-of-transfer marker.
1345     (goto-char (point-max))
1346     (forward-line -1)
1347     (when (looking-at ".\n")
1348       (delete-char 2))
1349     ;; Delete status line.
1350     (goto-char (point-min))
1351     (while (looking-at "[1-5][0-9][0-9] .*\n")
1352       ;; For some unknown reason, there is more than one status line.
1353       (delete-region (point) (progn (forward-line 1) (point))))
1354     ;; Remove "." -> ".." encoding.
1355     (while (search-forward "\n.." nil t)
1356       (delete-char -1))))
1357
1358 (defun nntp-encode-text ()
1359   "Encode the text in the current buffer."
1360   (save-excursion
1361     ;; Replace "." at beginning of line with "..".
1362     (goto-char (point-min))
1363     (while (re-search-forward "^\\." nil t)
1364       (insert "."))
1365     (goto-char (point-max))
1366     ;; Insert newline at the end of the buffer.
1367     (unless (bolp)
1368       (insert "\n"))
1369     ;; Insert `.' at end of buffer (end of text mark).
1370     (goto-char (point-max))
1371     (insert ".\n")
1372     (goto-char (point-min))
1373     (while (not (eobp))
1374       (end-of-line)
1375       (delete-char 1)
1376       (insert nntp-end-of-line))))
1377
1378 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1379   (set-buffer nntp-server-buffer)
1380   (erase-buffer)
1381   (cond
1382
1383    ;; This server does not talk NOV.
1384    ((not nntp-server-xover)
1385     nil)
1386
1387    ;; We don't care about gaps.
1388    ((or (not nntp-nov-gap)
1389         fetch-old)
1390     (nntp-send-xover-command
1391      (if fetch-old
1392          (if (numberp fetch-old)
1393              (max 1 (- (car articles) fetch-old))
1394            1)
1395        (car articles))
1396      (car (last articles)) 'wait)
1397
1398     (goto-char (point-min))
1399     (when (looking-at "[1-5][0-9][0-9] .*\n")
1400       (delete-region (point) (progn (forward-line 1) (point))))
1401     (while (search-forward "\r" nil t)
1402       (replace-match "" t t))
1403     (goto-char (point-max))
1404     (forward-line -1)
1405     (when (looking-at "\\.")
1406       (delete-region (point) (progn (forward-line 1) (point)))))
1407
1408    ;; We do it the hard way.  For each gap, an XOVER command is sent
1409    ;; to the server.  We do not wait for a reply from the server, we
1410    ;; just send them off as fast as we can.  That means that we have
1411    ;; to count the number of responses we get back to find out when we
1412    ;; have gotten all we asked for.
1413    ((numberp nntp-nov-gap)
1414     (let ((count 0)
1415           (received 0)
1416           last-point
1417           in-process-buffer-p
1418           (buf nntp-server-buffer)
1419           (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1420           first last status)
1421       ;; We have to check `nntp-server-xover'.  If it gets set to nil,
1422       ;; that means that the server does not understand XOVER, but we
1423       ;; won't know that until we try.
1424       (while (and nntp-server-xover articles)
1425         (setq first (car articles))
1426         ;; Search forward until we find a gap, or until we run out of
1427         ;; articles.
1428         (while (and (cdr articles)
1429                     (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1430           (setq articles (cdr articles)))
1431
1432         (setq in-process-buffer-p (stringp nntp-server-xover))
1433         (nntp-send-xover-command first (setq last (car articles)))
1434         (setq articles (cdr articles))
1435
1436         (when (and nntp-server-xover in-process-buffer-p)
1437           ;; Don't count tried request.
1438           (setq count (1+ count))
1439
1440           ;; Every 400 requests we have to read the stream in
1441           ;; order to avoid deadlocks.
1442           (when (or (null articles)     ;All requests have been sent.
1443                     (= 1 (% count nntp-maximum-request)))
1444
1445             (nntp-accept-response)
1446             ;; On some Emacs versions the preceding function has a
1447             ;; tendency to change the buffer.  Perhaps.  It's quite
1448             ;; difficult to reproduce, because it only seems to happen
1449             ;; once in a blue moon.
1450             (set-buffer process-buffer)
1451             (while (progn
1452                      (goto-char (or last-point (point-min)))
1453                      ;; Count replies.
1454                      (while (re-search-forward "^\\([0-9][0-9][0-9]\\) .*\n"
1455                                                nil t)
1456                        (incf received)
1457                        (setq status (match-string 1))
1458                        (if (string-match "^[45]" status)
1459                            (setq status 'error)
1460                          (setq status 'ok)))
1461                      (setq last-point (point))
1462                      (or (< received count)
1463                          (if (eq status 'error)
1464                              nil
1465                            ;; I haven't started reading the final response
1466                            (progn
1467                              (goto-char (point-max))
1468                              (forward-line -1)
1469                              (not (looking-at "^\\.\r?\n"))))))
1470               ;; I haven't read the end of the final response
1471               (nntp-accept-response)
1472               (set-buffer process-buffer))))
1473
1474         ;; Some nntp servers seem to have an extension to the XOVER
1475         ;; extension.  On these servers, requesting an article range
1476         ;; preceeding the active range does not return an error as
1477         ;; specified in the RFC.  What we instead get is the NOV entry
1478         ;; for the first available article.  Obviously, a client can
1479         ;; use that entry to avoid making unnecessary requests.  The
1480         ;; only problem is for a client that assumes that the response
1481         ;; will always be within the requested ranage.  For such a
1482         ;; client, we can get N copies of the same entry (one for each
1483         ;; XOVER command sent to the server).
1484
1485         (when (<= count 1)
1486           (goto-char (point-min))
1487           (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1488             (let ((low-limit (string-to-int
1489                               (buffer-substring (match-beginning 1) 
1490                                                 (match-end 1)))))
1491               (while (and articles (<= (car articles) low-limit))
1492                 (setq articles (cdr articles))))))
1493         (set-buffer buf))
1494
1495       (when nntp-server-xover
1496         (when in-process-buffer-p
1497           (set-buffer buf)
1498           (goto-char (point-max))
1499           (insert-buffer-substring process-buffer)
1500           (set-buffer process-buffer)
1501           (erase-buffer)
1502           (set-buffer buf))
1503
1504         ;; We remove any "." lines and status lines.
1505         (goto-char (point-min))
1506         (while (search-forward "\r" nil t)
1507           (delete-char -1))
1508         (goto-char (point-min))
1509         (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1510         t))))
1511
1512   nntp-server-xover)
1513
1514 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1515   "Send the XOVER command to the server."
1516   (let ((range (format "%d-%d" beg end))
1517         (nntp-inhibit-erase t))
1518     (if (stringp nntp-server-xover)
1519         ;; If `nntp-server-xover' is a string, then we just send this
1520         ;; command.
1521         (if wait-for-reply
1522             (nntp-send-command-nodelete
1523              "\r?\n\\.\r?\n" nntp-server-xover range)
1524           ;; We do not wait for the reply.
1525           (nntp-send-command-nodelete nil nntp-server-xover range))
1526       (let ((commands nntp-xover-commands))
1527         ;; `nntp-xover-commands' is a list of possible XOVER commands.
1528         ;; We try them all until we get at positive response.
1529         (while (and commands (eq nntp-server-xover 'try))
1530           (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1531           (save-excursion
1532             (set-buffer nntp-server-buffer)
1533             (goto-char (point-min))
1534             (and (looking-at "[23]")    ; No error message.
1535                  ;; We also have to look at the lines.  Some buggy
1536                  ;; servers give back simple lines with just the
1537                  ;; article number.  How... helpful.
1538                  (progn
1539                    (forward-line 1)
1540                    (looking-at "[0-9]+\t...")) ; More text after number.
1541                  (setq nntp-server-xover (car commands))))
1542           (setq commands (cdr commands)))
1543         ;; If none of the commands worked, we disable XOVER.
1544         (when (eq nntp-server-xover 'try)
1545           (nntp-erase-buffer nntp-server-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       (process-send-string proc "\^]")
1786       (nntp-wait-for-string "^r?telnet")
1787       (process-send-string proc "mode character\n")
1788       (accept-process-output proc 1)
1789       (sit-for 1)
1790       (goto-char (point-min))
1791       (forward-line 1)
1792       (delete-region (point) (point-max)))
1793     proc))
1794
1795 (defun nntp-open-via-telnet-and-telnet (buffer)
1796   "Open a connection to an nntp server through an intermediate host.
1797 First telnet the remote host, and then telnet the real news server
1798 from there.
1799
1800 Please refer to the following variables to customize the connection:
1801 - `nntp-pre-command',
1802 - `nntp-via-telnet-command',
1803 - `nntp-via-telnet-switches',
1804 - `nntp-via-address',
1805 - `nntp-via-envuser',
1806 - `nntp-via-user-name',
1807 - `nntp-via-user-password',
1808 - `nntp-via-shell-prompt',
1809 - `nntp-telnet-command',
1810 - `nntp-telnet-switches',
1811 - `nntp-address',
1812 - `nntp-port-number',
1813 - `nntp-end-of-line'."
1814   (save-excursion
1815     (set-buffer buffer)
1816     (erase-buffer)
1817     (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
1818           (case-fold-search t)
1819           proc)
1820       (and nntp-pre-command (push nntp-pre-command command))
1821       (setq proc (apply 'start-process "nntpd" buffer command))
1822       (when (memq (process-status proc) '(open run))
1823         (nntp-wait-for-string "^r?telnet")
1824         (process-send-string proc "set escape \^X\n")
1825         (cond
1826          ((and nntp-via-envuser nntp-via-user-name)
1827           (process-send-string proc (concat "open " "-l" nntp-via-user-name
1828                                             nntp-via-address "\n")))
1829          (t
1830           (process-send-string proc (concat "open " nntp-via-address
1831                                             "\n"))))
1832         (when (not nntp-via-envuser)
1833           (nntp-wait-for-string "^\r*.?login:")
1834           (process-send-string proc
1835                                (concat
1836                                 (or nntp-via-user-name
1837                                     (setq nntp-via-user-name
1838                                           (read-string "login: ")))
1839                                 "\n")))
1840         (nntp-wait-for-string "^\r*.?password:")
1841         (process-send-string proc
1842                              (concat
1843                               (or nntp-via-user-password
1844                                   (setq nntp-via-user-password
1845                                         (read-passwd "Password: ")))
1846                               "\n"))
1847         (nntp-wait-for-string nntp-via-shell-prompt)
1848         (let ((real-telnet-command `("exec"
1849                                      ,nntp-telnet-command
1850                                      ,@nntp-telnet-switches
1851                                      ,nntp-address
1852                                      ,nntp-port-number)))
1853           (process-send-string proc
1854                                (concat (mapconcat 'identity
1855                                                   real-telnet-command " ")
1856                                        "\n")))
1857         (nntp-wait-for-string "^\r*20[01]")
1858         (beginning-of-line)
1859         (delete-region (point-min) (point))
1860         (process-send-string proc "\^]")
1861         (nntp-wait-for-string "^r?telnet")
1862         (process-send-string proc "mode character\n")
1863         (accept-process-output proc 1)
1864         (sit-for 1)
1865         (goto-char (point-min))
1866         (forward-line 1)
1867         (delete-region (point) (point-max)))
1868       proc)))
1869
1870 (provide 'nntp)
1871
1872 ;;; nntp.el ends here