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