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, 2003 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))
38 "NNTP access for Gnus."
41 (defvoo nntp-address nil
42 "Address of the physical nntp server.")
44 (defvoo nntp-port-number "nntp"
45 "Port number on the physical nntp server.")
47 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
48 "*Hook used for sending commands to the server at startup.
49 The default value is `nntp-send-mode-reader', which makes an innd
50 server spawn an nnrpd server.")
52 (defvoo nntp-authinfo-function 'nntp-send-authinfo
53 "Function used to send AUTHINFO to the server.
54 It is called with no parameters.")
56 (defvoo nntp-server-action-alist
57 '(("nntpd 1\\.5\\.11t"
58 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
59 ("NNRP server Netscape"
60 (setq nntp-server-list-active-group nil)))
61 "Alist of regexps to match on server types and actions to be taken.
62 For instance, if you want Gnus to beep every time you connect
63 to innd, you could say something like:
65 \(setq nntp-server-action-alist
68 You probably don't want to do that, though.")
70 (defvoo nntp-open-connection-function 'nntp-open-network-stream
71 "*Function used for connecting to a remote system.
72 It will be called with the buffer to output in as argument.
74 Currently, five such functions are provided (please refer to their
75 respective doc string for more information), three of them establishing
76 direct connections to the nntp server, and two of them using an indirect
80 - `nntp-open-network-stream' (the default),
81 - `nntp-open-ssl-stream',
82 - `nntp-open-tls-stream',
83 - `nntp-open-telnet-stream'.
86 - `nntp-open-via-rlogin-and-telnet',
87 - `nntp-open-via-telnet-and-telnet'.")
89 (defvoo nntp-pre-command nil
90 "*Pre-command to use with the various nntp-open-via-* methods.
91 This is where you would put \"runsocks\" or stuff like that.")
93 (defvoo nntp-telnet-command "telnet"
94 "*Telnet command used to connect to the nntp server.
95 This command is used by the various nntp-open-via-* methods.")
97 (defvoo nntp-telnet-switches '("-8")
98 "*Switches given to the telnet command `nntp-telnet-command'.")
100 (defvoo nntp-end-of-line "\r\n"
101 "*String to use on the end of lines when talking to the NNTP server.
102 This is \"\\r\\n\" by default, but should be \"\\n\" when
103 using and indirect connection method (nntp-open-via-*).")
105 (defvoo nntp-via-rlogin-command "rsh"
106 "*Rlogin command used to connect to an intermediate host.
107 This command is used by the `nntp-open-via-rlogin-and-telnet' method.
108 The default is \"rsh\", but \"ssh\" is a popular alternative.")
110 (defvoo nntp-via-rlogin-command-switches nil
111 "*Switches given to the rlogin command `nntp-via-rlogin-command'.
112 If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
113 \(\"-C\") in order to compress all data connections, otherwise set this
114 to \(\"-t\" \"-e\" \"none\") or (\"-C\" \"-t\" \"-e\" \"none\") if the telnet
115 command requires a pseudo-tty allocation on an intermediate host.")
117 (defvoo nntp-via-telnet-command "telnet"
118 "*Telnet command used to connect to an intermediate host.
119 This command is used by the `nntp-open-via-telnet-and-telnet' method.")
121 (defvoo nntp-via-telnet-switches '("-8")
122 "*Switches given to the telnet command `nntp-via-telnet-command'.")
124 (defvoo nntp-via-user-name nil
125 "*User name to log in on an intermediate host with.
126 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
128 (defvoo nntp-via-user-password nil
129 "*Password to use to log in on an intermediate host with.
130 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
132 (defvoo nntp-via-address nil
133 "*Address of an intermediate host to connect to.
134 This variable is used by the `nntp-open-via-rlogin-and-telnet' and
135 `nntp-open-via-telnet-and-telnet' methods.")
137 (defvoo nntp-via-envuser nil
138 "*Whether both telnet client and server support the ENVIRON option.
139 If non-nil, there will be no prompt for a login name.")
141 (defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
142 "*Regular expression to match the shell prompt on an intermediate host.
143 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
145 (defvoo nntp-large-newsgroup 50
146 "*The number of articles which indicates a large newsgroup.
147 If the number of articles is greater than the value, verbose
148 messages will be shown to indicate the current status.")
150 (defvoo nntp-maximum-request 400
151 "*The maximum number of the requests sent to the NNTP server at one time.
152 If Emacs hangs up while retrieving headers, set the variable to a
155 (defvoo nntp-nov-is-evil nil
156 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
158 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
159 "*List of strings that are used as commands to fetch NOV lines from a server.
160 The strings are tried in turn until a positive response is gotten. If
161 none of the commands are successful, nntp will just grab headers one
164 (defvoo nntp-nov-gap 5
165 "*Maximum allowed gap between two articles.
166 If the gap between two consecutive articles is bigger than this
167 variable, split the XOVER request into two requests.")
169 (defvoo nntp-prepare-server-hook nil
170 "*Hook run before a server is opened.
171 If can be used to set up a server remotely, for instance. Say you
172 have an account at the machine \"other.machine\". This machine has
173 access to an NNTP server that you can't access locally. You could
174 then use this hook to rsh to the remote machine and start a proxy NNTP
175 server there that you can connect to. See also
176 `nntp-open-connection-function'")
178 (defvoo nntp-warn-about-losing-connection t
179 "*If non-nil, beep when a server closes connection.")
181 (defvoo nntp-coding-system-for-read 'binary
182 "*Coding system to read from NNTP.")
184 (defvoo nntp-coding-system-for-write 'binary
185 "*Coding system to write to NNTP.")
187 (defcustom nntp-authinfo-file "~/.authinfo"
188 ".netrc-like file that holds nntp authinfo passwords."
192 (repeat :tag "Entries"
195 :value ("" ("login" . "") ("password" . ""))
199 (const :format "" "login")
200 (string :format "Login: %v"))
202 (const :format "" "password")
203 (string :format "Password: %v")))))))
207 (defvoo nntp-connection-timeout nil
208 "*Number of seconds to wait before an nntp connection times out.
209 If this variable is nil, which is the default, no timers are set.
210 NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
212 (defvoo nntp-prepare-post-hook nil
213 "*Hook run just before posting an article. It is supposed to be used
214 to insert Cancel-Lock headers.")
216 ;;; Internal variables.
218 (defvar nntp-record-commands nil
219 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
221 (defvar nntp-have-messaged nil)
223 (defvar nntp-process-wait-for nil)
224 (defvar nntp-process-to-buffer nil)
225 (defvar nntp-process-callback nil)
226 (defvar nntp-process-decode nil)
227 (defvar nntp-process-start-point nil)
228 (defvar nntp-inside-change-function nil)
229 (defvoo nntp-last-command-time nil)
230 (defvoo nntp-last-command nil)
231 (defvoo nntp-authinfo-password nil)
232 (defvoo nntp-authinfo-user nil)
234 (defvar nntp-connection-list nil)
236 (defvoo nntp-server-type nil)
237 (defvoo nntp-connection-alist nil)
238 (defvoo nntp-status-string "")
239 (defconst nntp-version "nntp 5.0")
240 (defvoo nntp-inhibit-erase nil)
241 (defvoo nntp-inhibit-output nil)
243 (defvoo nntp-server-xover 'try)
244 (defvoo nntp-server-list-active-group 'try)
246 (defvar nntp-async-needs-kluge
247 (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
248 "*When non-nil, nntp will poll asynchronous connections
249 once a second. By default, this is turned on only for Emacs
250 20.3, which has a bug that breaks nntp's normal method of
251 noticing asynchronous data.")
253 (defvar nntp-async-timer nil)
254 (defvar nntp-async-process-list nil)
256 (defvar nntp-ssl-program
257 "openssl s_client -quiet -ssl3 -connect %s:%p"
258 "A string containing commands for SSL connections.
259 Within a string, %s is replaced with the server address and %p with
260 port number on server. The program should accept IMAP commands on
261 stdin and return responses to stdout.")
265 ;;; Internal functions.
267 (defsubst nntp-send-string (process string)
268 "Send STRING to PROCESS."
269 ;; We need to store the time to provide timeouts, and
270 ;; to store the command so the we can replay the command
271 ;; if the server gives us an AUTHINFO challenge.
272 (setq nntp-last-command-time (current-time)
273 nntp-last-command string)
274 (when nntp-record-commands
275 (nntp-record-command string))
276 (process-send-string process (concat string nntp-end-of-line))
277 (or (memq (process-status process) '(open run))
278 (nntp-report "Server closed connection")))
280 (defun nntp-record-command (string)
281 "Record the command STRING."
283 (set-buffer (get-buffer-create "*nntp-log*"))
284 (goto-char (point-max))
285 (let ((time (current-time)))
286 (insert (format-time-string "%Y%m%dT%H%M%S" time)
287 "." (format "%03d" (/ (nth 2 time) 1000))
288 " " nntp-address " " string "\n"))))
290 (defun nntp-report (&rest args)
291 "Report an error from the nntp backend. The first string in ARGS
292 can be a format string. For some commands, the failed command may be
293 retried once before actually displaying the error report."
295 (when nntp-record-commands
296 (nntp-record-command "*** CALLED nntp-report ***"))
298 (nnheader-report 'nntp args)
302 (defun nntp-report-1 (&rest args)
303 "Throws out to nntp-with-open-group-error so that the connection may
304 be restored and the command retried."
306 (when nntp-record-commands
307 (nntp-record-command "*** CONNECTION LOST ***"))
309 (throw 'nntp-with-open-group-error t))
311 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
312 "Wait for WAIT-FOR to arrive from PROCESS."
314 (set-buffer (process-buffer process))
315 (goto-char (point-min))
316 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
318 (memq (process-status process) '(open run)))
319 (when (looking-at "480")
320 (nntp-handle-authinfo process))
321 (when (looking-at "^.*\n")
322 (delete-region (point) (progn (forward-line 1) (point))))
323 (nntp-accept-process-output process)
324 (goto-char (point-min)))
329 (nntp-snarf-error-message)
331 ((not (memq (process-status process) '(open run)))
332 (nntp-report "Server closed connection"))
334 (goto-char (point-max))
335 (let ((limit (point-min))
337 (while (not (re-search-backward wait-for limit t))
338 (nntp-accept-process-output process)
339 ;; We assume that whatever we wait for is less than 1000
341 (setq limit (max (- (point-max) 1000) (point-min)))
342 (goto-char (point-max)))
343 (setq response (match-string 0))
344 (with-current-buffer nntp-server-buffer
345 (setq nntp-process-response response)))
346 (nntp-decode-text (not decode))
350 (goto-char (point-max))
351 (insert-buffer-substring (process-buffer process))
352 ;; Nix out "nntp reading...." message.
353 (when nntp-have-messaged
354 (setq nntp-have-messaged nil)
355 (nnheader-message 5 ""))))
360 (defun nntp-kill-buffer (buffer)
361 (when (buffer-name buffer)
363 (nnheader-init-server-buffer)))
365 (defsubst nntp-find-connection (buffer)
366 "Find the connection delivering to BUFFER."
367 (let ((alist nntp-connection-alist)
368 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
370 (while (and alist (setq entry (pop alist)))
371 (when (eq buffer (cadr entry))
372 (setq process (car entry)
375 (if (memq (process-status process) '(open run))
377 (nntp-kill-buffer (process-buffer process))
378 (setq nntp-connection-alist (delq entry nntp-connection-alist))
381 (defsubst nntp-find-connection-entry (buffer)
382 "Return the entry for the connection to BUFFER."
383 (assq (nntp-find-connection buffer) nntp-connection-alist))
385 (defun nntp-find-connection-buffer (buffer)
386 "Return the process connection buffer tied to BUFFER."
387 (let ((process (nntp-find-connection buffer)))
389 (process-buffer process))))
391 (defsubst nntp-retrieve-data (command address port buffer
392 &optional wait-for callback decode)
393 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
394 (let ((process (or (nntp-find-connection buffer)
395 (nntp-open-connection buffer))))
398 (unless (or nntp-inhibit-erase nnheader-callback-function)
400 (set-buffer (process-buffer process))
405 (nntp-send-string process command))
407 ((eq callback 'ignore)
409 ((and callback wait-for)
410 (nntp-async-wait process wait-for buffer decode callback)
413 (nntp-wait-for process wait-for buffer decode))
416 (nnheader-report 'nntp "Couldn't open connection to %s: %s"
419 (message "Quit retrieving data from nntp")
422 (nnheader-report 'nntp "Couldn't open connection to %s" address))))
424 (defsubst nntp-send-command (wait-for &rest strings)
425 "Send STRINGS to server and wait until WAIT-FOR returns."
426 (when (and (not nnheader-callback-function)
427 (not nntp-inhibit-output))
429 (set-buffer nntp-server-buffer)
431 (let* ((command (mapconcat 'identity strings " "))
432 (process (nntp-find-connection nntp-server-buffer))
433 (buffer (and process (process-buffer process)))
434 (pos (and buffer (with-current-buffer buffer (point)))))
437 (nntp-retrieve-data command
438 nntp-address nntp-port-number
440 wait-for nnheader-callback-function)
441 ;; If nothing to wait for, still remove possibly echo'ed commands.
442 ;; We don't have echos if nntp-open-connection-function
443 ;; is `nntp-open-network-stream', so we skip this in that case.
445 (equal nntp-open-connection-function
446 'nntp-open-network-stream))
447 (nntp-accept-response)
451 (if (looking-at (regexp-quote command))
452 (delete-region pos (progn (forward-line 1)
453 (gnus-point-at-bol))))
455 (nnheader-report 'nntp "Couldn't open connection to %s."
458 (defun nntp-send-command-nodelete (wait-for &rest strings)
459 "Send STRINGS to server and wait until WAIT-FOR returns."
460 (let* ((command (mapconcat 'identity strings " "))
461 (process (nntp-find-connection nntp-server-buffer))
462 (buffer (and process (process-buffer process)))
463 (pos (and buffer (with-current-buffer buffer (point)))))
466 (nntp-retrieve-data command
467 nntp-address nntp-port-number
469 wait-for nnheader-callback-function)
470 ;; If nothing to wait for, still remove possibly echo'ed commands
472 (nntp-accept-response)
476 (if (looking-at (regexp-quote command))
477 (delete-region pos (progn (forward-line 1)
478 (gnus-point-at-bol)))))))
479 (nnheader-report 'nntp "Couldn't open connection to %s."
482 (defun nntp-send-command-and-decode (wait-for &rest strings)
483 "Send STRINGS to server and wait until WAIT-FOR returns."
484 (when (and (not nnheader-callback-function)
485 (not nntp-inhibit-output))
487 (set-buffer nntp-server-buffer)
489 (let* ((command (mapconcat 'identity strings " "))
490 (process (nntp-find-connection nntp-server-buffer))
491 (buffer (and process (process-buffer process)))
492 (pos (and buffer (with-current-buffer buffer (point)))))
495 (nntp-retrieve-data command
496 nntp-address nntp-port-number
498 wait-for nnheader-callback-function t)
499 ;; If nothing to wait for, still remove possibly echo'ed commands
501 (nntp-accept-response)
505 (if (looking-at (regexp-quote command))
506 (delete-region pos (progn (forward-line 1) (gnus-point-at-bol))))
508 (nnheader-report 'nntp "Couldn't open connection to %s."
512 (defun nntp-send-buffer (wait-for)
513 "Send the current buffer to server and wait until WAIT-FOR returns."
514 (when (and (not nnheader-callback-function)
515 (not nntp-inhibit-output))
517 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
520 (mm-with-unibyte-current-buffer
521 ;; Some encoded unicode text contains character 0x80-0x9f e.g. Euro.
522 (process-send-region (nntp-find-connection nntp-server-buffer)
523 (point-min) (point-max)))
525 nil nntp-address nntp-port-number nntp-server-buffer
526 wait-for nnheader-callback-function))
530 ;;; Interface functions.
532 (nnoo-define-basics nntp)
534 (defsubst nntp-next-result-arrived-p ()
536 ;; A result that starts with a 2xx code is terminated by
537 ;; a line with only a "." on it.
538 ((eq (char-after) ?2)
539 (if (re-search-forward "\n\\.\r?\n" nil t)
542 ;; A result that starts with a 3xx or 4xx code is terminated
545 (if (search-forward "\n" nil t)
553 (defvar nntp-with-open-group-internal nil)
554 (defvar nntp-report-n nil))
556 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
557 "Protect against servers that don't like clients that keep idle connections opens.
558 The problem being that these servers may either close a connection or
559 simply ignore any further requests on a connection. Closed
560 connections are not detected until accept-process-output has updated
561 the process-status. Dropped connections are not detected until the
562 connection timeouts (which may be several minutes) or
563 nntp-connection-timeout has expired. When these occur
564 nntp-with-open-group, opens a new connection then re-issues the NNTP
565 command whose response triggered the error."
566 (when (and (listp connectionless)
567 (not (eq connectionless nil)))
568 (setq forms (cons connectionless forms)
570 `(letf ((nntp-report-n (symbol-function 'nntp-report))
571 ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1))
572 (nntp-with-open-group-internal nil))
573 (while (catch 'nntp-with-open-group-error
574 ;; Open the connection to the server
575 ;; NOTE: Existing connections are NOT tested.
576 (nntp-possibly-change-group ,group ,server ,connectionless)
579 (and nntp-connection-timeout
580 (nnheader-run-at-time
581 nntp-connection-timeout nil
583 (let ((process (nntp-find-connection
586 (process-buffer process))))
587 ;; When I an able to identify the
588 ;; connection to the server AND I've
589 ;; received NO reponse for
590 ;; nntp-connection-timeout seconds.
591 (when (and buffer (eq 0 (buffer-size buffer)))
592 ;; Close the connection. Take no
593 ;; other action as the accept input
594 ;; code will handle the closed
596 (nntp-kill-buffer buffer))))))))
598 (setq nntp-with-open-group-internal
603 (signal 'quit nil))))
605 (nnheader-cancel-timer timer)))
607 (setf (symbol-function 'nntp-report) nntp-report-n))
608 nntp-with-open-group-internal))
610 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
611 "Retrieve the headers of ARTICLES."
612 (nntp-with-open-group
615 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
617 (if (and (not gnus-nov-is-evil)
618 (not nntp-nov-is-evil)
619 (nntp-retrieve-headers-with-xover articles fetch-old))
620 ;; We successfully retrieved the headers via XOVER.
622 ;; XOVER didn't work, so we do it the hard, slow and inefficient
624 (let ((number (length articles))
628 (last-point (point-min))
629 (buf (nntp-find-connection-buffer nntp-server-buffer))
630 (nntp-inhibit-erase t)
632 ;; Send HEAD commands.
633 (while (setq article (pop articles))
636 "HEAD" (if (numberp article)
637 (int-to-string article)
638 ;; `articles' is either a list of article numbers
639 ;; or a list of article IDs.
642 ;; Every 400 requests we have to read the stream in
643 ;; order to avoid deadlocks.
644 (when (or (null articles) ;All requests have been sent.
645 (zerop (% count nntp-maximum-request)))
646 (nntp-accept-response)
649 (goto-char last-point)
651 (while (nntp-next-result-arrived-p)
652 (setq last-point (point))
655 ;; If number of headers is greater than 100, give
656 ;; informative messages.
657 (and (numberp nntp-large-newsgroup)
658 (> number nntp-large-newsgroup)
659 (zerop (% received 20))
660 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
661 (/ (* received 100) number)))
662 (nntp-accept-response))))
663 (and (numberp nntp-large-newsgroup)
664 (> number nntp-large-newsgroup)
665 (nnheader-message 6 "NNTP: Receiving headers...done"))
667 ;; Now all of replies are received. Fold continuation lines.
668 (nnheader-fold-continuation-lines)
669 ;; Remove all "\r"'s.
671 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
674 (deffoo nntp-retrieve-groups (groups &optional server)
675 "Retrieve group info on GROUPS."
676 (nntp-with-open-group
678 (when (nntp-find-connection-buffer nntp-server-buffer)
681 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
682 (set-buffer nntp-server-buffer)
684 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
685 ;; The first time this is run, this variable is `try'. So we
687 (when (eq nntp-server-list-active-group 'try)
688 (nntp-try-list-active (car groups)))
693 (last-point (point-min))
694 (nntp-inhibit-erase t)
695 (buf (nntp-find-connection-buffer nntp-server-buffer))
696 (command (if nntp-server-list-active-group
697 "LIST ACTIVE" "GROUP")))
699 ;; Timeout may have killed the buffer.
700 (unless (gnus-buffer-live-p buf)
701 (nnheader-report 'nntp "Connection to %s is closed." server)
703 ;; Send the command to the server.
704 (nntp-send-command nil command (pop groups))
706 ;; Every 400 requests we have to read the stream in
707 ;; order to avoid deadlocks.
708 (when (or (null groups) ;All requests have been sent.
709 (zerop (% count nntp-maximum-request)))
710 (nntp-accept-response)
711 (while (and (gnus-buffer-live-p buf)
713 ;; Search `blue moon' in this file for the
714 ;; reason why set-buffer here.
716 (goto-char last-point)
718 (while (re-search-forward "^[0-9]" nil t)
720 (setq last-point (point))
722 (nntp-accept-response))))
724 ;; Wait for the reply from the final command.
725 (unless (gnus-buffer-live-p buf)
726 (nnheader-report 'nntp "Connection to %s is closed." server)
729 (goto-char (point-max))
730 (re-search-backward "^[0-9]" nil t)
731 (when (looking-at "^[23]")
732 (while (and (gnus-buffer-live-p buf)
735 (goto-char (point-max))
736 (if (not nntp-server-list-active-group)
737 (not (re-search-backward "\r?\n"
739 (not (re-search-backward "^\\.\r?\n"
741 (nntp-accept-response)))
743 ;; Now all replies are received. We remove CRs.
744 (unless (gnus-buffer-live-p buf)
745 (nnheader-report 'nntp "Connection to %s is closed." server)
748 (goto-char (point-min))
749 (while (search-forward "\r" nil t)
750 (replace-match "" t t))
752 (if (not nntp-server-list-active-group)
754 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
756 ;; We have read active entries, so we just delete the
758 (goto-char (point-min))
759 (while (re-search-forward "^[.2-5]" nil t)
760 (delete-region (match-beginning 0)
761 (progn (forward-line 1) (point))))
762 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
765 (deffoo nntp-retrieve-articles (articles &optional group server)
766 (nntp-with-open-group
769 (let ((number (length articles))
773 (last-point (point-min))
774 (buf (nntp-find-connection-buffer nntp-server-buffer))
775 (nntp-inhibit-erase t)
776 (map (apply 'vector articles))
781 ;; Send ARTICLE command.
782 (while (setq article (pop articles))
785 "ARTICLE" (if (numberp article)
786 (int-to-string article)
787 ;; `articles' is either a list of article numbers
788 ;; or a list of article IDs.
791 ;; Every 400 requests we have to read the stream in
792 ;; order to avoid deadlocks.
793 (when (or (null articles) ;All requests have been sent.
794 (zerop (% count nntp-maximum-request)))
795 (nntp-accept-response)
798 (goto-char last-point)
800 (while (nntp-next-result-arrived-p)
801 (aset map received (cons (aref map received) (point)))
802 (setq last-point (point))
805 ;; If number of headers is greater than 100, give
806 ;; informative messages.
807 (and (numberp nntp-large-newsgroup)
808 (> number nntp-large-newsgroup)
809 (zerop (% received 20))
810 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
811 (/ (* received 100) number)))
812 (nntp-accept-response))))
813 (and (numberp nntp-large-newsgroup)
814 (> number nntp-large-newsgroup)
815 (nnheader-message 6 "NNTP: Receiving articles...done"))
817 ;; Now we have all the responses. We go through the results,
818 ;; wash it and copy it over to the server buffer.
819 (set-buffer nntp-server-buffer)
821 (setq last-point (point-min))
825 (setq point (goto-char (point-max)))
827 (insert-buffer-substring buf last-point (cdr entry))
829 (setq last-point (cdr entry))
832 (cons (car entry) point))
835 (defun nntp-try-list-active (group)
836 (nntp-list-active-group group)
838 (set-buffer nntp-server-buffer)
839 (goto-char (point-min))
841 (looking-at "5[0-9]+"))
842 (setq nntp-server-list-active-group nil))
844 (setq nntp-server-list-active-group t)))))
846 (deffoo nntp-list-active-group (group &optional server)
847 "Return the active info on GROUP (which can be a regexp)."
848 (nntp-with-open-group
850 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
852 (deffoo nntp-request-group-articles (group &optional server)
853 "Return the list of existing articles in GROUP."
854 (nntp-with-open-group
856 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
858 (deffoo nntp-request-article (article &optional group server buffer command)
859 (nntp-with-open-group
861 (when (nntp-send-command-and-decode
862 "\r?\n\\.\r?\n" "ARTICLE"
863 (if (numberp article) (int-to-string article) article))
865 (not (equal buffer nntp-server-buffer)))
867 (set-buffer nntp-server-buffer)
868 (copy-to-buffer buffer (point-min) (point-max))
869 (nntp-find-group-and-number group))
870 (nntp-find-group-and-number group)))))
872 (deffoo nntp-request-head (article &optional group server)
873 (nntp-with-open-group
875 (when (nntp-send-command
876 "\r?\n\\.\r?\n" "HEAD"
877 (if (numberp article) (int-to-string article) article))
879 (nntp-find-group-and-number group)
880 (nntp-decode-text)))))
882 (deffoo nntp-request-body (article &optional group server)
883 (nntp-with-open-group
885 (nntp-send-command-and-decode
886 "\r?\n\\.\r?\n" "BODY"
887 (if (numberp article) (int-to-string article) article))))
889 (deffoo nntp-request-group (group &optional server dont-check)
890 (nntp-with-open-group
892 (when (nntp-send-command "^[245].*\n" "GROUP" group)
893 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
894 (setcar (cddr entry) group)))))
896 (deffoo nntp-close-group (group &optional server)
899 (deffoo nntp-server-opened (&optional server)
900 "Say whether a connection to SERVER has been opened."
901 (and (nnoo-current-server-p 'nntp server)
903 (gnus-buffer-live-p nntp-server-buffer)
904 (nntp-find-connection nntp-server-buffer)))
906 (deffoo nntp-open-server (server &optional defs connectionless)
907 (nnheader-init-server-buffer)
908 (if (nntp-server-opened server)
910 (when (or (stringp (car defs))
911 (numberp (car defs)))
912 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
913 (unless (assq 'nntp-address defs)
914 (setq defs (append defs (list (list 'nntp-address server)))))
915 (nnoo-change-server 'nntp server defs)
916 (unless connectionless
917 (or (nntp-find-connection nntp-server-buffer)
918 (nntp-open-connection nntp-server-buffer)))))
920 (deffoo nntp-close-server (&optional server)
921 (nntp-possibly-change-group nil server t)
922 (let ((process (nntp-find-connection nntp-server-buffer)))
924 (when (memq (process-status process) '(open run))
926 (nntp-send-string process "QUIT")
927 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
928 ;; Ok, this is evil, but when using telnet and stuff
929 ;; as the connection method, it's important that the
930 ;; QUIT command actually is sent out before we kill
933 (nntp-kill-buffer (process-buffer process))
934 (setq process (car (pop nntp-connection-alist))))
935 (nnoo-close-server 'nntp)))
937 (deffoo nntp-request-close ()
939 (while (setq process (pop nntp-connection-list))
940 (when (memq (process-status process) '(open run))
942 (nntp-send-string process "QUIT")
943 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
944 ;; Ok, this is evil, but when using telnet and stuff
945 ;; as the connection method, it's important that the
946 ;; QUIT command actually is sent out before we kill
949 (nntp-kill-buffer (process-buffer process)))))
951 (deffoo nntp-request-list (&optional server)
952 (nntp-with-open-group
954 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
956 (deffoo nntp-request-list-newsgroups (&optional server)
957 (nntp-with-open-group
959 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
961 (deffoo nntp-request-newgroups (date &optional server)
962 (nntp-with-open-group
965 (set-buffer nntp-server-buffer)
966 (let* ((time (date-to-time date))
967 (ls (- (cadr time) (nth 8 (decode-time time)))))
969 (setcar time (1- (car time)))
970 (setcar (cdr time) (+ ls 65536)))
972 (setcar time (1+ (car time)))
973 (setcar (cdr time) (- ls 65536)))
975 (setcar (cdr time) ls)))
978 "^\\.\r?\n" "NEWGROUPS"
979 (format-time-string "%y%m%d %H%M%S" time)
981 (nntp-decode-text))))))
983 (deffoo nntp-request-post (&optional server)
984 (nntp-with-open-group
986 (when (nntp-send-command "^[23].*\r?\n" "POST")
987 (let ((response (with-current-buffer nntp-server-buffer
988 nntp-process-response))
991 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
993 (setq server-id (match-string 1 response))
994 (narrow-to-region (goto-char (point-min))
995 (if (search-forward "\n\n" nil t)
998 (unless (mail-fetch-field "Message-ID")
999 (goto-char (point-min))
1000 (insert "Message-ID: " server-id "\n"))
1002 (run-hooks 'nntp-prepare-post-hook)
1003 (nntp-send-buffer "^[23].*\n")))))
1005 (deffoo nntp-request-type (group article)
1008 (deffoo nntp-asynchronous-p ()
1011 ;;; Hooky functions.
1013 (defun nntp-send-mode-reader ()
1014 "Send the MODE READER command to the nntp server.
1015 This function is supposed to be called from `nntp-server-opened-hook'.
1016 It will make innd servers spawn an nnrpd process to allow actual article
1018 (nntp-send-command "^.*\n" "MODE READER"))
1020 (defun nntp-send-authinfo (&optional send-if-force)
1021 "Send the AUTHINFO to the nntp server.
1022 It will look in the \"~/.authinfo\" file for matching entries. If
1023 nothing suitable is found there, it will prompt for a user name
1026 If SEND-IF-FORCE, only send authinfo to the server if the
1027 .authinfo file has the FORCE token."
1028 (let* ((list (gnus-parse-netrc nntp-authinfo-file))
1029 (alist (gnus-netrc-machine list nntp-address "nntp"))
1030 (force (gnus-netrc-get alist "force"))
1031 (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
1032 (passwd (gnus-netrc-get alist "password")))
1033 (when (or (not send-if-force)
1036 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1037 nntp-authinfo-user user))
1038 (unless (member user '(nil ""))
1039 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1040 (when t ;???Should check if AUTHINFO succeeded
1042 "^2.*\r?\n" "AUTHINFO PASS"
1044 nntp-authinfo-password
1045 (setq nntp-authinfo-password
1046 (read-passwd (format "NNTP (%s@%s) password: "
1047 user nntp-address))))))))))
1049 (defun nntp-send-nosy-authinfo ()
1050 "Send the AUTHINFO to the nntp server."
1051 (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
1052 (unless (member user '(nil ""))
1053 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1054 (when t ;???Should check if AUTHINFO succeeded
1055 (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
1056 (read-passwd (format "NNTP (%s@%s) password: "
1057 user nntp-address)))))))
1059 (defun nntp-send-authinfo-from-file ()
1060 "Send the AUTHINFO to the nntp server.
1062 The authinfo login name is taken from the user's login name and the
1063 password contained in '~/.nntp-authinfo'."
1064 (when (file-exists-p "~/.nntp-authinfo")
1066 (insert-file-contents "~/.nntp-authinfo")
1067 (goto-char (point-min))
1068 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
1070 "^2.*\r?\n" "AUTHINFO PASS"
1071 (buffer-substring (point) (gnus-point-at-eol))))))
1073 ;;; Internal functions.
1075 (defun nntp-handle-authinfo (process)
1076 "Take care of an authinfo response from the server."
1077 (let ((last nntp-last-command))
1078 (funcall nntp-authinfo-function)
1079 ;; We have to re-send the function that was interrupted by
1080 ;; the authinfo request.
1082 (set-buffer nntp-server-buffer)
1084 (nntp-send-string process last)))
1086 (defun nntp-make-process-buffer (buffer)
1087 "Create a new, fresh buffer usable for nntp process connections."
1090 (generate-new-buffer
1091 (format " *server %s %s %s*"
1092 nntp-address nntp-port-number
1093 (gnus-buffer-exists-p buffer))))
1094 (mm-enable-multibyte)
1095 (set (make-local-variable 'after-change-functions) nil)
1096 (set (make-local-variable 'nntp-process-wait-for) nil)
1097 (set (make-local-variable 'nntp-process-callback) nil)
1098 (set (make-local-variable 'nntp-process-to-buffer) nil)
1099 (set (make-local-variable 'nntp-process-start-point) nil)
1100 (set (make-local-variable 'nntp-process-decode) nil)
1103 (defun nntp-open-connection (buffer)
1104 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1105 (run-hooks 'nntp-prepare-server-hook)
1106 (let* ((pbuffer (nntp-make-process-buffer buffer))
1108 (and nntp-connection-timeout
1109 (nnheader-run-at-time
1110 nntp-connection-timeout nil
1112 (nntp-kill-buffer ,pbuffer)))))
1115 (let ((coding-system-for-read nntp-coding-system-for-read)
1116 (coding-system-for-write nntp-coding-system-for-write))
1117 (funcall nntp-open-connection-function pbuffer))
1120 (message "Quit opening connection")
1121 (nntp-kill-buffer pbuffer)
1125 (nnheader-cancel-timer timer))
1127 (nntp-kill-buffer pbuffer))
1128 (when (and (buffer-name pbuffer)
1130 (process-kill-without-query process)
1131 (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1132 (memq (process-status process) '(open run)))
1134 (caar (push (list process buffer nil) nntp-connection-alist))
1135 (push process nntp-connection-list)
1137 (set-buffer pbuffer)
1138 (nntp-read-server-type)
1140 (set-buffer nntp-server-buffer)
1141 (let ((nnheader-callback-function nil))
1142 (run-hooks 'nntp-server-opened-hook)
1143 (nntp-send-authinfo t))))
1144 (nntp-kill-buffer (process-buffer process))
1147 (defun nntp-open-network-stream (buffer)
1148 (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
1150 (autoload 'format-spec "format")
1151 (autoload 'format-spec-make "format")
1152 (autoload 'open-tls-stream "tls")
1154 (defun nntp-open-ssl-stream (buffer)
1155 (let* ((process-connection-type nil)
1156 (proc (start-process "nntpd" buffer
1158 shell-command-switch
1159 (format-spec nntp-ssl-program
1162 ?p nntp-port-number)))))
1163 (process-kill-without-query proc)
1166 (let ((nntp-connection-alist (list proc buffer nil)))
1167 (nntp-wait-for-string "^\r*20[01]"))
1169 (delete-region (point-min) (point))
1172 (defun nntp-open-tls-stream (buffer)
1173 (let ((proc (open-tls-stream "nntpd" buffer nntp-address nntp-port-number)))
1174 (process-kill-without-query proc)
1177 (let ((nntp-connection-alist (list proc buffer nil)))
1178 (nntp-wait-for-string "^\r*20[01]"))
1180 (delete-region (point-min) (point))
1183 (defun nntp-read-server-type ()
1184 "Find out what the name of the server we have connected to is."
1185 ;; Wait for the status string to arrive.
1186 (setq nntp-server-type (buffer-string))
1187 (let ((alist nntp-server-action-alist)
1188 (case-fold-search t)
1190 ;; Run server-specific commands.
1192 (setq entry (pop alist))
1193 (when (string-match (car entry) nntp-server-type)
1194 (if (and (listp (cadr entry))
1195 (not (eq 'lambda (caadr entry))))
1197 (funcall (cadr entry)))))))
1199 (defun nntp-async-wait (process wait-for buffer decode callback)
1201 (set-buffer (process-buffer process))
1202 (unless nntp-inside-change-function
1204 (setq nntp-process-wait-for wait-for
1205 nntp-process-to-buffer buffer
1206 nntp-process-decode decode
1207 nntp-process-callback callback
1208 nntp-process-start-point (point-max))
1209 (setq after-change-functions '(nntp-after-change-function))
1210 (if nntp-async-needs-kluge
1211 (nntp-async-kluge process))))
1213 (defun nntp-async-kluge (process)
1214 ;; emacs 20.3 bug: process output with encoding 'binary
1215 ;; doesn't trigger after-change-functions.
1216 (unless nntp-async-timer
1217 (setq nntp-async-timer
1218 (nnheader-run-at-time 1 1 'nntp-async-timer-handler)))
1219 (add-to-list 'nntp-async-process-list process))
1221 (defun nntp-async-timer-handler ()
1224 (if (memq (process-status proc) '(open run))
1225 (nntp-async-trigger proc)
1226 (nntp-async-stop proc)))
1227 nntp-async-process-list))
1229 (defun nntp-async-stop (proc)
1230 (setq nntp-async-process-list (delq proc nntp-async-process-list))
1231 (when (and nntp-async-timer (not nntp-async-process-list))
1232 (nnheader-cancel-timer nntp-async-timer)
1233 (setq nntp-async-timer nil)))
1235 (defun nntp-after-change-function (beg end len)
1237 ;; we only care about insertions at eob
1238 (when (and (eq 0 len) (eq (point-max) end))
1240 (let ((proc (get-buffer-process (current-buffer))))
1242 (nntp-async-trigger proc)))))
1243 ;; any throw from after-change-functions will leave it
1244 ;; set to nil. so we reset it here, if necessary.
1246 (setq after-change-functions '(nntp-after-change-function)))))
1248 (defun nntp-async-trigger (process)
1250 (set-buffer (process-buffer process))
1251 (when nntp-process-callback
1252 ;; do we have an error message?
1253 (goto-char nntp-process-start-point)
1254 (if (memq (following-char) '(?4 ?5))
1255 ;; wants credentials?
1256 (if (looking-at "480")
1257 (nntp-handle-authinfo process)
1258 ;; report error message.
1259 (nntp-snarf-error-message)
1260 (nntp-do-callback nil))
1262 ;; got what we expect?
1263 (goto-char (point-max))
1264 (when (re-search-backward
1265 nntp-process-wait-for nntp-process-start-point t)
1266 (let ((response (match-string 0)))
1267 (with-current-buffer nntp-server-buffer
1268 (setq nntp-process-response response)))
1269 (nntp-async-stop process)
1271 (when (gnus-buffer-exists-p nntp-process-to-buffer)
1272 (let ((buf (current-buffer))
1273 (start nntp-process-start-point)
1274 (decode nntp-process-decode))
1276 (set-buffer nntp-process-to-buffer)
1277 (goto-char (point-max))
1279 (narrow-to-region (point) (point))
1280 (insert-buffer-substring buf start)
1282 (nntp-decode-text))))))
1284 (goto-char (point-max))
1286 (buffer-name (get-buffer nntp-process-to-buffer))))))))
1288 (defun nntp-do-callback (arg)
1289 (let ((callback nntp-process-callback)
1290 (nntp-inside-change-function t))
1291 (setq nntp-process-callback nil)
1292 (funcall callback arg)))
1294 (defun nntp-snarf-error-message ()
1295 "Save the error message in the current buffer."
1296 (let ((message (buffer-string)))
1297 (while (string-match "[\r\n]+" message)
1298 (setq message (replace-match " " t t message)))
1299 (nnheader-report 'nntp message)
1302 (defun nntp-accept-process-output (process)
1303 "Wait for output from PROCESS and message some dots."
1305 (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1306 nntp-server-buffer))
1307 (let ((len (/ (buffer-size) 1024))
1310 (setq nntp-have-messaged t)
1311 (nnheader-message 7 "nntp read: %dk" len)))
1312 (nnheader-accept-process-output process)
1313 ;; accept-process-output may update status of process to indicate
1314 ;; that the server has closed the connection. This MUST be
1315 ;; handled here as the buffer restored by the save-excursion may
1316 ;; be the process's former output buffer (i.e. now killed)
1318 (memq (process-status process) '(open run)))
1319 (nntp-report "Server closed connection"))))
1321 (defun nntp-accept-response ()
1322 "Wait for output from the process that outputs to BUFFER."
1323 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1325 (defun nntp-possibly-change-group (group server &optional connectionless)
1326 (let ((nnheader-callback-function nil))
1328 (or (nntp-server-opened server)
1329 (nntp-open-server server nil connectionless)))
1331 (unless connectionless
1332 (or (nntp-find-connection nntp-server-buffer)
1333 (nntp-open-connection nntp-server-buffer))))
1336 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1338 (nntp-report "Server closed connection"))
1339 ((not (equal group (caddr entry)))
1341 (set-buffer (process-buffer (car entry)))
1343 (nntp-send-command "^[245].*\n" "GROUP" group)
1344 (setcar (cddr entry) group)
1347 (set-buffer nntp-server-buffer)
1348 (erase-buffer))))))))
1350 (defun nntp-decode-text (&optional cr-only)
1351 "Decode the text in the current buffer."
1352 (goto-char (point-min))
1353 (while (search-forward "\r" nil t)
1356 ;; Remove trailing ".\n" end-of-transfer marker.
1357 (goto-char (point-max))
1359 (when (looking-at ".\n")
1361 ;; Delete status line.
1362 (goto-char (point-min))
1363 (while (looking-at "[1-5][0-9][0-9] .*\n")
1364 ;; For some unknown reason, there is more than one status line.
1365 (delete-region (point) (progn (forward-line 1) (point))))
1366 ;; Remove "." -> ".." encoding.
1367 (while (search-forward "\n.." nil t)
1370 (defun nntp-encode-text ()
1371 "Encode the text in the current buffer."
1373 ;; Replace "." at beginning of line with "..".
1374 (goto-char (point-min))
1375 (while (re-search-forward "^\\." nil t)
1377 (goto-char (point-max))
1378 ;; Insert newline at the end of the buffer.
1381 ;; Insert `.' at end of buffer (end of text mark).
1382 (goto-char (point-max))
1384 (goto-char (point-min))
1388 (insert nntp-end-of-line))))
1390 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1391 (set-buffer nntp-server-buffer)
1395 ;; This server does not talk NOV.
1396 ((not nntp-server-xover)
1399 ;; We don't care about gaps.
1400 ((or (not nntp-nov-gap)
1402 (nntp-send-xover-command
1404 (if (numberp fetch-old)
1405 (max 1 (- (car articles) fetch-old))
1408 (car (last articles)) 'wait)
1410 (goto-char (point-min))
1411 (when (looking-at "[1-5][0-9][0-9] .*\n")
1412 (delete-region (point) (progn (forward-line 1) (point))))
1413 (while (search-forward "\r" nil t)
1414 (replace-match "" t t))
1415 (goto-char (point-max))
1417 (when (looking-at "\\.")
1418 (delete-region (point) (progn (forward-line 1) (point)))))
1420 ;; We do it the hard way. For each gap, an XOVER command is sent
1421 ;; to the server. We do not wait for a reply from the server, we
1422 ;; just send them off as fast as we can. That means that we have
1423 ;; to count the number of responses we get back to find out when we
1424 ;; have gotten all we asked for.
1425 ((numberp nntp-nov-gap)
1430 (buf nntp-server-buffer)
1431 (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1433 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1434 ;; that means that the server does not understand XOVER, but we
1435 ;; won't know that until we try.
1436 (while (and nntp-server-xover articles)
1437 (setq first (car articles))
1438 ;; Search forward until we find a gap, or until we run out of
1440 (while (and (cdr articles)
1441 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1442 (setq articles (cdr articles)))
1444 (setq in-process-buffer-p (stringp nntp-server-xover))
1445 (nntp-send-xover-command first (setq last (car articles)))
1446 (setq articles (cdr articles))
1448 (when (and nntp-server-xover in-process-buffer-p)
1449 ;; Don't count tried request.
1450 (setq count (1+ count))
1452 ;; Every 400 requests we have to read the stream in
1453 ;; order to avoid deadlocks.
1454 (when (or (null articles) ;All requests have been sent.
1455 (= 1 (% count nntp-maximum-request)))
1457 (nntp-accept-response)
1458 ;; On some Emacs versions the preceding function has a
1459 ;; tendency to change the buffer. Perhaps. It's quite
1460 ;; difficult to reproduce, because it only seems to happen
1461 ;; once in a blue moon.
1462 (set-buffer process-buffer)
1464 (goto-char (or last-point (point-min)))
1466 (while (re-search-forward "^\\([0-9][0-9][0-9]\\) .*\n"
1469 (setq status (match-string 1))
1470 (if (string-match "^[45]" status)
1471 (setq status 'error)
1473 (setq last-point (point))
1474 (or (< received count)
1475 (if (eq status 'error)
1477 ;; I haven't started reading the final response
1479 (goto-char (point-max))
1481 (not (looking-at "^\\.\r?\n"))))))
1482 ;; I haven't read the end of the final response
1483 (nntp-accept-response)
1484 (set-buffer process-buffer))))
1486 ;; Some nntp servers seem to have an extension to the XOVER
1487 ;; extension. On these servers, requesting an article range
1488 ;; preceeding the active range does not return an error as
1489 ;; specified in the RFC. What we instead get is the NOV entry
1490 ;; for the first available article. Obviously, a client can
1491 ;; use that entry to avoid making unnecessary requests. The
1492 ;; only problem is for a client that assumes that the response
1493 ;; will always be within the requested ranage. For such a
1494 ;; client, we can get N copies of the same entry (one for each
1495 ;; XOVER command sent to the server).
1498 (goto-char (point-min))
1499 (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1500 (let ((low-limit (string-to-int
1501 (buffer-substring (match-beginning 1)
1503 (while (and articles (<= (car articles) low-limit))
1504 (setq articles (cdr articles))))))
1507 (when nntp-server-xover
1508 (when in-process-buffer-p
1510 (goto-char (point-max))
1511 (insert-buffer-substring process-buffer)
1512 (set-buffer process-buffer)
1516 ;; We remove any "." lines and status lines.
1517 (goto-char (point-min))
1518 (while (search-forward "\r" nil t)
1520 (goto-char (point-min))
1521 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1526 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1527 "Send the XOVER command to the server."
1528 (let ((range (format "%d-%d" beg end))
1529 (nntp-inhibit-erase t))
1530 (if (stringp nntp-server-xover)
1531 ;; If `nntp-server-xover' is a string, then we just send this
1534 (nntp-send-command-nodelete
1535 "\r?\n\\.\r?\n" nntp-server-xover range)
1536 ;; We do not wait for the reply.
1537 (nntp-send-command-nodelete nil nntp-server-xover range))
1538 (let ((commands nntp-xover-commands))
1539 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1540 ;; We try them all until we get at positive response.
1541 (while (and commands (eq nntp-server-xover 'try))
1542 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1544 (set-buffer nntp-server-buffer)
1545 (goto-char (point-min))
1546 (and (looking-at "[23]") ; No error message.
1547 ;; We also have to look at the lines. Some buggy
1548 ;; servers give back simple lines with just the
1549 ;; article number. How... helpful.
1552 (looking-at "[0-9]+\t...")) ; More text after number.
1553 (setq nntp-server-xover (car commands))))
1554 (setq commands (cdr commands)))
1555 ;; If none of the commands worked, we disable XOVER.
1556 (when (eq nntp-server-xover 'try)
1558 (set-buffer nntp-server-buffer)
1560 (setq nntp-server-xover nil)))
1561 nntp-server-xover))))
1563 (defun nntp-find-group-and-number (&optional group)
1566 (set-buffer nntp-server-buffer)
1567 (narrow-to-region (goto-char (point-min))
1568 (or (search-forward "\n\n" nil t) (point-max)))
1569 (goto-char (point-min))
1570 ;; We first find the number by looking at the status line.
1571 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1573 (buffer-substring (match-beginning 1)
1576 (and number (zerop number) (setq number nil))
1578 ;; Then we find the group name.
1581 ;; If there is only one group in the Newsgroups
1582 ;; header, then it seems quite likely that this
1583 ;; article comes from that group, I'd say.
1584 ((and (setq newsgroups
1585 (mail-fetch-field "newsgroups"))
1586 (not (string-match "," newsgroups)))
1588 ;; If there is more than one group in the
1589 ;; Newsgroups header, then the Xref header should
1590 ;; be filled out. We hazard a guess that the group
1591 ;; that has this article number in the Xref header
1592 ;; is the one we are looking for. This might very
1593 ;; well be wrong if this article happens to have
1594 ;; the same number in several groups, but that's
1596 ((and (setq xref (mail-fetch-field "xref"))
1599 (format "\\([^ :]+\\):%d" number) xref))
1600 (match-string 1 xref))
1603 ((and (setq xref (mail-fetch-field "xref"))
1606 (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1607 "\\([^ :]+\\):\\([0-9]+\\)")
1609 (setq group (match-string 1 xref)
1610 number (string-to-int (match-string 2 xref))))
1611 ((and (setq newsgroups
1612 (mail-fetch-field "newsgroups"))
1613 (not (string-match "," newsgroups)))
1614 (setq group newsgroups))
1616 (t (setq group ""))))
1617 (when (string-match "\r" group)
1618 (setq group (substring group 0 (match-beginning 0))))
1619 (cons group number)))))
1621 (defun nntp-wait-for-string (regexp)
1622 "Wait until string arrives in the buffer."
1623 (let ((buf (current-buffer))
1625 (goto-char (point-min))
1626 (while (and (setq proc (get-buffer-process buf))
1627 (memq (process-status proc) '(open run))
1628 (not (re-search-forward regexp nil t)))
1629 (accept-process-output proc)
1631 (goto-char (point-min)))))
1634 ;; ==========================================================================
1635 ;; Obsolete nntp-open-* connection methods -- drv
1636 ;; ==========================================================================
1638 (defvoo nntp-open-telnet-envuser nil
1639 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1641 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
1642 "*Regular expression to match the shell prompt on the remote machine.")
1644 (defvoo nntp-rlogin-program "rsh"
1645 "*Program used to log in on remote machines.
1646 The default is \"rsh\", but \"ssh\" is a popular alternative.")
1648 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1649 "*Parameters to `nntp-open-rlogin'.
1650 That function may be used as `nntp-open-connection-function'. In that
1651 case, this list will be used as the parameter list given to rsh.")
1653 (defvoo nntp-rlogin-user-name nil
1654 "*User name on remote system when using the rlogin connect method.")
1656 (defvoo nntp-telnet-parameters
1657 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1658 "*Parameters to `nntp-open-telnet'.
1659 That function may be used as `nntp-open-connection-function'. In that
1660 case, this list will be executed as a command after logging in
1663 (defvoo nntp-telnet-user-name nil
1664 "User name to log in via telnet with.")
1666 (defvoo nntp-telnet-passwd nil
1667 "Password to use to log in via telnet with.")
1669 (defun nntp-open-telnet (buffer)
1675 "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1676 (case-fold-search t))
1677 (when (memq (process-status proc) '(open run))
1678 (nntp-wait-for-string "^r?telnet")
1679 (process-send-string proc "set escape \^X\n")
1681 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1682 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1683 nntp-address "\n")))
1685 (process-send-string proc (concat "open " nntp-address "\n"))))
1687 ((not nntp-open-telnet-envuser)
1688 (nntp-wait-for-string "^\r*.?login:")
1689 (process-send-string
1691 (or nntp-telnet-user-name
1692 (setq nntp-telnet-user-name (read-string "login: ")))
1694 (nntp-wait-for-string "^\r*.?password:")
1695 (process-send-string
1697 (or nntp-telnet-passwd
1698 (setq nntp-telnet-passwd
1699 (read-passwd "Password: ")))
1701 (nntp-wait-for-string nntp-telnet-shell-prompt)
1702 (process-send-string
1703 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1704 (nntp-wait-for-string "^\r*20[01]")
1706 (delete-region (point-min) (point))
1707 (process-send-string proc "\^]")
1708 (nntp-wait-for-string "^r?telnet")
1709 (process-send-string proc "mode character\n")
1710 (accept-process-output proc 1)
1712 (goto-char (point-min))
1714 (delete-region (point) (point-max)))
1717 (defun nntp-open-rlogin (buffer)
1718 "Open a connection to SERVER using rsh."
1719 (let ((proc (if nntp-rlogin-user-name
1720 (apply 'start-process
1721 "nntpd" buffer nntp-rlogin-program
1722 nntp-address "-l" nntp-rlogin-user-name
1723 nntp-rlogin-parameters)
1724 (apply 'start-process
1725 "nntpd" buffer nntp-rlogin-program nntp-address
1726 nntp-rlogin-parameters))))
1729 (nntp-wait-for-string "^\r*20[01]")
1731 (delete-region (point-min) (point))
1735 ;; ==========================================================================
1736 ;; Replacements for the nntp-open-* functions -- drv
1737 ;; ==========================================================================
1739 (defun nntp-open-telnet-stream (buffer)
1740 "Open a nntp connection by telnet'ing the news server.
1742 Please refer to the following variables to customize the connection:
1743 - `nntp-pre-command',
1744 - `nntp-telnet-command',
1745 - `nntp-telnet-switches',
1747 - `nntp-port-number',
1748 - `nntp-end-of-line'."
1749 (let ((command `(,nntp-telnet-command
1750 ,@nntp-telnet-switches
1751 ,nntp-address ,nntp-port-number))
1753 (and nntp-pre-command
1754 (push nntp-pre-command command))
1755 (setq proc (apply 'start-process "nntpd" buffer command))
1758 (nntp-wait-for-string "^\r*20[01]")
1760 (delete-region (point-min) (point))
1763 (defun nntp-open-via-rlogin-and-telnet (buffer)
1764 "Open a connection to an nntp server through an intermediate host.
1765 First rlogin to the remote host, and then telnet the real news server
1768 Please refer to the following variables to customize the connection:
1769 - `nntp-pre-command',
1770 - `nntp-via-rlogin-command',
1771 - `nntp-via-rlogin-command-switches',
1772 - `nntp-via-user-name',
1773 - `nntp-via-address',
1774 - `nntp-telnet-command',
1775 - `nntp-telnet-switches',
1777 - `nntp-port-number',
1778 - `nntp-end-of-line'."
1779 (let ((command `(,nntp-via-address
1780 ,nntp-telnet-command
1781 ,@nntp-telnet-switches))
1783 (when nntp-via-user-name
1784 (setq command `("-l" ,nntp-via-user-name ,@command)))
1785 (when nntp-via-rlogin-command-switches
1786 (setq command (append nntp-via-rlogin-command-switches command)))
1787 (push nntp-via-rlogin-command command)
1788 (and nntp-pre-command
1789 (push nntp-pre-command command))
1790 (setq proc (apply 'start-process "nntpd" buffer command))
1793 (nntp-wait-for-string "^r?telnet")
1794 (process-send-string proc (concat "open " nntp-address
1795 " " nntp-port-number "\n"))
1796 (nntp-wait-for-string "^\r*20[01]")
1798 (delete-region (point-min) (point))
1799 (process-send-string proc "\^]")
1800 (nntp-wait-for-string "^r?telnet")
1801 (process-send-string proc "mode character\n")
1802 (accept-process-output proc 1)
1804 (goto-char (point-min))
1806 (delete-region (point) (point-max)))
1809 (defun nntp-open-via-telnet-and-telnet (buffer)
1810 "Open a connection to an nntp server through an intermediate host.
1811 First telnet the remote host, and then telnet the real news server
1814 Please refer to the following variables to customize the connection:
1815 - `nntp-pre-command',
1816 - `nntp-via-telnet-command',
1817 - `nntp-via-telnet-switches',
1818 - `nntp-via-address',
1819 - `nntp-via-envuser',
1820 - `nntp-via-user-name',
1821 - `nntp-via-user-password',
1822 - `nntp-via-shell-prompt',
1823 - `nntp-telnet-command',
1824 - `nntp-telnet-switches',
1826 - `nntp-port-number',
1827 - `nntp-end-of-line'."
1831 (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
1832 (case-fold-search t)
1834 (and nntp-pre-command (push nntp-pre-command command))
1835 (setq proc (apply 'start-process "nntpd" buffer command))
1836 (when (memq (process-status proc) '(open run))
1837 (nntp-wait-for-string "^r?telnet")
1838 (process-send-string proc "set escape \^X\n")
1840 ((and nntp-via-envuser nntp-via-user-name)
1841 (process-send-string proc (concat "open " "-l" nntp-via-user-name
1842 nntp-via-address "\n")))
1844 (process-send-string proc (concat "open " nntp-via-address
1846 (when (not nntp-via-envuser)
1847 (nntp-wait-for-string "^\r*.?login:")
1848 (process-send-string proc
1850 (or nntp-via-user-name
1851 (setq nntp-via-user-name
1852 (read-string "login: ")))
1854 (nntp-wait-for-string "^\r*.?password:")
1855 (process-send-string proc
1857 (or nntp-via-user-password
1858 (setq nntp-via-user-password
1859 (read-passwd "Password: ")))
1861 (nntp-wait-for-string nntp-via-shell-prompt)
1862 (let ((real-telnet-command `("exec"
1863 ,nntp-telnet-command
1864 ,@nntp-telnet-switches
1866 ,nntp-port-number)))
1867 (process-send-string proc
1868 (concat (mapconcat 'identity
1869 real-telnet-command " ")
1871 (nntp-wait-for-string "^\r*20[01]")
1873 (delete-region (point-min) (point))
1874 (process-send-string proc "\^]")
1875 (nntp-wait-for-string "^r?telnet")
1876 (process-send-string proc "mode character\n")
1877 (accept-process-output proc 1)
1879 (goto-char (point-min))
1881 (delete-region (point) (point-max)))
1886 ;;; arch-tag: 8655466a-b1b5-4929-9c45-7b1b2e767271
1887 ;;; nntp.el ends here