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