* lisp/gnus/nntp.el: Stop the `letf' madness.
[bpt/emacs.git] / lisp / gnus / nntp.el
CommitLineData
16409b0b 1;;; nntp.el --- nntp access for Gnus
f9936da6 2
7e67562f 3;; Copyright (C) 1987-1990, 1992-1998, 2000-2012
95df8112 4;; Free Software Foundation, Inc.
eec82323 5
6748645f 6;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
7;; Keywords: news
8
9;; This file is part of GNU Emacs.
10
5e809f55
GM
11;; GNU Emacs is free software: you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
eec82323 15
5e809f55
GM
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
eec82323
LMI
20
21;; You should have received a copy of the GNU General Public License
5e809f55 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
23
24;;; Commentary:
25
26;;; Code:
27
f0b7f5a8 28;; For Emacs <22.2 and XEmacs.
aa8f8277 29(eval-and-compile
da91b5f2
CY
30 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
31 ;; In Emacs 24, `open-protocol-stream' is an autoloaded alias for
32 ;; `make-network-stream'.
33 (unless (fboundp 'open-protocol-stream)
34 (require 'proto-stream)))
aa8f8277 35
eec82323
LMI
36(require 'nnheader)
37(require 'nnoo)
38(require 'gnus-util)
01c52d31
MB
39(require 'gnus)
40(require 'gnus-group) ;; gnus-group-name-charset
eec82323
LMI
41
42(nnoo-declare nntp)
43
16409b0b 44(eval-when-compile (require 'cl))
eec82323 45
b8e0f0cd 46(autoload 'auth-source-search "auth-source")
e952b711 47
e79f14a4
RS
48(defgroup nntp nil
49 "NNTP access for Gnus."
50 :group 'gnus)
51
eec82323
LMI
52(defvoo nntp-address nil
53 "Address of the physical nntp server.")
54
55(defvoo nntp-port-number "nntp"
56 "Port number on the physical nntp server.")
57
58(defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
59 "*Hook used for sending commands to the server at startup.
60The default value is `nntp-send-mode-reader', which makes an innd
6748645f 61server spawn an nnrpd server.")
eec82323
LMI
62
63(defvoo nntp-authinfo-function 'nntp-send-authinfo
6748645f
LMI
64 "Function used to send AUTHINFO to the server.
65It is called with no parameters.")
eec82323
LMI
66
67(defvoo nntp-server-action-alist
16409b0b
GM
68 '(("nntpd 1\\.5\\.11t"
69 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
70 ("NNRP server Netscape"
71 (setq nntp-server-list-active-group nil)))
eec82323
LMI
72 "Alist of regexps to match on server types and actions to be taken.
73For instance, if you want Gnus to beep every time you connect
74to innd, you could say something like:
75
76\(setq nntp-server-action-alist
77 '((\"innd\" (ding))))
78
79You probably don't want to do that, though.")
80
81(defvoo nntp-open-connection-function 'nntp-open-network-stream
e742e117
CY
82 "Method for connecting to a remote system.
83It should be a function, which is called with the output buffer
84as its single argument, or one of the following special values:
85
86- `nntp-open-network-stream' specifies a network connection,
87 upgrading to a TLS connection via STARTTLS if possible.
88- `nntp-open-plain-stream' specifies an unencrypted network
89 connection (no STARTTLS upgrade is attempted).
90- `nntp-open-ssl-stream' or `nntp-open-tls-stream' specify a TLS
91 network connection.
92
93Apart from the above special values, valid functions are as
94follows; please refer to their respective doc string for more
95information.
96For direct connections:
97- `nntp-open-netcat-stream'
98- `nntp-open-telnet-stream'
99For indirect connections:
100- `nntp-open-via-rlogin-and-netcat'
101- `nntp-open-via-rlogin-and-telnet'
102- `nntp-open-via-telnet-and-telnet'")
eec82323 103
45cb30ee
MB
104(defvoo nntp-never-echoes-commands nil
105 "*Non-nil means the nntp server never echoes commands.
106It is reported that some nntps server doesn't echo commands. So, you
107may want to set this to non-nil in the method for such a server setting
108`nntp-open-connection-function' to `nntp-open-ssl-stream' for example.
109Note that the `nntp-open-connection-functions-never-echo-commands'
110variable overrides the nil value of this variable.")
111
112(defvoo nntp-open-connection-functions-never-echo-commands
113 '(nntp-open-network-stream)
114 "*List of functions that never echo commands.
115Add or set a function which you set to `nntp-open-connection-function'
116to this list if it does not echo commands. Note that a non-nil value
117of the `nntp-never-echoes-commands' variable overrides this variable.")
118
23f87bed
MB
119(defvoo nntp-pre-command nil
120 "*Pre-command to use with the various nntp-open-via-* methods.
121This is where you would put \"runsocks\" or stuff like that.")
eec82323 122
23f87bed
MB
123(defvoo nntp-telnet-command "telnet"
124 "*Telnet command used to connect to the nntp server.
01c52d31
MB
125This command is used by the methods `nntp-open-telnet-stream',
126`nntp-open-via-rlogin-and-telnet' and `nntp-open-via-telnet-and-telnet'.")
eec82323 127
23f87bed
MB
128(defvoo nntp-telnet-switches '("-8")
129 "*Switches given to the telnet command `nntp-telnet-command'.")
eec82323 130
23f87bed
MB
131(defvoo nntp-end-of-line "\r\n"
132 "*String to use on the end of lines when talking to the NNTP server.
01c52d31
MB
133This is \"\\r\\n\" by default, but should be \"\\n\" when using an indirect
134connection method (nntp-open-via-*).")
eec82323 135
23f87bed
MB
136(defvoo nntp-via-rlogin-command "rsh"
137 "*Rlogin command used to connect to an intermediate host.
01c52d31
MB
138This command is used by the methods `nntp-open-via-rlogin-and-telnet'
139and `nntp-open-via-rlogin-and-netcat'. The default is \"rsh\", but \"ssh\"
140is a popular alternative.")
6748645f 141
23f87bed
MB
142(defvoo nntp-via-rlogin-command-switches nil
143 "*Switches given to the rlogin command `nntp-via-rlogin-command'.
144If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
145\(\"-C\") in order to compress all data connections, otherwise set this
146to \(\"-t\" \"-e\" \"none\") or (\"-C\" \"-t\" \"-e\" \"none\") if the telnet
147command requires a pseudo-tty allocation on an intermediate host.")
6748645f 148
23f87bed
MB
149(defvoo nntp-via-telnet-command "telnet"
150 "*Telnet command used to connect to an intermediate host.
151This command is used by the `nntp-open-via-telnet-and-telnet' method.")
a8151ef7 152
23f87bed
MB
153(defvoo nntp-via-telnet-switches '("-8")
154 "*Switches given to the telnet command `nntp-via-telnet-command'.")
a8151ef7 155
990e2c2f 156(defvoo nntp-netcat-command "nc"
01c52d31 157 "*Netcat command used to connect to the nntp server.
990e2c2f
SM
158This command is used by the `nntp-open-netcat-stream' and
159`nntp-open-via-rlogin-and-netcat' methods.")
01c52d31 160
990e2c2f
SM
161(defvoo nntp-netcat-switches nil
162 "*Switches given to the netcat command `nntp-netcat-command'.")
01c52d31 163
23f87bed
MB
164(defvoo nntp-via-user-name nil
165 "*User name to log in on an intermediate host with.
01c52d31 166This variable is used by the various nntp-open-via-* methods.")
23f87bed
MB
167
168(defvoo nntp-via-user-password nil
169 "*Password to use to log in on an intermediate host with.
170This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
171
172(defvoo nntp-via-address nil
173 "*Address of an intermediate host to connect to.
01c52d31 174This variable is used by the various nntp-open-via-* methods.")
23f87bed
MB
175
176(defvoo nntp-via-envuser nil
177 "*Whether both telnet client and server support the ENVIRON option.
178If non-nil, there will be no prompt for a login name.")
179
180(defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
181 "*Regular expression to match the shell prompt on an intermediate host.
182This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
eec82323
LMI
183
184(defvoo nntp-large-newsgroup 50
23f87bed
MB
185 "*The number of articles which indicates a large newsgroup.
186If the number of articles is greater than the value, verbose
eec82323
LMI
187messages will be shown to indicate the current status.")
188
189(defvoo nntp-maximum-request 400
190 "*The maximum number of the requests sent to the NNTP server at one time.
191If Emacs hangs up while retrieving headers, set the variable to a
192lower value.")
193
194(defvoo nntp-nov-is-evil nil
195 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
196
197(defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
198 "*List of strings that are used as commands to fetch NOV lines from a server.
199The strings are tried in turn until a positive response is gotten. If
200none of the commands are successful, nntp will just grab headers one
201by one.")
202
a8151ef7 203(defvoo nntp-nov-gap 5
eec82323
LMI
204 "*Maximum allowed gap between two articles.
205If the gap between two consecutive articles is bigger than this
206variable, split the XOVER request into two requests.")
207
37cc095b
MB
208(defvoo nntp-xref-number-is-evil nil
209 "*If non-nil, Gnus never trusts article numbers in the Xref header.
210Some news servers, e.g., ones running Diablo, run multiple engines
211having the same articles but article numbers are not kept synchronized
212between them. If you connect to such a server, set this to a non-nil
213value, and Gnus never uses article numbers (that appear in the Xref
214header and vary by which engine is chosen) to refer to articles.")
215
eec82323
LMI
216(defvoo nntp-prepare-server-hook nil
217 "*Hook run before a server is opened.
218If can be used to set up a server remotely, for instance. Say you
219have an account at the machine \"other.machine\". This machine has
220access to an NNTP server that you can't access locally. You could
221then use this hook to rsh to the remote machine and start a proxy NNTP
6748645f
LMI
222server there that you can connect to. See also
223`nntp-open-connection-function'")
eec82323 224
6748645f
LMI
225(defvoo nntp-coding-system-for-read 'binary
226 "*Coding system to read from NNTP.")
227
228(defvoo nntp-coding-system-for-write 'binary
229 "*Coding system to write to NNTP.")
230
01c52d31
MB
231;; Marks
232(defvoo nntp-marks-is-evil nil
233 "*If non-nil, Gnus will never generate and use marks file for nntp groups.
234See `nnml-marks-is-evil' for more information.")
235
236(defvoo nntp-marks-file-name ".marks")
237(defvoo nntp-marks nil)
238(defvar nntp-marks-modtime (gnus-make-hashtable))
239
240(defcustom nntp-marks-directory
241 (nnheader-concat gnus-directory "marks/")
242 "*The directory where marks for nntp groups will be stored."
243 :group 'nntp
244 :type 'directory)
245
6748645f
LMI
246(defcustom nntp-authinfo-file "~/.authinfo"
247 ".netrc-like file that holds nntp authinfo passwords."
e79f14a4 248 :group 'nntp
6748645f
LMI
249 :type
250 '(choice file
251 (repeat :tag "Entries"
252 :menu-tag "Inline"
253 (list :format "%v"
254 :value ("" ("login" . "") ("password" . ""))
255 (string :tag "Host")
256 (checklist :inline t
257 (cons :format "%v"
258 (const :format "" "login")
259 (string :format "Login: %v"))
260 (cons :format "%v"
261 (const :format "" "password")
23f87bed 262 (string :format "Password: %v")))))))
6748645f 263
1e91d506
G
264(make-obsolete 'nntp-authinfo-file nil "Emacs 24.1")
265
eec82323
LMI
266\f
267
6748645f
LMI
268(defvoo nntp-connection-timeout nil
269 "*Number of seconds to wait before an nntp connection times out.
2eebe218
DL
270If this variable is nil, which is the default, no timers are set.
271NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
6748645f 272
23f87bed
MB
273(defvoo nntp-prepare-post-hook nil
274 "*Hook run just before posting an article. It is supposed to be used
275to insert Cancel-Lock headers.")
276
6b958814
G
277(defvoo nntp-server-list-active-group 'try
278 "If nil, then always use GROUP instead of LIST ACTIVE.
279This is usually slower, but on misconfigured servers that don't
280update their active files often, this can help.")
281
eec82323
LMI
282;;; Internal variables.
283
7e67562f 284(defvoo nntp-retrieval-in-progress nil)
6748645f
LMI
285(defvar nntp-record-commands nil
286 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
287
eec82323
LMI
288(defvar nntp-have-messaged nil)
289
290(defvar nntp-process-wait-for nil)
291(defvar nntp-process-to-buffer nil)
292(defvar nntp-process-callback nil)
293(defvar nntp-process-decode nil)
294(defvar nntp-process-start-point nil)
295(defvar nntp-inside-change-function nil)
6748645f
LMI
296(defvoo nntp-last-command-time nil)
297(defvoo nntp-last-command nil)
298(defvoo nntp-authinfo-password nil)
299(defvoo nntp-authinfo-user nil)
01c52d31 300(defvoo nntp-authinfo-force nil)
eec82323
LMI
301
302(defvar nntp-connection-list nil)
303
304(defvoo nntp-server-type nil)
305(defvoo nntp-connection-alist nil)
306(defvoo nntp-status-string "")
307(defconst nntp-version "nntp 5.0")
308(defvoo nntp-inhibit-erase nil)
309(defvoo nntp-inhibit-output nil)
310
311(defvoo nntp-server-xover 'try)
eec82323 312
16409b0b
GM
313(defvar nntp-async-timer nil)
314(defvar nntp-async-process-list nil)
315
58090a8d 316(defvar nntp-authinfo-rejected nil
c9fc72fa
LMI
317"A custom error condition used to report 'Authentication Rejected' errors.
318Condition handlers that match just this condition ensure that the nntp
58090a8d
MB
319backend doesn't catch this error.")
320(put 'nntp-authinfo-rejected 'error-conditions '(error nntp-authinfo-rejected))
321(put 'nntp-authinfo-rejected 'error-message "Authorization Rejected")
322
eec82323
LMI
323\f
324
325;;; Internal functions.
326
327(defsubst nntp-send-string (process string)
328 "Send STRING to PROCESS."
6748645f
LMI
329 ;; We need to store the time to provide timeouts, and
330 ;; to store the command so the we can replay the command
331 ;; if the server gives us an AUTHINFO challenge.
332 (setq nntp-last-command-time (current-time)
333 nntp-last-command string)
334 (when nntp-record-commands
335 (nntp-record-command string))
23f87bed
MB
336 (process-send-string process (concat string nntp-end-of-line))
337 (or (memq (process-status process) '(open run))
338 (nntp-report "Server closed connection")))
eec82323 339
6748645f
LMI
340(defun nntp-record-command (string)
341 "Record the command STRING."
ed075cb4 342 (with-current-buffer (get-buffer-create "*nntp-log*")
6748645f 343 (goto-char (point-max))
240a298f
PE
344 (insert (format-time-string "%Y%m%dT%H%M%S.%3N")
345 " " nntp-address " " string "\n")))
6748645f 346
2612a3d6
SM
347(defvar nntp--report-1 nil)
348
23f87bed
MB
349(defun nntp-report (&rest args)
350 "Report an error from the nntp backend. The first string in ARGS
351can be a format string. For some commands, the failed command may be
352retried once before actually displaying the error report."
2612a3d6
SM
353 (if nntp--report-1
354 (progn
355 ;; Throw out to nntp-with-open-group-error so that the connection may
356 ;; be restored and the command retried."
357 (when nntp-record-commands
358 (nntp-record-command "*** CONNECTION LOST ***"))
359 (throw 'nntp-with-open-group-error t))
23f87bed 360
2612a3d6
SM
361 (when nntp-record-commands
362 (nntp-record-command "*** CALLED nntp-report ***"))
23f87bed 363
2612a3d6 364 (nnheader-report 'nntp args)
23f87bed 365
2612a3d6 366 (apply 'error args)))
23f87bed 367
1428d46b
MB
368(defmacro nntp-copy-to-buffer (buffer start end)
369 "Copy string from unibyte current buffer to multibyte buffer."
370 (if (featurep 'xemacs)
371 `(copy-to-buffer ,buffer ,start ,end)
372 `(let ((string (buffer-substring ,start ,end)))
373 (with-current-buffer ,buffer
374 (erase-buffer)
375 (insert (if enable-multibyte-characters
376 (mm-string-to-multibyte string)
377 string))
378 (goto-char (point-min))
379 nil))))
380
eec82323
LMI
381(defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
382 "Wait for WAIT-FOR to arrive from PROCESS."
01c52d31 383
ed075cb4 384 (with-current-buffer (process-buffer process)
eec82323 385 (goto-char (point-min))
01c52d31 386
6748645f 387 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
58090a8d 388 (looking-at "48[02]"))
6748645f 389 (memq (process-status process) '(open run)))
58090a8d 390 (cond ((looking-at "480")
01c52d31 391 (nntp-handle-authinfo process))
58090a8d 392 ((looking-at "482")
04db63bc
G
393 (nnheader-report 'nntp "%s"
394 (get 'nntp-authinfo-rejected 'error-message))
58090a8d
MB
395 (signal 'nntp-authinfo-rejected nil))
396 ((looking-at "^.*\n")
397 (delete-region (point) (progn (forward-line 1) (point)))))
eec82323
LMI
398 (nntp-accept-process-output process)
399 (goto-char (point-min)))
400 (prog1
6748645f
LMI
401 (cond
402 ((looking-at "[45]")
403 (progn
404 (nntp-snarf-error-message)
405 nil))
406 ((not (memq (process-status process) '(open run)))
23f87bed 407 (nntp-report "Server closed connection"))
6748645f 408 (t
eec82323 409 (goto-char (point-max))
23f87bed
MB
410 (let ((limit (point-min))
411 response)
eec82323 412 (while (not (re-search-backward wait-for limit t))
6748645f 413 (nntp-accept-process-output process)
eec82323
LMI
414 ;; We assume that whatever we wait for is less than 1000
415 ;; characters long.
416 (setq limit (max (- (point-max) 1000) (point-min)))
23f87bed
MB
417 (goto-char (point-max)))
418 (setq response (match-string 0))
419 (with-current-buffer nntp-server-buffer
420 (setq nntp-process-response response)))
eec82323
LMI
421 (nntp-decode-text (not decode))
422 (unless discard
ed075cb4 423 (with-current-buffer buffer
23f87bed 424 (goto-char (point-max))
9f5e78f7 425 (nnheader-insert-buffer-substring (process-buffer process))
eec82323
LMI
426 ;; Nix out "nntp reading...." message.
427 (when nntp-have-messaged
428 (setq nntp-have-messaged nil)
23f87bed
MB
429 (nnheader-message 5 ""))))
430 t))
eec82323
LMI
431 (unless discard
432 (erase-buffer)))))
433
16409b0b
GM
434(defun nntp-kill-buffer (buffer)
435 (when (buffer-name buffer)
1e91d506
G
436 (let ((process (get-buffer-process buffer)))
437 (when process
438 (delete-process process)))
16409b0b
GM
439 (kill-buffer buffer)
440 (nnheader-init-server-buffer)))
441
01c52d31
MB
442(defun nntp-erase-buffer (buffer)
443 "Erase contents of BUFFER."
444 (with-current-buffer buffer
445 (erase-buffer)))
446
eec82323
LMI
447(defsubst nntp-find-connection (buffer)
448 "Find the connection delivering to BUFFER."
449 (let ((alist nntp-connection-alist)
450 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
451 process entry)
23f87bed 452 (while (and alist (setq entry (pop alist)))
eec82323
LMI
453 (when (eq buffer (cadr entry))
454 (setq process (car entry)
455 alist nil)))
456 (when process
457 (if (memq (process-status process) '(open run))
458 process
16409b0b 459 (nntp-kill-buffer (process-buffer process))
eec82323
LMI
460 (setq nntp-connection-alist (delq entry nntp-connection-alist))
461 nil))))
462
463(defsubst nntp-find-connection-entry (buffer)
464 "Return the entry for the connection to BUFFER."
465 (assq (nntp-find-connection buffer) nntp-connection-alist))
466
467(defun nntp-find-connection-buffer (buffer)
468 "Return the process connection buffer tied to BUFFER."
469 (let ((process (nntp-find-connection buffer)))
470 (when process
471 (process-buffer process))))
472
473(defsubst nntp-retrieve-data (command address port buffer
6748645f 474 &optional wait-for callback decode)
eec82323
LMI
475 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
476 (let ((process (or (nntp-find-connection buffer)
477 (nntp-open-connection buffer))))
23f87bed
MB
478 (if process
479 (progn
480 (unless (or nntp-inhibit-erase nnheader-callback-function)
01c52d31 481 (nntp-erase-buffer (process-buffer process)))
23f87bed
MB
482 (condition-case err
483 (progn
484 (when command
485 (nntp-send-string process command))
486 (cond
487 ((eq callback 'ignore)
488 t)
489 ((and callback wait-for)
490 (nntp-async-wait process wait-for buffer decode callback)
491 t)
492 (wait-for
493 (nntp-wait-for process wait-for buffer decode))
494 (t t)))
58090a8d
MB
495 (nntp-authinfo-rejected
496 (signal 'nntp-authinfo-rejected (cdr err)))
23f87bed
MB
497 (error
498 (nnheader-report 'nntp "Couldn't open connection to %s: %s"
499 address err))
500 (quit
501 (message "Quit retrieving data from nntp")
502 (signal 'quit nil)
503 nil)))
504 (nnheader-report 'nntp "Couldn't open connection to %s" address))))
eec82323
LMI
505
506(defsubst nntp-send-command (wait-for &rest strings)
507 "Send STRINGS to server and wait until WAIT-FOR returns."
508 (when (and (not nnheader-callback-function)
509 (not nntp-inhibit-output))
01c52d31 510 (nntp-erase-buffer nntp-server-buffer))
23f87bed
MB
511 (let* ((command (mapconcat 'identity strings " "))
512 (process (nntp-find-connection nntp-server-buffer))
513 (buffer (and process (process-buffer process)))
514 (pos (and buffer (with-current-buffer buffer (point)))))
515 (if process
516 (prog1
517 (nntp-retrieve-data command
518 nntp-address nntp-port-number
519 nntp-server-buffer
520 wait-for nnheader-callback-function)
521 ;; If nothing to wait for, still remove possibly echo'ed commands.
45cb30ee
MB
522 ;; We don't have echoes if `nntp-never-echoes-commands' is non-nil
523 ;; or the value of `nntp-open-connection-function' is in
524 ;; `nntp-open-connection-functions-never-echo-commands', so we
525 ;; skip this in that cases.
23f87bed 526 (unless (or wait-for
45cb30ee
MB
527 nntp-never-echoes-commands
528 (memq
529 nntp-open-connection-function
530 nntp-open-connection-functions-never-echo-commands))
23f87bed 531 (nntp-accept-response)
ed075cb4 532 (with-current-buffer buffer
23f87bed
MB
533 (goto-char pos)
534 (if (looking-at (regexp-quote command))
535 (delete-region pos (progn (forward-line 1)
01c52d31 536 (point-at-bol)))))))
23f87bed
MB
537 (nnheader-report 'nntp "Couldn't open connection to %s."
538 nntp-address))))
eec82323
LMI
539
540(defun nntp-send-command-nodelete (wait-for &rest strings)
541 "Send STRINGS to server and wait until WAIT-FOR returns."
23f87bed
MB
542 (let* ((command (mapconcat 'identity strings " "))
543 (process (nntp-find-connection nntp-server-buffer))
544 (buffer (and process (process-buffer process)))
545 (pos (and buffer (with-current-buffer buffer (point)))))
546 (if process
547 (prog1
548 (nntp-retrieve-data command
549 nntp-address nntp-port-number
550 nntp-server-buffer
551 wait-for nnheader-callback-function)
552 ;; If nothing to wait for, still remove possibly echo'ed commands
553 (unless wait-for
554 (nntp-accept-response)
ed075cb4 555 (with-current-buffer buffer
23f87bed
MB
556 (goto-char pos)
557 (if (looking-at (regexp-quote command))
558 (delete-region pos (progn (forward-line 1)
01c52d31 559 (point-at-bol)))))))
23f87bed
MB
560 (nnheader-report 'nntp "Couldn't open connection to %s."
561 nntp-address))))
eec82323
LMI
562
563(defun nntp-send-command-and-decode (wait-for &rest strings)
564 "Send STRINGS to server and wait until WAIT-FOR returns."
565 (when (and (not nnheader-callback-function)
566 (not nntp-inhibit-output))
01c52d31 567 (nntp-erase-buffer nntp-server-buffer))
23f87bed
MB
568 (let* ((command (mapconcat 'identity strings " "))
569 (process (nntp-find-connection nntp-server-buffer))
570 (buffer (and process (process-buffer process)))
571 (pos (and buffer (with-current-buffer buffer (point)))))
572 (if process
573 (prog1
574 (nntp-retrieve-data command
575 nntp-address nntp-port-number
576 nntp-server-buffer
577 wait-for nnheader-callback-function t)
578 ;; If nothing to wait for, still remove possibly echo'ed commands
579 (unless wait-for
580 (nntp-accept-response)
ed075cb4 581 (with-current-buffer buffer
01c52d31
MB
582 (goto-char pos)
583 (if (looking-at (regexp-quote command))
584 (delete-region pos (progn (forward-line 1) (point-at-bol))))
585 )))
23f87bed
MB
586 (nnheader-report 'nntp "Couldn't open connection to %s."
587 nntp-address))))
588
eec82323
LMI
589
590(defun nntp-send-buffer (wait-for)
591 "Send the current buffer to server and wait until WAIT-FOR returns."
592 (when (and (not nnheader-callback-function)
593 (not nntp-inhibit-output))
01c52d31
MB
594 (nntp-erase-buffer
595 (nntp-find-connection-buffer nntp-server-buffer)))
eec82323 596 (nntp-encode-text)
ed075cb4
SM
597 ;; Make sure we did not forget to encode some of the content.
598 (assert (save-excursion (goto-char (point-min))
599 (not (re-search-forward "[^\000-\377]" nil t))))
600 (mm-disable-multibyte)
601 (process-send-region (nntp-find-connection nntp-server-buffer)
602 (point-min) (point-max))
eec82323
LMI
603 (nntp-retrieve-data
604 nil nntp-address nntp-port-number nntp-server-buffer
605 wait-for nnheader-callback-function))
606
607\f
608
609;;; Interface functions.
610
611(nnoo-define-basics nntp)
612
6748645f
LMI
613(defsubst nntp-next-result-arrived-p ()
614 (cond
615 ;; A result that starts with a 2xx code is terminated by
616 ;; a line with only a "." on it.
16409b0b 617 ((eq (char-after) ?2)
6748645f 618 (if (re-search-forward "\n\\.\r?\n" nil t)
01c52d31
MB
619 (progn
620 ;; Some broken news servers add another dot at the end.
621 ;; Protect against inflooping there.
622 (while (looking-at "^\\.\r?\n")
623 (forward-line 1))
624 t)
6748645f
LMI
625 nil))
626 ;; A result that starts with a 3xx or 4xx code is terminated
627 ;; by a newline.
628 ((looking-at "[34]")
629 (if (search-forward "\n" nil t)
630 t
631 nil))
632 ;; No result here.
633 (t
634 nil)))
635
ed075cb4
SM
636(defun nntp-with-open-group-function (-group -server -connectionless -bodyfun)
637 "Protect against servers that don't like clients that keep idle connections opens.
638The problem being that these servers may either close a connection or
639simply ignore any further requests on a connection. Closed
640connections are not detected until `accept-process-output' has updated
641the `process-status'. Dropped connections are not detected until the
642connection timeouts (which may be several minutes) or
643`nntp-connection-timeout' has expired. When these occur
644`nntp-with-open-group', opens a new connection then re-issues the NNTP
645command whose response triggered the error."
2612a3d6
SM
646 (let ((nntp-report-n nntp--report-1)
647 (nntp--report-1 t)
648 (nntp-with-open-group-internal nil))
ed075cb4
SM
649 (while (catch 'nntp-with-open-group-error
650 ;; Open the connection to the server
651 ;; NOTE: Existing connections are NOT tested.
652 (nntp-possibly-change-group -group -server -connectionless)
653
654 (let ((-timer
655 (and nntp-connection-timeout
656 (run-at-time
657 nntp-connection-timeout nil
658 (lambda ()
659 (let* ((-process (nntp-find-connection
660 nntp-server-buffer))
661 (-buffer (and -process
662 (process-buffer -process))))
663 ;; When I an able to identify the
664 ;; connection to the server AND I've
c80e3b4a 665 ;; received NO response for
ed075cb4
SM
666 ;; nntp-connection-timeout seconds.
667 (when (and -buffer (eq 0 (buffer-size -buffer)))
668 ;; Close the connection. Take no
669 ;; other action as the accept input
670 ;; code will handle the closed
671 ;; connection.
672 (nntp-kill-buffer -buffer))))))))
673 (unwind-protect
674 (setq nntp-with-open-group-internal
675 (condition-case nil
676 (funcall -bodyfun)
677 (quit
678 (unless debug-on-quit
679 (nntp-close-server))
680 (signal 'quit nil))))
681 (when -timer
682 (nnheader-cancel-timer -timer)))
683 nil))
2612a3d6 684 (setq nntp--report-1 nntp-report-n))
ed075cb4
SM
685 nntp-with-open-group-internal))
686
23f87bed
MB
687(defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
688 "Protect against servers that don't like clients that keep idle connections opens.
689The problem being that these servers may either close a connection or
690simply ignore any further requests on a connection. Closed
ed075cb4
SM
691connections are not detected until `accept-process-output' has updated
692the `process-status'. Dropped connections are not detected until the
23f87bed 693connection timeouts (which may be several minutes) or
ed075cb4
SM
694`nntp-connection-timeout' has expired. When these occur
695`nntp-with-open-group', opens a new connection then re-issues the NNTP
23f87bed 696command whose response triggered the error."
ed075cb4 697 (declare (indent 2) (debug (form form [&optional symbolp] def-body)))
23f87bed
MB
698 (when (and (listp connectionless)
699 (not (eq connectionless nil)))
700 (setq forms (cons connectionless forms)
701 connectionless nil))
ed075cb4 702 `(nntp-with-open-group-function ,group ,server ,connectionless (lambda () ,@forms)))
23f87bed 703
eec82323
LMI
704(deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
705 "Retrieve the headers of ARTICLES."
23f87bed
MB
706 (nntp-with-open-group
707 group server
ed075cb4 708 (with-current-buffer (nntp-find-connection-buffer nntp-server-buffer)
23f87bed
MB
709 (erase-buffer)
710 (if (and (not gnus-nov-is-evil)
711 (not nntp-nov-is-evil)
712 (nntp-retrieve-headers-with-xover articles fetch-old))
713 ;; We successfully retrieved the headers via XOVER.
714 'nov
715 ;; XOVER didn't work, so we do it the hard, slow and inefficient
716 ;; way.
717 (let ((number (length articles))
718 (articles articles)
719 (count 0)
720 (received 0)
721 (last-point (point-min))
722 (buf (nntp-find-connection-buffer nntp-server-buffer))
723 (nntp-inhibit-erase t)
724 article)
725 ;; Send HEAD commands.
726 (while (setq article (pop articles))
727 (nntp-send-command
728 nil
729 "HEAD" (if (numberp article)
730 (int-to-string article)
731 ;; `articles' is either a list of article numbers
732 ;; or a list of article IDs.
733 article))
734 (incf count)
735 ;; Every 400 requests we have to read the stream in
736 ;; order to avoid deadlocks.
737 (when (or (null articles) ;All requests have been sent.
738 (zerop (% count nntp-maximum-request)))
739 (nntp-accept-response)
740 (while (progn
741 (set-buffer buf)
742 (goto-char last-point)
743 ;; Count replies.
744 (while (nntp-next-result-arrived-p)
745 (setq last-point (point))
746 (incf received))
747 (< received count))
748 ;; If number of headers is greater than 100, give
749 ;; informative messages.
750 (and (numberp nntp-large-newsgroup)
751 (> number nntp-large-newsgroup)
752 (zerop (% received 20))
753 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
754 (/ (* received 100) number)))
755 (nntp-accept-response))))
756 (and (numberp nntp-large-newsgroup)
757 (> number nntp-large-newsgroup)
758 (nnheader-message 6 "NNTP: Receiving headers...done"))
759
760 ;; Now all of replies are received. Fold continuation lines.
761 (nnheader-fold-continuation-lines)
762 ;; Remove all "\r"'s.
763 (nnheader-strip-cr)
1428d46b 764 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
23f87bed 765 'headers)))))
eec82323 766
5415d076 767(deffoo nntp-retrieve-group-data-early (server infos)
1d88c091
LI
768 "Retrieve group info on INFOS."
769 (nntp-with-open-group nil server
7e67562f 770 (let ((buffer (nntp-find-connection-buffer nntp-server-buffer)))
964646c4
LI
771 (unless infos
772 (with-current-buffer buffer
773 (setq nntp-retrieval-in-progress nil)))
7e67562f 774 (when (and buffer
964646c4 775 infos
7e67562f
G
776 (with-current-buffer buffer
777 (not nntp-retrieval-in-progress)))
778 ;; The first time this is run, this variable is `try'. So we
779 ;; try.
780 (when (eq nntp-server-list-active-group 'try)
781 (nntp-try-list-active
782 (gnus-group-real-name (gnus-info-group (car infos)))))
783 (with-current-buffer buffer
784 (erase-buffer)
785 ;; Mark this buffer as "in use" in case we try to issue two
786 ;; retrievals from the same server. This shouldn't happen,
787 ;; so this is mostly a sanity check.
788 (setq nntp-retrieval-in-progress t)
789 (let ((nntp-inhibit-erase t)
790 (command (if nntp-server-list-active-group
791 "LIST ACTIVE" "GROUP")))
792 (dolist (info infos)
793 (nntp-send-command
794 nil command (gnus-group-real-name (gnus-info-group info)))))
795 (length infos))))))
1d88c091 796
5415d076 797(deffoo nntp-finish-retrieve-group-infos (server infos count)
1d88c091
LI
798 (nntp-with-open-group nil server
799 (let ((buf (nntp-find-connection-buffer nntp-server-buffer))
800 (method (gnus-find-method-for-group
801 (gnus-info-group (car infos))
802 (car infos)))
803 (received 0)
804 (last-point 1))
7e67562f
G
805 (with-current-buffer buf
806 (setq nntp-retrieval-in-progress nil))
76b8d9dd
LI
807 (when (and buf
808 count)
1d88c091
LI
809 (with-current-buffer buf
810 (while (and (gnus-buffer-live-p buf)
811 (progn
812 (goto-char last-point)
813 ;; Count replies.
06b840e0
LI
814 (while (re-search-forward
815 (if nntp-server-list-active-group
816 "^[.]"
817 "^[0-9]")
818 nil t)
1d88c091
LI
819 (incf received))
820 (setq last-point (point))
821 (< received count)))
822 (nntp-accept-response))
823 ;; We now have all the entries. Remove CRs.
5415d076 824 (nnheader-strip-cr)
1d88c091
LI
825 (if (not nntp-server-list-active-group)
826 (progn
827 (nntp-copy-to-buffer nntp-server-buffer
828 (point-min) (point-max))
829 (gnus-groups-to-gnus-format method gnus-active-hashtb t))
830 ;; We have read active entries, so we just delete the
831 ;; superfluous gunk.
832 (goto-char (point-min))
833 (while (re-search-forward "^[.2-5]" nil t)
834 (delete-region (match-beginning 0)
835 (progn (forward-line 1) (point))))
836 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
5415d076 837 (with-current-buffer nntp-server-buffer
06b840e0
LI
838 (gnus-active-to-gnus-format
839 ;; Kludge to use the extended method name if you have
840 ;; an extended one.
841 (if (consp (gnus-info-method (car infos)))
842 (gnus-info-method (car infos))
843 method)
844 gnus-active-hashtb nil t))))))))
1d88c091 845
eec82323
LMI
846(deffoo nntp-retrieve-groups (groups &optional server)
847 "Retrieve group info on GROUPS."
23f87bed
MB
848 (nntp-with-open-group
849 nil server
e7719ea1
G
850 (when (and (nntp-find-connection-buffer nntp-server-buffer)
851 (with-current-buffer
852 (nntp-find-connection-buffer nntp-server-buffer)
853 (if (not nntp-retrieval-in-progress)
854 t
855 (message "Warning: Refusing to do retrieval from %s because a retrieval is already happening"
856 server)
857 nil)))
23f87bed
MB
858 (catch 'done
859 (save-excursion
860 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
01c52d31 861 (nntp-erase-buffer nntp-server-buffer)
23f87bed
MB
862 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
863 ;; The first time this is run, this variable is `try'. So we
864 ;; try.
865 (when (eq nntp-server-list-active-group 'try)
866 (nntp-try-list-active (car groups)))
867 (erase-buffer)
868 (let ((count 0)
869 (groups groups)
870 (received 0)
871 (last-point (point-min))
872 (nntp-inhibit-erase t)
873 (buf (nntp-find-connection-buffer nntp-server-buffer))
874 (command (if nntp-server-list-active-group
875 "LIST ACTIVE" "GROUP")))
876 (while groups
877 ;; Timeout may have killed the buffer.
878 (unless (gnus-buffer-live-p buf)
879 (nnheader-report 'nntp "Connection to %s is closed." server)
880 (throw 'done nil))
881 ;; Send the command to the server.
882 (nntp-send-command nil command (pop groups))
883 (incf count)
884 ;; Every 400 requests we have to read the stream in
885 ;; order to avoid deadlocks.
886 (when (or (null groups) ;All requests have been sent.
887 (zerop (% count nntp-maximum-request)))
888 (nntp-accept-response)
889 (while (and (gnus-buffer-live-p buf)
890 (progn
891 ;; Search `blue moon' in this file for the
892 ;; reason why set-buffer here.
893 (set-buffer buf)
894 (goto-char last-point)
895 ;; Count replies.
896 (while (re-search-forward "^[0-9]" nil t)
897 (incf received))
898 (setq last-point (point))
899 (< received count)))
900 (nntp-accept-response))))
901
902 ;; Wait for the reply from the final command.
903 (unless (gnus-buffer-live-p buf)
904 (nnheader-report 'nntp "Connection to %s is closed." server)
905 (throw 'done nil))
906 (set-buffer buf)
907 (goto-char (point-max))
908 (re-search-backward "^[0-9]" nil t)
909 (when (looking-at "^[23]")
910 (while (and (gnus-buffer-live-p buf)
911 (progn
912 (set-buffer buf)
913 (goto-char (point-max))
914 (if (not nntp-server-list-active-group)
915 (not (re-search-backward "\r?\n"
916 (- (point) 3) t))
917 (not (re-search-backward "^\\.\r?\n"
918 (- (point) 4) t)))))
919 (nntp-accept-response)))
920
921 ;; Now all replies are received. We remove CRs.
922 (unless (gnus-buffer-live-p buf)
923 (nnheader-report 'nntp "Connection to %s is closed." server)
924 (throw 'done nil))
925 (set-buffer buf)
926 (goto-char (point-min))
927 (while (search-forward "\r" nil t)
928 (replace-match "" t t))
929
930 (if (not nntp-server-list-active-group)
931 (progn
1428d46b
MB
932 (nntp-copy-to-buffer nntp-server-buffer
933 (point-min) (point-max))
23f87bed
MB
934 'group)
935 ;; We have read active entries, so we just delete the
936 ;; superfluous gunk.
937 (goto-char (point-min))
938 (while (re-search-forward "^[.2-5]" nil t)
939 (delete-region (match-beginning 0)
940 (progn (forward-line 1) (point))))
1428d46b 941 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
23f87bed 942 'active)))))))
eec82323
LMI
943
944(deffoo nntp-retrieve-articles (articles &optional group server)
23f87bed
MB
945 (nntp-with-open-group
946 group server
947 (save-excursion
948 (let ((number (length articles))
949 (articles articles)
950 (count 0)
951 (received 0)
952 (last-point (point-min))
953 (buf (nntp-find-connection-buffer nntp-server-buffer))
954 (nntp-inhibit-erase t)
955 (map (apply 'vector articles))
956 (point 1)
957 article)
958 (set-buffer buf)
959 (erase-buffer)
960 ;; Send ARTICLE command.
961 (while (setq article (pop articles))
962 (nntp-send-command
963 nil
964 "ARTICLE" (if (numberp article)
965 (int-to-string article)
966 ;; `articles' is either a list of article numbers
967 ;; or a list of article IDs.
968 article))
969 (incf count)
970 ;; Every 400 requests we have to read the stream in
971 ;; order to avoid deadlocks.
972 (when (or (null articles) ;All requests have been sent.
973 (zerop (% count nntp-maximum-request)))
974 (nntp-accept-response)
975 (while (progn
976 (set-buffer buf)
977 (goto-char last-point)
978 ;; Count replies.
979 (while (nntp-next-result-arrived-p)
980 (aset map received (cons (aref map received) (point)))
981 (setq last-point (point))
982 (incf received))
983 (< received count))
984 ;; If number of headers is greater than 100, give
985 ;; informative messages.
986 (and (numberp nntp-large-newsgroup)
987 (> number nntp-large-newsgroup)
988 (zerop (% received 20))
989 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
990 (/ (* received 100) number)))
991 (nntp-accept-response))))
992 (and (numberp nntp-large-newsgroup)
993 (> number nntp-large-newsgroup)
994 (nnheader-message 6 "NNTP: Receiving articles...done"))
995
996 ;; Now we have all the responses. We go through the results,
997 ;; wash it and copy it over to the server buffer.
998 (set-buffer nntp-server-buffer)
999 (erase-buffer)
1000 (setq last-point (point-min))
1001 (mapcar
1002 (lambda (entry)
1003 (narrow-to-region
1004 (setq point (goto-char (point-max)))
1005 (progn
9f5e78f7 1006 (nnheader-insert-buffer-substring buf last-point (cdr entry))
23f87bed
MB
1007 (point-max)))
1008 (setq last-point (cdr entry))
1009 (nntp-decode-text)
1010 (widen)
1011 (cons (car entry) point))
1012 map)))))
eec82323 1013
eec82323
LMI
1014(defun nntp-try-list-active (group)
1015 (nntp-list-active-group group)
ed075cb4 1016 (with-current-buffer nntp-server-buffer
eec82323
LMI
1017 (goto-char (point-min))
1018 (cond ((or (eobp)
1019 (looking-at "5[0-9]+"))
1020 (setq nntp-server-list-active-group nil))
1021 (t
1022 (setq nntp-server-list-active-group t)))))
1023
1024(deffoo nntp-list-active-group (group &optional server)
16409b0b 1025 "Return the active info on GROUP (which can be a regexp)."
23f87bed
MB
1026 (nntp-with-open-group
1027 nil server
1028 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
16409b0b
GM
1029
1030(deffoo nntp-request-group-articles (group &optional server)
1031 "Return the list of existing articles in GROUP."
23f87bed
MB
1032 (nntp-with-open-group
1033 nil server
1034 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
eec82323
LMI
1035
1036(deffoo nntp-request-article (article &optional group server buffer command)
23f87bed 1037 (nntp-with-open-group
b87f32fc 1038 group server
23f87bed
MB
1039 (when (nntp-send-command-and-decode
1040 "\r?\n\\.\r?\n" "ARTICLE"
1041 (if (numberp article) (int-to-string article) article))
b87f32fc
G
1042 (when (and buffer
1043 (not (equal buffer nntp-server-buffer)))
1044 (with-current-buffer nntp-server-buffer
1045 (copy-to-buffer buffer (point-min) (point-max))))
1046 (nntp-find-group-and-number group))))
eec82323
LMI
1047
1048(deffoo nntp-request-head (article &optional group server)
23f87bed
MB
1049 (nntp-with-open-group
1050 group server
1051 (when (nntp-send-command
1052 "\r?\n\\.\r?\n" "HEAD"
1053 (if (numberp article) (int-to-string article) article))
1054 (prog1
1055 (nntp-find-group-and-number group)
1056 (nntp-decode-text)))))
eec82323
LMI
1057
1058(deffoo nntp-request-body (article &optional group server)
23f87bed
MB
1059 (nntp-with-open-group
1060 group server
1061 (nntp-send-command-and-decode
1062 "\r?\n\\.\r?\n" "BODY"
1063 (if (numberp article) (int-to-string article) article))))
eec82323 1064
286c4fc2 1065(deffoo nntp-request-group (group &optional server dont-check info)
e84b4b86 1066 (nntp-with-open-group
23f87bed
MB
1067 nil server
1068 (when (nntp-send-command "^[245].*\n" "GROUP" group)
1069 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1070 (setcar (cddr entry) group)))))
eec82323
LMI
1071
1072(deffoo nntp-close-group (group &optional server)
1073 t)
1074
1075(deffoo nntp-server-opened (&optional server)
1076 "Say whether a connection to SERVER has been opened."
1077 (and (nnoo-current-server-p 'nntp server)
1078 nntp-server-buffer
1079 (gnus-buffer-live-p nntp-server-buffer)
1080 (nntp-find-connection nntp-server-buffer)))
1081
1082(deffoo nntp-open-server (server &optional defs connectionless)
1083 (nnheader-init-server-buffer)
1084 (if (nntp-server-opened server)
1085 t
1086 (when (or (stringp (car defs))
1087 (numberp (car defs)))
1088 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
1089 (unless (assq 'nntp-address defs)
1090 (setq defs (append defs (list (list 'nntp-address server)))))
1091 (nnoo-change-server 'nntp server defs)
20a673b2
KY
1092 (if connectionless
1093 t
eec82323
LMI
1094 (or (nntp-find-connection nntp-server-buffer)
1095 (nntp-open-connection nntp-server-buffer)))))
1096
1097(deffoo nntp-close-server (&optional server)
1098 (nntp-possibly-change-group nil server t)
6748645f
LMI
1099 (let ((process (nntp-find-connection nntp-server-buffer)))
1100 (while process
eec82323 1101 (when (memq (process-status process) '(open run))
6748645f
LMI
1102 (ignore-errors
1103 (nntp-send-string process "QUIT")
1104 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
1105 ;; Ok, this is evil, but when using telnet and stuff
1106 ;; as the connection method, it's important that the
1107 ;; QUIT command actually is sent out before we kill
1108 ;; the process.
1109 (sleep-for 1))))
16409b0b 1110 (nntp-kill-buffer (process-buffer process))
6748645f 1111 (setq process (car (pop nntp-connection-alist))))
eec82323
LMI
1112 (nnoo-close-server 'nntp)))
1113
1114(deffoo nntp-request-close ()
1115 (let (process)
1116 (while (setq process (pop nntp-connection-list))
1117 (when (memq (process-status process) '(open run))
eec82323 1118 (ignore-errors
6748645f
LMI
1119 (nntp-send-string process "QUIT")
1120 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
1121 ;; Ok, this is evil, but when using telnet and stuff
1122 ;; as the connection method, it's important that the
1123 ;; QUIT command actually is sent out before we kill
1124 ;; the process.
1125 (sleep-for 1))))
16409b0b 1126 (nntp-kill-buffer (process-buffer process)))))
eec82323
LMI
1127
1128(deffoo nntp-request-list (&optional server)
23f87bed
MB
1129 (nntp-with-open-group
1130 nil server
1131 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
eec82323
LMI
1132
1133(deffoo nntp-request-list-newsgroups (&optional server)
23f87bed
MB
1134 (nntp-with-open-group
1135 nil server
1136 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
eec82323
LMI
1137
1138(deffoo nntp-request-newgroups (date &optional server)
23f87bed
MB
1139 (nntp-with-open-group
1140 nil server
ed075cb4 1141 (with-current-buffer nntp-server-buffer
23f87bed
MB
1142 (let* ((time (date-to-time date))
1143 (ls (- (cadr time) (nth 8 (decode-time time)))))
1144 (cond ((< ls 0)
1145 (setcar time (1- (car time)))
1146 (setcar (cdr time) (+ ls 65536)))
1147 ((>= ls 65536)
1148 (setcar time (1+ (car time)))
1149 (setcar (cdr time) (- ls 65536)))
1150 (t
1151 (setcar (cdr time) ls)))
1152 (prog1
1153 (nntp-send-command
1154 "^\\.\r?\n" "NEWGROUPS"
1155 (format-time-string "%y%m%d %H%M%S" time)
1156 "GMT")
1157 (nntp-decode-text))))))
eec82323
LMI
1158
1159(deffoo nntp-request-post (&optional server)
23f87bed
MB
1160 (nntp-with-open-group
1161 nil server
1162 (when (nntp-send-command "^[23].*\r?\n" "POST")
1163 (let ((response (with-current-buffer nntp-server-buffer
1164 nntp-process-response))
1165 server-id)
1166 (when (and response
1167 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
1168 response))
1169 (setq server-id (match-string 1 response))
1170 (narrow-to-region (goto-char (point-min))
1171 (if (search-forward "\n\n" nil t)
1172 (1- (point))
1173 (point-max)))
1174 (unless (mail-fetch-field "Message-ID")
1175 (goto-char (point-min))
1176 (insert "Message-ID: " server-id "\n"))
1177 (widen))
1178 (run-hooks 'nntp-prepare-post-hook)
1179 (nntp-send-buffer "^[23].*\n")))))
eec82323
LMI
1180
1181(deffoo nntp-request-type (group article)
1182 'news)
1183
1184(deffoo nntp-asynchronous-p ()
1185 t)
1186
01c52d31 1187(deffoo nntp-request-set-mark (group actions &optional server)
a93b858c
LMI
1188 (when (and (not nntp-marks-is-evil)
1189 nntp-marks-file-name)
01c52d31
MB
1190 (nntp-possibly-create-directory group server)
1191 (nntp-open-marks group server)
5f285722 1192 (setq nntp-marks (nnheader-update-marks-actions nntp-marks actions))
01c52d31
MB
1193 (nntp-save-marks group server))
1194 nil)
1195
b1ae92ba 1196(deffoo nntp-request-marks (group info &optional server)
a93b858c
LMI
1197 (when (and (not nntp-marks-is-evil)
1198 nntp-marks-file-name)
01c52d31
MB
1199 (nntp-possibly-create-directory group server)
1200 (when (nntp-marks-changed-p group server)
1201 (nnheader-message 8 "Updating marks for %s..." group)
1202 (nntp-open-marks group server)
1203 ;; Update info using `nntp-marks'.
1204 (mapc (lambda (pred)
1205 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1206 (gnus-info-set-marks
1207 info
1208 (gnus-update-alist-soft
1209 (cdr pred)
1210 (cdr (assq (cdr pred) nntp-marks))
1211 (gnus-info-marks info))
1212 t)))
1213 gnus-article-mark-lists)
1214 (let ((seen (cdr (assq 'read nntp-marks))))
1215 (gnus-info-set-read info
1216 (if (and (integerp (car seen))
1217 (null (cdr seen)))
1218 (list (cons (car seen) (car seen)))
1219 seen)))
1220 (nnheader-message 8 "Updating marks for %s...done" group)))
1221 nil)
1222
1223
1224
eec82323
LMI
1225;;; Hooky functions.
1226
1227(defun nntp-send-mode-reader ()
1228 "Send the MODE READER command to the nntp server.
1229This function is supposed to be called from `nntp-server-opened-hook'.
1230It will make innd servers spawn an nnrpd process to allow actual article
1231reading."
16409b0b 1232 (nntp-send-command "^.*\n" "MODE READER"))
eec82323 1233
c8ccffb1 1234(declare-function netrc-parse "netrc" (&optional file))
aa8f8277
GM
1235(declare-function netrc-machine "netrc"
1236 (list machine &optional port defaultport))
1237(declare-function netrc-get "netrc" (alist type))
1238
6748645f 1239(defun nntp-send-authinfo (&optional send-if-force)
eec82323 1240 "Send the AUTHINFO to the nntp server.
6748645f
LMI
1241It will look in the \"~/.authinfo\" file for matching entries. If
1242nothing suitable is found there, it will prompt for a user name
1243and a password.
1244
1245If SEND-IF-FORCE, only send authinfo to the server if the
1246.authinfo file has the FORCE token."
aa8f8277 1247 (require 'netrc)
01c52d31
MB
1248 (let* ((list (netrc-parse nntp-authinfo-file))
1249 (alist (netrc-machine list nntp-address "nntp"))
b8e0f0cd 1250 (auth-info
50fc698e
KY
1251 (nth 0 (auth-source-search
1252 :max 1
b63e0a68
KY
1253 :host (list nntp-address (nnoo-current-server 'nntp))
1254 :port `("119" "nntp" ,(format "%s" nntp-port-number)
1255 "563" "nntps" "snews"))))
b8e0f0cd 1256 (auth-user (plist-get auth-info :user))
638f517c 1257 (auth-force (plist-get auth-info :force))
b8e0f0cd
G
1258 (auth-passwd (plist-get auth-info :secret))
1259 (auth-passwd (if (functionp auth-passwd)
1260 (funcall auth-passwd)
1261 auth-passwd))
638f517c
TZ
1262 (force (or (netrc-get alist "force")
1263 nntp-authinfo-force
1264 auth-force))
8abf1b22 1265 (user (or
e952b711 1266 ;; this is preferred to netrc-*
3b36c17e 1267 auth-user
8abf1b22 1268 (netrc-get alist "login")
e952b711
MB
1269 nntp-authinfo-user))
1270 (passwd (or
1271 ;; this is preferred to netrc-*
3b36c17e 1272 auth-passwd
e952b711 1273 (netrc-get alist "password"))))
6748645f
LMI
1274 (when (or (not send-if-force)
1275 force)
1276 (unless user
1277 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1278 nntp-authinfo-user user))
1279 (unless (member user '(nil ""))
1280 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1281 (when t ;???Should check if AUTHINFO succeeded
16409b0b
GM
1282 (nntp-send-command
1283 "^2.*\r?\n" "AUTHINFO PASS"
1284 (or passwd
1285 nntp-authinfo-password
1286 (setq nntp-authinfo-password
23f87bed
MB
1287 (read-passwd (format "NNTP (%s@%s) password: "
1288 user nntp-address))))))))))
6748645f
LMI
1289
1290(defun nntp-send-nosy-authinfo ()
1291 "Send the AUTHINFO to the nntp server."
1292 (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
1293 (unless (member user '(nil ""))
1294 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1295 (when t ;???Should check if AUTHINFO succeeded
1296 (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
23f87bed
MB
1297 (read-passwd (format "NNTP (%s@%s) password: "
1298 user nntp-address)))))))
eec82323
LMI
1299
1300(defun nntp-send-authinfo-from-file ()
1301 "Send the AUTHINFO to the nntp server.
6748645f
LMI
1302
1303The authinfo login name is taken from the user's login name and the
1304password contained in '~/.nntp-authinfo'."
eec82323 1305 (when (file-exists-p "~/.nntp-authinfo")
16409b0b 1306 (with-temp-buffer
eec82323
LMI
1307 (insert-file-contents "~/.nntp-authinfo")
1308 (goto-char (point-min))
6748645f 1309 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
eec82323 1310 (nntp-send-command
6748645f 1311 "^2.*\r?\n" "AUTHINFO PASS"
01c52d31 1312 (buffer-substring (point) (point-at-eol))))))
eec82323
LMI
1313
1314;;; Internal functions.
1315
6748645f
LMI
1316(defun nntp-handle-authinfo (process)
1317 "Take care of an authinfo response from the server."
1318 (let ((last nntp-last-command))
1319 (funcall nntp-authinfo-function)
1320 ;; We have to re-send the function that was interrupted by
1321 ;; the authinfo request.
01c52d31 1322 (nntp-erase-buffer nntp-server-buffer)
6748645f
LMI
1323 (nntp-send-string process last)))
1324
eec82323
LMI
1325(defun nntp-make-process-buffer (buffer)
1326 "Create a new, fresh buffer usable for nntp process connections."
ed075cb4
SM
1327 (with-current-buffer
1328 (generate-new-buffer
1329 (format " *server %s %s %s*"
1330 nntp-address nntp-port-number
1331 (gnus-buffer-exists-p buffer)))
1428d46b 1332 (mm-disable-multibyte)
eec82323
LMI
1333 (set (make-local-variable 'after-change-functions) nil)
1334 (set (make-local-variable 'nntp-process-wait-for) nil)
1335 (set (make-local-variable 'nntp-process-callback) nil)
1336 (set (make-local-variable 'nntp-process-to-buffer) nil)
1337 (set (make-local-variable 'nntp-process-start-point) nil)
1338 (set (make-local-variable 'nntp-process-decode) nil)
7e67562f 1339 (set (make-local-variable 'nntp-retrieval-in-progress) nil)
eec82323
LMI
1340 (current-buffer)))
1341
1342(defun nntp-open-connection (buffer)
1343 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1344 (run-hooks 'nntp-prepare-server-hook)
1345 (let* ((pbuffer (nntp-make-process-buffer buffer))
6748645f
LMI
1346 (timer
1347 (and nntp-connection-timeout
01c52d31 1348 (run-at-time
6748645f
LMI
1349 nntp-connection-timeout nil
1350 `(lambda ()
16409b0b 1351 (nntp-kill-buffer ,pbuffer)))))
eec82323 1352 (process
ed797193 1353 (condition-case err
ccaab511 1354 (let ((coding-system-for-read nntp-coding-system-for-read)
ed797193 1355 (coding-system-for-write nntp-coding-system-for-write)
e742e117
CY
1356 (map '((nntp-open-network-stream network)
1357 (network-only plain) ; compat
1358 (nntp-open-plain-stream plain)
ed797193
G
1359 (nntp-open-ssl-stream tls)
1360 (nntp-open-tls-stream tls))))
1361 (if (assoc nntp-open-connection-function map)
f2eefd24
CY
1362 (open-protocol-stream
1363 "nntpd" pbuffer nntp-address nntp-port-number
e742e117 1364 :type (cadr (assoc nntp-open-connection-function map))
f2eefd24
CY
1365 :end-of-command "^\\([2345]\\|[.]\\).*\n"
1366 :capability-command "CAPABILITIES\r\n"
1367 :success "^3"
1368 :starttls-function
1369 (lambda (capabilities)
1370 (if (not (string-match "STARTTLS" capabilities))
1371 nil
1372 "STARTTLS\r\n")))
ed797193
G
1373 (funcall nntp-open-connection-function pbuffer)))
1374 (error
f2eefd24 1375 (nnheader-report 'nntp ">>> %s" err))
2eebe218 1376 (quit
01c52d31 1377 (message "Quit opening connection to %s" nntp-address)
2eebe218
DL
1378 (nntp-kill-buffer pbuffer)
1379 (signal 'quit nil)
1380 nil))))
6748645f
LMI
1381 (when timer
1382 (nnheader-cancel-timer timer))
f2eefd24
CY
1383 (when (and process
1384 (not (memq (process-status process) '(open run))))
02e8d7e9
LI
1385 (with-current-buffer pbuffer
1386 (goto-char (point-min))
1387 (nnheader-report 'nntp "Error when connecting: %s"
1388 (buffer-substring (point) (line-end-position))))
f2eefd24 1389 (setq process nil))
23f87bed
MB
1390 (unless process
1391 (nntp-kill-buffer pbuffer))
6748645f
LMI
1392 (when (and (buffer-name pbuffer)
1393 process)
7887e229
KY
1394 (when (and (fboundp 'set-network-process-option) ;; Unavailable in XEmacs.
1395 (fboundp 'process-type) ;; Emacs 22 doesn't provide it.
68b5d5ee
SM
1396 (eq (process-type process) 'network))
1397 ;; Use TCP-keepalive so that connections that pass through a NAT router
1398 ;; don't hang when left idle.
1399 (set-network-process-option process :keepalive t))
4a43ee9b 1400 (gnus-set-process-query-on-exit-flag process nil)
23f87bed
MB
1401 (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1402 (memq (process-status process) '(open run)))
eec82323
LMI
1403 (prog1
1404 (caar (push (list process buffer nil) nntp-connection-alist))
1405 (push process nntp-connection-list)
ed075cb4 1406 (with-current-buffer pbuffer
eec82323
LMI
1407 (nntp-read-server-type)
1408 (erase-buffer)
1409 (set-buffer nntp-server-buffer)
1410 (let ((nnheader-callback-function nil))
6748645f
LMI
1411 (run-hooks 'nntp-server-opened-hook)
1412 (nntp-send-authinfo t))))
16409b0b 1413 (nntp-kill-buffer (process-buffer process))
eec82323
LMI
1414 nil))))
1415
eec82323
LMI
1416(defun nntp-read-server-type ()
1417 "Find out what the name of the server we have connected to is."
1418 ;; Wait for the status string to arrive.
1419 (setq nntp-server-type (buffer-string))
01c52d31 1420 (let ((case-fold-search t))
eec82323 1421 ;; Run server-specific commands.
01c52d31 1422 (dolist (entry nntp-server-action-alist)
eec82323
LMI
1423 (when (string-match (car entry) nntp-server-type)
1424 (if (and (listp (cadr entry))
1425 (not (eq 'lambda (caadr entry))))
1426 (eval (cadr entry))
1427 (funcall (cadr entry)))))))
1428
16409b0b 1429(defun nntp-async-wait (process wait-for buffer decode callback)
ed075cb4 1430 (with-current-buffer (process-buffer process)
16409b0b
GM
1431 (unless nntp-inside-change-function
1432 (erase-buffer))
1433 (setq nntp-process-wait-for wait-for
1434 nntp-process-to-buffer buffer
1435 nntp-process-decode decode
1436 nntp-process-callback callback
1437 nntp-process-start-point (point-max))
6b8382e4 1438 (setq after-change-functions '(nntp-after-change-function))))
16409b0b
GM
1439
1440(defun nntp-async-timer-handler ()
1441 (mapcar
1442 (lambda (proc)
1443 (if (memq (process-status proc) '(open run))
1444 (nntp-async-trigger proc)
1445 (nntp-async-stop proc)))
1446 nntp-async-process-list))
1447
1448(defun nntp-async-stop (proc)
1449 (setq nntp-async-process-list (delq proc nntp-async-process-list))
1450 (when (and nntp-async-timer (not nntp-async-process-list))
1451 (nnheader-cancel-timer nntp-async-timer)
1452 (setq nntp-async-timer nil)))
1453
1454(defun nntp-after-change-function (beg end len)
1455 (unwind-protect
1456 ;; we only care about insertions at eob
1457 (when (and (eq 0 len) (eq (point-max) end))
1458 (save-match-data
1459 (let ((proc (get-buffer-process (current-buffer))))
1460 (when proc
1461 (nntp-async-trigger proc)))))
1462 ;; any throw from after-change-functions will leave it
1463 ;; set to nil. so we reset it here, if necessary.
1464 (when quit-flag
1465 (setq after-change-functions '(nntp-after-change-function)))))
1466
1467(defun nntp-async-trigger (process)
ed075cb4 1468 (with-current-buffer (process-buffer process)
16409b0b
GM
1469 (when nntp-process-callback
1470 ;; do we have an error message?
1471 (goto-char nntp-process-start-point)
1472 (if (memq (following-char) '(?4 ?5))
1473 ;; wants credentials?
1474 (if (looking-at "480")
2eebe218 1475 (nntp-handle-authinfo process)
16409b0b
GM
1476 ;; report error message.
1477 (nntp-snarf-error-message)
1478 (nntp-do-callback nil))
1479
1480 ;; got what we expect?
1481 (goto-char (point-max))
1482 (when (re-search-backward
1483 nntp-process-wait-for nntp-process-start-point t)
23f87bed
MB
1484 (let ((response (match-string 0)))
1485 (with-current-buffer nntp-server-buffer
1486 (setq nntp-process-response response)))
16409b0b
GM
1487 (nntp-async-stop process)
1488 ;; convert it.
6748645f 1489 (when (gnus-buffer-exists-p nntp-process-to-buffer)
16409b0b
GM
1490 (let ((buf (current-buffer))
1491 (start nntp-process-start-point)
1492 (decode nntp-process-decode))
ed075cb4 1493 (with-current-buffer nntp-process-to-buffer
eec82323 1494 (goto-char (point-max))
16409b0b
GM
1495 (save-restriction
1496 (narrow-to-region (point) (point))
9f5e78f7 1497 (nnheader-insert-buffer-substring buf start)
16409b0b
GM
1498 (when decode
1499 (nntp-decode-text))))))
1500 ;; report it.
1501 (goto-char (point-max))
1502 (nntp-do-callback
1503 (buffer-name (get-buffer nntp-process-to-buffer))))))))
1504
1505(defun nntp-do-callback (arg)
1506 (let ((callback nntp-process-callback)
1507 (nntp-inside-change-function t))
1508 (setq nntp-process-callback nil)
1509 (funcall callback arg)))
eec82323
LMI
1510
1511(defun nntp-snarf-error-message ()
1512 "Save the error message in the current buffer."
1513 (let ((message (buffer-string)))
1514 (while (string-match "[\r\n]+" message)
1515 (setq message (replace-match " " t t message)))
04db63bc 1516 (nnheader-report 'nntp "%s" message)
eec82323
LMI
1517 message))
1518
23f87bed 1519(defun nntp-accept-process-output (process)
eec82323 1520 "Wait for output from PROCESS and message some dots."
01c52d31
MB
1521 (with-current-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1522 nntp-server-buffer)
c7279817 1523 (let ((len (/ (buffer-size) 1024))
eec82323
LMI
1524 message-log-max)
1525 (unless (< len 10)
1526 (setq nntp-have-messaged t)
1527 (nnheader-message 7 "nntp read: %dk" len)))
01c52d31
MB
1528 (prog1
1529 (nnheader-accept-process-output process)
1530 ;; accept-process-output may update status of process to indicate
1531 ;; that the server has closed the connection. This MUST be
1532 ;; handled here as the buffer restored by the save-excursion may
1533 ;; be the process's former output buffer (i.e. now killed)
1534 (or (and process
1535 (memq (process-status process) '(open run)))
1536 (nntp-report "Server closed connection")))))
eec82323
LMI
1537
1538(defun nntp-accept-response ()
1539 "Wait for output from the process that outputs to BUFFER."
1540 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1541
1542(defun nntp-possibly-change-group (group server &optional connectionless)
1543 (let ((nnheader-callback-function nil))
1544 (when server
1545 (or (nntp-server-opened server)
1546 (nntp-open-server server nil connectionless)))
1547
1548 (unless connectionless
1549 (or (nntp-find-connection nntp-server-buffer)
1550 (nntp-open-connection nntp-server-buffer))))
1551
1552 (when group
1553 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
23f87bed
MB
1554 (cond ((not entry)
1555 (nntp-report "Server closed connection"))
1556 ((not (equal group (caddr entry)))
ed075cb4 1557 (with-current-buffer (process-buffer (car entry))
23f87bed
MB
1558 (erase-buffer)
1559 (nntp-send-command "^[245].*\n" "GROUP" group)
1560 (setcar (cddr entry) group)
1561 (erase-buffer)
01c52d31 1562 (nntp-erase-buffer nntp-server-buffer)))))))
eec82323
LMI
1563
1564(defun nntp-decode-text (&optional cr-only)
1565 "Decode the text in the current buffer."
1566 (goto-char (point-min))
1567 (while (search-forward "\r" nil t)
1568 (delete-char -1))
1569 (unless cr-only
1570 ;; Remove trailing ".\n" end-of-transfer marker.
1571 (goto-char (point-max))
1572 (forward-line -1)
1573 (when (looking-at ".\n")
1574 (delete-char 2))
1575 ;; Delete status line.
1576 (goto-char (point-min))
2eebe218
DL
1577 (while (looking-at "[1-5][0-9][0-9] .*\n")
1578 ;; For some unknown reason, there is more than one status line.
1579 (delete-region (point) (progn (forward-line 1) (point))))
eec82323
LMI
1580 ;; Remove "." -> ".." encoding.
1581 (while (search-forward "\n.." nil t)
1582 (delete-char -1))))
1583
1584(defun nntp-encode-text ()
1585 "Encode the text in the current buffer."
1586 (save-excursion
1587 ;; Replace "." at beginning of line with "..".
1588 (goto-char (point-min))
1589 (while (re-search-forward "^\\." nil t)
1590 (insert "."))
1591 (goto-char (point-max))
1592 ;; Insert newline at the end of the buffer.
1593 (unless (bolp)
1594 (insert "\n"))
1595 ;; Insert `.' at end of buffer (end of text mark).
1596 (goto-char (point-max))
45926d06
RS
1597 (insert ".\n")
1598 (goto-char (point-min))
1599 (while (not (eobp))
1600 (end-of-line)
1601 (delete-char 1)
1602 (insert nntp-end-of-line))))
eec82323
LMI
1603
1604(defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1605 (set-buffer nntp-server-buffer)
1606 (erase-buffer)
1607 (cond
1608
1609 ;; This server does not talk NOV.
1610 ((not nntp-server-xover)
1611 nil)
1612
1613 ;; We don't care about gaps.
1614 ((or (not nntp-nov-gap)
1615 fetch-old)
1616 (nntp-send-xover-command
1617 (if fetch-old
1618 (if (numberp fetch-old)
1619 (max 1 (- (car articles) fetch-old))
1620 1)
1621 (car articles))
1622 (car (last articles)) 'wait)
1623
1624 (goto-char (point-min))
16409b0b 1625 (when (looking-at "[1-5][0-9][0-9] .*\n")
eec82323
LMI
1626 (delete-region (point) (progn (forward-line 1) (point))))
1627 (while (search-forward "\r" nil t)
1628 (replace-match "" t t))
1629 (goto-char (point-max))
1630 (forward-line -1)
1631 (when (looking-at "\\.")
1632 (delete-region (point) (progn (forward-line 1) (point)))))
1633
1634 ;; We do it the hard way. For each gap, an XOVER command is sent
1635 ;; to the server. We do not wait for a reply from the server, we
1636 ;; just send them off as fast as we can. That means that we have
1637 ;; to count the number of responses we get back to find out when we
1638 ;; have gotten all we asked for.
1639 ((numberp nntp-nov-gap)
1640 (let ((count 0)
1641 (received 0)
16409b0b
GM
1642 last-point
1643 in-process-buffer-p
eec82323 1644 (buf nntp-server-buffer)
16409b0b 1645 (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
23f87bed 1646 first last status)
eec82323
LMI
1647 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1648 ;; that means that the server does not understand XOVER, but we
1649 ;; won't know that until we try.
1650 (while (and nntp-server-xover articles)
1651 (setq first (car articles))
1652 ;; Search forward until we find a gap, or until we run out of
1653 ;; articles.
1654 (while (and (cdr articles)
1655 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1656 (setq articles (cdr articles)))
1657
16409b0b 1658 (setq in-process-buffer-p (stringp nntp-server-xover))
23f87bed
MB
1659 (nntp-send-xover-command first (setq last (car articles)))
1660 (setq articles (cdr articles))
a1506d29 1661
16409b0b
GM
1662 (when (and nntp-server-xover in-process-buffer-p)
1663 ;; Don't count tried request.
1664 (setq count (1+ count))
a1506d29 1665
eec82323
LMI
1666 ;; Every 400 requests we have to read the stream in
1667 ;; order to avoid deadlocks.
1668 (when (or (null articles) ;All requests have been sent.
23f87bed 1669 (= 1 (% count nntp-maximum-request)))
16409b0b
GM
1670
1671 (nntp-accept-response)
1672 ;; On some Emacs versions the preceding function has a
1673 ;; tendency to change the buffer. Perhaps. It's quite
1674 ;; difficult to reproduce, because it only seems to happen
1675 ;; once in a blue moon.
1676 (set-buffer process-buffer)
eec82323 1677 (while (progn
16409b0b 1678 (goto-char (or last-point (point-min)))
eec82323 1679 ;; Count replies.
23f87bed
MB
1680 (while (re-search-forward "^\\([0-9][0-9][0-9]\\) .*\n"
1681 nil t)
1682 (incf received)
1683 (setq status (match-string 1))
1684 (if (string-match "^[45]" status)
1685 (setq status 'error)
1686 (setq status 'ok)))
eec82323 1687 (setq last-point (point))
23f87bed
MB
1688 (or (< received count)
1689 (if (eq status 'error)
1690 nil
1691 ;; I haven't started reading the final response
1692 (progn
1693 (goto-char (point-max))
1694 (forward-line -1)
1695 (not (looking-at "^\\.\r?\n"))))))
1696 ;; I haven't read the end of the final response
16409b0b 1697 (nntp-accept-response)
23f87bed
MB
1698 (set-buffer process-buffer))))
1699
1700 ;; Some nntp servers seem to have an extension to the XOVER
1701 ;; extension. On these servers, requesting an article range
97610156 1702 ;; preceding the active range does not return an error as
23f87bed
MB
1703 ;; specified in the RFC. What we instead get is the NOV entry
1704 ;; for the first available article. Obviously, a client can
1705 ;; use that entry to avoid making unnecessary requests. The
1706 ;; only problem is for a client that assumes that the response
40ba43b4 1707 ;; will always be within the requested range. For such a
23f87bed
MB
1708 ;; client, we can get N copies of the same entry (one for each
1709 ;; XOVER command sent to the server).
1710
1711 (when (<= count 1)
1712 (goto-char (point-min))
1713 (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
e9bd5782 1714 (let ((low-limit (string-to-number
e84b4b86 1715 (buffer-substring (match-beginning 1)
23f87bed
MB
1716 (match-end 1)))))
1717 (while (and articles (<= (car articles) low-limit))
1718 (setq articles (cdr articles))))))
1719 (set-buffer buf))
eec82323
LMI
1720
1721 (when nntp-server-xover
16409b0b 1722 (when in-process-buffer-p
16409b0b
GM
1723 (set-buffer buf)
1724 (goto-char (point-max))
9f5e78f7 1725 (nnheader-insert-buffer-substring process-buffer)
16409b0b
GM
1726 (set-buffer process-buffer)
1727 (erase-buffer)
1728 (set-buffer buf))
eec82323
LMI
1729
1730 ;; We remove any "." lines and status lines.
1731 (goto-char (point-min))
1732 (while (search-forward "\r" nil t)
1733 (delete-char -1))
1734 (goto-char (point-min))
1735 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
eec82323
LMI
1736 t))))
1737
1738 nntp-server-xover)
1739
1740(defun nntp-send-xover-command (beg end &optional wait-for-reply)
1741 "Send the XOVER command to the server."
1742 (let ((range (format "%d-%d" beg end))
1743 (nntp-inhibit-erase t))
1744 (if (stringp nntp-server-xover)
1745 ;; If `nntp-server-xover' is a string, then we just send this
1746 ;; command.
1747 (if wait-for-reply
1748 (nntp-send-command-nodelete
1749 "\r?\n\\.\r?\n" nntp-server-xover range)
1750 ;; We do not wait for the reply.
16409b0b 1751 (nntp-send-command-nodelete nil nntp-server-xover range))
eec82323
LMI
1752 (let ((commands nntp-xover-commands))
1753 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1754 ;; We try them all until we get at positive response.
1755 (while (and commands (eq nntp-server-xover 'try))
1756 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
ed075cb4 1757 (with-current-buffer nntp-server-buffer
eec82323
LMI
1758 (goto-char (point-min))
1759 (and (looking-at "[23]") ; No error message.
1760 ;; We also have to look at the lines. Some buggy
1761 ;; servers give back simple lines with just the
1762 ;; article number. How... helpful.
1763 (progn
1764 (forward-line 1)
5cedca8d
MB
1765 ;; More text after number, or a dot.
1766 (looking-at "[0-9]+\t...\\|\\.\r?\n"))
eec82323
LMI
1767 (setq nntp-server-xover (car commands))))
1768 (setq commands (cdr commands)))
1769 ;; If none of the commands worked, we disable XOVER.
1770 (when (eq nntp-server-xover 'try)
01c52d31
MB
1771 (nntp-erase-buffer nntp-server-buffer)
1772 (setq nntp-server-xover nil))
23f87bed 1773 nntp-server-xover))))
eec82323 1774
23f87bed
MB
1775(defun nntp-find-group-and-number (&optional group)
1776 (save-excursion
1777 (save-restriction
ed075cb4 1778 ;; FIXME: This is REALLY FISHY: set-buffer after save-restriction?!?
23f87bed
MB
1779 (set-buffer nntp-server-buffer)
1780 (narrow-to-region (goto-char (point-min))
1781 (or (search-forward "\n\n" nil t) (point-max)))
1782 (goto-char (point-min))
1783 ;; We first find the number by looking at the status line.
1784 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
e9bd5782 1785 (string-to-number
23f87bed
MB
1786 (buffer-substring (match-beginning 1)
1787 (match-end 1)))))
1788 newsgroups xref)
1789 (and number (zerop number) (setq number nil))
1790 (if number
1791 ;; Then we find the group name.
1792 (setq group
1793 (cond
1794 ;; If there is only one group in the Newsgroups
1795 ;; header, then it seems quite likely that this
1796 ;; article comes from that group, I'd say.
1797 ((and (setq newsgroups
1798 (mail-fetch-field "newsgroups"))
1799 (not (string-match "," newsgroups)))
1800 newsgroups)
1801 ;; If there is more than one group in the
1802 ;; Newsgroups header, then the Xref header should
1803 ;; be filled out. We hazard a guess that the group
1804 ;; that has this article number in the Xref header
1805 ;; is the one we are looking for. This might very
1806 ;; well be wrong if this article happens to have
1807 ;; the same number in several groups, but that's
1808 ;; life.
1809 ((and (setq xref (mail-fetch-field "xref"))
1810 number
1811 (string-match
1812 (format "\\([^ :]+\\):%d" number) xref))
1813 (match-string 1 xref))
1814 (t "")))
1815 (cond
37cc095b
MB
1816 ((and (not nntp-xref-number-is-evil)
1817 (setq xref (mail-fetch-field "xref"))
23f87bed
MB
1818 (string-match
1819 (if group
1820 (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1821 "\\([^ :]+\\):\\([0-9]+\\)")
1822 xref))
1823 (setq group (match-string 1 xref)
e9bd5782 1824 number (string-to-number (match-string 2 xref))))
23f87bed
MB
1825 ((and (setq newsgroups
1826 (mail-fetch-field "newsgroups"))
1827 (not (string-match "," newsgroups)))
1828 (setq group newsgroups))
1829 (group)
1830 (t (setq group ""))))
1831 (when (string-match "\r" group)
1832 (setq group (substring group 0 (match-beginning 0))))
1833 (cons group number)))))
eec82323
LMI
1834
1835(defun nntp-wait-for-string (regexp)
1836 "Wait until string arrives in the buffer."
23f87bed
MB
1837 (let ((buf (current-buffer))
1838 proc)
eec82323 1839 (goto-char (point-min))
23f87bed
MB
1840 (while (and (setq proc (get-buffer-process buf))
1841 (memq (process-status proc) '(open run))
1842 (not (re-search-forward regexp nil t)))
d832b437 1843 (accept-process-output proc 0.1)
eec82323
LMI
1844 (set-buffer buf)
1845 (goto-char (point-min)))))
1846
23f87bed
MB
1847
1848;; ==========================================================================
1849;; Obsolete nntp-open-* connection methods -- drv
1850;; ==========================================================================
1851
1852(defvoo nntp-open-telnet-envuser nil
1853 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1854
1855(defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
1856 "*Regular expression to match the shell prompt on the remote machine.")
1857
1858(defvoo nntp-rlogin-program "rsh"
1859 "*Program used to log in on remote machines.
1860The default is \"rsh\", but \"ssh\" is a popular alternative.")
1861
1862(defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1863 "*Parameters to `nntp-open-rlogin'.
1864That function may be used as `nntp-open-connection-function'. In that
1865case, this list will be used as the parameter list given to rsh.")
1866
1867(defvoo nntp-rlogin-user-name nil
1868 "*User name on remote system when using the rlogin connect method.")
1869
1870(defvoo nntp-telnet-parameters
1871 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1872 "*Parameters to `nntp-open-telnet'.
1873That function may be used as `nntp-open-connection-function'. In that
1874case, this list will be executed as a command after logging in
1875via telnet.")
1876
1877(defvoo nntp-telnet-user-name nil
1878 "User name to log in via telnet with.")
1879
1880(defvoo nntp-telnet-passwd nil
1881 "Password to use to log in via telnet with.")
1882
990e2c2f
SM
1883(defun nntp-service-to-port (svc)
1884 (cond
1885 ((integerp svc) (number-to-string svc))
49b196a5 1886 ((string-match "\\`[0-9]+\\'" svc) svc)
990e2c2f
SM
1887 (t
1888 (with-temp-buffer
1889 (ignore-errors (insert-file-contents "/etc/services"))
1890 (goto-char (point-min))
1891 (if (re-search-forward (concat "^" (regexp-quote svc)
49b196a5 1892 "[ \t]+\\([0-9]+\\)/tcp"))
990e2c2f
SM
1893 (match-string 1)
1894 svc)))))
1895
eec82323 1896(defun nntp-open-telnet (buffer)
990e2c2f 1897 (with-current-buffer buffer
eec82323 1898 (erase-buffer)
a8151ef7
LMI
1899 (let ((proc (apply
1900 'start-process
1901 "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
eec82323
LMI
1902 (case-fold-search t))
1903 (when (memq (process-status proc) '(open run))
2eebe218 1904 (nntp-wait-for-string "^r?telnet")
eec82323 1905 (process-send-string proc "set escape \^X\n")
6748645f
LMI
1906 (cond
1907 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1908 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1909 nntp-address "\n")))
1910 (t
1911 (process-send-string proc (concat "open " nntp-address "\n"))))
1912 (cond
1913 ((not nntp-open-telnet-envuser)
1914 (nntp-wait-for-string "^\r*.?login:")
1915 (process-send-string
1916 proc (concat
1917 (or nntp-telnet-user-name
1918 (setq nntp-telnet-user-name (read-string "login: ")))
1919 "\n"))))
eec82323
LMI
1920 (nntp-wait-for-string "^\r*.?password:")
1921 (process-send-string
1922 proc (concat
1923 (or nntp-telnet-passwd
1924 (setq nntp-telnet-passwd
23f87bed 1925 (read-passwd "Password: ")))
eec82323 1926 "\n"))
6748645f 1927 (nntp-wait-for-string nntp-telnet-shell-prompt)
eec82323
LMI
1928 (process-send-string
1929 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
6748645f 1930 (nntp-wait-for-string "^\r*20[01]")
eec82323
LMI
1931 (beginning-of-line)
1932 (delete-region (point-min) (point))
1933 (process-send-string proc "\^]")
2eebe218 1934 (nntp-wait-for-string "^r?telnet")
eec82323
LMI
1935 (process-send-string proc "mode character\n")
1936 (accept-process-output proc 1)
1937 (sit-for 1)
1938 (goto-char (point-min))
1939 (forward-line 1)
1940 (delete-region (point) (point-max)))
1941 proc)))
1942
1943(defun nntp-open-rlogin (buffer)
1944 "Open a connection to SERVER using rsh."
1945 (let ((proc (if nntp-rlogin-user-name
6748645f
LMI
1946 (apply 'start-process
1947 "nntpd" buffer nntp-rlogin-program
1948 nntp-address "-l" nntp-rlogin-user-name
1949 nntp-rlogin-parameters)
1950 (apply 'start-process
1951 "nntpd" buffer nntp-rlogin-program nntp-address
1952 nntp-rlogin-parameters))))
990e2c2f 1953 (with-current-buffer buffer
6748645f
LMI
1954 (nntp-wait-for-string "^\r*20[01]")
1955 (beginning-of-line)
1956 (delete-region (point-min) (point))
1957 proc)))
eec82323 1958
23f87bed
MB
1959
1960;; ==========================================================================
1961;; Replacements for the nntp-open-* functions -- drv
1962;; ==========================================================================
1963
1964(defun nntp-open-telnet-stream (buffer)
1965 "Open a nntp connection by telnet'ing the news server.
990e2c2f 1966`nntp-open-netcat-stream' is recommended in place of this function
ed075cb4 1967because it is more reliable.
23f87bed
MB
1968
1969Please refer to the following variables to customize the connection:
1970- `nntp-pre-command',
1971- `nntp-telnet-command',
1972- `nntp-telnet-switches',
1973- `nntp-address',
1974- `nntp-port-number',
1975- `nntp-end-of-line'."
1976 (let ((command `(,nntp-telnet-command
1977 ,@nntp-telnet-switches
9b3ebcb6 1978 ,nntp-address
990e2c2f 1979 ,(nntp-service-to-port nntp-port-number)))
23f87bed
MB
1980 proc)
1981 (and nntp-pre-command
1982 (push nntp-pre-command command))
1983 (setq proc (apply 'start-process "nntpd" buffer command))
ed075cb4 1984 (with-current-buffer buffer
23f87bed
MB
1985 (nntp-wait-for-string "^\r*20[01]")
1986 (beginning-of-line)
1987 (delete-region (point-min) (point))
1988 proc)))
1989
1990(defun nntp-open-via-rlogin-and-telnet (buffer)
1991 "Open a connection to an nntp server through an intermediate host.
1992First rlogin to the remote host, and then telnet the real news server
1993from there.
ed075cb4
SM
1994`nntp-open-via-rlogin-and-netcat' is recommended in place of this function
1995because it is more reliable.
23f87bed
MB
1996
1997Please refer to the following variables to customize the connection:
1998- `nntp-pre-command',
1999- `nntp-via-rlogin-command',
2000- `nntp-via-rlogin-command-switches',
2001- `nntp-via-user-name',
2002- `nntp-via-address',
2003- `nntp-telnet-command',
2004- `nntp-telnet-switches',
2005- `nntp-address',
2006- `nntp-port-number',
2007- `nntp-end-of-line'."
2008 (let ((command `(,nntp-via-address
2009 ,nntp-telnet-command
2010 ,@nntp-telnet-switches))
2011 proc)
2012 (when nntp-via-user-name
2013 (setq command `("-l" ,nntp-via-user-name ,@command)))
2014 (when nntp-via-rlogin-command-switches
2015 (setq command (append nntp-via-rlogin-command-switches command)))
2016 (push nntp-via-rlogin-command command)
2017 (and nntp-pre-command
2018 (push nntp-pre-command command))
2019 (setq proc (apply 'start-process "nntpd" buffer command))
ed075cb4 2020 (with-current-buffer buffer
23f87bed 2021 (nntp-wait-for-string "^r?telnet")
9b3ebcb6 2022 (process-send-string proc (concat "open " nntp-address " "
990e2c2f 2023 (nntp-service-to-port nntp-port-number)
9b3ebcb6 2024 "\n"))
23f87bed
MB
2025 (nntp-wait-for-string "^\r*20[01]")
2026 (beginning-of-line)
2027 (delete-region (point-min) (point))
2028 (process-send-string proc "\^]")
2029 (nntp-wait-for-string "^r?telnet")
2030 (process-send-string proc "mode character\n")
2031 (accept-process-output proc 1)
2032 (sit-for 1)
eec82323 2033 (goto-char (point-min))
23f87bed
MB
2034 (forward-line 1)
2035 (delete-region (point) (point-max)))
2036 proc))
2037
01c52d31
MB
2038(defun nntp-open-via-rlogin-and-netcat (buffer)
2039 "Open a connection to an nntp server through an intermediate host.
2040First rlogin to the remote host, and then connect to the real news
2041server from there using the netcat command.
2042
2043Please refer to the following variables to customize the connection:
2044- `nntp-pre-command',
2045- `nntp-via-rlogin-command',
2046- `nntp-via-rlogin-command-switches',
2047- `nntp-via-user-name',
2048- `nntp-via-address',
990e2c2f
SM
2049- `nntp-netcat-command',
2050- `nntp-netcat-switches',
01c52d31 2051- `nntp-address',
990e2c2f 2052- `nntp-port-number'."
01c52d31
MB
2053 (let ((command `(,@(when nntp-pre-command
2054 (list nntp-pre-command))
2055 ,nntp-via-rlogin-command
990e2c2f 2056 ,@nntp-via-rlogin-command-switches
01c52d31
MB
2057 ,@(when nntp-via-user-name
2058 (list "-l" nntp-via-user-name))
2059 ,nntp-via-address
990e2c2f
SM
2060 ,nntp-netcat-command
2061 ,@nntp-netcat-switches
01c52d31 2062 ,nntp-address
990e2c2f
SM
2063 ,(nntp-service-to-port nntp-port-number))))
2064 ;; A non-nil connection type results in mightily odd behavior where
2065 ;; (process-send-string proc "\^M") ends up sending a "\n" to the
2066 ;; ssh process. --Stef
2067 ;; Also a nil connection allow ssh-askpass to work under X11.
2068 (let ((process-connection-type nil))
2069 (apply 'start-process "nntpd" buffer command))))
2070
2071(defun nntp-open-netcat-stream (buffer)
2072 "Open a connection to an nntp server through netcat.
2073I.e. use the `nc' command rather than Emacs's builtin networking code.
2074
2075Please refer to the following variables to customize the connection:
2076- `nntp-pre-command',
2077- `nntp-netcat-command',
2078- `nntp-netcat-switches',
2079- `nntp-address',
2080- `nntp-port-number'."
2081 (let ((command `(,nntp-netcat-command
2082 ,@nntp-netcat-switches
2083 ,nntp-address
2084 ,(nntp-service-to-port nntp-port-number))))
2085 (and nntp-pre-command (push nntp-pre-command command))
2086 (let ((process-connection-type nil)) ;See `nntp-open-via-rlogin-and-netcat'.
2087 (apply 'start-process "nntpd" buffer command))))
c9fc72fa 2088
01c52d31 2089
23f87bed
MB
2090(defun nntp-open-via-telnet-and-telnet (buffer)
2091 "Open a connection to an nntp server through an intermediate host.
2092First telnet the remote host, and then telnet the real news server
2093from there.
2094
2095Please refer to the following variables to customize the connection:
2096- `nntp-pre-command',
2097- `nntp-via-telnet-command',
2098- `nntp-via-telnet-switches',
2099- `nntp-via-address',
2100- `nntp-via-envuser',
2101- `nntp-via-user-name',
2102- `nntp-via-user-password',
2103- `nntp-via-shell-prompt',
2104- `nntp-telnet-command',
2105- `nntp-telnet-switches',
2106- `nntp-address',
2107- `nntp-port-number',
2108- `nntp-end-of-line'."
ed075cb4 2109 (with-current-buffer buffer
23f87bed
MB
2110 (erase-buffer)
2111 (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
2112 (case-fold-search t)
2113 proc)
2114 (and nntp-pre-command (push nntp-pre-command command))
2115 (setq proc (apply 'start-process "nntpd" buffer command))
2116 (when (memq (process-status proc) '(open run))
2117 (nntp-wait-for-string "^r?telnet")
2118 (process-send-string proc "set escape \^X\n")
2119 (cond
2120 ((and nntp-via-envuser nntp-via-user-name)
2121 (process-send-string proc (concat "open " "-l" nntp-via-user-name
2122 nntp-via-address "\n")))
2123 (t
2124 (process-send-string proc (concat "open " nntp-via-address
2125 "\n"))))
2126 (when (not nntp-via-envuser)
2127 (nntp-wait-for-string "^\r*.?login:")
2128 (process-send-string proc
2129 (concat
2130 (or nntp-via-user-name
2131 (setq nntp-via-user-name
2132 (read-string "login: ")))
2133 "\n")))
2134 (nntp-wait-for-string "^\r*.?password:")
2135 (process-send-string proc
2136 (concat
2137 (or nntp-via-user-password
2138 (setq nntp-via-user-password
2139 (read-passwd "Password: ")))
2140 "\n"))
2141 (nntp-wait-for-string nntp-via-shell-prompt)
2142 (let ((real-telnet-command `("exec"
2143 ,nntp-telnet-command
2144 ,@nntp-telnet-switches
2145 ,nntp-address
990e2c2f 2146 ,(nntp-service-to-port nntp-port-number))))
23f87bed
MB
2147 (process-send-string proc
2148 (concat (mapconcat 'identity
2149 real-telnet-command " ")
2150 "\n")))
2151 (nntp-wait-for-string "^\r*20[01]")
2152 (beginning-of-line)
2153 (delete-region (point-min) (point))
2154 (process-send-string proc "\^]")
2155 (nntp-wait-for-string "^r?telnet")
2156 (process-send-string proc "mode character\n")
2157 (accept-process-output proc 1)
2158 (sit-for 1)
2159 (goto-char (point-min))
2160 (forward-line 1)
2161 (delete-region (point) (point-max)))
2162 proc)))
eec82323 2163
01c52d31
MB
2164;; Marks handling
2165
2166(defun nntp-marks-directory (server)
2167 (expand-file-name server nntp-marks-directory))
2168
2169(defvar nntp-server-to-method-cache nil
2170 "Alist of servers and select methods.")
2171
2172(defun nntp-group-pathname (server group &optional file)
2173 "Return an absolute file name of FILE for GROUP on SERVER."
2174 (let ((method (cdr (assoc server nntp-server-to-method-cache))))
2175 (unless method
2176 (push (cons server (setq method (or (gnus-server-to-method server)
2177 (gnus-find-method-for-group group))))
2178 nntp-server-to-method-cache))
2179 (nnmail-group-pathname
2180 (mm-decode-coding-string group
2181 (inline (gnus-group-name-charset method group)))
2182 (nntp-marks-directory server)
2183 file)))
2184
2185(defun nntp-possibly-create-directory (group server)
2186 (let ((dir (nntp-group-pathname server group))
2187 (file-name-coding-system nnmail-pathname-coding-system))
2188 (unless (file-exists-p dir)
2189 (make-directory (directory-file-name dir) t)
2190 (nnheader-message 5 "Creating nntp marks directory %s" dir))))
2191
8abf1b22 2192(autoload 'time-less-p "time-date")
01c52d31
MB
2193
2194(defun nntp-marks-changed-p (group server)
2195 (let ((file (nntp-group-pathname server group nntp-marks-file-name))
2196 (file-name-coding-system nnmail-pathname-coding-system))
2197 (if (null (gnus-gethash file nntp-marks-modtime))
2198 t ;; never looked at marks file, assume it has changed
2199 (time-less-p (gnus-gethash file nntp-marks-modtime)
2200 (nth 5 (file-attributes file))))))
2201
2202(defun nntp-save-marks (group server)
2203 (let ((file-name-coding-system nnmail-pathname-coding-system)
2204 (file (nntp-group-pathname server group nntp-marks-file-name)))
2205 (condition-case err
2206 (progn
2207 (nntp-possibly-create-directory group server)
2208 (with-temp-file file
2209 (erase-buffer)
2210 (gnus-prin1 nntp-marks)
2211 (insert "\n"))
2212 (gnus-sethash file
2213 (nth 5 (file-attributes file))
2214 nntp-marks-modtime))
2215 (error (or (gnus-yes-or-no-p
2216 (format "Could not write to %s (%s). Continue? " file err))
2217 (error "Cannot write to %s (%s)" file err))))))
2218
2219(defun nntp-open-marks (group server)
2220 (let ((file (nntp-group-pathname server group nntp-marks-file-name))
2221 (file-name-coding-system nnmail-pathname-coding-system))
2222 (if (file-exists-p file)
2223 (condition-case err
2224 (with-temp-buffer
2225 (gnus-sethash file (nth 5 (file-attributes file))
2226 nntp-marks-modtime)
2227 (nnheader-insert-file-contents file)
2228 (setq nntp-marks (read (current-buffer)))
2229 (dolist (el gnus-article-unpropagated-mark-lists)
2230 (setq nntp-marks (gnus-remassoc el nntp-marks))))
2231 (error (or (gnus-yes-or-no-p
2232 (format "Error reading nntp marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
2233 (error "Cannot read nntp marks file %s (%s)" file err))))
2234 ;; User didn't have a .marks file. Probably first time
2235 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
2236 (let ((info (gnus-get-info
2237 (gnus-group-prefixed-name
2238 group
2239 (gnus-server-to-method (format "nntp:%s" server)))))
2240 (decoded-name (mm-decode-coding-string
2241 group
2242 (gnus-group-name-charset
2243 (gnus-server-to-method server) group))))
2244 (nnheader-message 7 "Bootstrapping marks for %s..." decoded-name)
2245 (setq nntp-marks (gnus-info-marks info))
2246 (push (cons 'read (gnus-info-read info)) nntp-marks)
2247 (dolist (el gnus-article-unpropagated-mark-lists)
2248 (setq nntp-marks (gnus-remassoc el nntp-marks)))
2249 (nntp-save-marks group server)
2250 (nnheader-message 7 "Bootstrapping marks for %s...done"
2251 decoded-name)))))
2252
eec82323
LMI
2253(provide 'nntp)
2254
2255;;; nntp.el ends here