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