1 ;;; nntp.el --- nntp access for Gnus
2 ;; Copyright (C) 1987,88,89,90,92,93,94,95 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
32 (eval-when-compile (require 'cl))
35 (autoload 'news-setup "rnewspost")
36 (autoload 'news-reply-mode "rnewspost")
37 (autoload 'cancel-timer "timer")
38 (autoload 'telnet "telnet" nil t)
39 (autoload 'telnet-send-input "telnet" nil t)
40 (autoload 'timezone-parse-date "timezone"))
42 (defvar nntp-server-hook nil
43 "*Hooks for the NNTP server.
44 If the kanji code of the NNTP server is different from the local kanji
45 code, the correct kanji code of the buffer associated with the NNTP
46 server must be specified as follows:
48 \(setq nntp-server-hook
51 ;; Server's Kanji code is EUC (NEmacs hack).
52 (make-local-variable 'kanji-fileio-code)
53 (setq kanji-fileio-code 0))))
55 If you'd like to change something depending on the server in this
56 hook, use the variable `nntp-address'.")
58 (defvar nntp-server-opened-hook nil
59 "*Hook used for sending commands to the server at startup.
60 The default value is `nntp-send-mode-reader', which makes an innd
61 server spawn an nnrpd server. Another useful function to put in this
62 hook might be `nntp-send-authinfo', which will prompt for a password
63 to allow posting from the server. Note that this is only necessary to
64 do on servers that use strict access control.")
65 (add-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)
67 (defvar nntp-open-server-function 'nntp-open-network-stream
68 "*Function used for connecting to a remote system.
69 It will be called with the address of the remote system.
71 Two pre-made functions are `nntp-open-network-stream', which is the
72 default, and simply connects to some port or other on the remote
73 system (see nntp-port-number). The other is `nntp-open-rlogin', which
74 does an rlogin on the remote system, and then does a telnet to the
75 NNTP server available there (see nntp-rlogin-parameters).")
77 (defvar nntp-rlogin-parameters '("telnet" "${NNTPSERVER:=localhost}" "nntp")
78 "*Parameters to `nntp-open-login'.
79 That function may be used as `nntp-open-server-function'. In that
80 case, this list will be used as the parameter list given to rsh.")
82 (defvar nntp-rlogin-user-name nil
83 "*User name on remote system when using the rlogin connect method.")
85 (defvar nntp-address nil
86 "*The name of the NNTP server.")
88 (defvar nntp-port-number "nntp"
89 "*Port number to connect to.")
91 (defvar nntp-large-newsgroup 50
92 "*The number of the articles which indicates a large newsgroup.
93 If the number of the articles is greater than the value, verbose
94 messages will be shown to indicate the current status.")
96 (defvar nntp-buggy-select (memq system-type '(fujitsu-uts))
97 "*t if your select routine is buggy.
98 If the select routine signals error or fall into infinite loop while
99 waiting for the server response, the variable must be set to t. In
100 case of Fujitsu UTS, it is set to T since `accept-process-output'
101 doesn't work properly.")
103 (defvar nntp-maximum-request 400
104 "*The maximum number of the requests sent to the NNTP server at one time.
105 If Emacs hangs up while retrieving headers, set the variable to a
108 (defvar nntp-debug-read 10000
109 "*Display '...' every 10Kbytes of a message being received if it is non-nil.
110 If it is a number, dots are displayed per the number.")
112 (defvar nntp-nov-is-evil nil
113 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
115 (defvar nntp-xover-commands '("XOVER" "XOVERVIEW")
116 "*List of strings that are used as commands to fetch NOV lines from a server.
117 The strings are tried in turn until a positive response is gotten. If
118 none of the commands are successful, nntp will just grab headers one
121 (defvar nntp-nov-gap 20
122 "*Maximum allowed gap between two articles.
123 If the gap between two consecutive articles is bigger than this
124 variable, split the XOVER request into two requests.")
126 (defvar nntp-connection-timeout nil
127 "*Number of seconds to wait before an nntp connection times out.
128 If this variable is nil, which is the default, no timers are set.")
130 (defvar nntp-news-default-headers nil
131 "*If non-nil, override `mail-default-headers' when posting news.")
133 (defvar nntp-prepare-server-hook nil
134 "*Hook run before a server is opened.
135 If can be used to set up a server remotely, for instance. Say you
136 have an account at the machine \"other.machine\". This machine has
137 access to an NNTP server that you can't access locally. You could
138 then use this hook to rsh to the remote machine and start a proxy NNTP
139 server there that you can connect to.")
141 (defvar nntp-async-number 5
142 "*How many articles should be prefetched when in asynchronous mode.")
144 (defvar nntp-warn-about-losing-connection t
145 "*If non-nil, beep when a server closes connection.")
149 (defconst nntp-version "nntp 4.0"
150 "Version numbers of this version of NNTP.")
152 (defvar nntp-server-buffer nil
153 "Buffer associated with the NNTP server process.")
155 (defvar nntp-server-process nil
156 "The NNTP server process.
157 You'd better not use this variable in NNTP front-end program, but
158 instead use `nntp-server-buffer'.")
160 (defvar nntp-status-string nil
161 "Save the server response message.
162 You'd better not use this variable in NNTP front-end program but
163 instead call function `nntp-status-message' to get status message.")
165 (defvar nntp-opened-connections nil
166 "All (possibly) opened connections.")
168 (defvar nntp-server-xover 'try)
169 (defvar nntp-server-list-active-group 'try)
170 (defvar nntp-current-group "")
172 (defvar nntp-async-process nil)
173 (defvar nntp-async-buffer nil)
174 (defvar nntp-async-articles nil)
175 (defvar nntp-async-fetched nil)
176 (defvar nntp-async-group-alist nil)
180 (defvar nntp-current-server nil)
181 (defvar nntp-server-alist nil)
182 (defvar nntp-server-variables
183 `((nntp-server-hook ,nntp-server-hook)
184 (nntp-server-opened-hook ,nntp-server-opened-hook)
185 (nntp-port-number ,nntp-port-number)
186 (nntp-address ,nntp-address)
187 (nntp-large-newsgroup ,nntp-large-newsgroup)
188 (nntp-buggy-select ,nntp-buggy-select)
189 (nntp-maximum-request ,nntp-maximum-request)
190 (nntp-debug-read ,nntp-debug-read)
191 (nntp-nov-is-evil ,nntp-nov-is-evil)
192 (nntp-xover-commands ,nntp-xover-commands)
193 (nntp-connection-timeout ,nntp-connection-timeout)
194 (nntp-news-default-headers ,nntp-news-default-headers)
195 (nntp-prepare-server-hook ,nntp-prepare-server-hook)
196 (nntp-async-number ,nntp-async-number)
197 (nntp-async-process nil)
198 (nntp-async-buffer nil)
199 (nntp-async-articles nil)
200 (nntp-async-fetched nil)
201 (nntp-async-group-alist nil)
202 (nntp-server-process nil)
203 (nntp-status-string nil)
204 (nntp-server-xover try)
205 (nntp-server-list-active-group try)
206 (nntp-current-group "")))
209 ;;; Interface functions.
211 (defun nntp-retrieve-headers (articles &optional group server fetch-old)
212 "Retrieve the headers of ARTICLES."
213 (nntp-possibly-change-server group server)
215 (set-buffer nntp-server-buffer)
217 (if (and (not gnus-nov-is-evil)
218 (not nntp-nov-is-evil)
219 (nntp-retrieve-headers-with-xover articles fetch-old))
220 ;; We successfully retrieved the headers via XOVER.
222 ;; XOVER didn't work, so we do it the hard, slow and inefficient
224 (let ((number (length articles))
227 (last-point (point-min)))
228 ;; Send HEAD command.
230 (nntp-send-strings-to-server
231 "HEAD" (if (numberp (car articles))
232 (int-to-string (car articles))
233 ;; `articles' is either a list of article numbers
234 ;; or a list of article IDs.
236 (setq articles (cdr articles)
238 ;; Every 400 header requests we have to read the stream in
239 ;; order to avoid deadlocks.
240 (when (or (null articles) ;All requests have been sent.
241 (zerop (% count nntp-maximum-request)))
242 (nntp-accept-response)
244 (goto-char last-point)
246 (while (re-search-forward "^[0-9]" nil t)
247 (setq received (1+ received)))
248 (setq last-point (point))
250 ;; If number of headers is greater than 100, give
251 ;; informative messages.
252 (and (numberp nntp-large-newsgroup)
253 (> number nntp-large-newsgroup)
254 (zerop (% received 20))
255 (message "NNTP: Receiving headers... %d%%"
256 (/ (* received 100) number)))
257 (nntp-accept-response))))
258 ;; Wait for text of last command.
259 (goto-char (point-max))
260 (re-search-backward "^[0-9]" nil t)
261 (when (looking-at "^[23]")
263 (goto-char (- (point-max) 3))
264 (not (looking-at "^\\.\r?\n")))
265 (nntp-accept-response)))
266 (and (numberp nntp-large-newsgroup)
267 (> number nntp-large-newsgroup)
268 (message "NNTP: Receiving headers...done"))
270 ;; Now all of replies are received. Fold continuation lines.
271 (goto-char (point-min))
272 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
273 (replace-match " " t t))
274 ;; Remove all "\r"'s.
275 (goto-char (point-min))
276 (while (search-forward "\r" nil t)
277 (replace-match "" t t))
281 (defun nntp-retrieve-groups (groups &optional server)
282 "Retrieve group info on GROUPS."
283 (nntp-possibly-change-server nil server)
285 (set-buffer nntp-server-buffer)
286 ;; The first time this is run, this variable is `try'. So we
288 (when (eq nntp-server-list-active-group 'try)
289 (nntp-try-list-active (car groups)))
293 (last-point (point-min))
294 (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
296 ;; Send the command to the server.
297 (nntp-send-strings-to-server command (car groups))
298 (setq groups (cdr groups))
299 (setq count (1+ count))
300 ;; Every 400 requests we have to read the stream in
301 ;; order to avoid deadlocks.
302 (when (or (null groups) ;All requests have been sent.
303 (zerop (% count nntp-maximum-request)))
304 (nntp-accept-response)
306 (goto-char last-point)
308 (while (re-search-forward "^[0-9]" nil t)
309 (setq received (1+ received)))
310 (setq last-point (point))
312 (nntp-accept-response))))
314 ;; Wait for the reply from the final command.
315 (when nntp-server-list-active-group
316 (goto-char (point-max))
317 (re-search-backward "^[0-9]" nil t)
318 (when (looking-at "^[23]")
320 (goto-char (- (point-max) 3))
321 (not (looking-at "^\\.\r?\n")))
322 (nntp-accept-response))))
324 ;; Now all replies are received. We remove CRs.
325 (goto-char (point-min))
326 (while (search-forward "\r" nil t)
327 (replace-match "" t t))
329 (if (not nntp-server-list-active-group)
331 ;; We have read active entries, so we just delete the
333 (goto-char (point-min))
334 (while (re-search-forward "^[.2-5]" nil t)
335 (delete-region (match-beginning 0)
336 (progn (forward-line 1) (point))))
339 (defun nntp-open-server (server &optional defs connectionless)
340 "Open the virtual server SERVER.
341 If CONNECTIONLESS is non-nil, don't attempt to connect to any physical
343 (nnheader-init-server-buffer)
344 (if (nntp-server-opened server)
346 (if (or (stringp (car defs))
347 (numberp (car defs)))
348 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
349 (or (assq 'nntp-address defs)
350 (setq defs (append defs (list (list 'nntp-address server)))))
351 (if (and nntp-current-server
352 (not (equal server nntp-current-server)))
353 (setq nntp-server-alist
354 (cons (list nntp-current-server
355 (nnheader-save-variables nntp-server-variables))
357 (let ((state (assoc server nntp-server-alist)))
360 (nnheader-restore-variables (nth 1 state))
361 (setq nntp-server-alist (delq state nntp-server-alist)))
362 (nnheader-set-init-variables nntp-server-variables defs)))
363 (setq nntp-current-server server)
364 (or (nntp-server-opened server)
367 (run-hooks 'nntp-prepare-server-hook)
368 (nntp-open-server-semi-internal nntp-address nntp-port-number)))))
370 (defun nntp-close-server (&optional server)
371 "Close connection to SERVER."
372 (nntp-possibly-change-server nil server t)
375 ;; Un-set default sentinel function before closing connection.
376 (and nntp-server-process
377 (eq 'nntp-default-sentinel
378 (process-sentinel nntp-server-process))
379 (set-process-sentinel nntp-server-process nil))
380 ;; We cannot send QUIT command unless the process is running.
381 (if (nntp-server-opened)
382 (nntp-send-command nil "QUIT")))
383 (nntp-close-server-internal server)))
385 (defalias 'nntp-request-quit (symbol-function 'nntp-close-server))
387 (defun nntp-request-close ()
388 "Close all server connections."
390 (while nntp-opened-connections
391 (when (setq proc (pop nntp-opened-connections))
393 (process-send-string proc "QUIT\n")
395 (delete-process proc)))
396 (and nntp-async-buffer
397 (get-buffer nntp-async-buffer)
398 (kill-buffer nntp-async-buffer))
399 (while nntp-server-alist
400 (and (setq proc (nth 1 (assq 'nntp-async-buffer
401 (car nntp-server-alist))))
404 (setq nntp-server-alist (cdr nntp-server-alist)))
405 (setq nntp-current-server nil
406 nntp-async-group-alist nil)))
408 (defun nntp-server-opened (&optional server)
409 "Say whether a connection to SERVER has been opened."
410 (and (equal server nntp-current-server)
412 (buffer-name nntp-server-buffer)
414 (memq (process-status nntp-server-process) '(open run))))
416 (defun nntp-status-message (&optional server)
417 "Return server status as a string."
418 (if (and nntp-status-string
420 (string-match "[0-9][0-9][0-9][ \t]+\\([^\r]*\\).*$"
422 (substring nntp-status-string (match-beginning 1) (match-end 1))
423 ;; Empty message if nothing.
424 (or nntp-status-string "")))
426 (defun nntp-request-article (id &optional group server buffer)
427 "Request article ID (Message-ID or number)."
428 (nntp-possibly-change-server group server)
432 ;; First we see whether we can get the article from the async buffer.
433 (when (and (numberp id)
435 (memq id nntp-async-fetched))
437 (set-buffer nntp-async-buffer)
438 (let ((opoint (point))
439 (art (if (numberp id) (int-to-string id) id))
441 (when (and (or (re-search-forward (concat "^2.. +" art) nil t)
443 (goto-char (point-min))
444 (re-search-forward (concat "^2.. +" art) opoint t)))
448 end (re-search-forward "^\\.\r?\n" nil t))))
451 (set-buffer (or buffer nntp-server-buffer))
453 (insert-buffer-substring nntp-async-buffer beg end)
454 (let ((nntp-server-buffer (current-buffer)))
456 (delete-region beg end)
457 (when nntp-async-articles
458 (nntp-async-fetch-articles id))))))
462 ;; The article was not in the async buffer, so we fetch it now.
465 (if buffer (set-process-buffer nntp-server-process buffer))
466 (let ((nntp-server-buffer (or buffer nntp-server-buffer))
467 (art (or (and (numberp id) (int-to-string id)) id)))
469 (and (nntp-send-command "^\\.\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 (defun 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 (defun 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 (and (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 (defun 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 (defun nntp-request-group (group &optional server dont-check)
507 (setq nntp-current-group
508 (when (nntp-send-command "^2.*\r?\n" "GROUP" group)
511 (defun nntp-request-asynchronous (group &optional server articles)
512 "Enable pre-fetch in GROUP."
513 (when nntp-async-articles
514 (nntp-async-request-group group))
515 (when nntp-async-number
516 (if (not (or (nntp-async-server-opened)
517 (nntp-async-open-server)))
518 ;; Couldn't open the second connection
520 (message "Can't open second connection to %s" nntp-address)
522 (setq nntp-async-articles nil)
524 ;; We opened the second connection (or it was opened already).
525 (setq nntp-async-articles articles)
526 (setq nntp-async-fetched nil)
527 ;; Clear any old data.
529 (set-buffer nntp-async-buffer)
531 ;; Select the correct current group on this server.
532 (nntp-async-send-strings "GROUP" group)
535 (defun nntp-list-active-group (group &optional server)
536 "Return the active info on GROUP (which can be a regexp."
537 (nntp-possibly-change-server group server)
538 (nntp-send-command "^.*\r?\n" "LIST ACTIVE" group))
540 (defun nntp-request-group-description (group &optional server)
541 "Get the description of GROUP."
542 (nntp-possibly-change-server nil server)
544 (nntp-send-command "^.*\r?\n" "XGTITLE" group)
547 (defun nntp-close-group (group &optional server)
549 (setq nntp-current-group nil)
552 (defun nntp-request-list (&optional server)
553 "List all active groups."
554 (nntp-possibly-change-server nil server)
556 (nntp-send-command "^\\.\r?\n" "LIST")
559 (defun nntp-request-list-newsgroups (&optional server)
560 "Get descriptions on all groups on SERVER."
561 (nntp-possibly-change-server nil server)
563 (nntp-send-command "^\\.\r?\n" "LIST NEWSGROUPS")
566 (defun nntp-request-newgroups (date &optional server)
567 "List groups that have arrived since DATE."
568 (nntp-possibly-change-server nil server)
569 (let* ((date (timezone-parse-date date))
571 (format "%s%02d%02d %s%s%s"
572 (substring (aref date 0) 2) (string-to-int (aref date 1))
573 (string-to-int (aref date 2)) (substring (aref date 3) 0 2)
575 (aref date 3) 3 5) (substring (aref date 3) 6 8))))
577 (nntp-send-command "^\\.\r?\n" "NEWGROUPS" time-string)
578 (nntp-decode-text))))
580 (defun nntp-request-list-distributions (&optional server)
581 "List distributions."
582 (nntp-possibly-change-server nil server)
584 (nntp-send-command "^\\.\r?\n" "LIST DISTRIBUTIONS")
587 (defun nntp-request-last (&optional group server)
588 "Decrease the current article pointer."
589 (nntp-possibly-change-server group server)
590 (nntp-send-command "^[23].*\r?\n" "LAST"))
592 (defun nntp-request-next (&optional group server)
593 "Advance the current article pointer."
594 (nntp-possibly-change-server group server)
595 (nntp-send-command "^[23].*\r?\n" "NEXT"))
597 (defun nntp-request-post (&optional server)
598 "Post the current buffer."
599 (nntp-possibly-change-server nil server)
600 (when (nntp-send-command "^[23].*\r?\n" "POST")
602 (nntp-send-region-to-server (point-min) (point-max))
603 ;; 1.2a NNTP's post command is buggy. "^M" (\r) is not
604 ;; appended to end of the status message.
605 (nntp-wait-for-response "^[23].*\n")))
607 ;;; Internal functions.
609 (defun nntp-send-mode-reader ()
610 "Send the MODE READER command to the nntp server.
611 This function is supposed to be called from `nntp-server-opened-hook'.
612 It will make innd servers spawn an nnrpd process to allow actual article
614 (nntp-send-command "^.*\r?\n" "MODE READER"))
616 (defun nntp-send-nosy-authinfo ()
617 "Send the AUTHINFO to the nntp server.
618 This function is supposed to be called from `nntp-server-opened-hook'.
619 It will prompt for a password."
620 (nntp-send-command "^.*\r?\n" "AUTHINFO USER"
621 (read-string "NNTP user name: "))
622 (nntp-send-command "^.*\r?\n" "AUTHINFO PASS"
623 (read-string "NNTP password: ")))
625 (defun nntp-send-authinfo ()
626 "Send the AUTHINFO to the nntp server.
627 This function is supposed to be called from `nntp-server-opened-hook'.
628 It will prompt for a password."
629 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name))
630 (nntp-send-command "^.*\r?\n" "AUTHINFO PASS"
631 (read-string "NNTP password: ")))
633 (defun nntp-send-authinfo-from-file ()
634 "Send the AUTHINFO to the nntp server.
635 This function is supposed to be called from `nntp-server-opened-hook'.
636 It will prompt for a password."
637 (when (file-exists-p "~/.nntp-authinfo")
639 (set-buffer (get-buffer-create " *authinfo*"))
640 (buffer-disable-undo (current-buffer))
642 (insert-file-contents "~/.nntp-authinfo")
643 (goto-char (point-min))
644 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name))
646 "^.*\r?\n" "AUTHINFO PASS"
647 (buffer-substring (point) (progn (end-of-line) (point))))
648 (kill-buffer (current-buffer)))))
650 (defun nntp-default-sentinel (proc status)
651 "Default sentinel function for NNTP server process."
652 (let ((servers nntp-server-alist)
654 ;; Go through the alist of server names and find the name of the
655 ;; server that the process that sent the signal is connected to.
656 ;; If you get my drift.
657 (if (equal proc nntp-server-process)
658 (setq server nntp-address)
660 (not (equal proc (nth 1 (assq 'nntp-server-process
662 (setq servers (cdr servers)))
663 (setq server (car (car servers))))
665 nntp-warn-about-losing-connection)
666 (message "nntp: Connection closed to server %s" server)
669 (defun nntp-kill-connection (server)
670 "Choke the connection to SERVER."
671 (let ((proc (nth 1 (assq 'nntp-server-process
672 (assoc server nntp-server-alist)))))
674 (delete-process (process-name proc)))
675 (nntp-close-server server)
676 (setq nntp-status-string
677 (message "Connection timed out to server %s." server))
681 ;; Encoding and decoding of NNTP text.
683 (defun nntp-decode-text ()
684 "Decode text transmitted by NNTP.
685 0. Delete status line.
686 1. Delete `^M' at end of line.
687 2. Delete `.' at end of buffer (end of text mark).
688 3. Delete `.' at beginning of line."
690 (set-buffer nntp-server-buffer)
691 ;; Insert newline at end of buffer.
692 (goto-char (point-max))
693 (or (bolp) (insert "\n"))
694 ;; Delete status line.
695 (delete-region (goto-char (point-min)) (progn (forward-line 1) (point)))
697 (while (search-forward "\r" nil t)
698 (replace-match "" t t))
699 ;; Delete `.' at end of the buffer (end of text mark).
700 (goto-char (point-max))
702 (when (looking-at "^\\.\n")
703 (delete-region (point) (progn (forward-line 1) (point))))
704 ;; Replace `..' at beginning of line with `.'.
705 (goto-char (point-min))
706 ;; (replace-regexp "^\\.\\." ".")
707 (while (search-forward "\n.." nil t)
710 (defun nntp-encode-text ()
711 "Encode text in current buffer for NNTP transmission.
712 1. Insert `.' at beginning of line.
713 2. Insert `.' at end of buffer (end of text mark)."
715 ;; Replace `.' at beginning of line with `..'.
716 (goto-char (point-min))
717 (while (search-forward "\n." nil t)
719 (goto-char (point-max))
720 ;; Insert newline at end of buffer.
721 (or (bolp) (insert "\n"))
722 ;; Insert `.' at end of buffer (end of text mark).
727 ;;; Synchronous Communication with NNTP servers.
730 (defun nntp-send-command (response cmd &rest args)
731 "Wait for server RESPONSE after sending CMD and optional ARGS to server."
733 ;; Clear communication buffer.
734 (set-buffer nntp-server-buffer)
736 (apply 'nntp-send-strings-to-server cmd args)
738 (nntp-wait-for-response response)
741 (defun nntp-wait-for-response (regexp &optional slow)
742 "Wait for server response which matches REGEXP."
746 (dotnum 0) ;Number of "." being displayed.
747 (dotsize ;How often "." displayed.
748 (if (numberp nntp-debug-read) nntp-debug-read 10000)))
749 (set-buffer nntp-server-buffer)
750 ;; Wait for status response (RFC977).
751 ;; 1xx - Informative message.
753 ;; 3xx - Command ok so far, send the rest of it.
754 ;; 4xx - Command was correct, but couldn't be performed for some
756 ;; 5xx - Command unimplemented, or incorrect, or a serious
757 ;; program error occurred.
758 (nntp-accept-response)
760 (goto-char (point-min))
763 (cond ((re-search-forward "^[23][0-9][0-9]" nil t)
765 ((re-search-forward "^[45][0-9][0-9]" nil t)
768 (t (nntp-accept-response)))
769 (if (not wait) (delete-region (point-min)
770 (progn (beginning-of-line)
772 (cond ((looking-at "[23]")
777 (t (nntp-accept-response)))))
778 ;; Save status message.
780 (setq nntp-status-string
781 (buffer-substring (point-min) (point)))
785 (goto-char (point-max))
787 (if (looking-at regexp)
789 (when nntp-debug-read
790 (let ((newnum (/ (buffer-size) dotsize)))
791 (if (not (= dotnum newnum))
794 (message "NNTP: Reading %s"
795 (make-string dotnum ?.))))))
796 (nntp-accept-response)))
798 (when (and nntp-debug-read (> dotnum 0))
800 ;; Successfully received server response.
806 ;;; Low-Level Interface to NNTP Server.
809 (defun nntp-find-group-and-number ()
812 (set-buffer nntp-server-buffer)
813 (narrow-to-region (goto-char (point-min))
814 (or (search-forward "\n\n" nil t) (point-max)))
815 (goto-char (point-min))
816 ;; We first find the number by looking at the status line.
817 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
819 (buffer-substring (match-beginning 1)
821 group newsgroups xref)
822 (and number (zerop number) (setq number nil))
823 ;; Then we find the group name.
826 ;; If there is only one group in the Newsgroups header,
827 ;; then it seems quite likely that this article comes
828 ;; from that group, I'd say.
829 ((and (setq newsgroups (mail-fetch-field "newsgroups"))
830 (not (string-match "," newsgroups)))
832 ;; If there is more than one group in the Newsgroups
833 ;; header, then the Xref header should be filled out.
834 ;; We hazard a guess that the group that has this
835 ;; article number in the Xref header is the one we are
836 ;; looking for. This might very well be wrong if this
837 ;; article happens to have the same number in several
838 ;; groups, but that's life.
839 ((and (setq xref (mail-fetch-field "xref"))
841 (string-match (format "\\([^ :]+\\):%d" number) xref))
842 (substring xref (match-beginning 1) (match-end 1)))
844 (when (string-match "\r" group)
845 (setq group (substring group 0 (match-beginning 0))))
846 (cons group number)))))
848 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
852 ;; This server does not talk NOV.
853 ((not nntp-server-xover)
856 ;; We don't care about gaps.
857 ((or (not nntp-nov-gap)
859 (nntp-send-xover-command
861 (if (numberp fetch-old)
862 (max 1 (- (car articles) fetch-old))
865 (nntp-last-element articles) 'wait)
867 (goto-char (point-min))
868 (when (looking-at "[1-5][0-9][0-9] ")
869 (delete-region (point) (progn (forward-line 1) (point))))
870 (while (search-forward "\r" nil t)
871 (replace-match "" t t))
872 (goto-char (point-max))
874 (when (looking-at "\\.")
875 (delete-region (point) (progn (forward-line 1) (point)))))
877 ;; We do it the hard way. For each gap, an XOVER command is sent
878 ;; to the server. We do not wait for a reply from the server, we
879 ;; just send them off as fast as we can. That means that we have
880 ;; to count the number of responses we get back to find out when we
881 ;; have gotten all we asked for.
882 ((numberp nntp-nov-gap)
885 (last-point (point-min))
886 (buf (current-buffer))
888 ;; We have to check `nntp-server-xover'. If it gets set to nil,
889 ;; that means that the server does not understand XOVER, but we
890 ;; won't know that until we try.
891 (while (and nntp-server-xover articles)
892 (setq first (car articles))
893 ;; Search forward until we find a gap, or until we run out of
895 (while (and (cdr articles)
896 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
897 (setq articles (cdr articles)))
899 (when (nntp-send-xover-command first (car articles))
900 (setq articles (cdr articles)
903 ;; Every 400 requests we have to read the stream in
904 ;; order to avoid deadlocks.
905 (when (or (null articles) ;All requests have been sent.
906 (zerop (% count nntp-maximum-request)))
907 (accept-process-output)
908 ;; On some Emacs versions the preceding function has
909 ;; a tendency to change the buffer. Perhaps. It's
910 ;; quite difficult to reporduce, because it only
911 ;; seems to happen once in a blue moon.
914 (goto-char last-point)
916 (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
917 (setq received (1+ received)))
918 (setq last-point (point))
920 (accept-process-output)
923 (when nntp-server-xover
924 ;; Wait for the reply from the final command.
925 (goto-char (point-max))
926 (re-search-backward "^[0-9][0-9][0-9] " nil t)
927 (when (looking-at "^[23]")
929 (goto-char (point-max))
931 (not (looking-at "^\\.\r?\n")))
932 (nntp-accept-response)))
934 ;; We remove any "." lines and status lines.
935 (goto-char (point-min))
936 (while (search-forward "\r" nil t)
938 (goto-char (point-min))
939 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")))))
943 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
944 "Send the XOVER command to the server."
945 (let ((range (format "%d-%d" beg end)))
946 (if (stringp nntp-server-xover)
947 ;; If `nntp-server-xover' is a string, then we just send this
950 (nntp-send-command "^\\.\r?\n" nntp-server-xover range)
951 ;; We do not wait for the reply.
952 (nntp-send-strings-to-server nntp-server-xover range))
953 (let ((commands nntp-xover-commands))
954 ;; `nntp-xover-commands' is a list of possible XOVER commands.
955 ;; We try them all until we get at positive response.
956 (while (and commands (eq nntp-server-xover 'try))
957 (nntp-send-command "^\\.\r?\n" (car commands) range)
959 (set-buffer nntp-server-buffer)
960 (goto-char (point-min))
961 (and (looking-at "[23]") ; No error message.
962 ;; We also have to look at the lines. Some buggy
963 ;; servers give back simple lines with just the
964 ;; article number. How... helpful.
967 (looking-at "[0-9]+\t...")) ; More text after number.
968 (setq nntp-server-xover (car commands))))
969 (setq commands (cdr commands)))
970 ;; If none of the commands worked, we disable XOVER.
971 (when (eq nntp-server-xover 'try)
973 (set-buffer nntp-server-buffer)
975 (setq nntp-server-xover nil)))
976 nntp-server-xover))))
978 (defun nntp-send-strings-to-server (&rest strings)
979 "Send STRINGS to the server."
980 (let ((cmd (concat (mapconcat 'identity strings " ") "\r\n")))
981 ;; We open the nntp server if it is down.
982 (or (nntp-server-opened nntp-current-server)
983 (nntp-open-server nntp-current-server)
984 (error (nntp-status-message)))
986 (process-send-string nntp-server-process cmd)
989 (defun nntp-send-region-to-server (begin end)
990 "Send the current buffer region (from BEGIN to END) to the server."
992 ;; If we're not the the nntp server buffer, we copy the region
993 ;; over to that buffer.
994 (if (eq (get-buffer nntp-server-buffer) (current-buffer))
995 (let ((orig (current-buffer)))
996 (set-buffer nntp-server-buffer)
998 (insert-buffer-substring orig begin end))
999 ;; We are in the nntp buffer, so we just narrow it.
1000 (narrow-to-region begin end))
1001 ;; `process-send-region' does not work if the text to be sent is very
1002 ;; large, so we send it piecemeal.
1003 (let ((last (point-min))
1004 (size 100)) ;Size of text sent at once.
1005 (while (/= last (point-max))
1006 (process-send-region
1007 nntp-server-process last (setq last (min (+ last size) (point-max))))
1008 ;; Read any output from the server. May be unnecessary.
1009 (accept-process-output)))
1010 ;; Delete the area we sent.
1011 (delete-region (point-min) (point-max))
1014 (defun nntp-open-server-semi-internal (server &optional service)
1016 If SERVER is nil, use value of environment variable `NNTPSERVER'.
1017 If SERVICE, this this as the port number."
1018 (let ((server (or server (getenv "NNTPSERVER")))
1021 (and nntp-connection-timeout
1023 ((fboundp 'run-at-time)
1024 (run-at-time nntp-connection-timeout
1025 nil 'nntp-kill-connection server))
1026 ((fboundp 'start-itimer)
1027 ;; Not sure if this will work or not, only one way to
1029 (eval '(start-itimer "nntp-timeout"
1031 (nntp-kill-connection server))
1032 nntp-connection-timeout nil)))))))
1034 (set-buffer nntp-server-buffer)
1035 (setq nntp-status-string "")
1036 (message "nntp: Connecting to server on %s..." server)
1037 (cond ((and server (nntp-open-server-internal server service))
1038 (setq nntp-address server)
1041 (nntp-wait-for-response "^[23].*\r?\n" 'slow)
1044 (or status (nntp-close-server-internal server))
1045 (and nntp-server-process
1047 (set-process-sentinel
1048 nntp-server-process 'nntp-default-sentinel)
1049 ;; You can send commands at startup like AUTHINFO here.
1050 ;; Added by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>
1051 (run-hooks 'nntp-server-opened-hook))))
1053 (setq nntp-status-string "NNTP server is not specified."))
1054 (t ; We couldn't open the server.
1055 (setq nntp-status-string
1056 (buffer-substring (point-min) (point-max)))))
1057 (and timer (cancel-timer timer))
1060 (setq nntp-current-server nil
1061 nntp-async-number nil))
1064 (defun nntp-open-server-internal (server &optional service)
1065 "Open connection to news server on SERVER by SERVICE (default is nntp)."
1068 ;; Use TCP/IP stream emulation package if needed.
1069 (or (fboundp 'open-network-stream)
1071 ;; Initialize communication buffer.
1072 (nnheader-init-server-buffer)
1073 (set-buffer nntp-server-buffer)
1076 (funcall nntp-open-server-function server)
1079 (setq nntp-server-process proc)
1080 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1081 (process-kill-without-query proc)
1082 (setq nntp-address server)
1083 ;; It is possible to change kanji-fileio-code in this hook.
1084 (run-hooks 'nntp-server-hook)
1085 (push proc nntp-opened-connections)
1086 nntp-server-process)
1087 (setq nntp-status-string (format "Couldn't open server %s" server))
1090 (defun nntp-open-network-stream (server)
1091 (open-network-stream
1092 "nntpd" nntp-server-buffer server nntp-port-number))
1094 (defun nntp-open-rlogin (server)
1095 (let ((proc (start-process "nntpd" nntp-server-buffer "rsh" server)))
1096 (process-send-string proc (mapconcat 'identity nntp-rlogin-parameters
1098 (process-send-string proc "\n")))
1100 (defun nntp-telnet-to-machine ()
1102 (telnet "localhost")
1103 (goto-char (point-min))
1104 (while (not (re-search-forward "^login: *" nil t))
1106 (goto-char (point-min)))
1107 (goto-char (point-max))
1111 (while (not (re-search-forward ">" nil t))
1114 (goto-char (point-max))
1116 (telnet-send-input)))
1118 (defun nntp-close-server-internal (&optional server)
1119 "Close connection to news server."
1120 (nntp-possibly-change-server nil server)
1121 (if nntp-server-process
1122 (delete-process nntp-server-process))
1123 (setq nntp-server-process nil)
1124 (setq nntp-address ""))
1126 (defun nntp-accept-response ()
1127 "Read response of server.
1128 It is well-known that the communication speed will be much improved by
1129 defining this function as macro."
1130 ;; To deal with server process exiting before
1131 ;; accept-process-output is called.
1132 ;; Suggested by Jason Venner <jason@violet.berkeley.edu>.
1133 ;; This is a copy of `nntp-default-sentinel'.
1134 (let ((buf (current-buffer)))
1136 (if (or (not nntp-server-process)
1137 (not (memq (process-status nntp-server-process) '(open run))))
1138 (error "nntp: Process connection closed; %s" (nntp-status-message))
1139 (if nntp-buggy-select
1141 ;; We cannot use `accept-process-output'.
1142 ;; Fujitsu UTS requires messages during sleep-for.
1143 ;; I don't know why.
1144 (message "NNTP: Reading...")
1147 (condition-case errorcode
1148 (accept-process-output nntp-server-process)
1150 (cond ((string-equal "select error: Invalid argument"
1152 ;; Ignore select error.
1155 (signal (car errorcode) (cdr errorcode))))))))
1158 (defun nntp-last-element (list)
1159 "Return last element of LIST."
1161 (setq list (cdr list)))
1164 (defun nntp-possibly-change-server (newsgroup server &optional connectionless)
1165 "Check whether the virtual server needs changing."
1167 (not (nntp-server-opened server)))
1168 ;; This virtual server isn't open, so we (re)open it here.
1169 (nntp-open-server server nil t))
1171 (not (equal newsgroup nntp-current-group)))
1172 ;; Set the proper current group.
1173 (nntp-request-group newsgroup server)))
1175 (defun nntp-try-list-active (group)
1176 (nntp-list-active-group group)
1178 (set-buffer nntp-server-buffer)
1179 (goto-char (point-min))
1180 (cond ((looking-at "5[0-9]+")
1181 (setq nntp-server-list-active-group nil))
1183 (setq nntp-server-list-active-group t)))))
1185 (defun nntp-async-server-opened ()
1186 (and nntp-async-process
1187 (memq (process-status nntp-async-process) '(open run))))
1189 (defun nntp-async-open-server ()
1191 (set-buffer (generate-new-buffer " *async-nntp*"))
1192 (setq nntp-async-buffer (current-buffer))
1193 (buffer-disable-undo (current-buffer)))
1194 (let ((nntp-server-process nil)
1195 (nntp-server-buffer nntp-async-buffer))
1196 (nntp-open-server-semi-internal nntp-address nntp-port-number)
1197 (if (not (setq nntp-async-process nntp-server-process))
1199 (setq nntp-async-number nil))
1200 (set-process-buffer nntp-async-process nntp-async-buffer))))
1202 (defun nntp-async-fetch-articles (article)
1203 (if (stringp article)
1205 (let ((articles (cdr (memq (assq article nntp-async-articles)
1206 nntp-async-articles)))
1207 (max (cond ((numberp nntp-async-number)
1209 ((eq nntp-async-number t)
1210 (length nntp-async-articles))
1213 (while (and (>= (setq max (1- max)) 0)
1215 (or (memq (setq nart (car (car articles))) nntp-async-fetched)
1217 (nntp-async-send-strings "ARTICLE " (int-to-string nart))
1218 (setq nntp-async-fetched (cons nart nntp-async-fetched))))
1219 (setq articles (cdr articles))))))
1221 (defun nntp-async-send-strings (&rest strings)
1222 (let ((cmd (concat (mapconcat 'identity strings " ") "\r\n")))
1223 (or (nntp-async-server-opened)
1224 (nntp-async-open-server)
1225 (error (nntp-status-message)))
1226 (process-send-string nntp-async-process cmd)))
1228 (defun nntp-async-request-group (group)
1229 (if (equal group nntp-current-group)
1231 (let ((asyncs (assoc group nntp-async-group-alist)))
1232 ;; A new group has been selected, so we push the current state
1233 ;; of async articles on an alist, and pull the old state off.
1234 (setq nntp-async-group-alist
1235 (cons (list nntp-current-group
1236 nntp-async-articles nntp-async-fetched
1238 (delq asyncs nntp-async-group-alist)))
1241 (setq nntp-async-articles (nth 1 asyncs))
1242 (setq nntp-async-fetched (nth 2 asyncs))
1243 (setq nntp-async-process (nth 3 asyncs)))))))
1247 ;;; nntp.el ends here