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 'gnus-group) ;; gnus-group-name-charset
41 (eval-when-compile (require 'cl))
43 (autoload 'auth-source-user-or-password "auth-source")
46 "NNTP access for Gnus."
49 (defvoo nntp-address nil
50 "Address of the physical nntp server.")
52 (defvoo nntp-port-number "nntp"
53 "Port number on the physical nntp server.")
55 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
56 "*Hook used for sending commands to the server at startup.
57 The default value is `nntp-send-mode-reader', which makes an innd
58 server spawn an nnrpd server.")
60 (defvoo nntp-authinfo-function 'nntp-send-authinfo
61 "Function used to send AUTHINFO to the server.
62 It is called with no parameters.")
64 (defvoo nntp-server-action-alist
65 '(("nntpd 1\\.5\\.11t"
66 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
67 ("NNRP server Netscape"
68 (setq nntp-server-list-active-group nil)))
69 "Alist of regexps to match on server types and actions to be taken.
70 For instance, if you want Gnus to beep every time you connect
71 to innd, you could say something like:
73 \(setq nntp-server-action-alist
76 You probably don't want to do that, though.")
78 (defvoo nntp-open-connection-function 'nntp-open-network-stream
79 "*Function used for connecting to a remote system.
80 It will be called with the buffer to output in as argument.
82 Currently, five such functions are provided (please refer to their
83 respective doc string for more information), three of them establishing
84 direct connections to the nntp server, and two of them using an indirect
88 - `nntp-open-network-stream' (the default),
89 - `nntp-open-ssl-stream',
90 - `nntp-open-tls-stream',
91 - `nntp-open-netcat-stream'.
92 - `nntp-open-telnet-stream'.
95 - `nntp-open-via-rlogin-and-netcat',
96 - `nntp-open-via-rlogin-and-telnet',
97 - `nntp-open-via-telnet-and-telnet'.")
99 (defvoo nntp-never-echoes-commands nil
100 "*Non-nil means the nntp server never echoes commands.
101 It is reported that some nntps server doesn't echo commands. So, you
102 may want to set this to non-nil in the method for such a server setting
103 `nntp-open-connection-function' to `nntp-open-ssl-stream' for example.
104 Note that the `nntp-open-connection-functions-never-echo-commands'
105 variable overrides the nil value of this variable.")
107 (defvoo nntp-open-connection-functions-never-echo-commands
108 '(nntp-open-network-stream)
109 "*List of functions that never echo commands.
110 Add or set a function which you set to `nntp-open-connection-function'
111 to this list if it does not echo commands. Note that a non-nil value
112 of the `nntp-never-echoes-commands' variable overrides this variable.")
114 (defvoo nntp-pre-command nil
115 "*Pre-command to use with the various nntp-open-via-* methods.
116 This is where you would put \"runsocks\" or stuff like that.")
118 (defvoo nntp-telnet-command "telnet"
119 "*Telnet command used to connect to the nntp server.
120 This command is used by the methods `nntp-open-telnet-stream',
121 `nntp-open-via-rlogin-and-telnet' and `nntp-open-via-telnet-and-telnet'.")
123 (defvoo nntp-telnet-switches '("-8")
124 "*Switches given to the telnet command `nntp-telnet-command'.")
126 (defvoo nntp-end-of-line "\r\n"
127 "*String to use on the end of lines when talking to the NNTP server.
128 This is \"\\r\\n\" by default, but should be \"\\n\" when using an indirect
129 connection method (nntp-open-via-*).")
131 (defvoo nntp-via-rlogin-command "rsh"
132 "*Rlogin command used to connect to an intermediate host.
133 This command is used by the methods `nntp-open-via-rlogin-and-telnet'
134 and `nntp-open-via-rlogin-and-netcat'. The default is \"rsh\", but \"ssh\"
135 is a popular alternative.")
137 (defvoo nntp-via-rlogin-command-switches nil
138 "*Switches given to the rlogin command `nntp-via-rlogin-command'.
139 If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
140 \(\"-C\") in order to compress all data connections, otherwise set this
141 to \(\"-t\" \"-e\" \"none\") or (\"-C\" \"-t\" \"-e\" \"none\") if the telnet
142 command requires a pseudo-tty allocation on an intermediate host.")
144 (defvoo nntp-via-telnet-command "telnet"
145 "*Telnet command used to connect to an intermediate host.
146 This command is used by the `nntp-open-via-telnet-and-telnet' method.")
148 (defvoo nntp-via-telnet-switches '("-8")
149 "*Switches given to the telnet command `nntp-via-telnet-command'.")
151 (defvoo nntp-netcat-command "nc"
152 "*Netcat command used to connect to the nntp server.
153 This command is used by the `nntp-open-netcat-stream' and
154 `nntp-open-via-rlogin-and-netcat' methods.")
156 (defvoo nntp-netcat-switches nil
157 "*Switches given to the netcat command `nntp-netcat-command'.")
159 (defvoo nntp-via-user-name nil
160 "*User name to log in on an intermediate host with.
161 This variable is used by the various nntp-open-via-* methods.")
163 (defvoo nntp-via-user-password nil
164 "*Password to use to log in on an intermediate host with.
165 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
167 (defvoo nntp-via-address nil
168 "*Address of an intermediate host to connect to.
169 This variable is used by the various nntp-open-via-* methods.")
171 (defvoo nntp-via-envuser nil
172 "*Whether both telnet client and server support the ENVIRON option.
173 If non-nil, there will be no prompt for a login name.")
175 (defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
176 "*Regular expression to match the shell prompt on an intermediate host.
177 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
179 (defvoo nntp-large-newsgroup 50
180 "*The number of articles which indicates a large newsgroup.
181 If the number of articles is greater than the value, verbose
182 messages will be shown to indicate the current status.")
184 (defvoo nntp-maximum-request 400
185 "*The maximum number of the requests sent to the NNTP server at one time.
186 If Emacs hangs up while retrieving headers, set the variable to a
189 (defvoo nntp-nov-is-evil nil
190 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
192 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
193 "*List of strings that are used as commands to fetch NOV lines from a server.
194 The strings are tried in turn until a positive response is gotten. If
195 none of the commands are successful, nntp will just grab headers one
198 (defvoo nntp-nov-gap 5
199 "*Maximum allowed gap between two articles.
200 If the gap between two consecutive articles is bigger than this
201 variable, split the XOVER request into two requests.")
203 (defvoo nntp-xref-number-is-evil nil
204 "*If non-nil, Gnus never trusts article numbers in the Xref header.
205 Some news servers, e.g., ones running Diablo, run multiple engines
206 having the same articles but article numbers are not kept synchronized
207 between them. If you connect to such a server, set this to a non-nil
208 value, and Gnus never uses article numbers (that appear in the Xref
209 header and vary by which engine is chosen) to refer to articles.")
211 (defvoo nntp-prepare-server-hook nil
212 "*Hook run before a server is opened.
213 If can be used to set up a server remotely, for instance. Say you
214 have an account at the machine \"other.machine\". This machine has
215 access to an NNTP server that you can't access locally. You could
216 then use this hook to rsh to the remote machine and start a proxy NNTP
217 server there that you can connect to. See also
218 `nntp-open-connection-function'")
220 (defvoo nntp-coding-system-for-read 'binary
221 "*Coding system to read from NNTP.")
223 (defvoo nntp-coding-system-for-write 'binary
224 "*Coding system to write to NNTP.")
227 (defvoo nntp-marks-is-evil nil
228 "*If non-nil, Gnus will never generate and use marks file for nntp groups.
229 See `nnml-marks-is-evil' for more information.")
231 (defvoo nntp-marks-file-name ".marks")
232 (defvoo nntp-marks nil)
233 (defvar nntp-marks-modtime (gnus-make-hashtable))
235 (defcustom nntp-marks-directory
236 (nnheader-concat gnus-directory "marks/")
237 "*The directory where marks for nntp groups will be stored."
241 (defcustom nntp-authinfo-file "~/.authinfo"
242 ".netrc-like file that holds nntp authinfo passwords."
246 (repeat :tag "Entries"
249 :value ("" ("login" . "") ("password" . ""))
253 (const :format "" "login")
254 (string :format "Login: %v"))
256 (const :format "" "password")
257 (string :format "Password: %v")))))))
261 (defvoo nntp-connection-timeout nil
262 "*Number of seconds to wait before an nntp connection times out.
263 If this variable is nil, which is the default, no timers are set.
264 NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
266 (defvoo nntp-prepare-post-hook nil
267 "*Hook run just before posting an article. It is supposed to be used
268 to insert Cancel-Lock headers.")
270 (defvoo nntp-server-list-active-group 'try
271 "If nil, then always use GROUP instead of LIST ACTIVE.
272 This is usually slower, but on misconfigured servers that don't
273 update their active files often, this can help.")
275 ;;; Internal variables.
277 (defvar nntp-record-commands nil
278 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
280 (defvar nntp-have-messaged nil)
282 (defvar nntp-process-wait-for nil)
283 (defvar nntp-process-to-buffer nil)
284 (defvar nntp-process-callback nil)
285 (defvar nntp-process-decode nil)
286 (defvar nntp-process-start-point nil)
287 (defvar nntp-inside-change-function nil)
288 (defvoo nntp-last-command-time nil)
289 (defvoo nntp-last-command nil)
290 (defvoo nntp-authinfo-password nil)
291 (defvoo nntp-authinfo-user nil)
292 (defvoo nntp-authinfo-force nil)
294 (defvar nntp-connection-list nil)
296 (defvoo nntp-server-type nil)
297 (defvoo nntp-connection-alist nil)
298 (defvoo nntp-status-string "")
299 (defconst nntp-version "nntp 5.0")
300 (defvoo nntp-inhibit-erase nil)
301 (defvoo nntp-inhibit-output nil)
303 (defvoo nntp-server-xover 'try)
305 (defvar nntp-async-timer nil)
306 (defvar nntp-async-process-list nil)
308 (defvar nntp-ssl-program
309 "openssl s_client -quiet -ssl3 -connect %s:%p"
310 "A string containing commands for SSL connections.
311 Within a string, %s is replaced with the server address and %p with
312 port number on server. The program should accept IMAP commands on
313 stdin and return responses to stdout.")
315 (defvar nntp-authinfo-rejected nil
316 "A custom error condition used to report 'Authentication Rejected' errors.
317 Condition handlers that match just this condition ensure that the nntp
318 backend doesn't catch this error.")
319 (put 'nntp-authinfo-rejected 'error-conditions '(error nntp-authinfo-rejected))
320 (put 'nntp-authinfo-rejected 'error-message "Authorization Rejected")
324 ;;; Internal functions.
326 (defsubst nntp-send-string (process string)
327 "Send STRING to PROCESS."
328 ;; We need to store the time to provide timeouts, and
329 ;; to store the command so the we can replay the command
330 ;; if the server gives us an AUTHINFO challenge.
331 (setq nntp-last-command-time (current-time)
332 nntp-last-command string)
333 (when nntp-record-commands
334 (nntp-record-command string))
335 (process-send-string process (concat string nntp-end-of-line))
336 (or (memq (process-status process) '(open run))
337 (nntp-report "Server closed connection")))
339 (defun nntp-record-command (string)
340 "Record the command STRING."
341 (with-current-buffer (get-buffer-create "*nntp-log*")
342 (goto-char (point-max))
343 (let ((time (current-time)))
344 (insert (format-time-string "%Y%m%dT%H%M%S" time)
345 "." (format "%03d" (/ (nth 2 time) 1000))
346 " " nntp-address " " string "\n"))))
348 (defun nntp-report (&rest args)
349 "Report an error from the nntp backend. The first string in ARGS
350 can be a format string. For some commands, the failed command may be
351 retried once before actually displaying the error report."
353 (when nntp-record-commands
354 (nntp-record-command "*** CALLED nntp-report ***"))
356 (nnheader-report 'nntp args)
360 (defun nntp-report-1 (&rest args)
361 "Throws out to nntp-with-open-group-error so that the connection may
362 be restored and the command retried."
364 (when nntp-record-commands
365 (nntp-record-command "*** CONNECTION LOST ***"))
367 (throw 'nntp-with-open-group-error t))
369 (defmacro nntp-insert-buffer-substring (buffer &optional start end)
370 "Copy string from unibyte buffer to multibyte current buffer."
371 (if (featurep 'xemacs)
372 `(insert-buffer-substring ,buffer ,start ,end)
373 `(if enable-multibyte-characters
374 (insert (with-current-buffer ,buffer
375 (mm-string-to-multibyte
377 `(buffer-substring (or ,start (point-min))
378 (or ,end (point-max)))
380 (insert-buffer-substring ,buffer ,start ,end))))
382 (defmacro nntp-copy-to-buffer (buffer start end)
383 "Copy string from unibyte current buffer to multibyte buffer."
384 (if (featurep 'xemacs)
385 `(copy-to-buffer ,buffer ,start ,end)
386 `(let ((string (buffer-substring ,start ,end)))
387 (with-current-buffer ,buffer
389 (insert (if enable-multibyte-characters
390 (mm-string-to-multibyte string)
392 (goto-char (point-min))
395 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
396 "Wait for WAIT-FOR to arrive from PROCESS."
398 (with-current-buffer (process-buffer process)
399 (goto-char (point-min))
401 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
402 (looking-at "48[02]"))
403 (memq (process-status process) '(open run)))
404 (cond ((looking-at "480")
405 (nntp-handle-authinfo process))
407 (nnheader-report 'nntp (get 'nntp-authinfo-rejected 'error-message))
408 (signal 'nntp-authinfo-rejected nil))
409 ((looking-at "^.*\n")
410 (delete-region (point) (progn (forward-line 1) (point)))))
411 (nntp-accept-process-output process)
412 (goto-char (point-min)))
417 (nntp-snarf-error-message)
419 ((not (memq (process-status process) '(open run)))
420 (nntp-report "Server closed connection"))
422 (goto-char (point-max))
423 (let ((limit (point-min))
425 (while (not (re-search-backward wait-for limit t))
426 (nntp-accept-process-output process)
427 ;; We assume that whatever we wait for is less than 1000
429 (setq limit (max (- (point-max) 1000) (point-min)))
430 (goto-char (point-max)))
431 (setq response (match-string 0))
432 (with-current-buffer nntp-server-buffer
433 (setq nntp-process-response response)))
434 (nntp-decode-text (not decode))
436 (with-current-buffer buffer
437 (goto-char (point-max))
438 (nntp-insert-buffer-substring (process-buffer process))
439 ;; Nix out "nntp reading...." message.
440 (when nntp-have-messaged
441 (setq nntp-have-messaged nil)
442 (nnheader-message 5 ""))))
447 (defun nntp-kill-buffer (buffer)
448 (when (buffer-name buffer)
450 (nnheader-init-server-buffer)))
452 (defun nntp-erase-buffer (buffer)
453 "Erase contents of BUFFER."
454 (with-current-buffer buffer
457 (defsubst nntp-find-connection (buffer)
458 "Find the connection delivering to BUFFER."
459 (let ((alist nntp-connection-alist)
460 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
462 (while (and alist (setq entry (pop alist)))
463 (when (eq buffer (cadr entry))
464 (setq process (car entry)
467 (if (memq (process-status process) '(open run))
469 (nntp-kill-buffer (process-buffer process))
470 (setq nntp-connection-alist (delq entry nntp-connection-alist))
473 (defsubst nntp-find-connection-entry (buffer)
474 "Return the entry for the connection to BUFFER."
475 (assq (nntp-find-connection buffer) nntp-connection-alist))
477 (defun nntp-find-connection-buffer (buffer)
478 "Return the process connection buffer tied to BUFFER."
479 (let ((process (nntp-find-connection buffer)))
481 (process-buffer process))))
483 (defsubst nntp-retrieve-data (command address port buffer
484 &optional wait-for callback decode)
485 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
486 (let ((process (or (nntp-find-connection buffer)
487 (nntp-open-connection buffer))))
490 (unless (or nntp-inhibit-erase nnheader-callback-function)
491 (nntp-erase-buffer (process-buffer process)))
495 (nntp-send-string process command))
497 ((eq callback 'ignore)
499 ((and callback wait-for)
500 (nntp-async-wait process wait-for buffer decode callback)
503 (nntp-wait-for process wait-for buffer decode))
505 (nntp-authinfo-rejected
506 (signal 'nntp-authinfo-rejected (cdr err)))
508 (nnheader-report 'nntp "Couldn't open connection to %s: %s"
511 (message "Quit retrieving data from nntp")
514 (nnheader-report 'nntp "Couldn't open connection to %s" address))))
516 (defsubst nntp-send-command (wait-for &rest strings)
517 "Send STRINGS to server and wait until WAIT-FOR returns."
518 (when (and (not nnheader-callback-function)
519 (not nntp-inhibit-output))
520 (nntp-erase-buffer nntp-server-buffer))
521 (let* ((command (mapconcat 'identity strings " "))
522 (process (nntp-find-connection nntp-server-buffer))
523 (buffer (and process (process-buffer process)))
524 (pos (and buffer (with-current-buffer buffer (point)))))
527 (nntp-retrieve-data command
528 nntp-address nntp-port-number
530 wait-for nnheader-callback-function)
531 ;; If nothing to wait for, still remove possibly echo'ed commands.
532 ;; We don't have echoes if `nntp-never-echoes-commands' is non-nil
533 ;; or the value of `nntp-open-connection-function' is in
534 ;; `nntp-open-connection-functions-never-echo-commands', so we
535 ;; skip this in that cases.
537 nntp-never-echoes-commands
539 nntp-open-connection-function
540 nntp-open-connection-functions-never-echo-commands))
541 (nntp-accept-response)
542 (with-current-buffer buffer
544 (if (looking-at (regexp-quote command))
545 (delete-region pos (progn (forward-line 1)
547 (nnheader-report 'nntp "Couldn't open connection to %s."
550 (defun nntp-send-command-nodelete (wait-for &rest strings)
551 "Send STRINGS to server and wait until WAIT-FOR returns."
552 (let* ((command (mapconcat 'identity strings " "))
553 (process (nntp-find-connection nntp-server-buffer))
554 (buffer (and process (process-buffer process)))
555 (pos (and buffer (with-current-buffer buffer (point)))))
558 (nntp-retrieve-data command
559 nntp-address nntp-port-number
561 wait-for nnheader-callback-function)
562 ;; If nothing to wait for, still remove possibly echo'ed commands
564 (nntp-accept-response)
565 (with-current-buffer buffer
567 (if (looking-at (regexp-quote command))
568 (delete-region pos (progn (forward-line 1)
570 (nnheader-report 'nntp "Couldn't open connection to %s."
573 (defun nntp-send-command-and-decode (wait-for &rest strings)
574 "Send STRINGS to server and wait until WAIT-FOR returns."
575 (when (and (not nnheader-callback-function)
576 (not nntp-inhibit-output))
577 (nntp-erase-buffer nntp-server-buffer))
578 (let* ((command (mapconcat 'identity strings " "))
579 (process (nntp-find-connection nntp-server-buffer))
580 (buffer (and process (process-buffer process)))
581 (pos (and buffer (with-current-buffer buffer (point)))))
584 (nntp-retrieve-data command
585 nntp-address nntp-port-number
587 wait-for nnheader-callback-function t)
588 ;; If nothing to wait for, still remove possibly echo'ed commands
590 (nntp-accept-response)
591 (with-current-buffer buffer
593 (if (looking-at (regexp-quote command))
594 (delete-region pos (progn (forward-line 1) (point-at-bol))))
596 (nnheader-report 'nntp "Couldn't open connection to %s."
600 (defun nntp-send-buffer (wait-for)
601 "Send the current buffer to server and wait until WAIT-FOR returns."
602 (when (and (not nnheader-callback-function)
603 (not nntp-inhibit-output))
605 (nntp-find-connection-buffer nntp-server-buffer)))
607 ;; Make sure we did not forget to encode some of the content.
608 (assert (save-excursion (goto-char (point-min))
609 (not (re-search-forward "[^\000-\377]" nil t))))
610 (mm-disable-multibyte)
611 (process-send-region (nntp-find-connection nntp-server-buffer)
612 (point-min) (point-max))
614 nil nntp-address nntp-port-number nntp-server-buffer
615 wait-for nnheader-callback-function))
619 ;;; Interface functions.
621 (nnoo-define-basics nntp)
623 (defsubst nntp-next-result-arrived-p ()
625 ;; A result that starts with a 2xx code is terminated by
626 ;; a line with only a "." on it.
627 ((eq (char-after) ?2)
628 (if (re-search-forward "\n\\.\r?\n" nil t)
630 ;; Some broken news servers add another dot at the end.
631 ;; Protect against inflooping there.
632 (while (looking-at "^\\.\r?\n")
636 ;; A result that starts with a 3xx or 4xx code is terminated
639 (if (search-forward "\n" nil t)
647 (defvar nntp-with-open-group-internal nil)
648 (defvar nntp-report-n nil))
650 (defun nntp-with-open-group-function (-group -server -connectionless -bodyfun)
651 "Protect against servers that don't like clients that keep idle connections opens.
652 The problem being that these servers may either close a connection or
653 simply ignore any further requests on a connection. Closed
654 connections are not detected until `accept-process-output' has updated
655 the `process-status'. Dropped connections are not detected until the
656 connection timeouts (which may be several minutes) or
657 `nntp-connection-timeout' has expired. When these occur
658 `nntp-with-open-group', opens a new connection then re-issues the NNTP
659 command whose response triggered the error."
660 (letf ((nntp-report-n (symbol-function 'nntp-report))
661 ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1))
662 (nntp-with-open-group-internal nil))
663 (while (catch 'nntp-with-open-group-error
664 ;; Open the connection to the server
665 ;; NOTE: Existing connections are NOT tested.
666 (nntp-possibly-change-group -group -server -connectionless)
669 (and nntp-connection-timeout
671 nntp-connection-timeout nil
673 (let* ((-process (nntp-find-connection
675 (-buffer (and -process
676 (process-buffer -process))))
677 ;; When I an able to identify the
678 ;; connection to the server AND I've
679 ;; received NO reponse for
680 ;; nntp-connection-timeout seconds.
681 (when (and -buffer (eq 0 (buffer-size -buffer)))
682 ;; Close the connection. Take no
683 ;; other action as the accept input
684 ;; code will handle the closed
686 (nntp-kill-buffer -buffer))))))))
688 (setq nntp-with-open-group-internal
692 (unless debug-on-quit
694 (signal 'quit nil))))
696 (nnheader-cancel-timer -timer)))
698 (setf (symbol-function 'nntp-report) nntp-report-n))
699 nntp-with-open-group-internal))
701 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
702 "Protect against servers that don't like clients that keep idle connections opens.
703 The problem being that these servers may either close a connection or
704 simply ignore any further requests on a connection. Closed
705 connections are not detected until `accept-process-output' has updated
706 the `process-status'. Dropped connections are not detected until the
707 connection timeouts (which may be several minutes) or
708 `nntp-connection-timeout' has expired. When these occur
709 `nntp-with-open-group', opens a new connection then re-issues the NNTP
710 command whose response triggered the error."
711 (declare (indent 2) (debug (form form [&optional symbolp] def-body)))
712 (when (and (listp connectionless)
713 (not (eq connectionless nil)))
714 (setq forms (cons connectionless forms)
716 `(nntp-with-open-group-function ,group ,server ,connectionless (lambda () ,@forms)))
718 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
719 "Retrieve the headers of ARTICLES."
720 (nntp-with-open-group
722 (with-current-buffer (nntp-find-connection-buffer nntp-server-buffer)
724 (if (and (not gnus-nov-is-evil)
725 (not nntp-nov-is-evil)
726 (nntp-retrieve-headers-with-xover articles fetch-old))
727 ;; We successfully retrieved the headers via XOVER.
729 ;; XOVER didn't work, so we do it the hard, slow and inefficient
731 (let ((number (length articles))
735 (last-point (point-min))
736 (buf (nntp-find-connection-buffer nntp-server-buffer))
737 (nntp-inhibit-erase t)
739 ;; Send HEAD commands.
740 (while (setq article (pop articles))
743 "HEAD" (if (numberp article)
744 (int-to-string article)
745 ;; `articles' is either a list of article numbers
746 ;; or a list of article IDs.
749 ;; Every 400 requests we have to read the stream in
750 ;; order to avoid deadlocks.
751 (when (or (null articles) ;All requests have been sent.
752 (zerop (% count nntp-maximum-request)))
753 (nntp-accept-response)
756 (goto-char last-point)
758 (while (nntp-next-result-arrived-p)
759 (setq last-point (point))
762 ;; If number of headers is greater than 100, give
763 ;; informative messages.
764 (and (numberp nntp-large-newsgroup)
765 (> number nntp-large-newsgroup)
766 (zerop (% received 20))
767 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
768 (/ (* received 100) number)))
769 (nntp-accept-response))))
770 (and (numberp nntp-large-newsgroup)
771 (> number nntp-large-newsgroup)
772 (nnheader-message 6 "NNTP: Receiving headers...done"))
774 ;; Now all of replies are received. Fold continuation lines.
775 (nnheader-fold-continuation-lines)
776 ;; Remove all "\r"'s.
778 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
781 (deffoo nntp-retrieve-groups (groups &optional server)
782 "Retrieve group info on GROUPS."
783 (nntp-with-open-group
785 (when (nntp-find-connection-buffer nntp-server-buffer)
788 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
789 (nntp-erase-buffer nntp-server-buffer)
790 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
791 ;; The first time this is run, this variable is `try'. So we
793 (when (eq nntp-server-list-active-group 'try)
794 (nntp-try-list-active (car groups)))
799 (last-point (point-min))
800 (nntp-inhibit-erase t)
801 (buf (nntp-find-connection-buffer nntp-server-buffer))
802 (command (if nntp-server-list-active-group
803 "LIST ACTIVE" "GROUP")))
805 ;; Timeout may have killed the buffer.
806 (unless (gnus-buffer-live-p buf)
807 (nnheader-report 'nntp "Connection to %s is closed." server)
809 ;; Send the command to the server.
810 (nntp-send-command nil command (pop groups))
812 ;; Every 400 requests we have to read the stream in
813 ;; order to avoid deadlocks.
814 (when (or (null groups) ;All requests have been sent.
815 (zerop (% count nntp-maximum-request)))
816 (nntp-accept-response)
817 (while (and (gnus-buffer-live-p buf)
819 ;; Search `blue moon' in this file for the
820 ;; reason why set-buffer here.
822 (goto-char last-point)
824 (while (re-search-forward "^[0-9]" nil t)
826 (setq last-point (point))
828 (nntp-accept-response))))
830 ;; Wait for the reply from the final command.
831 (unless (gnus-buffer-live-p buf)
832 (nnheader-report 'nntp "Connection to %s is closed." server)
835 (goto-char (point-max))
836 (re-search-backward "^[0-9]" nil t)
837 (when (looking-at "^[23]")
838 (while (and (gnus-buffer-live-p buf)
841 (goto-char (point-max))
842 (if (not nntp-server-list-active-group)
843 (not (re-search-backward "\r?\n"
845 (not (re-search-backward "^\\.\r?\n"
847 (nntp-accept-response)))
849 ;; Now all replies are received. We remove CRs.
850 (unless (gnus-buffer-live-p buf)
851 (nnheader-report 'nntp "Connection to %s is closed." server)
854 (goto-char (point-min))
855 (while (search-forward "\r" nil t)
856 (replace-match "" t t))
858 (if (not nntp-server-list-active-group)
860 (nntp-copy-to-buffer nntp-server-buffer
861 (point-min) (point-max))
863 ;; We have read active entries, so we just delete the
865 (goto-char (point-min))
866 (while (re-search-forward "^[.2-5]" nil t)
867 (delete-region (match-beginning 0)
868 (progn (forward-line 1) (point))))
869 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
872 (deffoo nntp-retrieve-articles (articles &optional group server)
873 (nntp-with-open-group
876 (let ((number (length articles))
880 (last-point (point-min))
881 (buf (nntp-find-connection-buffer nntp-server-buffer))
882 (nntp-inhibit-erase t)
883 (map (apply 'vector articles))
888 ;; Send ARTICLE command.
889 (while (setq article (pop articles))
892 "ARTICLE" (if (numberp article)
893 (int-to-string article)
894 ;; `articles' is either a list of article numbers
895 ;; or a list of article IDs.
898 ;; Every 400 requests we have to read the stream in
899 ;; order to avoid deadlocks.
900 (when (or (null articles) ;All requests have been sent.
901 (zerop (% count nntp-maximum-request)))
902 (nntp-accept-response)
905 (goto-char last-point)
907 (while (nntp-next-result-arrived-p)
908 (aset map received (cons (aref map received) (point)))
909 (setq last-point (point))
912 ;; If number of headers is greater than 100, give
913 ;; informative messages.
914 (and (numberp nntp-large-newsgroup)
915 (> number nntp-large-newsgroup)
916 (zerop (% received 20))
917 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
918 (/ (* received 100) number)))
919 (nntp-accept-response))))
920 (and (numberp nntp-large-newsgroup)
921 (> number nntp-large-newsgroup)
922 (nnheader-message 6 "NNTP: Receiving articles...done"))
924 ;; Now we have all the responses. We go through the results,
925 ;; wash it and copy it over to the server buffer.
926 (set-buffer nntp-server-buffer)
928 (setq last-point (point-min))
932 (setq point (goto-char (point-max)))
934 (nntp-insert-buffer-substring buf last-point (cdr entry))
936 (setq last-point (cdr entry))
939 (cons (car entry) point))
942 (defun nntp-try-list-active (group)
943 (nntp-list-active-group group)
944 (with-current-buffer nntp-server-buffer
945 (goto-char (point-min))
947 (looking-at "5[0-9]+"))
948 (setq nntp-server-list-active-group nil))
950 (setq nntp-server-list-active-group t)))))
952 (deffoo nntp-list-active-group (group &optional server)
953 "Return the active info on GROUP (which can be a regexp)."
954 (nntp-with-open-group
956 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
958 (deffoo nntp-request-group-articles (group &optional server)
959 "Return the list of existing articles in GROUP."
960 (nntp-with-open-group
962 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
964 (deffoo nntp-request-article (article &optional group server buffer command)
965 (nntp-with-open-group
967 (when (nntp-send-command-and-decode
968 "\r?\n\\.\r?\n" "ARTICLE"
969 (if (numberp article) (int-to-string article) article))
971 (not (equal buffer nntp-server-buffer)))
972 (with-current-buffer nntp-server-buffer
973 (copy-to-buffer buffer (point-min) (point-max))
974 (nntp-find-group-and-number group))
975 (nntp-find-group-and-number group)))))
977 (deffoo nntp-request-head (article &optional group server)
978 (nntp-with-open-group
980 (when (nntp-send-command
981 "\r?\n\\.\r?\n" "HEAD"
982 (if (numberp article) (int-to-string article) article))
984 (nntp-find-group-and-number group)
985 (nntp-decode-text)))))
987 (deffoo nntp-request-body (article &optional group server)
988 (nntp-with-open-group
990 (nntp-send-command-and-decode
991 "\r?\n\\.\r?\n" "BODY"
992 (if (numberp article) (int-to-string article) article))))
994 (deffoo nntp-request-group (group &optional server dont-check info)
995 (nntp-with-open-group
997 (when (nntp-send-command "^[245].*\n" "GROUP" group)
998 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
999 (setcar (cddr entry) group)))))
1001 (deffoo nntp-close-group (group &optional server)
1004 (deffoo nntp-server-opened (&optional server)
1005 "Say whether a connection to SERVER has been opened."
1006 (and (nnoo-current-server-p 'nntp server)
1008 (gnus-buffer-live-p nntp-server-buffer)
1009 (nntp-find-connection nntp-server-buffer)))
1011 (deffoo nntp-open-server (server &optional defs connectionless)
1012 (nnheader-init-server-buffer)
1013 (if (nntp-server-opened server)
1015 (when (or (stringp (car defs))
1016 (numberp (car defs)))
1017 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
1018 (unless (assq 'nntp-address defs)
1019 (setq defs (append defs (list (list 'nntp-address server)))))
1020 (nnoo-change-server 'nntp server defs)
1023 (or (nntp-find-connection nntp-server-buffer)
1024 (nntp-open-connection nntp-server-buffer)))))
1026 (deffoo nntp-close-server (&optional server)
1027 (nntp-possibly-change-group nil server t)
1028 (let ((process (nntp-find-connection nntp-server-buffer)))
1030 (when (memq (process-status process) '(open run))
1032 (nntp-send-string process "QUIT")
1033 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
1034 ;; Ok, this is evil, but when using telnet and stuff
1035 ;; as the connection method, it's important that the
1036 ;; QUIT command actually is sent out before we kill
1039 (nntp-kill-buffer (process-buffer process))
1040 (setq process (car (pop nntp-connection-alist))))
1041 (nnoo-close-server 'nntp)))
1043 (deffoo nntp-request-close ()
1045 (while (setq process (pop nntp-connection-list))
1046 (when (memq (process-status process) '(open run))
1048 (nntp-send-string process "QUIT")
1049 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
1050 ;; Ok, this is evil, but when using telnet and stuff
1051 ;; as the connection method, it's important that the
1052 ;; QUIT command actually is sent out before we kill
1055 (nntp-kill-buffer (process-buffer process)))))
1057 (deffoo nntp-request-list (&optional server)
1058 (nntp-with-open-group
1060 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
1062 (deffoo nntp-request-list-newsgroups (&optional server)
1063 (nntp-with-open-group
1065 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
1067 (deffoo nntp-request-newgroups (date &optional server)
1068 (nntp-with-open-group
1070 (with-current-buffer nntp-server-buffer
1071 (let* ((time (date-to-time date))
1072 (ls (- (cadr time) (nth 8 (decode-time time)))))
1074 (setcar time (1- (car time)))
1075 (setcar (cdr time) (+ ls 65536)))
1077 (setcar time (1+ (car time)))
1078 (setcar (cdr time) (- ls 65536)))
1080 (setcar (cdr time) ls)))
1083 "^\\.\r?\n" "NEWGROUPS"
1084 (format-time-string "%y%m%d %H%M%S" time)
1086 (nntp-decode-text))))))
1088 (deffoo nntp-request-post (&optional server)
1089 (nntp-with-open-group
1091 (when (nntp-send-command "^[23].*\r?\n" "POST")
1092 (let ((response (with-current-buffer nntp-server-buffer
1093 nntp-process-response))
1096 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
1098 (setq server-id (match-string 1 response))
1099 (narrow-to-region (goto-char (point-min))
1100 (if (search-forward "\n\n" nil t)
1103 (unless (mail-fetch-field "Message-ID")
1104 (goto-char (point-min))
1105 (insert "Message-ID: " server-id "\n"))
1107 (run-hooks 'nntp-prepare-post-hook)
1108 (nntp-send-buffer "^[23].*\n")))))
1110 (deffoo nntp-request-type (group article)
1113 (deffoo nntp-asynchronous-p ()
1116 (deffoo nntp-request-set-mark (group actions &optional server)
1117 (when (and (not nntp-marks-is-evil)
1118 nntp-marks-file-name)
1119 (nntp-possibly-create-directory group server)
1120 (nntp-open-marks group server)
1121 (setq nntp-marks (nnheader-update-marks-actions nntp-marks actions))
1122 (nntp-save-marks group server))
1125 (deffoo nntp-request-marks (group info &optional server)
1126 (when (and (not nntp-marks-is-evil)
1127 nntp-marks-file-name)
1128 (nntp-possibly-create-directory group server)
1129 (when (nntp-marks-changed-p group server)
1130 (nnheader-message 8 "Updating marks for %s..." group)
1131 (nntp-open-marks group server)
1132 ;; Update info using `nntp-marks'.
1133 (mapc (lambda (pred)
1134 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1135 (gnus-info-set-marks
1137 (gnus-update-alist-soft
1139 (cdr (assq (cdr pred) nntp-marks))
1140 (gnus-info-marks info))
1142 gnus-article-mark-lists)
1143 (let ((seen (cdr (assq 'read nntp-marks))))
1144 (gnus-info-set-read info
1145 (if (and (integerp (car seen))
1147 (list (cons (car seen) (car seen)))
1149 (nnheader-message 8 "Updating marks for %s...done" group)))
1154 ;;; Hooky functions.
1156 (defun nntp-send-mode-reader ()
1157 "Send the MODE READER command to the nntp server.
1158 This function is supposed to be called from `nntp-server-opened-hook'.
1159 It will make innd servers spawn an nnrpd process to allow actual article
1161 (nntp-send-command "^.*\n" "MODE READER"))
1163 (declare-function netrc-parse "netrc" (&optional file))
1164 (declare-function netrc-machine "netrc"
1165 (list machine &optional port defaultport))
1166 (declare-function netrc-get "netrc" (alist type))
1168 (defun nntp-send-authinfo (&optional send-if-force)
1169 "Send the AUTHINFO to the nntp server.
1170 It will look in the \"~/.authinfo\" file for matching entries. If
1171 nothing suitable is found there, it will prompt for a user name
1174 If SEND-IF-FORCE, only send authinfo to the server if the
1175 .authinfo file has the FORCE token."
1177 (let* ((list (netrc-parse nntp-authinfo-file))
1178 (alist (netrc-machine list nntp-address "nntp"))
1179 (force (or (netrc-get alist "force") nntp-authinfo-force))
1181 (auth-source-user-or-password '("login" "password") nntp-address "nntp"))
1182 (auth-user (nth 0 auth-info))
1183 (auth-passwd (nth 1 auth-info))
1185 ;; this is preferred to netrc-*
1187 (netrc-get alist "login")
1188 nntp-authinfo-user))
1190 ;; this is preferred to netrc-*
1192 (netrc-get alist "password"))))
1193 (when (or (not send-if-force)
1196 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1197 nntp-authinfo-user user))
1198 (unless (member user '(nil ""))
1199 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1200 (when t ;???Should check if AUTHINFO succeeded
1202 "^2.*\r?\n" "AUTHINFO PASS"
1204 nntp-authinfo-password
1205 (setq nntp-authinfo-password
1206 (read-passwd (format "NNTP (%s@%s) password: "
1207 user nntp-address))))))))))
1209 (defun nntp-send-nosy-authinfo ()
1210 "Send the AUTHINFO to the nntp server."
1211 (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
1212 (unless (member user '(nil ""))
1213 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1214 (when t ;???Should check if AUTHINFO succeeded
1215 (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
1216 (read-passwd (format "NNTP (%s@%s) password: "
1217 user nntp-address)))))))
1219 (defun nntp-send-authinfo-from-file ()
1220 "Send the AUTHINFO to the nntp server.
1222 The authinfo login name is taken from the user's login name and the
1223 password contained in '~/.nntp-authinfo'."
1224 (when (file-exists-p "~/.nntp-authinfo")
1226 (insert-file-contents "~/.nntp-authinfo")
1227 (goto-char (point-min))
1228 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
1230 "^2.*\r?\n" "AUTHINFO PASS"
1231 (buffer-substring (point) (point-at-eol))))))
1233 ;;; Internal functions.
1235 (defun nntp-handle-authinfo (process)
1236 "Take care of an authinfo response from the server."
1237 (let ((last nntp-last-command))
1238 (funcall nntp-authinfo-function)
1239 ;; We have to re-send the function that was interrupted by
1240 ;; the authinfo request.
1241 (nntp-erase-buffer nntp-server-buffer)
1242 (nntp-send-string process last)))
1244 (defun nntp-make-process-buffer (buffer)
1245 "Create a new, fresh buffer usable for nntp process connections."
1246 (with-current-buffer
1247 (generate-new-buffer
1248 (format " *server %s %s %s*"
1249 nntp-address nntp-port-number
1250 (gnus-buffer-exists-p buffer)))
1251 (mm-disable-multibyte)
1252 (set (make-local-variable 'after-change-functions) nil)
1253 (set (make-local-variable 'nntp-process-wait-for) nil)
1254 (set (make-local-variable 'nntp-process-callback) nil)
1255 (set (make-local-variable 'nntp-process-to-buffer) nil)
1256 (set (make-local-variable 'nntp-process-start-point) nil)
1257 (set (make-local-variable 'nntp-process-decode) nil)
1260 (defun nntp-open-connection (buffer)
1261 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1262 (run-hooks 'nntp-prepare-server-hook)
1263 (let* ((pbuffer (nntp-make-process-buffer buffer))
1265 (and nntp-connection-timeout
1267 nntp-connection-timeout nil
1269 (nntp-kill-buffer ,pbuffer)))))
1272 (let ((coding-system-for-read nntp-coding-system-for-read)
1273 (coding-system-for-write nntp-coding-system-for-write))
1274 (funcall nntp-open-connection-function pbuffer))
1277 (message "Quit opening connection to %s" nntp-address)
1278 (nntp-kill-buffer pbuffer)
1282 (nnheader-cancel-timer timer))
1284 (nntp-kill-buffer pbuffer))
1285 (when (and (buffer-name pbuffer)
1287 (gnus-set-process-query-on-exit-flag process nil)
1288 (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1289 (memq (process-status process) '(open run)))
1291 (caar (push (list process buffer nil) nntp-connection-alist))
1292 (push process nntp-connection-list)
1293 (with-current-buffer pbuffer
1294 (nntp-read-server-type)
1296 (set-buffer nntp-server-buffer)
1297 (let ((nnheader-callback-function nil))
1298 (run-hooks 'nntp-server-opened-hook)
1299 (nntp-send-authinfo t))))
1300 (nntp-kill-buffer (process-buffer process))
1303 (defun nntp-open-network-stream (buffer)
1304 (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
1306 (autoload 'format-spec "format-spec")
1307 (autoload 'format-spec-make "format-spec")
1308 (autoload 'open-tls-stream "tls")
1310 (defun nntp-open-ssl-stream (buffer)
1311 (let* ((process-connection-type nil)
1312 (proc (start-process "nntpd" buffer
1314 shell-command-switch
1315 (format-spec nntp-ssl-program
1318 ?p nntp-port-number)))))
1319 (gnus-set-process-query-on-exit-flag proc nil)
1320 (with-current-buffer buffer
1321 (let ((nntp-connection-alist (list proc buffer nil)))
1322 (nntp-wait-for-string "^\r*20[01]"))
1324 (delete-region (point-min) (point))
1327 (defun nntp-open-tls-stream (buffer)
1328 (let ((proc (open-tls-stream "nntpd" buffer nntp-address nntp-port-number)))
1329 (gnus-set-process-query-on-exit-flag proc nil)
1330 (with-current-buffer buffer
1331 (let ((nntp-connection-alist (list proc buffer nil)))
1332 (nntp-wait-for-string "^\r*20[01]"))
1334 (delete-region (point-min) (point))
1337 (defun nntp-read-server-type ()
1338 "Find out what the name of the server we have connected to is."
1339 ;; Wait for the status string to arrive.
1340 (setq nntp-server-type (buffer-string))
1341 (let ((case-fold-search t))
1342 ;; Run server-specific commands.
1343 (dolist (entry nntp-server-action-alist)
1344 (when (string-match (car entry) nntp-server-type)
1345 (if (and (listp (cadr entry))
1346 (not (eq 'lambda (caadr entry))))
1348 (funcall (cadr entry)))))))
1350 (defun nntp-async-wait (process wait-for buffer decode callback)
1351 (with-current-buffer (process-buffer process)
1352 (unless nntp-inside-change-function
1354 (setq nntp-process-wait-for wait-for
1355 nntp-process-to-buffer buffer
1356 nntp-process-decode decode
1357 nntp-process-callback callback
1358 nntp-process-start-point (point-max))
1359 (setq after-change-functions '(nntp-after-change-function))))
1361 (defun nntp-async-timer-handler ()
1364 (if (memq (process-status proc) '(open run))
1365 (nntp-async-trigger proc)
1366 (nntp-async-stop proc)))
1367 nntp-async-process-list))
1369 (defun nntp-async-stop (proc)
1370 (setq nntp-async-process-list (delq proc nntp-async-process-list))
1371 (when (and nntp-async-timer (not nntp-async-process-list))
1372 (nnheader-cancel-timer nntp-async-timer)
1373 (setq nntp-async-timer nil)))
1375 (defun nntp-after-change-function (beg end len)
1377 ;; we only care about insertions at eob
1378 (when (and (eq 0 len) (eq (point-max) end))
1380 (let ((proc (get-buffer-process (current-buffer))))
1382 (nntp-async-trigger proc)))))
1383 ;; any throw from after-change-functions will leave it
1384 ;; set to nil. so we reset it here, if necessary.
1386 (setq after-change-functions '(nntp-after-change-function)))))
1388 (defun nntp-async-trigger (process)
1389 (with-current-buffer (process-buffer process)
1390 (when nntp-process-callback
1391 ;; do we have an error message?
1392 (goto-char nntp-process-start-point)
1393 (if (memq (following-char) '(?4 ?5))
1394 ;; wants credentials?
1395 (if (looking-at "480")
1396 (nntp-handle-authinfo process)
1397 ;; report error message.
1398 (nntp-snarf-error-message)
1399 (nntp-do-callback nil))
1401 ;; got what we expect?
1402 (goto-char (point-max))
1403 (when (re-search-backward
1404 nntp-process-wait-for nntp-process-start-point t)
1405 (let ((response (match-string 0)))
1406 (with-current-buffer nntp-server-buffer
1407 (setq nntp-process-response response)))
1408 (nntp-async-stop process)
1410 (when (gnus-buffer-exists-p nntp-process-to-buffer)
1411 (let ((buf (current-buffer))
1412 (start nntp-process-start-point)
1413 (decode nntp-process-decode))
1414 (with-current-buffer nntp-process-to-buffer
1415 (goto-char (point-max))
1417 (narrow-to-region (point) (point))
1418 (nntp-insert-buffer-substring buf start)
1420 (nntp-decode-text))))))
1422 (goto-char (point-max))
1424 (buffer-name (get-buffer nntp-process-to-buffer))))))))
1426 (defun nntp-do-callback (arg)
1427 (let ((callback nntp-process-callback)
1428 (nntp-inside-change-function t))
1429 (setq nntp-process-callback nil)
1430 (funcall callback arg)))
1432 (defun nntp-snarf-error-message ()
1433 "Save the error message in the current buffer."
1434 (let ((message (buffer-string)))
1435 (while (string-match "[\r\n]+" message)
1436 (setq message (replace-match " " t t message)))
1437 (nnheader-report 'nntp message)
1440 (defun nntp-accept-process-output (process)
1441 "Wait for output from PROCESS and message some dots."
1442 (with-current-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1444 (let ((len (/ (buffer-size) 1024))
1447 (setq nntp-have-messaged t)
1448 (nnheader-message 7 "nntp read: %dk" len)))
1450 (nnheader-accept-process-output process)
1451 ;; accept-process-output may update status of process to indicate
1452 ;; that the server has closed the connection. This MUST be
1453 ;; handled here as the buffer restored by the save-excursion may
1454 ;; be the process's former output buffer (i.e. now killed)
1456 (memq (process-status process) '(open run)))
1457 (nntp-report "Server closed connection")))))
1459 (defun nntp-accept-response ()
1460 "Wait for output from the process that outputs to BUFFER."
1461 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1463 (defun nntp-possibly-change-group (group server &optional connectionless)
1464 (let ((nnheader-callback-function nil))
1466 (or (nntp-server-opened server)
1467 (nntp-open-server server nil connectionless)))
1469 (unless connectionless
1470 (or (nntp-find-connection nntp-server-buffer)
1471 (nntp-open-connection nntp-server-buffer))))
1474 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1476 (nntp-report "Server closed connection"))
1477 ((not (equal group (caddr entry)))
1478 (with-current-buffer (process-buffer (car entry))
1480 (nntp-send-command "^[245].*\n" "GROUP" group)
1481 (setcar (cddr entry) group)
1483 (nntp-erase-buffer nntp-server-buffer)))))))
1485 (defun nntp-decode-text (&optional cr-only)
1486 "Decode the text in the current buffer."
1487 (goto-char (point-min))
1488 (while (search-forward "\r" nil t)
1491 ;; Remove trailing ".\n" end-of-transfer marker.
1492 (goto-char (point-max))
1494 (when (looking-at ".\n")
1496 ;; Delete status line.
1497 (goto-char (point-min))
1498 (while (looking-at "[1-5][0-9][0-9] .*\n")
1499 ;; For some unknown reason, there is more than one status line.
1500 (delete-region (point) (progn (forward-line 1) (point))))
1501 ;; Remove "." -> ".." encoding.
1502 (while (search-forward "\n.." nil t)
1505 (defun nntp-encode-text ()
1506 "Encode the text in the current buffer."
1508 ;; Replace "." at beginning of line with "..".
1509 (goto-char (point-min))
1510 (while (re-search-forward "^\\." nil t)
1512 (goto-char (point-max))
1513 ;; Insert newline at the end of the buffer.
1516 ;; Insert `.' at end of buffer (end of text mark).
1517 (goto-char (point-max))
1519 (goto-char (point-min))
1523 (insert nntp-end-of-line))))
1525 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1526 (set-buffer nntp-server-buffer)
1530 ;; This server does not talk NOV.
1531 ((not nntp-server-xover)
1534 ;; We don't care about gaps.
1535 ((or (not nntp-nov-gap)
1537 (nntp-send-xover-command
1539 (if (numberp fetch-old)
1540 (max 1 (- (car articles) fetch-old))
1543 (car (last articles)) 'wait)
1545 (goto-char (point-min))
1546 (when (looking-at "[1-5][0-9][0-9] .*\n")
1547 (delete-region (point) (progn (forward-line 1) (point))))
1548 (while (search-forward "\r" nil t)
1549 (replace-match "" t t))
1550 (goto-char (point-max))
1552 (when (looking-at "\\.")
1553 (delete-region (point) (progn (forward-line 1) (point)))))
1555 ;; We do it the hard way. For each gap, an XOVER command is sent
1556 ;; to the server. We do not wait for a reply from the server, we
1557 ;; just send them off as fast as we can. That means that we have
1558 ;; to count the number of responses we get back to find out when we
1559 ;; have gotten all we asked for.
1560 ((numberp nntp-nov-gap)
1565 (buf nntp-server-buffer)
1566 (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1568 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1569 ;; that means that the server does not understand XOVER, but we
1570 ;; won't know that until we try.
1571 (while (and nntp-server-xover articles)
1572 (setq first (car articles))
1573 ;; Search forward until we find a gap, or until we run out of
1575 (while (and (cdr articles)
1576 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1577 (setq articles (cdr articles)))
1579 (setq in-process-buffer-p (stringp nntp-server-xover))
1580 (nntp-send-xover-command first (setq last (car articles)))
1581 (setq articles (cdr articles))
1583 (when (and nntp-server-xover in-process-buffer-p)
1584 ;; Don't count tried request.
1585 (setq count (1+ count))
1587 ;; Every 400 requests we have to read the stream in
1588 ;; order to avoid deadlocks.
1589 (when (or (null articles) ;All requests have been sent.
1590 (= 1 (% count nntp-maximum-request)))
1592 (nntp-accept-response)
1593 ;; On some Emacs versions the preceding function has a
1594 ;; tendency to change the buffer. Perhaps. It's quite
1595 ;; difficult to reproduce, because it only seems to happen
1596 ;; once in a blue moon.
1597 (set-buffer process-buffer)
1599 (goto-char (or last-point (point-min)))
1601 (while (re-search-forward "^\\([0-9][0-9][0-9]\\) .*\n"
1604 (setq status (match-string 1))
1605 (if (string-match "^[45]" status)
1606 (setq status 'error)
1608 (setq last-point (point))
1609 (or (< received count)
1610 (if (eq status 'error)
1612 ;; I haven't started reading the final response
1614 (goto-char (point-max))
1616 (not (looking-at "^\\.\r?\n"))))))
1617 ;; I haven't read the end of the final response
1618 (nntp-accept-response)
1619 (set-buffer process-buffer))))
1621 ;; Some nntp servers seem to have an extension to the XOVER
1622 ;; extension. On these servers, requesting an article range
1623 ;; preceeding the active range does not return an error as
1624 ;; specified in the RFC. What we instead get is the NOV entry
1625 ;; for the first available article. Obviously, a client can
1626 ;; use that entry to avoid making unnecessary requests. The
1627 ;; only problem is for a client that assumes that the response
1628 ;; will always be within the requested ranage. For such a
1629 ;; client, we can get N copies of the same entry (one for each
1630 ;; XOVER command sent to the server).
1633 (goto-char (point-min))
1634 (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1635 (let ((low-limit (string-to-number
1636 (buffer-substring (match-beginning 1)
1638 (while (and articles (<= (car articles) low-limit))
1639 (setq articles (cdr articles))))))
1642 (when nntp-server-xover
1643 (when in-process-buffer-p
1645 (goto-char (point-max))
1646 (nntp-insert-buffer-substring process-buffer)
1647 (set-buffer process-buffer)
1651 ;; We remove any "." lines and status lines.
1652 (goto-char (point-min))
1653 (while (search-forward "\r" nil t)
1655 (goto-char (point-min))
1656 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1661 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1662 "Send the XOVER command to the server."
1663 (let ((range (format "%d-%d" beg end))
1664 (nntp-inhibit-erase t))
1665 (if (stringp nntp-server-xover)
1666 ;; If `nntp-server-xover' is a string, then we just send this
1669 (nntp-send-command-nodelete
1670 "\r?\n\\.\r?\n" nntp-server-xover range)
1671 ;; We do not wait for the reply.
1672 (nntp-send-command-nodelete nil nntp-server-xover range))
1673 (let ((commands nntp-xover-commands))
1674 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1675 ;; We try them all until we get at positive response.
1676 (while (and commands (eq nntp-server-xover 'try))
1677 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1678 (with-current-buffer nntp-server-buffer
1679 (goto-char (point-min))
1680 (and (looking-at "[23]") ; No error message.
1681 ;; We also have to look at the lines. Some buggy
1682 ;; servers give back simple lines with just the
1683 ;; article number. How... helpful.
1686 ;; More text after number, or a dot.
1687 (looking-at "[0-9]+\t...\\|\\.\r?\n"))
1688 (setq nntp-server-xover (car commands))))
1689 (setq commands (cdr commands)))
1690 ;; If none of the commands worked, we disable XOVER.
1691 (when (eq nntp-server-xover 'try)
1692 (nntp-erase-buffer nntp-server-buffer)
1693 (setq nntp-server-xover nil))
1694 nntp-server-xover))))
1696 (defun nntp-find-group-and-number (&optional group)
1699 ;; FIXME: This is REALLY FISHY: set-buffer after save-restriction?!?
1700 (set-buffer nntp-server-buffer)
1701 (narrow-to-region (goto-char (point-min))
1702 (or (search-forward "\n\n" nil t) (point-max)))
1703 (goto-char (point-min))
1704 ;; We first find the number by looking at the status line.
1705 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1707 (buffer-substring (match-beginning 1)
1710 (and number (zerop number) (setq number nil))
1712 ;; Then we find the group name.
1715 ;; If there is only one group in the Newsgroups
1716 ;; header, then it seems quite likely that this
1717 ;; article comes from that group, I'd say.
1718 ((and (setq newsgroups
1719 (mail-fetch-field "newsgroups"))
1720 (not (string-match "," newsgroups)))
1722 ;; If there is more than one group in the
1723 ;; Newsgroups header, then the Xref header should
1724 ;; be filled out. We hazard a guess that the group
1725 ;; that has this article number in the Xref header
1726 ;; is the one we are looking for. This might very
1727 ;; well be wrong if this article happens to have
1728 ;; the same number in several groups, but that's
1730 ((and (setq xref (mail-fetch-field "xref"))
1733 (format "\\([^ :]+\\):%d" number) xref))
1734 (match-string 1 xref))
1737 ((and (not nntp-xref-number-is-evil)
1738 (setq xref (mail-fetch-field "xref"))
1741 (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1742 "\\([^ :]+\\):\\([0-9]+\\)")
1744 (setq group (match-string 1 xref)
1745 number (string-to-number (match-string 2 xref))))
1746 ((and (setq newsgroups
1747 (mail-fetch-field "newsgroups"))
1748 (not (string-match "," newsgroups)))
1749 (setq group newsgroups))
1751 (t (setq group ""))))
1752 (when (string-match "\r" group)
1753 (setq group (substring group 0 (match-beginning 0))))
1754 (cons group number)))))
1756 (defun nntp-wait-for-string (regexp)
1757 "Wait until string arrives in the buffer."
1758 (let ((buf (current-buffer))
1760 (goto-char (point-min))
1761 (while (and (setq proc (get-buffer-process buf))
1762 (memq (process-status proc) '(open run))
1763 (not (re-search-forward regexp nil t)))
1764 (accept-process-output proc 0.1)
1766 (goto-char (point-min)))))
1769 ;; ==========================================================================
1770 ;; Obsolete nntp-open-* connection methods -- drv
1771 ;; ==========================================================================
1773 (defvoo nntp-open-telnet-envuser nil
1774 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1776 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
1777 "*Regular expression to match the shell prompt on the remote machine.")
1779 (defvoo nntp-rlogin-program "rsh"
1780 "*Program used to log in on remote machines.
1781 The default is \"rsh\", but \"ssh\" is a popular alternative.")
1783 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1784 "*Parameters to `nntp-open-rlogin'.
1785 That function may be used as `nntp-open-connection-function'. In that
1786 case, this list will be used as the parameter list given to rsh.")
1788 (defvoo nntp-rlogin-user-name nil
1789 "*User name on remote system when using the rlogin connect method.")
1791 (defvoo nntp-telnet-parameters
1792 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1793 "*Parameters to `nntp-open-telnet'.
1794 That function may be used as `nntp-open-connection-function'. In that
1795 case, this list will be executed as a command after logging in
1798 (defvoo nntp-telnet-user-name nil
1799 "User name to log in via telnet with.")
1801 (defvoo nntp-telnet-passwd nil
1802 "Password to use to log in via telnet with.")
1804 (defun nntp-service-to-port (svc)
1806 ((integerp svc) (number-to-string svc))
1807 ((string-match "\\`[0-9]+\\'" svc) svc)
1810 (ignore-errors (insert-file-contents "/etc/services"))
1811 (goto-char (point-min))
1812 (if (re-search-forward (concat "^" (regexp-quote svc)
1813 "[ \t]+\\([0-9]+\\)/tcp"))
1817 (defun nntp-open-telnet (buffer)
1818 (with-current-buffer buffer
1822 "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1823 (case-fold-search t))
1824 (when (memq (process-status proc) '(open run))
1825 (nntp-wait-for-string "^r?telnet")
1826 (process-send-string proc "set escape \^X\n")
1828 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1829 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1830 nntp-address "\n")))
1832 (process-send-string proc (concat "open " nntp-address "\n"))))
1834 ((not nntp-open-telnet-envuser)
1835 (nntp-wait-for-string "^\r*.?login:")
1836 (process-send-string
1838 (or nntp-telnet-user-name
1839 (setq nntp-telnet-user-name (read-string "login: ")))
1841 (nntp-wait-for-string "^\r*.?password:")
1842 (process-send-string
1844 (or nntp-telnet-passwd
1845 (setq nntp-telnet-passwd
1846 (read-passwd "Password: ")))
1848 (nntp-wait-for-string nntp-telnet-shell-prompt)
1849 (process-send-string
1850 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1851 (nntp-wait-for-string "^\r*20[01]")
1853 (delete-region (point-min) (point))
1854 (process-send-string proc "\^]")
1855 (nntp-wait-for-string "^r?telnet")
1856 (process-send-string proc "mode character\n")
1857 (accept-process-output proc 1)
1859 (goto-char (point-min))
1861 (delete-region (point) (point-max)))
1864 (defun nntp-open-rlogin (buffer)
1865 "Open a connection to SERVER using rsh."
1866 (let ((proc (if nntp-rlogin-user-name
1867 (apply 'start-process
1868 "nntpd" buffer nntp-rlogin-program
1869 nntp-address "-l" nntp-rlogin-user-name
1870 nntp-rlogin-parameters)
1871 (apply 'start-process
1872 "nntpd" buffer nntp-rlogin-program nntp-address
1873 nntp-rlogin-parameters))))
1874 (with-current-buffer buffer
1875 (nntp-wait-for-string "^\r*20[01]")
1877 (delete-region (point-min) (point))
1881 ;; ==========================================================================
1882 ;; Replacements for the nntp-open-* functions -- drv
1883 ;; ==========================================================================
1885 (defun nntp-open-telnet-stream (buffer)
1886 "Open a nntp connection by telnet'ing the news server.
1887 `nntp-open-netcat-stream' is recommended in place of this function
1888 because it is more reliable.
1890 Please refer to the following variables to customize the connection:
1891 - `nntp-pre-command',
1892 - `nntp-telnet-command',
1893 - `nntp-telnet-switches',
1895 - `nntp-port-number',
1896 - `nntp-end-of-line'."
1897 (let ((command `(,nntp-telnet-command
1898 ,@nntp-telnet-switches
1900 ,(nntp-service-to-port nntp-port-number)))
1902 (and nntp-pre-command
1903 (push nntp-pre-command command))
1904 (setq proc (apply 'start-process "nntpd" buffer command))
1905 (with-current-buffer buffer
1906 (nntp-wait-for-string "^\r*20[01]")
1908 (delete-region (point-min) (point))
1911 (defun nntp-open-via-rlogin-and-telnet (buffer)
1912 "Open a connection to an nntp server through an intermediate host.
1913 First rlogin to the remote host, and then telnet the real news server
1915 `nntp-open-via-rlogin-and-netcat' is recommended in place of this function
1916 because it is more reliable.
1918 Please refer to the following variables to customize the connection:
1919 - `nntp-pre-command',
1920 - `nntp-via-rlogin-command',
1921 - `nntp-via-rlogin-command-switches',
1922 - `nntp-via-user-name',
1923 - `nntp-via-address',
1924 - `nntp-telnet-command',
1925 - `nntp-telnet-switches',
1927 - `nntp-port-number',
1928 - `nntp-end-of-line'."
1929 (let ((command `(,nntp-via-address
1930 ,nntp-telnet-command
1931 ,@nntp-telnet-switches))
1933 (when nntp-via-user-name
1934 (setq command `("-l" ,nntp-via-user-name ,@command)))
1935 (when nntp-via-rlogin-command-switches
1936 (setq command (append nntp-via-rlogin-command-switches command)))
1937 (push nntp-via-rlogin-command command)
1938 (and nntp-pre-command
1939 (push nntp-pre-command command))
1940 (setq proc (apply 'start-process "nntpd" buffer command))
1941 (with-current-buffer buffer
1942 (nntp-wait-for-string "^r?telnet")
1943 (process-send-string proc (concat "open " nntp-address " "
1944 (nntp-service-to-port nntp-port-number)
1946 (nntp-wait-for-string "^\r*20[01]")
1948 (delete-region (point-min) (point))
1949 (process-send-string proc "\^]")
1950 (nntp-wait-for-string "^r?telnet")
1951 (process-send-string proc "mode character\n")
1952 (accept-process-output proc 1)
1954 (goto-char (point-min))
1956 (delete-region (point) (point-max)))
1959 (defun nntp-open-via-rlogin-and-netcat (buffer)
1960 "Open a connection to an nntp server through an intermediate host.
1961 First rlogin to the remote host, and then connect to the real news
1962 server from there using the netcat command.
1964 Please refer to the following variables to customize the connection:
1965 - `nntp-pre-command',
1966 - `nntp-via-rlogin-command',
1967 - `nntp-via-rlogin-command-switches',
1968 - `nntp-via-user-name',
1969 - `nntp-via-address',
1970 - `nntp-netcat-command',
1971 - `nntp-netcat-switches',
1973 - `nntp-port-number'."
1974 (let ((command `(,@(when nntp-pre-command
1975 (list nntp-pre-command))
1976 ,nntp-via-rlogin-command
1977 ,@nntp-via-rlogin-command-switches
1978 ,@(when nntp-via-user-name
1979 (list "-l" nntp-via-user-name))
1981 ,nntp-netcat-command
1982 ,@nntp-netcat-switches
1984 ,(nntp-service-to-port nntp-port-number))))
1985 ;; A non-nil connection type results in mightily odd behavior where
1986 ;; (process-send-string proc "\^M") ends up sending a "\n" to the
1987 ;; ssh process. --Stef
1988 ;; Also a nil connection allow ssh-askpass to work under X11.
1989 (let ((process-connection-type nil))
1990 (apply 'start-process "nntpd" buffer command))))
1992 (defun nntp-open-netcat-stream (buffer)
1993 "Open a connection to an nntp server through netcat.
1994 I.e. use the `nc' command rather than Emacs's builtin networking code.
1996 Please refer to the following variables to customize the connection:
1997 - `nntp-pre-command',
1998 - `nntp-netcat-command',
1999 - `nntp-netcat-switches',
2001 - `nntp-port-number'."
2002 (let ((command `(,nntp-netcat-command
2003 ,@nntp-netcat-switches
2005 ,(nntp-service-to-port nntp-port-number))))
2006 (and nntp-pre-command (push nntp-pre-command command))
2007 (let ((process-connection-type nil)) ;See `nntp-open-via-rlogin-and-netcat'.
2008 (apply 'start-process "nntpd" buffer command))))
2011 (defun nntp-open-via-telnet-and-telnet (buffer)
2012 "Open a connection to an nntp server through an intermediate host.
2013 First telnet the remote host, and then telnet the real news server
2016 Please refer to the following variables to customize the connection:
2017 - `nntp-pre-command',
2018 - `nntp-via-telnet-command',
2019 - `nntp-via-telnet-switches',
2020 - `nntp-via-address',
2021 - `nntp-via-envuser',
2022 - `nntp-via-user-name',
2023 - `nntp-via-user-password',
2024 - `nntp-via-shell-prompt',
2025 - `nntp-telnet-command',
2026 - `nntp-telnet-switches',
2028 - `nntp-port-number',
2029 - `nntp-end-of-line'."
2030 (with-current-buffer buffer
2032 (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
2033 (case-fold-search t)
2035 (and nntp-pre-command (push nntp-pre-command command))
2036 (setq proc (apply 'start-process "nntpd" buffer command))
2037 (when (memq (process-status proc) '(open run))
2038 (nntp-wait-for-string "^r?telnet")
2039 (process-send-string proc "set escape \^X\n")
2041 ((and nntp-via-envuser nntp-via-user-name)
2042 (process-send-string proc (concat "open " "-l" nntp-via-user-name
2043 nntp-via-address "\n")))
2045 (process-send-string proc (concat "open " nntp-via-address
2047 (when (not nntp-via-envuser)
2048 (nntp-wait-for-string "^\r*.?login:")
2049 (process-send-string proc
2051 (or nntp-via-user-name
2052 (setq nntp-via-user-name
2053 (read-string "login: ")))
2055 (nntp-wait-for-string "^\r*.?password:")
2056 (process-send-string proc
2058 (or nntp-via-user-password
2059 (setq nntp-via-user-password
2060 (read-passwd "Password: ")))
2062 (nntp-wait-for-string nntp-via-shell-prompt)
2063 (let ((real-telnet-command `("exec"
2064 ,nntp-telnet-command
2065 ,@nntp-telnet-switches
2067 ,(nntp-service-to-port nntp-port-number))))
2068 (process-send-string proc
2069 (concat (mapconcat 'identity
2070 real-telnet-command " ")
2072 (nntp-wait-for-string "^\r*20[01]")
2074 (delete-region (point-min) (point))
2075 (process-send-string proc "\^]")
2076 (nntp-wait-for-string "^r?telnet")
2077 (process-send-string proc "mode character\n")
2078 (accept-process-output proc 1)
2080 (goto-char (point-min))
2082 (delete-region (point) (point-max)))
2087 (defun nntp-marks-directory (server)
2088 (expand-file-name server nntp-marks-directory))
2090 (defvar nntp-server-to-method-cache nil
2091 "Alist of servers and select methods.")
2093 (defun nntp-group-pathname (server group &optional file)
2094 "Return an absolute file name of FILE for GROUP on SERVER."
2095 (let ((method (cdr (assoc server nntp-server-to-method-cache))))
2097 (push (cons server (setq method (or (gnus-server-to-method server)
2098 (gnus-find-method-for-group group))))
2099 nntp-server-to-method-cache))
2100 (nnmail-group-pathname
2101 (mm-decode-coding-string group
2102 (inline (gnus-group-name-charset method group)))
2103 (nntp-marks-directory server)
2106 (defun nntp-possibly-create-directory (group server)
2107 (let ((dir (nntp-group-pathname server group))
2108 (file-name-coding-system nnmail-pathname-coding-system))
2109 (unless (file-exists-p dir)
2110 (make-directory (directory-file-name dir) t)
2111 (nnheader-message 5 "Creating nntp marks directory %s" dir))))
2113 (autoload 'time-less-p "time-date")
2115 (defun nntp-marks-changed-p (group server)
2116 (let ((file (nntp-group-pathname server group nntp-marks-file-name))
2117 (file-name-coding-system nnmail-pathname-coding-system))
2118 (if (null (gnus-gethash file nntp-marks-modtime))
2119 t ;; never looked at marks file, assume it has changed
2120 (time-less-p (gnus-gethash file nntp-marks-modtime)
2121 (nth 5 (file-attributes file))))))
2123 (defun nntp-save-marks (group server)
2124 (let ((file-name-coding-system nnmail-pathname-coding-system)
2125 (file (nntp-group-pathname server group nntp-marks-file-name)))
2128 (nntp-possibly-create-directory group server)
2129 (with-temp-file file
2131 (gnus-prin1 nntp-marks)
2134 (nth 5 (file-attributes file))
2135 nntp-marks-modtime))
2136 (error (or (gnus-yes-or-no-p
2137 (format "Could not write to %s (%s). Continue? " file err))
2138 (error "Cannot write to %s (%s)" file err))))))
2140 (defun nntp-open-marks (group server)
2141 (let ((file (nntp-group-pathname server group nntp-marks-file-name))
2142 (file-name-coding-system nnmail-pathname-coding-system))
2143 (if (file-exists-p file)
2146 (gnus-sethash file (nth 5 (file-attributes file))
2148 (nnheader-insert-file-contents file)
2149 (setq nntp-marks (read (current-buffer)))
2150 (dolist (el gnus-article-unpropagated-mark-lists)
2151 (setq nntp-marks (gnus-remassoc el nntp-marks))))
2152 (error (or (gnus-yes-or-no-p
2153 (format "Error reading nntp marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
2154 (error "Cannot read nntp marks file %s (%s)" file err))))
2155 ;; User didn't have a .marks file. Probably first time
2156 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
2157 (let ((info (gnus-get-info
2158 (gnus-group-prefixed-name
2160 (gnus-server-to-method (format "nntp:%s" server)))))
2161 (decoded-name (mm-decode-coding-string
2163 (gnus-group-name-charset
2164 (gnus-server-to-method server) group))))
2165 (nnheader-message 7 "Bootstrapping marks for %s..." decoded-name)
2166 (setq nntp-marks (gnus-info-marks info))
2167 (push (cons 'read (gnus-info-read info)) nntp-marks)
2168 (dolist (el gnus-article-unpropagated-mark-lists)
2169 (setq nntp-marks (gnus-remassoc el nntp-marks)))
2170 (nntp-save-marks group server)
2171 (nnheader-message 7 "Bootstrapping marks for %s...done"
2176 ;;; nntp.el ends here