0e9c4028371c1160ed7c8be6151926d8df477699
[gnus] / lisp / nntp.el
1 ;;; nntp.el --- nntp access for Gnus
2 ;;; Copyright (C) 1987-90,92-97 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'nnheader)
28 (require 'nnoo)
29 (require 'gnus-util)
30
31 (nnoo-declare nntp)
32
33 (eval-and-compile
34   (unless (fboundp 'open-network-stream)
35     (require 'tcp)))
36
37 (eval-when-compile (require 'cl))
38
39 (defvoo nntp-address nil
40   "Address of the physical nntp server.")
41
42 (defvoo nntp-port-number "nntp"
43   "Port number on the physical nntp server.")
44
45 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
46   "*Hook used for sending commands to the server at startup.
47 The default value is `nntp-send-mode-reader', which makes an innd
48 server spawn an nnrpd server.  Another useful function to put in this
49 hook might be `nntp-send-authinfo', which will prompt for a password
50 to allow posting from the server.  Note that this is only necessary to
51 do on servers that use strict access control.")
52
53 (defvoo nntp-authinfo-function 'nntp-send-authinfo
54   "Function used to send AUTHINFO to the server.")
55
56 (defvoo nntp-server-action-alist
57   '(("nntpd 1\\.5\\.11t"
58      (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
59     ("NNRP server Netscape"
60      (setq nntp-server-list-active-group nil)))
61   "Alist of regexps to match on server types and actions to be taken.
62 For instance, if you want Gnus to beep every time you connect
63 to innd, you could say something like:
64
65 \(setq nntp-server-action-alist
66        '((\"innd\" (ding))))
67
68 You probably don't want to do that, though.")
69
70 (defvoo nntp-open-connection-function 'nntp-open-network-stream
71   "*Function used for connecting to a remote system.
72 It will be called with the buffer to output in.
73
74 Two pre-made functions are `nntp-open-network-stream', which is the
75 default, and simply connects to some port or other on the remote
76 system (see nntp-port-number).  The other are `nntp-open-rlogin',
77 which does an rlogin on the remote system, and then does a telnet to
78 the NNTP server available there (see nntp-rlogin-parameters) and
79 `nntp-open-telnet' which telnets to a remote system, logs in and does
80 the same.")
81
82 (defvoo nntp-rlogin-program "rsh"
83   "*Program used to log in on remote machines.
84 The default is \"rsh\", but \"ssh\" is a popular alternative.")
85
86 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
87   "*Parameters to `nntp-open-login'.
88 That function may be used as `nntp-open-connection-function'.  In that
89 case, this list will be used as the parameter list given to rsh.")
90
91 (defvoo nntp-rlogin-user-name nil
92   "*User name on remote system when using the rlogin connect method.")
93
94 (defvoo nntp-telnet-parameters '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
95   "*Parameters to `nntp-open-telnet'.
96 That function may be used as `nntp-open-connection-function'.  In that
97 case, this list will be executed as a command after logging in
98 via telnet.")
99
100 (defvoo nntp-telnet-user-name nil
101   "User name to log in via telnet with.")
102
103 (defvoo nntp-telnet-passwd nil
104   "Password to use to log in via telnet with.")
105
106 (defvoo nntp-open-telnet-envuser nil
107   "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
108
109 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
110   "*Regular expression to match the shell prompt on the remote machine.")
111
112 (defvoo nntp-telnet-command "telnet"
113   "Command used to start telnet.")
114
115 (defvoo nntp-telnet-switches '("-8")
116   "Switches given to the telnet command.")
117
118 (defvoo nntp-end-of-line "\r\n"
119   "String to use on the end of lines when talking to the NNTP server.
120 This is \"\\r\\n\" by default, but should be \"\\n\" when
121 using rlogin or telnet to communicate with the server.")
122
123 (defvoo nntp-large-newsgroup 50
124   "*The number of the articles which indicates a large newsgroup.
125 If the number of the articles is greater than the value, verbose
126 messages will be shown to indicate the current status.")
127
128 (defvoo nntp-maximum-request 400
129   "*The maximum number of the requests sent to the NNTP server at one time.
130 If Emacs hangs up while retrieving headers, set the variable to a
131 lower value.")
132
133 (defvoo nntp-nov-is-evil nil
134   "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
135
136 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
137   "*List of strings that are used as commands to fetch NOV lines from a server.
138 The strings are tried in turn until a positive response is gotten.  If
139 none of the commands are successful, nntp will just grab headers one
140 by one.")
141
142 (defvoo nntp-nov-gap 5
143   "*Maximum allowed gap between two articles.
144 If the gap between two consecutive articles is bigger than this
145 variable, split the XOVER request into two requests.")
146
147 (defvoo nntp-connection-timeout nil
148   "*Number of seconds to wait before an nntp connection times out.
149 If this variable is nil, which is the default, no timers are set.")
150
151 (defvoo nntp-prepare-server-hook nil
152   "*Hook run before a server is opened.
153 If can be used to set up a server remotely, for instance.  Say you
154 have an account at the machine \"other.machine\".  This machine has
155 access to an NNTP server that you can't access locally.  You could
156 then use this hook to rsh to the remote machine and start a proxy NNTP
157 server there that you can connect to.  See also `nntp-open-connection-function'")
158
159 (defvoo nntp-warn-about-losing-connection t
160   "*If non-nil, beep when a server closes connection.")
161
162 (defvoo nntp-coding-system-for-read 'binary
163   "*Coding system to read from NNTP.")
164
165 (defvoo nntp-coding-system-for-write 'binary
166     "*Coding system to write to NNTP.")
167
168 \f
169
170 ;;; Internal variables.
171
172 (defvar nntp-have-messaged nil)
173
174 (defvar nntp-process-wait-for nil)
175 (defvar nntp-process-to-buffer nil)
176 (defvar nntp-process-callback nil)
177 (defvar nntp-process-decode nil)
178 (defvar nntp-process-start-point nil)
179 (defvar nntp-inside-change-function nil)
180 (defvoo nntp-last-command-time nil)
181 (defvoo nntp-last-command nil)
182
183 (defvar nntp-connection-list nil)
184
185 (defvoo nntp-server-type nil)
186 (defvoo nntp-connection-alist nil)
187 (defvoo nntp-status-string "")
188 (defconst nntp-version "nntp 5.0")
189 (defvoo nntp-inhibit-erase nil)
190 (defvoo nntp-inhibit-output nil)
191
192 (defvoo nntp-server-xover 'try)
193 (defvoo nntp-server-list-active-group 'try)
194
195 (eval-and-compile
196   (autoload 'nnmail-read-passwd "nnmail"))
197
198 \f
199
200 ;;; Internal functions.
201
202 (defsubst nntp-send-string (process string)
203   "Send STRING to PROCESS."
204   (setq nntp-last-command-time (current-time)
205         nntp-last-command string)
206   (process-send-string process (concat string nntp-end-of-line)))
207
208 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
209   "Wait for WAIT-FOR to arrive from PROCESS."
210   (save-excursion
211     (set-buffer (process-buffer process))
212     (goto-char (point-min))
213     (while (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
214                (looking-at "480"))
215       (when (looking-at "480")
216         (nntp-handle-authinfo process))
217       (nntp-accept-process-output process)
218       (goto-char (point-min)))
219     (prog1
220         (if (looking-at "[45]")
221             (progn
222               (nntp-snarf-error-message)
223               nil)
224           (goto-char (point-max))
225           (let ((limit (point-min)))
226             (while (not (re-search-backward wait-for limit t))
227               (nntp-accept-process-output process)
228               ;; We assume that whatever we wait for is less than 1000
229               ;; characters long.
230               (setq limit (max (- (point-max) 1000) (point-min)))
231               (goto-char (point-max))))
232           (nntp-decode-text (not decode))
233           (unless discard
234             (save-excursion
235               (set-buffer buffer)
236               (goto-char (point-max))
237               (insert-buffer-substring (process-buffer process))
238               ;; Nix out "nntp reading...." message.
239               (when nntp-have-messaged
240                 (setq nntp-have-messaged nil)
241                 (message ""))
242               t)))
243       (unless discard
244         (erase-buffer)))))
245
246 (defsubst nntp-find-connection (buffer)
247   "Find the connection delivering to BUFFER."
248   (let ((alist nntp-connection-alist)
249         (buffer (if (stringp buffer) (get-buffer buffer) buffer))
250         process entry)
251     (while (setq entry (pop alist))
252       (when (eq buffer (cadr entry))
253         (setq process (car entry)
254               alist nil)))
255     (when process
256       (if (memq (process-status process) '(open run))
257           process
258         (when (buffer-name (process-buffer process))
259           (kill-buffer (process-buffer process)))
260         (setq nntp-connection-alist (delq entry nntp-connection-alist))
261         nil))))
262
263 (defsubst nntp-find-connection-entry (buffer)
264   "Return the entry for the connection to BUFFER."
265   (assq (nntp-find-connection buffer) nntp-connection-alist))
266
267 (defun nntp-find-connection-buffer (buffer)
268   "Return the process connection buffer tied to BUFFER."
269   (let ((process (nntp-find-connection buffer)))
270     (when process
271       (process-buffer process))))
272
273 (defsubst nntp-retrieve-data (command address port buffer
274                                       &optional wait-for callback decode)
275   "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
276   (let ((process (or (nntp-find-connection buffer)
277                      (nntp-open-connection buffer))))
278     (if (not process)
279         (nnheader-report 'nntp "Couldn't open connection to %s" address)
280       (unless (or nntp-inhibit-erase nnheader-callback-function)
281         (save-excursion
282           (set-buffer (process-buffer process))
283           (erase-buffer)))
284       (when command
285         (nntp-send-string process command))
286       (cond
287        ((eq callback 'ignore)
288         t)
289        ((and callback wait-for)
290         (save-excursion
291     &