Fix typos.
[emacs.git] / lisp / gnus / nntp.el
blob325aa67f80d811a8cdf629e5cda07926728b5284
1 ;;; nntp.el --- nntp access for Gnus
3 ;; Copyright (C) 1987-1990, 1992-1998, 2000-2011
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 (defvar nntp-record-commands nil
285 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
287 (defvar nntp-have-messaged nil)
289 (defvar nntp-process-wait-for nil)
290 (defvar nntp-process-to-buffer nil)
291 (defvar nntp-process-callback nil)
292 (defvar nntp-process-decode nil)
293 (defvar nntp-process-start-point nil)
294 (defvar nntp-inside-change-function nil)
295 (defvoo nntp-last-command-time nil)
296 (defvoo nntp-last-command nil)
297 (defvoo nntp-authinfo-password nil)
298 (defvoo nntp-authinfo-user nil)
299 (defvoo nntp-authinfo-force nil)
301 (defvar nntp-connection-list nil)
303 (defvoo nntp-server-type nil)
304 (defvoo nntp-connection-alist nil)
305 (defvoo nntp-status-string "")
306 (defconst nntp-version "nntp 5.0")
307 (defvoo nntp-inhibit-erase nil)
308 (defvoo nntp-inhibit-output nil)
310 (defvoo nntp-server-xover 'try)
312 (defvar nntp-async-timer nil)
313 (defvar nntp-async-process-list nil)
315 (defvar nntp-authinfo-rejected nil
316 "A custom error condition used to report 'Authentication Rejected' errors.
317 Condition handlers that match just this condition ensure that the nntp
318 backend doesn't catch this error.")
319 (put 'nntp-authinfo-rejected 'error-conditions '(error nntp-authinfo-rejected))
320 (put 'nntp-authinfo-rejected 'error-message "Authorization Rejected")
324 ;;; Internal functions.
326 (defsubst nntp-send-string (process string)
327 "Send STRING to PROCESS."
328 ;; We need to store the time to provide timeouts, and
329 ;; to store the command so the we can replay the command
330 ;; if the server gives us an AUTHINFO challenge.
331 (setq nntp-last-command-time (current-time)
332 nntp-last-command string)
333 (when nntp-record-commands
334 (nntp-record-command string))
335 (process-send-string process (concat string nntp-end-of-line))
336 (or (memq (process-status process) '(open run))
337 (nntp-report "Server closed connection")))
339 (defun nntp-record-command (string)
340 "Record the command STRING."
341 (with-current-buffer (get-buffer-create "*nntp-log*")
342 (goto-char (point-max))
343 (insert (format-time-string "%Y%m%dT%H%M%S.%3N")
344 " " nntp-address " " string "\n")))
346 (defun nntp-report (&rest args)
347 "Report an error from the nntp backend. The first string in ARGS
348 can be a format string. For some commands, the failed command may be
349 retried once before actually displaying the error report."
351 (when nntp-record-commands
352 (nntp-record-command "*** CALLED nntp-report ***"))
354 (nnheader-report 'nntp args)
356 (apply 'error args))
358 (defun nntp-report-1 (&rest args)
359 "Throws out to nntp-with-open-group-error so that the connection may
360 be restored and the command retried."
362 (when nntp-record-commands
363 (nntp-record-command "*** CONNECTION LOST ***"))
365 (throw 'nntp-with-open-group-error t))
367 (defmacro nntp-copy-to-buffer (buffer start end)
368 "Copy string from unibyte current buffer to multibyte buffer."
369 (if (featurep 'xemacs)
370 `(copy-to-buffer ,buffer ,start ,end)
371 `(let ((string (buffer-substring ,start ,end)))
372 (with-current-buffer ,buffer
373 (erase-buffer)
374 (insert (if enable-multibyte-characters
375 (mm-string-to-multibyte string)
376 string))
377 (goto-char (point-min))
378 nil))))
380 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
381 "Wait for WAIT-FOR to arrive from PROCESS."
383 (with-current-buffer (process-buffer process)
384 (goto-char (point-min))
386 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
387 (looking-at "48[02]"))
388 (memq (process-status process) '(open run)))
389 (cond ((looking-at "480")
390 (nntp-handle-authinfo process))
391 ((looking-at "482")
392 (nnheader-report 'nntp "%s"
393 (get 'nntp-authinfo-rejected 'error-message))
394 (signal 'nntp-authinfo-rejected nil))
395 ((looking-at "^.*\n")
396 (delete-region (point) (progn (forward-line 1) (point)))))
397 (nntp-accept-process-output process)
398 (goto-char (point-min)))
399 (prog1
400 (cond
401 ((looking-at "[45]")
402 (progn
403 (nntp-snarf-error-message)
404 nil))
405 ((not (memq (process-status process) '(open run)))
406 (nntp-report "Server closed connection"))
408 (goto-char (point-max))
409 (let ((limit (point-min))
410 response)
411 (while (not (re-search-backward wait-for limit t))
412 (nntp-accept-process-output process)
413 ;; We assume that whatever we wait for is less than 1000
414 ;; characters long.
415 (setq limit (max (- (point-max) 1000) (point-min)))
416 (goto-char (point-max)))
417 (setq response (match-string 0))
418 (with-current-buffer nntp-server-buffer
419 (setq nntp-process-response response)))
420 (nntp-decode-text (not decode))
421 (unless discard
422 (with-current-buffer buffer
423 (goto-char (point-max))
424 (nnheader-insert-buffer-substring (process-buffer process))
425 ;; Nix out "nntp reading...." message.
426 (when nntp-have-messaged
427 (setq nntp-have-messaged nil)
428 (nnheader-message 5 ""))))
430 (unless discard
431 (erase-buffer)))))
433 (defun nntp-kill-buffer (buffer)
434 (when (buffer-name buffer)
435 (let ((process (get-buffer-process buffer)))
436 (when process
437 (delete-process process)))
438 (kill-buffer buffer)
439 (nnheader-init-server-buffer)))
441 (defun nntp-erase-buffer (buffer)
442 "Erase contents of BUFFER."
443 (with-current-buffer buffer
444 (erase-buffer)))
446 (defsubst nntp-find-connection (buffer)
447 "Find the connection delivering to BUFFER."
448 (let ((alist nntp-connection-alist)
449 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
450 process entry)
451 (while (and alist (setq entry (pop alist)))
452 (when (eq buffer (cadr entry))
453 (setq process (car entry)
454 alist nil)))
455 (when process
456 (if (memq (process-status process) '(open run))
457 process
458 (nntp-kill-buffer (process-buffer process))
459 (setq nntp-connection-alist (delq entry nntp-connection-alist))
460 nil))))
462 (defsubst nntp-find-connection-entry (buffer)
463 "Return the entry for the connection to BUFFER."
464 (assq (nntp-find-connection buffer) nntp-connection-alist))
466 (defun nntp-find-connection-buffer (buffer)
467 "Return the process connection buffer tied to BUFFER."
468 (let ((process (nntp-find-connection buffer)))
469 (when process
470 (process-buffer process))))
472 (defsubst nntp-retrieve-data (command address port buffer
473 &optional wait-for callback decode)
474 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
475 (let ((process (or (nntp-find-connection buffer)
476 (nntp-open-connection buffer))))
477 (if process
478 (progn
479 (unless (or nntp-inhibit-erase nnheader-callback-function)
480 (nntp-erase-buffer (process-buffer process)))
481 (condition-case err
482 (progn
483 (when command
484 (nntp-send-string process command))
485 (cond
486 ((eq callback 'ignore)
488 ((and callback wait-for)
489 (nntp-async-wait process wait-for buffer decode callback)
491 (wait-for
492 (nntp-wait-for process wait-for buffer decode))
493 (t t)))
494 (nntp-authinfo-rejected
495 (signal 'nntp-authinfo-rejected (cdr err)))
496 (error
497 (nnheader-report 'nntp "Couldn't open connection to %s: %s"
498 address err))
499 (quit
500 (message "Quit retrieving data from nntp")
501 (signal 'quit nil)
502 nil)))
503 (nnheader-report 'nntp "Couldn't open connection to %s" address))))
505 (defsubst nntp-send-command (wait-for &rest strings)
506 "Send STRINGS to server and wait until WAIT-FOR returns."
507 (when (and (not nnheader-callback-function)
508 (not nntp-inhibit-output))
509 (nntp-erase-buffer nntp-server-buffer))
510 (let* ((command (mapconcat 'identity strings " "))
511 (process (nntp-find-connection nntp-server-buffer))
512 (buffer (and process (process-buffer process)))
513 (pos (and buffer (with-current-buffer buffer (point)))))
514 (if process
515 (prog1
516 (nntp-retrieve-data command
517 nntp-address nntp-port-number
518 nntp-server-buffer
519 wait-for nnheader-callback-function)
520 ;; If nothing to wait for, still remove possibly echo'ed commands.
521 ;; We don't have echoes if `nntp-never-echoes-commands' is non-nil
522 ;; or the value of `nntp-open-connection-function' is in
523 ;; `nntp-open-connection-functions-never-echo-commands', so we
524 ;; skip this in that cases.
525 (unless (or wait-for
526 nntp-never-echoes-commands
527 (memq
528 nntp-open-connection-function
529 nntp-open-connection-functions-never-echo-commands))
530 (nntp-accept-response)
531 (with-current-buffer buffer
532 (goto-char pos)
533 (if (looking-at (regexp-quote command))
534 (delete-region pos (progn (forward-line 1)
535 (point-at-bol)))))))
536 (nnheader-report 'nntp "Couldn't open connection to %s."
537 nntp-address))))
539 (defun nntp-send-command-nodelete (wait-for &rest strings)
540 "Send STRINGS to server and wait until WAIT-FOR returns."
541 (let* ((command (mapconcat 'identity strings " "))
542 (process (nntp-find-connection nntp-server-buffer))
543 (buffer (and process (process-buffer process)))
544 (pos (and buffer (with-current-buffer buffer (point)))))
545 (if process
546 (prog1
547 (nntp-retrieve-data command
548 nntp-address nntp-port-number
549 nntp-server-buffer
550 wait-for nnheader-callback-function)
551 ;; If nothing to wait for, still remove possibly echo'ed commands
552 (unless wait-for
553 (nntp-accept-response)
554 (with-current-buffer buffer
555 (goto-char pos)
556 (if (looking-at (regexp-quote command))
557 (delete-region pos (progn (forward-line 1)
558 (point-at-bol)))))))
559 (nnheader-report 'nntp "Couldn't open connection to %s."
560 nntp-address))))
562 (defun nntp-send-command-and-decode (wait-for &rest strings)
563 "Send STRINGS to server and wait until WAIT-FOR returns."
564 (when (and (not nnheader-callback-function)
565 (not nntp-inhibit-output))
566 (nntp-erase-buffer nntp-server-buffer))
567 (let* ((command (mapconcat 'identity strings " "))
568 (process (nntp-find-connection nntp-server-buffer))
569 (buffer (and process (process-buffer process)))
570 (pos (and buffer (with-current-buffer buffer (point)))))
571 (if process
572 (prog1
573 (nntp-retrieve-data command
574 nntp-address nntp-port-number
575 nntp-server-buffer
576 wait-for nnheader-callback-function t)
577 ;; If nothing to wait for, still remove possibly echo'ed commands
578 (unless wait-for
579 (nntp-accept-response)
580 (with-current-buffer buffer
581 (goto-char pos)
582 (if (looking-at (regexp-quote command))
583 (delete-region pos (progn (forward-line 1) (point-at-bol))))
585 (nnheader-report 'nntp "Couldn't open connection to %s."
586 nntp-address))))
589 (defun nntp-send-buffer (wait-for)
590 "Send the current buffer to server and wait until WAIT-FOR returns."
591 (when (and (not nnheader-callback-function)
592 (not nntp-inhibit-output))
593 (nntp-erase-buffer
594 (nntp-find-connection-buffer nntp-server-buffer)))
595 (nntp-encode-text)
596 ;; Make sure we did not forget to encode some of the content.
597 (assert (save-excursion (goto-char (point-min))
598 (not (re-search-forward "[^\000-\377]" nil t))))
599 (mm-disable-multibyte)
600 (process-send-region (nntp-find-connection nntp-server-buffer)
601 (point-min) (point-max))
602 (nntp-retrieve-data
603 nil nntp-address nntp-port-number nntp-server-buffer
604 wait-for nnheader-callback-function))
608 ;;; Interface functions.
610 (nnoo-define-basics nntp)
612 (defsubst nntp-next-result-arrived-p ()
613 (cond
614 ;; A result that starts with a 2xx code is terminated by
615 ;; a line with only a "." on it.
616 ((eq (char-after) ?2)
617 (if (re-search-forward "\n\\.\r?\n" nil t)
618 (progn
619 ;; Some broken news servers add another dot at the end.
620 ;; Protect against inflooping there.
621 (while (looking-at "^\\.\r?\n")
622 (forward-line 1))
624 nil))
625 ;; A result that starts with a 3xx or 4xx code is terminated
626 ;; by a newline.
627 ((looking-at "[34]")
628 (if (search-forward "\n" nil t)
630 nil))
631 ;; No result here.
633 nil)))
635 (eval-when-compile
636 (defvar nntp-with-open-group-internal nil)
637 (defvar nntp-report-n nil))
639 (defun nntp-with-open-group-function (-group -server -connectionless -bodyfun)
640 "Protect against servers that don't like clients that keep idle connections opens.
641 The problem being that these servers may either close a connection or
642 simply ignore any further requests on a connection. Closed
643 connections are not detected until `accept-process-output' has updated
644 the `process-status'. Dropped connections are not detected until the
645 connection timeouts (which may be several minutes) or
646 `nntp-connection-timeout' has expired. When these occur
647 `nntp-with-open-group', opens a new connection then re-issues the NNTP
648 command whose response triggered the error."
649 (letf ((nntp-report-n (symbol-function 'nntp-report))
650 ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1))
651 (nntp-with-open-group-internal nil))
652 (while (catch 'nntp-with-open-group-error
653 ;; Open the connection to the server
654 ;; NOTE: Existing connections are NOT tested.
655 (nntp-possibly-change-group -group -server -connectionless)
657 (let ((-timer
658 (and nntp-connection-timeout
659 (run-at-time
660 nntp-connection-timeout nil
661 (lambda ()
662 (let* ((-process (nntp-find-connection
663 nntp-server-buffer))
664 (-buffer (and -process
665 (process-buffer -process))))
666 ;; When I an able to identify the
667 ;; connection to the server AND I've
668 ;; received NO reponse for
669 ;; nntp-connection-timeout seconds.
670 (when (and -buffer (eq 0 (buffer-size -buffer)))
671 ;; Close the connection. Take no
672 ;; other action as the accept input
673 ;; code will handle the closed
674 ;; connection.
675 (nntp-kill-buffer -buffer))))))))
676 (unwind-protect
677 (setq nntp-with-open-group-internal
678 (condition-case nil
679 (funcall -bodyfun)
680 (quit
681 (unless debug-on-quit
682 (nntp-close-server))
683 (signal 'quit nil))))
684 (when -timer
685 (nnheader-cancel-timer -timer)))
686 nil))
687 (setf (symbol-function 'nntp-report) nntp-report-n))
688 nntp-with-open-group-internal))
690 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
691 "Protect against servers that don't like clients that keep idle connections opens.
692 The problem being that these servers may either close a connection or
693 simply ignore any further requests on a connection. Closed
694 connections are not detected until `accept-process-output' has updated
695 the `process-status'. Dropped connections are not detected until the
696 connection timeouts (which may be several minutes) or
697 `nntp-connection-timeout' has expired. When these occur
698 `nntp-with-open-group', opens a new connection then re-issues the NNTP
699 command whose response triggered the error."
700 (declare (indent 2) (debug (form form [&optional symbolp] def-body)))
701 (when (and (listp connectionless)
702 (not (eq connectionless nil)))
703 (setq forms (cons connectionless forms)
704 connectionless nil))
705 `(nntp-with-open-group-function ,group ,server ,connectionless (lambda () ,@forms)))
707 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
708 "Retrieve the headers of ARTICLES."
709 (nntp-with-open-group
710 group server
711 (with-current-buffer (nntp-find-connection-buffer nntp-server-buffer)
712 (erase-buffer)
713 (if (and (not gnus-nov-is-evil)
714 (not nntp-nov-is-evil)
715 (nntp-retrieve-headers-with-xover articles fetch-old))
716 ;; We successfully retrieved the headers via XOVER.
717 'nov
718 ;; XOVER didn't work, so we do it the hard, slow and inefficient
719 ;; way.
720 (let ((number (length articles))
721 (articles articles)
722 (count 0)
723 (received 0)
724 (last-point (point-min))
725 (buf (nntp-find-connection-buffer nntp-server-buffer))
726 (nntp-inhibit-erase t)
727 article)
728 ;; Send HEAD commands.
729 (while (setq article (pop articles))
730 (nntp-send-command
732 "HEAD" (if (numberp article)
733 (int-to-string article)
734 ;; `articles' is either a list of article numbers
735 ;; or a list of article IDs.
736 article))
737 (incf count)
738 ;; Every 400 requests we have to read the stream in
739 ;; order to avoid deadlocks.
740 (when (or (null articles) ;All requests have been sent.
741 (zerop (% count nntp-maximum-request)))
742 (nntp-accept-response)
743 (while (progn
744 (set-buffer buf)
745 (goto-char last-point)
746 ;; Count replies.
747 (while (nntp-next-result-arrived-p)
748 (setq last-point (point))
749 (incf received))
750 (< received count))
751 ;; If number of headers is greater than 100, give
752 ;; informative messages.
753 (and (numberp nntp-large-newsgroup)
754 (> number nntp-large-newsgroup)
755 (zerop (% received 20))
756 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
757 (/ (* received 100) number)))
758 (nntp-accept-response))))
759 (and (numberp nntp-large-newsgroup)
760 (> number nntp-large-newsgroup)
761 (nnheader-message 6 "NNTP: Receiving headers...done"))
763 ;; Now all of replies are received. Fold continuation lines.
764 (nnheader-fold-continuation-lines)
765 ;; Remove all "\r"'s.
766 (nnheader-strip-cr)
767 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
768 'headers)))))
770 (deffoo nntp-retrieve-group-data-early (server infos)
771 "Retrieve group info on INFOS."
772 (nntp-with-open-group nil server
773 (when (nntp-find-connection-buffer nntp-server-buffer)
774 ;; The first time this is run, this variable is `try'. So we
775 ;; try.
776 (when (eq nntp-server-list-active-group 'try)
777 (nntp-try-list-active
778 (gnus-group-real-name (gnus-info-group (car infos)))))
779 (with-current-buffer (nntp-find-connection-buffer nntp-server-buffer)
780 (erase-buffer)
781 (let ((nntp-inhibit-erase t)
782 (command (if nntp-server-list-active-group
783 "LIST ACTIVE" "GROUP")))
784 (dolist (info infos)
785 (nntp-send-command
786 nil command (gnus-group-real-name (gnus-info-group info)))))
787 (length infos)))))
789 (deffoo nntp-finish-retrieve-group-infos (server infos count)
790 (nntp-with-open-group nil server
791 (let ((buf (nntp-find-connection-buffer nntp-server-buffer))
792 (method (gnus-find-method-for-group
793 (gnus-info-group (car infos))
794 (car infos)))
795 (received 0)
796 (last-point 1))
797 (when (and buf
798 count)
799 (with-current-buffer buf
800 (while (and (gnus-buffer-live-p buf)
801 (progn
802 (goto-char last-point)
803 ;; Count replies.
804 (while (re-search-forward
805 (if nntp-server-list-active-group
806 "^[.]"
807 "^[0-9]")
808 nil t)
809 (incf received))
810 (setq last-point (point))
811 (< received count)))
812 (nntp-accept-response))
813 ;; We now have all the entries. Remove CRs.
814 (nnheader-strip-cr)
815 (if (not nntp-server-list-active-group)
816 (progn
817 (nntp-copy-to-buffer nntp-server-buffer
818 (point-min) (point-max))
819 (gnus-groups-to-gnus-format method gnus-active-hashtb t))
820 ;; We have read active entries, so we just delete the
821 ;; superfluous gunk.
822 (goto-char (point-min))
823 (while (re-search-forward "^[.2-5]" nil t)
824 (delete-region (match-beginning 0)
825 (progn (forward-line 1) (point))))
826 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
827 (with-current-buffer nntp-server-buffer
828 (gnus-active-to-gnus-format
829 ;; Kludge to use the extended method name if you have
830 ;; an extended one.
831 (if (consp (gnus-info-method (car infos)))
832 (gnus-info-method (car infos))
833 method)
834 gnus-active-hashtb nil t))))))))
836 (deffoo nntp-retrieve-groups (groups &optional server)
837 "Retrieve group info on GROUPS."
838 (nntp-with-open-group
839 nil server
840 (when (nntp-find-connection-buffer nntp-server-buffer)
841 (catch 'done
842 (save-excursion
843 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
844 (nntp-erase-buffer nntp-server-buffer)
845 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
846 ;; The first time this is run, this variable is `try'. So we
847 ;; try.
848 (when (eq nntp-server-list-active-group 'try)
849 (nntp-try-list-active (car groups)))
850 (erase-buffer)
851 (let ((count 0)
852 (groups groups)
853 (received 0)
854 (last-point (point-min))
855 (nntp-inhibit-erase t)
856 (buf (nntp-find-connection-buffer nntp-server-buffer))
857 (command (if nntp-server-list-active-group
858 "LIST ACTIVE" "GROUP")))
859 (while groups
860 ;; Timeout may have killed the buffer.
861 (unless (gnus-buffer-live-p buf)
862 (nnheader-report 'nntp "Connection to %s is closed." server)
863 (throw 'done nil))
864 ;; Send the command to the server.
865 (nntp-send-command nil command (pop groups))
866 (incf count)
867 ;; Every 400 requests we have to read the stream in
868 ;; order to avoid deadlocks.
869 (when (or (null groups) ;All requests have been sent.
870 (zerop (% count nntp-maximum-request)))
871 (nntp-accept-response)
872 (while (and (gnus-buffer-live-p buf)
873 (progn
874 ;; Search `blue moon' in this file for the
875 ;; reason why set-buffer here.
876 (set-buffer buf)
877 (goto-char last-point)
878 ;; Count replies.
879 (while (re-search-forward "^[0-9]" nil t)
880 (incf received))
881 (setq last-point (point))
882 (< received count)))
883 (nntp-accept-response))))
885 ;; Wait for the reply from the final command.
886 (unless (gnus-buffer-live-p buf)
887 (nnheader-report 'nntp "Connection to %s is closed." server)
888 (throw 'done nil))
889 (set-buffer buf)
890 (goto-char (point-max))
891 (re-search-backward "^[0-9]" nil t)
892 (when (looking-at "^[23]")
893 (while (and (gnus-buffer-live-p buf)
894 (progn
895 (set-buffer buf)
896 (goto-char (point-max))
897 (if (not nntp-server-list-active-group)
898 (not (re-search-backward "\r?\n"
899 (- (point) 3) t))
900 (not (re-search-backward "^\\.\r?\n"
901 (- (point) 4) t)))))
902 (nntp-accept-response)))
904 ;; Now all replies are received. We remove CRs.
905 (unless (gnus-buffer-live-p buf)
906 (nnheader-report 'nntp "Connection to %s is closed." server)
907 (throw 'done nil))
908 (set-buffer buf)
909 (goto-char (point-min))
910 (while (search-forward "\r" nil t)
911 (replace-match "" t t))
913 (if (not nntp-server-list-active-group)
914 (progn
915 (nntp-copy-to-buffer nntp-server-buffer
916 (point-min) (point-max))
917 'group)
918 ;; We have read active entries, so we just delete the
919 ;; superfluous gunk.
920 (goto-char (point-min))
921 (while (re-search-forward "^[.2-5]" nil t)
922 (delete-region (match-beginning 0)
923 (progn (forward-line 1) (point))))
924 (nntp-copy-to-buffer nntp-server-buffer (point-min) (point-max))
925 'active)))))))
927 (deffoo nntp-retrieve-articles (articles &optional group server)
928 (nntp-with-open-group
929 group server
930 (save-excursion
931 (let ((number (length articles))
932 (articles articles)
933 (count 0)
934 (received 0)
935 (last-point (point-min))
936 (buf (nntp-find-connection-buffer nntp-server-buffer))
937 (nntp-inhibit-erase t)
938 (map (apply 'vector articles))
939 (point 1)
940 article)
941 (set-buffer buf)
942 (erase-buffer)
943 ;; Send ARTICLE command.
944 (while (setq article (pop articles))
945 (nntp-send-command
947 "ARTICLE" (if (numberp article)
948 (int-to-string article)
949 ;; `articles' is either a list of article numbers
950 ;; or a list of article IDs.
951 article))
952 (incf count)
953 ;; Every 400 requests we have to read the stream in
954 ;; order to avoid deadlocks.
955 (when (or (null articles) ;All requests have been sent.
956 (zerop (% count nntp-maximum-request)))
957 (nntp-accept-response)
958 (while (progn
959 (set-buffer buf)
960 (goto-char last-point)
961 ;; Count replies.
962 (while (nntp-next-result-arrived-p)
963 (aset map received (cons (aref map received) (point)))
964 (setq last-point (point))
965 (incf received))
966 (< received count))
967 ;; If number of headers is greater than 100, give
968 ;; informative messages.
969 (and (numberp nntp-large-newsgroup)
970 (> number nntp-large-newsgroup)
971 (zerop (% received 20))
972 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
973 (/ (* received 100) number)))
974 (nntp-accept-response))))
975 (and (numberp nntp-large-newsgroup)
976 (> number nntp-large-newsgroup)
977 (nnheader-message 6 "NNTP: Receiving articles...done"))
979 ;; Now we have all the responses. We go through the results,
980 ;; wash it and copy it over to the server buffer.
981 (set-buffer nntp-server-buffer)
982 (erase-buffer)
983 (setq last-point (point-min))
984 (mapcar
985 (lambda (entry)
986 (narrow-to-region
987 (setq point (goto-char (point-max)))
988 (progn
989 (nnheader-insert-buffer-substring buf last-point (cdr entry))
990 (point-max)))
991 (setq last-point (cdr entry))
992 (nntp-decode-text)
993 (widen)
994 (cons (car entry) point))
995 map)))))
997 (defun nntp-try-list-active (group)
998 (nntp-list-active-group group)
999 (with-current-buffer nntp-server-buffer
1000 (goto-char (point-min))
1001 (cond ((or (eobp)
1002 (looking-at "5[0-9]+"))
1003 (setq nntp-server-list-active-group nil))
1005 (setq nntp-server-list-active-group t)))))
1007 (deffoo nntp-list-active-group (group &optional server)
1008 "Return the active info on GROUP (which can be a regexp)."
1009 (nntp-with-open-group
1010 nil server
1011 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
1013 (deffoo nntp-request-group-articles (group &optional server)
1014 "Return the list of existing articles in GROUP."
1015 (nntp-with-open-group
1016 nil server
1017 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
1019 (deffoo nntp-request-article (article &optional group server buffer command)
1020 (nntp-with-open-group
1021 group server
1022 (when (nntp-send-command-and-decode
1023 "\r?\n\\.\r?\n" "ARTICLE"
1024 (if (numberp article) (int-to-string article) article))
1025 (when (and buffer
1026 (not (equal buffer nntp-server-buffer)))
1027 (with-current-buffer nntp-server-buffer
1028 (copy-to-buffer buffer (point-min) (point-max))))
1029 (nntp-find-group-and-number group))))
1031 (deffoo nntp-request-head (article &optional group server)
1032 (nntp-with-open-group
1033 group server
1034 (when (nntp-send-command
1035 "\r?\n\\.\r?\n" "HEAD"
1036 (if (numberp article) (int-to-string article) article))
1037 (prog1
1038 (nntp-find-group-and-number group)
1039 (nntp-decode-text)))))
1041 (deffoo nntp-request-body (article &optional group server)
1042 (nntp-with-open-group
1043 group server
1044 (nntp-send-command-and-decode
1045 "\r?\n\\.\r?\n" "BODY"
1046 (if (numberp article) (int-to-string article) article))))
1048 (deffoo nntp-request-group (group &optional server dont-check info)
1049 (nntp-with-open-group
1050 nil server
1051 (when (nntp-send-command "^[245].*\n" "GROUP" group)
1052 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1053 (setcar (cddr entry) group)))))
1055 (deffoo nntp-close-group (group &optional server)
1058 (deffoo nntp-server-opened (&optional server)
1059 "Say whether a connection to SERVER has been opened."
1060 (and (nnoo-current-server-p 'nntp server)
1061 nntp-server-buffer
1062 (gnus-buffer-live-p nntp-server-buffer)
1063 (nntp-find-connection nntp-server-buffer)))
1065 (deffoo nntp-open-server (server &optional defs connectionless)
1066 (nnheader-init-server-buffer)
1067 (if (nntp-server-opened server)
1069 (when (or (stringp (car defs))
1070 (numberp (car defs)))
1071 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
1072 (unless (assq 'nntp-address defs)
1073 (setq defs (append defs (list (list 'nntp-address server)))))
1074 (nnoo-change-server 'nntp server defs)
1075 (if connectionless
1077 (or (nntp-find-connection nntp-server-buffer)
1078 (nntp-open-connection nntp-server-buffer)))))
1080 (deffoo nntp-close-server (&optional server)
1081 (nntp-possibly-change-group nil server t)
1082 (let ((process (nntp-find-connection nntp-server-buffer)))
1083 (while process
1084 (when (memq (process-status process) '(open run))
1085 (ignore-errors
1086 (nntp-send-string process "QUIT")
1087 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
1088 ;; Ok, this is evil, but when using telnet and stuff
1089 ;; as the connection method, it's important that the
1090 ;; QUIT command actually is sent out before we kill
1091 ;; the process.
1092 (sleep-for 1))))
1093 (nntp-kill-buffer (process-buffer process))
1094 (setq process (car (pop nntp-connection-alist))))
1095 (nnoo-close-server 'nntp)))
1097 (deffoo nntp-request-close ()
1098 (let (process)
1099 (while (setq process (pop nntp-connection-list))
1100 (when (memq (process-status process) '(open run))
1101 (ignore-errors
1102 (nntp-send-string process "QUIT")
1103 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
1104 ;; Ok, this is evil, but when using telnet and stuff
1105 ;; as the connection method, it's important that the
1106 ;; QUIT command actually is sent out before we kill
1107 ;; the process.
1108 (sleep-for 1))))
1109 (nntp-kill-buffer (process-buffer process)))))
1111 (deffoo nntp-request-list (&optional server)
1112 (nntp-with-open-group
1113 nil server
1114 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
1116 (deffoo nntp-request-list-newsgroups (&optional server)
1117 (nntp-with-open-group
1118 nil server
1119 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
1121 (deffoo nntp-request-newgroups (date &optional server)
1122 (nntp-with-open-group
1123 nil server
1124 (with-current-buffer nntp-server-buffer
1125 (let* ((time (date-to-time date))
1126 (ls (- (cadr time) (nth 8 (decode-time time)))))
1127 (cond ((< ls 0)
1128 (setcar time (1- (car time)))
1129 (setcar (cdr time) (+ ls 65536)))
1130 ((>= ls 65536)
1131 (setcar time (1+ (car time)))
1132 (setcar (cdr time) (- ls 65536)))
1134 (setcar (cdr time) ls)))
1135 (prog1
1136 (nntp-send-command
1137 "^\\.\r?\n" "NEWGROUPS"
1138 (format-time-string "%y%m%d %H%M%S" time)
1139 "GMT")
1140 (nntp-decode-text))))))
1142 (deffoo nntp-request-post (&optional server)
1143 (nntp-with-open-group
1144 nil server
1145 (when (nntp-send-command "^[23].*\r?\n" "POST")
1146 (let ((response (with-current-buffer nntp-server-buffer
1147 nntp-process-response))
1148 server-id)
1149 (when (and response
1150 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
1151 response))
1152 (setq server-id (match-string 1 response))
1153 (narrow-to-region (goto-char (point-min))
1154 (if (search-forward "\n\n" nil t)
1155 (1- (point))
1156 (point-max)))
1157 (unless (mail-fetch-field "Message-ID")
1158 (goto-char (point-min))
1159 (insert "Message-ID: " server-id "\n"))
1160 (widen))
1161 (run-hooks 'nntp-prepare-post-hook)
1162 (nntp-send-buffer "^[23].*\n")))))
1164 (deffoo nntp-request-type (group article)
1165 'news)
1167 (deffoo nntp-asynchronous-p ()
1170 (deffoo nntp-request-set-mark (group actions &optional server)
1171 (when (and (not nntp-marks-is-evil)
1172 nntp-marks-file-name)
1173 (nntp-possibly-create-directory group server)
1174 (nntp-open-marks group server)
1175 (setq nntp-marks (nnheader-update-marks-actions nntp-marks actions))
1176 (nntp-save-marks group server))
1177 nil)
1179 (deffoo nntp-request-marks (group info &optional server)
1180 (when (and (not nntp-marks-is-evil)
1181 nntp-marks-file-name)
1182 (nntp-possibly-create-directory group server)
1183 (when (nntp-marks-changed-p group server)
1184 (nnheader-message 8 "Updating marks for %s..." group)
1185 (nntp-open-marks group server)
1186 ;; Update info using `nntp-marks'.
1187 (mapc (lambda (pred)
1188 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1189 (gnus-info-set-marks
1190 info
1191 (gnus-update-alist-soft
1192 (cdr pred)
1193 (cdr (assq (cdr pred) nntp-marks))
1194 (gnus-info-marks info))
1195 t)))
1196 gnus-article-mark-lists)
1197 (let ((seen (cdr (assq 'read nntp-marks))))
1198 (gnus-info-set-read info
1199 (if (and (integerp (car seen))
1200 (null (cdr seen)))
1201 (list (cons (car seen) (car seen)))
1202 seen)))
1203 (nnheader-message 8 "Updating marks for %s...done" group)))
1204 nil)
1208 ;;; Hooky functions.
1210 (defun nntp-send-mode-reader ()
1211 "Send the MODE READER command to the nntp server.
1212 This function is supposed to be called from `nntp-server-opened-hook'.
1213 It will make innd servers spawn an nnrpd process to allow actual article
1214 reading."
1215 (nntp-send-command "^.*\n" "MODE READER"))
1217 (declare-function netrc-parse "netrc" (&optional file))
1218 (declare-function netrc-machine "netrc"
1219 (list machine &optional port defaultport))
1220 (declare-function netrc-get "netrc" (alist type))
1222 (defun nntp-send-authinfo (&optional send-if-force)
1223 "Send the AUTHINFO to the nntp server.
1224 It will look in the \"~/.authinfo\" file for matching entries. If
1225 nothing suitable is found there, it will prompt for a user name
1226 and a password.
1228 If SEND-IF-FORCE, only send authinfo to the server if the
1229 .authinfo file has the FORCE token."
1230 (require 'netrc)
1231 (let* ((list (netrc-parse nntp-authinfo-file))
1232 (alist (netrc-machine list nntp-address "nntp"))
1233 (auth-info
1234 (nth 0 (auth-source-search :max 1
1235 ;; TODO: allow the virtual server name too
1236 :host nntp-address
1237 :port '("119" "nntp"))))
1238 (auth-user (plist-get auth-info :user))
1239 (auth-force (plist-get auth-info :force))
1240 (auth-passwd (plist-get auth-info :secret))
1241 (auth-passwd (if (functionp auth-passwd)
1242 (funcall auth-passwd)
1243 auth-passwd))
1244 (force (or (netrc-get alist "force")
1245 nntp-authinfo-force
1246 auth-force))
1247 (user (or
1248 ;; this is preferred to netrc-*
1249 auth-user
1250 (netrc-get alist "login")
1251 nntp-authinfo-user))
1252 (passwd (or
1253 ;; this is preferred to netrc-*
1254 auth-passwd
1255 (netrc-get alist "password"))))
1256 (when (or (not send-if-force)
1257 force)
1258 (unless user
1259 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1260 nntp-authinfo-user user))
1261 (unless (member user '(nil ""))
1262 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1263 (when t ;???Should check if AUTHINFO succeeded
1264 (nntp-send-command
1265 "^2.*\r?\n" "AUTHINFO PASS"
1266 (or passwd
1267 nntp-authinfo-password
1268 (setq nntp-authinfo-password
1269 (read-passwd (format "NNTP (%s@%s) password: "
1270 user nntp-address))))))))))
1272 (defun nntp-send-nosy-authinfo ()
1273 "Send the AUTHINFO to the nntp server."
1274 (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
1275 (unless (member user '(nil ""))
1276 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1277 (when t ;???Should check if AUTHINFO succeeded
1278 (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
1279 (read-passwd (format "NNTP (%s@%s) password: "
1280 user nntp-address)))))))
1282 (defun nntp-send-authinfo-from-file ()
1283 "Send the AUTHINFO to the nntp server.
1285 The authinfo login name is taken from the user's login name and the
1286 password contained in '~/.nntp-authinfo'."
1287 (when (file-exists-p "~/.nntp-authinfo")
1288 (with-temp-buffer
1289 (insert-file-contents "~/.nntp-authinfo")
1290 (goto-char (point-min))
1291 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
1292 (nntp-send-command
1293 "^2.*\r?\n" "AUTHINFO PASS"
1294 (buffer-substring (point) (point-at-eol))))))
1296 ;;; Internal functions.
1298 (defun nntp-handle-authinfo (process)
1299 "Take care of an authinfo response from the server."
1300 (let ((last nntp-last-command))
1301 (funcall nntp-authinfo-function)
1302 ;; We have to re-send the function that was interrupted by
1303 ;; the authinfo request.
1304 (nntp-erase-buffer nntp-server-buffer)
1305 (nntp-send-string process last)))
1307 (defun nntp-make-process-buffer (buffer)
1308 "Create a new, fresh buffer usable for nntp process connections."
1309 (with-current-buffer
1310 (generate-new-buffer
1311 (format " *server %s %s %s*"
1312 nntp-address nntp-port-number
1313 (gnus-buffer-exists-p buffer)))
1314 (mm-disable-multibyte)
1315 (set (make-local-variable 'after-change-functions) nil)
1316 (set (make-local-variable 'nntp-process-wait-for) nil)
1317 (set (make-local-variable 'nntp-process-callback) nil)
1318 (set (make-local-variable 'nntp-process-to-buffer) nil)
1319 (set (make-local-variable 'nntp-process-start-point) nil)
1320 (set (make-local-variable 'nntp-process-decode) nil)
1321 (current-buffer)))
1323 (defun nntp-open-connection (buffer)
1324 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1325 (run-hooks 'nntp-prepare-server-hook)
1326 (let* ((pbuffer (nntp-make-process-buffer buffer))
1327 (timer
1328 (and nntp-connection-timeout
1329 (run-at-time
1330 nntp-connection-timeout nil
1331 `(lambda ()
1332 (nntp-kill-buffer ,pbuffer)))))
1333 (process
1334 (condition-case err
1335 (let ((coding-system-for-read nntp-coding-system-for-read)
1336 (coding-system-for-write nntp-coding-system-for-write)
1337 (map '((nntp-open-network-stream network)
1338 (network-only plain) ; compat
1339 (nntp-open-plain-stream plain)
1340 (nntp-open-ssl-stream tls)
1341 (nntp-open-tls-stream tls))))
1342 (if (assoc nntp-open-connection-function map)
1343 (open-protocol-stream
1344 "nntpd" pbuffer nntp-address nntp-port-number
1345 :type (cadr (assoc nntp-open-connection-function map))
1346 :end-of-command "^\\([2345]\\|[.]\\).*\n"
1347 :capability-command "CAPABILITIES\r\n"
1348 :success "^3"
1349 :starttls-function
1350 (lambda (capabilities)
1351 (if (not (string-match "STARTTLS" capabilities))
1353 "STARTTLS\r\n")))
1354 (funcall nntp-open-connection-function pbuffer)))
1355 (error
1356 (nnheader-report 'nntp ">>> %s" err))
1357 (quit
1358 (message "Quit opening connection to %s" nntp-address)
1359 (nntp-kill-buffer pbuffer)
1360 (signal 'quit nil)
1361 nil))))
1362 (when timer
1363 (nnheader-cancel-timer timer))
1364 (when (and process
1365 (not (memq (process-status process) '(open run))))
1366 (setq process nil))
1367 (unless process
1368 (nntp-kill-buffer pbuffer))
1369 (when (and (buffer-name pbuffer)
1370 process)
1371 (when (and (fboundp 'set-network-process-option) ;; Unavailable in XEmacs.
1372 (fboundp 'process-type) ;; Emacs 22 doesn't provide it.
1373 (eq (process-type process) 'network))
1374 ;; Use TCP-keepalive so that connections that pass through a NAT router
1375 ;; don't hang when left idle.
1376 (set-network-process-option process :keepalive t))
1377 (gnus-set-process-query-on-exit-flag process nil)
1378 (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1379 (memq (process-status process) '(open run)))
1380 (prog1
1381 (caar (push (list process buffer nil) nntp-connection-alist))
1382 (push process nntp-connection-list)
1383 (with-current-buffer pbuffer
1384 (nntp-read-server-type)
1385 (erase-buffer)
1386 (set-buffer nntp-server-buffer)
1387 (let ((nnheader-callback-function nil))
1388 (run-hooks 'nntp-server-opened-hook)
1389 (nntp-send-authinfo t))))
1390 (nntp-kill-buffer (process-buffer process))
1391 nil))))
1393 (defun nntp-read-server-type ()
1394 "Find out what the name of the server we have connected to is."
1395 ;; Wait for the status string to arrive.
1396 (setq nntp-server-type (buffer-string))
1397 (let ((case-fold-search t))
1398 ;; Run server-specific commands.
1399 (dolist (entry nntp-server-action-alist)
1400 (when (string-match (car entry) nntp-server-type)
1401 (if (and (listp (cadr entry))
1402 (not (eq 'lambda (caadr entry))))
1403 (eval (cadr entry))
1404 (funcall (cadr entry)))))))
1406 (defun nntp-async-wait (process wait-for buffer decode callback)
1407 (with-current-buffer (process-buffer process)
1408 (unless nntp-inside-change-function
1409 (erase-buffer))
1410 (setq nntp-process-wait-for wait-for
1411 nntp-process-to-buffer buffer
1412 nntp-process-decode decode
1413 nntp-process-callback callback
1414 nntp-process-start-point (point-max))
1415 (setq after-change-functions '(nntp-after-change-function))))
1417 (defun nntp-async-timer-handler ()
1418 (mapcar
1419 (lambda (proc)
1420 (if (memq (process-status proc) '(open run))
1421 (nntp-async-trigger proc)
1422 (nntp-async-stop proc)))
1423 nntp-async-process-list))
1425 (defun nntp-async-stop (proc)
1426 (setq nntp-async-process-list (delq proc nntp-async-process-list))
1427 (when (and nntp-async-timer (not nntp-async-process-list))
1428 (nnheader-cancel-timer nntp-async-timer)
1429 (setq nntp-async-timer nil)))
1431 (defun nntp-after-change-function (beg end len)
1432 (unwind-protect
1433 ;; we only care about insertions at eob
1434 (when (and (eq 0 len) (eq (point-max) end))
1435 (save-match-data
1436 (let ((proc (get-buffer-process (current-buffer))))
1437 (when proc
1438 (nntp-async-trigger proc)))))
1439 ;; any throw from after-change-functions will leave it
1440 ;; set to nil. so we reset it here, if necessary.
1441 (when quit-flag
1442 (setq after-change-functions '(nntp-after-change-function)))))
1444 (defun nntp-async-trigger (process)
1445 (with-current-buffer (process-buffer process)
1446 (when nntp-process-callback
1447 ;; do we have an error message?
1448 (goto-char nntp-process-start-point)
1449 (if (memq (following-char) '(?4 ?5))
1450 ;; wants credentials?
1451 (if (looking-at "480")
1452 (nntp-handle-authinfo process)
1453 ;; report error message.
1454 (nntp-snarf-error-message)
1455 (nntp-do-callback nil))
1457 ;; got what we expect?
1458 (goto-char (point-max))
1459 (when (re-search-backward
1460 nntp-process-wait-for nntp-process-start-point t)
1461 (let ((response (match-string 0)))
1462 (with-current-buffer nntp-server-buffer
1463 (setq nntp-process-response response)))
1464 (nntp-async-stop process)
1465 ;; convert it.
1466 (when (gnus-buffer-exists-p nntp-process-to-buffer)
1467 (let ((buf (current-buffer))
1468 (start nntp-process-start-point)
1469 (decode nntp-process-decode))
1470 (with-current-buffer nntp-process-to-buffer
1471 (goto-char (point-max))
1472 (save-restriction
1473 (narrow-to-region (point) (point))
1474 (nnheader-insert-buffer-substring buf start)
1475 (when decode
1476 (nntp-decode-text))))))
1477 ;; report it.
1478 (goto-char (point-max))
1479 (nntp-do-callback
1480 (buffer-name (get-buffer nntp-process-to-buffer))))))))
1482 (defun nntp-do-callback (arg)
1483 (let ((callback nntp-process-callback)
1484 (nntp-inside-change-function t))
1485 (setq nntp-process-callback nil)
1486 (funcall callback arg)))
1488 (defun nntp-snarf-error-message ()
1489 "Save the error message in the current buffer."
1490 (let ((message (buffer-string)))
1491 (while (string-match "[\r\n]+" message)
1492 (setq message (replace-match " " t t message)))
1493 (nnheader-report 'nntp "%s" message)
1494 message))
1496 (defun nntp-accept-process-output (process)
1497 "Wait for output from PROCESS and message some dots."
1498 (with-current-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1499 nntp-server-buffer)
1500 (let ((len (/ (buffer-size) 1024))
1501 message-log-max)
1502 (unless (< len 10)
1503 (setq nntp-have-messaged t)
1504 (nnheader-message 7 "nntp read: %dk" len)))
1505 (prog1
1506 (nnheader-accept-process-output process)
1507 ;; accept-process-output may update status of process to indicate
1508 ;; that the server has closed the connection. This MUST be
1509 ;; handled here as the buffer restored by the save-excursion may
1510 ;; be the process's former output buffer (i.e. now killed)
1511 (or (and process
1512 (memq (process-status process) '(open run)))
1513 (nntp-report "Server closed connection")))))
1515 (defun nntp-accept-response ()
1516 "Wait for output from the process that outputs to BUFFER."
1517 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1519 (defun nntp-possibly-change-group (group server &optional connectionless)
1520 (let ((nnheader-callback-function nil))
1521 (when server
1522 (or (nntp-server-opened server)
1523 (nntp-open-server server nil connectionless)))
1525 (unless connectionless
1526 (or (nntp-find-connection nntp-server-buffer)
1527 (nntp-open-connection nntp-server-buffer))))
1529 (when group
1530 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1531 (cond ((not entry)
1532 (nntp-report "Server closed connection"))
1533 ((not (equal group (caddr entry)))
1534 (with-current-buffer (process-buffer (car entry))
1535 (erase-buffer)
1536 (nntp-send-command "^[245].*\n" "GROUP" group)
1537 (setcar (cddr entry) group)
1538 (erase-buffer)
1539 (nntp-erase-buffer nntp-server-buffer)))))))
1541 (defun nntp-decode-text (&optional cr-only)
1542 "Decode the text in the current buffer."
1543 (goto-char (point-min))
1544 (while (search-forward "\r" nil t)
1545 (delete-char -1))
1546 (unless cr-only
1547 ;; Remove trailing ".\n" end-of-transfer marker.
1548 (goto-char (point-max))
1549 (forward-line -1)
1550 (when (looking-at ".\n")
1551 (delete-char 2))
1552 ;; Delete status line.
1553 (goto-char (point-min))
1554 (while (looking-at "[1-5][0-9][0-9] .*\n")
1555 ;; For some unknown reason, there is more than one status line.
1556 (delete-region (point) (progn (forward-line 1) (point))))
1557 ;; Remove "." -> ".." encoding.
1558 (while (search-forward "\n.." nil t)
1559 (delete-char -1))))
1561 (defun nntp-encode-text ()
1562 "Encode the text in the current buffer."
1563 (save-excursion
1564 ;; Replace "." at beginning of line with "..".
1565 (goto-char (point-min))
1566 (while (re-search-forward "^\\." nil t)
1567 (insert "."))
1568 (goto-char (point-max))
1569 ;; Insert newline at the end of the buffer.
1570 (unless (bolp)
1571 (insert "\n"))
1572 ;; Insert `.' at end of buffer (end of text mark).
1573 (goto-char (point-max))
1574 (insert ".\n")
1575 (goto-char (point-min))
1576 (while (not (eobp))
1577 (end-of-line)
1578 (delete-char 1)
1579 (insert nntp-end-of-line))))
1581 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1582 (set-buffer nntp-server-buffer)
1583 (erase-buffer)
1584 (cond
1586 ;; This server does not talk NOV.
1587 ((not nntp-server-xover)
1588 nil)
1590 ;; We don't care about gaps.
1591 ((or (not nntp-nov-gap)
1592 fetch-old)
1593 (nntp-send-xover-command
1594 (if fetch-old
1595 (if (numberp fetch-old)
1596 (max 1 (- (car articles) fetch-old))
1598 (car articles))
1599 (car (last articles)) 'wait)
1601 (goto-char (point-min))
1602 (when (looking-at "[1-5][0-9][0-9] .*\n")
1603 (delete-region (point) (progn (forward-line 1) (point))))
1604 (while (search-forward "\r" nil t)
1605 (replace-match "" t t))
1606 (goto-char (point-max))
1607 (forward-line -1)
1608 (when (looking-at "\\.")
1609 (delete-region (point) (progn (forward-line 1) (point)))))
1611 ;; We do it the hard way. For each gap, an XOVER command is sent
1612 ;; to the server. We do not wait for a reply from the server, we
1613 ;; just send them off as fast as we can. That means that we have
1614 ;; to count the number of responses we get back to find out when we
1615 ;; have gotten all we asked for.
1616 ((numberp nntp-nov-gap)
1617 (let ((count 0)
1618 (received 0)
1619 last-point
1620 in-process-buffer-p
1621 (buf nntp-server-buffer)
1622 (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1623 first last status)
1624 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1625 ;; that means that the server does not understand XOVER, but we
1626 ;; won't know that until we try.
1627 (while (and nntp-server-xover articles)
1628 (setq first (car articles))
1629 ;; Search forward until we find a gap, or until we run out of
1630 ;; articles.
1631 (while (and (cdr articles)
1632 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1633 (setq articles (cdr articles)))
1635 (setq in-process-buffer-p (stringp nntp-server-xover))
1636 (nntp-send-xover-command first (setq last (car articles)))
1637 (setq articles (cdr articles))
1639 (when (and nntp-server-xover in-process-buffer-p)
1640 ;; Don't count tried request.
1641 (setq count (1+ count))
1643 ;; Every 400 requests we have to read the stream in
1644 ;; order to avoid deadlocks.
1645 (when (or (null articles) ;All requests have been sent.
1646 (= 1 (% count nntp-maximum-request)))
1648 (nntp-accept-response)
1649 ;; On some Emacs versions the preceding function has a
1650 ;; tendency to change the buffer. Perhaps. It's quite
1651 ;; difficult to reproduce, because it only seems to happen
1652 ;; once in a blue moon.
1653 (set-buffer process-buffer)
1654 (while (progn
1655 (goto-char (or last-point (point-min)))
1656 ;; Count replies.
1657 (while (re-search-forward "^\\([0-9][0-9][0-9]\\) .*\n"
1658 nil t)
1659 (incf received)
1660 (setq status (match-string 1))
1661 (if (string-match "^[45]" status)
1662 (setq status 'error)
1663 (setq status 'ok)))
1664 (setq last-point (point))
1665 (or (< received count)
1666 (if (eq status 'error)
1668 ;; I haven't started reading the final response
1669 (progn
1670 (goto-char (point-max))
1671 (forward-line -1)
1672 (not (looking-at "^\\.\r?\n"))))))
1673 ;; I haven't read the end of the final response
1674 (nntp-accept-response)
1675 (set-buffer process-buffer))))
1677 ;; Some nntp servers seem to have an extension to the XOVER
1678 ;; extension. On these servers, requesting an article range
1679 ;; preceding the active range does not return an error as
1680 ;; specified in the RFC. What we instead get is the NOV entry
1681 ;; for the first available article. Obviously, a client can
1682 ;; use that entry to avoid making unnecessary requests. The
1683 ;; only problem is for a client that assumes that the response
1684 ;; will always be within the requested ranage. For such a
1685 ;; client, we can get N copies of the same entry (one for each
1686 ;; XOVER command sent to the server).
1688 (when (<= count 1)
1689 (goto-char (point-min))
1690 (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1691 (let ((low-limit (string-to-number
1692 (buffer-substring (match-beginning 1)
1693 (match-end 1)))))
1694 (while (and articles (<= (car articles) low-limit))
1695 (setq articles (cdr articles))))))
1696 (set-buffer buf))
1698 (when nntp-server-xover
1699 (when in-process-buffer-p
1700 (set-buffer buf)
1701 (goto-char (point-max))
1702 (nnheader-insert-buffer-substring process-buffer)
1703 (set-buffer process-buffer)
1704 (erase-buffer)
1705 (set-buffer buf))
1707 ;; We remove any "." lines and status lines.
1708 (goto-char (point-min))
1709 (while (search-forward "\r" nil t)
1710 (delete-char -1))
1711 (goto-char (point-min))
1712 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1713 t))))
1715 nntp-server-xover)
1717 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1718 "Send the XOVER command to the server."
1719 (let ((range (format "%d-%d" beg end))
1720 (nntp-inhibit-erase t))
1721 (if (stringp nntp-server-xover)
1722 ;; If `nntp-server-xover' is a string, then we just send this
1723 ;; command.
1724 (if wait-for-reply
1725 (nntp-send-command-nodelete
1726 "\r?\n\\.\r?\n" nntp-server-xover range)
1727 ;; We do not wait for the reply.
1728 (nntp-send-command-nodelete nil nntp-server-xover range))
1729 (let ((commands nntp-xover-commands))
1730 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1731 ;; We try them all until we get at positive response.
1732 (while (and commands (eq nntp-server-xover 'try))
1733 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1734 (with-current-buffer nntp-server-buffer
1735 (goto-char (point-min))
1736 (and (looking-at "[23]") ; No error message.
1737 ;; We also have to look at the lines. Some buggy
1738 ;; servers give back simple lines with just the
1739 ;; article number. How... helpful.
1740 (progn
1741 (forward-line 1)
1742 ;; More text after number, or a dot.
1743 (looking-at "[0-9]+\t...\\|\\.\r?\n"))
1744 (setq nntp-server-xover (car commands))))
1745 (setq commands (cdr commands)))
1746 ;; If none of the commands worked, we disable XOVER.
1747 (when (eq nntp-server-xover 'try)
1748 (nntp-erase-buffer nntp-server-buffer)
1749 (setq nntp-server-xover nil))
1750 nntp-server-xover))))
1752 (defun nntp-find-group-and-number (&optional group)
1753 (save-excursion
1754 (save-restriction
1755 ;; FIXME: This is REALLY FISHY: set-buffer after save-restriction?!?
1756 (set-buffer nntp-server-buffer)
1757 (narrow-to-region (goto-char (point-min))
1758 (or (search-forward "\n\n" nil t) (point-max)))
1759 (goto-char (point-min))
1760 ;; We first find the number by looking at the status line.
1761 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1762 (string-to-number
1763 (buffer-substring (match-beginning 1)
1764 (match-end 1)))))
1765 newsgroups xref)
1766 (and number (zerop number) (setq number nil))
1767 (if number
1768 ;; Then we find the group name.
1769 (setq group
1770 (cond
1771 ;; If there is only one group in the Newsgroups
1772 ;; header, then it seems quite likely that this
1773 ;; article comes from that group, I'd say.
1774 ((and (setq newsgroups
1775 (mail-fetch-field "newsgroups"))
1776 (not (string-match "," newsgroups)))
1777 newsgroups)
1778 ;; If there is more than one group in the
1779 ;; Newsgroups header, then the Xref header should
1780 ;; be filled out. We hazard a guess that the group
1781 ;; that has this article number in the Xref header
1782 ;; is the one we are looking for. This might very
1783 ;; well be wrong if this article happens to have
1784 ;; the same number in several groups, but that's
1785 ;; life.
1786 ((and (setq xref (mail-fetch-field "xref"))
1787 number
1788 (string-match
1789 (format "\\([^ :]+\\):%d" number) xref))
1790 (match-string 1 xref))
1791 (t "")))
1792 (cond
1793 ((and (not nntp-xref-number-is-evil)
1794 (setq xref (mail-fetch-field "xref"))
1795 (string-match
1796 (if group
1797 (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1798 "\\([^ :]+\\):\\([0-9]+\\)")
1799 xref))
1800 (setq group (match-string 1 xref)
1801 number (string-to-number (match-string 2 xref))))
1802 ((and (setq newsgroups
1803 (mail-fetch-field "newsgroups"))
1804 (not (string-match "," newsgroups)))
1805 (setq group newsgroups))
1806 (group)
1807 (t (setq group ""))))
1808 (when (string-match "\r" group)
1809 (setq group (substring group 0 (match-beginning 0))))
1810 (cons group number)))))
1812 (defun nntp-wait-for-string (regexp)
1813 "Wait until string arrives in the buffer."
1814 (let ((buf (current-buffer))
1815 proc)
1816 (goto-char (point-min))
1817 (while (and (setq proc (get-buffer-process buf))
1818 (memq (process-status proc) '(open run))
1819 (not (re-search-forward regexp nil t)))
1820 (accept-process-output proc 0.1)
1821 (set-buffer buf)
1822 (goto-char (point-min)))))
1825 ;; ==========================================================================
1826 ;; Obsolete nntp-open-* connection methods -- drv
1827 ;; ==========================================================================
1829 (defvoo nntp-open-telnet-envuser nil
1830 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1832 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
1833 "*Regular expression to match the shell prompt on the remote machine.")
1835 (defvoo nntp-rlogin-program "rsh"
1836 "*Program used to log in on remote machines.
1837 The default is \"rsh\", but \"ssh\" is a popular alternative.")
1839 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1840 "*Parameters to `nntp-open-rlogin'.
1841 That function may be used as `nntp-open-connection-function'. In that
1842 case, this list will be used as the parameter list given to rsh.")
1844 (defvoo nntp-rlogin-user-name nil
1845 "*User name on remote system when using the rlogin connect method.")
1847 (defvoo nntp-telnet-parameters
1848 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1849 "*Parameters to `nntp-open-telnet'.
1850 That function may be used as `nntp-open-connection-function'. In that
1851 case, this list will be executed as a command after logging in
1852 via telnet.")
1854 (defvoo nntp-telnet-user-name nil
1855 "User name to log in via telnet with.")
1857 (defvoo nntp-telnet-passwd nil
1858 "Password to use to log in via telnet with.")
1860 (defun nntp-service-to-port (svc)
1861 (cond
1862 ((integerp svc) (number-to-string svc))
1863 ((string-match "\\`[0-9]+\\'" svc) svc)
1865 (with-temp-buffer
1866 (ignore-errors (insert-file-contents "/etc/services"))
1867 (goto-char (point-min))
1868 (if (re-search-forward (concat "^" (regexp-quote svc)
1869 "[ \t]+\\([0-9]+\\)/tcp"))
1870 (match-string 1)
1871 svc)))))
1873 (defun nntp-open-telnet (buffer)
1874 (with-current-buffer buffer
1875 (erase-buffer)
1876 (let ((proc (apply
1877 'start-process
1878 "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1879 (case-fold-search t))
1880 (when (memq (process-status proc) '(open run))
1881 (nntp-wait-for-string "^r?telnet")
1882 (process-send-string proc "set escape \^X\n")
1883 (cond
1884 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1885 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1886 nntp-address "\n")))
1888 (process-send-string proc (concat "open " nntp-address "\n"))))
1889 (cond
1890 ((not nntp-open-telnet-envuser)
1891 (nntp-wait-for-string "^\r*.?login:")
1892 (process-send-string
1893 proc (concat
1894 (or nntp-telnet-user-name
1895 (setq nntp-telnet-user-name (read-string "login: ")))
1896 "\n"))))
1897 (nntp-wait-for-string "^\r*.?password:")
1898 (process-send-string
1899 proc (concat
1900 (or nntp-telnet-passwd
1901 (setq nntp-telnet-passwd
1902 (read-passwd "Password: ")))
1903 "\n"))
1904 (nntp-wait-for-string nntp-telnet-shell-prompt)
1905 (process-send-string
1906 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1907 (nntp-wait-for-string "^\r*20[01]")
1908 (beginning-of-line)
1909 (delete-region (point-min) (point))
1910 (process-send-string proc "\^]")
1911 (nntp-wait-for-string "^r?telnet")
1912 (process-send-string proc "mode character\n")
1913 (accept-process-output proc 1)
1914 (sit-for 1)
1915 (goto-char (point-min))
1916 (forward-line 1)
1917 (delete-region (point) (point-max)))
1918 proc)))
1920 (defun nntp-open-rlogin (buffer)
1921 "Open a connection to SERVER using rsh."
1922 (let ((proc (if nntp-rlogin-user-name
1923 (apply 'start-process
1924 "nntpd" buffer nntp-rlogin-program
1925 nntp-address "-l" nntp-rlogin-user-name
1926 nntp-rlogin-parameters)
1927 (apply 'start-process
1928 "nntpd" buffer nntp-rlogin-program nntp-address
1929 nntp-rlogin-parameters))))
1930 (with-current-buffer buffer
1931 (nntp-wait-for-string "^\r*20[01]")
1932 (beginning-of-line)
1933 (delete-region (point-min) (point))
1934 proc)))
1937 ;; ==========================================================================
1938 ;; Replacements for the nntp-open-* functions -- drv
1939 ;; ==========================================================================
1941 (defun nntp-open-telnet-stream (buffer)
1942 "Open a nntp connection by telnet'ing the news server.
1943 `nntp-open-netcat-stream' is recommended in place of this function
1944 because it is more reliable.
1946 Please refer to the following variables to customize the connection:
1947 - `nntp-pre-command',
1948 - `nntp-telnet-command',
1949 - `nntp-telnet-switches',
1950 - `nntp-address',
1951 - `nntp-port-number',
1952 - `nntp-end-of-line'."
1953 (let ((command `(,nntp-telnet-command
1954 ,@nntp-telnet-switches
1955 ,nntp-address
1956 ,(nntp-service-to-port nntp-port-number)))
1957 proc)
1958 (and nntp-pre-command
1959 (push nntp-pre-command command))
1960 (setq proc (apply 'start-process "nntpd" buffer command))
1961 (with-current-buffer buffer
1962 (nntp-wait-for-string "^\r*20[01]")
1963 (beginning-of-line)
1964 (delete-region (point-min) (point))
1965 proc)))
1967 (defun nntp-open-via-rlogin-and-telnet (buffer)
1968 "Open a connection to an nntp server through an intermediate host.
1969 First rlogin to the remote host, and then telnet the real news server
1970 from there.
1971 `nntp-open-via-rlogin-and-netcat' is recommended in place of this function
1972 because it is more reliable.
1974 Please refer to the following variables to customize the connection:
1975 - `nntp-pre-command',
1976 - `nntp-via-rlogin-command',
1977 - `nntp-via-rlogin-command-switches',
1978 - `nntp-via-user-name',
1979 - `nntp-via-address',
1980 - `nntp-telnet-command',
1981 - `nntp-telnet-switches',
1982 - `nntp-address',
1983 - `nntp-port-number',
1984 - `nntp-end-of-line'."
1985 (let ((command `(,nntp-via-address
1986 ,nntp-telnet-command
1987 ,@nntp-telnet-switches))
1988 proc)
1989 (when nntp-via-user-name
1990 (setq command `("-l" ,nntp-via-user-name ,@command)))
1991 (when nntp-via-rlogin-command-switches
1992 (setq command (append nntp-via-rlogin-command-switches command)))
1993 (push nntp-via-rlogin-command command)
1994 (and nntp-pre-command
1995 (push nntp-pre-command command))
1996 (setq proc (apply 'start-process "nntpd" buffer command))
1997 (with-current-buffer buffer
1998 (nntp-wait-for-string "^r?telnet")
1999 (process-send-string proc (concat "open " nntp-address " "
2000 (nntp-service-to-port nntp-port-number)
2001 "\n"))
2002 (nntp-wait-for-string "^\r*20[01]")
2003 (beginning-of-line)
2004 (delete-region (point-min) (point))
2005 (process-send-string proc "\^]")
2006 (nntp-wait-for-string "^r?telnet")
2007 (process-send-string proc "mode character\n")
2008 (accept-process-output proc 1)
2009 (sit-for 1)
2010 (goto-char (point-min))
2011 (forward-line 1)
2012 (delete-region (point) (point-max)))
2013 proc))
2015 (defun nntp-open-via-rlogin-and-netcat (buffer)
2016 "Open a connection to an nntp server through an intermediate host.
2017 First rlogin to the remote host, and then connect to the real news
2018 server from there using the netcat command.
2020 Please refer to the following variables to customize the connection:
2021 - `nntp-pre-command',
2022 - `nntp-via-rlogin-command',
2023 - `nntp-via-rlogin-command-switches',
2024 - `nntp-via-user-name',
2025 - `nntp-via-address',
2026 - `nntp-netcat-command',
2027 - `nntp-netcat-switches',
2028 - `nntp-address',
2029 - `nntp-port-number'."
2030 (let ((command `(,@(when nntp-pre-command
2031 (list nntp-pre-command))
2032 ,nntp-via-rlogin-command
2033 ,@nntp-via-rlogin-command-switches
2034 ,@(when nntp-via-user-name
2035 (list "-l" nntp-via-user-name))
2036 ,nntp-via-address
2037 ,nntp-netcat-command
2038 ,@nntp-netcat-switches
2039 ,nntp-address
2040 ,(nntp-service-to-port nntp-port-number))))
2041 ;; A non-nil connection type results in mightily odd behavior where
2042 ;; (process-send-string proc "\^M") ends up sending a "\n" to the
2043 ;; ssh process. --Stef
2044 ;; Also a nil connection allow ssh-askpass to work under X11.
2045 (let ((process-connection-type nil))
2046 (apply 'start-process "nntpd" buffer command))))
2048 (defun nntp-open-netcat-stream (buffer)
2049 "Open a connection to an nntp server through netcat.
2050 I.e. use the `nc' command rather than Emacs's builtin networking code.
2052 Please refer to the following variables to customize the connection:
2053 - `nntp-pre-command',
2054 - `nntp-netcat-command',
2055 - `nntp-netcat-switches',
2056 - `nntp-address',
2057 - `nntp-port-number'."
2058 (let ((command `(,nntp-netcat-command
2059 ,@nntp-netcat-switches
2060 ,nntp-address
2061 ,(nntp-service-to-port nntp-port-number))))
2062 (and nntp-pre-command (push nntp-pre-command command))
2063 (let ((process-connection-type nil)) ;See `nntp-open-via-rlogin-and-netcat'.
2064 (apply 'start-process "nntpd" buffer command))))
2067 (defun nntp-open-via-telnet-and-telnet (buffer)
2068 "Open a connection to an nntp server through an intermediate host.
2069 First telnet the remote host, and then telnet the real news server
2070 from there.
2072 Please refer to the following variables to customize the connection:
2073 - `nntp-pre-command',
2074 - `nntp-via-telnet-command',
2075 - `nntp-via-telnet-switches',
2076 - `nntp-via-address',
2077 - `nntp-via-envuser',
2078 - `nntp-via-user-name',
2079 - `nntp-via-user-password',
2080 - `nntp-via-shell-prompt',
2081 - `nntp-telnet-command',
2082 - `nntp-telnet-switches',
2083 - `nntp-address',
2084 - `nntp-port-number',
2085 - `nntp-end-of-line'."
2086 (with-current-buffer buffer
2087 (erase-buffer)
2088 (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
2089 (case-fold-search t)
2090 proc)
2091 (and nntp-pre-command (push nntp-pre-command command))
2092 (setq proc (apply 'start-process "nntpd" buffer command))
2093 (when (memq (process-status proc) '(open run))
2094 (nntp-wait-for-string "^r?telnet")
2095 (process-send-string proc "set escape \^X\n")
2096 (cond
2097 ((and nntp-via-envuser nntp-via-user-name)
2098 (process-send-string proc (concat "open " "-l" nntp-via-user-name
2099 nntp-via-address "\n")))
2101 (process-send-string proc (concat "open " nntp-via-address
2102 "\n"))))
2103 (when (not nntp-via-envuser)
2104 (nntp-wait-for-string "^\r*.?login:")
2105 (process-send-string proc
2106 (concat
2107 (or nntp-via-user-name
2108 (setq nntp-via-user-name
2109 (read-string "login: ")))
2110 "\n")))
2111 (nntp-wait-for-string "^\r*.?password:")
2112 (process-send-string proc
2113 (concat
2114 (or nntp-via-user-password
2115 (setq nntp-via-user-password
2116 (read-passwd "Password: ")))
2117 "\n"))
2118 (nntp-wait-for-string nntp-via-shell-prompt)
2119 (let ((real-telnet-command `("exec"
2120 ,nntp-telnet-command
2121 ,@nntp-telnet-switches
2122 ,nntp-address
2123 ,(nntp-service-to-port nntp-port-number))))
2124 (process-send-string proc
2125 (concat (mapconcat 'identity
2126 real-telnet-command " ")
2127 "\n")))
2128 (nntp-wait-for-string "^\r*20[01]")
2129 (beginning-of-line)
2130 (delete-region (point-min) (point))
2131 (process-send-string proc "\^]")
2132 (nntp-wait-for-string "^r?telnet")
2133 (process-send-string proc "mode character\n")
2134 (accept-process-output proc 1)
2135 (sit-for 1)
2136 (goto-char (point-min))
2137 (forward-line 1)
2138 (delete-region (point) (point-max)))
2139 proc)))
2141 ;; Marks handling
2143 (defun nntp-marks-directory (server)
2144 (expand-file-name server nntp-marks-directory))
2146 (defvar nntp-server-to-method-cache nil
2147 "Alist of servers and select methods.")
2149 (defun nntp-group-pathname (server group &optional file)
2150 "Return an absolute file name of FILE for GROUP on SERVER."
2151 (let ((method (cdr (assoc server nntp-server-to-method-cache))))
2152 (unless method
2153 (push (cons server (setq method (or (gnus-server-to-method server)
2154 (gnus-find-method-for-group group))))
2155 nntp-server-to-method-cache))
2156 (nnmail-group-pathname
2157 (mm-decode-coding-string group
2158 (inline (gnus-group-name-charset method group)))
2159 (nntp-marks-directory server)
2160 file)))
2162 (defun nntp-possibly-create-directory (group server)
2163 (let ((dir (nntp-group-pathname server group))
2164 (file-name-coding-system nnmail-pathname-coding-system))
2165 (unless (file-exists-p dir)
2166 (make-directory (directory-file-name dir) t)
2167 (nnheader-message 5 "Creating nntp marks directory %s" dir))))
2169 (autoload 'time-less-p "time-date")
2171 (defun nntp-marks-changed-p (group server)
2172 (let ((file (nntp-group-pathname server group nntp-marks-file-name))
2173 (file-name-coding-system nnmail-pathname-coding-system))
2174 (if (null (gnus-gethash file nntp-marks-modtime))
2175 t ;; never looked at marks file, assume it has changed
2176 (time-less-p (gnus-gethash file nntp-marks-modtime)
2177 (nth 5 (file-attributes file))))))
2179 (defun nntp-save-marks (group server)
2180 (let ((file-name-coding-system nnmail-pathname-coding-system)
2181 (file (nntp-group-pathname server group nntp-marks-file-name)))
2182 (condition-case err
2183 (progn
2184 (nntp-possibly-create-directory group server)
2185 (with-temp-file file
2186 (erase-buffer)
2187 (gnus-prin1 nntp-marks)
2188 (insert "\n"))
2189 (gnus-sethash file
2190 (nth 5 (file-attributes file))
2191 nntp-marks-modtime))
2192 (error (or (gnus-yes-or-no-p
2193 (format "Could not write to %s (%s). Continue? " file err))
2194 (error "Cannot write to %s (%s)" file err))))))
2196 (defun nntp-open-marks (group server)
2197 (let ((file (nntp-group-pathname server group nntp-marks-file-name))
2198 (file-name-coding-system nnmail-pathname-coding-system))
2199 (if (file-exists-p file)
2200 (condition-case err
2201 (with-temp-buffer
2202 (gnus-sethash file (nth 5 (file-attributes file))
2203 nntp-marks-modtime)
2204 (nnheader-insert-file-contents file)
2205 (setq nntp-marks (read (current-buffer)))
2206 (dolist (el gnus-article-unpropagated-mark-lists)
2207 (setq nntp-marks (gnus-remassoc el nntp-marks))))
2208 (error (or (gnus-yes-or-no-p
2209 (format "Error reading nntp marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
2210 (error "Cannot read nntp marks file %s (%s)" file err))))
2211 ;; User didn't have a .marks file. Probably first time
2212 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
2213 (let ((info (gnus-get-info
2214 (gnus-group-prefixed-name
2215 group
2216 (gnus-server-to-method (format "nntp:%s" server)))))
2217 (decoded-name (mm-decode-coding-string
2218 group
2219 (gnus-group-name-charset
2220 (gnus-server-to-method server) group))))
2221 (nnheader-message 7 "Bootstrapping marks for %s..." decoded-name)
2222 (setq nntp-marks (gnus-info-marks info))
2223 (push (cons 'read (gnus-info-read info)) nntp-marks)
2224 (dolist (el gnus-article-unpropagated-mark-lists)
2225 (setq nntp-marks (gnus-remassoc el nntp-marks)))
2226 (nntp-save-marks group server)
2227 (nnheader-message 7 "Bootstrapping marks for %s...done"
2228 decoded-name)))))
2230 (provide 'nntp)
2232 ;;; nntp.el ends here