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