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