comparison lisp/gnus/nntp.el @ 16:0293115a14e9 r19-15b91

Import from CVS: tag r19-15b91
author cvs
date Mon, 13 Aug 2007 08:49:20 +0200
parents ac2d302a0011
children 8fc7fe29b841
comparison
equal deleted inserted replaced
15:ad457d5f7d04 16:0293115a14e9
1 ;;; nntp.el --- nntp access for Gnus 1 ;;; nntp.el --- nntp access for Gnus
2 ;; Copyright (C) 1987,88,89,90,92,93,94,95,96 Free Software Foundation, Inc. 2 ;;; Copyright (C) 1987,88,89,90,92,93,94,95,96,97 Free Software Foundation, Inc.
3 3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet> 4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news 5 ;; Keywords: news
7 6
8 ;; This file is part of GNU Emacs. 7 ;; This file is part of GNU Emacs.
9 8
10 ;; GNU Emacs is free software; you can redistribute it and/or modify 9 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details. 17 ;; GNU General Public License for more details.
19 18
20 ;; You should have received a copy of the GNU General Public License 19 ;; 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 the 20 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, 21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 ;; Boston, MA 02111-1307, USA.
24 22
25 ;;; Commentary: 23 ;;; Commentary:
26 24
27 ;;; Code: 25 ;;; Code:
28 26
29 (require 'nnheader) 27 (require 'nnheader)
30 (require 'nnoo) 28 (require 'nnoo)
31 (eval-when-compile (require 'cl)) 29 (require 'gnus-util)
32 30
33 (nnoo-declare nntp) 31 (nnoo-declare nntp)
34 32
35 (eval-and-compile 33 (eval-and-compile
36 (unless (fboundp 'open-network-stream) 34 (unless (fboundp 'open-network-stream)
37 (require 'tcp))) 35 (require 'tcp)))
38 36
39 (eval-when-compile (require 'cl)) 37 (eval-when-compile (require 'cl))
40 38
41 (eval-and-compile 39 (defvoo nntp-address nil
42 (autoload 'cancel-timer "timer") 40 "Address of the physical nntp server.")
43 (autoload 'telnet "telnet" nil t) 41
44 (autoload 'telnet-send-input "telnet" nil t) 42 (defvoo nntp-port-number "nntp"
45 (autoload 'timezone-parse-date "timezone")) 43 "Port number on the physical nntp server.")
46
47 (defvoo nntp-server-hook nil
48 "*Hooks for the NNTP server.
49 If the kanji code of the NNTP server is different from the local kanji
50 code, the correct kanji code of the buffer associated with the NNTP
51 server must be specified as follows:
52
53 \(setq nntp-server-hook
54 (function
55 (lambda ()
56 ;; Server's Kanji code is EUC (NEmacs hack).
57 (make-local-variable 'kanji-fileio-code)
58 (setq kanji-fileio-code 0))))
59
60 If you'd like to change something depending on the server in this
61 hook, use the variable `nntp-address'.")
62 44
63 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader) 45 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
64 "*Hook used for sending commands to the server at startup. 46 "*Hook used for sending commands to the server at startup.
65 The default value is `nntp-send-mode-reader', which makes an innd 47 The default value is `nntp-send-mode-reader', which makes an innd
66 server spawn an nnrpd server. Another useful function to put in this 48 server spawn an nnrpd server. Another useful function to put in this
67 hook might be `nntp-send-authinfo', which will prompt for a password 49 hook might be `nntp-send-authinfo', which will prompt for a password
68 to allow posting from the server. Note that this is only necessary to 50 to allow posting from the server. Note that this is only necessary to
69 do on servers that use strict access control.") 51 do on servers that use strict access control.")
70 (add-hook 'nntp-server-opened-hook 'nntp-send-mode-reader) 52
53 (defvoo nntp-authinfo-function 'nntp-send-authinfo
54 "Function used to send AUTHINFO to the server.")
71 55
72 (defvoo nntp-server-action-alist 56 (defvoo nntp-server-action-alist
73 '(("nntpd 1\\.5\\.11t" 57 '(("nntpd 1\\.5\\.11t"
74 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))) 58 (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
59 ("NNRP server Netscape"
60 (setq nntp-server-list-active-group nil)))
75 "Alist of regexps to match on server types and actions to be taken. 61 "Alist of regexps to match on server types and actions to be taken.
76 For instance, if you want Gnus to beep every time you connect 62 For instance, if you want Gnus to beep every time you connect
77 to innd, you could say something like: 63 to innd, you could say something like:
78 64
79 \(setq nntp-server-action-alist 65 \(setq nntp-server-action-alist
80 '((\"innd\" (ding)))) 66 '((\"innd\" (ding))))
81 67
82 You probably don't want to do that, though.") 68 You probably don't want to do that, though.")
83 69
84 (defvoo nntp-open-server-function 'nntp-open-network-stream 70 (defvoo nntp-open-connection-function 'nntp-open-network-stream
85 "*Function used for connecting to a remote system. 71 "*Function used for connecting to a remote system.
86 It will be called with the address of the remote system. 72 It will be called with the buffer to output in.
87 73
88 Two pre-made functions are `nntp-open-network-stream', which is the 74 Two pre-made functions are `nntp-open-network-stream', which is the
89 default, and simply connects to some port or other on the remote 75 default, and simply connects to some port or other on the remote
90 system (see nntp-port-number). The other is `nntp-open-rlogin', which 76 system (see nntp-port-number). The other are `nntp-open-rlogin', which
91 does an rlogin on the remote system, and then does a telnet to the 77 does an rlogin on the remote system, and then does a telnet to the
92 NNTP server available there (see nntp-rlogin-parameters).") 78 NNTP server available there (see nntp-rlogin-parameters) and `nntp-open-telnet' which
93 79 telnets to a remote system, logs in and does the same")
94 (defvoo nntp-rlogin-parameters '("telnet" "${NNTPSERVER:=localhost}" "nntp") 80
95 "*Parameters to `nntp-open-rlogin'. 81 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
96 That function may be used as `nntp-open-server-function'. In that 82 "*Parameters to `nntp-open-login'.
83 That function may be used as `nntp-open-connection-function'. In that
97 case, this list will be used as the parameter list given to rsh.") 84 case, this list will be used as the parameter list given to rsh.")
98 85
99 (defvoo nntp-rlogin-user-name nil 86 (defvoo nntp-rlogin-user-name nil
100 "*User name on remote system when using the rlogin connect method.") 87 "*User name on remote system when using the rlogin connect method.")
101 88
102 (defvoo nntp-telnet-parameters '("exec" "telnet" "-8" "${NNTPSERVER:=localhost}" "nntp") 89 (defvoo nntp-telnet-parameters '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
103 "*Parameters to `nntp-open-telnet'. 90 "*Parameters to `nntp-open-telnet'.
104 That function may be used as `nntp-open-server-function'. In that 91 That function may be used as `nntp-open-connection-function'. In that
105 case, this list will be executed as a command after logging in 92 case, this list will be executed as a command after logging in
106 via telnet.") 93 via telnet.")
107 94
108 (defvoo nntp-telnet-user-name nil 95 (defvoo nntp-telnet-user-name nil
109 "User name to log in via telnet with.") 96 "User name to log in via telnet with.")
110 97
111 (defvoo nntp-telnet-passwd nil 98 (defvoo nntp-telnet-passwd nil
112 "Password to use to log in via telnet with.") 99 "Password to use to log in via telnet with.")
113
114 (defvoo nntp-address nil
115 "*The name of the NNTP server.")
116
117 (defvoo nntp-port-number "nntp"
118 "*Port number to connect to.")
119 100
120 (defvoo nntp-end-of-line "\r\n" 101 (defvoo nntp-end-of-line "\r\n"
121 "String to use on the end of lines when talking to the NNTP server. 102 "String to use on the end of lines when talking to the NNTP server.
122 This is \"\\r\\n\" by default, but should be \"\\n\" when 103 This is \"\\r\\n\" by default, but should be \"\\n\" when
123 using rlogin or telnet to communicate with the server.") 104 using rlogin or telnet to communicate with the server.")
125 (defvoo nntp-large-newsgroup 50 106 (defvoo nntp-large-newsgroup 50
126 "*The number of the articles which indicates a large newsgroup. 107 "*The number of the articles which indicates a large newsgroup.
127 If the number of the articles is greater than the value, verbose 108 If the number of the articles is greater than the value, verbose
128 messages will be shown to indicate the current status.") 109 messages will be shown to indicate the current status.")
129 110
130 (defvoo nntp-buggy-select (memq system-type '(fujitsu-uts))
131 "*t if your select routine is buggy.
132 If the select routine signals error or fall into infinite loop while
133 waiting for the server response, the variable must be set to t. In
134 case of Fujitsu UTS, it is set to T since `accept-process-output'
135 doesn't work properly.")
136
137 (defvoo nntp-maximum-request 400 111 (defvoo nntp-maximum-request 400
138 "*The maximum number of the requests sent to the NNTP server at one time. 112 "*The maximum number of the requests sent to the NNTP server at one time.
139 If Emacs hangs up while retrieving headers, set the variable to a 113 If Emacs hangs up while retrieving headers, set the variable to a
140 lower value.") 114 lower value.")
141 115
142 (defvoo nntp-debug-read 10000
143 "*Display '...' every 10Kbytes of a message being received if it is non-nil.
144 If it is a number, dots are displayed per the number.")
145
146 (defvoo nntp-nov-is-evil nil 116 (defvoo nntp-nov-is-evil nil
147 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.") 117 "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
148 118
149 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW") 119 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
150 "*List of strings that are used as commands to fetch NOV lines from a server. 120 "*List of strings that are used as commands to fetch NOV lines from a server.
151 The strings are tried in turn until a positive response is gotten. If 121 The strings are tried in turn until a positive response is gotten. If
152 none of the commands are successful, nntp will just grab headers one 122 none of the commands are successful, nntp will just grab headers one
153 by one.") 123 by one.")
154 124
155 (defvoo nntp-nov-gap 20 125 (defvoo nntp-nov-gap 20
156 "*Maximum allowed gap between two articles. 126 "*Maximum allowed gap between two articles.
158 variable, split the XOVER request into two requests.") 128 variable, split the XOVER request into two requests.")
159 129
160 (defvoo nntp-connection-timeout nil 130 (defvoo nntp-connection-timeout nil
161 "*Number of seconds to wait before an nntp connection times out. 131 "*Number of seconds to wait before an nntp connection times out.
162 If this variable is nil, which is the default, no timers are set.") 132 If this variable is nil, which is the default, no timers are set.")
163
164 (defvoo nntp-command-timeout nil
165 "*Number of seconds to wait for a response when sending a command.
166 If this variable is nil, which is the default, no timers are set.")
167
168 (defvoo nntp-retry-on-break nil
169 "*If non-nil, re-send the command when the user types `C-g'.")
170
171 (defvoo nntp-news-default-headers nil
172 "*If non-nil, override `mail-default-headers' when posting news.")
173 133
174 (defvoo nntp-prepare-server-hook nil 134 (defvoo nntp-prepare-server-hook nil
175 "*Hook run before a server is opened. 135 "*Hook run before a server is opened.
176 If can be used to set up a server remotely, for instance. Say you 136 If can be used to set up a server remotely, for instance. Say you
177 have an account at the machine \"other.machine\". This machine has 137 have an account at the machine \"other.machine\". This machine has
178 access to an NNTP server that you can't access locally. You could 138 access to an NNTP server that you can't access locally. You could
179 then use this hook to rsh to the remote machine and start a proxy NNTP 139 then use this hook to rsh to the remote machine and start a proxy NNTP
180 server there that you can connect to.") 140 server there that you can connect to. See also `nntp-open-connection-function'")
181
182 (defvoo nntp-async-number 5
183 "*How many articles should be prefetched when in asynchronous mode.")
184 141
185 (defvoo nntp-warn-about-losing-connection t 142 (defvoo nntp-warn-about-losing-connection t
186 "*If non-nil, beep when a server closes connection.") 143 "*If non-nil, beep when a server closes connection.")
187 144
188 145
189 146
190 (defconst nntp-version "nntp 4.0" 147 ;;; Internal variables.
191 "Version numbers of this version of NNTP.") 148
192 149 (defvar nntp-have-messaged nil)
193 (defvar nntp-server-buffer nil 150
194 "Buffer associated with the NNTP server process.") 151 (defvar nntp-process-wait-for nil)
195 152 (defvar nntp-process-to-buffer nil)
196 (defvoo nntp-server-process nil 153 (defvar nntp-process-callback nil)
197 "The NNTP server process. 154 (defvar nntp-process-decode nil)
198 You'd better not use this variable in NNTP front-end program, but 155 (defvar nntp-process-start-point nil)
199 instead use `nntp-server-buffer'.") 156 (defvar nntp-inside-change-function nil)
200 157
201 (defvoo nntp-status-string nil 158 (defvar nntp-connection-list nil)
202 "Save the server response message.") 159
203 160 (defvoo nntp-server-type nil)
204 (defvar nntp-opened-connections nil 161 (defvoo nntp-connection-alist nil)
205 "All (possibly) opened connections.") 162 (defvoo nntp-status-string "")
163 (defconst nntp-version "nntp 5.0")
164 (defvoo nntp-inhibit-erase nil)
165 (defvoo nntp-inhibit-output nil)
206 166
207 (defvoo nntp-server-xover 'try) 167 (defvoo nntp-server-xover 'try)
208 (defvoo nntp-server-list-active-group 'try) 168 (defvoo nntp-server-list-active-group 'try)
209 (defvoo nntp-current-group "") 169
210 (defvoo nntp-server-type nil) 170 (eval-and-compile
211 171 (autoload 'nnmail-read-passwd "nnmail"))
212 (defvoo nntp-async-process nil)
213 (defvoo nntp-async-buffer nil)
214 (defvoo nntp-async-articles nil)
215 (defvoo nntp-async-fetched nil)
216 (defvoo nntp-async-group-alist nil)
217 172
218 173
174
219 ;;; Interface functions. 175 ;;; Interface functions.
220 176
221 (nnoo-define-basics nntp) 177 (nnoo-define-basics nntp)
222 178
223 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old) 179 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
224 "Retrieve the headers of ARTICLES." 180 "Retrieve the headers of ARTICLES."
225 (nntp-possibly-change-server group server) 181 (nntp-possibly-change-group group server)
226 (save-excursion 182 (save-excursion
227 (set-buffer nntp-server-buffer) 183 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
228 (erase-buffer) 184 (erase-buffer)
229 (if (and (not gnus-nov-is-evil) 185 (if (and (not gnus-nov-is-evil)
230 (not nntp-nov-is-evil) 186 (not nntp-nov-is-evil)
231 (nntp-retrieve-headers-with-xover articles fetch-old)) 187 (nntp-retrieve-headers-with-xover articles fetch-old))
232 ;; We successfully retrieved the headers via XOVER. 188 ;; We successfully retrieved the headers via XOVER.
233 'nov 189 'nov
234 ;; XOVER didn't work, so we do it the hard, slow and inefficient 190 ;; XOVER didn't work, so we do it the hard, slow and inefficient
235 ;; way. 191 ;; way.
236 (let ((number (length articles)) 192 (let ((number (length articles))
237 (count 0) 193 (count 0)
238 (received 0) 194 (received 0)
239 (message-log-max nil) 195 (last-point (point-min))
240 (last-point (point-min))) 196 (buf (nntp-find-connection-buffer nntp-server-buffer))
197 (nntp-inhibit-erase t))
241 ;; Send HEAD command. 198 ;; Send HEAD command.
242 (while articles 199 (while articles
243 (nntp-send-strings-to-server 200 (nntp-send-command
244 "HEAD" (if (numberp (car articles)) 201 nil
202 "HEAD" (if (numberp (car articles))
245 (int-to-string (car articles)) 203 (int-to-string (car articles))
246 ;; `articles' is either a list of article numbers 204 ;; `articles' is either a list of article numbers
247 ;; or a list of article IDs. 205 ;; or a list of article IDs.
248 (car articles))) 206 (car articles)))
249 (setq articles (cdr articles) 207 (setq articles (cdr articles)
252 ;; order to avoid deadlocks. 210 ;; order to avoid deadlocks.
253 (when (or (null articles) ;All requests have been sent. 211 (when (or (null articles) ;All requests have been sent.
254 (zerop (% count nntp-maximum-request))) 212 (zerop (% count nntp-maximum-request)))
255 (nntp-accept-response) 213 (nntp-accept-response)
256 (while (progn 214 (while (progn
257 (goto-char last-point) 215 (progn
216 (set-buffer buf)
217 (goto-char last-point))
258 ;; Count replies. 218 ;; Count replies.
259 (while (re-search-forward "^[0-9]" nil t) 219 (while (re-search-forward "^[0-9]" nil t)
260 (setq received (1+ received))) 220 (incf received))
261 (setq last-point (point)) 221 (setq last-point (point))
262 (< received count)) 222 (< received count))
263 ;; If number of headers is greater than 100, give 223 ;; If number of headers is greater than 100, give
264 ;; informative messages. 224 ;; informative messages.
265 (and (numberp nntp-large-newsgroup) 225 (and (numberp nntp-large-newsgroup)
266 (> number nntp-large-newsgroup) 226 (> number nntp-large-newsgroup)
267 (zerop (% received 20)) 227 (zerop (% received 20))
268 (nnheader-message 7 "NNTP: Receiving headers... %d%%" 228 (nnheader-message 6 "NNTP: Receiving headers... %d%%"
269 (/ (* received 100) number))) 229 (/ (* received 100) number)))
270 (nntp-accept-response)))) 230 (nntp-accept-response))))
271 ;; Wait for text of last command. 231 ;; Wait for text of last command.
272 (goto-char (point-max)) 232 (goto-char (point-max))
273 (re-search-backward "^[0-9]" nil t) 233 (re-search-backward "^[0-9]" nil t)
276 (goto-char (- (point-max) 3)) 236 (goto-char (- (point-max) 3))
277 (not (looking-at "^\\.\r?\n"))) 237 (not (looking-at "^\\.\r?\n")))
278 (nntp-accept-response))) 238 (nntp-accept-response)))
279 (and (numberp nntp-large-newsgroup) 239 (and (numberp nntp-large-newsgroup)
280 (> number nntp-large-newsgroup) 240 (> number nntp-large-newsgroup)
281 (nnheader-message 7 "NNTP: Receiving headers...done")) 241 (nnheader-message 6 "NNTP: Receiving headers...done"))
282 242
283 ;; Now all of replies are received. Fold continuation lines. 243 ;; Now all of replies are received. Fold continuation lines.
284 (nnheader-fold-continuation-lines) 244 (nnheader-fold-continuation-lines)
285 ;; Remove all "\r"'s. 245 ;; Remove all "\r"'s.
286 (goto-char (point-min)) 246 (nnheader-strip-cr)
287 (while (search-forward "\r" nil t) 247 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
288 (replace-match "" t t))
289 'headers)))) 248 'headers))))
290
291 249
292 (deffoo nntp-retrieve-groups (groups &optional server) 250 (deffoo nntp-retrieve-groups (groups &optional server)
293 "Retrieve group info on GROUPS." 251 "Retrieve group info on GROUPS."
294 (nntp-possibly-change-server nil server) 252 (nntp-possibly-change-group nil server)
295 (save-excursion 253 (save-excursion
296 (set-buffer nntp-server-buffer) 254 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
297 ;; The first time this is run, this variable is `try'. So we 255 ;; The first time this is run, this variable is `try'. So we
298 ;; try. 256 ;; try.
299 (when (eq nntp-server-list-active-group 'try) 257 (when (eq nntp-server-list-active-group 'try)
300 (nntp-try-list-active (car groups))) 258 (nntp-try-list-active (car groups)))
301 (erase-buffer) 259 (erase-buffer)
302 (let ((count 0) 260 (let ((count 0)
303 (received 0) 261 (received 0)
304 (last-point (point-min)) 262 (last-point (point-min))
263 (nntp-inhibit-erase t)
305 (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP"))) 264 (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
306 (while groups 265 (while groups
307 ;; Send the command to the server. 266 ;; Send the command to the server.
308 (nntp-send-strings-to-server command (car groups)) 267 (nntp-send-command nil command (pop groups))
309 (setq groups (cdr groups)) 268 (incf count)
310 (setq count (1+ count))
311 ;; Every 400 requests we have to read the stream in 269 ;; Every 400 requests we have to read the stream in
312 ;; order to avoid deadlocks. 270 ;; order to avoid deadlocks.
313 (when (or (null groups) ;All requests have been sent. 271 (when (or (null groups) ;All requests have been sent.
314 (zerop (% count nntp-maximum-request))) 272 (zerop (% count nntp-maximum-request)))
315 (nntp-accept-response) 273 (nntp-accept-response)
316 (while (progn 274 (while (progn
317 (goto-char last-point) 275 (goto-char last-point)
318 ;; Count replies. 276 ;; Count replies.
319 (while (re-search-forward "^[0-9]" nil t) 277 (while (re-search-forward "^[0-9]" nil t)
320 (setq received (1+ received))) 278 (incf received))
321 (setq last-point (point)) 279 (setq last-point (point))
322 (< received count)) 280 (< received count))
323 (nntp-accept-response)))) 281 (nntp-accept-response))))
324 282
325 ;; Wait for the reply from the final command. 283 ;; Wait for the reply from the final command.
326 (when nntp-server-list-active-group 284 (goto-char (point-max))
327 (goto-char (point-max)) 285 (re-search-backward "^[0-9]" nil t)
328 (re-search-backward "^[0-9]" nil t) 286 (when (looking-at "^[23]")
329 (when (looking-at "^[23]") 287 (while (progn
330 (while (progn 288 (goto-char (point-max))
331 (goto-char (- (point-max) 3)) 289 (if (not nntp-server-list-active-group)
332 (not (looking-at "^\\.\r?\n"))) 290 (not (re-search-backward "\r?\n" (- (point) 3) t))
333 (nntp-accept-response)))) 291 (not (re-search-backward "^\\.\r?\n" (- (point) 4) t))))
334 292 (nntp-accept-response)))
335 ;; Now all replies are received. We remove CRs. 293
294 ;; Now all replies are received. We remove CRs.
336 (goto-char (point-min)) 295 (goto-char (point-min))
337 (while (search-forward "\r" nil t) 296 (while (search-forward "\r" nil t)
338 (replace-match "" t t)) 297 (replace-match "" t t))
339 298
340 (if (not nntp-server-list-active-group) 299 (if (not nntp-server-list-active-group)
341 'group 300 (progn
301 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
302 'group)
342 ;; We have read active entries, so we just delete the 303 ;; We have read active entries, so we just delete the
343 ;; superfluos gunk. 304 ;; superfluous gunk.
344 (goto-char (point-min)) 305 (goto-char (point-min))
345 (while (re-search-forward "^[.2-5]" nil t) 306 (while (re-search-forward "^[.2-5]" nil t)
346 (delete-region (match-beginning 0) 307 (delete-region (match-beginning 0)
347 (progn (forward-line 1) (point)))) 308 (progn (forward-line 1) (point))))
309 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
348 'active)))) 310 'active))))
349 311
350 (deffoo nntp-open-server (server &optional defs connectionless) 312 (deffoo nntp-retrieve-articles (articles &optional group server)
351 "Open the virtual server SERVER. 313 (nntp-possibly-change-group group server)
352 If CONNECTIONLESS is non-nil, don't attempt to connect to any physical 314 (save-excursion
353 servers." 315 (let ((number (length articles))
354 (nnheader-init-server-buffer) 316 (count 0)
355 ;; Called with just a port number as the defs. 317 (received 0)
356 (when (or (stringp (car defs)) 318 (last-point (point-min))
357 (numberp (car defs))) 319 (buf (nntp-find-connection-buffer nntp-server-buffer))
358 (setq defs `((nntp-port-number ,(car defs))))) 320 (nntp-inhibit-erase t)
359 (unless (assq 'nntp-address defs) 321 (map (apply 'vector articles))
360 (setq defs (append defs `((nntp-address ,server))))) 322 (point 1)
361 (nnoo-change-server 'nntp server defs) 323 article alist)
362 (if (nntp-server-opened server) 324 (set-buffer buf)
363 t 325 (erase-buffer)
364 (or (nntp-server-opened server) 326 ;; Send HEAD command.
365 connectionless 327 (while (setq article (pop articles))
366 (prog2 328 (nntp-send-command
367 (run-hooks 'nntp-prepare-server-hook) 329 nil
368 (nntp-open-server-semi-internal nntp-address nntp-port-number) 330 "ARTICLE" (if (numberp article)
369 (nnheader-insert ""))))) 331 (int-to-string article)
370 332 ;; `articles' is either a list of article numbers
371 (deffoo nntp-close-server (&optional server) 333 ;; or a list of article IDs.
372 "Close connection to SERVER." 334 article))
373 (nntp-possibly-change-server nil server t) 335 (incf count)
374 (unwind-protect 336 ;; Every 400 requests we have to read the stream in
375 (progn 337 ;; order to avoid deadlocks.
376 ;; Un-set default sentinel function before closing connection. 338 (when (or (null articles) ;All requests have been sent.
377 (and nntp-server-process 339 (zerop (% count nntp-maximum-request)))
378 (eq 'nntp-default-sentinel 340 (nntp-accept-response)
379 (process-sentinel nntp-server-process)) 341 (while (progn
380 (set-process-sentinel nntp-server-process nil)) 342 (progn
381 ;; We cannot send QUIT command unless the process is running. 343 (set-buffer buf)
382 (when (nntp-server-opened server) 344 (goto-char last-point))
383 (nntp-send-command nil "QUIT") 345 ;; Count replies.
384 ;; Give the QUIT time to arrive. 346 (while (nntp-next-result-arrived-p)
385 (sleep-for 1))) 347 (aset map received (cons (aref map received) (point)))
386 (nntp-close-server-internal server))) 348 (incf received))
387 349 (setq last-point (point))
388 (deffoo nntp-request-close () 350 (< received count))
389 "Close all server connections." 351 ;; If number of headers is greater than 100, give
390 (let (proc) 352 ;; informative messages.
391 (while nntp-opened-connections 353 (and (numberp nntp-large-newsgroup)
392 (when (setq proc (pop nntp-opened-connections)) 354 (> number nntp-large-newsgroup)
393 ;; Un-set default sentinel function before closing connection. 355 (zerop (% received 20))
394 (when (eq 'nntp-default-sentinel (process-sentinel proc)) 356 (nnheader-message 6 "NNTP: Receiving articles... %d%%"
395 (set-process-sentinel proc nil)) 357 (/ (* received 100) number)))
396 (condition-case () 358 (nntp-accept-response))))
397 (process-send-string proc (concat "QUIT" nntp-end-of-line)) 359 (and (numberp nntp-large-newsgroup)
398 (error nil)) 360 (> number nntp-large-newsgroup)
399 ;; Give the QUIT time to reach the server before we close 361 (nnheader-message 6 "NNTP: Receiving headers...done"))
400 ;; down the process. 362
401 (sleep-for 1) 363 ;; Now we have all the responses. We go through the results,
402 (delete-process proc))) 364 ;; washes it and copies it over to the server buffer.
403 (and nntp-async-buffer 365 (set-buffer nntp-server-buffer)
404 (buffer-name nntp-async-buffer) 366 (erase-buffer)
405 (kill-buffer nntp-async-buffer)) 367 (mapcar
406 (let ((alist (cddr (assq 'nntp nnoo-state-alist))) 368 (lambda (entry)
407 entry) 369 (narrow-to-region
408 (while (setq entry (pop alist)) 370 (setq point (goto-char (point-max)))
409 (and (setq proc (cdr (assq 'nntp-async-buffer entry))) 371 (progn
410 (buffer-name proc) 372 (insert-buffer-substring buf last-point (cdr entry))
411 (kill-buffer proc)))) 373 (point-max)))
412 (nnoo-close-server 'nntp) 374 (nntp-decode-text)
413 (setq nntp-async-group-alist nil 375 (widen)
414 nntp-async-articles nil))) 376 (cons (car entry) point))
377 map))))
378
379 (defun nntp-next-result-arrived-p ()
380 (let ((point (point)))
381 (cond
382 ((looking-at "2")
383 (if (re-search-forward "\n.\r?\n" nil t)
384 t
385 (goto-char point)
386 nil))
387 ((looking-at "[34]")
388 (forward-line 1)
389 t)
390 (t
391 nil))))
392
393 (defun nntp-try-list-active (group)
394 (nntp-list-active-group group)
395 (save-excursion
396 (set-buffer nntp-server-buffer)
397 (goto-char (point-min))
398 (cond ((or (eobp)
399 (looking-at "5[0-9]+"))
400 (setq nntp-server-list-active-group nil))
401 (t
402 (setq nntp-server-list-active-group t)))))
403
404 (deffoo nntp-list-active-group (group &optional server)
405 "Return the active info on GROUP (which can be a regexp."
406 (nntp-possibly-change-group nil server)
407 (nntp-send-command "^.*\r?\n" "LIST ACTIVE" group))
408
409 (deffoo nntp-request-article (article &optional group server buffer command)
410 (nntp-possibly-change-group group server)
411 (when (nntp-send-command-and-decode
412 "\r?\n\\.\r?\n" "ARTICLE"
413 (if (numberp article) (int-to-string article) article))
414 (when (and buffer
415 (not (equal buffer nntp-server-buffer)))
416 (save-excursion
417 (set-buffer nntp-server-buffer)
418 (copy-to-buffer buffer (point-min) (point-max))
419 (nntp-find-group-and-number)))
420 (nntp-find-group-and-number)))
421
422 (deffoo nntp-request-head (article &optional group server)
423 (nntp-possibly-change-group group server)
424 (when (nntp-send-command-and-decode
425 "\r?\n\\.\r?\n" "HEAD"
426 (if (numberp article) (int-to-string article) article))
427 (nntp-find-group-and-number)))
428
429 (deffoo nntp-request-body (article &optional group server)
430 (nntp-possibly-change-group group server)
431 (nntp-send-command-and-decode
432 "\r?\n\\.\r?\n" "BODY"
433 (if (numberp article) (int-to-string article) article)))
434
435 (deffoo nntp-request-group (group &optional server dont-check)
436 (nntp-possibly-change-group nil server)
437 (when (nntp-send-command "^2.*\n" "GROUP" group)
438 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
439 (setcar (cddr entry) group))))
440
441 (deffoo nntp-close-group (group &optional server)
442 t)
415 443
416 (deffoo nntp-server-opened (&optional server) 444 (deffoo nntp-server-opened (&optional server)
417 "Say whether a connection to SERVER has been opened." 445 "Say whether a connection to SERVER has been opened."
418 (and (nnoo-current-server-p 'nntp server) 446 (and (nnoo-current-server-p 'nntp server)
419 nntp-server-buffer 447 nntp-server-buffer
420 (buffer-name nntp-server-buffer) 448 (gnus-buffer-live-p nntp-server-buffer)
421 nntp-server-process 449 (nntp-find-connection nntp-server-buffer)))
422 (memq (process-status nntp-server-process) '(open run)))) 450
423 451 (deffoo nntp-open-server (server &optional defs connectionless)
424 (deffoo nntp-status-message (&optional server) 452 (nnheader-init-server-buffer)
425 "Return server status as a string." 453 (if (nntp-server-opened server)
426 (if (and nntp-status-string 454 t
427 ;; NNN MESSAGE 455 (when (or (stringp (car defs))
428 (string-match "[0-9][0-9][0-9][ \t]+\\([^\r]*\\).*$" 456 (numberp (car defs)))
429 nntp-status-string)) 457 (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
430 (substring nntp-status-string (match-beginning 1) (match-end 1)) 458 (unless (assq 'nntp-address defs)
431 ;; Empty message if nothing. 459 (setq defs (append defs (list (list 'nntp-address server)))))
432 (or nntp-status-string ""))) 460 (nnoo-change-server 'nntp server defs)
433 461 (unless connectionless
434 (deffoo nntp-request-article (id &optional group server buffer) 462 (or (nntp-find-connection nntp-server-buffer)
435 "Request article ID (Message-ID or number)." 463 (nntp-open-connection nntp-server-buffer)))))
436 (nntp-possibly-change-server group server) 464
437 465 (deffoo nntp-close-server (&optional server)
438 (let (found) 466 (nntp-possibly-change-group nil server t)
439 467 (let (process)
440 ;; First we see whether we can get the article from the async buffer. 468 (while (setq process (car (pop nntp-connection-alist)))
441 (when (and (numberp id) 469 (when (memq (process-status process) '(open run))
442 nntp-async-articles 470 (set-process-sentinel process nil)
443 (memq id nntp-async-fetched)) 471 (nntp-send-string process "QUIT"))
444 (save-excursion 472 (when (buffer-name (process-buffer process))
445 (set-buffer nntp-async-buffer) 473 (kill-buffer (process-buffer process))))
446 (let ((opoint (point)) 474 (nnoo-close-server 'nntp)))
447 (art (if (numberp id) (int-to-string id) id)) 475
448 beg end) 476 (deffoo nntp-request-close ()
449 (when (and (or (re-search-forward (concat "^2.. +" art) nil t) 477 (let (process)
450 (progn 478 (while (setq process (pop nntp-connection-list))
451 (goto-char (point-min)) 479 (when (memq (process-status process) '(open run))
452 (re-search-forward (concat "^2.. +" art) opoint t))) 480 (set-process-sentinel process nil)
453 (progn 481 (ignore-errors
454 (beginning-of-line) 482 (nntp-send-string process "QUIT")))
455 (setq beg (point) 483 (when (buffer-name (process-buffer process))
456 end (re-search-forward "^\\.\r?\n" nil t)))) 484 (kill-buffer (process-buffer process))))))
457 (setq found t) 485
458 (save-excursion 486 (deffoo nntp-request-list (&optional server)
459 (set-buffer (or buffer nntp-server-buffer)) 487 (nntp-possibly-change-group nil server)
460 (erase-buffer) 488 (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST"))
461 (insert-buffer-substring nntp-async-buffer beg end) 489
462 (let ((nntp-server-buffer (current-buffer))) 490 (deffoo nntp-request-list-newsgroups (&optional server)
463 (nntp-decode-text))) 491 (nntp-possibly-change-group nil server)
464 (delete-region beg end) 492 (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS"))
465 (when nntp-async-articles 493
466 (nntp-async-fetch-articles id)))))) 494 (deffoo nntp-request-newgroups (date &optional server)
467 495 (nntp-possibly-change-group nil server)
468 (if found 496 (save-excursion
469 id 497 (set-buffer nntp-server-buffer)
470 ;; The article was not in the async buffer, so we fetch it now. 498 (let* ((date (timezone-parse-date date))
471 (unwind-protect 499 (time-string
472 (progn 500 (format "%s%02d%02d %s%s%s"
473 (if buffer (set-process-buffer nntp-server-process buffer)) 501 (substring (aref date 0) 2) (string-to-int (aref date 1))
474 (let ((nntp-server-buffer (or buffer nntp-server-buffer)) 502 (string-to-int (aref date 2)) (substring (aref date 3) 0 2)
475 (art (or (and (numberp id) (int-to-string id)) id))) 503 (substring
476 (prog1 504 (aref date 3) 3 5) (substring (aref date 3) 6 8))))
477 (and (nntp-send-command 505 (prog1
478 ;; A bit odd regexp to ensure working over rlogin. 506 (nntp-send-command "^\\.\r?\n" "NEWGROUPS" time-string)
479 "^\\.\r?\n" "ARTICLE" art) 507 (nntp-decode-text)))))
480 (if (numberp id) 508
481 (cons nntp-current-group id) 509 (deffoo nntp-request-post (&optional server)
482 ;; We find out what the article number was. 510 (nntp-possibly-change-group nil server)
483 (nntp-find-group-and-number))) 511 (when (nntp-send-command "^[23].*\r?\n" "POST")
484 (nntp-decode-text) 512 (nntp-send-buffer "^[23].*\n")))
485 (and nntp-async-articles (nntp-async-fetch-articles id))))) 513
486 (when buffer 514 (deffoo nntp-request-type (group article)
487 (set-process-buffer nntp-server-process nntp-server-buffer))))))
488
489 (deffoo nntp-request-body (id &optional group server)
490 "Request body of article ID (Message-ID or number)."
491 (nntp-possibly-change-server group server)
492 (prog1
493 ;; If NEmacs, end of message may look like: "\256\215" (".^M")
494 (nntp-send-command
495 "^\\.\r?\n" "BODY" (or (and (numberp id) (int-to-string id)) id))
496 (nntp-decode-text)))
497
498 (deffoo nntp-request-head (id &optional group server)
499 "Request head of article ID (Message-ID or number)."
500 (nntp-possibly-change-server group server)
501 (prog1
502 (when (nntp-send-command
503 "^\\.\r?\n" "HEAD" (if (numberp id) (int-to-string id) id))
504 (if (numberp id) id
505 ;; We find out what the article number was.
506 (nntp-find-group-and-number)))
507 (nntp-decode-text)
508 (save-excursion
509 (set-buffer nntp-server-buffer)
510 (nnheader-fold-continuation-lines))))
511
512 (deffoo nntp-request-stat (id &optional group server)
513 "Request STAT of article ID (Message-ID or number)."
514 (nntp-possibly-change-server group server)
515 (nntp-send-command
516 "^[23].*\r?\n" "STAT" (or (and (numberp id) (int-to-string id)) id)))
517
518 (deffoo nntp-request-type (group &optional article)
519 'news) 515 'news)
520 516
521 (deffoo nntp-request-group (group &optional server dont-check) 517 (deffoo nntp-asynchronous-p ()
522 "Select GROUP."
523 (nntp-possibly-change-server nil server)
524 (setq nntp-current-group
525 (when (nntp-send-command "^2.*\r?\n" "GROUP" group)
526 group)))
527
528 (deffoo nntp-request-asynchronous (group &optional server articles)
529 "Enable pre-fetch in GROUP."
530 (when nntp-async-articles
531 (nntp-async-request-group group))
532 (when nntp-async-number
533 (if (not (or (nntp-async-server-opened)
534 (nntp-async-open-server)))
535 ;; Couldn't open the second connection
536 (progn
537 (message "Can't open second connection to %s" nntp-address)
538 (ding)
539 (setq nntp-async-articles nil)
540 (sit-for 2))
541 ;; We opened the second connection (or it was opened already).
542 (setq nntp-async-articles articles)
543 (setq nntp-async-fetched nil)
544 ;; Clear any old data.
545 (save-excursion
546 (set-buffer nntp-async-buffer)
547 (erase-buffer))
548 ;; Select the correct current group on this server.
549 (nntp-async-send-strings "GROUP" group)
550 t)))
551
552 (deffoo nntp-list-active-group (group &optional server)
553 "Return the active info on GROUP (which can be a regexp."
554 (nntp-possibly-change-server group server)
555 (nntp-send-command "^.*\r?\n" "LIST ACTIVE" group))
556
557 (deffoo nntp-request-group-description (group &optional server)
558 "Get the description of GROUP."
559 (nntp-possibly-change-server nil server)
560 (prog1
561 (nntp-send-command "^.*\r?\n" "XGTITLE" group)
562 (nntp-decode-text)))
563
564 (deffoo nntp-close-group (group &optional server)
565 "Close GROUP."
566 (setq nntp-current-group nil)
567 t) 518 t)
568 519
569 (deffoo nntp-request-list (&optional server) 520 ;;; Hooky functions.
570 "List all active groups."
571 (nntp-possibly-change-server nil server)
572 (prog1
573 (nntp-send-command "^\\.\r?\n" "LIST")
574 (nntp-decode-text)))
575
576 (deffoo nntp-request-list-newsgroups (&optional server)
577 "Get descriptions on all groups on SERVER."
578 (nntp-possibly-change-server nil server)
579 (prog1
580 (nntp-send-command "^\\.\r?\n" "LIST NEWSGROUPS")
581 (nntp-decode-text)))
582
583 (deffoo nntp-request-newgroups (date &optional server)
584 "List groups that have arrived since DATE."
585 (nntp-possibly-change-server nil server)
586 (let* ((date (timezone-parse-date date))
587 (time-string
588 (format "%s%02d%02d %s%s%s"
589 (substring (aref date 0) 2) (string-to-int (aref date 1))
590 (string-to-int (aref date 2)) (substring (aref date 3) 0 2)
591 (substring
592 (aref date 3) 3 5) (substring (aref date 3) 6 8))))
593 (prog1
594 (nntp-send-command "^\\.\r?\n" "NEWGROUPS" time-string)
595 (nntp-decode-text))))
596
597 (deffoo nntp-request-list-distributions (&optional server)
598 "List distributions."
599 (nntp-possibly-change-server nil server)
600 (prog1
601 (nntp-send-command "^\\.\r?\n" "LIST DISTRIBUTIONS")
602 (nntp-decode-text)))
603
604 (deffoo nntp-request-last (&optional group server)
605 "Decrease the current article pointer."
606 (nntp-possibly-change-server group server)
607 (nntp-send-command "^[23].*\r?\n" "LAST"))
608
609 (deffoo nntp-request-next (&optional group server)
610 "Advance the current article pointer."
611 (nntp-possibly-change-server group server)
612 (nntp-send-command "^[23].*\r?\n" "NEXT"))
613
614 (deffoo nntp-request-post (&optional server)
615 "Post the current buffer."
616 (nntp-possibly-change-server nil server)
617 (when (nntp-send-command "^[23].*\r?\n" "POST")
618 (nnheader-insert "")
619 (nntp-encode-text)
620 (nntp-send-region-to-server (point-min) (point-max))
621 ;; 1.2a NNTP's post command is buggy. "^M" (\r) is not
622 ;; appended to end of the status message.
623 (nntp-wait-for-response "^[23].*\n")))
624
625 ;;; Internal functions.
626 521
627 (defun nntp-send-mode-reader () 522 (defun nntp-send-mode-reader ()
628 "Send the MODE READER command to the nntp server. 523 "Send the MODE READER command to the nntp server.
629 This function is supposed to be called from `nntp-server-opened-hook'. 524 This function is supposed to be called from `nntp-server-opened-hook'.
630 It will make innd servers spawn an nnrpd process to allow actual article 525 It will make innd servers spawn an nnrpd process to allow actual article
633 528
634 (defun nntp-send-nosy-authinfo () 529 (defun nntp-send-nosy-authinfo ()
635 "Send the AUTHINFO to the nntp server. 530 "Send the AUTHINFO to the nntp server.
636 This function is supposed to be called from `nntp-server-opened-hook'. 531 This function is supposed to be called from `nntp-server-opened-hook'.
637 It will prompt for a password." 532 It will prompt for a password."
638 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" 533 (nntp-send-command
639 (read-string "NNTP user name: ")) 534 "^.*\r?\n" "AUTHINFO USER"
640 (nntp-send-command "^.*\r?\n" "AUTHINFO PASS" 535 (read-string (format "NNTP (%s) user name: " nntp-address)))
641 (read-string "NNTP password: "))) 536 (nntp-send-command
537 "^.*\r?\n" "AUTHINFO PASS"
538 (nnmail-read-passwd "NNTP (%s) password: " nntp-address)))
642 539
643 (defun nntp-send-authinfo () 540 (defun nntp-send-authinfo ()
644 "Send the AUTHINFO to the nntp server. 541 "Send the AUTHINFO to the nntp server.
645 This function is supposed to be called from `nntp-server-opened-hook'. 542 This function is supposed to be called from `nntp-server-opened-hook'.
646 It will prompt for a password." 543 It will prompt for a password."
647 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name)) 544 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name))
648 (nntp-send-command "^.*\r?\n" "AUTHINFO PASS" 545 (nntp-send-command
649 (read-string "NNTP password: "))) 546 "^.*\r?\n" "AUTHINFO PASS"
547 (nnmail-read-passwd (format "NNTP (%s) password: " nntp-address))))
650 548
651 (defun nntp-send-authinfo-from-file () 549 (defun nntp-send-authinfo-from-file ()
652 "Send the AUTHINFO to the nntp server. 550 "Send the AUTHINFO to the nntp server.
653 This function is supposed to be called from `nntp-server-opened-hook'. 551 This function is supposed to be called from `nntp-server-opened-hook'.
654 It will prompt for a password." 552 It will prompt for a password."
655 (when (file-exists-p "~/.nntp-authinfo") 553 (when (file-exists-p "~/.nntp-authinfo")
656 (save-excursion 554 (nnheader-temp-write nil
657 (set-buffer (get-buffer-create " *authinfo*"))
658 (buffer-disable-undo (current-buffer))
659 (erase-buffer)
660 (insert-file-contents "~/.nntp-authinfo") 555 (insert-file-contents "~/.nntp-authinfo")
661 (goto-char (point-min)) 556 (goto-char (point-min))
662 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name)) 557 (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name))
663 (nntp-send-command 558 (nntp-send-command
664 "^.*\r?\n" "AUTHINFO PASS" 559 "^.*\r?\n" "AUTHINFO PASS"
665 (buffer-substring (point) (progn (end-of-line) (point)))) 560 (buffer-substring (point) (progn (end-of-line) (point)))))))
666 (kill-buffer (current-buffer))))) 561
667 562 ;;; Internal functions.
668 (defun nntp-default-sentinel (proc status) 563
669 "Default sentinel function for NNTP server process." 564 (defun nntp-send-command (wait-for &rest strings)
670 (let ((servers (cddr (assq 'nntp nnoo-state-alist))) 565 "Send STRINGS to server and wait until WAIT-FOR returns."
671 server) 566 (when (and (not nnheader-callback-function)
672 ;; Go through the alist of server names and find the name of the 567 (not nntp-inhibit-output))
673 ;; server that the process that sent the signal is connected to. 568 (save-excursion
674 ;; If you get my drift. 569 (set-buffer nntp-server-buffer)
675 (if (equal proc nntp-server-process) 570 (erase-buffer)))
676 (setq server nntp-address) 571 (nntp-retrieve-data
677 (while (and servers 572 (mapconcat 'identity strings " ")
678 (not (equal proc (cdr (assq 'nntp-server-process 573 nntp-address nntp-port-number nntp-server-buffer
679 (car servers)))))) 574 wait-for nnheader-callback-function))
680 (setq servers (cdr servers))) 575
681 (setq server (caar servers))) 576 (defun nntp-send-command-nodelete (wait-for &rest strings)
682 (when (and server 577 "Send STRINGS to server and wait until WAIT-FOR returns."
683 nntp-warn-about-losing-connection) 578 (nntp-retrieve-data
684 (nnheader-message 3 "nntp: Connection closed to server %s" server) 579 (mapconcat 'identity strings " ")
685 (setq nntp-current-group "") 580 nntp-address nntp-port-number nntp-server-buffer
686 (ding)))) 581 wait-for nnheader-callback-function))
687 582
688 (defun nntp-kill-connection (server) 583 (defun nntp-send-command-and-decode (wait-for &rest strings)
689 "Choke the connection to SERVER." 584 "Send STRINGS to server and wait until WAIT-FOR returns."
690 (let ((proc (cdr (assq 'nntp-server-process 585 (when (and (not nnheader-callback-function)
691 (assoc server (cddr 586 (not nntp-inhibit-output))
692 (assq 'nntp nnoo-state-alist))))))) 587 (save-excursion
693 (when proc 588 (set-buffer nntp-server-buffer)
694 (delete-process (process-name proc))) 589 (erase-buffer)))
695 (nntp-close-server server) 590 (nntp-retrieve-data
696 (nnheader-report 591 (mapconcat 'identity strings " ")
697 'nntp (message "Connection timed out to server %s" server)) 592 nntp-address nntp-port-number nntp-server-buffer
698 (ding) 593 wait-for nnheader-callback-function t))
699 (sit-for 1))) 594
700 595 (defun nntp-send-buffer (wait-for)
701 ;; Encoding and decoding of NNTP text. 596 "Send the current buffer to server and wait until WAIT-FOR returns."
702 597 (when (and (not nnheader-callback-function)
703 (defun nntp-decode-text () 598 (not nntp-inhibit-output))
704 "Decode text transmitted by NNTP. 599 (save-excursion
705 0. Delete status line. 600 (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
706 1. Delete `^M' at end of line. 601 (erase-buffer)))
707 2. Delete `.' at end of buffer (end of text mark). 602 (nntp-encode-text)
708 3. Delete `.' at beginning of line." 603 (process-send-region (nntp-find-connection nntp-server-buffer)
604 (point-min) (point-max))
605 (nntp-retrieve-data
606 nil nntp-address nntp-port-number nntp-server-buffer
607 wait-for nnheader-callback-function))
608
609 (defun nntp-find-connection (buffer)
610 "Find the connection delivering to BUFFER."
611 (let ((alist nntp-connection-alist)
612 (buffer (if (stringp buffer) (get-buffer buffer) buffer))
613 process entry)
614 (while (setq entry (pop alist))
615 (when (eq buffer (cadr entry))
616 (setq process (car entry)
617 alist nil)))
618 (when process
619 (if (memq (process-status process) '(open run))
620 process
621 (when (buffer-name (process-buffer process))
622 (kill-buffer (process-buffer process)))
623 (setq nntp-connection-alist (delq entry nntp-connection-alist))
624 nil))))
625
626 (defun nntp-find-connection-entry (buffer)
627 "Return the entry for the connection to BUFFER."
628 (assq (nntp-find-connection buffer) nntp-connection-alist))
629
630 (defun nntp-find-connection-buffer (buffer)
631 "Return the process connection buffer tied to BUFFER."
632 (let ((process (nntp-find-connection buffer)))
633 (when process
634 (process-buffer process))))
635
636 (defun nntp-make-process-buffer (buffer)
637 "Create a new, fresh buffer usable for nntp process connections."
709 (save-excursion 638 (save-excursion
710 (set-buffer nntp-server-buffer) 639 (set-buffer
711 ;; Insert newline at end of buffer. 640 (generate-new-buffer
712 (goto-char (point-max)) 641 (format " *server %s %s %s*"
713 (or (bolp) (insert "\n")) 642 nntp-address nntp-port-number
714 ;; Delete status line. 643 (buffer-name (get-buffer buffer)))))
715 (delete-region (goto-char (point-min)) (progn (forward-line 1) (point))) 644 (buffer-disable-undo (current-buffer))
716 ;; Delete `^M's. 645 (set (make-local-variable 'after-change-functions) nil)
717 (while (search-forward "\r" nil t) 646 (set (make-local-variable 'nntp-process-wait-for) nil)
718 (replace-match "" t t)) 647 (set (make-local-variable 'nntp-process-callback) nil)
719 ;; Delete `.' at end of the buffer (end of text mark). 648 (set (make-local-variable 'nntp-process-to-buffer) nil)
649 (set (make-local-variable 'nntp-process-start-point) nil)
650 (set (make-local-variable 'nntp-process-decode) nil)
651 (current-buffer)))
652
653 (defun nntp-open-connection (buffer)
654 "Open a connection to PORT on ADDRESS delivering output to BUFFER."
655 (run-hooks 'nntp-prepare-server-hook)
656 (let* ((pbuffer (nntp-make-process-buffer buffer))
657 (process
658 (condition-case ()
659 (funcall nntp-open-connection-function pbuffer)
660 (error nil)
661 (quit nil))))
662 (when process
663 (process-kill-without-query process)
664 (nntp-wait-for process "^.*\n" buffer nil t)
665 (if (memq (process-status process) '(open run))
666 (prog1
667 (caar (push (list process buffer nil) nntp-connection-alist))
668 (push process nntp-connection-list)
669 (save-excursion
670 (set-buffer pbuffer)
671 (nntp-read-server-type)
672 (erase-buffer)
673 (set-buffer nntp-server-buffer)
674 (let ((nnheader-callback-function nil))
675 (run-hooks 'nntp-server-opened-hook))))
676 (when (buffer-name (process-buffer process))
677 (kill-buffer (process-buffer process)))
678 nil))))
679
680 (defun nntp-open-network-stream (buffer)
681 (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
682
683 (defun nntp-read-server-type ()
684 "Find out what the name of the server we have connected to is."
685 ;; Wait for the status string to arrive.
686 (setq nntp-server-type (buffer-string))
687 (let ((alist nntp-server-action-alist)
688 (case-fold-search t)
689 entry)
690 ;; Run server-specific commands.
691 (while alist
692 (setq entry (pop alist))
693 (when (string-match (car entry) nntp-server-type)
694 (if (and (listp (cadr entry))
695 (not (eq 'lambda (caadr entry))))
696 (eval (cadr entry))
697 (funcall (cadr entry)))))))
698
699 (defun nntp-after-change-function-callback (beg end len)
700 (when nntp-process-callback
701 (save-match-data
702 (if (and (= beg (point-min))
703 (memq (char-after beg) '(?4 ?5)))
704 ;; Report back error messages.
705 (save-excursion
706 (goto-char beg)
707 (if (looking-at "480")
708 (funcall nntp-authinfo-function)
709 (nntp-snarf-error-message)
710 (funcall nntp-process-callback nil)))
711 (goto-char end)
712 (when (and (> (point) nntp-process-start-point)
713 (re-search-backward nntp-process-wait-for
714 nntp-process-start-point t))
715 (when (buffer-name (get-buffer nntp-process-to-buffer))
716 (let ((cur (current-buffer))
717 (start nntp-process-start-point))
718 (save-excursion
719 (set-buffer (get-buffer nntp-process-to-buffer))
720 (goto-char (point-max))
721 (let ((b (point)))
722 (insert-buffer-substring cur start)
723 (narrow-to-region b (point-max))
724 (nntp-decode-text)
725 (widen)))))
726 (goto-char end)
727 (let ((callback nntp-process-callback)
728 (nntp-inside-change-function t))
729 (setq nntp-process-callback nil)
730 (save-excursion
731 (funcall callback (buffer-name
732 (get-buffer nntp-process-to-buffer))))))))))
733
734 (defun nntp-retrieve-data (command address port buffer
735 &optional wait-for callback decode)
736 "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
737 (let ((process (or (nntp-find-connection buffer)
738 (nntp-open-connection buffer))))
739 (if (not process)
740 (nnheader-report 'nntp "Couldn't open connection to %s" address)
741 (unless (or nntp-inhibit-erase nnheader-callback-function)
742 (save-excursion
743 (set-buffer (process-buffer process))
744 (erase-buffer)))
745 (when command
746 (nntp-send-string process command))
747 (cond
748 ((eq callback 'ignore)
749 t)
750 ((and callback wait-for)
751 (save-excursion
752 (set-buffer (process-buffer process))
753 (unless nntp-inside-change-function
754 (erase-buffer))
755 (setq nntp-process-decode decode
756 nntp-process-to-buffer buffer
757 nntp-process-wait-for wait-for
758 nntp-process-callback callback
759 nntp-process-start-point (point-max)
760 after-change-functions
761 (list 'nntp-after-change-function-callback)))
762 t)
763 (wait-for
764 (nntp-wait-for process wait-for buffer decode))
765 (t t)))))
766
767 (defun nntp-send-string (process string)
768 "Send STRING to PROCESS."
769 (process-send-string process (concat string nntp-end-of-line)))
770
771 (defun nntp-wait-for (process wait-for buffer &optional decode discard)
772 "Wait for WAIT-FOR to arrive from PROCESS."
773 (save-excursion
774 (set-buffer (process-buffer process))
775 (goto-char (point-min))
776 (while (or (not (memq (following-char) '(?2 ?3 ?4 ?5)))
777 (looking-at "480"))
778 (when (looking-at "480")
779 (erase-buffer)
780 (funcall nntp-authinfo-function))
781 (nntp-accept-process-output process)
782 (goto-char (point-min)))
783 (prog1
784 (if (looking-at "[45]")
785 (progn
786 (nntp-snarf-error-message)
787 nil)
788 (goto-char (point-max))
789 (let ((limit (point-min)))
790 (while (not (re-search-backward wait-for limit t))
791 ;; We assume that whatever we wait for is less than 1000
792 ;; characters long.
793 (setq limit (max (- (point-max) 1000) (point-min)))
794 (nntp-accept-process-output process)
795 (goto-char (point-max))))
796 (nntp-decode-text (not decode))
797 (unless discard
798 (save-excursion
799 (set-buffer buffer)
800 (goto-char (point-max))
801 (insert-buffer-substring (process-buffer process))
802 ;; Nix out "nntp reading...." message.
803 (when nntp-have-messaged
804 (setq nntp-have-messaged nil)
805 (message ""))
806 t)))
807 (unless discard
808 (erase-buffer)))))
809
810 (defun nntp-snarf-error-message ()
811 "Save the error message in the current buffer."
812 (let ((message (buffer-string)))
813 (while (string-match "[\r\n]+" message)
814 (setq message (replace-match " " t t message)))
815 (nnheader-report 'nntp message)
816 message))
817
818 (defun nntp-accept-process-output (process)
819 "Wait for output from PROCESS and message some dots."
820 (save-excursion
821 (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
822 nntp-server-buffer))
823 (let ((len (/ (point-max) 1024)))
824 (unless (< len 10)
825 (setq nntp-have-messaged t)
826 (nnheader-message 7 "nntp read: %dk" len)))
827 (accept-process-output process 1)))
828
829 (defun nntp-accept-response ()
830 "Wait for output from the process that outputs to BUFFER."
831 (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
832
833 (defun nntp-possibly-change-group (group server &optional connectionless)
834 (let ((nnheader-callback-function nil))
835 (when server
836 (or (nntp-server-opened server)
837 (nntp-open-server server nil connectionless)))
838
839 (unless connectionless
840 (or (nntp-find-connection nntp-server-buffer)
841 (nntp-open-connection nntp-server-buffer))))
842
843 (when group
844 (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
845 (when (not (equal group (caddr entry)))
846 (save-excursion
847 (set-buffer (process-buffer (car entry)))
848 (erase-buffer)
849 (nntp-send-string (car entry) (concat "GROUP " group))
850 (nntp-wait-for-string "^2.*\n")
851 (setcar (cddr entry) group)
852 (erase-buffer))))))
853
854 (defun nntp-decode-text (&optional cr-only)
855 "Decode the text in the current buffer."
856 (goto-char (point-min))
857 (while (search-forward "\r" nil t)
858 (delete-char -1))
859 (unless cr-only
860 ;; Remove trailing ".\n" end-of-transfer marker.
720 (goto-char (point-max)) 861 (goto-char (point-max))
721 (forward-line -1) 862 (forward-line -1)
722 (when (looking-at "^\\.\n") 863 (when (looking-at ".\n")
723 (delete-region (point) (progn (forward-line 1) (point)))) 864 (delete-char 2))
724 ;; Replace `..' at beginning of line with `.'. 865 ;; Delete status line.
725 (goto-char (point-min)) 866 (goto-char (point-min))
726 ;; (replace-regexp "^\\.\\." ".") 867 (delete-region (point) (progn (forward-line 1) (point)))
868 ;; Remove "." -> ".." encoding.
727 (while (search-forward "\n.." nil t) 869 (while (search-forward "\n.." nil t)
728 (delete-char -1)))) 870 (delete-char -1))))
729 871
730 (defun nntp-encode-text () 872 (defun nntp-encode-text ()
731 "Encode text in current buffer for NNTP transmission. 873 "Encode the text in the current buffer."
732 1. Insert `.' at beginning of line.
733 2. Insert `.' at end of buffer (end of text mark)."
734 (save-excursion 874 (save-excursion
735 ;; Replace `.' at beginning of line with `..'. 875 ;; Replace "." at beginning of line with "..".
736 (goto-char (point-min)) 876 (goto-char (point-min))
737 (while (re-search-forward "^\\." nil t) 877 (while (re-search-forward "^\\." nil t)
738 (insert ".")) 878 (insert "."))
739 (goto-char (point-max)) 879 (goto-char (point-max))
740 ;; Insert newline at end of buffer. 880 ;; Insert newline at the end of the buffer.
741 (or (bolp) (insert "\n")) 881 (unless (bolp)
742 ;(goto-char (point-min)) 882 (insert "\n"))
743 ;(while (not (eobp))
744 ; (end-of-line)
745 ; (insert "\r")
746 ; (forward-line 1))
747 ;; Insert `.' at end of buffer (end of text mark). 883 ;; Insert `.' at end of buffer (end of text mark).
748 (goto-char (point-max)) 884 (goto-char (point-max))
749 (insert "." nntp-end-of-line))) 885 (insert "." nntp-end-of-line)))
750 886
751 887 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
752 888 (set-buffer nntp-server-buffer)
753 ;;; 889 (erase-buffer)
754 ;;; Synchronous Communication with NNTP servers. 890 (cond
755 ;;; 891
756 892 ;; This server does not talk NOV.
757 (defvar nntp-retry-command) 893 ((not nntp-server-xover)
758 894 nil)
759 (defun nntp-send-command (response cmd &rest args) 895
760 "Wait for server RESPONSE after sending CMD and optional ARGS to server." 896 ;; We don't care about gaps.
761 (let ((timer 897 ((or (not nntp-nov-gap)
762 (and nntp-command-timeout 898 fetch-old)
763 (nnheader-run-at-time 899 (nntp-send-xover-command
764 nntp-command-timeout nil 'nntp-kill-command 900 (if fetch-old
765 (nnoo-current-server 'nntp)))) 901 (if (numberp fetch-old)
766 (nntp-retry-command t) 902 (max 1 (- (car articles) fetch-old))
767 result) 903 1)
768 (unwind-protect 904 (car articles))
769 (save-excursion 905 (car (last articles)) 'wait)
770 (while nntp-retry-command 906
771 (setq nntp-retry-command nil) 907 (goto-char (point-min))
772 ;; Clear communication buffer. 908 (when (looking-at "[1-5][0-9][0-9] ")
909 (delete-region (point) (progn (forward-line 1) (point))))
910 (while (search-forward "\r" nil t)
911 (replace-match "" t t))
912 (goto-char (point-max))
913 (forward-line -1)
914 (when (looking-at "\\.")
915 (delete-region (point) (progn (forward-line 1) (point)))))
916
917 ;; We do it the hard way. For each gap, an XOVER command is sent
918 ;; to the server. We do not wait for a reply from the server, we
919 ;; just send them off as fast as we can. That means that we have
920 ;; to count the number of responses we get back to find out when we
921 ;; have gotten all we asked for.
922 ((numberp nntp-nov-gap)
923 (let ((count 0)
924 (received 0)
925 (last-point (point-min))
926 (buf nntp-server-buffer)
927 ;;(process-buffer (nntp-find-connection (current-buffer))))
928 first)
929 ;; We have to check `nntp-server-xover'. If it gets set to nil,
930 ;; that means that the server does not understand XOVER, but we
931 ;; won't know that until we try.
932 (while (and nntp-server-xover articles)
933 (setq first (car articles))
934 ;; Search forward until we find a gap, or until we run out of
935 ;; articles.
936 (while (and (cdr articles)
937 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
938 (setq articles (cdr articles)))
939
940 (when (nntp-send-xover-command first (car articles))
941 (setq articles (cdr articles)
942 count (1+ count))
943
944 ;; Every 400 requests we have to read the stream in
945 ;; order to avoid deadlocks.
946 (when (or (null articles) ;All requests have been sent.
947 (zerop (% count nntp-maximum-request)))
948 (accept-process-output)
949 ;; On some Emacs versions the preceding function has
950 ;; a tendency to change the buffer. Perhaps. It's
951 ;; quite difficult to reproduce, because it only
952 ;; seems to happen once in a blue moon.
953 (set-buffer buf)
954 (while (progn
955 (goto-char last-point)
956 ;; Count replies.
957 (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
958 (setq received (1+ received)))
959 (setq last-point (point))
960 (< received count))
961 (accept-process-output)
962 (set-buffer buf)))))
963
964 (when nntp-server-xover
965 ;; Wait for the reply from the final command.
966 (goto-char (point-max))
967 (re-search-backward "^[0-9][0-9][0-9] " nil t)
968 (when (looking-at "^[23]")
969 (while (progn
970 (goto-char (point-max))
971 (forward-line -1)
972 (not (looking-at "^\\.\r?\n")))
973 (nntp-accept-response)))
974
975 ;; We remove any "." lines and status lines.
976 (goto-char (point-min))
977 (while (search-forward "\r" nil t)
978 (delete-char -1))
979 (goto-char (point-min))
980 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
981 ;;(copy-to-buffer nntp-server-buffer (point-min) (point-max))
982 t))))
983
984 nntp-server-xover)
985
986 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
987 "Send the XOVER command to the server."
988 (let ((range (format "%d-%d" beg end))
989 (nntp-inhibit-erase t))
990 (if (stringp nntp-server-xover)
991 ;; If `nntp-server-xover' is a string, then we just send this
992 ;; command.
993 (if wait-for-reply
994 (nntp-send-command-nodelete
995 "\r?\n\\.\r?\n" nntp-server-xover range)
996 ;; We do not wait for the reply.
997 (nntp-send-command-nodelete "\r?\n\\.\r?\n" nntp-server-xover range))
998 (let ((commands nntp-xover-commands))
999 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1000 ;; We try them all until we get at positive response.
1001 (while (and commands (eq nntp-server-xover 'try))
1002 (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1003 (save-excursion
773 (set-buffer nntp-server-buffer) 1004 (set-buffer nntp-server-buffer)
774 (widen) 1005 (goto-char (point-min))
1006 (and (looking-at "[23]") ; No error message.
1007 ;; We also have to look at the lines. Some buggy
1008 ;; servers give back simple lines with just the
1009 ;; article number. How... helpful.
1010 (progn
1011 (forward-line 1)
1012 (looking-at "[0-9]+\t...")) ; More text after number.
1013 (setq nntp-server-xover (car commands))))
1014 (setq commands (cdr commands)))
1015 ;; If none of the commands worked, we disable XOVER.
1016 (when (eq nntp-server-xover 'try)
1017 (save-excursion
1018 (set-buffer nntp-server-buffer)
775 (erase-buffer) 1019 (erase-buffer)
776 (if nntp-retry-on-break 1020 (setq nntp-server-xover nil)))
777 (condition-case () 1021 nntp-server-xover))))
778 (progn 1022
779 (apply 'nntp-send-strings-to-server cmd args) 1023 ;;; Alternative connection methods.
780 (setq result 1024
781 (if response 1025 (defun nntp-wait-for-string (regexp)
782 (nntp-wait-for-response response) 1026 "Wait until string arrives in the buffer."
783 t))) 1027 (let ((buf (current-buffer)))
784 (quit (setq nntp-retry-command t))) 1028 (goto-char (point-min))
785 (apply 'nntp-send-strings-to-server cmd args) 1029 (while (not (re-search-forward regexp nil t))
786 (setq result 1030 (accept-process-output (nntp-find-connection nntp-server-buffer))
787 (if response 1031 (set-buffer buf)
788 (nntp-wait-for-response response) 1032 (goto-char (point-min)))))
789 t)))) 1033
790 result) 1034 (defun nntp-open-telnet (buffer)
791 (when timer
792 (nnheader-cancel-timer timer)))))
793
794 (defun nntp-kill-command (server)
795 "Kill and restart the connection to SERVER."
796 (let ((proc (cdr (assq
797 'nntp-server-process
798 (assoc server (cddr (assq 'nntp nnoo-state-alist)))))))
799 (when proc
800 (delete-process (process-name proc)))
801 (nntp-close-server server)
802 (nntp-open-server server)
803 (when nntp-current-group
804 (nntp-request-group nntp-current-group))
805 (setq nntp-retry-command t)))
806
807 (defun nntp-send-command-old (response cmd &rest args)
808 "Wait for server RESPONSE after sending CMD and optional ARGS to server."
809 (save-excursion 1035 (save-excursion
810 ;; Clear communication buffer. 1036 (set-buffer buffer)
811 (set-buffer nntp-server-buffer)
812 (erase-buffer) 1037 (erase-buffer)
813 (apply 'nntp-send-strings-to-server cmd args) 1038 (let ((proc (start-process
814 (if response 1039 "nntpd" buffer "telnet" "-8"))
815 (nntp-wait-for-response response) 1040 (case-fold-search t))
816 t))) 1041 (when (memq (process-status proc) '(open run))
817 1042 (process-send-string proc "set escape \^X\n")
818 (defun nntp-wait-for-response (regexp &optional slow) 1043 (process-send-string proc (concat "open " nntp-address "\n"))
819 "Wait for server response which matches REGEXP." 1044 (nntp-wait-for-string "^\r*.?login:")
820 (save-excursion 1045 (process-send-string
821 (let ((status t) 1046 proc (concat
822 (wait t) 1047 (or nntp-telnet-user-name
823 (dotnum 0) ;Number of "." being displayed. 1048 (setq nntp-telnet-user-name (read-string "login: ")))
824 (dotsize ;How often "." displayed. 1049 "\n"))
825 (if (numberp nntp-debug-read) nntp-debug-read 10000))) 1050 (nntp-wait-for-string "^\r*.?password:")
826 (set-buffer nntp-server-buffer) 1051 (process-send-string
827 ;; Wait for status response (RFC977). 1052 proc (concat
828 ;; 1xx - Informative message. 1053 (or nntp-telnet-passwd
829 ;; 2xx - Command ok. 1054 (setq nntp-telnet-passwd
830 ;; 3xx - Command ok so far, send the rest of it. 1055 (nnmail-read-passwd "Password: ")))
831 ;; 4xx - Command was correct, but couldn't be performed for some 1056 "\n"))
832 ;; reason. 1057 (erase-buffer)
833 ;; 5xx - Command unimplemented, or incorrect, or a serious 1058 (nntp-wait-for-string "bash\\|\$ *\r?$\\|> *\r?")
834 ;; program error occurred. 1059 (process-send-string
835 (nntp-accept-response) 1060 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
836 (while wait 1061 (nntp-wait-for-string "^\r*200")
1062 (beginning-of-line)
1063 (delete-region (point-min) (point))
1064 (process-send-string proc "\^]")
1065 (nntp-wait-for-string "^telnet")
1066 (process-send-string proc "mode character\n")
1067 (accept-process-output proc 1)
1068 (sit-for 1)
837 (goto-char (point-min)) 1069 (goto-char (point-min))
838 (if slow 1070 (forward-line 1)
839 (progn 1071 (delete-region (point) (point-max)))
840 (cond ((re-search-forward "^[23][0-9][0-9]" nil t) 1072 proc)))
841 (setq wait nil)) 1073
842 ((re-search-forward "^[45][0-9][0-9]" nil t) 1074 (defun nntp-open-rlogin (buffer)
843 (setq status nil) 1075 "Open a connection to SERVER using rsh."
844 (setq wait nil)) 1076 (let ((proc (if nntp-rlogin-user-name
845 (t (nntp-accept-response))) 1077 (start-process
846 (if (not wait) (delete-region (point-min) 1078 "nntpd" buffer "rsh"
847 (progn (beginning-of-line) 1079 nntp-address "-l" nntp-rlogin-user-name
848 (point))))) 1080 (mapconcat 'identity
849 (cond ((looking-at "[23]") 1081 nntp-rlogin-parameters " "))
850 (setq wait nil)) 1082 (start-process
851 ((looking-at "[45]") 1083 "nntpd" buffer "rsh" nntp-address
852 (setq status nil) 1084 (mapconcat 'identity
853 (setq wait nil)) 1085 nntp-rlogin-parameters " ")))))
854 (t (nntp-accept-response))))) 1086 (set-buffer buffer)
855 ;; Save status message. 1087 (nntp-wait-for-string "^\r*200")
856 (end-of-line) 1088 (beginning-of-line)
857 (setq nntp-status-string 1089 (delete-region (point-min) (point))
858 (nnheader-replace-chars-in-string 1090 proc)
859 (buffer-substring (point-min) (point)) ?\r ? )) 1091 )
860 (when status
861 (setq wait t)
862 (while wait
863 (goto-char (point-max))
864 (if (bolp) (forward-line -1) (beginning-of-line))
865 (if (looking-at regexp)
866 (setq wait nil)
867 (when nntp-debug-read
868 (let ((newnum (/ (buffer-size) dotsize))
869 (message-log-max nil))
870 (unless (= dotnum newnum)
871 (setq dotnum newnum)
872 (nnheader-message 7 "NNTP: Reading %s"
873 (make-string dotnum ?.)))))
874 (nntp-accept-response)))
875 ;; Remove "...".
876 (when (and nntp-debug-read (> dotnum 0))
877 (message ""))
878 ;; Successfully received server response.
879 t))))
880
881
882
883 ;;;
884 ;;; Low-Level Interface to NNTP Server.
885 ;;;
886 1092
887 (defun nntp-find-group-and-number () 1093 (defun nntp-find-group-and-number ()
888 (save-excursion 1094 (save-excursion
889 (save-restriction 1095 (save-restriction
890 (set-buffer nntp-server-buffer) 1096 (set-buffer nntp-server-buffer)
917 ((and (setq xref (mail-fetch-field "xref")) 1123 ((and (setq xref (mail-fetch-field "xref"))
918 number 1124 number
919 (string-match (format "\\([^ :]+\\):%d" number) xref)) 1125 (string-match (format "\\([^ :]+\\):%d" number) xref))
920 (substring xref (match-beginning 1) (match-end 1))) 1126 (substring xref (match-beginning 1) (match-end 1)))
921 (t ""))) 1127 (t "")))
922 (when (string-match "\r" group) 1128 (when (string-match "\r" group)
923 (setq group (substring group 0 (match-beginning 0)))) 1129 (setq group (substring group 0 (match-beginning 0))))
924 (cons group number))))) 1130 (cons group number)))))
925 1131
926 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
927 (erase-buffer)
928 (cond
929
930 ;; This server does not talk NOV.
931 ((not nntp-server-xover)
932 nil)
933
934 ;; We don't care about gaps.
935 ((or (not nntp-nov-gap)
936 fetch-old)
937 (nntp-send-xover-command
938 (if fetch-old
939 (if (numberp fetch-old)
940 (max 1 (- (car articles) fetch-old))
941 1)
942 (car articles))
943 (nntp-last-element articles) 'wait)
944
945 (goto-char (point-min))
946 (when (looking-at "[1-5][0-9][0-9] ")
947 (delete-region (point) (progn (forward-line 1) (point))))
948 (while (search-forward "\r" nil t)
949 (replace-match "" t t))
950 (goto-char (point-max))
951 (forward-line -1)
952 (when (looking-at "\\.")
953 (delete-region (point) (progn (forward-line 1) (point)))))
954
955 ;; We do it the hard way. For each gap, an XOVER command is sent
956 ;; to the server. We do not wait for a reply from the server, we
957 ;; just send them off as fast as we can. That means that we have
958 ;; to count the number of responses we get back to find out when we
959 ;; have gotten all we asked for.
960 ((numberp nntp-nov-gap)
961 (let ((count 0)
962 (received 0)
963 (last-point (point-min))
964 (buf (current-buffer))
965 first)
966 ;; We have to check `nntp-server-xover'. If it gets set to nil,
967 ;; that means that the server does not understand XOVER, but we
968 ;; won't know that until we try.
969 (while (and nntp-server-xover articles)
970 (setq first (car articles))
971 ;; Search forward until we find a gap, or until we run out of
972 ;; articles.
973 (while (and (cdr articles)
974 (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
975 (setq articles (cdr articles)))
976
977 (when (nntp-send-xover-command first (car articles))
978 (setq articles (cdr articles)
979 count (1+ count))
980
981 ;; Every 400 requests we have to read the stream in
982 ;; order to avoid deadlocks.
983 (when (or (null articles) ;All requests have been sent.
984 (zerop (% count nntp-maximum-request)))
985 (accept-process-output nntp-server-process 1)
986 ;; On some Emacs versions the preceding function has
987 ;; a tendency to change the buffer. Perhaps. It's
988 ;; quite difficult to reproduce, because it only
989 ;; seems to happen once in a blue moon.
990 (set-buffer buf)
991 (while (progn
992 (goto-char last-point)
993 ;; Count replies.
994 (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
995 (setq received (1+ received)))
996 (setq last-point (point))
997 (< received count))
998 (accept-process-output nntp-server-process)
999 (set-buffer buf)))))
1000
1001 (when nntp-server-xover
1002 ;; Wait for the reply from the final command.
1003 (goto-char (point-max))
1004 (re-search-backward "^[0-9][0-9][0-9] " nil t)
1005 (when (looking-at "^[23]")
1006 (while (progn
1007 (goto-char (point-max))
1008 (forward-line -1)
1009 (not (looking-at "^\\.\r?\n")))
1010 (nntp-accept-response)))
1011
1012 ;; We remove any "." lines and status lines.
1013 (goto-char (point-min))
1014 (while (search-forward "\r" nil t)
1015 (delete-char -1))
1016 (goto-char (point-min))
1017 (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")))))
1018
1019 nntp-server-xover)
1020
1021 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1022 "Send the XOVER command to the server."
1023 (let ((range (format "%d-%d" (or beg 1) (or end beg 1))))
1024 (if (stringp nntp-server-xover)
1025 ;; If `nntp-server-xover' is a string, then we just send this
1026 ;; command.
1027 (if wait-for-reply
1028 (nntp-send-command "^\\.\r?\n" nntp-server-xover range)
1029 ;; We do not wait for the reply.
1030 (nntp-send-strings-to-server nntp-server-xover range))
1031 (let ((commands nntp-xover-commands))
1032 ;; `nntp-xover-commands' is a list of possible XOVER commands.
1033 ;; We try them all until we get at positive response.
1034 (while (and commands (eq nntp-server-xover 'try))
1035 (nntp-send-command "^\\.\r?\n" (car commands) range)
1036 (save-excursion
1037 (set-buffer nntp-server-buffer)
1038 (goto-char (point-min))
1039 (and (looking-at "[23]") ; No error message.
1040 ;; We also have to look at the lines. Some buggy
1041 ;; servers give back simple lines with just the
1042 ;; article number. How... helpful.
1043 (progn
1044 (forward-line 1)
1045 (looking-at "[0-9]+\t...")) ; More text after number.
1046 (setq nntp-server-xover (car commands))))
1047 (setq commands (cdr commands)))
1048 ;; If none of the commands worked, we disable XOVER.
1049 (when (eq nntp-server-xover 'try)
1050 (save-excursion
1051 (set-buffer nntp-server-buffer)
1052 (erase-buffer)
1053 (setq nntp-server-xover nil)))
1054 nntp-server-xover))))
1055
1056 (defun nntp-send-strings-to-server (&rest strings)
1057 "Send STRINGS to the server."
1058 (let ((cmd (concat (mapconcat 'identity strings " ") nntp-end-of-line)))
1059 ;; We open the nntp server if it is down.
1060 (or (nntp-server-opened (nnoo-current-server 'nntp))
1061 (nntp-open-server (nnoo-current-server 'nntp))
1062 (error "Couldn't open server: " (nntp-status-message)))
1063 ;; Send the strings.
1064 (process-send-string nntp-server-process cmd)
1065 t))
1066
1067 (defun nntp-send-region-to-server (begin end)
1068 "Send the current buffer region (from BEGIN to END) to the server."
1069 (save-excursion
1070 (let ((cur (current-buffer)))
1071 ;; Copy the buffer over to the send buffer.
1072 (nnheader-set-temp-buffer " *nntp send*")
1073 (insert-buffer-substring cur begin end)
1074 (save-excursion
1075 (set-buffer cur)
1076 (erase-buffer))
1077 ;; `process-send-region' does not work if the text to be sent is very
1078 ;; large, so we send it piecemeal.
1079 (let ((last (point-min))
1080 (size 100)) ;Size of text sent at once.
1081 (while (and (/= last (point-max))
1082 (memq (process-status nntp-server-process) '(open run)))
1083 (process-send-region
1084 nntp-server-process
1085 last (setq last (min (+ last size) (point-max))))
1086 ;; Read any output from the server. May be unnecessary.
1087 (accept-process-output)))
1088 (kill-buffer (current-buffer)))))
1089
1090 (defun nntp-open-server-semi-internal (server &optional service)
1091 "Open SERVER.
1092 If SERVER is nil, use value of environment variable `NNTPSERVER'.
1093 If SERVICE, use this as the port number."
1094 (nnheader-insert "")
1095 (let ((server (or server (getenv "NNTPSERVER")))
1096 (status nil)
1097 (timer
1098 (and nntp-connection-timeout
1099 (nnheader-run-at-time nntp-connection-timeout
1100 nil 'nntp-kill-connection server))))
1101 (save-excursion
1102 (set-buffer nntp-server-buffer)
1103 (setq nntp-status-string "")
1104 (nnheader-message 5 "nntp: Connecting to server on %s..." nntp-address)
1105 (cond ((and server (nntp-open-server-internal server service))
1106 (setq nntp-address server)
1107 (setq status
1108 (condition-case nil
1109 (nntp-wait-for-response "^[23].*\r?\n" 'slow)
1110 (error nil)
1111 ;(quit nil)
1112 ))
1113 (unless status
1114 (nntp-close-server-internal server)
1115 (nnheader-report
1116 'nntp "Couldn't open connection to %s"
1117 (if (and nntp-address
1118 (not (equal nntp-address "")))
1119 nntp-address server)))
1120 (when nntp-server-process
1121 (set-process-sentinel
1122 nntp-server-process 'nntp-default-sentinel)
1123 ;; You can send commands at startup like AUTHINFO here.
1124 ;; Added by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>
1125 (run-hooks 'nntp-server-opened-hook)))
1126 ((null server)
1127 (nnheader-report 'nntp "NNTP server is not specified."))
1128 (t ; We couldn't open the server.
1129 (nnheader-report 'nntp (buffer-string))))
1130 (when timer
1131 (nnheader-cancel-timer timer))
1132 (message "")
1133 (unless status
1134 (nnoo-close-server 'nntp server)
1135 (setq nntp-async-number nil))
1136 status)))
1137
1138 (defvar nntp-default-directories '("~" "/tmp" "/")
1139 "Directories to as current directory in the nntp server buffer.")
1140
1141 (defun nntp-open-server-internal (server &optional service)
1142 "Open connection to news server on SERVER by SERVICE (default is nntp)."
1143 (let (proc)
1144 (save-excursion
1145 (set-buffer nntp-server-buffer)
1146 ;; Make sure we have a valid current directory for the
1147 ;; nntp server buffer.
1148 (unless (file-exists-p default-directory)
1149 (let ((dirs nntp-default-directories))
1150 (while dirs
1151 (when (file-exists-p (car dirs))
1152 (setq default-directory (car dirs)
1153 dirs nil))
1154 (setq dirs (cdr dirs)))))
1155 (cond
1156 ((and (setq proc
1157 (condition-case nil
1158 (funcall nntp-open-server-function server)
1159 (error nil)))
1160 (memq (process-status proc) '(open run)))
1161 (setq nntp-server-process proc)
1162 (setq nntp-address server)
1163 ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1164 (process-kill-without-query proc)
1165 (run-hooks 'nntp-server-hook)
1166 (push proc nntp-opened-connections)
1167 (condition-case ()
1168 (nntp-read-server-type)
1169 (error
1170 (nnheader-report 'nntp "Couldn't open server %s" server)
1171 (nntp-close-server)))
1172 nntp-server-process)
1173 (t
1174 (nnheader-report 'nntp "Couldn't open server %s" server))))))
1175
1176 (defun nntp-read-server-type ()
1177 "Find out what the name of the server we have connected to is."
1178 ;; Wait for the status string to arrive.
1179 (nntp-wait-for-response "^.*\n" t)
1180 (setq nntp-server-type (buffer-string))
1181 (let ((alist nntp-server-action-alist)
1182 entry)
1183 ;; Run server-specific commmands.
1184 (while alist
1185 (setq entry (pop alist))
1186 (when (string-match (car entry) nntp-server-type)
1187 (if (and (listp (cadr entry))
1188 (not (eq 'lambda (caadr entry))))
1189 (eval (cadr entry))
1190 (funcall (cadr entry)))))))
1191
1192 (defun nntp-open-network-stream (server)
1193 (open-network-stream
1194 "nntpd" nntp-server-buffer server nntp-port-number))
1195
1196 (defun nntp-open-rlogin (server)
1197 "Open a connection to SERVER using rsh."
1198 (let ((proc (if nntp-rlogin-user-name
1199 (start-process
1200 "nntpd" nntp-server-buffer "rsh"
1201 server "-l" nntp-rlogin-user-name
1202 (mapconcat 'identity
1203 nntp-rlogin-parameters " "))
1204 (start-process
1205 "nntpd" nntp-server-buffer "rsh" server
1206 (mapconcat 'identity
1207 nntp-rlogin-parameters " ")))))
1208 proc))
1209
1210 (defun nntp-wait-for-string (regexp)
1211 "Wait until string arrives in the buffer."
1212 (let ((buf (current-buffer)))
1213 (goto-char (point-min))
1214 (while (not (re-search-forward regexp nil t))
1215 (accept-process-output nntp-server-process)
1216 (set-buffer buf)
1217 (goto-char (point-min)))))
1218
1219 (defun nntp-open-telnet (server)
1220 (save-excursion
1221 (set-buffer nntp-server-buffer)
1222 (erase-buffer)
1223 (let ((proc (start-process
1224 "nntpd" nntp-server-buffer "telnet" "-8"))
1225 (case-fold-search t))
1226 (when (memq (process-status proc) '(open run))
1227 (process-send-string proc "set escape \^X\n")
1228 (process-send-string proc (concat "open " server "\n"))
1229 (nntp-wait-for-string "^\r*.?login:")
1230 (process-send-string
1231 proc (concat
1232 (or nntp-telnet-user-name
1233 (setq nntp-telnet-user-name (read-string "login: ")))
1234 "\n"))
1235 (nntp-wait-for-string "^\r*.?password:")
1236 (process-send-string
1237 proc (concat
1238 (or nntp-telnet-passwd
1239 (setq nntp-telnet-passwd
1240 (nnmail-read-passwd "Password: ")))
1241 "\n"))
1242 (erase-buffer)
1243 (nntp-wait-for-string "bash\\|\$ *\r?$\\|> *\r?")
1244 (process-send-string
1245 proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1246 (nntp-wait-for-string "^\r*200")
1247 (beginning-of-line)
1248 (delete-region (point-min) (point))
1249 (process-send-string proc "\^]")
1250 (nntp-wait-for-string "^telnet")
1251 (process-send-string proc "mode character\n")
1252 (accept-process-output proc 1)
1253 (sit-for 1)
1254 (goto-char (point-min))
1255 (forward-line 1)
1256 (delete-region (point) (point-max)))
1257 proc)))
1258
1259 (defun nntp-close-server-internal (&optional server)
1260 "Close connection to news server."
1261 (nntp-possibly-change-server nil server)
1262 (if nntp-server-process
1263 (delete-process nntp-server-process))
1264 (setq nntp-server-process nil)
1265 ;(setq nntp-address "")
1266 )
1267
1268 (defun nntp-accept-response ()
1269 "Read response of server.
1270 It is well-known that the communication speed will be much improved by
1271 defining this function as macro."
1272 ;; To deal with server process exiting before
1273 ;; accept-process-output is called.
1274 ;; Suggested by Jason Venner <jason@violet.berkeley.edu>.
1275 ;; This is a copy of `nntp-default-sentinel'.
1276 (let ((buf (current-buffer)))
1277 (prog1
1278 (if (or (not nntp-server-process)
1279 (not (memq (process-status nntp-server-process) '(open run))))
1280 (error "nntp: Process connection closed; %s" (nntp-status-message))
1281 (if nntp-buggy-select
1282 (progn
1283 ;; We cannot use `accept-process-output'.
1284 ;; Fujitsu UTS requires messages during sleep-for.
1285 ;; I don't know why.
1286 (nnheader-message 5 "NNTP: Reading...")
1287 (sleep-for 1)
1288 (nnheader-message 5 ""))
1289 (condition-case errorcode
1290 (accept-process-output nntp-server-process 1)
1291 (error
1292 (cond ((string-equal "select error: Invalid argument"
1293 (nth 1 errorcode))
1294 ;; Ignore select error.
1295 nil)
1296 (t
1297 (signal (car errorcode) (cdr errorcode))))))))
1298 (set-buffer buf))))
1299
1300 (defun nntp-last-element (list)
1301 "Return last element of LIST."
1302 (while (cdr list)
1303 (setq list (cdr list)))
1304 (car list))
1305
1306 (defun nntp-possibly-change-server (newsgroup server &optional connectionless)
1307 "Check whether the virtual server needs changing."
1308 (when (and server
1309 (not (nntp-server-opened server)))
1310 ;; This virtual server isn't open, so we (re)open it here.
1311 (nntp-open-server server nil t))
1312 (when (and newsgroup
1313 (not (equal newsgroup nntp-current-group)))
1314 ;; Set the proper current group.
1315 (nntp-request-group newsgroup server)))
1316
1317 (defun nntp-try-list-active (group)
1318 (nntp-list-active-group group)
1319 (save-excursion
1320 (set-buffer nntp-server-buffer)
1321 (goto-char (point-min))
1322 (cond ((looking-at "5[0-9]+")
1323 (setq nntp-server-list-active-group nil))
1324 (t
1325 (setq nntp-server-list-active-group t)))))
1326
1327 (defun nntp-async-server-opened ()
1328 (and nntp-async-process
1329 (memq (process-status nntp-async-process) '(open run))))
1330
1331 (defun nntp-async-open-server ()
1332 (save-excursion
1333 (set-buffer (generate-new-buffer " *async-nntp*"))
1334 (setq nntp-async-buffer (current-buffer))
1335 (buffer-disable-undo (current-buffer)))
1336 (let ((nntp-server-process nil)
1337 (nntp-server-buffer nntp-async-buffer))
1338 (nntp-open-server-semi-internal nntp-address nntp-port-number)
1339 (if (not (setq nntp-async-process nntp-server-process))
1340 (progn
1341 (setq nntp-async-number nil))
1342 (set-process-buffer nntp-async-process nntp-async-buffer))))
1343
1344 (defun nntp-async-fetch-articles (article)
1345 (if (stringp article)
1346 ()
1347 (let ((articles (cdr (memq (assq article nntp-async-articles)
1348 nntp-async-articles)))
1349 (max (cond ((numberp nntp-async-number)
1350 nntp-async-number)
1351 ((eq nntp-async-number t)
1352 (length nntp-async-articles))
1353 (t 0)))
1354 nart)
1355 (while (and (>= (setq max (1- max)) 0)
1356 articles)
1357 (or (memq (setq nart (caar articles)) nntp-async-fetched)
1358 (progn
1359 (nntp-async-send-strings "ARTICLE " (int-to-string nart))
1360 (setq nntp-async-fetched (cons nart nntp-async-fetched))))
1361 (setq articles (cdr articles))))))
1362
1363 (defun nntp-async-send-strings (&rest strings)
1364 (let ((cmd (concat (mapconcat 'identity strings " ") nntp-end-of-line)))
1365 (or (nntp-async-server-opened)
1366 (nntp-async-open-server)
1367 (error (nntp-status-message)))
1368 (process-send-string nntp-async-process cmd)))
1369
1370 (defun nntp-async-request-group (group)
1371 (if (equal group nntp-current-group)
1372 ()
1373 (let ((asyncs (assoc group nntp-async-group-alist)))
1374 ;; A new group has been selected, so we push the current state
1375 ;; of async articles on an alist, and pull the old state off.
1376 (setq nntp-async-group-alist
1377 (cons (list nntp-current-group
1378 nntp-async-articles nntp-async-fetched
1379 nntp-async-process)
1380 (delq asyncs nntp-async-group-alist)))
1381 (and asyncs
1382 (progn
1383 (setq nntp-async-articles (nth 1 asyncs))
1384 (setq nntp-async-fetched (nth 2 asyncs))
1385 (setq nntp-async-process (nth 3 asyncs)))))))
1386
1387 (provide 'nntp) 1132 (provide 'nntp)
1388 1133
1389 ;;; nntp.el ends here 1134 ;;; nntp.el ends here