Speedups; see ChangeLog for fine details.
[elisp/gnus.git-] / lisp / nntp.el
1 ;;; nntp.el --- nntp access for Gnus
2 ;;; Copyright (C) 1987-90,92-97 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 (eval-and-compile
212   (autoload 'nnmail-read-passwd "nnmail")
213   (autoload 'open-ssl-stream "ssl"))
214
215 \f
216
217 ;;; Internal functions.
218
219 (defsubst nntp-send-string (process string)
220   "Send STRING to PROCESS."
221   ;; We need to store the time to provide timeouts, and
222   ;; to store the command so the we can replay the command
223   ;; if the server gives us an AUTHINFO challenge.
224   (setq nntp-last-command-time (current-time)
225         nntp-last-command string)
226   (when nntp-record-commands
227     (nntp-record-command string))
228   (process-send-string process (concat string nntp-end-of-line)))
229
230 (defun nntp-record-command (string)
231   "Record the command STRING."
232   (save-excursion
233     (set-buffer (get-buffer-create "*nntp-log*"))
234     (goto-char (point-max))
235     (let ((time (current-time)))
236       (insert (format-time-string "%Y%m%dT%H%M%S" time)
237               "." (format "%03d" (/ (nth 2 time) 1000))
238               " " nntp-address " " string "\n"))))
239
240 (defsubst nntp-wait-for (process wait-for buffer &optional decode discard)
241   "Wait for WAIT-FOR to arrive from PROCESS."
242   (save-excursion
243     (set-buffer (process-buffer process))
244     (goto-char (point-min))
245     (while (and (or (not (memq (char-after (point)) '(?2 ?3 ?4 ?5)))
246                     (looking-at "480"))
247                 (memq (process-status process) '(open run)))
248       (when (looking-at "480")
249         (nntp-handle-authinfo process))
250       (nntp-accept-process-output process)
251       (goto-char (point-min)))
252     (prog1
253         (cond
254          ((looking-at "[45]")
255           (progn
256             (nntp-snarf-error-message)
257             nil))
258          ((not (memq (process-status process) '(open run)))
259           (nnheader-report 'nntp "Server closed connection"))
260          (t
261           (goto-char (point-max))
262           (let ((limit (point-min))
263                 response)
264             (while (not (re-search-backward wait-for limit t))
265               (nntp-accept-process-output process)
266               ;; We assume that whatever we wait for is less than 1000
267               ;; characters long.
268               (setq limit (max (- (point-max) 1000) (point-min)))
269               (goto-char (point-max)))
270             (setq response (match-string 0))
271             (save-current-buffer
272               (set-buffer nntp-server-buffer)
273               (setq nntp-process-response response)))
274           (nntp-decode-text (not decode))
275           (unless discard
276             (save-excursion
277               (set-buffer buffer)
278               (goto-char (point-max))
279               (insert-buffer-substring (process-buffer process))
280               ;; Nix out "nntp reading...." message.
281               (when nntp-have-messaged
282                 (setq nntp-have-messaged nil)
283                 (nnheader-message 5 ""))
284               t))))
285       (unless discard
286         (erase-buffer)))))
287
288 (defsubst nntp-find-connection (buffer)
289   "Find the connection delivering to BUFFER."
290   (let ((alist nntp-connection-alist)
291         (buffer (if (stringp buffer) (get-buffer buffer) buffer))
292         process entry)
293     (while (setq entry (pop alist))
294       (when (eq buffer (cadr entry))
295         (setq process (car entry)
296               alist nil)))
297     (when process
298       (if (memq (process-status process) '(open run))
299           process
300         (when (buffer-name (process-buffer process))
301           (kill-buffer (process-buffer process)))
302         (setq nntp-connection-alist (delq entry nntp-connection-alist))
303         nil))))
304
305 (defsubst nntp-find-connection-entry (buffer)
306   "Return the entry for the connection to BUFFER."
307   (assq (nntp-find-connection buffer) nntp-connection-alist))
308
309 (defun nntp-find-connection-buffer (buffer)
310   "Return the process connection buffer tied to BUFFER."
311   (let ((process (nntp-find-connection buffer)))
312     (when process
313       (process-buffer process))))
314
315 (defsubst nntp-retrieve-data (command address port buffer
316                                       &optional wait-for callback decode)
317   "Use COMMAND to retrieve data into BUFFER from PORT on ADDRESS."
318   (let ((process (or (nntp-find-connection buffer)
319                      (nntp-open-connection buffer))))
320     (if (not process)
321         (nnheader-report 'nntp "Couldn't open connection to %s" address)
322       (unless (or nntp-inhibit-erase nnheader-callback-function)
323         (save-excursion
324           (set-buffer (process-buffer process))
325           (erase-buffer)))
326       (when command
327         (nntp-send-string process command))
328       (cond
329        ((eq callback 'ignore)
330         t)
331        ((and callback wait-for)
332         (save-excursion
333           (set-buffer (process-buffer process))
334           (unless nntp-inside-change-function
335             (erase-buffer))
336           (setq nntp-process-decode decode
337                 nntp-process-to-buffer buffer
338                 nntp-process-wait-for wait-for
339                 nntp-process-callback callback
340                 nntp-process-start-point (point-max)
341                 after-change-functions
342                 (list 'nntp-after-change-function-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-article (article &optional group server buffer command)
626   (nntp-possibly-change-group group server)
627   (when (nntp-send-command-and-decode
628          "\r?\n\\.\r?\n" "ARTICLE"
629          (if (numberp article) (int-to-string article) article))
630     (if (and buffer
631              (not (equal buffer nntp-server-buffer)))
632         (save-excursion
633           (set-buffer nntp-server-buffer)
634           (copy-to-buffer buffer (point-min) (point-max))
635           (nntp-find-group-and-number))
636       (nntp-find-group-and-number))))
637
638 (deffoo nntp-request-head (article &optional group server)
639   (nntp-possibly-change-group group server)
640   (when (nntp-send-command
641          "\r?\n\\.\r?\n" "HEAD"
642          (if (numberp article) (int-to-string article) article))
643     (prog1
644         (nntp-find-group-and-number)
645       (nntp-decode-text))))
646
647 (deffoo nntp-request-body (article &optional group server)
648   (nntp-possibly-change-group group server)
649   (nntp-send-command-and-decode
650    "\r?\n\\.\r?\n" "BODY"
651    (if (numberp article) (int-to-string article) article)))
652
653 (deffoo nntp-request-group (group &optional server dont-check)
654   (nntp-possibly-change-group nil server)
655   (when (nntp-send-command "^[245].*\n" "GROUP" group)
656     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
657       (setcar (cddr entry) group))))
658
659 (deffoo nntp-close-group (group &optional server)
660   t)
661
662 (deffoo nntp-server-opened (&optional server)
663   "Say whether a connection to SERVER has been opened."
664   (and (nnoo-current-server-p 'nntp server)
665        nntp-server-buffer
666        (gnus-buffer-live-p nntp-server-buffer)
667        (nntp-find-connection nntp-server-buffer)))
668
669 (deffoo nntp-open-server (server &optional defs connectionless)
670   (nnheader-init-server-buffer)
671   (if (nntp-server-opened server)
672       t
673     (when (or (stringp (car defs))
674               (numberp (car defs)))
675       (setq defs (cons (list 'nntp-port-number (car defs)) (cdr defs))))
676     (unless (assq 'nntp-address defs)
677       (setq defs (append defs (list (list 'nntp-address server)))))
678     (nnoo-change-server 'nntp server defs)
679     (unless connectionless
680       (or (nntp-find-connection nntp-server-buffer)
681           (nntp-open-connection nntp-server-buffer)))))
682
683 (deffoo nntp-close-server (&optional server)
684   (nntp-possibly-change-group nil server t)
685   (let ((process (nntp-find-connection nntp-server-buffer)))
686     (while process
687       (when (memq (process-status process) '(open run))
688         (ignore-errors
689           (nntp-send-string process "QUIT")
690           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
691             ;; Ok, this is evil, but when using telnet and stuff
692             ;; as the connection method, it's important that the
693             ;; QUIT command actually is sent out before we kill
694             ;; the process.  
695             (sleep-for 1))))
696       (when (buffer-name (process-buffer process))
697         (kill-buffer (process-buffer process)))
698       (setq process (car (pop nntp-connection-alist))))
699     (nnoo-close-server 'nntp)))
700
701 (deffoo nntp-request-close ()
702   (let (process)
703     (while (setq process (pop nntp-connection-list))
704       (when (memq (process-status process) '(open run))
705         (ignore-errors
706           (nntp-send-string process "QUIT")
707           (unless (eq nntp-open-connection-function 'nntp-open-network-stream)
708             ;; Ok, this is evil, but when using telnet and stuff
709             ;; as the connection method, it's important that the
710             ;; QUIT command actually is sent out before we kill
711             ;; the process.  
712             (sleep-for 1))))
713       (when (buffer-name (process-buffer process))
714         (kill-buffer (process-buffer process))))))
715
716 (deffoo nntp-request-list (&optional server)
717   (nntp-possibly-change-group nil server)
718   (nntp-send-command-and-decode "\r?\n\\.\r?\n" "LIST"))
719
720 (deffoo nntp-request-list-newsgroups (&optional server)
721   (nntp-possibly-change-group nil server)
722   (nntp-send-command "\r?\n\\.\r?\n" "LIST NEWSGROUPS"))
723
724 (deffoo nntp-request-newgroups (date &optional server)
725   (nntp-possibly-change-group nil server)
726   (save-excursion
727     (set-buffer nntp-server-buffer)
728     (prog1
729         (nntp-send-command
730          "^\\.\r?\n" "NEWGROUPS"
731          (format-time-string "%y%m%d %H%M%S" (nnmail-date-to-time date)))
732       (nntp-decode-text))))
733
734 (deffoo nntp-request-post (&optional server)
735   (nntp-possibly-change-group nil server)
736   (when (nntp-send-command "^[23].*\r?\n" "POST")
737     (let ((response (save-current-buffer
738                       (set-buffer nntp-server-buffer)
739                       nntp-process-response))
740           server-id)
741       (when (and response
742                  (string-match "^[23].*\\(<[^\t\n @<>]+@[^\t\n @<>]+>\\)"
743                                response))
744         (setq server-id (match-string 1 response))
745         (narrow-to-region (goto-char (point-min))
746                           (if (search-forward "\n\n" nil t)
747                               (1- (point))
748                             (point-max)))
749         (unless (mail-fetch-field "Message-ID")
750           (goto-char (point-min))
751           (insert "Message-ID: " server-id "\n"))
752         (widen))
753       (run-hooks 'nntp-prepare-post-hook)
754       (nntp-send-buffer "^[23].*\n"))))
755
756 (deffoo nntp-request-type (group article)
757   'news)
758
759 (deffoo nntp-asynchronous-p ()
760   t)
761
762 ;;; Hooky functions.
763
764 (defun nntp-send-mode-reader ()
765   "Send the MODE READER command to the nntp server.
766 This function is supposed to be called from `nntp-server-opened-hook'.
767 It will make innd servers spawn an nnrpd process to allow actual article
768 reading."
769   (nntp-send-command "^.*\r?\n" "MODE READER"))
770
771 (defun nntp-send-authinfo (&optional send-if-force)
772   "Send the AUTHINFO to the nntp server.
773 It will look in the \"~/.authinfo\" file for matching entries.  If
774 nothing suitable is found there, it will prompt for a user name
775 and a password.
776
777 If SEND-IF-FORCE, only send authinfo to the server if the
778 .authinfo file has the FORCE token."
779   (let* ((list (gnus-parse-netrc nntp-authinfo-file))
780          (alist (gnus-netrc-machine list nntp-address))
781          (force (gnus-netrc-get alist "force"))
782          (user (or (gnus-netrc-get alist "login") nntp-authinfo-user))
783          (passwd (gnus-netrc-get alist "password")))
784     (when (or (not send-if-force)
785               force)
786       (unless user
787         (setq user (read-string (format "NNTP (%s) user name: " nntp-address))
788               nntp-authinfo-user user))
789       (unless (member user '(nil ""))
790         (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
791         (when t                         ;???Should check if AUTHINFO succeeded
792       (nntp-send-command
793        "^2.*\r?\n" "AUTHINFO PASS"
794        (or passwd
795            nntp-authinfo-password
796            (setq nntp-authinfo-password
797                      (nnmail-read-passwd (format "NNTP (%s@%s) password: "
798                                                  user nntp-address))))))))))
799
800 (defun nntp-send-nosy-authinfo ()
801   "Send the AUTHINFO to the nntp server."
802   (let ((user (read-string (format "NNTP (%s) user name: " nntp-address))))
803     (unless (member user '(nil ""))
804       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" user)
805       (when t                           ;???Should check if AUTHINFO succeeded
806         (nntp-send-command "^2.*\r?\n" "AUTHINFO PASS"
807                            (nnmail-read-passwd "NNTP (%s@%s) password: "
808                                                user nntp-address))))))
809
810 (defun nntp-send-authinfo-from-file ()
811   "Send the AUTHINFO to the nntp server.
812
813 The authinfo login name is taken from the user's login name and the
814 password contained in '~/.nntp-authinfo'."
815   (when (file-exists-p "~/.nntp-authinfo")
816     (nnheader-temp-write nil
817       (insert-file-contents "~/.nntp-authinfo")
818       (goto-char (point-min))
819       (nntp-send-command "^3.*\r?\n" "AUTHINFO USER" (user-login-name))
820       (nntp-send-command
821        "^2.*\r?\n" "AUTHINFO PASS"
822        (buffer-substring (point) (progn (end-of-line) (point)))))))
823
824 ;;; Internal functions.
825
826 (defun nntp-handle-authinfo (process)
827   "Take care of an authinfo response from the server."
828   (let ((last nntp-last-command))
829     (funcall nntp-authinfo-function)
830     ;; We have to re-send the function that was interrupted by
831     ;; the authinfo request.
832     (save-excursion
833       (set-buffer nntp-server-buffer)
834       (erase-buffer))
835     (nntp-send-string process last)))
836
837 (defun nntp-make-process-buffer (buffer)
838   "Create a new, fresh buffer usable for nntp process connections."
839   (save-excursion
840     (set-buffer
841      (generate-new-buffer
842       (format " *server %s %s %s*"
843               nntp-address nntp-port-number
844               (gnus-buffer-exists-p buffer))))
845     (buffer-disable-undo (current-buffer))
846     (set (make-local-variable 'after-change-functions) nil)
847     (set (make-local-variable 'nntp-process-wait-for) nil)
848     (set (make-local-variable 'nntp-process-callback) nil)
849     (set (make-local-variable 'nntp-process-to-buffer) nil)
850     (set (make-local-variable 'nntp-process-start-point) nil)
851     (set (make-local-variable 'nntp-process-decode) nil)
852     (current-buffer)))
853
854 (defun nntp-open-connection (buffer)
855   "Open a connection to PORT on ADDRESS delivering output to BUFFER."
856   (run-hooks 'nntp-prepare-server-hook)
857   (let* ((pbuffer (nntp-make-process-buffer buffer))
858          (timer 
859           (and nntp-connection-timeout 
860                (nnheader-run-at-time
861                 nntp-connection-timeout nil
862                 `(lambda ()
863                    (when (buffer-name ,pbuffer)
864                      (kill-buffer ,pbuffer))))))
865          (process
866           (condition-case ()
867               (funcall nntp-open-connection-function pbuffer)
868             (error nil)
869             (quit nil))))
870     (when timer 
871       (nnheader-cancel-timer timer))
872     (when (and (buffer-name pbuffer)
873                process)
874       (process-kill-without-query process)
875       (nntp-wait-for process "^.*\n" buffer nil t)
876       (if (memq (process-status process) '(open run))
877           (prog1
878               (caar (push (list process buffer nil) nntp-connection-alist))
879             (push process nntp-connection-list)
880             (save-excursion
881               (set-buffer pbuffer)
882               (nntp-read-server-type)
883               (erase-buffer)
884               (set-buffer nntp-server-buffer)
885               (let ((nnheader-callback-function nil))
886                 (run-hooks 'nntp-server-opened-hook)
887                 (nntp-send-authinfo t))))
888         (when (buffer-name (process-buffer process))
889           (kill-buffer (process-buffer process)))
890         nil))))
891
892 (defun nntp-open-network-stream (buffer)
893   (open-network-stream-as-binary
894    "nntpd" buffer nntp-address nntp-port-number))
895
896 (defun nntp-open-ssl-stream (buffer)
897   (let* ((ssl-program-arguments '("-connect" (concat host ":" service)))
898          (proc (open-ssl-stream "nntpd" buffer nntp-address nntp-port-number)))
899     (save-excursion
900       (set-buffer buffer)
901       (nntp-wait-for-string "^\r*20[01]")
902       (beginning-of-line)
903       (delete-region (point-min) (point))
904       proc)))
905
906 (defun nntp-read-server-type ()
907   "Find out what the name of the server we have connected to is."
908   ;; Wait for the status string to arrive.
909   (setq nntp-server-type (buffer-substring (point-min) (point-max)))
910   (let ((alist nntp-server-action-alist)
911         (case-fold-search t)
912         entry)
913     ;; Run server-specific commands.
914     (while alist
915       (setq entry (pop alist))
916       (when (string-match (car entry) nntp-server-type)
917         (if (and (listp (cadr entry))
918                  (not (eq 'lambda (caadr entry))))
919             (eval (cadr entry))
920           (funcall (cadr entry)))))))
921
922 (defun nntp-after-change-function-callback (beg end len)
923   (when nntp-process-callback
924     (save-match-data
925       (if (and (= beg (point-min))
926                (memq (char-after beg) '(?4 ?5)))
927           ;; Report back error messages.
928           (save-excursion
929             (goto-char beg)
930             (if (looking-at "480")
931                 (nntp-handle-authinfo nntp-process-to-buffer)
932               (nntp-snarf-error-message)
933               (funcall nntp-process-callback nil)))
934         (goto-char end)
935         (when (and (> (point) nntp-process-start-point)
936                    (re-search-backward nntp-process-wait-for
937                                        nntp-process-start-point t))
938           (when (gnus-buffer-exists-p nntp-process-to-buffer)
939             (let ((cur (current-buffer))
940                   (start nntp-process-start-point))
941               (save-excursion
942                 (set-buffer nntp-process-to-buffer)
943                 (goto-char (point-max))
944                 (let ((b (point)))
945                   (insert-buffer-substring cur start)
946                   (narrow-to-region b (point-max))
947                   (nntp-decode-text)
948                   (widen)))))
949           (goto-char end)
950           (let ((callback nntp-process-callback)
951                 (nntp-inside-change-function t))
952             (setq nntp-process-callback nil)
953             (save-excursion
954               (funcall callback (buffer-name
955                                  (get-buffer nntp-process-to-buffer))))))))))
956
957 (defun nntp-snarf-error-message ()
958   "Save the error message in the current buffer."
959   (let ((message (buffer-substring (point-min) (point-max))))
960     (while (string-match "[\r\n]+" message)
961       (setq message (replace-match " " t t message)))
962     (nnheader-report 'nntp message)
963     message))
964
965 (defun nntp-accept-process-output (process)
966   "Wait for output from PROCESS and message some dots."
967   (save-excursion
968     (set-buffer (or (nntp-find-connection-buffer nntp-server-buffer)
969                     nntp-server-buffer))
970     (let ((len (/ (point-max) 1024))
971           message-log-max)
972       (unless (< len 10)
973         (setq nntp-have-messaged t)
974         (nnheader-message 7 "nntp read: %dk" len)))
975     (accept-process-output process 1)))
976
977 (defun nntp-accept-response ()
978   "Wait for output from the process that outputs to BUFFER."
979   (nntp-accept-process-output (nntp-find-connection nntp-server-buffer)))
980
981 (defun nntp-possibly-change-group (group server &optional connectionless)
982   (let ((nnheader-callback-function nil))
983     (when server
984       (or (nntp-server-opened server)
985           (nntp-open-server server nil connectionless)))
986
987     (unless connectionless
988       (or (nntp-find-connection nntp-server-buffer)
989           (nntp-open-connection nntp-server-buffer))))
990
991   (when group
992     (let ((entry (nntp-find-connection-entry nntp-server-buffer)))
993       (when (not (equal group (caddr entry)))
994         (save-excursion
995           (set-buffer (process-buffer (car entry)))
996           (erase-buffer)
997           (nntp-send-string (car entry) (concat "GROUP " group))
998           ;; allow for unexpected responses, since this can be called
999           ;; from a timer with quit inhibited
1000           (nntp-wait-for-string "^[245].*\n")
1001           (setcar (cddr entry) group)
1002           (erase-buffer))))))
1003
1004 (defun nntp-decode-text (&optional cr-only)
1005   "Decode the text in the current buffer."
1006   (goto-char (point-min))
1007   (while (search-forward "\r" nil t)
1008     (delete-char -1))
1009   (unless cr-only
1010     ;; Remove trailing ".\n" end-of-transfer marker.
1011     (goto-char (point-max))
1012     (forward-line -1)
1013     (when (looking-at ".\n")
1014       (delete-char 2))
1015     ;; Delete status line.
1016     (goto-char (point-min))
1017     (delete-region (point) (progn (forward-line 1) (point)))
1018     ;; Remove "." -> ".." encoding.
1019     (while (search-forward "\n.." nil t)
1020       (delete-char -1))))
1021
1022 (defun nntp-encode-text ()
1023   "Encode the text in the current buffer."
1024   (save-excursion
1025     ;; Replace "." at beginning of line with "..".
1026     (goto-char (point-min))
1027     (while (re-search-forward "^\\." nil t)
1028       (insert "."))
1029     (goto-char (point-max))
1030     ;; Insert newline at the end of the buffer.
1031     (unless (bolp)
1032       (insert "\n"))
1033     ;; Insert `.' at end of buffer (end of text mark).
1034     (goto-char (point-max))
1035     (insert ".\n")
1036     (goto-char (point-min))
1037     (while (not (eobp))
1038       (end-of-line)
1039       (delete-char 1)
1040       (insert nntp-end-of-line))))
1041
1042 (defun nntp-retrieve-headers-with-xover (articles &optional fetch-old)
1043   (set-buffer nntp-server-buffer)
1044   (erase-buffer)
1045   (cond
1046
1047    ;; This server does not talk NOV.
1048    ((not nntp-server-xover)
1049     nil)
1050
1051    ;; We don't care about gaps.
1052    ((or (not nntp-nov-gap)
1053         fetch-old)
1054     (nntp-send-xover-command
1055      (if fetch-old
1056          (if (numberp fetch-old)
1057              (max 1 (- (car articles) fetch-old))
1058            1)
1059        (car articles))
1060      (car (last articles)) 'wait)
1061
1062     (goto-char (point-min))
1063     (when (looking-at "[1-5][0-9][0-9] ")
1064       (delete-region (point) (progn (forward-line 1) (point))))
1065     (while (search-forward "\r" nil t)
1066       (replace-match "" t t))
1067     (goto-char (point-max))
1068     (forward-line -1)
1069     (when (looking-at "\\.")
1070       (delete-region (point) (progn (forward-line 1) (point)))))
1071
1072    ;; We do it the hard way.  For each gap, an XOVER command is sent
1073    ;; to the server.  We do not wait for a reply from the server, we
1074    ;; just send them off as fast as we can.  That means that we have
1075    ;; to count the number of responses we get back to find out when we
1076    ;; have gotten all we asked for.
1077    ((numberp nntp-nov-gap)
1078     (let ((count 0)
1079           (received 0)
1080           (last-point (point-min))
1081           (buf nntp-server-buffer)
1082           ;;(process-buffer (nntp-find-connection (current-buffer))))
1083           first)
1084       ;; We have to check `nntp-server-xover'.  If it gets set to nil,
1085       ;; that means that the server does not understand XOVER, but we
1086       ;; won't know that until we try.
1087       (while (and nntp-server-xover articles)
1088         (setq first (car articles))
1089         ;; Search forward until we find a gap, or until we run out of
1090         ;; articles.
1091         (while (and (cdr articles)
1092                     (< (- (nth 1 articles) (car articles)) nntp-nov-gap))
1093           (setq articles (cdr articles)))
1094
1095         (when (nntp-send-xover-command first (car articles))
1096           (setq articles (cdr articles)
1097                 count (1+ count))
1098
1099           ;; Every 400 requests we have to read the stream in
1100           ;; order to avoid deadlocks.
1101           (when (or (null articles)     ;All requests have been sent.
1102                     (zerop (% count nntp-maximum-request)))
1103             (accept-process-output)
1104             ;; On some Emacs versions the preceding function has
1105             ;; a tendency to change the buffer.  Perhaps.  It's
1106             ;; quite difficult to reproduce, because it only
1107             ;; seems to happen once in a blue moon.
1108             (set-buffer buf)
1109             (while (progn
1110                      (goto-char last-point)
1111                      ;; Count replies.
1112                      (while (re-search-forward "^[0-9][0-9][0-9] " nil t)
1113                        (setq received (1+ received)))
1114                      (setq last-point (point))
1115                      (< received count))
1116               (accept-process-output)
1117               (set-buffer buf)))))
1118
1119       (when nntp-server-xover
1120         ;; Wait for the reply from the final command.
1121         (goto-char (point-max))
1122         (re-search-backward "^[0-9][0-9][0-9] " nil t)
1123         (when (looking-at "^[23]")
1124           (while (progn
1125                    (goto-char (point-max))
1126                    (forward-line -1)
1127                    (not (looking-at "^\\.\r?\n")))
1128             (nntp-accept-response)))
1129
1130         ;; We remove any "." lines and status lines.
1131         (goto-char (point-min))
1132         (while (search-forward "\r" nil t)
1133           (delete-char -1))
1134         (goto-char (point-min))
1135         (delete-matching-lines "^\\.$\\|^[1-5][0-9][0-9] ")
1136         ;;(copy-to-buffer nntp-server-buffer (point-min) (point-max))
1137         t))))
1138
1139   nntp-server-xover)
1140
1141 (defun nntp-send-xover-command (beg end &optional wait-for-reply)
1142   "Send the XOVER command to the server."
1143   (let ((range (format "%d-%d" beg end))
1144         (nntp-inhibit-erase t))
1145     (if (stringp nntp-server-xover)
1146         ;; If `nntp-server-xover' is a string, then we just send this
1147         ;; command.
1148         (if wait-for-reply
1149             (nntp-send-command-nodelete
1150              "\r?\n\\.\r?\n" nntp-server-xover range)
1151           ;; We do not wait for the reply.
1152           (nntp-send-command-nodelete "\r?\n\\.\r?\n" nntp-server-xover range))
1153       (let ((commands nntp-xover-commands))
1154         ;; `nntp-xover-commands' is a list of possible XOVER commands.
1155         ;; We try them all until we get at positive response.
1156         (while (and commands (eq nntp-server-xover 'try))
1157           (nntp-send-command-nodelete "\r?\n\\.\r?\n" (car commands) range)
1158           (save-excursion
1159             (set-buffer nntp-server-buffer)
1160             (goto-char (point-min))
1161             (and (looking-at "[23]")    ; No error message.
1162                  ;; We also have to look at the lines.  Some buggy
1163                  ;; servers give back simple lines with just the
1164                  ;; article number.  How... helpful.
1165                  (progn
1166                    (forward-line 1)
1167                    (looking-at "[0-9]+\t...")) ; More text after number.
1168                  (setq nntp-server-xover (car commands))))
1169           (setq commands (cdr commands)))
1170         ;; If none of the commands worked, we disable XOVER.
1171         (when (eq nntp-server-xover 'try)
1172           (save-excursion
1173             (set-buffer nntp-server-buffer)
1174             (erase-buffer)
1175             (setq nntp-server-xover nil)))
1176         nntp-server-xover))))
1177
1178 ;;; Alternative connection methods.
1179
1180 (defun nntp-wait-for-string (regexp)
1181   "Wait until string arrives in the buffer."
1182   (let ((buf (current-buffer)))
1183     (goto-char (point-min))
1184     (while (not (re-search-forward regexp nil t))
1185       (accept-process-output (nntp-find-connection nntp-server-buffer))
1186       (set-buffer buf)
1187       (goto-char (point-min)))))
1188
1189 (defun nntp-open-telnet (buffer)
1190   (save-excursion
1191     (set-buffer buffer)
1192     (erase-buffer)
1193     (let ((proc (as-binary-process
1194                  (apply
1195                   'start-process
1196                   "nntpd" buffer nntp-telnet-command nntp-telnet-switches)))
1197           (case-fold-search t))
1198       (when (memq (process-status proc) '(open run))
1199         (process-send-string proc "set escape \^X\n")
1200         (cond
1201          ((and nntp-open-telnet-envuser nntp-telnet-user-name)
1202           (process-send-string proc (concat "open " "-l" nntp-telnet-user-name
1203                                             nntp-address "\n")))
1204          (t
1205           (process-send-string proc (concat "open " nntp-address "\n"))))
1206         (cond
1207          ((not nntp-open-telnet-envuser)
1208           (nntp-wait-for-string "^\r*.?login:")
1209           (process-send-string
1210            proc (concat
1211                  (or nntp-telnet-user-name
1212                      (setq nntp-telnet-user-name (read-string "login: ")))
1213                  "\n"))))
1214         (nntp-wait-for-string "^\r*.?password:")
1215         (process-send-string
1216          proc (concat
1217                (or nntp-telnet-passwd
1218                    (setq nntp-telnet-passwd
1219                          (nnmail-read-passwd "Password: ")))
1220                "\n"))
1221         (erase-buffer)
1222         (nntp-wait-for-string nntp-telnet-shell-prompt)
1223         (process-send-string
1224          proc (concat (mapconcat 'identity nntp-telnet-parameters " ") "\n"))
1225         (nntp-wait-for-string "^\r*20[01]")
1226         (beginning-of-line)
1227         (delete-region (point-min) (point))
1228         (process-send-string proc "\^]")
1229         (nntp-wait-for-string "^telnet")
1230         (process-send-string proc "mode character\n")
1231         (accept-process-output proc 1)
1232         (sit-for 1)
1233         (goto-char (point-min))
1234         (forward-line 1)
1235         (delete-region (point) (point-max)))
1236       proc)))
1237
1238 (defun nntp-open-rlogin (buffer)
1239   "Open a connection to SERVER using rsh."
1240   (let ((proc (if nntp-rlogin-user-name
1241                   (as-binary-process
1242                    (apply 'start-process
1243                           "nntpd" buffer nntp-rlogin-program
1244                           nntp-address "-l" nntp-rlogin-user-name
1245                           nntp-rlogin-parameters))
1246                 (as-binary-process
1247                  (apply 'start-process
1248                         "nntpd" buffer nntp-rlogin-program nntp-address
1249                         nntp-rlogin-parameters)))))
1250     (save-excursion
1251       (set-buffer buffer)
1252       (nntp-wait-for-string "^\r*20[01]")
1253       (beginning-of-line)
1254       (delete-region (point-min) (point))
1255       proc)))
1256
1257 (defun nntp-find-group-and-number ()
1258   (save-excursion
1259     (save-restriction
1260       (set-buffer nntp-server-buffer)
1261       (narrow-to-region (goto-char (point-min))
1262                         (or (search-forward "\n\n" nil t) (point-max)))
1263       (goto-char (point-min))
1264       ;; We first find the number by looking at the status line.
1265       (let ((number (and (looking-at "2[0-9][0-9] +\\([0-9]+\\) ")
1266                          (string-to-int
1267                           (buffer-substring (match-beginning 1)
1268                                             (match-end 1)))))
1269             group newsgroups xref)
1270         (and number (zerop number) (setq number nil))
1271         ;; Then we find the group name.
1272         (setq group
1273               (cond
1274                ;; If there is only one group in the Newsgroups header,
1275                ;; then it seems quite likely that this article comes
1276                ;; from that group, I'd say.
1277                ((and (setq newsgroups (mail-fetch-field "newsgroups"))
1278                      (not (string-match "," newsgroups)))
1279                 newsgroups)
1280                ;; If there is more than one group in the Newsgroups
1281                ;; header, then the Xref header should be filled out.
1282                ;; We hazard a guess that the group that has this
1283                ;; article number in the Xref header is the one we are
1284                ;; looking for.  This might very well be wrong if this
1285                ;; article happens to have the same number in several
1286                ;; groups, but that's life.
1287                ((and (setq xref (mail-fetch-field "xref"))
1288                      number
1289                      (string-match (format "\\([^ :]+\\):%d" number) xref))
1290                 (substring xref (match-beginning 1) (match-end 1)))
1291                (t "")))
1292         (when (string-match "\r" group)
1293           (setq group (substring group 0 (match-beginning 0))))
1294         (cons group number)))))
1295
1296 (provide 'nntp)
1297
1298 ;;; nntp.el ends here