1 ;;; nntp.el --- nntp access for Gnus
3 ;; Copyright (C) 1987, 1988, 1989, 1990, 1992, 1993, 1994, 1995, 1996,
4 ;; 1997, 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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
13 ;; by the Free Software Foundation; either version 2, or (at your
14 ;; option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
35 (eval-when-compile (require 'cl))
37 (defvoo nntp-address nil
38 "Address of the physical nntp server.")
40 (defvoo nntp-port-number "nntp"
41 "Port number on the physical nntp server.")
43 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
44 "*Hook used for sending commands to the server at startup.
45 The default value is `nntp-send-mode-reader', which makes an innd
46 server spawn an nnrpd server.")
48 (defvoo nntp-authinfo-function 'nntp-send-authinfo
49 "Function used to send AUTHINFO to the server.
50 It is called with no parameters.")
52 (defvoo nntp-server-action-alist
53 '(("nntpd 1\\.5\\.11t"
54 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
55 ("NNRP server Netscape"
56 (setq nntp-server-list-active-group nil)))
57 "Alist of regexps to match on server types and actions to be taken.
58 For instance, if you want Gnus to beep every time you connect
59 to innd, you could say something like:
61 \(setq nntp-server-action-alist
64 You probably don't want to do that, though.")
66 (defvoo nntp-open-connection-function 'nntp-open-network-stream
67 "*Function used for connecting to a remote system.
68 It will be called with the buffer to output in as argument.
70 Currently, five such functions are provided (please refer to their
71 respective doc string for more information), three of them establishing
72 direct connections to the nntp server, and two of them using an indirect
76 - `nntp-open-network-stream' (the default),
77 - `nntp-open-ssl-stream',
78 - `nntp-open-telnet-stream'.
81 - `nntp-open-via-rlogin-and-telnet',
82 - `nntp-open-via-telnet-and-telnet'.")
84 (defvoo nntp-pre-command nil
85 "*Pre-command to use with the various nntp-open-via-* methods.
86 This is where you would put \"runsocks\" or stuff like that.")
88 (defvoo nntp-telnet-command "telnet"
89 "*Telnet command used to connect to the nntp server.
90 This command is used by the various nntp-open-via-* methods.")
92 (defvoo nntp-telnet-switches '("-8")
93 "*Switches given to the telnet command `nntp-telnet-command'.")
95 (defvoo nntp-end-of-line "\r\n"
96 "*String to use on the end of lines when talking to the NNTP server.
97 This is \"\\r\\n\" by default, but should be \"\\n\" when
98 using and indirect connection method (nntp-open-via-*).")
100 (defvoo nntp-via-rlogin-command "rsh"
101 "*Rlogin command used to connect to an intermediate host.
102 This command is used by the `nntp-open-via-rlogin-and-telnet' method.
103 The default is \"rsh\", but \"ssh\" is a popular alternative.")
105 (defvoo nntp-via-rlogin-command-switches nil
106 "*Switches given to the rlogin command `nntp-via-rlogin-command'.
107 If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
108 \(\"-C\") in order to compress all data connections, otherwise set this
109 to \(\"-t\") or (\"-C\" \"-t\") if the telnet command requires a pseudo-tty
110 allocation on an intermediate host.")
112 (defvoo nntp-via-telnet-command "telnet"
113 "*Telnet command used to connect to an intermediate host.
114 This command is used by the `nntp-open-via-telnet-and-telnet' method.")
116 (defvoo nntp-via-telnet-switches '("-8")
117 "*Switches given to the telnet command `nntp-via-telnet-command'.")
119 (defvoo nntp-via-user-name nil
120 "*User name to log in on an intermediate host with.
121 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
123 (defvoo nntp-via-user-password nil
124 "*Password to use to log in on an intermediate host with.
125 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
127 (defvoo nntp-via-address nil
128 "*Address of an intermediate host to connect to.
129 This variable is used by the `nntp-open-via-rlogin-and-telnet' and
130 `nntp-open-via-telnet-and-telnet' methods.")
132 (defvoo nntp-via-envuser nil
133 "*Whether both telnet client and server support the ENVIRON option.
134 If non-nil, there will be no prompt for a login name.")
136 (defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
137 "*Regular expression to match the shell prompt on an intermediate host.
138 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
140 (defvoo nntp-large-newsgroup 50
141 "*The number of the articles which indicates a large newsgroup.
142 If the number of the articles is greater than the value, verbose
143 messages will be shown to indicate the current status.")
145 (defvoo nntp-maximum-request 400
146 "*The maximum number of the requests sent to the NNTP server at one time.
147 If Emacs hangs up while retrieving headers, set the variable to a
150 (defvoo nntp-nov-is-evil nil
151 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
153 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
154 "*List of strings that are used as commands to fetch NOV lines from a server.
155 The strings are tried in turn until a positive response is gotten. If
156 none of the commands are successful, nntp will just grab headers one
159 (defvoo nntp-nov-gap 5
160 "*Maximum allowed gap between two articles.
161 If the gap between two consecutive articles is bigger than this
162 variable, split the XOVER request into two requests.")
164 (defvoo nntp-prepare-server-hook nil
165 "*Hook run before a server is opened.
166 If can be used to set up a server remotely, for instance. Say you
167 have an account at the machine \"other.machine\". This machine has
168 access to an NNTP server that you can't access locally. You could
169 then use this hook to rsh to the remote machine and start a proxy NNTP
170 server there that you can connect to. See also
171 `nntp-open-connection-function'")
173 (defvoo nntp-warn-about-losing-connection t
174 "*If non-nil, beep when a server closes connection.")
176 (defvoo nntp-coding-system-for-read 'binary
177 "*Coding system to read from NNTP.")
179 (defvoo nntp-coding-system-for-write 'binary
180 "*Coding system to write to NNTP.")
182 (defcustom nntp-authinfo-file "~/.authinfo"
183 ".netrc-like file that holds nntp authinfo passwords."
186 (repeat :tag "Entries"
189 :value ("" ("login" . "") ("password" . ""))
193 (const :format "" "login")
194 (string :format "Login: %v"))
196 (const :format "" "password")
197 (string :format "Password: %v")))))))
201 (defvoo nntp-connection-timeout nil
202 "*Number of seconds to wait before an nntp connection times out.
203 If this variable is nil, which is the default, no timers are set.
204 NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
206 (defvoo nntp-prepare-post-hook nil
207 "*Hook run just before posting an article. It is supposed to be used
208 to insert Cancel-Lock headers.")
210 ;;; Internal variables.
212 (defvar nntp-record-commands nil
213 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
215 (defvar nntp-have-messaged nil)
217 (defvar nntp-process-wait-for nil)
218 (defvar nntp-process-to-buffer nil)
219 (defvar nntp-process-callback nil)
220 (defvar nntp-process-decode nil)
221 (defvar nntp-process-start-point nil)
222 (defvar nntp-inside-change-function nil)
223 (defvoo nntp-last-command-time nil)
224 (defvoo nntp-last-command nil)
225 (defvoo nntp-authinfo-password nil)
226 (defvoo nntp-authinfo-user nil)
228 (defvar nntp-connection-list nil)
230 (defvoo nntp-server-type nil)
231 (defvoo nntp-connection-alist nil)
232 (defvoo nntp-status-string "")
233 (defconst nntp-version "nntp 5.0")
234 (defvoo nntp-inhibit-erase nil)
235 (defvoo nntp-inhibit-output nil)
237 (defvoo nntp-server-xover 'try)
238 (defvoo nntp-server-list-active-group 'try)
240 (defvar nntp-async-needs-kluge
241 (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
242 "*When non-nil, nntp will poll asynchronous connections
243 once a second. By default, this is turned on only for Emacs
244 20.3, which has a bug that breaks nntp's normal method of
245 noticing asynchronous data.")
247 (defvar nntp-async-timer nil)
248 (defvar nntp-async-process-list nil)
251 (autoload 'mail-source-read-passwd "mail-source")
252 (autoload 'open-ssl-stream "ssl"))
256 ;;; Internal functions.
258 (defsubst nntp-send-string (process string)
259 "Send STRING to PROCESS."
260 ;; We need to store the time to provide timeouts, and
261 ;; to store the command so the we can replay the command
262 ;; if the server gives us an AUTHINFO challenge.
263 (setq nntp-last-command-time (current-time)
264 nntp-last-command string)
265 (when nntp-record-commands
266 (nntp-record-command string))
267 (process-send-string process (concat string nntp-end-of-line)))
269 (defun nntp-record-command (string)
270 "Record the command STRING."
272 (set-buffer (get-buffer-create "*nntp-log*"))
273 (goto-char (point-max))
274 (let ((time (current-time)))
275 (insert (format-time-string "%Y%m%dT%H%M%S" time)
276 "." (format "%03d" (/ (nth 2 time) 1000))
277 " " nntp-address " " string "\n"))))
279 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
280 "Wait for WAIT-FOR to arrive from PROCESS."
282 (set-buffer (process-buffer process))
283 (goto-char (point-min))
284 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
286 (memq (process-status process) '(open run)))
287 (when (looking-at "480")
288 (nntp-handle-authinfo process))
289 (when (looking-at "^.*\n")
290 (delete-region (point) (progn (forward-line 1) (point))))
291 (nntp-accept-process-output process)
292 (goto-char (point-min)))
297 (nntp-snarf-error-message)
299 ((not (memq (process-status process) '(open run)))
300 (nntp-report "Server closed connection"))
302 (goto-char (point-max))
303 (let ((limit (point-min))
305 (while (not (re-search-backward wait-for limit t))
306 (nntp-accept-process-output process)
307 ;; We assume that whatever we wait for is less than 1000
309 (setq limit (max (- (point-max) 1000) (point-min)))
310 (goto-char (point-max)))
311 (setq response (match-string 0))
312 (with-current-buffer nntp-server-buffer
313 (setq nntp-process-response response)))
314 (nntp-decode-text (not decode))
318 (goto-char (point-max))
319 (insert-buffer-substring (process-buffer process))
320 ;; Nix out "nntp reading...." message.
321 (when nntp-have-messaged
322 (setq nntp-have-messaged nil)
323 (nnheader-message 5 ""))))
328 (defun nntp-kill-buffer (buffer)
329 (when (buffer-name buffer)
331 (nnheader-init-server-buffer)))
333 (defsubst nntp-find-connection (buffer)
334 "Find the connection delivering to BUFFER."
335 (let ((alist nntp-connection-alist)
336 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
338 (while (and alist (setq entry (pop alist)))
339 (when (eq buffer (cadr entry))
340 (setq process (car entry)
343 (if (memq (process-status process) '(open run))
345 (nntp-kill-buffer (process-buffer process))
346 (setq nntp-connection-alist (delq entry nntp-connection-alist))
349 (defsubst nntp-find-connection-entry (buffer)
350 "Return the entry for the connection to BUFFER."
351 (assq (nntp-find-connection buffer) nntp-connection-alist))
353 (defun nntp-find-connection-buffer (buffer)
354 "Return the process connection buffer tied to BUFFER."
355 (let ((process (nntp-find-connection buffer)))
357 (process-buffer process))))
359 (defsubst nntp-retrieve-data (command address port buffer
360 &optional wait-for callback decode)
361 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
362 (let ((process (or (nntp-find-connection buffer)
363 (nntp-open-connection buffer))))
365 (nnheader-report 'nntp "Couldn't open connection to %s" address)
366 (unless (or nntp-inhibit-erase nnheader-callback-function)
368 (set-buffer (process-buffer process))
373 (nntp-send-string process command))
375 ((eq callback 'ignore)
377 ((and callback wait-for)
378 (nntp-async-wait process wait-for buffer decode callback)
381 (nntp-wait-for process wait-for buffer decode))
384 (nnheader-report 'nntp "Couldn't open connection to %s: %s"
387 (message "Quit retrieving data from nntp")
391 (defsubst nntp-send-command (wait-for &rest strings)
392 "Send STRINGS to server and wait until WAIT-FOR returns."
393 (when (and (not nnheader-callback-function)
394 (not nntp-inhibit-output))
396 (set-buffer nntp-server-buffer)
398 (let* ((command (mapconcat 'identity strings " "))
399 (process (nntp-find-connection nntp-server-buffer))
400 (buffer (and process (process-buffer process)))
401 (pos (and buffer (with-current-buffer buffer (point)))))
404 (nntp-retrieve-data command
405 nntp-address nntp-port-number
407 wait-for nnheader-callback-function)
408 ;; If nothing to wait for, still remove possibly echo'ed commands.
409 ;; We don't have echos if nntp-open-connection-function
410 ;; is `nntp-open-network-stream', so we skip this in that case.
412 (equal nntp-open-connection-function
413 'nntp-open-network-stream))
414 (nntp-accept-response)
418 (if (looking-at (regexp-quote command))
419 (delete-region pos (progn (forward-line 1)
420 (gnus-point-at-bol))))
422 (nnheader-report 'nntp "Couldn't open connection to %s."
425 (defun nntp-send-command-nodelete (wait-for &rest strings)
426 "Send STRINGS to server and wait until WAIT-FOR returns."
427 (let* ((command (mapconcat 'identity strings " "))
428 (process (nntp-find-connection nntp-server-buffer))
429 (buffer (and process (process-buffer process)))
430 (pos (and buffer (with-current-buffer buffer (point)))))
433 (nntp-retrieve-data command
434 nntp-address nntp-port-number
436 wait-for nnheader-callback-function)
437 ;; If nothing to wait for, still remove possibly echo'ed commands
439 (nntp-accept-response)
443 (if (looking-at (regexp-quote command))
444 (delete-region pos (progn (forward-line 1)
445 (gnus-point-at-bol))))
447 (nnheader-report 'nntp "Couldn't open connection to %s."
450 (defun nntp-send-command-and-decode (wait-for &rest strings)
451 "Send STRINGS to server and wait until WAIT-FOR returns."
452 (when (and (not nnheader-callback-function)
453 (not nntp-inhibit-output))
455 (set-buffer nntp-server-buffer)
457 (let* ((command (mapconcat 'identity strings " "))
458 (process (nntp-find-connection nntp-server-buffer))
459 (buffer (and process (process-buffer process)))
460 (pos (and buffer (with-current-buffer buffer (point)))))
463 (nntp-retrieve-data command
464 nntp-address nntp-port-number
466 wait-for nnheader-callback-function t)
467 ;; If nothing to wait for, still remove possibly echo'ed commands
469 (nntp-accept-response)
473 (if (looking-at (regexp-quote command))
474 (delete-region pos (progn (forward-line 1) (gnus-point-at-bol))))
476 (nnheader-report 'nntp "Couldn't open connection to %s."
480 (defun nntp-send-buffer (wait-for)
481 "Send the current buffer to server and wait until WAIT-FOR returns."
482 (when (and (not nnheader-callback-function)
483 (not nntp-inhibit-output))
485 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
488 (mm-with-unibyte-current-buffer
489 ;; Some encoded unicode text contains character 0x80-0x9f e.g. Euro.
490 (process-send-region (nntp-find-connection nntp-server-buffer)
491 (point-min) (point-max)))
493 nil nntp-address nntp-port-number nntp-server-buffer
494 wait-for nnheader-callback-function))
498 ;;; Interface functions.
500 (nnoo-define-basics nntp)
502 (defsubst nntp-next-result-arrived-p ()
504 ;; A result that starts with a 2xx code is terminated by
505 ;; a line with only a "." on it.
506 ((eq (char-after) ?2)
507 (if (re-search-forward "\n\\.\r?\n" nil t)
510 ;; A result that starts with a 3xx or 4xx code is terminated
513 (if (search-forward "\n" nil t)
520 (defvar nntp-with-open-group-first-pass nil)
522 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
523 "Protect against servers that don't like clients that keep idle connections opens.
524 The problem being that these servers may either close a connection or
525 simply ignore any further requests on a connection. Closed
526 connections are not detected until accept-process-output has updated
527 the process-status. Dropped connections are not detected until the
528 connection timeouts (which may be several minutes) or
529 nntp-connection-timeout has expired. When these occur
530 nntp-with-open-group, opens a new connection then re-issues the NNTP
531 command whose response triggered the error."
532 (when (and (listp connectionless)
533 (not (eq connectionless nil)))
534 (setq forms (cons connectionless forms)
536 `(let ((nntp-with-open-group-first-pass t)
537 nntp-with-open-group-internal)
539 (catch 'nntp-with-open-group-error
540 ;; Open the connection to the server
541 ;; NOTE: Existing connections are NOT tested.
542 (nntp-possibly-change-group ,group ,server ,connectionless)
546 nntp-connection-timeout
547 (nnheader-run-at-time
548 nntp-connection-timeout nil
550 (let ((process (nntp-find-connection
552 (buffer (and process (process-buffer process))))
553 ;; when I an able to identify the connection
554 ;; to the server AND I've received NO reponse
555 ;; for nntp-connection-timeout seconds.
556 (when (and buffer (eq 0 (buffer-size buffer)))
557 ;; Close the connection. Take no other
558 ;; action as the accept input code will
559 ;; handle the closed connection.
560 (nntp-kill-buffer buffer))))))))
562 (setq nntp-with-open-group-internal (progn ,@forms))
564 (nnheader-cancel-timer timer)))
566 (setq nntp-with-open-group-first-pass nil))
567 nntp-with-open-group-internal))
569 (defsubst nntp-report (&rest args)
570 "Report an error from the nntp backend.
571 The first string in ARGS can be a format string.
572 For some commands, the failed command may be retried once before actually displaying the error report."
574 (if nntp-with-open-group-first-pass
575 (throw 'nntp-with-open-group-error t))
577 (nnheader-report 'nntp args)
580 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
581 "Retrieve the headers of ARTICLES."
582 (nntp-with-open-group
585 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
587 (if (and (not gnus-nov-is-evil)
588 (not nntp-nov-is-evil)
589 (nntp-retrieve-headers-with-xover articles fetch-old))
590 ;; We successfully retrieved the headers via XOVER.
592 ;; XOVER didn't work, so we do it the hard, slow and inefficient
594 (let ((number (length articles))
597 (last-point (point-min))
598 (buf (nntp-find-connection-buffer nntp-server-buffer))
599 (nntp-inhibit-erase t)
601 ;; Send HEAD commands.
602 (while (setq article (pop articles))
605 "HEAD" (if (numberp article)
606 (int-to-string article)
607 ;; `articles' is either a list of article numbers
608 ;; or a list of article IDs.
611 ;; Every 400 requests we have to read the stream in
612 ;; order to avoid deadlocks.
613 (when (or (null articles) ;All requests have been sent.
614 (zerop (% count nntp-maximum-request)))
615 (nntp-accept-response)
618 (goto-char last-point)
620 (while (nntp-next-result-arrived-p)
621 (setq last-point (point))
624 ;; If number of headers is greater than 100, give
625 ;; informative messages.
626 (and (numberp nntp-large-newsgroup)
627 (> number nntp-large-newsgroup)
628 (zerop (% received 20))
629 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
630 (/ (* received 100) number)))
631 (nntp-accept-response))))
632 (and (numberp nntp-large-newsgroup)
633 (> number nntp-large-newsgroup)
634 (nnheader-message 6 "NNTP: Receiving headers...done"))
636 ;; Now all of replies are received. Fold continuation lines.
637 (nnheader-fold-continuation-lines)
638 ;; Remove all "\r"'s.
640 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
643 (deffoo nntp-retrieve-groups (groups &optional server)
644 "Retrieve group info on GROUPS."
645 (nntp-possibly-change-group nil server)
646 (when (nntp-find-connection-buffer nntp-server-buffer)
649 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
650 (set-buffer nntp-server-buffer)
652 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
653 ;; The first time this is run, this variable is `try'. So we
655 (when (eq nntp-server-list-active-group 'try)
656 (nntp-try-list-active (car groups)))
660 (last-point (point-min))
661 (nntp-inhibit-erase t)
662 (buf (nntp-find-connection-buffer nntp-server-buffer))
663 (command (if nntp-server-list-active-group
664 "LIST ACTIVE" "GROUP")))
666 ;; Timeout may have killed the buffer.
667 (unless (gnus-buffer-live-p buf)
668 (nnheader-report 'nntp "Connection to %s is closed." server)
670 ;; Send the command to the server.
671 (nntp-send-command nil command (pop groups))
673 ;; Every 400 requests we have to read the stream in
674 ;; order to avoid deadlocks.
675 (when (or (null groups) ;All requests have been sent.
676 (zerop (% count nntp-maximum-request)))
677 (nntp-accept-response)
678 (while (and (gnus-buffer-live-p buf)
680 ;; Search `blue moon' in this file for the
681 ;; reason why set-buffer here.
683 (goto-char last-point)
685 (while (re-search-forward "^[0-9]" nil t)
687 (setq last-point (point))
689 (nntp-accept-response))))
691 ;; Wait for the reply from the final command.
692 (unless (gnus-buffer-live-p buf)
693 (nnheader-report 'nntp "Connection to %s is closed." server)
696 (goto-char (point-max))
697 (re-search-backward "^[0-9]" nil t)
698 (when (looking-at "^[23]")
699 (while (and (gnus-buffer-live-p buf)
702 (goto-char (point-max))
703 (if (not nntp-server-list-active-group)
704 (not (re-search-backward "\r?\n" (- (point) 3) t))
705 (not (re-search-backward "^\\.\r?\n"
707 (nntp-accept-response)))
709 ;; Now all replies are received. We remove CRs.
710 (unless (gnus-buffer-live-p buf)
711 (nnheader-report 'nntp "Connection to %s is closed." server)
714 (goto-char (point-min))
715 (while (search-forward "\r" nil t)
716 (replace-match "" t t))
718 (if (not nntp-server-list-active-group)
720 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
722 ;; We have read active entries, so we just delete the
724 (goto-char (point-min))
725 (while (re-search-forward "^[.2-5]" nil t)
726 (delete-region (match-beginning 0)
727 (progn (forward-line 1) (point))))
728 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
731 (deffoo nntp-retrieve-articles (articles &optional group server)
732 (nntp-with-open-group
735 (let ((number (length articles))
738 (last-point (point-min))
739 (buf (nntp-find-connection-buffer nntp-server-buffer))
740 (nntp-inhibit-erase t)
741 (map (apply 'vector articles))
746 ;; Send ARTICLE command.
747 (while (setq article (pop articles))
750 "ARTICLE" (if (numberp article)
751 (int-to-string article)
752 ;; `articles' is either a list of article numbers
753 ;; or a list of article IDs.
756 ;; Every 400 requests we have to read the stream in
757 ;; order to avoid deadlocks.
758 (when (or (null articles) ;All requests have been sent.
759 (zerop (% count nntp-maximum-request)))
760 (nntp-accept-response)
763 (goto-char last-point)
765 (while (nntp-next-result-arrived-p)
766 (aset map received (cons (aref map received) (point)))
767 (setq last-point (point))
770 ;; If number of headers is greater than 100, give
771 ;; informative messages.
772 (and (numberp nntp-large-newsgroup)
773 (> number nntp-large-newsgroup)
774 (zerop (% received 20))
775 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
776 (/ (* received 100) number)))
777 (nntp-accept-response))))
778 (and (numberp nntp-large-newsgroup)
779 (> number nntp-large-newsgroup)
780 (nnheader-message 6 "NNTP: Receiving articles...done"))
782 ;; Now we have all the responses. We go through the results,
783 ;; wash it and copy it over to the server buffer.
784 (set-buffer nntp-server-buffer)
786 (setq last-point (point-min))
790 (setq point (goto-char (point-max)))
792 (insert-buffer-substring buf last-point (cdr entry))
794 (setq last-point (cdr entry))
797 (cons (car entry) point))
800 (defun nntp-try-list-active (group)
801 (nntp-list-active-group group)
803 (set-buffer nntp-server-buffer)
804 (goto-char (point-min))
806 (looking-at "5[0-9]+"))
807 (setq nntp-server-list-active-group nil))
809 (setq nntp-server-list-active-group t)))))
811 (deffoo nntp-list-active-group (group &optional server)
812 "Return the active info on GROUP (which can be a regexp)."
813 (nntp-possibly-change-group nil server)
814 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group))
816 (deffoo nntp-request-group-articles (group &optional server)
817 "Return the list of existing articles in GROUP."
818 (nntp-possibly-change-group nil server)
819 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group))
821 (deffoo nntp-request-article (article &optional group server buffer command)
822 (nntp-with-open-group
824 (when (nntp-send-command-and-decode
825 "\r?\n\\.\r?\n" "ARTICLE"
826 (if (numberp article) (int-to-string article) article))
828 (not (equal buffer nntp-server-buffer)))
830 (set-buffer nntp-server-buffer)
831 (copy-to-buffer buffer (point-min) (point-max))
832 (nntp-find-group-and-number group))
833 (nntp-find-group-and-number group)))))
835 (deffoo nntp-request-head (article &optional group server)
836 (nntp-possibly-change-group group server)
837 (when (nntp-send-command
838 "\r?\n\\.\r?\n" "HEAD"
839 (if (numberp article) (int-to-string article) article))
841 (nntp-find-group-and-number group)
842 (nntp-decode-text))))
844 (deffoo nntp-request-body (article &optional group server)
845 (nntp-possibly-change-group group server)
846 (nntp-send-command-and-decode
847 "\r?\n\\.\r?\n" "BODY"
848 (if (numberp article) (int-to-string article) article)))
850 (deffoo nntp-request-group (group &optional server dont-check)
851 (nntp-with-open-group
853 (when (nntp-send-command "^[245].*\n" "GROUP" group)
854 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
855 (setcar (cddr entry) group)))))
857 (deffoo nntp-close-group (group &optional server)
860 (deffoo nntp-server-opened (&optional server)
861 "Say whether a connection to SERVER has been opened."
862 (and (nnoo-current-server-p 'nntp server)
864 (gnus-buffer-live-p nntp-server-buffer)
865 (nntp-find-connection nntp-server-buffer)))
867 (deffoo nntp-open-server (server &optional defs connectionless)
868 (nnheader-init-server-buffer)
869 (if (nntp-server-opened server)
871 (when (or (stringp (car defs))
872 (numberp (car defs)))
873 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
874 (unless (assq 'nntp-address defs)
875 (setq defs (append defs (list (list 'nntp-address server)))))
876 (nnoo-change-server 'nntp server defs)
877 (unless connectionless
878 (or (nntp-find-connection nntp-server-buffer)
879 (nntp-open-connection nntp-server-buffer)))))
881 (deffoo nntp-close-server (&optional server)
882 (nntp-possibly-change-group nil server t)
883 (let ((process (nntp-find-connection nntp-server-buffer)))
885 (when (memq (process-status process) '(open run))
887 (nntp-send-string process "QUIT")
888 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
889 ;; Ok, this is evil, but when using telnet and stuff
890 ;; as the connection method, it's important that the
891 ;; QUIT command actually is sent out before we kill
894 (nntp-kill-buffer (process-buffer process))
895 (setq process (car (pop nntp-connection-alist))))
896 (nnoo-close-server 'nntp)))
898 (deffoo nntp-request-close ()
900 (while (setq process (pop nntp-connection-list))
901 (when (memq (process-status process) '(open run))
903 (nntp-send-string process "QUIT")
904 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
905 ;; Ok, this is evil, but when using telnet and stuff
906 ;; as the connection method, it's important that the
907 ;; QUIT command actually is sent out before we kill
910 (nntp-kill-buffer (process-buffer process)))))
912 (deffoo nntp-request-list (&optional server)
913 (nntp-possibly-change-group nil server)
914 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST"))
916 (deffoo nntp-request-list-newsgroups (&optional server)
917 (nntp-possibly-change-group nil server)
918 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS"))
920 (deffoo nntp-request-newgroups (date &optional server)
921 (nntp-possibly-change-group nil server)
923 (set-buffer nntp-server-buffer)
924 (let* ((time (date-to-time date))
925 (ls (- (cadr time) (nth 8 (decode-time time)))))
927 (setcar time (1- (car time)))
928 (setcar (cdr time) (+ ls 65536)))
930 (setcar time (1+ (car time)))
931 (setcar (cdr time) (- ls 65536)))
933 (setcar (cdr time) ls)))
936 "^\\.\r?\n" "NEWGROUPS"
937 (format-time-string "%y%m%d %H%M%S" time)
939 (nntp-decode-text)))))
941 (deffoo nntp-request-post (&optional server)
942 (nntp-possibly-change-group nil server)
943 (when (nntp-send-command "^[23].*\r?\n" "POST")
944 (let ((response (with-current-buffer nntp-server-buffer
945 nntp-process-response))
948 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
950 (setq server-id (match-string 1 response))
951 (narrow-to-region (goto-char (point-min))
952 (if (search-forward "\n\n" nil t)
955 (unless (mail-fetch-field "Message-ID")
956 (goto-char (point-min))
957 (insert "Message-ID: " server-id "\n"))
959 (run-hooks 'nntp-prepare-post-hook)
960 (nntp-send-buffer "^[23].*\n"))))
962 (deffoo nntp-request-type (group article)
965 (deffoo nntp-asynchronous-p ()
970 (defun nntp-send-mode-reader ()
971 "Send the MODE READER command to the nntp server.
972 This function is supposed to be called from `nntp-server-opened-hook'.
973 It will make innd servers spawn an nnrpd process to allow actual article
975 (nntp-send-command "^.*\n" "MODE READER"))
977 (defun nntp-send-authinfo (&optional send-if-force)
978 "Send the AUTHINFO to the nntp server.
979 It will look in the \"~/.authinfo\" file for matching entries. If
980 nothing suitable is found there, it will prompt for a user name
983 If SEND-IF-FORCE, only send authinfo to the server if the
984 .authinfo file has the FORCE token."
985 (let* ((list (gnus-parse-netrc nntp-authinfo-file))
986 (alist (gnus-netrc-machine list nntp-address "nntp"))
987 (force (gnus-netrc-get alist "force"))
988 (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
989 (passwd (gnus-netrc-get alist "password")))
990 (when (or (not send-if-force)
993 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
994 nntp-authinfo-user user))
995 (unless (member user '(nil ""))
996 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
997 (when t ;???Should check if AUTHINFO succeeded
999 "^2.*\r?\n" "AUTHINFO PASS"
1001 nntp-authinfo-password
1002 (setq nntp-authinfo-password
1003 (mail-source-read-passwd
1004 (format "NNTP (%s@%s) password: "
1005 user nntp-address))))))))))
1007 (defun nntp-send-nosy-authinfo ()
1008 "Send the AUTHINFO to the nntp server."
1009 (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
1010 (unless (member user '(nil ""))
1011 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1012 (when t ;???Should check if AUTHINFO succeeded
1013 (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
1014 (mail-source-read-passwd "NNTP (%s@%s) password: "
1015 user nntp-address))))))
1017 (defun nntp-send-authinfo-from-file ()
1018 "Send the AUTHINFO to the nntp server.
1020 The authinfo login name is taken from the user's login name and the
1021 password contained in '~/.nntp-authinfo'."
1022 (when (file-exists-p "~/.nntp-authinfo")
1024 (insert-file-contents "~/.nntp-authinfo")
1025 (goto-char (point-min))
1026 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
1028 "^2.*\r?\n" "AUTHINFO PASS"
1029 (buffer-substring (point) (progn (end-of-line) (point)))))))
1031 ;;; Internal functions.
1033 (defun nntp-handle-authinfo (process)
1034 "Take care of an authinfo response from the server."
1035 (let ((last nntp-last-command))
1036 (funcall nntp-authinfo-function)
1037 ;; We have to re-send the function that was interrupted by
1038 ;; the authinfo request.
1040 (set-buffer nntp-server-buffer)
1042 (nntp-send-string process last)))
1044 (defun nntp-make-process-buffer (buffer)
1045 "Create a new, fresh buffer usable for nntp process connections."
1048 (generate-new-buffer
1049 (format " *server %s %s %s*"
1050 nntp-address nntp-port-number
1051 (gnus-buffer-exists-p buffer))))
1052 (mm-enable-multibyte)
1053 (set (make-local-variable 'after-change-functions) nil)
1054 (set (make-local-variable 'nntp-process-wait-for) nil)
1055 (set (make-local-variable 'nntp-process-callback) nil)
1056 (set (make-local-variable 'nntp-process-to-buffer) nil)
1057 (set (make-local-variable 'nntp-process-start-point) nil)
1058 (set (make-local-variable 'nntp-process-decode) nil)
1061 (defun nntp-open-connection (buffer)
1062 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1063 (run-hooks 'nntp-prepare-server-hook)
1064 (let* ((pbuffer (nntp-make-process-buffer buffer))
1066 (and nntp-connection-timeout
1067 (nnheader-run-at-time
1068 nntp-connection-timeout nil
1070 (nntp-kill-buffer ,pbuffer)))))
1073 (let ((coding-system-for-read nntp-coding-system-for-read)
1074 (coding-system-for-write nntp-coding-system-for-write))
1075 (funcall nntp-open-connection-function pbuffer))
1078 (message "Quit opening connection")
1079 (nntp-kill-buffer pbuffer)
1083 (nnheader-cancel-timer timer))
1085 (nntp-kill-buffer pbuffer))
1086 (when (and (buffer-name pbuffer)
1088 (process-kill-without-query process)
1089 (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1090 (memq (process-status process) '(open run)))
1092 (caar (push (list process buffer nil) nntp-connection-alist))
1093 (push process nntp-connection-list)
1095 (set-buffer pbuffer)
1096 (nntp-read-server-type)
1098 (set-buffer nntp-server-buffer)
1099 (let ((nnheader-callback-function nil))
1100 (run-hooks 'nntp-server-opened-hook)
1101 (nntp-send-authinfo t))))
1102 (nntp-kill-buffer (process-buffer process))
1105 (defun nntp-open-network-stream (buffer)
1106 (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
1108 (defun nntp-open-ssl-stream (buffer)
1109 (let ((proc (open-ssl-stream "nntpd" buffer nntp-address nntp-port-number)))
1112 (nntp-wait-for-string "^\r*20[01]")
1114 (delete-region (point-min) (point))
1117 (defun nntp-read-server-type ()
1118 "Find out what the name of the server we have connected to is."
1119 ;; Wait for the status string to arrive.
1120 (setq nntp-server-type (buffer-string))
1121 (let ((alist nntp-server-action-alist)
1122 (case-fold-search t)
1124 ;; Run server-specific commands.
1126 (setq entry (pop alist))
1127 (when (string-match (car entry) nntp-server-type)
1128 (if (and (listp (cadr entry))
1129 (not (eq 'lambda (caadr entry))))
1131 (funcall (cadr entry)))))))
1133 (defun nntp-async-wait (process wait-for buffer decode callback)
1135 (set-buffer (process-buffer process))
1136 (unless nntp-inside-change-function
1138 (setq nntp-process-wait-for wait-for
1139 nntp-process-to-buffer buffer
1140 nntp-process-decode decode
1141 nntp-process-callback callback
1142 nntp-process-start-point (point-max))
1143 (setq after-change-functions '(nntp-after-change-function))
1144 (if nntp-async-needs-kluge
1145 (nntp-async-kluge process))))
1147 (defun nntp-async-kluge (process)
1148 ;; emacs 20.3 bug: process output with encoding 'binary
1149 ;; doesn't trigger after-change-functions.
1150 (unless nntp-async-timer
1151 (setq nntp-async-timer
1152 (nnheader-run-at-time 1 1 'nntp-async-timer-handler)))
1153 (add-to-list 'nntp-async-process-list process))
1155 (defun nntp-async-timer-handler ()
1158 (if (memq (process-status proc) '(open run))
1159 (nntp-async-trigger proc)
1160 (nntp-async-stop proc)))
1161 nntp-async-process-list))
1163 (defun nntp-async-stop (proc)
1164 (setq nntp-async-process-list (delq proc nntp-async-process-list))
1165 (when (and nntp-async-timer (not nntp-async-process-list))
1166 (nnheader-cancel-timer nntp-async-timer)
1167 (setq nntp-async-timer nil)))
1169 (defun nntp-after-change-function (beg end len)
1171 ;; we only care about insertions at eob
1172 (when (and (eq 0 len) (eq (point-max) end))
1174 (let ((proc (get-buffer-process (current-buffer))))
1176 (nntp-async-trigger proc)))))
1177 ;; any throw from after-change-functions will leave it
1178 ;; set to nil. so we reset it here, if necessary.
1180 (setq after-change-functions '(nntp-after-change-function)))))
1182 (defun nntp-async-trigger (process)
1184 (set-buffer (process-buffer process))
1185 (when nntp-process-callback
1186 ;; do we have an error message?
1187 (goto-char nntp-process-start-point)
1188 (if (memq (following-char) '(?4 ?5))
1189 ;; wants credentials?
1190 (if (looking-at "480")
1191 (nntp-handle-authinfo process)
1192 ;; report error message.
1193 (nntp-snarf-error-message)
1194 (nntp-do-callback nil))
1196 ;; got what we expect?
1197 (goto-char (point-max))
1198 (when (re-search-backward
1199 nntp-process-wait-for nntp-process-start-point t)
1200 (let ((response (match-string 0)))
1201 (with-current-buffer nntp-server-buffer
1202 (setq nntp-process-response response)))
1203 (nntp-async-stop process)
1205 (when (gnus-buffer-exists-p nntp-process-to-buffer)
1206 (let ((buf (current-buffer))
1207 (start nntp-process-start-point)
1208 (decode nntp-process-decode))
1210 (set-buffer nntp-process-to-buffer)
1211 (goto-char (point-max))
1213 (narrow-to-region (point) (point))
1214 (insert-buffer-substring buf start)
1216 (nntp-decode-text))))))
1218 (goto-char (point-max))
1220 (buffer-name (get-buffer nntp-process-to-buffer))))))))
1222 (defun nntp-do-callback (arg)
1223 (let ((callback nntp-process-callback)
1224 (nntp-inside-change-function t))
1225 (setq nntp-process-callback nil)
1226 (funcall callback arg)))
1228 (defun nntp-snarf-error-message ()
1229 "Save the error message in the current buffer."
1230 (let ((message (buffer-string)))
1231 (while (string-match "[\r\n]+" message)
1232 (setq message (replace-match " " t t message)))
1233 (nnheader-report 'nntp message)
1236 (defun nntp-accept-process-output (process &optional timeout)
1237 "Wait for output from PROCESS and message some dots."
1239 (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1240 nntp-server-buffer))
1241 (let ((len (/ (point-max) 1024))
1244 (setq nntp-have-messaged t)
1245 (nnheader-message 7 "nntp read: %dk" len)))
1247 (accept-process-output process timeout)
1248 (accept-process-output process 0 100))
1249 ;; accept-process-output may update status of process to indicate
1250 ;; that the server has closed the connection. This MUST be
1251 ;; handled here as the buffer restored by the save-excursion may
1252 ;; be the process's former output buffer (i.e. now killed)
1253 (or (memq (process-status process) '(open run))
1254 (nntp-report "Server closed connection"))))
1256 (defun nntp-accept-response ()
1257 "Wait for output from the process that outputs to BUFFER."
1258 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1260 (defun nntp-possibly-change-group (group server &optional connectionless)
1261 (let ((nnheader-callback-function nil))
1263 (or (nntp-server-opened server)
1264 (nntp-open-server server nil connectionless)))
1266 (unless connectionless
1267 (or (nntp-find-connection nntp-server-buffer)
1268 (nntp-open-connection nntp-server-buffer))))
1271 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1272 (when (not (equal group (caddr entry)))
1274 (set-buffer (process-buffer (car entry)))
1276 (nntp-send-command "^[245].*\n" "GROUP" group)
1277 (setcar (cddr entry) group)
1280 (set-buffer nntp-server-buffer)
1281 (erase-buffer)))))))
1283 (defun nntp-decode-text (&optional cr-only)
1284 "Decode the text in the current buffer."
1285 (goto-char (point-min))
1286 (while (search-forward "\r" nil t)
1289 ;; Remove trailing ".\n" end-of-transfer marker.
1290 (goto-char (point-max))
1292 (when (looking-at ".\n")
1294 ;; Delete status line.
1295 (goto-char (point-min))
1296 (while (looking-at "[1-5][0-9][0-9] .*\n")
1297 ;; For some unknown reason, there is more than one status line.
1298 (delete-region (point) (progn (forward-line 1) (point))))
1299 ;; Remove "." -> ".." encoding.
1300 (while (search-forward "\n.." nil t)
1303 (defun nntp-encode-text ()
1304 "Encode the text in the current buffer."
1306 ;; Replace "." at beginning of line with "..".
1307 (goto-char (point-min))
1308 (while (re-search-forward "^\\." nil t)
1310 (goto-char (point-max))
1311 ;; Insert newline at the end of the buffer.
1314 ;; Insert `.' at end of buffer (end of text mark).
1315 (goto-char (point-max))
1317 (goto-char (point-min))
1321 (insert nntp-end-of-line))))
1323 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1324 (set-buffer nntp-server-buffer)
1328 ;; This server does not talk NOV.
1329 ((not nntp-server-xover)
1332 ;; We don't care about gaps.
1333 ((or (not nntp-nov-gap)
1335 (nntp-send-xover-command
1337 (if (numberp fetch-old)
1338 (max 1 (- (car articles) fetch-old))
1341 (car (last articles)) 'wait)
1343 (goto-char (point-min))
1344 (when (looking-at "[1-5][0-9][0-9] .*\n")
1345 (delete-region (point) (progn (forward-line 1) (point))))
1346 (while (search-forward "\r" nil t)
1347 (replace-match "" t t))
1348 (goto-char (point-max))
1350 (when (looking-at "\\.")
1351 (delete-region (point) (progn (forward-line 1) (point)))))
1353 ;; We do it the hard way. For each gap, an XOVER command is sent
1354 ;; to the server. We do not wait for a reply from the server, we
1355 ;; just send them off as fast as we can. That means that we have
1356 ;; to count the number of responses we get back to find out when we
1357 ;; have gotten all we asked for.
1358 ((numberp nntp-nov-gap)
1363 (buf nntp-server-buffer)
1364 (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1367 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1368 ;; that means that the server does not understand XOVER, but we
1369 ;; won't know that until we try.
1370 (while (and nntp-server-xover articles)
1371 (setq first (car articles))
1372 ;; Search forward until we find a gap, or until we run out of
1374 (while (and (cdr articles)
1375 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1376 (setq articles (cdr articles)))
1378 (setq in-process-buffer-p (stringp nntp-server-xover))
1379 (nntp-send-xover-command first (setq last (car articles)))
1380 (setq articles (cdr articles))
1382 (when (and nntp-server-xover in-process-buffer-p)
1383 ;; Don't count tried request.
1384 (setq count (1+ count))
1386 ;; Every 400 requests we have to read the stream in
1387 ;; order to avoid deadlocks.
1388 (when (or (null articles) ;All requests have been sent.
1389 (= 1 (% count nntp-maximum-request)))
1391 (nntp-accept-response)
1392 ;; On some Emacs versions the preceding function has a
1393 ;; tendency to change the buffer. Perhaps. It's quite
1394 ;; difficult to reproduce, because it only seems to happen
1395 ;; once in a blue moon.
1396 (set-buffer process-buffer)
1398 (goto-char (or last-point (point-min)))
1400 (while (re-search-forward "^[0-9][0-9][0-9] .*\n" nil t)
1402 (setq last-point (point))
1403 (or (< received count) ;; I haven't started reading the final response
1405 (goto-char (point-max))
1407 (not (looking-at "^\\.\r?\n"))) ;; I haven't read the end of the final response
1409 (nntp-accept-response)
1410 (set-buffer process-buffer))))
1412 ;; Some nntp servers seem to have an extension to the XOVER extension. On these
1413 ;; servers, requesting an article range preceeding the active range does not return an
1414 ;; error as specified in the RFC. What we instead get is the NOV entry for the first
1415 ;; available article. Obviously, a client can use that entry to avoid making unnecessary
1416 ;; requests. The only problem is for a client that assumes that the response will always be
1417 ;; within the requested ranage. For such a client, we can get N copies of the same entry
1418 ;; (one for each XOVER command sent to the server).
1421 (goto-char (point-min))
1422 (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1423 (let ((low-limit (string-to-int (buffer-substring (match-beginning 1) (match-end 1)))))
1424 (while (and articles (<= (car articles) low-limit))
1425 (setq articles (cdr articles))))))
1428 (when nntp-server-xover
1429 (when in-process-buffer-p
1431 (goto-char (point-max))
1432 (insert-buffer-substring process-buffer)
1433 (set-buffer process-buffer)
1437 ;; We remove any "." lines and status lines.
1438 (goto-char (point-min))
1439 (while (search-forward "\r" nil t)
1441 (goto-char (point-min))
1442 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1447 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1448 "Send the XOVER command to the server."
1449 (let ((range (format "%d-%d" beg end))
1450 (nntp-inhibit-erase t))
1451 (if (stringp nntp-server-xover)
1452 ;; If `nntp-server-xover' is a string, then we just send this
1455 (nntp-send-command-nodelete
1456 "\r?\n\\.\r?\n" nntp-server-xover range)
1457 ;; We do not wait for the reply.
1458 (nntp-send-command-nodelete nil nntp-server-xover range))
1459 (let ((commands nntp-xover-commands))
1460 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1461 ;; We try them all until we get at positive response.
1462 (while (and commands (eq nntp-server-xover 'try))
1463 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1465 (set-buffer nntp-server-buffer)
1466 (goto-char (point-min))
1467 (and (looking-at "[23]") ; No error message.
1468 ;; We also have to look at the lines. Some buggy
1469 ;; servers give back simple lines with just the
1470 ;; article number. How... helpful.
1473 (looking-at "[0-9]+\t...")) ; More text after number.
1474 (setq nntp-server-xover (car commands))))
1475 (setq commands (cdr commands)))
1476 ;; If none of the commands worked, we disable XOVER.
1477 (when (eq nntp-server-xover 'try)
1479 (set-buffer nntp-server-buffer)
1481 (setq nntp-server-xover nil)))
1482 nntp-server-xover))))
1484 (defun nntp-find-group-and-number (&optional group)
1487 (set-buffer nntp-server-buffer)
1488 (narrow-to-region (goto-char (point-min))
1489 (or (search-forward "\n\n" nil t) (point-max)))
1490 (goto-char (point-min))
1491 ;; We first find the number by looking at the status line.
1492 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1494 (buffer-substring (match-beginning 1)
1497 (and number (zerop number) (setq number nil))
1499 ;; Then we find the group name.
1502 ;; If there is only one group in the Newsgroups
1503 ;; header, then it seems quite likely that this
1504 ;; article comes from that group, I'd say.
1505 ((and (setq newsgroups
1506 (mail-fetch-field "newsgroups"))
1507 (not (string-match "," newsgroups)))
1509 ;; If there is more than one group in the
1510 ;; Newsgroups header, then the Xref header should
1511 ;; be filled out. We hazard a guess that the group
1512 ;; that has this article number in the Xref header
1513 ;; is the one we are looking for. This might very
1514 ;; well be wrong if this article happens to have
1515 ;; the same number in several groups, but that's
1517 ((and (setq xref (mail-fetch-field "xref"))
1520 (format "\\([^ :]+\\):%d" number) xref))
1521 (match-string 1 xref))
1524 ((and (setq xref (mail-fetch-field "xref"))
1527 (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1528 "\\([^ :]+\\):\\([0-9]+\\)")
1530 (setq group (match-string 1 xref)
1531 number (string-to-int (match-string 2 xref))))
1532 ((and (setq newsgroups
1533 (mail-fetch-field "newsgroups"))
1534 (not (string-match "," newsgroups)))
1535 (setq group newsgroups))
1537 (t (setq group ""))))
1538 (when (string-match "\r" group)
1539 (setq group (substring group 0 (match-beginning 0))))
1540 (cons group number)))))
1542 (defun nntp-wait-for-string (regexp)
1543 "Wait until string arrives in the buffer."
1544 (let ((buf (current-buffer))
1546 (goto-char (point-min))
1547 (while (and (setq proc (get-buffer-process buf))
1548 (memq (process-status proc) '(open run))
1549 (not (re-search-forward regexp nil t)))
1550 (accept-process-output proc)
1552 (goto-char (point-min)))))
1555 ;; ==========================================================================
1556 ;; Obsolete nntp-open-* connection methods -- drv
1557 ;; ==========================================================================
1559 (defvoo nntp-open-telnet-envuser nil
1560 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1562 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
1563 "*Regular expression to match the shell prompt on the remote machine.")
1565 (defvoo nntp-rlogin-program "rsh"
1566 "*Program used to log in on remote machines.
1567 The default is \"rsh\", but \"ssh\" is a popular alternative.")
1569 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1570 "*Parameters to `nntp-open-rlogin'.
1571 That function may be used as `nntp-open-connection-function'. In that
1572 case, this list will be used as the parameter list given to rsh.")
1574 (defvoo nntp-rlogin-user-name nil
1575 "*User name on remote system when using the rlogin connect method.")
1577 (defvoo nntp-telnet-parameters
1578 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1579 "*Parameters to `nntp-open-telnet'.
1580 That function may be used as `nntp-open-connection-function'. In that
1581 case, this list will be executed as a command after logging in
1584 (defvoo nntp-telnet-user-name nil
1585 "User name to log in via telnet with.")
1587 (defvoo nntp-telnet-passwd nil
1588 "Password to use to log in via telnet with.")
1590 (defun nntp-open-telnet (buffer)
1596 "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1597 (case-fold-search t))
1598 (when (memq (process-status proc) '(open run))
1599 (nntp-wait-for-string "^r?telnet")
1600 (process-send-string proc "set escape \^X\n")
1602 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1603 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1604 nntp-address "\n")))
1606 (process-send-string proc (concat "open " nntp-address "\n"))))
1608 ((not nntp-open-telnet-envuser)
1609 (nntp-wait-for-string "^\r*.?login:")
1610 (process-send-string
1612 (or nntp-telnet-user-name
1613 (setq nntp-telnet-user-name (read-string "login: ")))
1615 (nntp-wait-for-string "^\r*.?password:")
1616 (process-send-string
1618 (or nntp-telnet-passwd
1619 (setq nntp-telnet-passwd
1620 (mail-source-read-passwd "Password: ")))
1622 (nntp-wait-for-string nntp-telnet-shell-prompt)
1623 (process-send-string
1624 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1625 (nntp-wait-for-string "^\r*20[01]")
1627 (delete-region (point-min) (point))
1628 (process-send-string proc "\^]")
1629 (nntp-wait-for-string "^r?telnet")
1630 (process-send-string proc "mode character\n")
1631 (accept-process-output proc 1)
1633 (goto-char (point-min))
1635 (delete-region (point) (point-max)))
1638 (defun nntp-open-rlogin (buffer)
1639 "Open a connection to SERVER using rsh."
1640 (let ((proc (if nntp-rlogin-user-name
1641 (apply 'start-process
1642 "nntpd" buffer nntp-rlogin-program
1643 nntp-address "-l" nntp-rlogin-user-name
1644 nntp-rlogin-parameters)
1645 (apply 'start-process
1646 "nntpd" buffer nntp-rlogin-program nntp-address
1647 nntp-rlogin-parameters))))
1650 (nntp-wait-for-string "^\r*20[01]")
1652 (delete-region (point-min) (point))
1656 ;; ==========================================================================
1657 ;; Replacements for the nntp-open-* functions -- drv
1658 ;; ==========================================================================
1660 (defun nntp-open-telnet-stream (buffer)
1661 "Open a nntp connection by telnet'ing the news server.
1663 Please refer to the following variables to customize the connection:
1664 - `nntp-pre-command',
1665 - `nntp-telnet-command',
1666 - `nntp-telnet-switches',
1668 - `nntp-port-number',
1669 - `nntp-end-of-line'."
1670 (let ((command `(,nntp-telnet-command
1671 ,@nntp-telnet-switches
1672 ,nntp-address ,nntp-port-number))
1674 (and nntp-pre-command
1675 (push nntp-pre-command command))
1676 (setq proc (apply 'start-process "nntpd" buffer command))
1679 (nntp-wait-for-string "^\r*20[01]")
1681 (delete-region (point-min) (point))
1684 (defun nntp-open-via-rlogin-and-telnet (buffer)
1685 "Open a connection to an nntp server through an intermediate host.
1686 First rlogin to the remote host, and then telnet the real news server
1689 Please refer to the following variables to customize the connection:
1690 - `nntp-pre-command',
1691 - `nntp-via-rlogin-command',
1692 - `nntp-via-rlogin-command-switches',
1693 - `nntp-via-user-name',
1694 - `nntp-via-address',
1695 - `nntp-telnet-command',
1696 - `nntp-telnet-switches',
1698 - `nntp-port-number',
1699 - `nntp-end-of-line'."
1700 (let ((command `(,nntp-via-address
1701 ,nntp-telnet-command
1702 ,@nntp-telnet-switches))
1704 (when nntp-via-user-name
1705 (setq command `("-l" ,nntp-via-user-name ,@command)))
1706 (when nntp-via-rlogin-command-switches
1707 (setq command (append nntp-via-rlogin-command-switches command)))
1708 (push nntp-via-rlogin-command command)
1709 (and nntp-pre-command
1710 (push nntp-pre-command command))
1711 (setq proc (apply 'start-process "nntpd" buffer command))
1714 (nntp-wait-for-string "^r?telnet")
1715 (process-send-string proc (concat "open " nntp-address
1716 " " nntp-port-number "\n"))
1717 (nntp-wait-for-string "^\r*20[01]")
1719 (delete-region (point-min) (point))
1722 (defun nntp-open-via-telnet-and-telnet (buffer)
1723 "Open a connection to an nntp server through an intermediate host.
1724 First telnet the remote host, and then telnet the real news server
1727 Please refer to the following variables to customize the connection:
1728 - `nntp-pre-command',
1729 - `nntp-via-telnet-command',
1730 - `nntp-via-telnet-switches',
1731 - `nntp-via-address',
1732 - `nntp-via-envuser',
1733 - `nntp-via-user-name',
1734 - `nntp-via-user-password',
1735 - `nntp-via-shell-prompt',
1736 - `nntp-telnet-command',
1737 - `nntp-telnet-switches',
1739 - `nntp-port-number',
1740 - `nntp-end-of-line'."
1744 (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
1745 (case-fold-search t)
1747 (and nntp-pre-command (push nntp-pre-command command))
1748 (setq proc (apply 'start-process "nntpd" buffer command))
1749 (when (memq (process-status proc) '(open run))
1750 (nntp-wait-for-string "^r?telnet")
1751 (process-send-string proc "set escape \^X\n")
1753 ((and nntp-via-envuser nntp-via-user-name)
1754 (process-send-string proc (concat "open " "-l" nntp-via-user-name
1755 nntp-via-address "\n")))
1757 (process-send-string proc (concat "open " nntp-via-address
1759 (when (not nntp-via-envuser)
1760 (nntp-wait-for-string "^\r*.?login:")
1761 (process-send-string proc
1763 (or nntp-via-user-name
1764 (setq nntp-via-user-name
1765 (read-string "login: ")))
1767 (nntp-wait-for-string "^\r*.?password:")
1768 (process-send-string proc
1770 (or nntp-via-user-password
1771 (setq nntp-via-user-password
1772 (mail-source-read-passwd
1775 (nntp-wait-for-string nntp-via-shell-prompt)
1776 (let ((real-telnet-command `("exec"
1777 ,nntp-telnet-command
1778 ,@nntp-telnet-switches
1780 ,nntp-port-number)))
1781 (process-send-string proc
1782 (concat (mapconcat 'identity
1783 real-telnet-command " ")
1785 (nntp-wait-for-string "^\r*20[01]")
1787 (delete-region (point-min) (point))
1788 (process-send-string proc "\^]")
1789 (nntp-wait-for-string "^r?telnet")
1790 (process-send-string proc "mode character\n")
1791 (accept-process-output proc 1)
1793 (goto-char (point-min))
1795 (delete-region (point) (point-max)))
1800 ;;; nntp.el ends here