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