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 by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the 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.
70 Two pre-made functions are `nntp-open-network-stream', which is the
71 default, and simply connects to some port or other on the remote
72 system (see nntp-port-number). The other are `nntp-open-rlogin',
73 which does an rlogin on the remote system, and then does a telnet to
74 the NNTP server available there (see nntp-rlogin-parameters) and
75 `nntp-open-telnet' which telnets to a remote system, logs in and does
78 (defvoo nntp-rlogin-program
"rsh"
79 "*Program used to log in on remote machines.
80 The default is \"rsh\", but \"ssh\" is a popular alternative.")
82 (defvoo nntp-rlogin-parameters
'("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
83 "*Parameters to `nntp-open-rlogin'.
84 That function may be used as `nntp-open-connection-function'. In that
85 case, this list will be used as the parameter list given to rsh.")
87 (defvoo nntp-rlogin-user-name nil
88 "*User name on remote system when using the rlogin connect method.")
90 (defvoo nntp-telnet-parameters
91 '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
92 "*Parameters to `nntp-open-telnet'.
93 That function may be used as `nntp-open-connection-function'. In that
94 case, this list will be executed as a command after logging in
97 (defvoo nntp-telnet-user-name nil
98 "User name to log in via telnet with.")
100 (defvoo nntp-telnet-passwd nil
101 "Password to use to log in via telnet with.")
103 (defvoo nntp-open-telnet-envuser nil
104 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
106 (defvoo nntp-telnet-shell-prompt
"bash\\|\$ *\r?$\\|> *\r?"
107 "*Regular expression to match the shell prompt on the remote machine.")
109 (defvoo nntp-telnet-command
"telnet"
110 "Command used to start telnet.")
112 (defvoo nntp-telnet-switches
'("-8")
113 "Switches given to the telnet command.")
115 (defvoo nntp-end-of-line
"\r\n"
116 "String to use on the end of lines when talking to the NNTP server.
117 This is \"\\r\\n\" by default, but should be \"\\n\" when
118 using rlogin or telnet to communicate with the server.")
120 (defvoo nntp-large-newsgroup
50
121 "*The number of the articles which indicates a large newsgroup.
122 If the number of the articles is greater than the value, verbose
123 messages will be shown to indicate the current status.")
125 (defvoo nntp-maximum-request
400
126 "*The maximum number of the requests sent to the NNTP server at one time.
127 If Emacs hangs up while retrieving headers, set the variable to a
130 (defvoo nntp-nov-is-evil nil
131 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
133 (defvoo nntp-xover-commands
'("XOVER" "XOVERVIEW")
134 "*List of strings that are used as commands to fetch NOV lines from a server.
135 The strings are tried in turn until a positive response is gotten. If
136 none of the commands are successful, nntp will just grab headers one
139 (defvoo nntp-nov-gap
5
140 "*Maximum allowed gap between two articles.
141 If the gap between two consecutive articles is bigger than this
142 variable, split the XOVER request into two requests.")
144 (defvoo nntp-prepare-server-hook nil
145 "*Hook run before a server is opened.
146 If can be used to set up a server remotely, for instance. Say you
147 have an account at the machine \"other.machine\". This machine has
148 access to an NNTP server that you can't access locally. You could
149 then use this hook to rsh to the remote machine and start a proxy NNTP
150 server there that you can connect to. See also
151 `nntp-open-connection-function'")
153 (defvoo nntp-warn-about-losing-connection t
154 "*If non-nil, beep when a server closes connection.")
156 (defvoo nntp-coding-system-for-read
'binary
157 "*Coding system to read from NNTP.")
159 (defvoo nntp-coding-system-for-write
'binary
160 "*Coding system to write to NNTP.")
162 (defcustom nntp-authinfo-file
"~/.authinfo"
163 ".netrc-like file that holds nntp authinfo passwords."
166 (repeat :tag
"Entries"
169 :value
("" ("login" .
"") ("password" .
""))
173 (const :format
"" "login")
174 (string :format
"Login: %v"))
176 (const :format
"" "password")
177 (string :format
"Password: %v"))))))
182 (defvoo nntp-connection-timeout nil
183 "*Number of seconds to wait before an nntp connection times out.
184 If this variable is nil, which is the default, no timers are set.
185 NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.")
187 ;;; Internal variables.
189 (defvar nntp-record-commands nil
190 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
192 (defvar nntp-have-messaged nil
)
194 (defvar nntp-process-wait-for nil
)
195 (defvar nntp-process-to-buffer nil
)
196 (defvar nntp-process-callback nil
)
197 (defvar nntp-process-decode nil
)
198 (defvar nntp-process-start-point nil
)
199 (defvar nntp-inside-change-function nil
)
200 (defvoo nntp-last-command-time nil
)
201 (defvoo nntp-last-command nil
)
202 (defvoo nntp-authinfo-password nil
)
203 (defvoo nntp-authinfo-user nil
)
205 (defvar nntp-connection-list nil
)
207 (defvoo nntp-server-type nil
)
208 (defvoo nntp-connection-alist nil
)
209 (defvoo nntp-status-string
"")
210 (defconst nntp-version
"nntp 5.0")
211 (defvoo nntp-inhibit-erase nil
)
212 (defvoo nntp-inhibit-output nil
)
214 (defvoo nntp-server-xover
'try
)
215 (defvoo nntp-server-list-active-group
'try
)
217 (defvar nntp-async-needs-kluge
218 (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
219 "*When non-nil, nntp will poll asynchronous connections
220 once a second. By default, this is turned on only for Emacs
221 20.3, which has a bug that breaks nntp's normal method of
222 noticing asynchronous data.")
224 (defvar nntp-async-timer nil
)
225 (defvar nntp-async-process-list nil
)
227 (defvar nntp-ssl-program
228 "openssl s_client -quiet -ssl3 -connect %s:%p"
229 "A string containing commands for SSL connections.
230 Within a string, %s is replaced with the server address and %p with
231 port number on server. The program should accept IMAP commands on
232 stdin and return responses to stdout.")
235 (autoload 'mail-source-read-passwd
"mail-source"))
239 ;;; Internal functions.
241 (defsubst nntp-send-string
(process string
)
242 "Send STRING to PROCESS."
243 ;; We need to store the time to provide timeouts, and
244 ;; to store the command so the we can replay the command
245 ;; if the server gives us an AUTHINFO challenge.
246 (setq nntp-last-command-time
(current-time)
247 nntp-last-command string
)
248 (when nntp-record-commands
249 (nntp-record-command string
))
250 (process-send-string process
(concat string nntp-end-of-line
)))
252 (defun nntp-record-command (string)
253 "Record the command STRING."
255 (set-buffer (get-buffer-create "*nntp-log*"))
256 (goto-char (point-max))
257 (let ((time (current-time)))
258 (insert (format-time-string "%Y%m%dT%H%M%S" time
)
259 "." (format "%03d" (/ (nth 2 time
) 1000))
260 " " nntp-address
" " string
"\n"))))
262 (defsubst nntp-wait-for
(process wait-for buffer
&optional decode discard
)
263 "Wait for WAIT-FOR to arrive from PROCESS."
265 (set-buffer (process-buffer process
))
266 (goto-char (point-min))
267 (while (and (or (not (memq (char-after (point)) '(?
2 ?
3 ?
4 ?
5)))
269 (memq (process-status process
) '(open run
)))
270 (when (looking-at "480")
271 (nntp-handle-authinfo process
))
272 (nntp-accept-process-output process
)
273 (goto-char (point-min)))
278 (nntp-snarf-error-message)
280 ((not (memq (process-status process
) '(open run
)))
281 (nnheader-report 'nntp
"Server closed connection"))
283 (goto-char (point-max))
284 (let ((limit (point-min)))
285 (while (not (re-search-backward wait-for limit t
))
286 (nntp-accept-process-output process
)
287 ;; We assume that whatever we wait for is less than 1000
289 (setq limit
(max (- (point-max) 1000) (point-min)))
290 (goto-char (point-max))))
291 (nntp-decode-text (not decode
))
295 (goto-char (point-max))
296 (insert-buffer-substring (process-buffer process
))
297 ;; Nix out "nntp reading...." message.
298 (when nntp-have-messaged
299 (setq nntp-have-messaged nil
)
300 (nnheader-message 5 ""))
305 (defun nntp-kill-buffer (buffer)
306 (when (buffer-name buffer
)
308 (nnheader-init-server-buffer)))
310 (defsubst nntp-find-connection
(buffer)
311 "Find the connection delivering to BUFFER."
312 (let ((alist nntp-connection-alist
)
313 (buffer (if (stringp buffer
) (get-buffer buffer
) buffer
))
315 (while (setq entry
(pop alist
))
316 (when (eq buffer
(cadr entry
))
317 (setq process
(car entry
)
320 (if (memq (process-status process
) '(open run
))
322 (nntp-kill-buffer (process-buffer process
))
323 (setq nntp-connection-alist
(delq entry nntp-connection-alist
))
326 (defsubst nntp-find-connection-entry
(buffer)
327 "Return the entry for the connection to BUFFER."
328 (assq (nntp-find-connection buffer
) nntp-connection-alist
))
330 (defun nntp-find-connection-buffer (buffer)
331 "Return the process connection buffer tied to BUFFER."
332 (let ((process (nntp-find-connection buffer
)))
334 (process-buffer process
))))
336 (defsubst nntp-retrieve-data
(command address port buffer
337 &optional wait-for callback decode
)
338 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
339 (let ((process (or (nntp-find-connection buffer
)
340 (nntp-open-connection buffer
))))
342 (nnheader-report 'nntp
"Couldn't open connection to %s" address
)
343 (unless (or nntp-inhibit-erase nnheader-callback-function
)
345 (set-buffer (process-buffer process
))
350 (nntp-send-string process command
))
352 ((eq callback
'ignore
)
354 ((and callback wait-for
)
355 (nntp-async-wait process wait-for buffer decode callback
)
358 (nntp-wait-for process wait-for buffer decode
))
361 (nnheader-report 'nntp
"Couldn't open connection to %s: %s"
364 (message "Quit retrieving data from nntp")
368 (defsubst nntp-send-command
(wait-for &rest strings
)
369 "Send STRINGS to server and wait until WAIT-FOR returns."
370 (when (and (not nnheader-callback-function
)
371 (not nntp-inhibit-output
))
373 (set-buffer nntp-server-buffer
)
376 (mapconcat 'identity strings
" ")
377 nntp-address nntp-port-number nntp-server-buffer
378 wait-for nnheader-callback-function
))
380 (defun nntp-send-command-nodelete (wait-for &rest strings
)
381 "Send STRINGS to server and wait until WAIT-FOR returns."
383 (mapconcat 'identity strings
" ")
384 nntp-address nntp-port-number nntp-server-buffer
385 wait-for nnheader-callback-function
))
387 (defun nntp-send-command-and-decode (wait-for &rest strings
)
388 "Send STRINGS to server and wait until WAIT-FOR returns."
389 (when (and (not nnheader-callback-function
)
390 (not nntp-inhibit-output
))
392 (set-buffer nntp-server-buffer
)
395 (mapconcat 'identity strings
" ")
396 nntp-address nntp-port-number nntp-server-buffer
397 wait-for nnheader-callback-function t
))
399 (defun nntp-send-buffer (wait-for)
400 "Send the current buffer to server and wait until WAIT-FOR returns."
401 (when (and (not nnheader-callback-function
)
402 (not nntp-inhibit-output
))
404 (set-buffer (nntp-find-connection-buffer nntp-server-buffer
))
407 (mm-with-unibyte-current-buffer
408 ;; Some encoded unicode text contains character 0x80-0x9f e.g. Euro.
409 (process-send-region (nntp-find-connection nntp-server-buffer
)
410 (point-min) (point-max)))
412 nil nntp-address nntp-port-number nntp-server-buffer
413 wait-for nnheader-callback-function
))
417 ;;; Interface functions.
419 (nnoo-define-basics nntp
)
421 (defsubst nntp-next-result-arrived-p
()
423 ;; A result that starts with a 2xx code is terminated by
424 ;; a line with only a "." on it.
425 ((eq (char-after) ?
2)
426 (if (re-search-forward "\n\\.\r?\n" nil t
)
429 ;; A result that starts with a 3xx or 4xx code is terminated
432 (if (search-forward "\n" nil t
)
439 (deffoo nntp-retrieve-headers
(articles &optional group server fetch-old
)
440 "Retrieve the headers of ARTICLES."
441 (nntp-possibly-change-group group server
)
443 (set-buffer (nntp-find-connection-buffer nntp-server-buffer
))
445 (if (and (not gnus-nov-is-evil
)
446 (not nntp-nov-is-evil
)
447 (nntp-retrieve-headers-with-xover articles fetch-old
))
448 ;; We successfully retrieved the headers via XOVER.
450 ;; XOVER didn't work, so we do it the hard, slow and inefficient
452 (let ((number (length articles
))
455 (last-point (point-min))
456 (buf (nntp-find-connection-buffer nntp-server-buffer
))
457 (nntp-inhibit-erase t
)
459 ;; Send HEAD commands.
460 (while (setq article
(pop articles
))
463 "HEAD" (if (numberp article
)
464 (int-to-string article
)
465 ;; `articles' is either a list of article numbers
466 ;; or a list of article IDs.
469 ;; Every 400 requests we have to read the stream in
470 ;; order to avoid deadlocks.
471 (when (or (null articles
) ;All requests have been sent.
472 (zerop (% count nntp-maximum-request
)))
473 (nntp-accept-response)
476 (goto-char last-point
)
478 (while (nntp-next-result-arrived-p)
479 (setq last-point
(point))
482 ;; If number of headers is greater than 100, give
483 ;; informative messages.
484 (and (numberp nntp-large-newsgroup
)
485 (> number nntp-large-newsgroup
)
486 (zerop (% received
20))
487 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
488 (/ (* received
100) number
)))
489 (nntp-accept-response))))
490 (and (numberp nntp-large-newsgroup
)
491 (> number nntp-large-newsgroup
)
492 (nnheader-message 6 "NNTP: Receiving headers...done"))
494 ;; Now all of replies are received. Fold continuation lines.
495 (nnheader-fold-continuation-lines)
496 ;; Remove all "\r"'s.
498 (copy-to-buffer nntp-server-buffer
(point-min) (point-max))
501 (deffoo nntp-retrieve-groups
(groups &optional server
)
502 "Retrieve group info on GROUPS."
503 (nntp-possibly-change-group nil server
)
504 (when (nntp-find-connection-buffer nntp-server-buffer
)
506 ;; Erase nntp-server-buffer before nntp-inhibit-erase.
507 (set-buffer nntp-server-buffer
)
509 (set-buffer (nntp-find-connection-buffer nntp-server-buffer
))
510 ;; The first time this is run, this variable is `try'. So we
512 (when (eq nntp-server-list-active-group
'try
)
513 (nntp-try-list-active (car groups
)))
517 (last-point (point-min))
518 (nntp-inhibit-erase t
)
519 (buf (nntp-find-connection-buffer nntp-server-buffer
))
520 (command (if nntp-server-list-active-group
"LIST ACTIVE" "GROUP")))
522 ;; Send the command to the server.
523 (nntp-send-command nil command
(pop groups
))
525 ;; Every 400 requests we have to read the stream in
526 ;; order to avoid deadlocks.
527 (when (or (null groups
) ;All requests have been sent.
528 (zerop (% count nntp-maximum-request
)))
529 (nntp-accept-response)
531 ;; Search `blue moon' in this file for the
532 ;; reason why set-buffer here.
534 (goto-char last-point
)
536 (while (re-search-forward "^[0-9]" nil t
)
538 (setq last-point
(point))
540 (nntp-accept-response))))
542 ;; Wait for the reply from the final command.
544 (goto-char (point-max))
545 (re-search-backward "^[0-9]" nil t
)
546 (when (looking-at "^[23]")
549 (goto-char (point-max))
550 (if (not nntp-server-list-active-group
)
551 (not (re-search-backward "\r?\n" (- (point) 3) t
))
552 (not (re-search-backward "^\\.\r?\n" (- (point) 4) t
))))
553 (nntp-accept-response)))
555 ;; Now all replies are received. We remove CRs.
557 (goto-char (point-min))
558 (while (search-forward "\r" nil t
)
559 (replace-match "" t t
))
561 (if (not nntp-server-list-active-group
)
563 (copy-to-buffer nntp-server-buffer
(point-min) (point-max))
565 ;; We have read active entries, so we just delete the
567 (goto-char (point-min))
568 (while (re-search-forward "^[.2-5]" nil t
)
569 (delete-region (match-beginning 0)
570 (progn (forward-line 1) (point))))
571 (copy-to-buffer nntp-server-buffer
(point-min) (point-max))
574 (deffoo nntp-retrieve-articles
(articles &optional group server
)
575 (nntp-possibly-change-group group server
)
577 (let ((number (length articles
))
580 (last-point (point-min))
581 (buf (nntp-find-connection-buffer nntp-server-buffer
))
582 (nntp-inhibit-erase t
)
583 (map (apply 'vector articles
))
588 ;; Send ARTICLE command.
589 (while (setq article
(pop articles
))
592 "ARTICLE" (if (numberp article
)
593 (int-to-string article
)
594 ;; `articles' is either a list of article numbers
595 ;; or a list of article IDs.
598 ;; Every 400 requests we have to read the stream in
599 ;; order to avoid deadlocks.
600 (when (or (null articles
) ;All requests have been sent.
601 (zerop (% count nntp-maximum-request
)))
602 (nntp-accept-response)
605 (goto-char last-point
)
607 (while (nntp-next-result-arrived-p)
608 (aset map received
(cons (aref map received
) (point)))
609 (setq last-point
(point))
612 ;; If number of headers is greater than 100, give
613 ;; informative messages.
614 (and (numberp nntp-large-newsgroup
)
615 (> number nntp-large-newsgroup
)
616 (zerop (% received
20))
617 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
618 (/ (* received
100) number
)))
619 (nntp-accept-response))))
620 (and (numberp nntp-large-newsgroup
)
621 (> number nntp-large-newsgroup
)
622 (nnheader-message 6 "NNTP: Receiving articles...done"))
624 ;; Now we have all the responses. We go through the results,
625 ;; wash it and copy it over to the server buffer.
626 (set-buffer nntp-server-buffer
)
628 (setq last-point
(point-min))
632 (setq point
(goto-char (point-max)))
634 (insert-buffer-substring buf last-point
(cdr entry
))
636 (setq last-point
(cdr entry
))
639 (cons (car entry
) point
))
642 (defun nntp-try-list-active (group)
643 (nntp-list-active-group group
)
645 (set-buffer nntp-server-buffer
)
646 (goto-char (point-min))
648 (looking-at "5[0-9]+"))
649 (setq nntp-server-list-active-group nil
))
651 (setq nntp-server-list-active-group t
)))))
653 (deffoo nntp-list-active-group
(group &optional server
)
654 "Return the active info on GROUP (which can be a regexp)."
655 (nntp-possibly-change-group nil server
)
656 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group
))
658 (deffoo nntp-request-group-articles
(group &optional server
)
659 "Return the list of existing articles in GROUP."
660 (nntp-possibly-change-group nil server
)
661 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group
))
663 (deffoo nntp-request-article
(article &optional group server buffer command
)
664 (nntp-possibly-change-group group server
)
665 (when (nntp-send-command-and-decode
666 "\r?\n\\.\r?\n" "ARTICLE"
667 (if (numberp article
) (int-to-string article
) article
))
669 (not (equal buffer nntp-server-buffer
)))
671 (set-buffer nntp-server-buffer
)
672 (copy-to-buffer buffer
(point-min) (point-max))
673 (nntp-find-group-and-number))
674 (nntp-find-group-and-number))))
676 (deffoo nntp-request-head
(article &optional group server
)
677 (nntp-possibly-change-group group server
)
678 (when (nntp-send-command
679 "\r?\n\\.\r?\n" "HEAD"
680 (if (numberp article
) (int-to-string article
) article
))
682 (nntp-find-group-and-number)
683 (nntp-decode-text))))
685 (deffoo nntp-request-body
(article &optional group server
)
686 (nntp-possibly-change-group group server
)
687 (nntp-send-command-and-decode
688 "\r?\n\\.\r?\n" "BODY"
689 (if (numberp article
) (int-to-string article
) article
)))
691 (deffoo nntp-request-group
(group &optional server dont-check
)
692 (nntp-possibly-change-group nil server
)
693 (when (nntp-send-command "^[245].*\n" "GROUP" group
)
694 (let ((entry (nntp-find-connection-entry nntp-server-buffer
)))
695 (setcar (cddr entry
) group
))))
697 (deffoo nntp-close-group
(group &optional server
)
700 (deffoo nntp-server-opened
(&optional server
)
701 "Say whether a connection to SERVER has been opened."
702 (and (nnoo-current-server-p 'nntp server
)
704 (gnus-buffer-live-p nntp-server-buffer
)
705 (nntp-find-connection nntp-server-buffer
)))
707 (deffoo nntp-open-server
(server &optional defs connectionless
)
708 (nnheader-init-server-buffer)
709 (if (nntp-server-opened server
)
711 (when (or (stringp (car defs
))
712 (numberp (car defs
)))
713 (setq defs
(cons (list 'nntp-port-number
(car defs
)) (cdr defs
))))
714 (unless (assq 'nntp-address defs
)
715 (setq defs
(append defs
(list (list 'nntp-address server
)))))
716 (nnoo-change-server 'nntp server defs
)
717 (unless connectionless
718 (or (nntp-find-connection nntp-server-buffer
)
719 (nntp-open-connection nntp-server-buffer
)))))
721 (deffoo nntp-close-server
(&optional server
)
722 (nntp-possibly-change-group nil server t
)
723 (let ((process (nntp-find-connection nntp-server-buffer
)))
725 (when (memq (process-status process
) '(open run
))
727 (nntp-send-string process
"QUIT")
728 (unless (eq nntp-open-connection-function
'nntp-open-network-stream
)
729 ;; Ok, this is evil, but when using telnet and stuff
730 ;; as the connection method, it's important that the
731 ;; QUIT command actually is sent out before we kill
734 (nntp-kill-buffer (process-buffer process
))
735 (setq process
(car (pop nntp-connection-alist
))))
736 (nnoo-close-server 'nntp
)))
738 (deffoo nntp-request-close
()
740 (while (setq process
(pop nntp-connection-list
))
741 (when (memq (process-status process
) '(open run
))
743 (nntp-send-string process
"QUIT")
744 (unless (eq nntp-open-connection-function
'nntp-open-network-stream
)
745 ;; Ok, this is evil, but when using telnet and stuff
746 ;; as the connection method, it's important that the
747 ;; QUIT command actually is sent out before we kill
750 (nntp-kill-buffer (process-buffer process
)))))
752 (deffoo nntp-request-list
(&optional server
)
753 (nntp-possibly-change-group nil server
)
754 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST"))
756 (deffoo nntp-request-list-newsgroups
(&optional server
)
757 (nntp-possibly-change-group nil server
)
758 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS"))
760 (deffoo nntp-request-newgroups
(date &optional server
)
761 (nntp-possibly-change-group nil server
)
763 (set-buffer nntp-server-buffer
)
764 (let* ((time (date-to-time date
))
765 (ls (- (cadr time
) (nth 8 (decode-time time
)))))
767 (setcar time
(1- (car time
)))
768 (setcar (cdr time
) (+ ls
65536)))
770 (setcar time
(1+ (car time
)))
771 (setcar (cdr time
) (- ls
65536)))
773 (setcar (cdr time
) ls
)))
776 "^\\.\r?\n" "NEWGROUPS"
777 (format-time-string "%y%m%d %H%M%S" time
)
779 (nntp-decode-text)))))
781 (deffoo nntp-request-post
(&optional server
)
782 (nntp-possibly-change-group nil server
)
783 (when (nntp-send-command "^[23].*\r?\n" "POST")
784 (nntp-send-buffer "^[23].*\n")))
786 (deffoo nntp-request-type
(group article
)
789 (deffoo nntp-asynchronous-p
()
794 (defun nntp-send-mode-reader ()
795 "Send the MODE READER command to the nntp server.
796 This function is supposed to be called from `nntp-server-opened-hook'.
797 It will make innd servers spawn an nnrpd process to allow actual article
799 (nntp-send-command "^.*\n" "MODE READER"))
801 (defun nntp-send-authinfo (&optional send-if-force
)
802 "Send the AUTHINFO to the nntp server.
803 It will look in the \"~/.authinfo\" file for matching entries. If
804 nothing suitable is found there, it will prompt for a user name
807 If SEND-IF-FORCE, only send authinfo to the server if the
808 .authinfo file has the FORCE token."
809 (let* ((list (gnus-parse-netrc nntp-authinfo-file
))
810 (alist (gnus-netrc-machine list nntp-address
"nntp"))
811 (force (gnus-netrc-get alist
"force"))
812 (user (or (gnus-netrc-get alist
"login") nntp-authinfo-user
))
813 (passwd (gnus-netrc-get alist
"password")))
814 (when (or (not send-if-force
)
817 (setq user
(read-string (format "NNTP (%s) user name: " nntp-address
))
818 nntp-authinfo-user user
))
819 (unless (member user
'(nil ""))
820 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user
)
821 (when t
;???Should check if AUTHINFO succeeded
823 "^2.*\r?\n" "AUTHINFO PASS"
825 nntp-authinfo-password
826 (setq nntp-authinfo-password
827 (mail-source-read-passwd
828 (format "NNTP (%s@%s) password: "
829 user nntp-address
))))))))))
831 (defun nntp-send-nosy-authinfo ()
832 "Send the AUTHINFO to the nntp server."
833 (let ((user (read-string (format "NNTP (%s) user name: " nntp-address
))))
834 (unless (member user
'(nil ""))
835 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user
)
836 (when t
;???Should check if AUTHINFO succeeded
837 (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
838 (mail-source-read-passwd "NNTP (%s@%s) password: "
839 user nntp-address
))))))
841 (defun nntp-send-authinfo-from-file ()
842 "Send the AUTHINFO to the nntp server.
844 The authinfo login name is taken from the user's login name and the
845 password contained in '~/.nntp-authinfo'."
846 (when (file-exists-p "~/.nntp-authinfo")
848 (insert-file-contents "~/.nntp-authinfo")
849 (goto-char (point-min))
850 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
852 "^2.*\r?\n" "AUTHINFO PASS"
853 (buffer-substring (point) (progn (end-of-line) (point)))))))
855 ;;; Internal functions.
857 (defun nntp-handle-authinfo (process)
858 "Take care of an authinfo response from the server."
859 (let ((last nntp-last-command
))
860 (funcall nntp-authinfo-function
)
861 ;; We have to re-send the function that was interrupted by
862 ;; the authinfo request.
864 (set-buffer nntp-server-buffer
)
866 (nntp-send-string process last
)))
868 (defun nntp-make-process-buffer (buffer)
869 "Create a new, fresh buffer usable for nntp process connections."
873 (format " *server %s %s %s*"
874 nntp-address nntp-port-number
875 (gnus-buffer-exists-p buffer
))))
876 (mm-enable-multibyte)
877 (set (make-local-variable 'after-change-functions
) nil
)
878 (set (make-local-variable 'nntp-process-wait-for
) nil
)
879 (set (make-local-variable 'nntp-process-callback
) nil
)
880 (set (make-local-variable 'nntp-process-to-buffer
) nil
)
881 (set (make-local-variable 'nntp-process-start-point
) nil
)
882 (set (make-local-variable 'nntp-process-decode
) nil
)
885 (defun nntp-open-connection (buffer)
886 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
887 (run-hooks 'nntp-prepare-server-hook
)
888 (let* ((pbuffer (nntp-make-process-buffer buffer
))
890 (and nntp-connection-timeout
891 (nnheader-run-at-time
892 nntp-connection-timeout nil
894 (nntp-kill-buffer ,pbuffer
)))))
897 (let ((coding-system-for-read nntp-coding-system-for-read
)
898 (coding-system-for-write nntp-coding-system-for-write
))
899 (funcall nntp-open-connection-function pbuffer
))
902 (message "Quit opening connection")
903 (nntp-kill-buffer pbuffer
)
907 (nnheader-cancel-timer timer
))
908 (when (and (buffer-name pbuffer
)
910 (process-kill-without-query process
)
911 (nntp-wait-for process
"^.*\n" buffer nil t
)
912 (if (memq (process-status process
) '(open run
))
914 (caar (push (list process buffer nil
) nntp-connection-alist
))
915 (push process nntp-connection-list
)
918 (nntp-read-server-type)
920 (set-buffer nntp-server-buffer
)
921 (let ((nnheader-callback-function nil
))
922 (run-hooks 'nntp-server-opened-hook
)
923 (nntp-send-authinfo t
))))
924 (nntp-kill-buffer (process-buffer process
))
927 (defun nntp-open-network-stream (buffer)
928 (open-network-stream "nntpd" buffer nntp-address nntp-port-number
))
930 (defun nntp-open-ssl-stream (buffer)
931 (let* ((process-connection-type nil
)
932 (proc (start-process "nntpd" buffer
935 (format-spec nntp-ssl-program
938 ?p nntp-port-number
)))))
939 (process-kill-without-query proc
)
942 (nntp-wait-for-string "^\r*20[01]")
944 (delete-region (point-min) (point))
947 (defun nntp-read-server-type ()
948 "Find out what the name of the server we have connected to is."
949 ;; Wait for the status string to arrive.
950 (setq nntp-server-type
(buffer-string))
951 (let ((alist nntp-server-action-alist
)
954 ;; Run server-specific commands.
956 (setq entry
(pop alist
))
957 (when (string-match (car entry
) nntp-server-type
)
958 (if (and (listp (cadr entry
))
959 (not (eq 'lambda
(caadr entry
))))
961 (funcall (cadr entry
)))))))
963 (defun nntp-async-wait (process wait-for buffer decode callback
)
965 (set-buffer (process-buffer process
))
966 (unless nntp-inside-change-function
968 (setq nntp-process-wait-for wait-for
969 nntp-process-to-buffer buffer
970 nntp-process-decode decode
971 nntp-process-callback callback
972 nntp-process-start-point
(point-max))
973 (setq after-change-functions
'(nntp-after-change-function))
974 (if nntp-async-needs-kluge
975 (nntp-async-kluge process
))))
977 (defun nntp-async-kluge (process)
978 ;; emacs 20.3 bug: process output with encoding 'binary
979 ;; doesn't trigger after-change-functions.
980 (unless nntp-async-timer
981 (setq nntp-async-timer
982 (nnheader-run-at-time 1 1 'nntp-async-timer-handler
)))
983 (add-to-list 'nntp-async-process-list process
))
985 (defun nntp-async-timer-handler ()
988 (if (memq (process-status proc
) '(open run
))
989 (nntp-async-trigger proc
)
990 (nntp-async-stop proc
)))
991 nntp-async-process-list
))
993 (defun nntp-async-stop (proc)
994 (setq nntp-async-process-list
(delq proc nntp-async-process-list
))
995 (when (and nntp-async-timer
(not nntp-async-process-list
))
996 (nnheader-cancel-timer nntp-async-timer
)
997 (setq nntp-async-timer nil
)))
999 (defun nntp-after-change-function (beg end len
)
1001 ;; we only care about insertions at eob
1002 (when (and (eq 0 len
) (eq (point-max) end
))
1004 (let ((proc (get-buffer-process (current-buffer))))
1006 (nntp-async-trigger proc
)))))
1007 ;; any throw from after-change-functions will leave it
1008 ;; set to nil. so we reset it here, if necessary.
1010 (setq after-change-functions
'(nntp-after-change-function)))))
1012 (defun nntp-async-trigger (process)
1014 (set-buffer (process-buffer process
))
1015 (when nntp-process-callback
1016 ;; do we have an error message?
1017 (goto-char nntp-process-start-point
)
1018 (if (memq (following-char) '(?
4 ?
5))
1019 ;; wants credentials?
1020 (if (looking-at "480")
1021 (nntp-handle-authinfo process
)
1022 ;; report error message.
1023 (nntp-snarf-error-message)
1024 (nntp-do-callback nil
))
1026 ;; got what we expect?
1027 (goto-char (point-max))
1028 (when (re-search-backward
1029 nntp-process-wait-for nntp-process-start-point t
)
1030 (nntp-async-stop process
)
1032 (when (gnus-buffer-exists-p nntp-process-to-buffer
)
1033 (let ((buf (current-buffer))
1034 (start nntp-process-start-point
)
1035 (decode nntp-process-decode
))
1037 (set-buffer nntp-process-to-buffer
)
1038 (goto-char (point-max))
1040 (narrow-to-region (point) (point))
1041 (insert-buffer-substring buf start
)
1043 (nntp-decode-text))))))
1045 (goto-char (point-max))
1047 (buffer-name (get-buffer nntp-process-to-buffer
))))))))
1049 (defun nntp-do-callback (arg)
1050 (let ((callback nntp-process-callback
)
1051 (nntp-inside-change-function t
))
1052 (setq nntp-process-callback nil
)
1053 (funcall callback arg
)))
1055 (defun nntp-snarf-error-message ()
1056 "Save the error message in the current buffer."
1057 (let ((message (buffer-string)))
1058 (while (string-match "[\r\n]+" message
)
1059 (setq message
(replace-match " " t t message
)))
1060 (nnheader-report 'nntp message
)
1063 (defun nntp-accept-process-output (process &optional timeout
)
1064 "Wait for output from PROCESS and message some dots."
1066 (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer
)
1067 nntp-server-buffer
))
1068 (let ((len (/ (buffer-size) 1024))
1071 (setq nntp-have-messaged t
)
1072 (nnheader-message 7 "nntp read: %dk" len
)))
1073 (accept-process-output process
(or timeout
1))))
1075 (defun nntp-accept-response ()
1076 "Wait for output from the process that outputs to BUFFER."
1077 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer
)))
1079 (defun nntp-possibly-change-group (group server
&optional connectionless
)
1080 (let ((nnheader-callback-function nil
))
1082 (or (nntp-server-opened server
)
1083 (nntp-open-server server nil connectionless
)))
1085 (unless connectionless
1086 (or (nntp-find-connection nntp-server-buffer
)
1087 (nntp-open-connection nntp-server-buffer
))))
1090 (let ((entry (nntp-find-connection-entry nntp-server-buffer
)))
1091 (when (not (equal group
(caddr entry
)))
1093 (set-buffer (process-buffer (car entry
)))
1095 (nntp-send-command "^[245].*\n" "GROUP" group
)
1096 (setcar (cddr entry
) group
)
1099 (defun nntp-decode-text (&optional cr-only
)
1100 "Decode the text in the current buffer."
1101 (goto-char (point-min))
1102 (while (search-forward "\r" nil t
)
1105 ;; Remove trailing ".\n" end-of-transfer marker.
1106 (goto-char (point-max))
1108 (when (looking-at ".\n")
1110 ;; Delete status line.
1111 (goto-char (point-min))
1112 (while (looking-at "[1-5][0-9][0-9] .*\n")
1113 ;; For some unknown reason, there is more than one status line.
1114 (delete-region (point) (progn (forward-line 1) (point))))
1115 ;; Remove "." -> ".." encoding.
1116 (while (search-forward "\n.." nil t
)
1119 (defun nntp-encode-text ()
1120 "Encode the text in the current buffer."
1122 ;; Replace "." at beginning of line with "..".
1123 (goto-char (point-min))
1124 (while (re-search-forward "^\\." nil t
)
1126 (goto-char (point-max))
1127 ;; Insert newline at the end of the buffer.
1130 ;; Insert `.' at end of buffer (end of text mark).
1131 (goto-char (point-max))
1133 (goto-char (point-min))
1137 (insert nntp-end-of-line
))))
1139 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old
)
1140 (set-buffer nntp-server-buffer
)
1144 ;; This server does not talk NOV.
1145 ((not nntp-server-xover
)
1148 ;; We don't care about gaps.
1149 ((or (not nntp-nov-gap
)
1151 (nntp-send-xover-command
1153 (if (numberp fetch-old
)
1154 (max 1 (- (car articles
) fetch-old
))
1157 (car (last articles
)) 'wait
)
1159 (goto-char (point-min))
1160 (when (looking-at "[1-5][0-9][0-9] .*\n")
1161 (delete-region (point) (progn (forward-line 1) (point))))
1162 (while (search-forward "\r" nil t
)
1163 (replace-match "" t t
))
1164 (goto-char (point-max))
1166 (when (looking-at "\\.")
1167 (delete-region (point) (progn (forward-line 1) (point)))))
1169 ;; We do it the hard way. For each gap, an XOVER command is sent
1170 ;; to the server. We do not wait for a reply from the server, we
1171 ;; just send them off as fast as we can. That means that we have
1172 ;; to count the number of responses we get back to find out when we
1173 ;; have gotten all we asked for.
1174 ((numberp nntp-nov-gap
)
1179 (buf nntp-server-buffer
)
1180 (process-buffer (nntp-find-connection-buffer nntp-server-buffer
))
1182 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1183 ;; that means that the server does not understand XOVER, but we
1184 ;; won't know that until we try.
1185 (while (and nntp-server-xover articles
)
1186 (setq first
(car articles
))
1187 ;; Search forward until we find a gap, or until we run out of
1189 (while (and (cdr articles
)
1190 (< (- (nth 1 articles
) (car articles
)) nntp-nov-gap
))
1191 (setq articles
(cdr articles
)))
1193 (setq in-process-buffer-p
(stringp nntp-server-xover
))
1194 (nntp-send-xover-command first
(car articles
))
1195 (setq articles
(cdr articles
))
1197 (when (and nntp-server-xover in-process-buffer-p
)
1198 ;; Don't count tried request.
1199 (setq count
(1+ count
))
1201 ;; Every 400 requests we have to read the stream in
1202 ;; order to avoid deadlocks.
1203 (when (or (null articles
) ;All requests have been sent.
1204 (zerop (% count nntp-maximum-request
)))
1206 (nntp-accept-response)
1207 ;; On some Emacs versions the preceding function has a
1208 ;; tendency to change the buffer. Perhaps. It's quite
1209 ;; difficult to reproduce, because it only seems to happen
1210 ;; once in a blue moon.
1211 (set-buffer process-buffer
)
1213 (goto-char (or last-point
(point-min)))
1215 (while (re-search-forward "^[0-9][0-9][0-9] .*\n" nil t
)
1217 (setq last-point
(point))
1219 (nntp-accept-response)
1220 (set-buffer process-buffer
))
1223 (when nntp-server-xover
1224 (when in-process-buffer-p
1225 (set-buffer process-buffer
)
1226 ;; Wait for the reply from the final command.
1227 (goto-char (point-max))
1228 (while (not (re-search-backward "^[0-9][0-9][0-9] " nil t
))
1229 (nntp-accept-response)
1230 (set-buffer process-buffer
)
1231 (goto-char (point-max)))
1232 (when (looking-at "^[23]")
1234 (goto-char (point-max))
1236 (not (looking-at "^\\.\r?\n")))
1237 (nntp-accept-response)
1238 (set-buffer process-buffer
)))
1240 (goto-char (point-max))
1241 (insert-buffer-substring process-buffer
)
1242 (set-buffer process-buffer
)
1246 ;; We remove any "." lines and status lines.
1247 (goto-char (point-min))
1248 (while (search-forward "\r" nil t
)
1250 (goto-char (point-min))
1251 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1256 (defun nntp-send-xover-command (beg end
&optional wait-for-reply
)
1257 "Send the XOVER command to the server."
1258 (let ((range (format "%d-%d" beg end
))
1259 (nntp-inhibit-erase t
))
1260 (if (stringp nntp-server-xover
)
1261 ;; If `nntp-server-xover' is a string, then we just send this
1264 (nntp-send-command-nodelete
1265 "\r?\n\\.\r?\n" nntp-server-xover range
)
1266 ;; We do not wait for the reply.
1267 (nntp-send-command-nodelete nil nntp-server-xover range
))
1268 (let ((commands nntp-xover-commands
))
1269 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1270 ;; We try them all until we get at positive response.
1271 (while (and commands
(eq nntp-server-xover
'try
))
1272 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands
) range
)
1274 (set-buffer nntp-server-buffer
)
1275 (goto-char (point-min))
1276 (and (looking-at "[23]") ; No error message.
1277 ;; We also have to look at the lines. Some buggy
1278 ;; servers give back simple lines with just the
1279 ;; article number. How... helpful.
1282 (looking-at "[0-9]+\t...")) ; More text after number.
1283 (setq nntp-server-xover
(car commands
))))
1284 (setq commands
(cdr commands
)))
1285 ;; If none of the commands worked, we disable XOVER.
1286 (when (eq nntp-server-xover
'try
)
1288 (set-buffer nntp-server-buffer
)
1290 (setq nntp-server-xover nil
)))
1291 nntp-server-xover
))))
1293 ;;; Alternative connection methods.
1295 (defun nntp-wait-for-string (regexp)
1296 "Wait until string arrives in the buffer."
1297 (let ((buf (current-buffer)))
1298 (goto-char (point-min))
1299 (while (not (re-search-forward regexp nil t
))
1300 (accept-process-output (nntp-find-connection nntp-server-buffer
))
1302 (goto-char (point-min)))))
1304 (defun nntp-open-telnet (buffer)
1310 "nntpd" buffer nntp-telnet-command nntp-telnet-switches
))
1311 (case-fold-search t
))
1312 (when (memq (process-status proc
) '(open run
))
1313 (nntp-wait-for-string "^r?telnet")
1314 (process-send-string proc
"set escape \^X\n")
1316 ((and nntp-open-telnet-envuser nntp-telnet-user-name
)
1317 (process-send-string proc
(concat "open " "-l" nntp-telnet-user-name
1318 nntp-address
"\n")))
1320 (process-send-string proc
(concat "open " nntp-address
"\n"))))
1322 ((not nntp-open-telnet-envuser
)
1323 (nntp-wait-for-string "^\r*.?login:")
1324 (process-send-string
1326 (or nntp-telnet-user-name
1327 (setq nntp-telnet-user-name
(read-string "login: ")))
1329 (nntp-wait-for-string "^\r*.?password:")
1330 (process-send-string
1332 (or nntp-telnet-passwd
1333 (setq nntp-telnet-passwd
1334 (mail-source-read-passwd "Password: ")))
1336 (nntp-wait-for-string nntp-telnet-shell-prompt
)
1337 (process-send-string
1338 proc
(concat (mapconcat 'identity nntp-telnet-parameters
" ") "\n"))
1339 (nntp-wait-for-string "^\r*20[01]")
1341 (delete-region (point-min) (point))
1342 (process-send-string proc
"\^]")
1343 (nntp-wait-for-string "^r?telnet")
1344 (process-send-string proc
"mode character\n")
1345 (accept-process-output proc
1)
1347 (goto-char (point-min))
1349 (delete-region (point) (point-max)))
1352 (defun nntp-open-rlogin (buffer)
1353 "Open a connection to SERVER using rsh."
1354 (let ((proc (if nntp-rlogin-user-name
1355 (apply 'start-process
1356 "nntpd" buffer nntp-rlogin-program
1357 nntp-address
"-l" nntp-rlogin-user-name
1358 nntp-rlogin-parameters
)
1359 (apply 'start-process
1360 "nntpd" buffer nntp-rlogin-program nntp-address
1361 nntp-rlogin-parameters
))))
1364 (nntp-wait-for-string "^\r*20[01]")
1366 (delete-region (point-min) (point))
1369 (defun nntp-find-group-and-number ()
1372 (set-buffer nntp-server-buffer
)
1373 (narrow-to-region (goto-char (point-min))
1374 (or (search-forward "\n\n" nil t
) (point-max)))
1375 (goto-char (point-min))
1376 ;; We first find the number by looking at the status line.
1377 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1379 (buffer-substring (match-beginning 1)
1381 group newsgroups xref
)
1382 (and number
(zerop number
) (setq number nil
))
1383 ;; Then we find the group name.
1386 ;; If there is only one group in the Newsgroups header,
1387 ;; then it seems quite likely that this article comes
1388 ;; from that group, I'd say.
1389 ((and (setq newsgroups
(mail-fetch-field "newsgroups"))
1390 (not (string-match "," newsgroups
)))
1392 ;; If there is more than one group in the Newsgroups
1393 ;; header, then the Xref header should be filled out.
1394 ;; We hazard a guess that the group that has this
1395 ;; article number in the Xref header is the one we are
1396 ;; looking for. This might very well be wrong if this
1397 ;; article happens to have the same number in several
1398 ;; groups, but that's life.
1399 ((and (setq xref
(mail-fetch-field "xref"))
1401 (string-match (format "\\([^ :]+\\):%d" number
) xref
))
1402 (substring xref
(match-beginning 1) (match-end 1)))
1404 (when (string-match "\r" group
)
1405 (setq group
(substring group
0 (match-beginning 0))))
1406 (cons group number
)))))
1410 ;;; arch-tag: 8655466a-b1b5-4929-9c45-7b1b2e767271
1411 ;;; nntp.el ends here