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