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