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