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