1 ;;; nntp.el --- nntp access for Gnus
3 ;; Copyright (C) 1987, 1988, 1989, 1990, 1992, 1993, 1994, 1995, 1996,
4 ;; 1997, 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published
13 ;; by the Free Software Foundation; either version 2, or (at your
14 ;; option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
35 (eval-when-compile (require 'cl))
37 (defvoo nntp-address nil
38 "Address of the physical nntp server.")
40 (defvoo nntp-port-number "nntp"
41 "Port number on the physical nntp server.")
43 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
44 "*Hook used for sending commands to the server at startup.
45 The default value is `nntp-send-mode-reader', which makes an innd
46 server spawn an nnrpd server.")
48 (defvoo nntp-authinfo-function 'nntp-send-authinfo
49 "Function used to send AUTHINFO to the server.
50 It is called with no parameters.")
52 (defvoo nntp-server-action-alist
53 '(("nntpd 1\\.5\\.11t"
54 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
55 ("NNRP server Netscape"
56 (setq nntp-server-list-active-group nil)))
57 "Alist of regexps to match on server types and actions to be taken.
58 For instance, if you want Gnus to beep every time you connect
59 to innd, you could say something like:
61 \(setq nntp-server-action-alist
64 You probably don't want to do that, though.")
66 (defvoo nntp-open-connection-function 'nntp-open-network-stream
67 "*Function used for connecting to a remote system.
68 It will be called with the buffer to output in as argument.
70 Currently, five such functions are provided (please refer to their
71 respective doc string for more information), three of them establishing
72 direct connections to the nntp server, and two of them using an indirect
76 - `nntp-open-network-stream' (the default),
77 - `nntp-open-ssl-stream',
78 - `nntp-open-telnet-stream'.
81 - `nntp-open-via-rlogin-and-telnet',
82 - `nntp-open-via-telnet-and-telnet'.")
84 (defvoo nntp-pre-command nil
85 "*Pre-command to use with the various nntp-open-via-* methods.
86 This is where you would put \"runsocks\" or stuff like that.")
88 (defvoo nntp-telnet-command "telnet"
89 "*Telnet command used to connect to the nntp server.
90 This command is used by the various nntp-open-via-* methods.")
92 (defvoo nntp-telnet-switches '("-8")
93 "*Switches given to the telnet command `nntp-telnet-command'.")
95 (defvoo nntp-end-of-line "\r\n"
96 "*String to use on the end of lines when talking to the NNTP server.
97 This is \"\\r\\n\" by default, but should be \"\\n\" when
98 using and indirect connection method (nntp-open-via-*).")
100 (defvoo nntp-via-rlogin-command "rsh"
101 "*Rlogin command used to connect to an intermediate host.
102 This command is used by the `nntp-open-via-rlogin-and-telnet' method.
103 The default is \"rsh\", but \"ssh\" is a popular alternative.")
105 (defvoo nntp-via-rlogin-command-switches nil
106 "*Switches given to the rlogin command `nntp-via-rlogin-command'.
107 If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to
108 \(\"-C\") in order to compress all data connections, otherwise set this
109 to \(\"-t\") or (\"-C\" \"-t\") if the telnet command requires a pseudo-tty
110 allocation on an intermediate host.")
112 (defvoo nntp-via-telnet-command "telnet"
113 "*Telnet command used to connect to an intermediate host.
114 This command is used by the `nntp-open-via-telnet-and-telnet' method.")
116 (defvoo nntp-via-telnet-switches '("-8")
117 "*Switches given to the telnet command `nntp-via-telnet-command'.")
119 (defvoo nntp-via-user-name nil
120 "*User name to log in on an intermediate host with.
121 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
123 (defvoo nntp-via-user-password nil
124 "*Password to use to log in on an intermediate host with.
125 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
127 (defvoo nntp-via-address nil
128 "*Address of an intermediate host to connect to.
129 This variable is used by the `nntp-open-via-rlogin-and-telnet' and
130 `nntp-open-via-telnet-and-telnet' methods.")
132 (defvoo nntp-via-envuser nil
133 "*Whether both telnet client and server support the ENVIRON option.
134 If non-nil, there will be no prompt for a login name.")
136 (defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
137 "*Regular expression to match the shell prompt on an intermediate host.
138 This variable is used by the `nntp-open-via-telnet-and-telnet' method.")
140 (defvoo nntp-large-newsgroup 50
141 "*The number of the articles which indicates a large newsgroup.
142 If the number of the articles is greater than the value, verbose
143 messages will be shown to indicate the current status.")
145 (defvoo nntp-maximum-request 400
146 "*The maximum number of the requests sent to the NNTP server at one time.
147 If Emacs hangs up while retrieving headers, set the variable to a
150 (defvoo nntp-nov-is-evil nil
151 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
153 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
154 "*List of strings that are used as commands to fetch NOV lines from a server.
155 The strings are tried in turn until a positive response is gotten. If
156 none of the commands are successful, nntp will just grab headers one
159 (defvoo nntp-nov-gap 5
160 "*Maximum allowed gap between two articles.
161 If the gap between two consecutive articles is bigger than this
162 variable, split the XOVER request into two requests.")
164 (defvoo nntp-prepare-server-hook nil
165 "*Hook run before a server is opened.
166 If can be used to set up a server remotely, for instance. Say you
167 have an account at the machine \"other.machine\". This machine has
168 access to an NNTP server that you can't access locally. You could
169 then use this hook to rsh to the remote machine and start a proxy NNTP
170 server there that you can connect to. See also
171 `nntp-open-connection-function'")
173 (defvoo nntp-warn-about-losing-connection t
174 "*If non-nil, beep when a server closes connection.")
176 (defvoo nntp-coding-system-for-read 'binary
177 "*Coding system to read from NNTP.")
179 (defvoo nntp-coding-system-for-write 'binary
180 "*Coding system to write to NNTP.")
182 (defcustom nntp-authinfo-file "~/.authinfo"
183 ".netrc-like file that holds nntp authinfo passwords."
186 (repeat :tag "Entries"
189 :value ("" ("login" . "") ("password" . ""))
193 (const :format "" "login")
194 (string :format "Login: %v"))
196 (const :format "" "password")
197 (string :format "Password: %v")))))))
201 (defvoo nntp-connection-timeout nil
202 "*Number of seconds to wait before an nntp connection times out.
203 If this variable is nil, which is the default, no timers are set.
204 NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
206 (defvoo nntp-prepare-post-hook nil
207 "*Hook run just before posting an article. It is supposed to be used
208 to insert Cancel-Lock headers.")
210 ;;; Internal variables.
212 (defvar nntp-record-commands nil
213 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
215 (defvar nntp-have-messaged nil)
217 (defvar nntp-process-wait-for nil)
218 (defvar nntp-process-to-buffer nil)
219 (defvar nntp-process-callback nil)
220 (defvar nntp-process-decode nil)
221 (defvar nntp-process-start-point nil)
222 (defvar nntp-inside-change-function nil)
223 (defvoo nntp-last-command-time nil)
224 (defvoo nntp-last-command nil)
225 (defvoo nntp-authinfo-password nil)
226 (defvoo nntp-authinfo-user nil)
228 (defvar nntp-connection-list nil)
230 (defvoo nntp-server-type nil)
231 (defvoo nntp-connection-alist nil)
232 (defvoo nntp-status-string "")
233 (defconst nntp-version "nntp 5.0")
234 (defvoo nntp-inhibit-erase nil)
235 (defvoo nntp-inhibit-output nil)
237 (defvoo nntp-server-xover 'try)
238 (defvoo nntp-server-list-active-group 'try)
240 (defvar nntp-async-needs-kluge
241 (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
242 "*When non-nil, nntp will poll asynchronous connections
243 once a second. By default, this is turned on only for Emacs
244 20.3, which has a bug that breaks nntp's normal method of
245 noticing asynchronous data.")
247 (defvar nntp-async-timer nil)
248 (defvar nntp-async-process-list nil)
251 (autoload 'mail-source-read-passwd "mail-source")
252 (autoload 'open-ssl-stream "ssl"))
256 ;;; Internal functions.
258 (defsubst nntp-send-string (process string)
259 "Send STRING to PROCESS."
260 ;; We need to store the time to provide timeouts, and
261 ;; to store the command so the we can replay the command
262 ;; if the server gives us an AUTHINFO challenge.
263 (setq nntp-last-command-time (current-time)
264 nntp-last-command string)
265 (when nntp-record-commands
266 (nntp-record-command string))
267 (process-send-string process (concat string nntp-end-of-line))
268 (or (memq (process-status process) '(open run))
269 (nntp-report "Server closed connection")))
271 (defun nntp-record-command (string)
272 "Record the command STRING."
274 (set-buffer (get-buffer-create "*nntp-log*"))
275 (goto-char (point-max))
276 (let ((time (current-time)))
277 (insert (format-time-string "%Y%m%dT%H%M%S" time)
278 "." (format "%03d" (/ (nth 2 time) 1000))
279 " " nntp-address " " string "\n"))))
281 (defun nntp-report (&rest args)
282 "Report an error from the nntp backend. The first string in ARGS
283 can be a format string. For some commands, the failed command may be
284 retried once before actually displaying the error report."
286 (when nntp-record-commands
287 (nntp-record-command "*** CALLED nntp-report ***"))
289 (nnheader-report 'nntp args)
293 (defun nntp-report-1 (&rest args)
294 "Throws out to nntp-with-open-group-error so that the connection may
295 be restored and the command retried."
297 (when nntp-record-commands
298 (nntp-record-command "*** CONNECTION LOST ***"))
300 (throw 'nntp-with-open-group-error t))
302 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
303 "Wait for WAIT-FOR to arrive from PROCESS."
305 (set-buffer (process-buffer process))
306 (goto-char (point-min))
307 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
309 (memq (process-status process) '(open run)))
310 (when (looking-at "480")
311 (nntp-handle-authinfo process))
312 (when (looking-at "^.*\n")
313 (delete-region (point) (progn (forward-line 1) (point))))
314 (nntp-accept-process-output process)
315 (goto-char (point-min)))
320 (nntp-snarf-error-message)
322 ((not (memq (process-status process) '(open run)))
323 (nntp-report "Server closed connection"))
325 (goto-char (point-max))
326 (let ((limit (point-min))
328 (while (not (re-search-backward wait-for limit t))
329 (nntp-accept-process-output process)
330 ;; We assume that whatever we wait for is less than 1000
332 (setq limit (max (- (point-max) 1000) (point-min)))
333 (goto-char (point-max)))
334 (setq response (match-string 0))
335 (with-current-buffer nntp-server-buffer
336 (setq nntp-process-response response)))
337 (nntp-decode-text (not decode))
341 (goto-char (point-max))
342 (insert-buffer-substring (process-buffer process))
343 ;; Nix out "nntp reading...." message.
344 (when nntp-have-messaged
345 (setq nntp-have-messaged nil)
346 (nnheader-message 5 ""))))
351 (defun nntp-kill-buffer (buffer)
352 (when (buffer-name buffer)
354 (nnheader-init-server-buffer)))
356 (defsubst nntp-find-connection (buffer)
357 "Find the connection delivering to BUFFER."
358 (let ((alist nntp-connection-alist)
359 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
361 (while (and alist (setq entry (pop alist)))
362 (when (eq buffer (cadr entry))
363 (setq process (car entry)
366 (if (memq (process-status process) '(open run))
368 (nntp-kill-buffer (process-buffer process))
369 (setq nntp-connection-alist (delq entry nntp-connection-alist))
372 (defsubst nntp-find-connection-entry (buffer)
373 "Return the entry for the connection to BUFFER."
374 (assq (nntp-find-connection buffer) nntp-connection-alist))
376 (defun nntp-find-connection-buffer (buffer)
377 "Return the process connection buffer tied to BUFFER."
378 (let ((process (nntp-find-connection buffer)))
380 (process-buffer process))))
382 (defsubst nntp-retrieve-data (command address port buffer
383 &optional wait-for callback decode)
384 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
385 (let ((process (or (nntp-find-connection buffer)
386 (nntp-open-connection buffer))))
389 (unless (or nntp-inhibit-erase nnheader-callback-function)
391 (set-buffer (process-buffer process))
396 (nntp-send-string process command))
398 ((eq callback 'ignore)
400 ((and callback wait-for)
401 (nntp-async-wait process wait-for buffer decode callback)
404 (nntp-wait-for process wait-for buffer decode))
407 (nnheader-report 'nntp "Couldn't open connection to %s: %s"
410 (message "Quit retrieving data from nntp")
413 (nnheader-report 'nntp "Couldn't open connection to %s" address))))
415 (defsubst nntp-send-command (wait-for &rest strings)
416 "Send STRINGS to server and wait until WAIT-FOR returns."
417 (when (and (not nnheader-callback-function)
418 (not nntp-inhibit-output))
420 (set-buffer nntp-server-buffer)
422 (let* ((command (mapconcat 'identity strings " "))
423 (process (nntp-find-connection nntp-server-buffer))
424 (buffer (and process (process-buffer process)))
425 (pos (and buffer (with-current-buffer buffer (point)))))
428 (nntp-retrieve-data command
429 nntp-address nntp-port-number
431 wait-for nnheader-callback-function)
432 ;; If nothing to wait for, still remove possibly echo'ed commands.
433 ;; We don't have echos if nntp-open-connection-function
434 ;; is `nntp-open-network-stream', so we skip this in that case.
436 (equal nntp-open-connection-function
437 'nntp-open-network-stream))
438 (nntp-accept-response)
442 (if (looking-at (regexp-quote command))
443 (delete-region pos (progn (forward-line 1)
444 (gnus-point-at-bol))))
446 (nnheader-report 'nntp "Couldn't open connection to %s."
449 (defun nntp-send-command-nodelete (wait-for &rest strings)
450 "Send STRINGS to server and wait until WAIT-FOR returns."
451 (let* ((command (mapconcat 'identity strings " "))
452 (process (nntp-find-connection nntp-server-buffer))
453 (buffer (and process (process-buffer process)))
454 (pos (and buffer (with-current-buffer buffer (point)))))
457 (nntp-retrieve-data command
458 nntp-address nntp-port-number
460 wait-for nnheader-callback-function)
461 ;; If nothing to wait for, still remove possibly echo'ed commands
463 (nntp-accept-response)
467 (if (looking-at (regexp-quote command))
468 (delete-region pos (progn (forward-line 1)
469 (gnus-point-at-bol))))
471 (nnheader-report 'nntp "Couldn't open connection to %s."
474 (defun nntp-send-command-and-decode (wait-for &rest strings)
475 "Send STRINGS to server and wait until WAIT-FOR returns."
476 (when (and (not nnheader-callback-function)
477 (not nntp-inhibit-output))
479 (set-buffer nntp-server-buffer)
481 (let* ((command (mapconcat 'identity strings " "))
482 (process (nntp-find-connection nntp-server-buffer))
483 (buffer (and process (process-buffer process)))
484 (pos (and buffer (with-current-buffer buffer (point)))))
487 (nntp-retrieve-data command
488 nntp-address nntp-port-number
490 wait-for nnheader-callback-function t)
491 ;; If nothing to wait for, still remove possibly echo'ed commands
493 (nntp-accept-response)
497 (if (looking-at (regexp-quote command))
498 (delete-region pos (progn (forward-line 1) (gnus-point-at-bol))))
500 (nnheader-report 'nntp "Couldn't open connection to %s."
504 (defun nntp-send-buffer (wait-for)
505 "Send the current buffer to server and wait until WAIT-FOR returns."
506 (when (and (not nnheader-callback-function)
507 (not nntp-inhibit-output))
509 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
512 (mm-with-unibyte-current-buffer
513 ;; Some encoded unicode text contains character 0x80-0x9f e.g. Euro.
514 (process-send-region (nntp-find-connection nntp-server-buffer)
515 (point-min) (point-max)))
517 nil nntp-address nntp-port-number nntp-server-buffer
518 wait-for nnheader-callback-function))
522 ;;; Interface functions.
524 (nnoo-define-basics nntp)
526 (defsubst nntp-next-result-arrived-p ()
528 ;; A result that starts with a 2xx code is terminated by
529 ;; a line with only a "." on it.
530 ((eq (char-after) ?2)
531 (if (re-search-forward "\n\\.\r?\n" nil t)
534 ;; A result that starts with a 3xx or 4xx code is terminated
537 (if (search-forward "\n" nil t)
545 (defvar nntp-with-open-group-internal nil)
546 (defvar nntp-report-n nil))
548 (defmacro nntp-with-open-group (group server &optional connectionless &rest forms)
549 "Protect against servers that don't like clients that keep idle connections opens.
550 The problem being that these servers may either close a connection or
551 simply ignore any further requests on a connection. Closed
552 connections are not detected until accept-process-output has updated
553 the process-status. Dropped connections are not detected until the
554 connection timeouts (which may be several minutes) or
555 nntp-connection-timeout has expired. When these occur
556 nntp-with-open-group, opens a new connection then re-issues the NNTP
557 command whose response triggered the error."
558 (when (and (listp connectionless)
559 (not (eq connectionless nil)))
560 (setq forms (cons connectionless forms)
562 `(letf ((nntp-report-n (symbol-function 'nntp-report))
563 ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1))
564 (nntp-with-open-group-internal nil))
565 (while (catch 'nntp-with-open-group-error
566 ;; Open the connection to the server
567 ;; NOTE: Existing connections are NOT tested.
568 (nntp-possibly-change-group ,group ,server ,connectionless)
571 (and nntp-connection-timeout
572 (nnheader-run-at-time
573 nntp-connection-timeout nil
575 (let ((process (nntp-find-connection
578 (process-buffer process))))
579 ; when I an able to identify
580 ; the connection to the server
581 ; AND I've received NO reponse
582 ; for nntp-connection-timeout
584 (when (and buffer (eq 0 (buffer-size buffer)))
585 ; Close the connection. Take
586 ; no other action as the
587 ; accept input code will
590 (nntp-kill-buffer buffer))))))))
592 (setq nntp-with-open-group-internal
600 (nnheader-cancel-timer timer)))
602 (setf (symbol-function 'nntp-report) nntp-report-n))
603 nntp-with-open-group-internal))
605 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
606 "Retrieve the headers of ARTICLES."
607 (nntp-with-open-group
610 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
612 (if (and (not gnus-nov-is-evil)
613 (not nntp-nov-is-evil)
614 (nntp-retrieve-headers-with-xover articles fetch-old))
615 ;; We successfully retrieved the headers via XOVER.
617 ;; XOVER didn't work, so we do it the hard, slow and inefficient
619 (let ((number (length articles))
623 (last-point (point-min))
624 (buf (nntp-find-connection-buffer nntp-server-buffer))
625 (nntp-inhibit-erase t)
627 ;; Send HEAD commands.
628 (while (setq article (pop articles))
631 "HEAD" (if (numberp article)
632 (int-to-string article)
633 ;; `articles' is either a list of article numbers
634 ;; or a list of article IDs.
637 ;; Every 400 requests we have to read the stream in
638 ;; order to avoid deadlocks.
639 (when (or (null articles) ;All requests have been sent.
640 (zerop (% count nntp-maximum-request)))
641 (nntp-accept-response)
644 (goto-char last-point)
646 (while (nntp-next-result-arrived-p)
647 (setq last-point (point))
650 ;; If number of headers is greater than 100, give
651 ;; informative messages.
652 (and (numberp nntp-large-newsgroup)
653 (> number nntp-large-newsgroup)
654 (zerop (% received 20))
655 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
656 (/ (* received 100) number)))
657 (nntp-accept-response))))
658 (and (numberp nntp-large-newsgroup)
659 (> number nntp-large-newsgroup)
660 (nnheader-message 6 "NNTP: Receiving headers...done"))
662 ;; Now all of replies are received. Fold continuation lines.
663 (nnheader-fold-continuation-lines)
664 ;; Remove all "\r"'s.
666 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
669 (deffoo nntp-retrieve-groups (groups &optional server)
670 "Retrieve group info on GROUPS."
671 (nntp-with-open-group
673 (when (nntp-find-connection-buffer nntp-server-buffer)
676 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
677 (set-buffer nntp-server-buffer)
679 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
680 ;; The first time this is run, this variable is `try'. So we
682 (when (eq nntp-server-list-active-group 'try)
683 (nntp-try-list-active (car groups)))
688 (last-point (point-min))
689 (nntp-inhibit-erase t)
690 (buf (nntp-find-connection-buffer nntp-server-buffer))
691 (command (if nntp-server-list-active-group
692 "LIST ACTIVE" "GROUP")))
694 ;; Timeout may have killed the buffer.
695 (unless (gnus-buffer-live-p buf)
696 (nnheader-report 'nntp "Connection to %s is closed." server)
698 ;; Send the command to the server.
699 (nntp-send-command nil command (pop groups))
701 ;; Every 400 requests we have to read the stream in
702 ;; order to avoid deadlocks.
703 (when (or (null groups) ;All requests have been sent.
704 (zerop (% count nntp-maximum-request)))
705 (nntp-accept-response)
706 (while (and (gnus-buffer-live-p buf)
708 ;; Search `blue moon' in this file for the
709 ;; reason why set-buffer here.
711 (goto-char last-point)
713 (while (re-search-forward "^[0-9]" nil t)
715 (setq last-point (point))
717 (nntp-accept-response))))
719 ;; Wait for the reply from the final command.
720 (unless (gnus-buffer-live-p buf)
721 (nnheader-report 'nntp "Connection to %s is closed." server)
724 (goto-char (point-max))
725 (re-search-backward "^[0-9]" nil t)
726 (when (looking-at "^[23]")
727 (while (and (gnus-buffer-live-p buf)
730 (goto-char (point-max))
731 (if (not nntp-server-list-active-group)
732 (not (re-search-backward "\r?\n"
734 (not (re-search-backward "^\\.\r?\n"
736 (nntp-accept-response)))
738 ;; Now all replies are received. We remove CRs.
739 (unless (gnus-buffer-live-p buf)
740 (nnheader-report 'nntp "Connection to %s is closed." server)
743 (goto-char (point-min))
744 (while (search-forward "\r" nil t)
745 (replace-match "" t t))
747 (if (not nntp-server-list-active-group)
749 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
751 ;; We have read active entries, so we just delete the
753 (goto-char (point-min))
754 (while (re-search-forward "^[.2-5]" nil t)
755 (delete-region (match-beginning 0)
756 (progn (forward-line 1) (point))))
757 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
760 (deffoo nntp-retrieve-articles (articles &optional group server)
761 (nntp-with-open-group
764 (let ((number (length articles))
768 (last-point (point-min))
769 (buf (nntp-find-connection-buffer nntp-server-buffer))
770 (nntp-inhibit-erase t)
771 (map (apply 'vector articles))
776 ;; Send ARTICLE command.
777 (while (setq article (pop articles))
780 "ARTICLE" (if (numberp article)
781 (int-to-string article)
782 ;; `articles' is either a list of article numbers
783 ;; or a list of article IDs.
786 ;; Every 400 requests we have to read the stream in
787 ;; order to avoid deadlocks.
788 (when (or (null articles) ;All requests have been sent.
789 (zerop (% count nntp-maximum-request)))
790 (nntp-accept-response)
793 (goto-char last-point)
795 (while (nntp-next-result-arrived-p)
796 (aset map received (cons (aref map received) (point)))
797 (setq last-point (point))
800 ;; If number of headers is greater than 100, give
801 ;; informative messages.
802 (and (numberp nntp-large-newsgroup)
803 (> number nntp-large-newsgroup)
804 (zerop (% received 20))
805 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
806 (/ (* received 100) number)))
807 (nntp-accept-response))))
808 (and (numberp nntp-large-newsgroup)
809 (> number nntp-large-newsgroup)
810 (nnheader-message 6 "NNTP: Receiving articles...done"))
812 ;; Now we have all the responses. We go through the results,
813 ;; wash it and copy it over to the server buffer.
814 (set-buffer nntp-server-buffer)
816 (setq last-point (point-min))
820 (setq point (goto-char (point-max)))
822 (insert-buffer-substring buf last-point (cdr entry))
824 (setq last-point (cdr entry))
827 (cons (car entry) point))
830 (defun nntp-try-list-active (group)
831 (nntp-list-active-group group)
833 (set-buffer nntp-server-buffer)
834 (goto-char (point-min))
836 (looking-at "5[0-9]+"))
837 (setq nntp-server-list-active-group nil))
839 (setq nntp-server-list-active-group t)))))
841 (deffoo nntp-list-active-group (group &optional server)
842 "Return the active info on GROUP (which can be a regexp)."
843 (nntp-with-open-group
845 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group)))
847 (deffoo nntp-request-group-articles (group &optional server)
848 "Return the list of existing articles in GROUP."
849 (nntp-with-open-group
851 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group)))
853 (deffoo nntp-request-article (article &optional group server buffer command)
854 (nntp-with-open-group
856 (when (nntp-send-command-and-decode
857 "\r?\n\\.\r?\n" "ARTICLE"
858 (if (numberp article) (int-to-string article) article))
860 (not (equal buffer nntp-server-buffer)))
862 (set-buffer nntp-server-buffer)
863 (copy-to-buffer buffer (point-min) (point-max))
864 (nntp-find-group-and-number group))
865 (nntp-find-group-and-number group)))))
867 (deffoo nntp-request-head (article &optional group server)
868 (nntp-with-open-group
870 (when (nntp-send-command
871 "\r?\n\\.\r?\n" "HEAD"
872 (if (numberp article) (int-to-string article) article))
874 (nntp-find-group-and-number group)
875 (nntp-decode-text)))))
877 (deffoo nntp-request-body (article &optional group server)
878 (nntp-with-open-group
880 (nntp-send-command-and-decode
881 "\r?\n\\.\r?\n" "BODY"
882 (if (numberp article) (int-to-string article) article))))
884 (deffoo nntp-request-group (group &optional server dont-check)
885 (nntp-with-open-group
887 (when (nntp-send-command "^[245].*\n" "GROUP" group)
888 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
889 (setcar (cddr entry) group)))))
891 (deffoo nntp-close-group (group &optional server)
894 (deffoo nntp-server-opened (&optional server)
895 "Say whether a connection to SERVER has been opened."
896 (and (nnoo-current-server-p 'nntp server)
898 (gnus-buffer-live-p nntp-server-buffer)
899 (nntp-find-connection nntp-server-buffer)))
901 (deffoo nntp-open-server (server &optional defs connectionless)
902 (nnheader-init-server-buffer)
903 (if (nntp-server-opened server)
905 (when (or (stringp (car defs))
906 (numberp (car defs)))
907 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
908 (unless (assq 'nntp-address defs)
909 (setq defs (append defs (list (list 'nntp-address server)))))
910 (nnoo-change-server 'nntp server defs)
911 (unless connectionless
912 (or (nntp-find-connection nntp-server-buffer)
913 (nntp-open-connection nntp-server-buffer)))))
915 (deffoo nntp-close-server (&optional server)
916 (nntp-possibly-change-group nil server t)
917 (let ((process (nntp-find-connection nntp-server-buffer)))
919 (when (memq (process-status process) '(open run))
921 (nntp-send-string process "QUIT")
922 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
923 ;; Ok, this is evil, but when using telnet and stuff
924 ;; as the connection method, it's important that the
925 ;; QUIT command actually is sent out before we kill
928 (nntp-kill-buffer (process-buffer process))
929 (setq process (car (pop nntp-connection-alist))))
930 (nnoo-close-server 'nntp)))
932 (deffoo nntp-request-close ()
934 (while (setq process (pop nntp-connection-list))
935 (when (memq (process-status process) '(open run))
937 (nntp-send-string process "QUIT")
938 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
939 ;; Ok, this is evil, but when using telnet and stuff
940 ;; as the connection method, it's important that the
941 ;; QUIT command actually is sent out before we kill
944 (nntp-kill-buffer (process-buffer process)))))
946 (deffoo nntp-request-list (&optional server)
947 (nntp-with-open-group
949 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST")))
951 (deffoo nntp-request-list-newsgroups (&optional server)
952 (nntp-with-open-group
954 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS")))
956 (deffoo nntp-request-newgroups (date &optional server)
957 (nntp-with-open-group
960 (set-buffer nntp-server-buffer)
961 (let* ((time (date-to-time date))
962 (ls (- (cadr time) (nth 8 (decode-time time)))))
964 (setcar time (1- (car time)))
965 (setcar (cdr time) (+ ls 65536)))
967 (setcar time (1+ (car time)))
968 (setcar (cdr time) (- ls 65536)))
970 (setcar (cdr time) ls)))
973 "^\\.\r?\n" "NEWGROUPS"
974 (format-time-string "%y%m%d %H%M%S" time)
976 (nntp-decode-text))))))
978 (deffoo nntp-request-post (&optional server)
979 (nntp-with-open-group
981 (when (nntp-send-command "^[23].*\r?\n" "POST")
982 (let ((response (with-current-buffer nntp-server-buffer
983 nntp-process-response))
986 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
988 (setq server-id (match-string 1 response))
989 (narrow-to-region (goto-char (point-min))
990 (if (search-forward "\n\n" nil t)
993 (unless (mail-fetch-field "Message-ID")
994 (goto-char (point-min))
995 (insert "Message-ID: " server-id "\n"))
997 (run-hooks 'nntp-prepare-post-hook)
998 (nntp-send-buffer "^[23].*\n")))))
1000 (deffoo nntp-request-type (group article)
1003 (deffoo nntp-asynchronous-p ()
1006 ;;; Hooky functions.
1008 (defun nntp-send-mode-reader ()
1009 "Send the MODE READER command to the nntp server.
1010 This function is supposed to be called from `nntp-server-opened-hook'.
1011 It will make innd servers spawn an nnrpd process to allow actual article
1013 (nntp-send-command "^.*\n" "MODE READER"))
1015 (defun nntp-send-authinfo (&optional send-if-force)
1016 "Send the AUTHINFO to the nntp server.
1017 It will look in the \"~/.authinfo\" file for matching entries. If
1018 nothing suitable is found there, it will prompt for a user name
1021 If SEND-IF-FORCE, only send authinfo to the server if the
1022 .authinfo file has the FORCE token."
1023 (let* ((list (gnus-parse-netrc nntp-authinfo-file))
1024 (alist (gnus-netrc-machine list nntp-address "nntp"))
1025 (force (gnus-netrc-get alist "force"))
1026 (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
1027 (passwd (gnus-netrc-get alist "password")))
1028 (when (or (not send-if-force)
1031 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
1032 nntp-authinfo-user user))
1033 (unless (member user '(nil ""))
1034 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
1035 (when t ;???Should check if AUTHINFO succeeded
1037 "^2.*\r?\n" "AUTHINFO PASS"
1039 nntp-authinfo-password
1040 (setq nntp-authinfo-password
1041 (mail-source-read-passwd
1042 (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 (mail-source-read-passwd "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) (progn (end-of-line) (point)))))))
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 ((proc (open-ssl-stream "nntpd" buffer nntp-address nntp-port-number)))
1150 (nntp-wait-for-string "^\r*20[01]")
1152 (delete-region (point-min) (point))
1155 (defun nntp-read-server-type ()
1156 "Find out what the name of the server we have connected to is."
1157 ;; Wait for the status string to arrive.
1158 (setq nntp-server-type (buffer-string))
1159 (let ((alist nntp-server-action-alist)
1160 (case-fold-search t)
1162 ;; Run server-specific commands.
1164 (setq entry (pop alist))
1165 (when (string-match (car entry) nntp-server-type)
1166 (if (and (listp (cadr entry))
1167 (not (eq 'lambda (caadr entry))))
1169 (funcall (cadr entry)))))))
1171 (defun nntp-async-wait (process wait-for buffer decode callback)
1173 (set-buffer (process-buffer process))
1174 (unless nntp-inside-change-function
1176 (setq nntp-process-wait-for wait-for
1177 nntp-process-to-buffer buffer
1178 nntp-process-decode decode
1179 nntp-process-callback callback
1180 nntp-process-start-point (point-max))
1181 (setq after-change-functions '(nntp-after-change-function))
1182 (if nntp-async-needs-kluge
1183 (nntp-async-kluge process))))
1185 (defun nntp-async-kluge (process)
1186 ;; emacs 20.3 bug: process output with encoding 'binary
1187 ;; doesn't trigger after-change-functions.
1188 (unless nntp-async-timer
1189 (setq nntp-async-timer
1190 (nnheader-run-at-time 1 1 'nntp-async-timer-handler)))
1191 (add-to-list 'nntp-async-process-list process))
1193 (defun nntp-async-timer-handler ()
1196 (if (memq (process-status proc) '(open run))
1197 (nntp-async-trigger proc)
1198 (nntp-async-stop proc)))
1199 nntp-async-process-list))
1201 (defun nntp-async-stop (proc)
1202 (setq nntp-async-process-list (delq proc nntp-async-process-list))
1203 (when (and nntp-async-timer (not nntp-async-process-list))
1204 (nnheader-cancel-timer nntp-async-timer)
1205 (setq nntp-async-timer nil)))
1207 (defun nntp-after-change-function (beg end len)
1209 ;; we only care about insertions at eob
1210 (when (and (eq 0 len) (eq (point-max) end))
1212 (let ((proc (get-buffer-process (current-buffer))))
1214 (nntp-async-trigger proc)))))
1215 ;; any throw from after-change-functions will leave it
1216 ;; set to nil. so we reset it here, if necessary.
1218 (setq after-change-functions '(nntp-after-change-function)))))
1220 (defun nntp-async-trigger (process)
1222 (set-buffer (process-buffer process))
1223 (when nntp-process-callback
1224 ;; do we have an error message?
1225 (goto-char nntp-process-start-point)
1226 (if (memq (following-char) '(?4 ?5))
1227 ;; wants credentials?
1228 (if (looking-at "480")
1229 (nntp-handle-authinfo process)
1230 ;; report error message.
1231 (nntp-snarf-error-message)
1232 (nntp-do-callback nil))
1234 ;; got what we expect?
1235 (goto-char (point-max))
1236 (when (re-search-backward
1237 nntp-process-wait-for nntp-process-start-point t)
1238 (let ((response (match-string 0)))
1239 (with-current-buffer nntp-server-buffer
1240 (setq nntp-process-response response)))
1241 (nntp-async-stop process)
1243 (when (gnus-buffer-exists-p nntp-process-to-buffer)
1244 (let ((buf (current-buffer))
1245 (start nntp-process-start-point)
1246 (decode nntp-process-decode))
1248 (set-buffer nntp-process-to-buffer)
1249 (goto-char (point-max))
1251 (narrow-to-region (point) (point))
1252 (insert-buffer-substring buf start)
1254 (nntp-decode-text))))))
1256 (goto-char (point-max))
1258 (buffer-name (get-buffer nntp-process-to-buffer))))))))
1260 (defun nntp-do-callback (arg)
1261 (let ((callback nntp-process-callback)
1262 (nntp-inside-change-function t))
1263 (setq nntp-process-callback nil)
1264 (funcall callback arg)))
1266 (defun nntp-snarf-error-message ()
1267 "Save the error message in the current buffer."
1268 (let ((message (buffer-string)))
1269 (while (string-match "[\r\n]+" message)
1270 (setq message (replace-match " " t t message)))
1271 (nnheader-report 'nntp message)
1274 (defun nntp-accept-process-output (process &optional timeout)
1275 "Wait for output from PROCESS and message some dots."
1277 (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1278 nntp-server-buffer))
1279 (let ((len (/ (point-max) 1024))
1282 (setq nntp-have-messaged t)
1283 (nnheader-message 7 "nntp read: %dk" len)))
1285 (accept-process-output process timeout)
1286 (accept-process-output process 0 100))
1287 ;; accept-process-output may update status of process to indicate
1288 ;; that the server has closed the connection. This MUST be
1289 ;; handled here as the buffer restored by the save-excursion may
1290 ;; be the process's former output buffer (i.e. now killed)
1292 (memq (process-status process) '(open run)))
1293 (nntp-report "Server closed connection"))))
1295 (defun nntp-accept-response ()
1296 "Wait for output from the process that outputs to BUFFER."
1297 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1299 (defun nntp-possibly-change-group (group server &optional connectionless)
1300 (let ((nnheader-callback-function nil))
1302 (or (nntp-server-opened server)
1303 (nntp-open-server server nil connectionless)))
1305 (unless connectionless
1306 (or (nntp-find-connection nntp-server-buffer)
1307 (nntp-open-connection nntp-server-buffer))))
1310 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1311 (when (not (equal group (caddr entry)))
1313 (set-buffer (process-buffer (car entry)))
1315 (nntp-send-command "^[245].*\n" "GROUP" group)
1316 (setcar (cddr entry) group)
1319 (set-buffer nntp-server-buffer)
1320 (erase-buffer)))))))
1322 (defun nntp-decode-text (&optional cr-only)
1323 "Decode the text in the current buffer."
1324 (goto-char (point-min))
1325 (while (search-forward "\r" nil t)
1328 ;; Remove trailing ".\n" end-of-transfer marker.
1329 (goto-char (point-max))
1331 (when (looking-at ".\n")
1333 ;; Delete status line.
1334 (goto-char (point-min))
1335 (while (looking-at "[1-5][0-9][0-9] .*\n")
1336 ;; For some unknown reason, there is more than one status line.
1337 (delete-region (point) (progn (forward-line 1) (point))))
1338 ;; Remove "." -> ".." encoding.
1339 (while (search-forward "\n.." nil t)
1342 (defun nntp-encode-text ()
1343 "Encode the text in the current buffer."
1345 ;; Replace "." at beginning of line with "..".
1346 (goto-char (point-min))
1347 (while (re-search-forward "^\\." nil t)
1349 (goto-char (point-max))
1350 ;; Insert newline at the end of the buffer.
1353 ;; Insert `.' at end of buffer (end of text mark).
1354 (goto-char (point-max))
1356 (goto-char (point-min))
1360 (insert nntp-end-of-line))))
1362 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1363 (set-buffer nntp-server-buffer)
1367 ;; This server does not talk NOV.
1368 ((not nntp-server-xover)
1371 ;; We don't care about gaps.
1372 ((or (not nntp-nov-gap)
1374 (nntp-send-xover-command
1376 (if (numberp fetch-old)
1377 (max 1 (- (car articles) fetch-old))
1380 (car (last articles)) 'wait)
1382 (goto-char (point-min))
1383 (when (looking-at "[1-5][0-9][0-9] .*\n")
1384 (delete-region (point) (progn (forward-line 1) (point))))
1385 (while (search-forward "\r" nil t)
1386 (replace-match "" t t))
1387 (goto-char (point-max))
1389 (when (looking-at "\\.")
1390 (delete-region (point) (progn (forward-line 1) (point)))))
1392 ;; We do it the hard way. For each gap, an XOVER command is sent
1393 ;; to the server. We do not wait for a reply from the server, we
1394 ;; just send them off as fast as we can. That means that we have
1395 ;; to count the number of responses we get back to find out when we
1396 ;; have gotten all we asked for.
1397 ((numberp nntp-nov-gap)
1402 (buf nntp-server-buffer)
1403 (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1406 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1407 ;; that means that the server does not understand XOVER, but we
1408 ;; won't know that until we try.
1409 (while (and nntp-server-xover articles)
1410 (setq first (car articles))
1411 ;; Search forward until we find a gap, or until we run out of
1413 (while (and (cdr articles)
1414 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1415 (setq articles (cdr articles)))
1417 (setq in-process-buffer-p (stringp nntp-server-xover))
1418 (nntp-send-xover-command first (setq last (car articles)))
1419 (setq articles (cdr articles))
1421 (when (and nntp-server-xover in-process-buffer-p)
1422 ;; Don't count tried request.
1423 (setq count (1+ count))
1425 ;; Every 400 requests we have to read the stream in
1426 ;; order to avoid deadlocks.
1427 (when (or (null articles) ;All requests have been sent.
1428 (= 1 (% count nntp-maximum-request)))
1430 (nntp-accept-response)
1431 ;; On some Emacs versions the preceding function has a
1432 ;; tendency to change the buffer. Perhaps. It's quite
1433 ;; difficult to reproduce, because it only seems to happen
1434 ;; once in a blue moon.
1435 (set-buffer process-buffer)
1437 (goto-char (or last-point (point-min)))
1439 (while (re-search-forward "^[0-9][0-9][0-9] .*\n" nil t)
1441 (setq last-point (point))
1442 (or (< received count)
1443 ;; I haven't started reading the final response
1445 (goto-char (point-max))
1447 (not (looking-at "^\\.\r?\n")))))
1448 ;; I haven't read the end of the final response
1449 (nntp-accept-response)
1450 (set-buffer process-buffer))))
1452 ;; Some nntp servers seem to have an extension to the XOVER
1453 ;; extension. On these servers, requesting an article range
1454 ;; preceeding the active range does not return an error as
1455 ;; specified in the RFC. What we instead get is the NOV entry
1456 ;; for the first available article. Obviously, a client can
1457 ;; use that entry to avoid making unnecessary requests. The
1458 ;; only problem is for a client that assumes that the response
1459 ;; will always be within the requested ranage. For such a
1460 ;; client, we can get N copies of the same entry (one for each
1461 ;; XOVER command sent to the server).
1464 (goto-char (point-min))
1465 (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t)
1466 (let ((low-limit (string-to-int
1467 (buffer-substring (match-beginning 1)
1469 (while (and articles (<= (car articles) low-limit))
1470 (setq articles (cdr articles))))))
1473 (when nntp-server-xover
1474 (when in-process-buffer-p
1476 (goto-char (point-max))
1477 (insert-buffer-substring process-buffer)
1478 (set-buffer process-buffer)
1482 ;; We remove any "." lines and status lines.
1483 (goto-char (point-min))
1484 (while (search-forward "\r" nil t)
1486 (goto-char (point-min))
1487 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1492 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1493 "Send the XOVER command to the server."
1494 (let ((range (format "%d-%d" beg end))
1495 (nntp-inhibit-erase t))
1496 (if (stringp nntp-server-xover)
1497 ;; If `nntp-server-xover' is a string, then we just send this
1500 (nntp-send-command-nodelete
1501 "\r?\n\\.\r?\n" nntp-server-xover range)
1502 ;; We do not wait for the reply.
1503 (nntp-send-command-nodelete nil nntp-server-xover range))
1504 (let ((commands nntp-xover-commands))
1505 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1506 ;; We try them all until we get at positive response.
1507 (while (and commands (eq nntp-server-xover 'try))
1508 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1510 (set-buffer nntp-server-buffer)
1511 (goto-char (point-min))
1512 (and (looking-at "[23]") ; No error message.
1513 ;; We also have to look at the lines. Some buggy
1514 ;; servers give back simple lines with just the
1515 ;; article number. How... helpful.
1518 (looking-at "[0-9]+\t...")) ; More text after number.
1519 (setq nntp-server-xover (car commands))))
1520 (setq commands (cdr commands)))
1521 ;; If none of the commands worked, we disable XOVER.
1522 (when (eq nntp-server-xover 'try)
1524 (set-buffer nntp-server-buffer)
1526 (setq nntp-server-xover nil)))
1527 nntp-server-xover))))
1529 (defun nntp-find-group-and-number (&optional group)
1532 (set-buffer nntp-server-buffer)
1533 (narrow-to-region (goto-char (point-min))
1534 (or (search-forward "\n\n" nil t) (point-max)))
1535 (goto-char (point-min))
1536 ;; We first find the number by looking at the status line.
1537 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1539 (buffer-substring (match-beginning 1)
1542 (and number (zerop number) (setq number nil))
1544 ;; Then we find the group name.
1547 ;; If there is only one group in the Newsgroups
1548 ;; header, then it seems quite likely that this
1549 ;; article comes from that group, I'd say.
1550 ((and (setq newsgroups
1551 (mail-fetch-field "newsgroups"))
1552 (not (string-match "," newsgroups)))
1554 ;; If there is more than one group in the
1555 ;; Newsgroups header, then the Xref header should
1556 ;; be filled out. We hazard a guess that the group
1557 ;; that has this article number in the Xref header
1558 ;; is the one we are looking for. This might very
1559 ;; well be wrong if this article happens to have
1560 ;; the same number in several groups, but that's
1562 ((and (setq xref (mail-fetch-field "xref"))
1565 (format "\\([^ :]+\\):%d" number) xref))
1566 (match-string 1 xref))
1569 ((and (setq xref (mail-fetch-field "xref"))
1572 (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)")
1573 "\\([^ :]+\\):\\([0-9]+\\)")
1575 (setq group (match-string 1 xref)
1576 number (string-to-int (match-string 2 xref))))
1577 ((and (setq newsgroups
1578 (mail-fetch-field "newsgroups"))
1579 (not (string-match "," newsgroups)))
1580 (setq group newsgroups))
1582 (t (setq group ""))))
1583 (when (string-match "\r" group)
1584 (setq group (substring group 0 (match-beginning 0))))
1585 (cons group number)))))
1587 (defun nntp-wait-for-string (regexp)
1588 "Wait until string arrives in the buffer."
1589 (let ((buf (current-buffer))
1591 (goto-char (point-min))
1592 (while (and (setq proc (get-buffer-process buf))
1593 (memq (process-status proc) '(open run))
1594 (not (re-search-forward regexp nil t)))
1595 (accept-process-output proc)
1597 (goto-char (point-min)))))
1600 ;; ==========================================================================
1601 ;; Obsolete nntp-open-* connection methods -- drv
1602 ;; ==========================================================================
1604 (defvoo nntp-open-telnet-envuser nil
1605 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
1607 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
1608 "*Regular expression to match the shell prompt on the remote machine.")
1610 (defvoo nntp-rlogin-program "rsh"
1611 "*Program used to log in on remote machines.
1612 The default is \"rsh\", but \"ssh\" is a popular alternative.")
1614 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1615 "*Parameters to `nntp-open-rlogin'.
1616 That function may be used as `nntp-open-connection-function'. In that
1617 case, this list will be used as the parameter list given to rsh.")
1619 (defvoo nntp-rlogin-user-name nil
1620 "*User name on remote system when using the rlogin connect method.")
1622 (defvoo nntp-telnet-parameters
1623 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
1624 "*Parameters to `nntp-open-telnet'.
1625 That function may be used as `nntp-open-connection-function'. In that
1626 case, this list will be executed as a command after logging in
1629 (defvoo nntp-telnet-user-name nil
1630 "User name to log in via telnet with.")
1632 (defvoo nntp-telnet-passwd nil
1633 "Password to use to log in via telnet with.")
1635 (defun nntp-open-telnet (buffer)
1641 "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1642 (case-fold-search t))
1643 (when (memq (process-status proc) '(open run))
1644 (nntp-wait-for-string "^r?telnet")
1645 (process-send-string proc "set escape \^X\n")
1647 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1648 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1649 nntp-address "\n")))
1651 (process-send-string proc (concat "open " nntp-address "\n"))))
1653 ((not nntp-open-telnet-envuser)
1654 (nntp-wait-for-string "^\r*.?login:")
1655 (process-send-string
1657 (or nntp-telnet-user-name
1658 (setq nntp-telnet-user-name (read-string "login: ")))
1660 (nntp-wait-for-string "^\r*.?password:")
1661 (process-send-string
1663 (or nntp-telnet-passwd
1664 (setq nntp-telnet-passwd
1665 (mail-source-read-passwd "Password: ")))
1667 (nntp-wait-for-string nntp-telnet-shell-prompt)
1668 (process-send-string
1669 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1670 (nntp-wait-for-string "^\r*20[01]")
1672 (delete-region (point-min) (point))
1673 (process-send-string proc "\^]")
1674 (nntp-wait-for-string "^r?telnet")
1675 (process-send-string proc "mode character\n")
1676 (accept-process-output proc 1)
1678 (goto-char (point-min))
1680 (delete-region (point) (point-max)))
1683 (defun nntp-open-rlogin (buffer)
1684 "Open a connection to SERVER using rsh."
1685 (let ((proc (if nntp-rlogin-user-name
1686 (apply 'start-process
1687 "nntpd" buffer nntp-rlogin-program
1688 nntp-address "-l" nntp-rlogin-user-name
1689 nntp-rlogin-parameters)
1690 (apply 'start-process
1691 "nntpd" buffer nntp-rlogin-program nntp-address
1692 nntp-rlogin-parameters))))
1695 (nntp-wait-for-string "^\r*20[01]")
1697 (delete-region (point-min) (point))
1701 ;; ==========================================================================
1702 ;; Replacements for the nntp-open-* functions -- drv
1703 ;; ==========================================================================
1705 (defun nntp-open-telnet-stream (buffer)
1706 "Open a nntp connection by telnet'ing the news server.
1708 Please refer to the following variables to customize the connection:
1709 - `nntp-pre-command',
1710 - `nntp-telnet-command',
1711 - `nntp-telnet-switches',
1713 - `nntp-port-number',
1714 - `nntp-end-of-line'."
1715 (let ((command `(,nntp-telnet-command
1716 ,@nntp-telnet-switches
1717 ,nntp-address ,nntp-port-number))
1719 (and nntp-pre-command
1720 (push nntp-pre-command command))
1721 (setq proc (apply 'start-process "nntpd" buffer command))
1724 (nntp-wait-for-string "^\r*20[01]")
1726 (delete-region (point-min) (point))
1729 (defun nntp-open-via-rlogin-and-telnet (buffer)
1730 "Open a connection to an nntp server through an intermediate host.
1731 First rlogin to the remote host, and then telnet the real news server
1734 Please refer to the following variables to customize the connection:
1735 - `nntp-pre-command',
1736 - `nntp-via-rlogin-command',
1737 - `nntp-via-rlogin-command-switches',
1738 - `nntp-via-user-name',
1739 - `nntp-via-address',
1740 - `nntp-telnet-command',
1741 - `nntp-telnet-switches',
1743 - `nntp-port-number',
1744 - `nntp-end-of-line'."
1745 (let ((command `(,nntp-via-address
1746 ,nntp-telnet-command
1747 ,@nntp-telnet-switches))
1749 (when nntp-via-user-name
1750 (setq command `("-l" ,nntp-via-user-name ,@command)))
1751 (when nntp-via-rlogin-command-switches
1752 (setq command (append nntp-via-rlogin-command-switches command)))
1753 (push nntp-via-rlogin-command command)
1754 (and nntp-pre-command
1755 (push nntp-pre-command command))
1756 (setq proc (apply 'start-process "nntpd" buffer command))
1759 (nntp-wait-for-string "^r?telnet")
1760 (process-send-string proc (concat "open " nntp-address
1761 " " nntp-port-number "\n"))
1762 (nntp-wait-for-string "^\r*20[01]")
1764 (delete-region (point-min) (point))
1767 (defun nntp-open-via-telnet-and-telnet (buffer)
1768 "Open a connection to an nntp server through an intermediate host.
1769 First telnet the remote host, and then telnet the real news server
1772 Please refer to the following variables to customize the connection:
1773 - `nntp-pre-command',
1774 - `nntp-via-telnet-command',
1775 - `nntp-via-telnet-switches',
1776 - `nntp-via-address',
1777 - `nntp-via-envuser',
1778 - `nntp-via-user-name',
1779 - `nntp-via-user-password',
1780 - `nntp-via-shell-prompt',
1781 - `nntp-telnet-command',
1782 - `nntp-telnet-switches',
1784 - `nntp-port-number',
1785 - `nntp-end-of-line'."
1789 (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches))
1790 (case-fold-search t)
1792 (and nntp-pre-command (push nntp-pre-command command))
1793 (setq proc (apply 'start-process "nntpd" buffer command))
1794 (when (memq (process-status proc) '(open run))
1795 (nntp-wait-for-string "^r?telnet")
1796 (process-send-string proc "set escape \^X\n")
1798 ((and nntp-via-envuser nntp-via-user-name)
1799 (process-send-string proc (concat "open " "-l" nntp-via-user-name
1800 nntp-via-address "\n")))
1802 (process-send-string proc (concat "open " nntp-via-address
1804 (when (not nntp-via-envuser)
1805 (nntp-wait-for-string "^\r*.?login:")
1806 (process-send-string proc
1808 (or nntp-via-user-name
1809 (setq nntp-via-user-name
1810 (read-string "login: ")))
1812 (nntp-wait-for-string "^\r*.?password:")
1813 (process-send-string proc
1815 (or nntp-via-user-password
1816 (setq nntp-via-user-password
1817 (mail-source-read-passwd
1820 (nntp-wait-for-string nntp-via-shell-prompt)
1821 (let ((real-telnet-command `("exec"
1822 ,nntp-telnet-command
1823 ,@nntp-telnet-switches
1825 ,nntp-port-number)))
1826 (process-send-string proc
1827 (concat (mapconcat 'identity
1828 real-telnet-command " ")
1830 (nntp-wait-for-string "^\r*20[01]")
1832 (delete-region (point-min) (point))
1833 (process-send-string proc "\^]")
1834 (nntp-wait-for-string "^r?telnet")
1835 (process-send-string proc "mode character\n")
1836 (accept-process-output proc 1)
1838 (goto-char (point-min))
1840 (delete-region (point) (point-max)))
1845 ;;; nntp.el ends here