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