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