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