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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
33 (unless (fboundp 'open-network-stream)
36 (eval-when-compile (require 'cl))
39 (autoload 'news-setup "rnewspost")
40 (autoload 'news-reply-mode "rnewspost")
41 (autoload 'cancel-timer "timer")
42 (autoload 'telnet "telnet" nil t)
43 (autoload 'telnet-send-input "telnet" nil t)
44 (autoload 'timezone-parse-date "timezone"))
46 (defvar nntp-server-hook nil
47 "*Hooks for the NNTP server.
48 If the kanji code of the NNTP server is different from the local kanji
49 code, the correct kanji code of the buffer associated with the NNTP
50 server must be specified as follows:
52 \(setq nntp-server-hook
55 ;; Server's Kanji code is EUC (NEmacs hack).
56 (make-local-variable 'kanji-fileio-code)
57 (setq kanji-fileio-code 0))))
59 If you'd like to change something depending on the server in this
60 hook, use the variable `nntp-address'.")
62 (defvar nntp-server-opened-hook nil
63 "*Hook used for sending commands to the server at startup.
64 The default value is `nntp-send-mode-reader', which makes an innd
65 server spawn an nnrpd server. Another useful function to put in this
66 hook might be `nntp-send-authinfo', which will prompt for a password
67 to allow posting from the server. Note that this is only necessary to
68 do on servers that use strict access control.")
69 (add-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)
71 (defvar nntp-server-action-alist
72 '(("nntpd 1\\.5\\.11t"
73 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)))
74 "Alist of regexps to match on server types and actions to be taken.
75 For instance, if you want Gnus to beep every time you connect
76 to innd, you could say something like:
78 \(setq nntp-server-action-alist
81 You probably don't want to do that, though.")
83 (defvar nntp-open-server-function 'nntp-open-network-stream
84 "*Function used for connecting to a remote system.
85 It will be called with the address of the remote system.
87 Two pre-made functions are `nntp-open-network-stream', which is the
88 default, and simply connects to some port or other on the remote
89 system (see nntp-port-number). The other is `nntp-open-rlogin', which
90 does an rlogin on the remote system, and then does a telnet to the
91 NNTP server available there (see nntp-rlogin-parameters).")
93 (defvar nntp-rlogin-parameters '("telnet" "${NNTPSERVER:=localhost}" "nntp")
94 "*Parameters to `nntp-open-login'.
95 That function may be used as `nntp-open-server-function'. In that
96 case, this list will be used as the parameter list given to rsh.")
98 (defvar nntp-rlogin-user-name nil
99 "*User name on remote system when using the rlogin connect method.")
101 (defvar nntp-address nil
102 "*The name of the NNTP server.")
104 (defvar nntp-port-number "nntp"
105 "*Port number to connect to.")
107 (defvar nntp-large-newsgroup 50
108 "*The number of the articles which indicates a large newsgroup.
109 If the number of the articles is greater than the value, verbose
110 messages will be shown to indicate the current status.")
112 (defvar nntp-buggy-select (memq system-type '(fujitsu-uts))
113 "*t if your select routine is buggy.
114 If the select routine signals error or fall into infinite loop while
115 waiting for the server response, the variable must be set to t. In
116 case of Fujitsu UTS, it is set to T since `accept-process-output'
117 doesn't work properly.")
119 (defvar nntp-maximum-request 400
120 "*The maximum number of the requests sent to the NNTP server at one time.
121 If Emacs hangs up while retrieving headers, set the variable to a
124 (defvar nntp-debug-read 10000
125 "*Display '...' every 10Kbytes of a message being received if it is non-nil.
126 If it is a number, dots are displayed per the number.")
128 (defvar nntp-nov-is-evil nil
129 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
131 (defvar nntp-xover-commands '("XOVER" "XOVERVIEW")
132 "*List of strings that are used as commands to fetch NOV lines from a server.
133 The strings are tried in turn until a positive response is gotten. If
134 none of the commands are successful, nntp will just grab headers one
137 (defvar nntp-nov-gap 20
138 "*Maximum allowed gap between two articles.
139 If the gap between two consecutive articles is bigger than this
140 variable, split the XOVER request into two requests.")
142 (defvar nntp-connection-timeout nil
143 "*Number of seconds to wait before an nntp connection times out.
144 If this variable is nil, which is the default, no timers are set.")
146 (defvar nntp-news-default-headers nil
147 "*If non-nil, override `mail-default-headers' when posting news.")
149 (defvar nntp-prepare-server-hook nil
150 "*Hook run before a server is opened.
151 If can be used to set up a server remotely, for instance. Say you
152 have an account at the machine \"other.machine\". This machine has
153 access to an NNTP server that you can't access locally. You could
154 then use this hook to rsh to the remote machine and start a proxy NNTP
155 server there that you can connect to.")
157 (defvar nntp-async-number 5
158 "*How many articles should be prefetched when in asynchronous mode.")
160 (defvar nntp-warn-about-losing-connection t
161 "*If non-nil, beep when a server closes connection.")
165 (defconst nntp-version "nntp 4.0"
166 "Version numbers of this version of NNTP.")
168 (defvar nntp-server-buffer nil
169 "Buffer associated with the NNTP server process.")
171 (defvar nntp-server-process nil
172 "The NNTP server process.
173 You'd better not use this variable in NNTP front-end program, but
174 instead use `nntp-server-buffer'.")
176 (defvar nntp-status-string nil
177 "Save the server response message.
178 You'd better not use this variable in NNTP front-end program but
179 instead call function `nntp-status-message' to get status message.")
181 (defvar nntp-opened-connections nil
182 "All (possibly) opened connections.")
184 (defvar nntp-server-xover 'try)
185 (defvar nntp-server-list-active-group 'try)
186 (defvar nntp-current-group "")
187 (defvar nntp-server-type nil)
189 (defvar nntp-async-process nil)
190 (defvar nntp-async-buffer nil)
191 (defvar nntp-async-articles nil)
192 (defvar nntp-async-fetched nil)
193 (defvar nntp-async-group-alist nil)
197 (defvar nntp-current-server nil)
198 (defvar nntp-server-alist nil)
199 (defvar nntp-server-variables
200 `((nntp-server-hook ,nntp-server-hook)
201 (nntp-server-opened-hook ,nntp-server-opened-hook)
202 (nntp-port-number ,nntp-port-number)
203 (nntp-address ,nntp-address)
204 (nntp-large-newsgroup ,nntp-large-newsgroup)
205 (nntp-buggy-select ,nntp-buggy-select)
206 (nntp-maximum-request ,nntp-maximum-request)
207 (nntp-debug-read ,nntp-debug-read)
208 (nntp-nov-is-evil ,nntp-nov-is-evil)
209 (nntp-xover-commands ,nntp-xover-commands)
210 (nntp-connection-timeout ,nntp-connection-timeout)
211 (nntp-news-default-headers ,nntp-news-default-headers)
212 (nntp-prepare-server-hook ,nntp-prepare-server-hook)
213 (nntp-async-number ,nntp-async-number)
214 (nntp-server-type nil)
215 (nntp-async-process nil)
216 (nntp-async-buffer nil)
217 (nntp-async-articles nil)
218 (nntp-async-fetched nil)
219 (nntp-async-group-alist nil)
220 (nntp-server-process nil)
221 (nntp-status-string nil)
222 (nntp-server-xover try)
223 (nntp-server-list-active-group try)
224 (nntp-current-group "")))
227 ;;; Interface functions.
229 (defun nntp-retrieve-headers (articles &optional group server fetch-old)
230 "Retrieve the headers of ARTICLES."
231 (nntp-possibly-change-server group server)
233 (set-buffer nntp-server-buffer)
235 (if (and (not gnus-nov-is-evil)
236 (not nntp-nov-is-evil)
237 (nntp-retrieve-headers-with-xover articles fetch-old))
238 ;; We successfully retrieved the headers via XOVER.
240 ;; XOVER didn't work, so we do it the hard, slow and inefficient
242 (let ((number (length articles))
245 (last-point (point-min)))
246 ;; Send HEAD command.
248 (nntp-send-strings-to-server
249 "HEAD" (if (numberp (car articles))
250 (int-to-string (car articles))
251 ;; `articles' is either a list of article numbers
252 ;; or a list of article IDs.
254 (setq articles (cdr articles)
256 ;; Every 400 header requests we have to read the stream in
257 ;; order to avoid deadlocks.
258 (when (or (null articles) ;All requests have been sent.
259 (zerop (% count nntp-maximum-request)))
260 (nntp-accept-response)
262 (goto-char last-point)
264 (while (re-search-forward "^[0-9]" nil t)
265 (setq received (1+ received)))
266 (setq last-point (point))
268 ;; If number of headers is greater than 100, give
269 ;; informative messages.
270 (and (numberp nntp-large-newsgroup)
271 (> number nntp-large-newsgroup)
272 (zerop (% received 20))
273 (message "NNTP: Receiving headers... %d%%"
274 (/ (* received 100) number)))
275 (nntp-accept-response))))
276 ;; Wait for text of last command.
277 (goto-char (point-max))
278 (re-search-backward "^[0-9]" nil t)
279 (when (looking-at "^[23]")
281 (goto-char (- (point-max) 3))
282 (not (looking-at "^\\.\r?\n")))
283 (nntp-accept-response)))
284 (and (numberp nntp-large-newsgroup)
285 (> number nntp-large-newsgroup)
286 (message "NNTP: Receiving headers...done"))
288 ;; Now all of replies are received. Fold continuation lines.
289 (nnheader-fold-continuation-lines)
290 ;; Remove all "\r"'s.
291 (goto-char (point-min))
292 (while (search-forward "\r" nil t)
293 (replace-match "" t t))
297 (defun nntp-retrieve-groups (groups &optional server)
298 "Retrieve group info on GROUPS."
299 (nntp-possibly-change-server nil server)
301 (set-buffer nntp-server-buffer)
302 ;; The first time this is run, this variable is `try'. So we
304 (when (eq nntp-server-list-active-group 'try)
305 (nntp-try-list-active (car groups)))
309 (last-point (point-min))
310 (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
312 ;; Send the command to the server.
313 (nntp-send-strings-to-server command (car groups))
314 (setq groups (cdr groups))
315 (setq count (1+ count))
316 ;; Every 400 requests we have to read the stream in
317 ;; order to avoid deadlocks.
318 (when (or (null groups) ;All requests have been sent.
319 (zerop (% count nntp-maximum-request)))
320 (nntp-accept-response)
322 (goto-char last-point)
324 (while (re-search-forward "^[0-9]" nil t)
325 (setq received (1+ received)))
326 (setq last-point (point))
328 (nntp-accept-response))))
330 ;; Wait for the reply from the final command.
331 (when nntp-server-list-active-group
332 (goto-char (point-max))
333 (re-search-backward "^[0-9]" nil t)
334 (when (looking-at "^[23]")
336 (goto-char (- (point-max) 3))
337 (not (looking-at "^\\.\r?\n")))
338 (nntp-accept-response))))
340 ;; Now all replies are received. We remove CRs.
341 (goto-char (point-min))
342 (while (search-forward "\r" nil t)
343 (replace-match "" t t))
345 (if (not nntp-server-list-active-group)
347 ;; We have read active entries, so we just delete the
349 (goto-char (point-min))
350 (while (re-search-forward "^[.2-5]" nil t)
351 (delete-region (match-beginning 0)
352 (progn (forward-line 1) (point))))
355 (defun nntp-open-server (server &optional defs connectionless)
356 "Open the virtual server SERVER.
357 If CONNECTIONLESS is non-nil, don't attempt to connect to any physical
359 (nnheader-init-server-buffer)
360 (if (nntp-server-opened server)
362 (if (or (stringp (car defs))
363 (numberp (car defs)))
364 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
365 (or (assq 'nntp-address defs)
366 (setq defs (append defs (list (list 'nntp-address server)))))
367 (if (and nntp-current-server
368 (not (equal server nntp-current-server)))
369 (setq nntp-server-alist
370 (cons (list nntp-current-server
371 (nnheader-save-variables nntp-server-variables))
373 (let ((state (assoc server nntp-server-alist)))
376 (nnheader-restore-variables (nth 1 state))
377 (setq nntp-server-alist (delq state nntp-server-alist)))
378 (nnheader-set-init-variables nntp-server-variables defs)))
379 (setq nntp-current-server server)
380 (or (nntp-server-opened server)
383 (run-hooks 'nntp-prepare-server-hook)
384 (nntp-open-server-semi-internal nntp-address nntp-port-number)))))
386 (defun nntp-close-server (&optional server)
387 "Close connection to SERVER."
388 (nntp-possibly-change-server nil server t)
391 ;; Un-set default sentinel function before closing connection.
392 (and nntp-server-process
393 (eq 'nntp-default-sentinel
394 (process-sentinel nntp-server-process))
395 (set-process-sentinel nntp-server-process nil))
396 ;; We cannot send QUIT command unless the process is running.
397 (if (nntp-server-opened)
398 (nntp-send-command nil "QUIT")))
399 (nntp-close-server-internal server)))
401 (defalias 'nntp-request-quit (symbol-function 'nntp-close-server))
403 (defun nntp-request-close ()
404 "Close all server connections."
406 (while nntp-opened-connections
407 (when (setq proc (pop nntp-opened-connections))
409 (process-send-string proc "QUIT\n")
411 (delete-process proc)))
412 (and nntp-async-buffer
413 (get-buffer nntp-async-buffer)
414 (kill-buffer nntp-async-buffer))
415 (while nntp-server-alist
416 (and (setq proc (nth 1 (assq 'nntp-async-buffer
417 (car nntp-server-alist))))
420 (setq nntp-server-alist (cdr nntp-server-alist)))
421 (setq nntp-current-server nil
422 nntp-async-group-alist nil)))
424 (defun nntp-server-opened (&optional server)
425 "Say whether a connection to SERVER has been opened."
426 (and (equal server nntp-current-server)
428 (buffer-name nntp-server-buffer)
430 (memq (process-status nntp-server-process) '(open run))))
432 (defun nntp-status-message (&optional server)
433 "Return server status as a string."
434 (if (and nntp-status-string
436 (string-match "[0-9][0-9][0-9][ \t]+\\([^\r]*\\).*$"
438 (substring nntp-status-string (match-beginning 1) (match-end 1))
439 ;; Empty message if nothing.
440 (or nntp-status-string "")))
442 (defun nntp-request-article (id &optional group server buffer)
443 "Request article ID (Message-ID or number)."
444 (nntp-possibly-change-server group server)
448 ;; First we see whether we can get the article from the async buffer.
449 (when (and (numberp id)
451 (memq id nntp-async-fetched))
453 (set-buffer nntp-async-buffer)
454 (let ((opoint (point))
455 (art (if (numberp id) (int-to-string id) id))
457 (when (and (or (re-search-forward (concat "^2.. +" art) nil t)
459 (goto-char (point-min))
460 (re-search-forward (concat "^2.. +" art) opoint t)))
464 end (re-search-forward "^\\.\r?\n" nil t))))
467 (set-buffer (or buffer nntp-server-buffer))
469 (insert-buffer-substring nntp-async-buffer beg end)
470 (let ((nntp-server-buffer (current-buffer)))
472 (delete-region beg end)
473 (when nntp-async-articles
474 (nntp-async-fetch-articles id))))))
478 ;; The article was not in the async buffer, so we fetch it now.
481 (if buffer (set-process-buffer nntp-server-process buffer))
482 (let ((nntp-server-buffer (or buffer nntp-server-buffer))
483 (art (or (and (numberp id) (int-to-string id)) id)))
485 (and (nntp-send-command "^\\.\r?\n" "ARTICLE" art)
487 (cons nntp-current-group id)
488 ;; We find out what the article number was.
489 (nntp-find-group-and-number)))
491 (and nntp-async-articles (nntp-async-fetch-articles id)))))
493 (set-process-buffer nntp-server-process nntp-server-buffer))))))
495 (defun nntp-request-body (id &optional group server)
496 "Request body of article ID (Message-ID or number)."
497 (nntp-possibly-change-server group server)
499 ;; If NEmacs, end of message may look like: "\256\215" (".^M")
501 "^\\.\r?\n" "BODY" (or (and (numberp id) (int-to-string id)) id))
504 (defun nntp-request-head (id &optional group server)
505 "Request head of article ID (Message-ID or number)."
506 (nntp-possibly-change-server group server)
508 (and (nntp-send-command
509 "^\\.\r?\n" "HEAD" (if (numberp id) (int-to-string id) id))
511 ;; We find out what the article number was.
512 (nntp-find-group-and-number)))
515 (defun nntp-request-stat (id &optional group server)
516 "Request STAT of article ID (Message-ID or number)."
517 (nntp-possibly-change-server group server)
519 "^[23].*\r?\n" "STAT" (or (and (numberp id) (int-to-string id)) id)))
521 (defun nntp-request-type (group &optional article)
524 (defun nntp-request-group (group &optional server dont-check)
526 (setq nntp-current-group
527 (when (nntp-send-command "^2.*\r?\n" "GROUP" group)
530 (defun nntp-request-asynchronous (group &optional server articles)
531 "Enable pre-fetch in GROUP."
532 (when nntp-async-articles
533 (nntp-async-request-group group))
534 (when nntp-async-number
535 (if (not (or (nntp-async-server-opened)
536 (nntp-async-open-server)))
537 ;; Couldn't open the second connection
539 (message "Can't open second connection to %s" nntp-address)
541 (setq nntp-async-articles nil)
543 ;; We opened the second connection (or it was opened already).
544 (setq nntp-async-articles articles)
545 (setq nntp-async-fetched nil)
546 ;; Clear any old data.
548 (set-buffer nntp-async-buffer)
550 ;; Select the correct current group on this server.
551 (nntp-async-send-strings "GROUP" group)
554 (defun nntp-list-active-group (group &optional server)
555 "Return the active info on GROUP (which can be a regexp."
556 (nntp-possibly-change-server group server)
557 (nntp-send-command "^.*\r?\n" "LIST ACTIVE" group))
559 (defun nntp-request-group-description (group &optional server)
560 "Get the description of GROUP."
561 (nntp-possibly-change-server nil server)
563 (nntp-send-command "^.*\r?\n" "XGTITLE" group)
566 (defun nntp-close-group (group &optional server)
568 (setq nntp-current-group nil)
571 (defun nntp-request-list (&optional server)
572 "List all active groups."
573 (nntp-possibly-change-server nil server)
575 (nntp-send-command "^\\.\r?\n" "LIST")
578 (defun nntp-request-list-newsgroups (&optional server)
579 "Get descriptions on all groups on SERVER."
580 (nntp-possibly-change-server nil server)
582 (nntp-send-command "^\\.\r?\n" "LIST NEWSGROUPS")
585 (defun nntp-request-newgroups (date &optional server)
586 "List groups that have arrived since DATE."
587 (nntp-possibly-change-server nil server)
588 (let* ((date (timezone-parse-date date))
590 (format "%s%02d%02d %s%s%s"
591 (substring (aref date 0) 2) (string-to-int (aref date 1))
592 (string-to-int (aref date 2)) (substring (aref date 3) 0 2)
594 (aref date 3) 3 5) (substring (aref date 3) 6 8))))
596 (nntp-send-command "^\\.\r?\n" "NEWGROUPS" time-string)
597 (nntp-decode-text))))
599 (defun nntp-request-list-distributions (&optional server)
600 "List distributions."
601 (nntp-possibly-change-server nil server)
603 (nntp-send-command "^\\.\r?\n" "LIST DISTRIBUTIONS")
606 (defun nntp-request-last (&optional group server)
607 "Decrease the current article pointer."
608 (nntp-possibly-change-server group server)
609 (nntp-send-command "^[23].*\r?\n" "LAST"))
611 (defun nntp-request-next (&optional group server)
612 "Advance the current article pointer."
613 (nntp-possibly-change-server group server)
614 (nntp-send-command "^[23].*\r?\n" "NEXT"))
616 (defun nntp-request-post (&optional server)
617 "Post the current buffer."
618 (nntp-possibly-change-server nil server)
619 (when (nntp-send-command "^[23].*\r?\n" "POST")
621 (nntp-send-region-to-server (point-min) (point-max))
622 ;; 1.2a NNTP's post command is buggy. "^M" (\r) is not
623 ;; appended to end of the status message.
624 (nntp-wait-for-response "^[23].*\n")))
626 ;;; Internal functions.
628 (defun nntp-send-mode-reader ()
629 "Send the MODE READER command to the nntp server.
630 This function is supposed to be called from `nntp-server-opened-hook'.
631 It will make innd servers spawn an nnrpd process to allow actual article
633 (nntp-send-command "^.*\r?\n" "MODE READER"))
635 (defun nntp-send-nosy-authinfo ()
636 "Send the AUTHINFO to the nntp server.
637 This function is supposed to be called from `nntp-server-opened-hook'.
638 It will prompt for a password."
639 (nntp-send-command "^.*\r?\n" "AUTHINFO USER"
640 (read-string "NNTP user name: "))
641 (nntp-send-command "^.*\r?\n" "AUTHINFO PASS"
642 (read-string "NNTP password: ")))
644 (defun nntp-send-authinfo ()
645 "Send the AUTHINFO to the nntp server.
646 This function is supposed to be called from `nntp-server-opened-hook'.
647 It will prompt for a password."
648 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name))
649 (nntp-send-command "^.*\r?\n" "AUTHINFO PASS"
650 (read-string "NNTP password: ")))
652 (defun nntp-send-authinfo-from-file ()
653 "Send the AUTHINFO to the nntp server.
654 This function is supposed to be called from `nntp-server-opened-hook'.
655 It will prompt for a password."
656 (when (file-exists-p "~/.nntp-authinfo")
658 (set-buffer (get-buffer-create " *authinfo*"))
659 (buffer-disable-undo (current-buffer))
661 (insert-file-contents "~/.nntp-authinfo")
662 (goto-char (point-min))
663 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name))
665 "^.*\r?\n" "AUTHINFO PASS"
666 (buffer-substring (point) (progn (end-of-line) (point))))
667 (kill-buffer (current-buffer)))))
669 (defun nntp-default-sentinel (proc status)
670 "Default sentinel function for NNTP server process."
671 (let ((servers nntp-server-alist)
673 ;; Go through the alist of server names and find the name of the
674 ;; server that the process that sent the signal is connected to.
675 ;; If you get my drift.
676 (if (equal proc nntp-server-process)
677 (setq server nntp-address)
679 (not (equal proc (nth 1 (assq 'nntp-server-process
681 (setq servers (cdr servers)))
682 (setq server (car (car servers))))
684 nntp-warn-about-losing-connection)
685 (message "nntp: Connection closed to server %s" server)
688 (defun nntp-kill-connection (server)
689 "Choke the connection to SERVER."
690 (let ((proc (nth 1 (assq 'nntp-server-process
691 (assoc server nntp-server-alist)))))
693 (delete-process (process-name proc)))
694 (nntp-close-server server)
695 (setq nntp-status-string
696 (message "Connection timed out to server %s." server))
700 ;; Encoding and decoding of NNTP text.
702 (defun nntp-decode-text ()
703 "Decode text transmitted by NNTP.
704 0. Delete status line.
705 1. Delete `^M' at end of line.
706 2. Delete `.' at end of buffer (end of text mark).
707 3. Delete `.' at beginning of line."
709 (set-buffer nntp-server-buffer)
710 ;; Insert newline at end of buffer.
711 (goto-char (point-max))
712 (or (bolp) (insert "\n"))
713 ;; Delete status line.
714 (delete-region (goto-char (point-min)) (progn (forward-line 1) (point)))
716 (while (search-forward "\r" nil t)
717 (replace-match "" t t))
718 ;; Delete `.' at end of the buffer (end of text mark).
719 (goto-char (point-max))
721 (when (looking-at "^\\.\n")
722 (delete-region (point) (progn (forward-line 1) (point))))
723 ;; Replace `..' at beginning of line with `.'.
724 (goto-char (point-min))
725 ;; (replace-regexp "^\\.\\." ".")
726 (while (search-forward "\n.." nil t)
729 (defun nntp-encode-text ()
730 "Encode text in current buffer for NNTP transmission.
731 1. Insert `.' at beginning of line.
732 2. Insert `.' at end of buffer (end of text mark)."
734 ;; Replace `.' at beginning of line with `..'.
735 (goto-char (point-min))
736 (while (search-forward "\n." nil t)
738 (goto-char (point-max))
739 ;; Insert newline at end of buffer.
740 (or (bolp) (insert "\n"))
741 ;; Insert `.' at end of buffer (end of text mark).
746 ;;; Synchronous Communication with NNTP servers.
749 (defun nntp-send-command (response cmd &rest args)
750 "Wait for server RESPONSE after sending CMD and optional ARGS to server."
752 ;; Clear communication buffer.
753 (set-buffer nntp-server-buffer)
755 (apply 'nntp-send-strings-to-server cmd args)
757 (nntp-wait-for-response response)
760 (defun nntp-wait-for-response (regexp &optional slow)
761 "Wait for server response which matches REGEXP."
765 (dotnum 0) ;Number of "." being displayed.
766 (dotsize ;How often "." displayed.
767 (if (numberp nntp-debug-read) nntp-debug-read 10000)))
768 (set-buffer nntp-server-buffer)
769 ;; Wait for status response (RFC977).
770 ;; 1xx - Informative message.
772 ;; 3xx - Command ok so far, send the rest of it.
773 ;; 4xx - Command was correct, but couldn't be performed for some
775 ;; 5xx - Command unimplemented, or incorrect, or a serious
776 ;; program error occurred.
777 (nntp-accept-response)
779 (goto-char (point-min))
782 (cond ((re-search-forward "^[23][0-9][0-9]" nil t)
784 ((re-search-forward "^[45][0-9][0-9]" nil t)
787 (t (nntp-accept-response)))
788 (if (not wait) (delete-region (point-min)
789 (progn (beginning-of-line)
791 (cond ((looking-at "[23]")
796 (t (nntp-accept-response)))))
797 ;; Save status message.
799 (setq nntp-status-string
800 (buffer-substring (point-min) (point)))
804 (goto-char (point-max))
806 (if (looking-at regexp)
808 (when nntp-debug-read
809 (let ((newnum (/ (buffer-size) dotsize)))
810 (if (not (= dotnum newnum))
813 (message "NNTP: Reading %s"
814 (make-string dotnum ?.))))))
815 (nntp-accept-response)))
817 (when (and nntp-debug-read (> dotnum 0))
819 ;; Successfully received server response.
825 ;;; Low-Level Interface to NNTP Server.
828 (defun nntp-find-group-and-number ()
831 (set-buffer nntp-server-buffer)
832 (narrow-to-region (goto-char (point-min))
833 (or (search-forward "\n\n" nil t) (point-max)))
834 (goto-char (point-min))
835 ;; We first find the number by looking at the status line.
836 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
838 (buffer-substring (match-beginning 1)
840 group newsgroups xref)
841 (and number (zerop number) (setq number nil))
842 ;; Then we find the group name.
845 ;; If there is only one group in the Newsgroups header,
846 ;; then it seems quite likely that this article comes
847 ;; from that group, I'd say.
848 ((and (setq newsgroups (mail-fetch-field "newsgroups"))
849 (not (string-match "," newsgroups)))
851 ;; If there is more than one group in the Newsgroups
852 ;; header, then the Xref header should be filled out.
853 ;; We hazard a guess that the group that has this
854 ;; article number in the Xref header is the one we are
855 ;; looking for. This might very well be wrong if this
856 ;; article happens to have the same number in several
857 ;; groups, but that's life.
858 ((and (setq xref (mail-fetch-field "xref"))
860 (string-match (format "\\([^ :]+\\):%d" number) xref))
861 (substring xref (match-beginning 1) (match-end 1)))
863 (when (string-match "\r" group)
864 (setq group (substring group 0 (match-beginning 0))))
865 (cons group number)))))
867 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
871 ;; This server does not talk NOV.
872 ((not nntp-server-xover)
875 ;; We don't care about gaps.
876 ((or (not nntp-nov-gap)
878 (nntp-send-xover-command
880 (if (numberp fetch-old)
881 (max 1 (- (car articles) fetch-old))
884 (nntp-last-element articles) 'wait)
886 (goto-char (point-min))
887 (when (looking-at "[1-5][0-9][0-9] ")
888 (delete-region (point) (progn (forward-line 1) (point))))
889 (while (search-forward "\r" nil t)
890 (replace-match "" t t))
891 (goto-char (point-max))
893 (when (looking-at "\\.")
894 (delete-region (point) (progn (forward-line 1) (point)))))
896 ;; We do it the hard way. For each gap, an XOVER command is sent
897 ;; to the server. We do not wait for a reply from the server, we
898 ;; just send them off as fast as we can. That means that we have
899 ;; to count the number of responses we get back to find out when we
900 ;; have gotten all we asked for.
901 ((numberp nntp-nov-gap)
904 (last-point (point-min))
905 (buf (current-buffer))
907 ;; We have to check `nntp-server-xover'. If it gets set to nil,
908 ;; that means that the server does not understand XOVER, but we
909 ;; won't know that until we try.
910 (while (and nntp-server-xover articles)
911 (setq first (car articles))
912 ;; Search forward until we find a gap, or until we run out of
914 (while (and (cdr articles)
915 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
916 (setq articles (cdr articles)))
918 (when (nntp-send-xover-command first (car articles))
919 (setq articles (cdr articles)
922 ;; Every 400 requests we have to read the stream in
923 ;; order to avoid deadlocks.
924 (when (or (null articles) ;All requests have been sent.
925 (zerop (% count nntp-maximum-request)))
926 (accept-process-output)
927 ;; On some Emacs versions the preceding function has
928 ;; a tendency to change the buffer. Perhaps. It's
929 ;; quite difficult to reproduce, because it only
930 ;; seems to happen once in a blue moon.
933 (goto-char last-point)
935 (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
936 (setq received (1+ received)))
937 (setq last-point (point))
939 (accept-process-output)
942 (when nntp-server-xover
943 ;; Wait for the reply from the final command.
944 (goto-char (point-max))
945 (re-search-backward "^[0-9][0-9][0-9] " nil t)
946 (when (looking-at "^[23]")
948 (goto-char (point-max))
950 (not (looking-at "^\\.\r?\n")))
951 (nntp-accept-response)))
953 ;; We remove any "." lines and status lines.
954 (goto-char (point-min))
955 (while (search-forward "\r" nil t)
957 (goto-char (point-min))
958 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")))))
962 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
963 "Send the XOVER command to the server."
964 (let ((range (format "%d-%d" beg end)))
965 (if (stringp nntp-server-xover)
966 ;; If `nntp-server-xover' is a string, then we just send this
969 (nntp-send-command "^\\.\r?\n" nntp-server-xover range)
970 ;; We do not wait for the reply.
971 (nntp-send-strings-to-server nntp-server-xover range))
972 (let ((commands nntp-xover-commands))
973 ;; `nntp-xover-commands' is a list of possible XOVER commands.
974 ;; We try them all until we get at positive response.
975 (while (and commands (eq nntp-server-xover 'try))
976 (nntp-send-command "^\\.\r?\n" (car commands) range)
978 (set-buffer nntp-server-buffer)
979 (goto-char (point-min))
980 (and (looking-at "[23]") ; No error message.
981 ;; We also have to look at the lines. Some buggy
982 ;; servers give back simple lines with just the
983 ;; article number. How... helpful.
986 (looking-at "[0-9]+\t...")) ; More text after number.
987 (setq nntp-server-xover (car commands))))
988 (setq commands (cdr commands)))
989 ;; If none of the commands worked, we disable XOVER.
990 (when (eq nntp-server-xover 'try)
992 (set-buffer nntp-server-buffer)
994 (setq nntp-server-xover nil)))
995 nntp-server-xover))))
997 (defun nntp-send-strings-to-server (&rest strings)
998 "Send STRINGS to the server."
999 (let ((cmd (concat (mapconcat 'identity strings " ") "\r\n")))
1000 ;; We open the nntp server if it is down.
1001 (or (nntp-server-opened nntp-current-server)
1002 (nntp-open-server nntp-current-server)
1003 (error (nntp-status-message)))
1004 ;; Send the strings.
1005 (process-send-string nntp-server-process cmd)
1008 (defun nntp-send-region-to-server (begin end)
1009 "Send the current buffer region (from BEGIN to END) to the server."
1011 ;; If we're not the the nntp server buffer, we copy the region
1012 ;; over to that buffer.
1013 (if (eq (get-buffer nntp-server-buffer) (current-buffer))
1014 (let ((orig (current-buffer)))
1015 (set-buffer nntp-server-buffer)
1017 (insert-buffer-substring orig begin end))
1018 ;; We are in the nntp buffer, so we just narrow it.
1019 (narrow-to-region begin end))
1020 ;; `process-send-region' does not work if the text to be sent is very
1021 ;; large, so we send it piecemeal.
1022 (let ((last (point-min))
1023 (size 100)) ;Size of text sent at once.
1024 (while (/= last (point-max))
1025 (process-send-region
1026 nntp-server-process last (setq last (min (+ last size) (point-max))))
1027 ;; Read any output from the server. May be unnecessary.
1028 (accept-process-output)))
1029 ;; Delete the area we sent.
1030 (delete-region (point-min) (point-max))
1033 (defun nntp-open-server-semi-internal (server &optional service)
1035 If SERVER is nil, use value of environment variable `NNTPSERVER'.
1036 If SERVICE, this this as the port number."
1037 (let ((server (or server (getenv "NNTPSERVER")))
1040 (and nntp-connection-timeout
1042 ((fboundp 'run-at-time)
1043 (run-at-time nntp-connection-timeout
1044 nil 'nntp-kill-connection server))
1045 ((fboundp 'start-itimer)
1046 ;; Not sure if this will work or not, only one way to
1048 (eval '(start-itimer "nntp-timeout"
1050 (nntp-kill-connection server))
1051 nntp-connection-timeout nil)))))))
1053 (set-buffer nntp-server-buffer)
1054 (setq nntp-status-string "")
1055 (message "nntp: Connecting to server on %s..." server)
1056 (cond ((and server (nntp-open-server-internal server service))
1057 (setq nntp-address server)
1060 (nntp-wait-for-response "^[23].*\r?\n" 'slow)
1063 (or status (nntp-close-server-internal server))
1064 (and nntp-server-process
1066 (set-process-sentinel
1067 nntp-server-process 'nntp-default-sentinel)
1068 ;; You can send commands at startup like AUTHINFO here.
1069 ;; Added by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>
1070 (run-hooks 'nntp-server-opened-hook))))
1072 (setq nntp-status-string "NNTP server is not specified."))
1073 (t ; We couldn't open the server.
1074 (setq nntp-status-string
1075 (buffer-substring (point-min) (point-max)))))
1076 (and timer (cancel-timer timer))
1079 (setq nntp-current-server nil
1080 nntp-async-number nil))
1083 (defun nntp-open-server-internal (server &optional service)
1084 "Open connection to news server on SERVER by SERVICE (default is nntp)."
1087 (set-buffer nntp-server-buffer)
1091 (funcall nntp-open-server-function server)
1093 (memq (process-status proc) '(open run)))
1094 (setq nntp-server-process proc)
1095 (setq nntp-address server)
1096 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1097 (process-kill-without-query proc)
1098 (run-hooks 'nntp-server-hook)
1099 (push proc nntp-opened-connections)
1100 (nntp-read-server-type)
1101 nntp-server-process)
1103 (nnheader-report 'nntp (format "Couldn't open server %s" server)))))))
1105 (defun nntp-read-server-type ()
1106 "Find out what the name of the server we have connected to is."
1107 ;; Wait for the status string to arrive.
1108 (nntp-wait-for-response "^.*\n")
1109 (setq nntp-server-type (buffer-string))
1110 (let ((alist nntp-server-action-alist)
1112 ;; Run server-specific commmands.
1114 (setq entry (pop alist))
1115 (when (string-match (car entry) nntp-server-type)
1116 (if (and (listp (cadr entry))
1117 (not (eq 'lambda (caadr entry))))
1119 (funcall (cadr entry)))))))
1121 (defun nntp-open-network-stream (server)
1122 (open-network-stream
1123 "nntpd" nntp-server-buffer server nntp-port-number))
1125 (defun nntp-open-rlogin (server)
1126 (let ((proc (start-process "nntpd" nntp-server-buffer "rsh" server)))
1127 (process-send-string proc (mapconcat 'identity nntp-rlogin-parameters
1129 (process-send-string proc "\n")))
1131 (defun nntp-telnet-to-machine ()
1133 (telnet "localhost")
1134 (goto-char (point-min))
1135 (while (not (re-search-forward "^login: *" nil t))
1137 (goto-char (point-min)))
1138 (goto-char (point-max))
1142 (while (not (re-search-forward ">" nil t))
1145 (goto-char (point-max))
1147 (telnet-send-input)))
1149 (defun nntp-close-server-internal (&optional server)
1150 "Close connection to news server."
1151 (nntp-possibly-change-server nil server)
1152 (if nntp-server-process
1153 (delete-process nntp-server-process))
1154 (setq nntp-server-process nil)
1155 (setq nntp-address ""))
1157 (defun nntp-accept-response ()
1158 "Read response of server.
1159 It is well-known that the communication speed will be much improved by
1160 defining this function as macro."
1161 ;; To deal with server process exiting before
1162 ;; accept-process-output is called.
1163 ;; Suggested by Jason Venner <jason@violet.berkeley.edu>.
1164 ;; This is a copy of `nntp-default-sentinel'.
1165 (let ((buf (current-buffer)))
1167 (if (or (not nntp-server-process)
1168 (not (memq (process-status nntp-server-process) '(open run))))
1169 (error "nntp: Process connection closed; %s" (nntp-status-message))
1170 (if nntp-buggy-select
1172 ;; We cannot use `accept-process-output'.
1173 ;; Fujitsu UTS requires messages during sleep-for.
1174 ;; I don't know why.
1175 (message "NNTP: Reading...")
1178 (condition-case errorcode
1179 (accept-process-output nntp-server-process 1)
1181 (cond ((string-equal "select error: Invalid argument"
1183 ;; Ignore select error.
1186 (signal (car errorcode) (cdr errorcode))))))))
1189 (defun nntp-last-element (list)
1190 "Return last element of LIST."
1192 (setq list (cdr list)))
1195 (defun nntp-possibly-change-server (newsgroup server &optional connectionless)
1196 "Check whether the virtual server needs changing."
1198 (not (nntp-server-opened server)))
1199 ;; This virtual server isn't open, so we (re)open it here.
1200 (nntp-open-server server nil t))
1202 (not (equal newsgroup nntp-current-group)))
1203 ;; Set the proper current group.
1204 (nntp-request-group newsgroup server)))
1206 (defun nntp-try-list-active (group)
1207 (nntp-list-active-group group)
1209 (set-buffer nntp-server-buffer)
1210 (goto-char (point-min))
1211 (cond ((looking-at "5[0-9]+")
1212 (setq nntp-server-list-active-group nil))
1214 (setq nntp-server-list-active-group t)))))
1216 (defun nntp-async-server-opened ()
1217 (and nntp-async-process
1218 (memq (process-status nntp-async-process) '(open run))))
1220 (defun nntp-async-open-server ()
1222 (set-buffer (generate-new-buffer " *async-nntp*"))
1223 (setq nntp-async-buffer (current-buffer))
1224 (buffer-disable-undo (current-buffer)))
1225 (let ((nntp-server-process nil)
1226 (nntp-server-buffer nntp-async-buffer))
1227 (nntp-open-server-semi-internal nntp-address nntp-port-number)
1228 (if (not (setq nntp-async-process nntp-server-process))
1230 (setq nntp-async-number nil))
1231 (set-process-buffer nntp-async-process nntp-async-buffer))))
1233 (defun nntp-async-fetch-articles (article)
1234 (if (stringp article)
1236 (let ((articles (cdr (memq (assq article nntp-async-articles)
1237 nntp-async-articles)))
1238 (max (cond ((numberp nntp-async-number)
1240 ((eq nntp-async-number t)
1241 (length nntp-async-articles))
1244 (while (and (>= (setq max (1- max)) 0)
1246 (or (memq (setq nart (car (car articles))) nntp-async-fetched)
1248 (nntp-async-send-strings "ARTICLE " (int-to-string nart))
1249 (setq nntp-async-fetched (cons nart nntp-async-fetched))))
1250 (setq articles (cdr articles))))))
1252 (defun nntp-async-send-strings (&rest strings)
1253 (let ((cmd (concat (mapconcat 'identity strings " ") "\r\n")))
1254 (or (nntp-async-server-opened)
1255 (nntp-async-open-server)
1256 (error (nntp-status-message)))
1257 (process-send-string nntp-async-process cmd)))
1259 (defun nntp-async-request-group (group)
1260 (if (equal group nntp-current-group)
1262 (let ((asyncs (assoc group nntp-async-group-alist)))
1263 ;; A new group has been selected, so we push the current state
1264 ;; of async articles on an alist, and pull the old state off.
1265 (setq nntp-async-group-alist
1266 (cons (list nntp-current-group
1267 nntp-async-articles nntp-async-fetched
1269 (delq asyncs nntp-async-group-alist)))
1272 (setq nntp-async-articles (nth 1 asyncs))
1273 (setq nntp-async-fetched (nth 2 asyncs))
1274 (setq nntp-async-process (nth 3 asyncs)))))))
1278 ;;; nntp.el ends here