1 ;;; nntp.el --- nntp access for Gnus
3 ;; Copyright (C) 1987, 1988, 1989, 1990, 1992, 1993, 1994, 1995, 1996,
4 ;; 1997, 1998, 2000, 2001, 2002, 2003 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
13 ;; by the Free Software Foundation; either version 2, or (at your
14 ;; option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; 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 the
23 ;; 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 as argument.
70 Currently, five such functions are provided (please refer to their
71 respective doc string for more information), three of them establishing
72 direct connections to the nntp server, and two of them using an indirect
76 - `nntp-open-network-stream' (the default),
77 - `nntp-open-ssl-stream',
78 - `nntp-open-tls-stream',
79 - `nntp-open-telnet-stream'.
82 - `nntp-open-via-rlogin-and-telnet',
83 - `nntp-open-via-telnet-and-telnet'.")
85 (defvoo nntp-pre-command nil
86 "*Pre-command to use with the various nntp-open-via-* methods.
87 This is where you would put \"runsocks\" or stuff like that.")
89 (defvoo nntp-telnet-command "telnet"
90 "*Telnet command used to connect to the nntp server.
91 This command is used by the various nntp-open-via-* methods.")
93 (defvoo nntp-telnet-switches '("-8")
94 "*Switches given to the telnet command `nntp-telnet-command'.")
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 and indirect connection method (nntp-open-via-*).")
101 (defvoo nntp-via-rlogin-command "rsh"
102 "*Rlogin command used to connect to an intermediate host.
103 This command is used by the `nntp-open-via-rlogin-and-telnet' method.
104 The default is \"rsh\", but \"ssh\" is a popular alternative.")
106 (defvoo nntp-via-rlogin-command-switches nil
107 "*Switches given to the rlogin command `nntp-via-rlogin-command'.
108 If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
109 \(\"-C\") in order to compress all data connections, otherwise set this
110 to \(\"-t\") or (\"-C\" \"-t\") if the telnet command requires a pseudo-tty
111 allocation on an intermediate host.")
113 (defvoo nntp-via-telnet-command "telnet"
114 "*Telnet command used to connect to an intermediate host.
115 This command is used by the `nntp-open-via-telnet-and-telnet' method.")
117 (defvoo nntp-via-telnet-switches '("-8")
118 "*Switches given to the telnet command `nntp-via-telnet-command'.")
120 (defvoo nntp-via-user-name nil
121 "*User name to log in on an intermediate host with.
122 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
124 (defvoo nntp-via-user-password nil
125 "*Password to use to log in on an intermediate host with.
126 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
128 (defvoo nntp-via-address nil
129 "*Address of an intermediate host to connect to.
130 This variable is used by the `nntp-open-via-rlogin-and-telnet' and
131 `nntp-open-via-telnet-and-telnet' methods.")
133 (defvoo nntp-via-envuser nil
134 "*Whether both telnet client and server support the ENVIRON option.
135 If non-nil, there will be no prompt for a login name.")
137 (defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
138 "*Regular expression to match the shell prompt on an intermediate host.
139 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
141 (defvoo nntp-large-newsgroup 50
142 "*The number of the articles which indicates a large newsgroup.
143 If the number of the articles is greater than the value, verbose
144 messages will be shown to indicate the current status.")
146 (defvoo nntp-maximum-request 400
147 "*The maximum number of the requests sent to the NNTP server at one time.
148 If Emacs hangs up while retrieving headers, set the variable to a
151 (defvoo nntp-nov-is-evil nil
152 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
154 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
155 "*List of strings that are used as commands to fetch NOV lines from a server.
156 The strings are tried in turn until a positive response is gotten. If
157 none of the commands are successful, nntp will just grab headers one
160 (defvoo nntp-nov-gap 5
161 "*Maximum allowed gap between two articles.
162 If the gap between two consecutive articles is bigger than this
163 variable, split the XOVER request into two requests.")
165 (defvoo nntp-prepare-server-hook nil
166 "*Hook run before a server is opened.
167 If can be used to set up a server remotely, for instance. Say you
168 have an account at the machine \"other.machine\". This machine has
169 access to an NNTP server that you can't access locally. You could
170 then use this hook to rsh to the remote machine and start a proxy NNTP
171 server there that you can connect to. See also
172 `nntp-open-connection-function'")
174 (defvoo nntp-warn-about-losing-connection t
175 "*If non-nil, beep when a server closes connection.")
177 (defvoo nntp-coding-system-for-read 'binary
178 "*Coding system to read from NNTP.")
180 (defvoo nntp-coding-system-for-write 'binary
181 "*Coding system to write to NNTP.")
183 (defcustom nntp-authinfo-file "~/.authinfo"
184 ".netrc-like file that holds nntp authinfo passwords."
187 (repeat :tag "Entries"
190 :value ("" ("login" . "") ("password" . ""))
194 (const :format "" "login")
195 (string :format "Login: %v"))
197 (const :format "" "password")
198 (string :format "Password: %v")))))))
202 (defvoo nntp-connection-timeout nil
203 "*Number of seconds to wait before an nntp connection times out.
204 If this variable is nil, which is the default, no timers are set.
205 NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
207 (defvoo nntp-prepare-post-hook nil
208 "*Hook run just before posting an article. It is supposed to be used
209 to insert Cancel-Lock headers.")
211 (defvoo nntp-read-timeout (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
212 (symbol-name system-type))
215 "How long nntp should wait between checking for the end of output.
216 Shorter values mean quicker response, but is more CPU intensive.")
218 ;;; Internal variables.
220 (defvar nntp-record-commands nil
221 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
223 (defvar nntp-have-messaged nil)
225 (defvar nntp-process-wait-for nil)
226 (defvar nntp-process-to-buffer nil)
227 (defvar nntp-process-callback nil)
228 (defvar nntp-process-decode nil)
229 (defvar nntp-process-start-point nil)
230 (defvar nntp-inside-change-function nil)
231 (defvoo nntp-last-command-time nil)
232 (defvoo nntp-last-command nil)
233 (defvoo nntp-authinfo-password nil)
234 (defvoo nntp-authinfo-user nil)
236 (defvar nntp-connection-list nil)
238 (defvoo nntp-server-type nil)
239 (defvoo nntp-connection-alist nil)
240 (defvoo nntp-status-string "")
241 (defconst nntp-version "nntp 5.0")
242 (defvoo nntp-inhibit-erase nil)
243 (defvoo nntp-inhibit-output nil)
245 (defvoo nntp-server-xover 'try)
246 (defvoo nntp-server-list-active-group 'try)
248 (defvar nntp-async-needs-kluge
249 (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
250 "*When non-nil, nntp will poll asynchronous connections
251 once a second. By default, this is turned on only for Emacs
252 20.3, which has a bug that breaks nntp's normal method of
253 noticing asynchronous data.")
255 (defvar nntp-async-timer nil)
256 (defvar nntp-async-process-list nil)
258 (defvar nntp-ssl-program
259 "openssl s_client -quiet -ssl3 -connect %s:%p"
260 "A string containing commands for SSL connections.
261 Within a string, %s is replaced with the server address and %p with
262 port number on server. The program should accept IMAP commands on
263 stdin and return responses to stdout.")
267 ;;; Internal functions.
269 (defsubst nntp-send-string (process string)
270 "Send STRING to PROCESS."
271 ;; We need to store the time to provide timeouts, and
272 ;; to store the command so the we can replay the command
273 ;; if the server gives us an AUTHINFO challenge.
274 (setq nntp-last-command-time (current-time)
275 nntp-last-command string)
276 (when nntp-record-commands
277 (nntp-record-command string))
278 (process-send-string process (concat string nntp-end-of-line))
279 (or (memq (process-status process) '(open run))
280 (nntp-report "Server closed connection")))
282 (defun nntp-record-command (string)
283 "Record the command STRING."
285 (set-buffer (get-buffer-create "*nntp-log*"))
286 (goto-char (point-max))
287 (let ((time (current-time)))
288 (insert (format-time-string "%Y%m%dT%H%M%S" time)
289 "." (format "%03d" (/ (nth 2 time) 1000))
290 " " nntp-address " " string "\n"))))
292 (defun nntp-report (&rest args)
293 "Report an error from the nntp backend. The first string in ARGS
294 can be a format string. For some commands, the failed command may be
295 retried once before actually displaying the error report."
297 (when nntp-record-commands
298 (nntp-record-command "*** CALLED nntp-report ***"))
300 (nnheader-report 'nntp args)
304 (defun nntp-report-1 (&rest args)
305 "Throws out to nntp-with-open-group-error so that the connection may
306 be restored and the command retried."
308 (when nntp-record-commands
309 (nntp-record-command "*** CONNECTION LOST ***"))
311 (throw 'nntp-with-open-group-error t))
313 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
314 "Wait for WAIT-FOR to arrive from PROCESS."
316 (set-buffer (process-buffer process))
317 (goto-char (point-min))
318 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
320 (memq (process-status process) '(open run)))
321 (when (looking-at "480")
322 (nntp-handle-authinfo process))
323 (when (looking-at "^.*\n")
324 (delete-region (point) (progn (forward-line 1) (point))))
325 (nntp-accept-process-output process)
326 (goto-char (point-min)))
331 (nntp-snarf-error-message)
333 ((not (memq (process-status process) '(open run)))
334 (nntp-report "Server closed connection"))
336 (goto-char (point-max))
337 (let ((limit (point-min))
339 (while (not (re-search-backward wait-for limit t))
340 (nntp-accept-process-output process)
341 ;; We assume that whatever we wait for is less than 1000
343 (setq limit (max (- (point-max) 1000) (point-min)))
344 (goto-char (point-max)))
345 (setq response (match-string 0))
346 (with-current-buffer nntp-server-buffer
347 (setq nntp-process-response response)))
348 (nntp-decode-text (not decode))
352 (goto-char (point-max))
353 (insert-buffer-substring (process-buffer process))
354 ;; Nix out "nntp reading...." message.
355 (when nntp-have-messaged
356 (setq nntp-have-messaged nil)
357 (nnheader-message 5 ""))))
362 (defun nntp-kill-buffer (buffer)
363 (when (buffer-name buffer)
365 (nnheader-init-server-buffer)))
367 (defsubst nntp-find-connection (buffer)
368 "Find the connection delivering to BUFFER."
369 (let ((alist nntp-connection-alist)
370 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
372 (while (and alist (setq entry (pop alist)))
373 (when (eq buffer (cadr entry))
374 (setq process (car entry)
377 (if (memq (process-status process) '(open run))
379 (nntp-kill-buffer (process-buffer process))
380 (setq nntp-connection-alist (delq entry nntp-connection-alist))
383 (defsubst nntp-find-connection-entry (buffer)
384 "Return the entry for the connection to BUFFER."
385 (assq (nntp-find-connection buffer) nntp-connection-alist))
387 (defun nntp-find-connection-buffer (buffer)
388 "Return the process connection buffer tied to BUFFER."
389 (let ((process (nntp-find-connection buffer)))
391 (process-buffer process))))
393 (defsubst nntp-retrieve-data (command address port buffer
394 &optional wait-for callback decode)
395 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
396 (let ((process (or (nntp-find-connection buffer)
397 (nntp-open-connection buffer))))
400 (unless (or nntp-inhibit-erase nnheader-callback-function)
402 (set-buffer (process-buffer process))
407 (nntp-send-string process command))
409 ((eq callback 'ignore)
411 ((and callback wait-for)
412 (nntp-async-wait process wait-for buffer decode callback)
415 (nntp-wait-for process wait-for buffer decode))
418 (nnheader-report 'nntp "Couldn't open connection to %s: %s"
421 (message "Quit retrieving data from nntp")
424 (nnheader-report 'nntp "Couldn't open connection to %s" address))))
426 (defsubst nntp-send-command (wait-for &rest strings)
427 "Send STRINGS to server and wait until WAIT-FOR returns."
428 (when (and (not nnheader-callback-function)
429 (not nntp-inhibit-output))
431 (set-buffer nntp-server-buffer)
433 (let* ((command (mapconcat 'identity strings " "))
434 (process (nntp-find-connection nntp-server-buffer))
435 (buffer (and process (process-buffer process)))
436 (pos (and buffer (with-current-buffer buffer (point)))))
439 (nntp-retrieve-data command
440 nntp-address nntp-port-number
442 wait-for nnheader-callback-function)
443 ;; If nothing to wait for, still remove possibly echo'ed commands.
444 ;; We don't have echos if nntp-open-connection-function
445 ;; is `nntp-open-network-stream', so we skip this in that case.
447 (equal nntp-open-connection-function
448 'nntp-open-network-stream))
449 (nntp-accept-response)
453 (if (looking-at (regexp-quote command))
454 (delete-region pos (progn (forward-line 1)
455 (gnus-point-at-bol))))
457 (nnheader-report 'nntp "Couldn't open connection to %s."
460 (defun nntp-send-command-nodelete (wait-for &rest strings)
461 "Send STRINGS to server and wait until WAIT-FOR returns."
462 (let* ((command (mapconcat 'identity strings " "))
463 (process (nntp-find-connection nntp-server-buffer))
464 (buffer (and process (process-buffer process)))
465 (pos (and buffer (with-current-buffer buffer (point)))))
468 (nntp-retrieve-data command
469 nntp-address nntp-port-number
471 wait-for nnheader-callback-function)
472 ;; If nothing to wait for, still remove possibly echo'ed commands
474 (nntp-accept-response)
478 (if (looking-at (regexp-quote command))
479 (delete-region pos (progn (forward-line 1)
480 (gnus-point-at-bol))))
482 (nnheader-report 'nntp "Couldn't open connection to %s."
485 (defun nntp-send-command-and-decode (wait-for &rest strings)
486 "Send STRINGS to server and wait until WAIT-FOR returns."
487 (when (and (not nnheader-callback-function)
488 (not nntp-inhibit-output))
490 (set-buffer nntp-server-buffer)
492 (let* ((command (mapconcat 'identity strings " "))
493 (process (nntp-find-connection nntp-server-buffer))
494 (buffer (and process (process-buffer process)))
495 (pos (and buffer (with-current-buffer buffer (point)))))
498 (nntp-retrieve-data command
499 nntp-address nntp-port-number
501 wait-for nnheader-callback-function t)
502 ;; If nothing to wait for, still remove possibly echo'ed commands
504 (nntp-accept-response)
508 (if (looking-at (regexp-quote command))
509 (delete-region pos (progn (forward-line 1) (gnus-point-at-bol))))
511 (nnheader-report 'nntp "Couldn't open connection to %s."
515 (defun nntp-send-buffer (wait-for)
516 "Send the current buffer to server and wait until WAIT-FOR returns."
517 (when (and (not nnheader-callback-function)
518 (not nntp-inhibit-output))
520 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
523 (mm-with-unibyte-current-buffer
524 ;; Some encoded unicode text contains character 0x80-0x9f e.g. Euro.
525 (process-send-region (nntp-find-connection nntp-server-buffer)
526 (point-min) (point-max)))
528 nil nntp-address nntp-port-number nntp-server-buffer
529 wait-for nnheader-callback-function))
533 ;;; Interface functions.
535 (nnoo-define-basics nntp)
537 (defsubst nntp-next-result-arrived-p ()
539 ;; A result that starts with a 2xx code is terminated by
540 ;; a line with only a "." on it.
541 ((eq (char-after) ?2)
542 (if (re-search-forward "\n\\.\r?\n" nil t)
545 ;; A result that starts with a 3xx or 4xx code is terminated
548 (if (search-forward "\n" nil t)
556 (defvar nntp-with-open-group-internal nil)
557 (defvar nntp-report-n nil))
559 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
560 "Protect against servers that don't like clients that keep idle connections opens.
561 The problem being that these servers may either close a connection or
562 simply ignore any further requests on a connection. Closed
563 connections are not detected until accept-process-output has updated
564 the process-status. Dropped connections are not detected until the
565 connection timeouts (which may be several minutes) or
566 nntp-connection-timeout has expired. When these occur
567 nntp-with-open-group, opens a new connection then re-issues the NNTP
568 command whose response triggered the error."
569 (when (and (listp connectionless)
570 (not (eq connectionless nil)))
571 (setq forms (cons connectionless forms)
573 `(letf ((nntp-report-n (symbol-function 'nntp-report))
574 ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1))
575 (nntp-with-open-group-internal nil))
576 (while (catch 'nntp-with-open-group-error
577 ;; Open the connection to the server
578 ;; NOTE: Existing connections are NOT tested.
579 (nntp-possibly-change-group ,group ,server ,connectionless)
582 (and nntp-connection-timeout
583 (nnheader-run-at-time
584 nntp-connection-timeout nil
586 (let ((process (nntp-find-connection
589 (process-buffer process))))
590 ;; When I an able to identify the
591 ;; connection to the server AND I've
592 ;; received NO reponse for
593 ;; nntp-connection-timeout seconds.
594 (when (and buffer (eq 0 (buffer-size buffer)))
595 ;; Close the connection. Take no
596 ;; other action as the accept input
597 ;; code will handle the closed
599 (nntp-kill-buffer buffer))))))))
601 (setq nntp-with-open-group-internal
606 (signal 'quit nil))))
608 (nnheader-cancel-timer timer)))
610 (setf (symbol-function 'nntp-report) nntp-report-n))
611 nntp-with-open-group-internal))
613 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
614 "Retrieve the headers of ARTICLES."
615 (nntp-with-open-group
618 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
620 (if (and (not gnus-nov-is-evil)
621 (not nntp-nov-is-evil)
622 (nntp-retrieve-headers-with-xover articles fetch-old))
623 ;; We successfully retrieved the headers via XOVER.
625 ;; XOVER didn't work, so we do it the hard, slow and inefficient
627 (let ((number (length articles))
631 (last-point (point-min))
632 (buf (nntp-find-connection-buffer nntp-server-buffer))
633 (nntp-inhibit-erase t)
635 ;; Send HEAD commands.
636 (while (setq article (pop articles))
639 "HEAD" (if (numberp article)
640 (int-to-string article)
641 ;; `articles' is either a list of article numbers
642 ;; or a list of article IDs.
645 ;; Every 400 requests we have to read the stream in
646 ;; order to avoid deadlocks.
647 (when (or (null articles) ;All requests have been sent.
648 (zerop (% count nntp-maximum-request)))
649 (nntp-accept-response)
652 (goto-char last-point)
654 (while (nntp-next-result-arrived-p)
655 (setq last-point (point))
658 ;; If number of headers is greater than 100, give
659 ;; informative messages.
660 (and (numberp nntp-large-newsgroup)
661 (> number nntp-large-newsgroup)
662 (zerop (% received 20))
663 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
664 (/ (* received 100) number)))
665 (nntp-accept-response))))
666 (and (numberp nntp-large-newsgroup)
667 (> number nntp-large-newsgroup)
668 (nnheader-message 6 "NNTP: Receiving headers...done"))
670 ;; Now all of replies are received. Fold continuation lines.
671 (nnheader-fold-continuation-lines)
672 ;; Remove all "\r"'s.
674 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
677 (deffoo nntp-retrieve-groups (groups &optional server)
678 "Retrieve group info on GROUPS."
679 (nntp-with-open-group
681 (when (nntp-find-connection-buffer nntp-server-buffer)
684 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
685 (set-buffer nntp-server-buffer)
687 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
688 ;; The first time this is run, this variable is `try'. So we
690 (when (eq nntp-server-list-active-group 'try)
691 (nntp-try-list-active (car groups)))
696 (last-point (point-min))
697 (nntp-inhibit-erase t)
698 (buf (nntp-find-connection-buffer nntp-server-buffer))
699 (command (if nntp-server-list-active-group
700 "LIST ACTIVE" "GROUP")))
702 ;; Timeout may have killed the buffer.
703 (unless (gnus-buffer-live-p buf)
704 (nnheader-report 'nntp "Connection to %s is closed." server)
706 ;; Send the command to the server.
707 (nntp-send-command nil command (pop groups))
709 ;; Every 400 requests we have to read the stream in
710 ;; order to avoid deadlocks.
711 (when (or (null groups) ;All requests have been sent.
712 (zerop (% count nntp-maximum-request)))
713 (nntp-accept-response)
714 (while (and (gnus-buffer-live-p buf)
716 ;; Search `blue moon' in this file for the
717 ;; reason why set-buffer here.
719 (goto-char last-point)
721 (while (re-search-forward "^[0-9]" nil t)
723 (setq last-point (point))
725 (nntp-accept-response))))
727 ;; Wait for the reply from the final command.
728 (unless (gnus-buffer-live-p buf)
729 (nnheader-report 'nntp "Connection to %s is closed." server)
732 (goto-char (point-max))
733 (re-search-backward "^[0-9]" nil t)
734 (when (looking-at "^[23]")
735 (while (and (gnus-buffer-live-p buf)
738 (goto-char (point-max))
739 (if (not nntp-server-list-active-group)
740 (not (re-search-backward "\r?\n"
742 (not (re-search-backward "^\\.\r?\n"
744 (nntp-accept-response)))
746 ;; Now all replies are received. We remove CRs.
747 (unless (gnus-buffer-live-p buf)
748 (nnheader-report 'nntp "Connection to %s is closed." server)
751 (goto-char (point-min))
752 (while (search-forward "\r" nil t)
753 (replace-match "" t t))
755 (if (not nntp-server-list-active-group)
757 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
759 ;; We have read active entries, so we just delete the
761 (goto-char (point-min))
762 (while (re-search-forward "^[.2-5]" nil t)
763 (delete-region (match-beginning 0)
764 (progn (forward-line 1) (point))))
765 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
768 (deffoo nntp-retrieve-articles (articles &optional group server)
769 (nntp-with-open-group
772 (let ((number (length articles))
776 (last-point (point-min))
777 (buf (nntp-find-connection-buffer nntp-server-buffer))
778 (nntp-inhibit-erase t)
779 (map (apply 'vector articles))
784 ;; Send ARTICLE command.
785 (while (setq article (pop articles))
788 "ARTICLE" (if (numberp article)
789 (int-to-string article)
790 ;; `articles' is either a list of article numbers
791 ;; or a list of article IDs.
794 ;; Every 400 requests we have to read the stream in
795 ;; order to avoid deadlocks.
796 (when (or (null articles) ;All requests have been sent.
797 (zerop (% count nntp-maximum-request)))
798 (nntp-accept-response)
801 (goto-char last-point)
803 (while (nntp-next-result-arrived-p)
804 (aset map received (cons (aref map received) (point)))
805 (setq last-point (point))
808 ;; If number of headers is greater than 100, give
809 ;; informative messages.
810 (and (numberp nntp-large-newsgroup)
811 (> number nntp-large-newsgroup)
812 (zerop (% received 20))
813 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
814 (/ (* received 100) number)))
815 (nntp-accept-response))))
816 (and (numberp nntp-large-newsgroup)
817 (> number nntp-large-newsgroup)
818 (nnheader-message 6 "NNTP: Receiving articles...done"))
820 ;; Now we have all the responses. We go through the results,
821 ;; wash it and copy it over to the server buffer.
822 (set-buffer nntp-server-buffer)
824 (setq last-point (point-min))
828 (setq point (goto-char (point-max)))
830 (insert-buffer-substring buf last-point (cdr entry))
832 (setq last-point (cdr entry))
835 (cons (car entry) point))
838 (defun nntp-try-list-active (group)
839 (nntp-list-active-group group)
841 (set-buffer nntp-server-buffer)
842 (goto-char (point-min))
844 (looking-at "5[0-9]+"))
845 (setq nntp-server-list-active-group nil))
847 (setq nntp-server-list-active-group t)))))
849 (deffoo nntp-list-active-group (group &optional server)
850 "Return the active info on GROUP (which can be a regexp)."
851 (nntp-with-open-group
853 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
855 (deffoo nntp-request-group-articles (group &optional server)
856 "Return the list of existing articles in GROUP."
857 (nntp-with-open-group
859 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
861 (deffoo nntp-request-article (article &optional group server buffer command)
862 (nntp-with-open-group
864 (when (nntp-send-command-and-decode
865 "\r?\n\\.\r?\n" "ARTICLE"
866 (if (numberp article) (int-to-string article) article))
868 (not (equal buffer nntp-server-buffer)))
870 (set-buffer nntp-server-buffer)
871 (copy-to-buffer buffer (point-min) (point-max))
872 (nntp-find-group-and-number group))
873 (nntp-find-group-and-number group)))))
875 (deffoo nntp-request-head (article &optional group server)
876 (nntp-with-open-group
878 (when (nntp-send-command
879 "\r?\n\\.\r?\n" "HEAD"
880 (if (numberp article) (int-to-string article) article))
882 (nntp-find-group-and-number group)
883 (nntp-decode-text)))))
885 (deffoo nntp-request-body (article &optional group server)
886 (nntp-with-open-group
888 (nntp-send-command-and-decode
889 "\r?\n\\.\r?\n" "BODY"
890 (if (numberp article) (int-to-string article) article))))
892 (deffoo nntp-request-group (group &optional server dont-check)
893 (nntp-with-open-group
895 (when (nntp-send-command "^[245].*\n" "GROUP" group)
896 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
897 (setcar (cddr entry) group)))))
899 (deffoo nntp-close-group (group &optional server)
902 (deffoo nntp-server-opened (&optional server)
903 "Say whether a connection to SERVER has been opened."
904 (and (nnoo-current-server-p 'nntp server)
906 (gnus-buffer-live-p nntp-server-buffer)
907 (nntp-find-connection nntp-server-buffer)))
909 (deffoo nntp-open-server (server &optional defs connectionless)
910 (nnheader-init-server-buffer)
911 (if (nntp-server-opened server)
913 (when (or (stringp (car defs))
914 (numberp (car defs)))
915 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
916 (unless (assq 'nntp-address defs)
917 (setq defs (append defs (list (list 'nntp-address server)))))
918 (nnoo-change-server 'nntp server defs)
919 (unless connectionless
920 (or (nntp-find-connection nntp-server-buffer)
921 (nntp-open-connection nntp-server-buffer)))))
923 (deffoo nntp-close-server (&optional server)
924 (nntp-possibly-change-group nil server t)
925 (let ((process (nntp-find-connection nntp-server-buffer)))
927 (when (memq (process-status process) '(open run))
929 (nntp-send-string process "QUIT")
930 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
931 ;; Ok, this is evil, but when using telnet and stuff
932 ;; as the connection method, it's important that the
933 ;; QUIT command actually is sent out before we kill
936 (nntp-kill-buffer (process-buffer process))
937 (setq process (car (pop nntp-connection-alist))))
938 (nnoo-close-server 'nntp)))
940 (deffoo nntp-request-close ()
942 (while (setq process (pop nntp-connection-list))
943 (when (memq (process-status process) '(open run))
945 (nntp-send-string process "QUIT")
946 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
947 ;; Ok, this is evil, but when using telnet and stuff
948 ;; as the connection method, it's important that the
949 ;; QUIT command actually is sent out before we kill
952 (nntp-kill-buffer (process-buffer process)))))
954 (deffoo nntp-request-list (&optional server)
955 (nntp-with-open-group
957 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
959 (deffoo nntp-request-list-newsgroups (&optional server)
960 (nntp-with-open-group
962 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
964 (deffoo nntp-request-newgroups (date &optional server)
965 (nntp-with-open-group
968 (set-buffer nntp-server-buffer)
969 (let* ((time (date-to-time date))
970 (ls (- (cadr time) (nth 8 (decode-time time)))))
972 (setcar time (1- (car time)))
973 (setcar (cdr time) (+ ls 65536)))
975 (setcar time (1+ (car time)))
976 (setcar (cdr time) (- ls 65536)))
978 (setcar (cdr time) ls)))
981 "^\\.\r?\n" "NEWGROUPS"
982 (format-time-string "%y%m%d %H%M%S" time)
984 (nntp-decode-text))))))
986 (deffoo nntp-request-post (&optional server)
987 (nntp-with-open-group
989 (when (nntp-send-command "^[23].*\r?\n" "POST")
990 (let ((response (with-current-buffer nntp-server-buffer
991 nntp-process-response))
994 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
996 (setq server-id (match-string 1 response))
997 (narrow-to-region (goto-char (point-min))
998 (if (search-forward "\n\n" nil t)
1001 (unless (mail-fetch-field "Message-ID")
1002 (goto-char (point-min))
1003 (insert "Message-ID: " server-id "\n"))
1005 (run-hooks 'nntp-prepare-post-hook)
1006 (nntp-send-buffer "^[23].*\n")))))
1008 (deffoo nntp-request-type (group article)
1011 (deffoo nntp-asynchronous-p ()
1014 ;;; Hooky functions.
1016 (defun nntp-send-mode-reader ()
1017 "Send the MODE READER command to the nntp server.
1018 This function is supposed to be called from `nntp-server-opened-hook'.
1019 It will make innd servers spawn an nnrpd process to allow actual article
1021 (nntp-send-command "^.*\n" "MODE READER"))
1023 (defun nntp-send-authinfo (&optional send-if-force)
1024 "Send the AUTHINFO to the nntp server.
1025 It will look in the \"~/.authinfo\" file for matching entries. If
1026 nothing suitable is found there, it will prompt for a user name
1029 If SEND-IF-FORCE, only send authinfo to the server if the
1030 .authinfo file has the FORCE token."
1031 (let* ((list (gnus-parse-netrc nntp-authinfo-file))
1032 (alist (gnus-netrc-machine list nntp-address "nntp"))
1033 (force (gnus-netrc-get alist "force"))
1034 (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
1035 (passwd (gnus-netrc-get alist "password")))
1036 (when (or (not send-if-force)
1039 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1040 nntp-authinfo-user user))
1041 (unless (member user '(nil ""))
1042 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1043 (when t ;???Should check if AUTHINFO succeeded
1045 "^2.*\r?\n" "AUTHINFO PASS"
1047 nntp-authinfo-password
1048 (setq nntp-authinfo-password
1049 (read-passwd (format "NNTP (%s@%s) password: "
1050 user nntp-address))))))))))
1052 (defun nntp-send-nosy-authinfo ()
1053 "Send the AUTHINFO to the nntp server."
1054 (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
1055 (unless (member user '(nil ""))
1056 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1057 (when t ;???Should check if AUTHINFO succeeded
1058 (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
1059 (read-passwd (format "NNTP (%s@%s) password: "
1060 user nntp-address)))))))
1062 (defun nntp-send-authinfo-from-file ()
1063 "Send the AUTHINFO to the nntp server.
1065 The authinfo login name is taken from the user's login name and the
1066 password contained in '~/.nntp-authinfo'."
1067 (when (file-exists-p "~/.nntp-authinfo")
1069 (insert-file-contents "~/.nntp-authinfo")
1070 (goto-char (point-min))
1071 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
1073 "^2.*\r?\n" "AUTHINFO PASS"
1074 (buffer-substring (point) (gnus-point-at-eol))))))
1076 ;;; Internal functions.
1078 (defun nntp-handle-authinfo (process)
1079 "Take care of an authinfo response from the server."
1080 (let ((last nntp-last-command))
1081 (funcall nntp-authinfo-function)
1082 ;; We have to re-send the function that was interrupted by
1083 ;; the authinfo request.
1085 (set-buffer nntp-server-buffer)
1087 (nntp-send-string process last)))
1089 (defun nntp-make-process-buffer (buffer)
1090 "Create a new, fresh buffer usable for nntp process connections."
1093 (generate-new-buffer
1094 (format " *server %s %s %s*"
1095 nntp-address nntp-port-number
1096 (gnus-buffer-exists-p buffer))))
1097 (mm-enable-multibyte)
1098 (set (make-local-variable 'after-change-functions) nil)
1099 (set (make-local-variable 'nntp-process-wait-for) nil)
1100 (set (make-local-variable 'nntp-process-callback) nil)
1101 (set (make-local-variable 'nntp-process-to-buffer) nil)
1102 (set (make-local-variable 'nntp-process-start-point) nil)
1103 (set (make-local-variable 'nntp-process-decode) nil)
1106 (defun nntp-open-connection (buffer)
1107 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1108 (run-hooks 'nntp-prepare-server-hook)
1109 (let* ((pbuffer (nntp-make-process-buffer buffer))
1111 (and nntp-connection-timeout
1112 (nnheader-run-at-time
1113 nntp-connection-timeout nil
1115 (nntp-kill-buffer ,pbuffer)))))
1118 (let ((coding-system-for-read nntp-coding-system-for-read)
1119 (coding-system-for-write nntp-coding-system-for-write))
1120 (funcall nntp-open-connection-function pbuffer))
1123 (message "Quit opening connection")
1124 (nntp-kill-buffer pbuffer)
1128 (nnheader-cancel-timer timer))
1130 (nntp-kill-buffer pbuffer))
1131 (when (and (buffer-name pbuffer)
1133 (process-kill-without-query process)
1134 (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1135 (memq (process-status process) '(open run)))
1137 (caar (push (list process buffer nil) nntp-connection-alist))
1138 (push process nntp-connection-list)
1140 (set-buffer pbuffer)
1141 (nntp-read-server-type)
1143 (set-buffer nntp-server-buffer)
1144 (let ((nnheader-callback-function nil))
1145 (run-hooks 'nntp-server-opened-hook)
1146 (nntp-send-authinfo t))))
1147 (nntp-kill-buffer (process-buffer process))
1150 (defun nntp-open-network-stream (buffer)
1151 (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
1153 (defun nntp-open-ssl-stream (buffer)
1154 (let* ((process-connection-type nil)
1155 (proc (start-process "nntpd" buffer
1157 shell-command-switch
1158 (format-spec nntp-ssl-program
1161 ?p nntp-port-number)))))
1162 (process-kill-without-query proc)
1165 (let ((nntp-connection-alist (list proc buffer nil)))
1166 (nntp-wait-for-string "^\r*20[01]"))
1168 (delete-region (point-min) (point))
1171 (defun nntp-open-tls-stream (buffer)
1172 (let ((proc (open-tls-stream "nntpd" buffer nntp-address nntp-port-number)))
1173 (process-kill-without-query proc)
1176 (let ((nntp-connection-alist (list proc buffer nil)))
1177 (nntp-wait-for-string "^\r*20[01]"))
1179 (delete-region (point-min) (point))
1182 (defun nntp-read-server-type ()
1183 "Find out what the name of the server we have connected to is."
1184 ;; Wait for the status string to arrive.
1185 (setq nntp-server-type (buffer-string))
1186 (let ((alist nntp-server-action-alist)
1187 (case-fold-search t)
1189 ;; Run server-specific commands.
1191 (setq entry (pop alist))
1192 (when (string-match (car entry) nntp-server-type)
1193 (if (and (listp (cadr entry))
1194 (not (eq 'lambda (caadr entry))))
1196 (funcall (cadr entry)))))))
1198 (defun nntp-async-wait (process wait-for buffer decode callback)
1200 (set-buffer (process-buffer process))
1201 (unless nntp-inside-change-function
1203 (setq nntp-process-wait-for wait-for
1204 nntp-process-to-buffer buffer
1205 nntp-process-decode decode
1206 nntp-process-callback callback
1207 nntp-process-start-point (point-max))
1208 (setq after-change-functions '(nntp-after-change-function))
1209 (if nntp-async-needs-kluge
1210 (nntp-async-kluge process))))
1212 (defun nntp-async-kluge (process)
1213 ;; emacs 20.3 bug: process output with encoding 'binary
1214 ;; doesn't trigger after-change-functions.
1215 (unless nntp-async-timer
1216 (setq nntp-async-timer
1217 (nnheader-run-at-time 1 1 'nntp-async-timer-handler)))
1218 (add-to-list 'nntp-async-process-list process))
1220 (defun nntp-async-timer-handler ()
1223 (if (memq (process-status proc) '(open run))
1224 (nntp-async-trigger proc)
1225 (nntp-async-stop proc)))
1226 nntp-async-process-list))
1228 (defun nntp-async-stop (proc)
1229 (setq nntp-async-process-list (delq proc nntp-async-process-list))
1230 (when (and nntp-async-timer (not nntp-async-process-list))
1231 (nnheader-cancel-timer nntp-async-timer)
1232 (setq nntp-async-timer nil)))
1234 (defun nntp-after-change-function (beg end len)
1236 ;; we only care about insertions at eob
1237 (when (and (eq 0 len) (eq (point-max) end))
1239 (let ((proc (get-buffer-process (current-buffer))))
1241 (nntp-async-trigger proc)))))
1242 ;; any throw from after-change-functions will leave it
1243 ;; set to nil. so we reset it here, if necessary.
1245 (setq after-change-functions '(nntp-after-change-function)))))
1247 (defun nntp-async-trigger (process)
1249 (set-buffer (process-buffer process))
1250 (when nntp-process-callback
1251 ;; do we have an error message?
1252 (goto-char nntp-process-start-point)
1253 (if (memq (following-char) '(?4 ?5))
1254 ;; wants credentials?
1255 (if (looking-at "480")
1256 (nntp-handle-authinfo process)
1257 ;; report error message.
1258 (nntp-snarf-error-message)
1259 (nntp-do-callback nil))
1261 ;; got what we expect?
1262 (goto-char (point-max))
1263 (when (re-search-backward
1264 nntp-process-wait-for nntp-process-start-point t)
1265 (let ((response (match-string 0)))
1266 (with-current-buffer nntp-server-buffer
1267 (setq nntp-process-response response)))
1268 (nntp-async-stop process)
1270 (when (gnus-buffer-exists-p nntp-process-to-buffer)
1271 (let ((buf (current-buffer))
1272 (start nntp-process-start-point)
1273 (decode nntp-process-decode))
1275 (set-buffer nntp-process-to-buffer)
1276 (goto-char (point-max))
1278 (narrow-to-region (point) (point))
1279 (insert-buffer-substring buf start)
1281 (nntp-decode-text))))))
1283 (goto-char (point-max))
1285 (buffer-name (get-buffer nntp-process-to-buffer))))))))
1287 (defun nntp-do-callback (arg)
1288 (let ((callback nntp-process-callback)
1289 (nntp-inside-change-function t))
1290 (setq nntp-process-callback nil)
1291 (funcall callback arg)))
1293 (defun nntp-snarf-error-message ()
1294 "Save the error message in the current buffer."
1295 (let ((message (buffer-string)))
1296 (while (string-match "[\r\n]+" message)
1297 (setq message (replace-match " " t t message)))
1298 (nnheader-report 'nntp message)
1301 (defun nntp-accept-process-output (process)
1302 "Wait for output from PROCESS and message some dots."
1304 (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1305 nntp-server-buffer))
1306 (let ((len (/ (buffer-size) 1024))
1309 (setq nntp-have-messaged t)
1310 (nnheader-message 7 "nntp read: %dk" len)))
1311 (accept-process-output
1313 (truncate nntp-read-timeout)
1314 (truncate (* (- nntp-read-timeout
1315 (truncate nntp-read-timeout))
1317 ;; accept-process-output may update status of process to indicate
1318 ;; that the server has closed the connection. This MUST be
1319 ;; handled here as the buffer restored by the save-excursion may
1320 ;; be the process's former output buffer (i.e. now killed)
1322 (memq (process-status process) '(open run)))
1323 (nntp-report "Server closed connection"))))
1325 (defun nntp-accept-response ()
1326 "Wait for output from the process that outputs to BUFFER."
1327 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1329 (defun nntp-possibly-change-group (group server &optional connectionless)
1330 (let ((nnheader-callback-function nil))
1332 (or (nntp-server-opened server)
1333 (nntp-open-server server nil connectionless)))
1335 (unless connectionless
1336 (or (nntp-find-connection nntp-server-buffer)
1337 (nntp-open-connection nntp-server-buffer))))
1340 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1342 (nntp-report "Server closed connection"))
1343 ((not (equal group (caddr entry)))
1345 (set-buffer (process-buffer (car entry)))
1347 (nntp-send-command "^[245].*\n" "GROUP" group)
1348 (setcar (cddr entry) group)
1351 (set-buffer nntp-server-buffer)
1352 (erase-buffer))))))))
1354 (defun nntp-decode-text (&optional cr-only)
1355 "Decode the text in the current buffer."
1356 (goto-char (point-min))
1357 (while (search-forward "\r" nil t)
1360 ;; Remove trailing ".\n" end-of-transfer marker.
1361 (goto-char (point-max))
1363 (when (looking-at ".\n")
1365 ;; Delete status line.
1366 (goto-char (point-min))
1367 (while (looking-at "[1-5][0-9][0-9] .*\n")
1368 ;; For some unknown reason, there is more than one status line.
1369 (delete-region (point) (progn (forward-line 1) (point))))
1370 ;; Remove "." -> ".." encoding.
1371 (while (search-forward "\n.." nil t)
1374 (defun nntp-encode-text ()
1375 "Encode the text in the current buffer."
1377 ;; Replace "." at beginning of line with "..".
1378 (goto-char (point-min))
1379 (while (re-search-forward "^\\." nil t)
1381 (goto-char (point-max))
1382 ;; Insert newline at the end of the buffer.
1385 ;; Insert `.' at end of buffer (end of text mark).
1386 (goto-char (point-max))
1388 (goto-char (point-min))
1392 (insert nntp-end-of-line))))
1394 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1395 (set-buffer nntp-server-buffer)
1399 ;; This server does not talk NOV.
1400 ((not nntp-server-xover)
1403 ;; We don't care about gaps.
1404 ((or (not nntp-nov-gap)
1406 (nntp-send-xover-command
1408 (if (numberp fetch-old)
1409 (max 1 (- (car articles) fetch-old))
1412 (car (last articles)) 'wait)
1414 (goto-char (point-min))
1415 (when (looking-at "[1-5][0-9][0-9] .*\n")
1416 (delete-region (point) (progn (forward-line 1) (point))))
1417 (while (search-forward "\r" nil t)
1418 (replace-match "" t t))
1419 (goto-char (point-max))
1421 (when (looking-at "\\.")
1422 (delete-region (point) (progn (forward-line 1) (point)))))
1424 ;; We do it the hard way. For each gap, an XOVER command is sent
1425 ;; to the server. We do not wait for a reply from the server, we
1426 ;; just send them off as fast as we can. That means that we have
1427 ;; to count the number of responses we get back to find out when we
1428 ;; have gotten all we asked for.
1429 ((numberp nntp-nov-gap)
1434 (buf nntp-server-buffer)
1435 (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1438 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1439 ;; that means that the server does not understand XOVER, but we
1440 ;; won't know that until we try.
1441 (while (and nntp-server-xover articles)
1442 (setq first (car articles))
1443 ;; Search forward until we find a gap, or until we run out of
1445 (while (and (cdr articles)
1446 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1447 (setq articles (cdr articles)))
1449 (setq in-process-buffer-p (stringp nntp-server-xover))
1450 (nntp-send-xover-command first (setq last (car articles)))
1451 (setq articles (cdr articles))
1453 (when (and nntp-server-xover in-process-buffer-p)
1454 ;; Don't count tried request.
1455 (setq count (1+ count))
1457 ;; Every 400 requests we have to read the stream in
1458 ;; order to avoid deadlocks.
1459 (when (or (null articles) ;All requests have been sent.
1460 (= 1 (% count nntp-maximum-request)))
1462 (nntp-accept-response)
1463 ;; On some Emacs versions the preceding function has a
1464 ;; tendency to change the buffer. Perhaps. It's quite
1465 ;; difficult to reproduce, because it only seems to happen
1466 ;; once in a blue moon.
1467 (set-buffer process-buffer)
1469 (goto-char (or last-point (point-min)))
1471 (while (re-search-forward "^[0-9][0-9][0-9] .*\n" nil t)
1473 (setq last-point (point))
1474 (or (< received count)
1475 ;; I haven't started reading the final response
1477 (goto-char (point-max))
1479 (not (looking-at "^\\.\r?\n")))))
1480 ;; I haven't read the end of the final response
1481 (nntp-accept-response)
1482 (set-buffer process-buffer))))
1484 ;; Some nntp servers seem to have an extension to the XOVER
1485 ;; extension. On these servers, requesting an article range
1486 ;; preceeding the active range does not return an error as
1487 ;; specified in the RFC. What we instead get is the NOV entry
1488 ;; for the first available article. Obviously, a client can
1489 ;; use that entry to avoid making unnecessary requests. The
1490 ;; only problem is for a client that assumes that the response
1491 ;; will always be within the requested ranage. For such a
1492 ;; client, we can get N copies of the same entry (one for each
1493 ;; XOVER command sent to the server).
1496 (goto-char (point-min))
1497 (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1498 (let ((low-limit (string-to-int
1499 (buffer-substring (match-beginning 1)
1501 (while (and articles (<= (car articles) low-limit))
1502 (setq articles (cdr articles))))))
1505 (when nntp-server-xover
1506 (when in-process-buffer-p
1508 (goto-char (point-max))
1509 (insert-buffer-substring process-buffer)
1510 (set-buffer process-buffer)
1514 ;; We remove any "." lines and status lines.
1515 (goto-char (point-min))
1516 (while (search-forward "\r" nil t)
1518 (goto-char (point-min))
1519 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1524 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1525 "Send the XOVER command to the server."
1526 (let ((range (format "%d-%d" beg end))
1527 (nntp-inhibit-erase t))
1528 (if (stringp nntp-server-xover)
1529 ;; If `nntp-server-xover' is a string, then we just send this
1532 (nntp-send-command-nodelete
1533 "\r?\n\\.\r?\n" nntp-server-xover range)
1534 ;; We do not wait for the reply.
1535 (nntp-send-command-nodelete nil nntp-server-xover range))
1536 (let ((commands nntp-xover-commands))
1537 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1538 ;; We try them all until we get at positive response.
1539 (while (and commands (eq nntp-server-xover 'try))
1540 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1542 (set-buffer nntp-server-buffer)
1543 (goto-char (point-min))
1544 (and (looking-at "[23]") ; No error message.
1545 ;; We also have to look at the lines. Some buggy
1546 ;; servers give back simple lines with just the
1547 ;; article number. How... helpful.
1550 (looking-at "[0-9]+\t...")) ; More text after number.
1551 (setq nntp-server-xover (car commands))))
1552 (setq commands (cdr commands)))
1553 ;; If none of the commands worked, we disable XOVER.
1554 (when (eq nntp-server-xover 'try)
1556 (set-buffer nntp-server-buffer)
1558 (setq nntp-server-xover nil)))
1559 nntp-server-xover))))
1561 (defun nntp-find-group-and-number (&optional group)
1564 (set-buffer nntp-server-buffer)
1565 (narrow-to-region (goto-char (point-min))
1566 (or (search-forward "\n\n" nil t) (point-max)))
1567 (goto-char (point-min))
1568 ;; We first find the number by looking at the status line.
1569 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1571 (buffer-substring (match-beginning 1)
1574 (and number (zerop number) (setq number nil))
1576 ;; Then we find the group name.
1579 ;; If there is only one group in the Newsgroups
1580 ;; header, then it seems quite likely that this
1581 ;; article comes from that group, I'd say.
1582 ((and (setq newsgroups
1583 (mail-fetch-field "newsgroups"))
1584 (not (string-match "," newsgroups)))
1586 ;; If there is more than one group in the
1587 ;; Newsgroups header, then the Xref header should
1588 ;; be filled out. We hazard a guess that the group
1589 ;; that has this article number in the Xref header
1590 ;; is the one we are looking for. This might very
1591 ;; well be wrong if this article happens to have
1592 ;; the same number in several groups, but that's
1594 ((and (setq xref (mail-fetch-field "xref"))
1597 (format "\\([^ :]+\\):%d" number) xref))
1598 (match-string 1 xref))
1601 ((and (setq xref (mail-fetch-field "xref"))
1604 (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1605 "\\([^ :]+\\):\\([0-9]+\\)")
1607 (setq group (match-string 1 xref)
1608 number (string-to-int (match-string 2 xref))))
1609 ((and (setq newsgroups
1610 (mail-fetch-field "newsgroups"))
1611 (not (string-match "," newsgroups)))
1612 (setq group newsgroups))
1614 (t (setq group ""))))
1615 (when (string-match "\r" group)
1616 (setq group (substring group 0 (match-beginning 0))))
1617 (cons group number)))))
1619 (defun nntp-wait-for-string (regexp)
1620 "Wait until string arrives in the buffer."
1621 (let ((buf (current-buffer))
1623 (goto-char (point-min))
1624 (while (and (setq proc (get-buffer-process buf))
1625 (memq (process-status proc) '(open run))
1626 (not (re-search-forward regexp nil t)))
1627 (accept-process-output proc)
1629 (goto-char (point-min)))))
1632 ;; ==========================================================================
1633 ;; Obsolete nntp-open-* connection methods -- drv
1634 ;; ==========================================================================
1636 (defvoo nntp-open-telnet-envuser nil
1637 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1639 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
1640 "*Regular expression to match the shell prompt on the remote machine.")
1642 (defvoo nntp-rlogin-program "rsh"
1643 "*Program used to log in on remote machines.
1644 The default is \"rsh\", but \"ssh\" is a popular alternative.")
1646 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1647 "*Parameters to `nntp-open-rlogin'.
1648 That function may be used as `nntp-open-connection-function'. In that
1649 case, this list will be used as the parameter list given to rsh.")
1651 (defvoo nntp-rlogin-user-name nil
1652 "*User name on remote system when using the rlogin connect method.")
1654 (defvoo nntp-telnet-parameters
1655 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1656 "*Parameters to `nntp-open-telnet'.
1657 That function may be used as `nntp-open-connection-function'. In that
1658 case, this list will be executed as a command after logging in
1661 (defvoo nntp-telnet-user-name nil
1662 "User name to log in via telnet with.")
1664 (defvoo nntp-telnet-passwd nil
1665 "Password to use to log in via telnet with.")
1667 (defun nntp-open-telnet (buffer)
1673 "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1674 (case-fold-search t))
1675 (when (memq (process-status proc) '(open run))
1676 (nntp-wait-for-string "^r?telnet")
1677 (process-send-string proc "set escape \^X\n")
1679 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1680 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1681 nntp-address "\n")))
1683 (process-send-string proc (concat "open " nntp-address "\n"))))
1685 ((not nntp-open-telnet-envuser)
1686 (nntp-wait-for-string "^\r*.?login:")
1687 (process-send-string
1689 (or nntp-telnet-user-name
1690 (setq nntp-telnet-user-name (read-string "login: ")))
1692 (nntp-wait-for-string "^\r*.?password:")
1693 (process-send-string
1695 (or nntp-telnet-passwd
1696 (setq nntp-telnet-passwd
1697 (read-passwd "Password: ")))
1699 (nntp-wait-for-string nntp-telnet-shell-prompt)
1700 (process-send-string
1701 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1702 (nntp-wait-for-string "^\r*20[01]")
1704 (delete-region (point-min) (point))
1705 (process-send-string proc "\^]")
1706 (nntp-wait-for-string "^r?telnet")
1707 (process-send-string proc "mode character\n")
1708 (accept-process-output proc 1)
1710 (goto-char (point-min))
1712 (delete-region (point) (point-max)))
1715 (defun nntp-open-rlogin (buffer)
1716 "Open a connection to SERVER using rsh."
1717 (let ((proc (if nntp-rlogin-user-name
1718 (apply 'start-process
1719 "nntpd" buffer nntp-rlogin-program
1720 nntp-address "-l" nntp-rlogin-user-name
1721 nntp-rlogin-parameters)
1722 (apply 'start-process
1723 "nntpd" buffer nntp-rlogin-program nntp-address
1724 nntp-rlogin-parameters))))
1727 (nntp-wait-for-string "^\r*20[01]")
1729 (delete-region (point-min) (point))
1733 ;; ==========================================================================
1734 ;; Replacements for the nntp-open-* functions -- drv
1735 ;; ==========================================================================
1737 (defun nntp-open-telnet-stream (buffer)
1738 "Open a nntp connection by telnet'ing the news server.
1740 Please refer to the following variables to customize the connection:
1741 - `nntp-pre-command',
1742 - `nntp-telnet-command',
1743 - `nntp-telnet-switches',
1745 - `nntp-port-number',
1746 - `nntp-end-of-line'."
1747 (let ((command `(,nntp-telnet-command
1748 ,@nntp-telnet-switches
1749 ,nntp-address ,nntp-port-number))
1751 (and nntp-pre-command
1752 (push nntp-pre-command command))
1753 (setq proc (apply 'start-process "nntpd" buffer command))
1756 (nntp-wait-for-string "^\r*20[01]")
1758 (delete-region (point-min) (point))
1761 (defun nntp-open-via-rlogin-and-telnet (buffer)
1762 "Open a connection to an nntp server through an intermediate host.
1763 First rlogin to the remote host, and then telnet the real news server
1766 Please refer to the following variables to customize the connection:
1767 - `nntp-pre-command',
1768 - `nntp-via-rlogin-command',
1769 - `nntp-via-rlogin-command-switches',
1770 - `nntp-via-user-name',
1771 - `nntp-via-address',
1772 - `nntp-telnet-command',
1773 - `nntp-telnet-switches',
1775 - `nntp-port-number',
1776 - `nntp-end-of-line'."
1777 (let ((command `(,nntp-via-address
1778 ,nntp-telnet-command
1779 ,@nntp-telnet-switches))
1781 (when nntp-via-user-name
1782 (setq command `("-l" ,nntp-via-user-name ,@command)))
1783 (when nntp-via-rlogin-command-switches
1784 (setq command (append nntp-via-rlogin-command-switches command)))
1785 (push nntp-via-rlogin-command command)
1786 (and nntp-pre-command
1787 (push nntp-pre-command command))
1788 (setq proc (apply 'start-process "nntpd" buffer command))
1791 (nntp-wait-for-string "^r?telnet")
1792 (process-send-string proc (concat "open " nntp-address
1793 " " nntp-port-number "\n"))
1794 (nntp-wait-for-string "^\r*20[01]")
1796 (delete-region (point-min) (point))
1799 (defun nntp-open-via-telnet-and-telnet (buffer)
1800 "Open a connection to an nntp server through an intermediate host.
1801 First telnet the remote host, and then telnet the real news server
1804 Please refer to the following variables to customize the connection:
1805 - `nntp-pre-command',
1806 - `nntp-via-telnet-command',
1807 - `nntp-via-telnet-switches',
1808 - `nntp-via-address',
1809 - `nntp-via-envuser',
1810 - `nntp-via-user-name',
1811 - `nntp-via-user-password',
1812 - `nntp-via-shell-prompt',
1813 - `nntp-telnet-command',
1814 - `nntp-telnet-switches',
1816 - `nntp-port-number',
1817 - `nntp-end-of-line'."
1821 (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
1822 (case-fold-search t)
1824 (and nntp-pre-command (push nntp-pre-command command))
1825 (setq proc (apply 'start-process "nntpd" buffer command))
1826 (when (memq (process-status proc) '(open run))
1827 (nntp-wait-for-string "^r?telnet")
1828 (process-send-string proc "set escape \^X\n")
1830 ((and nntp-via-envuser nntp-via-user-name)
1831 (process-send-string proc (concat "open " "-l" nntp-via-user-name
1832 nntp-via-address "\n")))
1834 (process-send-string proc (concat "open " nntp-via-address
1836 (when (not nntp-via-envuser)
1837 (nntp-wait-for-string "^\r*.?login:")
1838 (process-send-string proc
1840 (or nntp-via-user-name
1841 (setq nntp-via-user-name
1842 (read-string "login: ")))
1844 (nntp-wait-for-string "^\r*.?password:")
1845 (process-send-string proc
1847 (or nntp-via-user-password
1848 (setq nntp-via-user-password
1849 (read-passwd "Password: ")))
1851 (nntp-wait-for-string nntp-via-shell-prompt)
1852 (let ((real-telnet-command `("exec"
1853 ,nntp-telnet-command
1854 ,@nntp-telnet-switches
1856 ,nntp-port-number)))
1857 (process-send-string proc
1858 (concat (mapconcat 'identity
1859 real-telnet-command " ")
1861 (nntp-wait-for-string "^\r*20[01]")
1863 (delete-region (point-min) (point))
1864 (process-send-string proc "\^]")
1865 (nntp-wait-for-string "^r?telnet")
1866 (process-send-string proc "mode character\n")
1867 (accept-process-output proc 1)
1869 (goto-char (point-min))
1871 (delete-region (point) (point-max)))
1876 ;;; nntp.el ends here