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