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