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