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