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