* ChangeLog: Fix latin chars.
[elisp/gnus.git-] / lisp / nntp.el
1 ;;; nntp.el --- nntp access for Gnus
2 ;;; Copyright (C) 1987-90,92-98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'nnheader)
28 (require 'nnoo)
29 (require 'gnus-util)
30
31 (nnoo-declare nntp)
32
33 (eval-when-compile (require 'cl))
34
35 (defvoo nntp-address nil
36   "Address of the physical nntp server.")
37
38 (defvoo nntp-port-number "nntp"
39   "Port number on the physical nntp server.")
40
41 (defvoo nntp-server-opened-hook '(nntp-send-mode-reader)
42   "*Hook used for sending commands to the server at startup.
43 The default value is `nntp-send-mode-reader', which makes an innd
44 server spawn an nnrpd server.")
45
46 (defvoo nntp-authinfo-function 'nntp-send-authinfo
47   "Function used to send AUTHINFO to the server.
48 It is called with no parameters.")
49
50 (defvoo nntp-server-action-alist
51   '(("nntpd 1\\.5\\.11t"
52      (remove-hook 'nntp-server-opened-hook 'nntp-send-mode-reader))
53     ("NNRP server Netscape"
54      (setq nntp-server-list-active-group nil)))
55   "Alist of regexps to match on server types and actions to be taken.
56 For instance, if you want Gnus to beep every time you connect
57 to innd, you could say something like:
58
59 \(setq nntp-server-action-alist
60        '((\"innd\" (ding))))
61
62 You probably don't want to do that, though.")
63
64 (defvoo nntp-open-connection-function 'nntp-open-network-stream
65   "*Function used for connecting to a remote system.
66 It will be called with the buffer to output in.
67
68 Two pre-made functions are `nntp-open-network-stream', which is the
69 default, and simply connects to some port or other on the remote
70 system (see nntp-port-number).  The other are `nntp-open-rlogin',
71 which does an rlogin on the remote system, and then does a telnet to
72 the NNTP server available there (see nntp-rlogin-parameters) and
73 `nntp-open-telnet' which telnets to a remote system, logs in and does
74 the same.")
75
76 (defvoo nntp-rlogin-program "rsh"
77   "*Program used to log in on remote machines.
78 The default is \"rsh\", but \"ssh\" is a popular alternative.")
79
80 (defvoo nntp-rlogin-parameters '("telnet" "-8" "${NNTPSERVER:=news}" "nntp")
81   "*Parameters to `nntp-open-rlogin'.
82 That function may be used as `nntp-open-connection-function'.  In that
83 case, this list will be used as the parameter list given to rsh.")
84
85 (defvoo nntp-rlogin-user-name nil
86   "*User name on remote system when using the rlogin connect method.")
87
88 (defvoo nntp-telnet-parameters '("exec" "telnet" "-8" "${NNTPSERVER:=news}" "nntp")
89   "*Parameters to `nntp-open-telnet'.
90 That function may be used as `nntp-open-connection-function'.  In that
91 case, this list will be executed as a command after logging in
92 via telnet.")
93
94 (defvoo nntp-telnet-user-name nil
95   "User name to log in via telnet with.")
96
97 (defvoo nntp-telnet-passwd nil
98   "Password to use to log in via telnet with.")
99
100 (defvoo nntp-open-telnet-envuser nil
101   "*If non-nil, telnet session (client and server both) will support the ENVIRON option and not prompt for login name.")
102
103 (defvoo nntp-telnet-shell-prompt "bash\\|\$ *\r?$\\|> *\r?"
104   "*Regular expression to match the shell prompt on the remote machine.")
105
106 (defvoo nntp-telnet-command "telnet"
107   "Command used to start telnet.")
108
109 (defvoo nntp-telnet-switches '("-8")
110   "Switches given to the telnet command.")
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 or telnet 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-maximum-request 400
123   "*The maximum number of the requests sent to the NNTP server at one time.
124 If Emacs hangs up while retrieving headers, set the variable to a
125 lower value.")
126
127 (defvoo nntp-nov-is-evil nil
128   "*If non-nil, nntp will never attempt to use XOVER when talking to the server.")
129
130 (defvoo nntp-xover-commands '("XOVER" "XOVERVIEW")
131   "*List of strings that are used as commands to fetch NOV lines from a server.
132 The strings are tried in turn until a positive response is gotten.  If
133 none of the commands are successful, nntp will just grab headers one
134 by one.")
135
136 (defvoo nntp-nov-gap 5
137   "*Maximum allowed gap between two articles.
138 If the gap between two consecutive articles is bigger than this
139 variable, split the XOVER request into two requests.")
140
141 (defvoo nntp-prepare-server-hook nil
142   "*Hook run before a server is opened.
143 If can be used to set up a server remotely, for instance.  Say you
144 have an account at the machine \"other.machine\".  This machine has
145 access to an NNTP server that you can't access locally.  You could
146 then use this hook to rsh to the remote machine and start a proxy NNTP
147 server there that you can connect to.  See also
148 `nntp-open-connection-function'")
149
150 (defvoo nntp-warn-about-losing-connection t
151   "*If non-nil, beep when a server closes connection.")
152
153 (defcustom nntp-authinfo-file "~/.authinfo"
154   ".netrc-like file that holds nntp authinfo passwords."
155   :type
156   '(choice file
157            (repeat :tag "Entries"
158                    :menu-tag "Inline"
159                    (list :format "%v"
160                          :value ("" ("login" . "") ("password" . ""))
161                          (string :tag "Host")
162                          (checklist :inline t
163                                     (cons :format "%v"
164                                           (const :format "" "login")
165                                           (string :format "Login: %v"))
166                                     (cons :format "%v"
167                                           (const :format "" "password")
168                                           (string :format "Password: %v")))))))
169
170 \f
171
172 (defvoo nntp-connection-timeout nil
173   "*Number of seconds to wait before an nntp connection times out.
174 If this variable is nil, which is the default, no timers are set.")
175
176 ;;; Internal variables.
177
178 (defvar nntp-record-commands nil
179   "*If non-nil, nntp will record all commands in the \"*nntp-log*\" buffer.")
180
181 (defvar nntp-have-messaged nil)
182
183 (defvar nntp-process-wait-for nil)
184 (defvar nntp-process-to-buffer nil)
185 (defvar nntp-process-callback nil)
186 (defvar nntp-process-decode nil)
187 (defvar nntp-process-start-point nil)
188 (defvar nntp-inside-change-function nil)
189 (defvoo nntp-last-command-time nil)
190 (defvoo nntp-last-command nil)
191 (defvoo nntp-authinfo-password nil)
192 (defvoo nntp-authinfo-user nil)
193
194 (defvar nntp-connection-list nil)
195
196 (defvoo nntp-server-type nil)
197 (defvoo nntp-connection-alist nil)
198 (defvoo nntp-status-string "")
199 (defconst nntp-version "nntp 5.0")
200 (defvoo nntp-inhibit-erase nil)
201 (defvoo nntp-inhibit-output nil)
202
203 (defvoo nntp-server-xover 'try)
204 (defvoo nntp-server-list-active-group 'try)
205
206 (defvar nntp-async-needs-kluge
207   (string-match "^GNU Emacs 20\\.3\\." (emacs-version))
208   "*When non-nil, nntp will poll asynchronous connections
209 once a second.  By default, this is turned on only for Emacs
210 20.3, which has a bug that breaks nntp's normal method of
211 noticing asynchronous data.")
212
213 (defvar nntp-async-timer nil)
214 (defvar nntp-async-process-list nil)
215
216 (eval-and-compile
217   (autoload 'nnmail-read-passwd "nnmail")
218   (autoload 'open-ssl-stream "ssl"))
219
220 \f
221
222 ;;; Internal functions.
223
224 (defsubst nntp-send-string (process string)
225   "Send STRING to PROCESS."
226   ;; We need to store the time to provide timeouts, and
227   ;; to store the command so the we can replay the command
228   ;; if the server gives us an AUTHINFO challenge.
229   (setq nntp-last-command-time (current-time)
230         nntp-last-command string)
231   (when nntp-record-commands
232     (nntp-record-command string))
233   (process-send-string process (concat string nntp-end-of-line)))
234
235 (defun nntp-record-command (string)
236   "Record the command STRING."
237   (save-excursion
238     (set-buffer (get-buffer-create "*nntp-log*"))
239     (goto-char (point-max))
240     (let ((time (current-time)))
241       (insert (format-time-string "%Y%m%dT%H%M%S" time)
242               "." (format "%03d" (/ (nth 2 time) 1000))
243               " " nntp-address " " string "\n"))))
244
245 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
246   "Wait for WAIT-FOR to arrive from PROCESS."
247   (save-excursion
248     (set-buffer (process-buffer process))
249     (goto-char (point-min))
250     (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
251                     (looking-at "480"))
252                 (memq (process-status process) '(open run)))
253       (when (looking-at "480")
254         (nntp-handle-authinfo process))
255       (nntp-accept-process-output process)
256       (goto-char (point-min)))
257     (prog1
258         (cond
259          ((looking-at "[45]")
260           (progn
261             (nntp-snarf-error-message)
262             nil))
263          ((not (memq (process-status process) '(open run)))
264           (nnheader-report 'nntp "Server closed connection"))
265          (t
266           (goto-char (point-max))
267           (let ((limit (point-min)))
268             (while (not (re-search-backward wait-for limit t))
269               (nntp-accept-process-output process)
270               ;; We assume that whatever we wait for is less than 1000
271               ;; characters long.
272               (setq limit (max (- (point-max) 1000) (point-min)))
273               (goto-char (point-max))))
274           (nntp-decode-text (not decode))
275           (unless discard
276             (save-excursion
277               (set-buffer buffer)
278               (goto-char (point-max))
279               (insert-buffer-substring (process-buffer process))
280               ;; Nix out "nntp reading...." message.
281               (when nntp-have-messaged
282                 (setq nntp-have-messaged nil)
283                 (nnheader-message 5 ""))
284               t))))
285       (unless discard
286         (erase-buffer)))))
287
288 (defsubst nntp-find-connection (buffer)
289   "Find the connection delivering to BUFFER."
290   (let ((alist nntp-connection-alist)
291         (buffer (if (stringp buffer) (get-buffer buffer) buffer))
292         process entry)
293     (while (setq entry (pop alist))
294       (when (eq buffer (cadr entry))
295         (setq process (car entry)
296               alist nil)))
297     (when process
298       (if (memq (process-status process) '(open run))
299           process
300         (when (buffer-name (process-buffer process))
301           (kill-buffer (process-buffer process)))
302         (setq nntp-connection-alist (delq entry nntp-connection-alist))
303         nil))))
304
305 (defsubst nntp-find-connection-entry (buffer)
306   "Return the entry for the connection to BUFFER."
307   (assq (nntp-find-connection buffer) nntp-connection-alist))
308
309 (defun nntp-find-connection-buffer (buffer)
310   "Return the process connection buffer tied to BUFFER."
311   (let ((process (nntp-find-connection buffer)))
312     (when process
313       (process-buffer process))))
314
315 (defsubst nntp-retrieve-data (command address port buffer
316                                       &optional wait-for callback decode)
317   "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
318   (let ((process (or (nntp-find-connection buffer)
319                      (nntp-open-connection buffer))))
320     (if (not process)
321         (nnheader-report 'nntp "Couldn't open connection to %s" address)
322       (unless (or nntp-inhibit-erase nnheader-callback-function)
323         (save-excursion
324           (set-buffer (process-buffer process))
325           (erase-buffer)))
326       (when command
327         (nntp-send-string process command))
328       (cond
329        ((eq callback 'ignore)
330         t)
331        ((and callback wait-for)
332         (nntp-async-wait process wait-for buffer decode callback)
333         t)
334        (wait-for
335         (nntp-wait-for process wait-for buffer decode))
336        (t t)))))
337
338 (defsubst nntp-send-command (wait-for &rest strings)
339   "Send STRINGS to server and wait until WAIT-FOR returns."
340   (when (and (not nnheader-callback-function)
341              (not nntp-inhibit-output))
342     (save-excursion
343       (set-buffer nntp-server-buffer)
344       (erase-buffer)))
345   (nntp-retrieve-data
346    (mapconcat 'identity strings " ")
347    nntp-address nntp-port-number nntp-server-buffer
348    wait-for nnheader-callback-function))
349
350 (defun nntp-send-command-nodelete (wait-for &rest strings)
351   "Send STRINGS to server and wait until WAIT-FOR returns."
352   (nntp-retrieve-data
353    (mapconcat 'identity strings " ")
354    nntp-address nntp-port-number nntp-server-buffer
355    wait-for nnheader-callback-function))
356
357 (defun nntp-send-command-and-decode (wait-for &rest strings)
358   "Send STRINGS to server and wait until WAIT-FOR returns."
359   (when (and (not nnheader-callback-function)
360              (not nntp-inhibit-output))
361     (save-excursion
362       (set-buffer nntp-server-buffer)
363       (erase-buffer)))
364   (nntp-retrieve-data
365    (mapconcat 'identity strings " ")
366    nntp-address nntp-port-number nntp-server-buffer
367    wait-for nnheader-callback-function t))
368
369 (defun nntp-send-buffer (wait-for)
370   "Send the current buffer to server and wait until WAIT-FOR returns."
371   (when (and (not nnheader-callback-function)
372              (not nntp-inhibit-output))
373     (save-excursion
374       (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
375       (erase-buffer)))
376   (nntp-encode-text)
377   (process-send-region (nntp-find-connection nntp-server-buffer)
378                        (point-min) (point-max))
379   (nntp-retrieve-data
380    nil nntp-address nntp-port-number nntp-server-buffer
381    wait-for nnheader-callback-function))
382
383 \f
384
385 ;;; Interface functions.
386
387 (nnoo-define-basics nntp)
388
389 (defsubst nntp-next-result-arrived-p ()
390   (cond
391    ;; A result that starts with a 2xx code is terminated by
392    ;; a line with only a "." on it.
393    ((eq (char-after) ?2)
394     (if (re-search-forward "\n\\.\r?\n" nil t)
395         t
396       nil))
397    ;; A result that starts with a 3xx or 4xx code is terminated
398    ;; by a newline.
399    ((looking-at "[34]")
400     (if (search-forward "\n" nil t)
401         t
402       nil))
403    ;; No result here.
404    (t
405     nil)))
406
407 (deffoo nntp-retrieve-headers (articles &optional group server fetch-old)
408   "Retrieve the headers of ARTICLES."
409   (nntp-possibly-change-group group server)
410   (save-excursion
411     (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
412     (erase-buffer)
413     (if (and (not gnus-nov-is-evil)
414              (not nntp-nov-is-evil)
415              (nntp-retrieve-headers-with-xover articles fetch-old))
416         ;; We successfully retrieved the headers via XOVER.
417         'nov
418       ;; XOVER didn't work, so we do it the hard, slow and inefficient
419       ;; way.
420       (let ((number (length articles))
421             (count 0)
422             (received 0)
423             (last-point (point-min))
424             (buf (nntp-find-connection-buffer nntp-server-buffer))
425             (nntp-inhibit-erase t)
426             article)
427         ;; Send HEAD commands.
428       (while (setq article (pop articles))
429         (nntp-send-command
430          nil
431          "HEAD" (if (numberp article)
432                     (int-to-string article)
433                   ;; `articles' is either a list of article numbers
434                   ;; or a list of article IDs.
435                   article))
436         (incf count)
437         ;; Every 400 requests we have to read the stream in
438         ;; order to avoid deadlocks.
439         (when (or (null articles)       ;All requests have been sent.
440                   (zerop (% count nntp-maximum-request)))
441           (nntp-accept-response)
442           (while (progn
443                    (set-buffer buf)
444                    (goto-char last-point)
445                    ;; Count replies.
446                    (while (nntp-next-result-arrived-p)
447                      (setq last-point (point))
448                      (incf received))
449                    (< received count))
450             ;; If number of headers is greater than 100, give
451             ;;  informative messages.
452             (and (numberp nntp-large-newsgroup)
453                  (> number nntp-large-newsgroup)
454                  (zerop (% received 20))
455                  (nnheader-message 6 "NNTP: Receiving headers... %d%%"
456                                    (/ (* received 100) number)))
457             (nntp-accept-response))))
458         (and (numberp nntp-large-newsgroup)
459              (> number nntp-large-newsgroup)
460              (nnheader-message 6 "NNTP: Receiving headers...done"))
461
462         ;; Now all of replies are received.  Fold continuation lines.
463         (nnheader-fold-continuation-lines)
464         ;; Remove all "\r"'s.
465         (nnheader-strip-cr)
466         (copy-to-buffer nntp-server-buffer (point-min) (point-max))
467         'headers))))
468
469 (deffoo nntp-retrieve-groups (groups &optional server)
470   "Retrieve group info on GROUPS."
471   (nntp-possibly-change-group nil server)
472   (save-excursion
473     (set-buffer (nntp-find-connection-buffer nntp-server-buffer))
474     ;; The first time this is run, this variable is `try'.  So we
475     ;; try.
476     (when (eq nntp-server-list-active-group 'try)
477       (nntp-try-list-active (car groups)))
478     (erase-buffer)
479     (let ((count 0)
480           (received 0)
481           (last-point (point-min))
482           (nntp-inhibit-erase t)
483           (command (if nntp-server-list-active-group "LIST ACTIVE" "GROUP")))
484       (while groups
485         ;; Send the command to the server.
486         (nntp-send-command nil command (pop groups))
487         (incf count)
488         ;; Every 400 requests we have to read the stream in
489         ;; order to avoid deadlocks.
490         (when (or (null groups)         ;All requests have been sent.
491                   (zerop (% count nntp-maximum-request)))
492           (nntp-accept-response)
493           (while (progn
494                    (goto-char last-point)
495                    ;; Count replies.
496                    (while (re-search-forward "^[0-9]" nil t)
497                      (incf received))
498                    (setq last-point (point))
499                    (< received count))
500             (nntp-accept-response))))
501
502       ;; Wait for the reply from the final command.
503       (goto-char (point-max))
504       (re-search-backward "^[0-9]" nil t)
505       (when (looking-at "^[23]")
506         (while (progn
507                  (goto-char (point-max))
508                  (if (not nntp-server-list-active-group)
509                      (not (re-search-backward "\r?\n" (- (point) 3) t))
510                    (not (re-search-backward "^\\.\r?\n" (- (point) 4) t))))
511           (nntp-accept-response)))
512
513       ;; Now all replies are received.  We remove CRs.
514       (goto-char (point-min))
515       (while (search-forward "\r" nil t)
516         (replace-match "" t t))
517
518       (if (not nntp-server-list-active-group)
519           (progn
520             (copy-to-buffer nntp-server-buffer (point-min) (point-max))
521             'group)
522         ;; We have read active entries, so we just delete the
523         ;; superfluous gunk.
524         (goto-char (point-min))
525         (while (re-search-forward "^[.2-5]" nil t)
526           (delete-region (match-beginning 0)
527                          (progn (forward-line 1) (point))))
528         (copy-to-buffer nntp-server-buffer (point-min) (point-max))
529         'active))))
530
531 (deffoo nntp-retrieve-articles (articles &optional group server)
532   (nntp-possibly-change-group group server)
533   (save-excursion
534     (let ((number (length articles))
535           (count 0)
536           (received 0)
537           (last-point (point-min))
538           (buf (nntp-find-connection-buffer nntp-server-buffer))
539           (nntp-inhibit-erase t)
540           (map (apply 'vector articles))
541           (point 1)
542           article)
543       (set-buffer buf)
544       (erase-buffer)
545       ;; Send ARTICLE command.
546       (while (setq article (pop articles))
547         (nntp-send-command
548          nil
549          "ARTICLE" (if (numberp article)
550                        (int-to-string article)
551                      ;; `articles' is either a list of article numbers
552                      ;; or a list of article IDs.
553                      article))
554         (incf count)
555         ;; Every 400 requests we have to read the stream in
556         ;; order to avoid deadlocks.
557         (when (or (null articles)       ;All requests have been sent.
558                   (zerop (% count nntp-maximum-request)))
559           (nntp-accept-response)
560           (while (progn
561                    (set-buffer buf)
562                    (goto-char last-point)
563                    ;; Count replies.
564                    (while (nntp-next-result-arrived-p)
565                      (aset map received (cons (aref map received) (point)))
566                      (setq last-point (point))
567                      (incf received))
568                    (< received count))
569             ;; If number of headers is greater than 100, give
570             ;;  informative messages.
571             (and (numberp nntp-large-newsgroup)
572                  (> number nntp-large-newsgroup)
573                  (zerop (% received 20))
574                  (nnheader-message 6 "NNTP: Receiving articles... %d%%"
575                                    (/ (* received 100) number)))
576             (nntp-accept-response))))
577       (and (numberp nntp-large-newsgroup)
578            (> number nntp-large-newsgroup)
579            (nnheader-message 6 "NNTP: Receiving articles...done"))
580       
581       ;; Now we have all the responses.  We go through the results,
582       ;; wash it and copy it over to the server buffer.
583       (set-buffer nntp-server-buffer)
584       (erase-buffer)
585       (setq last-point (point-min))
586       (mapcar
587        (lambda (entry)
588          (narrow-to-region
589           (setq point (goto-char (point-max)))
590           (progn
591             (insert-buffer-substring buf last-point (cdr entry))
592             (point-max)))
593          (setq last-point (cdr entry))
594          (nntp-decode-text)
595          (widen)
596          (cons (car entry) point))
597        map))))
598
599 (defun nntp-try-list-active (group)
600   (nntp-list-active-group group)
601   (save-excursion
602     (set-buffer nntp-server-buffer)
603     (goto-char (point-min))
604     (cond ((or (eobp)
605                (looking-at "5[0-9]+"))
606            (setq nntp-server-list-active-group nil))
607           (t
608            (setq nntp-server-list-active-group t)))))
609
610 (deffoo nntp-list-active-group (group &optional server)
611   "Return the active info on GROUP (which can be a regexp)."
612   (nntp-possibly-change-group nil server)
613   (nntp-send-command "^\\.*\r?\n" "LIST ACTIVE" group))
614
615 (deffoo nntp-request-group-articles (group &optional server)
616   "Return the list of existing articles in GROUP."
617   (nntp-possibly-change-group nil server)
618   (nntp-send-command "^\\.*\r?\n" "LISTGROUP" group))
619
620 (deffoo nntp-request-article (article &optional group server buffer command)
621   (nntp-possibly-change-group group server)
622   (when (nntp-send-command-and-decode
623          "\r?\n\\.\r?\n" "ARTICLE"
624          (if (numberp article) (int-to-string article) article))
625     (if (and buffer
626              (not (equal buffer nntp-server-buffer)))
627         (save-excursion
628           (set-buffer nntp-server-buffer)
629           (copy-to-buffer buffer (point-min) (point-max))
630           (nntp-find-group-and-number))
631       (nntp-find-group-and-number))))
632
633 (deffoo nntp-request-head (article &optional group server)
634   (nntp-possibly-change-group group server)
635   (when (nntp-send-command
636          "\r?\n\\.\r?\n" "HEAD"
637          (if (numberp article) (int-to-string article) article))
638     (prog1
639         (nntp-find-group-and-number)
640       (nntp-decode-text))))
641
642 (deffoo nntp-request-body (article &optional group server)
643   (nntp-possibly-change-group group server)
644   (nntp-send-command-and-decode
645    "\r?\n\\.\r?\n" "BODY"
646    (if (numberp article) (int-to-string article) article)))
647
648 (deffoo nntp-request-group (group &optional server dont-check)
649   (nntp-possibly-change-group nil server)
650   (when (nntp-send-command "^[245].*\n" "GROUP" group)
651     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
652       (setcar (cddr entry) group))))
653
654 (deffoo nntp-close-group (group &optional server)
655   t)
656
657 (deffoo nntp-server-opened (&optional server)
658   "Say whether a connection to SERVER has been opened."
659   (and (nnoo-current-server-p 'nntp server)
660        nntp-server-buffer
661        (gnus-buffer-live-p nntp-server-buffer)
662        (nntp-find-connection nntp-server-buffer)))
663
664 (deffoo nntp-open-server (server &optional defs connectionless)
665   (nnheader-init-server-buffer)
666   (if (nntp-server-opened server)
667       t
668     (when (or (stringp (car defs))
669               (numberp (car defs)))
670       (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
671     (unless (assq 'nntp-address defs)
672       (setq defs (append defs (list (list 'nntp-address server)))))
673     (nnoo-change-server 'nntp server defs)
674     (unless connectionless
675       (or (nntp-find-connection nntp-server-buffer)
676           (nntp-open-connection nntp-server-buffer)))))
677
678 (deffoo nntp-close-server (&optional server)
679   (nntp-possibly-change-group nil server t)
680   (let ((process (nntp-find-connection nntp-server-buffer)))
681     (while process
682       (when (memq (process-status process) '(open run))
683         (ignore-errors
684           (nntp-send-string process "QUIT")
685           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
686             ;; Ok, this is evil, but when using telnet and stuff
687             ;; as the connection method, it's important that the
688             ;; QUIT command actually is sent out before we kill
689             ;; the process.  
690             (sleep-for 1))))
691       (when (buffer-name (process-buffer process))
692         (kill-buffer (process-buffer process)))
693       (setq process (car (pop nntp-connection-alist))))
694     (nnoo-close-server 'nntp)))
695
696 (deffoo nntp-request-close ()
697   (let (process)
698     (while (setq process (pop nntp-connection-list))
699       (when (memq (process-status process) '(open run))
700         (ignore-errors
701           (nntp-send-string process "QUIT")
702           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
703             ;; Ok, this is evil, but when using telnet and stuff
704             ;; as the connection method, it's important that the
705             ;; QUIT command actually is sent out before we kill
706             ;; the process.  
707             (sleep-for 1))))
708       (when (buffer-name (process-buffer process))
709         (kill-buffer (process-buffer process))))))
710
711 (deffoo nntp-request-list (&optional server)
712   (nntp-possibly-change-group nil server)
713   (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST"))
714
715 (deffoo nntp-request-list-newsgroups (&optional server)
716   (nntp-possibly-change-group nil server)
717   (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS"))
718
719 (deffoo nntp-request-newgroups (date &optional server)
720   (nntp-possibly-change-group nil server)
721   (save-excursion
722     (set-buffer nntp-server-buffer)
723     (prog1
724         (nntp-send-command
725          "^\\.\r?\n" "NEWGROUPS"
726          (format-time-string "%y%m%d %H%M%S" (date-to-time date)))
727       (nntp-decode-text))))
728
729 (deffoo nntp-request-post (&optional server)
730   (nntp-possibly-change-group nil server)
731   (when (nntp-send-command "^[23].*\r?\n" "POST")
732     (nntp-send-buffer "^[23].*\n")))
733
734 (deffoo nntp-request-type (group article)
735   'news)
736
737 (deffoo nntp-asynchronous-p ()
738   t)
739
740 ;;; Hooky functions.
741
742 (defun nntp-send-mode-reader ()
743   "Send the MODE READER command to the nntp server.
744 This function is supposed to be called from `nntp-server-opened-hook'.
745 It will make innd servers spawn an nnrpd process to allow actual article
746 reading."
747   (nntp-send-command "^.*\n" "MODE READER"))
748
749 (defun nntp-send-authinfo (&optional send-if-force)
750   "Send the AUTHINFO to the nntp server.
751 It will look in the \"~/.authinfo\" file for matching entries.  If
752 nothing suitable is found there, it will prompt for a user name
753 and a password.
754
755 If SEND-IF-FORCE, only send authinfo to the server if the
756 .authinfo file has the FORCE token."
757   (let* ((list (gnus-parse-netrc nntp-authinfo-file))
758          (alist (gnus-netrc-machine list nntp-address))
759          (force (gnus-netrc-get alist "force"))
760          (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
761          (passwd (gnus-netrc-get alist "password")))
762     (when (or (not send-if-force)
763               force)
764       (unless user
765         (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
766               nntp-authinfo-user user))
767       (unless (member user '(nil ""))
768         (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
769         (when t                         ;???Should check if AUTHINFO succeeded
770       (nntp-send-command
771        "^2.*\r?\n" "AUTHINFO PASS"
772        (or passwd
773            nntp-authinfo-password
774            (setq nntp-authinfo-password
775                      (nnmail-read-passwd (format "NNTP (%s@%s) password: "
776                                                  user nntp-address))))))))))
777
778 (defun nntp-send-nosy-authinfo ()
779   "Send the AUTHINFO to the nntp server."
780   (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
781     (unless (member user '(nil ""))
782       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
783       (when t                           ;???Should check if AUTHINFO succeeded
784         (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
785                            (nnmail-read-passwd "NNTP (%s@%s) password: "
786                                                user nntp-address))))))
787
788 (defun nntp-send-authinfo-from-file ()
789   "Send the AUTHINFO to the nntp server.
790
791 The authinfo login name is taken from the user's login name and the
792 password contained in '~/.nntp-authinfo'."
793   (when (file-exists-p "~/.nntp-authinfo")
794     (with-temp-buffer
795       (insert-file-contents "~/.nntp-authinfo")
796       (goto-char (point-min))
797       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
798       (nntp-send-command
799        "^2.*\r?\n" "AUTHINFO PASS"
800        (buffer-substring (point) (progn (end-of-line) (point)))))))
801
802 ;;; Internal functions.
803
804 (defun nntp-handle-authinfo (process)
805   "Take care of an authinfo response from the server."
806   (let ((last nntp-last-command))
807     (funcall nntp-authinfo-function)
808     ;; We have to re-send the function that was interrupted by
809     ;; the authinfo request.
810     (save-excursion
811       (set-buffer nntp-server-buffer)
812       (erase-buffer))
813     (nntp-send-string process last)))
814
815 (defun nntp-make-process-buffer (buffer)
816   "Create a new, fresh buffer usable for nntp process connections."
817   (save-excursion
818     (set-buffer
819      (generate-new-buffer
820       (format " *server %s %s %s*"
821               nntp-address nntp-port-number
822               (gnus-buffer-exists-p buffer))))
823     (set (make-local-variable 'after-change-functions) nil)
824     (set (make-local-variable 'nntp-process-wait-for) nil)
825     (set (make-local-variable 'nntp-process-callback) nil)
826     (set (make-local-variable 'nntp-process-to-buffer) nil)
827     (set (make-local-variable 'nntp-process-start-point) nil)
828     (set (make-local-variable 'nntp-process-decode) nil)
829     (current-buffer)))
830
831 (defun nntp-open-connection (buffer)
832   "Open a connection to PORT on ADDRESS delivering output to BUFFER."
833   (run-hooks 'nntp-prepare-server-hook)
834   (let* ((pbuffer (nntp-make-process-buffer buffer))
835          (timer 
836           (and nntp-connection-timeout 
837                (nnheader-run-at-time
838                 nntp-connection-timeout nil
839                 `(lambda ()
840                    (when (buffer-name ,pbuffer)
841                      (kill-buffer ,pbuffer))))))
842          (process
843           (condition-case ()
844               (funcall nntp-open-connection-function pbuffer)
845             (error nil)
846             (quit nil))))
847     (when timer 
848       (nnheader-cancel-timer timer))
849     (when (and (buffer-name pbuffer)
850                process)
851       (process-kill-without-query process)
852       (nntp-wait-for process "^.*\n" buffer nil t)
853       (if (memq (process-status process) '(open run))
854           (prog1
855               (caar (push (list process buffer nil) nntp-connection-alist))
856             (push process nntp-connection-list)
857             (save-excursion
858               (set-buffer pbuffer)
859               (nntp-read-server-type)
860               (erase-buffer)
861               (set-buffer nntp-server-buffer)
862               (let ((nnheader-callback-function nil))
863                 (run-hooks 'nntp-server-opened-hook)
864                 (nntp-send-authinfo t))))
865         (when (buffer-name (process-buffer process))
866           (kill-buffer (process-buffer process)))
867         nil))))
868
869 (defun nntp-open-network-stream (buffer)
870   (open-network-stream-as-binary
871    "nntpd" buffer nntp-address nntp-port-number))
872
873 (defun nntp-open-ssl-stream (buffer)
874   (let* ((ssl-program-arguments '("-connect" (concat host ":" service)))
875          (proc (open-ssl-stream "nntpd" buffer nntp-address nntp-port-number)))
876     (save-excursion
877       (set-buffer buffer)
878       (nntp-wait-for-string "^\r*20[01]")
879       (beginning-of-line)
880       (delete-region (point-min) (point))
881       proc)))
882
883 (defun nntp-read-server-type ()
884   "Find out what the name of the server we have connected to is."
885   ;; Wait for the status string to arrive.
886   (setq nntp-server-type (buffer-string))
887   (let ((alist nntp-server-action-alist)
888         (case-fold-search t)
889         entry)
890     ;; Run server-specific commands.
891     (while alist
892       (setq entry (pop alist))
893       (when (string-match (car entry) nntp-server-type)
894         (if (and (listp (cadr entry))
895                  (not (eq 'lambda (caadr entry))))
896             (eval (cadr entry))
897           (funcall (cadr entry)))))))
898
899 (defun nntp-async-wait (process wait-for buffer decode callback)
900   (save-excursion
901     (set-buffer (process-buffer process))
902     (unless nntp-inside-change-function
903       (erase-buffer))
904     (setq nntp-process-wait-for wait-for
905           nntp-process-to-buffer buffer
906           nntp-process-decode decode
907           nntp-process-callback callback
908           nntp-process-start-point (point-max))
909     (setq after-change-functions '(nntp-after-change-function))
910     (if nntp-async-needs-kluge
911         (nntp-async-kluge process))))
912
913 (defun nntp-async-kluge (process)
914   ;; emacs 20.3 bug: process output with encoding 'binary
915   ;; doesn't trigger after-change-functions.
916   (unless nntp-async-timer
917     (setq nntp-async-timer
918           (nnheader-run-at-time 1 1 'nntp-async-timer-handler)))
919   (add-to-list 'nntp-async-process-list process))
920
921 (defun nntp-async-timer-handler ()
922   (mapcar
923    (lambda (proc)
924      (if (memq (process-status proc) '(open run))
925          (nntp-async-trigger proc)
926        (nntp-async-stop proc)))
927    nntp-async-process-list))
928
929 (defun nntp-async-stop (proc)
930   (setq nntp-async-process-list (delq proc nntp-async-process-list))
931   (when (and nntp-async-timer (not nntp-async-process-list))
932     (nnheader-cancel-timer nntp-async-timer)
933     (setq nntp-async-timer nil)))
934
935 (defun nntp-after-change-function (beg end len)
936   (unwind-protect
937       ;; we only care about insertions at eob
938       (when (and (eq 0 len) (eq (point-max) end))
939         (save-match-data
940           (let ((proc (get-buffer-process (current-buffer))))
941             (when proc
942               (nntp-async-trigger proc)))))
943     ;; any throw from after-change-functions will leave it
944     ;; set to nil.  so we reset it here, if necessary.
945     (when quit-flag
946       (setq after-change-functions '(nntp-after-change-function)))))
947
948 (defun nntp-async-trigger (process)
949   (save-excursion
950     (set-buffer (process-buffer process))
951     (when nntp-process-callback
952       ;; do we have an error message?
953       (goto-char nntp-process-start-point)
954       (if (memq (following-char) '(?4 ?5))
955           ;; wants credentials?
956           (if (looking-at "480")
957               (nntp-handle-authinfo nntp-process-to-buffer)
958             ;; report error message.
959             (nntp-snarf-error-message)
960             (nntp-do-callback nil))
961
962         ;; got what we expect?
963         (goto-char (point-max))
964         (when (re-search-backward
965                nntp-process-wait-for nntp-process-start-point t)
966           (nntp-async-stop process)
967           ;; convert it.
968           (when (gnus-buffer-exists-p nntp-process-to-buffer)
969             (let ((buf (current-buffer))
970                   (start nntp-process-start-point)
971                   (decode nntp-process-decode))
972               (save-excursion
973                 (set-buffer nntp-process-to-buffer)
974                 (goto-char (point-max))
975                 (save-restriction
976                   (narrow-to-region (point) (point))
977                   (insert-buffer-substring buf start)
978                   (when decode
979                     (nntp-decode-text))))))
980           ;; report it.
981           (goto-char (point-max))
982           (nntp-do-callback
983            (buffer-name (get-buffer nntp-process-to-buffer))))))))
984
985 (defun nntp-do-callback (arg)
986   (let ((callback nntp-process-callback)
987         (nntp-inside-change-function t))
988     (setq nntp-process-callback nil)
989     (funcall callback arg)))
990
991 (defun nntp-snarf-error-message ()
992   "Save the error message in the current buffer."
993   (let ((message (buffer-string)))
994     (while (string-match "[\r\n]+" message)
995       (setq message (replace-match " " t t message)))
996     (nnheader-report 'nntp message)
997     message))
998
999 (defun nntp-accept-process-output (process &optional timeout)
1000   "Wait for output from PROCESS and message some dots."
1001   (save-excursion
1002     (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
1003                     nntp-server-buffer))
1004     (let ((len (/ (point-max) 1024))
1005           message-log-max)
1006       (unless (< len 10)
1007         (setq nntp-have-messaged t)
1008         (nnheader-message 7 "nntp read: %dk" len)))
1009     (accept-process-output process (or timeout 1))))
1010
1011 (defun nntp-accept-response ()
1012   "Wait for output from the process that outputs to BUFFER."
1013   (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
1014
1015 (defun nntp-possibly-change-group (group server &optional connectionless)
1016   (let ((nnheader-callback-function nil))
1017     (when server
1018       (or (nntp-server-opened server)
1019           (nntp-open-server server nil connectionless)))
1020
1021     (unless connectionless
1022       (or (nntp-find-connection nntp-server-buffer)
1023           (nntp-open-connection nntp-server-buffer))))
1024
1025   (when group
1026     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
1027       (when (not (equal group (caddr entry)))
1028         (save-excursion
1029           (set-buffer (process-buffer (car entry)))
1030           (erase-buffer)
1031           (nntp-send-command "^[245].*\n" "GROUP" group)
1032           (setcar (cddr entry) group)
1033           (erase-buffer))))))
1034
1035 (defun nntp-decode-text (&optional cr-only)
1036   "Decode the text in the current buffer."
1037   (goto-char (point-min))
1038   (while (search-forward "\r" nil t)
1039     (delete-char -1))
1040   (unless cr-only
1041     ;; Remove trailing ".\n" end-of-transfer marker.
1042     (goto-char (point-max))
1043     (forward-line -1)
1044     (when (looking-at ".\n")
1045       (delete-char 2))
1046     ;; Delete status line.
1047     (goto-char (point-min))
1048     (delete-region (point) (progn (forward-line 1) (point)))
1049     ;; Remove "." -> ".." encoding.
1050     (while (search-forward "\n.." nil t)
1051       (delete-char -1))))
1052
1053 (defun nntp-encode-text ()
1054   "Encode the text in the current buffer."
1055   (save-excursion
1056     ;; Replace "." at beginning of line with "..".
1057     (goto-char (point-min))
1058     (while (re-search-forward "^\\." nil t)
1059       (insert "."))
1060     (goto-char (point-max))
1061     ;; Insert newline at the end of the buffer.
1062     (unless (bolp)
1063       (insert "\n"))
1064     ;; Insert `.' at end of buffer (end of text mark).
1065     (goto-char (point-max))
1066     (insert ".\n")
1067     (goto-char (point-min))
1068     (while (not (eobp))
1069       (end-of-line)
1070       (delete-char 1)
1071       (insert nntp-end-of-line))))
1072
1073 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1074   (set-buffer nntp-server-buffer)
1075   (erase-buffer)
1076   (cond
1077
1078    ;; This server does not talk NOV.
1079    ((not nntp-server-xover)
1080     nil)
1081
1082    ;; We don't care about gaps.
1083    ((or (not nntp-nov-gap)
1084         fetch-old)
1085     (nntp-send-xover-command
1086      (if fetch-old
1087          (if (numberp fetch-old)
1088              (max 1 (- (car articles) fetch-old))
1089            1)
1090        (car articles))
1091      (car (last articles)) 'wait)
1092
1093     (goto-char (point-min))
1094     (when (looking-at "[1-5][0-9][0-9] ")
1095       (delete-region (point) (progn (forward-line 1) (point))))
1096     (while (search-forward "\r" nil t)
1097       (replace-match "" t t))
1098     (goto-char (point-max))
1099     (forward-line -1)
1100     (when (looking-at "\\.")
1101       (delete-region (point) (progn (forward-line 1) (point)))))
1102
1103    ;; We do it the hard way.  For each gap, an XOVER command is sent
1104    ;; to the server.  We do not wait for a reply from the server, we
1105    ;; just send them off as fast as we can.  That means that we have
1106    ;; to count the number of responses we get back to find out when we
1107    ;; have gotten all we asked for.
1108    ((numberp nntp-nov-gap)
1109     (let ((count 0)
1110           (received 0)
1111           (last-point (point-min))
1112           (buf nntp-server-buffer)
1113           ;;(process-buffer (nntp-find-connection (current-buffer))))
1114           first)
1115       ;; We have to check `nntp-server-xover'.  If it gets set to nil,
1116       ;; that means that the server does not understand XOVER, but we
1117       ;; won't know that until we try.
1118       (while (and nntp-server-xover articles)
1119         (setq first (car articles))
1120         ;; Search forward until we find a gap, or until we run out of
1121         ;; articles.
1122         (while (and (cdr articles)
1123                     (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1124           (setq articles (cdr articles)))
1125
1126         (when (nntp-send-xover-command first (car articles))
1127           (setq articles (cdr articles)
1128                 count (1+ count))
1129
1130           ;; Every 400 requests we have to read the stream in
1131           ;; order to avoid deadlocks.
1132           (when (or (null articles)     ;All requests have been sent.
1133                     (zerop (% count nntp-maximum-request)))
1134             (accept-process-output)
1135             ;; On some Emacs versions the preceding function has
1136             ;; a tendency to change the buffer.  Perhaps.  It's
1137             ;; quite difficult to reproduce, because it only
1138             ;; seems to happen once in a blue moon.
1139             (set-buffer buf)
1140             (while (progn
1141                      (goto-char last-point)
1142                      ;; Count replies.
1143                      (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
1144                        (setq received (1+ received)))
1145                      (setq last-point (point))
1146                      (< received count))
1147               (accept-process-output)
1148               (set-buffer buf)))))
1149
1150       (when nntp-server-xover
1151         ;; Wait for the reply from the final command.
1152         (goto-char (point-max))
1153         (re-search-backward "^[0-9][0-9][0-9] " nil t)
1154         (when (looking-at "^[23]")
1155           (while (progn
1156                    (goto-char (point-max))
1157                    (forward-line -1)
1158                    (not (looking-at "^\\.\r?\n")))
1159             (nntp-accept-response)))
1160
1161         ;; We remove any "." lines and status lines.
1162         (goto-char (point-min))
1163         (while (search-forward "\r" nil t)
1164           (delete-char -1))
1165         (goto-char (point-min))
1166         (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1167         t))))
1168
1169   nntp-server-xover)
1170
1171 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1172   "Send the XOVER command to the server."
1173   (let ((range (format "%d-%d" beg end))
1174         (nntp-inhibit-erase t))
1175     (if (stringp nntp-server-xover)
1176         ;; If `nntp-server-xover' is a string, then we just send this
1177         ;; command.
1178         (if wait-for-reply
1179             (nntp-send-command-nodelete
1180              "\r?\n\\.\r?\n" nntp-server-xover range)
1181           ;; We do not wait for the reply.
1182           (nntp-send-command-nodelete "\r?\n\\.\r?\n" nntp-server-xover range))
1183       (let ((commands nntp-xover-commands))
1184         ;; `nntp-xover-commands' is a list of possible XOVER commands.
1185         ;; We try them all until we get at positive response.
1186         (while (and commands (eq nntp-server-xover 'try))
1187           (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1188           (save-excursion
1189             (set-buffer nntp-server-buffer)
1190             (goto-char (point-min))
1191             (and (looking-at "[23]")    ; No error message.
1192                  ;; We also have to look at the lines.  Some buggy
1193                  ;; servers give back simple lines with just the
1194                  ;; article number.  How... helpful.
1195                  (progn
1196                    (forward-line 1)
1197                    (looking-at "[0-9]+\t...")) ; More text after number.
1198                  (setq nntp-server-xover (car commands))))
1199           (setq commands (cdr commands)))
1200         ;; If none of the commands worked, we disable XOVER.
1201         (when (eq nntp-server-xover 'try)
1202           (save-excursion
1203             (set-buffer nntp-server-buffer)
1204             (erase-buffer)
1205             (setq nntp-server-xover nil)))
1206         nntp-server-xover))))
1207
1208 ;;; Alternative connection methods.
1209
1210 (defun nntp-wait-for-string (regexp)
1211   "Wait until string arrives in the buffer."
1212   (let ((buf (current-buffer)))
1213     (goto-char (point-min))
1214     (while (not (re-search-forward regexp nil t))
1215       (accept-process-output (nntp-find-connection nntp-server-buffer))
1216       (set-buffer buf)
1217       (goto-char (point-min)))))
1218
1219 (defun nntp-open-telnet (buffer)
1220   (save-excursion
1221     (set-buffer buffer)
1222     (erase-buffer)
1223     (let ((proc (as-binary-process
1224                  (apply
1225                   'start-process
1226                   "nntpd" buffer nntp-telnet-command nntp-telnet-switches)))
1227           (case-fold-search t))
1228       (when (memq (process-status proc) '(open run))
1229         (process-send-string proc "set escape \^X\n")
1230         (cond
1231          ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1232           (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1233                                             nntp-address "\n")))
1234          (t
1235           (process-send-string proc (concat "open " nntp-address "\n"))))
1236         (cond
1237          ((not nntp-open-telnet-envuser)
1238           (nntp-wait-for-string "^\r*.?login:")
1239           (process-send-string
1240            proc (concat
1241                  (or nntp-telnet-user-name
1242                      (setq nntp-telnet-user-name (read-string "login: ")))
1243                  "\n"))))
1244         (nntp-wait-for-string "^\r*.?password:")
1245         (process-send-string
1246          proc (concat
1247                (or nntp-telnet-passwd
1248                    (setq nntp-telnet-passwd
1249                          (nnmail-read-passwd "Password: ")))
1250                "\n"))
1251         (erase-buffer)
1252         (nntp-wait-for-string nntp-telnet-shell-prompt)
1253         (process-send-string
1254          proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1255         (nntp-wait-for-string "^\r*20[01]")
1256         (beginning-of-line)
1257         (delete-region (point-min) (point))
1258         (process-send-string proc "\^]")
1259         (nntp-wait-for-string "^telnet")
1260         (process-send-string proc "mode character\n")
1261         (accept-process-output proc 1)
1262         (sit-for 1)
1263         (goto-char (point-min))
1264         (forward-line 1)
1265         (delete-region (point) (point-max)))
1266       proc)))
1267
1268 (defun nntp-open-rlogin (buffer)
1269   "Open a connection to SERVER using rsh."
1270   (let ((proc (if nntp-rlogin-user-name
1271                   (as-binary-process
1272                    (apply 'start-process
1273                           "nntpd" buffer nntp-rlogin-program
1274                           nntp-address "-l" nntp-rlogin-user-name
1275                           nntp-rlogin-parameters))
1276                 (as-binary-process
1277                  (apply 'start-process
1278                         "nntpd" buffer nntp-rlogin-program nntp-address
1279                         nntp-rlogin-parameters)))))
1280     (save-excursion
1281       (set-buffer buffer)
1282       (nntp-wait-for-string "^\r*20[01]")
1283       (beginning-of-line)
1284       (delete-region (point-min) (point))
1285       proc)))
1286
1287 (defun nntp-find-group-and-number ()
1288   (save-excursion
1289     (save-restriction
1290       (set-buffer nntp-server-buffer)
1291       (narrow-to-region (goto-char (point-min))
1292                         (or (search-forward "\n\n" nil t) (point-max)))
1293       (goto-char (point-min))
1294       ;; We first find the number by looking at the status line.
1295       (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1296                          (string-to-int
1297                           (buffer-substring (match-beginning 1)
1298                                             (match-end 1)))))
1299             group newsgroups xref)
1300         (and number (zerop number) (setq number nil))
1301         ;; Then we find the group name.
1302         (setq group
1303               (cond
1304                ;; If there is only one group in the Newsgroups header,
1305                ;; then it seems quite likely that this article comes
1306                ;; from that group, I'd say.
1307                ((and (setq newsgroups (mail-fetch-field "newsgroups"))
1308                      (not (string-match "," newsgroups)))
1309                 newsgroups)
1310                ;; If there is more than one group in the Newsgroups
1311                ;; header, then the Xref header should be filled out.
1312                ;; We hazard a guess that the group that has this
1313                ;; article number in the Xref header is the one we are
1314                ;; looking for.  This might very well be wrong if this
1315                ;; article happens to have the same number in several
1316                ;; groups, but that's life.
1317                ((and (setq xref (mail-fetch-field "xref"))
1318                      number
1319                      (string-match (format "\\([^ :]+\\):%d" number) xref))
1320                 (substring xref (match-beginning 1) (match-end 1)))
1321                (t "")))
1322         (when (string-match "\r" group)
1323           (setq group (substring group 0 (match-beginning 0))))
1324         (cons group number)))))
1325
1326 (provide 'nntp)
1327
1328 ;;; nntp.el ends here