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