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