*** 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 'nnheader)
30 (require 'nnoo)
31 (eval-when-compile (require 'cl))
32
33 (nnoo-declare nntp)
34
35 (eval-and-compile
36   (unless (fboundp 'open-network-stream)
37     (require 'tcp)))
38
39 (eval-when-compile (require 'cl))
40
41 (eval-and-compile
42   (autoload 'cancel-timer "timer")
43   (autoload 'telnet "telnet" nil t)
44   (autoload 'telnet-send-input "telnet" nil t)
45   (autoload 'timezone-parse-date "timezone"))
46
47 (defvoo nntp-server-hook nil
48   "*Hooks for the NNTP server.
49 If the kanji code of the NNTP server is different from the local kanji
50 code, the correct kanji code of the buffer associated with the NNTP
51 server must be specified as follows:
52
53 \(setq nntp-server-hook
54       (function
55        (lambda ()
56          ;; Server's Kanji code is EUC (NEmacs hack).
57          (make-local-variable 'kanji-fileio-code)
58          (setq kanji-fileio-code 0))))
59
60 If you'd like to change something depending on the server in this
61 hook, use the variable `nntp-address'.")
62
63 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
64   "*Hook used for sending commands to the server at startup.  
65 The default value is `nntp-send-mode-reader', which makes an innd
66 server spawn an nnrpd server.  Another useful function to put in this
67 hook might be `nntp-send-authinfo', which will prompt for a password
68 to allow posting from the server.  Note that this is only necessary to
69 do on servers that use strict access control.")  
70 (add-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)
71
72 (defvoo nntp-server-action-alist 
73   '(("nntpd 1\\.5\\.11t" 
74      (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader)))
75   "Alist of regexps to match on server types and actions to be taken.
76 For instance, if you want Gnus to beep every time you connect
77 to innd, you could say something like:
78
79 \(setq nntp-server-action-alist
80        '((\"innd\" (ding))))
81
82 You probably don't want to do that, though.")
83
84 (defvoo nntp-open-server-function 'nntp-open-network-stream
85   "*Function used for connecting to a remote system.
86 It will be called with the address of the remote system.
87
88 Two pre-made functions are `nntp-open-network-stream', which is the
89 default, and simply connects to some port or other on the remote
90 system (see nntp-port-number).  The other is `nntp-open-rlogin', which
91 does an rlogin on the remote system, and then does a telnet to the
92 NNTP server available there (see nntp-rlogin-parameters).")
93
94 (defvoo nntp-rlogin-parameters '("telnet" "${NNTPSERVER:=localhost}" "nntp")
95   "*Parameters to `nntp-open-login'.
96 That function may be used as `nntp-open-server-function'.  In that
97 case, this list will be used as the parameter list given to rsh.")
98
99 (defvoo nntp-rlogin-user-name nil
100   "*User name on remote system when using the rlogin connect method.")
101
102 (defvoo nntp-address nil
103   "*The name of the NNTP server.")
104
105 (defvoo nntp-port-number "nntp"
106   "*Port number to connect to.")
107
108 (defvoo nntp-end-of-line "\r\n"
109   "String to use on the end of lines when talking to the NNTP server.
110 This is \"\\r\\n\" by default, but should be \"\\n\" when
111 using rlogin to communicate with the server.")
112
113 (defvoo nntp-large-newsgroup 50
114   "*The number of the articles which indicates a large newsgroup.
115 If the number of the articles is greater than the value, verbose
116 messages will be shown to indicate the current status.")
117
118 (defvoo nntp-buggy-select (memq system-type '(fujitsu-uts))
119   "*t if your select routine is buggy.
120 If the select routine signals error or fall into infinite loop while
121 waiting for the server response, the variable must be set to t.  In
122 case of Fujitsu UTS, it is set to T since `accept-process-output'
123 doesn't work properly.")
124
125 (defvoo nntp-maximum-request 400
126   "*The maximum number of the requests sent to the NNTP server at one time.
127 If Emacs hangs up while retrieving headers, set the variable to a
128 lower value.")
129
130 (defvoo nntp-debug-read 10000
131   "*Display '...' every 10Kbytes of a message being received if it is non-nil.
132 If it is a number, dots are displayed per the number.")
133
134 (defvoo nntp-nov-is-evil nil
135   "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
136
137 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
138   "*List of strings that are used as commands to fetch NOV lines from a server.
139 The strings are tried in turn until a positive response is gotten. If
140 none of the commands are successful, nntp will just grab headers one
141 by one.")
142
143 (defvoo nntp-nov-gap 20
144   "*Maximum allowed gap between two articles.
145 If the gap between two consecutive articles is bigger than this
146 variable, split the XOVER request into two requests.")
147
148 (defvoo nntp-connection-timeout nil
149   "*Number of seconds to wait before an nntp connection times out.
150 If this variable is nil, which is the default, no timers are set.")
151
152 (defvoo nntp-command-timeout nil
153   "*Number of seconds to wait for a response when sending a command.
154 If this variable is nil, which is the default, no timers are set.")
155
156 (defvoo nntp-retry-on-break nil
157   "*If non-nil, re-send the command when the user types `C-g'.")
158
159 (defvoo nntp-news-default-headers nil
160   "*If non-nil, override `mail-default-headers' when posting news.")
161
162 (defvoo nntp-prepare-server-hook nil
163   "*Hook run before a server is opened.
164 If can be used to set up a server remotely, for instance.  Say you
165 have an account at the machine \"other.machine\".  This machine has
166 access to an NNTP server that you can't access locally.  You could
167 then use this hook to rsh to the remote machine and start a proxy NNTP
168 server there that you can connect to.")
169
170 (defvoo nntp-async-number 5
171   "*How many articles should be prefetched when in asynchronous mode.")
172
173 (defvoo nntp-warn-about-losing-connection t
174   "*If non-nil, beep when a server closes connection.")
175
176 \f
177
178 (defconst nntp-version "nntp 4.0"
179   "Version numbers of this version of NNTP.")
180
181 (defvar nntp-server-buffer nil
182   "Buffer associated with the NNTP server process.")
183
184 (defvoo nntp-server-process nil
185   "The NNTP server process.
186 You'd better not use this variable in NNTP front-end program, but
187 instead use `nntp-server-buffer'.")
188
189 (defvoo nntp-status-string nil
190   "Save the server response message.")
191
192 (defvar nntp-opened-connections nil
193   "All (possibly) opened connections.")
194
195 (defvoo nntp-server-xover 'try)
196 (defvoo nntp-server-list-active-group 'try)
197 (defvoo nntp-current-group "")
198 (defvoo nntp-server-type nil)
199
200 (defvoo nntp-async-process nil)
201 (defvoo nntp-async-buffer nil)
202 (defvoo nntp-async-articles nil)
203 (defvoo nntp-async-fetched nil)
204 (defvoo nntp-async-group-alist nil)
205
206 \f
207 ;;; Interface functions.
208
209 (nnoo-define-basics nntp)
210
211 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
212   "Retrieve the headers of ARTICLES."
213   (nntp-possibly-change-server group server)
214   (save-excursion
215     (set-buffer nntp-server-buffer)
216     (erase-buffer)
217     (if (and (not gnus-nov-is-evil) 
218              (not nntp-nov-is-evil)
219              (nntp-retrieve-headers-with-xover articles fetch-old))
220         ;; We successfully retrieved the headers via XOVER.
221         'nov
222       ;; XOVER didn't work, so we do it the hard, slow and inefficient
223       ;; way.  
224       (let ((number (length articles))
225             (count 0)
226             (received 0)
227             (message-log-max nil)
228             (last-point (point-min)))
229         ;; Send HEAD command.
230         (while articles
231           (nntp-send-strings-to-server 
232            "HEAD" (if (numberp (car articles)) 
233                       (int-to-string (car articles))
234                     ;; `articles' is either a list of article numbers
235                     ;; or a list of article IDs.
236                     (car articles)))
237           (setq articles (cdr articles)
238                 count (1+ count))
239           ;; Every 400 header requests we have to read the stream in
240           ;; order to avoid deadlocks.
241           (when (or (null articles)     ;All requests have been sent.
242                     (zerop (% count nntp-maximum-request)))
243             (nntp-accept-response)
244             (while (progn
245                      (goto-char last-point)
246                      ;; Count replies.
247                      (while (re-search-forward "^[0-9]" nil t)
248                        (setq received (1+ received)))
249                      (setq last-point (point))
250                      (< received count))
251               ;; If number of headers is greater than 100, give
252               ;;  informative messages.
253               (and (numberp nntp-large-newsgroup)
254                    (> number nntp-large-newsgroup)
255                    (zerop (% received 20))
256                    (nnheader-message 7 "NNTP: Receiving headers... %d%%"
257                                      (/ (* received 100) number)))
258               (nntp-accept-response))))
259         ;; Wait for text of last command.
260         (goto-char (point-max))
261         (re-search-backward "^[0-9]" nil t)
262         (when (looking-at "^[23]")
263           (while (progn
264                    (goto-char (- (point-max) 3))
265                    (not (looking-at "^\\.\r?\n")))
266             (nntp-accept-response)))
267         (and (numberp nntp-large-newsgroup)
268              (> number nntp-large-newsgroup)
269              (nnheader-message 7 "NNTP: Receiving headers...done"))
270
271         ;; Now all of replies are received.  Fold continuation lines.
272         (nnheader-fold-continuation-lines)
273         ;; Remove all "\r"'s.
274         (goto-char (point-min))
275         (while (search-forward "\r" nil t)
276           (replace-match "" t t))
277         'headers))))
278
279
280 (deffoo nntp-retrieve-groups (groups &optional server)
281   "Retrieve group info on GROUPS."
282   (nntp-possibly-change-server nil server)
283   (save-excursion
284     (set-buffer nntp-server-buffer)
285     ;; The first time this is run, this variable is `try'.  So we
286     ;; try.   
287     (when (eq nntp-server-list-active-group 'try)
288       (nntp-try-list-active (car groups)))
289     (erase-buffer)
290     (let ((count 0)
291           (received 0)
292           (last-point (point-min))
293           (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
294       (while groups
295         ;; Send the command to the server.
296         (nntp-send-strings-to-server command (car groups))
297         (setq groups (cdr groups))
298         (setq count (1+ count))
299         ;; Every 400 requests we have to read the stream in
300         ;; order to avoid deadlocks.
301         (when (or (null groups)         ;All requests have been sent.
302                   (zerop (% count nntp-maximum-request)))
303           (nntp-accept-response)
304           (while (progn
305                    (goto-char last-point)
306                    ;; Count replies.
307                    (while (re-search-forward "^[0-9]" nil t)
308                      (setq received (1+ received)))
309                    (setq last-point (point))
310                    (< received count))
311             (nntp-accept-response))))
312
313       ;; Wait for the reply from the final command.
314       (when nntp-server-list-active-group
315         (goto-char (point-max))
316         (re-search-backward "^[0-9]" nil t)
317         (when (looking-at "^[23]")
318           (while (progn
319                    (goto-char (- (point-max) 3))
320                    (not (looking-at "^\\.\r?\n")))
321             (nntp-accept-response))))
322
323       ;; Now all replies are received. We remove CRs.
324       (goto-char (point-min))
325       (while (search-forward "\r" nil t)
326         (replace-match "" t t))
327
328       (if (not nntp-server-list-active-group)
329           'group
330         ;; We have read active entries, so we just delete the
331         ;; superfluos gunk.
332         (goto-char (point-min))
333         (while (re-search-forward "^[.2-5]" nil t)
334           (delete-region (match-beginning 0) 
335                          (progn (forward-line 1) (point))))
336         'active))))
337
338 (deffoo nntp-open-server (server &optional defs connectionless)
339   "Open the virtual server SERVER.
340 If CONNECTIONLESS is non-nil, don't attempt to connect to any physical
341 servers."
342   ;; Called with just a port number as the defs.
343   (when (or (stringp (car defs))
344             (numberp (car defs)))
345     (setq defs `((nntp-port-number ,(car defs)))))
346   (unless (assq 'nntp-address defs)
347     (setq defs (append defs `((nntp-address ,server)))))
348   (nnoo-change-server 'nntp server defs)
349   (if (nntp-server-opened server)
350       t
351     (or (nntp-server-opened server)
352         connectionless
353         (prog2
354             (run-hooks 'nntp-prepare-server-hook)
355             (nntp-open-server-semi-internal nntp-address nntp-port-number)
356           (nnheader-insert "")))))
357
358 (deffoo nntp-close-server (&optional server)
359   "Close connection to SERVER."
360   (nntp-possibly-change-server nil server t)
361   (unwind-protect
362       (progn
363         ;; Un-set default sentinel function before closing connection.
364         (and nntp-server-process
365              (eq 'nntp-default-sentinel
366                  (process-sentinel nntp-server-process))
367              (set-process-sentinel nntp-server-process nil))
368         ;; We cannot send QUIT command unless the process is running.
369         (when (nntp-server-opened server)
370           (nntp-send-command nil "QUIT")
371           ;; Give the QUIT time to arrive.
372           (sleep-for 1)))
373     (nntp-close-server-internal server)))
374
375 (deffoo nntp-request-close ()
376   "Close all server connections."
377   (let (proc)
378     (while nntp-opened-connections
379       (when (setq proc (pop nntp-opened-connections))
380         ;; Un-set default sentinel function before closing connection.
381         (when (eq 'nntp-default-sentinel (process-sentinel proc))
382           (set-process-sentinel proc nil))
383         (condition-case ()
384             (process-send-string proc (concat "QUIT" nntp-end-of-line))
385           (error nil))
386         ;; Give the QUIT time to reach the server before we close
387         ;; down the process.
388         (sleep-for 1)
389         (delete-process proc)))
390     (and nntp-async-buffer
391          (buffer-name nntp-async-buffer)
392          (kill-buffer nntp-async-buffer))
393     (let ((alist (cddr (assq 'nntp nnoo-state-alist)))
394           entry)
395       (while (setq entry (pop alist))
396         (and (setq proc (cdr (assq 'nntp-async-buffer entry)))
397              (buffer-name proc)
398              (kill-buffer proc))))
399     (nnoo-close-server 'nntp)
400     (setq nntp-async-group-alist nil
401           nntp-async-articles nil)))
402
403 (deffoo nntp-server-opened (&optional server)
404   "Say whether a connection to SERVER has been opened."
405   (and (nnoo-current-server-p 'nntp server)
406        nntp-server-buffer
407        (buffer-name nntp-server-buffer)
408        nntp-server-process
409        (memq (process-status nntp-server-process) '(open run))))
410
411 (deffoo nntp-status-message (&optional server)
412   "Return server status as a string."
413   (if (and nntp-status-string
414            ;; NNN MESSAGE
415            (string-match "[0-9][0-9][0-9][ \t]+\\([^\r]*\\).*$"
416                          nntp-status-string))
417       (substring nntp-status-string (match-beginning 1) (match-end 1))
418     ;; Empty message if nothing.
419     (or nntp-status-string "")))
420
421 (deffoo nntp-request-article (id &optional group server buffer)
422   "Request article ID (Message-ID or number)."
423   (nntp-possibly-change-server group server)
424
425   (let (found)
426
427     ;; First we see whether we can get the article from the async buffer. 
428     (when (and (numberp id)
429                nntp-async-articles
430                (memq id nntp-async-fetched))
431       (save-excursion
432         (set-buffer nntp-async-buffer)
433         (let ((opoint (point))
434               (art (if (numberp id) (int-to-string id) id))
435               beg end)
436           (when (and (or (re-search-forward (concat "^2.. +" art) nil t)
437                          (progn
438                            (goto-char (point-min))
439                            (re-search-forward (concat "^2.. +" art) opoint t)))
440                      (progn
441                        (beginning-of-line)
442                        (setq beg (point)
443                              end (re-search-forward "^\\.\r?\n" nil t))))
444             (setq found t)
445             (save-excursion
446               (set-buffer (or buffer nntp-server-buffer))
447               (erase-buffer)
448               (insert-buffer-substring nntp-async-buffer beg end)
449               (let ((nntp-server-buffer (current-buffer)))
450                 (nntp-decode-text)))
451             (delete-region beg end)
452             (when nntp-async-articles
453               (nntp-async-fetch-articles id))))))
454
455     (if found 
456         id
457       ;; The article was not in the async buffer, so we fetch it now.
458       (unwind-protect
459           (progn
460             (if buffer (set-process-buffer nntp-server-process buffer))
461             (let ((nntp-server-buffer (or buffer nntp-server-buffer))
462                   (art (or (and (numberp id) (int-to-string id)) id)))
463               (prog1
464                   (and (nntp-send-command 
465                         ;; A bit odd regexp to ensure working over rlogin.
466                         "^\\.\r?\n" "ARTICLE" art)
467                        (if (numberp id) 
468                            (cons nntp-current-group id)
469                          ;; We find out what the article number was.
470                          (nntp-find-group-and-number)))
471                 (nntp-decode-text)
472                 (and nntp-async-articles (nntp-async-fetch-articles id)))))
473         (when buffer 
474           (set-process-buffer nntp-server-process nntp-server-buffer))))))
475
476 (deffoo nntp-request-body (id &optional group server)
477   "Request body of article ID (Message-ID or number)."
478   (nntp-possibly-change-server group server)
479   (prog1
480       ;; If NEmacs, end of message may look like: "\256\215" (".^M")
481       (nntp-send-command
482        "^\\.\r?\n" "BODY" (or (and (numberp id) (int-to-string id)) id))
483     (nntp-decode-text)))
484
485 (deffoo nntp-request-head (id &optional group server)
486   "Request head of article ID (Message-ID or number)."
487   (nntp-possibly-change-server group server)
488   (prog1
489       (when (nntp-send-command 
490              "^\\.\r?\n" "HEAD" (if (numberp id) (int-to-string id) id))
491         (if (numberp id) id
492           ;; We find out what the article number was.
493           (nntp-find-group-and-number)))
494     (nntp-decode-text)
495     (save-excursion
496       (set-buffer nntp-server-buffer)
497       (nnheader-fold-continuation-lines))))
498
499 (deffoo nntp-request-stat (id &optional group server)
500   "Request STAT of article ID (Message-ID or number)."
501   (nntp-possibly-change-server group server)
502   (nntp-send-command 
503    "^[23].*\r?\n" "STAT" (or (and (numberp id) (int-to-string id)) id)))
504
505 (deffoo nntp-request-type (group &optional article)
506   'news)
507
508 (deffoo nntp-request-group (group &optional server dont-check)
509   "Select GROUP."
510   (nntp-possibly-change-server nil server)
511   (setq nntp-current-group
512         (when (nntp-send-command "^2.*\r?\n" "GROUP" group)
513           group)))
514
515 (deffoo nntp-request-asynchronous (group &optional server articles)
516   "Enable pre-fetch in GROUP."
517   (when nntp-async-articles
518     (nntp-async-request-group group))
519   (when nntp-async-number
520     (if (not (or (nntp-async-server-opened)
521                  (nntp-async-open-server)))
522         ;; Couldn't open the second connection
523         (progn
524           (message "Can't open second connection to %s" nntp-address)
525           (ding)
526           (setq nntp-async-articles nil)
527           (sit-for 2))
528       ;; We opened the second connection (or it was opened already).  
529       (setq nntp-async-articles articles)
530       (setq nntp-async-fetched nil)
531       ;; Clear any old data.
532       (save-excursion
533         (set-buffer nntp-async-buffer)
534         (erase-buffer))
535       ;; Select the correct current group on this server.
536       (nntp-async-send-strings "GROUP" group)
537       t)))
538
539 (deffoo nntp-list-active-group (group &optional server)
540   "Return the active info on GROUP (which can be a regexp."
541   (nntp-possibly-change-server group server)
542   (nntp-send-command "^.*\r?\n" "LIST ACTIVE" group))
543
544 (deffoo nntp-request-group-description (group &optional server)
545   "Get the description of GROUP."
546   (nntp-possibly-change-server nil server)
547   (prog1
548       (nntp-send-command "^.*\r?\n" "XGTITLE" group)
549     (nntp-decode-text)))
550
551 (deffoo nntp-close-group (group &optional server)
552   "Close GROUP."
553   (setq nntp-current-group nil)
554   t)
555
556 (deffoo nntp-request-list (&optional server)
557   "List all active groups."
558   (nntp-possibly-change-server nil server)
559   (prog1
560       (nntp-send-command "^\\.\r?\n" "LIST")
561     (nntp-decode-text)))
562
563 (deffoo nntp-request-list-newsgroups (&optional server)
564   "Get descriptions on all groups on SERVER."
565   (nntp-possibly-change-server nil server)
566   (prog1
567       (nntp-send-command "^\\.\r?\n" "LIST NEWSGROUPS")
568     (nntp-decode-text)))
569
570 (deffoo nntp-request-newgroups (date &optional server)
571   "List groups that have arrived since DATE."
572   (nntp-possibly-change-server nil server)
573   (let* ((date (timezone-parse-date date))
574          (time-string
575           (format "%s%02d%02d %s%s%s"
576                   (substring (aref date 0) 2) (string-to-int (aref date 1)) 
577                   (string-to-int (aref date 2)) (substring (aref date 3) 0 2)
578                   (substring 
579                    (aref date 3) 3 5) (substring (aref date 3) 6 8))))
580     (prog1
581         (nntp-send-command "^\\.\r?\n" "NEWGROUPS" time-string)
582       (nntp-decode-text))))
583
584 (deffoo nntp-request-list-distributions (&optional server)
585   "List distributions."
586   (nntp-possibly-change-server nil server)
587   (prog1
588       (nntp-send-command "^\\.\r?\n" "LIST DISTRIBUTIONS")
589     (nntp-decode-text)))
590
591 (deffoo nntp-request-last (&optional group server)
592   "Decrease the current article pointer."
593   (nntp-possibly-change-server group server)
594   (nntp-send-command "^[23].*\r?\n" "LAST"))
595
596 (deffoo nntp-request-next (&optional group server)
597   "Advance the current article pointer."
598   (nntp-possibly-change-server group server)
599   (nntp-send-command "^[23].*\r?\n" "NEXT"))
600
601 (deffoo nntp-request-post (&optional server)
602   "Post the current buffer."
603   (nntp-possibly-change-server nil server)
604   (when (nntp-send-command "^[23].*\r?\n" "POST")
605     (nnheader-insert "")
606     (nntp-encode-text)
607     (nntp-send-region-to-server (point-min) (point-max))
608     ;; 1.2a NNTP's post command is buggy. "^M" (\r) is not
609     ;;  appended to end of the status message.
610     (nntp-wait-for-response "^[23].*\n")))
611
612 ;;; Internal functions.
613
614 (defun nntp-send-mode-reader ()
615   "Send the MODE READER command to the nntp server.
616 This function is supposed to be called from `nntp-server-opened-hook'.
617 It will make innd servers spawn an nnrpd process to allow actual article
618 reading."
619   (nntp-send-command "^.*\r?\n" "MODE READER"))
620
621 (defun nntp-send-nosy-authinfo ()
622   "Send the AUTHINFO to the nntp server.
623 This function is supposed to be called from `nntp-server-opened-hook'.
624 It will prompt for a password."
625   (nntp-send-command "^.*\r?\n" "AUTHINFO USER"
626                      (read-string "NNTP user name: "))
627   (nntp-send-command "^.*\r?\n" "AUTHINFO PASS" 
628                      (read-string "NNTP password: ")))
629
630 (defun nntp-send-authinfo ()
631   "Send the AUTHINFO to the nntp server.
632 This function is supposed to be called from `nntp-server-opened-hook'.
633 It will prompt for a password."
634   (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name))
635   (nntp-send-command "^.*\r?\n" "AUTHINFO PASS" 
636                      (read-string "NNTP password: ")))
637
638 (defun nntp-send-authinfo-from-file ()
639   "Send the AUTHINFO to the nntp server.
640 This function is supposed to be called from `nntp-server-opened-hook'.
641 It will prompt for a password."
642   (when (file-exists-p "~/.nntp-authinfo")
643     (save-excursion
644       (set-buffer (get-buffer-create " *authinfo*"))
645       (buffer-disable-undo (current-buffer))
646       (erase-buffer)
647       (insert-file-contents "~/.nntp-authinfo")
648       (goto-char (point-min))
649       (nntp-send-command "^.*\r?\n" "AUTHINFO USER" (user-login-name))
650       (nntp-send-command 
651        "^.*\r?\n" "AUTHINFO PASS" 
652        (buffer-substring (point) (progn (end-of-line) (point))))
653       (kill-buffer (current-buffer)))))
654
655 (defun nntp-default-sentinel (proc status)
656   "Default sentinel function for NNTP server process."
657   (let ((servers (cddr (assq 'nntp nnoo-state-alist)))
658         server)
659     ;; Go through the alist of server names and find the name of the
660     ;; server that the process that sent the signal is connected to.
661     ;; If you get my drift.
662     (if (equal proc nntp-server-process)
663         (setq server nntp-address)
664       (while (and servers 
665                   (not (equal proc (cdr (assq 'nntp-server-process
666                                               (car servers))))))
667         (setq servers (cdr servers)))
668       (setq server (caar servers)))
669     (when (and server
670                nntp-warn-about-losing-connection)
671       (nnheader-message 3 "nntp: Connection closed to server %s" server)
672       (setq nntp-current-group "")
673       (ding))))
674
675 (defun nntp-kill-connection (server)
676   "Choke the connection to SERVER."
677   (let ((proc (cdr (assq 'nntp-server-process 
678                          (assoc server (cddr 
679                                         (assq 'nntp nnoo-state-alist)))))))
680     (when proc 
681       (delete-process (process-name proc)))
682     (nntp-close-server server)
683     (nnheader-report
684      'nntp (message "Connection timed out to server %s" server))
685     (ding)
686     (sit-for 1)))
687
688 ;; Encoding and decoding of NNTP text.
689
690 (defun nntp-decode-text ()
691   "Decode text transmitted by NNTP.
692 0. Delete status line.
693 1. Delete `^M' at end of line.
694 2. Delete `.' at end of buffer (end of text mark).
695 3. Delete `.' at beginning of line."
696   (save-excursion
697     (set-buffer nntp-server-buffer)
698     ;; Insert newline at end of buffer.
699     (goto-char (point-max))
700     (or (bolp) (insert "\n"))
701     ;; Delete status line.
702     (delete-region (goto-char (point-min)) (progn (forward-line 1) (point)))
703     ;; Delete `^M's.
704     (while (search-forward "\r" nil t)
705       (replace-match "" t t))
706     ;; Delete `.' at end of the buffer (end of text mark).
707     (goto-char (point-max))
708     (forward-line -1)
709     (when (looking-at "^\\.\n")
710       (delete-region (point) (progn (forward-line 1) (point))))
711     ;; Replace `..' at beginning of line with `.'.
712     (goto-char (point-min))
713     ;; (replace-regexp "^\\.\\." ".")
714     (while (search-forward "\n.." nil t)
715       (delete-char -1))))
716
717 (defun nntp-encode-text ()
718   "Encode text in current buffer for NNTP transmission.
719 1. Insert `.' at beginning of line.
720 2. Insert `.' at end of buffer (end of text mark)."
721   (save-excursion
722     ;; Replace `.' at beginning of line with `..'.
723     (goto-char (point-min))
724     (while (search-forward "\n." nil t)
725       (insert "."))
726     (goto-char (point-max))
727     ;; Insert newline at end of buffer.
728     (or (bolp) (insert "\n"))
729     ;; Insert `.' at end of buffer (end of text mark).
730     (insert "." nntp-end-of-line)))
731
732 \f
733 ;;;
734 ;;; Synchronous Communication with NNTP servers.
735 ;;;
736
737 (defvar nntp-retry-command)
738
739 (defun nntp-send-command (response cmd &rest args)
740   "Wait for server RESPONSE after sending CMD and optional ARGS to server."
741   (let ((timer 
742          (and nntp-command-timeout 
743               (nnheader-run-at-time
744                nntp-command-timeout nil 'nntp-kill-command 
745                (nnoo-current-server 'nntp))))
746         (nntp-retry-command t)
747         result)
748     (unwind-protect
749         (save-excursion
750           (while nntp-retry-command
751             (setq nntp-retry-command nil)
752             ;; Clear communication buffer.
753             (set-buffer nntp-server-buffer)
754             (widen)
755             (erase-buffer)
756             (if nntp-retry-on-break
757                 (condition-case ()
758                     (progn
759                       (apply 'nntp-send-strings-to-server cmd args)
760                       (setq result
761                             (if response
762                                 (nntp-wait-for-response response)
763                               t)))
764                   (quit (setq nntp-retry-command t)))
765               (apply 'nntp-send-strings-to-server cmd args)
766               (setq result
767                     (if response
768                         (nntp-wait-for-response response)
769                       t))))
770           result)
771       (when timer 
772         (nnheader-cancel-timer timer)))))
773
774 (defun nntp-kill-command (server)
775   "Kill and restart the connection to SERVER."
776   (let ((proc (cdr (assq 
777                     'nntp-server-process 
778                     (assoc server (cddr (assq 'nntp nnoo-state-alist)))))))
779     (when proc 
780       (delete-process (process-name proc)))
781     (nntp-close-server server)
782     (nntp-open-server server)
783     (when nntp-current-group
784       (nntp-request-group nntp-current-group))
785     (setq nntp-retry-command t)))
786
787 (defun nntp-send-command-old (response cmd &rest args)
788   "Wait for server RESPONSE after sending CMD and optional ARGS to server."
789   (save-excursion
790     ;; Clear communication buffer.
791     (set-buffer nntp-server-buffer)
792     (erase-buffer)
793     (apply 'nntp-send-strings-to-server cmd args)
794     (if response
795         (nntp-wait-for-response response)
796       t)))
797
798 (defun nntp-wait-for-response (regexp &optional slow)
799   "Wait for server response which matches REGEXP."
800   (save-excursion
801     (let ((status t)
802           (wait t)
803           (dotnum 0)                    ;Number of "." being displayed.
804           (dotsize                      ;How often "." displayed.
805