1 ;;; nntp.el --- nntp access for Gnus
2 ;; Copyright (C) 1987, 1988, 1989, 1990, 1992, 1993, 1994, 1995, 1996,
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
35 (eval-when-compile (require 'cl))
37 (defvoo nntp-address nil
38 "Address of the physical nntp server.")
40 (defvoo nntp-port-number "nntp"
41 "Port number on the physical nntp server.")
43 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
44 "*Hook used for sending commands to the server at startup.
45 The default value is `nntp-send-mode-reader', which makes an innd
46 server spawn an nnrpd server.")
48 (defvoo nntp-authinfo-function 'nntp-send-authinfo
49 "Function used to send AUTHINFO to the server.
50 It is called with no parameters.")
52 (defvoo nntp-server-action-alist
53 '(("nntpd 1\\.5\\.11t"
54 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
55 ("NNRP server Netscape"
56 (setq nntp-server-list-active-group nil)))
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:
61 \(setq nntp-server-action-alist
64 You probably don't want to do that, though.")
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 buffer to output in.
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 are `nntp-open-rlogin',
73 which does an rlogin on the remote system, and then does a telnet to
74 the NNTP server available there (see nntp-rlogin-parameters) and
75 `nntp-open-telnet' which telnets to a remote system, logs in and does
78 (defvoo nntp-rlogin-program "rsh"
79 "*Program used to log in on remote machines.
80 The default is \"rsh\", but \"ssh\" is a popular alternative.")
82 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
83 "*Parameters to `nntp-open-rlogin'.
84 That function may be used as `nntp-open-connection-function'. In that
85 case, this list will be used as the parameter list given to rsh.")
87 (defvoo nntp-rlogin-user-name nil
88 "*User name on remote system when using the rlogin connect method.")
90 (defvoo nntp-telnet-parameters
91 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
92 "*Parameters to `nntp-open-telnet'.
93 That function may be used as `nntp-open-connection-function'. In that
94 case, this list will be executed as a command after logging in
97 (defvoo nntp-telnet-user-name nil
98 "User name to log in via telnet with.")
100 (defvoo nntp-telnet-passwd nil
101 "Password to use to log in via telnet with.")
103 (defvoo nntp-open-telnet-envuser nil
104 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
106 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
107 "*Regular expression to match the shell prompt on the remote machine.")
109 (defvoo nntp-telnet-command "telnet"
110 "Command used to start telnet.")
112 (defvoo nntp-telnet-switches '("-8")
113 "Switches given to the telnet command.")
115 (defvoo nntp-end-of-line "\r\n"
116 "String to use on the end of lines when talking to the NNTP server.
117 This is \"\\r\\n\" by default, but should be \"\\n\" when
118 using rlogin or telnet to communicate with the server.")
120 (defvoo nntp-large-newsgroup 50
121 "*The number of the articles which indicates a large newsgroup.
122 If the number of the articles is greater than the value, verbose
123 messages will be shown to indicate the current status.")
125 (defvoo nntp-maximum-request 400
126 "*The maximum number of the requests sent to the NNTP server at one time.
127 If Emacs hangs up while retrieving headers, set the variable to a
130 (defvoo nntp-nov-is-evil nil
131 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
133 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
134 "*List of strings that are used as commands to fetch NOV lines from a server.
135 The strings are tried in turn until a positive response is gotten. If
136 none of the commands are successful, nntp will just grab headers one
139 (defvoo nntp-nov-gap 5
140 "*Maximum allowed gap between two articles.
141 If the gap between two consecutive articles is bigger than this
142 variable, split the XOVER request into two requests.")
144 (defvoo nntp-prepare-server-hook nil
145 "*Hook run before a server is opened.
146 If can be used to set up a server remotely, for instance. Say you
147 have an account at the machine \"other.machine\". This machine has
148 access to an NNTP server that you can't access locally. You could
149 then use this hook to rsh to the remote machine and start a proxy NNTP
150 server there that you can connect to. See also
151 `nntp-open-connection-function'")
153 (defvoo nntp-warn-about-losing-connection t
154 "*If non-nil, beep when a server closes connection.")
156 (defvoo nntp-coding-system-for-read 'binary
157 "*Coding system to read from NNTP.")
159 (defvoo nntp-coding-system-for-write 'binary
160 "*Coding system to write to NNTP.")
162 (defcustom nntp-authinfo-file "~/.authinfo"
163 ".netrc-like file that holds nntp authinfo passwords."
166 (repeat :tag "Entries"
169 :value ("" ("login" . "") ("password" . ""))
173 (const :format "" "login")
174 (string :format "Login: %v"))
176 (const :format "" "password")
177 (string :format "Password: %v")))))))
181 (defvoo nntp-connection-timeout nil
182 "*Number of seconds to wait before an nntp connection times out.
183 If this variable is nil, which is the default, no timers are set.
184 NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
186 ;;; Internal variables.
188 (defvar nntp-record-commands nil
189 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
191 (defvar nntp-have-messaged nil)
193 (defvar nntp-process-wait-for nil)
194 (defvar nntp-process-to-buffer nil)
195 (defvar nntp-process-callback nil)
196 (defvar nntp-process-decode nil)
197 (defvar nntp-process-start-point nil)
198 (defvar nntp-inside-change-function nil)
199 (defvoo nntp-last-command-time nil)
200 (defvoo nntp-last-command nil)
201 (defvoo nntp-authinfo-password nil)
202 (defvoo nntp-authinfo-user nil)
204 (defvar nntp-connection-list nil)
206 (defvoo nntp-server-type nil)
207 (defvoo nntp-connection-alist nil)
208 (defvoo nntp-status-string "")
209 (defconst nntp-version "nntp 5.0")
210 (defvoo nntp-inhibit-erase nil)
211 (defvoo nntp-inhibit-output nil)
213 (defvoo nntp-server-xover 'try)
214 (defvoo nntp-server-list-active-group 'try)
216 (defvar nntp-async-needs-kluge
217 (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
218 "*When non-nil, nntp will poll asynchronous connections
219 once a second. By default, this is turned on only for Emacs
220 20.3, which has a bug that breaks nntp's normal method of
221 noticing asynchronous data.")
223 (defvar nntp-async-timer nil)
224 (defvar nntp-async-process-list nil)
227 (autoload 'mail-source-read-passwd "mail-source")
228 (autoload 'open-ssl-stream "ssl"))
232 ;;; Internal functions.
234 (defsubst nntp-send-string (process string)
235 "Send STRING to PROCESS."
236 ;; We need to store the time to provide timeouts, and
237 ;; to store the command so the we can replay the command
238 ;; if the server gives us an AUTHINFO challenge.
239 (setq nntp-last-command-time (current-time)
240 nntp-last-command string)
241 (when nntp-record-commands
242 (nntp-record-command string))
243 (process-send-string process (concat string nntp-end-of-line)))
245 (defun nntp-record-command (string)
246 "Record the command STRING."
248 (set-buffer (get-buffer-create "*nntp-log*"))
249 (goto-char (point-max))
250 (let ((time (current-time)))
251 (insert (format-time-string "%Y%m%dT%H%M%S" time)
252 "." (format "%03d" (/ (nth 2 time) 1000))
253 " " nntp-address " " string "\n"))))
255 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
256 "Wait for WAIT-FOR to arrive from PROCESS."
258 (set-buffer (process-buffer process))
259 (goto-char (point-min))
260 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
262 (memq (process-status process) '(open run)))
263 (when (looking-at "480")
264 (nntp-handle-authinfo process))
265 (nntp-accept-process-output process)
266 (goto-char (point-min)))
271 (nntp-snarf-error-message)
273 ((not (memq (process-status process) '(open run)))
274 (nnheader-report 'nntp "Server closed connection"))
276 (goto-char (point-max))
277 (let ((limit (point-min)))
278 (while (not (re-search-backward wait-for limit t))
279 (nntp-accept-process-output process)
280 ;; We assume that whatever we wait for is less than 1000
282 (setq limit (max (- (point-max) 1000) (point-min)))
283 (goto-char (point-max))))
284 (nntp-decode-text (not decode))
288 (goto-char (point-max))
289 (insert-buffer-substring (process-buffer process))
290 ;; Nix out "nntp reading...." message.
291 (when nntp-have-messaged
292 (setq nntp-have-messaged nil)
293 (nnheader-message 5 ""))))
298 (defun nntp-kill-buffer (buffer)
299 (when (buffer-name buffer)
301 (nnheader-init-server-buffer)))
303 (defsubst nntp-find-connection (buffer)
304 "Find the connection delivering to BUFFER."
305 (let ((alist nntp-connection-alist)
306 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
308 (while (and alist (setq entry (pop alist)))
309 (when (eq buffer (cadr entry))
310 (setq process (car entry)
313 (if (memq (process-status process) '(open run))
315 (nntp-kill-buffer (process-buffer process))
316 (setq nntp-connection-alist (delq entry nntp-connection-alist))
319 (defsubst nntp-find-connection-entry (buffer)
320 "Return the entry for the connection to BUFFER."
321 (assq (nntp-find-connection buffer) nntp-connection-alist))
323 (defun nntp-find-connection-buffer (buffer)
324 "Return the process connection buffer tied to BUFFER."
325 (let ((process (nntp-find-connection buffer)))
327 (process-buffer process))))
329 (defsubst nntp-retrieve-data (command address port buffer
330 &optional wait-for callback decode)
331 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
332 (let ((process (or (nntp-find-connection buffer)
333 (nntp-open-connection buffer))))
335 (nnheader-report 'nntp "Couldn't open connection to %s" address)
336 (unless (or nntp-inhibit-erase nnheader-callback-function)
338 (set-buffer (process-buffer process))
343 (nntp-send-string process command))
345 ((eq callback 'ignore)
347 ((and callback wait-for)
348 (nntp-async-wait process wait-for buffer decode callback)
351 (nntp-wait-for process wait-for buffer decode))
354 (nnheader-report 'nntp "Couldn't open connection to %s: %s"
357 (message "Quit retrieving data from nntp")
361 (defsubst nntp-send-command (wait-for &rest strings)
362 "Send STRINGS to server and wait until WAIT-FOR returns."
363 (when (and (not nnheader-callback-function)
364 (not nntp-inhibit-output))
366 (set-buffer nntp-server-buffer)
369 (mapconcat 'identity strings " ")
370 nntp-address nntp-port-number nntp-server-buffer
371 wait-for nnheader-callback-function))
373 (defun nntp-send-command-nodelete (wait-for &rest strings)
374 "Send STRINGS to server and wait until WAIT-FOR returns."
376 (mapconcat 'identity strings " ")
377 nntp-address nntp-port-number nntp-server-buffer
378 wait-for nnheader-callback-function))
380 (defun nntp-send-command-and-decode (wait-for &rest strings)
381 "Send STRINGS to server and wait until WAIT-FOR returns."
382 (when (and (not nnheader-callback-function)
383 (not nntp-inhibit-output))
385 (set-buffer nntp-server-buffer)
388 (mapconcat 'identity strings " ")
389 nntp-address nntp-port-number nntp-server-buffer
390 wait-for nnheader-callback-function t))
392 (defun nntp-send-buffer (wait-for)
393 "Send the current buffer to server and wait until WAIT-FOR returns."
394 (when (and (not nnheader-callback-function)
395 (not nntp-inhibit-output))
397 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
400 (process-send-region (nntp-find-connection nntp-server-buffer)
401 (point-min) (point-max))
403 nil nntp-address nntp-port-number nntp-server-buffer
404 wait-for nnheader-callback-function))
408 ;;; Interface functions.
410 (nnoo-define-basics nntp)
412 (defsubst nntp-next-result-arrived-p ()
414 ;; A result that starts with a 2xx code is terminated by
415 ;; a line with only a "." on it.
416 ((eq (char-after) ?2)
417 (if (re-search-forward "\n\\.\r?\n" nil t)
420 ;; A result that starts with a 3xx or 4xx code is terminated
423 (if (search-forward "\n" nil t)
430 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
431 "Retrieve the headers of ARTICLES."
432 (nntp-possibly-change-group group server)
434 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
436 (if (and (not gnus-nov-is-evil)
437 (not nntp-nov-is-evil)
438 (nntp-retrieve-headers-with-xover articles fetch-old))
439 ;; We successfully retrieved the headers via XOVER.
441 ;; XOVER didn't work, so we do it the hard, slow and inefficient
443 (let ((number (length articles))
446 (last-point (point-min))
447 (buf (nntp-find-connection-buffer nntp-server-buffer))
448 (nntp-inhibit-erase t)
450 ;; Send HEAD commands.
451 (while (setq article (pop articles))
454 "HEAD" (if (numberp article)
455 (int-to-string article)
456 ;; `articles' is either a list of article numbers
457 ;; or a list of article IDs.
460 ;; Every 400 requests we have to read the stream in
461 ;; order to avoid deadlocks.
462 (when (or (null articles) ;All requests have been sent.
463 (zerop (% count nntp-maximum-request)))
464 (nntp-accept-response)
467 (goto-char last-point)
469 (while (nntp-next-result-arrived-p)
470 (setq last-point (point))
473 ;; If number of headers is greater than 100, give
474 ;; informative messages.
475 (and (numberp nntp-large-newsgroup)
476 (> number nntp-large-newsgroup)
477 (zerop (% received 20))
478 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
479 (/ (* received 100) number)))
480 (nntp-accept-response))))
481 (and (numberp nntp-large-newsgroup)
482 (> number nntp-large-newsgroup)
483 (nnheader-message 6 "NNTP: Receiving headers...done"))
485 ;; Now all of replies are received. Fold continuation lines.
486 (nnheader-fold-continuation-lines)
487 ;; Remove all "\r"'s.
489 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
492 (deffoo nntp-retrieve-groups (groups &optional server)
493 "Retrieve group info on GROUPS."
494 (nntp-possibly-change-group nil server)
495 (when (nntp-find-connection-buffer nntp-server-buffer)
497 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
498 (set-buffer nntp-server-buffer)
500 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
501 ;; The first time this is run, this variable is `try'. So we
503 (when (eq nntp-server-list-active-group 'try)
504 (nntp-try-list-active (car groups)))
508 (last-point (point-min))
509 (nntp-inhibit-erase t)
510 (buf (nntp-find-connection-buffer nntp-server-buffer))
511 (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
513 ;; Send the command to the server.
514 (nntp-send-command nil command (pop groups))
516 ;; Every 400 requests we have to read the stream in
517 ;; order to avoid deadlocks.
518 (when (or (null groups) ;All requests have been sent.
519 (zerop (% count nntp-maximum-request)))
520 (nntp-accept-response)
521 (while (and (gnus-buffer-live-p buf)
523 ;; Search `blue moon' in this file for the
524 ;; reason why set-buffer here.
526 (goto-char last-point)
528 (while (re-search-forward "^[0-9]" nil t)
530 (setq last-point (point))
532 (nntp-accept-response))))
534 ;; Wait for the reply from the final command.
535 (unless (gnus-buffer-live-p buf)
537 (nnheader-report 'nntp "Connection to %s is closed." server)))
539 (goto-char (point-max))
540 (re-search-backward "^[0-9]" nil t)
541 (when (looking-at "^[23]")
542 (while (and (gnus-buffer-live-p buf)
545 (goto-char (point-max))
546 (if (not nntp-server-list-active-group)
547 (not (re-search-backward "\r?\n" (- (point) 3) t))
548 (not (re-search-backward "^\\.\r?\n"
550 (nntp-accept-response)))
552 ;; Now all replies are received. We remove CRs.
553 (unless (gnus-buffer-live-p buf)
555 (nnheader-report 'nntp "Connection to %s is closed." server)))
557 (goto-char (point-min))
558 (while (search-forward "\r" nil t)
559 (replace-match "" t t))
561 (if (not nntp-server-list-active-group)
563 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
565 ;; We have read active entries, so we just delete the
567 (goto-char (point-min))
568 (while (re-search-forward "^[.2-5]" nil t)
569 (delete-region (match-beginning 0)
570 (progn (forward-line 1) (point))))
571 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
574 (deffoo nntp-retrieve-articles (articles &optional group server)
575 (nntp-possibly-change-group group server)
577 (let ((number (length articles))
580 (last-point (point-min))
581 (buf (nntp-find-connection-buffer nntp-server-buffer))
582 (nntp-inhibit-erase t)
583 (map (apply 'vector articles))
588 ;; Send ARTICLE command.
589 (while (setq article (pop articles))
592 "ARTICLE" (if (numberp article)
593 (int-to-string article)
594 ;; `articles' is either a list of article numbers
595 ;; or a list of article IDs.
598 ;; Every 400 requests we have to read the stream in
599 ;; order to avoid deadlocks.
600 (when (or (null articles) ;All requests have been sent.
601 (zerop (% count nntp-maximum-request)))
602 (nntp-accept-response)
605 (goto-char last-point)
607 (while (nntp-next-result-arrived-p)
608 (aset map received (cons (aref map received) (point)))
609 (setq last-point (point))
612 ;; If number of headers is greater than 100, give
613 ;; informative messages.
614 (and (numberp nntp-large-newsgroup)
615 (> number nntp-large-newsgroup)
616 (zerop (% received 20))
617 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
618 (/ (* received 100) number)))
619 (nntp-accept-response))))
620 (and (numberp nntp-large-newsgroup)
621 (> number nntp-large-newsgroup)
622 (nnheader-message 6 "NNTP: Receiving articles...done"))
624 ;; Now we have all the responses. We go through the results,
625 ;; wash it and copy it over to the server buffer.
626 (set-buffer nntp-server-buffer)
628 (setq last-point (point-min))
632 (setq point (goto-char (point-max)))
634 (insert-buffer-substring buf last-point (cdr entry))
636 (setq last-point (cdr entry))
639 (cons (car entry) point))
642 (defun nntp-try-list-active (group)
643 (nntp-list-active-group group)
645 (set-buffer nntp-server-buffer)
646 (goto-char (point-min))
648 (looking-at "5[0-9]+"))
649 (setq nntp-server-list-active-group nil))
651 (setq nntp-server-list-active-group t)))))
653 (deffoo nntp-list-active-group (group &optional server)
654 "Return the active info on GROUP (which can be a regexp)."
655 (nntp-possibly-change-group nil server)
656 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group))
658 (deffoo nntp-request-group-articles (group &optional server)
659 "Return the list of existing articles in GROUP."
660 (nntp-possibly-change-group nil server)
661 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group))
663 (deffoo nntp-request-article (article &optional group server buffer command)
664 (nntp-possibly-change-group group server)
665 (when (nntp-send-command-and-decode
666 "\r?\n\\.\r?\n" "ARTICLE"
667 (if (numberp article) (int-to-string article) article))
669 (not (equal buffer nntp-server-buffer)))
671 (set-buffer nntp-server-buffer)
672 (copy-to-buffer buffer (point-min) (point-max))
673 (nntp-find-group-and-number))
674 (nntp-find-group-and-number))))
676 (deffoo nntp-request-head (article &optional group server)
677 (nntp-possibly-change-group group server)
678 (when (nntp-send-command
679 "\r?\n\\.\r?\n" "HEAD"
680 (if (numberp article) (int-to-string article) article))
682 (nntp-find-group-and-number)
683 (nntp-decode-text))))
685 (deffoo nntp-request-body (article &optional group server)
686 (nntp-possibly-change-group group server)
687 (nntp-send-command-and-decode
688 "\r?\n\\.\r?\n" "BODY"
689 (if (numberp article) (int-to-string article) article)))
691 (deffoo nntp-request-group (group &optional server dont-check)
692 (nntp-possibly-change-group nil server)
693 (when (nntp-send-command "^[245].*\n" "GROUP" group)
694 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))