1 ;;; nntp.el --- nntp access for Gnus
2 ;;; Copyright (C) 1987-90,92-97 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)
212 (autoload 'nnmail-read-passwd "nnmail")
213 (autoload 'open-ssl-stream "ssl"))
217 ;;; Internal functions.
219 (defsubst nntp-send-string (process string)
220 "Send STRING to PROCESS."
221 ;; We need to store the time to provide timeouts, and
222 ;; to store the command so the we can replay the command
223 ;; if the server gives us an AUTHINFO challenge.
224 (setq nntp-last-command-time (current-time)
225 nntp-last-command string)
226 (when nntp-record-commands
227 (nntp-record-command string))
228 (process-send-string process (concat string nntp-end-of-line)))
230 (defun nntp-record-command (string)
231 "Record the command STRING."
233 (set-buffer (get-buffer-create "*nntp-log*"))
234 (goto-char (point-max))
235 (let ((time (current-time)))
236 (insert (format-time-string "%Y%m%dT%H%M%S" time)
237 "." (format "%03d" (/ (nth 2 time) 1000))
238 " " nntp-address " " string "\n"))))
240 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
241 "Wait for WAIT-FOR to arrive from PROCESS."
243 (set-buffer (process-buffer process))
244 (goto-char (point-min))
245 (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
247 (memq (process-status process) '(open run)))
248 (when (looking-at "480")
249 (nntp-handle-authinfo process))
250 (nntp-accept-process-output process)
251 (goto-char (point-min)))
256 (nntp-snarf-error-message)
258 ((not (memq (process-status process) '(open run)))
259 (nnheader-report 'nntp "Server closed connection"))
261 (goto-char (point-max))
262 (let ((limit (point-min))
264 (while (not (re-search-backward wait-for limit t))
265 (nntp-accept-process-output process)
266 ;; We assume that whatever we wait for is less than 1000
268 (setq limit (max (- (point-max) 1000) (point-min)))
269 (goto-char (point-max)))
270 (setq response (match-string 0))
272 (set-buffer nntp-server-buffer)
273 (setq nntp-process-response response)))
274 (nntp-decode-text (not decode))
278 (goto-char (point-max))
279 (insert-buffer-substring (process-buffer process))
280 ;; Nix out "nntp reading...." message.
281 (when nntp-have-messaged
282 (setq nntp-have-messaged nil)
283 (nnheader-message 5 ""))
288 (defsubst nntp-find-connection (buffer)
289 "Find the connection delivering to BUFFER."
290 (let ((alist nntp-connection-alist)
291 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
293 (while (setq entry (pop alist))
294 (when (eq buffer (cadr entry))
295 (setq process (car entry)
298 (if (memq (process-status process) '(open run))
300 (when (buffer-name (process-buffer process))
301 (kill-buffer (process-buffer process)))
302 (setq nntp-connection-alist (delq entry nntp-connection-alist))
305 (defsubst nntp-find-connection-entry (buffer)
306 "Return the entry for the connection to BUFFER."
307 (assq (nntp-find-connection buffer) nntp-connection-alist))
309 (defun nntp-find-connection-buffer (buffer)
310 "Return the process connection buffer tied to BUFFER."
311 (let ((process (nntp-find-connection buffer)))
313 (process-buffer process))))
315 (defsubst nntp-retrieve-data (command address port buffer
316 &optional wait-for callback decode)
317 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
318 (let ((process (or (nntp-find-connection buffer)
319 (nntp-open-connection buffer))))
321 (nnheader-report 'nntp "Couldn't open connection to %s" address)
322 (unless (or nntp-inhibit-erase nnheader-callback-function)
324 (set-buffer (process-buffer process))
327 (nntp-send-string process command))
329 ((eq callback 'ignore)
331 ((and callback wait-for)
333 (set-buffer (process-buffer process))
334 (unless nntp-inside-change-function
336 (setq nntp-process-decode decode
337 nntp-process-to-buffer buffer
338 nntp-process-wait-for wait-for
339 nntp-process-callback callback
340 nntp-process-start-point (point-max)
341 after-change-functions
342 (list 'nntp-after-change-function-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-article (article &optional group server buffer command)
626 (nntp-possibly-change-group group server)
627 (when (nntp-send-command-and-decode
628 "\r?\n\\.\r?\n" "ARTICLE"
629 (if (numberp article) (int-to-string article) article))
631 (not (equal buffer nntp-server-buffer)))
633 (set-buffer nntp-server-buffer)
634 (copy-to-buffer buffer (point-min) (point-max))
635 (nntp-find-group-and-number))
636 (nntp-find-group-and-number))))
638 (deffoo nntp-request-head (article &optional group server)
639 (nntp-possibly-change-group group server)
640 (when (nntp-send-command
641 "\r?\n\\.\r?\n" "HEAD"
642 (if (numberp article) (int-to-string article) article))
644 (nntp-find-group-and-number)
645 (nntp-decode-text))))
647 (deffoo nntp-request-body (article &optional group server)
648 (nntp-possibly-change-group group server)
649 (nntp-send-command-and-decode
650 "\r?\n\\.\r?\n" "BODY"
651 (if (numberp article) (int-to-string article) article)))
653 (deffoo nntp-request-group (group &optional server dont-check)
654 (nntp-possibly-change-group nil server)
655 (when (nntp-send-command "^[245].*\n" "GROUP" group)
656 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
657 (setcar (cddr entry) group))))
659 (deffoo nntp-close-group (group &optional server)
662 (deffoo nntp-server-opened (&optional server)
663 "Say whether a connection to SERVER has been opened."
664 (and (nnoo-current-server-p 'nntp server)
666 (gnus-buffer-live-p nntp-server-buffer)
667 (nntp-find-connection nntp-server-buffer)))
669 (deffoo nntp-open-server (server &optional defs connectionless)
670 (nnheader-init-server-buffer)
671 (if (nntp-server-opened server)
673 (when (or (stringp (car defs))
674 (numberp (car defs)))
675 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
676 (unless (assq 'nntp-address defs)
677 (setq defs (append defs (list (list 'nntp-address server)))))
678 (nnoo-change-server 'nntp server defs)
679 (unless connectionless
680 (or (nntp-find-connection nntp-server-buffer)
681 (nntp-open-connection nntp-server-buffer)))))
683 (deffoo nntp-close-server (&optional server)
684 (nntp-possibly-change-group nil server t)
685 (let ((process (nntp-find-connection nntp-server-buffer)))
687 (when (memq (process-status process) '(open run))
689 (nntp-send-string process "QUIT")
690 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
691 ;; Ok, this is evil, but when using telnet and stuff
692 ;; as the connection method, it's important that the
693 ;; QUIT command actually is sent out before we kill
696 (when (buffer-name (process-buffer process))
697 (kill-buffer (process-buffer process)))
698 (setq process (car (pop nntp-connection-alist))))
699 (nnoo-close-server 'nntp)))
701 (deffoo nntp-request-close ()
703 (while (setq process (pop nntp-connection-list))
704 (when (memq (process-status process) '(open run))
706 (nntp-send-string process "QUIT")
707 (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
708 ;; Ok, this is evil, but when using telnet and stuff
709 ;; as the connection method, it's important that the
710 ;; QUIT command actually is sent out before we kill
713 (when (buffer-name (process-buffer process))
714 (kill-buffer (process-buffer process))))))
716 (deffoo nntp-request-list (&optional server)
717 (nntp-possibly-change-group nil server)
718 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST"))
720 (deffoo nntp-request-list-newsgroups (&optional server)
721 (nntp-possibly-change-group nil server)
722 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS"))
724 (deffoo nntp-request-newgroups (date &optional server)
725 (nntp-possibly-change-group nil server)
727 (set-buffer nntp-server-buffer)
730 "^\\.\r?\n" "NEWGROUPS"
731 (format-time-string "%y%m%d %H%M%S" (nnmail-date-to-time date)))
732 (nntp-decode-text))))
734 (deffoo nntp-request-post (&optional server)
735 (nntp-possibly-change-group nil server)
736 (when (nntp-send-command "^[23].*\r?\n" "POST")
737 (let ((response (save-current-buffer
738 (set-buffer nntp-server-buffer)
739 nntp-process-response))
742 (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
744 (setq server-id (match-string 1 response))
745 (narrow-to-region (goto-char (point-min))
746 (if (search-forward "\n\n" nil t)
749 (unless (mail-fetch-field "Message-ID")
750 (goto-char (point-min))
751 (insert "Message-ID: " server-id "\n"))
753 (run-hooks 'nntp-prepare-post-hook)
754 (nntp-send-buffer "^[23].*\n"))))
756 (deffoo nntp-request-type (group article)
759 (deffoo nntp-asynchronous-p ()
764 (defun nntp-send-mode-reader ()
765 "Send the MODE READER command to the nntp server.
766 This function is supposed to be called from `nntp-server-opened-hook'.
767 It will make innd servers spawn an nnrpd process to allow actual article
769 (nntp-send-command "^.*\r?\n" "MODE READER"))
771 (defun nntp-send-authinfo (&optional send-if-force)
772 "Send the AUTHINFO to the nntp server.
773 It will look in the \"~/.authinfo\" file for matching entries. If
774 nothing suitable is found there, it will prompt for a user name
777 If SEND-IF-FORCE, only send authinfo to the server if the
778 .authinfo file has the FORCE token."
779 (let* ((list (gnus-parse-netrc nntp-authinfo-file))
780 (alist (gnus-netrc-machine list nntp-address))
781 (force (gnus-netrc-get alist "force"))
782 (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
783 (passwd (gnus-netrc-get alist "password")))
784 (when (or (not send-if-force)
787 (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
788 nntp-authinfo-user user))
789 (unless (member user '(nil ""))
790 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
791 (when t ;???Should check if AUTHINFO succeeded
793 "^2.*\r?\n" "AUTHINFO PASS"
795 nntp-authinfo-password
796 (setq nntp-authinfo-password
797 (nnmail-read-passwd (format "NNTP (%s@%s) password: "
798 user nntp-address))))))))))
800 (defun nntp-send-nosy-authinfo ()
801 "Send the AUTHINFO to the nntp server."
802 (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
803 (unless (member user '(nil ""))
804 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
805 (when t ;???Should check if AUTHINFO succeeded
806 (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
807 (nnmail-read-passwd "NNTP (%s@%s) password: "
808 user nntp-address))))))
810 (defun nntp-send-authinfo-from-file ()
811 "Send the AUTHINFO to the nntp server.
813 The authinfo login name is taken from the user's login name and the
814 password contained in '~/.nntp-authinfo'."
815 (when (file-exists-p "~/.nntp-authinfo")
816 (nnheader-temp-write nil
817 (insert-file-contents "~/.nntp-authinfo")
818 (goto-char (point-min))
819 (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
821 "^2.*\r?\n" "AUTHINFO PASS"
822 (buffer-substring (point) (progn (end-of-line) (point)))))))
824 ;;; Internal functions.
826 (defun nntp-handle-authinfo (process)
827 "Take care of an authinfo response from the server."
828 (let ((last nntp-last-command))
829 (funcall nntp-authinfo-function)
830 ;; We have to re-send the function that was interrupted by
831 ;; the authinfo request.
833 (set-buffer nntp-server-buffer)
835 (nntp-send-string process last)))
837 (defun nntp-make-process-buffer (buffer)
838 "Create a new, fresh buffer usable for nntp process connections."
842 (format " *server %s %s %s*"
843 nntp-address nntp-port-number
844 (gnus-buffer-exists-p buffer))))
845 (buffer-disable-undo (current-buffer))
846 (set (make-local-variable 'after-change-functions) nil)
847 (set (make-local-variable 'nntp-process-wait-for) nil)
848 (set (make-local-variable 'nntp-process-callback) nil)
849 (set (make-local-variable 'nntp-process-to-buffer) nil)
850 (set (make-local-variable 'nntp-process-start-point) nil)
851 (set (make-local-variable 'nntp-process-decode) nil)
854 (defun nntp-open-connection (buffer)
855 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
856 (run-hooks 'nntp-prepare-server-hook)
857 (let* ((pbuffer (nntp-make-process-buffer buffer))
859 (and nntp-connection-timeout
860 (nnheader-run-at-time
861 nntp-connection-timeout nil
863 (when (buffer-name ,pbuffer)
864 (kill-buffer ,pbuffer))))))
867 (funcall nntp-open-connection-function pbuffer)
871 (nnheader-cancel-timer timer))
872 (when (and (buffer-name pbuffer)
874 (process-kill-without-query process)
875 (nntp-wait-for process "^.*\n" buffer nil t)
876 (if (memq (process-status process) '(open run))
878 (caar (push (list process buffer nil) nntp-connection-alist))
879 (push process nntp-connection-list)
882 (nntp-read-server-type)
884 (set-buffer nntp-server-buffer)
885 (let ((nnheader-callback-function nil))
886 (run-hooks 'nntp-server-opened-hook)
887 (nntp-send-authinfo t))))
888 (when (buffer-name (process-buffer process))
889 (kill-buffer (process-buffer process)))
892 (defun nntp-open-network-stream (buffer)
893 (open-network-stream-as-binary
894 "nntpd" buffer nntp-address nntp-port-number))
896 (defun nntp-open-ssl-stream (buffer)
897 (let* ((ssl-program-arguments '("-connect" (concat host ":" service)))
898 (proc (open-ssl-stream "nntpd" buffer nntp-address nntp-port-number)))
901 (nntp-wait-for-string "^\r*20[01]")
903 (delete-region (point-min) (point))
906 (defun nntp-read-server-type ()
907 "Find out what the name of the server we have connected to is."
908 ;; Wait for the status string to arrive.
909 (setq nntp-server-type (buffer-string))
910 (let ((alist nntp-server-action-alist)
913 ;; Run server-specific commands.
915 (setq entry (pop alist))
916 (when (string-match (car entry) nntp-server-type)
917 (if (and (listp (cadr entry))
918 (not (eq 'lambda (caadr entry))))
920 (funcall (cadr entry)))))))
922 (defun nntp-after-change-function-callback (beg end len)
923 (when nntp-process-callback
925 (if (and (= beg (point-min))
926 (memq (char-after beg) '(?4 ?5)))
927 ;; Report back error messages.
930 (if (looking-at "480")
931 (nntp-handle-authinfo nntp-process-to-buffer)
932 (nntp-snarf-error-message)
933 (funcall nntp-process-callback nil)))
935 (when (and (> (point) nntp-process-start-point)
936 (re-search-backward nntp-process-wait-for
937 nntp-process-start-point t))
938 (when (gnus-buffer-exists-p nntp-process-to-buffer)
939 (let ((cur (current-buffer))
940 (start nntp-process-start-point))
942 (set-buffer nntp-process-to-buffer)
943 (goto-char (point-max))
945 (insert-buffer-substring cur start)
946 (narrow-to-region b (point-max))
950 (let ((callback nntp-process-callback)
951 (nntp-inside-change-function t))
952 (setq nntp-process-callback nil)
954 (funcall callback (buffer-name
955 (get-buffer nntp-process-to-buffer))))))))))
957 (defun nntp-snarf-error-message ()
958 "Save the error message in the current buffer."
959 (let ((message (buffer-string)))
960 (while (string-match "[\r\n]+" message)
961 (setq message (replace-match " " t t message)))
962 (nnheader-report 'nntp message)
965 (defun nntp-accept-process-output (process)
966 "Wait for output from PROCESS and message some dots."
968 (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
970 (let ((len (/ (point-max) 1024))
973 (setq nntp-have-messaged t)
974 (nnheader-message 7 "nntp read: %dk" len)))
975 (accept-process-output process 1)))
977 (defun nntp-accept-response ()
978 "Wait for output from the process that outputs to BUFFER."
979 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
981 (defun nntp-possibly-change-group (group server &optional connectionless)
982 (let ((nnheader-callback-function nil))
984 (or (nntp-server-opened server)
985 (nntp-open-server server nil connectionless)))
987 (unless connectionless
988 (or (nntp-find-connection nntp-server-buffer)
989 (nntp-open-connection nntp-server-buffer))))
992 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
993 (when (not (equal group (caddr entry)))
995 (set-buffer (process-buffer (car entry)))
997 (nntp-send-string (car entry) (concat "GROUP " group))
998 ;; allow for unexpected responses, since this can be called
999 ;; from a timer with quit inhibited
1000 (nntp-wait-for-string "^[245].*\n")
1001 (setcar (cddr entry) group)
1004 (defun nntp-decode-text (&optional cr-only)
1005 "Decode the text in the current buffer."
1006 (goto-char (point-min))
1007 (while (search-forward "\r" nil t)
1010 ;; Remove trailing ".\n" end-of-transfer marker.
1011 (goto-char (point-max))
1013 (when (looking-at ".\n")
1015 ;; Delete status line.
1016 (goto-char (point-min))
1017 (delete-region (point) (progn (forward-line 1) (point)))
1018 ;; Remove "." -> ".." encoding.
1019 (while (search-forward "\n.." nil t)
1022 (defun nntp-encode-text ()
1023 "Encode the text in the current buffer."
1025 ;; Replace "." at beginning of line with "..".
1026 (goto-char (point-min))
1027 (while (re-search-forward "^\\." nil t)
1029 (goto-char (point-max))
1030 ;; Insert newline at the end of the buffer.
1033 ;; Insert `.' at end of buffer (end of text mark).
1034 (goto-char (point-max))
1036 (goto-char (point-min))
1040 (insert nntp-end-of-line))))
1042 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1043 (set-buffer nntp-server-buffer)
1047 ;; This server does not talk NOV.
1048 ((not nntp-server-xover)
1051 ;; We don't care about gaps.
1052 ((or (not nntp-nov-gap)
1054 (nntp-send-xover-command
1056 (if (numberp fetch-old)
1057 (max 1 (- (car articles) fetch-old))
1060 (car (last articles)) 'wait)
1062 (goto-char (point-min))
1063 (when (looking-at "[1-5][0-9][0-9] ")
1064 (delete-region (point) (progn (forward-line 1) (point))))
1065 (while (search-forward "\r" nil t)
1066 (replace-match "" t t))
1067 (goto-char (point-max))
1069 (when (looking-at "\\.")
1070 (delete-region (point) (progn (forward-line 1) (point)))))
1072 ;; We do it the hard way. For each gap, an XOVER command is sent
1073 ;; to the server. We do not wait for a reply from the server, we
1074 ;; just send them off as fast as we can. That means that we have
1075 ;; to count the number of responses we get back to find out when we
1076 ;; have gotten all we asked for.
1077 ((numberp nntp-nov-gap)
1080 (last-point (point-min))
1081 (buf nntp-server-buffer)
1082 ;;(process-buffer (nntp-find-connection (current-buffer))))
1084 ;; We have to check `nntp-server-xover'. If it gets set to nil,
1085 ;; that means that the server does not understand XOVER, but we
1086 ;; won't know that until we try.
1087 (while (and nntp-server-xover articles)
1088 (setq first (car articles))
1089 ;; Search forward until we find a gap, or until we run out of
1091 (while (and (cdr articles)
1092 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1093 (setq articles (cdr articles)))
1095 (when (nntp-send-xover-command first (car articles))
1096 (setq articles (cdr articles)
1099 ;; Every 400 requests we have to read the stream in
1100 ;; order to avoid deadlocks.
1101 (when (or (null articles) ;All requests have been sent.
1102 (zerop (% count nntp-maximum-request)))
1103 (accept-process-output)
1104 ;; On some Emacs versions the preceding function has
1105 ;; a tendency to change the buffer. Perhaps. It's
1106 ;; quite difficult to reproduce, because it only
1107 ;; seems to happen once in a blue moon.
1110 (goto-char last-point)
1112 (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
1113 (setq received (1+ received)))
1114 (setq last-point (point))
1116 (accept-process-output)
1117 (set-buffer buf)))))
1119 (when nntp-server-xover
1120 ;; Wait for the reply from the final command.
1121 (goto-char (point-max))
1122 (re-search-backward "^[0-9][0-9][0-9] " nil t)
1123 (when (looking-at "^[23]")
1125 (goto-char (point-max))
1127 (not (looking-at "^\\.\r?\n")))
1128 (nntp-accept-response)))
1130 ;; We remove any "." lines and status lines.
1131 (goto-char (point-min))
1132 (while (search-forward "\r" nil t)
1134 (goto-char (point-min))
1135 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1136 ;;(copy-to-buffer nntp-server-buffer (point-min) (point-max))
1141 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1142 "Send the XOVER command to the server."
1143 (let ((range (format "%d-%d" beg end))
1144 (nntp-inhibit-erase t))
1145 (if (stringp nntp-server-xover)
1146 ;; If `nntp-server-xover' is a string, then we just send this
1149 (nntp-send-command-nodelete
1150 "\r?\n\\.\r?\n" nntp-server-xover range)
1151 ;; We do not wait for the reply.
1152 (nntp-send-command-nodelete "\r?\n\\.\r?\n" nntp-server-xover range))
1153 (let ((commands nntp-xover-commands))
1154 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1155 ;; We try them all until we get at positive response.
1156 (while (and commands (eq nntp-server-xover 'try))
1157 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1159 (set-buffer nntp-server-buffer)
1160 (goto-char (point-min))
1161 (and (looking-at "[23]") ; No error message.
1162 ;; We also have to look at the lines. Some buggy
1163 ;; servers give back simple lines with just the
1164 ;; article number. How... helpful.
1167 (looking-at "[0-9]+\t...")) ; More text after number.
1168 (setq nntp-server-xover (car commands))))
1169 (setq commands (cdr commands)))
1170 ;; If none of the commands worked, we disable XOVER.
1171 (when (eq nntp-server-xover 'try)
1173 (set-buffer nntp-server-buffer)
1175 (setq nntp-server-xover nil)))
1176 nntp-server-xover))))
1178 ;;; Alternative connection methods.
1180 (defun nntp-wait-for-string (regexp)
1181 "Wait until string arrives in the buffer."
1182 (let ((buf (current-buffer)))
1183 (goto-char (point-min))
1184 (while (not (re-search-forward regexp nil t))
1185 (accept-process-output (nntp-find-connection nntp-server-buffer))
1187 (goto-char (point-min)))))
1189 (defun nntp-open-telnet (buffer)
1193 (let ((proc (as-binary-process
1196 "nntpd" buffer nntp-telnet-command nntp-telnet-switches)))
1197 (case-fold-search t))
1198 (when (memq (process-status proc) '(open run))
1199 (process-send-string proc "set escape \^X\n")
1201 ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1202 (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1203 nntp-address "\n")))
1205 (process-send-string proc (concat "open " nntp-address "\n"))))
1207 ((not nntp-open-telnet-envuser)
1208 (nntp-wait-for-string "^\r*.?login:")
1209 (process-send-string
1211 (or nntp-telnet-user-name
1212 (setq nntp-telnet-user-name (read-string "login: ")))
1214 (nntp-wait-for-string "^\r*.?password:")
1215 (process-send-string
1217 (or nntp-telnet-passwd
1218 (setq nntp-telnet-passwd
1219 (nnmail-read-passwd "Password: ")))
1222 (nntp-wait-for-string nntp-telnet-shell-prompt)
1223 (process-send-string
1224 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1225 (nntp-wait-for-string "^\r*20[01]")
1227 (delete-region (point-min) (point))
1228 (process-send-string proc "\^]")
1229 (nntp-wait-for-string "^telnet")
1230 (process-send-string proc "mode character\n")
1231 (accept-process-output proc 1)
1233 (goto-char (point-min))
1235 (delete-region (point) (point-max)))
1238 (defun nntp-open-rlogin (buffer)
1239 "Open a connection to SERVER using rsh."
1240 (let ((proc (if nntp-rlogin-user-name
1242 (apply 'start-process
1243 "nntpd" buffer nntp-rlogin-program
1244 nntp-address "-l" nntp-rlogin-user-name
1245 nntp-rlogin-parameters))
1247 (apply 'start-process
1248 "nntpd" buffer nntp-rlogin-program nntp-address
1249 nntp-rlogin-parameters)))))
1252 (nntp-wait-for-string "^\r*20[01]")
1254 (delete-region (point-min) (point))
1257 (defun nntp-find-group-and-number ()
1260 (set-buffer nntp-server-buffer)
1261 (narrow-to-region (goto-char (point-min))
1262 (or (search-forward "\n\n" nil t) (point-max)))
1263 (goto-char (point-min))
1264 ;; We first find the number by looking at the status line.
1265 (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1267 (buffer-substring (match-beginning 1)
1269 group newsgroups xref)
1270 (and number (zerop number) (setq number nil))
1271 ;; Then we find the group name.
1274 ;; If there is only one group in the Newsgroups header,
1275 ;; then it seems quite likely that this article comes
1276 ;; from that group, I'd say.
1277 ((and (setq newsgroups (mail-fetch-field "newsgroups"))
1278 (not (string-match "," newsgroups)))
1280 ;; If there is more than one group in the Newsgroups
1281 ;; header, then the Xref header should be filled out.
1282 ;; We hazard a guess that the group that has this
1283 ;; article number in the Xref header is the one we are
1284 ;; looking for. This might very well be wrong if this
1285 ;; article happens to have the same number in several
1286 ;; groups, but that's life.
1287 ((and (setq xref (mail-fetch-field "xref"))
1289 (string-match (format "\\([^ :]+\\):%d" number) xref))
1290 (substring xref (match-beginning 1) (match-end 1)))
1292 (when (string-match "\r" group)
1293 (setq group (substring group 0 (match-beginning 0))))
1294 (cons group number)))))
1298 ;;; nntp.el ends here