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