*** empty log message ***
[gnus] / lisp / nntp.el
1 ;;; nntp.el --- nntp access for Gnus
2 ;; Copyright (C) 1987,88,89,90,92,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'nnheader)
28 (require 'nnoo)
29 (require 'gnus-util)
30
31 (nnoo-declare nntp)
32
33 (eval-and-compile
34   (unless (fboundp 'open-network-stream)
35     (require 'tcp)))
36
37 (eval-when-compile (require 'cl))
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-server-opened-hook nil
46   "*Hook used for sending commands to the server at startup.  
47 The default value is `nntp-send-mode-reader', which makes an innd
48 server spawn an nnrpd server.  Another useful function to put in this
49 hook might be `nntp-send-authinfo', which will prompt for a password
50 to allow posting from the server.  Note that this is only necessary to
51 do on servers that use strict access control.")  
52 (add-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)
53
54 (defvoo nntp-server-action-alist 
55   '(("nntpd 1\\.5\\.11t" 
56      (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)))
57   "Alist of regexps to match on server types and actions to be taken.
58 For instance, if you want Gnus to beep every time you connect
59 to innd, you could say something like:
60
61 \(setq nntp-server-action-alist
62        '((\"innd\" (ding))))
63
64 You probably don't want to do that, though.")
65
66 (defvoo nntp-open-connection-function 'nntp-open-network-stream
67   "*Function used for connecting to a remote system.
68 It will be called with the address of the remote system.
69
70 Two pre-made functions are `nntp-open-network-stream', which is the
71 default, and simply connects to some port or other on the remote
72 system (see nntp-port-number).  The other is `nntp-open-rlogin', which
73 does an rlogin on the remote system, and then does a telnet to the
74 NNTP server available there (see nntp-rlogin-parameters).")
75
76 (defvoo nntp-rlogin-parameters '("telnet" "${NNTPSERVER:=localhost}" "nntp")
77   "*Parameters to `nntp-open-login'.
78 That function may be used as `nntp-open-server-function'.  In that
79 case, this list will be used as the parameter list given to rsh.")
80
81 (defvoo nntp-rlogin-user-name nil
82   "*User name on remote system when using the rlogin connect method.")
83
84 (defvoo nntp-telnet-parameters '("exec" "telnet" "-8" "${NNTPSERVER:=localhost}" "nntp")
85   "*Parameters to `nntp-open-telnet'.
86 That function may be used as `nntp-open-server-function'.  In that
87 case, this list will be executed as a command after logging in
88 via telnet.")
89
90 (defvoo nntp-telnet-user-name nil
91   "User name to log in via telnet with.")
92
93 (defvoo nntp-telnet-passwd nil
94   "Password to use to log in via telnet with.")
95
96 (defvoo nntp-end-of-line "\r\n"
97   "String to use on the end of lines when talking to the NNTP server.
98 This is \"\\r\\n\" by default, but should be \"\\n\" when
99 using rlogin or telnet to communicate with the server.")
100
101 (defvoo nntp-large-newsgroup 50
102   "*The number of the articles which indicates a large newsgroup.
103 If the number of the articles is greater than the value, verbose
104 messages will be shown to indicate the current status.")
105
106 (defvoo nntp-maximum-request 400
107   "*The maximum number of the requests sent to the NNTP server at one time.
108 If Emacs hangs up while retrieving headers, set the variable to a
109 lower value.")
110
111 (defvoo nntp-nov-is-evil nil
112   "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
113
114 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
115   "*List of strings that are used as commands to fetch NOV lines from a server.
116 The strings are tried in turn until a positive response is gotten. If
117 none of the commands are successful, nntp will just grab headers one
118 by one.")
119
120 (defvoo nntp-nov-gap 20
121   "*Maximum allowed gap between two articles.
122 If the gap between two consecutive articles is bigger than this
123 variable, split the XOVER request into two requests.")
124
125 (defvoo nntp-connection-timeout nil
126   "*Number of seconds to wait before an nntp connection times out.
127 If this variable is nil, which is the default, no timers are set.")
128
129 (defvoo nntp-prepare-server-hook nil
130   "*Hook run before a server is opened.
131 If can be used to set up a server remotely, for instance.  Say you
132 have an account at the machine \"other.machine\".  This machine has
133 access to an NNTP server that you can't access locally.  You could
134 then use this hook to rsh to the remote machine and start a proxy NNTP
135 server there that you can connect to.")
136
137 (defvoo nntp-warn-about-losing-connection t
138   "*If non-nil, beep when a server closes connection.")
139
140 \f
141
142 ;;; Internal variables.
143
144 (defvoo nntp-server-type nil)
145 (defvar nntp-connection-alist nil)
146 (defvar nntp-status-string "")
147 (defconst nntp-version "nntp 5.0")
148 (defvar nntp-inhibit-erase nil)
149
150 (defvar nntp-server-xover 'try)
151 (defvar nntp-server-list-active-group 'try)
152
153 \f
154
155 ;;; Interface functions.
156
157 (nnoo-define-basics nntp)
158
159 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
160   "Retrieve the headers of ARTICLES."
161   (nntp-possibly-change-group group server)
162   (save-excursion
163     (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
164     (erase-buffer)
165     (if (and (not gnus-nov-is-evil) 
166              (not nntp-nov-is-evil)
167              (nntp-retrieve-headers-with-xover articles fetch-old))
168         ;; We successfully retrieved the headers via XOVER.
169         'nov
170       ;; XOVER didn't work, so we do it the hard, slow and inefficient
171       ;; way.  
172       (let ((number (length articles))
173             (count 0)
174             (received 0)
175             (last-point (point-min)))
176         ;; Send HEAD command.
177         (while articles
178           (nntp-send-command 
179            nil
180            "HEAD" (if (numberp (car articles)) 
181                       (int-to-string (car articles))
182                     ;; `articles' is either a list of article numbers
183                     ;; or a list of article IDs.
184                     (car articles)))
185           (setq articles (cdr articles)
186                 count (1+ count))
187           ;; Every 400 header requests we have to read the stream in
188           ;; order to avoid deadlocks.
189           (when (or (null articles)     ;All requests have been sent.
190                     (zerop (% count nntp-maximum-request)))
191             (nntp-accept-response)
192             (while (progn
193                      (goto-char last-point)
194                      ;; Count replies.
195                      (while (re-search-forward "^[0-9]" nil t)
196                        (setq received (1+ received)))
197                      (setq last-point (point))
198                      (< received count))
199               ;; If number of headers is greater than 100, give
200               ;;  informative messages.
201               (and (numberp nntp-large-newsgroup)
202                    (> number nntp-large-newsgroup)
203                    (zerop (% received 20))
204                    (message "NNTP: Receiving headers... %d%%"
205                             (/ (* received 100) number)))
206               (nntp-accept-response))))
207         ;; Wait for text of last command.
208         (goto-char (point-max))
209         (re-search-backward "^[0-9]" nil t)
210         (when (looking-at "^[23]")
211           (while (progn
212                    (goto-char (- (point-max) 3))
213                    (not (looking-at "^\\.\r?\n")))
214             (nntp-accept-response)))
215         (and (numberp nntp-large-newsgroup)
216              (> number nntp-large-newsgroup)
217              (message "NNTP: Receiving headers...done"))
218
219         ;; Now all of replies are received.  Fold continuation lines.
220         (nnheader-fold-continuation-lines)
221         ;; Remove all "\r"'s.
222         (goto-char (point-min))
223         (while (search-forward "\r" nil t)
224           (replace-match "" t t))
225         (copy-to-buffer nntp-server-buffer (point-min) (point-max))
226         'headers))))
227
228 (deffoo nntp-request-article (article &optional group server buffer)
229   (nntp-possibly-change-group group server)
230   (when (nntp-send-command-and-decode
231          "\r\n\\.\r\n" "ARTICLE"
232          (if (numberp article) (int-to-string article) article))
233     (when buffer
234       (save-excursion
235         (set-buffer nntp-server-buffer)
236         (copy-to-buffer buffer (point-min) (point-max)))
237       t)))
238
239 (deffoo nntp-request-body (article &optional group server)
240   (nntp-possibly-change-group group server)
241   (nntp-send-command
242    "\r\n\\.\r\n" "BODY"
243    (if (numberp article) (int-to-string article) article)))
244
245 (deffoo nntp-request-group (group &optional server dont-check)
246   (nntp-possibly-change-group nil server)
247   (when (nntp-send-command "^2.*\r\n" "GROUP" group)
248     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
249       (setcar (cddr entry) group))))
250
251 (deffoo nntp-close-group (group &optional server)
252   t)
253
254 (deffoo nntp-server-opened (&optional server)
255   "Say whether a connection to SERVER has been opened."
256   (and (nnoo-current-server-p 'nntp server)
257        nntp-server-buffer
258        (gnus-buffer-live-p nntp-server-buffer)
259        (nntp-find-connection nntp-server-buffer)))
260
261 (deffoo nntp-open-server (server &optional defs connectionless)
262   (nnheader-init-server-buffer)
263   (if (nntp-server-opened server)
264       t
265     (when (or (stringp (car defs))
266               (numberp (car defs)))
267       (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
268     (unless (assq 'nntp-address defs)
269       (setq defs (append defs (list (list 'nntp-address server)))))
270     (nnoo-change-server 'nntp server defs)
271     (unless connectionless
272       (or (nntp-find-connection nntp-server-buffer)
273           (nntp-open-connection nntp-server-buffer)))))
274
275 (deffoo nntp-close-server (&optional server)
276   (nntp-possibly-change-group nil server t)
277   (let (process)
278     (while (setq process (car (pop nntp-connection-alist)))
279       (when (memq (process-status process) '(open run))
280         (set-process-sentinel process nil)
281         (set-process-filter process nil)
282         (nntp-send-string process "QUIT"))
283       (when (buffer-name (process-buffer process))
284         (kill-buffer (process-buffer process))))
285     (nnoo-close-server 'nntp)))
286
287 (deffoo nntp-request-list (&optional server)
288   (nntp-possibly-change-group nil server)
289   (nntp-send-command "\r\n\\.\r\n" "LIST"))
290
291 (deffoo nntp-request-list-newsgroups (&optional server)
292   (nntp-possibly-change-group nil server)
293   (nntp-send-command "\r\n\\.\r\n" "LIST NEWSGROUPS"))
294
295 (deffoo nntp-request-newgroups (date &optional server)
296   (nntp-possibly-change-group nil server)
297   (let* ((date (timezone-parse-date date))
298          (time-string
299           (format "%s%02d%02d %s%s%s"
300                   (substring (aref date 0) 2) (string-to-int (aref date 1)) 
301                   (string-to-int (aref date 2)) (substring (aref date 3) 0 2)
302                   (substring 
303                    (aref date 3) 3 5) (substring (aref date 3) 6 8))))
304     (prog1
305         (nntp-send-command "^\\.\r?\n" "NEWGROUPS" time-string)
306       (nntp-decode-text))))
307
308
309 (deffoo nntp-asynchronous-p ()
310   t)
311   
312 ;;; Hooky functions.
313
314 (defun nntp-send-mode-reader ()
315   "Send the MODE READER command to the nntp server.
316 This function is supposed to be called from `nntp-server-opened-hook'.
317 It will make innd servers spawn an nnrpd process to allow actual article
318 reading."
319   (nntp-send-command "^.*\r\n" "MODE READER"))
320
321 (defun nntp-send-nosy-authinfo ()
322   "Send the AUTHINFO to the nntp server.
323 This function is supposed to be called from `nntp-server-opened-hook'.
324 It will prompt for a password."
325   (nntp-send-command "^.*\r\n" "AUTHINFO USER"
326                      (read-string "NNTP user name: "))
327   (nntp-send-command "^.*\r\n" "AUTHINFO PASS" 
328                      (read-string "NNTP password: ")))
329
330 (defun nntp-send-authinfo ()
331   "Send the AUTHINFO to the nntp server.
332 This function is supposed to be called from `nntp-server-opened-hook'.
333 It will prompt for a password."
334   (nntp-send-command "^.*\r\n" "AUTHINFO USER" (user-login-name))
335   (nntp-send-command "^.*\r\n" "AUTHINFO PASS" 
336                      (read-string "NNTP password: ")))
337
338 (defun nntp-send-authinfo-from-file ()
339   "Send the AUTHINFO to the nntp server.
340 This function is supposed to be called from `nntp-server-opened-hook'.
341 It will prompt for a password."
342   (when (file-exists-p "~/.nntp-authinfo")
343     (save-excursion
344       (set-buffer (get-buffer-create " *authinfo*"))
345       (buffer-disable-undo (current-buffer))
346       (erase-buffer)
347       (insert-file-contents "~/.nntp-authinfo")
348       (goto-char (point-min))
349       (nntp-send-command "^.*\r\n" "AUTHINFO USER" (user-login-name))
350       (nntp-send-command 
351        "^.*\r\n" "AUTHINFO PASS" 
352        (buffer-substring (point) (progn (end-of-line) (point))))
353       (kill-buffer (current-buffer)))))
354
355 ;;; Internal functions.
356
357 (defun nntp-send-command (wait-for &rest strings)
358   "Send STRINGS to server and wait until WAIT-FOR returns."
359   (unless nnheader-callback-function
360     (save-excursion
361       (set-buffer nntp-server-buffer)
362       (erase-buffer)))
363   (nntp-retrieve-data
364    (mapconcat 'identity strings " ") 
365    nntp-address nntp-port-number nntp-server-buffer
366    wait-for nnheader-callback-function))
367
368 (defun nntp-send-command-and-decode (wait-for &rest strings)
369   "Send STRINGS to server and wait until WAIT-FOR returns."
370   (unless nnheader-callback-function
371     (save-excursion
372       (set-buffer nntp-server-buffer)
373       (erase-buffer)))
374   (nntp-retrieve-data
375    (mapconcat 'identity strings " ") 
376    nntp-address nntp-port-number nntp-server-buffer
377    wait-for nnheader-callback-function t))
378
379 (defun nntp-find-connection (buffer)
380   "Find the connection delivering to BUFFER."
381   (let ((alist nntp-connection-alist)
382         (buffer (if (stringp buffer) (get-buffer buffer) buffer))
383         process entry)
384     (while (setq entry (pop alist))
385       (when (eq buffer (cadr entry))
386         (setq process (car entry)
387               alist nil)))
388     (when process
389       (if (memq (process-status process) '(open run))
390           process
391         (when (buffer-name (process-buffer process))
392           (kill-buffer (process-buffer process)))
393         (setq nntp-connection-alist (delq entry nntp-connection-alist))
394         nil))))
395
396 (defun nntp-find-connection-entry (buffer)
397   "Return the entry for the connection to BUFFER."
398   (assq (nntp-find-connection buffer) nntp-connection-alist))
399
400 (defun nntp-open-connection (buffer)
401   "Open a connection to PORT on ADDRESS delivering output to BUFFER."
402   (run-hooks 'nntp-prepare-server-hook)
403   (let* ((pbuffer (save-excursion
404                     (set-buffer 
405                      (generate-new-buffer
406                       (format " *server %s %s %s*"
407                               nntp-address nntp-port-number
408                               (buffer-name (get-buffer buffer)))))
409                     (buffer-disable-undo (current-buffer))
410                     (current-buffer)))
411          (process (funcall nntp-open-connection-function pbuffer)))
412     (when process
413       (process-kill-without-query process)
414       (nntp-wait-for process "^.*\r\n" buffer)
415       (if (memq (process-status process) '(open run))
416           (prog1
417               (caar (push (list process buffer nil) 
418                           nntp-connection-alist))
419             (nntp-read-server-type)
420             (run-hooks 'nntp-server-opened-hook))
421         (when (buffer-name (process-buffer process))
422           (kill-buffer (process-buffer process)))
423         nil))))
424
425 (defun nntp-open-network-stream (buffer)
426   (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
427
428 (defun nntp-read-server-type ()
429   "Find out what the name of the server we have connected to is."
430   ;; Wait for the status string to arrive.
431   (setq nntp-server-type (buffer-string))
432   (let ((alist nntp-server-action-alist)
433         entry)
434     ;; Run server-specific commmands.
435     (while alist
436       (setq entry (pop alist))
437       (when (string-match (car entry) nntp-server-type)
438         (if (and (listp (cadr entry))
439                  (not (eq 'lambda (caadr entry))))
440             (eval (cadr entry))
441           (funcall (cadr entry)))))))
442
443 (defvar nntp-tmp-first)
444 (defvar nntp-tmp-wait-for)
445 (defvar nntp-tmp-callback)
446 (defvar nntp-tmp-buffer)
447
448 (defun nntp-make-process-filter (wait-for callback buffer decode)
449   `(lambda (proc string)
450      (let ((nntp-tmp-wait-for ,wait-for)
451            (nntp-tmp-callback ,callback)
452            (nntp-tmp-buffer ,buffer))
453        (nntp-process-filter proc string))))
454
455 (defun nntp-process-filter (proc string)
456   (let ((old-buffer (current-buffer)))
457     (unwind-protect
458         (let (point)
459           (set-buffer (process-buffer proc))
460           ;; Insert the text, moving the process-marker.
461           (setq point (goto-char (process-mark proc)))
462           (insert string)
463           (set-marker (process-mark proc) (point))
464           (if (and (= point (point-min))
465                    (string-match "^45" string))
466               (progn
467                 (nntp-snarf-error-message)
468                 (set-process-filter proc nil)
469                 (funcall nntp-tmp-callback nil))
470             (setq nntp-tmp-first nil)
471             (if (re-search-backward nntp-tmp-wait-for nil t)
472                 (progn
473                   (if (buffer-name (get-buffer nntp-tmp-buffer))
474                       (save-excursion
475                         (set-buffer (get-buffer nntp-tmp-buffer))
476                         (insert-buffer-substring (process-buffer proc))))
477                   (set-process-filter proc nil)
478                   (erase-buffer)
479                   (funcall nntp-tmp-callback t)))))
480       (set-buffer old-buffer))))
481
482 (defun nntp-retrieve-data (command address port buffer
483                                    &optional wait-for callback decode)
484   "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
485   (let ((process (or (nntp-find-connection buffer)
486                      (nntp-open-connection buffer))))
487     (if (not process)
488         (nnheader-report 'nntp "Couldn't open connection to %a" address)
489       (unless nntp-inhibit-erase
490         (save-excursion
491           (set-buffer (process-buffer process))
492           (erase-buffer)))
493       (nntp-send-string process command)
494       (cond 
495        ((eq callback 'ignore)
496         t)
497        ((and callback wait-for)
498         (set-process-filter
499          process (nntp-make-process-filter wait-for callback buffer decode))
500         t)
501        (wait-for 
502         (set-process-filter process nil)
503         (nntp-wait-for process wait-for buffer decode))
504        (t t)))))
505
506 (defun nntp-send-string (process string)
507   "Send STRING to PROCESS."
508   (process-send-string process (concat string nntp-end-of-line)))
509
510 (defun nntp-wait-for (process wait-for buffer &optional decode)
511   "Wait for WAIT-FOR to arrive from PROCESS."
512   (save-excursion
513     (set-buffer (process-buffer process))
514     (goto-char (point-min))
515     (while (not (looking-at "[2345]"))
516       (nntp-accept-process-output process)
517       (goto-char (point-min)))
518     (prog1
519         (if (looking-at "[345]")
520             (progn
521               (nntp-snarf-error-message)
522               nil)
523           (goto-char (point-max))
524           (while (not (re-search-backward wait-for nil t))
525             (nntp-accept-process-output process)
526             (goto-char (point-max)))
527           (nntp-decode-text (not decode))
528           (save-excursion
529             (set-buffer buffer)
530             (insert-buffer-substring (process-buffer process))
531             t))
532       (erase-buffer))))
533
534 (defun nntp-snarf-error-message ()
535   "Save the error message in the current buffer."
536   (setq nntp-status-string (buffer-string)))
537
538 (defun nntp-accept-process-output (process)
539   "Wait for output from PROCESS and message some dots."
540   (save-excursion
541     (set-buffer nntp-server-buffer)
542     (message "nntp reading%s" (make-string (/ (point-max) 100) ?.))
543     (accept-process-output process 1)))
544
545 (defun nntp-accept-response ()
546   "Wait for output from the process that outputs to BUFFER."
547   (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
548
549 (defun nntp-possibly-change-group (group server &optional connectionless)
550   (when server
551     (or (nntp-server-opened server)
552         (nntp-open-server server nil connectionless)))
553
554   (unless connectionless
555     (or (nntp-find-connection nntp-server-buffer)
556         (nntp-open-connection nntp-server-buffer)))
557
558   (when group
559     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
560       (when (not (equal group (caddr entry)))
561         (nntp-request-group group)))))
562
563 (defun nntp-decode-text (&optional cr-only)
564   "Decode the text in the current buffer."
565   (goto-char (point-min))
566   (while (search-forward "\r" nil t)
567     (delete-char -1))
568   (unless cr-only
569     (goto-char (point-max))
570     (forward-line -1)
571     (when (looking-at ".\n")
572       (delete-char 2))
573     (goto-char (point-min))
574     (delete-region (point) (progn (forward-line 1) (point)))
575     (while (search-forward "\n.." nil t)
576       (delete-char -1))))
577
578 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
579   (erase-buffer)
580   (cond 
581
582    ;; This server does not talk NOV.
583    ((not nntp-server-xover)
584     nil)
585
586    ;; We don't care about gaps.
587    ((or (not nntp-nov-gap)
588         fetch-old)
589     (nntp-send-xover-command 
590      (if fetch-old
591          (if (numberp fetch-old) 
592              (max 1 (- (car articles) fetch-old)) 
593            1)
594        (car articles))
595      (last articles) 'wait)
596
597     (goto-char (point-min))
598     (when (looking-at "[1-5][0-9][0-9] ")
599       (delete-region (point) (progn (forward-line 1) (point))))
600     (while (search-forward "\r" nil t)
601       (replace-match "" t t))
602     (goto-char (point-max))
603     (forward-line -1)
604     (when (looking-at "\\.")
605       (delete-region (point) (progn (forward-line 1) (point)))))
606
607    ;; We do it the hard way.  For each gap, an XOVER command is sent
608    ;; to the server.  We do not wait for a reply from the server, we
609    ;; just send them off as fast as we can.  That means that we have
610    ;; to count the number of responses we get back to find out when we
611    ;; have gotten all we asked for.
612    ((numberp nntp-nov-gap)
613     (let ((count 0)
614           (received 0)
615           (last-point (point-min))
616           (buf nntp-server-buffer)
617           ;;(process-buffer (nntp-find-connection (current-buffer))))
618           first)
619       ;; We have to check `nntp-server-xover'.  If it gets set to nil,
620       ;; that means that the server does not understand XOVER, but we
621       ;; won't know that until we try.
622       (while (and nntp-server-xover articles)
623         (setq first (car articles))
624         ;; Search forward until we find a gap, or until we run out of
625         ;; articles. 
626         (while (and (cdr articles) 
627                     (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
628           (setq articles (cdr articles)))
629
630         (when (nntp-send-xover-command first (car articles))
631           (setq articles (cdr articles)
632                 count (1+ count))
633
634           ;; Every 400 requests we have to read the stream in
635           ;; order to avoid deadlocks.
636           (when (or (null articles)     ;All requests have been sent.
637                     (zerop (% count nntp-maximum-request)))
638             (accept-process-output)
639             ;; On some Emacs versions the preceding function has
640             ;; a tendency to change the buffer. Perhaps. It's
641             ;; quite difficult to reproduce, because it only
642             ;; seems to happen once in a blue moon. 
643             (set-buffer buf) 
644             (while (progn
645                      (goto-char last-point)
646                      ;; Count replies.
647                      (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
648                        (setq received (1+ received)))
649                      (setq last-point (point))
650                      (< received count))
651               (accept-process-output)
652               (set-buffer buf)))))
653
654       (when nntp-server-xover
655         ;; Wait for the reply from the final command.
656         (goto-char (point-max))
657         (re-search-backward "^[0-9][0-9][0-9] " nil t)
658         (when (looking-at "^[23]")
659           (while (progn
660                    (goto-char (point-max))
661                    (forward-line -1)
662                    (not (looking-at "^\\.\r?\n")))
663             (nntp-accept-response)))
664         
665         ;; We remove any "." lines and status lines.
666         (goto-char (point-min))
667         (while (search-forward "\r" nil t)
668           (delete-char -1))
669         (goto-char (point-min))
670         (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
671         ;(save-excursion
672         ;  (set-buffer nntp-server-buffer)
673         ;  (insert-buffer-substring buf))
674         ;(erase-buffer)
675         ))))
676
677   nntp-server-xover)
678
679 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
680   "Send the XOVER command to the server."
681   (let ((range (format "%d-%d" beg end))
682         (nntp-inhibit-erase t))
683     (if (stringp nntp-server-xover)
684         ;; If `nntp-server-xover' is a string, then we just send this
685         ;; command.
686         (if wait-for-reply
687             (nntp-send-command "\r\n\\.\r\n" nntp-server-xover range)
688           ;; We do not wait for the reply.
689           (nntp-send-command "\r\n\\.\r\n" nntp-server-xover range))
690       (let ((commands nntp-xover-commands))
691         ;; `nntp-xover-commands' is a list of possible XOVER commands.
692         ;; We try them all until we get at positive response. 
693         (while (and commands (eq nntp-server-xover 'try))
694           (nntp-send-command "\r\n\\.\r\n" (car commands) range)
695           (save-excursion
696             (set-buffer nntp-server-buffer)
697             (goto-char (point-min))
698             (and (looking-at "[23]") ; No error message.
699                  ;; We also have to look at the lines.  Some buggy
700                  ;; servers give back simple lines with just the
701                  ;; article number.  How... helpful.
702                  (progn
703                    (forward-line 1)
704                    (looking-at "[0-9]+\t...")) ; More text after number.
705                  (setq nntp-server-xover (car commands))))
706           (setq commands (cdr commands)))
707         ;; If none of the commands worked, we disable XOVER.
708         (when (eq nntp-server-xover 'try)
709           (save-excursion
710             (set-buffer nntp-server-buffer)
711             (erase-buffer)
712             (setq nntp-server-xover nil)))
713         nntp-server-xover))))
714
715 ;;; Alternative connection methods.
716
717 (defun nntp-wait-for-string (regexp)
718   "Wait until string arrives in the buffer."
719   (let ((buf (current-buffer)))
720     (goto-char (point-min))
721     (while (not (re-search-forward regexp nil t))
722       (accept-process-output (nntp-find-connection nntp-server-buffer))
723       (set-buffer buf)
724       (goto-char (point-min)))))
725
726 (defun nntp-open-telnet (server)
727   (save-excursion
728     (set-buffer nntp-server-buffer)
729     (erase-buffer)
730     (let ((proc (start-process
731                  "nntpd" nntp-server-buffer "telnet" "-8"))
732           (case-fold-search t))
733       (when (memq (process-status proc) '(open run))
734         (process-send-string proc "set escape \^X\n")
735         (process-send-string proc (concat "open " server "\n"))
736         (nntp-wait-for-string "^\r*.?login:")
737         (process-send-string
738          proc (concat
739                (or nntp-telnet-user-name
740                    (setq nntp-telnet-user-name (read-string "login: ")))
741                "\n"))
742         (nntp-wait-for-string "^\r*.?password:")
743         (process-send-string
744          proc (concat
745                (or nntp-telnet-passwd
746                    (setq nntp-telnet-passwd
747                          (nnmail-read-passwd "Password: ")))
748                "\n"))
749         (erase-buffer)
750         (nntp-wait-for-string "bash\\|\$ *\r?$\\|> *\r?")
751         (process-send-string
752          proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
753         (nntp-wait-for-string "^\r*200")
754         (beginning-of-line)
755         (delete-region (point-min) (point))
756         (process-send-string proc "\^]")
757         (nntp-wait-for-string "^telnet")
758         (process-send-string proc "mode character\n")
759         (accept-process-output proc 1)
760         (sit-for 1)
761         (goto-char (point-min))
762         (forward-line 1)
763         (delete-region (point) (point-max)))
764       proc)))
765
766 (defun nntp-open-rlogin (server)
767   "Open a connection to SERVER using rsh."
768   (let ((proc (if nntp-rlogin-user-name
769                   (start-process
770                    "nntpd" nntp-server-buffer "rsh"
771                    server "-l" nntp-rlogin-user-name
772                    (mapconcat 'identity
773                               nntp-rlogin-parameters " "))
774                 (start-process
775                  "nntpd" nntp-server-buffer "rsh" server
776                  (mapconcat 'identity
777                             nntp-rlogin-parameters " ")))))
778     proc))
779
780 (defun nntp-find-connection-buffer (buffer)
781   "Return the process connection buffer tied to BUFFER."
782   (let ((process (nntp-find-connection buffer)))
783     (when process
784       (process-buffer process))))
785
786 (provide 'nntp)
787
788 ;;; nntp.el ends here