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))
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-tls-stream',
79 - `nntp-open-telnet-stream'.
82 - `nntp-open-via-rlogin-and-telnet',
83 - `nntp-open-via-telnet-and-telnet'.")
85 (defvoo nntp-pre-command nil
86 "*Pre-command to use with the various nntp-open-via-* methods.
87 This is where you would put \"runsocks\" or stuff like that.")
89 (defvoo nntp-telnet-command "telnet"
90 "*Telnet command used to connect to the nntp server.
91 This command is used by the various nntp-open-via-* methods.")
93 (defvoo nntp-telnet-switches '("-8")
94 "*Switches given to the telnet command `nntp-telnet-command'.")
96 (defvoo nntp-end-of-line "\r\n"
97 "*String to use on the end of lines when talking to the NNTP server.
98 This is \"\\r\\n\" by default, but should be \"\\n\" when
99 using and indirect connection method (nntp-open-via-*).")
101 (defvoo nntp-via-rlogin-command "rsh"
102 "*Rlogin command used to connect to an intermediate host.
103 This command is used by the `nntp-open-via-rlogin-and-telnet' method.
104 The default is \"rsh\", but \"ssh\" is a popular alternative.")
106 (defvoo nntp-via-rlogin-command-switches nil
107 "*Switches given to the rlogin command `nntp-via-rlogin-command'.
108 If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
109 \(\"-C\") in order to compress all data connections, otherwise set this
110 to \(\"-t\" \"-e\" \"none\") or (\"-C\" \"-t\" \"-e\" \"none\") if the telnet
111 command requires a pseudo-tty allocation on an intermediate host.")
113 (defvoo nntp-via-telnet-command "telnet"
114 "*Telnet command used to connect to an intermediate host.
115 This command is used by the `nntp-open-via-telnet-and-telnet' method.")
117 (defvoo nntp-via-telnet-switches '("-8")
118 "*Switches given to the telnet command `nntp-via-telnet-command'.")
120 (defvoo nntp-via-user-name nil
121 "*User name to log in on an intermediate host with.
122 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
124 (defvoo nntp-via-user-password nil
125 "*Password to use 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-address nil
129 "*Address of an intermediate host to connect to.
130 This variable is used by the `nntp-open-via-rlogin-and-telnet' and
131 `nntp-open-via-telnet-and-telnet' methods.")
133 (defvoo nntp-via-envuser nil
134 "*Whether both telnet client and server support the ENVIRON option.
135 If non-nil, there will be no prompt for a login name.")
137 (defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
138 "*Regular expression to match the shell prompt on an intermediate host.
139 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
141 (defvoo nntp-large-newsgroup 50
142 "*The number of the articles which indicates a large newsgroup.
143 If the number of the articles is greater than the value, verbose
144 messages will be shown to indicate the current status.")
146 (defvoo nntp-maximum-request 400
147 "*The maximum number of the requests sent to the NNTP server at one time.
148 If Emacs hangs up while retrieving headers, set the variable to a
151 (defvoo nntp-nov-is-evil nil
152 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
154 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
155 "*List of strings that are used as commands to fetch NOV lines from a server.
156 The strings are tried in turn until a positive response is gotten. If
157 none of the commands are successful, nntp will just grab headers one
160 (defvoo nntp-nov-gap 5
161 "*Maximum allowed gap between two articles.
162 If the gap between two consecutive articles is bigger than this
163 variable, split the XOVER request into two requests.")
165 (defvoo nntp-prepare-server-hook nil
166 "*Hook run before a server is opened.
167 If can be used to set up a server remotely, for instance. Say you
168 have an account at the machine \"other.machine\". This machine has
169 access to an NNTP server that you can't access locally. You could
170 then use this hook to rsh to the remote machine and start a proxy NNTP
171 server there that you can connect to. See also
172 `nntp-open-connection-function'")
174 (defvoo nntp-warn-about-losing-connection t
175 "*If non-nil, beep when a server closes connection.")
177 (defvoo nntp-coding-system-for-read 'binary
178 "*Coding system to read from NNTP.")
180 (defvoo nntp-coding-system-for-write 'binary
181 "*Coding system to write to NNTP.")
183 (defcustom nntp-authinfo-file "~/.authinfo"
184 ".netrc-like file that holds nntp authinfo passwords."
187 (repeat :tag "Entries"
190 :value ("" ("login" . "") ("password" . ""))
194 (const :format "" "login")
195 (string :format "Login: %v"))
197 (const :format "" "password")
198 (string :format "Password: %v")))))))
202 (defvoo nntp-connection-timeout nil
203 "*Number of seconds to wait before an nntp connection times out.
204 If this variable is nil, which is the default, no timers are set.
205 NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
207 (defvoo nntp-prepare-post-hook nil
208 "*Hook run just before posting an article. It is supposed to be used
209 to insert Cancel-Lock headers.")
211 ;;; Internal variables.
213 (defvar nntp-record-commands nil
214 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
216 (defvar nntp-have-messaged nil)
218 (defvar nntp-process-wait-for nil)
219 (defvar nntp-process-to-buffer nil)
220 (defvar nntp-process-callback nil)
221 (defvar nntp-process-decode nil)
222 (defvar nntp-process-start-point nil)
223 (defvar nntp-inside-change-function nil)
224 (defvoo nntp-last-command-time nil)
225 (defvoo nntp-last-command nil)
226 (defvoo nntp-authinfo-password nil)
227 (defvoo nntp-authinfo-user nil)
229 (defvar nntp-connection-list nil)
231 (defvoo nntp-server-type nil)
232 (defvoo nntp-connection-alist nil)
233 (defvoo nntp-status-string "")
234 (defconst nntp-version "nntp 5.0")
235 (defvoo nntp-inhibit-erase nil)
236 (defvoo nntp-inhibit-output nil)
238 (defvoo nntp-server-xover 'try)
239 (defvoo nntp-server-list-active-group 'try)
241 (defvar nntp-async-needs-kluge
242 (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
243 "*When non-nil, nntp will poll asynchronous connections
244 once a second. By default, this is turned on only for Emacs
245 20.3, which has a bug that breaks nntp's normal method of
246 noticing asynchronous data.")
248 (defvar nntp-async-timer nil)
249 (defvar nntp-async-process-list nil)
251 (defvar nntp-ssl-program
252 "openssl s_client -quiet -ssl3 -connect %s:%p"
253 "A string containing commands for SSL connections.
254 Within a string, %s is replaced with the server address and %p with
255 port number on server. The program should accept IMAP commands on
256 stdin and return responses to stdout.")
260 ;;; Internal functions.
262 (defsubst nntp-send-string (process string)
263 "Send STRING to PROCESS."
264 ;; We need to store the time to provide timeouts, and
265 ;; to store the command so the we can replay the command
266 ;; if the server gives us an AUTHINFO challenge.
267 (setq nntp-last-command-time (current-time)
268 nntp-last-command string)
269 (when nntp-record-commands
270 (nntp-record-command string))
271 (process-send-string process (concat string nntp-end-of-line))
272 (or (memq (process-status process) '(open run))
273 (nntp-report "Server closed connection")))
275 (defun nntp-record-command (string)
276 "Record the command STRING."
278 (set-buffer (get-buffer-create "*nntp-log*"))
279 (goto-char (point-max))
280 (let ((time (current-time)))
281 (insert (format-time-string "%Y%m%dT%H%M%S" time)
282 "." (format "%03d" (/ (nth 2 time) 1000))
283 " " nntp-address " " string "\n"))))
285 (defun nntp-report (&rest args)
286 "Report an error from the nntp backend. The first string in ARGS
287 can be a format string. For some commands, the failed command may be
288 retried once before actually displaying the error report."
290 (when nntp-record-commands
291 (nntp-record-command "*** CALLED nntp-report ***"))
293 (nnheader-report 'nntp args)
297 (defun nntp-report-1 (&rest args)
298 "Throws out to nntp-with-open-group-error so that the connection may
299 be restored and the command retried."
301 (when nntp-record-commands
302 (nntp-record-command "*** CONNECTION LOST ***"))
304 (throw 'nntp-with-open-group-error t))
306 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
307 "Wait for WAIT-FOR to arrive from PROCESS."
309 (set-buffer (process-buffer process))
310 (goto-char (point-min))
311 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
313 (memq (process-status process) '(open run)))
314 (when (looking-at "480")
315 (nntp-handle-authinfo process))
316 (when (looking-at "^.*\n")
317 (delete-region (point) (progn (forward-line 1) (point))))
318 (nntp-accept-process-output process)
319 (goto-char (point-min)))
324 (nntp-snarf-error-message)
326 ((not (memq (process-status process) '(open run)))
327 (nntp-report "Server closed connection"))
329 (goto-char (point-max))
330 (let ((limit (point-min))
332 (while (not (re-search-backward wait-for limit t))
333 (nntp-accept-process-output process)
334 ;; We assume that whatever we wait for is less than 1000
336 (setq limit (max (- (point-max) 1000) (point-min)))
337 (goto-char (point-max)))
338 (setq response (match-string 0))
339 (with-current-buffer nntp-server-buffer
340 (setq nntp-process-response response)))
341 (nntp-decode-text (not decode))
345 (goto-char (point-max))
346 (insert-buffer-substring (process-buffer process))
347 ;; Nix out "nntp reading...." message.
348 (when nntp-have-messaged
349 (setq nntp-have-messaged nil)
350 (nnheader-message 5 ""))))
355 (defun nntp-kill-buffer (buffer)
356 (when (buffer-name buffer)
358 (nnheader-init-server-buffer)))
360 (defsubst nntp-find-connection (buffer)
361 "Find the connection delivering to BUFFER."
362 (let ((alist nntp-connection-alist)
363 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
365 (while (and alist (setq entry (pop alist)))
366 (when (eq buffer (cadr entry))
367 (setq process (car entry)
370 (if (memq (process-status process) '(open run))
372 (nntp-kill-buffer (process-buffer process))
373 (setq nntp-connection-alist (delq entry nntp-connection-alist))
376 (defsubst nntp-find-connection-entry (buffer)
377 "Return the entry for the connection to BUFFER."
378 (assq (nntp-find-connection buffer) nntp-connection-alist))
380 (defun nntp-find-connection-buffer (buffer)
381 "Return the process connection buffer tied to BUFFER."
382 (let ((process (nntp-find-connection buffer)))
384 (process-buffer process))))
386 (defsubst nntp-retrieve-data (command address port buffer
387 &optional wait-for callback decode)
388 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
389 (let ((process (or (nntp-find-connection buffer)
390 (nntp-open-connection buffer))))
393 (unless (or nntp-inhibit-erase nnheader-callback-function)
395 (set-buffer (process-buffer process))
400 (nntp-send-string process command))
402 ((eq callback 'ignore)
404 ((and callback wait-for)
405 (nntp-async-wait process wait-for buffer decode callback)
408 (nntp-wait-for process wait-for buffer decode))
411 (nnheader-report 'nntp "Couldn't open connection to %s: %s"
414 (message "Quit retrieving data from nntp")
417 (nnheader-report 'nntp "Couldn't open connection to %s" address))))
419 (defsubst nntp-send-command (wait-for &rest strings)
420 "Send STRINGS to server and wait until WAIT-FOR returns."
421 (when (and (not nnheader-callback-function)
422 (not nntp-inhibit-output))
424 (set-buffer nntp-server-buffer)
426 (let* ((command (mapconcat 'identity strings " "))
427 (process (nntp-find-connection nntp-server-buffer))
428 (buffer (and process (process-buffer process)))
429 (pos (and buffer (with-current-buffer buffer (point)))))
432 (nntp-retrieve-data command
433 nntp-address nntp-port-number
435 wait-for nnheader-callback-function)
436 ;; If nothing to wait for, still remove possibly echo'ed commands.
437 ;; We don't have echos if nntp-open-connection-function
438 ;; is `nntp-open-network-stream', so we skip this in that case.
440 (equal nntp-open-connection-function
441 'nntp-open-network-stream))
442 (nntp-accept-response)
446 (if (looking-at (regexp-quote command))
447 (delete-region pos (progn (forward-line 1)
448 (gnus-point-at-bol))))
450 (nnheader-report 'nntp "Couldn't open connection to %s."
453 (defun nntp-send-command-nodelete (wait-for &rest strings)
454 "Send STRINGS to server and wait until WAIT-FOR returns."
455 (let* ((command (mapconcat 'identity strings " "))
456 (process (nntp-find-connection nntp-server-buffer))
457 (buffer (and process (process-buffer process)))
458 (pos (and buffer (with-current-buffer buffer (point)))))
461 (nntp-retrieve-data command
462 nntp-address nntp-port-number
464 wait-for nnheader-callback-function)
465 ;; If nothing to wait for, still remove possibly echo'ed commands
467 (nntp-accept-response)
471 (if (looking-at (regexp-quote command))
472 (delete-region pos (progn (forward-line 1)
473 (gnus-point-at-bol))))
475 (nnheader-report 'nntp "Couldn't open connection to %s."
478 (defun nntp-send-command-and-decode (wait-for &rest strings)
479 "Send STRINGS to server and wait until WAIT-FOR returns."
480 (when (and (not nnheader-callback-function)
481 (not nntp-inhibit-output))
483 (set-buffer nntp-server-buffer)
485 (let* ((command (mapconcat 'identity strings " "))
486 (process (nntp-find-connection nntp-server-buffer))
487 (buffer (and process (process-buffer process)))
488 (pos (and buffer (with-current-buffer buffer (point)))))
491 (nntp-retrieve-data command
492 nntp-address nntp-port-number
494 wait-for nnheader-callback-function t)
495 ;; If nothing to wait for, still remove possibly echo'ed commands
497 (nntp-accept-response)
501 (if (looking-at (regexp-quote command))
502 (delete-region pos (progn (forward-line 1) (gnus-point-at-bol))))
504 (nnheader-report 'nntp "Couldn't open connection to %s."
508 (defun nntp-send-buffer (wait-for)
509 "Send the current buffer to server and wait until WAIT-FOR returns."
510 (when (and (not nnheader-callback-function)
511 (not nntp-inhibit-output))
513 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
516 (mm-with-unibyte-current-buffer
517 ;; Some encoded unicode text contains character 0x80-0x9f e.g. Euro.
518 (process-send-region (nntp-find-connection nntp-server-buffer)
519 (point-min) (point-max)))
521 nil nntp-address nntp-port-number nntp-server-buffer
522 wait-for nnheader-callback-function))
526 ;;; Interface functions.
528 (nnoo-define-basics nntp)
530 (defsubst nntp-next-result-arrived-p ()
532 ;; A result that starts with a 2xx code is terminated by
533 ;; a line with only a "." on it.
534 ((eq (char-after) ?2)
535 (if (re-search-forward "\n\\.\r?\n" nil t)
538 ;; A result that starts with a 3xx or 4xx code is terminated
541 (if (search-forward "\n" nil t)
549 (defvar nntp-with-open-group-internal nil)
550 (defvar nntp-report-n nil))
552 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
553 "Protect against servers that don't like clients that keep idle connections opens.
554 The problem being that these servers may either close a connection or
555 simply ignore any further requests on a connection. Closed
556 connections are not detected until accept-process-output has updated
557 the process-status. Dropped connections are not detected until the
558 connection timeouts (which may be several minutes) or
559 nntp-connection-timeout has expired. When these occur
560 nntp-with-open-group, opens a new connection then re-issues the NNTP
561 command whose response triggered the error."
562 (when (and (listp connectionless)
563 (not (eq connectionless nil)))
564 (setq forms (cons connectionless forms)
566 `(letf ((nntp-report-n (symbol-function 'nntp-report))
567 ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1))
568 (nntp-with-open-group-internal nil))
569 (while (catch 'nntp-with-open-group-error
570 ;; Open the connection to the server
571 ;; NOTE: Existing connections are NOT tested.
572 (nntp-possibly-change-group ,group ,server ,connectionless)
575 (and nntp-connection-timeout
576 (nnheader-run-at-time
577 nntp-connection-timeout nil
579 (let ((process (nntp-find-connection
582 (process-buffer process))))
583 ;; When I an able to identify the
584 ;; connection to the server AND I've
585 ;; received NO reponse for
586 ;; nntp-connection-timeout seconds.
587 (when (and buffer (eq 0 (buffer-size buffer)))
588 ;; Close the connection. Take no
589 ;; other action as the accept input
590 ;; code will handle the closed
592 (nntp-kill-buffer buffer))))))))
594 (setq nntp-with-open-group-internal
599 (signal 'quit nil))))
601 (nnheader-cancel-timer timer)))
603 (setf (symbol-function 'nntp-report) nntp-report-n))
604 nntp-with-open-group-internal))
606 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
607 "Retrieve the headers of ARTICLES."
608 (nntp-with-open-group
611 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
613 (if (and (not gnus-nov-is-evil)
614 (not nntp-nov-is-evil)
615 (nntp-retrieve-headers-with-xover articles fetch-old))
616 ;; We successfully retrieved the headers via XOVER.
618 ;; XOVER didn't work, so we do it the hard, slow and inefficient
620 (let ((number (length articles))
624 (last-point (point-min))
625 (buf (nntp-find-connection-buffer nntp-server-buffer))
626 (nntp-inhibit-erase t)
628 ;; Send HEAD commands.
629 (while (setq article (pop articles))
632 "HEAD" (if (numberp article)
633 (int-to-string article)
634 ;; `articles' is either a list of article numbers
635 ;; or a list of article IDs.
638 ;; Every 400 requests we have to read the stream in
639 ;; order to avoid deadlocks.
640 (when (or (null articles) ;All requests have been sent.
641 (zerop (% count nntp-maximum-request)))
642 (nntp-accept-response)
645 (goto-char last-point)
647 (while (nntp-next-result-arrived-p)
648 (setq last-point (point))
651 ;; If number of headers is greater than 100, give
652 ;; informative messages.
653 (and (numberp nntp-large-newsgroup)
654 (> number nntp-large-newsgroup)
655 (zerop (% received 20))
656 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
657 (/ (* received 100) number)))
658 (nntp-accept-response))))
659 (and (numberp nntp-large-newsgroup)
660 (> number nntp-large-newsgroup)
661 (nnheader-message 6 "NNTP: Receiving headers...done"))
663 ;; Now all of replies are received. Fold continuation lines.
664 (nnheader-fold-continuation-lines)
665 ;; Remove all "\r"'s.
667 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
670 (deffoo nntp-retrieve-groups (groups &optional server)
671 "Retrieve group info on GROUPS."
672 (nntp-with-open-group
674 (when (nntp-find-connection-buffer nntp-server-buffer)
677 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
678 (set-buffer nntp-server-buffer)
680 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
681 ;; The first time this is run, this variable is `try'. So we
683 (when (eq nntp-server-list-active-group 'try)
684 (nntp-try-list-active (car groups)))
689 (last-point (point-min))
690 (nntp-inhibit-erase t)
691 (buf (nntp-find-connection-buffer nntp-server-buffer))
692 (command (if nntp-server-list-active-group
693 "LIST ACTIVE" "GROUP")))
695 ;; Timeout may have killed the buffer.
696 (unless (gnus-buffer-live-p buf)
697 (nnheader-report 'nntp "Connection to %s is closed." server)
699 ;; Send the command to the server.
700 (nntp-send-command nil command (pop groups))
702 ;; Every 400 requests we have to read the stream in
703 ;; order to avoid deadlocks.
704 (when (or (null groups) ;All requests have been sent.
705 (zerop (% count nntp-maximum-request)))
706 (nntp-accept-response)
707 (while (and (gnus-buffer-live-p buf)
709 ;; Search `blue moon' in this file for the
710 ;; reason why set-buffer here.
712 (goto-char last-point)
714 (while (re-search-forward "^[0-9]" nil t)
716 (setq last-point (point))
718 (nntp-accept-response))))
720 ;; Wait for the reply from the final command.
721 (unless (gnus-buffer-live-p buf)
722 (nnheader-report 'nntp "Connection to %s is closed." server)
725 (goto-char (point-max))
726 (re-search-backward "^[0-9]" nil t)
727 (when (looking-at "^[23]")
728 (while (and (gnus-buffer-live-p buf)
731 (goto-char (point-max))
732 (if (not nntp-server-list-active-group)
733 (not (re-search-backward "\r?\n"
735 (not (re-search-backward "^\\.\r?\n"
737 (nntp-accept-response)))
739 ;; Now all replies are received. We remove CRs.
740 (unless (gnus-buffer-live-p buf)
741 (nnheader-report 'nntp "Connection to %s is closed." server)
744 (goto-char (point-min))
745 (while (search-forward "\r" nil t)
746 (replace-match "" t t))
748 (if (not nntp-server-list-active-group)
750 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
752 ;; We have read active entries, so we just delete the
754 (goto-char (point-min))
755 (while (re-search-forward "^[.2-5]" nil t)
756 (delete-region (match-beginning 0)
757 (progn (forward-line 1) (point))))
758 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
761 (deffoo nntp-retrieve-articles (articles &optional group server)
762 (nntp-with-open-group
765 (let ((number (length articles))
769 (last-point (point-min))
770 (buf (nntp-find-connection-buffer nntp-server-buffer))
771 (nntp-inhibit-erase t)
772 (map (apply 'vector articles))
777 ;; Send ARTICLE command.
778 (while (setq article (pop articles))
781 "ARTICLE" (if (numberp article)
782 (int-to-string article)
783 ;; `articles' is either a list of article numbers
784 ;; or a list of article IDs.
787 ;; Every 400 requests we have to read the stream in
788 ;; order to avoid deadlocks.
789 (when (or (null articles) ;All requests have been sent.
790 (zerop (% count nntp-maximum-request)))
791 (nntp-accept-response)
794 (goto-char last-point)
796 (while (nntp-next-result-arrived-p)
797 (aset map received (cons (aref map received) (point)))
798 (setq last-point (point))
801 ;; If number of headers is greater than 100, give
802 ;; informative messages.
803 (and (numberp nntp-large-newsgroup)
804 (> number nntp-large-newsgroup)
805 (zerop (% received 20))
806 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
807 (/ (* received 100) number)))
808 (nntp-accept-response))))
809 (and (numberp nntp-large-newsgroup)
810 (> number nntp-large-newsgroup)
811 (nnheader-message 6 "NNTP: Receiving articles...done"))
813 ;; Now we have all the responses. We go through the results,
814 ;; wash it and copy it over to the server buffer.
815 (set-buffer nntp-server-buffer)
817 (setq last-point (point-min))
821 (setq point (goto-char (point-max)))
823 (insert-buffer-substring buf last-point (cdr entry))
825 (setq last-point (cdr entry))
828 (cons (car entry) point))
831 (defun nntp-try-list-active (group)
832 (nntp-list-active-group group)
834 (set-buffer nntp-server-buffer)
835 (goto-char (point-min))
837 (looking-at "5[0-9]+"))
838 (setq nntp-server-list-active-group nil))
840 (setq nntp-server-list-active-group t)))))
842 (deffoo nntp-list-active-group (group &optional server)
843 "Return the active info on GROUP (which can be a regexp)."
844 (nntp-with-open-group
846 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
848 (deffoo nntp-request-group-articles (group &optional server)
849 "Return the list of existing articles in GROUP."
850 (nntp-with-open-group
852 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
854 (deffoo nntp-request-article (article &optional group server buffer command)
855 (nntp-with-open-group
857 (when (nntp-send-command-and-decode
858 "\r?\n\\.\r?\n" "ARTICLE"
859 (if (numberp article) (int-to-string article) article))
861 (not (equal buffer nntp-server-buffer)))
863 (set-buffer nntp-server-buffer)
864 (copy-to-buffer buffer (point-min) (point-max))
865 (nntp-find-group-and-number group))
866 (nntp-find-group-and-number group)))))
868 (deffoo nntp-request-head (article &optional group server)
869 (nntp-with-open-group
871 (when (nntp-send-command
872 "\r?\n\\.\r?\n" "HEAD"
873 (if (numberp article) (int-to-string article) article))
875 (nntp-find-group-and-number group)
876 (nntp-decode-text)))))
878 (deffoo nntp-request-body (article &optional group server)
879 (nntp-with-open-group
881 (nntp-send-command-and-decode
882 "\r?\n\\.\r?\n" "BODY"
883 (if (numberp article) (int-to-string article) article))))
885 (deffoo nntp-request-group (group &optional server dont-check)
886 (nntp-with-open-group
888 (when (nntp-send-command "^[245].*\n" "GROUP" group)
889 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
890 (setcar (cddr entry) group)))))
892 (deffoo nntp-close-group (group &optional server)
895 (deffoo nntp-server-opened (&optional server)
896 "Say whether a connection to SERVER has been opened."
897 (and (nnoo-current-server-p 'nntp server)
899 (gnus-buffer-live-p nntp-server-buffer)
900 (nntp-find-connection nntp-server-buffer)))
902 (deffoo nntp-open-server (server &optional defs connectionless)
903 (nnheader-init-server-buffer)
904 (if (nntp-server-opened server)
906 (when (or (stringp (car defs))
907 (numberp (car defs)))
908 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
909 (unless (assq 'nntp-address defs)
910 (setq defs (append defs (list (list 'nntp-address server)))))
911 (nnoo-change-server 'nntp server defs)
912 (unless connectionless
913 (or (nntp-find-connection nntp-server-buffer)
914 (nntp-open-connection nntp-server-buffer)))))
916 (deffoo nntp-close-server (&optional server)
917 (nntp-possibly-change-group nil server t)
918 (let ((process (nntp-find-connection nntp-server-buffer)))
920 (when (memq (process-status process) '(open run))
922 (nntp-send-string process "QUIT")
923 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
924 ;; Ok, this is evil, but when using telnet and stuff
925 ;; as the connection method, it's important that the
926 ;; QUIT command actually is sent out before we kill
929 (nntp-kill-buffer (process-buffer process))
930 (setq process (car (pop nntp-connection-alist))))
931 (nnoo-close-server 'nntp)))
933 (deffoo nntp-request-close ()
935 (while (setq process (pop nntp-connection-list))
936 (when (memq (process-status process) '(open run))
938 (nntp-send-string process "QUIT")
939 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
940 ;; Ok, this is evil, but when using telnet and stuff
941 ;; as the connection method, it's important that the
942 ;; QUIT command actually is sent out before we kill
945 (nntp-kill-buffer (process-buffer process)))))
947 (deffoo nntp-request-list (&optional server)
948 (nntp-with-open-group
950 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
952 (deffoo nntp-request-list-newsgroups (&optional server)
953 (nntp-with-open-group
955 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
957 (deffoo nntp-request-newgroups (date &optional server)
958 (nntp-with-open-group
961 (set-buffer nntp-server-buffer)
962 (let* ((time (date-to-time date))
963 (ls (- (cadr time) (nth 8 (decode-time time)))))
965 (setcar time (1- (car time)))
966 (setcar (cdr time) (+ ls 65536)))
968 (setcar time (1+ (car time)))
969 (setcar (cdr time) (- ls 65536)))
971 (setcar (cdr time) ls)))
974 "^\\.\r?\n" "NEWGROUPS"
975 (format-time-string "%y%m%d %H%M%S" time)
977 (nntp-decode-text))))))
979 (deffoo nntp-request-post (&optional server)
980 (nntp-with-open-group
982 (when (nntp-send-command "^[23].*\r?\n" "POST")
983 (let ((response (with-current-buffer nntp-server-buffer
984 nntp-process-response))
987 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
989 (setq server-id (match-string 1 response))
990 (narrow-to-region (goto-char (point-min))
991 (if (search-forward "\n\n" nil t)
994 (unless (mail-fetch-field "Message-ID")
995 (goto-char (point-min))
996 (insert "Message-ID: " server-id "\n"))
998 (run-hooks 'nntp-prepare-post-hook)
999 (nntp-send-buffer "^[23].*\n")))))
1001 (deffoo nntp-request-type (group article)
1004 (deffoo nntp-asynchronous-p ()
1007 ;;; Hooky functions.
1009 (defun nntp-send-mode-reader ()
1010 "Send the MODE READER command to the nntp server.
1011 This function is supposed to be called from `nntp-server-opened-hook'.
1012 It will make innd servers spawn an nnrpd process to allow actual article
1014 (nntp-send-command "^.*\n" "MODE READER"))
1016 (defun nntp-send-authinfo (&optional send-if-force)
1017 "Send the AUTHINFO to the nntp server.
1018 It will look in the \"~/.authinfo\" file for matching entries. If
1019 nothing suitable is found there, it will prompt for a user name
1022 If SEND-IF-FORCE, only send authinfo to the server if the
1023 .authinfo file has the FORCE token."
1024 (let* ((list (gnus-parse-netrc nntp-authinfo-file))
1025 (alist (gnus-netrc-machine list nntp-address "nntp"))
1026 (force (gnus-netrc-get alist "force"))
1027 (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
1028 (passwd (gnus-netrc-get alist "password")))
1029 (when (or (not send-if-force)
1032 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1033 nntp-authinfo-user user))
1034 (unless (member user '(nil ""))
1035 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1036 (when t ;???Should check if AUTHINFO succeeded
1038 "^2.*\r?\n" "AUTHINFO PASS"
1040 nntp-authinfo-password
1041 (setq nntp-authinfo-password
1042 (read-passwd (format "NNTP (%s@%s) password: "
1043 user nntp-address))))))))))
1045 (defun nntp-send-nosy-authinfo ()
1046 "Send the AUTHINFO to the nntp server."
1047 (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
1048 (unless (member user '(nil ""))
1049 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1050 (when t ;???Should check if AUTHINFO succeeded
1051 (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
1052 (read-passwd (format "NNTP (%s@%s) password: "
1053 user nntp-address)))))))
1055 (defun nntp-send-authinfo-from-file ()
1056 "Send the AUTHINFO to the nntp server.
1058 The authinfo login name is taken from the user's login name and the
1059 password contained in '~/.nntp-authinfo'."
1060 (when (file-exists-p "~/.nntp-authinfo")
1062 (insert-file-contents "~/.nntp-authinfo")
1063 (goto-char (point-min))
1064 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
1066 "^2.*\r?\n" "AUTHINFO PASS"
1067 (buffer-substring (point) (gnus-point-at-eol))))))
1069 ;;; Internal functions.
1071 (defun nntp-handle-authinfo (process)
1072 "Take care of an authinfo response from the server."
1073 (let ((last nntp-last-command))
1074 (funcall nntp-authinfo-function)
1075 ;; We have to re-send the function that was interrupted by
1076 ;; the authinfo request.
1078 (set-buffer nntp-server-buffer)
1080 (nntp-send-string process last)))
1082 (defun nntp-make-process-buffer (buffer)
1083 "Create a new, fresh buffer usable for nntp process connections."
1086 (generate-new-buffer
1087 (format " *server %s %s %s*"
1088 nntp-address nntp-port-number
1089 (gnus-buffer-exists-p buffer))))
1090 (mm-enable-multibyte)
1091 (set (make-local-variable 'after-change-functions) nil)
1092 (set (make-local-variable 'nntp-process-wait-for) nil)
1093 (set (make-local-variable 'nntp-process-callback) nil)
1094 (set (make-local-variable 'nntp-process-to-buffer) nil)
1095 (set (make-local-variable 'nntp-process-start-point) nil)
1096 (set (make-local-variable 'nntp-process-decode) nil)
1099 (defun nntp-open-connection (buffer)
1100 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
1101 (run-hooks 'nntp-prepare-server-hook)
1102 (let* ((pbuffer (nntp-make-process-buffer buffer))
1104 (and nntp-connection-timeout
1105 (nnheader-run-at-time
1106 nntp-connection-timeout nil
1108 (nntp-kill-buffer ,pbuffer)))))
1111 (let ((coding-system-for-read nntp-coding-system-for-read)
1112 (coding-system-for-write nntp-coding-system-for-write))
1113 (funcall nntp-open-connection-function pbuffer))
1116 (message "Quit opening connection")
1117 (nntp-kill-buffer pbuffer)
1121 (nnheader-cancel-timer timer))
1123 (nntp-kill-buffer pbuffer))
1124 (when (and (buffer-name pbuffer)
1126 (process-kill-without-query process)
1127 (if (and (nntp-wait-for process "^2.*\n" buffer nil t)
1128 (memq (process-status process) '(open run)))
1130 (caar (push (list process buffer nil) nntp-connection-alist))
1131 (push process nntp-connection-list)
1133 (set-buffer pbuffer)
1134 (nntp-read-server-type)
1136 (set-buffer nntp-server-buffer)
1137 (let ((nnheader-callback-function nil))
1138 (run-hooks 'nntp-server-opened-hook)
1139 (nntp-send-authinfo t))))
1140 (nntp-kill-buffer (process-buffer process))
1143 (defun nntp-open-network-stream (buffer)
1144 (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
1146 (defun nntp-open-ssl-stream (buffer)
1147 (let* ((process-connection-type nil)
1148 (proc (start-process "nntpd" buffer
1150 shell-command-switch
1151 (format-spec nntp-ssl-program
1154 ?p nntp-port-number)))))
1155 (process-kill-without-query proc)
1158 (let ((nntp-connection-alist (list proc buffer nil)))
1159 (nntp-wait-for-string "^\r*20[01]"))
1161 (delete-region (point-min) (point))
1164 (defun nntp-open-tls-stream (buffer)
1165 (let ((proc (open-tls-stream "nntpd" buffer nntp-address nntp-port-number)))
1166 (process-kill-without-query proc)
1169 (let ((nntp-connection-alist (list proc buffer nil)))
1170 (nntp-wait-for-string "^\r*20[01]"))
1172 (delete-region (point-min) (point))
1175 (defun nntp-read-server-type ()
1176 "Find out what the name of the server we have connected to is."
1177 ;; Wait for the status string to arrive.
1178 (setq nntp-server-type (buffer-string))
1179 (let ((alist nntp-server-action-alist)
1180 (case-fold-search t)
1182 ;; Run server-specific commands.
1184 (setq entry (pop alist))
1185 (when (string-match (car entry) nntp-server-type)
1186 (if (and (listp (cadr entry))
1187 (not (eq 'lambda (caadr entry))))
1189 (funcall (cadr entry)))))))
1191 (defun nntp-async-wait (process wait-for buffer decode callback)
1193 (set-buffer (process-buffer process))
1194 (unless nntp-inside-change-function
1196 (setq nntp-process-wait-for wait-for
1197 nntp-process-to-buffer buffer
1198 nntp-process-decode decode
1199 nntp-process-callback callback
1200 nntp-process-start-point (point-max))
1201 (setq after-change-functions '(nntp-after-change-function))
1202 (if nntp-async-needs-kluge
1203 (nntp-async-kluge process))))
1205 (defun nntp-async-kluge (process)
1206 ;; emacs 20.3 bug: process output with encoding 'binary
1207 ;; doesn't trigger after-change-functions.
1208 (unless nntp-async-timer
1209 (setq nntp-async-timer
1210 (nnheader-run-at-time 1 1 'nntp-async-timer-handler)))
1211 (add-to-list 'nntp-async-process-list process))
1213 (defun nntp-async-timer-handler ()
1216 (if (memq (process-status proc) '(open run))
1217 (nntp-async-trigger proc)
1218 (nntp-async-stop proc)))
1219 nntp-async-process-list))
1221 (defun nntp-async-stop (proc)
1222 (setq nntp-async-process-list (delq proc nntp-async-process-list))
1223 (when (and nntp-async-timer (not nntp-async-process-list))
1224 (nnheader-cancel-timer nntp-async-timer)
1225 (setq nntp-async-timer nil)))
1227 (defun nntp-after-change-function (beg end len)
1229 ;; we only care about insertions at eob
1230 (when (and (eq 0 len) (eq (point-max) end))
1232 (let ((proc (get-buffer-process (current-buffer))))
1234 (nntp-async-trigger proc)))))
1235 ;; any throw from after-change-functions will leave it
1236 ;; set to nil. so we reset it here, if necessary.
1238 (setq after-change-functions '(nntp-after-change-function)))))
1240 (defun nntp-async-trigger (process)
1242 (set-buffer (process-buffer process))
1243 (when nntp-process-callback
1244 ;; do we have an error message?
1245 (goto-char nntp-process-start-point)
1246 (if (memq (following-char) '(?4 ?5))
1247 ;; wants credentials?
1248 (if (looking-at "480")
1249 (nntp-handle-authinfo process)
1250 ;; report error message.
1251 (nntp-snarf-error-message)
1252 (nntp-do-callback nil))
1254 ;; got what we expect?
1255 (goto-char (point-max))
1256 (when (re-search-backward
1257 nntp-process-wait-for nntp-process-start-point t)
1258 (let ((response (match-string 0)))
1259 (with-current-buffer nntp-server-buffer
1260 (setq nntp-process-response response)))
1261 (nntp-async-stop process)
1263 (when (gnus-buffer-exists-p nntp-process-to-buffer)
1264 (let ((buf (current-buffer))
1265 (start nntp-process-start-point)
1266 (decode nntp-process-decode))
1268 (set-buffer nntp-process-to-buffer)
1269 (goto-char (point-max))
1271 (narrow-to-region (point) (point))
1272 (insert-buffer-substring buf start)
1274 (nntp-decode-text))))))
1276 (goto-char (point-max))
1278 (buffer-name (get-buffer nntp-process-to-buffer))))))))
1280 (defun nntp-do-callback (arg)
1281 (let ((callback nntp-process-callback)
1282 (nntp-inside-change-function t))
1283 (setq nntp-process-callback nil)
1284 (funcall callback arg)))
1286 (defun nntp-snarf-error-message ()
1287 "Save the error message in the current buffer."
1288 (let ((message (buffer-string)))
1289 (while (string-match "[\r\n]+" message)
1290 (setq message (replace-match " " t t message)))
1291 (nnheader-report 'nntp message)
1294 (defun nntp-accept-process-output (process)
1295 "Wait for output from PROCESS and message some dots."
1297 (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1298 nntp-server-buffer))
1299 (let ((len (/ (buffer-size) 1024))
1302 (setq nntp-have-messaged t)
1303 (nnheader-message 7 "nntp read: %dk" len)))
1304 (nnheader-accept-process-output process)
1305 ;; accept-process-output may update status of process to indicate
1306 ;; that the server has closed the connection. This MUST be
1307 ;; handled here as the buffer restored by the save-excursion may
1308 ;; be the process's former output buffer (i.e. now killed)
1310 (memq (process-status process) '(open run)))
1311 (nntp-report "Server closed connection"))))
1313 (defun nntp-accept-response ()
1314 "Wait for output from the process that outputs to BUFFER."
1315 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1317 (defun nntp-possibly-change-group (group server &optional connectionless)
1318 (let ((nnheader-callback-function nil))
1320 (or (nntp-server-opened server)
1321 (nntp-open-server server nil connectionless)))
1323 (unless connectionless
1324 (or (nntp-find-connection nntp-server-buffer)
1325 (nntp-open-connection nntp-server-buffer))))
1328 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1330 (nntp-report "Server closed connection"))
1331 ((not (equal group (caddr entry)))
1333 (set-buffer (process-buffer (car entry)))
1335 (nntp-send-command "^[245].*\n" "GROUP" group)
1336 (setcar (cddr entry) group)
1339 (set-buffer nntp-server-buffer)
1340 (erase-buffer))))))))
1342 (defun nntp-decode-text (&optional cr-only)
1343 "Decode the text in the current buffer."
1344 (goto-char (point-min))
1345 (while (search-forward "\r" nil t)
1348 ;; Remove trailing ".\n" end-of-transfer marker.
1349 (goto-char (point-max))
1351 (when (looking-at ".\n")
1353 ;; Delete status line.
1354 (goto-char (point-min))
1355 (while (looking-at "[1-5][0-9][0-9] .*\n")
1356 ;; For some unknown reason, there is more than one status line.
1357 (delete-region (point) (progn (forward-line 1) (point))))
1358 ;; Remove "." -> ".." encoding.
1359 (while (search-forward "\n.." nil t)
1362 (defun nntp-encode-text ()
1363 "Encode the text in the current buffer."
1365 ;; Replace "." at beginning of line with "..".
1366 (goto-char (point-min))
1367 (while (re-search-forward "^\\." nil t)
1369 (goto-char (point-max))
1370 ;; Insert newline at the end of the buffer.
1373 ;; Insert `.' at end of buffer (end of text mark).
1374 (goto-char (point-max))
1376 (goto-char (point-min))
1380 (insert nntp-end-of-line))))
1382 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1383 (set-buffer nntp-server-buffer)
1387 ;; This server does not talk NOV.
1388 ((not nntp-server-xover)
1391 ;; We don't care about gaps.
1392 ((or (not nntp-nov-gap)
1394 (nntp-send-xover-command
1396 (if (numberp fetch-old)
1397 (max 1 (- (car articles) fetch-old))
1400 (car (last articles)) 'wait)
1402 (goto-char (point-min))
1403 (when (looking-at "[1-5][0-9][0-9] .*\n")
1404 (delete-region (point) (progn (forward-line 1) (point))))
1405 (while (search-forward "\r" nil t)
1406 (replace-match "" t t))
1407 (goto-char (point-max))
1409 (when (looking-at "\\.")
1410 (delete-region (point) (progn (forward-line 1) (point)))))
1412 ;; We do it the hard way. For each gap, an XOVER command is sent
1413 ;; to the server. We do not wait for a reply from the server, we
1414 ;; just send them off as fast as we can. That means that we have
1415 ;; to count the number of responses we get back to find out when we
1416 ;; have gotten all we asked for.
1417 ((numberp nntp-nov-gap)
1422 (buf nntp-server-buffer)
1423 (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1426 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1427 ;; that means that the server does not understand XOVER, but we
1428 ;; won't know that until we try.
1429 (while (and nntp-server-xover articles)
1430 (setq first (car articles))
1431 ;; Search forward until we find a gap, or until we run out of
1433 (while (and (cdr articles)
1434 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1435 (setq articles (cdr articles)))
1437 (setq in-process-buffer-p (stringp nntp-server-xover))
1438 (nntp-send-xover-command first (setq last (car articles)))
1439 (setq articles (cdr articles))
1441 (when (and nntp-server-xover in-process-buffer-p)
1442 ;; Don't count tried request.
1443 (setq count (1+ count))
1445 ;; Every 400 requests we have to read the stream in
1446 ;; order to avoid deadlocks.
1447 (when (or (null articles) ;All requests have been sent.
1448 (= 1 (% count nntp-maximum-request)))
1450 (nntp-accept-response)
1451 ;; On some Emacs versions the preceding function has a
1452 ;; tendency to change the buffer. Perhaps. It's quite
1453 ;; difficult to reproduce, because it only seems to happen
1454 ;; once in a blue moon.
1455 (set-buffer process-buffer)
1457 (goto-char (or last-point (point-min)))
1459 (while (re-search-forward "^[0-9][0-9][0-9] .*\n" nil t)
1461 (setq last-point (point))
1462 (or (< received count)
1463 ;; I haven't started reading the final response
1465 (goto-char (point-max))
1467 (not (looking-at "^\\.\r?\n")))))
1468 ;; I haven't read the end of the final response
1469 (nntp-accept-response)
1470 (set-buffer process-buffer))))
1472 ;; Some nntp servers seem to have an extension to the XOVER
1473 ;; extension. On these servers, requesting an article range
1474 ;; preceeding the active range does not return an error as
1475 ;; specified in the RFC. What we instead get is the NOV entry
1476 ;; for the first available article. Obviously, a client can
1477 ;; use that entry to avoid making unnecessary requests. The
1478 ;; only problem is for a client that assumes that the response
1479 ;; will always be within the requested ranage. For such a
1480 ;; client, we can get N copies of the same entry (one for each
1481 ;; XOVER command sent to the server).
1484 (goto-char (point-min))
1485 (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1486 (let ((low-limit (string-to-int
1487 (buffer-substring (match-beginning 1)
1489 (while (and articles (<= (car articles) low-limit))
1490 (setq articles (cdr articles))))))
1493 (when nntp-server-xover
1494 (when in-process-buffer-p
1496 (goto-char (point-max))
1497 (insert-buffer-substring process-buffer)
1498 (set-buffer process-buffer)
1502 ;; We remove any "." lines and status lines.
1503 (goto-char (point-min))
1504 (while (search-forward "\r" nil t)
1506 (goto-char (point-min))
1507 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1512 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1513 "Send the XOVER command to the server."
1514 (let ((range (format "%d-%d" beg end))
1515 (nntp-inhibit-erase t))
1516 (if (stringp nntp-server-xover)
1517 ;; If `nntp-server-xover' is a string, then we just send this
1520 (nntp-send-command-nodelete
1521 "\r?\n\\.\r?\n" nntp-server-xover range)
1522 ;; We do not wait for the reply.
1523 (nntp-send-command-nodelete nil nntp-server-xover range))
1524 (let ((commands nntp-xover-commands))
1525 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1526 ;; We try them all until we get at positive response.
1527 (while (and commands (eq nntp-server-xover 'try))
1528 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1530 (set-buffer nntp-server-buffer)
1531 (goto-char (point-min))
1532 (and (looking-at "[23]") ; No error message.
1533 ;; We also have to look at the lines. Some buggy
1534 ;; servers give back simple lines with just the
1535 ;; article number. How... helpful.
1538 (looking-at "[0-9]+\t...")) ; More text after number.
1539 (setq nntp-server-xover (car commands))))
1540 (setq commands (cdr commands)))
1541 ;; If none of the commands worked, we disable XOVER.
1542 (when (eq nntp-server-xover 'try)
1544 (set-buffer nntp-server-buffer)
1546 (setq nntp-server-xover nil)))
1547 nntp-server-xover))))
1549 (defun nntp-find-group-and-number (&optional group)
1552 (set-buffer nntp-server-buffer)
1553 (narrow-to-region (goto-char (point-min))
1554 (or (search-forward "\n\n" nil t) (point-max)))
1555 (goto-char (point-min))
1556 ;; We first find the number by looking at the status line.
1557 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1559 (buffer-substring (match-beginning 1)
1562 (and number (zerop number) (setq number nil))
1564 ;; Then we find the group name.
1567 ;; If there is only one group in the Newsgroups
1568 ;; header, then it seems quite likely that this
1569 ;; article comes from that group, I'd say.
1570 ((and (setq newsgroups
1571 (mail-fetch-field "newsgroups"))
1572 (not (string-match "," newsgroups)))
1574 ;; If there is more than one group in the
1575 ;; Newsgroups header, then the Xref header should
1576 ;; be filled out. We hazard a guess that the group
1577 ;; that has this article number in the Xref header
1578 ;; is the one we are looking for. This might very
1579 ;; well be wrong if this article happens to have
1580 ;; the same number in several groups, but that's
1582 ((and (setq xref (mail-fetch-field "xref"))
1585 (format "\\([^ :]+\\):%d" number) xref))
1586 (match-string 1 xref))
1589 ((and (setq xref (mail-fetch-field "xref"))
1592 (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1593 "\\([^ :]+\\):\\([0-9]+\\)")
1595 (setq group (match-string 1 xref)
1596 number (string-to-int (match-string 2 xref))))
1597 ((and (setq newsgroups
1598 (mail-fetch-field "newsgroups"))
1599 (not (string-match "," newsgroups)))
1600 (setq group newsgroups))
1602 (t (setq group ""))))
1603 (when (string-match "\r" group)
1604 (setq group (substring group 0 (match-beginning 0))))
1605 (cons group number)))))
1607 (defun nntp-wait-for-string (regexp)
1608 "Wait until string arrives in the buffer."
1609 (let ((buf (current-buffer))
1611 (goto-char (point-min))
1612 (while (and (setq proc (get-buffer-process buf))
1613 (memq (process-status proc) '(open run))
1614 (not (re-search-forward regexp nil t)))
1615 (accept-process-output proc)
1617 (goto-char (point-min)))))
1620 ;; ==========================================================================
1621 ;; Obsolete nntp-open-* connection methods -- drv
1622 ;; ==========================================================================
1624 (defvoo nntp-open-telnet-envuser nil
1625 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1627 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
1628 "*Regular expression to match the shell prompt on the remote machine.")
1630 (defvoo nntp-rlogin-program "rsh"
1631 "*Program used to log in on remote machines.
1632 The default is \"rsh\", but \"ssh\" is a popular alternative.")
1634 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1635 "*Parameters to `nntp-open-rlogin'.
1636 That function may be used as `nntp-open-connection-function'. In that
1637 case, this list will be used as the parameter list given to rsh.")
1639 (defvoo nntp-rlogin-user-name nil
1640 "*User name on remote system when using the rlogin connect method.")
1642 (defvoo nntp-telnet-parameters
1643 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1644 "*Parameters to `nntp-open-telnet'.
1645 That function may be used as `nntp-open-connection-function'. In that
1646 case, this list will be executed as a command after logging in
1649 (defvoo nntp-telnet-user-name nil
1650 "User name to log in via telnet with.")
1652 (defvoo nntp-telnet-passwd nil
1653 "Password to use to log in via telnet with.")
1655 (defun nntp-open-telnet (buffer)
1661 "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1662 (case-fold-search t))
1663 (when (memq (process-status proc) '(open run))
1664 (nntp-wait-for-string "^r?telnet")
1665 (process-send-string proc "set escape \^X\n")
1667 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1668 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1669 nntp-address "\n")))
1671 (process-send-string proc (concat "open " nntp-address "\n"))))
1673 ((not nntp-open-telnet-envuser)
1674 (nntp-wait-for-string "^\r*.?login:")
1675 (process-send-string
1677 (or nntp-telnet-user-name
1678 (setq nntp-telnet-user-name (read-string "login: ")))
1680 (nntp-wait-for-string "^\r*.?password:")
1681 (process-send-string
1683 (or nntp-telnet-passwd
1684 (setq nntp-telnet-passwd
1685 (read-passwd "Password: ")))
1687 (nntp-wait-for-string nntp-telnet-shell-prompt)
1688 (process-send-string
1689 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1690 (nntp-wait-for-string "^\r*20[01]")
1692 (delete-region (point-min) (point))
1693 (process-send-string proc "\^]")
1694 (nntp-wait-for-string "^r?telnet")
1695 (process-send-string proc "mode character\n")
1696 (accept-process-output proc 1)
1698 (goto-char (point-min))
1700 (delete-region (point) (point-max)))
1703 (defun nntp-open-rlogin (buffer)
1704 "Open a connection to SERVER using rsh."
1705 (let ((proc (if nntp-rlogin-user-name
1706 (apply 'start-process
1707 "nntpd" buffer nntp-rlogin-program
1708 nntp-address "-l" nntp-rlogin-user-name
1709 nntp-rlogin-parameters)
1710 (apply 'start-process
1711 "nntpd" buffer nntp-rlogin-program nntp-address
1712 nntp-rlogin-parameters))))
1715 (nntp-wait-for-string "^\r*20[01]")
1717 (delete-region (point-min) (point))
1721 ;; ==========================================================================
1722 ;; Replacements for the nntp-open-* functions -- drv
1723 ;; ==========================================================================
1725 (defun nntp-open-telnet-stream (buffer)
1726 "Open a nntp connection by telnet'ing the news server.
1728 Please refer to the following variables to customize the connection:
1729 - `nntp-pre-command',
1730 - `nntp-telnet-command',
1731 - `nntp-telnet-switches',
1733 - `nntp-port-number',
1734 - `nntp-end-of-line'."
1735 (let ((command `(,nntp-telnet-command
1736 ,@nntp-telnet-switches
1737 ,nntp-address ,nntp-port-number))
1739 (and nntp-pre-command
1740 (push nntp-pre-command command))
1741 (setq proc (apply 'start-process "nntpd" buffer command))
1744 (nntp-wait-for-string "^\r*20[01]")
1746 (delete-region (point-min) (point))
1749 (defun nntp-open-via-rlogin-and-telnet (buffer)
1750 "Open a connection to an nntp server through an intermediate host.
1751 First rlogin to the remote host, and then telnet the real news server
1754 Please refer to the following variables to customize the connection:
1755 - `nntp-pre-command',
1756 - `nntp-via-rlogin-command',
1757 - `nntp-via-rlogin-command-switches',
1758 - `nntp-via-user-name',
1759 - `nntp-via-address',
1760 - `nntp-telnet-command',
1761 - `nntp-telnet-switches',
1763 - `nntp-port-number',
1764 - `nntp-end-of-line'."
1765 (let ((command `(,nntp-via-address
1766 ,nntp-telnet-command
1767 ,@nntp-telnet-switches))
1769 (when nntp-via-user-name
1770 (setq command `("-l" ,nntp-via-user-name ,@command)))
1771 (when nntp-via-rlogin-command-switches
1772 (setq command (append nntp-via-rlogin-command-switches command)))
1773 (push nntp-via-rlogin-command command)
1774 (and nntp-pre-command
1775 (push nntp-pre-command command))
1776 (setq proc (apply 'start-process "nntpd" buffer command))
1779 (nntp-wait-for-string "^r?telnet")
1780 (process-send-string proc (concat "open " nntp-address
1781 " " nntp-port-number "\n"))
1782 (nntp-wait-for-string "^\r*20[01]")
1784 (delete-region (point-min) (point))
1785 (process-send-string proc "\^]")
1786 (nntp-wait-for-string "^r?telnet")
1787 (process-send-string proc "mode character\n")
1788 (accept-process-output proc 1)
1790 (goto-char (point-min))
1792 (delete-region (point) (point-max)))
1795 (defun nntp-open-via-telnet-and-telnet (buffer)
1796 "Open a connection to an nntp server through an intermediate host.
1797 First telnet the remote host, and then telnet the real news server
1800 Please refer to the following variables to customize the connection:
1801 - `nntp-pre-command',
1802 - `nntp-via-telnet-command',
1803 - `nntp-via-telnet-switches',
1804 - `nntp-via-address',
1805 - `nntp-via-envuser',
1806 - `nntp-via-user-name',
1807 - `nntp-via-user-password',
1808 - `nntp-via-shell-prompt',
1809 - `nntp-telnet-command',
1810 - `nntp-telnet-switches',
1812 - `nntp-port-number',
1813 - `nntp-end-of-line'."
1817 (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
1818 (case-fold-search t)
1820 (and nntp-pre-command (push nntp-pre-command command))
1821 (setq proc (apply 'start-process "nntpd" buffer command))
1822 (when (memq (process-status proc) '(open run))
1823 (nntp-wait-for-string "^r?telnet")
1824 (process-send-string proc "set escape \^X\n")
1826 ((and nntp-via-envuser nntp-via-user-name)
1827 (process-send-string proc (concat "open " "-l" nntp-via-user-name
1828 nntp-via-address "\n")))
1830 (process-send-string proc (concat "open " nntp-via-address
1832 (when (not nntp-via-envuser)
1833 (nntp-wait-for-string "^\r*.?login:")
1834 (process-send-string proc
1836 (or nntp-via-user-name
1837 (setq nntp-via-user-name
1838 (read-string "login: ")))
1840 (nntp-wait-for-string "^\r*.?password:")
1841 (process-send-string proc
1843 (or nntp-via-user-password
1844 (setq nntp-via-user-password
1845 (read-passwd "Password: ")))
1847 (nntp-wait-for-string nntp-via-shell-prompt)
1848 (let ((real-telnet-command `("exec"
1849 ,nntp-telnet-command
1850 ,@nntp-telnet-switches
1852 ,nntp-port-number)))
1853 (process-send-string proc
1854 (concat (mapconcat 'identity
1855 real-telnet-command " ")
1857 (nntp-wait-for-string "^\r*20[01]")
1859 (delete-region (point-min) (point))
1860 (process-send-string proc "\^]")
1861 (nntp-wait-for-string "^r?telnet")
1862 (process-send-string proc "mode character\n")
1863 (accept-process-output proc 1)
1865 (goto-char (point-min))
1867 (delete-region (point) (point-max)))
1872 ;;; nntp.el ends here