13f877f1b03ff8c6615e4754a80febdff621a737
[elisp/gnus.git-] / 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         (quit nil)))))
356
357 (defsubst nntp-send-command (wait-for &rest strings)
358   "Send STRINGS to server and wait until WAIT-FOR returns."
359   (when (and (not nnheader-callback-function)
360              (not nntp-inhibit-output))
361     (save-excursion
362       (set-buffer nntp-server-buffer)
363       (erase-buffer)))
364   (nntp-retrieve-data
365    (mapconcat 'identity strings " ")
366    nntp-address nntp-port-number nntp-server-buffer
367    wait-for nnheader-callback-function))
368
369 (defun nntp-send-command-nodelete (wait-for &rest strings)
370   "Send STRINGS to server and wait until WAIT-FOR returns."
371   (nntp-retrieve-data
372    (mapconcat 'identity strings " ")
373    nntp-address nntp-port-number nntp-server-buffer
374    wait-for nnheader-callback-function))
375
376 (defun nntp-send-command-and-decode (wait-for &rest strings)
377   "Send STRINGS to server and wait until WAIT-FOR returns."
378   (when (and (not nnheader-callback-function)
379              (not nntp-inhibit-output))
380     (save-excursion
381       (set-buffer nntp-server-buffer)
382       (erase-buffer)))
383   (nntp-retrieve-data
384    (mapconcat 'identity strings " ")
385    nntp-address nntp-port-number nntp-server-buffer
386    wait-for nnheader-callback-function t))
387
388 (defun nntp-send-buffer (wait-for)
389   "Send the current buffer to server and wait until WAIT-FOR returns."
390   (when (and (not nnheader-callback-function)
391              (not nntp-inhibit-output))
392     (save-excursion
393       (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
394       (erase-buffer)))
395   (nntp-encode-text)
396   (process-send-region (nntp-find-connection nntp-server-buffer)
397                        (point-min) (point-max))
398   (nntp-retrieve-data
399    nil nntp-address nntp-port-number nntp-server-buffer
400    wait-for nnheader-callback-function))
401
402 \f
403
404 ;;; Interface functions.
405
406 (nnoo-define-basics nntp)
407
408 (defsubst nntp-next-result-arrived-p ()
409   (cond
410    ;; A result that starts with a 2xx code is terminated by
411    ;; a line with only a "." on it.
412    ((eq (char-after) ?2)
413     (if (re-search-forward "\n\\.\r?\n" nil t)
414         t
415       nil))
416    ;; A result that starts with a 3xx or 4xx code is terminated
417    ;; by a newline.
418    ((looking-at "[34]")
419     (if (search-forward "\n" nil t)
420         t
421       nil))
422    ;; No result here.
423    (t
424     nil)))
425
426 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
427   "Retrieve the headers of ARTICLES."
428   (nntp-possibly-change-group group server)
429   (save-excursion
430     (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
431     (erase-buffer)
432     (if (and (not gnus-nov-is-evil)
433              (not nntp-nov-is-evil)
434              (nntp-retrieve-headers-with-xover articles fetch-old))
435         ;; We successfully retrieved the headers via XOVER.
436         'nov
437       ;; XOVER didn't work, so we do it the hard, slow and inefficient
438       ;; way.
439       (let ((number (length articles))
440             (count 0)
441             (received 0)
442             (last-point (point-min))
443             (buf (nntp-find-connection-buffer nntp-server-buffer))
444             (nntp-inhibit-erase t)
445             article)
446         ;; Send HEAD commands.
447         (while (setq article (pop articles))
448           (nntp-send-command
449            nil
450            "HEAD" (if (numberp article)
451                       (int-to-string article)
452                     ;; `articles' is either a list of article numbers
453                     ;; or a list of article IDs.
454                     article))
455           (incf count)
456           ;; Every 400 requests we have to read the stream in
457           ;; order to avoid deadlocks.
458           (when (or (null articles)     ;All requests have been sent.
459                     (zerop (% count nntp-maximum-request)))
460             (nntp-accept-response)
461             (while (progn
462                      (set-buffer buf)
463                      (goto-char last-point)
464                      ;; Count replies.
465                      (while (nntp-next-result-arrived-p)
466                        (setq last-point (point))
467                        (incf received))
468                      (< received count))
469               ;; If number of headers is greater than 100, give
470               ;;  informative messages.
471               (and (numberp nntp-large-newsgroup)
472                    (> number nntp-large-newsgroup)
473                    (zerop (% received 20))
474                    (nnheader-message 6 "NNTP: Receiving headers... %d%%"
475                                      (/ (* received 100) number)))
476               (nntp-accept-response))))
477         (and (numberp nntp-large-newsgroup)
478              (> number nntp-large-newsgroup)
479              (nnheader-message 6 "NNTP: Receiving headers...done"))
480
481         ;; Now all of replies are received.  Fold continuation lines.
482         (nnheader-fold-continuation-lines)
483         ;; Remove all "\r"'s.
484         (nnheader-strip-cr)
485         (copy-to-buffer nntp-server-buffer (point-min) (point-max))
486         'headers))))
487
488 (deffoo nntp-retrieve-groups (groups &optional server)
489   "Retrieve group info on GROUPS."
490   (nntp-possibly-change-group nil server)
491   (when (nntp-find-connection-buffer nntp-server-buffer)
492     (save-excursion
493       ;; Erase nntp-server-buffer before nntp-inhibit-erase.
494       (set-buffer nntp-server-buffer)
495       (erase-buffer)
496       (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
497       ;; The first time this is run, this variable is `try'.  So we
498       ;; try.
499       (when (eq nntp-server-list-active-group 'try)
500         (nntp-try-list-active (car groups)))
501       (erase-buffer)
502       (let ((count 0)
503             (received 0)
504             (last-point (point-min))
505             (nntp-inhibit-erase t)
506             (buf (nntp-find-connection-buffer nntp-server-buffer))
507             (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
508         (while groups
509           ;; Send the command to the server.
510           (nntp-send-command nil command (pop groups))
511           (incf count)
512           ;; Every 400 requests we have to read the stream in
513           ;; order to avoid deadlocks.
514           (when (or (null groups)       ;All requests have been sent.
515                     (zerop (% count nntp-maximum-request)))
516             (nntp-accept-response)
517             (while (progn
518                      ;; Search `blue moon' in this file for the
519                      ;; reason why set-buffer here.
520                      (set-buffer buf)
521                      (goto-char last-point)
522                      ;; Count replies.
523                      (while (re-search-forward "^[0-9]" nil t)
524                        (incf received))
525                      (setq last-point (point))
526                      (< received count))
527               (nntp-accept-response))))
528
529         ;; Wait for the reply from the final command.
530         (set-buffer buf)
531         (goto-char (point-max))
532         (re-search-backward "^[0-9]" nil t)
533         (when (looking-at "^[23]")
534           (while (progn
535                    (set-buffer buf)
536                    (goto-char (point-max))
537                    (if (not nntp-server-list-active-group)
538                        (not (re-search-backward "\r?\n" (- (point) 3) t))
539                      (not (re-search-backward "^\\.\r?\n" (- (point) 4) t))))
540             (nntp-accept-response)))
541
542         ;; Now all replies are received.  We remove CRs.
543         (set-buffer buf)
544         (goto-char (point-min))
545         (while (search-forward "\r" nil t)
546           (replace-match "" t t))
547
548         (if (not nntp-server-list-active-group)
549             (progn
550               (copy-to-buffer nntp-server-buffer (point-min) (point-max))
551               'group)
552           ;; We have read active entries, so we just delete the
553           ;; superfluous gunk.
554           (goto-char (point-min))
555           (while (re-search-forward "^[.2-5]" nil t)
556             (delete-region (match-beginning 0)
557                            (progn (forward-line 1) (point))))
558           (copy-to-buffer nntp-server-buffer (point-min) (point-max))
559           'active)))))
560
561 (deffoo nntp-retrieve-articles (articles &optional group server)
562   (nntp-possibly-change-group group server)
563   (save-excursion
564     (let ((number (length articles))
565           (count 0)
566           (received 0)
567           (last-point (point-min))
568           (buf (nntp-find-connection-buffer nntp-server-buffer))
569           (nntp-inhibit-erase t)
570           (map (apply 'vector articles))
571           (point 1)
572           article)
573       (set-buffer buf)
574       (erase-buffer)
575       ;; Send ARTICLE command.
576       (while (setq article (pop articles))
577         (nntp-send-command
578          nil
579          "ARTICLE" (if (numberp article)
580                        (int-to-string article)
581                      ;; `articles' is either a list of article numbers
582                      ;; or a list of article IDs.
583                      article))
584         (incf count)
585         ;; Every 400 requests we have to read the stream in
586         ;; order to avoid deadlocks.
587         (when (or (null articles)       ;All requests have been sent.
588                   (zerop (% count nntp-maximum-request)))
589           (nntp-accept-response)
590           (while (progn
591                    (set-buffer buf)
592                    (goto-char last-point)
593                    ;; Count replies.
594                    (while (nntp-next-result-arrived-p)
595                      (aset map received (cons (aref map received) (point)))
596                      (setq last-point (point))
597                      (incf received))
598                    (< received count))
599             ;; If number of headers is greater than 100, give
600             ;;  informative messages.
601             (and (numberp nntp-large-newsgroup)
602                  (> number nntp-large-newsgroup)
603                  (zerop (% received 20))
604                  (nnheader-message 6 "NNTP: Receiving articles... %d%%"
605                                    (/ (* received 100) number)))
606             (nntp-accept-response))))
607       (and (numberp nntp-large-newsgroup)
608            (> number nntp-large-newsgroup)
609            (nnheader-message 6 "NNTP: Receiving articles...done"))
610
611       ;; Now we have all the responses.  We go through the results,
612       ;; wash it and copy it over to the server buffer.
613       (set-buffer nntp-server-buffer)
614       (erase-buffer)
615       (setq last-point (point-min))
616       (mapcar
617        (lambda (entry)
618          (narrow-to-region
619           (setq point (goto-char (point-max)))
620           (progn
621             (insert-buffer-substring buf last-point (cdr entry))
622             (point-max)))
623          (setq last-point (cdr entry))
624          (nntp-decode-text)
625          (widen)
626          (cons (car entry) point))
627        map))))
628
629 (defun nntp-try-list-active (group)
630   (nntp-list-active-group group)
631   (save-excursion
632     (set-buffer nntp-server-buffer)
633     (goto-char (point-min))
634     (cond ((or (eobp)
635                (looking-at "5[0-9]+"))
636            (setq nntp-server-list-active-group nil))
637           (t
638            (setq nntp-server-list-active-group t)))))
639
640 (deffoo nntp-list-active-group (group &optional server)
641   "Return the active info on GROUP (which can be a regexp)."
642   (nntp-possibly-change-group nil server)
643   (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group))
644
645 (deffoo nntp-request-group-articles (group &optional server)
646   "Return the list of existing articles in GROUP."
647   (nntp-possibly-change-group nil server)
648   (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group))
649
650 (deffoo nntp-request-article (article &optional group server buffer command)
651   (nntp-possibly-change-group group server)
652   (when (nntp-send-command-and-decode
653          "\r?\n\\.\r?\n" "ARTICLE"
654          (if (numberp article) (int-to-string article) article))
655     (if (and buffer
656              (not (equal buffer nntp-server-buffer)))
657         (save-excursion
658           (set-buffer nntp-server-buffer)
659           (copy-to-buffer buffer (point-min) (point-max))
660           (nntp-find-group-and-number))
661       (nntp-find-group-and-number))))
662
663 (deffoo nntp-request-head (article &optional group server)
664   (nntp-possibly-change-group group server)
665   (when (nntp-send-command
666          "\r?\n\\.\r?\n" "HEAD"
667          (if (numberp article) (int-to-string article) article))
668     (prog1
669         (nntp-find-group-and-number)
670       (nntp-decode-text))))
671
672 (deffoo nntp-request-body (article &optional group server)
673   (nntp-possibly-change-group group server)
674   (nntp-send-command-and-decode
675    "\r?\n\\.\r?\n" "BODY"
676    (if (numberp article) (int-to-string article) article)))
677
678 (deffoo nntp-request-group (group &optional server dont-check)
679   (nntp-possibly-change-group nil server)
680   (when (nntp-send-command "^[245].*\n" "GROUP" group)
681     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
682       (setcar (cddr entry) group))))
683
684 (deffoo nntp-close-group (group &optional server)
685   t)
686
687 (deffoo nntp-server-opened (&optional server)
688   "Say whether a connection to SERVER has been opened."
689   (and (nnoo-current-server-p 'nntp server)
690        nntp-server-buffer
691        (gnus-buffer-live-p nntp-server-buffer)
692        (nntp-find-connection nntp-server-buffer)))
693
694 (deffoo nntp-open-server (server &optional defs connectionless)
695   (nnheader-init-server-buffer)
696   (if (nntp-server-opened server)
697       t
698     (when (or (stringp (car defs))
699               (numberp (car defs)))
700       (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
701     (unless (assq 'nntp-address defs)
702       (setq defs (append defs (list (list 'nntp-address server)))))
703     (nnoo-change-server 'nntp server defs)
704     (unless connectionless
705       (or (nntp-find-connection nntp-server-buffer)
706           (nntp-open-connection nntp-server-buffer)))))
707
708 (deffoo nntp-close-server (&optional server)
709   (nntp-possibly-change-group nil server t)
710   (let ((process (nntp-find-connection nntp-server-buffer)))
711     (while process
712       (when (memq (process-status process) '(open run))
713         (ignore-errors
714           (nntp-send-string process "QUIT")
715           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
716             ;; Ok, this is evil, but when using telnet and stuff
717             ;; as the connection method, it's important that the
718             ;; QUIT command actually is sent out before we kill
719             ;; the process.
720             (sleep-for 1))))
721       (nntp-kill-buffer (process-buffer process))
722       (setq process (car (pop nntp-connection-alist))))
723     (nnoo-close-server 'nntp)))
724
725 (deffoo nntp-request-close ()
726   (let (process)
727     (while (setq process (pop nntp-connection-list))
728       (when (memq (process-status process) '(open run))
729         (ignore-errors
730           (nntp-send-string process "QUIT")
731           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
732             ;; Ok, this is evil, but when using telnet and stuff
733             ;; as the connection method, it's important that the
734             ;; QUIT command actually is sent out before we kill
735             ;; the process.
736             (sleep-for 1))))
737       (nntp-kill-buffer (process-buffer process)))))
738
739 (deffoo nntp-request-list (&optional server)
740   (nntp-possibly-change-group nil server)
741   (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST"))
742
743 (deffoo nntp-request-list-newsgroups (&optional server)
744   (nntp-possibly-change-group nil server)
745   (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS"))
746
747 (deffoo nntp-request-newgroups (date &optional server)
748   (nntp-possibly-change-group nil server)
749   (save-excursion
750     (set-buffer nntp-server-buffer)
751     (let* ((time (date-to-time date))
752            (ls (- (cadr time) (nth 8 (decode-time time)))))
753       (cond ((< ls 0)
754              (setcar time (1- (car time)))
755              (setcar (cdr time) (+ ls 65536)))
756             ((>= ls 65536)
757              (setcar time (1+ (car time)))
758              (setcar (cdr time) (- ls 65536)))
759             (t
760              (setcar (cdr time) ls)))
761       (prog1
762           (nntp-send-command
763            "^\\.\r?\n" "NEWGROUPS"
764            (format-time-string "%y%m%d %H%M%S" time)
765            "GMT")
766         (nntp-decode-text)))))
767
768 (deffoo nntp-request-post (&optional server)
769   (nntp-possibly-change-group nil server)
770   (when (nntp-send-command "^[23].*\r?\n" "POST")
771     (nntp-send-buffer "^[23].*\n")))
772
773 (deffoo nntp-request-type (group article)
774   'news)
775
776 (deffoo nntp-asynchronous-p ()
777   t)
778
779 ;;; Hooky functions.
780
781 (defun nntp-send-mode-reader ()
782   "Send the MODE READER command to the nntp server.
783 This function is supposed to be called from `nntp-server-opened-hook'.
784 It will make innd servers spawn an nnrpd process to allow actual article
785 reading."
786   (nntp-send-command "^.*\n" "MODE READER"))
787
788 (defun nntp-send-authinfo (&optional send-if-force)
789   "Send the AUTHINFO to the nntp server.
790 It will look in the \"~/.authinfo\" file for matching entries.  If
791 nothing suitable is found there, it will prompt for a user name
792 and a password.
793
794 If SEND-IF-FORCE, only send authinfo to the server if the
795 .authinfo file has the FORCE token."
796   (let* ((list (gnus-parse-netrc nntp-authinfo-file))
797          (alist (gnus-netrc-machine list nntp-address "nntp"))
798          (force (gnus-netrc-get alist "force"))
799          (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
800          (passwd (gnus-netrc-get alist "password")))
801     (when (or (not send-if-force)
802               force)
803       (unless user
804         (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
805               nntp-authinfo-user user))
806       (unless (member user '(nil ""))
807         (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
808         (when t                         ;???Should check if AUTHINFO succeeded
809           (nntp-send-command
810            "^2.*\r?\n" "AUTHINFO PASS"
811            (or passwd
812                nntp-authinfo-password
813                (setq nntp-authinfo-password
814                      (mail-source-read-passwd (format "NNTP (%s@%s) password: "
815                                                       user nntp-address))))))))))
816
817 (defun nntp-send-nosy-authinfo ()
818   "Send the AUTHINFO to the nntp server."
819   (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
820     (unless (member user '(nil ""))
821       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
822       (when t                           ;???Should check if AUTHINFO succeeded
823         (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
824                            (mail-source-read-passwd "NNTP (%s@%s) password: "
825                                                     user nntp-address))))))
826
827 (defun nntp-send-authinfo-from-file ()
828   "Send the AUTHINFO to the nntp server.
829
830 The authinfo login name is taken from the user's login name and the
831 password contained in '~/.nntp-authinfo'."
832   (when (file-exists-p "~/.nntp-authinfo")
833     (with-temp-buffer
834       (insert-file-contents "~/.nntp-authinfo")
835       (goto-char (point-min))
836       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
837       (nntp-send-command
838        "^2.*\r?\n" "AUTHINFO PASS"
839        (buffer-substring (point) (progn (end-of-line) (point)))))))
840
841 ;;; Internal functions.
842
843 (defun nntp-handle-authinfo (process)
844   "Take care of an authinfo response from the server."
845   (let ((last nntp-last-command))
846     (funcall nntp-authinfo-function)
847     ;; We have to re-send the function that was interrupted by
848     ;; the authinfo request.
849     (save-excursion
850       (set-buffer nntp-server-buffer)
851       (erase-buffer))
852     (nntp-send-string process last)))
853
854 (defun nntp-make-process-buffer (buffer)
855   "Create a new, fresh buffer usable for nntp process connections."
856   (save-excursion
857     (set-buffer
858      (generate-new-buffer
859       (format " *server %s %s %s*"
860               nntp-address nntp-port-number
861               (gnus-buffer-exists-p buffer))))
862     (mm-enable-multibyte)
863     (set (make-local-variable 'after-change-functions) nil)
864     (set (make-local-variable 'nntp-process-wait-for) nil)
865     (set (make-local-variable 'nntp-process-callback) nil)
866     (set (make-local-variable 'nntp-process-to-buffer) nil)
867     (set (make-local-variable 'nntp-process-start-point) nil)
868     (set (make-local-variable 'nntp-process-decode) nil)
869     (current-buffer)))
870
871 (defun nntp-open-connection (buffer)
872   "Open a connection to PORT on ADDRESS delivering output to BUFFER."
873   (run-hooks 'nntp-prepare-server-hook)
874   (let* ((pbuffer (nntp-make-process-buffer buffer))
875          (timer
876           (and nntp-connection-timeout
877                (nnheader-run-at-time
878                 nntp-connection-timeout nil
879                 `(lambda ()
880                    (nntp-kill-buffer ,pbuffer)))))
881          (process
882           (condition-case ()
883               (let ((coding-system-for-read nntp-coding-system-for-read)
884                     (coding-system-for-write nntp-coding-system-for-write))
885                 (funcall nntp-open-connection-function pbuffer))
886             (error nil)
887             (quit nil))))
888     (when timer
889       (nnheader-cancel-timer timer))
890     (when (and (buffer-name pbuffer)
891                process)
892       (process-kill-without-query process)
893       (nntp-wait-for process "^.*\n" buffer nil t)
894       (if (memq (process-status process) '(open run))
895           (prog1
896               (caar (push (list process buffer nil) nntp-connection-alist))
897             (push process nntp-connection-list)
898             (save-excursion
899               (set-buffer pbuffer)
900               (nntp-read-server-type)
901               (erase-buffer)
902               (set-buffer nntp-server-buffer)
903               (let ((nnheader-callback-function nil))
904                 (run-hooks 'nntp-server-opened-hook)
905                 (nntp-send-authinfo t))))
906         (nntp-kill-buffer (process-buffer process))
907         nil))))
908
909 (defun nntp-open-network-stream (buffer)
910   (open-network-stream "nntpd" buffer nntp-address nntp-port-number))
911
912 (defun nntp-open-ssl-stream (buffer)
913   (let* ((ssl-program-arguments '("-connect" (concat host ":" service)))
914          (proc (open-ssl-stream "nntpd" buffer nntp-address nntp-port-number)))
915     (save-excursion
916       (set-buffer buffer)
917       (nntp-wait-for-string "^\r*20[01]")
918       (beginning-of-line)
919       (delete-region (point-min) (point))
920       proc)))
921
922 (defun nntp-read-server-type ()
923   "Find out what the name of the server we have connected to is."
924   ;; Wait for the status string to arrive.
925   (setq nntp-server-type (buffer-string))
926   (let ((alist nntp-server-action-alist)
927         (case-fold-search t)
928         entry)
929     ;; Run server-specific commands.
930     (while alist
931       (setq entry (pop alist))
932       (when (string-match (car entry) nntp-server-type)
933         (if (and (listp (cadr entry))
934                  (not (eq 'lambda (caadr entry))))
935             (eval (cadr entry))
936           (funcall (cadr entry)))))))
937
938 (defun nntp-async-wait (process wait-for buffer decode callback)
939   (save-excursion
940     (set-buffer (process-buffer process))
941     (unless nntp-inside-change-function
942       (erase-buffer))
943     (setq nntp-process-wait-for wait-for
944           nntp-process-to-buffer buffer
945           nntp-process-decode decode
946           nntp-process-callback callback
947           nntp-process-start-point (point-max))
948     (setq after-change-functions '(nntp-after-change-function))
949     (if nntp-async-needs-kluge
950         (nntp-async-kluge process))))
951
952 (defun nntp-async-kluge (process)
953   ;; emacs 20.3 bug: process output with encoding 'binary
954   ;; doesn't trigger after-change-functions.
955   (unless nntp-async-timer
956     (setq nntp-async-timer
957           (nnheader-run-at-time 1 1 'nntp-async-timer-handler)))
958   (add-to-list 'nntp-async-process-list process))
959
960 (defun nntp-async-timer-handler ()
961   (mapcar
962    (lambda (proc)
963      (if (memq (process-status proc) '(open run))
964          (nntp-async-trigger proc)
965        (nntp-async-stop proc)))
966    nntp-async-process-list))
967
968 (defun nntp-async-stop (proc)
969   (setq nntp-async-process-list (delq proc nntp-async-process-list))
970   (when (and nntp-async-timer (not nntp-async-process-list))
971     (nnheader-cancel-timer nntp-async-timer)
972     (setq nntp-async-timer nil)))
973
974 (defun nntp-after-change-function (beg end len)
975   (unwind-protect
976       ;; we only care about insertions at eob
977       (when (and (eq 0 len) (eq (point-max) end))
978         (save-match-data
979           (let ((proc (get-buffer-process (current-buffer))))
980             (when proc
981               (nntp-async-trigger proc)))))
982     ;; any throw from after-change-functions will leave it
983     ;; set to nil.  so we reset it here, if necessary.
984     (when quit-flag
985       (setq after-change-functions '(nntp-after-change-function)))))
986
987 (defun nntp-async-trigger (process)
988   (save-excursion
989     (set-buffer (process-buffer process))
990     (when nntp-process-callback
991       ;; do we have an error message?
992       (goto-char nntp-process-start-point)
993       (if (memq (following-char) '(?4 ?5))
994           ;; wants credentials?
995           (if (looking-at "480")
996               (nntp-handle-authinfo nntp-process-to-buffer)
997             ;; report error message.
998             (nntp-snarf-error-message)
999             (nntp-do-callback nil))
1000
1001         ;; got what we expect?
1002         (goto-char (point-max))
1003         (when (re-search-backward
1004                nntp-process-wait-for nntp-process-start-point t)
1005           (nntp-async-stop process)
1006           ;; convert it.
1007           (when (gnus-buffer-exists-p nntp-process-to-buffer)
1008             (let ((buf (current-buffer))
1009                   (start nntp-process-start-point)
1010                   (decode nntp-process-decode))
1011               (save-excursion
1012                 (set-buffer nntp-process-to-buffer)
1013                 (goto-char (point-max))
1014                 (save-restriction
1015                   (narrow-to-region (point) (point))
1016                   (insert-buffer-substring buf start)
1017                   (when decode
1018                     (nntp-decode-text))))))
1019           ;; report it.
1020           (goto-char (point-max))
1021           (nntp-do-callback
1022            (buffer-name (get-buffer nntp-process-to-buffer))))))))
1023
1024 (defun nntp-do-callback (arg)
1025   (let ((callback nntp-process-callback)
1026         (nntp-inside-change-function t))
1027     (setq nntp-process-callback nil)
1028     (funcall callback arg)))
1029
1030 (defun nntp-snarf-error-message ()
1031   "Save the error message in the current buffer."
1032   (let ((message (buffer-string)))
1033     (while (string-match "[\r\n]+" message)
1034       (setq message (replace-match " " t t message)))
1035     (nnheader-report 'nntp message)
1036     message))
1037
1038 (defun nntp-accept-process-output (process &optional timeout)
1039   "Wait for output from PROCESS and message some dots."
1040   (save-excursion
1041     (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1042                     nntp-server-buffer))
1043     (let ((len (/ (point-max) 1024))
1044           message-log-max)
1045       (unless (< len 10)
1046         (setq nntp-have-messaged t)
1047         (nnheader-message 7 "nntp read: %dk" len)))
1048     (accept-process-output process (or timeout 1))))
1049
1050 (defun nntp-accept-response ()
1051   "Wait for output from the process that outputs to BUFFER."
1052   (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1053
1054 (defun nntp-possibly-change-group (group server &optional connectionless)
1055   (let ((nnheader-callback-function nil))
1056     (when server
1057       (or (nntp-server-opened server)
1058           (nntp-open-server server nil connectionless)))
1059
1060     (unless connectionless
1061       (or (nntp-find-connection nntp-server-buffer)
1062           (nntp-open-connection nntp-server-buffer))))
1063
1064   (when group
1065     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1066       (when (not (equal group (caddr entry)))
1067         (save-excursion
1068           (set-buffer (process-buffer (car entry)))
1069           (erase-buffer)
1070           (nntp-send-command "^[245].*\n" "GROUP" group)
1071           (setcar (cddr entry) group)
1072           (erase-buffer)
1073           (set-buffer nntp-server-buffer)
1074           (erase-buffer))))))
1075
1076 (defun nntp-decode-text (&optional cr-only)
1077   "Decode the text in the current buffer."
1078   (goto-char (point-min))
1079   (while (search-forward "\r" nil t)
1080     (delete-char -1))
1081   (unless cr-only
1082     ;; Remove trailing ".\n" end-of-transfer marker.
1083     (goto-char (point-max))
1084     (forward-line -1)
1085     (when (looking-at ".\n")
1086       (delete-char 2))
1087     ;; Delete status line.
1088     (goto-char (point-min))
1089     (delete-region (point) (progn (forward-line 1) (point)))
1090     ;; Remove "." -> ".." encoding.
1091     (while (search-forward "\n.." nil t)
1092       (delete-char -1))))
1093
1094 (defun nntp-encode-text ()
1095   "Encode the text in the current buffer."
1096   (save-excursion
1097     ;; Replace "." at beginning of line with "..".
1098     (goto-char (point-min))
1099     (while (re-search-forward "^\\." nil t)
1100       (insert "."))
1101     (goto-char (point-max))
1102     ;; Insert newline at the end of the buffer.
1103     (unless (bolp)
1104       (insert "\n"))
1105     ;; Insert `.' at end of buffer (end of text mark).
1106     (goto-char (point-max))
1107     (insert ".\n")
1108     (goto-char (point-min))
1109     (while (not (eobp))
1110       (end-of-line)
1111       (delete-char 1)
1112       (insert nntp-end-of-line))))
1113
1114 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1115   (set-buffer nntp-server-buffer)
1116   (erase-buffer)
1117   (cond
1118
1119    ;; This server does not talk NOV.
1120    ((not nntp-server-xover)
1121     nil)
1122
1123    ;; We don't care about gaps.
1124    ((or (not nntp-nov-gap)
1125         fetch-old)
1126     (nntp-send-xover-command
1127      (if fetch-old
1128          (if (numberp fetch-old)
1129              (max 1 (- (car articles) fetch-old))
1130            1)
1131        (car articles))
1132      (car (last articles)) 'wait)
1133
1134     (goto-char (point-min))
1135     (when (looking-at "[1-5][0-9][0-9] .*\n")
1136       (delete-region (point) (progn (forward-line 1) (point))))
1137     (while (search-forward "\r" nil t)
1138       (replace-match "" t t))
1139     (goto-char (point-max))
1140     (forward-line -1)
1141     (when (looking-at "\\.")
1142       (delete-region (point) (progn (forward-line 1) (point)))))
1143
1144    ;; We do it the hard way.  For each gap, an XOVER command is sent
1145    ;; to the server.  We do not wait for a reply from the server, we
1146    ;; just send them off as fast as we can.  That means that we have
1147    ;; to count the number of responses we get back to find out when we
1148    ;; have gotten all we asked for.
1149    ((numberp nntp-nov-gap)
1150     (let ((count 0)
1151           (received 0)
1152           last-point
1153           in-process-buffer-p
1154           (buf nntp-server-buffer)
1155           (process-buffer (nntp-find-connection-buffer nntp-server-buffer))
1156           first)
1157       ;; We have to check `nntp-server-xover'.  If it gets set to nil,
1158       ;; that means that the server does not understand XOVER, but we
1159       ;; won't know that until we try.
1160       (while (and nntp-server-xover articles)
1161         (setq first (car articles))
1162         ;; Search forward until we find a gap, or until we run out of
1163         ;; articles.
1164         (while (and (cdr articles)
1165                     (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1166           (setq articles (cdr articles)))
1167
1168         (setq in-process-buffer-p (stringp nntp-server-xover))
1169         (nntp-send-xover-command first (car articles))
1170         (setq articles (cdr articles))
1171         
1172         (when (and nntp-server-xover in-process-buffer-p)
1173           ;; Don't count tried request.
1174           (setq count (1+ count))
1175           
1176           ;; Every 400 requests we have to read the stream in
1177           ;; order to avoid deadlocks.
1178           (when (or (null articles)     ;All requests have been sent.
1179                     (zerop (% count nntp-maximum-request)))
1180
1181             (nntp-accept-response)
1182             ;; On some Emacs versions the preceding function has a
1183             ;; tendency to change the buffer.  Perhaps.  It's quite
1184             ;; difficult to reproduce, because it only seems to happen
1185             ;; once in a blue moon.
1186             (set-buffer process-buffer)
1187             (while (progn
1188                      (goto-char (or last-point (point-min)))
1189                      ;; Count replies.
1190                      (while (re-search-forward "^[0-9][0-9][0-9] .*\n" nil t)
1191                        (incf received))
1192                      (setq last-point (point))
1193                      (< received count))
1194               (nntp-accept-response)
1195               (set-buffer process-buffer))
1196             (set-buffer buf))))
1197
1198       (when nntp-server-xover
1199         (when in-process-buffer-p
1200           (set-buffer process-buffer)
1201           ;; Wait for the reply from the final command.
1202           (goto-char (point-max))
1203           (while (not (re-search-backward "^[0-9][0-9][0-9] " nil t))
1204             (nntp-accept-response)
1205             (set-buffer process-buffer)
1206             (goto-char (point-max)))
1207           (when (looking-at "^[23]")
1208             (while (progn
1209                      (goto-char (point-max))
1210                      (forward-line -1)
1211                      (not (looking-at "^\\.\r?\n")))
1212               (nntp-accept-response)
1213               (set-buffer process-buffer)))
1214           (set-buffer buf)
1215           (goto-char (point-max))
1216           (insert-buffer-substring process-buffer)
1217           (set-buffer process-buffer)
1218           (erase-buffer)
1219           (set-buffer buf))
1220
1221         ;; We remove any "." lines and status lines.
1222         (goto-char (point-min))
1223         (while (search-forward "\r" nil t)
1224           (delete-char -1))
1225         (goto-char (point-min))
1226         (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1227         t))))
1228
1229   nntp-server-xover)
1230
1231 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1232   "Send the XOVER command to the server."
1233   (let ((range (format "%d-%d" beg end))
1234         (nntp-inhibit-erase t))
1235     (if (stringp nntp-server-xover)
1236         ;; If `nntp-server-xover' is a string, then we just send this
1237         ;; command.
1238         (if wait-for-reply
1239             (nntp-send-command-nodelete
1240              "\r?\n\\.\r?\n" nntp-server-xover range)
1241           ;; We do not wait for the reply.
1242           (nntp-send-command-nodelete nil nntp-server-xover range))
1243       (let ((commands nntp-xover-commands))
1244         ;; `nntp-xover-commands' is a list of possible XOVER commands.
1245         ;; We try them all until we get at positive response.
1246         (while (and commands (eq nntp-server-xover 'try))
1247           (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1248           (save-excursion
1249             (set-buffer nntp-server-buffer)
1250             (goto-char (point-min))
1251             (and (looking-at "[23]")    ; No error message.
1252                  ;; We also have to look at the lines.  Some buggy
1253                  ;; servers give back simple lines with just the
1254                  ;; article number.  How... helpful.
1255                  (progn
1256                    (forward-line 1)
1257                    (looking-at "[0-9]+\t...")) ; More text after number.
1258                  (setq nntp-server-xover (car commands))))
1259           (setq commands (cdr commands)))
1260         ;; If none of the commands worked, we disable XOVER.
1261         (when (eq nntp-server-xover 'try)
1262           (save-excursion
1263             (set-buffer nntp-server-buffer)
1264             (erase-buffer)
1265             (setq nntp-server-xover nil)))
1266         nntp-server-xover))))
1267
1268 ;;; Alternative connection methods.
1269
1270 (defun nntp-wait-for-string (regexp)
1271   "Wait until string arrives in the buffer."
1272   (let ((buf (current-buffer)))
1273     (goto-char (point-min))
1274     (while (not (re-search-forward regexp nil t))
1275       (accept-process-output (nntp-find-connection nntp-server-buffer))
1276       (set-buffer buf)
1277       (goto-char (point-min)))))
1278
1279 (defun nntp-open-telnet (buffer)
1280   (save-excursion
1281     (set-buffer buffer)
1282     (erase-buffer)
1283     (let ((proc (apply
1284                  'start-process
1285                  "nntpd" buffer nntp-telnet-command nntp-telnet-switches))
1286           (case-fold-search t))
1287       (when (memq (process-status proc) '(open run))
1288         (process-send-string proc "set escape \^X\n")
1289         (cond
1290          ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1291           (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1292                                             nntp-address "\n")))
1293          (t
1294           (process-send-string proc (concat "open " nntp-address "\n"))))
1295         (cond
1296          ((not nntp-open-telnet-envuser)
1297           (nntp-wait-for-string "^\r*.?login:")
1298           (process-send-string
1299            proc (concat
1300                  (or nntp-telnet-user-name
1301                      (setq nntp-telnet-user-name (read-string "login: ")))
1302                  "\n"))))
1303         (nntp-wait-for-string "^\r*.?password:")
1304         (process-send-string
1305          proc (concat
1306                (or nntp-telnet-passwd
1307                    (setq nntp-telnet-passwd
1308                          (mail-source-read-passwd "Password: ")))
1309                "\n"))
1310         (nntp-wait-for-string nntp-telnet-shell-prompt)
1311         (process-send-string
1312          proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1313         (nntp-wait-for-string "^\r*20[01]")
1314         (beginning-of-line)
1315         (delete-region (point-min) (point))
1316         (process-send-string proc "\^]")
1317         (nntp-wait-for-string "^telnet")
1318         (process-send-string proc "mode character\n")
1319         (accept-process-output proc 1)
1320         (sit-for 1)
1321         (goto-char (point-min))
1322         (forward-line 1)
1323         (delete-region (point) (point-max)))
1324       proc)))
1325
1326 (defun nntp-open-rlogin (buffer)
1327   "Open a connection to SERVER using rsh."
1328   (let ((proc (if nntp-rlogin-user-name
1329                   (apply 'start-process
1330                          "nntpd" buffer nntp-rlogin-program
1331                          nntp-address "-l" nntp-rlogin-user-name
1332                          nntp-rlogin-parameters)
1333                 (apply 'start-process
1334                        "nntpd" buffer nntp-rlogin-program nntp-address
1335                        nntp-rlogin-parameters))))
1336     (save-excursion
1337       (set-buffer buffer)
1338       (nntp-wait-for-string "^\r*20[01]")
1339       (beginning-of-line)
1340       (delete-region (point-min) (point))
1341       proc)))
1342
1343 (defun nntp-find-group-and-number ()
1344   (save-excursion
1345     (save-restriction
1346       (set-buffer nntp-server-buffer)
1347       (narrow-to-region (goto-char (point-min))
1348                         (or (search-forward "\n\n" nil t) (point-max)))
1349       (goto-char (point-min))
1350       ;; We first find the number by looking at the status line.
1351       (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1352                          (string-to-int
1353                           (buffer-substring (match-beginning 1)
1354                                             (match-end 1)))))
1355             group newsgroups xref)
1356         (and number (zerop number) (setq number nil))
1357         ;; Then we find the group name.
1358         (setq group
1359               (cond
1360                ;; If there is only one group in the Newsgroups header,
1361                ;; then it seems quite likely that this article comes
1362                ;; from that group, I'd say.
1363                ((and (setq newsgroups (mail-fetch-field "newsgroups"))
1364                      (not (string-match "," newsgroups)))
1365                 newsgroups)
1366                ;; If there is more than one group in the Newsgroups
1367                ;; header, then the Xref header should be filled out.
1368                ;; We hazard a guess that the group that has this
1369                ;; article number in the Xref header is the one we are
1370                ;; looking for.  This might very well be wrong if this
1371                ;; article happens to have the same number in several
1372                ;; groups, but that's life.
1373                ((and (setq xref (mail-fetch-field "xref"))
1374                      number
1375                      (string-match (format "\\([^ :]+\\):%d" number) xref))
1376                 (substring xref (match-beginning 1) (match-end 1)))
1377                (t "")))
1378         (when (string-match "\r" group)
1379           (setq group (substring group 0 (match-beginning 0))))
1380         (cons group number)))))
1381
1382 (provide 'nntp)
1383
1384 ;;; nntp.el ends here