(nntp-netcat-command): New variable.
[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, 2004 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-rlogin-and-netcat',
84 - `nntp-open-via-telnet-and-telnet'.")
85
86 (defvoo nntp-pre-command nil
87   "*Pre-command to use with the various nntp-open-via-* methods.
88 This is where you would put \"runsocks\" or stuff like that.")
89
90 (defvoo nntp-telnet-command "telnet"
91   "*Telnet command used to connect to the nntp server.
92 This command is used by the methods `nntp-open-telnet-stream',
93 `nntp-open-via-rlogin-and-telnet' and `nntp-open-via-telnet-and-telnet'.
94
95 See `nntp-netcat-command' for the `nntp-open-via-rlogin-and-netcat'
96 method.")
97
98 (defvoo nntp-telnet-switches '("-8")
99   "*Switches given to the telnet command `nntp-telnet-command'.")
100
101 (defvoo nntp-netcat-command "nc"
102   "*Netcat command used to connect to the nntp server.
103 This command is used by the `nntp-open-via-rlogin-and-netcat' method.")
104
105 (defvoo nntp-netcat-switches nil
106   "*Switches given to the netcat command `nntp-netcat-command'.")
107
108 (defvoo nntp-end-of-line "\r\n"
109   "*String to use on the end of lines when talking to the NNTP server.
110 This is \"\\r\\n\" by default, but should be \"\\n\" when
111 using and indirect connection method (nntp-open-via-*).")
112
113 (defvoo nntp-via-rlogin-command "rsh"
114   "*Rlogin command used to connect to an intermediate host.
115 This command is used by the methods `nntp-open-via-rlogin-and-telnet'
116 and `nntp-open-via-rlogin-and-netcat'.  The default is \"rsh\", but \"ssh\"
117 is a popular alternative.")
118
119 (defvoo nntp-via-rlogin-command-switches nil
120   "*Switches given to the rlogin command `nntp-via-rlogin-command'.
121 If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
122 \(\"-C\") in order to compress all data connections, otherwise set this
123 to \(\"-t\" \"-e\" \"none\") or (\"-C\" \"-t\" \"-e\" \"none\") if the telnet
124 command requires a pseudo-tty allocation on an intermediate host.")
125
126 (defvoo nntp-via-telnet-command "telnet"
127   "*Telnet command used to connect to an intermediate host.
128 This command is used by the `nntp-open-via-telnet-and-telnet' method.")
129
130 (defvoo nntp-via-telnet-switches '("-8")
131   "*Switches given to the telnet command `nntp-via-telnet-command'.")
132
133 (defvoo nntp-via-user-name nil
134   "*User name to log in on an intermediate host with.
135 This variable is used by the various nntp-open-via-* methods.")
136
137 (defvoo nntp-via-user-password nil
138   "*Password to use to log in on an intermediate host with.
139 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
140
141 (defvoo nntp-via-address nil
142   "*Address of an intermediate host to connect to.
143 This variable is used by the various nntp-open-via-* methods.")
144
145 (defvoo nntp-via-envuser nil
146   "*Whether both telnet client and server support the ENVIRON option.
147 If non-nil, there will be no prompt for a login name.")
148
149 (defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
150   "*Regular expression to match the shell prompt on an intermediate host.
151 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
152
153 (defvoo nntp-large-newsgroup 50
154   "*The number of articles which indicates a large newsgroup.
155 If the number of articles is greater than the value, verbose
156 messages will be shown to indicate the current status.")
157
158 (defvoo nntp-maximum-request 400
159   "*The maximum number of the requests sent to the NNTP server at one time.
160 If Emacs hangs up while retrieving headers, set the variable to a
161 lower value.")
162
163 (defvoo nntp-nov-is-evil nil
164   "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
165
166 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
167   "*List of strings that are used as commands to fetch NOV lines from a server.
168 The strings are tried in turn until a positive response is gotten.  If
169 none of the commands are successful, nntp will just grab headers one
170 by one.")
171
172 (defvoo nntp-nov-gap 5
173   "*Maximum allowed gap between two articles.
174 If the gap between two consecutive articles is bigger than this
175 variable, split the XOVER request into two requests.")
176
177 (defvoo nntp-prepare-server-hook nil
178   "*Hook run before a server is opened.
179 If can be used to set up a server remotely, for instance.  Say you
180 have an account at the machine \"other.machine\".  This machine has
181 access to an NNTP server that you can't access locally.  You could
182 then use this hook to rsh to the remote machine and start a proxy NNTP
183 server there that you can connect to.  See also
184 `nntp-open-connection-function'")
185
186 (defvoo nntp-warn-about-losing-connection t
187   "*If non-nil, beep when a server closes connection.")
188
189 (defvoo nntp-coding-system-for-read 'binary
190   "*Coding system to read from NNTP.")
191
192 (defvoo nntp-coding-system-for-write 'binary
193   "*Coding system to write to NNTP.")
194
195 (defcustom nntp-authinfo-file "~/.authinfo"
196   ".netrc-like file that holds nntp authinfo passwords."
197   :type
198   '(choice file
199            (repeat :tag "Entries"
200                    :menu-tag "Inline"
201                    (list :format "%v"
202                          :value ("" ("login" . "") ("password" . ""))
203                          (string :tag "Host")
204                          (checklist :inline t
205                                     (cons :format "%v"
206                                           (const :format "" "login")
207                                           (string :format "Login: %v"))
208                                     (cons :format "%v"
209                                           (const :format "" "password")
210                                           (string :format "Password: %v")))))))
211
212 \f
213
214 (defvoo nntp-connection-timeout nil
215   "*Number of seconds to wait before an nntp connection times out.
216 If this variable is nil, which is the default, no timers are set.
217 NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
218
219 (defvoo nntp-prepare-post-hook nil
220   "*Hook run just before posting an article.  It is supposed to be used
221 to insert Cancel-Lock headers.")
222
223 ;;; Internal variables.
224
225 (defvar nntp-record-commands nil
226   "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
227
228 (defvar nntp-have-messaged nil)
229
230 (defvar nntp-process-wait-for nil)
231 (defvar nntp-process-to-buffer nil)
232 (defvar nntp-process-callback nil)
233 (defvar nntp-process-decode nil)
234 (defvar nntp-process-start-point nil)
235 (defvar nntp-inside-change-function nil)
236 (defvoo nntp-last-command-time nil)
237 (defvoo nntp-last-command nil)
238 (defvoo nntp-authinfo-password nil)
239 (defvoo nntp-authinfo-user nil)
240
241 (defvar nntp-connection-list nil)
242
243 (defvoo nntp-server-type nil)
244 (defvoo nntp-connection-alist nil)
245 (defvoo nntp-status-string "")
246 (defconst nntp-version "nntp 5.0")
247 (defvoo nntp-inhibit-erase nil)
248 (defvoo nntp-inhibit-output nil)
249
250 (defvoo nntp-server-xover 'try)
251 (defvoo nntp-server-list-active-group 'try)
252
253 (defvar nntp-async-needs-kluge
254   (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
255   "*When non-nil, nntp will poll asynchronous connections
256 once a second.  By default, this is turned on only for Emacs
257 20.3, which has a bug that breaks nntp's normal method of
258 noticing asynchronous data.")
259
260 (defvar nntp-async-timer nil)
261 (defvar nntp-async-process-list nil)
262
263 (defvar nntp-ssl-program 
264   "openssl s_client -quiet -ssl3 -connect %s:%p"
265 "A string containing commands for SSL connections.
266 Within a string, %s is replaced with the server address and %p with
267 port number on server.  The program should accept IMAP commands on
268 stdin and return responses to stdout.")
269
270 \f
271
272 ;;; Internal functions.
273
274 (defsubst nntp-send-string (process string)
275   "Send STRING to PROCESS."
276   ;; We need to store the time to provide timeouts, and
277   ;; to store the command so the we can replay the command
278   ;; if the server gives us an AUTHINFO challenge.
279   (setq nntp-last-command-time (current-time)
280         nntp-last-command string)
281   (when nntp-record-commands
282     (nntp-record-command string))
283   (process-send-string process (concat string nntp-end-of-line))
284   (or (memq (process-status process) '(open run))
285       (nntp-report "Server closed connection")))
286
287 (defun nntp-record-command (string)
288   "Record the command STRING."
289   (save-excursion
290     (set-buffer (get-buffer-create "*nntp-log*"))
291     (goto-char (point-max))
292     (let ((time (current-time)))
293       (insert (format-time-string "%Y%m%dT%H%M%S" time)
294               "." (format "%03d" (/ (nth 2 time) 1000))
295               " " nntp-address " " string "\n"))))
296
297 (defun nntp-report (&rest args)
298   "Report an error from the nntp backend.  The first string in ARGS
299 can be a format string.  For some commands, the failed command may be
300 retried once before actually displaying the error report."
301
302   (when nntp-record-commands
303     (nntp-record-command "*** CALLED nntp-report ***"))
304
305   (nnheader-report 'nntp args)
306
307   (apply 'error args))
308
309 (defun nntp-report-1 (&rest args)
310   "Throws out to nntp-with-open-group-error so that the connection may
311 be restored and the command retried."
312
313   (when nntp-record-commands
314     (nntp-record-command "*** CONNECTION LOST ***"))
315
316   (throw 'nntp-with-open-group-error t))
317
318 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
319   "Wait for WAIT-FOR to arrive from PROCESS."
320   (save-excursion
321     (set-buffer (process-buffer process))
322     (goto-char (point-min))
323     (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
324                     (looking-at "480"))
325                 (memq (process-status process) '(open run)))
326       (when (looking-at "480")
327         (nntp-handle-authinfo process))
328       (when (looking-at "^.*\n")
329         (delete-region (point) (progn (forward-line 1) (point))))
330       (nntp-accept-process-output process)
331       (goto-char (point-min)))
332     (prog1
333         (cond
334          ((looking-at "[45]")
335           (progn
336             (nntp-snarf-error-message)
337             nil))
338          ((not (memq (process-status process) '(open run)))
339           (nntp-report "Server closed connection"))
340          (t
341           (goto-char (point-max))
342           (let ((limit (point-min))
343                 response)
344             (while (not (re-search-backward wait-for limit t))
345               (nntp-accept-process-output process)
346               ;; We assume that whatever we wait for is less than 1000
347               ;; characters long.
348               (setq limit (max (- (point-max) 1000) (point-min)))
349               (goto-char (point-max)))
350             (setq response (match-string 0))
351             (with-current-buffer nntp-server-buffer
352               (setq nntp-process-response response)))
353           (nntp-decode-text (not decode))
354           (unless discard
355             (save-excursion
356               (set-buffer buffer)
357               (goto-char (point-max))
358               (insert-buffer-substring (process-buffer process))
359               ;; Nix out "nntp reading...." message.
360               (when nntp-have-messaged
361                 (setq nntp-have-messaged nil)
362                 (nnheader-message 5 ""))))
363           t))
364       (unless discard
365         (erase-buffer)))))
366
367 (defun nntp-kill-buffer (buffer)
368   (when (buffer-name buffer)
369     (kill-buffer buffer)
370     (nnheader-init-server-buffer)))
371
372 (defun nntp-erase-buffer (buffer)
373   "Erase contents of BUFFER."
374   (with-current-buffer buffer
375     (erase-buffer)))
376
377 (defsubst nntp-find-connection (buffer)
378   "Find the connection delivering to BUFFER."
379   (let ((alist nntp-connection-alist)
380         (buffer (if (stringp buffer) (get-buffer buffer) buffer))
381         process entry)
382     (while (and alist (setq entry (pop alist)))
383       (when (eq buffer (cadr entry))
384         (setq process (car entry)
385               alist nil)))
386     (when process
387       (if (memq (process-status process) '(open run))
388           process
389         (nntp-kill-buffer (process-buffer process))
390         (setq nntp-connection-alist (delq entry nntp-connection-alist))
391         nil))))
392
393 (defsubst nntp-find-connection-entry (buffer)
394   "Return the entry for the connection to BUFFER."
395   (assq (nntp-find-connection buffer) nntp-connection-alist))
396
397 (defun nntp-find-connection-buffer (buffer)
398   "Return the process connection buffer tied to BUFFER."
399   (let ((process (nntp-find-connection buffer)))
400     (when process
401       (process-buffer process))))
402
403 (defsubst nntp-retrieve-data (command address port buffer
404                                       &optional wait-for callback decode)
405   "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
406   (let ((process (or (nntp-find-connection buffer)
407                      (nntp-open-connection buffer))))
408     (if process
409         (progn
410           (unless (or nntp-inhibit-erase nnheader-callback-function)
411             (nntp-erase-buffer (process-buffer process)))
412           (condition-case err
413               (progn
414                 (when command
415                   (nntp-send-string process command))
416                 (cond
417                  ((eq callback 'ignore)
418                   t)
419                  ((and callback wait-for)
420                   (nntp-async-wait process wait-for buffer decode callback)
421                   t)
422                  (wait-for
423                   (nntp-wait-for process wait-for buffer decode))
424                  (t t)))
425             (error
426              (nnheader-report 'nntp "Couldn't open connection to %s: %s"
427                               address err))
428             (quit
429              (message "Quit retrieving data from nntp")
430              (signal 'quit nil)
431              nil)))
432       (nnheader-report 'nntp "Couldn't open connection to %s" address))))
433
434 (defsubst nntp-send-command (wait-for &rest strings)
435   "Send STRINGS to server and wait until WAIT-FOR returns."
436   (when (and (not nnheader-callback-function)
437              (not nntp-inhibit-output))
438     (nntp-erase-buffer nntp-server-buffer))
439   (let* ((command (mapconcat 'identity strings " "))
440          (process (nntp-find-connection nntp-server-buffer))
441          (buffer (and process (process-buffer process)))
442          (pos (and buffer (with-current-buffer buffer (point)))))
443     (if process
444         (prog1
445             (nntp-retrieve-data command
446                                 nntp-address nntp-port-number
447                                 nntp-server-buffer
448                                 wait-for nnheader-callback-function)
449           ;; If nothing to wait for, still remove possibly echo'ed commands.
450           ;; We don't have echos if nntp-open-connection-function
451           ;; is `nntp-open-network-stream', so we skip this in that case.
452           (unless (or wait-for
453                       (equal nntp-open-connection-function
454                              'nntp-open-network-stream))
455             (nntp-accept-response)
456             (save-excursion
457               (set-buffer buffer)
458               (goto-char pos)
459               (if (looking-at (regexp-quote command))
460                   (delete-region pos (progn (forward-line 1)
461                                             (point-at-bol))))
462               )))
463       (nnheader-report 'nntp "Couldn't open connection to %s."
464                        nntp-address))))
465
466 (defun nntp-send-command-nodelete (wait-for &rest strings)
467   "Send STRINGS to server and wait until WAIT-FOR returns."
468   (let* ((command (mapconcat 'identity strings " "))
469          (process (nntp-find-connection nntp-server-buffer))
470          (buffer (and process (process-buffer process)))
471          (pos (and buffer (with-current-buffer buffer (point)))))
472     (if process
473         (prog1
474             (nntp-retrieve-data command
475                                 nntp-address nntp-port-number
476                                 nntp-server-buffer
477                                 wait-for nnheader-callback-function)
478           ;; If nothing to wait for, still remove possibly echo'ed commands
479           (unless wait-for
480             (nntp-accept-response)
481             (save-excursion
482               (set-buffer buffer)
483               (goto-char pos)
484               (if (looking-at (regexp-quote command))
485                   (delete-region pos (progn (forward-line 1)
486                                             (point-at-bol)))))))
487       (nnheader-report 'nntp "Couldn't open connection to %s."
488                        nntp-address))))
489
490 (defun nntp-send-command-and-decode (wait-for &rest strings)
491   "Send STRINGS to server and wait until WAIT-FOR returns."
492   (when (and (not nnheader-callback-function)
493              (not nntp-inhibit-output))
494     (nntp-erase-buffer nntp-server-buffer))
495   (let* ((command (mapconcat 'identity strings " "))
496          (process (nntp-find-connection nntp-server-buffer))
497          (buffer (and process (process-buffer process)))
498          (pos (and buffer (with-current-buffer buffer (point)))))
499     (if process
500         (prog1
501             (nntp-retrieve-data command
502                                 nntp-address nntp-port-number
503                                 nntp-server-buffer
504                                 wait-for nnheader-callback-function t)
505           ;; If nothing to wait for, still remove possibly echo'ed commands
506           (unless wait-for
507             (nntp-accept-response)
508             (save-excursion
509               (set-buffer buffer)
510               (goto-char pos)
511               (if (looking-at (regexp-quote command))
512                   (delete-region pos (progn (forward-line 1) (point-at-bol))))
513               )))
514       (nnheader-report 'nntp "Couldn't open connection to %s."
515                        nntp-address))))
516
517
518 (defun nntp-send-buffer (wait-for)
519   "Send the current buffer to server and wait until WAIT-FOR returns."
520   (when (and (not nnheader-callback-function)
521              (not nntp-inhibit-output))
522     (nntp-erase-buffer
523      (nntp-find-connection-buffer nntp-server-buffer)))
524   (nntp-encode-text)
525   (mm-with-unibyte-current-buffer
526     ;; Some encoded unicode text contains character 0x80-0x9f e.g. Euro.
527     (process-send-region (nntp-find-connection nntp-server-buffer)
528                          (point-min) (point-max)))
529   (nntp-retrieve-data
530    nil nntp-address nntp-port-number nntp-server-buffer
531    wait-for nnheader-callback-function))
532
533 \f
534
535 ;;; Interface functions.
536
537 (nnoo-define-basics nntp)
538
539 (defsubst nntp-next-result-arrived-p ()
540   (cond
541    ;; A result that starts with a 2xx code is terminated by
542    ;; a line with only a "." on it.
543    ((eq (char-after) ?2)
544     (if (re-search-forward "\n\\.\r?\n" nil t)
545         t
546       nil))
547    ;; A result that starts with a 3xx or 4xx code is terminated
548    ;; by a newline.
549    ((looking-at "[34]")
550     (if (search-forward "\n" nil t)
551         t
552       nil))
553    ;; No result here.
554    (t
555     nil)))
556
557 (eval-when-compile
558   (defvar nntp-with-open-group-internal nil)
559   (defvar nntp-report-n nil))
560
561 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
562   "Protect against servers that don't like clients that keep idle connections opens.
563 The problem being that these servers may either close a connection or
564 simply ignore any further requests on a connection.  Closed
565 connections are not detected until accept-process-output has updated
566 the process-status.  Dropped connections are not detected until the
567 connection timeouts (which may be several minutes) or
568 nntp-connection-timeout has expired.  When these occur
569 nntp-with-open-group, opens a new connection then re-issues the NNTP
570 command whose response triggered the error."
571   (when (and (listp connectionless)
572              (not (eq connectionless nil)))
573     (setq forms (cons connectionless forms)
574           connectionless nil))
575   `(letf ((nntp-report-n (symbol-function 'nntp-report))
576           ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1))
577           (nntp-with-open-group-internal nil))
578      (while (catch 'nntp-with-open-group-error
579               ;; Open the connection to the server
580               ;; NOTE: Existing connections are NOT tested.
581               (nntp-possibly-change-group ,group ,server ,connectionless)
582
583               (let ((timer
584                      (and nntp-connection-timeout
585                           (run-at-time
586                            nntp-connection-timeout nil
587                            '(lambda ()
588                               (let ((process (nntp-find-connection
589                                               nntp-server-buffer))
590                                     (buffer  (and process
591                                                   (process-buffer process))))
592                                 ;; When I an able to identify the
593                                 ;; connection to the server AND I've
594                                 ;; received NO reponse for
595                                 ;; nntp-connection-timeout seconds.
596                                 (when (and buffer (eq 0 (buffer-size buffer)))
597                                   ;; Close the connection.  Take no
598                                   ;; other action as the accept input
599                                   ;; code will handle the closed
600                                   ;; connection.
601                                   (nntp-kill-buffer buffer))))))))
602                 (unwind-protect
603                     (setq nntp-with-open-group-internal
604                           (condition-case nil
605                               (progn ,@forms)
606                             (quit
607                              (nntp-close-server)
608                              (signal 'quit nil))))
609                   (when timer
610                     (nnheader-cancel-timer timer)))
611                 nil))
612        (setf (symbol-function 'nntp-report) nntp-report-n))
613      nntp-with-open-group-internal))
614
615 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
616   "Retrieve the headers of ARTICLES."
617   (nntp-with-open-group
618    group server
619    (save-excursion
620      (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
621      (erase-buffer)
622      (if (and (not gnus-nov-is-evil)
623               (not nntp-nov-is-evil)
624               (nntp-retrieve-headers-with-xover articles fetch-old))
625          ;; We successfully retrieved the headers via XOVER.
626          'nov
627        ;; XOVER didn't work, so we do it the hard, slow and inefficient
628        ;; way.
629        (let ((number (length articles))
630              (articles articles)
631              (count 0)
632              (received 0)
633              (last-point (point-min))
634              (buf (nntp-find-connection-buffer nntp-server-buffer))
635              (nntp-inhibit-erase t)
636              article)
637          ;; Send HEAD commands.
638          (while (setq article (pop articles))
639            (nntp-send-command
640             nil
641             "HEAD" (if (numberp article)
642                        (int-to-string article)
643                      ;; `articles' is either a list of article numbers
644                      ;; or a list of article IDs.
645                      article))
646            (incf count)
647            ;; Every 400 requests we have to read the stream in
648            ;; order to avoid deadlocks.
649            (when (or (null articles)    ;All requests have been sent.
650                      (zerop (% count nntp-maximum-request)))
651              (nntp-accept-response)
652              (while (progn
653                       (set-buffer buf)
654                       (goto-char last-point)
655                       ;; Count replies.
656                       (while (nntp-next-result-arrived-p)
657                         (setq last-point (point))
658                         (incf received))
659                       (< received count))
660                ;; If number of headers is greater than 100, give
661                ;;  informative messages.
662                (and (numberp nntp-large-newsgroup)
663                     (> number nntp-large-newsgroup)
664                     (zerop (% received 20))
665                     (nnheader-message 6 "NNTP: Receiving headers... %d%%"
666                                       (/ (* received 100) number)))
667                (nntp-accept-response))))
668          (and (numberp nntp-large-newsgroup)
669               (> number nntp-large-newsgroup)
670               (nnheader-message 6 "NNTP: Receiving headers...done"))
671
672          ;; Now all of replies are received.  Fold continuation lines.
673          (nnheader-fold-continuation-lines)
674          ;; Remove all "\r"'s.
675          (nnheader-strip-cr)
676          (copy-to-buffer nntp-server-buffer (point-min) (point-max))
677          'headers)))))
678
679 (deffoo nntp-retrieve-groups (groups &optional server)
680   "Retrieve group info on GROUPS."
681   (nntp-with-open-group
682    nil server
683    (when (nntp-find-connection-buffer nntp-server-buffer)
684      (catch 'done
685        (save-excursion
686          ;; Erase nntp-server-buffer before nntp-inhibit-erase.
687          (nntp-erase-buffer nntp-server-buffer)
688          (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
689          ;; The first time this is run, this variable is `try'.  So we
690          ;; try.
691          (when (eq nntp-server-list-active-group 'try)
692            (nntp-try-list-active (car groups)))
693          (erase-buffer)
694          (let ((count 0)
695                (groups groups)
696                (received 0)
697                (last-point (point-min))
698                (nntp-inhibit-erase t)
699                (buf (nntp-find-connection-buffer nntp-server-buffer))
700                (command (if nntp-server-list-active-group
701                             "LIST ACTIVE" "GROUP")))
702            (while groups
703              ;; Timeout may have killed the buffer.
704              (unless (gnus-buffer-live-p buf)
705                (nnheader-report 'nntp "Connection to %s is closed." server)
706                (throw 'done nil))
707              ;; Send the command to the server.
708              (nntp-send-command nil command (pop groups))
709              (incf count)
710              ;; Every 400 requests we have to read the stream in
711              ;; order to avoid deadlocks.
712              (when (or (null groups)    ;All requests have been sent.
713                        (zerop (% count nntp-maximum-request)))
714                (nntp-accept-response)
715                (while (and (gnus-buffer-live-p buf)
716                            (progn
717                              ;; Search `blue moon' in this file for the
718                              ;; reason why set-buffer here.
719                              (set-buffer buf)
720                              (goto-char last-point)
721                              ;; Count replies.
722                              (while (re-search-forward "^[0-9]" nil t)
723                                (incf received))
724                              (setq last-point (point))
725                              (< received count)))
726                  (nntp-accept-response))))
727
728            ;; Wait for the reply from the final command.
729            (unless (gnus-buffer-live-p buf)
730              (nnheader-report 'nntp "Connection to %s is closed." server)
731              (throw 'done nil))
732            (set-buffer buf)
733            (goto-char (point-max))
734            (re-search-backward "^[0-9]" nil t)
735            (when (looking-at "^[23]")
736              (while (and (gnus-buffer-live-p buf)
737                          (progn
738                            (set-buffer buf)
739                            (goto-char (point-max))
740                            (if (not nntp-server-list-active-group)
741                                (not (re-search-backward "\r?\n"
742                                                         (- (point) 3) t))
743                              (not (re-search-backward "^\\.\r?\n"
744                                                       (- (point) 4) t)))))
745                (nntp-accept-response)))
746
747            ;; Now all replies are received.  We remove CRs.
748            (unless (gnus-buffer-live-p buf)
749              (nnheader-report 'nntp "Connection to %s is closed." server)
750              (throw 'done nil))
751            (set-buffer buf)
752            (goto-char (point-min))
753            (while (search-forward "\r" nil t)
754              (replace-match "" t t))
755
756            (if (not nntp-server-list-active-group)
757                (progn
758                  (copy-to-buffer nntp-server-buffer (point-min) (point-max))
759                  'group)
760              ;; We have read active entries, so we just delete the
761              ;; superfluous gunk.
762              (goto-char (point-min))
763              (while (re-search-forward "^[.2-5]" nil t)
764                (delete-region (match-beginning 0)
765                               (progn (forward-line 1) (point))))
766              (copy-to-buffer nntp-server-buffer (point-min) (point-max))
767              'active)))))))
768
769 (deffoo nntp-retrieve-articles (articles &optional group server)
770   (nntp-with-open-group
771     group server
772    (save-excursion
773      (let ((number (length articles))
774            (articles articles)
775            (count 0)
776            (received 0)
777            (last-point (point-min))
778            (buf (nntp-find-connection-buffer nntp-server-buffer))
779            (nntp-inhibit-erase t)
780            (map (apply 'vector articles))
781            (point 1)
782            article)
783        (set-buffer buf)
784        (erase-buffer)
785        ;; Send ARTICLE command.
786        (while (setq article (pop articles))
787          (nntp-send-command
788           nil
789           "ARTICLE" (if (numberp article)
790                         (int-to-string article)
791                       ;; `articles' is either a list of article numbers
792                       ;; or a list of article IDs.
793                       article))
794          (incf count)
795          ;; Every 400 requests we have to read the stream in
796          ;; order to avoid deadlocks.
797          (when (or (null articles)      ;All requests have been sent.
798                    (zerop (% count nntp-maximum-request)))
799            (nntp-accept-response)
800            (while (progn
801                     (set-buffer buf)
802                     (goto-char last-point)
803                     ;; Count replies.
804                     (while (nntp-next-result-arrived-p)
805                       (aset map received (cons (aref map received) (point)))
806                       (setq last-point (point))
807                       (incf received))
808                     (< received count))
809              ;; If number of headers is greater than 100, give
810              ;;  informative messages.
811              (and (numberp nntp-large-newsgroup)
812                   (> number nntp-large-newsgroup)
813                   (zerop (% received 20))
814                   (nnheader-message 6 "NNTP: Receiving articles... %d%%"
815                                     (/ (* received 100) number)))
816              (nntp-accept-response))))
817        (and (numberp nntp-large-newsgroup)
818             (> number nntp-large-newsgroup)
819             (nnheader-message 6 "NNTP: Receiving articles...done"))
820
821        ;; Now we have all the responses.  We go through the results,
822        ;; wash it and copy it over to the server buffer.
823        (set-buffer nntp-server-buffer)
824        (erase-buffer)
825        (setq last-point (point-min))
826        (mapcar
827         (lambda (entry)
828           (narrow-to-region
829            (setq point (goto-char (point-max)))
830            (progn
831              (insert-buffer-substring buf last-point (cdr entry))
832              (point-max)))
833           (setq last-point (cdr entry))
834           (nntp-decode-text)
835           (widen)
836           (cons (car entry) point))
837         map)))))
838
839 (defun nntp-try-list-active (group)
840   (nntp-list-active-group group)
841   (save-excursion
842     (set-buffer nntp-server-buffer)
843     (goto-char (point-min))
844     (cond ((or (eobp)
845                (looking-at "5[0-9]+"))
846            (setq nntp-server-list-active-group nil))
847           (t
848            (setq nntp-server-list-active-group t)))))
849
850 (deffoo nntp-list-active-group (group &optional server)
851   "Return the active info on GROUP (which can be a regexp)."
852   (nntp-with-open-group
853    nil server
854    (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
855
856 (deffoo nntp-request-group-articles (group &optional server)
857   "Return the list of existing articles in GROUP."
858   (nntp-with-open-group
859    nil server
860    (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
861
862 (deffoo nntp-request-article (article &optional group server buffer command)
863   (nntp-with-open-group
864     group server
865     (when (nntp-send-command-and-decode
866            "\r?\n\\.\r?\n" "ARTICLE"
867            (if (numberp article) (int-to-string article) article))
868       (if (and buffer
869                (not (equal buffer nntp-server-buffer)))
870           (save-excursion
871             (set-buffer nntp-server-buffer)
872             (copy-to-buffer buffer (point-min) (point-max))
873             (nntp-find-group-and-number group))
874         (nntp-find-group-and-number group)))))
875
876 (deffoo nntp-request-head (article &optional group server)
877   (nntp-with-open-group
878    group server
879    (when (nntp-send-command
880           "\r?\n\\.\r?\n" "HEAD"
881           (if (numberp article) (int-to-string article) article))
882      (prog1
883          (nntp-find-group-and-number group)
884        (nntp-decode-text)))))
885
886 (deffoo nntp-request-body (article &optional group server)
887   (nntp-with-open-group
888    group server
889    (nntp-send-command-and-decode
890     "\r?\n\\.\r?\n" "BODY"
891     (if (numberp article) (int-to-string article) article))))
892
893 (deffoo nntp-request-group (group &optional server dont-check)
894   (nntp-with-open-group 
895     nil server
896     (when (nntp-send-command "^[245].*\n" "GROUP" group)
897       (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
898         (setcar (cddr entry) group)))))
899
900 (deffoo nntp-close-group (group &optional server)
901   t)
902
903 (deffoo nntp-server-opened (&optional server)
904   "Say whether a connection to SERVER has been opened."
905   (and (nnoo-current-server-p 'nntp server)
906        nntp-server-buffer
907        (gnus-buffer-live-p nntp-server-buffer)
908        (nntp-find-connection nntp-server-buffer)))
909
910 (deffoo nntp-open-server (server &optional defs connectionless)
911   (nnheader-init-server-buffer)
912   (if (nntp-server-opened server)
913       t
914     (when (or (stringp (car defs))
915               (numberp (car defs)))
916       (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
917     (unless (assq 'nntp-address defs)
918       (setq defs (append defs (list (list 'nntp-address server)))))
919     (nnoo-change-server 'nntp server defs)
920     (unless connectionless
921       (or (nntp-find-connection nntp-server-buffer)
922           (nntp-open-connection nntp-server-buffer)))))
923
924 (deffoo nntp-close-server (&optional server)
925   (nntp-possibly-change-group nil server t)
926   (let ((process (nntp-find-connection nntp-server-buffer)))
927     (while process
928       (when (memq (process-status process) '(open run))
929         (ignore-errors
930           (nntp-send-string process "QUIT")
931           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
932             ;; Ok, this is evil, but when using telnet and stuff
933             ;; as the connection method, it's important that the
934             ;; QUIT command actually is sent out before we kill
935             ;; the process.
936             (sleep-for 1))))
937       (nntp-kill-buffer (process-buffer process))
938       (setq process (car (pop nntp-connection-alist))))
939     (nnoo-close-server 'nntp)))
940
941 (deffoo nntp-request-close ()
942   (let (process)
943     (while (setq process (pop nntp-connection-list))
944       (when (memq (process-status process) '(open run))
945         (ignore-errors
946           (nntp-send-string process "QUIT")
947           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
948             ;; Ok, this is evil, but when using telnet and stuff
949             ;; as the connection method, it's important that the
950             ;; QUIT command actually is sent out before we kill
951             ;; the process.
952             (sleep-for 1))))
953       (nntp-kill-buffer (process-buffer process)))))
954
955 (deffoo nntp-request-list (&optional server)
956   (nntp-with-open-group
957    nil server
958    (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
959
960 (deffoo nntp-request-list-newsgroups (&optional server)
961   (nntp-with-open-group
962    nil server
963    (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
964
965 (deffoo nntp-request-newgroups (date &optional server)
966   (nntp-with-open-group
967    nil server
968    (save-excursion
969      (set-buffer nntp-server-buffer)
970      (let* ((time (date-to-time date))
971             (ls (- (cadr time) (nth 8 (decode-time time)))))
972        (cond ((< ls 0)
973               (setcar time (1- (car time)))
974               (setcar (cdr time) (+ ls 65536)))
975              ((>= ls 65536)
976               (setcar time (1+ (car time)))
977               (setcar (cdr time) (- ls 65536)))
978              (t
979               (setcar (cdr time) ls)))
980        (prog1
981            (nntp-send-command
982             "^\\.\r?\n" "NEWGROUPS"
983             (format-time-string "%y%m%d %H%M%S" time)
984             "GMT")
985          (nntp-decode-text))))))
986
987 (deffoo nntp-request-post (&optional server)
988   (nntp-with-open-group
989    nil server
990    (when (nntp-send-command "^[23].*\r?\n" "POST")
991      (let ((response (with-current-buffer nntp-server-buffer
992                        nntp-process-response))
993            server-id)
994        (when (and response
995                   (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
996                                 response))
997          (setq server-id (match-string 1 response))
998          (narrow-to-region (goto-char (point-min))
999                            (if (search-forward "\n\n" nil t)
1000                                (1- (point))
1001                              (point-max)))
1002          (unless (mail-fetch-field "Message-ID")
1003            (goto-char (point-min))
1004            (insert "Message-ID: " server-id "\n"))
1005          (widen))
1006        (run-hooks 'nntp-prepare-post-hook)
1007        (nntp-send-buffer "^[23].*\n")))))
1008
1009 (deffoo nntp-request-type (group article)
1010   'news)
1011
1012 (deffoo nntp-asynchronous-p ()
1013   t)
1014
1015 ;;; Hooky functions.
1016
1017 (defun nntp-send-mode-reader ()
1018   "Send the MODE READER command to the nntp server.
1019 This function is supposed to be called from `nntp-server-opened-hook'.
1020 It will make innd servers spawn an nnrpd process to allow actual article
1021 reading."
1022   (nntp-send-command "^.*\n" "MODE READER"))
1023
1024 (defun nntp-send-authinfo (&optional send-if-force)
1025   "Send the AUTHINFO to the nntp server.
1026 It will look in the \"~/.authinfo\" file for matching entries.  If
1027 nothing suitable is found there, it will prompt for a user name
1028 and a password.
1029
1030 If SEND-IF-FORCE, only send authinfo to the server if the
1031 .authinfo file has the FORCE token."
1032   (let* ((list (gnus-parse-netrc nntp-authinfo-file))
1033          (alist (gnus-netrc-machine list nntp-address "nntp"))
1034          (force (gnus-netrc-get alist "force"))
1035          (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
1036          (passwd (gnus-netrc-get alist "password")))
1037     (when (or (not send-if-force)
1038               force)
1039       (unless user
1040         (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1041               nntp-authinfo-user user))
1042       (unless (member user '(nil ""))
1043         (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1044         (when t                         ;???Should check if AUTHINFO succeeded
1045           (nntp-send-command
1046            "^2.*\r?\n" "AUTHINFO PASS"
1047            (or passwd
1048                nntp-authinfo-password
1049                (setq nntp-authinfo-password
1050                      (read-passwd (format "NNTP (%s@%s) password: "
1051                                           user nntp-address))))))))))
1052
1053 (defun nntp-send-nosy-authinfo ()
1054   "Send the AUTHINFO to the nntp server."
1055   (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
1056     (unless (member user '(nil ""))
1057       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1058       (when t                           ;???Should check if AUTHINFO succeeded
1059         (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
1060                            (read-passwd (format "NNTP (%s@%s) password: "
1061                                                 user nntp-address)))))))
1062
1063 (defun nntp-send-authinfo-from-file ()
1064   "Send the AUTHINFO to the nntp server.
1065
1066 The authinfo login name is taken from the user's login name and the
1067 password contained in '~/.nntp-authinfo'."
1068   (when (file-exists-p "~/.nntp-authinfo")
1069     (with-temp-buffer
1070       (insert-file-contents "~/.nntp-authinfo")
1071       (goto-char (point-min))
1072       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
1073       (nntp-send-command
1074        "^2.*\r?\n" "AUTHINFO PASS"
1075        (buffer-substring (point) (point-at-eol))))))
1076
1077 ;;; Internal functions.
1078
1079 (defun nntp-handle-authinfo (process)
1080   "Take care of an authinfo response from the server."
1081   (let ((last nntp-last-command))
1082     (funcall nntp-authinfo-function)
1083     ;; We have to re-send the function that was interrupted by
1084     ;; the authinfo request.
1085     (nntp-erase-buffer nntp-server-buffer)
1086     (nntp-send-string process last)))
1087
1088 (defun nntp-make-process-buffer (buffer)
1089   "Create a new, fresh buffer usable for nntp process connections."
1090   (save-excursion
1091     (set-buffer
1092      (generate-new-buffer
1093       (format " *server %s %s %s*"
1094               nntp-address nntp-port-number
1095               (gnus-buffer-exists-p buffer))))
1096     (mm-enable-multibyte)
1097     (set (make-local-variable 'after-change-functions) nil)
1098     (set (make-local-variable 'nntp-process-wait-for) nil)
1099     (set (make-local-variable 'nntp-process-callback) nil)
1100     (set (make-local-variable 'nntp-process-to-buffer) nil)
1101     (set (make-local-variable 'nntp-process-start-point) nil)
1102     (set (make-local-variable 'nntp-process-decode) nil)
1103     (current-buffer)))
1104
1105 (defun nntp-open-connection (buffer)
1106   "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1107   (run-hooks 'nntp-prepare-server-hook)
1108   (let* ((pbuffer (nntp-make-process-buffer buffer))
1109          (timer
1110           (and nntp-connection-timeout
1111                (run-at-time
1112                 nntp-connection-timeout nil
1113                 `(lambda ()
1114                    (nntp-kill-buffer ,pbuffer)))))
1115          (process
1116           (condition-case ()
1117               (let ((coding-system-for-read nntp-coding-system-for-read)
1118                     (coding-system-for-write nntp-coding-system-for-write))
1119                 (funcall nntp-open-connection-function pbuffer))
1120             (error nil)
1121             (quit
1122              (message "Quit opening connection")
1123              (nntp-kill-buffer pbuffer)
1124              (signal 'quit nil)
1125              nil))))
1126     (when timer
1127       (nnheader-cancel-timer timer))
1128     (unless process
1129       (nntp-kill-buffer pbuffer))
1130     (when (and (buffer-name pbuffer)
1131                process)
1132       (process-kill-without-query process)
1133       (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1134                (memq (process-status process) '(open run)))
1135           (prog1
1136               (caar (push (list process buffer nil) nntp-connection-alist))
1137             (push process nntp-connection-list)
1138             (save-excursion
1139               (set-buffer pbuffer)
1140               (nntp-read-server-type)
1141               (erase-buffer)
1142               (set-buffer nntp-server-buffer)
1143               (let ((nnheader-callback-function nil))
1144                 (run-hooks 'nntp-server-opened-hook)
1145                 (nntp-send-authinfo t))))
1146         (nntp-kill-buffer (process-buffer process))
1147         nil))))
1148
1149 (defun nntp-open-network-stream (buffer)
1150   (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
1151
1152 (autoload 'format-spec "format")
1153 (autoload 'format-spec-make "format")
1154 (autoload 'open-tls-stream "tls")
1155
1156 (defun nntp-open-ssl-stream (buffer)
1157   (let* ((process-connection-type nil)
1158          (proc (start-process "nntpd" buffer 
1159                               shell-file-name
1160                               shell-command-switch
1161                               (format-spec nntp-ssl-program 
1162                                            (format-spec-make
1163                                             ?s nntp-address
1164                                             ?p nntp-port-number)))))
1165     (process-kill-without-query proc)
1166     (save-excursion
1167       (set-buffer buffer)
1168       (let ((nntp-connection-alist (list proc buffer nil)))
1169         (nntp-wait-for-string "^\r*20[01]"))
1170       (beginning-of-line)
1171       (delete-region (point-min) (point))
1172       proc)))
1173
1174 (defun nntp-open-tls-stream (buffer)
1175   (let ((proc (open-tls-stream "nntpd" buffer nntp-address nntp-port-number)))
1176     (process-kill-without-query proc)
1177     (save-excursion
1178       (set-buffer buffer)
1179       (let ((nntp-connection-alist (list proc buffer nil)))
1180         (nntp-wait-for-string "^\r*20[01]"))
1181       (beginning-of-line)
1182       (delete-region (point-min) (point))
1183       proc)))
1184
1185 (defun nntp-read-server-type ()
1186   "Find out what the name of the server we have connected to is."
1187   ;; Wait for the status string to arrive.
1188   (setq nntp-server-type (buffer-string))
1189   (let ((alist nntp-server-action-alist)
1190         (case-fold-search t)
1191         entry)
1192     ;; Run server-specific commands.
1193     (while alist
1194       (setq entry (pop alist))
1195       (when (string-match (car entry) nntp-server-type)
1196         (if (and (listp (cadr entry))
1197                  (not (eq 'lambda (caadr entry))))
1198             (eval (cadr entry))
1199           (funcall (cadr entry)))))))
1200
1201 (defun nntp-async-wait (process wait-for buffer decode callback)
1202   (save-excursion
1203     (set-buffer (process-buffer process))
1204     (unless nntp-inside-change-function
1205       (erase-buffer))
1206     (setq nntp-process-wait-for wait-for
1207           nntp-process-to-buffer buffer
1208           nntp-process-decode decode
1209           nntp-process-callback callback
1210           nntp-process-start-point (point-max))
1211     (setq after-change-functions '(nntp-after-change-function))
1212     (if nntp-async-needs-kluge
1213         (nntp-async-kluge process))))
1214
1215 (defun nntp-async-kluge (process)
1216   ;; emacs 20.3 bug: process output with encoding 'binary
1217   ;; doesn't trigger after-change-functions.
1218   (unless nntp-async-timer
1219     (setq nntp-async-timer
1220           (run-at-time 1 1 'nntp-async-timer-handler)))
1221   (add-to-list 'nntp-async-process-list process))
1222
1223 (defun nntp-async-timer-handler ()
1224   (mapcar
1225    (lambda (proc)
1226      (if (memq (process-status proc) '(open run))
1227          (nntp-async-trigger proc)
1228        (nntp-async-stop proc)))
1229    nntp-async-process-list))
1230
1231 (defun nntp-async-stop (proc)
1232   (setq nntp-async-process-list (delq proc nntp-async-process-list))
1233   (when (and nntp-async-timer (not nntp-async-process-list))
1234     (nnheader-cancel-timer nntp-async-timer)
1235     (setq nntp-async-timer nil)))
1236
1237 (defun nntp-after-change-function (beg end len)
1238   (unwind-protect
1239       ;; we only care about insertions at eob
1240       (when (and (eq 0 len) (eq (point-max) end))
1241         (save-match-data
1242           (let ((proc (get-buffer-process (current-buffer))))
1243             (when proc
1244               (nntp-async-trigger proc)))))
1245     ;; any throw from after-change-functions will leave it
1246     ;; set to nil.  so we reset it here, if necessary.
1247     (when quit-flag
1248       (setq after-change-functions '(nntp-after-change-function)))))
1249
1250 (defun nntp-async-trigger (process)
1251   (save-excursion
1252     (set-buffer (process-buffer process))
1253     (when nntp-process-callback
1254       ;; do we have an error message?
1255       (goto-char nntp-process-start-point)
1256       (if (memq (following-char) '(?4 ?5))
1257           ;; wants credentials?
1258           (if (looking-at "480")
1259               (nntp-handle-authinfo process)
1260             ;; report error message.
1261             (nntp-snarf-error-message)
1262             (nntp-do-callback nil))
1263
1264         ;; got what we expect?
1265         (goto-char (point-max))
1266         (when (re-search-backward
1267                nntp-process-wait-for nntp-process-start-point t)
1268           (let ((response (match-string 0)))
1269             (with-current-buffer nntp-server-buffer
1270               (setq nntp-process-response response)))
1271           (nntp-async-stop process)
1272           ;; convert it.
1273           (when (gnus-buffer-exists-p nntp-process-to-buffer)
1274             (let ((buf (current-buffer))
1275                   (start nntp-process-start-point)
1276                   (decode nntp-process-decode))
1277               (save-excursion
1278                 (set-buffer nntp-process-to-buffer)
1279                 (goto-char (point-max))
1280                 (save-restriction
1281                   (narrow-to-region (point) (point))
1282                   (insert-buffer-substring buf start)
1283                   (when decode
1284                     (nntp-decode-text))))))
1285           ;; report it.
1286           (goto-char (point-max))
1287           (nntp-do-callback
1288            (buffer-name (get-buffer nntp-process-to-buffer))))))))
1289
1290 (defun nntp-do-callback (arg)
1291   (let ((callback nntp-process-callback)
1292         (nntp-inside-change-function t))
1293     (setq nntp-process-callback nil)
1294     (funcall callback arg)))
1295
1296 (defun nntp-snarf-error-message ()
1297   "Save the error message in the current buffer."
1298   (let ((message (buffer-string)))
1299     (while (string-match "[\r\n]+" message)
1300       (setq message (replace-match " " t t message)))
1301     (nnheader-report 'nntp message)
1302     message))
1303
1304 (defun nntp-accept-process-output (process)
1305   "Wait for output from PROCESS and message some dots."
1306   (save-excursion
1307     (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1308                     nntp-server-buffer))
1309     (let ((len (/ (buffer-size) 1024))
1310           message-log-max)
1311       (unless (< len 10)
1312         (setq nntp-have-messaged t)
1313         (nnheader-message 7 "nntp read: %dk" len)))
1314     (nnheader-accept-process-output process)
1315     ;; accept-process-output may update status of process to indicate
1316     ;; that the server has closed the connection.  This MUST be
1317     ;; handled here as the buffer restored by the save-excursion may
1318     ;; be the process's former output buffer (i.e. now killed)
1319     (or (and process 
1320              (memq (process-status process) '(open run)))
1321         (nntp-report "Server closed connection"))))
1322
1323 (defun nntp-accept-response ()
1324   "Wait for output from the process that outputs to BUFFER."
1325   (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1326
1327 (defun nntp-possibly-change-group (group server &optional connectionless)
1328   (let ((nnheader-callback-function nil))
1329     (when server
1330       (or (nntp-server-opened server)
1331           (nntp-open-server server nil connectionless)))
1332
1333     (unless connectionless
1334       (or (nntp-find-connection nntp-server-buffer)
1335           (nntp-open-connection nntp-server-buffer))))
1336
1337   (when group
1338     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1339       (cond ((not entry)
1340              (nntp-report "Server closed connection"))
1341             ((not (equal group (caddr entry)))
1342              (save-excursion
1343                (set-buffer (process-buffer (car entry)))
1344                (erase-buffer)
1345                (nntp-send-command "^[245].*\n" "GROUP" group)
1346                (setcar (cddr entry) group)
1347                (erase-buffer)
1348                (nntp-erase-buffer nntp-server-buffer)))))))
1349
1350 (defun nntp-decode-text (&optional cr-only)
1351   "Decode the text in the current buffer."
1352   (goto-char (point-min))
1353   (while (search-forward "\r" nil t)
1354     (delete-char -1))
1355   (unless cr-only
1356     ;; Remove trailing ".\n" end-of-transfer marker.
1357     (goto-char (point-max))
1358     (forward-line -1)
1359     (when (looking-at ".\n")
1360       (delete-char 2))
1361     ;; Delete status line.
1362     (goto-char (point-min))
1363     (while (looking-at "[1-5][0-9][0-9] .*\n")
1364       ;; For some unknown reason, there is more than one status line.
1365       (delete-region (point) (progn (forward-line 1) (point))))
1366     ;; Remove "." -> ".." encoding.
1367     (while (search-forward "\n.." nil t)
1368       (delete-char -1))))
1369
1370 (defun nntp-encode-text ()
1371   "Encode the text in the current buffer."
1372   (save-excursion
1373     ;; Replace "." at beginning of line with "..".
1374     (goto-char (point-min))
1375     (while (re-search-forward "^\\." nil t)
1376       (insert "."))
1377     (goto-char (point-max))
1378     ;; Insert newline at the end of the buffer.
1379     (unless (bolp)
1380       (insert "\n"))
1381     ;; Insert `.' at end of buffer (end of text mark).
1382     (goto-char (point-max))
1383     (insert ".\n")
1384     (goto-char (point-min))
1385     (while (not (eobp))
1386       (end-of-line)
1387       (delete-char 1)
1388       (insert nntp-end-of-line))))
1389
1390 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1391   (set-buffer nntp-server-buffer)
1392   (erase-buffer)
1393   (cond
1394
1395    ;; This server does not talk NOV.
1396    ((not nntp-server-xover)
1397     nil)
1398
1399    ;; We don't care about gaps.
1400    ((or (not nntp-nov-gap)
1401         fetch-old)
1402     (nntp-send-xover-command
1403      (if fetch-old
1404          (if (numberp fetch-old)
1405              (max 1 (- (car articles) fetch-old))
1406            1)
1407        (car articles))
1408      (car (last articles)) 'wait)
1409
1410     (goto-char (point-min))
1411     (when (looking-at "[1-5][0-9][0-9] .*\n")
1412       (delete-region (point) (progn (forward-line 1) (point))))
1413     (while (search-forward "\r" nil t)
1414       (replace-match "" t t))
1415     (goto-char (point-max))
1416     (forward-line -1)
1417     (when (looking-at "\\.")
1418       (delete-region (point) (progn (forward-line 1) (point)))))
1419
1420    ;; We do it the hard way.  For each gap, an XOVER command is sent
1421    ;; to the server.  We do not wait for a reply from the server, we
1422    ;; just send them off as fast as we can.  That means that we have
1423    ;; to count the number of responses we get back to find out when we
1424    ;; have gotten all we asked for.
1425    ((numberp nntp-nov-gap)
1426     (let ((count 0)
1427           (received 0)
1428           last-point
1429           in-process-buffer-p
1430           (buf nntp-server-buffer)
1431           (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1432           first last status)
1433       ;; We have to check `nntp-server-xover'.  If it gets set to nil,
1434       ;; that means that the server does not understand XOVER, but we
1435       ;; won't know that until we try.
1436       (while (and nntp-server-xover articles)
1437         (setq first (car articles))
1438         ;; Search forward until we find a gap, or until we run out of
1439         ;; articles.
1440         (while (and (cdr articles)
1441                     (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1442           (setq articles (cdr articles)))
1443
1444         (setq in-process-buffer-p (stringp nntp-server-xover))
1445         (nntp-send-xover-command first (setq last (car articles)))
1446         (setq articles (cdr articles))
1447
1448         (when (and nntp-server-xover in-process-buffer-p)
1449           ;; Don't count tried request.
1450           (setq count (1+ count))
1451
1452           ;; Every 400 requests we have to read the stream in
1453           ;; order to avoid deadlocks.
1454           (when (or (null articles)     ;All requests have been sent.
1455                     (= 1 (% count nntp-maximum-request)))
1456
1457             (nntp-accept-response)
1458             ;; On some Emacs versions the preceding function has a
1459             ;; tendency to change the buffer.  Perhaps.  It's quite
1460             ;; difficult to reproduce, because it only seems to happen
1461             ;; once in a blue moon.
1462             (set-buffer process-buffer)
1463             (while (progn
1464                      (goto-char (or last-point (point-min)))
1465                      ;; Count replies.
1466                      (while (re-search-forward "^\\([0-9][0-9][0-9]\\) .*\n"
1467                                                nil t)
1468                        (incf received)
1469                        (setq status (match-string 1))
1470                        (if (string-match "^[45]" status)
1471                            (setq status 'error)
1472                          (setq status 'ok)))
1473                      (setq last-point (point))
1474                      (or (< received count)
1475                          (if (eq status 'error)
1476                              nil
1477                            ;; I haven't started reading the final response
1478                            (progn
1479                              (goto-char (point-max))
1480                              (forward-line -1)
1481                              (not (looking-at "^\\.\r?\n"))))))
1482               ;; I haven't read the end of the final response
1483               (nntp-accept-response)
1484               (set-buffer process-buffer))))
1485
1486         ;; Some nntp servers seem to have an extension to the XOVER
1487         ;; extension.  On these servers, requesting an article range
1488         ;; preceeding the active range does not return an error as
1489         ;; specified in the RFC.  What we instead get is the NOV entry
1490         ;; for the first available article.  Obviously, a client can
1491         ;; use that entry to avoid making unnecessary requests.  The
1492         ;; only problem is for a client that assumes that the response
1493         ;; will always be within the requested ranage.  For such a
1494         ;; client, we can get N copies of the same entry (one for each
1495         ;; XOVER command sent to the server).
1496
1497         (when (<= count 1)
1498           (goto-char (point-min))
1499           (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1500             (let ((low-limit (string-to-int
1501                               (buffer-substring (match-beginning 1) 
1502                                                 (match-end 1)))))
1503               (while (and articles (<= (car articles) low-limit))
1504                 (setq articles (cdr articles))))))
1505         (set-buffer buf))
1506
1507       (when nntp-server-xover
1508         (when in-process-buffer-p
1509           (set-buffer buf)
1510           (goto-char (point-max))
1511           (insert-buffer-substring process-buffer)
1512           (set-buffer process-buffer)
1513           (erase-buffer)
1514           (set-buffer buf))
1515
1516         ;; We remove any "." lines and status lines.
1517         (goto-char (point-min))
1518         (while (search-forward "\r" nil t)
1519           (delete-char -1))
1520         (goto-char (point-min))
1521         (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1522         t))))
1523
1524   nntp-server-xover)
1525
1526 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1527   "Send the XOVER command to the server."
1528   (let ((range (format "%d-%d" beg end))
1529         (nntp-inhibit-erase t))
1530     (if (stringp nntp-server-xover)
1531         ;; If `nntp-server-xover' is a string, then we just send this
1532         ;; command.
1533         (if wait-for-reply
1534             (nntp-send-command-nodelete
1535              "\r?\n\\.\r?\n" nntp-server-xover range)
1536           ;; We do not wait for the reply.
1537           (nntp-send-command-nodelete nil nntp-server-xover range))
1538       (let ((commands nntp-xover-commands))
1539         ;; `nntp-xover-commands' is a list of possible XOVER commands.
1540         ;; We try them all until we get at positive response.
1541         (while (and commands (eq nntp-server-xover 'try))
1542           (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1543           (save-excursion
1544             (set-buffer nntp-server-buffer)
1545             (goto-char (point-min))
1546             (and (looking-at "[23]")    ; No error message.
1547                  ;; We also have to look at the lines.  Some buggy
1548                  ;; servers give back simple lines with just the
1549                  ;; article number.  How... helpful.
1550                  (progn
1551                    (forward-line 1)
1552                    (looking-at "[0-9]+\t...")) ; More text after number.
1553                  (setq nntp-server-xover (car commands))))
1554           (setq commands (cdr commands)))
1555         ;; If none of the commands worked, we disable XOVER.
1556         (when (eq nntp-server-xover 'try)
1557           (nntp-erase-buffer nntp-server-buffer)
1558           (setq nntp-server-xover nil))
1559         nntp-server-xover))))
1560
1561 (defun nntp-find-group-and-number (&optional group)
1562   (save-excursion
1563     (save-restriction
1564       (set-buffer nntp-server-buffer)
1565       (narrow-to-region (goto-char (point-min))
1566                         (or (search-forward "\n\n" nil t) (point-max)))
1567       (goto-char (point-min))
1568       ;; We first find the number by looking at the status line.
1569       (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1570                          (string-to-int
1571                           (buffer-substring (match-beginning 1)
1572                                             (match-end 1)))))
1573             newsgroups xref)
1574         (and number (zerop number) (setq number nil))
1575         (if number
1576             ;; Then we find the group name.
1577             (setq group
1578                   (cond
1579                    ;; If there is only one group in the Newsgroups
1580                    ;; header, then it seems quite likely that this
1581                    ;; article comes from that group, I'd say.
1582                    ((and (setq newsgroups
1583                                (mail-fetch-field "newsgroups"))
1584                          (not (string-match "," newsgroups)))
1585                     newsgroups)
1586                    ;; If there is more than one group in the
1587                    ;; Newsgroups header, then the Xref header should
1588                    ;; be filled out.  We hazard a guess that the group
1589                    ;; that has this article number in the Xref header
1590                    ;; is the one we are looking for.  This might very
1591                    ;; well be wrong if this article happens to have
1592                    ;; the same number in several groups, but that's
1593                    ;; life.
1594                    ((and (setq xref (mail-fetch-field "xref"))
1595                          number
1596                          (string-match
1597                           (format "\\([^ :]+\\):%d" number) xref))
1598                     (match-string 1 xref))
1599                    (t "")))
1600           (cond
1601            ((and (setq xref (mail-fetch-field "xref"))
1602                  (string-match
1603                   (if group
1604                       (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1605                     "\\([^ :]+\\):\\([0-9]+\\)")
1606                   xref))
1607             (setq group (match-string 1 xref)
1608                   number (string-to-int (match-string 2 xref))))
1609            ((and (setq newsgroups
1610                        (mail-fetch-field "newsgroups"))
1611                  (not (string-match "," newsgroups)))
1612             (setq group newsgroups))
1613            (group)
1614            (t (setq group ""))))
1615         (when (string-match "\r" group)
1616           (setq group (substring group 0 (match-beginning 0))))
1617         (cons group number)))))
1618
1619 (defun nntp-wait-for-string (regexp)
1620   "Wait until string arrives in the buffer."
1621   (let ((buf (current-buffer))
1622         proc)
1623     (goto-char (point-min))
1624     (while (and (setq proc (get-buffer-process buf))
1625                 (memq (process-status proc) '(open run))
1626                 (not (re-search-forward regexp nil t)))
1627       (accept-process-output proc)
1628       (set-buffer buf)
1629       (goto-char (point-min)))))
1630
1631
1632 ;; ==========================================================================
1633 ;; Obsolete nntp-open-* connection methods -- drv
1634 ;; ==========================================================================
1635
1636 (defvoo nntp-open-telnet-envuser nil
1637   "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1638
1639 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
1640   "*Regular expression to match the shell prompt on the remote machine.")
1641
1642 (defvoo nntp-rlogin-program "rsh"
1643   "*Program used to log in on remote machines.
1644 The default is \"rsh\", but \"ssh\" is a popular alternative.")
1645
1646 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1647   "*Parameters to `nntp-open-rlogin'.
1648 That function may be used as `nntp-open-connection-function'.  In that
1649 case, this list will be used as the parameter list given to rsh.")
1650
1651 (defvoo nntp-rlogin-user-name nil
1652   "*User name on remote system when using the rlogin connect method.")
1653
1654 (defvoo nntp-telnet-parameters
1655     '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1656   "*Parameters to `nntp-open-telnet'.
1657 That function may be used as `nntp-open-connection-function'.  In that
1658 case, this list will be executed as a command after logging in
1659 via telnet.")
1660
1661 (defvoo nntp-telnet-user-name nil
1662   "User name to log in via telnet with.")
1663
1664 (defvoo nntp-telnet-passwd nil
1665   "Password to use to log in via telnet with.")
1666
1667 (defun nntp-open-telnet (buffer)
1668   (save-excursion
1669     (set-buffer buffer)
1670     (erase-buffer)
1671     (let ((proc (apply
1672                  'start-process
1673                  "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1674           (case-fold-search t))
1675       (when (memq (process-status proc) '(open run))
1676         (nntp-wait-for-string "^r?telnet")
1677         (process-send-string proc "set escape \^X\n")
1678         (cond
1679          ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1680           (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1681                                             nntp-address "\n")))
1682          (t
1683           (process-send-string proc (concat "open " nntp-address "\n"))))
1684         (cond
1685          ((not nntp-open-telnet-envuser)
1686           (nntp-wait-for-string "^\r*.?login:")
1687           (process-send-string
1688            proc (concat
1689                  (or nntp-telnet-user-name
1690                      (setq nntp-telnet-user-name (read-string "login: ")))
1691                  "\n"))))
1692         (nntp-wait-for-string "^\r*.?password:")
1693         (process-send-string
1694          proc (concat
1695                (or nntp-telnet-passwd
1696                    (setq nntp-telnet-passwd
1697                          (read-passwd "Password: ")))
1698                "\n"))
1699         (nntp-wait-for-string nntp-telnet-shell-prompt)
1700         (process-send-string
1701          proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1702         (nntp-wait-for-string "^\r*20[01]")
1703         (beginning-of-line)
1704         (delete-region (point-min) (point))
1705         (process-send-string proc "\^]")
1706         (nntp-wait-for-string "^r?telnet")
1707         (process-send-string proc "mode character\n")
1708         (accept-process-output proc 1)
1709         (sit-for 1)
1710         (goto-char (point-min))
1711         (forward-line 1)
1712         (delete-region (point) (point-max)))
1713       proc)))
1714
1715 (defun nntp-open-rlogin (buffer)
1716   "Open a connection to SERVER using rsh."
1717   (let ((proc (if nntp-rlogin-user-name
1718                   (apply 'start-process
1719                          "nntpd" buffer nntp-rlogin-program
1720                          nntp-address "-l" nntp-rlogin-user-name
1721                          nntp-rlogin-parameters)
1722                 (apply 'start-process
1723                        "nntpd" buffer nntp-rlogin-program nntp-address
1724                        nntp-rlogin-parameters))))
1725     (save-excursion
1726       (set-buffer buffer)
1727       (nntp-wait-for-string "^\r*20[01]")
1728       (beginning-of-line)
1729       (delete-region (point-min) (point))
1730       proc)))
1731
1732
1733 ;; ==========================================================================
1734 ;; Replacements for the nntp-open-* functions -- drv
1735 ;; ==========================================================================
1736
1737 (defun nntp-open-telnet-stream (buffer)
1738   "Open a nntp connection by telnet'ing the news server.
1739
1740 Please refer to the following variables to customize the connection:
1741 - `nntp-pre-command',
1742 - `nntp-telnet-command',
1743 - `nntp-telnet-switches',
1744 - `nntp-address',
1745 - `nntp-port-number',
1746 - `nntp-end-of-line'."
1747   (let ((command `(,nntp-telnet-command
1748                    ,@nntp-telnet-switches
1749                    ,nntp-address ,nntp-port-number))
1750         proc)
1751     (and nntp-pre-command
1752          (push nntp-pre-command command))
1753     (setq proc (apply 'start-process "nntpd" buffer command))
1754     (save-excursion
1755       (set-buffer buffer)
1756       (nntp-wait-for-string "^\r*20[01]")
1757       (beginning-of-line)
1758       (delete-region (point-min) (point))
1759       proc)))
1760
1761 (defun nntp-open-via-rlogin-and-telnet (buffer)
1762   "Open a connection to an nntp server through an intermediate host.
1763 First rlogin to the remote host, and then telnet the real news server
1764 from there.
1765
1766 Please refer to the following variables to customize the connection:
1767 - `nntp-pre-command',
1768 - `nntp-via-rlogin-command',
1769 - `nntp-via-rlogin-command-switches',
1770 - `nntp-via-user-name',
1771 - `nntp-via-address',
1772 - `nntp-telnet-command',
1773 - `nntp-telnet-switches',
1774 - `nntp-address',
1775 - `nntp-port-number',
1776 - `nntp-end-of-line'."
1777   (let ((command `(,nntp-via-address
1778                    ,nntp-telnet-command
1779                    ,@nntp-telnet-switches))
1780         proc)
1781     (when nntp-via-user-name
1782       (setq command `("-l" ,nntp-via-user-name ,@command)))
1783     (when nntp-via-rlogin-command-switches
1784       (setq command (append nntp-via-rlogin-command-switches command)))
1785     (push nntp-via-rlogin-command command)
1786     (and nntp-pre-command
1787          (push nntp-pre-command command))
1788     (setq proc (apply 'start-process "nntpd" buffer command))
1789     (save-excursion
1790       (set-buffer buffer)
1791       (nntp-wait-for-string "^r?telnet")
1792       (process-send-string proc (concat "open " nntp-address
1793                                         " " nntp-port-number "\n"))
1794       (nntp-wait-for-string "^\r*20[01]")
1795       (beginning-of-line)
1796       (delete-region (point-min) (point))
1797       (process-send-string proc "\^]")
1798       (nntp-wait-for-string "^r?telnet")
1799       (process-send-string proc "mode character\n")
1800       (accept-process-output proc 1)
1801       (sit-for 1)
1802       (goto-char (point-min))
1803       (forward-line 1)
1804       (delete-region (point) (point-max)))
1805     proc))
1806
1807 (defun nntp-open-via-rlogin-and-netcat (buffer)
1808   "Open a connection to an nntp server through an intermediate host.
1809 First rlogin to the remote host, and then connect to the real news
1810 server from there using the netcat command.
1811
1812 Please refer to the following variables to customize the connection:
1813 - `nntp-pre-command',
1814 - `nntp-via-rlogin-command',
1815 - `nntp-via-rlogin-command-switches',
1816 - `nntp-via-user-name',
1817 - `nntp-via-address',
1818 - `nntp-netcat-command',
1819 - `nntp-netcat-switches',
1820 - `nntp-address',
1821 - `nntp-port-number',
1822 - `nntp-end-of-line'."
1823   (let ((command `(,@(when nntp-pre-command
1824                        (list nntp-pre-command))
1825                    ,nntp-via-rlogin-command
1826                    ,@(when nntp-via-rlogin-command-switches
1827                        nntp-via-rlogin-command-switches)
1828                    ,@(when nntp-via-user-name
1829                        (list "-l" nntp-via-user-name))
1830                    ,nntp-via-address
1831                    ,nntp-netcat-command
1832                    ,@nntp-netcat-switches
1833                    ,nntp-address
1834                    ,nntp-port-number)))
1835     (apply 'start-process "nntpd" buffer command)))
1836
1837 (defun nntp-open-via-telnet-and-telnet (buffer)
1838   "Open a connection to an nntp server through an intermediate host.
1839 First telnet the remote host, and then telnet the real news server
1840 from there.
1841
1842 Please refer to the following variables to customize the connection:
1843 - `nntp-pre-command',
1844 - `nntp-via-telnet-command',
1845 - `nntp-via-telnet-switches',
1846 - `nntp-via-address',
1847 - `nntp-via-envuser',
1848 - `nntp-via-user-name',
1849 - `nntp-via-user-password',
1850 - `nntp-via-shell-prompt',
1851 - `nntp-telnet-command',
1852 - `nntp-telnet-switches',
1853 - `nntp-address',
1854 - `nntp-port-number',
1855 - `nntp-end-of-line'."
1856   (save-excursion
1857     (set-buffer buffer)
1858     (erase-buffer)
1859     (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
1860           (case-fold-search t)
1861           proc)
1862       (and nntp-pre-command (push nntp-pre-command command))
1863       (setq proc (apply 'start-process "nntpd" buffer command))
1864       (when (memq (process-status proc) '(open run))
1865         (nntp-wait-for-string "^r?telnet")
1866         (process-send-string proc "set escape \^X\n")
1867         (cond
1868          ((and nntp-via-envuser nntp-via-user-name)
1869           (process-send-string proc (concat "open " "-l" nntp-via-user-name
1870                                             nntp-via-address "\n")))
1871          (t
1872           (process-send-string proc (concat "open " nntp-via-address
1873                                             "\n"))))
1874         (when (not nntp-via-envuser)
1875           (nntp-wait-for-string "^\r*.?login:")
1876           (process-send-string proc
1877                                (concat
1878                                 (or nntp-via-user-name
1879                                     (setq nntp-via-user-name
1880                                           (read-string "login: ")))
1881                                 "\n")))
1882         (nntp-wait-for-string "^\r*.?password:")
1883         (process-send-string proc
1884                              (concat
1885                               (or nntp-via-user-password
1886                                   (setq nntp-via-user-password
1887                                         (read-passwd "Password: ")))
1888                               "\n"))
1889         (nntp-wait-for-string nntp-via-shell-prompt)
1890         (let ((real-telnet-command `("exec"
1891                                      ,nntp-telnet-command
1892                                      ,@nntp-telnet-switches
1893                                      ,nntp-address
1894                                      ,nntp-port-number)))
1895           (process-send-string proc
1896                                (concat (mapconcat 'identity
1897                                                   real-telnet-command " ")
1898                                        "\n")))
1899         (nntp-wait-for-string "^\r*20[01]")
1900         (beginning-of-line)
1901         (delete-region (point-min) (point))
1902         (process-send-string proc "\^]")
1903         (nntp-wait-for-string "^r?telnet")
1904         (process-send-string proc "mode character\n")
1905         (accept-process-output proc 1)
1906         (sit-for 1)
1907         (goto-char (point-min))
1908         (forward-line 1)
1909         (delete-region (point) (point-max)))
1910       proc)))
1911
1912 (provide 'nntp)
1913
1914 ;;; nntp.el ends here