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>
7 ;; Katsumi Yamaoka <yamaoka@jpl.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published
14 ;; by the Free Software Foundation; either version 2, or (at your
15 ;; option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 (eval-when-compile (require 'cl))
31 (eval-when-compile (require 'gnus-clfns))
39 (defvoo nntp-address nil
40 "Address of the physical nntp server.")
42 (defvoo nntp-port-number "nntp"
43 "Port number on the physical nntp server.")
45 (defvoo nntp-list-options nil
46 "List of newsgroup name used for a option of the LIST command to
47 restrict the listing output to only the specified newsgroups.
48 Each newsgroup name can be a shell-style wildcard, for instance,
49 \"fj.*\", \"japan.*\", etc. Fortunately, if the server can accept
50 such a option, it will probably make gnus run faster. You may
51 use it as a server variable as follows:
53 \(setq gnus-select-method
54 '(nntp \"news.somewhere.edu\"
55 (nntp-list-options (\"fj.*\" \"japan.*\"))))")
57 (defvoo nntp-options-subscribe nil
58 "Regexp matching the newsgroup names which will be subscribed
59 unconditionally. It may be effective as well as `nntp-list-options'
60 even though the server could not accept a shell-style wildcard as a
61 option of the LIST command. You may use it as a server variable as
64 \(setq gnus-select-method
65 '(nntp \"news.somewhere.edu\"
66 (nntp-options-subscribe \"^fj\\\\.\\\\|^japan\\\\.\")))")
68 (defvoo nntp-options-not-subscribe nil
69 "Regexp matching the newsgroup names which will not be subscribed
70 unconditionally. It may be effective as well as `nntp-list-options'
71 even though the server could not accept a shell-style wildcard as a
72 option of the LIST command. You may use it as a server variable as
75 \(setq gnus-select-method
76 '(nntp \"news.somewhere.edu\"
77 (nntp-options-not-subscribe \"\\\\.binaries\\\\.\")))")
79 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
80 "*Hook used for sending commands to the server at startup.
81 The default value is `nntp-send-mode-reader', which makes an innd
82 server spawn an nnrpd server.")
84 (defvoo nntp-authinfo-function 'nntp-send-authinfo
85 "Function used to send AUTHINFO to the server.
86 It is called with no parameters.")
88 (defvoo nntp-server-action-alist
89 '(("nntpd 1\\.5\\.11t"
90 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
91 ("NNRP server Netscape"
92 (setq nntp-server-list-active-group nil)))
93 "Alist of regexps to match on server types and actions to be taken.
94 For instance, if you want Gnus to beep every time you connect
95 to innd, you could say something like:
97 \(setq nntp-server-action-alist
100 You probably don't want to do that, though.")
102 (defvoo nntp-open-connection-function 'nntp-open-network-stream
103 "*Function used for connecting to a remote system.
104 It will be called with the buffer to output in as argument.
106 Currently, five such functions are provided (please refer to their
107 respective doc string for more information), three of them establishing
108 direct connections to the nntp server, and two of them using an indirect
112 - `nntp-open-network-stream' (the default),
113 - `nntp-open-ssl-stream',
114 - `nntp-open-telnet-stream'.
116 Indirect connections:
117 - `nntp-open-via-rlogin-and-telnet',
118 - `nntp-open-via-telnet-and-telnet'.")
120 (defvoo nntp-pre-command nil
121 "*Pre-command to use with the various nntp-open-via-* methods.
122 This is where you would put \"runsocks\" or stuff like that.")
124 (defvoo nntp-telnet-command "telnet"
125 "*Telnet command used to connect to the nntp server.
126 This command is used by the various nntp-open-via-* methods.")
128 (defvoo nntp-telnet-switches '("-8")
129 "*Switches given to the telnet command `nntp-telnet-command'.")
131 (defvoo nntp-end-of-line "\r\n"
132 "*String to use on the end of lines when talking to the NNTP server.
133 This is \"\\r\\n\" by default, but should be \"\\n\" when
134 using and indirect connection method (nntp-open-via-*).")
136 (defvoo nntp-via-rlogin-command "rsh"
137 "*Rlogin command used to connect to an intermediate host.
138 This command is used by the `nntp-open-via-rlogin-and-telnet' method.
139 The default is \"rsh\", but \"ssh\" is a popular alternative.")
141 (defvoo nntp-via-rlogin-command-switches nil
142 "*Switches given to the rlogin command `nntp-via-rlogin-command'.
143 If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
144 \(\"-C\") in order to compress all data connections, otherwise set this
145 to \(\"-t\") or (\"-C\" \"-t\") if the telnet command requires a pseudo-tty
146 allocation on an intermediate host.")
148 (defvoo nntp-via-telnet-command "telnet"
149 "*Telnet command used to connect to an intermediate host.
150 This command is used by the `nntp-open-via-telnet-and-telnet' method.")
152 (defvoo nntp-via-telnet-switches '("-8")
153 "*Switches given to the telnet command `nntp-via-telnet-command'.")
155 (defvoo nntp-via-user-name nil
156 "*User name to log in on an intermediate host with.
157 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
159 (defvoo nntp-via-user-password nil
160 "*Password to use to log in on an intermediate host with.
161 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
163 (defvoo nntp-via-address nil
164 "*Address of an intermediate host to connect to.
165 This variable is used by the `nntp-open-via-rlogin-and-telnet' and
166 `nntp-open-via-telnet-and-telnet' methods.")
168 (defvoo nntp-via-envuser nil
169 "*Whether both telnet client and server support the ENVIRON option.
170 If non-nil, there will be no prompt for a login name.")
172 (defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
173 "*Regular expression to match the shell prompt on an intermediate host.
174 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
176 (defvoo nntp-large-newsgroup 50
177 "*The number of the articles which indicates a large newsgroup.
178 If the number of the articles is greater than the value, verbose
179 messages will be shown to indicate the current status.")
181 (defvoo nntp-maximum-request 400
182 "*The maximum number of the requests sent to the NNTP server at one time.
183 If Emacs hangs up while retrieving headers, set the variable to a
186 (defvoo nntp-nov-is-evil nil
187 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
189 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
190 "*List of strings that are used as commands to fetch NOV lines from a server.
191 The strings are tried in turn until a positive response is gotten. If
192 none of the commands are successful, nntp will just grab headers one
195 (defvoo nntp-nov-gap 5
196 "*Maximum allowed gap between two articles.
197 If the gap between two consecutive articles is bigger than this
198 variable, split the XOVER request into two requests.")
200 (defvoo nntp-prepare-server-hook nil
201 "*Hook run before a server is opened.
202 If can be used to set up a server remotely, for instance. Say you
203 have an account at the machine \"other.machine\". This machine has
204 access to an NNTP server that you can't access locally. You could
205 then use this hook to rsh to the remote machine and start a proxy NNTP
206 server there that you can connect to. See also
207 `nntp-open-connection-function'")
209 (defvoo nntp-warn-about-losing-connection t
210 "*If non-nil, beep when a server closes connection.")
212 (defcustom nntp-authinfo-file "~/.authinfo"
213 ".netrc-like file that holds nntp authinfo passwords."
216 (repeat :tag "Entries"
219 :value ("" ("login" . "") ("password" . ""))
223 (const :format "" "login")
224 (string :format "Login: %v"))
226 (const :format "" "password")
227 (string :format "Password: %v")))))))
231 (defvoo nntp-connection-timeout nil
232 "*Number of seconds to wait before an nntp connection times out.
233 If this variable is nil, which is the default, no timers are set.
234 NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
236 (defvoo nntp-prepare-post-hook nil
237 "*Hook run just before posting an article. It is supposed to be used
238 to insert Cancel-Lock headers.")
240 (defvoo nntp-read-timeout (if (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
241 (symbol-name system-type))
244 "How long nntp should wait between checking for the end of output.
245 Shorter values mean quicker response, but is more CPU intensive.")
247 ;;; Internal variables.
249 (defvar nntp-record-commands nil
250 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
252 (defvar nntp-have-messaged nil)
254 (defvar nntp-process-wait-for nil)
255 (defvar nntp-process-to-buffer nil)
256 (defvar nntp-process-callback nil)
257 (defvar nntp-process-decode nil)
258 (defvar nntp-process-start-point nil)
259 (defvar nntp-inside-change-function nil)
260 (defvoo nntp-last-command-time nil)
261 (defvoo nntp-last-command nil)
262 (defvoo nntp-authinfo-password nil)
263 (defvoo nntp-authinfo-user nil)
265 (defvar nntp-connection-list nil)
267 (defvoo nntp-server-type nil)
268 (defvoo nntp-connection-alist nil)
269 (defvoo nntp-status-string "")
270 (defconst nntp-version "nntp 5.0")
271 (defvoo nntp-inhibit-erase nil)
272 (defvoo nntp-inhibit-output nil)
274 (defvoo nntp-server-xover 'try)
275 (defvoo nntp-server-list-active-group 'try)
277 (defvar nntp-async-needs-kluge
278 (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
279 "*When non-nil, nntp will poll asynchronous connections
280 once a second. By default, this is turned on only for Emacs
281 20.3, which has a bug that breaks nntp's normal method of
282 noticing asynchronous data.")
284 (defvar nntp-async-timer nil)
285 (defvar nntp-async-process-list nil)
287 (defvar nntp-ssl-program
288 "openssl s_client -quiet -ssl3 -connect %s:%p"
289 "A string containing commands for SSL connections.
290 Within a string, %s is replaced with the server address and %p with
291 port number on server. The program should accept IMAP commands on
292 stdin and return responses to stdout.")
296 ;;; Internal functions.
298 (defsubst nntp-send-string (process string)
299 "Send STRING to PROCESS."
300 ;; We need to store the time to provide timeouts, and
301 ;; to store the command so the we can replay the command
302 ;; if the server gives us an AUTHINFO challenge.
303 (setq nntp-last-command-time (current-time)
304 nntp-last-command string)
305 (when nntp-record-commands
306 (nntp-record-command string))
307 (process-send-string process (concat string nntp-end-of-line))
308 (or (memq (process-status process) '(open run))
309 (nntp-report "Server closed connection")))
311 (defun nntp-record-command (string)
312 "Record the command STRING."
314 (set-buffer (get-buffer-create "*nntp-log*"))
315 (goto-char (point-max))
316 (let ((time (current-time)))
317 (insert (format-time-string "%Y%m%dT%H%M%S" time)
318 "." (format "%03d" (/ (nth 2 time) 1000))
319 " " nntp-address " " string "\n"))))
321 (defun nntp-report (&rest args)
322 "Report an error from the nntp backend. The first string in ARGS
323 can be a format string. For some commands, the failed command may be
324 retried once before actually displaying the error report."
326 (when nntp-record-commands
327 (nntp-record-command "*** CALLED nntp-report ***"))
329 (nnheader-report 'nntp args)
333 (defun nntp-report-1 (&rest args)
334 "Throws out to nntp-with-open-group-error so that the connection may
335 be restored and the command retried."
337 (when nntp-record-commands
338 (nntp-record-command "*** CONNECTION LOST ***"))
340 (throw 'nntp-with-open-group-error t))
342 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
343 "Wait for WAIT-FOR to arrive from PROCESS."
345 (set-buffer (process-buffer process))
346 (goto-char (point-min))
347 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
349 (memq (process-status process) '(open run)))
350 (when (looking-at "480")
351 (nntp-handle-authinfo process))
352 (when (looking-at "^.*\n")
353 (delete-region (point) (progn (forward-line 1) (point))))
354 (nntp-accept-process-output process)
355 (goto-char (point-min)))
360 (nntp-snarf-error-message)
362 ((not (memq (process-status process) '(open run)))
363 (nntp-report "Server closed connection"))
365 (goto-char (point-max))
366 (let ((limit (point-min))
368 (while (not (re-search-backward wait-for limit t))
369 (nntp-accept-process-output process)
370 ;; We assume that whatever we wait for is less than 1000
372 (setq limit (max (- (point-max) 1000) (point-min)))
373 (goto-char (point-max)))
374 (setq response (match-string 0))
375 (with-current-buffer nntp-server-buffer
376 (setq nntp-process-response response)))
377 (nntp-decode-text (not decode))
381 (goto-char (point-max))
382 (insert-buffer-substring (process-buffer process))
383 ;; Nix out "nntp reading...." message.
384 (when nntp-have-messaged
385 (setq nntp-have-messaged nil)
386 (nnheader-message 5 ""))))
391 (defun nntp-kill-buffer (buffer)
392 (when (buffer-name buffer)
394 (nnheader-init-server-buffer)))
396 (defsubst nntp-find-connection (buffer)
397 "Find the connection delivering to BUFFER."
398 (let ((alist nntp-connection-alist)
399 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
401 (while (and alist (setq entry (pop alist)))
402 (when (eq buffer (cadr entry))
403 (setq process (car entry)
406 (if (memq (process-status process) '(open run))
408 (nntp-kill-buffer (process-buffer process))
409 (setq nntp-connection-alist (delq entry nntp-connection-alist))
412 (defsubst nntp-find-connection-entry (buffer)
413 "Return the entry for the connection to BUFFER."
414 (assq (nntp-find-connection buffer) nntp-connection-alist))
416 (defun nntp-find-connection-buffer (buffer)
417 "Return the process connection buffer tied to BUFFER."
418 (let ((process (nntp-find-connection buffer)))
420 (process-buffer process))))
422 (defsubst nntp-retrieve-data (command address port buffer
423 &optional wait-for callback decode)
424 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
425 (let ((process (or (nntp-find-connection buffer)
426 (nntp-open-connection buffer))))
429 (unless (or nntp-inhibit-erase nnheader-callback-function)
431 (set-buffer (process-buffer process))
436 (nntp-send-string process command))
438 ((eq callback 'ignore)
440 ((and callback wait-for)
441 (nntp-async-wait process wait-for buffer decode callback)
444 (nntp-wait-for process wait-for buffer decode))
447 (nnheader-report 'nntp "Couldn't open connection to %s: %s"
450 (message "Quit retrieving data from nntp")
453 (nnheader-report 'nntp "Couldn't open connection to %s" address))))
455 (defsubst nntp-send-command (wait-for &rest strings)
456 "Send STRINGS to server and wait until WAIT-FOR returns."
457 (when (and (not nnheader-callback-function)
458 (not nntp-inhibit-output))
460 (set-buffer nntp-server-buffer)
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.
473 ;; We don't have echos if nntp-open-connection-function
474 ;; is `nntp-open-network-stream', so we skip this in that case.
476 (equal nntp-open-connection-function
477 'nntp-open-network-stream))
478 (nntp-accept-response)
482 (if (looking-at (regexp-quote command))
483 (delete-region pos (progn (forward-line 1)
484 (gnus-point-at-bol))))
486 (nnheader-report 'nntp "Couldn't open connection to %s."
489 (defun nntp-send-command-nodelete (wait-for &rest strings)
490 "Send STRINGS to server and wait until WAIT-FOR returns."
491 (let* ((command (mapconcat 'identity strings " "))
492 (process (nntp-find-connection nntp-server-buffer))
493 (buffer (and process (process-buffer process)))
494 (pos (and buffer (with-current-buffer buffer (point)))))
497 (nntp-retrieve-data command
498 nntp-address nntp-port-number
500 wait-for nnheader-callback-function)
501 ;; If nothing to wait for, still remove possibly echo'ed commands
503 (nntp-accept-response)
507 (if (looking-at (regexp-quote command))
508 (delete-region pos (progn (forward-line 1)
509 (gnus-point-at-bol))))
511 (nnheader-report 'nntp "Couldn't open connection to %s."
514 (defun nntp-send-command-and-decode (wait-for &rest strings)
515 "Send STRINGS to server and wait until WAIT-FOR returns."
516 (when (and (not nnheader-callback-function)
517 (not nntp-inhibit-output))
519 (set-buffer nntp-server-buffer)
521 (let* ((command (mapconcat 'identity strings " "))
522 (process (nntp-find-connection nntp-server-buffer))
523 (buffer (and process (process-buffer process)))
524 (pos (and buffer (with-current-buffer buffer (point)))))
527 (nntp-retrieve-data command
528 nntp-address nntp-port-number
530 wait-for nnheader-callback-function t)
531 ;; If nothing to wait for, still remove possibly echo'ed commands
533 (nntp-accept-response)
537 (if (looking-at (regexp-quote command))
538 (delete-region pos (progn (forward-line 1) (gnus-point-at-bol))))
540 (nnheader-report 'nntp "Couldn't open connection to %s."
543 (defun nntp-send-buffer (wait-for)
544 "Send the current buffer to server and wait until WAIT-FOR returns."
545 (when (and (not nnheader-callback-function)
546 (not nntp-inhibit-output))
548 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
551 (let ((multibyte (and (boundp 'enable-multibyte-characters)
552 (symbol-value 'enable-multibyte-characters))))
554 ;; Some encoded unicode text contains character 0x80-0x9f e.g. Euro.
555 (let (default-enable-multibyte-characters mc-flag)
556 ;; `set-buffer-multibyte' will be provided by APEL for all Emacsen.
557 (set-buffer-multibyte nil)
558 (process-send-region (nntp-find-connection nntp-server-buffer)
559 (point-min) (point-max))))
560 (set-buffer-multibyte multibyte))
562 nil nntp-address nntp-port-number nntp-server-buffer
563 wait-for nnheader-callback-function))
567 ;;; Interface functions.
569 (nnoo-define-basics nntp)
571 (defsubst nntp-next-result-arrived-p ()
573 ;; A result that starts with a 2xx code is terminated by
574 ;; a line with only a "." on it.
575 ((eq (char-after) ?2)
576 (if (re-search-forward "\n\\.\r?\n" nil t)
579 ;; A result that starts with a 3xx or 4xx code is terminated
582 (if (search-forward "\n" nil t)
590 (defvar nntp-with-open-group-internal nil)
591 (defvar nntp-report-n nil))
593 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
594 "Protect against servers that don't like clients that keep idle connections opens.
595 The problem being that these servers may either close a connection or
596 simply ignore any further requests on a connection. Closed
597 connections are not detected until accept-process-output has updated
598 the process-status. Dropped connections are not detected until the
599 connection timeouts (which may be several minutes) or
600 nntp-connection-timeout has expired. When these occur
601 nntp-with-open-group, opens a new connection then re-issues the NNTP
602 command whose response triggered the error."
603 (when (and (listp connectionless)
604 (not (eq connectionless nil)))
605 (setq forms (cons connectionless forms)
607 `(letf ((nntp-report-n (symbol-function 'nntp-report))
608 ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1))
609 (nntp-with-open-group-internal nil))
610 (while (catch 'nntp-with-open-group-error
611 ;; Open the connection to the server
612 ;; NOTE: Existing connections are NOT tested.
613 (nntp-possibly-change-group ,group ,server ,connectionless)
616 (and nntp-connection-timeout
617 (nnheader-run-at-time
618 nntp-connection-timeout nil
620 (let ((process (nntp-find-connection
623 (process-buffer process))))
624 ;; When I an able to identify the
625 ;; connection to the server AND I've
626 ;; received NO reponse for
627 ;; nntp-connection-timeout seconds.
628 (when (and buffer (eq 0 (buffer-size buffer)))
629 ;; Close the connection. Take no
630 ;; other action as the accept input
631 ;; code will handle the closed
633 (nntp-kill-buffer buffer))))))))
635 (setq nntp-with-open-group-internal
640 (signal 'quit nil))))
642 (nnheader-cancel-timer timer)))
644 (setf (symbol-function 'nntp-report) nntp-report-n))
645 nntp-with-open-group-internal))
647 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
648 "Retrieve the headers of ARTICLES."
649 (nntp-with-open-group
652 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
654 (if (and (not gnus-nov-is-evil)
655 (not nntp-nov-is-evil)
656 (nntp-retrieve-headers-with-xover articles fetch-old))
657 ;; We successfully retrieved the headers via XOVER.
659 ;; XOVER didn't work, so we do it the hard, slow and inefficient
661 (let ((number (length articles))
665 (last-point (point-min))
666 (buf (nntp-find-connection-buffer nntp-server-buffer))
667 (nntp-inhibit-erase t)
669 ;; Send HEAD commands.
670 (while (setq article (pop articles))
673 "HEAD" (if (numberp article)
674 (int-to-string article)
675 ;; `articles' is either a list of article numbers
676 ;; or a list of article IDs.
679 ;; Every 400 requests we have to read the stream in
680 ;; order to avoid deadlocks.
681 (when (or (null articles) ;All requests have been sent.
682 (zerop (% count nntp-maximum-request)))
683 (nntp-accept-response)
686 (goto-char last-point)
688 (while (nntp-next-result-arrived-p)
689 (setq last-point (point))
692 ;; If number of headers is greater than 100, give
693 ;; informative messages.
694 (and (numberp nntp-large-newsgroup)
695 (> number nntp-large-newsgroup)
696 (zerop (% received 20))
697 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
698 (/ (* received 100) number)))
699 (nntp-accept-response))))
700 (and (numberp nntp-large-newsgroup)
701 (> number nntp-large-newsgroup)
702 (nnheader-message 6 "NNTP: Receiving headers...done"))
704 ;; Now all of replies are received. Fold continuation lines.
705 (nnheader-fold-continuation-lines)
706 ;; Remove all "\r"'s.
708 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
711 (deffoo nntp-retrieve-groups (groups &optional server)
712 "Retrieve group info on GROUPS."
713 (nntp-with-open-group
715 (when (nntp-find-connection-buffer nntp-server-buffer)
718 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
719 (set-buffer nntp-server-buffer)
721 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
722 ;; The first time this is run, this variable is `try'. So we
724 (when (eq nntp-server-list-active-group 'try)
725 (nntp-try-list-active (car groups)))
730 (last-point (point-min))
731 (nntp-inhibit-erase t)
732 (buf (nntp-find-connection-buffer nntp-server-buffer))
733 (command (if nntp-server-list-active-group
734 "LIST ACTIVE" "GROUP")))
736 ;; Timeout may have killed the buffer.
737 (unless (gnus-buffer-live-p buf)
738 (nnheader-report 'nntp "Connection to %s is closed." server)
740 ;; Send the command to the server.
741 (nntp-send-command nil command (pop groups))
743 ;; Every 400 requests we have to read the stream in
744 ;; order to avoid deadlocks.
745 (when (or (null groups) ;All requests have been sent.
746 (zerop (% count nntp-maximum-request)))
747 (nntp-accept-response)
748 (while (and (gnus-buffer-live-p buf)
750 ;; Search `blue moon' in this file for the
751 ;; reason why set-buffer here.
753 (goto-char last-point)
755 (while (re-search-forward "^[0-9]" nil t)
757 (setq last-point (point))
759 (nntp-accept-response))))
761 ;; Wait for the reply from the final command.
762 (unless (gnus-buffer-live-p buf)
763 (nnheader-report 'nntp "Connection to %s is closed." server)
766 (goto-char (point-max))
767 (re-search-backward "^[0-9]" nil t)
768 (when (looking-at "^[23]")
769 (while (and (gnus-buffer-live-p buf)
772 (goto-char (point-max))
773 (if (not nntp-server-list-active-group)
774 (not (re-search-backward "\r?\n"
776 (not (re-search-backward "^\\.\r?\n"
778 (nntp-accept-response)))
780 ;; Now all replies are received. We remove CRs.
781 (unless (gnus-buffer-live-p buf)
782 (nnheader-report 'nntp "Connection to %s is closed." server)
785 (goto-char (point-min))
786 (while (search-forward "\r" nil t)
787 (replace-match "" t t))
789 (if (not nntp-server-list-active-group)
791 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
793 ;; We have read active entries, so we just delete the
795 (goto-char (point-min))
796 (while (re-search-forward "^[.2-5]" nil t)
797 (delete-region (match-beginning 0)
798 (progn (forward-line 1) (point))))
799 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
802 (deffoo nntp-retrieve-articles (articles &optional group server)
803 (nntp-with-open-group
806 (let ((number (length articles))
810 (last-point (point-min))
811 (buf (nntp-find-connection-buffer nntp-server-buffer))
812 (nntp-inhibit-erase t)
813 (map (apply 'vector articles))
818 ;; Send ARTICLE command.
819 (while (setq article (pop articles))
822 "ARTICLE" (if (numberp article)
823 (int-to-string article)
824 ;; `articles' is either a list of article numbers
825 ;; or a list of article IDs.
828 ;; Every 400 requests we have to read the stream in
829 ;; order to avoid deadlocks.
830 (when (or (null articles) ;All requests have been sent.
831 (zerop (% count nntp-maximum-request)))
832 (nntp-accept-response)
835 (goto-char last-point)
837 (while (nntp-next-result-arrived-p)
838 (aset map received (cons (aref map received) (point)))
839 (setq last-point (point))
842 ;; If number of headers is greater than 100, give
843 ;; informative messages.
844 (and (numberp nntp-large-newsgroup)
845 (> number nntp-large-newsgroup)
846 (zerop (% received 20))
847 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
848 (/ (* received 100) number)))
849 (nntp-accept-response))))
850 (and (numberp nntp-large-newsgroup)
851 (> number nntp-large-newsgroup)
852 (nnheader-message 6 "NNTP: Receiving articles...done"))
854 ;; Now we have all the responses. We go through the results,
855 ;; wash it and copy it over to the server buffer.
856 (set-buffer nntp-server-buffer)
858 (setq last-point (point-min))
862 (setq point (goto-char (point-max)))
864 (insert-buffer-substring buf last-point (cdr entry))
866 (setq last-point (cdr entry))
869 (cons (car entry) point))
872 (defun nntp-try-list-active (group)
873 (nntp-list-active-group group)
875 (set-buffer nntp-server-buffer)
876 (goto-char (point-min))
878 (looking-at "5[0-9]+"))
879 (setq nntp-server-list-active-group nil))
881 (setq nntp-server-list-active-group t)))))
883 (deffoo nntp-list-active-group (group &optional server)
884 "Return the active info on GROUP (which can be a regexp)."
885 (nntp-with-open-group
887 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
889 (deffoo nntp-request-group-articles (group &optional server)
890 "Return the list of existing articles in GROUP."
891 (nntp-with-open-group
893 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
895 (deffoo nntp-request-article (article &optional group server buffer command)
896 (nntp-with-open-group
898 (when (nntp-send-command-and-decode
899 "\r?\n\\.\r?\n" "ARTICLE"
900 (if (numberp article) (int-to-string article) article))
902 (not (equal buffer nntp-server-buffer)))
904 (set-buffer nntp-server-buffer)
905 (copy-to-buffer buffer (point-min) (point-max))
906 (nntp-find-group-and-number group))
907 (nntp-find-group-and-number group)))))
909 (deffoo nntp-request-head (article &optional group server)
910 (nntp-with-open-group
912 (when (nntp-send-command
913 "\r?\n\\.\r?\n" "HEAD"
914 (if (numberp article) (int-to-string article) article))
916 (nntp-find-group-and-number group)
917 (nntp-decode-text)))))
919 (deffoo nntp-request-body (article &optional group server)
920 (nntp-with-open-group
922 (nntp-send-command-and-decode
923 "\r?\n\\.\r?\n" "BODY"
924 (if (numberp article) (int-to-string article) article))))
926 (deffoo nntp-request-group (group &optional server dont-check)
927 (nntp-with-open-group
929 (when (nntp-send-command "^[245].*\n" "GROUP" group)
930 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
931 (setcar (cddr entry) group)))))
933 (deffoo nntp-close-group (group &optional server)
936 (deffoo nntp-server-opened (&optional server)
937 "Say whether a connection to SERVER has been opened."
938 (and (nnoo-current-server-p 'nntp server)
940 (gnus-buffer-live-p nntp-server-buffer)
941 (nntp-find-connection nntp-server-buffer)))
943 (deffoo nntp-open-server (server &optional defs connectionless)
944 (nnheader-init-server-buffer)
945 (if (nntp-server-opened server)
947 (when (or (stringp (car defs))
948 (numberp (car defs)))
949 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
950 (unless (assq 'nntp-address defs)
951 (setq defs (append defs (list (list 'nntp-address server)))))
952 (nnoo-change-server 'nntp server defs)
953 (unless connectionless
954 (or (nntp-find-connection nntp-server-buffer)
955 (nntp-open-connection nntp-server-buffer)))))
957 (deffoo nntp-close-server (&optional server)
958 (nntp-possibly-change-group nil server t)
959 (let ((process (nntp-find-connection nntp-server-buffer)))
961 (when (memq (process-status process) '(open run))
963 (nntp-send-string process "QUIT")
964 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
965 ;; Ok, this is evil, but when using telnet and stuff
966 ;; as the connection method, it's important that the
967 ;; QUIT command actually is sent out before we kill
970 (nntp-kill-buffer (process-buffer process))
971 (setq process (car (pop nntp-connection-alist))))
972 (nnoo-close-server 'nntp)))
974 (deffoo nntp-request-close ()
976 (while (setq process (pop nntp-connection-list))
977 (when (memq (process-status process) '(open run))
979 (nntp-send-string process "QUIT")
980 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
981 ;; Ok, this is evil, but when using telnet and stuff
982 ;; as the connection method, it's important that the
983 ;; QUIT command actually is sent out before we kill
986 (nntp-kill-buffer (process-buffer process)))))
988 (deffoo nntp-request-list (&optional server)
989 "List active groups. If `nntp-list-options' is non-nil, the listing
990 output from the server will be restricted to the specified newsgroups.
991 If `nntp-options-subscribe' is non-nil, remove newsgroups that do not
992 match the regexp. If `nntp-options-not-subscribe' is non-nil, remove
993 newsgroups that match the regexp."
994 (nntp-with-open-group
996 (with-current-buffer nntp-server-buffer
998 (if (not nntp-list-options)
999 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")
1000 (let ((options (if (consp nntp-list-options)
1002 (list nntp-list-options)))
1006 (goto-char (point-max))
1007 (narrow-to-region (point) (point))
1009 (nntp-send-command-nodelete
1011 (format "LIST ACTIVE %s" (car options))))
1012 options (cdr options))
1016 (when (and (stringp nntp-options-subscribe)
1017 (not (string-equal "" nntp-options-subscribe)))
1018 (goto-char (point-min))
1019 (keep-lines nntp-options-subscribe))
1020 (when (and (stringp nntp-options-not-subscribe)
1021 (not (string-equal "" nntp-options-not-subscribe)))
1022 (goto-char (point-min))
1023 (flush-lines nntp-options-subscribe))))))
1025 (deffoo nntp-request-list-newsgroups (&optional server)
1026 (nntp-with-open-group
1028 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
1030 (deffoo nntp-request-newgroups (date &optional server)
1031 (nntp-with-open-group
1034 (set-buffer nntp-server-buffer)
1035 (let* ((time (date-to-time date))
1036 (ls (- (cadr time) (nth 8 (decode-time time)))))
1038 (setcar time (1- (car time)))
1039 (setcar (cdr time) (+ ls 65536)))
1041 (setcar time (1+ (car time)))
1042 (setcar (cdr time) (- ls 65536)))
1044 (setcar (cdr time) ls)))
1047 "^\\.\r?\n" "NEWGROUPS"
1048 (format-time-string "%y%m%d %H%M%S" time)
1050 (nntp-decode-text))))))
1052 (deffoo nntp-request-post (&optional server)
1053 (nntp-with-open-group
1055 (when (nntp-send-command "^[23].*\r?\n" "POST")
1056 (let ((response (with-current-buffer nntp-server-buffer
1057 nntp-process-response))
1060 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
1062 (setq server-id (match-string 1 response))
1063 (narrow-to-region (goto-char (point-min))
1064 (if (search-forward "\n\n" nil t)
1067 (unless (mail-fetch-field "Message-ID")
1068 (goto-char (point-min))
1069 (insert "Message-ID: " server-id "\n"))
1071 (run-hooks 'nntp-prepare-post-hook)
1072 (nntp-send-buffer "^[23].*\n")))))
1074 (deffoo nntp-request-type (group article)
1077 (deffoo nntp-asynchronous-p ()
1080 ;;; Hooky functions.
1082 (defun nntp-send-mode-reader ()
1083 "Send the MODE READER command to the nntp server.
1084 This function is supposed to be called from `nntp-server-opened-hook'.
1085 It will make innd servers spawn an nnrpd process to allow actual article
1087 (nntp-send-command "^.*\n" "MODE READER"))
1089 (defun nntp-send-authinfo (&optional send-if-force)
1090 "Send the AUTHINFO to the nntp server.
1091 It will look in the \"~/.authinfo\" file for matching entries. If
1092 nothing suitable is found there, it will prompt for a user name
1095 If SEND-IF-FORCE, only send authinfo to the server if the
1096 .authinfo file has the FORCE token."
1097 (let* ((list (gnus-parse-netrc nntp-authinfo-file))
1098 (alist (gnus-netrc-machine list nntp-address "nntp"))
1099 (force (gnus-netrc-get alist "force"))
1100 (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
1101 (passwd (gnus-netrc-get alist "password")))
1102 (when (or (not send-if-force)
1105 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1106 nntp-authinfo-user user))
1107 (unless (member user '(nil ""))
1108 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1109 (when t ;???Should check if AUTHINFO succeeded
1111 "^2.*\r?\n" "AUTHINFO PASS"
1113 nntp-authinfo-password
1114 (setq nntp-authinfo-password
1115 (read-passwd (format "NNTP (%s@%s) password: "
1116 user nntp-address))))))))))
1118 (defun nntp-send-nosy-authinfo ()
1119 "Send the AUTHINFO to the nntp server."
1120 (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
1121 (unless (member user '(nil ""))
1122 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1123 (when t ;???Should check if AUTHINFO succeeded
1124 (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
1125 (read-passwd (format "NNTP (%s@%s) password: "
1126 user nntp-address)))))))
1128 (defun nntp-send-authinfo-from-file ()
1129 "Send the AUTHINFO to the nntp server.
1131 The authinfo login name is taken from the user's login name and the
1132 password contained in '~/.nntp-authinfo'."
1133 (when (file-exists-p "~/.nntp-authinfo")
1135 (insert-file-contents "~/.nntp-authinfo")
1136 (goto-char (point-min))
1137 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
1139 "^2.*\r?\n" "AUTHINFO PASS"
1140 (buffer-substring (point) (gnus-point-at-eol))))))
1142 ;;; Internal functions.
1144 (defun nntp-handle-authinfo (process)
1145 "Take care of an authinfo response from the server."
1146 (let ((last nntp-last-command))
1147 (funcall nntp-authinfo-function)
1148 ;; We have to re-send the function that was interrupted by
1149 ;; the authinfo request.
1151 (set-buffer nntp-server-buffer)
1153 (nntp-send-string process last)))
1155 (defun nntp-make-process-buffer (buffer)
1156 "Create a new, fresh buffer usable for nntp process connections."
1159 (generate-new-buffer
1160 (format " *server %s %s %s*"
1161 nntp-address nntp-port-number
1162 (gnus-buffer-exists-p buffer))))
1163 (set (make-local-variable 'after-change-functions) nil)
1164 (set (make-local-variable 'nntp-process-wait-for) nil)
1165 (set (make-local-variable 'nntp-process-callback) nil)
1166 (set (make-local-variable 'nntp-process-to-buffer) nil)
1167 (set (make-local-variable 'nntp-process-start-point) nil)
1168 (set (make-local-variable 'nntp-process-decode) nil)
1171 (defun nntp-open-connection (buffer)
1172 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1173 (run-hooks 'nntp-prepare-server-hook)
1174 (let* ((pbuffer (nntp-make-process-buffer buffer))
1176 (and nntp-connection-timeout
1177 (nnheader-run-at-time
1178 nntp-connection-timeout nil
1180 (nntp-kill-buffer ,pbuffer)))))
1183 (funcall nntp-open-connection-function pbuffer)
1186 (message "Quit opening connection")
1187 (nntp-kill-buffer pbuffer)
1191 (nnheader-cancel-timer timer))
1193 (nntp-kill-buffer pbuffer))
1194 (when (and (buffer-name pbuffer)
1196 (process-kill-without-query process)
1197 (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1198 (memq (process-status process) '(open run)))
1200 (caar (push (list process buffer nil) nntp-connection-alist))
1201 (push process nntp-connection-list)
1203 (set-buffer pbuffer)
1204 (nntp-read-server-type)
1206 (set-buffer nntp-server-buffer)
1207 (let ((nnheader-callback-function nil))
1208 (run-hooks 'nntp-server-opened-hook)
1209 (nntp-send-authinfo t))))
1210 (nntp-kill-buffer (process-buffer process))
1213 (defun nntp-open-network-stream (buffer)
1214 (open-network-stream-as-binary
1215 "nntpd" buffer nntp-address nntp-port-number))
1217 (defun nntp-open-ssl-stream (buffer)
1218 (let* ((process-connection-type nil)
1219 (proc (as-binary-process
1220 (start-process "nntpd" buffer
1222 shell-command-switch
1223 (format-spec nntp-ssl-program
1226 ?p nntp-port-number))))))
1227 (process-kill-without-query proc)
1230 (nntp-wait-for-string "^\r*20[01]")
1232 (delete-region (point-min) (point))
1235 (defun nntp-read-server-type ()
1236 "Find out what the name of the server we have connected to is."
1237 ;; Wait for the status string to arrive.
1238 (setq nntp-server-type (buffer-string))
1239 (let ((alist nntp-server-action-alist)
1240 (case-fold-search t)
1242 ;; Run server-specific commands.
1244 (setq entry (pop alist))
1245 (when (string-match (car entry) nntp-server-type)
1246 (if (and (listp (cadr entry))
1247 (not (eq 'lambda (caadr entry))))
1249 (funcall (cadr entry)))))))
1251 (defun nntp-async-wait (process wait-for buffer decode callback)
1253 (set-buffer (process-buffer process))
1254 (unless nntp-inside-change-function
1256 (setq nntp-process-wait-for wait-for
1257 nntp-process-to-buffer buffer
1258 nntp-process-decode decode
1259 nntp-process-callback callback
1260 nntp-process-start-point (point-max))
1261 (setq after-change-functions '(nntp-after-change-function))
1262 (if nntp-async-needs-kluge
1263 (nntp-async-kluge process))))
1265 (defun nntp-async-kluge (process)
1266 ;; emacs 20.3 bug: process output with encoding 'binary
1267 ;; doesn't trigger after-change-functions.
1268 (unless nntp-async-timer
1269 (setq nntp-async-timer
1270 (nnheader-run-at-time 1 1 'nntp-async-timer-handler)))
1271 (add-to-list 'nntp-async-process-list process))
1273 (defun nntp-async-timer-handler ()
1276 (if (memq (process-status proc) '(open run))
1277 (nntp-async-trigger proc)
1278 (nntp-async-stop proc)))
1279 nntp-async-process-list))
1281 (defun nntp-async-stop (proc)
1282 (setq nntp-async-process-list (delq proc nntp-async-process-list))
1283 (when (and nntp-async-timer (not nntp-async-process-list))
1284 (nnheader-cancel-timer nntp-async-timer)
1285 (setq nntp-async-timer nil)))
1287 (defun nntp-after-change-function (beg end len)
1289 ;; we only care about insertions at eob
1290 (when (and (eq 0 len) (eq (point-max) end))
1292 (let ((proc (get-buffer-process (current-buffer))))
1294 (nntp-async-trigger proc)))))
1295 ;; any throw from after-change-functions will leave it
1296 ;; set to nil. so we reset it here, if necessary.
1298 (setq after-change-functions '(nntp-after-change-function)))))
1300 (defun nntp-async-trigger (process)
1302 (set-buffer (process-buffer process))
1303 (when nntp-process-callback
1304 ;; do we have an error message?
1305 (goto-char nntp-process-start-point)
1306 (if (memq (following-char) '(?4 ?5))
1307 ;; wants credentials?
1308 (if (looking-at "480")
1309 (nntp-handle-authinfo process)
1310 ;; report error message.
1311 (nntp-snarf-error-message)
1312 (nntp-do-callback nil))
1314 ;; got what we expect?
1315 (goto-char (point-max))
1316 (when (re-search-backward
1317 nntp-process-wait-for nntp-process-start-point t)
1318 (let ((response (match-string 0)))
1319 (with-current-buffer nntp-server-buffer
1320 (setq nntp-process-response response)))
1321 (nntp-async-stop process)
1323 (when (gnus-buffer-exists-p nntp-process-to-buffer)
1324 (let ((buf (current-buffer))
1325 (start nntp-process-start-point)
1326 (decode nntp-process-decode))
1328 (set-buffer nntp-process-to-buffer)
1329 (goto-char (point-max))
1331 (narrow-to-region (point) (point))
1332 (insert-buffer-substring buf start)
1334 (nntp-decode-text))))))
1336 (goto-char (point-max))
1338 (buffer-name (get-buffer nntp-process-to-buffer))))))))
1340 (defun nntp-do-callback (arg)
1341 (let ((callback nntp-process-callback)
1342 (nntp-inside-change-function t))
1343 (setq nntp-process-callback nil)
1344 (funcall callback arg)))
1346 (defun nntp-snarf-error-message ()
1347 "Save the error message in the current buffer."
1348 (let ((message (buffer-string)))
1349 (while (string-match "[\r\n]+" message)
1350 (setq message (replace-match " " t t message)))
1351 (nnheader-report 'nntp message)
1354 (defun nntp-accept-process-output (process)
1355 "Wait for output from PROCESS and message some dots."
1357 (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1358 nntp-server-buffer))
1359 (let ((len (/ (buffer-size) 1024))
1362 (setq nntp-have-messaged t)
1363 (nnheader-message 7 "nntp read: %dk" len)))
1364 (accept-process-output
1366 (truncate nntp-read-timeout)
1367 (truncate (* (- nntp-read-timeout
1368 (truncate nntp-read-timeout))
1370 ;; accept-process-output may update status of process to indicate
1371 ;; that the server has closed the connection. This MUST be
1372 ;; handled here as the buffer restored by the save-excursion may
1373 ;; be the process's former output buffer (i.e. now killed)
1375 (memq (process-status process) '(open run)))
1376 (nntp-report "Server closed connection"))))
1378 (defun nntp-accept-response ()
1379 "Wait for output from the process that outputs to BUFFER."
1380 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1382 (defun nntp-possibly-change-group (group server &optional connectionless)
1383 (let ((nnheader-callback-function nil))
1385 (or (nntp-server-opened server)
1386 (nntp-open-server server nil connectionless)))
1388 (unless connectionless
1389 (or (nntp-find-connection nntp-server-buffer)
1390 (nntp-open-connection nntp-server-buffer))))
1393 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1395 (nntp-report "Server closed connection"))
1396 ((not (equal group (caddr entry)))
1398 (set-buffer (process-buffer (car entry)))
1400 (nntp-send-command "^[245].*\n" "GROUP" group)
1401 (setcar (cddr entry) group)
1404 (set-buffer nntp-server-buffer)
1405 (erase-buffer))))))))
1407 (defun nntp-decode-text (&optional cr-only)
1408 "Decode the text in the current buffer."
1409 (goto-char (point-min))
1410 (while (search-forward "\r" nil t)
1413 ;; Remove trailing ".\n" end-of-transfer marker.
1414 (goto-char (point-max))
1416 (when (looking-at ".\n")
1418 ;; Delete status line.
1419 (goto-char (point-min))
1420 (while (looking-at "[1-5][0-9][0-9] .*\n")
1421 ;; For some unknown reason, there is more than one status line.
1422 (delete-region (point) (progn (forward-line 1) (point))))
1423 ;; Remove "." -> ".." encoding.
1424 (while (search-forward "\n.." nil t)
1427 (defun nntp-encode-text ()
1428 "Encode the text in the current buffer."
1430 ;; Replace "." at beginning of line with "..".
1431 (goto-char (point-min))
1432 (while (re-search-forward "^\\." nil t)
1434 (goto-char (point-max))
1435 ;; Insert newline at the end of the buffer.
1438 ;; Insert `.' at end of buffer (end of text mark).
1439 (goto-char (point-max))
1441 (goto-char (point-min))
1445 (insert nntp-end-of-line))))
1447 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1448 (set-buffer nntp-server-buffer)
1452 ;; This server does not talk NOV.
1453 ((not nntp-server-xover)
1456 ;; We don't care about gaps.
1457 ((or (not nntp-nov-gap)
1459 (nntp-send-xover-command
1461 (if (numberp fetch-old)
1462 (max 1 (- (car articles) fetch-old))
1465 (car (last articles)) 'wait)
1467 (goto-char (point-min))
1468 (when (looking-at "[1-5][0-9][0-9] .*\n")
1469 (delete-region (point) (progn (forward-line 1) (point))))
1470 (while (search-forward "\r" nil t)
1471 (replace-match "" t t))
1472 (goto-char (point-max))
1474 (when (looking-at "\\.")
1475 (delete-region (point) (progn (forward-line 1) (point)))))
1477 ;; We do it the hard way. For each gap, an XOVER command is sent
1478 ;; to the server. We do not wait for a reply from the server, we
1479 ;; just send them off as fast as we can. That means that we have
1480 ;; to count the number of responses we get back to find out when we
1481 ;; have gotten all we asked for.
1482 ((numberp nntp-nov-gap)
1487 (buf nntp-server-buffer)
1488 (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1491 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1492 ;; that means that the server does not understand XOVER, but we
1493 ;; won't know that until we try.
1494 (while (and nntp-server-xover articles)
1495 (setq first (car articles))
1496 ;; Search forward until we find a gap, or until we run out of
1498 (while (and (cdr articles)
1499 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1500 (setq articles (cdr articles)))
1502 (setq in-process-buffer-p (stringp nntp-server-xover))
1503 (nntp-send-xover-command first (setq last (car articles)))
1504 (setq articles (cdr articles))
1506 (when (and nntp-server-xover in-process-buffer-p)
1507 ;; Don't count tried request.
1508 (setq count (1+ count))
1510 ;; Every 400 requests we have to read the stream in
1511 ;; order to avoid deadlocks.
1512 (when (or (null articles) ;All requests have been sent.
1513 (= 1 (% count nntp-maximum-request)))
1515 (nntp-accept-response)
1516 ;; On some Emacs versions the preceding function has a
1517 ;; tendency to change the buffer. Perhaps. It's quite
1518 ;; difficult to reproduce, because it only seems to happen
1519 ;; once in a blue moon.
1520 (set-buffer process-buffer)
1522 (goto-char (or last-point (point-min)))
1524 (while (re-search-forward "^[0-9][0-9][0-9] .*\n" nil t)
1526 (setq last-point (point))
1527 (or (< received count)
1528 ;; I haven't started reading the final response
1530 (goto-char (point-max))
1532 (not (looking-at "^\\.\r?\n")))))
1533 ;; I haven't read the end of the final response
1534 (nntp-accept-response)
1535 (set-buffer process-buffer))))
1537 ;; Some nntp servers seem to have an extension to the XOVER
1538 ;; extension. On these servers, requesting an article range
1539 ;; preceeding the active range does not return an error as
1540 ;; specified in the RFC. What we instead get is the NOV entry
1541 ;; for the first available article. Obviously, a client can
1542 ;; use that entry to avoid making unnecessary requests. The
1543 ;; only problem is for a client that assumes that the response
1544 ;; will always be within the requested ranage. For such a
1545 ;; client, we can get N copies of the same entry (one for each
1546 ;; XOVER command sent to the server).
1549 (goto-char (point-min))
1550 (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1551 (let ((low-limit (string-to-int
1552 (buffer-substring (match-beginning 1)
1554 (while (and articles (<= (car articles) low-limit))
1555 (setq articles (cdr articles))))))
1558 (when nntp-server-xover
1559 (when in-process-buffer-p
1561 (goto-char (point-max))
1562 (insert-buffer-substring process-buffer)
1563 (set-buffer process-buffer)
1567 ;; We remove any "." lines and status lines.
1568 (goto-char (point-min))
1569 (while (search-forward "\r" nil t)
1571 (goto-char (point-min))
1572 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1577 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1578 "Send the XOVER command to the server."
1579 (let ((range (format "%d-%d" beg end))
1580 (nntp-inhibit-erase t))
1581 (if (stringp nntp-server-xover)
1582 ;; If `nntp-server-xover' is a string, then we just send this
1585 (nntp-send-command-nodelete
1586 "\r?\n\\.\r?\n" nntp-server-xover range)
1587 ;; We do not wait for the reply.
1588 (nntp-send-command-nodelete nil nntp-server-xover range))
1589 (let ((commands nntp-xover-commands))
1590 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1591 ;; We try them all until we get at positive response.
1592 (while (and commands (eq nntp-server-xover 'try))
1593 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1595 (set-buffer nntp-server-buffer)
1596 (goto-char (point-min))
1597 (and (looking-at "[23]") ; No error message.
1598 ;; We also have to look at the lines. Some buggy
1599 ;; servers give back simple lines with just the
1600 ;; article number. How... helpful.
1603 (looking-at "[0-9]+\t...")) ; More text after number.
1604 (setq nntp-server-xover (car commands))))
1605 (setq commands (cdr commands)))
1606 ;; If none of the commands worked, we disable XOVER.
1607 (when (eq nntp-server-xover 'try)
1609 (set-buffer nntp-server-buffer)
1611 (setq nntp-server-xover nil)))
1612 nntp-server-xover))))
1614 (defun nntp-find-group-and-number (&optional group)
1617 (set-buffer nntp-server-buffer)
1618 (narrow-to-region (goto-char (point-min))
1619 (or (search-forward "\n\n" nil t) (point-max)))
1620 (goto-char (point-min))
1621 ;; We first find the number by looking at the status line.
1622 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1624 (buffer-substring (match-beginning 1)
1627 (and number (zerop number) (setq number nil))
1629 ;; Then we find the group name.
1632 ;; If there is only one group in the Newsgroups
1633 ;; header, then it seems quite likely that this
1634 ;; article comes from that group, I'd say.
1635 ((and (setq newsgroups
1636 (mail-fetch-field "newsgroups"))
1637 (not (string-match "," newsgroups)))
1639 ;; If there is more than one group in the
1640 ;; Newsgroups header, then the Xref header should
1641 ;; be filled out. We hazard a guess that the group
1642 ;; that has this article number in the Xref header
1643 ;; is the one we are looking for. This might very
1644 ;; well be wrong if this article happens to have
1645 ;; the same number in several groups, but that's
1647 ((and (setq xref (mail-fetch-field "xref"))
1650 (format "\\([^ :]+\\):%d" number) xref))
1651 (match-string 1 xref))
1654 ((and (setq xref (mail-fetch-field "xref"))
1657 (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1658 "\\([^ :]+\\):\\([0-9]+\\)")
1660 (setq group (match-string 1 xref)
1661 number (string-to-int (match-string 2 xref))))
1662 ((and (setq newsgroups
1663 (mail-fetch-field "newsgroups"))
1664 (not (string-match "," newsgroups)))
1665 (setq group newsgroups))
1667 (t (setq group ""))))
1668 (when (string-match "\r" group)
1669 (setq group (substring group 0 (match-beginning 0))))
1670 (cons group number)))))
1672 (defun nntp-wait-for-string (regexp)
1673 "Wait until string arrives in the buffer."
1674 (let ((buf (current-buffer))
1676 (goto-char (point-min))
1677 (while (and (setq proc (get-buffer-process buf))
1678 (memq (process-status proc) '(open run))
1679 (not (re-search-forward regexp nil t)))
1680 (accept-process-output proc)
1682 (goto-char (point-min)))))
1685 ;; ==========================================================================
1686 ;; Obsolete nntp-open-* connection methods -- drv
1687 ;; ==========================================================================
1689 (defvoo nntp-open-telnet-envuser nil
1690 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1692 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
1693 "*Regular expression to match the shell prompt on the remote machine.")
1695 (defvoo nntp-rlogin-program "rsh"
1696 "*Program used to log in on remote machines.
1697 The default is \"rsh\", but \"ssh\" is a popular alternative.")
1699 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1700 "*Parameters to `nntp-open-rlogin'.
1701 That function may be used as `nntp-open-connection-function'. In that
1702 case, this list will be used as the parameter list given to rsh.")
1704 (defvoo nntp-rlogin-user-name nil
1705 "*User name on remote system when using the rlogin connect method.")
1707 (defvoo nntp-telnet-parameters
1708 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1709 "*Parameters to `nntp-open-telnet'.
1710 That function may be used as `nntp-open-connection-function'. In that
1711 case, this list will be executed as a command after logging in
1714 (defvoo nntp-telnet-user-name nil
1715 "User name to log in via telnet with.")
1717 (defvoo nntp-telnet-passwd nil
1718 "Password to use to log in via telnet with.")
1720 (defun nntp-open-telnet (buffer)
1724 (let ((proc (as-binary-process
1727 "nntpd" buffer nntp-telnet-command nntp-telnet-switches)))
1728 (case-fold-search t))
1729 (when (memq (process-status proc) '(open run))
1730 (nntp-wait-for-string "^r?telnet")
1731 (process-send-string proc "set escape \^X\n")
1733 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1734 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1735 nntp-address "\n")))
1737 (process-send-string proc (concat "open " nntp-address "\n"))))
1739 ((not nntp-open-telnet-envuser)
1740 (nntp-wait-for-string "^\r*.?login:")
1741 (process-send-string
1743 (or nntp-telnet-user-name
1744 (setq nntp-telnet-user-name (read-string "login: ")))
1746 (nntp-wait-for-string "^\r*.?password:")
1747 (process-send-string
1749 (or nntp-telnet-passwd
1750 (setq nntp-telnet-passwd
1751 (read-passwd "Password: ")))
1753 (nntp-wait-for-string nntp-telnet-shell-prompt)
1754 (process-send-string
1755 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1756 (nntp-wait-for-string "^\r*20[01]")
1758 (delete-region (point-min) (point))
1759 (process-send-string proc "\^]")
1760 (nntp-wait-for-string "^r?telnet")
1761 (process-send-string proc "mode character\n")
1762 (accept-process-output proc 1)
1764 (goto-char (point-min))
1766 (delete-region (point) (point-max)))
1769 (defun nntp-open-rlogin (buffer)
1770 "Open a connection to SERVER using rsh."
1771 (let ((proc (if nntp-rlogin-user-name
1773 (apply 'start-process
1774 "nntpd" buffer nntp-rlogin-program
1775 nntp-address "-l" nntp-rlogin-user-name
1776 nntp-rlogin-parameters))
1778 (apply 'start-process
1779 "nntpd" buffer nntp-rlogin-program nntp-address
1780 nntp-rlogin-parameters)))))
1783 (nntp-wait-for-string "^\r*20[01]")
1785 (delete-region (point-min) (point))
1789 ;; ==========================================================================
1790 ;; Replacements for the nntp-open-* functions -- drv
1791 ;; ==========================================================================
1793 (defun nntp-open-telnet-stream (buffer)
1794 "Open a nntp connection by telnet'ing the news server.
1796 Please refer to the following variables to customize the connection:
1797 - `nntp-pre-command',
1798 - `nntp-telnet-command',
1799 - `nntp-telnet-switches',
1801 - `nntp-port-number',
1802 - `nntp-end-of-line'."
1803 (let ((command `(,nntp-telnet-command
1804 ,@nntp-telnet-switches
1805 ,nntp-address ,nntp-port-number))
1807 (and nntp-pre-command
1808 (push nntp-pre-command command))
1809 (setq proc (as-binary-process
1810 (apply 'start-process "nntpd" buffer command)))
1813 (nntp-wait-for-string "^\r*20[01]")
1815 (delete-region (point-min) (point))
1818 (defun nntp-open-via-rlogin-and-telnet (buffer)
1819 "Open a connection to an nntp server through an intermediate host.
1820 First rlogin to the remote host, and then telnet the real news server
1823 Please refer to the following variables to customize the connection:
1824 - `nntp-pre-command',
1825 - `nntp-via-rlogin-command',
1826 - `nntp-via-rlogin-command-switches',
1827 - `nntp-via-user-name',
1828 - `nntp-via-address',
1829 - `nntp-telnet-command',
1830 - `nntp-telnet-switches',
1832 - `nntp-port-number',
1833 - `nntp-end-of-line'."
1834 (let ((command `(,nntp-via-address
1835 ,nntp-telnet-command
1836 ,@nntp-telnet-switches))
1838 (when nntp-via-user-name
1839 (setq command `("-l" ,nntp-via-user-name ,@command)))
1840 (when nntp-via-rlogin-command-switches
1841 (setq command (append nntp-via-rlogin-command-switches command)))
1842 (push nntp-via-rlogin-command command)
1843 (and nntp-pre-command
1844 (push nntp-pre-command command))
1845 (setq proc (as-binary-process
1846 (apply 'start-process "nntpd" buffer command)))
1849 (nntp-wait-for-string "^r?telnet")
1850 (process-send-string proc (concat "open " nntp-address
1851 " " nntp-port-number "\n"))
1852 (nntp-wait-for-string "^\r*20[01]")
1854 (delete-region (point-min) (point))
1857 (defun nntp-open-via-telnet-and-telnet (buffer)
1858 "Open a connection to an nntp server through an intermediate host.
1859 First telnet the remote host, and then telnet the real news server
1862 Please refer to the following variables to customize the connection:
1863 - `nntp-pre-command',
1864 - `nntp-via-telnet-command',
1865 - `nntp-via-telnet-switches',
1866 - `nntp-via-address',
1867 - `nntp-via-envuser',
1868 - `nntp-via-user-name',
1869 - `nntp-via-user-password',
1870 - `nntp-via-shell-prompt',
1871 - `nntp-telnet-command',
1872 - `nntp-telnet-switches',
1874 - `nntp-port-number',
1875 - `nntp-end-of-line'."
1879 (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
1880 (case-fold-search t)
1882 (and nntp-pre-command (push nntp-pre-command command))
1883 (setq proc (as-binary-process
1884 (apply 'start-process "nntpd" buffer command)))
1885 (when (memq (process-status proc) '(open run))
1886 (nntp-wait-for-string "^r?telnet")
1887 (process-send-string proc "set escape \^X\n")
1889 ((and nntp-via-envuser nntp-via-user-name)
1890 (process-send-string proc (concat "open " "-l" nntp-via-user-name
1891 nntp-via-address "\n")))
1893 (process-send-string proc (concat "open " nntp-via-address
1895 (when (not nntp-via-envuser)
1896 (nntp-wait-for-string "^\r*.?login:")
1897 (process-send-string proc
1899 (or nntp-via-user-name
1900 (setq nntp-via-user-name
1901 (read-string "login: ")))
1903 (nntp-wait-for-string "^\r*.?password:")
1904 (process-send-string proc
1906 (or nntp-via-user-password
1907 (setq nntp-via-user-password
1908 (read-passwd "Password: ")))
1910 (nntp-wait-for-string nntp-via-shell-prompt)
1911 (let ((real-telnet-command `("exec"
1912 ,nntp-telnet-command
1913 ,@nntp-telnet-switches
1915 ,nntp-port-number)))
1916 (process-send-string proc
1917 (concat (mapconcat 'identity
1918 real-telnet-command " ")
1920 (nntp-wait-for-string "^\r*20[01]")
1922 (delete-region (point-min) (point))
1923 (process-send-string proc "\^]")
1924 (nntp-wait-for-string "^r?telnet")
1925 (process-send-string proc "mode character\n")
1926 (accept-process-output proc 1)
1928 (goto-char (point-min))
1930 (delete-region (point) (point-max)))
1935 ;;; nntp.el ends here