1 ;;; nntp.el --- nntp access for Gnus
2 ;; Copyright (C) 1987,88,89,90,92,93,94,95,96 Free Software Foundation, Inc.
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
33 (eval-when-compile (require 'cl))
38 (unless (fboundp 'open-network-stream)
41 (eval-when-compile (require 'cl))
44 (autoload 'news-setup "rnewspost")
45 (autoload 'news-reply-mode "rnewspost")
46 (autoload 'cancel-timer "timer")
47 (autoload 'telnet "telnet" nil t)
48 (autoload 'telnet-send-input "telnet" nil t)
49 (autoload 'timezone-parse-date "timezone"))
51 (defvoo nntp-server-hook nil
52 "*Hooks for the NNTP server.
53 If the kanji code of the NNTP server is different from the local kanji
54 code, the correct kanji code of the buffer associated with the NNTP
55 server must be specified as follows:
57 \(setq nntp-server-hook
60 ;; Server's Kanji code is EUC (NEmacs hack).
61 (make-local-variable 'kanji-fileio-code)
62 (setq kanji-fileio-code 0))))
64 If you'd like to change something depending on the server in this
65 hook, use the variable `nntp-address'.")
67 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
68 "*Hook used for sending commands to the server at startup.
69 The default value is `nntp-send-mode-reader', which makes an innd
70 server spawn an nnrpd server. Another useful function to put in this
71 hook might be `nntp-send-authinfo', which will prompt for a password
72 to allow posting from the server. Note that this is only necessary to
73 do on servers that use strict access control.")
74 (add-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)
76 (defvoo nntp-server-action-alist
77 '(("nntpd 1\\.5\\.11t"
78 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)))
79 "Alist of regexps to match on server types and actions to be taken.
80 For instance, if you want Gnus to beep every time you connect
81 to innd, you could say something like:
83 \(setq nntp-server-action-alist
86 You probably don't want to do that, though.")
88 (defvoo nntp-open-server-function 'nntp-open-network-stream
89 "*Function used for connecting to a remote system.
90 It will be called with the address of the remote system.
92 Two pre-made functions are `nntp-open-network-stream', which is the
93 default, and simply connects to some port or other on the remote
94 system (see nntp-port-number). The other is `nntp-open-rlogin', which
95 does an rlogin on the remote system, and then does a telnet to the
96 NNTP server available there (see nntp-rlogin-parameters).")
98 (defvoo nntp-rlogin-parameters '("telnet" "${NNTPSERVER:=localhost}" "nntp")
99 "*Parameters to `nntp-open-login'.
100 That function may be used as `nntp-open-server-function'. In that
101 case, this list will be used as the parameter list given to rsh.")
103 (defvoo nntp-rlogin-user-name nil
104 "*User name on remote system when using the rlogin connect method.")
106 (defvoo nntp-address nil
107 "*The name of the NNTP server.")
109 (defvoo nntp-port-number "nntp"
110 "*Port number to connect to.")
112 (defvoo nntp-end-of-line "\r\n"
113 "String to use on the end of lines when talking to the NNTP server.
114 This is \"\\r\\n\" by default, but should be \"\\n\" when
115 using rlogin to communicate with the server.")
117 (defvoo nntp-large-newsgroup 50
118 "*The number of the articles which indicates a large newsgroup.
119 If the number of the articles is greater than the value, verbose
120 messages will be shown to indicate the current status.")
122 (defvoo nntp-buggy-select (memq system-type '(fujitsu-uts))
123 "*t if your select routine is buggy.
124 If the select routine signals error or fall into infinite loop while
125 waiting for the server response, the variable must be set to t. In
126 case of Fujitsu UTS, it is set to T since `accept-process-output'
127 doesn't work properly.")
129 (defvoo nntp-maximum-request 400
130 "*The maximum number of the requests sent to the NNTP server at one time.
131 If Emacs hangs up while retrieving headers, set the variable to a
134 (defvoo nntp-debug-read 10000
135 "*Display '...' every 10Kbytes of a message being received if it is non-nil.
136 If it is a number, dots are displayed per the number.")
138 (defvoo nntp-nov-is-evil nil
139 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
141 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
142 "*List of strings that are used as commands to fetch NOV lines from a server.
143 The strings are tried in turn until a positive response is gotten. If
144 none of the commands are successful, nntp will just grab headers one
147 (defvoo nntp-nov-gap 20
148 "*Maximum allowed gap between two articles.
149 If the gap between two consecutive articles is bigger than this
150 variable, split the XOVER request into two requests.")
152 (defvoo nntp-connection-timeout nil
153 "*Number of seconds to wait before an nntp connection times out.
154 If this variable is nil, which is the default, no timers are set.")
156 (defvoo nntp-command-timeout nil
157 "*Number of seconds to wait for a response when sending a command.
158 If this variable is nil, which is the default, no timers are set.")
160 (defvoo nntp-retry-on-break nil
161 "*If non-nil, re-send the command when the user types `C-g'.")
163 (defvoo nntp-news-default-headers nil
164 "*If non-nil, override `mail-default-headers' when posting news.")
166 (defvoo nntp-prepare-server-hook nil
167 "*Hook run before a server is opened.
168 If can be used to set up a server remotely, for instance. Say you
169 have an account at the machine \"other.machine\". This machine has
170 access to an NNTP server that you can't access locally. You could
171 then use this hook to rsh to the remote machine and start a proxy NNTP
172 server there that you can connect to.")
174 (defvoo nntp-async-number 5
175 "*How many articles should be prefetched when in asynchronous mode.")
177 (defvoo nntp-warn-about-losing-connection t
178 "*If non-nil, beep when a server closes connection.")
182 (defconst nntp-version "nntp 4.0"
183 "Version numbers of this version of NNTP.")
185 (defvar nntp-server-buffer nil
186 "Buffer associated with the NNTP server process.")
188 (defvoo nntp-server-process nil
189 "The NNTP server process.
190 You'd better not use this variable in NNTP front-end program, but
191 instead use `nntp-server-buffer'.")
193 (defvoo nntp-status-string nil
194 "Save the server response message.")
196 (defvar nntp-opened-connections nil
197 "All (possibly) opened connections.")
199 (defvoo nntp-server-xover 'try)
200 (defvoo nntp-server-list-active-group 'try)
201 (defvoo nntp-current-group "")
202 (defvoo nntp-server-type nil)
204 (defvoo nntp-async-process nil)
205 (defvoo nntp-async-buffer nil)
206 (defvoo nntp-async-articles nil)
207 (defvoo nntp-async-fetched nil)
208 (defvoo nntp-async-group-alist nil)
211 ;;; Interface functions.
213 (nnoo-define-basics nntp)
215 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
216 "Retrieve the headers of ARTICLES."
217 (nntp-possibly-change-server group server)
219 (set-buffer nntp-server-buffer)
221 (if (and (not gnus-nov-is-evil)
222 (not nntp-nov-is-evil)
223 (nntp-retrieve-headers-with-xover articles fetch-old))
224 ;; We successfully retrieved the headers via XOVER.
226 ;; XOVER didn't work, so we do it the hard, slow and inefficient
228 (let ((number (length articles))
231 (message-log-max nil)
232 (last-point (point-min)))
233 ;; Send HEAD command.
235 (nntp-send-strings-to-server
236 "HEAD" (if (numberp (car articles))
237 (int-to-string (car articles))
238 ;; `articles' is either a list of article numbers
239 ;; or a list of article IDs.
241 (setq articles (cdr articles)
243 ;; Every 400 header requests we have to read the stream in
244 ;; order to avoid deadlocks.
245 (when (or (null articles) ;All requests have been sent.
246 (zerop (% count nntp-maximum-request)))
247 (nntp-accept-response)
249 (goto-char last-point)
251 (while (re-search-forward "^[0-9]" nil t)
252 (setq received (1+ received)))
253 (setq last-point (point))
255 ;; If number of headers is greater than 100, give
256 ;; informative messages.
257 (and (numberp nntp-large-newsgroup)
258 (> number nntp-large-newsgroup)
259 (zerop (% received 20))
260 (nnheader-message 7 "NNTP: Receiving headers... %d%%"
261 (/ (* received 100) number)))
262 (nntp-accept-response))))
263 ;; Wait for text of last command.
264 (goto-char (point-max))
265 (re-search-backward "^[0-9]" nil t)
266 (when (looking-at "^[23]")
268 (goto-char (- (point-max) 3))
269 (not (looking-at "^\\.\r?\n")))
270 (nntp-accept-response)))
271 (and (numberp nntp-large-newsgroup)
272 (> number nntp-large-newsgroup)
273 (nnheader-message 7 "NNTP: Receiving headers...done"))
275 ;; Now all of replies are received. Fold continuation lines.
276 (nnheader-fold-continuation-lines)
277 ;; Remove all "\r"'s.
278 (goto-char (point-min))
279 (while (search-forward "\r" nil t)
280 (replace-match "" t t))
284 (deffoo nntp-retrieve-groups (groups &optional server)
285 "Retrieve group info on GROUPS."
286 (nntp-possibly-change-server nil server)
288 (set-buffer nntp-server-buffer)
289 ;; The first time this is run, this variable is `try'. So we
291 (when (eq nntp-server-list-active-group 'try)
292 (nntp-try-list-active (car groups)))
296 (last-point (point-min))
297 (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
299 ;; Send the command to the server.
300 (nntp-send-strings-to-server command (car groups))
301 (setq groups (cdr groups))
302 (setq count (1+ count))
303 ;; Every 400 requests we have to read the stream in
304 ;; order to avoid deadlocks.
305 (when (or (null groups) ;All requests have been sent.
306 (zerop (% count nntp-maximum-request)))
307 (nntp-accept-response)
309 (goto-char last-point)
311 (while (re-search-forward "^[0-9]" nil t)
312 (setq received (1+ received)))
313 (setq last-point (point))
315 (nntp-accept-response))))
317 ;; Wait for the reply from the final command.
318 (when nntp-server-list-active-group
319 (goto-char (point-max))
320 (re-search-backward "^[0-9]" nil t)
321 (when (looking-at "^[23]")
323 (goto-char (- (point-max) 3))
324 (not (looking-at "^\\.\r?\n")))
325 (nntp-accept-response))))
327 ;; Now all replies are received. We remove CRs.
328 (goto-char (point-min))
329 (while (search-forward "\r" nil t)
330 (replace-match "" t t))
332 (if (not nntp-server-list-active-group)
334 ;; We have read active entries, so we just delete the
336 (goto-char (point-min))
337 (while (re-search-forward "^[.2-5]" nil t)
338 (delete-region (match-beginning 0)
339 (progn (forward-line 1) (point))))
342 (deffoo nntp-open-server (server &optional defs connectionless)
343 "Open the virtual server SERVER.
344 If CONNECTIONLESS is non-nil, don't attempt to connect to any physical
346 ;; Called with just a port number as the defs.
347 (when (or (stringp (car defs))
348 (numberp (car defs)))
349 (setq defs `((nntp-port-number ,(car defs)))))
350 (unless (assq 'nntp-address defs)
351 (setq defs (append defs `((nntp-address ,server)))))
352 (nnoo-change-server 'nntp server defs)
353 (if (nntp-server-opened server)
355 (or (nntp-server-opened server)
358 (run-hooks 'nntp-prepare-server-hook)
359 (nntp-open-server-semi-internal nntp-address nntp-port-number)
360 (nnheader-insert "")))))
362 (deffoo nntp-close-server (&optional server)
363 "Close connection to SERVER."
364 (nntp-possibly-change-server nil server t)
367 ;; Un-set default sentinel function before closing connection.
368 (and nntp-server-process
369 (eq 'nntp-default-sentinel
370 (process-sentinel nntp-server-process))
371 (set-process-sentinel nntp-server-process nil))
372 ;; We cannot send QUIT command unless the process is running.
373 (when (nntp-server-opened server)
374 (nntp-send-command nil "QUIT")
375 ;; Give the QUIT time to arrive.
377 (nntp-close-server-internal server)))
379 (deffoo nntp-request-close ()
380 "Close all server connections."
382 (while nntp-opened-connections
383 (when (setq proc (pop nntp-opened-connections))
384 ;; Un-set default sentinel function before closing connection.
385 (when (eq 'nntp-default-sentinel (process-sentinel proc))
386 (set-process-sentinel proc nil))
388 (process-send-string proc (concat "QUIT" nntp-end-of-line))
390 ;; Give the QUIT time to reach the server before we close
393 (delete-process proc)))
394 (and nntp-async-buffer
395 (buffer-name nntp-async-buffer)
396 (kill-buffer nntp-async-buffer))
397 (let ((alist (cddr (assq 'nntp nnoo-state-alist)))
399 (while (setq entry (pop alist))
400 (and (setq proc (cdr (assq 'nntp-async-buffer entry)))
402 (kill-buffer proc))))
403 (nnoo-close-server 'nntp)
404 (setq nntp-async-group-alist nil)))
406 (deffoo nntp-server-opened (&optional server)
407 "Say whether a connection to SERVER has been opened."
408 (and (nnoo-current-server-p 'nntp server)
410 (buffer-name nntp-server-buffer)
412 (memq (process-status nntp-server-process) '(open run))))
414 (deffoo nntp-status-message (&optional server)
415 "Return server status as a string."
416 (if (and nntp-status-string
418 (string-match "[0-9][0-9][0-9][ \t]+\\([^\r]*\\).*$"
420 (substring nntp-status-string (match-beginning 1) (match-end 1))
421 ;; Empty message if nothing.
422 (or nntp-status-string "")))
424 (deffoo nntp-request-article (id &optional group server buffer)
425 "Request article ID (Message-ID or number)."
426 (nntp-possibly-change-server group server)
430 ;; First we see whether we can get the article from the async buffer.
431 (when (and (numberp id)
433 (memq id nntp-async-fetched))
435 (set-buffer nntp-async-buffer)
436 (let ((opoint (point))
437 (art (if (numberp id) (int-to-string id) id))
439 (when (and (or (re-search-forward (concat "^2.. +" art) nil t)
441 (goto-char (point-min))
442 (re-search-forward (concat "^2.. +" art) opoint t)))
446 end (re-search-forward "^\\.\r?\n" nil t))))
449 (set-buffer (or buffer nntp-server-buffer))
451 (insert-buffer-substring nntp-async-buffer beg end)
452 (let ((nntp-server-buffer (current-buffer)))
454 (delete-region beg end)
455 (when nntp-async-articles
456 (nntp-async-fetch-articles id))))))
460 ;; The article was not in the async buffer, so we fetch it now.
463 (if buffer (set-process-buffer nntp-server-process buffer))
464 (let ((nntp-server-buffer (or buffer nntp-server-buffer))
465 (art (or (and (numberp id) (int-to-string id)) id)))
467 (and (nntp-send-command
468 ;; A bit odd regexp to ensure working over rlogin.
469 "^\\.\r?\n" "ARTICLE" art)
471 (cons nntp-current-group id)
472 ;; We find out what the article number was.
473 (nntp-find-group-and-number)))
475 (and nntp-async-articles (nntp-async-fetch-articles id)))))
477 (set-process-buffer nntp-server-process nntp-server-buffer))))))
479 (deffoo nntp-request-body (id &optional group server)
480 "Request body of article ID (Message-ID or number)."
481 (nntp-possibly-change-server group server)
483 ;; If NEmacs, end of message may look like: "\256\215" (".^M")
485 "^\\.\r?\n" "BODY" (or (and (numberp id) (int-to-string id)) id))
488 (deffoo nntp-request-head (id &optional group server)
489 "Request head of article ID (Message-ID or number)."
490 (nntp-possibly-change-server group server)
492 (when (nntp-send-command
493 "^\\.\r?\n" "HEAD" (if (numberp id) (int-to-string id) id))
495 ;; We find out what the article number was.
496 (nntp-find-group-and-number)))
499 (deffoo nntp-request-stat (id &optional group server)
500 "Request STAT of article ID (Message-ID or number)."
501 (nntp-possibly-change-server group server)
503 "^[23].*\r?\n" "STAT" (or (and (numberp id) (int-to-string id)) id)))
505 (deffoo nntp-request-type (group &optional article)
508 (deffoo nntp-request-group (group &optional server dont-check)
510 (nntp-possibly-change-server nil server)
511 (setq nntp-current-group
512 (when (nntp-send-command "^2.*\r?\n" "GROUP" group)
515 (deffoo nntp-request-asynchronous (group &optional server articles)
516 "Enable pre-fetch in GROUP."
517 (when nntp-async-articles
518 (nntp-async-request-group group))
519 (when nntp-async-number
520 (if (not (or (nntp-async-server-opened)
521 (nntp-async-open-server)))
522 ;; Couldn't open the second connection
524 (message "Can't open second connection to %s" nntp-address)
526 (setq nntp-async-articles nil)
528 ;; We opened the second connection (or it was opened already).
529 (setq nntp-async-articles articles)
530 (setq nntp-async-fetched nil)
531 ;; Clear any old data.
533 (set-buffer nntp-async-buffer)
535 ;; Select the correct current group on this server.
536 (nntp-async-send-strings "GROUP" group)
539 (deffoo nntp-list-active-group (group &optional server)
540 "Return the active info on GROUP (which can be a regexp."
541 (nntp-possibly-change-server group server)
542 (nntp-send-command "^.*\r?\n" "LIST ACTIVE" group))
544 (deffoo nntp-request-group-description (group &optional server)
545 "Get the description of GROUP."
546 (nntp-possibly-change-server nil server)
548 (nntp-send-command "^.*\r?\n" "XGTITLE" group)
551 (deffoo nntp-close-group (group &optional server)
553 (setq nntp-current-group nil)
556 (deffoo nntp-request-list (&optional server)
557 "List all active groups."
558 (nntp-possibly-change-server nil server)
560 (nntp-send-command "^\\.\r?\n" "LIST")
563 (deffoo nntp-request-list-newsgroups (&optional server)
564 "Get descriptions on all groups on SERVER."
565 (nntp-possibly-change-server nil server)
567 (nntp-send-command "^\\.\r?\n" "LIST NEWSGROUPS")
570 (deffoo nntp-request-newgroups (date &optional server)
571 "List groups that have arrived since DATE."
572 (nntp-possibly-change-server nil server)
573 (let* ((date (timezone-parse-date date))
575 (format "%s%02d%02d %s%s%s"
576 (substring (aref date 0) 2) (string-to-int (aref date 1))
577 (string-to-int (aref date 2)) (substring (aref date 3) 0 2)
579 (aref date 3) 3 5) (substring (aref date 3) 6 8))))
581 (nntp-send-command "^\\.\r?\n" "NEWGROUPS" time-string)
582 (nntp-decode-text))))
584 (deffoo nntp-request-list-distributions (&optional server)
585 "List distributions."
586 (nntp-possibly-change-server nil server)
588 (nntp-send-command "^\\.\r?\n" "LIST DISTRIBUTIONS")
591 (deffoo nntp-request-last (&optional group server)
592 "Decrease the current article pointer."
593 (nntp-possibly-change-server group server)
594 (nntp-send-command "^[23].*\r?\n" "LAST"))
596 (deffoo nntp-request-next (&optional group server)
597 "Advance the current article pointer."
598 (nntp-possibly-change-server group server)
599 (nntp-send-command "^[23].*\r?\n" "NEXT"))
601 (deffoo nntp-request-post (&optional server)
602 "Post the current buffer."
603 (nntp-possibly-change-server nil server)
604 (when (nntp-send-command "^[23].*\r?\n" "POST")
607 (nntp-send-region-to-server (point-min) (point-max))
608 ;; 1.2a NNTP's post command is buggy. "^M" (\r) is not
609 ;; appended to end of the status message.
610 (nntp-wait-for-response "^[23].*\n")))
612 ;;; Internal functions.
614 (defun nntp-send-mode-reader ()
615 "Send the MODE READER command to the nntp server.
616 This function is supposed to be called from `nntp-server-opened-hook'.
617 It will make innd servers spawn an nnrpd process to allow actual article
619 (nntp-send-command "^.*\r?\n" "MODE READER"))
621 (defun nntp-send-nosy-authinfo ()
622 "Send the AUTHINFO to the nntp server.
623 This function is supposed to be called from `nntp-server-opened-hook'.
624 It will prompt for a password."
625 (nntp-send-command "^.*\r?\n" "AUTHINFO USER"
626 (read-string "NNTP user name: "))
627 (nntp-send-command "^.*\r?\n" "AUTHINFO PASS"
628 (read-string "NNTP password: ")))
630 (defun nntp-send-authinfo ()
631 "Send the AUTHINFO to the nntp server.
632 This function is supposed to be called from `nntp-server-opened-hook'.
633 It will prompt for a password."
634 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name))
635 (nntp-send-command "^.*\r?\n" "AUTHINFO PASS"
636 (read-string "NNTP password: ")))
638 (defun nntp-send-authinfo-from-file ()
639 "Send the AUTHINFO to the nntp server.
640 This function is supposed to be called from `nntp-server-opened-hook'.
641 It will prompt for a password."
642 (when (file-exists-p "~/.nntp-authinfo")
644 (set-buffer (get-buffer-create " *authinfo*"))
645 (buffer-disable-undo (current-buffer))
647 (insert-file-contents "~/.nntp-authinfo")
648 (goto-char (point-min))
649 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name))
651 "^.*\r?\n" "AUTHINFO PASS"
652 (buffer-substring (point) (progn (end-of-line) (point))))
653 (kill-buffer (current-buffer)))))
655 (defun nntp-default-sentinel (proc status)
656 "Default sentinel function for NNTP server process."
657 (let ((servers (cddr (assq 'nntp nnoo-state-alist)))
659 ;; Go through the alist of server names and find the name of the
660 ;; server that the process that sent the signal is connected to.
661 ;; If you get my drift.
662 (if (equal proc nntp-server-process)
663 (setq server nntp-address)
665 (not (equal proc (cdr (assq 'nntp-server-process
667 (setq servers (cdr servers)))
668 (setq server (caar servers)))
670 nntp-warn-about-losing-connection)
671 (nnheader-message 3 "nntp: Connection closed to server %s" server)
672 (setq nntp-current-group "")
675 (defun nntp-kill-connection (server)
676 "Choke the connection to SERVER."
677 (let ((proc (cdr (assq 'nntp-server-process
679 (assq 'nntp nnoo-state-alist)))))))
681 (delete-process (process-name proc)))
682 (nntp-close-server server)
684 'nntp (message "Connection timed out to server %s" server))
688 ;; Encoding and decoding of NNTP text.
690 (defun nntp-decode-text ()
691 "Decode text transmitted by NNTP.
692 0. Delete status line.
693 1. Delete `^M' at end of line.
694 2. Delete `.' at end of buffer (end of text mark).
695 3. Delete `.' at beginning of line."
697 (set-buffer nntp-server-buffer)
698 ;; Insert newline at end of buffer.
699 (goto-char (point-max))
700 (or (bolp) (insert "\n"))
701 ;; Delete status line.
702 (delete-region (goto-char (point-min)) (progn (forward-line 1) (point)))
704 (while (search-forward "\r" nil t)
705 (replace-match "" t t))
706 ;; Delete `.' at end of the buffer (end of text mark).
707 (goto-char (point-max))
709 (when (looking-at "^\\.\n")
710 (delete-region (point) (progn (forward-line 1) (point))))
711 ;; Replace `..' at beginning of line with `.'.
712 (goto-char (point-min))
713 ;; (replace-regexp "^\\.\\." ".")
714 (while (search-forward "\n.." nil t)
717 (defun nntp-encode-text ()
718 "Encode text in current buffer for NNTP transmission.
719 1. Insert `.' at beginning of line.
720 2. Insert `.' at end of buffer (end of text mark)."
722 ;; Replace `.' at beginning of line with `..'.
723 (goto-char (point-min))
724 (while (search-forward "\n." nil t)
726 (goto-char (point-max))
727 ;; Insert newline at end of buffer.
728 (or (bolp) (insert "\n"))
729 ;; Insert `.' at end of buffer (end of text mark).
730 (insert "." nntp-end-of-line)))
734 ;;; Synchronous Communication with NNTP servers.
737 (defvar nntp-retry-command)
739 (defun nntp-send-command (response cmd &rest args)
740 "Wait for server RESPONSE after sending CMD and optional ARGS to server."
742 (and nntp-command-timeout
743 (nnheader-run-at-time
744 nntp-command-timeout nil 'nntp-kill-command
745 (nnoo-current-server 'nntp))))
746 (nntp-retry-command t)
750 (while nntp-retry-command
751 (setq nntp-retry-command nil)
752 ;; Clear communication buffer.
753 (set-buffer nntp-server-buffer)
756 (if nntp-retry-on-break
759 (apply 'nntp-send-strings-to-server cmd args)
762 (nntp-wait-for-response response)
764 (quit (setq nntp-retry-command t)))
765 (apply 'nntp-send-strings-to-server cmd args)
768 (nntp-wait-for-response response)
772 (nnheader-cancel-timer timer)))))
774 (defun nntp-kill-command (server)
775 "Kill and restart the connection to SERVER."
776 (let ((proc (cdr (assq
778 (assoc server (cddr (assq 'nntp nnoo-state-alist)))))))
780 (delete-process (process-name proc)))
781 (nntp-close-server server)
782 (nntp-open-server server)
783 (when nntp-current-group
784 (nntp-request-group nntp-current-group))
785 (setq nntp-retry-command t)))
787 (defun nntp-send-command-old (response cmd &rest args)
788 "Wait for server RESPONSE after sending CMD and optional ARGS to server."
790 ;; Clear communication buffer.
791 (set-buffer nntp-server-buffer)
793 (apply 'nntp-send-strings-to-server cmd args)
795 (nntp-wait-for-response response)
798 (defun nntp-wait-for-response (regexp &optional slow)
799 "Wait for server response which matches REGEXP."
803 (dotnum 0) ;Number of "." being displayed.
804 (dotsize ;How often "." displayed.
805 (if (numberp nntp-debug-read) nntp-debug-read 10000)))
806 (set-buffer nntp-server-buffer)
807 ;; Wait for status response (RFC977).
808 ;; 1xx - Informative message.
810 ;; 3xx - Command ok so far, send the rest of it.
811 ;; 4xx - Command was correct, but couldn't be performed for some
813 ;; 5xx - Command unimplemented, or incorrect, or a serious
814 ;; program error occurred.
815 (nntp-accept-response)
817 (goto-char (point-min))
820 (cond ((re-search-forward "^[23][0-9][0-9]" nil t)
822 ((re-search-forward "^[45][0-9][0-9]" nil t)
825 (t (nntp-accept-response)))
826 (if (not wait) (delete-region (point-min)
827 (progn (beginning-of-line)
829 (cond ((looking-at "[23]")
834 (t (nntp-accept-response)))))
835 ;; Save status message.
837 (setq nntp-status-string
838 (nnheader-replace-chars-in-string
839 (buffer-substring (point-min) (point)) ?\r ? ))
843 (goto-char (point-max))
844 (if (bolp) (forward-line -1) (beginning-of-line))
845 (if (looking-at regexp)
847 (when nntp-debug-read
848 (let ((newnum (/ (buffer-size) dotsize))
849 (message-log-max nil))
850 (unless (= dotnum newnum)
852 (nnheader-message 7 "NNTP: Reading %s"
853 (make-string dotnum ?.)))))
854 (nntp-accept-response)))
856 (when (and nntp-debug-read (> dotnum 0))
858 ;; Successfully received server response.
864 ;;; Low-Level Interface to NNTP Server.
867 (defun nntp-find-group-and-number ()
870 (set-buffer nntp-server-buffer)
871 (narrow-to-region (goto-char (point-min))
872 (or (search-forward "\n\n" nil t) (point-max)))
873 (goto-char (point-min))
874 ;; We first find the number by looking at the status line.
875 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
877 (buffer-substring (match-beginning 1)
879 group newsgroups xref)
880 (and number (zerop number) (setq number nil))
881 ;; Then we find the group name.
884 ;; If there is only one group in the Newsgroups header,
885 ;; then it seems quite likely that this article comes
886 ;; from that group, I'd say.
887 ((and (setq newsgroups (mail-fetch-field "newsgroups"))
888 (not (string-match "," newsgroups)))
890 ;; If there is more than one group in the Newsgroups
891 ;; header, then the Xref header should be filled out.
892 ;; We hazard a guess that the group that has this
893 ;; article number in the Xref header is the one we are
894 ;; looking for. This might very well be wrong if this
895 ;; article happens to have the same number in several
896 ;; groups, but that's life.
897 ((and (setq xref (mail-fetch-field "xref"))
899 (string-match (format "\\([^ :]+\\):%d" number) xref))
900 (substring xref (match-beginning 1) (match-end 1)))
902 (when (string-match "\r" group)
903 (setq group (substring group 0 (match-beginning 0))))
904 (cons group number)))))
906 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
910 ;; This server does not talk NOV.
911 ((not nntp-server-xover)
914 ;; We don't care about gaps.
915 ((or (not nntp-nov-gap)
917 (nntp-send-xover-command
919 (if (numberp fetch-old)
920 (max 1 (- (car articles) fetch-old))
923 (nntp-last-element articles) 'wait)
925 (goto-char (point-min))
926 (when (looking-at "[1-5][0-9][0-9] ")
927 (delete-region (point) (progn (forward-line 1) (point))))
928 (while (search-forward "\r" nil t)
929 (replace-match "" t t))
930 (goto-char (point-max))
932 (when (looking-at "\\.")
933 (delete-region (point) (progn (forward-line 1) (point)))))
935 ;; We do it the hard way. For each gap, an XOVER command is sent
936 ;; to the server. We do not wait for a reply from the server, we
937 ;; just send them off as fast as we can. That means that we have
938 ;; to count the number of responses we get back to find out when we
939 ;; have gotten all we asked for.
940 ((numberp nntp-nov-gap)
943 (last-point (point-min))
944 (buf (current-buffer))
946 ;; We have to check `nntp-server-xover'. If it gets set to nil,
947 ;; that means that the server does not understand XOVER, but we
948 ;; won't know that until we try.
949 (while (and nntp-server-xover articles)
950 (setq first (car articles))
951 ;; Search forward until we find a gap, or until we run out of
953 (while (and (cdr articles)
954 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
955 (setq articles (cdr articles)))
957 (when (nntp-send-xover-command first (car articles))
958 (setq articles (cdr articles)
961 ;; Every 400 requests we have to read the stream in
962 ;; order to avoid deadlocks.
963 (when (or (null articles) ;All requests have been sent.
964 (zerop (% count nntp-maximum-request)))
965 (accept-process-output)
966 ;; On some Emacs versions the preceding function has
967 ;; a tendency to change the buffer. Perhaps. It's
968 ;; quite difficult to reproduce, because it only
969 ;; seems to happen once in a blue moon.
972 (goto-char last-point)
974 (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
975 (setq received (1+ received)))
976 (setq last-point (point))
978 (accept-process-output)
981 (when nntp-server-xover
982 ;; Wait for the reply from the final command.
983 (goto-char (point-max))
984 (re-search-backward "^[0-9][0-9][0-9] " nil t)
985 (when (looking-at "^[23]")
987 (goto-char (point-max))
989 (not (looking-at "^\\.\r?\n")))
990 (nntp-accept-response)))
992 ;; We remove any "." lines and status lines.
993 (goto-char (point-min))
994 (while (search-forward "\r" nil t)
996 (goto-char (point-min))
997 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")))))
1001 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1002 "Send the XOVER command to the server."
1003 (let ((range (format "%d-%d" (or beg 1) (or end beg 1))))
1004 (if (stringp nntp-server-xover)
1005 ;; If `nntp-server-xover' is a string, then we just send this
1008 (nntp-send-command "^\\.\r?\n" nntp-server-xover range)
1009 ;; We do not wait for the reply.
1010 (nntp-send-strings-to-server nntp-server-xover range))
1011 (let ((commands nntp-xover-commands))
1012 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1013 ;; We try them all until we get at positive response.
1014 (while (and commands (eq nntp-server-xover 'try))
1015 (nntp-send-command "^\\.\r?\n" (car commands) range)
1017 (set-buffer nntp-server-buffer)
1018 (goto-char (point-min))
1019 (and (looking-at "[23]") ; No error message.
1020 ;; We also have to look at the lines. Some buggy
1021 ;; servers give back simple lines with just the
1022 ;; article number. How... helpful.
1025 (looking-at "[0-9]+\t...")) ; More text after number.
1026 (setq nntp-server-xover (car commands))))
1027 (setq commands (cdr commands)))
1028 ;; If none of the commands worked, we disable XOVER.
1029 (when (eq nntp-server-xover 'try)
1031 (set-buffer nntp-server-buffer)
1033 (setq nntp-server-xover nil)))
1034 nntp-server-xover))))
1036 (defun nntp-send-strings-to-server (&rest strings)
1037 "Send STRINGS to the server."
1038 (let ((cmd (concat (mapconcat 'identity strings " ") nntp-end-of-line)))
1039 ;; We open the nntp server if it is down.
1040 (or (nntp-server-opened (nnoo-current-server 'nntp))
1041 (nntp-open-server (nnoo-current-server 'nntp))
1042 (error (nntp-status-message)))
1043 ;; Send the strings.
1044 (process-send-string nntp-server-process cmd)
1047 (defun nntp-send-region-to-server (begin end)
1048 "Send the current buffer region (from BEGIN to END) to the server."
1050 (let ((cur (current-buffer)))
1051 ;; Copy the buffer over to the send buffer.
1052 (nnheader-set-temp-buffer " *nntp send*")
1053 (insert-buffer-substring cur begin end)
1057 ;; `process-send-region' does not work if the text to be sent is very
1058 ;; large, so we send it piecemeal.
1059 (let ((last (point-min))
1060 (size 100)) ;Size of text sent at once.
1061 (while (/= last (point-max))
1062 (process-send-region
1064 last (setq last (min (+ last size) (point-max))))
1065 ;; Read any output from the server. May be unnecessary.
1066 (accept-process-output)))
1067 (kill-buffer (current-buffer)))))
1069 (defun nntp-open-server-semi-internal (server &optional service)
1071 If SERVER is nil, use value of environment variable `NNTPSERVER'.
1072 If SERVICE, this this as the port number."
1073 (nnheader-insert "")
1074 (let ((server (or server (getenv "NNTPSERVER")))
1077 (and nntp-connection-timeout
1078 (nnheader-run-at-time nntp-connection-timeout
1079 nil 'nntp-kill-connection server))))
1081 (set-buffer nntp-server-buffer)
1082 (setq nntp-status-string "")
1083 (nnheader-message 5 "nntp: Connecting to server on %s..." nntp-address)
1084 (cond ((and server (nntp-open-server-internal server service))
1085 (setq nntp-address server)
1088 (nntp-wait-for-response "^[23].*\r?\n" 'slow)
1092 (nntp-close-server-internal server)
1094 'nntp "Couldn't open connection to %s" nntp-address))
1095 (when nntp-server-process
1096 (set-process-sentinel
1097 nntp-server-process 'nntp-default-sentinel)
1098 ;; You can send commands at startup like AUTHINFO here.
1099 ;; Added by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>
1100 (run-hooks 'nntp-server-opened-hook)))
1102 (nnheader-report 'nntp "NNTP server is not specified."))
1103 (t ; We couldn't open the server.
1105 'nntp (buffer-substring (point-min) (point-max)))))
1107 (nnheader-cancel-timer timer))
1110 (nnoo-close-server 'nntp server)
1111 (setq nntp-async-number nil))
1114 (defvar nntp-default-directories '("~" "/tmp" "/")
1115 "Directories to as current directory in the nntp server buffer.")
1117 (defun nntp-open-server-internal (server &optional service)
1118 "Open connection to news server on SERVER by SERVICE (default is nntp)."
1121 (set-buffer nntp-server-buffer)
1122 ;; Make sure we have a valid current directory for the
1123 ;; nntp server buffer.
1124 (unless (file-exists-p default-directory)
1125 (let ((dirs nntp-default-directories))
1127 (when (file-exists-p (car dirs))
1128 (setq default-directory (car dirs)
1130 (setq dirs (cdr dirs)))))
1134 (funcall nntp-open-server-function server)
1136 (memq (process-status proc) '(open run)))
1137 (setq nntp-server-process proc)
1138 (setq nntp-address server)
1139 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1140 (process-kill-without-query proc)
1141 (run-hooks 'nntp-server-hook)
1142 (push proc nntp-opened-connections)
1144 (nntp-read-server-type)
1146 (nnheader-report 'nntp "Couldn't open server %s" server)
1147 (nntp-close-server)))
1148 nntp-server-process)
1150 (nnheader-report 'nntp "Couldn't open server %s" server))))))
1152 (defun nntp-read-server-type ()
1153 "Find out what the name of the server we have connected to is."
1154 ;; Wait for the status string to arrive.
1155 (nntp-wait-for-response "^.*\n" t)
1156 (setq nntp-server-type (buffer-string))
1157 (let ((alist nntp-server-action-alist)
1159 ;; Run server-specific commmands.
1161 (setq entry (pop alist))
1162 (when (string-match (car entry) nntp-server-type)
1163 (if (and (listp (cadr entry))
1164 (not (eq 'lambda (caadr entry))))
1166 (funcall (cadr entry)))))))
1168 (defun nntp-open-network-stream (server)
1169 (open-network-stream
1170 "nntpd" nntp-server-buffer server nntp-port-number))
1172 (defun nntp-open-rlogin (server)
1173 (let ((proc (if nntp-rlogin-user-name
1175 "nntpd" nntp-server-buffer "rsh"
1176 "-l" nntp-rlogin-user-name server
1177 (mapconcat 'identity
1178 nntp-rlogin-parameters " "))
1180 "nntpd" nntp-server-buffer "rsh" server
1181 (mapconcat 'identity
1182 nntp-rlogin-parameters " ")))))
1185 (defun nntp-telnet-to-machine ()
1187 (telnet "localhost")
1188 (goto-char (point-min))
1189 (while (not (re-search-forward "^login: *" nil t))
1191 (goto-char (point-min)))
1192 (goto-char (point-max))
1196 (while (not (re-search-forward ">" nil t))
1199 (goto-char (point-max))
1201 (telnet-send-input)))
1203 (defun nntp-close-server-internal (&optional server)
1204 "Close connection to news server."
1205 (nntp-possibly-change-server nil server)
1206 (if nntp-server-process
1207 (delete-process nntp-server-process))
1208 (setq nntp-server-process nil)
1209 (setq nntp-address ""))
1211 (defun nntp-accept-response ()
1212 "Read response of server.
1213 It is well-known that the communication speed will be much improved by
1214 defining this function as macro."
1215 ;; To deal with server process exiting before
1216 ;; accept-process-output is called.
1217 ;; Suggested by Jason Venner <jason@violet.berkeley.edu>.
1218 ;; This is a copy of `nntp-default-sentinel'.
1219 (let ((buf (current-buffer)))
1221 (if (or (not nntp-server-process)
1222 (not (memq (process-status nntp-server-process) '(open run))))
1223 (error "nntp: Process connection closed; %s" (nntp-status-message))
1224 (if nntp-buggy-select
1226 ;; We cannot use `accept-process-output'.
1227 ;; Fujitsu UTS requires messages during sleep-for.
1228 ;; I don't know why.
1229 (nnheader-message 5 "NNTP: Reading...")
1231 (nnheader-message 5 ""))
1232 (condition-case errorcode
1233 (accept-process-output nntp-server-process 1)
1235 (cond ((string-equal "select error: Invalid argument"
1237 ;; Ignore select error.
1240 (signal (car errorcode) (cdr errorcode))))))))
1243 (defun nntp-last-element (list)
1244 "Return last element of LIST."
1246 (setq list (cdr list)))
1249 (defun nntp-possibly-change-server (newsgroup server &optional connectionless)
1250 "Check whether the virtual server needs changing."
1252 (not (nntp-server-opened server)))
1253 ;; This virtual server isn't open, so we (re)open it here.
1254 (nntp-open-server server nil t))
1255 (when (and newsgroup
1256 (not (equal newsgroup nntp-current-group)))
1257 ;; Set the proper current group.
1258 (nntp-request-group newsgroup server)))
1260 (defun nntp-try-list-active (group)
1261 (nntp-list-active-group group)
1263 (set-buffer nntp-server-buffer)
1264 (goto-char (point-min))
1265 (cond ((looking-at "5[0-9]+")
1266 (setq nntp-server-list-active-group nil))
1268 (setq nntp-server-list-active-group t)))))
1270 (defun nntp-async-server-opened ()
1271 (and nntp-async-process
1272 (memq (process-status nntp-async-process) '(open run))))
1274 (defun nntp-async-open-server ()
1276 (set-buffer (generate-new-buffer " *async-nntp*"))
1277 (setq nntp-async-buffer (current-buffer))
1278 (buffer-disable-undo (current-buffer)))
1279 (let ((nntp-server-process nil)
1280 (nntp-server-buffer nntp-async-buffer))
1281 (nntp-open-server-semi-internal nntp-address nntp-port-number)
1282 (if (not (setq nntp-async-process nntp-server-process))
1284 (setq nntp-async-number nil))
1285 (set-process-buffer nntp-async-process nntp-async-buffer))))
1287 (defun nntp-async-fetch-articles (article)
1288 (if (stringp article)
1290 (let ((articles (cdr (memq (assq article nntp-async-articles)
1291 nntp-async-articles)))
1292 (max (cond ((numberp nntp-async-number)
1294 ((eq nntp-async-number t)
1295 (length nntp-async-articles))
1298 (while (and (>= (setq max (1- max)) 0)
1300 (or (memq (setq nart (caar articles)) nntp-async-fetched)
1302 (nntp-async-send-strings "ARTICLE " (int-to-string nart))
1303 (setq nntp-async-fetched (cons nart nntp-async-fetched))))
1304 (setq articles (cdr articles))))))
1306 (defun nntp-async-send-strings (&rest strings)
1307 (let ((cmd (concat (mapconcat 'identity strings " ") nntp-end-of-line)))
1308 (or (nntp-async-server-opened)
1309 (nntp-async-open-server)
1310 (error (nntp-status-message)))
1311 (process-send-string nntp-async-process cmd)))
1313 (defun nntp-async-request-group (group)
1314 (if (equal group nntp-current-group)
1316 (let ((asyncs (assoc group nntp-async-group-alist)))
1317 ;; A new group has been selected, so we push the current state
1318 ;; of async articles on an alist, and pull the old state off.
1319 (setq nntp-async-group-alist
1320 (cons (list nntp-current-group
1321 nntp-async-articles nntp-async-fetched
1323 (delq asyncs nntp-async-group-alist)))
1326 (setq nntp-async-articles (nth 1 asyncs))
1327 (setq nntp-async-fetched (nth 2 asyncs))
1328 (setq nntp-async-process (nth 3 asyncs)))))))
1332 ;;; nntp.el ends here