1 ;;; nntp.el --- nntp access for Gnus
3 ;; Copyright (C) 1987-1990, 1992-1998, 2000-2016 Free Software
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; In Emacs 24, `open-protocol-stream' is an autoloaded alias for
30 ;; `make-network-stream'.
31 (unless (fboundp 'open-protocol-stream)
32 (require 'proto-stream)))
38 (require 'gnus-group) ;; gnus-group-name-charset
42 (eval-when-compile (require 'cl))
44 (autoload 'auth-source-search "auth-source")
47 "NNTP access for Gnus."
50 (defvoo nntp-address nil
51 "Address of the physical nntp server.")
53 (defvoo nntp-port-number "nntp"
54 "Port number on the physical nntp server.")
56 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
57 "*Hook used for sending commands to the server at startup.
58 The default value is `nntp-send-mode-reader', which makes an innd
59 server spawn an nnrpd server.")
61 (defvoo nntp-authinfo-function 'nntp-send-authinfo
62 "Function used to send AUTHINFO to the server.
63 It is called with no parameters.")
65 (defvoo nntp-server-action-alist
66 '(("nntpd 1\\.5\\.11t"
67 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
68 ("NNRP server Netscape"
69 (setq nntp-server-list-active-group nil)))
70 "Alist of regexps to match on server types and actions to be taken.
71 For instance, if you want Gnus to beep every time you connect
72 to innd, you could say something like:
74 \(setq nntp-server-action-alist
75 \\='((\"innd\" (ding))))
77 You probably don't want to do that, though.")
79 (defvoo nntp-open-connection-function 'nntp-open-network-stream
80 "Method for connecting to a remote system.
81 It should be a function, which is called with the output buffer
82 as its single argument, or one of the following special values:
84 - `nntp-open-network-stream' specifies a network connection,
85 upgrading to a TLS connection via STARTTLS if possible.
86 - `nntp-open-plain-stream' specifies an unencrypted network
87 connection (no STARTTLS upgrade is attempted).
88 - `nntp-open-ssl-stream' or `nntp-open-tls-stream' specify a TLS
91 Apart from the above special values, valid functions are as
92 follows; please refer to their respective doc string for more
94 For direct connections:
95 - `nntp-open-netcat-stream'
96 - `nntp-open-telnet-stream'
97 For indirect connections:
98 - `nntp-open-via-rlogin-and-netcat'
99 - `nntp-open-via-rlogin-and-telnet'
100 - `nntp-open-via-telnet-and-telnet'")
102 (defvoo nntp-never-echoes-commands nil
103 "*Non-nil means the nntp server never echoes commands.
104 It is reported that some nntps server doesn't echo commands. So, you
105 may want to set this to non-nil in the method for such a server setting
106 `nntp-open-connection-function' to `nntp-open-ssl-stream' for example.
107 Note that the `nntp-open-connection-functions-never-echo-commands'
108 variable overrides the nil value of this variable.")
110 (defvoo nntp-open-connection-functions-never-echo-commands
111 '(nntp-open-network-stream)
112 "*List of functions that never echo commands.
113 Add or set a function which you set to `nntp-open-connection-function'
114 to this list if it does not echo commands. Note that a non-nil value
115 of the `nntp-never-echoes-commands' variable overrides this variable.")
117 (defvoo nntp-pre-command nil
118 "*Pre-command to use with the various nntp-open-via-* methods.
119 This is where you would put \"runsocks\" or stuff like that.")
121 (defvoo nntp-telnet-command "telnet"
122 "*Telnet command used to connect to the nntp server.
123 This command is used by the methods `nntp-open-telnet-stream',
124 `nntp-open-via-rlogin-and-telnet' and `nntp-open-via-telnet-and-telnet'.")
126 (defvoo nntp-telnet-switches '("-8")
127 "*Switches given to the telnet command `nntp-telnet-command'.")
129 (defvoo nntp-end-of-line "\r\n"
130 "*String to use on the end of lines when talking to the NNTP server.
131 This is \"\\r\\n\" by default, but should be \"\\n\" when using an indirect
132 connection method (nntp-open-via-*).")
134 (defvoo nntp-via-rlogin-command "rsh"
135 "*Rlogin command used to connect to an intermediate host.
136 This command is used by the methods `nntp-open-via-rlogin-and-telnet'
137 and `nntp-open-via-rlogin-and-netcat'. The default is \"rsh\", but \"ssh\"
138 is a popular alternative.")
140 (defvoo nntp-via-rlogin-command-switches nil
141 "*Switches given to the rlogin command `nntp-via-rlogin-command'.
142 If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
143 \(\"-C\") in order to compress all data connections, otherwise set this
144 to \(\"-t\" \"-e\" \"none\") or (\"-C\" \"-t\" \"-e\" \"none\") if the telnet
145 command requires a pseudo-tty allocation on an intermediate host.")
147 (defvoo nntp-via-telnet-command "telnet"
148 "*Telnet command used to connect to an intermediate host.
149 This command is used by the `nntp-open-via-telnet-and-telnet' method.")
151 (defvoo nntp-via-telnet-switches '("-8")
152 "*Switches given to the telnet command `nntp-via-telnet-command'.")
154 (defvoo nntp-netcat-command "nc"
155 "*Netcat command used to connect to the nntp server.
156 This command is used by the `nntp-open-netcat-stream' and
157 `nntp-open-via-rlogin-and-netcat' methods.")
159 (defvoo nntp-netcat-switches nil
160 "*Switches given to the netcat command `nntp-netcat-command'.")
162 (defvoo nntp-via-user-name nil
163 "*User name to log in on an intermediate host with.
164 This variable is used by the various nntp-open-via-* methods.")
166 (defvoo nntp-via-user-password nil
167 "*Password to use to log in on an intermediate host with.
168 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
170 (defvoo nntp-via-address nil
171 "*Address of an intermediate host to connect to.
172 This variable is used by the various nntp-open-via-* methods.")
174 (defvoo nntp-via-envuser nil
175 "*Whether both telnet client and server support the ENVIRON option.
176 If non-nil, there will be no prompt for a login name.")
178 (defvoo nntp-via-shell-prompt "bash\\|[$>] *\r?$"
179 "*Regular expression to match the shell prompt on an intermediate host.
180 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
182 (defvoo nntp-large-newsgroup 50
183 "*The number of articles which indicates a large newsgroup.
184 If the number of articles is greater than the value, verbose
185 messages will be shown to indicate the current status.")
187 (defvoo nntp-maximum-request 400
188 "*The maximum number of the requests sent to the NNTP server at one time.
189 If Emacs hangs up while retrieving headers, set the variable to a
192 (defvoo nntp-nov-is-evil nil
193 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
195 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
196 "*List of strings that are used as commands to fetch NOV lines from a server.
197 The strings are tried in turn until a positive response is gotten. If
198 none of the commands are successful, nntp will just grab headers one
201 (defvoo nntp-nov-gap 5
202 "*Maximum allowed gap between two articles.
203 If the gap between two consecutive articles is bigger than this
204 variable, split the XOVER request into two requests.")
206 (defvoo nntp-xref-number-is-evil nil
207 "*If non-nil, Gnus never trusts article numbers in the Xref header.
208 Some news servers, e.g., ones running Diablo, run multiple engines
209 having the same articles but article numbers are not kept synchronized
210 between them. If you connect to such a server, set this to a non-nil
211 value, and Gnus never uses article numbers (that appear in the Xref
212 header and vary by which engine is chosen) to refer to articles.")
214 (defvoo nntp-prepare-server-hook nil
215 "*Hook run before a server is opened.
216 If can be used to set up a server remotely, for instance. Say you
217 have an account at the machine \"other.machine\". This machine has
218 access to an NNTP server that you can't access locally. You could
219 then use this hook to rsh to the remote machine and start a proxy NNTP
220 server there that you can connect to. See also
221 `nntp-open-connection-function'")
223 (defcustom nntp-authinfo-file "~/.authinfo"
224 ".netrc-like file that holds nntp authinfo passwords."
228 (repeat :tag "Entries"
231 :value ("" ("login" . "") ("password" . ""))
235 (const :format "" "login")
236 (string :format "Login: %v"))
238 (const :format "" "password")
239 (string :format "Password: %v")))))))
241 (make-obsolete 'nntp-authinfo-file nil "Emacs 24.1")
245 (defvoo nntp-connection-timeout nil
246 "*Number of seconds to wait before an nntp connection times out.
247 If this variable is nil, which is the default, no timers are set.")
249 (defvoo nntp-prepare-post-hook nil
250 "*Hook run just before posting an article. It is supposed to be used
251 to insert Cancel-Lock headers.")
253 (defvoo nntp-server-list-active-group 'try
254 "If nil, then always use GROUP instead of LIST ACTIVE.
255 This is usually slower, but on misconfigured servers that don't
256 update their active files often, this can help.")
258 ;;; Internal variables.
260 (defvoo nntp-retrieval-in-progress nil)
261 (defvar nntp-record-commands nil
262 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
264 (defvar nntp-have-messaged nil)
266 (defvar nntp-process-wait-for nil)
267 (defvar nntp-process-to-buffer nil)
268 (defvar nntp-process-callback nil)
269 (defvar nntp-process-decode nil)
270 (defvar nntp-process-start-point nil)
271 (defvar nntp-inside-change-function nil)
272 (defvoo nntp-last-command-time nil)
273 (defvoo nntp-last-command nil)
274 (defvoo nntp-authinfo-password nil)
275 (defvoo nntp-authinfo-user nil)
276 (defvoo nntp-authinfo-force nil)
278 (defvar nntp-connection-list nil)
280 (defvoo nntp-server-type nil)
281 (defvoo nntp-connection-alist nil)
282 (defvoo nntp-status-string "")
283 (defconst nntp-version "nntp 5.0")
284 (defvoo nntp-inhibit-erase nil)
285 (defvoo nntp-inhibit-output nil)
287 (defvoo nntp-server-xover 'try)
289 (defvar nntp-async-timer nil)
290 (defvar nntp-async-process-list nil)
292 (defvar nntp-authinfo-rejected nil
293 "A custom error condition used to report `Authentication Rejected' errors.
294 Condition handlers that match just this condition ensure that the nntp
295 backend doesn't catch this error.")
296 (put 'nntp-authinfo-rejected 'error-conditions '(error nntp-authinfo-rejected))
297 (put 'nntp-authinfo-rejected 'error-message "Authorization Rejected")
301 ;;; Internal functions.
303 (defsubst nntp-send-string (process string)
304 "Send STRING to PROCESS."
305 ;; We need to store the time to provide timeouts, and
306 ;; to store the command so the we can replay the command
307 ;; if the server gives us an AUTHINFO challenge.
308 (setq nntp-last-command-time (current-time)
309 nntp-last-command string)
310 (when nntp-record-commands
311 (nntp-record-command string))
312 (process-send-string process (concat string nntp-end-of-line))
313 (or (memq (process-status process) '(open run))
314 (nntp-report "Server closed connection")))
316 (defun nntp-record-command (string)
317 "Record the command STRING."
318 (with-current-buffer (get-buffer-create "*nntp-log*")
319 (goto-char (point-max))
320 (insert (format-time-string "%Y%m%dT%H%M%S.%3N")
321 " " nntp-address " " string "\n")))
323 (defvar nntp--report-1 nil)
325 (defun nntp-report (&rest args)
326 "Report an error from the nntp backend. The first string in ARGS
327 can be a format string. For some commands, the failed command may be
328 retried once before actually displaying the error report."
331 ;; Throw out to nntp-with-open-group-error so that the connection may
332 ;; be restored and the command retried."
333 (when nntp-record-commands
334 (nntp-record-command "*** CONNECTION LOST ***"))
335 (throw 'nntp-with-open-group-error t))
337 (when nntp-record-commands
338 (nntp-record-command "*** CALLED nntp-report ***"))
340 (nnheader-report 'nntp args)
342 (apply 'error args)))
344 (defmacro nntp-copy-to-buffer (buffer start end)
345 "Copy string from unibyte current buffer to multibyte buffer."
346 `(let ((string (buffer-substring ,start ,end)))
347 (with-current-buffer ,buffer
349 (insert (if enable-multibyte-characters
350 (string-to-multibyte string)
352 (goto-char (point-min))
355 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
356 "Wait for WAIT-FOR to arrive from PROCESS."
358 (with-current-buffer (process-buffer process)
359 (goto-char (point-min))
361 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
362 (looking-at "48[02]"))
363 (memq (process-status process) '(open run)))
364 (cond ((looking-at "480")
365 (nntp-handle-authinfo process))
367 (nnheader-report 'nntp "%s"
368 (get 'nntp-authinfo-rejected 'error-message))
369 (signal 'nntp-authinfo-rejected nil))
370 ((looking-at "^.*\n")
371 (delete-region (point) (progn (forward-line 1) (point)))))
372 (nntp-accept-process-output process)
373 (goto-char (point-min)))
378 (nntp-snarf-error-message)
380 ((not (memq (process-status process) '(open run)))
381 (nntp-report "Server closed connection"))
383 (goto-char (point-max))
384 (let ((limit (point-min))
386 (while (not (re-search-backward wait-for limit t))
387 (nntp-accept-process-output process)
388 ;; We assume that whatever we wait for is less than 1000
390 (setq limit (max (- (point-max) 1000) (point-min)))
391 (goto-char (point-max)))
392 (setq response (match-string 0))
393 (with-current-buffer nntp-server-buffer
394 (setq nntp-process-response response)))
395 (nntp-decode-text (not decode))
397 (with-current-buffer buffer
398 (goto-char (point-max))
399 (nnheader-insert-buffer-substring (process-buffer process))
400 ;; Nix out "nntp reading...." message.
401 (when nntp-have-messaged
402 (setq nntp-have-messaged nil)
403 (nnheader-message 5 ""))))
408 (defun nntp-kill-buffer (buffer)
409 (when (buffer-name buffer)
410 (let ((process (get-buffer-process buffer)))
412 (delete-process process)))
414 (nnheader-init-server-buffer)))
416 (defun nntp-erase-buffer (buffer)
417 "Erase contents of BUFFER."
418 (with-current-buffer buffer
421 (defsubst nntp-find-connection (buffer)
422 "Find the connection delivering to BUFFER."
423 (let ((alist nntp-connection-alist)
424 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
426 (while (and alist (setq entry (pop alist)))
427 (when (eq buffer (cadr entry))
428 (setq process (car entry)
431 (if (memq (process-status process) '(open run))
433 (nntp-kill-buffer (process-buffer process))
434 (setq nntp-connection-alist (delq entry nntp-connection-alist))
437 (defsubst nntp-find-connection-entry (buffer)
438 "Return the entry for the connection to BUFFER."
439 (assq (nntp-find-connection buffer) nntp-connection-alist))
441 (defun nntp-find-connection-buffer (buffer)
442 "Return the process connection buffer tied to BUFFER."
443 (let ((process (nntp-find-connection buffer)))
445 (process-buffer process))))
447 (defsubst nntp-retrieve-data (command address port buffer
448 &optional wait-for callback decode)
449 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
450 (let ((process (or (nntp-find-connection buffer)
451 (nntp-open-connection buffer))))
454 (unless (or nntp-inhibit-erase nnheader-callback-function)
455 (nntp-erase-buffer (process-buffer process)))
459 (nntp-send-string process command))
461 ((eq callback 'ignore)
463 ((and callback wait-for)
464 (nntp-async-wait process wait-for buffer decode callback)
467 (nntp-wait-for process wait-for buffer decode))
469 (nntp-authinfo-rejected
470 (signal 'nntp-authinfo-rejected (cdr err)))
472 (nnheader-report 'nntp "Couldn't open connection to %s: %s"
475 (message "Quit retrieving data from nntp")
478 (nnheader-report 'nntp "Couldn't open connection to %s" address))))
480 (defsubst nntp-send-command (wait-for &rest strings)
481 "Send STRINGS to server and wait until WAIT-FOR returns."
482 (when (and (not nnheader-callback-function)
483 (not nntp-inhibit-output))
484 (nntp-erase-buffer nntp-server-buffer))
485 (let* ((command (mapconcat 'identity strings " "))
486 (process (nntp-find-connection nntp-server-buffer))
487 (buffer (and process (process-buffer process)))
488 (pos (and buffer (with-current-buffer buffer (point)))))
491 (nntp-retrieve-data command
492 nntp-address nntp-port-number
494 wait-for nnheader-callback-function)
495 ;; If nothing to wait for, still remove possibly echo'ed commands.
496 ;; We don't have echoes if `nntp-never-echoes-commands' is non-nil
497 ;; or the value of `nntp-open-connection-function' is in
498 ;; `nntp-open-connection-functions-never-echo-commands', so we
499 ;; skip this in that cases.
501 nntp-never-echoes-commands
503 nntp-open-connection-function
504 nntp-open-connection-functions-never-echo-commands))
505 (nntp-accept-response)
506 (with-current-buffer buffer
508 (if (looking-at (regexp-quote command))
509 (delete-region pos (progn (forward-line 1)
511 (nnheader-report 'nntp "Couldn't open connection to %s."
514 (defun nntp-send-command-nodelete (wait-for &rest strings)
515 "Send STRINGS to server and wait until WAIT-FOR returns."
516 (let* ((command (mapconcat 'identity strings " "))
517 (process (nntp-find-connection nntp-server-buffer))
518 (buffer (and process (process-buffer process)))
519 (pos (and buffer (with-current-buffer buffer (point)))))
522 (nntp-retrieve-data command
523 nntp-address nntp-port-number
525 wait-for nnheader-callback-function)
526 ;; If nothing to wait for, still remove possibly echo'ed commands
528 (nntp-accept-response)
529 (with-current-buffer buffer
531 (if (looking-at (regexp-quote command))
532 (delete-region pos (progn (forward-line 1)
534 (nnheader-report 'nntp "Couldn't open connection to %s."
537 (defun nntp-send-command-and-decode (wait-for &rest strings)
538 "Send STRINGS to server and wait until WAIT-FOR returns."
539 (when (and (not nnheader-callback-function)
540 (not nntp-inhibit-output))
541 (nntp-erase-buffer nntp-server-buffer))
542 (let* ((command (mapconcat 'identity strings " "))
543 (process (nntp-find-connection nntp-server-buffer))
544 (buffer (and process (process-buffer process)))
545 (pos (and buffer (with-current-buffer buffer (point)))))
548 (nntp-retrieve-data command
549 nntp-address nntp-port-number
551 wait-for nnheader-callback-function t)
552 ;; If nothing to wait for, still remove possibly echo'ed commands
554 (nntp-accept-response)
555 (with-current-buffer buffer
557 (if (looking-at (regexp-quote command))
558 (delete-region pos (progn (forward-line 1) (point-at-bol))))
560 (nnheader-report 'nntp "Couldn't open connection to %s."
564 (defun nntp-send-buffer (wait-for)
565 "Send the current buffer to server and wait until WAIT-FOR returns."
566 (when (and (not nnheader-callback-function)
567 (not nntp-inhibit-output))
569 (nntp-find-connection-buffer nntp-server-buffer)))
571 ;; Make sure we did not forget to encode some of the content.
572 (assert (save-excursion (goto-char (point-min))
573 (not (re-search-forward "[^\000-\377]" nil t))))
574 (mm-disable-multibyte)
575 (process-send-region (nntp-find-connection nntp-server-buffer)
576 (point-min) (point-max))
578 nil nntp-address nntp-port-number nntp-server-buffer
579 wait-for nnheader-callback-function))
583 ;;; Interface functions.
585 (nnoo-define-basics nntp)
587 (defsubst nntp-next-result-arrived-p ()
589 ;; A result that starts with a 2xx code is terminated by
590 ;; a line with only a "." on it.
591 ((eq (char-after) ?2)
592 (if (re-search-forward "\n\\.\r?\n" nil t)
594 ;; Some broken news servers add another dot at the end.
595 ;; Protect against inflooping there.
596 (while (looking-at "^\\.\r?\n")
600 ;; A result that starts with a 3xx or 4xx code is terminated
603 (if (search-forward "\n" nil t)
610 (defun nntp-with-open-group-function (-group -server -connectionless -bodyfun)
611 "Protect against servers that don't like clients that keep idle connections opens.
612 The problem being that these servers may either close a connection or
613 simply ignore any further requests on a connection. Closed
614 connections are not detected until `accept-process-output' has updated
615 the `process-status'. Dropped connections are not detected until the
616 connection timeouts (which may be several minutes) or
617 `nntp-connection-timeout' has expired. When these occur
618 `nntp-with-open-group', opens a new connection then re-issues the NNTP
619 command whose response triggered the error."
620 (let ((nntp-report-n nntp--report-1)
622 (nntp-with-open-group-internal nil))
623 (while (catch 'nntp-with-open-group-error
624 ;; Open the connection to the server
625 ;; NOTE: Existing connections are NOT tested.
626 (nntp-possibly-change-group -group -server -connectionless)
629 (and nntp-connection-timeout
631 nntp-connection-timeout nil
633 (let* ((-process (nntp-find-connection
635 (-buffer (and -process
636 (process-buffer -process))))
637 ;; When I an able to identify the
638 ;; connection to the server AND I've
639 ;; received NO response for
640 ;; nntp-connection-timeout seconds.
641 (when (and -buffer (eq 0 (buffer-size -buffer)))
642 ;; Close the connection. Take no
643 ;; other action as the accept input
644 ;; code will handle the closed
646 (nntp-kill-buffer -buffer))))))))
648 (setq nntp-with-open-group-internal
652 (unless debug-on-quit
654 (signal 'quit nil))))
656 (nnheader-cancel-timer -timer)))
658 (setq nntp--report-1 nntp-report-n))
659 nntp-with-open-group-internal))
661 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
662 "Protect against servers that don't like clients that keep idle connections opens.
663 The problem being that these servers may either close a connection or
664 simply ignore any further requests on a connection. Closed
665 connections are not detected until `accept-process-output' has updated
666 the `process-status'. Dropped connections are not detected until the
667 connection timeouts (which may be several minutes) or
668 `nntp-connection-timeout' has expired. When these occur
669 `nntp-with-open-group', opens a new connection then re-issues the NNTP
670 command whose response triggered the error."
671 (declare (indent 2) (debug (form form [&optional symbolp] def-body)))
672 (when (and (listp connectionless)
673 (not (eq connectionless nil)))
674 (setq forms (cons connectionless forms)
676 `(nntp-with-open-group-function ,group ,server ,connectionless (lambda () ,@forms)))
678 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
679 "Retrieve the headers of ARTICLES."
680 (nntp-with-open-group
682 (with-current-buffer (nntp-find-connection-buffer nntp-server-buffer)
684 (if (and (not gnus-nov-is-evil)
685 (not nntp-nov-is-evil)
686 (nntp-retrieve-headers-with-xover articles fetch-old))
687 ;; We successfully retrieved the headers via XOVER.
689 ;; XOVER didn't work, so we do it the hard, slow and inefficient
691 (let ((number (length articles))
695 (last-point (point-min))
696 (buf (nntp-find-connection-buffer nntp-server-buffer))
697 (nntp-inhibit-erase t)
699 ;; Send HEAD commands.
700 (while (setq article (pop articles))
703 "HEAD" (if (numberp article)
704 (int-to-string article)
705 ;; `articles' is either a list of article numbers
706 ;; or a list of article IDs.
709 ;; Every 400 requests we have to read the stream in
710 ;; order to avoid deadlocks.
711 (when (or (null articles) ;All requests have been sent.
712 (zerop (% count nntp-maximum-request)))
713 (nntp-accept-response)
716 (goto-char last-point)
718 (while (nntp-next-result-arrived-p)
719 (setq last-point (point))
722 ;; If number of headers is greater than 100, give
723 ;; informative messages.
724 (and (numberp nntp-large-newsgroup)
725 (> number nntp-large-newsgroup)
726 (zerop (% received 20))
727 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
728 (floor (* received 100.0) number)))
729 (nntp-accept-response))))
730 (and (numberp nntp-large-newsgroup)
731 (> number nntp-large-newsgroup)
732 (nnheader-message 6 "NNTP: Receiving headers...done"))
734 ;; Now all of replies are received. Fold continuation lines.
735 (nnheader-fold-continuation-lines)
736 ;; Remove all "\r"'s.
738 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
741 (deffoo nntp-retrieve-group-data-early (server infos)
742 "Retrieve group info on INFOS."
743 (nntp-with-open-group nil server
744 (let ((buffer (nntp-find-connection-buffer nntp-server-buffer)))
746 (with-current-buffer buffer
747 (setq nntp-retrieval-in-progress nil)))
750 (with-current-buffer buffer
751 (not nntp-retrieval-in-progress)))
752 ;; The first time this is run, this variable is `try'. So we
754 (when (eq nntp-server-list-active-group 'try)
755 (nntp-try-list-active
756 (gnus-group-real-name (gnus-info-group (car infos)))))
757 (with-current-buffer buffer
759 ;; Mark this buffer as "in use" in case we try to issue two
760 ;; retrievals from the same server. This shouldn't happen,
761 ;; so this is mostly a sanity check.
762 (setq nntp-retrieval-in-progress t)
763 (let ((nntp-inhibit-erase t)
764 (command (if nntp-server-list-active-group
765 "LIST ACTIVE" "GROUP")))
768 nil command (gnus-group-real-name (gnus-info-group info)))))
771 (deffoo nntp-finish-retrieve-group-infos (server infos count)
772 (nntp-with-open-group nil server
773 (let ((buf (nntp-find-connection-buffer nntp-server-buffer))
774 (method (gnus-find-method-for-group
775 (gnus-info-group (car infos))
779 (with-current-buffer buf
780 (setq nntp-retrieval-in-progress nil))
783 (with-current-buffer buf
784 (while (and (gnus-buffer-live-p buf)
786 (goto-char last-point)
788 (while (re-search-forward
789 (if nntp-server-list-active-group
794 (setq last-point (point))
796 (nntp-accept-response))
797 ;; We now have all the entries. Remove CRs.
799 (if (not nntp-server-list-active-group)
801 (nntp-copy-to-buffer nntp-server-buffer
802 (point-min) (point-max))
803 (with-current-buffer nntp-server-buffer
804 (gnus-groups-to-gnus-format method gnus-active-hashtb t)))
805 ;; We have read active entries, so we just delete the
807 (goto-char (point-min))
808 (while (re-search-forward "^[.2-5]" nil t)
809 (delete-region (match-beginning 0)
810 (progn (forward-line 1) (point))))
811 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
812 (with-current-buffer nntp-server-buffer
813 (gnus-active-to-gnus-format
814 ;; Kludge to use the extended method name if you have
816 (if (consp (gnus-info-method (car infos)))
817 (gnus-info-method (car infos))
819 gnus-active-hashtb nil t))))))))
821 (deffoo nntp-retrieve-groups (groups &optional server)
822 "Retrieve group info on GROUPS."
823 (nntp-with-open-group
825 (when (and (nntp-find-connection-buffer nntp-server-buffer)
827 (nntp-find-connection-buffer nntp-server-buffer)
828 (if (not nntp-retrieval-in-progress)
830 (message "Warning: Refusing to do retrieval from %s because a retrieval is already happening"
835 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
836 (nntp-erase-buffer nntp-server-buffer)
837 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
838 ;; The first time this is run, this variable is `try'. So we
840 (when (eq nntp-server-list-active-group 'try)
841 (nntp-try-list-active (car groups)))
846 (last-point (point-min))
847 (nntp-inhibit-erase t)
848 (buf (nntp-find-connection-buffer nntp-server-buffer))
849 (command (if nntp-server-list-active-group
850 "LIST ACTIVE" "GROUP")))
852 ;; Timeout may have killed the buffer.
853 (unless (gnus-buffer-live-p buf)
854 (nnheader-report 'nntp "Connection to %s is closed." server)
856 ;; Send the command to the server.
857 (nntp-send-command nil command (pop groups))
859 ;; Every 400 requests we have to read the stream in
860 ;; order to avoid deadlocks.
861 (when (or (null groups) ;All requests have been sent.
862 (zerop (% count nntp-maximum-request)))
863 (nntp-accept-response)
864 (while (and (gnus-buffer-live-p buf)
866 ;; Search `blue moon' in this file for the
867 ;; reason why set-buffer here.
869 (goto-char last-point)
871 (while (re-search-forward "^[0-9]" nil t)
873 (setq last-point (point))
875 (nntp-accept-response))))
877 ;; Wait for the reply from the final command.
878 (unless (gnus-buffer-live-p buf)
879 (nnheader-report 'nntp "Connection to %s is closed." server)
882 (goto-char (point-max))
883 (re-search-backward "^[0-9]" nil t)
884 (when (looking-at "^[23]")
885 (while (and (gnus-buffer-live-p buf)
888 (goto-char (point-max))
889 (if (not nntp-server-list-active-group)
890 (not (re-search-backward "\r?\n"
892 (not (re-search-backward "^\\.\r?\n"
894 (nntp-accept-response)))
896 ;; Now all replies are received. We remove CRs.
897 (unless (gnus-buffer-live-p buf)
898 (nnheader-report 'nntp "Connection to %s is closed." server)
901 (goto-char (point-min))
902 (while (search-forward "\r" nil t)
903 (replace-match "" t t))
905 (if (not nntp-server-list-active-group)
907 (nntp-copy-to-buffer nntp-server-buffer
908 (point-min) (point-max))
910 ;; We have read active entries, so we just delete the
912 (goto-char (point-min))
913 (while (re-search-forward "^[.2-5]" nil t)
914 (delete-region (match-beginning 0)
915 (progn (forward-line 1) (point))))
916 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
919 (deffoo nntp-retrieve-articles (articles &optional group server)
920 (nntp-with-open-group
923 (let ((number (length articles))
927 (last-point (point-min))
928 (buf (nntp-find-connection-buffer nntp-server-buffer))
929 (nntp-inhibit-erase t)
930 (map (apply 'vector articles))
935 ;; Send ARTICLE command.
936 (while (setq article (pop articles))
939 "ARTICLE" (if (numberp article)
940 (int-to-string article)
941 ;; `articles' is either a list of article numbers
942 ;; or a list of article IDs.
945 ;; Every 400 requests we have to read the stream in
946 ;; order to avoid deadlocks.
947 (when (or (null articles) ;All requests have been sent.
948 (zerop (% count nntp-maximum-request)))
949 (nntp-accept-response)
952 (goto-char last-point)
954 (while (nntp-next-result-arrived-p)
955 (aset map received (cons (aref map received) (point)))
956 (setq last-point (point))
959 ;; If number of headers is greater than 100, give
960 ;; informative messages.
961 (and (numberp nntp-large-newsgroup)
962 (> number nntp-large-newsgroup)
963 (zerop (% received 20))
964 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
965 (floor (* received 100.0) number)))
966 (nntp-accept-response))))
967 (and (numberp nntp-large-newsgroup)
968 (> number nntp-large-newsgroup)
969 (nnheader-message 6 "NNTP: Receiving articles...done"))
971 ;; Now we have all the responses. We go through the results,
972 ;; wash it and copy it over to the server buffer.
973 (set-buffer nntp-server-buffer)
975 (setq last-point (point-min))
979 (setq point (goto-char (point-max)))
981 (nnheader-insert-buffer-substring buf last-point (cdr entry))
983 (setq last-point (cdr entry))
986 (cons (car entry) point))
989 (defun nntp-try-list-active (group)
990 (nntp-list-active-group group)
991 (with-current-buffer nntp-server-buffer
992 (goto-char (point-min))
994 (looking-at "5[0-9]+"))
995 (setq nntp-server-list-active-group nil))
997 (setq nntp-server-list-active-group t)))))
999 (deffoo nntp-list-active-group (group &optional server)
1000 "Return the active info on GROUP (which can be a regexp)."
1001 (nntp-with-open-group
1003 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
1005 (deffoo nntp-request-group-articles (group &optional server)
1006 "Return the list of existing articles in GROUP."
1007 (nntp-with-open-group
1009 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
1011 (deffoo nntp-request-article (article &optional group server buffer command)
1012 (nntp-with-open-group
1014 (when (nntp-send-command-and-decode
1015 "\r?\n\\.\r?\n" "ARTICLE"
1016 (if (numberp article) (int-to-string article) article))
1018 (not (equal buffer nntp-server-buffer)))
1019 (with-current-buffer nntp-server-buffer
1020 (copy-to-buffer buffer (point-min) (point-max))))
1021 (nntp-find-group-and-number group))))
1023 (deffoo nntp-request-head (article &optional group server)
1024 (nntp-with-open-group
1026 (when (nntp-send-command
1027 "\r?\n\\.\r?\n" "HEAD"
1028 (if (numberp article) (int-to-string article) article))
1030 (nntp-find-group-and-number group)
1031 (nntp-decode-text)))))
1033 (deffoo nntp-request-body (article &optional group server)
1034 (nntp-with-open-group
1036 (nntp-send-command-and-decode
1037 "\r?\n\\.\r?\n" "BODY"
1038 (if (numberp article) (int-to-string article) article))))
1040 (deffoo nntp-request-group (group &optional server dont-check info)
1041 (nntp-with-open-group
1043 (when (nntp-send-command "^[245].*\n" "GROUP" group)
1044 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1045 (setcar (cddr entry) group)))))
1047 (deffoo nntp-close-group (group &optional server)
1050 (deffoo nntp-server-opened (&optional server)
1051 "Say whether a connection to SERVER has been opened."
1052 (and (nnoo-current-server-p 'nntp server)
1054 (gnus-buffer-live-p nntp-server-buffer)
1055 (nntp-find-connection nntp-server-buffer)))
1057 (deffoo nntp-open-server (server &optional defs connectionless)
1058 (nnheader-init-server-buffer)
1059 (if (nntp-server-opened server)
1061 (when (or (stringp (car defs))
1062 (numberp (car defs)))
1063 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
1064 (unless (assq 'nntp-address defs)
1065 (setq defs (append defs (list (list 'nntp-address server)))))
1066 (nnoo-change-server 'nntp server defs)
1069 (or (nntp-find-connection nntp-server-buffer)
1070 (nntp-open-connection nntp-server-buffer)))))
1072 (deffoo nntp-close-server (&optional server)
1073 (nntp-possibly-change-group nil server t)
1074 (let ((process (nntp-find-connection nntp-server-buffer)))
1076 (when (memq (process-status process) '(open run))
1078 (nntp-send-string process "QUIT")
1079 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
1080 ;; Ok, this is evil, but when using telnet and stuff
1081 ;; as the connection method, it's important that the
1082 ;; QUIT command actually is sent out before we kill
1085 (nntp-kill-buffer (process-buffer process))
1086 (setq process (car (pop nntp-connection-alist))))
1087 (nnoo-close-server 'nntp)))
1089 (deffoo nntp-request-close ()
1091 (while (setq process (pop nntp-connection-list))
1092 (when (memq (process-status process) '(open run))
1094 (nntp-send-string process "QUIT")
1095 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
1096 ;; Ok, this is evil, but when using telnet and stuff
1097 ;; as the connection method, it's important that the
1098 ;; QUIT command actually is sent out before we kill
1101 (nntp-kill-buffer (process-buffer process)))))
1103 (deffoo nntp-request-list (&optional server)
1104 (nntp-with-open-group
1106 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
1108 (deffoo nntp-request-list-newsgroups (&optional server)
1109 (nntp-with-open-group
1111 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
1113 (deffoo nntp-request-newgroups (date &optional server)
1114 (nntp-with-open-group
1116 (with-current-buffer nntp-server-buffer
1119 "^\\.\r?\n" "NEWGROUPS"
1120 (format-time-string "%y%m%d %H%M%S" (date-to-time date) t)
1122 (nntp-decode-text)))))
1124 (deffoo nntp-request-post (&optional server)
1125 (nntp-with-open-group
1127 (when (nntp-send-command "^[23].*\r?\n" "POST")
1128 (let ((response (with-current-buffer nntp-server-buffer
1129 nntp-process-response))
1132 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
1134 (setq server-id (match-string 1 response))
1135 (narrow-to-region (goto-char (point-min))
1136 (if (search-forward "\n\n" nil t)
1139 (unless (mail-fetch-field "Message-ID")
1140 (goto-char (point-min))
1141 (insert "Message-ID: " server-id "\n"))
1143 (run-hooks 'nntp-prepare-post-hook)
1144 (nntp-send-buffer "^[23].*\n")))))
1146 (deffoo nntp-request-type (group article)
1149 (deffoo nntp-asynchronous-p ()
1153 ;;; Hooky functions.
1155 (defun nntp-send-mode-reader ()
1156 "Send the MODE READER command to the nntp server.
1157 This function is supposed to be called from `nntp-server-opened-hook'.
1158 It will make innd servers spawn an nnrpd process to allow actual article
1160 (nntp-send-command "^.*\n" "MODE READER"))
1162 (declare-function netrc-parse "netrc" (&optional file))
1163 (declare-function netrc-machine "netrc"
1164 (list machine &optional port defaultport))
1165 (declare-function netrc-get "netrc" (alist type))
1167 (defun nntp-send-authinfo (&optional send-if-force)
1168 "Send the AUTHINFO to the nntp server.
1169 It will look in the \"~/.authinfo\" file for matching entries. If
1170 nothing suitable is found there, it will prompt for a user name
1173 If SEND-IF-FORCE, only send authinfo to the server if the
1174 .authinfo file has the FORCE token."
1176 (let* ((list (netrc-parse nntp-authinfo-file))
1177 (alist (netrc-machine list nntp-address "nntp"))
1179 (nth 0 (auth-source-search
1181 :host (list nntp-address (nnoo-current-server 'nntp))
1182 :port `("119" "nntp" ,(format "%s" nntp-port-number)
1183 "563" "nntps" "snews"))))
1184 (auth-user (plist-get auth-info :user))
1185 (auth-force (plist-get auth-info :force))
1186 (auth-passwd (plist-get auth-info :secret))
1187 (auth-passwd (if (functionp auth-passwd)
1188 (funcall auth-passwd)
1190 (force (or (netrc-get alist "force")
1194 ;; this is preferred to netrc-*
1196 (netrc-get alist "login")
1197 nntp-authinfo-user))
1199 ;; this is preferred to netrc-*
1201 (netrc-get alist "password"))))
1202 (when (or (not send-if-force)
1205 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1206 nntp-authinfo-user user))
1207 (unless (member user '(nil ""))
1208 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1211 "^2.*\r?\n" "AUTHINFO PASS"
1213 nntp-authinfo-password
1214 (setq nntp-authinfo-password
1215 (read-passwd (format "NNTP (%s@%s) password: "
1216 user nntp-address)))))))
1218 (signal 'nntp-authinfo-rejected "Password rejected")
1221 ;;; Internal functions.
1223 (defun nntp-handle-authinfo (process)
1224 "Take care of an authinfo response from the server."
1225 (let ((last nntp-last-command))
1226 (funcall nntp-authinfo-function)
1227 ;; We have to re-send the function that was interrupted by
1228 ;; the authinfo request.
1229 (nntp-erase-buffer nntp-server-buffer)
1230 (nntp-send-string process last)))
1232 (defun nntp-make-process-buffer (buffer)
1233 "Create a new, fresh buffer usable for nntp process connections."
1234 (with-current-buffer
1235 (generate-new-buffer
1236 (format " *server %s %s %s*"
1237 nntp-address nntp-port-number
1238 (gnus-buffer-exists-p buffer)))
1239 (mm-disable-multibyte)
1240 (set (make-local-variable 'after-change-functions) nil)
1241 (set (make-local-variable 'nntp-process-wait-for) nil)
1242 (set (make-local-variable 'nntp-process-callback) nil)
1243 (set (make-local-variable 'nntp-process-to-buffer) nil)
1244 (set (make-local-variable 'nntp-process-start-point) nil)
1245 (set (make-local-variable 'nntp-process-decode) nil)
1246 (set (make-local-variable 'nntp-retrieval-in-progress) nil)
1249 (defun nntp-open-connection (buffer)
1250 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1251 (run-hooks 'nntp-prepare-server-hook)
1252 (let* ((pbuffer (nntp-make-process-buffer buffer))
1254 (and nntp-connection-timeout
1256 nntp-connection-timeout nil
1258 (nntp-kill-buffer ,pbuffer)))))
1261 (let ((coding-system-for-read 'binary)
1262 (coding-system-for-write 'binary)
1263 (map '((nntp-open-network-stream network)
1264 (network-only plain) ; compat
1265 (nntp-open-plain-stream plain)
1266 (nntp-open-ssl-stream tls)
1267 (nntp-open-tls-stream tls))))
1268 (if (assoc nntp-open-connection-function map)
1269 (open-protocol-stream
1270 "nntpd" pbuffer nntp-address nntp-port-number
1271 :type (cadr (assoc nntp-open-connection-function map))
1272 :end-of-command "^\\([2345]\\|[.]\\).*\n"
1273 :capability-command "HELP\r\n"
1276 (lambda (capabilities)
1277 (if (not (string-match "STARTTLS" capabilities))
1280 (funcall nntp-open-connection-function pbuffer)))
1282 (nnheader-report 'nntp ">>> %s" err))
1284 (message "Quit opening connection to %s" nntp-address)
1285 (nntp-kill-buffer pbuffer)
1289 (nnheader-cancel-timer timer))
1291 (not (memq (process-status process) '(open run))))
1292 (with-current-buffer pbuffer
1293 (goto-char (point-min))
1294 (nnheader-report 'nntp "Error when connecting: %s"
1295 (buffer-substring (point) (line-end-position))))
1298 (nntp-kill-buffer pbuffer))
1299 (when (and (buffer-name pbuffer)
1301 (when (eq (process-type process) 'network)
1302 ;; Use TCP-keepalive so that connections that pass through a NAT router
1303 ;; don't hang when left idle.
1304 (set-network-process-option process :keepalive t))
1305 (gnus-set-process-query-on-exit-flag process nil)
1306 (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1307 (memq (process-status process) '(open run)))
1309 (caar (push (list process buffer nil) nntp-connection-alist))
1310 (push process nntp-connection-list)
1311 (with-current-buffer pbuffer
1312 (nntp-read-server-type)
1314 (set-buffer nntp-server-buffer)
1315 (let ((nnheader-callback-function nil))
1316 (run-hooks 'nntp-server-opened-hook)
1317 (nntp-send-authinfo t))))
1318 (nntp-kill-buffer (process-buffer process))
1321 (defun nntp-read-server-type ()
1322 "Find out what the name of the server we have connected to is."
1323 ;; Wait for the status string to arrive.
1324 (setq nntp-server-type (buffer-string))
1325 (let ((case-fold-search t))
1326 ;; Run server-specific commands.
1327 (dolist (entry nntp-server-action-alist)
1328 (when (string-match (car entry) nntp-server-type)
1329 (if (and (listp (cadr entry))
1330 (not (eq 'lambda (caadr entry))))
1332 (funcall (cadr entry)))))))
1334 (defun nntp-async-wait (process wait-for buffer decode callback)
1335 (with-current-buffer (process-buffer process)
1336 (unless nntp-inside-change-function
1338 (setq nntp-process-wait-for wait-for
1339 nntp-process-to-buffer buffer
1340 nntp-process-decode decode
1341 nntp-process-callback callback
1342 nntp-process-start-point (point-max))
1343 (setq after-change-functions '(nntp-after-change-function))))
1345 (defun nntp-async-stop (proc)
1346 (setq nntp-async-process-list (delq proc nntp-async-process-list))
1347 (when (and nntp-async-timer (not nntp-async-process-list))
1348 (nnheader-cancel-timer nntp-async-timer)
1349 (setq nntp-async-timer nil)))
1351 (defun nntp-after-change-function (beg end len)
1353 ;; we only care about insertions at eob
1354 (when (and (eq 0 len) (eq (point-max) end))
1356 (let ((proc (get-buffer-process (current-buffer))))
1358 (nntp-async-trigger proc)))))
1359 ;; any throw from after-change-functions will leave it
1360 ;; set to nil. so we reset it here, if necessary.
1362 (setq after-change-functions '(nntp-after-change-function)))))
1364 (defun nntp-async-trigger (process)
1365 (with-current-buffer (process-buffer process)
1366 (when nntp-process-callback
1367 ;; do we have an error message?
1368 (goto-char nntp-process-start-point)
1369 (if (memq (following-char) '(?4 ?5))
1370 ;; wants credentials?
1371 (if (looking-at "480")
1372 (nntp-handle-authinfo process)
1373 ;; report error message.
1374 (nntp-snarf-error-message)
1375 (nntp-do-callback nil))
1377 ;; got what we expect?
1378 (goto-char (point-max))
1379 (when (re-search-backward
1380 nntp-process-wait-for nntp-process-start-point t)
1381 (let ((response (match-string 0)))
1382 (with-current-buffer nntp-server-buffer
1383 (setq nntp-process-response response)))
1384 (nntp-async-stop process)
1386 (when (gnus-buffer-exists-p nntp-process-to-buffer)
1387 (let ((buf (current-buffer))
1388 (start nntp-process-start-point)
1389 (decode nntp-process-decode))
1390 (with-current-buffer nntp-process-to-buffer
1391 (goto-char (point-max))
1393 (narrow-to-region (point) (point))
1394 (nnheader-insert-buffer-substring buf start)
1396 (nntp-decode-text))))))
1398 (goto-char (point-max))
1400 (buffer-name (get-buffer nntp-process-to-buffer))))))))
1402 (defun nntp-do-callback (arg)
1403 (let ((callback nntp-process-callback)
1404 (nntp-inside-change-function t))
1405 (setq nntp-process-callback nil)
1406 (funcall callback arg)))
1408 (defun nntp-snarf-error-message ()
1409 "Save the error message in the current buffer."
1410 (let ((message (buffer-string)))
1411 (while (string-match "[\r\n]+" message)
1412 (setq message (replace-match " " t t message)))
1413 (nnheader-report 'nntp "%s" message)
1416 (defun nntp-accept-process-output (process)
1417 "Wait for output from PROCESS and message some dots."
1418 (with-current-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1420 (let ((len (/ (buffer-size) 1024))
1423 (setq nntp-have-messaged t)
1424 (nnheader-message 7 "nntp read: %dk" len)))
1426 (nnheader-accept-process-output process)
1427 ;; accept-process-output may update status of process to indicate
1428 ;; that the server has closed the connection. This MUST be
1429 ;; handled here as the buffer restored by the save-excursion may
1430 ;; be the process's former output buffer (i.e. now killed)
1432 (memq (process-status process) '(open run)))
1433 (nntp-report "Server closed connection")))))
1435 (defun nntp-accept-response ()
1436 "Wait for output from the process that outputs to BUFFER."
1437 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1439 (defun nntp-possibly-change-group (group server &optional connectionless)
1440 (let ((nnheader-callback-function nil))
1442 (or (nntp-server-opened server)
1443 (nntp-open-server server nil connectionless)))
1445 (unless connectionless
1446 (or (nntp-find-connection nntp-server-buffer)
1447 (nntp-open-connection nntp-server-buffer))))
1450 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1452 (nntp-report "Server closed connection"))
1453 ((not (equal group (caddr entry)))
1454 (with-current-buffer (process-buffer (car entry))
1456 (nntp-send-command "^[245].*\n" "GROUP" group)
1457 (setcar (cddr entry) group)
1459 (nntp-erase-buffer nntp-server-buffer)))))))
1461 (defun nntp-decode-text (&optional cr-only)
1462 "Decode the text in the current buffer."
1463 (goto-char (point-min))
1464 (while (search-forward "\r" nil t)
1467 ;; Remove trailing ".\n" end-of-transfer marker.
1468 (goto-char (point-max))
1470 (when (looking-at ".\n")
1472 ;; Delete status line.
1473 (goto-char (point-min))
1474 (while (looking-at "[1-5][0-9][0-9] .*\n")
1475 ;; For some unknown reason, there is more than one status line.
1476 (delete-region (point) (progn (forward-line 1) (point))))
1477 ;; Remove "." -> ".." encoding.
1478 (while (search-forward "\n.." nil t)
1481 (defun nntp-encode-text ()
1482 "Encode the text in the current buffer."
1484 ;; Replace "." at beginning of line with "..".
1485 (goto-char (point-min))
1486 (while (re-search-forward "^\\." nil t)
1488 (goto-char (point-max))
1489 ;; Insert newline at the end of the buffer.
1492 ;; Insert `.' at end of buffer (end of text mark).
1493 (goto-char (point-max))
1495 (goto-char (point-min))
1499 (insert nntp-end-of-line))))
1501 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1502 (set-buffer nntp-server-buffer)
1506 ;; This server does not talk NOV.
1507 ((not nntp-server-xover)
1510 ;; We don't care about gaps.
1511 ((or (not nntp-nov-gap)
1513 (nntp-send-xover-command
1515 (if (numberp fetch-old)
1516 (max 1 (- (car articles) fetch-old))
1519 (car (last articles)) 'wait)
1521 (goto-char (point-min))
1522 (when (looking-at "[1-5][0-9][0-9] .*\n")
1523 (delete-region (point) (progn (forward-line 1) (point))))
1524 (while (search-forward "\r" nil t)
1525 (replace-match "" t t))
1526 (goto-char (point-max))
1528 (when (looking-at "\\.")
1529 (delete-region (point) (progn (forward-line 1) (point)))))
1531 ;; We do it the hard way. For each gap, an XOVER command is sent
1532 ;; to the server. We do not wait for a reply from the server, we
1533 ;; just send them off as fast as we can. That means that we have
1534 ;; to count the number of responses we get back to find out when we
1535 ;; have gotten all we asked for.
1536 ((numberp nntp-nov-gap)
1541 (buf nntp-server-buffer)
1542 (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1544 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1545 ;; that means that the server does not understand XOVER, but we
1546 ;; won't know that until we try.
1547 (while (and nntp-server-xover articles)
1548 (setq first (car articles))
1549 ;; Search forward until we find a gap, or until we run out of
1551 (while (and (cdr articles)
1552 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1553 (setq articles (cdr articles)))
1555 (setq in-process-buffer-p (stringp nntp-server-xover))
1556 (nntp-send-xover-command first (setq last (car articles)))
1557 (setq articles (cdr articles))
1559 (when (and nntp-server-xover in-process-buffer-p)
1560 ;; Don't count tried request.
1561 (setq count (1+ count))
1563 ;; Every 400 requests we have to read the stream in
1564 ;; order to avoid deadlocks.
1565 (when (or (null articles) ;All requests have been sent.
1566 (= 1 (% count nntp-maximum-request)))
1568 (nntp-accept-response)
1569 ;; On some Emacs versions the preceding function has a
1570 ;; tendency to change the buffer. Perhaps. It's quite
1571 ;; difficult to reproduce, because it only seems to happen
1572 ;; once in a blue moon.
1573 (set-buffer process-buffer)
1575 (goto-char (or last-point (point-min)))
1577 (while (re-search-forward "^\\([0-9][0-9][0-9]\\) .*\n"
1580 (setq status (match-string 1))
1581 (if (string-match "^[45]" status)
1582 (setq status 'error)
1584 (setq last-point (point))
1585 (or (< received count)
1586 (if (eq status 'error)
1588 ;; I haven't started reading the final response
1590 (goto-char (point-max))
1592 (not (looking-at "^\\.\r?\n"))))))
1593 ;; I haven't read the end of the final response
1594 (nntp-accept-response)
1595 (set-buffer process-buffer))))
1597 ;; Some nntp servers seem to have an extension to the XOVER
1598 ;; extension. On these servers, requesting an article range
1599 ;; preceding the active range does not return an error as
1600 ;; specified in the RFC. What we instead get is the NOV entry
1601 ;; for the first available article. Obviously, a client can
1602 ;; use that entry to avoid making unnecessary requests. The
1603 ;; only problem is for a client that assumes that the response
1604 ;; will always be within the requested range. For such a
1605 ;; client, we can get N copies of the same entry (one for each
1606 ;; XOVER command sent to the server).
1609 (goto-char (point-min))
1610 (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1611 (let ((low-limit (string-to-number
1612 (buffer-substring (match-beginning 1)
1614 (while (and articles (<= (car articles) low-limit))
1615 (setq articles (cdr articles))))))
1618 (when nntp-server-xover
1619 (when in-process-buffer-p
1621 (goto-char (point-max))
1622 (nnheader-insert-buffer-substring process-buffer)
1623 (set-buffer process-buffer)
1627 ;; We remove any "." lines and status lines.
1628 (goto-char (point-min))
1629 (while (search-forward "\r" nil t)
1631 (goto-char (point-min))
1632 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1637 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1638 "Send the XOVER command to the server."
1639 (let ((range (format "%d-%d" beg end))
1640 (nntp-inhibit-erase t))
1641 (if (stringp nntp-server-xover)
1642 ;; If `nntp-server-xover' is a string, then we just send this
1645 (nntp-send-command-nodelete
1646 "\r?\n\\.\r?\n" nntp-server-xover range)
1647 ;; We do not wait for the reply.
1648 (nntp-send-command-nodelete nil nntp-server-xover range))
1649 (let ((commands nntp-xover-commands))
1650 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1651 ;; We try them all until we get at positive response.
1652 (while (and commands (eq nntp-server-xover 'try))
1653 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1654 (with-current-buffer nntp-server-buffer
1655 (goto-char (point-min))
1656 (and (looking-at "[23]") ; No error message.
1657 ;; We also have to look at the lines. Some buggy
1658 ;; servers give back simple lines with just the
1659 ;; article number. How... helpful.
1662 ;; More text after number, or a dot.
1663 (looking-at "[0-9]+\t...\\|\\.\r?\n"))
1664 (setq nntp-server-xover (car commands))))
1665 (setq commands (cdr commands)))
1666 ;; If none of the commands worked, we disable XOVER.
1667 (when (eq nntp-server-xover 'try)
1668 (nntp-erase-buffer nntp-server-buffer)
1669 (setq nntp-server-xover nil))
1670 nntp-server-xover))))
1672 (defun nntp-find-group-and-number (&optional group)
1675 ;; FIXME: This is REALLY FISHY: set-buffer after save-restriction?!?
1676 (set-buffer nntp-server-buffer)
1677 (narrow-to-region (goto-char (point-min))
1678 (or (search-forward "\n\n" nil t) (point-max)))
1679 (goto-char (point-min))
1680 ;; We first find the number by looking at the status line.
1681 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1683 (buffer-substring (match-beginning 1)
1686 (and number (zerop number) (setq number nil))
1688 ;; Then we find the group name.
1691 ;; If there is only one group in the Newsgroups
1692 ;; header, then it seems quite likely that this
1693 ;; article comes from that group, I'd say.
1694 ((and (setq newsgroups
1695 (mail-fetch-field "newsgroups"))
1696 (not (string-match "," newsgroups)))
1698 ;; If there is more than one group in the
1699 ;; Newsgroups header, then the Xref header should
1700 ;; be filled out. We hazard a guess that the group
1701 ;; that has this article number in the Xref header
1702 ;; is the one we are looking for. This might very
1703 ;; well be wrong if this article happens to have
1704 ;; the same number in several groups, but that's
1706 ((and (setq xref (mail-fetch-field "xref"))
1709 (format "\\([^ :]+\\):%d" number) xref))
1710 (match-string 1 xref))
1713 ((and (not nntp-xref-number-is-evil)
1714 (setq xref (mail-fetch-field "xref"))
1717 (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1718 "\\([^ :]+\\):\\([0-9]+\\)")
1720 (setq group (match-string 1 xref)
1721 number (string-to-number (match-string 2 xref))))
1722 ((and (setq newsgroups
1723 (mail-fetch-field "newsgroups"))
1724 (not (string-match "," newsgroups)))
1725 (setq group newsgroups))
1727 (t (setq group ""))))
1728 (when (string-match "\r" group)
1729 (setq group (substring group 0 (match-beginning 0))))
1730 (cons group number)))))
1732 (defun nntp-wait-for-string (regexp)
1733 "Wait until string arrives in the buffer."
1734 (let ((buf (current-buffer))
1736 (goto-char (point-min))
1737 (while (and (setq proc (get-buffer-process buf))
1738 (memq (process-status proc) '(open run))
1739 (not (re-search-forward regexp nil t)))
1740 (accept-process-output proc 0.1)
1742 (goto-char (point-min)))))
1745 ;; ==========================================================================
1746 ;; Obsolete nntp-open-* connection methods -- drv
1747 ;; ==========================================================================
1749 (defvoo nntp-open-telnet-envuser nil
1750 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1752 (defvoo nntp-telnet-shell-prompt "bash\\|[$>] *\r?$"
1753 "*Regular expression to match the shell prompt on the remote machine.")
1755 (defvoo nntp-rlogin-program "rsh"
1756 "*Program used to log in on remote machines.
1757 The default is \"rsh\", but \"ssh\" is a popular alternative.")
1759 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1760 "*Parameters to `nntp-open-rlogin'.
1761 That function may be used as `nntp-open-connection-function'. In that
1762 case, this list will be used as the parameter list given to rsh.")
1764 (defvoo nntp-rlogin-user-name nil
1765 "*User name on remote system when using the rlogin connect method.")
1767 (defvoo nntp-telnet-parameters
1768 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1769 "*Parameters to `nntp-open-telnet'.
1770 That function may be used as `nntp-open-connection-function'. In that
1771 case, this list will be executed as a command after logging in
1774 (defvoo nntp-telnet-user-name nil
1775 "User name to log in via telnet with.")
1777 (defvoo nntp-telnet-passwd nil
1778 "Password to use to log in via telnet with.")
1780 (defun nntp-service-to-port (svc)
1782 ((integerp svc) (number-to-string svc))
1783 ((string-match "\\`[0-9]+\\'" svc) svc)
1786 (ignore-errors (insert-file-contents "/etc/services"))
1787 (goto-char (point-min))
1788 (if (re-search-forward (concat "^" (regexp-quote svc)
1789 "[ \t]+\\([0-9]+\\)/tcp"))
1793 (defun nntp-open-telnet (buffer)
1794 (with-current-buffer buffer
1798 "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1799 (case-fold-search t))
1800 (when (memq (process-status proc) '(open run))
1801 (nntp-wait-for-string "^r?telnet")
1802 (process-send-string proc "set escape \^X\n")
1804 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1805 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1806 nntp-address "\n")))
1808 (process-send-string proc (concat "open " nntp-address "\n"))))
1810 ((not nntp-open-telnet-envuser)
1811 (nntp-wait-for-string "^\r*.?login:")
1812 (process-send-string
1814 (or nntp-telnet-user-name
1815 (setq nntp-telnet-user-name (read-string "login: ")))
1817 (nntp-wait-for-string "^\r*.?password:")
1818 (process-send-string
1820 (or nntp-telnet-passwd
1821 (setq nntp-telnet-passwd
1822 (read-passwd "Password: ")))
1824 (nntp-wait-for-string nntp-telnet-shell-prompt)
1825 (process-send-string
1826 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1827 (nntp-wait-for-string "^\r*20[01]")
1829 (delete-region (point-min) (point))
1830 (process-send-string proc "\^]")
1831 (nntp-wait-for-string "^r?telnet")
1832 (process-send-string proc "mode character\n")
1833 (accept-process-output proc 1)
1835 (goto-char (point-min))
1837 (delete-region (point) (point-max)))
1840 (defun nntp-open-rlogin (buffer)
1841 "Open a connection to SERVER using rsh."
1842 (let ((proc (if nntp-rlogin-user-name
1843 (apply 'start-process
1844 "nntpd" buffer nntp-rlogin-program
1845 nntp-address "-l" nntp-rlogin-user-name
1846 nntp-rlogin-parameters)
1847 (apply 'start-process
1848 "nntpd" buffer nntp-rlogin-program nntp-address
1849 nntp-rlogin-parameters))))
1850 (with-current-buffer buffer
1851 (nntp-wait-for-string "^\r*20[01]")
1853 (delete-region (point-min) (point))
1857 ;; ==========================================================================
1858 ;; Replacements for the nntp-open-* functions -- drv
1859 ;; ==========================================================================
1861 (defun nntp-open-telnet-stream (buffer)
1862 "Open a nntp connection by telnet'ing the news server.
1863 `nntp-open-netcat-stream' is recommended in place of this function
1864 because it is more reliable.
1866 Please refer to the following variables to customize the connection:
1867 - `nntp-pre-command',
1868 - `nntp-telnet-command',
1869 - `nntp-telnet-switches',
1871 - `nntp-port-number',
1872 - `nntp-end-of-line'."
1873 (let ((command `(,nntp-telnet-command
1874 ,@nntp-telnet-switches
1876 ,(nntp-service-to-port nntp-port-number)))
1878 (and nntp-pre-command
1879 (push nntp-pre-command command))
1880 (setq proc (apply 'start-process "nntpd" buffer command))
1881 (with-current-buffer buffer
1882 (nntp-wait-for-string "^\r*20[01]")
1884 (delete-region (point-min) (point))
1887 (defun nntp-open-via-rlogin-and-telnet (buffer)
1888 "Open a connection to an nntp server through an intermediate host.
1889 First rlogin to the remote host, and then telnet the real news server
1891 `nntp-open-via-rlogin-and-netcat' is recommended in place of this function
1892 because it is more reliable.
1894 Please refer to the following variables to customize the connection:
1895 - `nntp-pre-command',
1896 - `nntp-via-rlogin-command',
1897 - `nntp-via-rlogin-command-switches',
1898 - `nntp-via-user-name',
1899 - `nntp-via-address',
1900 - `nntp-telnet-command',
1901 - `nntp-telnet-switches',
1903 - `nntp-port-number',
1904 - `nntp-end-of-line'."
1905 (let ((command `(,nntp-via-address
1906 ,nntp-telnet-command
1907 ,@nntp-telnet-switches))
1909 (when nntp-via-user-name
1910 (setq command `("-l" ,nntp-via-user-name ,@command)))
1911 (when nntp-via-rlogin-command-switches
1912 (setq command (append nntp-via-rlogin-command-switches command)))
1913 (push nntp-via-rlogin-command command)
1914 (and nntp-pre-command
1915 (push nntp-pre-command command))
1916 (setq proc (apply 'start-process "nntpd" buffer command))
1917 (with-current-buffer buffer
1918 (nntp-wait-for-string "^r?telnet")
1919 (process-send-string proc (concat "open " nntp-address " "
1920 (nntp-service-to-port nntp-port-number)
1922 (nntp-wait-for-string "^\r*20[01]")
1924 (delete-region (point-min) (point))
1925 (process-send-string proc "\^]")
1926 (nntp-wait-for-string "^r?telnet")
1927 (process-send-string proc "mode character\n")
1928 (accept-process-output proc 1)
1930 (goto-char (point-min))
1932 (delete-region (point) (point-max)))
1935 (defun nntp-open-via-rlogin-and-netcat (buffer)
1936 "Open a connection to an nntp server through an intermediate host.
1937 First rlogin to the remote host, and then connect to the real news
1938 server from there using the netcat command.
1940 Please refer to the following variables to customize the connection:
1941 - `nntp-pre-command',
1942 - `nntp-via-rlogin-command',
1943 - `nntp-via-rlogin-command-switches',
1944 - `nntp-via-user-name',
1945 - `nntp-via-address',
1946 - `nntp-netcat-command',
1947 - `nntp-netcat-switches',
1949 - `nntp-port-number'."
1950 (let ((command `(,@(when nntp-pre-command
1951 (list nntp-pre-command))
1952 ,nntp-via-rlogin-command
1953 ,@nntp-via-rlogin-command-switches
1954 ,@(when nntp-via-user-name
1955 (list "-l" nntp-via-user-name))
1957 ,nntp-netcat-command
1958 ,@nntp-netcat-switches
1960 ,(nntp-service-to-port nntp-port-number))))
1961 ;; A non-nil connection type results in mightily odd behavior where
1962 ;; (process-send-string proc "\^M") ends up sending a "\n" to the
1963 ;; ssh process. --Stef
1964 ;; Also a nil connection allow ssh-askpass to work under X11.
1965 (let ((process-connection-type nil))
1966 (apply 'start-process "nntpd" buffer command))))
1968 (defun nntp-open-netcat-stream (buffer)
1969 "Open a connection to an nntp server through netcat.
1970 I.e. use the `nc' command rather than Emacs's builtin networking code.
1972 Please refer to the following variables to customize the connection:
1973 - `nntp-pre-command',
1974 - `nntp-netcat-command',
1975 - `nntp-netcat-switches',
1977 - `nntp-port-number'."
1978 (let ((command `(,nntp-netcat-command
1979 ,@nntp-netcat-switches
1981 ,(nntp-service-to-port nntp-port-number))))
1982 (and nntp-pre-command (push nntp-pre-command command))
1983 (let ((process-connection-type nil)) ;See `nntp-open-via-rlogin-and-netcat'.
1984 (apply 'start-process "nntpd" buffer command))))
1987 (defun nntp-open-via-telnet-and-telnet (buffer)
1988 "Open a connection to an nntp server through an intermediate host.
1989 First telnet the remote host, and then telnet the real news server
1992 Please refer to the following variables to customize the connection:
1993 - `nntp-pre-command',
1994 - `nntp-via-telnet-command',
1995 - `nntp-via-telnet-switches',
1996 - `nntp-via-address',
1997 - `nntp-via-envuser',
1998 - `nntp-via-user-name',
1999 - `nntp-via-user-password',
2000 - `nntp-via-shell-prompt',
2001 - `nntp-telnet-command',
2002 - `nntp-telnet-switches',
2004 - `nntp-port-number',
2005 - `nntp-end-of-line'."
2006 (with-current-buffer buffer
2008 (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
2009 (case-fold-search t)
2011 (and nntp-pre-command (push nntp-pre-command command))
2012 (setq proc (apply 'start-process "nntpd" buffer command))
2013 (when (memq (process-status proc) '(open run))
2014 (nntp-wait-for-string "^r?telnet")
2015 (process-send-string proc "set escape \^X\n")
2017 ((and nntp-via-envuser nntp-via-user-name)
2018 (process-send-string proc (concat "open " "-l" nntp-via-user-name
2019 nntp-via-address "\n")))
2021 (process-send-string proc (concat "open " nntp-via-address
2023 (when (not nntp-via-envuser)
2024 (nntp-wait-for-string "^\r*.?login:")
2025 (process-send-string proc
2027 (or nntp-via-user-name
2028 (setq nntp-via-user-name
2029 (read-string "login: ")))
2031 (nntp-wait-for-string "^\r*.?password:")
2032 (process-send-string proc
2034 (or nntp-via-user-password
2035 (setq nntp-via-user-password
2036 (read-passwd "Password: ")))
2038 (nntp-wait-for-string nntp-via-shell-prompt)
2039 (let ((real-telnet-command `("exec"
2040 ,nntp-telnet-command
2041 ,@nntp-telnet-switches
2043 ,(nntp-service-to-port nntp-port-number))))
2044 (process-send-string proc
2045 (concat (mapconcat 'identity
2046 real-telnet-command " ")
2048 (nntp-wait-for-string "^\r*20[01]")
2050 (delete-region (point-min) (point))
2051 (process-send-string proc "\^]")
2052 (nntp-wait-for-string "^r?telnet")
2053 (process-send-string proc "mode character\n")
2054 (accept-process-output proc 1)
2056 (goto-char (point-min))
2058 (delete-region (point) (point-max)))
2063 ;;; nntp.el ends here