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