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