2000-10-02 17:38:12 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / nntp.el
1 ;;; nntp.el --- nntp access for Gnus
2 ;; Copyright (C) 1987, 1988, 1989, 1990, 1992, 1993, 1994, 1995, 1996,
3 ;;        1997, 1998, 2000
4 ;;        Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'nnheader)
30 (require 'nnoo)
31 (require 'gnus-util)
32
33 (nnoo-declare nntp)
34
35 (eval-when-compile (require 'cl))
36
37 (defvoo nntp-address nil
38   "Address of the physical nntp server.")
39
40 (defvoo nntp-port-number "nntp"
41   "Port number on the physical nntp server.")
42
43 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
44   "*Hook used for sending commands to the server at startup.
45 The default value is `nntp-send-mode-reader', which makes an innd
46 server spawn an nnrpd server.")
47
48 (defvoo nntp-authinfo-function 'nntp-send-authinfo
49   "Function used to send AUTHINFO to the server.
50 It is called with no parameters.")
51
52 (defvoo nntp-server-action-alist
53     '(("nntpd 1\\.5\\.11t"
54        (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
55       ("NNRP server Netscape"
56        (setq nntp-server-list-active-group nil)))
57   "Alist of regexps to match on server types and actions to be taken.
58 For instance, if you want Gnus to beep every time you connect
59 to innd, you could say something like:
60
61 \(setq nntp-server-action-alist
62        '((\"innd\" (ding))))
63
64 You probably don't want to do that, though.")
65
66 (defvoo nntp-open-connection-function 'nntp-open-network-stream
67   "*Function used for connecting to a remote system.
68 It will be called with the buffer to output in.
69
70 Two pre-made functions are `nntp-open-network-stream', which is the
71 default, and simply connects to some port or other on the remote
72 system (see nntp-port-number).  The other are `nntp-open-rlogin',
73 which does an rlogin on the remote system, and then does a telnet to
74 the NNTP server available there (see nntp-rlogin-parameters) and
75 `nntp-open-telnet' which telnets to a remote system, logs in and does
76 the same.")
77
78 (defvoo nntp-rlogin-program "rsh"
79   "*Program used to log in on remote machines.
80 The default is \"rsh\", but \"ssh\" is a popular alternative.")
81
82 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
83   "*Parameters to `nntp-open-rlogin'.
84 That function may be used as `nntp-open-connection-function'.  In that
85 case, this list will be used as the parameter list given to rsh.")
86
87 (defvoo nntp-rlogin-user-name nil
88   "*User name on remote system when using the rlogin connect method.")
89
90 (defvoo nntp-telnet-parameters
91     '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
92   "*Parameters to `nntp-open-telnet'.
93 That function may be used as `nntp-open-connection-function'.  In that
94 case, this list will be executed as a command after logging in
95 via telnet.")
96
97 (defvoo nntp-telnet-user-name nil
98   "User name to log in via telnet with.")
99
100 (defvoo nntp-telnet-passwd nil
101   "Password to use to log in via telnet with.")
102
103 (defvoo nntp-open-telnet-envuser nil
104   "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
105
106 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
107   "*Regular expression to match the shell prompt on the remote machine.")
108
109 (defvoo nntp-telnet-command "telnet"
110   "Command used to start telnet.")
111
112 (defvoo nntp-telnet-switches '("-8")
113   "Switches given to the telnet command.")
114
115 (defvoo nntp-end-of-line "\r\n"
116   "String to use on the end of lines when talking to the NNTP server.
117 This is \"\\r\\n\" by default, but should be \"\\n\" when
118 using rlogin or telnet to communicate with the server.")
119
120 (defvoo nntp-large-newsgroup 50
121   "*The number of the articles which indicates a large newsgroup.
122 If the number of the articles is greater than the value, verbose
123 messages will be shown to indicate the current status.")
124
125 (defvoo nntp-maximum-request 400
126   "*The maximum number of the requests sent to the NNTP server at one time.
127 If Emacs hangs up while retrieving headers, set the variable to a
128 lower value.")
129
130 (defvoo nntp-nov-is-evil nil
131   "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
132
133 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
134   "*List of strings that are used as commands to fetch NOV lines from a server.
135 The strings are tried in turn until a positive response is gotten.  If
136 none of the commands are successful, nntp will just grab headers one
137 by one.")
138
139 (defvoo nntp-nov-gap 5
140   "*Maximum allowed gap between two articles.
141 If the gap between two consecutive articles is bigger than this
142 variable, split the XOVER request into two requests.")
143
144 (defvoo nntp-prepare-server-hook nil
145   "*Hook run before a server is opened.
146 If can be used to set up a server remotely, for instance.  Say you
147 have an account at the machine \"other.machine\".  This machine has
148 access to an NNTP server that you can't access locally.  You could
149 then use this hook to rsh to the remote machine and start a proxy NNTP
150 server there that you can connect to.  See also
151 `nntp-open-connection-function'")
152
153 (defvoo nntp-warn-about-losing-connection t
154   "*If non-nil, beep when a server closes connection.")
155
156 (defvoo nntp-coding-system-for-read 'binary
157   "*Coding system to read from NNTP.")
158
159 (defvoo nntp-coding-system-for-write 'binary
160   "*Coding system to write to NNTP.")
161
162 (defcustom nntp-authinfo-file "~/.authinfo"
163   ".netrc-like file that holds nntp authinfo passwords."
164   :type
165   '(choice file
166            (repeat :tag "Entries"
167                    :menu-tag "Inline"
168                    (list :format "%v"
169                          :value ("" ("login" . "") ("password" . ""))
170                          (string :tag "Host")
171                          (checklist :inline t
172                                     (cons :format "%v"
173                                           (const :format "" "login")
174                                           (string :format "Login: %v"))
175                                     (cons :format "%v"
176                                           (const :format "" "password")
177                                           (string :format "Password: %v")))))))
178
179 \f
180
181 (defvoo nntp-connection-timeout nil
182   "*Number of seconds to wait before an nntp connection times out.
183 If this variable is nil, which is the default, no timers are set.")
184
185 ;;; Internal variables.
186
187 (defvar nntp-record-commands nil
188   "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
189
190 (defvar nntp-have-messaged nil)
191
192 (defvar nntp-process-wait-for nil)
193 (defvar nntp-process-to-buffer nil)
194 (defvar nntp-process-callback nil)
195 (defvar nntp-process-decode nil)
196 (defvar nntp-process-start-point nil)
197 (defvar nntp-inside-change-function nil)
198 (defvoo nntp-last-command-time nil)
199 (defvoo nntp-last-command nil)
200 (defvoo nntp-authinfo-password nil)
201 (defvoo nntp-authinfo-user nil)
202
203 (defvar nntp-connection-list nil)
204
205 (defvoo nntp-server-type nil)
206 (defvoo nntp-connection-alist nil)
207 (defvoo nntp-status-string "")
208 (defconst nntp-version "nntp 5.0")
209 (defvoo nntp-inhibit-erase nil)
210 (defvoo nntp-inhibit-output nil)
211
212 (defvoo nntp-server-xover 'try)
213 (defvoo nntp-server-list-active-group 'try)
214
215 (defvar nntp-async-needs-kluge
216   (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
217   "*When non-nil, nntp will poll asynchronous connections
218 once a second.  By default, this is turned on only for Emacs
219 20.3, which has a bug that breaks nntp's normal method of
220 noticing asynchronous data.")
221
222 (defvar nntp-async-timer nil)
223 (defvar nntp-async-process-list nil)
224
225 (eval-and-compile
226   (autoload 'mail-source-read-passwd "mail-source")
227   (autoload 'open-ssl-stream "ssl"))
228
229 \f
230
231 ;;; Internal functions.
232
233 (defsubst nntp-send-string (process string)
234   "Send STRING to PROCESS."
235   ;; We need to store the time to provide timeouts, and
236   ;; to store the command so the we can replay the command
237   ;; if the server gives us an AUTHINFO challenge.
238   (setq nntp-last-command-time (current-time)
239         nntp-last-command string)
240   (when nntp-record-commands
241     (nntp-record-command string))
242   (process-send-string process (concat string nntp-end-of-line)))
243
244 (defun nntp-record-command (string)
245   "Record the command STRING."
246   (save-excursion
247     (set-buffer (get-buffer-create "*nntp-log*"))
248     (goto-char (point-max))
249     (let ((time (current-time)))
250       (insert (format-time-string "%Y%m%dT%H%M%S" time)
251               "." (format "%03d" (/ (nth 2 time) 1000))
252               " " nntp-address " " string "\n"))))
253
254 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
255   "Wait for WAIT-FOR to arrive from PROCESS."
256   (save-excursion
257     (set-buffer (process-buffer process))
258     (goto-char (point-min))
259     (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
260                     (looking-at "480"))
261                 (memq (process-status process) '(open run)))
262       (when (looking-at "480")
263         (nntp-handle-authinfo process))
264       (nntp-accept-process-output process)
265       (goto-char (point-min)))
266     (prog1
267         (cond
268          ((looking-at "[45]")
269           (progn
270             (nntp-snarf-error-message)
271             nil))
272          ((not (memq (process-status process) '(open run)))
273           (nnheader-report 'nntp "Server closed connection"))
274          (t
275           (goto-char (point-max))
276           (let ((limit (point-min)))
277             (while (not (re-search-backward wait-for limit t))
278               (nntp-accept-process-output process)
279               ;; We assume that whatever we wait for is less than 1000
280               ;; characters long.
281               (setq limit (max (- (point-max) 1000) (point-min)))
282               (goto-char (point-max))))
283           (nntp-decode-text (not decode))
284           (unless discard
285             (save-excursion
286               (set-buffer buffer)
287               (goto-char (point-max))
288               (insert-buffer-substring (process-buffer process))
289               ;; Nix out "nntp reading...." message.
290               (when nntp-have-messaged
291                 (setq nntp-have-messaged nil)
292                 (nnheader-message 5 ""))
293               t))))
294       (unless discard
295         (erase-buffer)))))
296
297 (defun nntp-kill-buffer (buffer)
298   (when (buffer-name buffer)
299     (kill-buffer buffer)
300     (nnheader-init-server-buffer)))
301
302 (defsubst nntp-find-connection (buffer)
303   "Find the connection delivering to BUFFER."
304   (let ((alist nntp-connection-alist)
305         (buffer (if (stringp buffer) (get-buffer buffer) buffer))
306         process entry)
307     (while (setq entry (pop alist))
308       (when (eq buffer (cadr entry))
309         (setq process (car entry)
310               alist nil)))
311     (when process
312       (if (memq (process-status process) '(open run))
313           process
314         (nntp-kill-buffer (process-buffer process))
315         (setq nntp-connection-alist (delq entry nntp-connection-alist))
316         nil))))
317
318 (defsubst nntp-find-connection-entry (buffer)
319   "Return the entry for the connection to BUFFER."
320   (assq (nntp-find-connection buffer) nntp-connection-alist))
321
322 (defun nntp-find-connection-buffer (buffer)
323   "Return the process connection buffer tied to BUFFER."
324   (let ((process (nntp-find-connection buffer)))
325     (when process
326       (process-buffer process))))
327
328 (defsubst nntp-retrieve-data (command address port buffer
329                                       &optional wait-for callback decode)
330   "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
331   (let ((process (or (nntp-find-connection buffer)
332                      (nntp-open-connection buffer))))
333     (if (not process)
334         (nnheader-report 'nntp "Couldn't open connection to %s" address)
335       (unless (or nntp-inhibit-erase nnheader-callback-function)
336         (save-excursion
337           (set-buffer (process-buffer process))
338           (erase-buffer)))
339       (condition-case err
340           (progn
341             (when command
342               (nntp-send-string process command))
343             (cond
344              ((eq callback 'ignore)
345               t)
346              ((and callback wait-for)
347               (nntp-async-wait process wait-for buffer decode callback)
348               t)
349              (wait-for
350               (nntp-wait-for process wait-for buffer decode))
351              (t t)))
352         (error 
353          (nnheader-report 'nntp "Couldn't open connection to %s: %s" 
354                           address err))))))
355
356 (defsubst nntp-send-command (wait-for &rest strings)
357   "Send STRINGS to server and wait until WAIT-FOR returns."
358   (when (and (not nnheader-callback-function)
359              (not nntp-inhibit-output))
360     (save-excursion
361       (set-buffer nntp-server-buffer)
362       (erase-buffer)))
363   (nntp-retrieve-data
364    (mapconcat 'identity strings " ")
365    nntp-address nntp-port-number nntp-server-buffer
366    wait-for nnheader-callback-function))
367
368 (defun nntp-send-command-nodelete (wait-for &rest strings)
369   "Send STRINGS to server and wait until WAIT-FOR returns."
370   (nntp-retrieve-data
371    (mapconcat 'identity strings " ")
372    nntp-address nntp-port-number nntp-server-buffer
373    wait-for nnheader-callback-function))
374
375 (defun nntp-send-command-and-decode (wait-for &rest strings)
376   "Send STRINGS to server and wait until WAIT-FOR returns."
377   (when (and (not nnheader-callback-function)
378              (not nntp-inhibit-output))
379     (save-excursion
380       (set-buffer nntp-server-buffer)
381       (erase-buffer)))
382   (nntp-retrieve-data
383    (mapconcat 'identity strings " ")
384    nntp-address nntp-port-number nntp-server-buffer
385    wait-for nnheader-callback-function t))
386
387 (defun nntp-send-buffer (wait-for)
388   "Send the current buffer to server and wait until WAIT-FOR returns."
389   (when (and (not nnheader-callback-function)
390              (not nntp-inhibit-output))
391     (save-excursion
392       (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
393       (erase-buffer)))
394   (nntp-encode-text)
395   (process-send-region (nntp-find-connection nntp-server-buffer)
396                        (point-min) (point-max))
397   (nntp-retrieve-data
398    nil nntp-address nntp-port-number nntp-server-buffer
399    wait-for nnheader-callback-function))
400
401 \f
402
403 ;;; Interface functions.
404
405 (nnoo-define-basics nntp)
406
407 (defsubst nntp-next-result-arrived-p ()
408   (cond
409    ;; A result that starts with a 2xx code is terminated by
410    ;; a line with only a "." on it.
411    ((eq (char-after) ?2)
412     (if (re-search-forward "\n\\.\r?\n" nil t)
413         t
414       nil))
415    ;; A result that starts with a 3xx or 4xx code is terminated
416    ;; by a newline.
417    ((looking-at "[34]")
418     (if (search-forward "\n" nil t)
419         t
420       nil))
421    ;; No result here.
422    (t
423     nil)))
424
425 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
426   "Retrieve the headers of ARTICLES."
427   (nntp-possibly-change-group group server)
428   (save-excursion
429     (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
430     (erase-buffer)
431     (if (and (not gnus-nov-is-evil)
432              (not nntp-nov-is-evil)
433              (nntp-retrieve-headers-with-xover articles fetch-old))
434         ;; We successfully retrieved the headers via XOVER.
435         'nov
436       ;; XOVER didn't work, so we do it the hard, slow and inefficient
437       ;; way.
438       (let ((number (length articles))
439             (count 0)
440             (received 0)
441             (last-point (point-min))
442             (buf (nntp-find-connection-buffer nntp-server-buffer))
443             (nntp-inhibit-erase t)
444             article)
445         ;; Send HEAD commands.
446         (while (setq article (pop articles))
447           (nntp-send-command
448            nil
449            "HEAD" (if (numberp article)
450                       (int-to-string article)
451                     ;; `articles' is either a list of article numbers
452                     ;; or a list of article IDs.
453                     article))
454           (incf count)
455           ;; Every 400 requests we have to read the stream in
456           ;; order to avoid deadlocks.
457           (when (or (null articles)     ;All requests have been sent.
458                     (zerop (% count nntp-maximum-request)))
459             (nntp-accept-response)
460             (while (progn
461                      (set-buffer buf)
462                      (goto-char last-point)
463                      ;; Count replies.
464                      (while (nntp-next-result-arrived-p)
465                        (setq last-point (point))
466                        (incf received))
467                      (< received count))
468               ;; If number of headers is greater than 100, give
469               ;;  informative messages.
470               (and (numberp nntp-large-newsgroup)
471                    (> number nntp-large-newsgroup)
472                    (zerop (% received 20))
473                    (nnheader-message 6 "NNTP: Receiving headers... %d%%"
474                                      (/ (* received 100) number)))
475               (nntp-accept-response))))
476         (and (numberp nntp-large-newsgroup)
477              (> number nntp-large-newsgroup)
478              (nnheader-message 6 "NNTP: Receiving headers...done"))
479
480         ;; Now all of replies are received.  Fold continuation lines.
481         (nnheader-fold-continuation-lines)
482         ;; Remove all "\r"'s.
483         (nnheader-strip-cr)
484         (copy-to-buffer nntp-server-buffer (point-min) (point-max))
485         'headers))))
486
487 (deffoo nntp-retrieve-groups (groups &optional server)
488   "Retrieve group info on GROUPS."
489   (nntp-possibly-change-group nil server)
490   (when (nntp-find-connection-buffer nntp-server-buffer)
491     (save-excursion
492       ;; Erase nntp-server-buffer before nntp-inhibit-erase.
493       (set-buffer nntp-server-buffer)
494       (erase-buffer)
495       (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
496       ;; The first time this is run, this variable is `try'.  So we
497       ;; try.
498       (when (eq nntp-server-list-active-group 'try)
499         (nntp-try-list-active (car groups)))
500       (erase-buffer)
501       (let ((count 0)
502             (received 0)
503             (last-point (point-min))
504             (nntp-inhibit-erase t)
505             (buf (nntp-find-connection-buffer nntp-server-buffer))
506             (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
507         (while groups
508           ;; Send the command to the server.
509           (nntp-send-command nil command (pop groups))
510           (incf count)
511           ;; Every 400 requests we have to read the stream in
512           ;; order to avoid deadlocks.
513           (when (or (null groups)       ;All requests have been sent.
514                     (zerop (% count nntp-maximum-request)))
515             (nntp-accept-response)
516             (while (progn
517                      ;; Search `blue moon' in this file for the
518                      ;; reason why set-buffer here.
519                      (set-buffer buf)
520                      (goto-char last-point)
521                      ;; Count replies.
522                      (while (re-search-forward "^[0-9]" nil t)
523                        (incf received))
524                      (setq last-point (point))
525                      (< received count))
526               (nntp-accept-response))))
527
528         ;; Wait for the reply from the final command.
529         (set-buffer buf)
530         (goto-char (point-max))
531         (re-search-backward "^[0-9]" nil t)
532         (when (looking-at "^[23]")
533           (while (progn
534                    (set-buffer buf)
535                    (goto-char (point-max))
536                    (if (not nntp-server-list-active-group)
537                        (not (re-search-backward "\r?\n" (- (point) 3) t))
538                      (not (re-search-backward "^\\.\r?\n" (- (point) 4) t))))
539             (nntp-accept-response)))
540
541         ;; Now all replies are received.  We remove CRs.
542         (set-buffer buf)
543         (goto-char (point-min))
544         (while (search-forward "\r" nil t)
545           (replace-match "" t t))
546
547         (if (not nntp-server-list-active-group)
548             (progn
549               (copy-to-buffer nntp-server-buffer (point-min) (point-max))
550               'group)
551           ;; We have read active entries, so we just delete the
552           ;; superfluous gunk.
553           (goto-char (point-min))
554           (while (re-search-forward "^[.2-5]" nil t)
555             (delete-region (match-beginning 0)
556                            (progn (forward-line 1) (point))))
557           (copy-to-buffer nntp-server-buffer (point-min) (point-max))
558           'active)))))
559
560 (deffoo nntp-retrieve-articles (articles &optional group server)
561   (nntp-possibly-change-group group server)
562   (save-excursion
563     (let ((number (length articles))
564           (count 0)
565           (received 0)
566           (last-point (point-min))
567           (buf (nntp-find-connection-buffer nntp-server-buffer))
568           (nntp-inhibit-erase t)
569           (map (apply 'vector articles))
570           (point 1)
571           article)
572       (set-buffer buf)
573       (erase-buffer)
574       ;; Send ARTICLE command.
575       (while (setq article (pop articles))
576         (nntp-send-command
577          nil
578          "ARTICLE" (if (numberp article)
579                        (int-to-string article)
580                      ;; `articles' is either a list of article numbers
581                      ;; or a list of article IDs.
582                      article))
583         (incf count)
584         ;; Every 400 requests we have to read the stream in
585         ;; order to avoid deadlocks.
586         (when (or (null articles)       ;All requests have been sent.
587                   (zerop (% count nntp-maximum-request)))
588           (nntp-accept-response)
589           (while (progn
590                    (set-buffer buf)
591                    (goto-char last-point)
592                    ;; Count replies.
593                    (while (nntp-next-result-arrived-p)
594                      (aset map received (cons (aref map received) (point)))
595                      (setq last-point (point))
596                      (incf received))
597                    (< received count))
598             ;; If number of headers is greater than 100, give
599             ;;  informative messages.
600             (and (numberp nntp-large-newsgroup)
601                  (> number nntp-large-newsgroup)
602                  (zerop (% received 20))
603                  (nnheader-message 6 "NNTP: Receiving articles... %d%%"
604                                    (/ (* received 100) number)))
605             (nntp-accept-response))))
606       (and (numberp nntp-large-newsgroup)
607            (> number nntp-large-newsgroup)
608            (nnheader-message 6 "NNTP: Receiving articles...done"))
609
610       ;; Now we have all the responses.  We go through the results,
611       ;; wash it and copy it over to the server buffer.
612       (set-buffer nntp-server-buffer)
613       (erase-buffer)
614       (setq last-point (point-min))
615       (mapcar
616        (lambda (entry)
617          (narrow-to-region
618           (setq point (goto-char (point-max)))
619           (progn
620             (insert-buffer-substring buf last-point (cdr entry))
621             (point-max)))
622          (setq last-point (cdr entry))
623          (nntp-decode-text)
624          (widen)
625          (cons (car entry) point))
626        map))))
627
628 (defun nntp-try-list-active (group)
629   (nntp-list-active-group group)
630   (save-excursion
631     (set-buffer nntp-server-buffer)
632     (goto-char (point-min))
633     (cond ((or (eobp)
634                (looking-at "5[0-9]+"))
635            (setq nntp-server-list-active-group nil))
636           (t
637            (setq nntp-server-list-active-group t)))))
638
639 (deffoo nntp-list-active-group (group &optional server)
640   "Return the active info on GROUP (which can be a regexp)."
641   (nntp-possibly-change-group nil server)
642   (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group))
643
644 (deffoo nntp-request-group-articles (group &optional server)
645   "Return the list of existing articles in GROUP."
646   (nntp-possibly-change-group nil server)
647   (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group))
648
649 (deffoo nntp-request-article (article &optional group server buffer command)
650   (nntp-possibly-change-group group server)
651   (when (nntp-send-command-and-decode
652          "\r?\n\\.\r?\n" "ARTICLE"
653          (if (numberp article) (int-to-string article) article))
654     (if (and buffer
655              (not (equal buffer nntp-server-buffer)))
656         (save-excursion
657           (set-buffer nntp-server-buffer)
658           (copy-to-buffer buffer (point-min) (point-max))
659           (nntp-find-group-and-number))
660       (nntp-find-group-and-number))))
661
662 (deffoo nntp-request-head (article &optional group server)
663   (nntp-possibly-change-group group server)
664   (when (nntp-send-command
665          "\r?\n\\.\r?\n" "HEAD"
666          (if (numberp article) (int-to-string article) article))
667     (prog1
668         (nntp-find-group-and-number)
669       (nntp-decode-text))))
670
671 (deffoo nntp-request-body (article &optional group server)
672   (nntp-possibly-change-group group server)
673   (nntp-send-command-and-decode
674    "\r?\n\\.\r?\n" "BODY"
675    (if (numberp article) (int-to-string article) article)))
676
677 (deffoo nntp-request-group (group &optional server dont-check)
678   (nntp-possibly-change-group nil server)
679   (when (nntp-send-command "^[245].*\n" "GROUP" group)
680     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
681       (setcar (cddr entry) group))))
682
683 (deffoo nntp-close-group (group &optional server)
684   t)
685
686 (deffoo nntp-server-opened (&optional server)
687   "Say whether a connection to SERVER has been opened."
688   (and (nnoo-current-server-p 'nntp server)
689        nntp-server-buffer
690        (gnus-buffer-live-p nntp-server-buffer)
691        (nntp-find-connection nntp-server-buffer)))
692
693 (deffoo nntp-open-server (server &optional defs connectionless)
694   (nnheader-init-server-buffer)
695   (if (nntp-server-opened server)
696       t
697     (when (or (stringp (car defs))
698               (numberp (car defs)))
699       (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
700     (unless (assq 'nntp-address defs)
701       (setq defs (append defs (list (list 'nntp-address server)))))
702     (nnoo-change-server 'nntp server defs)
703     (unless connectionless
704       (or (nntp-find-connection nntp-server-buffer)
705           (nntp-open-connection nntp-server-buffer)))))
706
707 (deffoo nntp-close-server (&optional server)
708   (nntp-possibly-change-group nil server t)
709   (let ((process (nntp-find-connection nntp-server-buffer)))
710     (while process
711       (when (memq (process-status process) '(open run))
712         (ignore-errors
713           (nntp-send-string process "QUIT")
714           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
715             ;; Ok, this is evil, but when using telnet and stuff
716             ;; as the connection method, it's important that the
717             ;; QUIT command actually is sent out before we kill
718             ;; the process.
719             (sleep-for 1))))
720       (nntp-kill-buffer (process-buffer process))
721       (setq process (car (pop nntp-connection-alist))))
722     (nnoo-close-server 'nntp)))
723
724 (deffoo nntp-request-close ()
725   (let (process)
726     (while (setq process (pop nntp-connection-list))
727       (when (memq (process-status process) '(open run))
728         (ignore-errors
729           (nntp-send-string process "QUIT")
730           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
731             ;; Ok, this is evil, but when using telnet and stuff
732             ;; as the connection method, it's important that the
733             ;; QUIT command actually is sent out before we kill
734             ;; the process.
735             (sleep-for 1))))
736       (nntp-kill-buffer (process-buffer process)))))
737
738 (deffoo nntp-request-list (&optional server)
739   (nntp-possibly-change-group nil server)
740   (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST"))
741
742 (deffoo nntp-request-list-newsgroups (&optional server)
743   (nntp-possibly-change-group nil server)
744   (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS"))
745
746 (deffoo nntp-request-newgroups (date &optional server)
747   (nntp-possibly-change-group nil server)
748   (save-excursion
749     (set-buffer nntp-server-buffer)
750     (prog1
751         (nntp-send-command
752          "^\\.\r?\n" "NEWGROUPS"
753          (format-time-string "%y%m%d %H%M%S" (date-to-time date)))
754       (nntp-decode-text))))
755
756 (deffoo nntp-request-post (&optional server)
757   (nntp-possibly-change-group nil server)
758   (when (nntp-send-command "^[23].*\r?\n" "POST")
759     (nntp-send-buffer "^[23].*\n")))
760
761 (deffoo nntp-request-type (group article)
762   'news)
763
764 (deffoo nntp-asynchronous-p ()
765   t)
766
767 ;;; Hooky functions.
768
769 (defun nntp-send-mode-reader ()
770   "Send the MODE READER command to the nntp server.
771 This function is supposed to be called from `nntp-server-opened-hook'.
772 It will make innd servers spawn an nnrpd process to allow actual article
773 reading."
774   (nntp-send-command "^.*\n" "MODE READER"))
775
776 (defun nntp-send-authinfo (&optional send-if-force)
777   "Send the AUTHINFO to the nntp server.
778 It will look in the \"~/.authinfo\" file for matching entries.  If
779 nothing suitable is found there, it will prompt for a user name
780 and a password.
781
782 If SEND-IF-FORCE, only send authinfo to the server if the
783 .authinfo file has the FORCE token."
784   (let* ((list (gnus-parse-netrc nntp-authinfo-file))
785          (alist (gnus-netrc-machine list nntp-address "nntp"))
786          (force (gnus-netrc-get alist "force"))
787          (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
788          (passwd (gnus-netrc-get alist "password")))
789     (when (or (not send-if-force)
790               force)
791       (unless user
792         (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
793               nntp-authinfo-user user))
794       (unless (member user '(nil ""))
795         (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
796         (when t                         ;???Should check if AUTHINFO succeeded
797           (nntp-send-command
798            "^2.*\r?\n" "AUTHINFO PASS"
799            (or passwd
800                nntp-authinfo-password
801                (setq nntp-authinfo-password
802                      (mail-source-read-passwd (format "NNTP (%s@%s) password: "
803                                                       user nntp-address))))))))))
804
805 (defun nntp-send-nosy-authinfo ()
806   "Send the AUTHINFO to the nntp server."
807   (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
808     (unless (member user '(nil ""))
809       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
810       (when t                           ;???Should check if AUTHINFO succeeded
811         (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
812                            (mail-source-read-passwd "NNTP (%s@%s) password: "
813                                                     user nntp-address))))))
814
815 (defun nntp-send-authinfo-from-file ()
816   "Send the AUTHINFO to the nntp server.
817
818 The authinfo login name is taken from the user's login name and the
819 password contained in '~/.nntp-authinfo'."
820   (when (file-exists-p "~/.nntp-authinfo")
821     (with-temp-buffer
822       (insert-file-contents "~/.nntp-authinfo")
823       (goto-char (point-min))
824       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
825       (nntp-send-command
826        "^2.*\r?\n" "AUTHINFO PASS"
827        (buffer-substring (point) (progn (end-of-line) (point)))))))
828
829 ;;; Internal functions.
830
831 (defun nntp-handle-authinfo (process)
832   "Take care of an authinfo response from the server."
833   (let ((last nntp-last-command))
834     (funcall nntp-authinfo-function)
835     ;; We have to re-send the function that was interrupted by
836     ;; the authinfo request.
837     (save-excursion
838       (set-buffer nntp-server-buffer)
839       (erase-buffer))
840     (nntp-send-string process last)))
841
842 (defun nntp-make-process-buffer (buffer)
843   "Create a new, fresh buffer usable for nntp process connections."
844   (save-excursion
845     (set-buffer
846      (generate-new-buffer
847       (format " *server %s %s %s*"
848               nntp-address nntp-port-number
849               (gnus-buffer-exists-p buffer))))
850     (mm-enable-multibyte)
851     (set (make-local-variable 'after-change-functions) nil)
852     (set (make-local-variable 'nntp-process-wait-for) nil)
853     (set (make-local-variable 'nntp-process-callback) nil)
854     (set (make-local-variable 'nntp-process-to-buffer) nil)
855     (set (make-local-variable 'nntp-process-start-point) nil)
856     (set (make-local-variable 'nntp-process-decode) nil)
857     (current-buffer)))
858
859 (defun nntp-open-connection (buffer)
860   "Open a connection to PORT on ADDRESS delivering output to BUFFER."
861   (run-hooks 'nntp-prepare-server-hook)
862   (let* ((pbuffer (nntp-make-process-buffer buffer))
863          (timer
864           (and nntp-connection-timeout
865                (nnheader-run-at-time
866                 nntp-connection-timeout nil
867                 `(lambda ()
868                    (nntp-kill-buffer ,pbuffer)))))
869          (process
870           (condition-case ()
871               (let ((coding-system-for-read nntp-coding-system-for-read)
872                     (coding-system-for-write nntp-coding-system-for-write))
873                 (funcall nntp-open-connection-function pbuffer))
874             (error nil)
875             (quit nil))))
876     (when timer
877       (nnheader-cancel-timer timer))
878     (when (and (buffer-name pbuffer)
879                process)
880       (process-kill-without-query process)
881       (nntp-wait-for process "^.*\n" buffer nil t)
882       (if (memq (process-status process) '(open run))
883           (prog1
884               (caar (push (list process buffer nil) nntp-connection-alist))
885             (push process nntp-connection-list)
886             (save-excursion
887               (set-buffer pbuffer)
888               (nntp-read-server-type)
889               (erase-buffer)
890               (set-buffer nntp-server-buffer)
891               (let ((nnheader-callback-function nil))
892                 (run-hooks 'nntp-server-opened-hook)
893                 (nntp-send-authinfo t))))
894         (nntp-kill-buffer (process-buffer process))
895         nil))))
896
897 (defun nntp-open-network-stream (buffer)
898   (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
899
900 (defun nntp-open-ssl-stream (buffer)
901   (let* ((ssl-program-arguments '("-connect" (concat host ":" service)))
902          (proc (open-ssl-stream "nntpd" buffer nntp-address nntp-port-number)))
903     (save-excursion
904       (set-buffer buffer)
905       (nntp-wait-for-string "^\r*20[01]")
906       (beginning-of-line)
907       (delete-region (point-min) (point))
908       proc)))
909
910 (defun nntp-read-server-type ()
911   "Find out what the name of the server we have connected to is."
912   ;; Wait for the status string to arrive.
913   (setq nntp-server-type (buffer-string))
914   (let ((alist nntp-server-action-alist)
915         (case-fold-search t)
916         entry)
917     ;; Run server-specific commands.
918     (while alist
919       (setq entry (pop alist))
920       (when (string-match (car entry) nntp-server-type)
921         (if (and (listp (cadr entry))
922                  (not (eq 'lambda (caadr entry))))
923             (eval (cadr entry))
924           (funcall (cadr entry)))))))
925
926 (defun nntp-async-wait (process wait-for buffer decode callback)
927   (save-excursion
928     (set-buffer (process-buffer process))
929     (unless nntp-inside-change-function
930       (erase-buffer))
931     (setq nntp-process-wait-for wait-for
932           nntp-process-to-buffer buffer
933           nntp-process-decode decode
934           nntp-process-callback callback
935           nntp-process-start-point (point-max))
936     (setq after-change-functions '(nntp-after-change-function))
937     (if nntp-async-needs-kluge
938         (nntp-async-kluge process))))
939
940 (defun nntp-async-kluge (process)
941   ;; emacs 20.3 bug: process output with encoding 'binary
942   ;; doesn't trigger after-change-functions.
943   (unless nntp-async-timer
944     (setq nntp-async-timer
945           (nnheader-run-at-time 1 1 'nntp-async-timer-handler)))
946   (add-to-list 'nntp-async-process-list process))
947
948 (defun nntp-async-timer-handler ()
949   (mapcar
950    (lambda (proc)
951      (if (memq (process-status proc) '(open run))
952          (nntp-async-trigger proc)
953        (nntp-async-stop proc)))
954    nntp-async-process-list))
955
956 (defun nntp-async-stop (proc)
957   (setq nntp-async-process-list (delq proc nntp-async-process-list))
958   (when (and nntp-async-timer (not nntp-async-process-list))
959     (nnheader-cancel-timer nntp-async-timer)
960     (setq nntp-async-timer nil)))
961
962 (defun nntp-after-change-function (beg end len)
963   (unwind-protect
964       ;; we only care about insertions at eob
965       (when (and (eq 0 len) (eq (point-max) end))
966         (save-match-data
967           (let ((proc (get-buffer-process (current-buffer))))
968             (when proc
969               (nntp-async-trigger proc)))))
970     ;; any throw from after-change-functions will leave it
971     ;; set to nil.  so we reset it here, if necessary.
972     (when quit-flag
973       (setq after-change-functions '(nntp-after-change-function)))))
974
975 (defun nntp-async-trigger (process)
976   (save-excursion
977     (set-buffer (process-buffer process))
978     (when nntp-process-callback
979       ;; do we have an error message?
980       (goto-char nntp-process-start-point)
981       (if (memq (following-char) '(?4 ?5))
982           ;; wants credentials?
983           (if (looking-at "480")
984               (nntp-handle-authinfo process)
985             ;; report error message.
986             (nntp-snarf-error-message)
987             (nntp-do-callback nil))
988
989         ;; got what we expect?
990         (goto-char (point-max))
991         (when (re-search-backward
992                nntp-process-wait-for nntp-process-start-point t)
993           (nntp-async-stop process)
994           ;; convert it.
995           (when (gnus-buffer-exists-p nntp-process-to-buffer)
996             (let ((buf (current-buffer))
997                   (start nntp-process-start-point)
998                   (decode nntp-process-decode))
999               (save-excursion
1000                 (set-buffer nntp-process-to-buffer)
1001                 (goto-char (point-max))
1002                 (save-restriction
1003                   (narrow-to-region (point) (point))
1004                   (insert-buffer-substring buf start)
1005                   (when decode
1006                     (nntp-decode-text))))))
1007           ;; report it.
1008           (goto-char (point-max))
1009           (nntp-do-callback
1010            (buffer-name (get-buffer nntp-process-to-buffer))))))))
1011
1012 (defun nntp-do-callback (arg)
1013   (let ((callback nntp-process-callback)
1014         (nntp-inside-change-function t))
1015     (setq nntp-process-callback nil)
1016     (funcall callback arg)))
1017
1018 (defun nntp-snarf-error-message ()
1019   "Save the error message in the current buffer."
1020   (let ((message (buffer-string)))
1021     (while (string-match "[\r\n]+" message)
1022       (setq message (replace-match " " t t message)))
1023     (nnheader-report 'nntp message)
1024     message))
1025
1026 (defun nntp-accept-process-output (process &optional timeout)
1027   "Wait for output from PROCESS and message some dots."
1028   (save-excursion
1029     (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1030                     nntp-server-buffer))
1031     (let ((len (/ (point-max) 1024))
1032           message-log-max)
1033       (unless (< len 10)
1034         (setq nntp-have-messaged t)
1035         (nnheader-message 7 "nntp read: %dk" len)))
1036     (accept-process-output process (or timeout 1))))
1037
1038 (defun nntp-accept-response ()
1039   "Wait for output from the process that outputs to BUFFER."
1040   (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1041
1042 (defun nntp-possibly-change-group (group server &optional connectionless)
1043   (let ((nnheader-callback-function nil))
1044     (when server
1045       (or (nntp-server-opened server)
1046           (nntp-open-server server nil connectionless)))
1047
1048     (unless connectionless
1049       (or (nntp-find-connection nntp-server-buffer)
1050           (nntp-open-connection nntp-server-buffer))))
1051
1052   (when group
1053     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1054       (when (not (equal group (caddr entry)))
1055         (save-excursion
1056           (set-buffer (process-buffer (car entry)))
1057           (erase-buffer)
1058           (nntp-send-command "^[245].*\n" "GROUP" group)
1059           (setcar (cddr entry) group)
1060           (erase-buffer))))))
1061
1062 (defun nntp-decode-text (&optional cr-only)
1063   "Decode the text in the current buffer."
1064   (goto-char (point-min))
1065   (while (search-forward "\r" nil t)
1066     (delete-char -1))
1067   (unless cr-only
1068     ;; Remove trailing ".\n" end-of-transfer marker.
1069     (goto-char (point-max))
1070     (forward-line -1)
1071     (when (looking-at ".\n")
1072       (delete-char 2))
1073     ;; Delete status line.
1074     (goto-char (point-min))
1075     (delete-region (point) (progn (forward-line 1) (point)))
1076     ;; Remove "." -> ".." encoding.
1077     (while (search-forward "\n.." nil t)
1078       (delete-char -1))))
1079
1080 (defun nntp-encode-text ()
1081   "Encode the text in the current buffer."
1082   (save-excursion
1083     ;; Replace "." at beginning of line with "..".
1084     (goto-char (point-min))
1085     (while (re-search-forward "^\\." nil t)
1086       (insert "."))
1087     (goto-char (point-max))
1088     ;; Insert newline at the end of the buffer.
1089     (unless (bolp)
1090       (insert "\n"))
1091     ;; Insert `.' at end of buffer (end of text mark).
1092     (goto-char (point-max))
1093     (insert ".\n")
1094     (goto-char (point-min))
1095     (while (not (eobp))
1096       (end-of-line)
1097       (delete-char 1)
1098       (insert nntp-end-of-line))))
1099
1100 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1101   (set-buffer nntp-server-buffer)
1102   (erase-buffer)
1103   (cond
1104
1105    ;; This server does not talk NOV.
1106    ((not nntp-server-xover)
1107     nil)
1108
1109    ;; We don't care about gaps.
1110    ((or (not nntp-nov-gap)
1111         fetch-old)
1112     (nntp-send-xover-command
1113      (if fetch-old
1114          (if (numberp fetch-old)
1115              (max 1 (- (car articles) fetch-old))
1116            1)
1117        (car articles))
1118      (car (last articles)) 'wait)
1119
1120     (goto-char (point-min))
1121     (when (looking-at "[1-5][0-9][0-9] .*\n")
1122       (delete-region (point) (progn (forward-line 1) (point))))
1123     (while (search-forward "\r" nil t)
1124       (replace-match "" t t))
1125     (goto-char (point-max))
1126     (forward-line -1)
1127     (when (looking-at "\\.")
1128       (delete-region (point) (progn (forward-line 1) (point)))))
1129
1130    ;; We do it the hard way.  For each gap, an XOVER command is sent
1131    ;; to the server.  We do not wait for a reply from the server, we
1132    ;; just send them off as fast as we can.  That means that we have
1133    ;; to count the number of responses we get back to find out when we
1134    ;; have gotten all we asked for.
1135    ((numberp nntp-nov-gap)
1136     (let ((count 0)
1137           (received 0)
1138           last-point
1139           in-process-buffer-p
1140           (buf nntp-server-buffer)
1141           (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1142           first)
1143       ;; We have to check `nntp-server-xover'.  If it gets set to nil,
1144       ;; that means that the server does not understand XOVER, but we
1145       ;; won't know that until we try.
1146       (while (and nntp-server-xover articles)
1147         (setq first (car articles))
1148         ;; Search forward until we find a gap, or until we run out of
1149         ;; articles.
1150         (while (and (cdr articles)
1151                     (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1152           (setq articles (cdr articles)))
1153
1154         (setq in-process-buffer-p (stringp nntp-server-xover))
1155         (nntp-send-xover-command first (car articles))
1156         (setq articles (cdr articles))
1157         
1158         (when (and nntp-server-xover in-process-buffer-p)
1159           ;; Don't count tried request.
1160           (setq count (1+ count))
1161           
1162           ;; Every 400 requests we have to read the stream in
1163           ;; order to avoid deadlocks.
1164           (when (or (null articles)     ;All requests have been sent.
1165                     (zerop (% count nntp-maximum-request)))
1166
1167             (nntp-accept-response)
1168             ;; On some Emacs versions the preceding function has a
1169             ;; tendency to change the buffer.  Perhaps.  It's quite
1170             ;; difficult to reproduce, because it only seems to happen
1171             ;; once in a blue moon.
1172             (set-buffer process-buffer)
1173             (while (progn
1174                      (goto-char (or last-point (point-min)))
1175                      ;; Count replies.
1176                      (while (re-search-forward "^[0-9][0-9][0-9] .*\n" nil t)
1177                        (incf received))
1178                      (setq last-point (point))
1179                      (< received count))
1180               (nntp-accept-response)
1181               (set-buffer process-buffer))
1182             (set-buffer buf))))
1183
1184       (when nntp-server-xover
1185         (when in-process-buffer-p
1186           (set-buffer process-buffer)
1187           ;; Wait for the reply from the final command.
1188           (goto-char (point-max))
1189           (while (not (re-search-backward "^[0-9][0-9][0-9] " nil t))
1190             (nntp-accept-response)
1191             (set-buffer process-buffer)
1192             (goto-char (point-max)))
1193           (when (looking-at "^[23]")
1194             (while (progn
1195                      (goto-char (point-max))
1196                      (forward-line -1)
1197                      (not (looking-at "^\\.\r?\n")))
1198               (nntp-accept-response)
1199               (set-buffer process-buffer)))
1200           (set-buffer buf)
1201           (goto-char (point-max))
1202           (insert-buffer-substring process-buffer)
1203           (set-buffer process-buffer)
1204           (erase-buffer)
1205           (set-buffer buf))
1206
1207         ;; We remove any "." lines and status lines.
1208         (goto-char (point-min))
1209         (while (search-forward "\r" nil t)
1210           (delete-char -1))
1211         (goto-char (point-min))
1212         (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1213         t))))
1214
1215   nntp-server-xover)
1216
1217 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1218   "Send the XOVER command to the server."
1219   (let ((range (format "%d-%d" beg end))
1220         (nntp-inhibit-erase t))
1221     (if (stringp nntp-server-xover)
1222         ;; If `nntp-server-xover' is a string, then we just send this
1223         ;; command.
1224         (if wait-for-reply
1225             (nntp-send-command-nodelete
1226              "\r?\n\\.\r?\n" nntp-server-xover range)
1227           ;; We do not wait for the reply.
1228           (nntp-send-command-nodelete nil nntp-server-xover range))
1229       (let ((commands nntp-xover-commands))
1230         ;; `nntp-xover-commands' is a list of possible XOVER commands.
1231         ;; We try them all until we get at positive response.
1232         (while (and commands (eq nntp-server-xover 'try))
1233           (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1234           (save-excursion
1235             (set-buffer nntp-server-buffer)
1236             (goto-char (point-min))
1237             (and (looking-at "[23]")    ; No error message.
1238                  ;; We also have to look at the lines.  Some buggy
1239                  ;; servers give back simple lines with just the
1240                  ;; article number.  How... helpful.
1241                  (progn
1242                    (forward-line 1)
1243                    (looking-at "[0-9]+\t...")) ; More text after number.
1244                  (setq nntp-server-xover (car commands))))
1245           (setq commands (cdr commands)))
1246         ;; If none of the commands worked, we disable XOVER.
1247         (when (eq nntp-server-xover 'try)
1248           (save-excursion
1249             (set-buffer nntp-server-buffer)
1250             (erase-buffer)
1251             (setq nntp-server-xover nil)))
1252         nntp-server-xover))))
1253
1254 ;;; Alternative connection methods.
1255
1256 (defun nntp-wait-for-string (regexp)
1257   "Wait until string arrives in the buffer."
1258   (let ((buf (current-buffer)))
1259     (goto-char (point-min))
1260     (while (not (re-search-forward regexp nil t))
1261       (accept-process-output (nntp-find-connection nntp-server-buffer))
1262       (set-buffer buf)
1263       (goto-char (point-min)))))
1264
1265 (defun nntp-open-telnet (buffer)
1266   (save-excursion
1267     (set-buffer buffer)
1268     (erase-buffer)
1269     (let ((proc (apply
1270                  'start-process
1271                  "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1272           (case-fold-search t))
1273       (when (memq (process-status proc) '(open run))
1274         (nntp-wait-for-string "^r?telnet")
1275         (process-send-string proc "set escape \^X\n")
1276         (cond
1277          ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1278           (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1279                                             nntp-address "\n")))
1280          (t
1281           (process-send-string proc (concat "open " nntp-address "\n"))))
1282         (cond
1283          ((not nntp-open-telnet-envuser)
1284           (nntp-wait-for-string "^\r*.?login:")
1285           (process-send-string
1286            proc (concat
1287                  (or nntp-telnet-user-name
1288                      (setq nntp-telnet-user-name (read-string "login: ")))
1289                  "\n"))))
1290         (nntp-wait-for-string "^\r*.?password:")
1291         (process-send-string
1292          proc (concat
1293                (or nntp-telnet-passwd
1294                    (setq nntp-telnet-passwd
1295                          (mail-source-read-passwd "Password: ")))
1296                "\n"))
1297         (nntp-wait-for-string nntp-telnet-shell-prompt)
1298         (process-send-string
1299          proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1300         (nntp-wait-for-string "^\r*20[01]")
1301         (beginning-of-line)
1302         (delete-region (point-min) (point))
1303         (process-send-string proc "\^]")
1304         (nntp-wait-for-string "^r?telnet")
1305         (process-send-string proc "mode character\n")
1306         (accept-process-output proc 1)
1307         (sit-for 1)
1308         (goto-char (point-min))
1309         (forward-line 1)
1310         (delete-region (point) (point-max)))
1311       proc)))
1312
1313 (defun nntp-open-rlogin (buffer)
1314   "Open a connection to SERVER using rsh."
1315   (let ((proc (if nntp-rlogin-user-name
1316                   (apply 'start-process
1317                          "nntpd" buffer nntp-rlogin-program
1318                          nntp-address "-l" nntp-rlogin-user-name
1319                          nntp-rlogin-parameters)
1320                 (apply 'start-process
1321                        "nntpd" buffer nntp-rlogin-program nntp-address
1322                        nntp-rlogin-parameters))))
1323     (save-excursion
1324       (set-buffer buffer)
1325       (nntp-wait-for-string "^\r*20[01]")
1326       (beginning-of-line)
1327       (delete-region (point-min) (point))
1328       proc)))
1329
1330 (defun nntp-find-group-and-number ()
1331   (save-excursion
1332     (save-restriction
1333       (set-buffer nntp-server-buffer)
1334       (narrow-to-region (goto-char (point-min))
1335                         (or (search-forward "\n\n" nil t) (point-max)))
1336       (goto-char (point-min))
1337       ;; We first find the number by looking at the status line.
1338       (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1339                          (string-to-int
1340                           (buffer-substring (match-beginning 1)
1341                                             (match-end 1)))))
1342             group newsgroups xref)
1343         (and number (zerop number) (setq number nil))
1344         ;; Then we find the group name.
1345         (setq group
1346               (cond
1347                ;; If there is only one group in the Newsgroups header,
1348                ;; then it seems quite likely that this article comes
1349                ;; from that group, I'd say.
1350                ((and (setq newsgroups (mail-fetch-field "newsgroups"))
1351                      (not (string-match "," newsgroups)))
1352                 newsgroups)
1353                ;; If there is more than one group in the Newsgroups
1354                ;; header, then the Xref header should be filled out.
1355                ;; We hazard a guess that the group that has this
1356                ;; article number in the Xref header is the one we are
1357                ;; looking for.  This might very well be wrong if this
1358                ;; article happens to have the same number in several
1359                ;; groups, but that's life.
1360                ((and (setq xref (mail-fetch-field "xref"))
1361                      number
1362                      (string-match (format "\\([^ :]+\\):%d" number) xref))
1363                 (substring xref (match-beginning 1) (match-end 1)))
1364                (t "")))
1365         (when (string-match "\r" group)
1366           (setq group (substring group 0 (match-beginning 0))))
1367         (cons group number)))))
1368
1369 (provide 'nntp)
1370
1371 ;;; nntp.el ends here