Commit | Line | Data |
---|---|---|
16409b0b | 1 | ;;; nntp.el --- nntp access for Gnus |
f9936da6 | 2 | |
16409b0b | 3 | ;; Copyright (C) 1987, 1988, 1989, 1990, 1992, 1993, 1994, 1995, 1996, |
8654e13a MB |
4 | ;; 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005 |
5 | ;; Free Software Foundation, Inc. | |
eec82323 | 6 | |
6748645f | 7 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> |
eec82323 LMI |
8 | ;; Keywords: news |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
23f87bed MB |
13 | ;; it under the terms of the GNU General Public License as published |
14 | ;; by the Free Software Foundation; either version 2, or (at your | |
15 | ;; option) any later version. | |
eec82323 | 16 | |
23f87bed MB |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, but |
18 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | ;; General Public License for more details. | |
eec82323 LMI |
21 | |
22 | ;; You should have received a copy of the GNU General Public License | |
23f87bed | 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
3ef97fb6 LK |
24 | ;; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, |
25 | ;; MA 02110-1301, USA. | |
eec82323 LMI |
26 | |
27 | ;;; Commentary: | |
28 | ||
29 | ;;; Code: | |
30 | ||
31 | (require 'nnheader) | |
32 | (require 'nnoo) | |
33 | (require 'gnus-util) | |
34 | ||
35 | (nnoo-declare nntp) | |
36 | ||
16409b0b | 37 | (eval-when-compile (require 'cl)) |
eec82323 | 38 | |
e79f14a4 RS |
39 | (defgroup nntp nil |
40 | "NNTP access for Gnus." | |
41 | :group 'gnus) | |
42 | ||
eec82323 LMI |
43 | (defvoo nntp-address nil |
44 | "Address of the physical nntp server.") | |
45 | ||
46 | (defvoo nntp-port-number "nntp" | |
47 | "Port number on the physical nntp server.") | |
48 | ||
49 | (defvoo nntp-server-opened-hook '(nntp-send-mode-reader) | |
50 | "*Hook used for sending commands to the server at startup. | |
51 | The default value is `nntp-send-mode-reader', which makes an innd | |
6748645f | 52 | server spawn an nnrpd server.") |
eec82323 LMI |
53 | |
54 | (defvoo nntp-authinfo-function 'nntp-send-authinfo | |
6748645f LMI |
55 | "Function used to send AUTHINFO to the server. |
56 | It is called with no parameters.") | |
eec82323 LMI |
57 | |
58 | (defvoo nntp-server-action-alist | |
16409b0b GM |
59 | '(("nntpd 1\\.5\\.11t" |
60 | (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)) | |
61 | ("NNRP server Netscape" | |
62 | (setq nntp-server-list-active-group nil))) | |
eec82323 LMI |
63 | "Alist of regexps to match on server types and actions to be taken. |
64 | For instance, if you want Gnus to beep every time you connect | |
65 | to innd, you could say something like: | |
66 | ||
67 | \(setq nntp-server-action-alist | |
68 | '((\"innd\" (ding)))) | |
69 | ||
70 | You probably don't want to do that, though.") | |
71 | ||
72 | (defvoo nntp-open-connection-function 'nntp-open-network-stream | |
73 | "*Function used for connecting to a remote system. | |
23f87bed | 74 | It will be called with the buffer to output in as argument. |
eec82323 | 75 | |
23f87bed MB |
76 | Currently, five such functions are provided (please refer to their |
77 | respective doc string for more information), three of them establishing | |
78 | direct connections to the nntp server, and two of them using an indirect | |
79 | host. | |
eec82323 | 80 | |
23f87bed MB |
81 | Direct connections: |
82 | - `nntp-open-network-stream' (the default), | |
83 | - `nntp-open-ssl-stream', | |
84 | - `nntp-open-tls-stream', | |
85 | - `nntp-open-telnet-stream'. | |
6748645f | 86 | |
23f87bed MB |
87 | Indirect connections: |
88 | - `nntp-open-via-rlogin-and-telnet', | |
89 | - `nntp-open-via-telnet-and-telnet'.") | |
eec82323 | 90 | |
23f87bed MB |
91 | (defvoo nntp-pre-command nil |
92 | "*Pre-command to use with the various nntp-open-via-* methods. | |
93 | This is where you would put \"runsocks\" or stuff like that.") | |
eec82323 | 94 | |
23f87bed MB |
95 | (defvoo nntp-telnet-command "telnet" |
96 | "*Telnet command used to connect to the nntp server. | |
97 | This command is used by the various nntp-open-via-* methods.") | |
eec82323 | 98 | |
23f87bed MB |
99 | (defvoo nntp-telnet-switches '("-8") |
100 | "*Switches given to the telnet command `nntp-telnet-command'.") | |
eec82323 | 101 | |
23f87bed MB |
102 | (defvoo nntp-end-of-line "\r\n" |
103 | "*String to use on the end of lines when talking to the NNTP server. | |
104 | This is \"\\r\\n\" by default, but should be \"\\n\" when | |
105 | using and indirect connection method (nntp-open-via-*).") | |
eec82323 | 106 | |
23f87bed MB |
107 | (defvoo nntp-via-rlogin-command "rsh" |
108 | "*Rlogin command used to connect to an intermediate host. | |
109 | This command is used by the `nntp-open-via-rlogin-and-telnet' method. | |
110 | The default is \"rsh\", but \"ssh\" is a popular alternative.") | |
6748645f | 111 | |
23f87bed MB |
112 | (defvoo nntp-via-rlogin-command-switches nil |
113 | "*Switches given to the rlogin command `nntp-via-rlogin-command'. | |
114 | If you use \"ssh\" for `nntp-via-rlogin-command', you may set this to | |
115 | \(\"-C\") in order to compress all data connections, otherwise set this | |
116 | to \(\"-t\" \"-e\" \"none\") or (\"-C\" \"-t\" \"-e\" \"none\") if the telnet | |
117 | command requires a pseudo-tty allocation on an intermediate host.") | |
6748645f | 118 | |
23f87bed MB |
119 | (defvoo nntp-via-telnet-command "telnet" |
120 | "*Telnet command used to connect to an intermediate host. | |
121 | This command is used by the `nntp-open-via-telnet-and-telnet' method.") | |
a8151ef7 | 122 | |
23f87bed MB |
123 | (defvoo nntp-via-telnet-switches '("-8") |
124 | "*Switches given to the telnet command `nntp-via-telnet-command'.") | |
a8151ef7 | 125 | |
23f87bed MB |
126 | (defvoo nntp-via-user-name nil |
127 | "*User name to log in on an intermediate host with. | |
128 | This variable is used by the `nntp-open-via-telnet-and-telnet' method.") | |
129 | ||
130 | (defvoo nntp-via-user-password nil | |
131 | "*Password to use to log in on an intermediate host with. | |
132 | This variable is used by the `nntp-open-via-telnet-and-telnet' method.") | |
133 | ||
134 | (defvoo nntp-via-address nil | |
135 | "*Address of an intermediate host to connect to. | |
136 | This variable is used by the `nntp-open-via-rlogin-and-telnet' and | |
137 | `nntp-open-via-telnet-and-telnet' methods.") | |
138 | ||
139 | (defvoo nntp-via-envuser nil | |
140 | "*Whether both telnet client and server support the ENVIRON option. | |
141 | If non-nil, there will be no prompt for a login name.") | |
142 | ||
143 | (defvoo nntp-via-shell-prompt "bash\\|\$ *\r?$\\|> *\r?" | |
144 | "*Regular expression to match the shell prompt on an intermediate host. | |
145 | This variable is used by the `nntp-open-via-telnet-and-telnet' method.") | |
eec82323 LMI |
146 | |
147 | (defvoo nntp-large-newsgroup 50 | |
23f87bed MB |
148 | "*The number of articles which indicates a large newsgroup. |
149 | If the number of articles is greater than the value, verbose | |
eec82323 LMI |
150 | messages will be shown to indicate the current status.") |
151 | ||
152 | (defvoo nntp-maximum-request 400 | |
153 | "*The maximum number of the requests sent to the NNTP server at one time. | |
154 | If Emacs hangs up while retrieving headers, set the variable to a | |
155 | lower value.") | |
156 | ||
157 | (defvoo nntp-nov-is-evil nil | |
158 | "*If non-nil, nntp will never attempt to use XOVER when talking to the server.") | |
159 | ||
160 | (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW") | |
161 | "*List of strings that are used as commands to fetch NOV lines from a server. | |
162 | The strings are tried in turn until a positive response is gotten. If | |
163 | none of the commands are successful, nntp will just grab headers one | |
164 | by one.") | |
165 | ||
a8151ef7 | 166 | (defvoo nntp-nov-gap 5 |
eec82323 LMI |
167 | "*Maximum allowed gap between two articles. |
168 | If the gap between two consecutive articles is bigger than this | |
169 | variable, split the XOVER request into two requests.") | |
170 | ||
eec82323 LMI |
171 | (defvoo nntp-prepare-server-hook nil |
172 | "*Hook run before a server is opened. | |
173 | If can be used to set up a server remotely, for instance. Say you | |
174 | have an account at the machine \"other.machine\". This machine has | |
175 | access to an NNTP server that you can't access locally. You could | |
176 | then use this hook to rsh to the remote machine and start a proxy NNTP | |
6748645f LMI |
177 | server there that you can connect to. See also |
178 | `nntp-open-connection-function'") | |
eec82323 LMI |
179 | |
180 | (defvoo nntp-warn-about-losing-connection t | |
181 | "*If non-nil, beep when a server closes connection.") | |
182 | ||
6748645f LMI |
183 | (defvoo nntp-coding-system-for-read 'binary |
184 | "*Coding system to read from NNTP.") | |
185 | ||
186 | (defvoo nntp-coding-system-for-write 'binary | |
187 | "*Coding system to write to NNTP.") | |
188 | ||
189 | (defcustom nntp-authinfo-file "~/.authinfo" | |
190 | ".netrc-like file that holds nntp authinfo passwords." | |
e79f14a4 | 191 | :group 'nntp |
6748645f LMI |
192 | :type |
193 | '(choice file | |
194 | (repeat :tag "Entries" | |
195 | :menu-tag "Inline" | |
196 | (list :format "%v" | |
197 | :value ("" ("login" . "") ("password" . "")) | |
198 | (string :tag "Host") | |
199 | (checklist :inline t | |
200 | (cons :format "%v" | |
201 | (const :format "" "login") | |
202 | (string :format "Login: %v")) | |
203 | (cons :format "%v" | |
204 | (const :format "" "password") | |
23f87bed | 205 | (string :format "Password: %v"))))))) |
6748645f | 206 | |
eec82323 LMI |
207 | \f |
208 | ||
6748645f LMI |
209 | (defvoo nntp-connection-timeout nil |
210 | "*Number of seconds to wait before an nntp connection times out. | |
2eebe218 DL |
211 | If this variable is nil, which is the default, no timers are set. |
212 | NOTE: This variable is never seen to work in Emacs 20 and XEmacs 21.") | |
6748645f | 213 | |
23f87bed MB |
214 | (defvoo nntp-prepare-post-hook nil |
215 | "*Hook run just before posting an article. It is supposed to be used | |
216 | to insert Cancel-Lock headers.") | |
217 | ||
eec82323 LMI |
218 | ;;; Internal variables. |
219 | ||
6748645f LMI |
220 | (defvar nntp-record-commands nil |
221 | "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.") | |
222 | ||
eec82323 LMI |
223 | (defvar nntp-have-messaged nil) |
224 | ||
225 | (defvar nntp-process-wait-for nil) | |
226 | (defvar nntp-process-to-buffer nil) | |
227 | (defvar nntp-process-callback nil) | |
228 | (defvar nntp-process-decode nil) | |
229 | (defvar nntp-process-start-point nil) | |
230 | (defvar nntp-inside-change-function nil) | |
6748645f LMI |
231 | (defvoo nntp-last-command-time nil) |
232 | (defvoo nntp-last-command nil) | |
233 | (defvoo nntp-authinfo-password nil) | |
234 | (defvoo nntp-authinfo-user nil) | |
eec82323 LMI |
235 | |
236 | (defvar nntp-connection-list nil) | |
237 | ||
238 | (defvoo nntp-server-type nil) | |
239 | (defvoo nntp-connection-alist nil) | |
240 | (defvoo nntp-status-string "") | |
241 | (defconst nntp-version "nntp 5.0") | |
242 | (defvoo nntp-inhibit-erase nil) | |
243 | (defvoo nntp-inhibit-output nil) | |
244 | ||
245 | (defvoo nntp-server-xover 'try) | |
246 | (defvoo nntp-server-list-active-group 'try) | |
247 | ||
16409b0b GM |
248 | (defvar nntp-async-needs-kluge |
249 | (string-match "^GNU Emacs 20\\.3\\." (emacs-version)) | |
250 | "*When non-nil, nntp will poll asynchronous connections | |
251 | once a second. By default, this is turned on only for Emacs | |
252 | 20.3, which has a bug that breaks nntp's normal method of | |
253 | noticing asynchronous data.") | |
254 | ||
255 | (defvar nntp-async-timer nil) | |
256 | (defvar nntp-async-process-list nil) | |
257 | ||
23f87bed | 258 | (defvar nntp-ssl-program |
f9936da6 SZ |
259 | "openssl s_client -quiet -ssl3 -connect %s:%p" |
260 | "A string containing commands for SSL connections. | |
261 | Within a string, %s is replaced with the server address and %p with | |
262 | port number on server. The program should accept IMAP commands on | |
263 | stdin and return responses to stdout.") | |
264 | ||
eec82323 LMI |
265 | \f |
266 | ||
267 | ;;; Internal functions. | |
268 | ||
269 | (defsubst nntp-send-string (process string) | |
270 | "Send STRING to PROCESS." | |
6748645f LMI |
271 | ;; We need to store the time to provide timeouts, and |
272 | ;; to store the command so the we can replay the command | |
273 | ;; if the server gives us an AUTHINFO challenge. | |
274 | (setq nntp-last-command-time (current-time) | |
275 | nntp-last-command string) | |
276 | (when nntp-record-commands | |
277 | (nntp-record-command string)) | |
23f87bed MB |
278 | (process-send-string process (concat string nntp-end-of-line)) |
279 | (or (memq (process-status process) '(open run)) | |
280 | (nntp-report "Server closed connection"))) | |
eec82323 | 281 | |
6748645f LMI |
282 | (defun nntp-record-command (string) |
283 | "Record the command STRING." | |
284 | (save-excursion | |
285 | (set-buffer (get-buffer-create "*nntp-log*")) | |
286 | (goto-char (point-max)) | |
287 | (let ((time (current-time))) | |
288 | (insert (format-time-string "%Y%m%dT%H%M%S" time) | |
289 | "." (format "%03d" (/ (nth 2 time) 1000)) | |
290 | " " nntp-address " " string "\n")))) | |
291 | ||
23f87bed MB |
292 | (defun nntp-report (&rest args) |
293 | "Report an error from the nntp backend. The first string in ARGS | |
294 | can be a format string. For some commands, the failed command may be | |
295 | retried once before actually displaying the error report." | |
296 | ||
297 | (when nntp-record-commands | |
298 | (nntp-record-command "*** CALLED nntp-report ***")) | |
299 | ||
300 | (nnheader-report 'nntp args) | |
301 | ||
302 | (apply 'error args)) | |
303 | ||
304 | (defun nntp-report-1 (&rest args) | |
305 | "Throws out to nntp-with-open-group-error so that the connection may | |
306 | be restored and the command retried." | |
307 | ||
308 | (when nntp-record-commands | |
309 | (nntp-record-command "*** CONNECTION LOST ***")) | |
310 | ||
311 | (throw 'nntp-with-open-group-error t)) | |
312 | ||
eec82323 LMI |
313 | (defsubst nntp-wait-for (process wait-for buffer &optional decode discard) |
314 | "Wait for WAIT-FOR to arrive from PROCESS." | |
315 | (save-excursion | |
316 | (set-buffer (process-buffer process)) | |
317 | (goto-char (point-min)) | |
6748645f LMI |
318 | (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5))) |
319 | (looking-at "480")) | |
320 | (memq (process-status process) '(open run))) | |
eec82323 | 321 | (when (looking-at "480") |
6748645f | 322 | (nntp-handle-authinfo process)) |
23f87bed MB |
323 | (when (looking-at "^.*\n") |
324 | (delete-region (point) (progn (forward-line 1) (point)))) | |
eec82323 LMI |
325 | (nntp-accept-process-output process) |
326 | (goto-char (point-min))) | |
327 | (prog1 | |
6748645f LMI |
328 | (cond |
329 | ((looking-at "[45]") | |
330 | (progn | |
331 | (nntp-snarf-error-message) | |
332 | nil)) | |
333 | ((not (memq (process-status process) '(open run))) | |
23f87bed | 334 | (nntp-report "Server closed connection")) |
6748645f | 335 | (t |
eec82323 | 336 | (goto-char (point-max)) |
23f87bed MB |
337 | (let ((limit (point-min)) |
338 | response) | |
eec82323 | 339 | (while (not (re-search-backward wait-for limit t)) |
6748645f | 340 | (nntp-accept-process-output process) |
eec82323 LMI |
341 | ;; We assume that whatever we wait for is less than 1000 |
342 | ;; characters long. | |
343 | (setq limit (max (- (point-max) 1000) (point-min))) | |
23f87bed MB |
344 | (goto-char (point-max))) |
345 | (setq response (match-string 0)) | |
346 | (with-current-buffer nntp-server-buffer | |
347 | (setq nntp-process-response response))) | |
eec82323 LMI |
348 | (nntp-decode-text (not decode)) |
349 | (unless discard | |
350 | (save-excursion | |
23f87bed MB |
351 | (set-buffer buffer) |
352 | (goto-char (point-max)) | |
353 | (insert-buffer-substring (process-buffer process)) | |
eec82323 LMI |
354 | ;; Nix out "nntp reading...." message. |
355 | (when nntp-have-messaged | |
356 | (setq nntp-have-messaged nil) | |
23f87bed MB |
357 | (nnheader-message 5 "")))) |
358 | t)) | |
eec82323 LMI |
359 | (unless discard |
360 | (erase-buffer))))) | |
361 | ||
16409b0b GM |
362 | (defun nntp-kill-buffer (buffer) |
363 | (when (buffer-name buffer) | |
364 | (kill-buffer buffer) | |
365 | (nnheader-init-server-buffer))) | |
366 | ||
eec82323 LMI |
367 | (defsubst nntp-find-connection (buffer) |
368 | "Find the connection delivering to BUFFER." | |
369 | (let ((alist nntp-connection-alist) | |
370 | (buffer (if (stringp buffer) (get-buffer buffer) buffer)) | |
371 | process entry) | |
23f87bed | 372 | (while (and alist (setq entry (pop alist))) |
eec82323 LMI |
373 | (when (eq buffer (cadr entry)) |
374 | (setq process (car entry) | |
375 | alist nil))) | |
376 | (when process | |
377 | (if (memq (process-status process) '(open run)) | |
378 | process | |
16409b0b | 379 | (nntp-kill-buffer (process-buffer process)) |
eec82323 LMI |
380 | (setq nntp-connection-alist (delq entry nntp-connection-alist)) |
381 | nil)))) | |
382 | ||
383 | (defsubst nntp-find-connection-entry (buffer) | |
384 | "Return the entry for the connection to BUFFER." | |
385 | (assq (nntp-find-connection buffer) nntp-connection-alist)) | |
386 | ||
387 | (defun nntp-find-connection-buffer (buffer) | |
388 | "Return the process connection buffer tied to BUFFER." | |
389 | (let ((process (nntp-find-connection buffer))) | |
390 | (when process | |
391 | (process-buffer process)))) | |
392 | ||
393 | (defsubst nntp-retrieve-data (command address port buffer | |
6748645f | 394 | &optional wait-for callback decode) |
eec82323 LMI |
395 | "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS." |
396 | (let ((process (or (nntp-find-connection buffer) | |
397 | (nntp-open-connection buffer)))) | |
23f87bed MB |
398 | (if process |
399 | (progn | |
400 | (unless (or nntp-inhibit-erase nnheader-callback-function) | |
401 | (save-excursion | |
402 | (set-buffer (process-buffer process)) | |
403 | (erase-buffer))) | |
404 | (condition-case err | |
405 | (progn | |
406 | (when command | |
407 | (nntp-send-string process command)) | |
408 | (cond | |
409 | ((eq callback 'ignore) | |
410 | t) | |
411 | ((and callback wait-for) | |
412 | (nntp-async-wait process wait-for buffer decode callback) | |
413 | t) | |
414 | (wait-for | |
415 | (nntp-wait-for process wait-for buffer decode)) | |
416 | (t t))) | |
417 | (error | |
418 | (nnheader-report 'nntp "Couldn't open connection to %s: %s" | |
419 | address err)) | |
420 | (quit | |
421 | (message "Quit retrieving data from nntp") | |
422 | (signal 'quit nil) | |
423 | nil))) | |
424 | (nnheader-report 'nntp "Couldn't open connection to %s" address)))) | |
eec82323 LMI |
425 | |
426 | (defsubst nntp-send-command (wait-for &rest strings) | |
427 | "Send STRINGS to server and wait until WAIT-FOR returns." | |
428 | (when (and (not nnheader-callback-function) | |
429 | (not nntp-inhibit-output)) | |
430 | (save-excursion | |
431 | (set-buffer nntp-server-buffer) | |
432 | (erase-buffer))) | |
23f87bed MB |
433 | (let* ((command (mapconcat 'identity strings " ")) |
434 | (process (nntp-find-connection nntp-server-buffer)) | |
435 | (buffer (and process (process-buffer process))) | |
436 | (pos (and buffer (with-current-buffer buffer (point))))) | |
437 | (if process | |
438 | (prog1 | |
439 | (nntp-retrieve-data command | |
440 | nntp-address nntp-port-number | |
441 | nntp-server-buffer | |
442 | wait-for nnheader-callback-function) | |
443 | ;; If nothing to wait for, still remove possibly echo'ed commands. | |
444 | ;; We don't have echos if nntp-open-connection-function | |
445 | ;; is `nntp-open-network-stream', so we skip this in that case. | |
446 | (unless (or wait-for | |
447 | (equal nntp-open-connection-function | |
448 | 'nntp-open-network-stream)) | |
449 | (nntp-accept-response) | |
450 | (save-excursion | |
451 | (set-buffer buffer) | |
452 | (goto-char pos) | |
453 | (if (looking-at (regexp-quote command)) | |
454 | (delete-region pos (progn (forward-line 1) | |
455 | (gnus-point-at-bol)))) | |
456 | ))) | |
457 | (nnheader-report 'nntp "Couldn't open connection to %s." | |
458 | nntp-address)))) | |
eec82323 LMI |
459 | |
460 | (defun nntp-send-command-nodelete (wait-for &rest strings) | |
461 | "Send STRINGS to server and wait until WAIT-FOR returns." | |
23f87bed MB |
462 | (let* ((command (mapconcat 'identity strings " ")) |
463 | (process (nntp-find-connection nntp-server-buffer)) | |
464 | (buffer (and process (process-buffer process))) | |
465 | (pos (and buffer (with-current-buffer buffer (point))))) | |
466 | (if process | |
467 | (prog1 | |
468 | (nntp-retrieve-data command | |
469 | nntp-address nntp-port-number | |
470 | nntp-server-buffer | |
471 | wait-for nnheader-callback-function) | |
472 | ;; If nothing to wait for, still remove possibly echo'ed commands | |
473 | (unless wait-for | |
474 | (nntp-accept-response) | |
475 | (save-excursion | |
476 | (set-buffer buffer) | |
477 | (goto-char pos) | |
478 | (if (looking-at (regexp-quote command)) | |
479 | (delete-region pos (progn (forward-line 1) | |
480 | (gnus-point-at-bol))))))) | |
481 | (nnheader-report 'nntp "Couldn't open connection to %s." | |
482 | nntp-address)))) | |
eec82323 LMI |
483 | |
484 | (defun nntp-send-command-and-decode (wait-for &rest strings) | |
485 | "Send STRINGS to server and wait until WAIT-FOR returns." | |
486 | (when (and (not nnheader-callback-function) | |
487 | (not nntp-inhibit-output)) | |
488 | (save-excursion | |
489 | (set-buffer nntp-server-buffer) | |
490 | (erase-buffer))) | |
23f87bed MB |
491 | (let* ((command (mapconcat 'identity strings " ")) |
492 | (process (nntp-find-connection nntp-server-buffer)) | |
493 | (buffer (and process (process-buffer process))) | |
494 | (pos (and buffer (with-current-buffer buffer (point))))) | |
495 | (if process | |
496 | (prog1 | |
497 | (nntp-retrieve-data command | |
498 | nntp-address nntp-port-number | |
499 | nntp-server-buffer | |
500 | wait-for nnheader-callback-function t) | |
501 | ;; If nothing to wait for, still remove possibly echo'ed commands | |
502 | (unless wait-for | |
503 | (nntp-accept-response) | |
504 | (save-excursion | |
505 | (set-buffer buffer) | |
506 | (goto-char pos) | |
507 | (if (looking-at (regexp-quote command)) | |
508 | (delete-region pos (progn (forward-line 1) (gnus-point-at-bol)))) | |
509 | ))) | |
510 | (nnheader-report 'nntp "Couldn't open connection to %s." | |
511 | nntp-address)))) | |
512 | ||
eec82323 LMI |
513 | |
514 | (defun nntp-send-buffer (wait-for) | |
515 | "Send the current buffer to server and wait until WAIT-FOR returns." | |
516 | (when (and (not nnheader-callback-function) | |
517 | (not nntp-inhibit-output)) | |
518 | (save-excursion | |
519 | (set-buffer (nntp-find-connection-buffer nntp-server-buffer)) | |
520 | (erase-buffer))) | |
521 | (nntp-encode-text) | |
1f7d2e14 SZ |
522 | (mm-with-unibyte-current-buffer |
523 | ;; Some encoded unicode text contains character 0x80-0x9f e.g. Euro. | |
524 | (process-send-region (nntp-find-connection nntp-server-buffer) | |
525 | (point-min) (point-max))) | |
eec82323 LMI |
526 | (nntp-retrieve-data |
527 | nil nntp-address nntp-port-number nntp-server-buffer | |
528 | wait-for nnheader-callback-function)) | |
529 | ||
530 | \f | |
531 | ||
532 | ;;; Interface functions. | |
533 | ||
534 | (nnoo-define-basics nntp) | |
535 | ||
6748645f LMI |
536 | (defsubst nntp-next-result-arrived-p () |
537 | (cond | |
538 | ;; A result that starts with a 2xx code is terminated by | |
539 | ;; a line with only a "." on it. | |
16409b0b | 540 | ((eq (char-after) ?2) |
6748645f LMI |
541 | (if (re-search-forward "\n\\.\r?\n" nil t) |
542 | t | |
543 | nil)) | |
544 | ;; A result that starts with a 3xx or 4xx code is terminated | |
545 | ;; by a newline. | |
546 | ((looking-at "[34]") | |
547 | (if (search-forward "\n" nil t) | |
548 | t | |
549 | nil)) | |
550 | ;; No result here. | |
551 | (t | |
552 | nil))) | |
553 | ||
23f87bed MB |
554 | (eval-when-compile |
555 | (defvar nntp-with-open-group-internal nil) | |
556 | (defvar nntp-report-n nil)) | |
557 | ||
558 | (defmacro nntp-with-open-group (group server &optional connectionless &rest forms) | |
559 | "Protect against servers that don't like clients that keep idle connections opens. | |
560 | The problem being that these servers may either close a connection or | |
561 | simply ignore any further requests on a connection. Closed | |
562 | connections are not detected until accept-process-output has updated | |
563 | the process-status. Dropped connections are not detected until the | |
564 | connection timeouts (which may be several minutes) or | |
565 | nntp-connection-timeout has expired. When these occur | |
566 | nntp-with-open-group, opens a new connection then re-issues the NNTP | |
567 | command whose response triggered the error." | |
568 | (when (and (listp connectionless) | |
569 | (not (eq connectionless nil))) | |
570 | (setq forms (cons connectionless forms) | |
571 | connectionless nil)) | |
572 | `(letf ((nntp-report-n (symbol-function 'nntp-report)) | |
573 | ((symbol-function 'nntp-report) (symbol-function 'nntp-report-1)) | |
574 | (nntp-with-open-group-internal nil)) | |
575 | (while (catch 'nntp-with-open-group-error | |
576 | ;; Open the connection to the server | |
577 | ;; NOTE: Existing connections are NOT tested. | |
578 | (nntp-possibly-change-group ,group ,server ,connectionless) | |
579 | ||
580 | (let ((timer | |
581 | (and nntp-connection-timeout | |
582 | (nnheader-run-at-time | |
583 | nntp-connection-timeout nil | |
584 | '(lambda () | |
585 | (let ((process (nntp-find-connection | |
586 | nntp-server-buffer)) | |
587 | (buffer (and process | |
588 | (process-buffer process)))) | |
589 | ;; When I an able to identify the | |
590 | ;; connection to the server AND I've | |
591 | ;; received NO reponse for | |
592 | ;; nntp-connection-timeout seconds. | |
593 | (when (and buffer (eq 0 (buffer-size buffer))) | |
594 | ;; Close the connection. Take no | |
595 | ;; other action as the accept input | |
596 | ;; code will handle the closed | |
597 | ;; connection. | |
598 | (nntp-kill-buffer buffer)))))))) | |
599 | (unwind-protect | |
600 | (setq nntp-with-open-group-internal | |
601 | (condition-case nil | |
602 | (progn ,@forms) | |
603 | (quit | |
604 | (nntp-close-server) | |
605 | (signal 'quit nil)))) | |
606 | (when timer | |
607 | (nnheader-cancel-timer timer))) | |
608 | nil)) | |
609 | (setf (symbol-function 'nntp-report) nntp-report-n)) | |
610 | nntp-with-open-group-internal)) | |
611 | ||
eec82323 LMI |
612 | (deffoo nntp-retrieve-headers (articles &optional group server fetch-old) |
613 | "Retrieve the headers of ARTICLES." | |
23f87bed MB |
614 | (nntp-with-open-group |
615 | group server | |
616 | (save-excursion | |
617 | (set-buffer (nntp-find-connection-buffer nntp-server-buffer)) | |
618 | (erase-buffer) | |
619 | (if (and (not gnus-nov-is-evil) | |
620 | (not nntp-nov-is-evil) | |
621 | (nntp-retrieve-headers-with-xover articles fetch-old)) | |
622 | ;; We successfully retrieved the headers via XOVER. | |
623 | 'nov | |
624 | ;; XOVER didn't work, so we do it the hard, slow and inefficient | |
625 | ;; way. | |
626 | (let ((number (length articles)) | |
627 | (articles articles) | |
628 | (count 0) | |
629 | (received 0) | |
630 | (last-point (point-min)) | |
631 | (buf (nntp-find-connection-buffer nntp-server-buffer)) | |
632 | (nntp-inhibit-erase t) | |
633 | article) | |
634 | ;; Send HEAD commands. | |
635 | (while (setq article (pop articles)) | |
636 | (nntp-send-command | |
637 | nil | |
638 | "HEAD" (if (numberp article) | |
639 | (int-to-string article) | |
640 | ;; `articles' is either a list of article numbers | |
641 | ;; or a list of article IDs. | |
642 | article)) | |
643 | (incf count) | |
644 | ;; Every 400 requests we have to read the stream in | |
645 | ;; order to avoid deadlocks. | |
646 | (when (or (null articles) ;All requests have been sent. | |
647 | (zerop (% count nntp-maximum-request))) | |
648 | (nntp-accept-response) | |
649 | (while (progn | |
650 | (set-buffer buf) | |
651 | (goto-char last-point) | |
652 | ;; Count replies. | |
653 | (while (nntp-next-result-arrived-p) | |
654 | (setq last-point (point)) | |
655 | (incf received)) | |
656 | (< received count)) | |
657 | ;; If number of headers is greater than 100, give | |
658 | ;; informative messages. | |
659 | (and (numberp nntp-large-newsgroup) | |
660 | (> number nntp-large-newsgroup) | |
661 | (zerop (% received 20)) | |
662 | (nnheader-message 6 "NNTP: Receiving headers... %d%%" | |
663 | (/ (* received 100) number))) | |
664 | (nntp-accept-response)))) | |
665 | (and (numberp nntp-large-newsgroup) | |
666 | (> number nntp-large-newsgroup) | |
667 | (nnheader-message 6 "NNTP: Receiving headers...done")) | |
668 | ||
669 | ;; Now all of replies are received. Fold continuation lines. | |
670 | (nnheader-fold-continuation-lines) | |
671 | ;; Remove all "\r"'s. | |
672 | (nnheader-strip-cr) | |
673 | (copy-to-buffer nntp-server-buffer (point-min) (point-max)) | |
674 | 'headers))))) | |
eec82323 LMI |
675 | |
676 | (deffoo nntp-retrieve-groups (groups &optional server) | |
677 | "Retrieve group info on GROUPS." | |
23f87bed MB |
678 | (nntp-with-open-group |
679 | nil server | |
680 | (when (nntp-find-connection-buffer nntp-server-buffer) | |
681 | (catch 'done | |
682 | (save-excursion | |
683 | ;; Erase nntp-server-buffer before nntp-inhibit-erase. | |
684 | (set-buffer nntp-server-buffer) | |
685 | (erase-buffer) | |
686 | (set-buffer (nntp-find-connection-buffer nntp-server-buffer)) | |
687 | ;; The first time this is run, this variable is `try'. So we | |
688 | ;; try. | |
689 | (when (eq nntp-server-list-active-group 'try) | |
690 | (nntp-try-list-active (car groups))) | |
691 | (erase-buffer) | |
692 | (let ((count 0) | |
693 | (groups groups) | |
694 | (received 0) | |
695 | (last-point (point-min)) | |
696 | (nntp-inhibit-erase t) | |
697 | (buf (nntp-find-connection-buffer nntp-server-buffer)) | |
698 | (command (if nntp-server-list-active-group | |
699 | "LIST ACTIVE" "GROUP"))) | |
700 | (while groups | |
701 | ;; Timeout may have killed the buffer. | |
702 | (unless (gnus-buffer-live-p buf) | |
703 | (nnheader-report 'nntp "Connection to %s is closed." server) | |
704 | (throw 'done nil)) | |
705 | ;; Send the command to the server. | |
706 | (nntp-send-command nil command (pop groups)) | |
707 | (incf count) | |
708 | ;; Every 400 requests we have to read the stream in | |
709 | ;; order to avoid deadlocks. | |
710 | (when (or (null groups) ;All requests have been sent. | |
711 | (zerop (% count nntp-maximum-request))) | |
712 | (nntp-accept-response) | |
713 | (while (and (gnus-buffer-live-p buf) | |
714 | (progn | |
715 | ;; Search `blue moon' in this file for the | |
716 | ;; reason why set-buffer here. | |
717 | (set-buffer buf) | |
718 | (goto-char last-point) | |
719 | ;; Count replies. | |
720 | (while (re-search-forward "^[0-9]" nil t) | |
721 | (incf received)) | |
722 | (setq last-point (point)) | |
723 | (< received count))) | |
724 | (nntp-accept-response)))) | |
725 | ||
726 | ;; Wait for the reply from the final command. | |
727 | (unless (gnus-buffer-live-p buf) | |
728 | (nnheader-report 'nntp "Connection to %s is closed." server) | |
729 | (throw 'done nil)) | |
730 | (set-buffer buf) | |
731 | (goto-char (point-max)) | |
732 | (re-search-backward "^[0-9]" nil t) | |
733 | (when (looking-at "^[23]") | |
734 | (while (and (gnus-buffer-live-p buf) | |
735 | (progn | |
736 | (set-buffer buf) | |
737 | (goto-char (point-max)) | |
738 | (if (not nntp-server-list-active-group) | |
739 | (not (re-search-backward "\r?\n" | |
740 | (- (point) 3) t)) | |
741 | (not (re-search-backward "^\\.\r?\n" | |
742 | (- (point) 4) t))))) | |
743 | (nntp-accept-response))) | |
744 | ||
745 | ;; Now all replies are received. We remove CRs. | |
746 | (unless (gnus-buffer-live-p buf) | |
747 | (nnheader-report 'nntp "Connection to %s is closed." server) | |
748 | (throw 'done nil)) | |
749 | (set-buffer buf) | |
750 | (goto-char (point-min)) | |
751 | (while (search-forward "\r" nil t) | |
752 | (replace-match "" t t)) | |
753 | ||
754 | (if (not nntp-server-list-active-group) | |
755 | (progn | |
756 | (copy-to-buffer nntp-server-buffer (point-min) (point-max)) | |
757 | 'group) | |
758 | ;; We have read active entries, so we just delete the | |
759 | ;; superfluous gunk. | |
760 | (goto-char (point-min)) | |
761 | (while (re-search-forward "^[.2-5]" nil t) | |
762 | (delete-region (match-beginning 0) | |
763 | (progn (forward-line 1) (point)))) | |
764 | (copy-to-buffer nntp-server-buffer (point-min) (point-max)) | |
765 | 'active))))))) | |
eec82323 LMI |
766 | |
767 | (deffoo nntp-retrieve-articles (articles &optional group server) | |
23f87bed MB |
768 | (nntp-with-open-group |
769 | group server | |
770 | (save-excursion | |
771 | (let ((number (length articles)) | |
772 | (articles articles) | |
773 | (count 0) | |
774 | (received 0) | |
775 | (last-point (point-min)) | |
776 | (buf (nntp-find-connection-buffer nntp-server-buffer)) | |
777 | (nntp-inhibit-erase t) | |
778 | (map (apply 'vector articles)) | |
779 | (point 1) | |
780 | article) | |
781 | (set-buffer buf) | |
782 | (erase-buffer) | |
783 | ;; Send ARTICLE command. | |
784 | (while (setq article (pop articles)) | |
785 | (nntp-send-command | |
786 | nil | |
787 | "ARTICLE" (if (numberp article) | |
788 | (int-to-string article) | |
789 | ;; `articles' is either a list of article numbers | |
790 | ;; or a list of article IDs. | |
791 | article)) | |
792 | (incf count) | |
793 | ;; Every 400 requests we have to read the stream in | |
794 | ;; order to avoid deadlocks. | |
795 | (when (or (null articles) ;All requests have been sent. | |
796 | (zerop (% count nntp-maximum-request))) | |
797 | (nntp-accept-response) | |
798 | (while (progn | |
799 | (set-buffer buf) | |
800 | (goto-char last-point) | |
801 | ;; Count replies. | |
802 | (while (nntp-next-result-arrived-p) | |
803 | (aset map received (cons (aref map received) (point))) | |
804 | (setq last-point (point)) | |
805 | (incf received)) | |
806 | (< received count)) | |
807 | ;; If number of headers is greater than 100, give | |
808 | ;; informative messages. | |
809 | (and (numberp nntp-large-newsgroup) | |
810 | (> number nntp-large-newsgroup) | |
811 | (zerop (% received 20)) | |
812 | (nnheader-message 6 "NNTP: Receiving articles... %d%%" | |
813 | (/ (* received 100) number))) | |
814 | (nntp-accept-response)))) | |
815 | (and (numberp nntp-large-newsgroup) | |
816 | (> number nntp-large-newsgroup) | |
817 | (nnheader-message 6 "NNTP: Receiving articles...done")) | |
818 | ||
819 | ;; Now we have all the responses. We go through the results, | |
820 | ;; wash it and copy it over to the server buffer. | |
821 | (set-buffer nntp-server-buffer) | |
822 | (erase-buffer) | |
823 | (setq last-point (point-min)) | |
824 | (mapcar | |
825 | (lambda (entry) | |
826 | (narrow-to-region | |
827 | (setq point (goto-char (point-max))) | |
828 | (progn | |
829 | (insert-buffer-substring buf last-point (cdr entry)) | |
830 | (point-max))) | |
831 | (setq last-point (cdr entry)) | |
832 | (nntp-decode-text) | |
833 | (widen) | |
834 | (cons (car entry) point)) | |
835 | map))))) | |
eec82323 | 836 | |
eec82323 LMI |
837 | (defun nntp-try-list-active (group) |
838 | (nntp-list-active-group group) | |
839 | (save-excursion | |
840 | (set-buffer nntp-server-buffer) | |
841 | (goto-char (point-min)) | |
842 | (cond ((or (eobp) | |
843 | (looking-at "5[0-9]+")) | |
844 | (setq nntp-server-list-active-group nil)) | |
845 | (t | |
846 | (setq nntp-server-list-active-group t))))) | |
847 | ||
848 | (deffoo nntp-list-active-group (group &optional server) | |
16409b0b | 849 | "Return the active info on GROUP (which can be a regexp)." |
23f87bed MB |
850 | (nntp-with-open-group |
851 | nil server | |
852 | (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group))) | |
16409b0b GM |
853 | |
854 | (deffoo nntp-request-group-articles (group &optional server) | |
855 | "Return the list of existing articles in GROUP." | |
23f87bed MB |
856 | (nntp-with-open-group |
857 | nil server | |
858 | (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group))) | |
eec82323 LMI |
859 | |
860 | (deffoo nntp-request-article (article &optional group server buffer command) | |
23f87bed MB |
861 | (nntp-with-open-group |
862 | group server | |
863 | (when (nntp-send-command-and-decode | |
864 | "\r?\n\\.\r?\n" "ARTICLE" | |
865 | (if (numberp article) (int-to-string article) article)) | |
866 | (if (and buffer | |
867 | (not (equal buffer nntp-server-buffer))) | |
868 | (save-excursion | |
869 | (set-buffer nntp-server-buffer) | |
870 | (copy-to-buffer buffer (point-min) (point-max)) | |
871 | (nntp-find-group-and-number group)) | |
872 | (nntp-find-group-and-number group))))) | |
eec82323 LMI |
873 | |
874 | (deffoo nntp-request-head (article &optional group server) | |
23f87bed MB |
875 | (nntp-with-open-group |
876 | group server | |
877 | (when (nntp-send-command | |
878 | "\r?\n\\.\r?\n" "HEAD" | |
879 | (if (numberp article) (int-to-string article) article)) | |
880 | (prog1 | |
881 | (nntp-find-group-and-number group) | |
882 | (nntp-decode-text))))) | |
eec82323 LMI |
883 | |
884 | (deffoo nntp-request-body (article &optional group server) | |
23f87bed MB |
885 | (nntp-with-open-group |
886 | group server | |
887 | (nntp-send-command-and-decode | |
888 | "\r?\n\\.\r?\n" "BODY" | |
889 | (if (numberp article) (int-to-string article) article)))) | |
eec82323 LMI |
890 | |
891 | (deffoo nntp-request-group (group &optional server dont-check) | |
23f87bed MB |
892 | (nntp-with-open-group |
893 | nil server | |
894 | (when (nntp-send-command "^[245].*\n" "GROUP" group) | |
895 | (let ((entry (nntp-find-connection-entry nntp-server-buffer))) | |
896 | (setcar (cddr entry) group))))) | |
eec82323 LMI |
897 | |
898 | (deffoo nntp-close-group (group &optional server) | |
899 | t) | |
900 | ||
901 | (deffoo nntp-server-opened (&optional server) | |
902 | "Say whether a connection to SERVER has been opened." | |
903 | (and (nnoo-current-server-p 'nntp server) | |
904 | nntp-server-buffer | |
905 | (gnus-buffer-live-p nntp-server-buffer) | |
906 | (nntp-find-connection nntp-server-buffer))) | |
907 | ||
908 | (deffoo nntp-open-server (server &optional defs connectionless) | |
909 | (nnheader-init-server-buffer) | |
910 | (if (nntp-server-opened server) | |
911 | t | |
912 | (when (or (stringp (car defs)) | |
913 | (numberp (car defs))) | |
914 | (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs)))) | |
915 | (unless (assq 'nntp-address defs) | |
916 | (setq defs (append defs (list (list 'nntp-address server))))) | |
917 | (nnoo-change-server 'nntp server defs) | |
918 | (unless connectionless | |
919 | (or (nntp-find-connection nntp-server-buffer) | |
920 | (nntp-open-connection nntp-server-buffer))))) | |
921 | ||
922 | (deffoo nntp-close-server (&optional server) | |
923 | (nntp-possibly-change-group nil server t) | |
6748645f LMI |
924 | (let ((process (nntp-find-connection nntp-server-buffer))) |
925 | (while process | |
eec82323 | 926 | (when (memq (process-status process) '(open run)) |
6748645f LMI |
927 | (ignore-errors |
928 | (nntp-send-string process "QUIT") | |
929 | (unless (eq nntp-open-connection-function 'nntp-open-network-stream) | |
930 | ;; Ok, this is evil, but when using telnet and stuff | |
931 | ;; as the connection method, it's important that the | |
932 | ;; QUIT command actually is sent out before we kill | |
933 | ;; the process. | |
934 | (sleep-for 1)))) | |
16409b0b | 935 | (nntp-kill-buffer (process-buffer process)) |
6748645f | 936 | (setq process (car (pop nntp-connection-alist)))) |
eec82323 LMI |
937 | (nnoo-close-server 'nntp))) |
938 | ||
939 | (deffoo nntp-request-close () | |
940 | (let (process) | |
941 | (while (setq process (pop nntp-connection-list)) | |
942 | (when (memq (process-status process) '(open run)) | |
eec82323 | 943 | (ignore-errors |
6748645f LMI |
944 | (nntp-send-string process "QUIT") |
945 | (unless (eq nntp-open-connection-function 'nntp-open-network-stream) | |
946 | ;; Ok, this is evil, but when using telnet and stuff | |
947 | ;; as the connection method, it's important that the | |
948 | ;; QUIT command actually is sent out before we kill | |
949 | ;; the process. | |
950 | (sleep-for 1)))) | |
16409b0b | 951 | (nntp-kill-buffer (process-buffer process))))) |
eec82323 LMI |
952 | |
953 | (deffoo nntp-request-list (&optional server) | |
23f87bed MB |
954 | (nntp-with-open-group |
955 | nil server | |
956 | (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST"))) | |
eec82323 LMI |
957 | |
958 | (deffoo nntp-request-list-newsgroups (&optional server) | |
23f87bed MB |
959 | (nntp-with-open-group |
960 | nil server | |
961 | (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS"))) | |
eec82323 LMI |
962 | |
963 | (deffoo nntp-request-newgroups (date &optional server) | |
23f87bed MB |
964 | (nntp-with-open-group |
965 | nil server | |
966 | (save-excursion | |
967 | (set-buffer nntp-server-buffer) | |
968 | (let* ((time (date-to-time date)) | |
969 | (ls (- (cadr time) (nth 8 (decode-time time))))) | |
970 | (cond ((< ls 0) | |
971 | (setcar time (1- (car time))) | |
972 | (setcar (cdr time) (+ ls 65536))) | |
973 | ((>= ls 65536) | |
974 | (setcar time (1+ (car time))) | |
975 | (setcar (cdr time) (- ls 65536))) | |
976 | (t | |
977 | (setcar (cdr time) ls))) | |
978 | (prog1 | |
979 | (nntp-send-command | |
980 | "^\\.\r?\n" "NEWGROUPS" | |
981 | (format-time-string "%y%m%d %H%M%S" time) | |
982 | "GMT") | |
983 | (nntp-decode-text)))))) | |
eec82323 LMI |
984 | |
985 | (deffoo nntp-request-post (&optional server) | |
23f87bed MB |
986 | (nntp-with-open-group |
987 | nil server | |
988 | (when (nntp-send-command "^[23].*\r?\n" "POST") | |
989 | (let ((response (with-current-buffer nntp-server-buffer | |
990 | nntp-process-response)) | |
991 | server-id) | |
992 | (when (and response | |
993 | (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)" | |
994 | response)) | |
995 | (setq server-id (match-string 1 response)) | |
996 | (narrow-to-region (goto-char (point-min)) | |
997 | (if (search-forward "\n\n" nil t) | |
998 | (1- (point)) | |
999 | (point-max))) | |
1000 | (unless (mail-fetch-field "Message-ID") | |
1001 | (goto-char (point-min)) | |
1002 | (insert "Message-ID: " server-id "\n")) | |
1003 | (widen)) | |
1004 | (run-hooks 'nntp-prepare-post-hook) | |
1005 | (nntp-send-buffer "^[23].*\n"))))) | |
eec82323 LMI |
1006 | |
1007 | (deffoo nntp-request-type (group article) | |
1008 | 'news) | |
1009 | ||
1010 | (deffoo nntp-asynchronous-p () | |
1011 | t) | |
1012 | ||
1013 | ;;; Hooky functions. | |
1014 | ||
1015 | (defun nntp-send-mode-reader () | |
1016 | "Send the MODE READER command to the nntp server. | |
1017 | This function is supposed to be called from `nntp-server-opened-hook'. | |
1018 | It will make innd servers spawn an nnrpd process to allow actual article | |
1019 | reading." | |
16409b0b | 1020 | (nntp-send-command "^.*\n" "MODE READER")) |
eec82323 | 1021 | |
6748645f | 1022 | (defun nntp-send-authinfo (&optional send-if-force) |
eec82323 | 1023 | "Send the AUTHINFO to the nntp server. |
6748645f LMI |
1024 | It will look in the \"~/.authinfo\" file for matching entries. If |
1025 | nothing suitable is found there, it will prompt for a user name | |
1026 | and a password. | |
1027 | ||
1028 | If SEND-IF-FORCE, only send authinfo to the server if the | |
1029 | .authinfo file has the FORCE token." | |
1030 | (let* ((list (gnus-parse-netrc nntp-authinfo-file)) | |
16409b0b | 1031 | (alist (gnus-netrc-machine list nntp-address "nntp")) |
6748645f LMI |
1032 | (force (gnus-netrc-get alist "force")) |
1033 | (user (or (gnus-netrc-get alist "login") nntp-authinfo-user)) | |
1034 | (passwd (gnus-netrc-get alist "password"))) | |
1035 | (when (or (not send-if-force) | |
1036 | force) | |
1037 | (unless user | |
1038 | (setq user (read-string (format "NNTP (%s) user name: " nntp-address)) | |
1039 | nntp-authinfo-user user)) | |
1040 | (unless (member user '(nil "")) | |
1041 | (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user) | |
1042 | (when t ;???Should check if AUTHINFO succeeded | |
16409b0b GM |
1043 | (nntp-send-command |
1044 | "^2.*\r?\n" "AUTHINFO PASS" | |
1045 | (or passwd | |
1046 | nntp-authinfo-password | |
1047 | (setq nntp-authinfo-password | |
23f87bed MB |
1048 | (read-passwd (format "NNTP (%s@%s) password: " |
1049 | user nntp-address)))))))))) | |
6748645f LMI |
1050 | |
1051 | (defun nntp-send-nosy-authinfo () | |
1052 | "Send the AUTHINFO to the nntp server." | |
1053 | (let ((user (read-string (format "NNTP (%s) user name: " nntp-address)))) | |
1054 | (unless (member user '(nil "")) | |
1055 | (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user) | |
1056 | (when t ;???Should check if AUTHINFO succeeded | |
1057 | (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS" | |
23f87bed MB |
1058 | (read-passwd (format "NNTP (%s@%s) password: " |
1059 | user nntp-address))))))) | |
eec82323 LMI |
1060 | |
1061 | (defun nntp-send-authinfo-from-file () | |
1062 | "Send the AUTHINFO to the nntp server. | |
6748645f LMI |
1063 | |
1064 | The authinfo login name is taken from the user's login name and the | |
1065 | password contained in '~/.nntp-authinfo'." | |
eec82323 | 1066 | (when (file-exists-p "~/.nntp-authinfo") |
16409b0b | 1067 | (with-temp-buffer |
eec82323 LMI |
1068 | (insert-file-contents "~/.nntp-authinfo") |
1069 | (goto-char (point-min)) | |
6748645f | 1070 | (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name)) |
eec82323 | 1071 | (nntp-send-command |
6748645f | 1072 | "^2.*\r?\n" "AUTHINFO PASS" |
23f87bed | 1073 | (buffer-substring (point) (gnus-point-at-eol)))))) |
eec82323 LMI |
1074 | |
1075 | ;;; Internal functions. | |
1076 | ||
6748645f LMI |
1077 | (defun nntp-handle-authinfo (process) |
1078 | "Take care of an authinfo response from the server." | |
1079 | (let ((last nntp-last-command)) | |
1080 | (funcall nntp-authinfo-function) | |
1081 | ;; We have to re-send the function that was interrupted by | |
1082 | ;; the authinfo request. | |
1083 | (save-excursion | |
1084 | (set-buffer nntp-server-buffer) | |
1085 | (erase-buffer)) | |
1086 | (nntp-send-string process last))) | |
1087 | ||
eec82323 LMI |
1088 | (defun nntp-make-process-buffer (buffer) |
1089 | "Create a new, fresh buffer usable for nntp process connections." | |
1090 | (save-excursion | |
1091 | (set-buffer | |
1092 | (generate-new-buffer | |
1093 | (format " *server %s %s %s*" | |
1094 | nntp-address nntp-port-number | |
6748645f | 1095 | (gnus-buffer-exists-p buffer)))) |
16409b0b | 1096 | (mm-enable-multibyte) |
eec82323 LMI |
1097 | (set (make-local-variable 'after-change-functions) nil) |
1098 | (set (make-local-variable 'nntp-process-wait-for) nil) | |
1099 | (set (make-local-variable 'nntp-process-callback) nil) | |
1100 | (set (make-local-variable 'nntp-process-to-buffer) nil) | |
1101 | (set (make-local-variable 'nntp-process-start-point) nil) | |
1102 | (set (make-local-variable 'nntp-process-decode) nil) | |
1103 | (current-buffer))) | |
1104 | ||
1105 | (defun nntp-open-connection (buffer) | |
1106 | "Open a connection to PORT on ADDRESS delivering output to BUFFER." | |
1107 | (run-hooks 'nntp-prepare-server-hook) | |
1108 | (let* ((pbuffer (nntp-make-process-buffer buffer)) | |
6748645f LMI |
1109 | (timer |
1110 | (and nntp-connection-timeout | |
1111 | (nnheader-run-at-time | |
1112 | nntp-connection-timeout nil | |
1113 | `(lambda () | |
16409b0b | 1114 | (nntp-kill-buffer ,pbuffer))))) |
eec82323 LMI |
1115 | (process |
1116 | (condition-case () | |
ccaab511 | 1117 | (let ((coding-system-for-read nntp-coding-system-for-read) |
23f87bed | 1118 | (coding-system-for-write nntp-coding-system-for-write)) |
fc46309d | 1119 | (funcall nntp-open-connection-function pbuffer)) |
eec82323 | 1120 | (error nil) |
2eebe218 DL |
1121 | (quit |
1122 | (message "Quit opening connection") | |
1123 | (nntp-kill-buffer pbuffer) | |
1124 | (signal 'quit nil) | |
1125 | nil)))) | |
6748645f LMI |
1126 | (when timer |
1127 | (nnheader-cancel-timer timer)) | |
23f87bed MB |
1128 | (unless process |
1129 | (nntp-kill-buffer pbuffer)) | |
6748645f LMI |
1130 | (when (and (buffer-name pbuffer) |
1131 | process) | |
4a43ee9b | 1132 | (gnus-set-process-query-on-exit-flag process nil) |
23f87bed MB |
1133 | (if (and (nntp-wait-for process "^2.*\n" buffer nil t) |
1134 | (memq (process-status process) '(open run))) | |
eec82323 LMI |
1135 | (prog1 |
1136 | (caar (push (list process buffer nil) nntp-connection-alist)) | |
1137 | (push process nntp-connection-list) | |
1138 | (save-excursion | |
1139 | (set-buffer pbuffer) | |
1140 | (nntp-read-server-type) | |
1141 | (erase-buffer) | |
1142 | (set-buffer nntp-server-buffer) | |
1143 | (let ((nnheader-callback-function nil)) | |
6748645f LMI |
1144 | (run-hooks 'nntp-server-opened-hook) |
1145 | (nntp-send-authinfo t)))) | |
16409b0b | 1146 | (nntp-kill-buffer (process-buffer process)) |
eec82323 LMI |
1147 | nil)))) |
1148 | ||
1149 | (defun nntp-open-network-stream (buffer) | |
1150 | (open-network-stream "nntpd" buffer nntp-address nntp-port-number)) | |
1151 | ||
8654e13a MB |
1152 | (eval-and-compile |
1153 | (autoload 'format-spec "format-spec") | |
1154 | (autoload 'format-spec-make "format-spec") | |
1155 | (autoload 'open-tls-stream "tls")) | |
23f87bed | 1156 | |
6748645f | 1157 | (defun nntp-open-ssl-stream (buffer) |
f9936da6 | 1158 | (let* ((process-connection-type nil) |
23f87bed | 1159 | (proc (start-process "nntpd" buffer |
f9936da6 SZ |
1160 | shell-file-name |
1161 | shell-command-switch | |
23f87bed | 1162 | (format-spec nntp-ssl-program |
f9936da6 SZ |
1163 | (format-spec-make |
1164 | ?s nntp-address | |
1165 | ?p nntp-port-number))))) | |
4a43ee9b | 1166 | (gnus-set-process-query-on-exit-flag proc nil) |
6748645f LMI |
1167 | (save-excursion |
1168 | (set-buffer buffer) | |
23f87bed MB |
1169 | (let ((nntp-connection-alist (list proc buffer nil))) |
1170 | (nntp-wait-for-string "^\r*20[01]")) | |
1171 | (beginning-of-line) | |
1172 | (delete-region (point-min) (point)) | |
1173 | proc))) | |
1174 | ||
1175 | (defun nntp-open-tls-stream (buffer) | |
1176 | (let ((proc (open-tls-stream "nntpd" buffer nntp-address nntp-port-number))) | |
4a43ee9b | 1177 | (gnus-set-process-query-on-exit-flag proc nil) |
23f87bed MB |
1178 | (save-excursion |
1179 | (set-buffer buffer) | |
1180 | (let ((nntp-connection-alist (list proc buffer nil))) | |
1181 | (nntp-wait-for-string "^\r*20[01]")) | |
6748645f LMI |
1182 | (beginning-of-line) |
1183 | (delete-region (point-min) (point)) | |
1184 | proc))) | |
1185 | ||
eec82323 LMI |
1186 | (defun nntp-read-server-type () |
1187 | "Find out what the name of the server we have connected to is." | |
1188 | ;; Wait for the status string to arrive. | |
1189 | (setq nntp-server-type (buffer-string)) | |
1190 | (let ((alist nntp-server-action-alist) | |
1191 | (case-fold-search t) | |
1192 | entry) | |
1193 | ;; Run server-specific commands. | |
1194 | (while alist | |
1195 | (setq entry (pop alist)) | |
1196 | (when (string-match (car entry) nntp-server-type) | |
1197 | (if (and (listp (cadr entry)) | |
1198 | (not (eq 'lambda (caadr entry)))) | |
1199 | (eval (cadr entry)) | |
1200 | (funcall (cadr entry))))))) | |
1201 | ||
16409b0b GM |
1202 | (defun nntp-async-wait (process wait-for buffer decode callback) |
1203 | (save-excursion | |
1204 | (set-buffer (process-buffer process)) | |
1205 | (unless nntp-inside-change-function | |
1206 | (erase-buffer)) | |
1207 | (setq nntp-process-wait-for wait-for | |
1208 | nntp-process-to-buffer buffer | |
1209 | nntp-process-decode decode | |
1210 | nntp-process-callback callback | |
1211 | nntp-process-start-point (point-max)) | |
1212 | (setq after-change-functions '(nntp-after-change-function)) | |
1213 | (if nntp-async-needs-kluge | |
1214 | (nntp-async-kluge process)))) | |
1215 | ||
1216 | (defun nntp-async-kluge (process) | |
1217 | ;; emacs 20.3 bug: process output with encoding 'binary | |
1218 | ;; doesn't trigger after-change-functions. | |
1219 | (unless nntp-async-timer | |
1220 | (setq nntp-async-timer | |
1221 | (nnheader-run-at-time 1 1 'nntp-async-timer-handler))) | |
1222 | (add-to-list 'nntp-async-process-list process)) | |
1223 | ||
1224 | (defun nntp-async-timer-handler () | |
1225 | (mapcar | |
1226 | (lambda (proc) | |
1227 | (if (memq (process-status proc) '(open run)) | |
1228 | (nntp-async-trigger proc) | |
1229 | (nntp-async-stop proc))) | |
1230 | nntp-async-process-list)) | |
1231 | ||
1232 | (defun nntp-async-stop (proc) | |
1233 | (setq nntp-async-process-list (delq proc nntp-async-process-list)) | |
1234 | (when (and nntp-async-timer (not nntp-async-process-list)) | |
1235 | (nnheader-cancel-timer nntp-async-timer) | |
1236 | (setq nntp-async-timer nil))) | |
1237 | ||
1238 | (defun nntp-after-change-function (beg end len) | |
1239 | (unwind-protect | |
1240 | ;; we only care about insertions at eob | |
1241 | (when (and (eq 0 len) (eq (point-max) end)) | |
1242 | (save-match-data | |
1243 | (let ((proc (get-buffer-process (current-buffer)))) | |
1244 | (when proc | |
1245 | (nntp-async-trigger proc))))) | |
1246 | ;; any throw from after-change-functions will leave it | |
1247 | ;; set to nil. so we reset it here, if necessary. | |
1248 | (when quit-flag | |
1249 | (setq after-change-functions '(nntp-after-change-function))))) | |
1250 | ||
1251 | (defun nntp-async-trigger (process) | |
1252 | (save-excursion | |
1253 | (set-buffer (process-buffer process)) | |
1254 | (when nntp-process-callback | |
1255 | ;; do we have an error message? | |
1256 | (goto-char nntp-process-start-point) | |
1257 | (if (memq (following-char) '(?4 ?5)) | |
1258 | ;; wants credentials? | |
1259 | (if (looking-at "480") | |
2eebe218 | 1260 | (nntp-handle-authinfo process) |
16409b0b GM |
1261 | ;; report error message. |
1262 | (nntp-snarf-error-message) | |
1263 | (nntp-do-callback nil)) | |
1264 | ||
1265 | ;; got what we expect? | |
1266 | (goto-char (point-max)) | |
1267 | (when (re-search-backward | |
1268 | nntp-process-wait-for nntp-process-start-point t) | |
23f87bed MB |
1269 | (let ((response (match-string 0))) |
1270 | (with-current-buffer nntp-server-buffer | |
1271 | (setq nntp-process-response response))) | |
16409b0b GM |
1272 | (nntp-async-stop process) |
1273 | ;; convert it. | |
6748645f | 1274 | (when (gnus-buffer-exists-p nntp-process-to-buffer) |
16409b0b GM |
1275 | (let ((buf (current-buffer)) |
1276 | (start nntp-process-start-point) | |
1277 | (decode nntp-process-decode)) | |
eec82323 | 1278 | (save-excursion |
6748645f | 1279 | (set-buffer nntp-process-to-buffer) |
eec82323 | 1280 | (goto-char (point-max)) |
16409b0b GM |
1281 | (save-restriction |
1282 | (narrow-to-region (point) (point)) | |
1283 | (insert-buffer-substring buf start) | |
1284 | (when decode | |
1285 | (nntp-decode-text)))))) | |
1286 | ;; report it. | |
1287 | (goto-char (point-max)) | |
1288 | (nntp-do-callback | |
1289 | (buffer-name (get-buffer nntp-process-to-buffer)))))))) | |
1290 | ||
1291 | (defun nntp-do-callback (arg) | |
1292 | (let ((callback nntp-process-callback) | |
1293 | (nntp-inside-change-function t)) | |
1294 | (setq nntp-process-callback nil) | |
1295 | (funcall callback arg))) | |
eec82323 LMI |
1296 | |
1297 | (defun nntp-snarf-error-message () | |
1298 | "Save the error message in the current buffer." | |
1299 | (let ((message (buffer-string))) | |
1300 | (while (string-match "[\r\n]+" message) | |
1301 | (setq message (replace-match " " t t message))) | |
1302 | (nnheader-report 'nntp message) | |
1303 | message)) | |
1304 | ||
23f87bed | 1305 | (defun nntp-accept-process-output (process) |
eec82323 LMI |
1306 | "Wait for output from PROCESS and message some dots." |
1307 | (save-excursion | |
1308 | (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer) | |
1309 | nntp-server-buffer)) | |
c7279817 | 1310 | (let ((len (/ (buffer-size) 1024)) |
eec82323 LMI |
1311 | message-log-max) |
1312 | (unless (< len 10) | |
1313 | (setq nntp-have-messaged t) | |
1314 | (nnheader-message 7 "nntp read: %dk" len))) | |
23f87bed MB |
1315 | (nnheader-accept-process-output process) |
1316 | ;; accept-process-output may update status of process to indicate | |
1317 | ;; that the server has closed the connection. This MUST be | |
1318 | ;; handled here as the buffer restored by the save-excursion may | |
1319 | ;; be the process's former output buffer (i.e. now killed) | |
1320 | (or (and process | |
1321 | (memq (process-status process) '(open run))) | |
1322 | (nntp-report "Server closed connection")))) | |
eec82323 LMI |
1323 | |
1324 | (defun nntp-accept-response () | |
1325 | "Wait for output from the process that outputs to BUFFER." | |
1326 | (nntp-accept-process-output (nntp-find-connection nntp-server-buffer))) | |
1327 | ||
1328 | (defun nntp-possibly-change-group (group server &optional connectionless) | |
1329 | (let ((nnheader-callback-function nil)) | |
1330 | (when server | |
1331 | (or (nntp-server-opened server) | |
1332 | (nntp-open-server server nil connectionless))) | |
1333 | ||
1334 | (unless connectionless | |
1335 | (or (nntp-find-connection nntp-server-buffer) | |
1336 | (nntp-open-connection nntp-server-buffer)))) | |
1337 | ||
1338 | (when group | |
1339 | (let ((entry (nntp-find-connection-entry nntp-server-buffer))) | |
23f87bed MB |
1340 | (cond ((not entry) |
1341 | (nntp-report "Server closed connection")) | |
1342 | ((not (equal group (caddr entry))) | |
1343 | (save-excursion | |
1344 | (set-buffer (process-buffer (car entry))) | |
1345 | (erase-buffer) | |
1346 | (nntp-send-command "^[245].*\n" "GROUP" group) | |
1347 | (setcar (cddr entry) group) | |
1348 | (erase-buffer) | |
1349 | (save-excursion | |
1350 | (set-buffer nntp-server-buffer) | |
1351 | (erase-buffer)))))))) | |
eec82323 LMI |
1352 | |
1353 | (defun nntp-decode-text (&optional cr-only) | |
1354 | "Decode the text in the current buffer." | |
1355 | (goto-char (point-min)) | |
1356 | (while (search-forward "\r" nil t) | |
1357 | (delete-char -1)) | |
1358 | (unless cr-only | |
1359 | ;; Remove trailing ".\n" end-of-transfer marker. | |
1360 | (goto-char (point-max)) | |
1361 | (forward-line -1) | |
1362 | (when (looking-at ".\n") | |
1363 | (delete-char 2)) | |
1364 | ;; Delete status line. | |
1365 | (goto-char (point-min)) | |
2eebe218 DL |
1366 | (while (looking-at "[1-5][0-9][0-9] .*\n") |
1367 | ;; For some unknown reason, there is more than one status line. | |
1368 | (delete-region (point) (progn (forward-line 1) (point)))) | |
eec82323 LMI |
1369 | ;; Remove "." -> ".." encoding. |
1370 | (while (search-forward "\n.." nil t) | |
1371 | (delete-char -1)))) | |
1372 | ||
1373 | (defun nntp-encode-text () | |
1374 | "Encode the text in the current buffer." | |
1375 | (save-excursion | |
1376 | ;; Replace "." at beginning of line with "..". | |
1377 | (goto-char (point-min)) | |
1378 | (while (re-search-forward "^\\." nil t) | |
1379 | (insert ".")) | |
1380 | (goto-char (point-max)) | |
1381 | ;; Insert newline at the end of the buffer. | |
1382 | (unless (bolp) | |
1383 | (insert "\n")) | |
1384 | ;; Insert `.' at end of buffer (end of text mark). | |
1385 | (goto-char (point-max)) | |
45926d06 RS |
1386 | (insert ".\n") |
1387 | (goto-char (point-min)) | |
1388 | (while (not (eobp)) | |
1389 | (end-of-line) | |
1390 | (delete-char 1) | |
1391 | (insert nntp-end-of-line)))) | |
eec82323 LMI |
1392 | |
1393 | (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old) | |
1394 | (set-buffer nntp-server-buffer) | |
1395 | (erase-buffer) | |
1396 | (cond | |
1397 | ||
1398 | ;; This server does not talk NOV. | |
1399 | ((not nntp-server-xover) | |
1400 | nil) | |
1401 | ||
1402 | ;; We don't care about gaps. | |
1403 | ((or (not nntp-nov-gap) | |
1404 | fetch-old) | |
1405 | (nntp-send-xover-command | |
1406 | (if fetch-old | |
1407 | (if (numberp fetch-old) | |
1408 | (max 1 (- (car articles) fetch-old)) | |
1409 | 1) | |
1410 | (car articles)) | |
1411 | (car (last articles)) 'wait) | |
1412 | ||
1413 | (goto-char (point-min)) | |
16409b0b | 1414 | (when (looking-at "[1-5][0-9][0-9] .*\n") |
eec82323 LMI |
1415 | (delete-region (point) (progn (forward-line 1) (point)))) |
1416 | (while (search-forward "\r" nil t) | |
1417 | (replace-match "" t t)) | |
1418 | (goto-char (point-max)) | |
1419 | (forward-line -1) | |
1420 | (when (looking-at "\\.") | |
1421 | (delete-region (point) (progn (forward-line 1) (point))))) | |
1422 | ||
1423 | ;; We do it the hard way. For each gap, an XOVER command is sent | |
1424 | ;; to the server. We do not wait for a reply from the server, we | |
1425 | ;; just send them off as fast as we can. That means that we have | |
1426 | ;; to count the number of responses we get back to find out when we | |
1427 | ;; have gotten all we asked for. | |
1428 | ((numberp nntp-nov-gap) | |
1429 | (let ((count 0) | |
1430 | (received 0) | |
16409b0b GM |
1431 | last-point |
1432 | in-process-buffer-p | |
eec82323 | 1433 | (buf nntp-server-buffer) |
16409b0b | 1434 | (process-buffer (nntp-find-connection-buffer nntp-server-buffer)) |
23f87bed | 1435 | first last status) |
eec82323 LMI |
1436 | ;; We have to check `nntp-server-xover'. If it gets set to nil, |
1437 | ;; that means that the server does not understand XOVER, but we | |
1438 | ;; won't know that until we try. | |
1439 | (while (and nntp-server-xover articles) | |
1440 | (setq first (car articles)) | |
1441 | ;; Search forward until we find a gap, or until we run out of | |
1442 | ;; articles. | |
1443 | (while (and (cdr articles) | |
1444 | (< (- (nth 1 articles) (car articles)) nntp-nov-gap)) | |
1445 | (setq articles (cdr articles))) | |
1446 | ||
16409b0b | 1447 | (setq in-process-buffer-p (stringp nntp-server-xover)) |
23f87bed MB |
1448 | (nntp-send-xover-command first (setq last (car articles))) |
1449 | (setq articles (cdr articles)) | |
a1506d29 | 1450 | |
16409b0b GM |
1451 | (when (and nntp-server-xover in-process-buffer-p) |
1452 | ;; Don't count tried request. | |
1453 | (setq count (1+ count)) | |
a1506d29 | 1454 | |
eec82323 LMI |
1455 | ;; Every 400 requests we have to read the stream in |
1456 | ;; order to avoid deadlocks. | |
1457 | (when (or (null articles) ;All requests have been sent. | |
23f87bed | 1458 | (= 1 (% count nntp-maximum-request))) |
16409b0b GM |
1459 | |
1460 | (nntp-accept-response) | |
1461 | ;; On some Emacs versions the preceding function has a | |
1462 | ;; tendency to change the buffer. Perhaps. It's quite | |
1463 | ;; difficult to reproduce, because it only seems to happen | |
1464 | ;; once in a blue moon. | |
1465 | (set-buffer process-buffer) | |
eec82323 | 1466 | (while (progn |
16409b0b | 1467 | (goto-char (or last-point (point-min))) |
eec82323 | 1468 | ;; Count replies. |
23f87bed MB |
1469 | (while (re-search-forward "^\\([0-9][0-9][0-9]\\) .*\n" |
1470 | nil t) | |
1471 | (incf received) | |
1472 | (setq status (match-string 1)) | |
1473 | (if (string-match "^[45]" status) | |
1474 | (setq status 'error) | |
1475 | (setq status 'ok))) | |
eec82323 | 1476 | (setq last-point (point)) |
23f87bed MB |
1477 | (or (< received count) |
1478 | (if (eq status 'error) | |
1479 | nil | |
1480 | ;; I haven't started reading the final response | |
1481 | (progn | |
1482 | (goto-char (point-max)) | |
1483 | (forward-line -1) | |
1484 | (not (looking-at "^\\.\r?\n")))))) | |
1485 | ;; I haven't read the end of the final response | |
16409b0b | 1486 | (nntp-accept-response) |
23f87bed MB |
1487 | (set-buffer process-buffer)))) |
1488 | ||
1489 | ;; Some nntp servers seem to have an extension to the XOVER | |
1490 | ;; extension. On these servers, requesting an article range | |
1491 | ;; preceeding the active range does not return an error as | |
1492 | ;; specified in the RFC. What we instead get is the NOV entry | |
1493 | ;; for the first available article. Obviously, a client can | |
1494 | ;; use that entry to avoid making unnecessary requests. The | |
1495 | ;; only problem is for a client that assumes that the response | |
1496 | ;; will always be within the requested ranage. For such a | |
1497 | ;; client, we can get N copies of the same entry (one for each | |
1498 | ;; XOVER command sent to the server). | |
1499 | ||
1500 | (when (<= count 1) | |
1501 | (goto-char (point-min)) | |
1502 | (when (re-search-forward "^[0-9][0-9][0-9] .*\n\\([0-9]+\\)" nil t) | |
e9bd5782 | 1503 | (let ((low-limit (string-to-number |
23f87bed MB |
1504 | (buffer-substring (match-beginning 1) |
1505 | (match-end 1))))) | |
1506 | (while (and articles (<= (car articles) low-limit)) | |
1507 | (setq articles (cdr articles)))))) | |
1508 | (set-buffer buf)) | |
eec82323 LMI |
1509 | |
1510 | (when nntp-server-xover | |
16409b0b | 1511 | (when in-process-buffer-p |
16409b0b GM |
1512 | (set-buffer buf) |
1513 | (goto-char (point-max)) | |
1514 | (insert-buffer-substring process-buffer) | |
1515 | (set-buffer process-buffer) | |
1516 | (erase-buffer) | |
1517 | (set-buffer buf)) | |
eec82323 LMI |
1518 | |
1519 | ;; We remove any "." lines and status lines. | |
1520 | (goto-char (point-min)) | |
1521 | (while (search-forward "\r" nil t) | |
1522 | (delete-char -1)) | |
1523 | (goto-char (point-min)) | |
1524 | (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ") | |
eec82323 LMI |
1525 | t)))) |
1526 | ||
1527 | nntp-server-xover) | |
1528 | ||
1529 | (defun nntp-send-xover-command (beg end &optional wait-for-reply) | |
1530 | "Send the XOVER command to the server." | |
1531 | (let ((range (format "%d-%d" beg end)) | |
1532 | (nntp-inhibit-erase t)) | |
1533 | (if (stringp nntp-server-xover) | |
1534 | ;; If `nntp-server-xover' is a string, then we just send this | |
1535 | ;; command. | |
1536 | (if wait-for-reply | |
1537 | (nntp-send-command-nodelete | |
1538 | "\r?\n\\.\r?\n" nntp-server-xover range) | |
1539 | ;; We do not wait for the reply. | |
16409b0b | 1540 | (nntp-send-command-nodelete nil nntp-server-xover range)) |
eec82323 LMI |
1541 | (let ((commands nntp-xover-commands)) |
1542 | ;; `nntp-xover-commands' is a list of possible XOVER commands. | |
1543 | ;; We try them all until we get at positive response. | |
1544 | (while (and commands (eq nntp-server-xover 'try)) | |
1545 | (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range) | |
1546 | (save-excursion | |
1547 | (set-buffer nntp-server-buffer) | |
1548 | (goto-char (point-min)) | |
1549 | (and (looking-at "[23]") ; No error message. | |
1550 | ;; We also have to look at the lines. Some buggy | |
1551 | ;; servers give back simple lines with just the | |
1552 | ;; article number. How... helpful. | |
1553 | (progn | |
1554 | (forward-line 1) | |
1555 | (looking-at "[0-9]+\t...")) ; More text after number. | |
1556 | (setq nntp-server-xover (car commands)))) | |
1557 | (setq commands (cdr commands))) | |
1558 | ;; If none of the commands worked, we disable XOVER. | |
1559 | (when (eq nntp-server-xover 'try) | |
1560 | (save-excursion | |
1561 | (set-buffer nntp-server-buffer) | |
1562 | (erase-buffer) | |
1563 | (setq nntp-server-xover nil))) | |
23f87bed | 1564 | nntp-server-xover)))) |
eec82323 | 1565 | |
23f87bed MB |
1566 | (defun nntp-find-group-and-number (&optional group) |
1567 | (save-excursion | |
1568 | (save-restriction | |
1569 | (set-buffer nntp-server-buffer) | |
1570 | (narrow-to-region (goto-char (point-min)) | |
1571 | (or (search-forward "\n\n" nil t) (point-max))) | |
1572 | (goto-char (point-min)) | |
1573 | ;; We first find the number by looking at the status line. | |
1574 | (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ") | |
e9bd5782 | 1575 | (string-to-number |
23f87bed MB |
1576 | (buffer-substring (match-beginning 1) |
1577 | (match-end 1))))) | |
1578 | newsgroups xref) | |
1579 | (and number (zerop number) (setq number nil)) | |
1580 | (if number | |
1581 | ;; Then we find the group name. | |
1582 | (setq group | |
1583 | (cond | |
1584 | ;; If there is only one group in the Newsgroups | |
1585 | ;; header, then it seems quite likely that this | |
1586 | ;; article comes from that group, I'd say. | |
1587 | ((and (setq newsgroups | |
1588 | (mail-fetch-field "newsgroups")) | |
1589 | (not (string-match "," newsgroups))) | |
1590 | newsgroups) | |
1591 | ;; If there is more than one group in the | |
1592 | ;; Newsgroups header, then the Xref header should | |
1593 | ;; be filled out. We hazard a guess that the group | |
1594 | ;; that has this article number in the Xref header | |
1595 | ;; is the one we are looking for. This might very | |
1596 | ;; well be wrong if this article happens to have | |
1597 | ;; the same number in several groups, but that's | |
1598 | ;; life. | |
1599 | ((and (setq xref (mail-fetch-field "xref")) | |
1600 | number | |
1601 | (string-match | |
1602 | (format "\\([^ :]+\\):%d" number) xref)) | |
1603 | (match-string 1 xref)) | |
1604 | (t ""))) | |
1605 | (cond | |
1606 | ((and (setq xref (mail-fetch-field "xref")) | |
1607 | (string-match | |
1608 | (if group | |
1609 | (concat "\\(" (regexp-quote group) "\\):\\([0-9]+\\)") | |
1610 | "\\([^ :]+\\):\\([0-9]+\\)") | |
1611 | xref)) | |
1612 | (setq group (match-string 1 xref) | |
e9bd5782 | 1613 | number (string-to-number (match-string 2 xref)))) |
23f87bed MB |
1614 | ((and (setq newsgroups |
1615 | (mail-fetch-field "newsgroups")) | |
1616 | (not (string-match "," newsgroups))) | |
1617 | (setq group newsgroups)) | |
1618 | (group) | |
1619 | (t (setq group "")))) | |
1620 | (when (string-match "\r" group) | |
1621 | (setq group (substring group 0 (match-beginning 0)))) | |
1622 | (cons group number))))) | |
eec82323 LMI |
1623 | |
1624 | (defun nntp-wait-for-string (regexp) | |
1625 | "Wait until string arrives in the buffer." | |
23f87bed MB |
1626 | (let ((buf (current-buffer)) |
1627 | proc) | |
eec82323 | 1628 | (goto-char (point-min)) |
23f87bed MB |
1629 | (while (and (setq proc (get-buffer-process buf)) |
1630 | (memq (process-status proc) '(open run)) | |
1631 | (not (re-search-forward regexp nil t))) | |
1632 | (accept-process-output proc) | |
eec82323 LMI |
1633 | (set-buffer buf) |
1634 | (goto-char (point-min))))) | |
1635 | ||
23f87bed MB |
1636 | |
1637 | ;; ========================================================================== | |
1638 | ;; Obsolete nntp-open-* connection methods -- drv | |
1639 | ;; ========================================================================== | |
1640 | ||
1641 | (defvoo nntp-open-telnet-envuser nil | |
1642 | "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.") | |
1643 | ||
1644 | (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?" | |
1645 | "*Regular expression to match the shell prompt on the remote machine.") | |
1646 | ||
1647 | (defvoo nntp-rlogin-program "rsh" | |
1648 | "*Program used to log in on remote machines. | |
1649 | The default is \"rsh\", but \"ssh\" is a popular alternative.") | |
1650 | ||
1651 | (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp") | |
1652 | "*Parameters to `nntp-open-rlogin'. | |
1653 | That function may be used as `nntp-open-connection-function'. In that | |
1654 | case, this list will be used as the parameter list given to rsh.") | |
1655 | ||
1656 | (defvoo nntp-rlogin-user-name nil | |
1657 | "*User name on remote system when using the rlogin connect method.") | |
1658 | ||
1659 | (defvoo nntp-telnet-parameters | |
1660 | '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp") | |
1661 | "*Parameters to `nntp-open-telnet'. | |
1662 | That function may be used as `nntp-open-connection-function'. In that | |
1663 | case, this list will be executed as a command after logging in | |
1664 | via telnet.") | |
1665 | ||
1666 | (defvoo nntp-telnet-user-name nil | |
1667 | "User name to log in via telnet with.") | |
1668 | ||
1669 | (defvoo nntp-telnet-passwd nil | |
1670 | "Password to use to log in via telnet with.") | |
1671 | ||
eec82323 LMI |
1672 | (defun nntp-open-telnet (buffer) |
1673 | (save-excursion | |
1674 | (set-buffer buffer) | |
1675 | (erase-buffer) | |
a8151ef7 LMI |
1676 | (let ((proc (apply |
1677 | 'start-process | |
1678 | "nntpd" buffer nntp-telnet-command nntp-telnet-switches)) | |
eec82323 LMI |
1679 | (case-fold-search t)) |
1680 | (when (memq (process-status proc) '(open run)) | |
2eebe218 | 1681 | (nntp-wait-for-string "^r?telnet") |
eec82323 | 1682 | (process-send-string proc "set escape \^X\n") |
6748645f LMI |
1683 | (cond |
1684 | ((and nntp-open-telnet-envuser nntp-telnet-user-name) | |
1685 | (process-send-string proc (concat "open " "-l" nntp-telnet-user-name | |
1686 | nntp-address "\n"))) | |
1687 | (t | |
1688 | (process-send-string proc (concat "open " nntp-address "\n")))) | |
1689 | (cond | |
1690 | ((not nntp-open-telnet-envuser) | |
1691 | (nntp-wait-for-string "^\r*.?login:") | |
1692 | (process-send-string | |
1693 | proc (concat | |
1694 | (or nntp-telnet-user-name | |
1695 | (setq nntp-telnet-user-name (read-string "login: "))) | |
1696 | "\n")))) | |
eec82323 LMI |
1697 | (nntp-wait-for-string "^\r*.?password:") |
1698 | (process-send-string | |
1699 | proc (concat | |
1700 | (or nntp-telnet-passwd | |
1701 | (setq nntp-telnet-passwd | |
23f87bed | 1702 | (read-passwd "Password: "))) |
eec82323 | 1703 | "\n")) |
6748645f | 1704 | (nntp-wait-for-string nntp-telnet-shell-prompt) |
eec82323 LMI |
1705 | (process-send-string |
1706 | proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n")) | |
6748645f | 1707 | (nntp-wait-for-string "^\r*20[01]") |
eec82323 LMI |
1708 | (beginning-of-line) |
1709 | (delete-region (point-min) (point)) | |
1710 | (process-send-string proc "\^]") | |
2eebe218 | 1711 | (nntp-wait-for-string "^r?telnet") |
eec82323 LMI |
1712 | (process-send-string proc "mode character\n") |
1713 | (accept-process-output proc 1) | |
1714 | (sit-for 1) | |
1715 | (goto-char (point-min)) | |
1716 | (forward-line 1) | |
1717 | (delete-region (point) (point-max))) | |
1718 | proc))) | |
1719 | ||
1720 | (defun nntp-open-rlogin (buffer) | |
1721 | "Open a connection to SERVER using rsh." | |
1722 | (let ((proc (if nntp-rlogin-user-name | |
6748645f LMI |
1723 | (apply 'start-process |
1724 | "nntpd" buffer nntp-rlogin-program | |
1725 | nntp-address "-l" nntp-rlogin-user-name | |
1726 | nntp-rlogin-parameters) | |
1727 | (apply 'start-process | |
1728 | "nntpd" buffer nntp-rlogin-program nntp-address | |
1729 | nntp-rlogin-parameters)))) | |
1730 | (save-excursion | |
1731 | (set-buffer buffer) | |
1732 | (nntp-wait-for-string "^\r*20[01]") | |
1733 | (beginning-of-line) | |
1734 | (delete-region (point-min) (point)) | |
1735 | proc))) | |
eec82323 | 1736 | |
23f87bed MB |
1737 | |
1738 | ;; ========================================================================== | |
1739 | ;; Replacements for the nntp-open-* functions -- drv | |
1740 | ;; ========================================================================== | |
1741 | ||
1742 | (defun nntp-open-telnet-stream (buffer) | |
1743 | "Open a nntp connection by telnet'ing the news server. | |
1744 | ||
1745 | Please refer to the following variables to customize the connection: | |
1746 | - `nntp-pre-command', | |
1747 | - `nntp-telnet-command', | |
1748 | - `nntp-telnet-switches', | |
1749 | - `nntp-address', | |
1750 | - `nntp-port-number', | |
1751 | - `nntp-end-of-line'." | |
1752 | (let ((command `(,nntp-telnet-command | |
1753 | ,@nntp-telnet-switches | |
1754 | ,nntp-address ,nntp-port-number)) | |
1755 | proc) | |
1756 | (and nntp-pre-command | |
1757 | (push nntp-pre-command command)) | |
1758 | (setq proc (apply 'start-process "nntpd" buffer command)) | |
1759 | (save-excursion | |
1760 | (set-buffer buffer) | |
1761 | (nntp-wait-for-string "^\r*20[01]") | |
1762 | (beginning-of-line) | |
1763 | (delete-region (point-min) (point)) | |
1764 | proc))) | |
1765 | ||
1766 | (defun nntp-open-via-rlogin-and-telnet (buffer) | |
1767 | "Open a connection to an nntp server through an intermediate host. | |
1768 | First rlogin to the remote host, and then telnet the real news server | |
1769 | from there. | |
1770 | ||
1771 | Please refer to the following variables to customize the connection: | |
1772 | - `nntp-pre-command', | |
1773 | - `nntp-via-rlogin-command', | |
1774 | - `nntp-via-rlogin-command-switches', | |
1775 | - `nntp-via-user-name', | |
1776 | - `nntp-via-address', | |
1777 | - `nntp-telnet-command', | |
1778 | - `nntp-telnet-switches', | |
1779 | - `nntp-address', | |
1780 | - `nntp-port-number', | |
1781 | - `nntp-end-of-line'." | |
1782 | (let ((command `(,nntp-via-address | |
1783 | ,nntp-telnet-command | |
1784 | ,@nntp-telnet-switches)) | |
1785 | proc) | |
1786 | (when nntp-via-user-name | |
1787 | (setq command `("-l" ,nntp-via-user-name ,@command))) | |
1788 | (when nntp-via-rlogin-command-switches | |
1789 | (setq command (append nntp-via-rlogin-command-switches command))) | |
1790 | (push nntp-via-rlogin-command command) | |
1791 | (and nntp-pre-command | |
1792 | (push nntp-pre-command command)) | |
1793 | (setq proc (apply 'start-process "nntpd" buffer command)) | |
1794 | (save-excursion | |
1795 | (set-buffer buffer) | |
1796 | (nntp-wait-for-string "^r?telnet") | |
1797 | (process-send-string proc (concat "open " nntp-address | |
1798 | " " nntp-port-number "\n")) | |
1799 | (nntp-wait-for-string "^\r*20[01]") | |
1800 | (beginning-of-line) | |
1801 | (delete-region (point-min) (point)) | |
1802 | (process-send-string proc "\^]") | |
1803 | (nntp-wait-for-string "^r?telnet") | |
1804 | (process-send-string proc "mode character\n") | |
1805 | (accept-process-output proc 1) | |
1806 | (sit-for 1) | |
eec82323 | 1807 | (goto-char (point-min)) |
23f87bed MB |
1808 | (forward-line 1) |
1809 | (delete-region (point) (point-max))) | |
1810 | proc)) | |
1811 | ||
1812 | (defun nntp-open-via-telnet-and-telnet (buffer) | |
1813 | "Open a connection to an nntp server through an intermediate host. | |
1814 | First telnet the remote host, and then telnet the real news server | |
1815 | from there. | |
1816 | ||
1817 | Please refer to the following variables to customize the connection: | |
1818 | - `nntp-pre-command', | |
1819 | - `nntp-via-telnet-command', | |
1820 | - `nntp-via-telnet-switches', | |
1821 | - `nntp-via-address', | |
1822 | - `nntp-via-envuser', | |
1823 | - `nntp-via-user-name', | |
1824 | - `nntp-via-user-password', | |
1825 | - `nntp-via-shell-prompt', | |
1826 | - `nntp-telnet-command', | |
1827 | - `nntp-telnet-switches', | |
1828 | - `nntp-address', | |
1829 | - `nntp-port-number', | |
1830 | - `nntp-end-of-line'." | |
1831 | (save-excursion | |
1832 | (set-buffer buffer) | |
1833 | (erase-buffer) | |
1834 | (let ((command `(,nntp-via-telnet-command ,@nntp-via-telnet-switches)) | |
1835 | (case-fold-search t) | |
1836 | proc) | |
1837 | (and nntp-pre-command (push nntp-pre-command command)) | |
1838 | (setq proc (apply 'start-process "nntpd" buffer command)) | |
1839 | (when (memq (process-status proc) '(open run)) | |
1840 | (nntp-wait-for-string "^r?telnet") | |
1841 | (process-send-string proc "set escape \^X\n") | |
1842 | (cond | |
1843 | ((and nntp-via-envuser nntp-via-user-name) | |
1844 | (process-send-string proc (concat "open " "-l" nntp-via-user-name | |
1845 | nntp-via-address "\n"))) | |
1846 | (t | |
1847 | (process-send-string proc (concat "open " nntp-via-address | |
1848 | "\n")))) | |
1849 | (when (not nntp-via-envuser) | |
1850 | (nntp-wait-for-string "^\r*.?login:") | |
1851 | (process-send-string proc | |
1852 | (concat | |
1853 | (or nntp-via-user-name | |
1854 | (setq nntp-via-user-name | |
1855 | (read-string "login: "))) | |
1856 | "\n"))) | |
1857 | (nntp-wait-for-string "^\r*.?password:") | |
1858 | (process-send-string proc | |
1859 | (concat | |
1860 | (or nntp-via-user-password | |
1861 | (setq nntp-via-user-password | |
1862 | (read-passwd "Password: "))) | |
1863 | "\n")) | |
1864 | (nntp-wait-for-string nntp-via-shell-prompt) | |
1865 | (let ((real-telnet-command `("exec" | |
1866 | ,nntp-telnet-command | |
1867 | ,@nntp-telnet-switches | |
1868 | ,nntp-address | |
1869 | ,nntp-port-number))) | |
1870 | (process-send-string proc | |
1871 | (concat (mapconcat 'identity | |
1872 | real-telnet-command " ") | |
1873 | "\n"))) | |
1874 | (nntp-wait-for-string "^\r*20[01]") | |
1875 | (beginning-of-line) | |
1876 | (delete-region (point-min) (point)) | |
1877 | (process-send-string proc "\^]") | |
1878 | (nntp-wait-for-string "^r?telnet") | |
1879 | (process-send-string proc "mode character\n") | |
1880 | (accept-process-output proc 1) | |
1881 | (sit-for 1) | |
1882 | (goto-char (point-min)) | |
1883 | (forward-line 1) | |
1884 | (delete-region (point) (point-max))) | |
1885 | proc))) | |
eec82323 LMI |
1886 | |
1887 | (provide 'nntp) | |
1888 | ||
ab5796a9 | 1889 | ;;; arch-tag: 8655466a-b1b5-4929-9c45-7b1b2e767271 |
eec82323 | 1890 | ;;; nntp.el ends here |