*** 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 (defvoo nntp-connection-alist nil)
146 (defvoo nntp-status-string "")
147 (defconst nntp-version "nntp 5.0")
148 (defvoo nntp-inhibit-erase nil)
149
150 (defvoo nntp-server-xover 'try)
151 (defvoo 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-and-decode
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   (prog1 (nntp-send-command "\r\n\\.\r\n" "LIST")
290     (nntp-decode-text t)))
291
292 (deffoo nntp-request-list-newsgroups (&optional server)
293   (nntp-possibly-change-group nil server)
294   (prog1 (nntp-send-command "\r\n\\.\r\n" "LIST NEWSGROUPS")
295     (nntp-decode-text t)))
296
297 (deffoo nntp-request-newgroups (date &optional server)
298   (nntp-possibly-change-group nil server)
299   (let* ((date (timezone-parse-date date))
300          (time-string
301           (format "%s%02d%02d %s%s%s"
302                   (substring (aref date 0) 2) (string-to-int (aref date 1)) 
303                   (string-to-int (aref date 2)) (substring (aref date 3) 0 2)
304                   (substring 
305                    (aref date 3) 3 5) (substring (aref date 3) 6 8))))
306     (prog1
307         (nntp-send-command "^\\.\r?\n" "NEWGROUPS" time-string)
308       (nntp-decode-text))))
309
310 (deffoo nntp-asynchronous-p ()
311   t)
312
313 (deffoo nntp-request-post (&optional server)
314   (nntp-possibly-change-group nil server)
315   (when (nntp-send-command "^[23].*\r?\n" "POST")
316     (nntp-send-buffer "^[23].*\n")))
317   
318 ;;; Hooky functions.
319
320 (defun nntp-send-mode-reader ()
321   "Send the MODE READER command to the nntp server.
322 This function is supposed to be called from `nntp-server-opened-hook'.
323 It will make innd servers spawn an nnrpd process to allow actual article
324 reading."
325   (nntp-send-command "^.*\r\n" "MODE READER"))
326
327 (defun nntp-send-nosy-authinfo ()
328   "Send the AUTHINFO to the nntp server.
329 This function is supposed to be called from `nntp-server-opened-hook'.
330 It will prompt for a password."
331   (nntp-send-command "^.*\r\n" "AUTHINFO USER"
332                      (read-string "NNTP user name: "))
333   (nntp-send-command "^.*\r\n" "AUTHINFO PASS" 
334                      (read-string "NNTP password: ")))
335
336 (defun nntp-send-authinfo ()
337   "Send the AUTHINFO to the nntp server.
338 This function is supposed to be called from `nntp-server-opened-hook'.
339 It will prompt for a password."
340   (nntp-send-command "^.*\r\n" "AUTHINFO USER" (user-login-name))
341   (nntp-send-command "^.*\r\n" "AUTHINFO PASS" 
342                      (read-string "NNTP password: ")))
343
344 (defun nntp-send-authinfo-from-file ()
345   "Send the AUTHINFO to the nntp server.
346 This function is supposed to be called from `nntp-server-opened-hook'.
347 It will prompt for a password."
348   (when (file-exists-p "~/.nntp-authinfo")
349     (save-excursion
350       (set-buffer (get-buffer-create " *authinfo*"))
351       (buffer-disable-undo (current-buffer))
352       (erase-buffer)
353       (insert-file-contents "~/.nntp-authinfo")
354       (goto-char (point-min))
355       (nntp-send-command "^.*\r\n" "AUTHINFO USER" (user-login-name))
356       (nntp-send-command 
357        "^.*\r\n" "AUTHINFO PASS" 
358        (buffer-substring (point) (progn (end-of-line) (point))))
359       (kill-buffer (current-buffer)))))
360
361 ;;; Internal functions.
362
363 (defun nntp-send-command (wait-for &rest strings)
364   "Send STRINGS to server and wait until WAIT-FOR returns."
365   (unless nnheader-callback-function
366     (save-excursion
367       (set-buffer nntp-server-buffer)
368       (erase-buffer)))
369   (nntp-retrieve-data
370    (mapconcat 'identity strings " ") 
371    nntp-address nntp-port-number nntp-server-buffer
372    wait-for nnheader-callback-function))
373
374 (defun nntp-send-command-and-decode (wait-for &rest strings)
375   "Send STRINGS to server and wait until WAIT-FOR returns."
376   (unless nnheader-callback-function
377     (save-excursion
378       (set-buffer nntp-server-buffer)
379       (erase-buffer)))
380   (nntp-retrieve-data
381    (mapconcat 'identity strings " ") 
382    nntp-address nntp-port-number nntp-server-buffer
383    wait-for nnheader-callback-function t))
384
385 (defun nntp-send-buffer (wait-for)
386   "Send the current buffer to server and wait until WAIT-FOR returns."
387   (unless nnheader-callback-function
388     (save-excursion
389       (set-buffer nntp-server-buffer)
390       (erase-buffer)))
391   (nntp-encode-text)
392   (process-send-region (nntp-find-connection nntp-server-buffer)
393                        (point-min) (point-max))
394   (nntp-retrieve-data
395    nil nntp-address nntp-port-number nntp-server-buffer
396    wait-for nnheader-callback-function))
397
398 (defun nntp-find-connection (buffer)
399   "Find the connection delivering to BUFFER."
400   (let ((alist nntp-connection-alist)
401         (buffer (if (stringp buffer) (get-buffer buffer) buffer))
402         process entry)
403     (while (setq entry (pop alist))
404       (when (eq buffer (cadr entry))
405         (setq process (car entry)
406               alist nil)))
407     (when process
408       (if (memq (process-status process) '(open run))
409           process
410         (when (buffer-name (process-buffer process))
411           (kill-buffer (process-buffer process)))
412         (setq nntp-connection-alist (delq entry nntp-connection-alist))
413         nil))))
414
415 (defun nntp-find-connection-entry (buffer)
416   "Return the entry for the connection to BUFFER."
417   (assq (nntp-find-connection buffer) nntp-connection-alist))
418
419 (defun nntp-open-connection (buffer)
420   "Open a connection to PORT on ADDRESS delivering output to BUFFER."
421   (run-hooks 'nntp-prepare-server-hook)
422   (let* ((pbuffer (save-excursion
423                     (set-buffer 
424                      (generate-new-buffer
425                       (format " *server %s %s %s*"
426                               nntp-address nntp-port-number
427                               (buffer-name (get-buffer buffer)))))
428                     (buffer-disable-undo (current-buffer))
429                     (current-buffer)))
430          (process
431           (condition-case ()
432               (funcall nntp-open-connection-function pbuffer)
433             (error nil))))
434     (when process
435       (process-kill-without-query process)
436       (nntp-wait-for process "^.*\r\n" buffer)
437       (if (memq (process-status process) '(open run))
438           (prog1
439               (caar (push (list process buffer nil) 
440                           nntp-connection-alist))
441             (nntp-read-server-type)
442             (run-hooks 'nntp-server-opened-hook))
443         (when (buffer-name (process-buffer process))
444           (kill-buffer (process-buffer process)))
445         nil))))
446
447 (defun nntp-open-network-stream (buffer)
448   (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
449
450 (defun nntp-read-server-type ()
451   "Find out what the name of the server we have connected to is."
452   ;; Wait for the status string to arrive.
453   (setq nntp-server-type (buffer-string))
454   (let ((alist nntp-server-action-alist)
455         entry)
456     ;; Run server-specific commmands.
457     (while alist
458       (setq entry (pop alist))
459       (when (string-match (car entry) nntp-server-type)
460         (if (and (listp (cadr entry))
461                  (not (eq 'lambda (caadr entry))))
462             (eval (cadr entry))
463           (funcall (cadr entry)))))))
464
465 (defvar nntp-tmp-first)
466 (defvar nntp-tmp-wait-for)
467 (defvar nntp-tmp-callback)
468 (defvar nntp-tmp-buffer)
469
470 (defun nntp-make-process-filter (wait-for callback buffer decode)
471   `(lambda (proc string)
472      (let ((nntp-tmp-wait-for ,wait-for)
473            (nntp-tmp-callback ,callback)
474            (nntp-tmp-buffer ,buffer))
475        (nntp-process-filter proc string))))
476
477 (defun nntp-process-filter (proc string)
478   (let ((old-buffer (current-buffer)))
479     (unwind-protect
480         (let (point)
481           (set-buffer (process-buffer proc))
482           ;; Insert the text, moving the process-marker.
483           (setq point (goto-char (process-mark proc)))
484           (insert string)
485           (set-marker (process-mark proc) (point))
486           (if (and (= point (point-min))
487                    (string-match "^45" string))
488               (progn
489                 (nntp-snarf-error-message)
490                 (set-process-filter proc nil)
491                 (funcall nntp-tmp-callback nil))
492             (setq nntp-tmp-first nil)
493             (if (re-search-backward nntp-tmp-wait-for nil t)
494                 (progn
495                   (if (buffer-name (get-buffer nntp-tmp-buffer))
496                       (save-excursion
497                         (set-buffer (get-buffer nntp-tmp-buffer))
498                         (insert-buffer-substring (process-buffer proc))))
499                   (set-process-filter proc nil)
500                   (erase-buffer)
501                   (funcall nntp-tmp-callback t)))))
502       (set-buffer old-buffer))))
503
504 (defun nntp-retrieve-data (command address port buffer
505                                    &optional wait-for callback decode)
506   "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
507   (let ((process (or (nntp-find-connection buffer)
508                      (nntp-open-connection buffer))))
509     (if (not process)
510         (nnheader-report 'nntp "Couldn't open connection to %a" address)
511       (unless nntp-inhibit-erase
512         (save-excursion
513           (set-buffer (process-buffer process))
514           (erase-buffer)))
515       (when command
516         (nntp-send-string process command))
517       (cond 
518        ((eq callback 'ignore)
519         t)
520        ((and callback wait-for)
521         (set-process-filter
522          process (nntp-make-process-filter wait-for callback buffer decode))
523         t)
524        (wait-for 
525         (set-process-filter process nil)
526         (nntp-wait-for process wait-for buffer decode))
527        (t t)))))
528
529 (defun nntp-send-string (process string)
530   "Send STRING to PROCESS."
531   (process-send-string process (concat string nntp-end-of-line)))
532
533 (defun nntp-wait-for (process wait-for buffer &optional decode)
534   "Wait for WAIT-FOR to arrive from PROCESS."
535   (save-excursion
536     (set-buffer (process-buffer process))
537     (goto-char (point-min))
538     (while (not (looking-at "[2345]"))
539       (nntp-accept-process-output process)
540       (goto-char (point-min)))
541     (prog1
542         (if (looking-at "[345]")
543             (progn
544               (nntp-snarf-error-message)
545               nil)
546           (goto-char (point-max))
547           (while (not (re-search-backward wait-for nil t))
548             (nntp-accept-process-output process)
549             (goto-char (point-max)))
550           (nntp-decode-text (not decode))
551           (save-excursion
552             (set-buffer buffer)
553             (insert-buffer-substring (process-buffer process))
554             t))
555       (erase-buffer))))
556
557 (defun nntp-snarf-error-message ()
558   "Save the error message in the current buffer."
559   (setq nntp-status-string (buffer-string)))
560
561 (defun nntp-accept-process-output (process)
562   "Wait for output from PROCESS and message some dots."
563   (save-excursion
564     (set-buffer nntp-server-buffer)
565     (message "nntp reading%s" (make-string (/ (point-max) 100) ?.))
566     (accept-process-output process 1)))
567
568 (defun nntp-accept-response ()
569   "Wait for output from the process that outputs to BUFFER."
570   (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
571
572 (defun nntp-possibly-change-group (group server &optional connectionless)
573   (when server
574     (or (nntp-server-opened server)
575         (nntp-open-server server nil connectionless)))
576
577   (unless connectionless
578     (or (nntp-find-connection nntp-server-buffer)
579         (nntp-open-connection nntp-server-buffer)))
580
581   (when group
582     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
583       (when (not (equal group (caddr entry)))
584         (nntp-request-group group)))))
585
586 (defun nntp-decode-text (&optional cr-only)
587   "Decode the text in the current buffer."
588   (goto-char (point-min))
589   (while (search-forward "\r" nil t)
590     (delete-char -1))
591   (unless cr-only
592     (goto-char (point-max))
593     (forward-line -1)
594     (when (looking-at ".\n")
595       (delete-char 2))
596     (goto-char (point-min))
597     (delete-region (point) (progn (forward-line 1) (point)))
598     (while (search-forward "\n.." nil t)
599       (delete-char -1))))
600
601 (defun nntp-encode-text ()
602   "Encode the text in the current buffer."
603   (save-excursion
604     ;; Replace "." at beginning of line with "..".
605     (goto-char (point-min))
606     (while (re-search-forward "^\\." nil t)
607       (insert "."))
608     (goto-char (point-max))
609     ;; Insert newline at the end of the buffer.
610     (unless (bolp)
611       (insert "\n"))
612     ;; Insert `.' at end of buffer (end of text mark).
613     (goto-char (point-max))
614     (insert "." nntp-end-of-line)))
615
616 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
617   (erase-buffer)
618   (cond 
619
620    ;; This server does not talk NOV.
621    ((not nntp-server-xover)
622     nil)
623
624    ;; We don't care about gaps.
625    ((or (not nntp-nov-gap)
626         fetch-old)
627     (nntp-send-xover-command 
628      (if fetch-old
629          (if (numberp fetch-old) 
630              (max 1 (- (car articles) fetch-old)) 
631            1)
632        (car articles))
633      (car (last articles)) 'wait)
634
635     (goto-char (point-min))
636     (when (looking-at "[1-5][0-9][0-9] ")
637       (delete-region (point) (progn (forward-line 1) (point))))
638     (while (search-forward "\r" nil t)
639       (replace-match "" t t))
640     (goto-char (point-max))
641     (forward-line -1)
642     (when (looking-at "\\.")
643       (delete-region (point) (progn (forward-line 1) (point)))))
644
645    ;; We do it the hard way.  For each gap, an XOVER command is sent
646    ;; to the server.  We do not wait for a reply from the server, we
647    ;; just send them off as fast as we can.  That means that we have
648    ;; to count the number of responses we get back to find out when we
649    ;; have gotten all we asked for.
650    ((numberp nntp-nov-gap)
651     (let ((count 0)
652           (received 0)
653           (last-point (point-min))
654           (buf nntp-server-buffer)
655           ;;(process-buffer (nntp-find-connection (current-buffer))))
656           first)
657       ;; We have to check `nntp-server-xover'.  If it gets set to nil,
658       ;; that means that the server does not understand XOVER, but we
659       ;; won't know that until we try.
660       (while (and nntp-server-xover articles)
661         (setq first (car articles))
662         ;; Search forward until we find a gap, or until we run out of
663         ;; articles. 
664         (while (and (cdr articles) 
665                     (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
666           (setq articles (cdr articles)))
667
668         (when (nntp-send-xover-command first (car articles))
669           (setq articles (cdr articles)
670                 count (1+ count))
671
672           ;; Every 400 requests we have to read the stream in
673           ;; order to avoid deadlocks.
674           (when (or (null articles)     ;All requests have been sent.
675                     (zerop (% count nntp-maximum-request)))
676             (accept-process-output)
677             ;; On some Emacs versions the preceding function has
678             ;; a tendency to change the buffer. Perhaps. It's
679             ;; quite difficult to reproduce, because it only
680             ;; seems to happen once in a blue moon. 
681             (set-buffer buf) 
682             (while (progn
683                      (goto-char last-point)
684                      ;; Count replies.
685                      (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
686                        (setq received (1+ received)))
687                      (setq last-point (point))
688                      (< received count))
689               (accept-process-output)
690               (set-buffer buf)))))
691
692       (when nntp-server-xover
693         ;; Wait for the reply from the final command.
694         (goto-char (point-max))
695         (re-search-backward "^[0-9][0-9][0-9] " nil t)
696         (when (looking-at "^[23]")
697           (while (progn
698                    (goto-char (point-max))
699                    (forward-line -1)
700                    (not (looking-at "^\\.\r?\n")))
701             (nntp-accept-response)))
702         
703         ;; We remove any "." lines and status lines.
704         (goto-char (point-min))
705         (while (search-forward "\r" nil t)
706           (delete-char -1))
707         (goto-char (point-min))
708         (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
709         (copy-to-buffer nntp-server-buffer (point-min) (point-max))))))
710
711   nntp-server-xover)
712
713 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
714   "Send the XOVER command to the server."
715   (let ((range (format "%d-%d" beg end))
716         (nntp-inhibit-erase t))
717     (if (stringp nntp-server-xover)
718         ;; If `nntp-server-xover' is a string, then we just send this
719         ;; command.
720         (if wait-for-reply
721             (nntp-send-command "\r\n\\.\r\n" nntp-server-xover range)
722           ;; We do not wait for the reply.
723           (nntp-send-command "\r\n\\.\r\n" nntp-server-xover range))
724       (let ((commands nntp-xover-commands))
725         ;; `nntp-xover-commands' is a list of possible XOVER commands.
726         ;; We try them all until we get at positive response. 
727         (while (and commands (eq nntp-server-xover 'try))
728           (nntp-send-command "\r\n\\.\r\n" (car commands) range)
729           (save-excursion
730             (set-buffer nntp-server-buffer)
731             (goto-char (point-min))
732             (and (looking-at "[23]") ; No error message.
733                  ;; We also have to look at the lines.  Some buggy
734                  ;; servers give back simple lines with just the
735                  ;; article number.  How... helpful.
736                  (progn
737                    (forward-line 1)
738                    (looking-at "[0-9]+\t...")) ; More text after number.
739                  (setq nntp-server-xover (car commands))))
740           (setq commands (cdr commands)))
741         ;; If none of the commands worked, we disable XOVER.
742         (when (eq nntp-server-xover 'try)
743           (save-excursion
744             (set-buffer nntp-server-buffer)
745             (erase-buffer)
746             (setq nntp-server-xover nil)))
747         nntp-server-xover))))
748
749 ;;; Alternative connection methods.
750
751 (defun nntp-wait-for-string (regexp)
752   "Wait until string arrives in the buffer."
753   (let ((buf (current-buffer)))
754     (goto-char (point-min))
755     (while (not (re-search-forward regexp nil t))
756       (accept-process-output (nntp-find-connection nntp-server-buffer))
757       (set-buffer buf)
758       (goto-char (point-min)))))
759
760 (defun nntp-open-telnet (server)
761   (save-excursion
762     (set-buffer nntp-server-buffer)
763     (erase-buffer)
764     (let ((proc (start-process
765                  "nntpd" nntp-server-buffer "telnet" "-8"))
766           (case-fold-search t))
767       (when (memq (process-status proc) '(open run))
768         (process-send-string proc "set escape \^X\n")
769         (process-send-string proc (concat "open " server "\n"))
770         (nntp-wait-for-string "^\r*.?login:")
771         (process-send-string
772          proc (concat
773                (or nntp-telnet-user-name
774                    (setq nntp-telnet-user-name (read-string "login: ")))
775                "\n"))
776         (nntp-wait-for-string "^\r*.?password:")
777         (process-send-string
778          proc (concat
779                (or nntp-telnet-passwd
780                    (setq nntp-telnet-passwd
781                          (nnmail-read-passwd "Password: ")))
782                "\n"))
783         (erase-buffer)
784         (nntp-wait-for-string "bash\\|\$ *\r?$\\|> *\r?")
785         (process-send-string
786          proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
787         (nntp-wait-for-string "^\r*200")
788         (beginning-of-line)
789         (delete-region (point-min) (point))
790         (process-send-string proc "\^]")
791         (nntp-wait-for-string "^telnet")
792         (process-send-string proc "mode character\n")
793         (accept-process-output proc 1)
794         (sit-for 1)
795         (goto-char (point-min))
796         (forward-line 1)
797         (delete-region (point) (point-max)))
798       proc)))
799
800 (defun nntp-open-rlogin (server)
801   "Open a connection to SERVER using rsh."
802   (let ((proc (if nntp-rlogin-user-name
803                   (start-process
804                    "nntpd" nntp-server-buffer "rsh"
805                    server "-l" nntp-rlogin-user-name
806                    (mapconcat 'identity
807                               nntp-rlogin-parameters " "))
808                 (start-process
809                  "nntpd" nntp-server-buffer "rsh" server
810                  (mapconcat 'identity
811                             nntp-rlogin-parameters " ")))))
812     proc))
813
814 (defun nntp-find-connection-buffer (buffer)
815   "Return the process connection buffer tied to BUFFER."
816   (let ((process (nntp-find-connection buffer)))
817     (when process
818       (process-buffer process))))
819
820 (provide 'nntp)
821
822 ;;; nntp.el ends here