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