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, 2004, 2005, 2006, 2007, 2008,
5 ;; 2009, 2010 Free Software Foundation, Inc.
7 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.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 by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; For Emacs <22.2 and XEmacs.
31 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
37 (require 'proto-stream)
38 (require 'gnus-group) ;; gnus-group-name-charset
42 (eval-when-compile (require 'cl))
44 (autoload 'auth-source-user-or-password "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
77 You probably don't want to do that, though.")
79 (defvoo nntp-open-connection-function 'nntp-open-network-stream
80 "*Function used for connecting to a remote system.
81 It will be called with the buffer to output in as argument.
83 Currently, five such functions are provided (please refer to their
84 respective doc string for more information), three of them establishing
85 direct connections to the nntp server, and two of them using an indirect
89 - `nntp-open-network-stream' (the default),
90 - `nntp-open-ssl-stream',
91 - `nntp-open-tls-stream',
92 - `nntp-open-netcat-stream'.
93 - `nntp-open-telnet-stream'.
96 - `nntp-open-via-rlogin-and-netcat',
97 - `nntp-open-via-rlogin-and-telnet',
98 - `nntp-open-via-telnet-and-telnet'.")
100 (defvoo nntp-never-echoes-commands nil
101 "*Non-nil means the nntp server never echoes commands.
102 It is reported that some nntps server doesn't echo commands. So, you
103 may want to set this to non-nil in the method for such a server setting
104 `nntp-open-connection-function' to `nntp-open-ssl-stream' for example.
105 Note that the `nntp-open-connection-functions-never-echo-commands'
106 variable overrides the nil value of this variable.")
108 (defvoo nntp-open-connection-functions-never-echo-commands
109 '(nntp-open-network-stream)
110 "*List of functions that never echo commands.
111 Add or set a function which you set to `nntp-open-connection-function'
112 to this list if it does not echo commands. Note that a non-nil value
113 of the `nntp-never-echoes-commands' variable overrides this variable.")
115 (defvoo nntp-pre-command nil
116 "*Pre-command to use with the various nntp-open-via-* methods.
117 This is where you would put \"runsocks\" or stuff like that.")
119 (defvoo nntp-telnet-command "telnet"
120 "*Telnet command used to connect to the nntp server.
121 This command is used by the methods `nntp-open-telnet-stream',
122 `nntp-open-via-rlogin-and-telnet' and `nntp-open-via-telnet-and-telnet'.")
124 (defvoo nntp-telnet-switches '("-8")
125 "*Switches given to the telnet command `nntp-telnet-command'.")
127 (defvoo nntp-end-of-line "\r\n"
128 "*String to use on the end of lines when talking to the NNTP server.
129 This is \"\\r\\n\" by default, but should be \"\\n\" when using an indirect
130 connection method (nntp-open-via-*).")
132 (defvoo nntp-via-rlogin-command "rsh"
133 "*Rlogin command used to connect to an intermediate host.
134 This command is used by the methods `nntp-open-via-rlogin-and-telnet'
135 and `nntp-open-via-rlogin-and-netcat'. The default is \"rsh\", but \"ssh\"
136 is a popular alternative.")
138 (defvoo nntp-via-rlogin-command-switches nil
139 "*Switches given to the rlogin command `nntp-via-rlogin-command'.
140 If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
141 \(\"-C\") in order to compress all data connections, otherwise set this
142 to \(\"-t\" \"-e\" \"none\") or (\"-C\" \"-t\" \"-e\" \"none\") if the telnet
143 command requires a pseudo-tty allocation on an intermediate host.")
145 (defvoo nntp-via-telnet-command "telnet"
146 "*Telnet command used to connect to an intermediate host.
147 This command is used by the `nntp-open-via-telnet-and-telnet' method.")
149 (defvoo nntp-via-telnet-switches '("-8")
150 "*Switches given to the telnet command `nntp-via-telnet-command'.")
152 (defvoo nntp-netcat-command "nc"
153 "*Netcat command used to connect to the nntp server.
154 This command is used by the `nntp-open-netcat-stream' and
155 `nntp-open-via-rlogin-and-netcat' methods.")
157 (defvoo nntp-netcat-switches nil
158 "*Switches given to the netcat command `nntp-netcat-command'.")
160 (defvoo nntp-via-user-name nil
161 "*User name to log in on an intermediate host with.
162 This variable is used by the various nntp-open-via-* methods.")
164 (defvoo nntp-via-user-password nil
165 "*Password to use to log in on an intermediate host with.
166 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
168 (defvoo nntp-via-address nil
169 "*Address of an intermediate host to connect to.
170 This variable is used by the various nntp-open-via-* methods.")
172 (defvoo nntp-via-envuser nil
173 "*Whether both telnet client and server support the ENVIRON option.
174 If non-nil, there will be no prompt for a login name.")
176 (defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
177 "*Regular expression to match the shell prompt on an intermediate host.
178 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
180 (defvoo nntp-large-newsgroup 50
181 "*The number of articles which indicates a large newsgroup.
182 If the number of articles is greater than the value, verbose
183 messages will be shown to indicate the current status.")
185 (defvoo nntp-maximum-request 400
186 "*The maximum number of the requests sent to the NNTP server at one time.
187 If Emacs hangs up while retrieving headers, set the variable to a
190 (defvoo nntp-nov-is-evil nil
191 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
193 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
194 "*List of strings that are used as commands to fetch NOV lines from a server.
195 The strings are tried in turn until a positive response is gotten. If
196 none of the commands are successful, nntp will just grab headers one
199 (defvoo nntp-nov-gap 5
200 "*Maximum allowed gap between two articles.
201 If the gap between two consecutive articles is bigger than this
202 variable, split the XOVER request into two requests.")
204 (defvoo nntp-xref-number-is-evil nil
205 "*If non-nil, Gnus never trusts article numbers in the Xref header.
206 Some news servers, e.g., ones running Diablo, run multiple engines
207 having the same articles but article numbers are not kept synchronized
208 between them. If you connect to such a server, set this to a non-nil
209 value, and Gnus never uses article numbers (that appear in the Xref
210 header and vary by which engine is chosen) to refer to articles.")
212 (defvoo nntp-prepare-server-hook nil
213 "*Hook run before a server is opened.
214 If can be used to set up a server remotely, for instance. Say you
215 have an account at the machine \"other.machine\". This machine has
216 access to an NNTP server that you can't access locally. You could
217 then use this hook to rsh to the remote machine and start a proxy NNTP
218 server there that you can connect to. See also
219 `nntp-open-connection-function'")
221 (defvoo nntp-coding-system-for-read 'binary
222 "*Coding system to read from NNTP.")
224 (defvoo nntp-coding-system-for-write 'binary
225 "*Coding system to write to NNTP.")
228 (defvoo nntp-marks-is-evil nil
229 "*If non-nil, Gnus will never generate and use marks file for nntp groups.
230 See `nnml-marks-is-evil' for more information.")
232 (defvoo nntp-marks-file-name ".marks")
233 (defvoo nntp-marks nil)
234 (defvar nntp-marks-modtime (gnus-make-hashtable))
236 (defcustom nntp-marks-directory
237 (nnheader-concat gnus-directory "marks/")
238 "*The directory where marks for nntp groups will be stored."
242 (defcustom nntp-authinfo-file "~/.authinfo"
243 ".netrc-like file that holds nntp authinfo passwords."
247 (repeat :tag "Entries"
250 :value ("" ("login" . "") ("password" . ""))
254 (const :format "" "login")
255 (string :format "Login: %v"))
257 (const :format "" "password")
258 (string :format "Password: %v")))))))
262 (defvoo nntp-connection-timeout nil
263 "*Number of seconds to wait before an nntp connection times out.
264 If this variable is nil, which is the default, no timers are set.
265 NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
267 (defvoo nntp-prepare-post-hook nil
268 "*Hook run just before posting an article. It is supposed to be used
269 to insert Cancel-Lock headers.")
271 (defvoo nntp-server-list-active-group 'try
272 "If nil, then always use GROUP instead of LIST ACTIVE.
273 This is usually slower, but on misconfigured servers that don't
274 update their active files often, this can help.")
276 ;;; Internal variables.
278 (defvar nntp-record-commands nil
279 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
281 (defvar nntp-have-messaged nil)
283 (defvar nntp-process-wait-for nil)
284 (defvar nntp-process-to-buffer nil)
285 (defvar nntp-process-callback nil)
286 (defvar nntp-process-decode nil)
287 (defvar nntp-process-start-point nil)
288 (defvar nntp-inside-change-function nil)
289 (defvoo nntp-last-command-time nil)
290 (defvoo nntp-last-command nil)
291 (defvoo nntp-authinfo-password nil)
292 (defvoo nntp-authinfo-user nil)
293 (defvoo nntp-authinfo-force nil)
295 (defvar nntp-connection-list nil)
297 (defvoo nntp-server-type nil)
298 (defvoo nntp-connection-alist nil)
299 (defvoo nntp-status-string "")
300 (defconst nntp-version "nntp 5.0")
301 (defvoo nntp-inhibit-erase nil)
302 (defvoo nntp-inhibit-output nil)
304 (defvoo nntp-server-xover 'try)
306 (defvar nntp-async-timer nil)
307 (defvar nntp-async-process-list nil)
309 (defvar nntp-authinfo-rejected nil
310 "A custom error condition used to report 'Authentication Rejected' errors.
311 Condition handlers that match just this condition ensure that the nntp
312 backend doesn't catch this error.")
313 (put 'nntp-authinfo-rejected 'error-conditions '(error nntp-authinfo-rejected))
314 (put 'nntp-authinfo-rejected 'error-message "Authorization Rejected")
318 ;;; Internal functions.
320 (defsubst nntp-send-string (process string)
321 "Send STRING to PROCESS."
322 ;; We need to store the time to provide timeouts, and
323 ;; to store the command so the we can replay the command
324 ;; if the server gives us an AUTHINFO challenge.
325 (setq nntp-last-command-time (current-time)
326 nntp-last-command string)
327 (when nntp-record-commands
328 (nntp-record-command string))
329 (process-send-string process (concat string nntp-end-of-line))
330 (or (memq (process-status process) '(open run))
331 (nntp-report "Server closed connection")))
333 (defun nntp-record-command (string)
334 "Record the command STRING."
335 (with-current-buffer (get-buffer-create "*nntp-log*")
336 (goto-char (point-max))
337 (let ((time (current-time)))
338 (insert (format-time-string "%Y%m%dT%H%M%S" time)
339 "." (format "%03d" (/ (nth 2 time) 1000))
340 " " nntp-address " " string "\n"))))
342 (defun nntp-report (&rest args)
343 "Report an error from the nntp backend. The first string in ARGS
344 can be a format string. For some commands, the failed command may be
345 retried once before actually displaying the error report."
347 (when nntp-record-commands
348 (nntp-record-command "*** CALLED nntp-report ***"))
350 (nnheader-report 'nntp args)
354 (defun nntp-report-1 (&rest args)
355 "Throws out to nntp-with-open-group-error so that the connection may
356 be restored and the command retried."
358 (when nntp-record-commands
359 (nntp-record-command "*** CONNECTION LOST ***"))
361 (throw 'nntp-with-open-group-error t))
363 (defmacro nntp-insert-buffer-substring (buffer &optional start end)
364 "Copy string from unibyte buffer to multibyte current buffer."
365 (if (featurep 'xemacs)
366 `(insert-buffer-substring ,buffer ,start ,end)
367 `(if enable-multibyte-characters
368 (insert (with-current-buffer ,buffer
369 (mm-string-to-multibyte
371 `(buffer-substring (or ,start (point-min))
372 (or ,end (point-max)))
374 (insert-buffer-substring ,buffer ,start ,end))))
376 (defmacro nntp-copy-to-buffer (buffer start end)
377 "Copy string from unibyte current buffer to multibyte buffer."
378 (if (featurep 'xemacs)
379 `(copy-to-buffer ,buffer ,start ,end)
380 `(let ((string (buffer-substring ,start ,end)))
381 (with-current-buffer ,buffer
383 (insert (if enable-multibyte-characters
384 (mm-string-to-multibyte string)
386 (goto-char (point-min))
389 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
390 "Wait for WAIT-FOR to arrive from PROCESS."
392 (with-current-buffer (process-buffer process)
393 (goto-char (point-min))
395 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
396 (looking-at "48[02]"))
397 (memq (process-status process) '(open run)))
398 (cond ((looking-at "480")
399 (nntp-handle-authinfo process))
401 (nnheader-report 'nntp (get 'nntp-authinfo-rejected 'error-message))
402 (signal 'nntp-authinfo-rejected nil))
403 ((looking-at "^.*\n")
404 (delete-region (point) (progn (forward-line 1) (point)))))
405 (nntp-accept-process-output process)
406 (goto-char (point-min)))
411 (nntp-snarf-error-message)
413 ((not (memq (process-status process) '(open run)))
414 (nntp-report "Server closed connection"))
416 (goto-char (point-max))
417 (let ((limit (point-min))
419 (while (not (re-search-backward wait-for limit t))
420 (nntp-accept-process-output process)
421 ;; We assume that whatever we wait for is less than 1000
423 (setq limit (max (- (point-max) 1000) (point-min)))
424 (goto-char (point-max)))
425 (setq response (match-string 0))
426 (with-current-buffer nntp-server-buffer
427 (setq nntp-process-response response)))
428 (nntp-decode-text (not decode))
430 (with-current-buffer buffer
431 (goto-char (point-max))
432 (nntp-insert-buffer-substring (process-buffer process))
433 ;; Nix out "nntp reading...." message.
434 (when nntp-have-messaged
435 (setq nntp-have-messaged nil)
436 (nnheader-message 5 ""))))
441 (defun nntp-kill-buffer (buffer)
442 (when (buffer-name buffer)
444 (nnheader-init-server-buffer)))
446 (defun nntp-erase-buffer (buffer)
447 "Erase contents of BUFFER."
448 (with-current-buffer buffer
451 (defsubst nntp-find-connection (buffer)
452 "Find the connection delivering to BUFFER."
453 (let ((alist nntp-connection-alist)
454 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
456 (while (and alist (setq entry (pop alist)))
457 (when (eq buffer (cadr entry))
458 (setq process (car entry)
461 (if (memq (process-status process) '(open run))
463 (nntp-kill-buffer (process-buffer process))
464 (setq nntp-connection-alist (delq entry nntp-connection-alist))
467 (defsubst nntp-find-connection-entry (buffer)
468 "Return the entry for the connection to BUFFER."
469 (assq (nntp-find-connection buffer) nntp-connection-alist))
471 (defun nntp-find-connection-buffer (buffer)
472 "Return the process connection buffer tied to BUFFER."
473 (let ((process (nntp-find-connection buffer)))
475 (process-buffer process))))
477 (defsubst nntp-retrieve-data (command address port buffer
478 &optional wait-for callback decode)
479 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
480 (let ((process (or (nntp-find-connection buffer)
481 (nntp-open-connection buffer))))
484 (unless (or nntp-inhibit-erase nnheader-callback-function)
485 (nntp-erase-buffer (process-buffer process)))
489 (nntp-send-string process command))
491 ((eq callback 'ignore)
493 ((and callback wait-for)
494 (nntp-async-wait process wait-for buffer decode callback)
497 (nntp-wait-for process wait-for buffer decode))
499 (nntp-authinfo-rejected
500 (signal 'nntp-authinfo-rejected (cdr err)))
502 (nnheader-report 'nntp "Couldn't open connection to %s: %s"
505 (message "Quit retrieving data from nntp")
508 (nnheader-report 'nntp "Couldn't open connection to %s" address))))
510 (defsubst nntp-send-command (wait-for &rest strings)
511 "Send STRINGS to server and wait until WAIT-FOR returns."
512 (when (and (not nnheader-callback-function)
513 (not nntp-inhibit-output))
514 (nntp-erase-buffer nntp-server-buffer))
515 (let* ((command (mapconcat 'identity strings " "))
516 (process (nntp-find-connection nntp-server-buffer))
517 (buffer (and process (process-buffer process)))
518 (pos (and buffer (with-current-buffer buffer (point)))))
521 (nntp-retrieve-data command
522 nntp-address nntp-port-number
524 wait-for nnheader-callback-function)
525 ;; If nothing to wait for, still remove possibly echo'ed commands.
526 ;; We don't have echoes if `nntp-never-echoes-commands' is non-nil
527 ;; or the value of `nntp-open-connection-function' is in
528 ;; `nntp-open-connection-functions-never-echo-commands', so we
529 ;; skip this in that cases.
531 nntp-never-echoes-commands
533 nntp-open-connection-function
534 nntp-open-connection-functions-never-echo-commands))
535 (nntp-accept-response)
536 (with-current-buffer buffer
538 (if (looking-at (regexp-quote command))
539 (delete-region pos (progn (forward-line 1)
541 (nnheader-report 'nntp "Couldn't open connection to %s."
544 (defun nntp-send-command-nodelete (wait-for &rest strings)
545 "Send STRINGS to server and wait until WAIT-FOR returns."
546 (let* ((command (mapconcat 'identity strings " "))
547 (process (nntp-find-connection nntp-server-buffer))
548 (buffer (and process (process-buffer process)))
549 (pos (and buffer (with-current-buffer buffer (point)))))
552 (nntp-retrieve-data command
553 nntp-address nntp-port-number
555 wait-for nnheader-callback-function)
556 ;; If nothing to wait for, still remove possibly echo'ed commands
558 (nntp-accept-response)
559 (with-current-buffer buffer
561 (if (looking-at (regexp-quote command))
562 (delete-region pos (progn (forward-line 1)
564 (nnheader-report 'nntp "Couldn't open connection to %s."
567 (defun nntp-send-command-and-decode (wait-for &rest strings)
568 "Send STRINGS to server and wait until WAIT-FOR returns."
569 (when (and (not nnheader-callback-function)
570 (not nntp-inhibit-output))
571 (nntp-erase-buffer nntp-server-buffer))
572 (let* ((command (mapconcat 'identity strings " "))
573 (process (nntp-find-connection nntp-server-buffer))
574 (buffer (and process (process-buffer process)))
575 (pos (and buffer (with-current-buffer buffer (point)))))
578 (nntp-retrieve-data command
579 nntp-address nntp-port-number
581 wait-for nnheader-callback-function t)
582 ;; If nothing to wait for, still remove possibly echo'ed commands
584 (nntp-accept-response)
585 (with-current-buffer buffer
587 (if (looking-at (regexp-quote command))
588 (delete-region pos (progn (forward-line 1) (point-at-bol))))
590 (nnheader-report 'nntp "Couldn't open connection to %s."
594 (defun nntp-send-buffer (wait-for)
595 "Send the current buffer to server and wait until WAIT-FOR returns."
596 (when (and (not nnheader-callback-function)
597 (not nntp-inhibit-output))
599 (nntp-find-connection-buffer nntp-server-buffer)))
601 ;; Make sure we did not forget to encode some of the content.
602 (assert (save-excursion (goto-char (point-min))
603 (not (re-search-forward "[^\000-\377]" nil t))))
604 (mm-disable-multibyte)
605 (process-send-region (nntp-find-connection nntp-server-buffer)
606 (point-min) (point-max))
608 nil nntp-address nntp-port-number nntp-server-buffer
609 wait-for nnheader-callback-function))
613 ;;; Interface functions.
615 (nnoo-define-basics nntp)
617 (defsubst nntp-next-result-arrived-p ()
619 ;; A result that starts with a 2xx code is terminated by
620 ;; a line with only a "." on it.
621 ((eq (char-after) ?2)
622 (if (re-search-forward "\n\\.\r?\n" nil t)
624 ;; Some broken news servers add another dot at the end.
625 ;; Protect against inflooping there.
626 (while (looking-at "^\\.\r?\n")
630 ;; A result that starts with a 3xx or 4xx code is terminated
633 (if (search-forward "\n" nil t)
641 (defvar nntp-with-open-group-internal nil)
642 (defvar nntp-report-n nil))
644 (defun nntp-with-open-group-function (-group -server -connectionless -bodyfun)
645 "Protect against servers that don't like clients that keep idle connections opens.
646 The problem being that these servers may either close a connection or
647 simply ignore any further requests on a connection. Closed
648 connections are not detected until `accept-process-output' has updated
649 the `process-status'. Dropped connections are not detected until the
650 connection timeouts (which may be several minutes) or
651 `nntp-connection-timeout' has expired. When these occur
652 `nntp-with-open-group', opens a new connection then re-issues the NNTP
653 command whose response triggered the error."
654 (letf ((nntp-report-n (symbol-function 'nntp-report))
655 ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1))
656 (nntp-with-open-group-internal nil))
657 (while (catch 'nntp-with-open-group-error
658 ;; Open the connection to the server
659 ;; NOTE: Existing connections are NOT tested.
660 (nntp-possibly-change-group -group -server -connectionless)
663 (and nntp-connection-timeout
665 nntp-connection-timeout nil
667 (let* ((-process (nntp-find-connection
669 (-buffer (and -process
670 (process-buffer -process))))
671 ;; When I an able to identify the
672 ;; connection to the server AND I've
673 ;; received NO reponse for
674 ;; nntp-connection-timeout seconds.
675 (when (and -buffer (eq 0 (buffer-size -buffer)))
676 ;; Close the connection. Take no
677 ;; other action as the accept input
678 ;; code will handle the closed
680 (nntp-kill-buffer -buffer))))))))
682 (setq nntp-with-open-group-internal
686 (unless debug-on-quit
688 (signal 'quit nil))))
690 (nnheader-cancel-timer -timer)))
692 (setf (symbol-function 'nntp-report) nntp-report-n))
693 nntp-with-open-group-internal))
695 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
696 "Protect against servers that don't like clients that keep idle connections opens.
697 The problem being that these servers may either close a connection or
698 simply ignore any further requests on a connection. Closed
699 connections are not detected until `accept-process-output' has updated
700 the `process-status'. Dropped connections are not detected until the
701 connection timeouts (which may be several minutes) or
702 `nntp-connection-timeout' has expired. When these occur
703 `nntp-with-open-group', opens a new connection then re-issues the NNTP
704 command whose response triggered the error."
705 (declare (indent 2) (debug (form form [&optional symbolp] def-body)))
706 (when (and (listp connectionless)
707 (not (eq connectionless nil)))
708 (setq forms (cons connectionless forms)
710 `(nntp-with-open-group-function ,group ,server ,connectionless (lambda () ,@forms)))
712 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
713 "Retrieve the headers of ARTICLES."
714 (nntp-with-open-group
716 (with-current-buffer (nntp-find-connection-buffer nntp-server-buffer)
718 (if (and (not gnus-nov-is-evil)
719 (not nntp-nov-is-evil)
720 (nntp-retrieve-headers-with-xover articles fetch-old))
721 ;; We successfully retrieved the headers via XOVER.
723 ;; XOVER didn't work, so we do it the hard, slow and inefficient
725 (let ((number (length articles))
729 (last-point (point-min))
730 (buf (nntp-find-connection-buffer nntp-server-buffer))
731 (nntp-inhibit-erase t)
733 ;; Send HEAD commands.
734 (while (setq article (pop articles))
737 "HEAD" (if (numberp article)
738 (int-to-string article)
739 ;; `articles' is either a list of article numbers
740 ;; or a list of article IDs.
743 ;; Every 400 requests we have to read the stream in
744 ;; order to avoid deadlocks.
745 (when (or (null articles) ;All requests have been sent.
746 (zerop (% count nntp-maximum-request)))
747 (nntp-accept-response)
750 (goto-char last-point)
752 (while (nntp-next-result-arrived-p)
753 (setq last-point (point))
756 ;; If number of headers is greater than 100, give
757 ;; informative messages.
758 (and (numberp nntp-large-newsgroup)
759 (> number nntp-large-newsgroup)
760 (zerop (% received 20))
761 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
762 (/ (* received 100) number)))
763 (nntp-accept-response))))
764 (and (numberp nntp-large-newsgroup)
765 (> number nntp-large-newsgroup)
766 (nnheader-message 6 "NNTP: Receiving headers...done"))
768 ;; Now all of replies are received. Fold continuation lines.
769 (nnheader-fold-continuation-lines)
770 ;; Remove all "\r"'s.
772 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
775 (deffoo nntp-retrieve-groups (groups &optional server)
776 "Retrieve group info on GROUPS."
777 (nntp-with-open-group
779 (when (nntp-find-connection-buffer nntp-server-buffer)
782 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
783 (nntp-erase-buffer nntp-server-buffer)
784 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
785 ;; The first time this is run, this variable is `try'. So we
787 (when (eq nntp-server-list-active-group 'try)
788 (nntp-try-list-active (car groups)))
793 (last-point (point-min))
794 (nntp-inhibit-erase t)
795 (buf (nntp-find-connection-buffer nntp-server-buffer))
796 (command (if nntp-server-list-active-group
797 "LIST ACTIVE" "GROUP")))
799 ;; Timeout may have killed the buffer.
800 (unless (gnus-buffer-live-p buf)
801 (nnheader-report 'nntp "Connection to %s is closed." server)
803 ;; Send the command to the server.
804 (nntp-send-command nil command (pop groups))
806 ;; Every 400 requests we have to read the stream in
807 ;; order to avoid deadlocks.
808 (when (or (null groups) ;All requests have been sent.
809 (zerop (% count nntp-maximum-request)))
810 (nntp-accept-response)
811 (while (and (gnus-buffer-live-p buf)
813 ;; Search `blue moon' in this file for the
814 ;; reason why set-buffer here.
816 (goto-char last-point)
818 (while (re-search-forward "^[0-9]" nil t)
820 (setq last-point (point))
822 (nntp-accept-response))))
824 ;; Wait for the reply from the final command.
825 (unless (gnus-buffer-live-p buf)
826 (nnheader-report 'nntp "Connection to %s is closed." server)
829 (goto-char (point-max))
830 (re-search-backward "^[0-9]" nil t)
831 (when (looking-at "^[23]")
832 (while (and (gnus-buffer-live-p buf)
835 (goto-char (point-max))
836 (if (not nntp-server-list-active-group)
837 (not (re-search-backward "\r?\n"
839 (not (re-search-backward "^\\.\r?\n"
841 (nntp-accept-response)))
843 ;; Now all replies are received. We remove CRs.
844 (unless (gnus-buffer-live-p buf)
845 (nnheader-report 'nntp "Connection to %s is closed." server)
848 (goto-char (point-min))
849 (while (search-forward "\r" nil t)
850 (replace-match "" t t))
852 (if (not nntp-server-list-active-group)
854 (nntp-copy-to-buffer nntp-server-buffer
855 (point-min) (point-max))
857 ;; We have read active entries, so we just delete the
859 (goto-char (point-min))
860 (while (re-search-forward "^[.2-5]" nil t)
861 (delete-region (match-beginning 0)
862 (progn (forward-line 1) (point))))
863 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
866 (deffoo nntp-retrieve-articles (articles &optional group server)
867 (nntp-with-open-group
870 (let ((number (length articles))
874 (last-point (point-min))
875 (buf (nntp-find-connection-buffer nntp-server-buffer))
876 (nntp-inhibit-erase t)
877 (map (apply 'vector articles))
882 ;; Send ARTICLE command.
883 (while (setq article (pop articles))
886 "ARTICLE" (if (numberp article)
887 (int-to-string article)
888 ;; `articles' is either a list of article numbers
889 ;; or a list of article IDs.
892 ;; Every 400 requests we have to read the stream in
893 ;; order to avoid deadlocks.
894 (when (or (null articles) ;All requests have been sent.
895 (zerop (% count nntp-maximum-request)))
896 (nntp-accept-response)
899 (goto-char last-point)
901 (while (nntp-next-result-arrived-p)
902 (aset map received (cons (aref map received) (point)))
903 (setq last-point (point))
906 ;; If number of headers is greater than 100, give
907 ;; informative messages.
908 (and (numberp nntp-large-newsgroup)
909 (> number nntp-large-newsgroup)
910 (zerop (% received 20))
911 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
912 (/ (* received 100) number)))
913 (nntp-accept-response))))
914 (and (numberp nntp-large-newsgroup)
915 (> number nntp-large-newsgroup)
916 (nnheader-message 6 "NNTP: Receiving articles...done"))
918 ;; Now we have all the responses. We go through the results,
919 ;; wash it and copy it over to the server buffer.
920 (set-buffer nntp-server-buffer)
922 (setq last-point (point-min))
926 (setq point (goto-char (point-max)))
928 (nntp-insert-buffer-substring buf last-point (cdr entry))
930 (setq last-point (cdr entry))
933 (cons (car entry) point))
936 (defun nntp-try-list-active (group)
937 (nntp-list-active-group group)
938 (with-current-buffer nntp-server-buffer
939 (goto-char (point-min))
941 (looking-at "5[0-9]+"))
942 (setq nntp-server-list-active-group nil))
944 (setq nntp-server-list-active-group t)))))
946 (deffoo nntp-list-active-group (group &optional server)
947 "Return the active info on GROUP (which can be a regexp)."
948 (nntp-with-open-group
950 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
952 (deffoo nntp-request-group-articles (group &optional server)
953 "Return the list of existing articles in GROUP."
954 (nntp-with-open-group
956 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
958 (deffoo nntp-request-article (article &optional group server buffer command)
959 (nntp-with-open-group
961 (when (nntp-send-command-and-decode
962 "\r?\n\\.\r?\n" "ARTICLE"
963 (if (numberp article) (int-to-string article) article))
965 (not (equal buffer nntp-server-buffer)))
966 (with-current-buffer nntp-server-buffer
967 (copy-to-buffer buffer (point-min) (point-max))
968 (nntp-find-group-and-number group))
969 (nntp-find-group-and-number group)))))
971 (deffoo nntp-request-head (article &optional group server)
972 (nntp-with-open-group
974 (when (nntp-send-command
975 "\r?\n\\.\r?\n" "HEAD"
976 (if (numberp article) (int-to-string article) article))
978 (nntp-find-group-and-number group)
979 (nntp-decode-text)))))
981 (deffoo nntp-request-body (article &optional group server)
982 (nntp-with-open-group
984 (nntp-send-command-and-decode
985 "\r?\n\\.\r?\n" "BODY"
986 (if (numberp article) (int-to-string article) article))))
988 (deffoo nntp-request-group (group &optional server dont-check info)
989 (nntp-with-open-group
991 (when (nntp-send-command "^[245].*\n" "GROUP" group)
992 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
993 (setcar (cddr entry) group)))))
995 (deffoo nntp-close-group (group &optional server)
998 (deffoo nntp-server-opened (&optional server)
999 "Say whether a connection to SERVER has been opened."
1000 (and (nnoo-current-server-p 'nntp server)
1002 (gnus-buffer-live-p nntp-server-buffer)
1003 (nntp-find-connection nntp-server-buffer)))
1005 (deffoo nntp-open-server (server &optional defs connectionless)
1006 (nnheader-init-server-buffer)
1007 (if (nntp-server-opened server)
1009 (when (or (stringp (car defs))
1010 (numberp (car defs)))
1011 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
1012 (unless (assq 'nntp-address defs)
1013 (setq defs (append defs (list (list 'nntp-address server)))))
1014 (nnoo-change-server 'nntp server defs)
1017 (or (nntp-find-connection nntp-server-buffer)
1018 (nntp-open-connection nntp-server-buffer)))))
1020 (deffoo nntp-close-server (&optional server)
1021 (nntp-possibly-change-group nil server t)
1022 (let ((process (nntp-find-connection nntp-server-buffer)))
1024 (when (memq (process-status process) '(open run))
1026 (nntp-send-string process "QUIT")
1027 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
1028 ;; Ok, this is evil, but when using telnet and stuff
1029 ;; as the connection method, it's important that the
1030 ;; QUIT command actually is sent out before we kill
1033 (nntp-kill-buffer (process-buffer process))
1034 (setq process (car (pop nntp-connection-alist))))
1035 (nnoo-close-server 'nntp)))
1037 (deffoo nntp-request-close ()
1039 (while (setq process (pop nntp-connection-list))
1040 (when (memq (process-status process) '(open run))
1042 (nntp-send-string process "QUIT")
1043 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
1044 ;; Ok, this is evil, but when using telnet and stuff
1045 ;; as the connection method, it's important that the
1046 ;; QUIT command actually is sent out before we kill
1049 (nntp-kill-buffer (process-buffer process)))))
1051 (deffoo nntp-request-list (&optional server)
1052 (nntp-with-open-group
1054 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
1056 (deffoo nntp-request-list-newsgroups (&optional server)
1057 (nntp-with-open-group
1059 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
1061 (deffoo nntp-request-newgroups (date &optional server)
1062 (nntp-with-open-group
1064 (with-current-buffer nntp-server-buffer
1065 (let* ((time (date-to-time date))
1066 (ls (- (cadr time) (nth 8 (decode-time time)))))
1068 (setcar time (1- (car time)))
1069 (setcar (cdr time) (+ ls 65536)))
1071 (setcar time (1+ (car time)))
1072 (setcar (cdr time) (- ls 65536)))
1074 (setcar (cdr time) ls)))
1077 "^\\.\r?\n" "NEWGROUPS"
1078 (format-time-string "%y%m%d %H%M%S" time)
1080 (nntp-decode-text))))))
1082 (deffoo nntp-request-post (&optional server)
1083 (nntp-with-open-group
1085 (when (nntp-send-command "^[23].*\r?\n" "POST")
1086 (let ((response (with-current-buffer nntp-server-buffer
1087 nntp-process-response))
1090 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
1092 (setq server-id (match-string 1 response))
1093 (narrow-to-region (goto-char (point-min))
1094 (if (search-forward "\n\n" nil t)
1097 (unless (mail-fetch-field "Message-ID")
1098 (goto-char (point-min))
1099 (insert "Message-ID: " server-id "\n"))
1101 (run-hooks 'nntp-prepare-post-hook)
1102 (nntp-send-buffer "^[23].*\n")))))
1104 (deffoo nntp-request-type (group article)
1107 (deffoo nntp-asynchronous-p ()
1110 (deffoo nntp-request-set-mark (group actions &optional server)
1111 (when (and (not nntp-marks-is-evil)
1112 nntp-marks-file-name)
1113 (nntp-possibly-create-directory group server)
1114 (nntp-open-marks group server)
1115 (setq nntp-marks (nnheader-update-marks-actions nntp-marks actions))
1116 (nntp-save-marks group server))
1119 (deffoo nntp-request-marks (group info &optional server)
1120 (when (and (not nntp-marks-is-evil)
1121 nntp-marks-file-name)
1122 (nntp-possibly-create-directory group server)
1123 (when (nntp-marks-changed-p group server)
1124 (nnheader-message 8 "Updating marks for %s..." group)
1125 (nntp-open-marks group server)
1126 ;; Update info using `nntp-marks'.
1127 (mapc (lambda (pred)
1128 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1129 (gnus-info-set-marks
1131 (gnus-update-alist-soft
1133 (cdr (assq (cdr pred) nntp-marks))
1134 (gnus-info-marks info))
1136 gnus-article-mark-lists)
1137 (let ((seen (cdr (assq 'read nntp-marks))))
1138 (gnus-info-set-read info
1139 (if (and (integerp (car seen))
1141 (list (cons (car seen) (car seen)))
1143 (nnheader-message 8 "Updating marks for %s...done" group)))
1148 ;;; Hooky functions.
1150 (defun nntp-send-mode-reader ()
1151 "Send the MODE READER command to the nntp server.
1152 This function is supposed to be called from `nntp-server-opened-hook'.
1153 It will make innd servers spawn an nnrpd process to allow actual article
1155 (nntp-send-command "^.*\n" "MODE READER"))
1157 (declare-function netrc-parse "netrc" (&optional file))
1158 (declare-function netrc-machine "netrc"
1159 (list machine &optional port defaultport))
1160 (declare-function netrc-get "netrc" (alist type))
1162 (defun nntp-send-authinfo (&optional send-if-force)
1163 "Send the AUTHINFO to the nntp server.
1164 It will look in the \"~/.authinfo\" file for matching entries. If
1165 nothing suitable is found there, it will prompt for a user name
1168 If SEND-IF-FORCE, only send authinfo to the server if the
1169 .authinfo file has the FORCE token."
1171 (let* ((list (netrc-parse nntp-authinfo-file))
1172 (alist (netrc-machine list nntp-address "nntp"))
1173 (force (or (netrc-get alist "force") nntp-authinfo-force))
1175 (auth-source-user-or-password '("login" "password") nntp-address "nntp"))
1176 (auth-user (nth 0 auth-info))
1177 (auth-passwd (nth 1 auth-info))
1179 ;; this is preferred to netrc-*
1181 (netrc-get alist "login")
1182 nntp-authinfo-user))
1184 ;; this is preferred to netrc-*
1186 (netrc-get alist "password"))))
1187 (when (or (not send-if-force)
1190 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1191 nntp-authinfo-user user))
1192 (unless (member user '(nil ""))
1193 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1194 (when t ;???Should check if AUTHINFO succeeded
1196 "^2.*\r?\n" "AUTHINFO PASS"
1198 nntp-authinfo-password
1199 (setq nntp-authinfo-password
1200 (read-passwd (format "NNTP (%s@%s) password: "
1201 user nntp-address))))))))))
1203 (defun nntp-send-nosy-authinfo ()
1204 "Send the AUTHINFO to the nntp server."
1205 (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
1206 (unless (member user '(nil ""))
1207 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1208 (when t ;???Should check if AUTHINFO succeeded
1209 (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
1210 (read-passwd (format "NNTP (%s@%s) password: "
1211 user nntp-address)))))))
1213 (defun nntp-send-authinfo-from-file ()
1214 "Send the AUTHINFO to the nntp server.
1216 The authinfo login name is taken from the user's login name and the
1217 password contained in '~/.nntp-authinfo'."
1218 (when (file-exists-p "~/.nntp-authinfo")
1220 (insert-file-contents "~/.nntp-authinfo")
1221 (goto-char (point-min))
1222 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
1224 "^2.*\r?\n" "AUTHINFO PASS"
1225 (buffer-substring (point) (point-at-eol))))))
1227 ;;; Internal functions.
1229 (defun nntp-handle-authinfo (process)
1230 "Take care of an authinfo response from the server."
1231 (let ((last nntp-last-command))
1232 (funcall nntp-authinfo-function)
1233 ;; We have to re-send the function that was interrupted by
1234 ;; the authinfo request.
1235 (nntp-erase-buffer nntp-server-buffer)
1236 (nntp-send-string process last)))
1238 (defun nntp-make-process-buffer (buffer)
1239 "Create a new, fresh buffer usable for nntp process connections."
1240 (with-current-buffer
1241 (generate-new-buffer
1242 (format " *server %s %s %s*"
1243 nntp-address nntp-port-number
1244 (gnus-buffer-exists-p buffer)))
1245 (mm-disable-multibyte)
1246 (set (make-local-variable 'after-change-functions) nil)
1247 (set (make-local-variable 'nntp-process-wait-for) nil)
1248 (set (make-local-variable 'nntp-process-callback) nil)
1249 (set (make-local-variable 'nntp-process-to-buffer) nil)
1250 (set (make-local-variable 'nntp-process-start-point) nil)
1251 (set (make-local-variable 'nntp-process-decode) nil)
1254 (defun nntp-open-connection (buffer)
1255 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1256 (run-hooks 'nntp-prepare-server-hook)
1257 (let* ((pbuffer (nntp-make-process-buffer buffer))
1259 (and nntp-connection-timeout
1261 nntp-connection-timeout nil
1263 (nntp-kill-buffer ,pbuffer)))))
1266 (let ((coding-system-for-read nntp-coding-system-for-read)
1267 (coding-system-for-write nntp-coding-system-for-write)
1268 (map '((nntp-open-network-stream network)
1269 (nntp-open-ssl-stream tls)
1270 (nntp-open-tls-stream tls))))
1271 (if (assoc nntp-open-connection-function map)
1272 (car (open-protocol-stream
1273 "nntpd" pbuffer nntp-address nntp-port-number
1275 (assoc nntp-open-connection-function map))
1276 :end-of-command "^\\([2345]\\|[.]\\).*\n"
1277 :capability-command "CAPABILITIES\r\n"
1280 (lambda (capabilities)
1281 (if (not (string-match "STARTTLS" capabilities))
1284 (funcall nntp-open-connection-function pbuffer)))
1286 (nnheader-report 'nntp "%s" err))
1288 (message "Quit opening connection to %s" nntp-address)
1289 (nntp-kill-buffer pbuffer)
1293 (nnheader-cancel-timer timer))
1295 (nntp-kill-buffer pbuffer))
1296 (when (and (buffer-name pbuffer)
1298 (gnus-set-process-query-on-exit-flag process nil)
1299 (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1300 (memq (process-status process) '(open run)))
1302 (caar (push (list process buffer nil) nntp-connection-alist))
1303 (push process nntp-connection-list)
1304 (with-current-buffer pbuffer
1305 (nntp-read-server-type)
1307 (set-buffer nntp-server-buffer)
1308 (let ((nnheader-callback-function nil))
1309 (run-hooks 'nntp-server-opened-hook)
1310 (nntp-send-authinfo t))))
1311 (nntp-kill-buffer (process-buffer process))
1314 (defun nntp-read-server-type ()
1315 "Find out what the name of the server we have connected to is."
1316 ;; Wait for the status string to arrive.
1317 (setq nntp-server-type (buffer-string))
1318 (let ((case-fold-search t))
1319 ;; Run server-specific commands.
1320 (dolist (entry nntp-server-action-alist)
1321 (when (string-match (car entry) nntp-server-type)
1322 (if (and (listp (cadr entry))
1323 (not (eq 'lambda (caadr entry))))
1325 (funcall (cadr entry)))))))
1327 (defun nntp-async-wait (process wait-for buffer decode callback)
1328 (with-current-buffer (process-buffer process)
1329 (unless nntp-inside-change-function
1331 (setq nntp-process-wait-for wait-for
1332 nntp-process-to-buffer buffer
1333 nntp-process-decode decode
1334 nntp-process-callback callback
1335 nntp-process-start-point (point-max))
1336 (setq after-change-functions '(nntp-after-change-function))))
1338 (defun nntp-async-timer-handler ()
1341 (if (memq (process-status proc) '(open run))
1342 (nntp-async-trigger proc)
1343 (nntp-async-stop proc)))
1344 nntp-async-process-list))
1346 (defun nntp-async-stop (proc)
1347 (setq nntp-async-process-list (delq proc nntp-async-process-list))
1348 (when (and nntp-async-timer (not nntp-async-process-list))
1349 (nnheader-cancel-timer nntp-async-timer)
1350 (setq nntp-async-timer nil)))
1352 (defun nntp-after-change-function (beg end len)
1354 ;; we only care about insertions at eob
1355 (when (and (eq 0 len) (eq (point-max) end))
1357 (let ((proc (get-buffer-process (current-buffer))))
1359 (nntp-async-trigger proc)))))
1360 ;; any throw from after-change-functions will leave it
1361 ;; set to nil. so we reset it here, if necessary.
1363 (setq after-change-functions '(nntp-after-change-function)))))
1365 (defun nntp-async-trigger (process)
1366 (with-current-buffer (process-buffer process)
1367 (when nntp-process-callback
1368 ;; do we have an error message?
1369 (goto-char nntp-process-start-point)
1370 (if (memq (following-char) '(?4 ?5))
1371 ;; wants credentials?
1372 (if (looking-at "480")
1373 (nntp-handle-authinfo process)
1374 ;; report error message.
1375 (nntp-snarf-error-message)
1376 (nntp-do-callback nil))
1378 ;; got what we expect?
1379 (goto-char (point-max))
1380 (when (re-search-backward
1381 nntp-process-wait-for nntp-process-start-point t)
1382 (let ((response (match-string 0)))
1383 (with-current-buffer nntp-server-buffer
1384 (setq nntp-process-response response)))
1385 (nntp-async-stop process)
1387 (when (gnus-buffer-exists-p nntp-process-to-buffer)
1388 (let ((buf (current-buffer))
1389 (start nntp-process-start-point)
1390 (decode nntp-process-decode))
1391 (with-current-buffer nntp-process-to-buffer
1392 (goto-char (point-max))
1394 (narrow-to-region (point) (point))
1395 (nntp-insert-buffer-substring buf start)
1397 (nntp-decode-text))))))
1399 (goto-char (point-max))
1401 (buffer-name (get-buffer nntp-process-to-buffer))))))))
1403 (defun nntp-do-callback (arg)
1404 (let ((callback nntp-process-callback)
1405 (nntp-inside-change-function t))
1406 (setq nntp-process-callback nil)
1407 (funcall callback arg)))
1409 (defun nntp-snarf-error-message ()
1410 "Save the error message in the current buffer."
1411 (let ((message (buffer-string)))
1412 (while (string-match "[\r\n]+" message)
1413 (setq message (replace-match " " t t message)))
1414 (nnheader-report 'nntp message)
1417 (defun nntp-accept-process-output (process)
1418 "Wait for output from PROCESS and message some dots."
1419 (with-current-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1421 (let ((len (/ (buffer-size) 1024))
1424 (setq nntp-have-messaged t)
1425 (nnheader-message 7 "nntp read: %dk" len)))
1427 (nnheader-accept-process-output process)
1428 ;; accept-process-output may update status of process to indicate
1429 ;; that the server has closed the connection. This MUST be
1430 ;; handled here as the buffer restored by the save-excursion may
1431 ;; be the process's former output buffer (i.e. now killed)
1433 (memq (process-status process) '(open run)))
1434 (nntp-report "Server closed connection")))))
1436 (defun nntp-accept-response ()
1437 "Wait for output from the process that outputs to BUFFER."
1438 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1440 (defun nntp-possibly-change-group (group server &optional connectionless)
1441 (let ((nnheader-callback-function nil))
1443 (or (nntp-server-opened server)
1444 (nntp-open-server server nil connectionless)))
1446 (unless connectionless
1447 (or (nntp-find-connection nntp-server-buffer)
1448 (nntp-open-connection nntp-server-buffer))))
1451 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1453 (nntp-report "Server closed connection"))
1454 ((not (equal group (caddr entry)))
1455 (with-current-buffer (process-buffer (car entry))
1457 (nntp-send-command "^[245].*\n" "GROUP" group)
1458 (setcar (cddr entry) group)
1460 (nntp-erase-buffer nntp-server-buffer)))))))
1462 (defun nntp-decode-text (&optional cr-only)
1463 "Decode the text in the current buffer."
1464 (goto-char (point-min))
1465 (while (search-forward "\r" nil t)
1468 ;; Remove trailing ".\n" end-of-transfer marker.
1469 (goto-char (point-max))
1471 (when (looking-at ".\n")
1473 ;; Delete status line.
1474 (goto-char (point-min))
1475 (while (looking-at "[1-5][0-9][0-9] .*\n")
1476 ;; For some unknown reason, there is more than one status line.
1477 (delete-region (point) (progn (forward-line 1) (point))))
1478 ;; Remove "." -> ".." encoding.
1479 (while (search-forward "\n.." nil t)
1482 (defun nntp-encode-text ()
1483 "Encode the text in the current buffer."
1485 ;; Replace "." at beginning of line with "..".
1486 (goto-char (point-min))
1487 (while (re-search-forward "^\\." nil t)
1489 (goto-char (point-max))
1490 ;; Insert newline at the end of the buffer.
1493 ;; Insert `.' at end of buffer (end of text mark).
1494 (goto-char (point-max))
1496 (goto-char (point-min))
1500 (insert nntp-end-of-line))))
1502 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1503 (set-buffer nntp-server-buffer)
1507 ;; This server does not talk NOV.
1508 ((not nntp-server-xover)
1511 ;; We don't care about gaps.
1512 ((or (not nntp-nov-gap)
1514 (nntp-send-xover-command
1516 (if (numberp fetch-old)
1517 (max 1 (- (car articles) fetch-old))
1520 (car (last articles)) 'wait)
1522 (goto-char (point-min))
1523 (when (looking-at "[1-5][0-9][0-9] .*\n")
1524 (delete-region (point) (progn (forward-line 1) (point))))
1525 (while (search-forward "\r" nil t)
1526 (replace-match "" t t))
1527 (goto-char (point-max))
1529 (when (looking-at "\\.")
1530 (delete-region (point) (progn (forward-line 1) (point)))))
1532 ;; We do it the hard way. For each gap, an XOVER command is sent
1533 ;; to the server. We do not wait for a reply from the server, we
1534 ;; just send them off as fast as we can. That means that we have
1535 ;; to count the number of responses we get back to find out when we
1536 ;; have gotten all we asked for.
1537 ((numberp nntp-nov-gap)
1542 (buf nntp-server-buffer)
1543 (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1545 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1546 ;; that means that the server does not understand XOVER, but we
1547 ;; won't know that until we try.
1548 (while (and nntp-server-xover articles)
1549 (setq first (car articles))
1550 ;; Search forward until we find a gap, or until we run out of
1552 (while (and (cdr articles)
1553 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1554 (setq articles (cdr articles)))
1556 (setq in-process-buffer-p (stringp nntp-server-xover))
1557 (nntp-send-xover-command first (setq last (car articles)))
1558 (setq articles (cdr articles))
1560 (when (and nntp-server-xover in-process-buffer-p)
1561 ;; Don't count tried request.
1562 (setq count (1+ count))
1564 ;; Every 400 requests we have to read the stream in
1565 ;; order to avoid deadlocks.
1566 (when (or (null articles) ;All requests have been sent.
1567 (= 1 (% count nntp-maximum-request)))
1569 (nntp-accept-response)
1570 ;; On some Emacs versions the preceding function has a
1571 ;; tendency to change the buffer. Perhaps. It's quite
1572 ;; difficult to reproduce, because it only seems to happen
1573 ;; once in a blue moon.
1574 (set-buffer process-buffer)
1576 (goto-char (or last-point (point-min)))
1578 (while (re-search-forward "^\\([0-9][0-9][0-9]\\) .*\n"
1581 (setq status (match-string 1))
1582 (if (string-match "^[45]" status)
1583 (setq status 'error)
1585 (setq last-point (point))
1586 (or (< received count)
1587 (if (eq status 'error)
1589 ;; I haven't started reading the final response
1591 (goto-char (point-max))
1593 (not (looking-at "^\\.\r?\n"))))))
1594 ;; I haven't read the end of the final response
1595 (nntp-accept-response)
1596 (set-buffer process-buffer))))
1598 ;; Some nntp servers seem to have an extension to the XOVER
1599 ;; extension. On these servers, requesting an article range
1600 ;; preceeding the active range does not return an error as
1601 ;; specified in the RFC. What we instead get is the NOV entry
1602 ;; for the first available article. Obviously, a client can
1603 ;; use that entry to avoid making unnecessary requests. The
1604 ;; only problem is for a client that assumes that the response
1605 ;; will always be within the requested ranage. For such a
1606 ;; client, we can get N copies of the same entry (one for each
1607 ;; XOVER command sent to the server).
1610 (goto-char (point-min))
1611 (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1612 (let ((low-limit (string-to-number
1613 (buffer-substring (match-beginning 1)
1615 (while (and articles (<= (car articles) low-limit))
1616 (setq articles (cdr articles))))))
1619 (when nntp-server-xover
1620 (when in-process-buffer-p
1622 (goto-char (point-max))
1623 (nntp-insert-buffer-substring process-buffer)
1624 (set-buffer process-buffer)
1628 ;; We remove any "." lines and status lines.
1629 (goto-char (point-min))
1630 (while (search-forward "\r" nil t)
1632 (goto-char (point-min))
1633 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1638 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1639 "Send the XOVER command to the server."
1640 (let ((range (format "%d-%d" beg end))
1641 (nntp-inhibit-erase t))
1642 (if (stringp nntp-server-xover)
1643 ;; If `nntp-server-xover' is a string, then we just send this
1646 (nntp-send-command-nodelete
1647 "\r?\n\\.\r?\n" nntp-server-xover range)
1648 ;; We do not wait for the reply.
1649 (nntp-send-command-nodelete nil nntp-server-xover range))
1650 (let ((commands nntp-xover-commands))
1651 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1652 ;; We try them all until we get at positive response.
1653 (while (and commands (eq nntp-server-xover 'try))
1654 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1655 (with-current-buffer nntp-server-buffer
1656 (goto-char (point-min))
1657 (and (looking-at "[23]") ; No error message.
1658 ;; We also have to look at the lines. Some buggy
1659 ;; servers give back simple lines with just the
1660 ;; article number. How... helpful.
1663 ;; More text after number, or a dot.
1664 (looking-at "[0-9]+\t...\\|\\.\r?\n"))
1665 (setq nntp-server-xover (car commands))))
1666 (setq commands (cdr commands)))
1667 ;; If none of the commands worked, we disable XOVER.
1668 (when (eq nntp-server-xover 'try)
1669 (nntp-erase-buffer nntp-server-buffer)
1670 (setq nntp-server-xover nil))
1671 nntp-server-xover))))
1673 (defun nntp-find-group-and-number (&optional group)
1676 ;; FIXME: This is REALLY FISHY: set-buffer after save-restriction?!?
1677 (set-buffer nntp-server-buffer)
1678 (narrow-to-region (goto-char (point-min))
1679 (or (search-forward "\n\n" nil t) (point-max)))
1680 (goto-char (point-min))
1681 ;; We first find the number by looking at the status line.
1682 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1684 (buffer-substring (match-beginning 1)
1687 (and number (zerop number) (setq number nil))
1689 ;; Then we find the group name.
1692 ;; If there is only one group in the Newsgroups
1693 ;; header, then it seems quite likely that this
1694 ;; article comes from that group, I'd say.
1695 ((and (setq newsgroups
1696 (mail-fetch-field "newsgroups"))
1697 (not (string-match "," newsgroups)))
1699 ;; If there is more than one group in the
1700 ;; Newsgroups header, then the Xref header should
1701 ;; be filled out. We hazard a guess that the group
1702 ;; that has this article number in the Xref header
1703 ;; is the one we are looking for. This might very
1704 ;; well be wrong if this article happens to have
1705 ;; the same number in several groups, but that's
1707 ((and (setq xref (mail-fetch-field "xref"))
1710 (format "\\([^ :]+\\):%d" number) xref))
1711 (match-string 1 xref))
1714 ((and (not nntp-xref-number-is-evil)
1715 (setq xref (mail-fetch-field "xref"))
1718 (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1719 "\\([^ :]+\\):\\([0-9]+\\)")
1721 (setq group (match-string 1 xref)
1722 number (string-to-number (match-string 2 xref))))
1723 ((and (setq newsgroups
1724 (mail-fetch-field "newsgroups"))
1725 (not (string-match "," newsgroups)))
1726 (setq group newsgroups))
1728 (t (setq group ""))))
1729 (when (string-match "\r" group)
1730 (setq group (substring group 0 (match-beginning 0))))
1731 (cons group number)))))
1733 (defun nntp-wait-for-string (regexp)
1734 "Wait until string arrives in the buffer."
1735 (let ((buf (current-buffer))
1737 (goto-char (point-min))
1738 (while (and (setq proc (get-buffer-process buf))
1739 (memq (process-status proc) '(open run))
1740 (not (re-search-forward regexp nil t)))
1741 (accept-process-output proc 0.1)
1743 (goto-char (point-min)))))
1746 ;; ==========================================================================
1747 ;; Obsolete nntp-open-* connection methods -- drv
1748 ;; ==========================================================================
1750 (defvoo nntp-open-telnet-envuser nil
1751 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1753 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
1754 "*Regular expression to match the shell prompt on the remote machine.")
1756 (defvoo nntp-rlogin-program "rsh"
1757 "*Program used to log in on remote machines.
1758 The default is \"rsh\", but \"ssh\" is a popular alternative.")
1760 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1761 "*Parameters to `nntp-open-rlogin'.
1762 That function may be used as `nntp-open-connection-function'. In that
1763 case, this list will be used as the parameter list given to rsh.")
1765 (defvoo nntp-rlogin-user-name nil
1766 "*User name on remote system when using the rlogin connect method.")
1768 (defvoo nntp-telnet-parameters
1769 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1770 "*Parameters to `nntp-open-telnet'.
1771 That function may be used as `nntp-open-connection-function'. In that
1772 case, this list will be executed as a command after logging in
1775 (defvoo nntp-telnet-user-name nil
1776 "User name to log in via telnet with.")
1778 (defvoo nntp-telnet-passwd nil
1779 "Password to use to log in via telnet with.")
1781 (defun nntp-service-to-port (svc)
1783 ((integerp svc) (number-to-string svc))
1784 ((string-match "\\`[0-9]+\\'" svc) svc)
1787 (ignore-errors (insert-file-contents "/etc/services"))
1788 (goto-char (point-min))
1789 (if (re-search-forward (concat "^" (regexp-quote svc)
1790 "[ \t]+\\([0-9]+\\)/tcp"))
1794 (defun nntp-open-telnet (buffer)
1795 (with-current-buffer buffer
1799 "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1800 (case-fold-search t))
1801 (when (memq (process-status proc) '(open run))
1802 (nntp-wait-for-string "^r?telnet")
1803 (process-send-string proc "set escape \^X\n")
1805 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1806 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1807 nntp-address "\n")))
1809 (process-send-string proc (concat "open " nntp-address "\n"))))
1811 ((not nntp-open-telnet-envuser)
1812 (nntp-wait-for-string "^\r*.?login:")
1813 (process-send-string
1815 (or nntp-telnet-user-name
1816 (setq nntp-telnet-user-name (read-string "login: ")))
1818 (nntp-wait-for-string "^\r*.?password:")
1819 (process-send-string
1821 (or nntp-telnet-passwd
1822 (setq nntp-telnet-passwd
1823 (read-passwd "Password: ")))
1825 (nntp-wait-for-string nntp-telnet-shell-prompt)
1826 (process-send-string
1827 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1828 (nntp-wait-for-string "^\r*20[01]")
1830 (delete-region (point-min) (point))
1831 (process-send-string proc "\^]")
1832 (nntp-wait-for-string "^r?telnet")
1833 (process-send-string proc "mode character\n")
1834 (accept-process-output proc 1)
1836 (goto-char (point-min))
1838 (delete-region (point) (point-max)))
1841 (defun nntp-open-rlogin (buffer)
1842 "Open a connection to SERVER using rsh."
1843 (let ((proc (if nntp-rlogin-user-name
1844 (apply 'start-process
1845 "nntpd" buffer nntp-rlogin-program
1846 nntp-address "-l" nntp-rlogin-user-name
1847 nntp-rlogin-parameters)
1848 (apply 'start-process
1849 "nntpd" buffer nntp-rlogin-program nntp-address
1850 nntp-rlogin-parameters))))
1851 (with-current-buffer buffer
1852 (nntp-wait-for-string "^\r*20[01]")
1854 (delete-region (point-min) (point))
1858 ;; ==========================================================================
1859 ;; Replacements for the nntp-open-* functions -- drv
1860 ;; ==========================================================================
1862 (defun nntp-open-telnet-stream (buffer)
1863 "Open a nntp connection by telnet'ing the news server.
1864 `nntp-open-netcat-stream' is recommended in place of this function
1865 because it is more reliable.
1867 Please refer to the following variables to customize the connection:
1868 - `nntp-pre-command',
1869 - `nntp-telnet-command',
1870 - `nntp-telnet-switches',
1872 - `nntp-port-number',
1873 - `nntp-end-of-line'."
1874 (let ((command `(,nntp-telnet-command
1875 ,@nntp-telnet-switches
1877 ,(nntp-service-to-port nntp-port-number)))
1879 (and nntp-pre-command
1880 (push nntp-pre-command command))
1881 (setq proc (apply 'start-process "nntpd" buffer command))
1882 (with-current-buffer buffer
1883 (nntp-wait-for-string "^\r*20[01]")
1885 (delete-region (point-min) (point))
1888 (defun nntp-open-via-rlogin-and-telnet (buffer)
1889 "Open a connection to an nntp server through an intermediate host.
1890 First rlogin to the remote host, and then telnet the real news server
1892 `nntp-open-via-rlogin-and-netcat' is recommended in place of this function
1893 because it is more reliable.
1895 Please refer to the following variables to customize the connection:
1896 - `nntp-pre-command',
1897 - `nntp-via-rlogin-command',
1898 - `nntp-via-rlogin-command-switches',
1899 - `nntp-via-user-name',
1900 - `nntp-via-address',
1901 - `nntp-telnet-command',
1902 - `nntp-telnet-switches',
1904 - `nntp-port-number',
1905 - `nntp-end-of-line'."
1906 (let ((command `(,nntp-via-address
1907 ,nntp-telnet-command
1908 ,@nntp-telnet-switches))
1910 (when nntp-via-user-name
1911 (setq command `("-l" ,nntp-via-user-name ,@command)))
1912 (when nntp-via-rlogin-command-switches
1913 (setq command (append nntp-via-rlogin-command-switches command)))
1914 (push nntp-via-rlogin-command command)
1915 (and nntp-pre-command
1916 (push nntp-pre-command command))
1917 (setq proc (apply 'start-process "nntpd" buffer command))
1918 (with-current-buffer buffer
1919 (nntp-wait-for-string "^r?telnet")
1920 (process-send-string proc (concat "open " nntp-address " "
1921 (nntp-service-to-port nntp-port-number)
1923 (nntp-wait-for-string "^\r*20[01]")
1925 (delete-region (point-min) (point))
1926 (process-send-string proc "\^]")
1927 (nntp-wait-for-string "^r?telnet")
1928 (process-send-string proc "mode character\n")
1929 (accept-process-output proc 1)
1931 (goto-char (point-min))
1933 (delete-region (point) (point-max)))
1936 (defun nntp-open-via-rlogin-and-netcat (buffer)
1937 "Open a connection to an nntp server through an intermediate host.
1938 First rlogin to the remote host, and then connect to the real news
1939 server from there using the netcat command.
1941 Please refer to the following variables to customize the connection:
1942 - `nntp-pre-command',
1943 - `nntp-via-rlogin-command',
1944 - `nntp-via-rlogin-command-switches',
1945 - `nntp-via-user-name',
1946 - `nntp-via-address',
1947 - `nntp-netcat-command',
1948 - `nntp-netcat-switches',
1950 - `nntp-port-number'."
1951 (let ((command `(,@(when nntp-pre-command
1952 (list nntp-pre-command))
1953 ,nntp-via-rlogin-command
1954 ,@nntp-via-rlogin-command-switches
1955 ,@(when nntp-via-user-name
1956 (list "-l" nntp-via-user-name))
1958 ,nntp-netcat-command
1959 ,@nntp-netcat-switches
1961 ,(nntp-service-to-port nntp-port-number))))
1962 ;; A non-nil connection type results in mightily odd behavior where
1963 ;; (process-send-string proc "\^M") ends up sending a "\n" to the
1964 ;; ssh process. --Stef
1965 ;; Also a nil connection allow ssh-askpass to work under X11.
1966 (let ((process-connection-type nil))
1967 (apply 'start-process "nntpd" buffer command))))
1969 (defun nntp-open-netcat-stream (buffer)
1970 "Open a connection to an nntp server through netcat.
1971 I.e. use the `nc' command rather than Emacs's builtin networking code.
1973 Please refer to the following variables to customize the connection:
1974 - `nntp-pre-command',
1975 - `nntp-netcat-command',
1976 - `nntp-netcat-switches',
1978 - `nntp-port-number'."
1979 (let ((command `(,nntp-netcat-command
1980 ,@nntp-netcat-switches
1982 ,(nntp-service-to-port nntp-port-number))))
1983 (and nntp-pre-command (push nntp-pre-command command))
1984 (let ((process-connection-type nil)) ;See `nntp-open-via-rlogin-and-netcat'.
1985 (apply 'start-process "nntpd" buffer command))))
1988 (defun nntp-open-via-telnet-and-telnet (buffer)
1989 "Open a connection to an nntp server through an intermediate host.
1990 First telnet the remote host, and then telnet the real news server
1993 Please refer to the following variables to customize the connection:
1994 - `nntp-pre-command',
1995 - `nntp-via-telnet-command',
1996 - `nntp-via-telnet-switches',
1997 - `nntp-via-address',
1998 - `nntp-via-envuser',
1999 - `nntp-via-user-name',
2000 - `nntp-via-user-password',
2001 - `nntp-via-shell-prompt',
2002 - `nntp-telnet-command',
2003 - `nntp-telnet-switches',
2005 - `nntp-port-number',
2006 - `nntp-end-of-line'."
2007 (with-current-buffer buffer
2009 (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
2010 (case-fold-search t)
2012 (and nntp-pre-command (push nntp-pre-command command))
2013 (setq proc (apply 'start-process "nntpd" buffer command))
2014 (when (memq (process-status proc) '(open run))
2015 (nntp-wait-for-string "^r?telnet")
2016 (process-send-string proc "set escape \^X\n")
2018 ((and nntp-via-envuser nntp-via-user-name)
2019 (process-send-string proc (concat "open " "-l" nntp-via-user-name
2020 nntp-via-address "\n")))
2022 (process-send-string proc (concat "open " nntp-via-address
2024 (when (not nntp-via-envuser)
2025 (nntp-wait-for-string "^\r*.?login:")
2026 (process-send-string proc
2028 (or nntp-via-user-name
2029 (setq nntp-via-user-name
2030 (read-string "login: ")))
2032 (nntp-wait-for-string "^\r*.?password:")
2033 (process-send-string proc
2035 (or nntp-via-user-password
2036 (setq nntp-via-user-password
2037 (read-passwd "Password: ")))
2039 (nntp-wait-for-string nntp-via-shell-prompt)
2040 (let ((real-telnet-command `("exec"
2041 ,nntp-telnet-command
2042 ,@nntp-telnet-switches
2044 ,(nntp-service-to-port nntp-port-number))))
2045 (process-send-string proc
2046 (concat (mapconcat 'identity
2047 real-telnet-command " ")
2049 (nntp-wait-for-string "^\r*20[01]")
2051 (delete-region (point-min) (point))
2052 (process-send-string proc "\^]")
2053 (nntp-wait-for-string "^r?telnet")
2054 (process-send-string proc "mode character\n")
2055 (accept-process-output proc 1)
2057 (goto-char (point-min))
2059 (delete-region (point) (point-max)))
2064 (defun nntp-marks-directory (server)
2065 (expand-file-name server nntp-marks-directory))
2067 (defvar nntp-server-to-method-cache nil
2068 "Alist of servers and select methods.")
2070 (defun nntp-group-pathname (server group &optional file)
2071 "Return an absolute file name of FILE for GROUP on SERVER."
2072 (let ((method (cdr (assoc server nntp-server-to-method-cache))))
2074 (push (cons server (setq method (or (gnus-server-to-method server)
2075 (gnus-find-method-for-group group))))
2076 nntp-server-to-method-cache))
2077 (nnmail-group-pathname
2078 (mm-decode-coding-string group
2079 (inline (gnus-group-name-charset method group)))
2080 (nntp-marks-directory server)
2083 (defun nntp-possibly-create-directory (group server)
2084 (let ((dir (nntp-group-pathname server group))
2085 (file-name-coding-system nnmail-pathname-coding-system))
2086 (unless (file-exists-p dir)
2087 (make-directory (directory-file-name dir) t)
2088 (nnheader-message 5 "Creating nntp marks directory %s" dir))))
2090 (autoload 'time-less-p "time-date")
2092 (defun nntp-marks-changed-p (group server)
2093 (let ((file (nntp-group-pathname server group nntp-marks-file-name))
2094 (file-name-coding-system nnmail-pathname-coding-system))
2095 (if (null (gnus-gethash file nntp-marks-modtime))
2096 t ;; never looked at marks file, assume it has changed
2097 (time-less-p (gnus-gethash file nntp-marks-modtime)
2098 (nth 5 (file-attributes file))))))
2100 (defun nntp-save-marks (group server)
2101 (let ((file-name-coding-system nnmail-pathname-coding-system)
2102 (file (nntp-group-pathname server group nntp-marks-file-name)))
2105 (nntp-possibly-create-directory group server)
2106 (with-temp-file file
2108 (gnus-prin1 nntp-marks)
2111 (nth 5 (file-attributes file))
2112 nntp-marks-modtime))
2113 (error (or (gnus-yes-or-no-p
2114 (format "Could not write to %s (%s). Continue? " file err))
2115 (error "Cannot write to %s (%s)" file err))))))
2117 (defun nntp-open-marks (group server)
2118 (let ((file (nntp-group-pathname server group nntp-marks-file-name))
2119 (file-name-coding-system nnmail-pathname-coding-system))
2120 (if (file-exists-p file)
2123 (gnus-sethash file (nth 5 (file-attributes file))
2125 (nnheader-insert-file-contents file)
2126 (setq nntp-marks (read (current-buffer)))
2127 (dolist (el gnus-article-unpropagated-mark-lists)
2128 (setq nntp-marks (gnus-remassoc el nntp-marks))))
2129 (error (or (gnus-yes-or-no-p
2130 (format "Error reading nntp marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
2131 (error "Cannot read nntp marks file %s (%s)" file err))))
2132 ;; User didn't have a .marks file. Probably first time
2133 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
2134 (let ((info (gnus-get-info
2135 (gnus-group-prefixed-name
2137 (gnus-server-to-method (format "nntp:%s" server)))))
2138 (decoded-name (mm-decode-coding-string
2140 (gnus-group-name-charset
2141 (gnus-server-to-method server) group))))
2142 (nnheader-message 7 "Bootstrapping marks for %s..." decoded-name)
2143 (setq nntp-marks (gnus-info-marks info))
2144 (push (cons 'read (gnus-info-read info)) nntp-marks)
2145 (dolist (el gnus-article-unpropagated-mark-lists)
2146 (setq nntp-marks (gnus-remassoc el nntp-marks)))
2147 (nntp-save-marks group server)
2148 (nnheader-message 7 "Bootstrapping marks for %s...done"
2153 ;;; nntp.el ends here