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