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