*** empty log message ***
[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 'rnews)
30 (require 'sendmail)
31 (require 'nnheader)
32 (require 'nnoo)
33 (eval-when-compile (require 'cl))
34
35 (nnoo-declare nntp)
36
37 (eval-and-compile
38   (unless (fboundp 'open-network-stream)
39     (require 'tcp)))
40
41 (eval-when-compile (require 'cl))
42
43 (eval-and-compile
44   (autoload 'news-setup "rnewspost")
45   (autoload 'news-reply-mode "rnewspost")
46   (autoload 'cancel-timer "timer")
47   (autoload 'telnet "telnet" nil t)
48   (autoload 'telnet-send-input "telnet" nil t)
49   (autoload 'timezone-parse-date "timezone"))
50
51 (defvoo nntp-server-hook nil
52   "*Hooks for the NNTP server.
53 If the kanji code of the NNTP server is different from the local kanji
54 code, the correct kanji code of the buffer associated with the NNTP
55 server must be specified as follows:
56
57 \(setq nntp-server-hook
58       (function
59        (lambda ()
60          ;; Server's Kanji code is EUC (NEmacs hack).
61          (make-local-variable 'kanji-fileio-code)
62          (setq kanji-fileio-code 0))))
63
64 If you'd like to change something depending on the server in this
65 hook, use the variable `nntp-address'.")
66
67 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
68   "*Hook used for sending commands to the server at startup.  
69 The default value is `nntp-send-mode-reader', which makes an innd
70 server spawn an nnrpd server.  Another useful function to put in this
71 hook might be `nntp-send-authinfo', which will prompt for a password
72 to allow posting from the server.  Note that this is only necessary to
73 do on servers that use strict access control.")  
74 (add-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)
75
76 (defvoo nntp-server-action-alist 
77   '(("nntpd 1\\.5\\.11t" 
78      (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)))
79   "Alist of regexps to match on server types and actions to be taken.
80 For instance, if you want Gnus to beep every time you connect
81 to innd, you could say something like:
82
83 \(setq nntp-server-action-alist
84        '((\"innd\" (ding))))
85
86 You probably don't want to do that, though.")
87
88 (defvoo nntp-open-server-function 'nntp-open-network-stream
89   "*Function used for connecting to a remote system.
90 It will be called with the address of the remote system.
91
92 Two pre-made functions are `nntp-open-network-stream', which is the
93 default, and simply connects to some port or other on the remote
94 system (see nntp-port-number).  The other is `nntp-open-rlogin', which
95 does an rlogin on the remote system, and then does a telnet to the
96 NNTP server available there (see nntp-rlogin-parameters).")
97
98 (defvoo nntp-rlogin-parameters '("telnet" "${NNTPSERVER:=localhost}" "nntp")
99   "*Parameters to `nntp-open-login'.
100 That function may be used as `nntp-open-server-function'.  In that
101 case, this list will be used as the parameter list given to rsh.")
102
103 (defvoo nntp-rlogin-user-name nil
104   "*User name on remote system when using the rlogin connect method.")
105
106 (defvoo nntp-address nil
107   "*The name of the NNTP server.")
108
109 (defvoo nntp-port-number "nntp"
110   "*Port number to connect to.")
111
112 (defvoo nntp-end-of-line "\r\n"
113   "String to use on the end of lines when talking to the NNTP server.
114 This is \"\\r\\n\" by default, but should be \"\\n\" when
115 using rlogin to communicate with the server.")
116
117 (defvoo nntp-large-newsgroup 50
118   "*The number of the articles which indicates a large newsgroup.
119 If the number of the articles is greater than the value, verbose
120 messages will be shown to indicate the current status.")
121
122 (defvoo nntp-buggy-select (memq system-type '(fujitsu-uts))
123   "*t if your select routine is buggy.
124 If the select routine signals error or fall into infinite loop while
125 waiting for the server response, the variable must be set to t.  In
126 case of Fujitsu UTS, it is set to T since `accept-process-output'
127 doesn't work properly.")
128
129 (defvoo nntp-maximum-request 400
130   "*The maximum number of the requests sent to the NNTP server at one time.
131 If Emacs hangs up while retrieving headers, set the variable to a
132 lower value.")
133
134 (defvoo nntp-debug-read 10000
135   "*Display '...' every 10Kbytes of a message being received if it is non-nil.
136 If it is a number, dots are displayed per the number.")
137
138 (defvoo nntp-nov-is-evil nil
139   "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
140
141 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
142   "*List of strings that are used as commands to fetch NOV lines from a server.
143 The strings are tried in turn until a positive response is gotten. If
144 none of the commands are successful, nntp will just grab headers one
145 by one.")
146
147 (defvoo nntp-nov-gap 20
148   "*Maximum allowed gap between two articles.
149 If the gap between two consecutive articles is bigger than this
150 variable, split the XOVER request into two requests.")
151
152 (defvoo nntp-connection-timeout nil
153   "*Number of seconds to wait before an nntp connection times out.
154 If this variable is nil, which is the default, no timers are set.")
155
156 (defvoo nntp-command-timeout nil
157   "*Number of seconds to wait for a response when sending a command.
158 If this variable is nil, which is the default, no timers are set.")
159
160 (defvoo nntp-retry-on-break nil
161   "*If non-nil, re-send the command when the user types `C-g'.")
162
163 (defvoo nntp-news-default-headers nil
164   "*If non-nil, override `mail-default-headers' when posting news.")
165
166 (defvoo nntp-prepare-server-hook nil
167   "*Hook run before a server is opened.
168 If can be used to set up a server remotely, for instance.  Say you
169 have an account at the machine \"other.machine\".  This machine has
170 access to an NNTP server that you can't access locally.  You could
171 then use this hook to rsh to the remote machine and start a proxy NNTP
172 server there that you can connect to.")
173
174 (defvoo nntp-async-number 5
175   "*How many articles should be prefetched when in asynchronous mode.")
176
177 (defvoo nntp-warn-about-losing-connection t
178   "*If non-nil, beep when a server closes connection.")
179
180 \f
181
182 (defconst nntp-version "nntp 4.0"
183   "Version numbers of this version of NNTP.")
184
185 (defvar nntp-server-buffer nil
186   "Buffer associated with the NNTP server process.")
187
188 (defvoo nntp-server-process nil
189   "The NNTP server process.
190 You'd better not use this variable in NNTP front-end program, but
191 instead use `nntp-server-buffer'.")
192
193 (defvoo nntp-status-string nil
194   "Save the server response message.")
195
196 (defvar nntp-opened-connections nil
197   "All (possibly) opened connections.")
198
199 (defvoo nntp-server-xover 'try)
200 (defvoo nntp-server-list-active-group 'try)
201 (defvoo nntp-current-group "")
202 (defvoo nntp-server-type nil)
203
204 (defvoo nntp-async-process nil)
205 (defvoo nntp-async-buffer nil)
206 (defvoo nntp-async-articles nil)
207 (defvoo nntp-async-fetched nil)
208 (defvoo nntp-async-group-alist nil)
209
210 \f
211 ;;; Interface functions.
212
213 (nnoo-define-basics nntp)
214
215 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
216   "Retrieve the headers of ARTICLES."
217   (nntp-possibly-change-server group server)
218   (save-excursion
219     (set-buffer nntp-server-buffer)
220     (erase-buffer)
221     (if (and (not gnus-nov-is-evil) 
222              (not nntp-nov-is-evil)
223              (nntp-retrieve-headers-with-xover articles fetch-old))
224         ;; We successfully retrieved the headers via XOVER.
225         'nov
226       ;; XOVER didn't work, so we do it the hard, slow and inefficient
227       ;; way.  
228       (let ((number (length articles))
229             (count 0)
230             (received 0)
231             (message-log-max nil)
232             (last-point (point-min)))
233         ;; Send HEAD command.
234         (while articles
235           (nntp-send-strings-to-server 
236            "HEAD" (if (numberp (car articles)) 
237                       (int-to-string (car articles))
238                     ;; `articles' is either a list of article numbers
239                     ;; or a list of article IDs.
240                     (car articles)))
241           (setq articles (cdr articles)
242                 count (1+ count))
243           ;; Every 400 header requests we have to read the stream in
244           ;; order to avoid deadlocks.
245           (when (or (null articles)     ;All requests have been sent.
246                     (zerop (% count nntp-maximum-request)))
247             (nntp-accept-response)
248             (while (progn
249                      (goto-char last-point)
250                      ;; Count replies.
251                      (while (re-search-forward "^[0-9]" nil t)
252                        (setq received (1+ received)))
253                      (setq last-point (point))
254                      (< received count))
255               ;; If number of headers is greater than 100, give
256               ;;  informative messages.
257               (and (numberp nntp-large-newsgroup)
258                    (> number nntp-large-newsgroup)
259                    (zerop (% received 20))
260                    (message "NNTP: Receiving headers... %d%%"
261                             (/ (* received 100) number)))
262               (nntp-accept-response))))
263         ;; Wait for text of last command.
264         (goto-char (point-max))
265         (re-search-backward "^[0-9]" nil t)
266         (when (looking-at "^[23]")
267           (while (progn
268                    (goto-char (- (point-max) 3))
269                    (not (looking-at "^\\.\r?\n")))
270             (nntp-accept-response)))
271         (and (numberp nntp-large-newsgroup)
272              (> number nntp-large-newsgroup)
273              (message "NNTP: Receiving headers...done"))
274
275         ;; Now all of replies are received.  Fold continuation lines.
276         (nnheader-fold-continuation-lines)
277         ;; Remove all "\r"'s.
278         (goto-char (point-min))
279         (while (search-forward "\r" nil t)
280           (replace-match "" t t))
281         'headers))))
282
283
284 (deffoo nntp-retrieve-groups (groups &optional server)
285   "Retrieve group info on GROUPS."
286   (nntp-possibly-change-server nil server)
287   (save-excursion
288     (set-buffer nntp-server-buffer)
289     ;; The first time this is run, this variable is `try'.  So we
290     ;; try.   
291     (when (eq nntp-server-list-active-group 'try)
292       (nntp-try-list-active (car groups)))
293     (erase-buffer)
294     (let ((count 0)
295           (received 0)
296           (last-point (point-min))
297           (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
298       (while groups
299         ;; Send the command to the server.
300         (nntp-send-strings-to-server command (car groups))
301         (setq groups (cdr groups))
302         (setq count (1+ count))
303         ;; Every 400 requests we have to read the stream in
304         ;; order to avoid deadlocks.
305         (when (or (null groups)         ;All requests have been sent.
306                   (zerop (% count nntp-maximum-request)))
307           (nntp-accept-response)
308           (while (progn
309                    (goto-char last-point)
310                    ;; Count replies.
311                    (while (re-search-forward "^[0-9]" nil t)
312                      (setq received (1+ received)))
313                    (setq last-point (point))
314                    (< received count))
315             (nntp-accept-response))))
316
317       ;; Wait for the reply from the final command.
318       (when nntp-server-list-active-group
319         (goto-char (point-max))
320         (re-search-backward "^[0-9]" nil t)
321         (when (looking-at "^[23]")
322           (while (progn
323                    (goto-char (- (point-max) 3))
324                    (not (looking-at "^\\.\r?\n")))
325             (nntp-accept-response))))
326
327       ;; Now all replies are received. We remove CRs.
328       (goto-char (point-min))
329       (while (search-forward "\r" nil t)
330         (replace-match "" t t))
331
332       (if (not nntp-server-list-active-group)
333           'group
334         ;; We have read active entries, so we just delete the
335         ;; superfluos gunk.
336         (goto-char (point-min))
337         (while (re-search-forward "^[.2-5]" nil t)
338           (delete-region (match-beginning 0) 
339                          (progn (forward-line 1) (point))))
340         'active))))
341
342 (deffoo nntp-open-server (server &optional defs connectionless)
343   "Open the virtual server SERVER.
344 If CONNECTIONLESS is non-nil, don't attempt to connect to any physical
345 servers."
346   ;; Called with just a port number as the defs.
347   (when (or (stringp (car defs))
348             (numberp (car defs)))
349     (setq defs `((nntp-port-number ,(car defs)))))
350   (unless (assq 'nntp-address defs)
351     (setq defs (append defs `((nntp-address ,server)))))
352   (nnoo-change-server 'nntp server defs)
353   (if (nntp-server-opened server)
354       t
355     (or (nntp-server-opened server)
356         connectionless
357         (prog2
358             (run-hooks 'nntp-prepare-server-hook)
359             (nntp-open-server-semi-internal nntp-address nntp-port-number)
360           (nnheader-insert "")))))
361
362 (deffoo nntp-close-server (&optional server)
363   "Close connection to SERVER."
364   (nntp-possibly-change-server nil server t)
365   (unwind-protect
366       (progn
367         ;; Un-set default sentinel function before closing connection.
368         (and nntp-server-process
369              (eq 'nntp-default-sentinel
370                  (process-sentinel nntp-server-process))
371              (set-process-sentinel nntp-server-process nil))
372         ;; We cannot send QUIT command unless the process is running.
373         (when (nntp-server-opened)
374           (nntp-send-command nil "QUIT")
375           ;; Give the QUIT time to arrive.
376           (sleep-for 1)))
377     (nntp-close-server-internal server)))
378
379 (deffoo nntp-request-close ()
380   "Close all server connections."
381   (let (proc entry)
382     (while nntp-opened-connections
383       (when (setq proc (pop nntp-opened-connections))
384         (condition-case ()
385             (process-send-string proc (concat "QUIT" nntp-end-of-line))
386           (error nil))
387         ;; Give the QUIT time to reach the server before we close
388         ;; down the process.
389         (sleep-for 1)
390         (delete-process proc)))
391     (and nntp-async-buffer
392          (buffer-name nntp-async-buffer)
393          (kill-buffer nntp-async-buffer))
394     (let ((alist (cddr (assq 'nntp nnoo-state-alist))))
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\\|\r$\\)" "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       (message "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             (buffer-substring (point-min) (point)))
835       (when status
836         (setq wait t)
837         (while wait
838           (goto-char (point-max))
839           (if (bolp) (forward-line -1) (beginning-of-line))
840           (if (looking-at regexp)
841               (setq wait nil)
842             (when nntp-debug-read
843               (let ((newnum (/ (buffer-size) dotsize))
844                     (message-log-max nil))
845                 (unless (= dotnum newnum)
846                   (setq dotnum newnum)
847                   (message "NNTP: Reading %s"
848                            (make-string dotnum ?.)))))
849             (nntp-accept-response)))
850         ;; Remove "...".
851         (when (and nntp-debug-read (> dotnum 0))
852           (message ""))
853         ;; Successfully received server response.
854         t))))
855
856 \f
857
858 ;;;
859 ;;; Low-Level Interface to NNTP Server.
860 ;;; 
861
862 (defun nntp-find-group-and-number ()
863   (save-excursion
864     (save-restriction
865       (set-buffer nntp-server-buffer)
866       (narrow-to-region (goto-char (point-min))
867                         (or (search-forward "\n\n" nil t) (point-max)))
868       (goto-char (point-min))
869       ;; We first find the number by looking at the status line.
870       (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
871                          (string-to-int
872                           (buffer-substring (match-beginning 1)
873                                             (match-end 1)))))
874             group newsgroups xref)
875         (and number (zerop number) (setq number nil))
876         ;; Then we find the group name.
877         (setq group
878               (cond 
879                ;; If there is only one group in the Newsgroups header,
880                ;; then it seems quite likely that this article comes
881                ;; from that group, I'd say.
882                ((and (setq newsgroups (mail-fetch-field "newsgroups"))
883                      (not (string-match "," newsgroups)))
884                 newsgroups)
885                ;; If there is more than one group in the Newsgroups
886                ;; header, then the Xref header should be filled out.
887                ;; We hazard a guess that the group that has this
888                ;; article number in the Xref header is the one we are
889                ;; looking for.  This might very well be wrong if this
890                ;; article happens to have the same number in several
891                ;; groups, but that's life. 
892                ((and (setq xref (mail-fetch-field "xref"))
893                      number
894                      (string-match (format "\\([^ :]+\\):%d" number) xref))
895                 (substring xref (match-beginning 1) (match-end 1)))
896                (t "")))
897         (when (string-match "\r" group) 
898           (setq group (substring group 0 (match-beginning 0))))
899         (cons group number)))))
900
901 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
902   (erase-buffer)
903   (cond 
904
905    ;; This server does not talk NOV.
906    ((not nntp-server-xover)
907     nil)
908
909    ;; We don't care about gaps.
910    ((or (not nntp-nov-gap)
911         fetch-old)
912     (nntp-send-xover-command 
913      (if fetch-old
914          (if (numberp fetch-old) 
915              (max 1 (- (car articles) fetch-old)) 
916            1)
917        (car articles))
918      (nntp-last-element articles) 'wait)
919
920     (goto-char (point-min))
921     (when (looking-at "[1-5][0-9][0-9] ")
922       (delete-region (point) (progn (forward-line 1) (point))))
923     (while (search-forward "\r" nil t)
924       (replace-match "" t t))
925     (goto-char (point-max))
926     (forward-line -1)
927     (when (looking-at "\\.")
928       (delete-region (point) (progn (forward-line 1) (point)))))
929
930    ;; We do it the hard way.  For each gap, an XOVER command is sent
931    ;; to the server.  We do not wait for a reply from the server, we
932    ;; just send them off as fast as we can.  That means that we have
933    ;; to count the number of responses we get back to find out when we
934    ;; have gotten all we asked for.
935    ((numberp nntp-nov-gap)
936     (let ((count 0)
937           (received 0)
938           (last-point (point-min))
939           (buf (current-buffer))
940           first)
941       ;; We have to check `nntp-server-xover'.  If it gets set to nil,
942       ;; that means that the server does not understand XOVER, but we
943       ;; won't know that until we try.
944       (while (and nntp-server-xover articles)
945         (setq first (car articles))
946         ;; Search forward until we find a gap, or until we run out of
947         ;; articles. 
948         (while (and (cdr articles) 
949                     (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
950           (setq articles (cdr articles)))
951
952         (when (nntp-send-xover-command first (car articles))
953           (setq articles (cdr articles)
954                 count (1+ count))
955
956           ;; Every 400 requests we have to read the stream in
957           ;; order to avoid deadlocks.
958           (when (or (null articles)     ;All requests have been sent.
959                     (zerop (% count nntp-maximum-request)))
960             (accept-process-output)
961             ;; On some Emacs versions the preceding function has
962             ;; a tendency to change the buffer. Perhaps. It's
963             ;; quite difficult to reproduce, because it only
964             ;; seems to happen once in a blue moon. 
965             (set-buffer buf) 
966             (while (progn
967                      (goto-char last-point)
968                      ;; Count replies.
969                      (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
970                        (setq received (1+ received)))
971                      (setq last-point (point))
972                      (< received count))
973               (accept-process-output)
974               (set-buffer buf)))))
975
976       (when nntp-server-xover
977         ;; Wait for the reply from the final command.
978         (goto-char (point-max))
979         (re-search-backward "^[0-9][0-9][0-9] " nil t)
980         (when (looking-at "^[23]")
981           (while (progn
982                    (goto-char (point-max))
983                    (forward-line -1)
984                    (not (looking-at "^\\.\r?\n")))
985             (nntp-accept-response)))
986         
987         ;; We remove any "." lines and status lines.
988         (goto-char (point-min))
989         (while (search-forward "\r" nil t)
990           (delete-char -1))
991         (goto-char (point-min))
992         (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")))))
993
994   nntp-server-xover)
995
996 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
997   "Send the XOVER command to the server."
998   (let ((range (format "%d-%d" (or beg 1) (or end beg 1))))
999     (if (stringp nntp-server-xover)
1000         ;; If `nntp-server-xover' is a string, then we just send this
1001         ;; command.
1002         (if wait-for-reply
1003             (nntp-send-command "^\\.\r?\n" nntp-server-xover range)
1004           ;; We do not wait for the reply.
1005           (nntp-send-strings-to-server nntp-server-xover range))
1006       (let ((commands nntp-xover-commands))
1007         ;; `nntp-xover-commands' is a list of possible XOVER commands.
1008         ;; We try them all until we get at positive response. 
1009         (while (and commands (eq nntp-server-xover 'try))
1010           (nntp-send-command "^\\.\r?\n" (car commands) range)
1011           (save-excursion
1012             (set-buffer nntp-server-buffer)
1013             (goto-char (point-min))
1014             (and (looking-at "[23]") ; No error message.
1015                  ;; We also have to look at the lines.  Some buggy
1016                  ;; servers give back simple lines with just the
1017                  ;; article number.  How... helpful.
1018                  (progn
1019                    (forward-line 1)
1020                    (looking-at "[0-9]+\t...")) ; More text after number.
1021                  (setq nntp-server-xover (car commands))))
1022           (setq commands (cdr commands)))
1023         ;; If none of the commands worked, we disable XOVER.
1024         (when (eq nntp-server-xover 'try)
1025           (save-excursion
1026             (set-buffer nntp-server-buffer)
1027             (erase-buffer)
1028             (setq nntp-server-xover nil)))
1029         nntp-server-xover))))
1030
1031 (defun nntp-send-strings-to-server (&rest strings)
1032   "Send STRINGS to the server."
1033   (let ((cmd (concat (mapconcat 'identity strings " ") nntp-end-of-line)))
1034     ;; We open the nntp server if it is down.
1035     (or (nntp-server-opened (nnoo-current-server 'nntp))
1036         (nntp-open-server (nnoo-current-server 'nntp))
1037         (error (nntp-status-message)))
1038     ;; Send the strings.
1039     (process-send-string nntp-server-process cmd)
1040     t))
1041
1042 (defun nntp-send-region-to-server (begin end)
1043   "Send the current buffer region (from BEGIN to END) to the server."
1044   (save-excursion
1045     (let ((cur (current-buffer)))
1046       ;; Copy the buffer over to the send buffer.
1047       (nnheader-set-temp-buffer " *nntp send*")
1048       (insert-buffer-substring cur begin end)
1049       (save-excursion
1050         (set-buffer cur)
1051         (erase-buffer))
1052       ;; `process-send-region' does not work if the text to be sent is very
1053       ;; large, so we send it piecemeal.
1054       (let ((last (point-min))
1055             (size 100))                 ;Size of text sent at once.
1056         (while (/= last (point-max))
1057           (process-send-region 
1058            nntp-server-process
1059            last (setq last (min (+ last size) (point-max))))
1060           ;; Read any output from the server.  May be unnecessary.
1061           (accept-process-output)))
1062       (kill-buffer (current-buffer)))))
1063
1064 (defun nntp-open-server-semi-internal (server &optional service)
1065   "Open SERVER.
1066 If SERVER is nil, use value of environment variable `NNTPSERVER'.
1067 If SERVICE, this this as the port number."
1068   (nnheader-insert "")
1069   (let ((server (or server (getenv "NNTPSERVER")))
1070         (status nil)
1071         (timer 
1072          (and nntp-connection-timeout 
1073               (nnheader-run-at-time nntp-connection-timeout
1074                                     nil 'nntp-kill-connection server))))
1075     (save-excursion
1076       (set-buffer nntp-server-buffer)
1077       (setq nntp-status-string "")
1078       (message "nntp: Connecting to server on %s..." nntp-address)
1079       (cond ((and server (nntp-open-server-internal server service))
1080              (setq nntp-address server)
1081              (setq status
1082                    (condition-case nil
1083                        (nntp-wait-for-response "^[23].*\r?\n" 'slow)
1084                      (error nil)
1085                      (quit nil)))
1086              (unless status
1087                (nntp-close-server-internal server)
1088                (nnheader-report 
1089                 'nntp "Couldn't open connection to %s" nntp-address))
1090              (when nntp-server-process
1091                (set-process-sentinel 
1092                 nntp-server-process 'nntp-default-sentinel)
1093                ;; You can send commands at startup like AUTHINFO here.
1094                ;; Added by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>
1095                (run-hooks 'nntp-server-opened-hook)))
1096             ((null server)
1097              (nnheader-report 'nntp "NNTP server is not specified."))
1098             (t                          ; We couldn't open the server.
1099              (nnheader-report 
1100               'nntp (buffer-substring (point-min) (point-max)))))
1101       (when timer 
1102         (nnheader-cancel-timer timer))
1103       (message "")
1104       (unless status
1105         (nnoo-close-server 'nntp server)
1106         (setq nntp-async-number nil))
1107       status)))
1108
1109 (defvar nntp-default-directories '("~" "/tmp" "/")
1110   "Directories to as current directory in the nntp server buffer.")
1111
1112 (defun nntp-open-server-internal (server &optional service)
1113   "Open connection to news server on SERVER by SERVICE (default is nntp)."
1114   (let (proc)
1115     (save-excursion
1116       (set-buffer nntp-server-buffer)
1117       ;; Make sure we have a valid current directory for the
1118       ;; nntp server buffer.
1119       (unless (file-exists-p default-directory)
1120         (let ((dirs nntp-default-directories))
1121           (while dirs
1122             (when (file-exists-p (car dirs))
1123               (setq default-directory (car dirs)
1124                     dirs nil))
1125             (setq dirs (cdr dirs)))))
1126       (cond
1127        ((and (setq proc
1128                    (condition-case nil
1129                        (funcall nntp-open-server-function server)
1130                      (error nil)))
1131              (memq (process-status proc) '(open run)))
1132         (setq nntp-server-process proc)
1133         (setq nntp-address server)
1134         ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1135         (process-kill-without-query proc)
1136         (run-hooks 'nntp-server-hook)
1137         (push proc nntp-opened-connections)
1138         (condition-case ()
1139             (nntp-read-server-type)
1140           (error 
1141            (nnheader-report 'nntp "Couldn't open server %s" server)
1142            (nntp-close-server)))
1143         nntp-server-process)
1144        (t
1145         (nnheader-report 'nntp "Couldn't open server %s" server))))))
1146
1147 (defun nntp-read-server-type ()
1148   "Find out what the name of the server we have connected to is."
1149   ;; Wait for the status string to arrive.
1150   (nntp-wait-for-response "^.*\n" t)
1151   (setq nntp-server-type (buffer-string))
1152   (let ((alist nntp-server-action-alist)
1153         entry)
1154     ;; Run server-specific commmands.
1155     (while alist
1156       (setq entry (pop alist))
1157       (when (string-match (car entry) nntp-server-type)
1158         (if (and (listp (cadr entry))
1159                  (not (eq 'lambda (caadr entry))))
1160             (eval (cadr entry))
1161           (funcall (cadr entry)))))))
1162
1163 (defun nntp-open-network-stream (server)
1164   (open-network-stream 
1165    "nntpd" nntp-server-buffer server nntp-port-number))
1166
1167 (defun nntp-open-rlogin (server)
1168   (let ((proc (if nntp-rlogin-user-name
1169                   (start-process
1170                    "nntpd" nntp-server-buffer "rsh"
1171                    "-l" nntp-rlogin-user-name server
1172                    (mapconcat 'identity
1173                               nntp-rlogin-parameters " "))
1174                 (start-process
1175                  "nntpd" nntp-server-buffer "rsh" server
1176                  (mapconcat 'identity
1177                             nntp-rlogin-parameters " ")))))
1178     proc))
1179
1180 (defun nntp-telnet-to-machine ()
1181   (let (b)
1182     (telnet "localhost")
1183     (goto-char (point-min))
1184     (while (not (re-search-forward "^login: *" nil t))
1185       (sit-for 1)
1186       (goto-char (point-min)))
1187     (goto-char (point-max))
1188     (insert "larsi")
1189     (telnet-send-input)
1190     (setq b (point))
1191     (while (not (re-search-forward ">" nil t))
1192       (sit-for 1)
1193       (goto-char b))
1194     (goto-char (point-max))
1195     (insert "ls")
1196     (telnet-send-input)))
1197
1198 (defun nntp-close-server-internal (&optional server)
1199   "Close connection to news server."
1200   (nntp-possibly-change-server nil server)
1201   (if nntp-server-process
1202       (delete-process nntp-server-process))
1203   (setq nntp-server-process nil)
1204   (setq nntp-address ""))
1205
1206 (defun nntp-accept-response ()
1207   "Read response of server.
1208 It is well-known that the communication speed will be much improved by
1209 defining this function as macro."
1210   ;; To deal with server process exiting before
1211   ;;  accept-process-output is called.
1212   ;; Suggested by Jason Venner <jason@violet.berkeley.edu>.
1213   ;; This is a copy of `nntp-default-sentinel'.
1214   (let ((buf (current-buffer)))
1215     (prog1
1216         (if (or (not nntp-server-process)
1217                 (not (memq (process-status nntp-server-process) '(open run))))
1218             (error "nntp: Process connection closed; %s" (nntp-status-message))
1219           (if nntp-buggy-select
1220               (progn
1221                 ;; We cannot use `accept-process-output'.
1222                 ;; Fujitsu UTS requires messages during sleep-for.
1223                 ;; I don't know why.
1224                 (message "NNTP: Reading...")
1225                 (sleep-for 1)
1226                 (message ""))
1227             (condition-case errorcode
1228                 (accept-process-output nntp-server-process 1)
1229               (error
1230                (cond ((string-equal "select error: Invalid argument" 
1231                                     (nth 1 errorcode))
1232                       ;; Ignore select error.
1233                       nil)
1234                      (t
1235                       (signal (car errorcode) (cdr errorcode))))))))
1236       (set-buffer buf))))
1237
1238 (defun nntp-last-element (list)
1239   "Return last element of LIST."
1240   (while (cdr list)
1241     (setq list (cdr list)))
1242   (car list))
1243
1244 (defun nntp-possibly-change-server (newsgroup server &optional connectionless)
1245   "Check whether the virtual server needs changing."
1246   (when (and server
1247              (not (nntp-server-opened server)))
1248     ;; This virtual server isn't open, so we (re)open it here.
1249     (nntp-open-server server nil t))
1250   (when (and newsgroup 
1251              (not (equal newsgroup nntp-current-group)))
1252     ;; Set the proper current group.
1253     (nntp-request-group newsgroup server)))
1254  
1255 (defun nntp-try-list-active (group)
1256   (nntp-list-active-group group)
1257   (save-excursion
1258     (set-buffer nntp-server-buffer)
1259     (goto-char (point-min))
1260     (cond ((looking-at "5[0-9]+")
1261            (setq nntp-server-list-active-group nil))
1262           (t
1263            (setq nntp-server-list-active-group t)))))
1264
1265 (defun nntp-async-server-opened ()
1266   (and nntp-async-process
1267        (memq (process-status nntp-async-process) '(open run))))
1268
1269 (defun nntp-async-open-server ()
1270   (save-excursion
1271     (set-buffer (generate-new-buffer " *async-nntp*"))
1272     (setq nntp-async-buffer (current-buffer))
1273     (buffer-disable-undo (current-buffer)))
1274   (let ((nntp-server-process nil)
1275         (nntp-server-buffer nntp-async-buffer))
1276     (nntp-open-server-semi-internal nntp-address nntp-port-number)
1277     (if (not (setq nntp-async-process nntp-server-process))
1278         (progn
1279           (setq nntp-async-number nil))
1280       (set-process-buffer nntp-async-process nntp-async-buffer))))
1281
1282 (defun nntp-async-fetch-articles (article)
1283   (if (stringp article)
1284       ()
1285     (let ((articles (cdr (memq (assq article nntp-async-articles)
1286                                nntp-async-articles)))
1287           (max (cond ((numberp nntp-async-number)
1288                       nntp-async-number) 
1289                      ((eq nntp-async-number t)
1290                       (length nntp-async-articles))
1291                      (t 0)))
1292           nart)
1293       (while (and (>= (setq max (1- max)) 0)
1294                   articles)
1295         (or (memq (setq nart (caar articles)) nntp-async-fetched)
1296             (progn
1297               (nntp-async-send-strings "ARTICLE " (int-to-string nart))
1298               (setq nntp-async-fetched (cons nart nntp-async-fetched))))
1299         (setq articles (cdr articles))))))
1300
1301 (defun nntp-async-send-strings (&rest strings)
1302   (let ((cmd (concat (mapconcat 'identity strings " ") nntp-end-of-line)))
1303     (or (nntp-async-server-opened)
1304         (nntp-async-open-server)
1305         (error (nntp-status-message)))
1306     (process-send-string nntp-async-process cmd)))
1307
1308 (defun nntp-async-request-group (group)
1309   (if (equal group nntp-current-group)
1310       ()
1311     (let ((asyncs (assoc group nntp-async-group-alist)))
1312       ;; A new group has been selected, so we push the current state
1313       ;; of async articles on an alist, and pull the old state off.
1314       (setq nntp-async-group-alist 
1315             (cons (list nntp-current-group
1316                         nntp-async-articles nntp-async-fetched
1317                         nntp-async-process)
1318                   (delq asyncs nntp-async-group-alist)))
1319       (and asyncs
1320            (progn
1321              (setq nntp-async-articles (nth 1 asyncs))
1322              (setq nntp-async-fetched (nth 2 asyncs))
1323              (setq nntp-async-process (nth 3 asyncs)))))))
1324
1325 (provide 'nntp)
1326
1327 ;;; nntp.el ends here