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