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