0185a20a54173a7cb34a909b6ef00b01f25547d9
[gnus] / lisp / nntp.el
1 ;;; nntp.el --- nntp access for Gnus
2 ;; Copyright (C) 1987,88,89,90,92,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'nnheader)
30 (require 'nnoo)
31 (eval-when-compile (require 'cl))
32
33 (nnoo-declare nntp)
34
35 (eval-and-compile
36   (unless (fboundp 'open-network-stream)
37     (require 'tcp)))
38
39 (eval-when-compile (require 'cl))
40
41 (eval-and-compile
42   (autoload 'cancel-timer "timer")
43   (autoload 'telnet "telnet" nil t)
44   (autoload 'telnet-send-input "telnet" nil t)
45   (autoload 'timezone-parse-date "timezone"))
46
47 (defvoo nntp-server-hook nil
48   "*Hooks for the NNTP server.
49 If the kanji code of the NNTP server is different from the local kanji
50 code, the correct kanji code of the buffer associated with the NNTP
51 server must be specified as follows:
52
53 \(setq nntp-server-hook
54       (function
55        (lambda ()
56          ;; Server's Kanji code is EUC (NEmacs hack).
57          (make-local-variable 'kanji-fileio-code)
58          (setq kanji-fileio-code 0))))
59
60 If you'd like to change something depending on the server in this
61 hook, use the variable `nntp-address'.")
62
63 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
64   "*Hook used for sending commands to the server at startup.  
65 The default value is `nntp-send-mode-reader', which makes an innd
66 server spawn an nnrpd server.  Another useful function to put in this
67 hook might be `nntp-send-authinfo', which will prompt for a password
68 to allow posting from the server.  Note that this is only necessary to
69 do on servers that use strict access control.")  
70 (add-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)
71
72 (defvoo nntp-server-action-alist 
73   '(("nntpd 1\\.5\\.11t" 
74      (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)))
75   "Alist of regexps to match on server types and actions to be taken.
76 For instance, if you want Gnus to beep every time you connect
77 to innd, you could say something like:
78
79 \(setq nntp-server-action-alist
80        '((\"innd\" (ding))))
81
82 You probably don't want to do that, though.")
83
84 (defvoo nntp-open-server-function 'nntp-open-network-stream
85   "*Function used for connecting to a remote system.
86 It will be called with the address of the remote system.
87
88 Two pre-made functions are `nntp-open-network-stream', which is the
89 default, and simply connects to some port or other on the remote
90 system (see nntp-port-number).  The other is `nntp-open-rlogin', which
91 does an rlogin on the remote system, and then does a telnet to the
92 NNTP server available there (see nntp-rlogin-parameters).")
93
94 (defvoo nntp-rlogin-parameters '("telnet" "${NNTPSERVER:=localhost}" "nntp")
95   "*Parameters to `nntp-open-login'.
96 That function may be used as `nntp-open-server-function'.  In that
97 case, this list will be used as the parameter list given to rsh.")
98
99 (defvoo nntp-rlogin-user-name nil
100   "*User name on remote system when using the rlogin connect method.")
101
102 (defvoo nntp-address nil
103   "*The name of the NNTP server.")
104
105 (defvoo nntp-port-number "nntp"
106   "*Port number to connect to.")
107
108 (defvoo nntp-end-of-line "\r\n"
109   "String to use on the end of lines when talking to the NNTP server.
110 This is \"\\r\\n\" by default, but should be \"\\n\" when
111 using rlogin to communicate with the server.")
112
113 (defvoo nntp-large-newsgroup 50
114   "*The number of the articles which indicates a large newsgroup.
115 If the number of the articles is greater than the value, verbose
116 messages will be shown to indicate the current status.")
117
118 (defvoo nntp-buggy-select (memq system-type '(fujitsu-uts))
119   "*t if your select routine is buggy.
120 If the select routine signals error or fall into infinite loop while
121 waiting for the server response, the variable must be set to t.  In
122 case of Fujitsu UTS, it is set to T since `accept-process-output'
123 doesn't work properly.")
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-debug-read 10000
131   "*Display '...' every 10Kbytes of a message being received if it is non-nil.
132 If it is a number, dots are displayed per the number.")
133
134 (defvoo nntp-nov-is-evil nil
135   "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
136
137 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
138   "*List of strings that are used as commands to fetch NOV lines from a server.
139 The strings are tried in turn until a positive response is gotten. If
140 none of the commands are successful, nntp will just grab headers one
141 by one.")
142
143 (defvoo nntp-nov-gap 20
144   "*Maximum allowed gap between two articles.
145 If the gap between two consecutive articles is bigger than this
146 variable, split the XOVER request into two requests.")
147
148 (defvoo nntp-connection-timeout nil
149   "*Number of seconds to wait before an nntp connection times out.
150 If this variable is nil, which is the default, no timers are set.")
151
152 (defvoo nntp-command-timeout nil
153   "*Number of seconds to wait for a response when sending a command.
154 If this variable is nil, which is the default, no timers are set.")
155
156 (defvoo nntp-retry-on-break nil
157   "*If non-nil, re-send the command when the user types `C-g'.")
158
159 (defvoo nntp-news-default-headers nil
160   "*If non-nil, override `mail-default-headers' when posting news.")
161
162 (defvoo nntp-prepare-server-hook nil
163   "*Hook run before a server is opened.
164 If can be used to set up a server remotely, for instance.  Say you
165 have an account at the machine \"other.machine\".  This machine has
166 access to an NNTP server that you can't access locally.  You could
167 then use this hook to rsh to the remote machine and start a proxy NNTP
168 server there that you can connect to.")
169
170 (defvoo nntp-async-number 5
171   "*How many articles should be prefetched when in asynchronous mode.")
172
173 (defvoo nntp-warn-about-losing-connection t
174   "*If non-nil, beep when a server closes connection.")
175
176 \f
177
178 (defconst nntp-version "nntp 4.0"
179   "Version numbers of this version of NNTP.")
180
181 (defvar nntp-server-buffer nil
182   "Buffer associated with the NNTP server process.")
183
184 (defvoo nntp-server-process nil
185   "The NNTP server process.
186 You'd better not use this variable in NNTP front-end program, but
187 instead use `nntp-server-buffer'.")
188
189 (defvoo nntp-status-string nil
190   "Save the server response message.")
191
192 (defvar nntp-opened-connections nil
193   "All (possibly) opened connections.")
194
195 (defvoo nntp-server-xover 'try)
196 (defvoo nntp-server-list-active-group 'try)
197 (defvoo nntp-current-group "")
198 (defvoo nntp-server-type nil)
199
200 (defvoo nntp-async-process nil)
201 (defvoo nntp-async-buffer nil)
202 (defvoo nntp-async-articles nil)
203 (defvoo nntp-async-fetched nil)
204 (defvoo nntp-async-group-alist nil)
205
206 \f
207 ;;; Interface functions.
208
209 (nnoo-define-basics nntp)
210
211 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
212   "Retrieve the headers of ARTICLES."
213   (nntp-possibly-change-server group server)
214   (save-excursion
215     (set-buffer nntp-server-buffer)
216     (erase-buffer)
217     (if (and (not gnus-nov-is-evil) 
218              (not nntp-nov-is-evil)
219              (nntp-retrieve-headers-with-xover articles fetch-old))
220         ;; We successfully retrieved the headers via XOVER.
221         'nov
222       ;; XOVER didn't work, so we do it the hard, slow and inefficient
223       ;; way.  
224       (let ((number (length articles))
225             (count 0)
226             (received 0)
227             (message-log-max nil)
228             (last-point (point-min)))
229         ;; Send HEAD command.
230         (while articles
231           (nntp-send-strings-to-server 
232            "HEAD" (if (numberp (car articles)) 
233                       (int-to-string (car articles))
234                     ;; `articles' is either a list of article numbers
235                     ;; or a list of article IDs.
236                     (car articles)))
237           (setq articles (cdr articles)
238                 count (1+ count))
239           ;; Every 400 header requests we have to read the stream in
240           ;; order to avoid deadlocks.
241           (when (or (null articles)     ;All requests have been sent.
242                     (zerop (% count nntp-maximum-request)))
243             (nntp-accept-response)
244             (while (progn
245                      (goto-char last-point)
246                      ;; Count replies.
247                      (while (re-search-forward "^[0-9]" nil t)
248                        (setq received (1+ received)))
249                      (setq last-point (point))
250                      (< received count))
251               ;; If number of headers is greater than 100, give
252               ;;  informative messages.
253               (and (numberp nntp-large-newsgroup)
254                    (> number nntp-large-newsgroup)
255                    (zerop (% received 20))
256                    (nnheader-message 7 "NNTP: Receiving headers... %d%%"
257                                      (/ (* received 100) number)))
258               (nntp-accept-response))))
259         ;; Wait for text of last command.
260         (goto-char (point-max))
261         (re-search-backward "^[0-9]" nil t)
262         (when (looking-at "^[23]")
263           (while (progn
264                    (goto-char (- (point-max) 3))
265                    (not (looking-at "^\\.\r?\n")))
266             (nntp-accept-response)))
267         (and (numberp nntp-large-newsgroup)
268              (> number nntp-large-newsgroup)
269              (nnheader-message 7 "NNTP: Receiving headers...done"))
270
271         ;; Now all of replies are received.  Fold continuation lines.
272         (nnheader-fold-continuation-lines)
273         ;; Remove all "\r"'s.
274         (goto-char (point-min))
275         (while (search-forward "\r" nil t)
276           (replace-match "" t t))
277         'headers))))
278
279
280 (deffoo nntp-retrieve-groups (groups &optional server)
281   "Retrieve group info on GROUPS."
282   (nntp-possibly-change-server nil server)
283   (save-excursion
284     (set-buffer nntp-server-buffer)
285     ;; The first time this is run, this variable is `try'.  So we
286     ;; try.   
287     (when (eq nntp-server-list-active-group 'try)
288       (nntp-try-list-active (car groups)))
289     (erase-buffer)
290     (let ((count 0)
291           (received 0)
292           (last-point (point-min))
293           (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
294       (while groups
295         ;; Send the command to the server.
296         (nntp-send-strings-to-server command (car groups))
297         (setq groups (cdr groups))
298         (setq count (1+ count))
299         ;; Every 400 requests we have to read the stream in
300         ;; order to avoid deadlocks.
301         (when (or (null groups)         ;All requests have been sent.
302                   (zerop (% count nntp-maximum-request)))
303           (nntp-accept-response)
304           (while (progn
305                    (goto-char last-point)
306                    ;; Count replies.
307                    (while (re-search-forward "^[0-9]" nil t)
308                      (setq received (1+ received)))
309                    (setq last-point (point))
310                    (< received count))
311             (nntp-accept-response))))
312
313       ;; Wait for the reply from the final command.
314       (when nntp-server-list-active-group
315         (goto-char (point-max))
316         (re-search-backward "^[0-9]" nil t)
317         (when (looking-at "^[23]")
318           (while (progn
319                    (goto-char (- (point-max) 3))
320                    (not (looking-at "^\\.\r?\n")))
321             (nntp-accept-response))))
322
323       ;; Now all replies are received. We remove CRs.
324       (goto-char (point-min))
325       (while (search-forward "\r" nil t)
326         (replace-match "" t t))
327
328       (if (not nntp-server-list-active-group)
329           'group
330         ;; We have read active entries, so we just delete the
331         ;; superfluos gunk.
332         (goto-char (point-min))
333         (while (re-search-forward "^[.2-5]" nil t)
334           (delete-region (match-beginning 0) 
335                          (progn (forward-line 1) (point))))
336         'active))))
337
338 (deffoo nntp-open-server (server &optional defs connectionless)
339   "Open the virtual server SERVER.
340 If CONNECTIONLESS is non-nil, don't attempt to connect to any physical
341 servers."
342   ;; Called with just a port number as the defs.
343   (when (or (stringp (car defs))
344             (numberp (car defs)))
345     (setq defs `((nntp-port-number ,(car defs)))))
346   (unless (assq 'nntp-address defs)
347     (setq defs (append defs `((nntp-address ,server)))))
348   (nnoo-change-server 'nntp server defs)
349   (if (nntp-server-opened server)
350       t
351     (or (nntp-server-opened server)
352         connectionless
353         (prog2
354             (run-hooks 'nntp-prepare-server-hook)
355             (nntp-open-server-semi-internal nntp-address nntp-port-number)
356           (nnheader-insert "")))))
357
358 (deffoo nntp-close-server (&optional server)
359   "Close connection to SERVER."
360   (nntp-possibly-change-server nil server t)
361   (unwind-protect
362       (progn
363         ;; Un-set default sentinel function before closing connection.
364         (and nntp-server-process
365              (eq 'nntp-default-sentinel
366                  (process-sentinel nntp-server-process))
367              (set-process-sentinel nntp-server-process nil))
368         ;; We cannot send QUIT command unless the process is running.
369         (when (nntp-server-opened server)
370           (nntp-send-command nil "QUIT")
371           ;; Give the QUIT time to arrive.
372           (sleep-for 1)))
373     (nntp-close-server-internal server)))
374
375 (deffoo nntp-request-close ()
376   "Close all server connections."
377   (let (proc)
378     (while nntp-opened-connections
379       (when (setq proc (pop nntp-opened-connections))
380         ;; Un-set default sentinel function before closing connection.
381         (when (eq 'nntp-default-sentinel (process-sentinel proc))
382           (set-process-sentinel proc nil))
383         (condition-case ()
384             (process-send-string proc (concat "QUIT" nntp-end-of-line))
385           (error nil))
386         ;; Give the QUIT time to reach the server before we close
387         ;; down the process.
388         (sleep-for 1)
389         (delete-process proc)))
390     (and nntp-async-buffer
391          (buffer-name nntp-async-buffer)
392          (kill-buffer nntp-async-buffer))
393     (let ((alist (cddr (assq 'nntp nnoo-state-alist)))
394           entry)
395       (while (setq entry (pop alist))
396         (and (setq proc (cdr (assq 'nntp-async-buffer entry)))
397              (buffer-name proc)
398              (kill-buffer proc))))
399     (nnoo-close-server 'nntp)
400     (setq nntp-async-group-alist nil)))
401
402 (deffoo nntp-server-opened (&optional server)
403   "Say whether a connection to SERVER has been opened."
404   (and (nnoo-current-server-p 'nntp server)
405        nntp-server-buffer
406        (buffer-name nntp-server-buffer)
407        nntp-server-process
408        (memq (process-status nntp-server-process) '(open run))))
409
410 (deffoo nntp-status-message (&optional server)
411   "Return server status as a string."
412   (if (and nntp-status-string
413            ;; NNN MESSAGE
414            (string-match "[0-9][0-9][0-9][ \t]+\\([^\r]*\\).*$"
415                          nntp-status-string))
416       (substring nntp-status-string (match-beginning 1) (match-end 1))
417     ;; Empty message if nothing.
418     (or nntp-status-string "")))
419
420 (deffoo nntp-request-article (id &optional group server buffer)
421   "Request article ID (Message-ID or number)."
422   (nntp-possibly-change-server group server)
423
424   (let (found)
425
426     ;; First we see whether we can get the article from the async buffer. 
427     (when (and (numberp id)
428                nntp-async-articles
429                (memq id nntp-async-fetched))
430       (save-excursion
431         (set-buffer nntp-async-buffer)
432         (let ((opoint (point))
433               (art (if (numberp id) (int-to-string id) id))
434               beg end)
435           (when (and (or (re-search-forward (concat "^2.. +" art) nil t)
436                          (progn
437                            (goto-char (point-min))
438                            (re-search-forward (concat "^2.. +" art) opoint t)))
439                      (progn
440                        (beginning-of-line)
441                        (setq beg (point)
442                              end (re-search-forward "^\\.\r?\n" nil t))))
443             (setq found t)
444             (save-excursion
445               (set-buffer (or buffer nntp-server-buffer))
446               (erase-buffer)
447               (insert-buffer-substring nntp-async-buffer beg end)
448               (let ((nntp-server-buffer (current-buffer)))
449                 (nntp-decode-text)))
450             (delete-region beg end)
451             (when nntp-async-articles
452               (nntp-async-fetch-articles id))))))
453
454     (if found 
455         id
456       ;; The article was not in the async buffer, so we fetch it now.
457       (unwind-protect
458           (progn
459             (if buffer (set-process-buffer nntp-server-process buffer))
460             (let ((nntp-server-buffer (or buffer nntp-server-buffer))
461                   (art (or (and (numberp id) (int-to-string id)) id)))
462               (prog1
463                   (and (nntp-send-command 
464                         ;; A bit odd regexp to ensure working over rlogin.
465                         "^\\.\r?\n" "ARTICLE" art)
466                        (if (numberp id) 
467                            (cons nntp-current-group id)
468                          ;; We find out what the article number was.
469                          (nntp-find-group-and-number)))
470                 (nntp-decode-text)
471                 (and nntp-async-articles (nntp-async-fetch-articles id)))))
472         (when buffer 
473           (set-process-buffer nntp-server-process nntp-server-buffer))))))
474
475 (deffoo nntp-request-body (id &optional group server)
476   "Request body of article ID (Message-ID or number)."
477   (nntp-possibly-change-server group server)
478   (prog1
479       ;; If NEmacs, end of message may look like: "\256\215" (".^M")
480       (nntp-send-command
481        "^\\.\r?\n" "BODY" (or (and (numberp id) (int-to-string id)) id))
482     (nntp-decode-text)))
483
484 (deffoo nntp-request-head (id &optional group server)
485   "Request head of article ID (Message-ID or number)."
486   (nntp-possibly-change-server group server)
487   (prog1
488       (when (nntp-send-command 
489              "^\\.\r?\n" "HEAD" (if (numberp id) (int-to-string id) id))
490         (if (numberp id) id
491           ;; We find out what the article number was.
492           (nntp-find-group-and-number)))
493     (nntp-decode-text)))
494
495 (deffoo nntp-request-stat (id &optional group server)
496   "Request STAT of article ID (Message-ID or number)."
497   (nntp-possibly-change-server group server)
498   (nntp-send-command 
499    "^[23].*\r?\n" "STAT" (or (and (numberp id) (int-to-string id)) id)))
500
501 (deffoo nntp-request-type (group &optional article)
502   'news)
503
504 (deffoo nntp-request-group (group &optional server dont-check)
505   "Select GROUP."
506   (nntp-possibly-change-server nil server)
507   (setq nntp-current-group
508         (when (nntp-send-command "^2.*\r?\n" "GROUP" group)
509           group)))
510
511 (deffoo nntp-request-asynchronous (group &optional server articles)
512   "Enable pre-fetch in GROUP."
513   (when nntp-async-articles
514     (nntp-async-request-group group))
515   (when nntp-async-number
516     (if (not (or (nntp-async-server-opened)
517                  (nntp-async-open-server)))
518         ;; Couldn't open the second connection
519         (progn
520           (message "Can't open second connection to %s" nntp-address)
521           (ding)
522           (setq nntp-async-articles nil)
523           (sit-for 2))
524       ;; We opened the second connection (or it was opened already).  
525       (setq nntp-async-articles articles)
526       (setq nntp-async-fetched nil)
527       ;; Clear any old data.
528       (save-excursion
529         (set-buffer nntp-async-buffer)
530         (erase-buffer))
531       ;; Select the correct current group on this server.
532       (nntp-async-send-strings "GROUP" group)
533       t)))
534
535 (deffoo nntp-list-active-group (group &optional server)
536   "Return the active info on GROUP (which can be a regexp."
537   (nntp-possibly-change-server group server)
538   (nntp-send-command "^.*\r?\n" "LIST ACTIVE" group))
539
540 (deffoo nntp-request-group-description (group &optional server)
541   "Get the description of GROUP."
542   (nntp-possibly-change-server nil server)
543   (prog1
544       (nntp-send-command "^.*\r?\n" "XGTITLE" group)
545     (nntp-decode-text)))
546
547 (deffoo nntp-close-group (group &optional server)
548   "Close GROUP."
549   (setq nntp-current-group nil)
550   t)
551
552 (deffoo nntp-request-list (&optional server)
553   "List all active groups."
554   (nntp-possibly-change-server nil server)
555   (prog1
556       (nntp-send-command "^\\.\r?\n" "LIST")
557     (nntp-decode-text)))
558
559 (deffoo nntp-request-list-newsgroups (&optional server)
560   "Get descriptions on all groups on SERVER."
561   (nntp-possibly-change-server nil server)
562   (prog1
563       (nntp-send-command "^\\.\r?\n" "LIST NEWSGROUPS")
564     (nntp-decode-text)))
565
566 (deffoo nntp-request-newgroups (date &optional server)
567   "List groups that have arrived since DATE."
568   (nntp-possibly-change-server nil server)
569   (let* ((date (timezone-parse-date date))
570          (time-string
571           (format "%s%02d%02d %s%s%s"
572                   (substring (aref date 0) 2) (string-to-int (aref date 1)) 
573                   (string-to-int (aref date 2)) (substring (aref date 3) 0 2)
574                   (substring 
575                    (aref date 3) 3 5) (substring (aref date 3) 6 8))))
576     (prog1
577         (nntp-send-command "^\\.\r?\n" "NEWGROUPS" time-string)
578       (nntp-decode-text))))
579
580 (deffoo nntp-request-list-distributions (&optional server)
581   "List distributions."
582   (nntp-possibly-change-server nil server)
583   (prog1
584       (nntp-send-command "^\\.\r?\n" "LIST DISTRIBUTIONS")
585     (nntp-decode-text)))
586
587 (deffoo nntp-request-last (&optional group server)
588   "Decrease the current article pointer."
589   (nntp-possibly-change-server group server)
590   (nntp-send-command "^[23].*\r?\n" "LAST"))
591
592 (deffoo nntp-request-next (&optional group server)
593   "Advance the current article pointer."
594   (nntp-possibly-change-server group server)
595   (nntp-send-command "^[23].*\r?\n" "NEXT"))
596
597 (deffoo nntp-request-post (&optional server)
598   "Post the current buffer."
599   (nntp-possibly-change-server nil server)
600   (when (nntp-send-command "^[23].*\r?\n" "POST")
601     (nnheader-insert "")
602     (nntp-encode-text)
603     (nntp-send-region-to-server (point-min) (point-max))
604     ;; 1.2a NNTP's post command is buggy. "^M" (\r) is not
605     ;;  appended to end of the status message.
606     (nntp-wait-for-response "^[23].*\n")))
607
608 ;;; Internal functions.
609
610 (defun nntp-send-mode-reader ()
611   "Send the MODE READER command to the nntp server.
612 This function is supposed to be called from `nntp-server-opened-hook'.
613 It will make innd servers spawn an nnrpd process to allow actual article
614 reading."
615   (nntp-send-command "^.*\r?\n" "MODE READER"))
616
617 (defun nntp-send-nosy-authinfo ()
618   "Send the AUTHINFO to the nntp server.
619 This function is supposed to be called from `nntp-server-opened-hook'.
620 It will prompt for a password."
621   (nntp-send-command "^.*\r?\n" "AUTHINFO USER"
622                      (read-string "NNTP user name: "))
623   (nntp-send-command "^.*\r?\n" "AUTHINFO PASS" 
624                      (read-string "NNTP password: ")))
625
626 (defun nntp-send-authinfo ()
627   "Send the AUTHINFO to the nntp server.
628 This function is supposed to be called from `nntp-server-opened-hook'.
629 It will prompt for a password."
630   (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name))
631   (nntp-send-command "^.*\r?\n" "AUTHINFO PASS" 
632                      (read-string "NNTP password: ")))
633
634 (defun nntp-send-authinfo-from-file ()
635   "Send the AUTHINFO to the nntp server.
636 This function is supposed to be called from `nntp-server-opened-hook'.
637 It will prompt for a password."
638   (when (file-exists-p "~/.nntp-authinfo")
639     (save-excursion
640       (set-buffer (get-buffer-create " *authinfo*"))
641       (buffer-disable-undo (current-buffer))
642       (erase-buffer)
643       (insert-file-contents "~/.nntp-authinfo")
644       (goto-char (point-min))
645       (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name))
646       (nntp-send-command 
647        "^.*\r?\n" "AUTHINFO PASS" 
648        (buffer-substring (point) (progn (end-of-line) (point))))
649       (kill-buffer (current-buffer)))))
650
651 (defun nntp-default-sentinel (proc status)
652   "Default sentinel function for NNTP server process."
653   (let ((servers (cddr (assq 'nntp nnoo-state-alist)))
654         server)
655     ;; Go through the alist of server names and find the name of the
656     ;; server that the process that sent the signal is connected to.
657     ;; If you get my drift.
658     (if (equal proc nntp-server-process)
659         (setq server nntp-address)
660       (while (and servers 
661                   (not (equal proc (cdr (assq 'nntp-server-process
662                                               (car servers))))))
663         (setq servers (cdr servers)))
664       (setq server (caar servers)))
665     (when (and server
666                nntp-warn-about-losing-connection)
667       (nnheader-message 3 "nntp: Connection closed to server %s" server)
668       (setq nntp-current-group "")
669       (ding))))
670
671 (defun nntp-kill-connection (server)
672   "Choke the connection to SERVER."
673   (let ((proc (cdr (assq 'nntp-server-process 
674                          (assoc server (cddr 
675                                         (assq 'nntp nnoo-state-alist)))))))
676     (when proc 
677       (delete-process (process-name proc)))
678     (nntp-close-server server)
679     (nnheader-report
680      'nntp (message "Connection timed out to server %s" server))
681     (ding)
682     (sit-for 1)))
683
684 ;; Encoding and decoding of NNTP text.
685
686 (defun nntp-decode-text ()
687   "Decode text transmitted by NNTP.
688 0. Delete status line.
689 1. Delete `^M' at end of line.
690 2. Delete `.' at end of buffer (end of text mark).
691 3. Delete `.' at beginning of line."
692   (save-excursion
693     (set-buffer nntp-server-buffer)
694     ;; Insert newline at end of buffer.
695     (goto-char (point-max))
696     (or (bolp) (insert "\n"))
697     ;; Delete status line.
698     (delete-region (goto-char (point-min)) (progn (forward-line 1) (point)))
699     ;; Delete `^M's.
700     (while (search-forward "\r" nil t)
701       (replace-match "" t t))
702     ;; Delete `.' at end of the buffer (end of text mark).
703     (goto-char (point-max))
704     (forward-line -1)
705     (when (looking-at "^\\.\n")
706       (delete-region (point) (progn (forward-line 1) (point))))
707     ;; Replace `..' at beginning of line with `.'.
708     (goto-char (point-min))
709     ;; (replace-regexp "^\\.\\." ".")
710     (while (search-forward "\n.." nil t)
711       (delete-char -1))))
712
713 (defun nntp-encode-text ()
714   "Encode text in current buffer for NNTP transmission.
715 1. Insert `.' at beginning of line.
716 2. Insert `.' at end of buffer (end of text mark)."
717   (save-excursion
718     ;; Replace `.' at beginning of line with `..'.
719     (goto-char (point-min))
720     (while (search-forward "\n." nil t)
721       (insert "."))
722     (goto-char (point-max))
723     ;; Insert newline at end of buffer.
724     (or (bolp) (insert "\n"))
725     ;; Insert `.' at end of buffer (end of text mark).
726     (insert "." nntp-end-of-line)))
727
728 \f
729 ;;;
730 ;;; Synchronous Communication with NNTP servers.
731 ;;;
732
733 (defvar nntp-retry-command)
734
735 (defun nntp-send-command (response cmd &rest args)
736   "Wait for server RESPONSE after sending CMD and optional ARGS to server."
737   (let ((timer 
738          (and nntp-command-timeout 
739               (nnheader-run-at-time
740                nntp-command-timeout nil 'nntp-kill-command 
741                (nnoo-current-server 'nntp))))
742         (nntp-retry-command t)
743         result)
744     (unwind-protect
745         (save-excursion
746           (while nntp-retry-command
747             (setq nntp-retry-command nil)
748             ;; Clear communication buffer.
749             (set-buffer nntp-server-buffer)
750             (widen)
751             (erase-buffer)
752             (if nntp-retry-on-break
753                 (condition-case ()
754                     (progn
755                       (apply 'nntp-send-strings-to-server cmd args)
756                       (setq result
757                             (if response
758                                 (nntp-wait-for-response response)
759                               t)))
760                   (quit (setq nntp-retry-command t)))
761               (apply 'nntp-send-strings-to-server cmd args)
762               (setq result
763                     (if response
764                         (nntp-wait-for-response response)
765                       t))))
766           result)
767       (when timer 
768         (nnheader-cancel-timer timer)))))
769
770 (defun nntp-kill-command (server)
771   "Kill and restart the connection to SERVER."
772   (let ((proc (cdr (assq 
773                     'nntp-server-process 
774                     (assoc server (cddr (assq 'nntp nnoo-state-alist)))))))
775     (when proc 
776       (delete-process (process-name proc)))
777     (nntp-close-server server)
778     (nntp-open-server server)
779     (when nntp-current-group
780       (nntp-request-group nntp-current-group))
781     (setq nntp-retry-command t)))
782
783 (defun nntp-send-command-old (response cmd &rest args)
784   "Wait for server RESPONSE after sending CMD and optional ARGS to server."
785   (save-excursion
786     ;; Clear communication buffer.
787     (set-buffer nntp-server-buffer)
788     (erase-buffer)
789     (apply 'nntp-send-strings-to-server cmd args)
790     (if response
791         (nntp-wait-for-response response)
792       t)))
793
794 (defun nntp-wait-for-response (regexp &optional slow)
795   "Wait for server response which matches REGEXP."
796   (save-excursion
797     (let ((status t)
798           (wait t)
799           (dotnum 0)                    ;Number of "." being displayed.
800           (dotsize                      ;How often "." displayed.
801            (if (numberp nntp-debug-read) nntp-debug-read 10000)))
802       (set-buffer nntp-server-buffer)
803       ;; Wait for status response (RFC977).
804       ;; 1xx - Informative message.
805       ;; 2xx - Command ok.
806       ;; 3xx - Command ok so far, send the rest of it.
807       ;; 4xx - Command was correct, but couldn't be performed for some
808       ;;       reason.
809       ;; 5xx - Command unimplemented, or incorrect, or a serious
810       ;;       program error occurred.
811       (nntp-accept-response)
812       (while wait
813         (goto-char (point-min))
814         (if slow
815             (progn
816               (cond ((re-search-forward "^[23][0-9][0-9]" nil t)
817                      (setq wait nil))
818                     ((re-search-forward "^[45][0-9][0-9]" nil t)
819                      (setq status nil)
820                      (setq wait nil))
821                     (t (nntp-accept-response)))
822               (if (not wait) (delete-region (point-min) 
823                                             (progn (beginning-of-line)
824                                                    (point)))))
825           (cond ((looking-at "[23]")
826                  (setq wait nil))
827                 ((looking-at "[45]")
828                  (setq status nil)
829                  (setq wait nil))
830                 (t (nntp-accept-response)))))
831       ;; Save status message.
832       (end-of-line)
833       (setq nntp-status-string
834             (nnheader-replace-chars-in-string 
835              (buffer-substring (point-min) (point)) ?\r ? ))
836       (when status
837         (setq wait t)
838         (while wait
839           (goto-char (point-max))
840           (if (bolp) (forward-line -1) (beginning-of-line))
841           (if (looking-at regexp)
842               (setq wait nil)
843             (when nntp-debug-read
844               (let ((newnum (/ (buffer-size) dotsize))
845                     (message-log-max nil))
846                 (unless (= dotnum newnum)
847                   (setq dotnum newnum)
848                   (nnheader-message 7 "NNTP: Reading %s"
849                                     (make-string dotnum ?.)))))
850             (nntp-accept-response)))
851         ;; Remove "...".
852         (when (and nntp-debug-read (> dotnum 0))
853           (message ""))
854         ;; Successfully received server response.
855         t))))
856
857 \f
858
859 ;;;
860 ;;; Low-Level Interface to NNTP Server.
861 ;;; 
862
863 (defun nntp-find-group-and-number ()
864   (save-excursion
865     (save-restriction
866       (set-buffer nntp-server-buffer)
867       (narrow-to-region (goto-char (point-min))
868                         (or (search-forward "\n\n" nil t) (point-max)))
869       (goto-char (point-min))
870       ;; We first find the number by looking at the status line.
871       (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
872                          (string-to-int
873                           (buffer-substring (match-beginning 1)
874                                             (match-end 1)))))
875             group newsgroups xref)
876         (and number (zerop number) (setq number nil))
877         ;; Then we find the group name.
878         (setq group
879               (cond 
880                ;; If there is only one group in the Newsgroups header,
881                ;; then it seems quite likely that this article comes
882                ;; from that group, I'd say.
883                ((and (setq newsgroups (mail-fetch-field "newsgroups"))
884                      (not (string-match "," newsgroups)))
885                 newsgroups)
886                ;; If there is more than one group in the Newsgroups
887                ;; header, then the Xref header should be filled out.
888                ;; We hazard a guess that the group that has this
889                ;; article number in the Xref header is the one we are
890                ;; looking for.  This might very well be wrong if this
891                ;; article happens to have the same number in several
892                ;; groups, but that's life. 
893                ((and (setq xref (mail-fetch-field "xref"))
894                      number
895                      (string-match (format "\\([^ :]+\\):%d" number) xref))
896                 (substring xref (match-beginning 1) (match-end 1)))
897                (t "")))
898         (when (string-match "\r" group) 
899           (setq group (substring group 0 (match-beginning 0))))
900         (cons group number)))))
901
902 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
903   (erase-buffer)
904   (cond 
905
906    ;; This server does not talk NOV.
907    ((not nntp-server-xover)
908     nil)
909
910    ;; We don't care about gaps.
911    ((or (not nntp-nov-gap)
912         fetch-old)
913     (nntp-send-xover-command 
914      (if fetch-old
915          (if (numberp fetch-old) 
916              (max 1 (- (car articles) fetch-old)) 
917            1)
918        (car articles))
919      (nntp-last-element articles) 'wait)
920
921     (goto-char (point-min))
922     (when (looking-at "[1-5][0-9][0-9] ")
923       (delete-region (point) (progn (forward-line 1) (point))))
924     (while (search-forward "\r" nil t)
925       (replace-match "" t t))
926     (goto-char (point-max))
927     (forward-line -1)
928     (when (looking-at "\\.")
929       (delete-region (point) (progn (forward-line 1) (point)))))
930
931    ;; We do it the hard way.  For each gap, an XOVER command is sent
932    ;; to the server.  We do not wait for a reply from the server, we
933    ;; just send them off as fast as we can.  That means that we have
934    ;; to count the number of responses we get back to find out when we
935    ;; have gotten all we asked for.
936    ((numberp nntp-nov-gap)
937     (let ((count 0)
938           (received 0)
939           (last-point (point-min))
940           (buf (current-buffer))
941           first)
942       ;; We have to check `nntp-server-xover'.  If it gets set to nil,
943       ;; that means that the server does not understand XOVER, but we
944       ;; won't know that until we try.
945       (while (and nntp-server-xover articles)
946         (setq first (car articles))
947         ;; Search forward until we find a gap, or until we run out of
948         ;; articles. 
949         (while (and (cdr articles) 
950                     (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
951           (setq articles (cdr articles)))
952
953         (when (nntp-send-xover-command first (car articles))
954           (setq articles (cdr articles)
955                 count (1+ count))
956
957           ;; Every 400 requests we have to read the stream in
958           ;; order to avoid deadlocks.
959           (when (or (null articles)     ;All requests have been sent.
960                     (zerop (% count nntp-maximum-request)))
961             (accept-process-output)
962             ;; On some Emacs versions the preceding function has
963             ;; a tendency to change the buffer. Perhaps. It's
964             ;; quite difficult to reproduce, because it only
965             ;; seems to happen once in a blue moon. 
966             (set-buffer buf) 
967             (while (progn
968                      (goto-char last-point)
969                      ;; Count replies.
970                      (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
971                        (setq received (1+ received)))
972                      (setq last-point (point))
973                      (< received count))
974               (accept-process-output)
975               (set-buffer buf)))))
976
977       (when nntp-server-xover
978         ;; Wait for the reply from the final command.
979         (goto-char (point-max))
980         (re-search-backward "^[0-9][0-9][0-9] " nil t)
981         (when (looking-at "^[23]")
982           (while (progn
983                    (goto-char (point-max))
984                    (forward-line -1)
985                    (not (looking-at "^\\.\r?\n")))
986             (nntp-accept-response)))
987         
988         ;; We remove any "." lines and status lines.
989         (goto-char (point-min))
990         (while (search-forward "\r" nil t)
991           (delete-char -1))
992         (goto-char (point-min))
993         (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")))))
994
995   nntp-server-xover)
996
997 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
998   "Send the XOVER command to the server."
999   (let ((range (format "%d-%d" (or beg 1) (or end beg 1))))
1000     (if (stringp nntp-server-xover)
1001         ;; If `nntp-server-xover' is a string, then we just send this
1002         ;; command.
1003         (if wait-for-reply
1004             (nntp-send-command "^\\.\r?\n" nntp-server-xover range)
1005           ;; We do not wait for the reply.
1006           (nntp-send-strings-to-server nntp-server-xover range))
1007       (let ((commands nntp-xover-commands))
1008         ;; `nntp-xover-commands' is a list of possible XOVER commands.
1009         ;; We try them all until we get at positive response. 
1010         (while (and commands (eq nntp-server-xover 'try))
1011           (nntp-send-command "^\\.\r?\n" (car commands) range)
1012           (save-excursion
1013             (set-buffer nntp-server-buffer)
1014             (goto-char (point-min))
1015             (and (looking-at "[23]") ; No error message.
1016                  ;; We also have to look at the lines.  Some buggy
1017                  ;; servers give back simple lines with just the
1018                  ;; article number.  How... helpful.
1019                  (progn
1020                    (forward-line 1)
1021                    (looking-at "[0-9]+\t...")) ; More text after number.
1022                  (setq nntp-server-xover (car commands))))
1023           (setq commands (cdr commands)))
1024         ;; If none of the commands worked, we disable XOVER.
1025         (when (eq nntp-server-xover 'try)
1026           (save-excursion
1027             (set-buffer nntp-server-buffer)
1028             (erase-buffer)
1029             (setq nntp-server-xover nil)))
1030         nntp-server-xover))))
1031
1032 (defun nntp-send-strings-to-server (&rest strings)
1033   "Send STRINGS to the server."
1034   (let ((cmd (concat (mapconcat 'identity strings " ") nntp-end-of-line)))
1035     ;; We open the nntp server if it is down.
1036     (or (nntp-server-opened (nnoo-current-server 'nntp))
1037         (nntp-open-server (nnoo-current-server 'nntp))
1038         (error (nntp-status-message)))
1039     ;; Send the strings.
1040     (process-send-string nntp-server-process cmd)
1041     t))
1042
1043 (defun nntp-send-region-to-server (begin end)
1044   "Send the current buffer region (from BEGIN to END) to the server."
1045   (save-excursion
1046     (let ((cur (current-buffer)))
1047       ;; Copy the buffer over to the send buffer.
1048       (nnheader-set-temp-buffer " *nntp send*")
1049       (insert-buffer-substring cur begin end)
1050       (save-excursion
1051         (set-buffer cur)
1052         (erase-buffer))
1053       ;; `process-send-region' does not work if the text to be sent is very
1054       ;; large, so we send it piecemeal.
1055       (let ((last (point-min))
1056             (size 100))                 ;Size of text sent at once.
1057         (while (/= last (point-max))
1058           (process-send-region 
1059            nntp-server-process
1060            last (setq last (min (+ last size) (point-max))))
1061           ;; Read any output from the server.  May be unnecessary.
1062           (accept-process-output)))
1063       (kill-buffer (current-buffer)))))
1064
1065 (defun nntp-open-server-semi-internal (server &optional service)
1066   "Open SERVER.
1067 If SERVER is nil, use value of environment variable `NNTPSERVER'.
1068 If SERVICE, this this as the port number."
1069   (nnheader-insert "")
1070   (let ((server (or server (getenv "NNTPSERVER")))
1071         (status nil)
1072         (timer 
1073          (and nntp-connection-timeout 
1074               (nnheader-run-at-time nntp-connection-timeout
1075                                     nil 'nntp-kill-connection server))))
1076     (save-excursion
1077       (set-buffer nntp-server-buffer)
1078       (setq nntp-status-string "")
1079       (nnheader-message 5 "nntp: Connecting to server on %s..." nntp-address)
1080       (cond ((and server (nntp-open-server-internal server service))
1081              (setq nntp-address server)
1082              (setq status
1083                    (condition-case nil
1084                        (nntp-wait-for-response "^[23].*\r?\n" 'slow)
1085                      (error nil)
1086                      (quit nil)))
1087              (unless status
1088                (nntp-close-server-internal server)
1089                (nnheader-report 
1090                 'nntp "Couldn't open connection to %s" nntp-address))
1091              (when nntp-server-process
1092                (set-process-sentinel 
1093                 nntp-server-process 'nntp-default-sentinel)
1094                ;; You can send commands at startup like AUTHINFO here.
1095                ;; Added by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>
1096                (run-hooks 'nntp-server-opened-hook)))
1097             ((null server)
1098              (nnheader-report 'nntp "NNTP server is not specified."))
1099             (t                          ; We couldn't open the server.
1100              (nnheader-report 
1101               'nntp (buffer-substring (point-min) (point-max)))))
1102       (when timer 
1103         (nnheader-cancel-timer timer))
1104       (message "")
1105       (unless status
1106         (nnoo-close-server 'nntp server)
1107         (setq nntp-async-number nil))
1108       status)))
1109
1110 (defvar nntp-default-directories '("~" "/tmp" "/")
1111   "Directories to as current directory in the nntp server buffer.")
1112
1113 (defun nntp-open-server-internal (server &optional service)
1114   "Open connection to news server on SERVER by SERVICE (default is nntp)."
1115   (let (proc)
1116     (save-excursion
1117       (set-buffer nntp-server-buffer)
1118       ;; Make sure we have a valid current directory for the
1119       ;; nntp server buffer.
1120       (unless (file-exists-p default-directory)
1121         (let ((dirs nntp-default-directories))
1122           (while dirs
1123             (when (file-exists-p (car dirs))
1124               (setq default-directory (car dirs)
1125                     dirs nil))
1126             (setq dirs (cdr dirs)))))
1127       (cond
1128        ((and (setq proc
1129                    (condition-case nil
1130                        (funcall nntp-open-server-function server)
1131                      (error nil)))
1132              (memq (process-status proc) '(open run)))
1133         (setq nntp-server-process proc)
1134         (setq nntp-address server)
1135         ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1136         (process-kill-without-query proc)
1137         (run-hooks 'nntp-server-hook)
1138         (push proc nntp-opened-connections)
1139         (condition-case ()
1140             (nntp-read-server-type)
1141           (error 
1142            (nnheader-report 'nntp "Couldn't open server %s" server)
1143            (nntp-close-server)))
1144         nntp-server-process)
1145        (t
1146         (nnheader-report 'nntp "Couldn't open server %s" server))))))
1147
1148 (defun nntp-read-server-type ()
1149   "Find out what the name of the server we have connected to is."
1150   ;; Wait for the status string to arrive.
1151   (nntp-wait-for-response "^.*\n" t)
1152   (setq nntp-server-type (buffer-string))
1153   (let ((alist nntp-server-action-alist)
1154         entry)
1155     ;; Run server-specific commmands.
1156     (while alist
1157       (setq entry (pop alist))
1158       (when (string-match (car entry) nntp-server-type)
1159         (if (and (listp (cadr entry))
1160                  (not (eq 'lambda (caadr entry))))
1161             (eval (cadr entry))
1162           (funcall (cadr entry)))))))
1163
1164 (defun nntp-open-network-stream (server)
1165   (open-network-stream 
1166    "nntpd" nntp-server-buffer server nntp-port-number))
1167
1168 (defun nntp-open-rlogin (server)
1169   (let ((proc (if nntp-rlogin-user-name
1170                   (start-process
1171                    "nntpd" nntp-server-buffer "rsh"
1172                    "-l" nntp-rlogin-user-name server
1173                    (mapconcat 'identity
1174                               nntp-rlogin-parameters " "))
1175                 (start-process
1176                  "nntpd" nntp-server-buffer "rsh" server
1177                  (mapconcat 'identity
1178                             nntp-rlogin-parameters " ")))))
1179     proc))
1180
1181 (defun nntp-telnet-to-machine ()
1182   (let (b)
1183     (telnet "localhost")
1184     (goto-char (point-min))
1185     (while (not (re-search-forward "^login: *" nil t))
1186       (sit-for 1)
1187       (goto-char (point-min)))
1188     (goto-char (point-max))
1189     (insert "larsi")
1190     (telnet-send-input)
1191     (setq b (point))
1192     (while (not (re-search-forward ">" nil t))
1193       (sit-for 1)
1194       (goto-char b))
1195     (goto-char (point-max))
1196     (insert "ls")
1197     (telnet-send-input)))
1198
1199 (defun nntp-close-server-internal (&optional server)
1200   "Close connection to news server."
1201   (nntp-possibly-change-server nil server)
1202   (if nntp-server-process
1203       (delete-process nntp-server-process))
1204   (setq nntp-server-process nil)
1205   (setq nntp-address ""))
1206
1207 (defun nntp-accept-response ()
1208   "Read response of server.
1209 It is well-known that the communication speed will be much improved by
1210 defining this function as macro."
1211   ;; To deal with server process exiting before
1212   ;;  accept-process-output is called.
1213   ;; Suggested by Jason Venner <jason@violet.berkeley.edu>.
1214   ;; This is a copy of `nntp-default-sentinel'.
1215   (let ((buf (current-buffer)))
1216     (prog1
1217         (if (or (not nntp-server-process)
1218                 (not (memq (process-status nntp-server-process) '(open run))))
1219             (error "nntp: Process connection closed; %s" (nntp-status-message))
1220           (if nntp-buggy-select
1221               (progn
1222                 ;; We cannot use `accept-process-output'.
1223                 ;; Fujitsu UTS requires messages during sleep-for.
1224                 ;; I don't know why.
1225                 (nnheader-message 5 "NNTP: Reading...")
1226                 (sleep-for 1)
1227                 (nnheader-message 5 ""))
1228             (condition-case errorcode
1229                 (accept-process-output nntp-server-process 1)
1230               (error
1231                (cond ((string-equal "select error: Invalid argument" 
1232                                     (nth 1 errorcode))
1233                       ;; Ignore select error.
1234                       nil)
1235                      (t
1236                       (signal (car errorcode) (cdr errorcode))))))))
1237       (set-buffer buf))))
1238
1239 (defun nntp-last-element (list)
1240   "Return last element of LIST."
1241   (while (cdr list)
1242     (setq list (cdr list)))
1243   (car list))
1244
1245 (defun nntp-possibly-change-server (newsgroup server &optional connectionless)
1246   "Check whether the virtual server needs changing."
1247   (when (and server
1248              (not (nntp-server-opened server)))
1249     ;; This virtual server isn't open, so we (re)open it here.
1250     (nntp-open-server server nil t))
1251   (when (and newsgroup 
1252              (not (equal newsgroup nntp-current-group)))
1253     ;; Set the proper current group.
1254     (nntp-request-group newsgroup server)))
1255  
1256 (defun nntp-try-list-active (group)
1257   (nntp-list-active-group group)
1258   (save-excursion
1259     (set-buffer nntp-server-buffer)
1260     (goto-char (point-min))
1261     (cond ((looking-at "5[0-9]+")
1262            (setq nntp-server-list-active-group nil))
1263           (t
1264            (setq nntp-server-list-active-group t)))))
1265
1266 (defun nntp-async-server-opened ()
1267   (and nntp-async-process
1268        (memq (process-status nntp-async-process) '(open run))))
1269
1270 (defun nntp-async-open-server ()
1271   (save-excursion
1272     (set-buffer (generate-new-buffer " *async-nntp*"))
1273     (setq nntp-async-buffer (current-buffer))
1274     (buffer-disable-undo (current-buffer)))
1275   (let ((nntp-server-process nil)
1276         (nntp-server-buffer nntp-async-buffer))
1277     (nntp-open-server-semi-internal nntp-address nntp-port-number)
1278     (if (not (setq nntp-async-process nntp-server-process))
1279         (progn
1280           (setq nntp-async-number nil))
1281       (set-process-buffer nntp-async-process nntp-async-buffer))))
1282
1283 (defun nntp-async-fetch-articles (article)
1284   (if (stringp article)
1285       ()
1286     (let ((articles (cdr (memq (assq article nntp-async-articles)
1287                                nntp-async-articles)))
1288           (max (cond ((numberp nntp-async-number)
1289                       nntp-async-number) 
1290                      ((eq nntp-async-number t)
1291                       (length nntp-async-articles))
1292                      (t 0)))
1293           nart)
1294       (while (and (>= (setq max (1- max)) 0)
1295                   articles)
1296         (or (memq (setq nart (caar articles)) nntp-async-fetched)
1297             (progn
1298               (nntp-async-send-strings "ARTICLE " (int-to-string nart))
1299               (setq nntp-async-fetched (cons nart nntp-async-fetched))))
1300         (setq articles (cdr articles))))))
1301
1302 (defun nntp-async-send-strings (&rest strings)
1303   (let ((cmd (concat (mapconcat 'identity strings " ") nntp-end-of-line)))
1304     (or (nntp-async-server-opened)
1305         (nntp-async-open-server)
1306         (error (nntp-status-message)))
1307     (process-send-string nntp-async-process cmd)))
1308
1309 (defun nntp-async-request-group (group)
1310   (if (equal group nntp-current-group)
1311       ()
1312     (let ((asyncs (assoc group nntp-async-group-alist)))
1313       ;; A new group has been selected, so we push the current state
1314       ;; of async articles on an alist, and pull the old state off.
1315       (setq nntp-async-group-alist 
1316             (cons (list nntp-current-group
1317                         nntp-async-articles nntp-async-fetched
1318                         nntp-async-process)
1319                   (delq asyncs nntp-async-group-alist)))
1320       (and asyncs
1321            (progn
1322              (setq nntp-async-articles (nth 1 asyncs))
1323              (setq nntp-async-fetched (nth 2 asyncs))
1324              (setq nntp-async-process (nth 3 asyncs)))))))
1325
1326 (provide 'nntp)
1327
1328 ;;; nntp.el ends here