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