1 ;;; nntp.el --- nntp access for Gnus
2 ;;; Copyright (C) 1987-90,92-98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Katsumi Yamaoka <yamaoka@jpl.org>
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.
34 (eval-when-compile (require 'cl))
36 (defvoo nntp-address nil
37 "Address of the physical nntp server.")
39 (defvoo nntp-port-number "nntp"
40 "Port number on the physical nntp server.")
42 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
43 "*Hook used for sending commands to the server at startup.
44 The default value is `nntp-send-mode-reader', which makes an innd
45 server spawn an nnrpd server.")
47 (defvoo nntp-authinfo-function 'nntp-send-authinfo
48 "Function used to send AUTHINFO to the server.
49 It is called with no parameters.")
51 (defvoo nntp-server-action-alist
52 '(("nntpd 1\\.5\\.11t"
53 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
54 ("NNRP server Netscape"
55 (setq nntp-server-list-active-group nil)))
56 "Alist of regexps to match on server types and actions to be taken.
57 For instance, if you want Gnus to beep every time you connect
58 to innd, you could say something like:
60 \(setq nntp-server-action-alist
63 You probably don't want to do that, though.")
65 (defvoo nntp-open-connection-function 'nntp-open-network-stream
66 "*Function used for connecting to a remote system.
67 It will be called with the buffer to output in.
69 Two pre-made functions are `nntp-open-network-stream', which is the
70 default, and simply connects to some port or other on the remote
71 system (see nntp-port-number). The other are `nntp-open-rlogin',
72 which does an rlogin on the remote system, and then does a telnet to
73 the NNTP server available there (see nntp-rlogin-parameters) and
74 `nntp-open-telnet' which telnets to a remote system, logs in and does
77 (defvoo nntp-rlogin-program "rsh"
78 "*Program used to log in on remote machines.
79 The default is \"rsh\", but \"ssh\" is a popular alternative.")
81 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
82 "*Parameters to `nntp-open-rlogin'.
83 That function may be used as `nntp-open-connection-function'. In that
84 case, this list will be used as the parameter list given to rsh.")
86 (defvoo nntp-rlogin-user-name nil
87 "*User name on remote system when using the rlogin connect method.")
89 (defvoo nntp-telnet-parameters '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
90 "*Parameters to `nntp-open-telnet'.
91 That function may be used as `nntp-open-connection-function'. In that
92 case, this list will be executed as a command after logging in
95 (defvoo nntp-telnet-user-name nil
96 "User name to log in via telnet with.")
98 (defvoo nntp-telnet-passwd nil
99 "Password to use to log in via telnet with.")
101 (defvoo nntp-open-telnet-envuser nil
102 "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
104 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
105 "*Regular expression to match the shell prompt on the remote machine.")
107 (defvoo nntp-telnet-command "telnet"
108 "Command used to start telnet.")
110 (defvoo nntp-telnet-switches '("-8")
111 "Switches given to the telnet command.")
113 (defvoo nntp-end-of-line "\r\n"
114 "String to use on the end of lines when talking to the NNTP server.
115 This is \"\\r\\n\" by default, but should be \"\\n\" when
116 using rlogin or telnet to communicate with the server.")
118 (defvoo nntp-large-newsgroup 50
119 "*The number of the articles which indicates a large newsgroup.
120 If the number of the articles is greater than the value, verbose
121 messages will be shown to indicate the current status.")
123 (defvoo nntp-maximum-request 400
124 "*The maximum number of the requests sent to the NNTP server at one time.
125 If Emacs hangs up while retrieving headers, set the variable to a
128 (defvoo nntp-nov-is-evil nil
129 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
131 (defvoo 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 (defvoo nntp-nov-gap 5
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 (defvoo nntp-prepare-server-hook nil
143 "*Hook run before a server is opened.
144 If can be used to set up a server remotely, for instance. Say you
145 have an account at the machine \"other.machine\". This machine has
146 access to an NNTP server that you can't access locally. You could
147 then use this hook to rsh to the remote machine and start a proxy NNTP
148 server there that you can connect to. See also
149 `nntp-open-connection-function'")
151 (defvoo nntp-warn-about-losing-connection t
152 "*If non-nil, beep when a server closes connection.")
154 (defcustom nntp-authinfo-file "~/.authinfo"
155 ".netrc-like file that holds nntp authinfo passwords."
158 (repeat :tag "Entries"
161 :value ("" ("login" . "") ("password" . ""))
165 (const :format "" "login")
166 (string :format "Login: %v"))
168 (const :format "" "password")
169 (string :format "Password: %v")))))))
173 (defvoo nntp-connection-timeout nil
174 "*Number of seconds to wait before an nntp connection times out.
175 If this variable is nil, which is the default, no timers are set.")
177 (defvoo nntp-prepare-post-hook nil
178 "*Hook run just before posting an article. It is supposed to be used for
179 inserting Cancel-Lock headers, signing with Gpg, etc.")
181 ;;; Internal variables.
183 (defvar nntp-record-commands nil
184 "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
186 (defvar nntp-have-messaged nil)
188 (defvar nntp-process-wait-for nil)
189 (defvar nntp-process-to-buffer nil)
190 (defvar nntp-process-callback nil)
191 (defvar nntp-process-decode nil)
192 (defvar nntp-process-start-point nil)
193 (defvar nntp-inside-change-function nil)
194 (defvoo nntp-last-command-time nil)
195 (defvoo nntp-last-command nil)
196 (defvoo nntp-authinfo-password nil)
197 (defvoo nntp-authinfo-user nil)
199 (defvar nntp-connection-list nil)
201 (defvoo nntp-server-type nil)
202 (defvoo nntp-connection-alist nil)
203 (defvoo nntp-status-string "")
204 (defconst nntp-version "nntp 5.0")
205 (defvoo nntp-inhibit-erase nil)
206 (defvoo nntp-inhibit-output nil)
208 (defvoo nntp-server-xover 'try)
209 (defvoo nntp-server-list-active-group 'try)
211 (defvar nntp-async-needs-kluge
212 (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
213 "*When non-nil, nntp will poll asynchronous connections
214 once a second. By default, this is turned on only for Emacs
215 20.3, which has a bug that breaks nntp's normal method of
216 noticing asynchronous data.")
218 (defvar nntp-async-timer nil)
219 (defvar nntp-async-process-list nil)
222 (autoload 'nnmail-read-passwd "nnmail")
223 (autoload 'open-ssl-stream "ssl"))
227 ;;; Internal functions.
229 (defsubst nntp-send-string (process string)
230 "Send STRING to PROCESS."
231 ;; We need to store the time to provide timeouts, and
232 ;; to store the command so the we can replay the command
233 ;; if the server gives us an AUTHINFO challenge.
234 (setq nntp-last-command-time (current-time)
235 nntp-last-command string)
236 (when nntp-record-commands
237 (nntp-record-command string))
238 (process-send-string process (concat string nntp-end-of-line)))
240 (defun nntp-record-command (string)
241 "Record the command STRING."
243 (set-buffer (get-buffer-create "*nntp-log*"))
244 (goto-char (point-max))
245 (let ((time (current-time)))
246 (insert (format-time-string "%Y%m%dT%H%M%S" time)
247 "." (format "%03d" (/ (nth 2 time) 1000))
248 " " nntp-address " " string "\n"))))
250 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
251 "Wait for WAIT-FOR to arrive from PROCESS."
253 (set-buffer (process-buffer process))
254 (goto-char (point-min))
255 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
257 (memq (process-status process) '(open run)))
258 (when (looking-at "480")
259 (nntp-handle-authinfo process))
260 (nntp-accept-process-output process)
261 (goto-char (point-min)))
266 (nntp-snarf-error-message)
268 ((not (memq (process-status process) '(open run)))
269 (nnheader-report 'nntp "Server closed connection"))
271 (goto-char (point-max))
272 (let ((limit (point-min))
274 (while (not (re-search-backward wait-for limit t))
275 (nntp-accept-process-output process)
276 ;; We assume that whatever we wait for is less than 1000
278 (setq limit (max (- (point-max) 1000) (point-min)))
279 (goto-char (point-max)))
280 (setq response (match-string 0))
282 (set-buffer nntp-server-buffer)
283 (setq nntp-process-response response)))
284 (nntp-decode-text (not decode))
288 (goto-char (point-max))
289 (insert-buffer-substring (process-buffer process))
290 ;; Nix out "nntp reading...." message.
291 (when nntp-have-messaged
292 (setq nntp-have-messaged nil)
293 (nnheader-message 5 ""))
298 (defsubst nntp-find-connection (buffer)
299 "Find the connection delivering to BUFFER."
300 (let ((alist nntp-connection-alist)
301 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
303 (while (setq entry (pop alist))
304 (when (eq buffer (cadr entry))
305 (setq process (car entry)
308 (if (memq (process-status process) '(open run))
310 (when (buffer-name (process-buffer process))
311 (kill-buffer (process-buffer process)))
312 (setq nntp-connection-alist (delq entry nntp-connection-alist))
315 (defsubst nntp-find-connection-entry (buffer)
316 "Return the entry for the connection to BUFFER."
317 (assq (nntp-find-connection buffer) nntp-connection-alist))
319 (defun nntp-find-connection-buffer (buffer)
320 "Return the process connection buffer tied to BUFFER."
321 (let ((process (nntp-find-connection buffer)))
323 (process-buffer process))))
325 (defsubst nntp-retrieve-data (command address port buffer
326 &optional wait-for callback decode)
327 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
328 (let ((process (or (nntp-find-connection buffer)
329 (nntp-open-connection buffer))))
331 (nnheader-report 'nntp "Couldn't open connection to %s" address)
332 (unless (or nntp-inhibit-erase nnheader-callback-function)
334 (set-buffer (process-buffer process))
337 (nntp-send-string process command))
339 ((eq callback 'ignore)
341 ((and callback wait-for)
342 (nntp-async-wait process wait-for buffer decode callback)
345 (nntp-wait-for process wait-for buffer decode))
348 (defsubst nntp-send-command (wait-for &rest strings)
349 "Send STRINGS to server and wait until WAIT-FOR returns."
350 (when (and (not nnheader-callback-function)
351 (not nntp-inhibit-output))
353 (set-buffer nntp-server-buffer)
356 (mapconcat 'identity strings " ")
357 nntp-address nntp-port-number nntp-server-buffer
358 wait-for nnheader-callback-function))
360 (defun nntp-send-command-nodelete (wait-for &rest strings)
361 "Send STRINGS to server and wait until WAIT-FOR returns."
363 (mapconcat 'identity strings " ")
364 nntp-address nntp-port-number nntp-server-buffer
365 wait-for nnheader-callback-function))
367 (defun nntp-send-command-and-decode (wait-for &rest strings)
368 "Send STRINGS to server and wait until WAIT-FOR returns."
369 (when (and (not nnheader-callback-function)
370 (not nntp-inhibit-output))
372 (set-buffer nntp-server-buffer)
375 (mapconcat 'identity strings " ")
376 nntp-address nntp-port-number nntp-server-buffer
377 wait-for nnheader-callback-function t))
379 (defun nntp-send-buffer (wait-for)
380 "Send the current buffer to server and wait until WAIT-FOR returns."
381 (when (and (not nnheader-callback-function)
382 (not nntp-inhibit-output))
384 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
387 (process-send-region (nntp-find-connection nntp-server-buffer)
388 (point-min) (point-max))
390 nil nntp-address nntp-port-number nntp-server-buffer
391 wait-for nnheader-callback-function))
395 ;;; Interface functions.
397 (nnoo-define-basics nntp)
399 (defsubst nntp-next-result-arrived-p ()
401 ;; A result that starts with a 2xx code is terminated by
402 ;; a line with only a "." on it.
403 ((eq (char-after) ?2)
404 (if (re-search-forward "\n\\.\r?\n" nil t)
407 ;; A result that starts with a 3xx or 4xx code is terminated
410 (if (search-forward "\n" nil t)
417 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
418 "Retrieve the headers of ARTICLES."
419 (nntp-possibly-change-group group server)
421 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
423 (if (and (not gnus-nov-is-evil)
424 (not nntp-nov-is-evil)
425 (nntp-retrieve-headers-with-xover articles fetch-old))
426 ;; We successfully retrieved the headers via XOVER.
428 ;; XOVER didn't work, so we do it the hard, slow and inefficient
430 (let ((number (length articles))
433 (last-point (point-min))
434 (buf (nntp-find-connection-buffer nntp-server-buffer))
435 (nntp-inhibit-erase t)
437 ;; Send HEAD commands.
438 (while (setq article (pop articles))
441 "HEAD" (if (numberp article)
442 (int-to-string article)
443 ;; `articles' is either a list of article numbers
444 ;; or a list of article IDs.
447 ;; Every 400 requests we have to read the stream in
448 ;; order to avoid deadlocks.
449 (when (or (null articles) ;All requests have been sent.
450 (zerop (% count nntp-maximum-request)))
451 (nntp-accept-response)
454 (goto-char last-point)
456 (while (nntp-next-result-arrived-p)
457 (setq last-point (point))
460 ;; If number of headers is greater than 100, give
461 ;; informative messages.
462 (and (numberp nntp-large-newsgroup)
463 (> number nntp-large-newsgroup)
464 (zerop (% received 20))
465 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
466 (/ (* received 100) number)))
467 (nntp-accept-response))))
468 (and (numberp nntp-large-newsgroup)
469 (> number nntp-large-newsgroup)
470 (nnheader-message 6 "NNTP: Receiving headers...done"))
472 ;; Now all of replies are received. Fold continuation lines.
473 (nnheader-fold-continuation-lines)
474 ;; Remove all "\r"'s.
476 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
479 (deffoo nntp-retrieve-groups (groups &optional server)
480 "Retrieve group info on GROUPS."
481 (nntp-possibly-change-group nil server)
483 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
484 ;; The first time this is run, this variable is `try'. So we
486 (when (eq nntp-server-list-active-group 'try)
487 (nntp-try-list-active (car groups)))
491 (last-point (point-min))
492 (nntp-inhibit-erase t)
493 (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
495 ;; Send the command to the server.
496 (nntp-send-command nil command (pop groups))
498 ;; Every 400 requests we have to read the stream in
499 ;; order to avoid deadlocks.
500 (when (or (null groups) ;All requests have been sent.
501 (zerop (% count nntp-maximum-request)))
502 (nntp-accept-response)
504 (goto-char last-point)
506 (while (re-search-forward "^[0-9]" nil t)
508 (setq last-point (point))
510 (nntp-accept-response))))
512 ;; Wait for the reply from the final command.
513 (goto-char (point-max))
514 (re-search-backward "^[0-9]" nil t)
515 (when (looking-at "^[23]")
517 (goto-char (point-max))
518 (if (not nntp-server-list-active-group)
519 (not (re-search-backward "\r?\n" (- (point) 3) t))
520 (not (re-search-backward "^\\.\r?\n" (- (point) 4) t))))
521 (nntp-accept-response)))
523 ;; Now all replies are received. We remove CRs.
524 (goto-char (point-min))
525 (while (search-forward "\r" nil t)
526 (replace-match "" t t))
528 (if (not nntp-server-list-active-group)
530 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
532 ;; We have read active entries, so we just delete the
534 (goto-char (point-min))
535 (while (re-search-forward "^[.2-5]" nil t)
536 (delete-region (match-beginning 0)
537 (progn (forward-line 1) (point))))
538 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
541 (deffoo nntp-retrieve-articles (articles &optional group server)
542 (nntp-possibly-change-group group server)
544 (let ((number (length articles))
547 (last-point (point-min))
548 (buf (nntp-find-connection-buffer nntp-server-buffer))
549 (nntp-inhibit-erase t)
550 (map (apply 'vector articles))
555 ;; Send ARTICLE command.
556 (while (setq article (pop articles))
559 "ARTICLE" (if (numberp article)
560 (int-to-string article)
561 ;; `articles' is either a list of article numbers
562 ;; or a list of article IDs.
565 ;; Every 400 requests we have to read the stream in
566 ;; order to avoid deadlocks.
567 (when (or (null articles) ;All requests have been sent.
568 (zerop (% count nntp-maximum-request)))
569 (nntp-accept-response)
572 (goto-char last-point)
574 (while (nntp-next-result-arrived-p)
575 (aset map received (cons (aref map received) (point)))
576 (setq last-point (point))
579 ;; If number of headers is greater than 100, give
580 ;; informative messages.
581 (and (numberp nntp-large-newsgroup)
582 (> number nntp-large-newsgroup)
583 (zerop (% received 20))
584 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
585 (/ (* received 100) number)))
586 (nntp-accept-response))))
587 (and (numberp nntp-large-newsgroup)
588 (> number nntp-large-newsgroup)
589 (nnheader-message 6 "NNTP: Receiving articles...done"))
591 ;; Now we have all the responses. We go through the results,
592 ;; wash it and copy it over to the server buffer.
593 (set-buffer nntp-server-buffer)
595 (setq last-point (point-min))
599 (setq point (goto-char (point-max)))
601 (insert-buffer-substring buf last-point (cdr entry))
603 (setq last-point (cdr entry))
606 (cons (car entry) point))
609 (defun nntp-try-list-active (group)
610 (nntp-list-active-group group)
612 (set-buffer nntp-server-buffer)
613 (goto-char (point-min))
615 (looking-at "5[0-9]+"))
616 (setq nntp-server-list-active-group nil))
618 (setq nntp-server-list-active-group t)))))
620 (deffoo nntp-list-active-group (group &optional server)
621 "Return the active info on GROUP (which can be a regexp)."
622 (nntp-possibly-change-group nil server)
623 (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group))
625 (deffoo nntp-request-group-articles (group &optional server)
626 "Return the list of existing articles in GROUP."
627 (nntp-possibly-change-group nil server)
628 (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group))
630 (deffoo nntp-request-article (article &optional group server buffer command)
631 (nntp-possibly-change-group group server)
632 (when (nntp-send-command-and-decode
633 "\r?\n\\.\r?\n" "ARTICLE"
634 (if (numberp article) (int-to-string article) article))
636 (not (equal buffer nntp-server-buffer)))
638 (set-buffer nntp-server-buffer)
639 (copy-to-buffer buffer (point-min) (point-max))
640 (nntp-find-group-and-number))
641 (nntp-find-group-and-number))))
643 (deffoo nntp-request-head (article &optional group server)
644 (nntp-possibly-change-group group server)
645 (when (nntp-send-command
646 "\r?\n\\.\r?\n" "HEAD"
647 (if (numberp article) (int-to-string article) article))
649 (nntp-find-group-and-number)
650 (nntp-decode-text))))
652 (deffoo nntp-request-body (article &optional group server)
653 (nntp-possibly-change-group group server)
654 (nntp-send-command-and-decode
655 "\r?\n\\.\r?\n" "BODY"
656 (if (numberp article) (int-to-string article) article)))
658 (deffoo nntp-request-group (group &optional server dont-check)
659 (nntp-possibly-change-group nil server)
660 (when (nntp-send-command "^[245].*\n" "GROUP" group)
661 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
662 (setcar (cddr entry) group))))
664 (deffoo nntp-close-group (group &optional server)
667 (deffoo nntp-server-opened (&optional server)
668 "Say whether a connection to SERVER has been opened."
669 (and (nnoo-current-server-p 'nntp server)
671 (gnus-buffer-live-p nntp-server-buffer)
672 (nntp-find-connection nntp-server-buffer)))
674 (deffoo nntp-open-server (server &optional defs connectionless)
675 (nnheader-init-server-buffer)
676 (if (nntp-server-opened server)
678 (when (or (stringp (car defs))
679 (numberp (car defs)))
680 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
681 (unless (assq 'nntp-address defs)
682 (setq defs (append defs (list (list 'nntp-address server)))))
683 (nnoo-change-server 'nntp server defs)
684 (unless connectionless
685 (or (nntp-find-connection nntp-server-buffer)
686 (nntp-open-connection nntp-server-buffer)))))
688 (deffoo nntp-close-server (&optional server)
689 (nntp-possibly-change-group nil server t)
690 (let ((process (nntp-find-connection nntp-server-buffer)))
692 (when (memq (process-status process) '(open run))
694 (nntp-send-string process "QUIT")
695 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
696 ;; Ok, this is evil, but when using telnet and stuff
697 ;; as the connection method, it's important that the
698 ;; QUIT command actually is sent out before we kill
701 (when (buffer-name (process-buffer process))
702 (kill-buffer (process-buffer process)))
703 (setq process (car (pop nntp-connection-alist))))
704 (nnoo-close-server 'nntp)))
706 (deffoo nntp-request-close ()
708 (while (setq process (pop nntp-connection-list))
709 (when (memq (process-status process) '(open run))
711 (nntp-send-string process "QUIT")
712 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
713 ;; Ok, this is evil, but when using telnet and stuff
714 ;; as the connection method, it's important that the
715 ;; QUIT command actually is sent out before we kill
718 (when (buffer-name (process-buffer process))
719 (kill-buffer (process-buffer process))))))
721 (deffoo nntp-request-list (&optional server)
722 (nntp-possibly-change-group nil server)
723 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST"))
725 (deffoo nntp-request-list-newsgroups (&optional server)
726 (nntp-possibly-change-group nil server)
727 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS"))
729 (deffoo nntp-request-newgroups (date &optional server)
730 (nntp-possibly-change-group nil server)
732 (set-buffer nntp-server-buffer)
735 "^\\.\r?\n" "NEWGROUPS"
736 (format-time-string "%y%m%d %H%M%S" (date-to-time date)))
737 (nntp-decode-text))))
739 (deffoo nntp-request-post (&optional server)
740 (nntp-possibly-change-group nil server)
741 (when (nntp-send-command "^[23].*\r?\n" "POST")
742 (let ((response (save-current-buffer
743 (set-buffer nntp-server-buffer)
744 nntp-process-response))
747 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
749 (setq server-id (match-string 1 response))
750 (narrow-to-region (goto-char (point-min))
751 (if (search-forward "\n\n" nil t)
754 (unless (mail-fetch-field "Message-ID")
755 (goto-char (point-min))
756 (insert "Message-ID: " server-id "\n"))
758 (run-hooks 'nntp-prepare-post-hook)
759 (nntp-send-buffer "^[23].*\n"))))
761 (deffoo nntp-request-type (group article)
764 (deffoo nntp-asynchronous-p ()
769 (defun nntp-send-mode-reader ()
770 "Send the MODE READER command to the nntp server.
771 This function is supposed to be called from `nntp-server-opened-hook'.
772 It will make innd servers spawn an nnrpd process to allow actual article
774 (nntp-send-command "^.*\n" "MODE READER"))
776 (defun nntp-send-authinfo (&optional send-if-force)
777 "Send the AUTHINFO to the nntp server.
778 It will look in the \"~/.authinfo\" file for matching entries. If
779 nothing suitable is found there, it will prompt for a user name
782 If SEND-IF-FORCE, only send authinfo to the server if the
783 .authinfo file has the FORCE token."
784 (let* ((list (gnus-parse-netrc nntp-authinfo-file))
785 (alist (gnus-netrc-machine list nntp-address))
786 (force (gnus-netrc-get alist "force"))
787 (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
788 (passwd (gnus-netrc-get alist "password")))
789 (when (or (not send-if-force)
792 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
793 nntp-authinfo-user user))
794 (unless (member user '(nil ""))
795 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
796 (when t ;???Should check if AUTHINFO succeeded
798 "^2.*\r?\n" "AUTHINFO PASS"
800 nntp-authinfo-password
801 (setq nntp-authinfo-password
802 (nnmail-read-passwd (format "NNTP (%s@%s) password: "
803 user nntp-address))))))))))
805 (defun nntp-send-nosy-authinfo ()
806 "Send the AUTHINFO to the nntp server."
807 (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
808 (unless (member user '(nil ""))
809 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
810 (when t ;???Should check if AUTHINFO succeeded
811 (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
812 (nnmail-read-passwd "NNTP (%s@%s) password: "
813 user nntp-address))))))
815 (defun nntp-send-authinfo-from-file ()
816 "Send the AUTHINFO to the nntp server.
818 The authinfo login name is taken from the user's login name and the
819 password contained in '~/.nntp-authinfo'."
820 (when (file-exists-p "~/.nntp-authinfo")
822 (insert-file-contents "~/.nntp-authinfo")
823 (goto-char (point-min))
824 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
826 "^2.*\r?\n" "AUTHINFO PASS"
827 (buffer-substring (point) (progn (end-of-line) (point)))))))
829 ;;; Internal functions.
831 (defun nntp-handle-authinfo (process)
832 "Take care of an authinfo response from the server."
833 (let ((last nntp-last-command))
834 (funcall nntp-authinfo-function)
835 ;; We have to re-send the function that was interrupted by
836 ;; the authinfo request.
838 (set-buffer nntp-server-buffer)
840 (nntp-send-string process last)))
842 (defun nntp-make-process-buffer (buffer)
843 "Create a new, fresh buffer usable for nntp process connections."
847 (format " *server %s %s %s*"
848 nntp-address nntp-port-number
849 (gnus-buffer-exists-p buffer))))
850 (set (make-local-variable 'after-change-functions) nil)
851 (set (make-local-variable 'nntp-process-wait-for) nil)
852 (set (make-local-variable 'nntp-process-callback) nil)
853 (set (make-local-variable 'nntp-process-to-buffer) nil)
854 (set (make-local-variable 'nntp-process-start-point) nil)
855 (set (make-local-variable 'nntp-process-decode) nil)
858 (defun nntp-open-connection (buffer)
859 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
860 (run-hooks 'nntp-prepare-server-hook)
861 (let* ((pbuffer (nntp-make-process-buffer buffer))
863 (and nntp-connection-timeout
864 (nnheader-run-at-time
865 nntp-connection-timeout nil
867 (when (buffer-name ,pbuffer)
868 (kill-buffer ,pbuffer))))))
871 (funcall nntp-open-connection-function pbuffer)
875 (nnheader-cancel-timer timer))
876 (when (and (buffer-name pbuffer)
878 (process-kill-without-query process)
879 (nntp-wait-for process "^.*\n" buffer nil t)
880 (if (memq (process-status process) '(open run))
882 (caar (push (list process buffer nil) nntp-connection-alist))
883 (push process nntp-connection-list)
886 (nntp-read-server-type)
888 (set-buffer nntp-server-buffer)
889 (let ((nnheader-callback-function nil))
890 (run-hooks 'nntp-server-opened-hook)
891 (nntp-send-authinfo t))))
892 (when (buffer-name (process-buffer process))
893 (kill-buffer (process-buffer process)))
896 (defun nntp-open-network-stream (buffer)
897 (open-network-stream-as-binary
898 "nntpd" buffer nntp-address nntp-port-number))
900 (defun nntp-open-ssl-stream (buffer)
901 (let* ((ssl-program-arguments '("-connect" (concat host ":" service)))
902 (proc (open-ssl-stream "nntpd" buffer nntp-address nntp-port-number)))
905 (nntp-wait-for-string "^\r*20[01]")
907 (delete-region (point-min) (point))
910 (defun nntp-read-server-type ()
911 "Find out what the name of the server we have connected to is."
912 ;; Wait for the status string to arrive.
913 (setq nntp-server-type (buffer-string))
914 (let ((alist nntp-server-action-alist)
917 ;; Run server-specific commands.
919 (setq entry (pop alist))
920 (when (string-match (car entry) nntp-server-type)
921 (if (and (listp (cadr entry))
922 (not (eq 'lambda (caadr entry))))
924 (funcall (cadr entry)))))))
926 (defun nntp-async-wait (process wait-for buffer decode callback)
928 (set-buffer (process-buffer process))
929 (unless nntp-inside-change-function
931 (setq nntp-process-wait-for wait-for
932 nntp-process-to-buffer buffer
933 nntp-process-decode decode
934 nntp-process-callback callback
935 nntp-process-start-point (point-max))
936 (setq after-change-functions '(nntp-after-change-function))
937 (if nntp-async-needs-kluge
938 (nntp-async-kluge process))))
940 (defun nntp-async-kluge (process)
941 ;; emacs 20.3 bug: process output with encoding 'binary
942 ;; doesn't trigger after-change-functions.
943 (unless nntp-async-timer
944 (setq nntp-async-timer
945 (nnheader-run-at-time 1 1 'nntp-async-timer-handler)))
946 (add-to-list 'nntp-async-process-list process))
948 (defun nntp-async-timer-handler ()
951 (if (memq (process-status proc) '(open run))
952 (nntp-async-trigger proc)
953 (nntp-async-stop proc)))
954 nntp-async-process-list))
956 (defun nntp-async-stop (proc)
957 (setq nntp-async-process-list (delq proc nntp-async-process-list))
958 (when (and nntp-async-timer (not nntp-async-process-list))
959 (nnheader-cancel-timer nntp-async-timer)
960 (setq nntp-async-timer nil)))
962 (defun nntp-after-change-function (beg end len)
964 ;; we only care about insertions at eob
965 (when (and (eq 0 len) (eq (point-max) end))
967 (let ((proc (get-buffer-process (current-buffer))))
969 (nntp-async-trigger proc)))))
970 ;; any throw from after-change-functions will leave it
971 ;; set to nil. so we reset it here, if necessary.
973 (setq after-change-functions '(nntp-after-change-function)))))
975 (defun nntp-async-trigger (process)
977 (set-buffer (process-buffer process))
978 (when nntp-process-callback
979 ;; do we have an error message?
980 (goto-char nntp-process-start-point)
981 (if (memq (following-char) '(?4 ?5))
982 ;; wants credentials?
983 (if (looking-at "480")
984 (nntp-handle-authinfo nntp-process-to-buffer)
985 ;; report error message.
986 (nntp-snarf-error-message)
987 (nntp-do-callback nil))
989 ;; got what we expect?
990 (goto-char (point-max))
991 (when (re-search-backward
992 nntp-process-wait-for nntp-process-start-point t)
993 (let ((response (match-string 0)))
995 (set-buffer nntp-server-buffer)
996 (setq nntp-process-response response)))
997 (nntp-async-stop process)
999 (when (gnus-buffer-exists-p nntp-process-to-buffer)
1000 (let ((buf (current-buffer))
1001 (start nntp-process-start-point)
1002 (decode nntp-process-decode))
1004 (set-buffer nntp-process-to-buffer)
1005 (goto-char (point-max))
1007 (narrow-to-region (point) (point))
1008 (insert-buffer-substring buf start)
1010 (nntp-decode-text))))))
1012 (goto-char (point-max))
1014 (buffer-name (get-buffer nntp-process-to-buffer))))))))
1016 (defun nntp-do-callback (arg)
1017 (let ((callback nntp-process-callback)
1018 (nntp-inside-change-function t))
1019 (setq nntp-process-callback nil)
1020 (funcall callback arg)))
1022 (defun nntp-snarf-error-message ()
1023 "Save the error message in the current buffer."
1024 (let ((message (buffer-string)))
1025 (while (string-match "[\r\n]+" message)
1026 (setq message (replace-match " " t t message)))
1027 (nnheader-report 'nntp message)
1030 (defun nntp-accept-process-output (process &optional timeout)
1031 "Wait for output from PROCESS and message some dots."
1033 (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1034 nntp-server-buffer))
1035 (let ((len (/ (point-max) 1024))
1038 (setq nntp-have-messaged t)
1039 (nnheader-message 7 "nntp read: %dk" len)))
1040 (accept-process-output process (or timeout 1))))
1042 (defun nntp-accept-response ()
1043 "Wait for output from the process that outputs to BUFFER."
1044 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1046 (defun nntp-possibly-change-group (group server &optional connectionless)
1047 (let ((nnheader-callback-function nil))
1049 (or (nntp-server-opened server)
1050 (nntp-open-server server nil connectionless)))
1052 (unless connectionless
1053 (or (nntp-find-connection nntp-server-buffer)
1054 (nntp-open-connection nntp-server-buffer))))
1057 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1058 (when (not (equal group (caddr entry)))
1060 (set-buffer (process-buffer (car entry)))
1062 (nntp-send-command "^[245].*\n" "GROUP" group)
1063 (setcar (cddr entry) group)
1066 (defun nntp-decode-text (&optional cr-only)
1067 "Decode the text in the current buffer."
1068 (goto-char (point-min))
1069 (while (search-forward "\r" nil t)
1072 ;; Remove trailing ".\n" end-of-transfer marker.
1073 (goto-char (point-max))
1075 (when (looking-at ".\n")
1077 ;; Delete status line.
1078 (goto-char (point-min))
1079 (delete-region (point) (progn (forward-line 1) (point)))
1080 ;; Remove "." -> ".." encoding.
1081 (while (search-forward "\n.." nil t)
1084 (defun nntp-encode-text ()
1085 "Encode the text in the current buffer."
1087 ;; Replace "." at beginning of line with "..".
1088 (goto-char (point-min))
1089 (while (re-search-forward "^\\." nil t)
1091 (goto-char (point-max))
1092 ;; Insert newline at the end of the buffer.
1095 ;; Insert `.' at end of buffer (end of text mark).
1096 (goto-char (point-max))
1098 (goto-char (point-min))
1102 (insert nntp-end-of-line))))
1104 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1105 (set-buffer nntp-server-buffer)
1109 ;; This server does not talk NOV.
1110 ((not nntp-server-xover)
1113 ;; We don't care about gaps.
1114 ((or (not nntp-nov-gap)
1116 (nntp-send-xover-command
1118 (if (numberp fetch-old)
1119 (max 1 (- (car articles) fetch-old))
1122 (car (last articles)) 'wait)
1124 (goto-char (point-min))
1125 (when (looking-at "[1-5][0-9][0-9] ")
1126 (delete-region (point) (progn (forward-line 1) (point))))
1127 (while (search-forward "\r" nil t)
1128 (replace-match "" t t))
1129 (goto-char (point-max))
1131 (when (looking-at "\\.")
1132 (delete-region (point) (progn (forward-line 1) (point)))))
1134 ;; We do it the hard way. For each gap, an XOVER command is sent
1135 ;; to the server. We do not wait for a reply from the server, we
1136 ;; just send them off as fast as we can. That means that we have
1137 ;; to count the number of responses we get back to find out when we
1138 ;; have gotten all we asked for.
1139 ((numberp nntp-nov-gap)
1142 (last-point (point-min))
1143 (buf nntp-server-buffer)
1144 ;;(process-buffer (nntp-find-connection (current-buffer))))
1146 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1147 ;; that means that the server does not understand XOVER, but we
1148 ;; won't know that until we try.
1149 (while (and nntp-server-xover articles)
1150 (setq first (car articles))
1151 ;; Search forward until we find a gap, or until we run out of
1153 (while (and (cdr articles)
1154 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1155 (setq articles (cdr articles)))
1157 (when (nntp-send-xover-command first (car articles))
1158 (setq articles (cdr articles)
1161 ;; Every 400 requests we have to read the stream in
1162 ;; order to avoid deadlocks.
1163 (when (or (null articles) ;All requests have been sent.
1164 (zerop (% count nntp-maximum-request)))
1165 (accept-process-output)
1166 ;; On some Emacs versions the preceding function has
1167 ;; a tendency to change the buffer. Perhaps. It's
1168 ;; quite difficult to reproduce, because it only
1169 ;; seems to happen once in a blue moon.
1172 (goto-char last-point)
1174 (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
1175 (setq received (1+ received)))
1176 (setq last-point (point))
1178 (accept-process-output)
1179 (set-buffer buf)))))
1181 (when nntp-server-xover
1182 ;; Wait for the reply from the final command.
1183 (goto-char (point-max))
1184 (re-search-backward "^[0-9][0-9][0-9] " nil t)
1185 (when (looking-at "^[23]")
1187 (goto-char (point-max))
1189 (not (looking-at "^\\.\r?\n")))
1190 (nntp-accept-response)))
1192 ;; We remove any "." lines and status lines.
1193 (goto-char (point-min))
1194 (while (search-forward "\r" nil t)
1196 (goto-char (point-min))
1197 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1202 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1203 "Send the XOVER command to the server."
1204 (let ((range (format "%d-%d" beg end))
1205 (nntp-inhibit-erase t))
1206 (if (stringp nntp-server-xover)
1207 ;; If `nntp-server-xover' is a string, then we just send this
1210 (nntp-send-command-nodelete
1211 "\r?\n\\.\r?\n" nntp-server-xover range)
1212 ;; We do not wait for the reply.
1213 (nntp-send-command-nodelete "\r?\n\\.\r?\n" nntp-server-xover range))
1214 (let ((commands nntp-xover-commands))
1215 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1216 ;; We try them all until we get at positive response.
1217 (while (and commands (eq nntp-server-xover 'try))
1218 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1220 (set-buffer nntp-server-buffer)
1221 (goto-char (point-min))
1222 (and (looking-at "[23]") ; No error message.
1223 ;; We also have to look at the lines. Some buggy
1224 ;; servers give back simple lines with just the
1225 ;; article number. How... helpful.
1228 (looking-at "[0-9]+\t...")) ; More text after number.
1229 (setq nntp-server-xover (car commands))))
1230 (setq commands (cdr commands)))
1231 ;; If none of the commands worked, we disable XOVER.
1232 (when (eq nntp-server-xover 'try)
1234 (set-buffer nntp-server-buffer)
1236 (setq nntp-server-xover nil)))
1237 nntp-server-xover))))
1239 ;;; Alternative connection methods.
1241 (defun nntp-wait-for-string (regexp)
1242 "Wait until string arrives in the buffer."
1243 (let ((buf (current-buffer)))
1244 (goto-char (point-min))
1245 (while (not (re-search-forward regexp nil t))
1246 (accept-process-output (nntp-find-connection nntp-server-buffer))
1248 (goto-char (point-min)))))
1250 (defun nntp-open-telnet (buffer)
1254 (let ((proc (as-binary-process
1257 "nntpd" buffer nntp-telnet-command nntp-telnet-switches)))
1258 (case-fold-search t))
1259 (when (memq (process-status proc) '(open run))
1260 (process-send-string proc "set escape \^X\n")
1262 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1263 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1264 nntp-address "\n")))
1266 (process-send-string proc (concat "open " nntp-address "\n"))))
1268 ((not nntp-open-telnet-envuser)
1269 (nntp-wait-for-string "^\r*.?login:")
1270 (process-send-string
1272 (or nntp-telnet-user-name
1273 (setq nntp-telnet-user-name (read-string "login: ")))
1275 (nntp-wait-for-string "^\r*.?password:")
1276 (process-send-string
1278 (or nntp-telnet-passwd
1279 (setq nntp-telnet-passwd
1280 (nnmail-read-passwd "Password: ")))
1283 (nntp-wait-for-string nntp-telnet-shell-prompt)
1284 (process-send-string
1285 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1286 (nntp-wait-for-string "^\r*20[01]")
1288 (delete-region (point-min) (point))
1289 (process-send-string proc "\^]")
1290 (nntp-wait-for-string "^telnet")
1291 (process-send-string proc "mode character\n")
1292 (accept-process-output proc 1)
1294 (goto-char (point-min))
1296 (delete-region (point) (point-max)))
1299 (defun nntp-open-rlogin (buffer)
1300 "Open a connection to SERVER using rsh."
1301 (let ((proc (if nntp-rlogin-user-name
1303 (apply 'start-process
1304 "nntpd" buffer nntp-rlogin-program
1305 nntp-address "-l" nntp-rlogin-user-name
1306 nntp-rlogin-parameters))
1308 (apply 'start-process
1309 "nntpd" buffer nntp-rlogin-program nntp-address
1310 nntp-rlogin-parameters)))))
1313 (nntp-wait-for-string "^\r*20[01]")
1315 (delete-region (point-min) (point))
1318 (defun nntp-find-group-and-number ()
1321 (set-buffer nntp-server-buffer)
1322 (narrow-to-region (goto-char (point-min))
1323 (or (search-forward "\n\n" nil t) (point-max)))
1324 (goto-char (point-min))
1325 ;; We first find the number by looking at the status line.
1326 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1328 (buffer-substring (match-beginning 1)
1330 group newsgroups xref)
1331 (and number (zerop number) (setq number nil))
1332 ;; Then we find the group name.
1335 ;; If there is only one group in the Newsgroups header,
1336 ;; then it seems quite likely that this article comes
1337 ;; from that group, I'd say.
1338 ((and (setq newsgroups (mail-fetch-field "newsgroups"))
1339 (not (string-match "," newsgroups)))
1341 ;; If there is more than one group in the Newsgroups
1342 ;; header, then the Xref header should be filled out.
1343 ;; We hazard a guess that the group that has this
1344 ;; article number in the Xref header is the one we are
1345 ;; looking for. This might very well be wrong if this
1346 ;; article happens to have the same number in several
1347 ;; groups, but that's life.
1348 ((and (setq xref (mail-fetch-field "xref"))
1350 (string-match (format "\\([^ :]+\\):%d" number) xref))
1351 (substring xref (match-beginning 1) (match-end 1)))
1353 (when (string-match "\r" group)
1354 (setq group (substring group 0 (match-beginning 0))))
1355 (cons group number)))))
1359 ;;; nntp.el ends here