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