*** empty log message ***
[bpt/emacs.git] / lisp / nntp.el
CommitLineData
05328297 1;;; NNTP (RFC977) Interface for GNU Emacs
2;; Copyright (C) 1987, 1988, 1989 Fujitsu Laboratories LTD.
3;; Copyright (C) 1987, 1988, 1989, 1990 Masanobu UMEDA
4;; $Header: nntp.el,v 3.10 90/03/23 13:25:27 umerin Locked $
5
6;; This file is part of GNU Emacs.
7
8;; GNU Emacs is distributed in the hope that it will be useful,
9;; but WITHOUT ANY WARRANTY. No author or distributor
10;; accepts responsibility to anyone for the consequences of using it
11;; or for whether it serves any particular purpose or works at all,
12;; unless he says so in writing. Refer to the GNU Emacs General Public
13;; License for full details.
14
15;; Everyone is granted permission to copy, modify and redistribute
16;; GNU Emacs, but only under the conditions described in the
17;; GNU Emacs General Public License. A copy of this license is
18;; supposed to have been given to you along with GNU Emacs so you
19;; can know your rights and responsibilities. It should be in a
20;; file named COPYING. Among other things, the copyright notice
21;; and this notice must be preserved on all copies.
22
23;; This implementation is tested on both 1.2a and 1.5 version of the
24;; NNTP package.
25
26;; Troubleshooting of NNTP
27;;
28;; (1) Select routine may signal an error or fall into infinite loop
29;; while waiting for the server response. In this case, you'd better
30;; not use byte-compiled codes but original source. If you still have
31;; a problems with it, set the variable `nntp-buggy-select' to T.
32;;
33;; (2) Emacs may hang up while retrieving headers since too many
34;; requests have been sent to the NNTP server without reading their
35;; replies. In this case, reduce the number of the requests sent to
36;; the server at one time by setting the variable
37;; `nntp-maximum-request' to a lower value.
38;;
39;; (3) If the TCP/IP stream (open-network-stream) is not supported by
40;; emacs, compile and install `tcp.el' and `tcp.c' which is an
41;; emulation program of the stream. If you modified `tcp.c' for your
42;; system, please send me the diffs. I'll include some of them in the
43;; future releases.
44
05328297 45(defvar nntp-server-hook nil
46 "*Hooks for the NNTP server.
47If the kanji code of the NNTP server is different from the local kanji
48code, the correct kanji code of the buffer associated with the NNTP
49server must be specified as follows:
50
51(setq nntp-server-hook
52 '(lambda ()
53 ;; Server's Kanji code is EUC (NEmacs hack).
54 (make-local-variable 'kanji-fileio-code)
55 (setq kanji-fileio-code 0)))
56
57If you'd like to change something depending on the server in this
58hook, use the variable `nntp-server-name'.")
59
60(defvar nntp-buggy-select (memq system-type '(usg-unix-v fujitsu-uts))
61 "*T if your select routine is buggy.
62If the select routine signals error or fall into infinite loop while
63waiting for the server response, the variable must be set to t. In
64case of Fujitsu UTS, it is set to T since `accept-process-output'
65doesn't work properly.")
66
67(defvar nntp-maximum-request 400
68 "*The maximum number of the requests sent to the NNTP server at one time.
69If Emacs hangs up while retrieving headers, set the variable to a
70lower value.")
71
72(defvar nntp-large-newsgroup 50
73 "*The number of the articles which indicates a large newsgroup.
74If the number of the articles is greater than the value, verbose
75messages will be shown to indicate the current status.")
76
77\f
78(defconst nntp-version "NNTP 3.10"
79 "Version numbers of this version of NNTP.")
80
81(defvar nntp-server-name nil
82 "The name of the host running NNTP server.")
83
84(defvar nntp-server-buffer nil
85 "Buffer associated with NNTP server process.")
86
87(defvar nntp-server-process nil
88 "The NNTP server process.
89You'd better not use this variable in NNTP front-end program but
90instead use `nntp-server-buffer'.")
91
92(defvar nntp-status-message-string nil
93 "Save the server response message.
94You'd better not use this variable in NNTP front-end program but
95instead call function `nntp-status-message' to get status message.")
96
97;;;
98;;; Extended Command for retrieving many headers.
99;;;
100;; Retrieving lots of headers by sending command asynchronously.
101;; Access functions to headers are defined as macro.
102
103(defmacro nntp-header-number (header)
104 "Return article number in HEADER."
105 (` (aref (, header) 0)))
106
107(defmacro nntp-set-header-number (header number)
108 "Set article number of HEADER to NUMBER."
109 (` (aset (, header) 0 (, number))))
110
111(defmacro nntp-header-subject (header)
112 "Return subject string in HEADER."
113 (` (aref (, header) 1)))
114
115(defmacro nntp-set-header-subject (header subject)
116 "Set article subject of HEADER to SUBJECT."
117 (` (aset (, header) 1 (, subject))))
118
119(defmacro nntp-header-from (header)
120 "Return author string in HEADER."
121 (` (aref (, header) 2)))
122
123(defmacro nntp-set-header-from (header from)
124 "Set article author of HEADER to FROM."
125 (` (aset (, header) 2 (, from))))
126
127(defmacro nntp-header-xref (header)
128 "Return xref string in HEADER."
129 (` (aref (, header) 3)))
130
131(defmacro nntp-set-header-xref (header xref)
132 "Set article xref of HEADER to xref."
133 (` (aset (, header) 3 (, xref))))
134
135(defmacro nntp-header-lines (header)
136 "Return lines in HEADER."
137 (` (aref (, header) 4)))
138
139(defmacro nntp-set-header-lines (header lines)
140 "Set article lines of HEADER to LINES."
141 (` (aset (, header) 4 (, lines))))
142
143(defmacro nntp-header-date (header)
144 "Return date in HEADER."
145 (` (aref (, header) 5)))
146
147(defmacro nntp-set-header-date (header date)
148 "Set article date of HEADER to DATE."
149 (` (aset (, header) 5 (, date))))
150
151(defmacro nntp-header-id (header)
152 "Return Id in HEADER."
153 (` (aref (, header) 6)))
154
155(defmacro nntp-set-header-id (header id)
156 "Set article Id of HEADER to ID."
157 (` (aset (, header) 6 (, id))))
158
159(defmacro nntp-header-references (header)
160 "Return references in HEADER."
161 (` (aref (, header) 7)))
162
163(defmacro nntp-set-header-references (header ref)
164 "Set article references of HEADER to REF."
165 (` (aset (, header) 7 (, ref))))
166
167(defun nntp-retrieve-headers (sequence)
168 "Return list of article headers specified by SEQUENCE of article id.
169The format of list is
170 `([NUMBER SUBJECT FROM XREF LINES DATE MESSAGE-ID REFERENCES] ...)'.
171Reader macros for the vector are defined as `nntp-header-FIELD'.
172Writer macros for the vector are defined as `nntp-set-header-FIELD'.
173News group must be selected before calling me."
174 (save-excursion
175 (set-buffer nntp-server-buffer)
176 (erase-buffer)
177 (let ((number (length sequence))
178 (last-point (point-min))
179 (received 0)
180 (count 0)
181 (headers nil) ;Result list.
182 (article 0)
183 (subject nil)
184 (message-id)
185 (from nil)
186 (xref nil)
187 (lines 0)
188 (date nil)
189 (references nil))
190 ;; Send HEAD command.
191 (while sequence
192 (nntp-send-strings-to-server "HEAD" (car sequence))
193 (setq sequence (cdr sequence))
194 (setq count (1+ count))
195 ;; Every 400 header requests we have to read stream in order
196 ;; to avoid deadlock.
197 (if (or (null sequence) ;All requests have been sent.
198 (zerop (% count nntp-maximum-request)))
199 (progn
200 (accept-process-output)
201 (while (progn
202 (goto-char last-point)
203 ;; Count replies.
204 (while (re-search-forward "^[0-9]" nil t)
205 (setq received (1+ received)))
206 (setq last-point (point))
207 (< received count))
208 ;; If number of headers is greater than 100, give
209 ;; informative messages.
210 (and (numberp nntp-large-newsgroup)
211 (> number nntp-large-newsgroup)
212 (zerop (% received 20))
213 (message "NNTP: %d%% of headers received."
214 (/ (* received 100) number)))
215 (nntp-accept-response))
216 ))
217 )
218 ;; Wait for text of last command.
219 (goto-char (point-max))
220 (re-search-backward "^[0-9]" nil t)
221 (if (looking-at "^[23]")
222 (while (progn
223 (goto-char (- (point-max) 3))
224 (not (looking-at "^\\.\r$")))
225 (nntp-accept-response)))
226 (and (numberp nntp-large-newsgroup)
227 (> number nntp-large-newsgroup)
228 (message "NNTP: 100%% of headers received."))
229 ;; Now all of replies are received.
230 (setq received number)
231 ;; First, fold continuation lines.
232 (goto-char (point-min))
233 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
234 (replace-match " " t t))
235 ;;(delete-non-matching-lines
236 ;; "^Subject:\\|^Xref:\\|^From:\\|^Lines:\\|^Date:\\|^References:\\|^[23]")
237 (and (numberp nntp-large-newsgroup)
238 (> number nntp-large-newsgroup)
239 (message "NNTP: Parsing headers..."))
240 ;; Then examines replies.
241 (goto-char (point-min))
242 (while (not (eobp))
243 (cond ((looking-at "^[23][0-9][0-9][ \t]+\\([0-9]+\\)[ \t]+\\(<[^>]+>\\)")
244 (setq article
245 (string-to-int
246 (buffer-substring (match-beginning 1) (match-end 1))))
247 (setq message-id
248 (buffer-substring (match-beginning 2) (match-end 2)))
249 (forward-line 1)
250 ;; Set default value.
251 (setq subject nil)
252 (setq xref nil)
253 (setq from nil)
254 (setq lines 0)
255 (setq date nil)
256 (setq references nil)
257 ;; Thanks go to mly@AI.MIT.EDU (Richard Mlynarik)
258 (while (and (not (eobp))
259 (not (memq (following-char) '(?2 ?3))))
260 (if (looking-at "\\(From\\|Subject\\|Date\\|Lines\\|Xref\\|References\\):[ \t]+\\([^ \t\n]+.*\\)\r$")
261 (let ((s (buffer-substring
262 (match-beginning 2) (match-end 2)))
263 (c (char-after (match-beginning 0))))
264 ;; We don't have to worry about letter case.
265 (cond ((char-equal c ?F) ;From:
266 (setq from s))
267 ((char-equal c ?S) ;Subject:
268 (setq subject s))
269 ((char-equal c ?D) ;Date:
270 (setq date s))
271 ((char-equal c ?L) ;Lines:
272 (setq lines (string-to-int s)))
273 ((char-equal c ?X) ;Xref:
274 (setq xref s))
275 ((char-equal c ?R) ;References:
276 (setq references s))
277 )))
278 (forward-line 1))
279 ;; Finished to parse one header.
280 (if (null subject)
281 (setq subject "(None)"))
282 (if (null from)
283 (setq from "(Unknown User)"))
284 (setq headers
285 (cons (vector article subject from
286 xref lines date
287 message-id references) headers))
288 )
289 (t (forward-line 1))
290 )
291 (setq received (1- received))
292 (and (numberp nntp-large-newsgroup)
293 (> number nntp-large-newsgroup)
294 (zerop (% received 20))
295 (message "NNTP: Parsing headers... %d%%"
296 (/ (* received 100) number)))
297 )
298 (and (numberp nntp-large-newsgroup)
299 (> number nntp-large-newsgroup)
300 (message "NNTP: Parsing headers... done"))
301 (nreverse headers)
302 )))
303
304\f
305;;;
306;;; Raw Interface to Network News Transfer Protocol (RFC977).
307;;;
308
309(defun nntp-open-server (host &optional service)
310 "Open news server on HOST.
311If HOST is nil, use value of environment variable `NNTPSERVER'.
312If optional argument SERVICE is non-nil, open by the service name."
313 (let ((host (or host (getenv "NNTPSERVER")))
314 (status nil))
315 (setq nntp-status-message-string "")
316 (cond ((and host (nntp-open-server-internal host service))
317 (setq status (nntp-wait-for-response "^[23].*\r$"))
318 ;; Do check unexpected close of connection.
319 ;; Suggested by feldmark@hanako.stars.flab.fujitsu.junet.
320 (if status
321 (set-process-sentinel nntp-server-process
322 'nntp-default-sentinel)
323 ;; We have to close connection here, since function
324 ;; `nntp-server-opened' may return incorrect status.
325 (nntp-close-server-internal)
326 ))
327 ((null host)
328 (setq nntp-status-message-string "NNTP server is not specified."))
329 )
330 status
331 ))
332
333(defun nntp-close-server ()
334 "Close news server."
335 (unwind-protect
336 (progn
337 ;; Un-set default sentinel function before closing connection.
338 (and nntp-server-process
339 (eq 'nntp-default-sentinel
340 (process-sentinel nntp-server-process))
341 (set-process-sentinel nntp-server-process nil))
342 ;; We cannot send QUIT command unless the process is running.
343 (if (nntp-server-opened)
344 (nntp-send-command nil "QUIT"))
345 )
346 (nntp-close-server-internal)
347 ))
348
349(fset 'nntp-request-quit (symbol-function 'nntp-close-server))
350
351(defun nntp-server-opened ()
352 "Return server process status, T or NIL.
353If the stream is opened, return T, otherwise return NIL."
354 (and nntp-server-process
355 (memq (process-status nntp-server-process) '(open run))))
356
357(defun nntp-status-message ()
358 "Return server status response as string."
359 (if (and nntp-status-message-string
360 ;; NNN MESSAGE
361 (string-match "[0-9][0-9][0-9][ \t]+\\([^\r]*\\).*$"
362 nntp-status-message-string))
363 (substring nntp-status-message-string (match-beginning 1) (match-end 1))
364 ;; Empty message if nothing.
365 ""
366 ))
367
368(defun nntp-request-article (id)
369 "Select article by message ID (or number)."
370 (prog1
371 ;; If NEmacs, end of message may look like: "\256\215" (".^M")
372 (nntp-send-command "^\\.\r$" "ARTICLE" id)
373 (nntp-decode-text)
374 ))
375
376(defun nntp-request-body (id)
377 "Select article body by message ID (or number)."
378 (prog1
379 ;; If NEmacs, end of message may look like: "\256\215" (".^M")
380 (nntp-send-command "^\\.\r$" "BODY" id)
381 (nntp-decode-text)
382 ))
383
384(defun nntp-request-head (id)
385 "Select article head by message ID (or number)."
386 (prog1
387 (nntp-send-command "^\\.\r$" "HEAD" id)
388 (nntp-decode-text)
389 ))
390
391(defun nntp-request-stat (id)
392 "Select article by message ID (or number)."
393 (nntp-send-command "^[23].*\r$" "STAT" id))
394
395(defun nntp-request-group (group)
396 "Select news GROUP."
397 ;; 1.2a NNTP's group command is buggy. "^M" (\r) is not appended to
398 ;; end of the status message.
399 (nntp-send-command "^[23].*$" "GROUP" group))
400
401(defun nntp-request-list ()
402 "List valid newsgoups."
403 (prog1
404 (nntp-send-command "^\\.\r$" "LIST")
405 (nntp-decode-text)
406 ))
407
408(defun nntp-request-last ()
a0963566 409 "Set current article pointer to the previous article in the current news group."
05328297 410 (nntp-send-command "^[23].*\r$" "LAST"))
411
412(defun nntp-request-next ()
413 "Advance current article pointer."
414 (nntp-send-command "^[23].*\r$" "NEXT"))
415
416(defun nntp-request-post ()
417 "Post a new news in current buffer."
418 (if (nntp-send-command "^[23].*\r$" "POST")
419 (progn
420 (nntp-encode-text)
421 (nntp-send-region-to-server (point-min) (point-max))
422 ;; 1.2a NNTP's post command is buggy. "^M" (\r) is not
423 ;; appended to end of the status message.
424 (nntp-wait-for-response "^[23].*$")
425 )))
426
427(defun nntp-default-sentinel (proc status)
428 "Default sentinel function for NNTP server process."
429 (if (and nntp-server-process
430 (not (nntp-server-opened)))
431 (error "NNTP: Connection closed.")
432 ))
433
434;; Encoding and decoding of NNTP text.
435
436(defun nntp-decode-text ()
437 "Decode text transmitted by NNTP.
4380. Delete status line.
4391. Delete `^M' at end of line.
4402. Delete `.' at end of buffer (end of text mark).
4413. Delete `.' at beginning of line."
442 (save-excursion
443 (set-buffer nntp-server-buffer)
444 ;; Insert newline at end of buffer.
445 (goto-char (point-max))
446 (if (not (bolp))
447 (insert "\n"))
448 ;; Delete status line.
449 (goto-char (point-min))
450 (delete-region (point) (progn (forward-line 1) (point)))
451 ;; Delete `^M' at end of line.
452 ;; (replace-regexp "\r$" "")
453 (while (not (eobp))
454 (end-of-line)
455 (if (= (preceding-char) ?\r)
456 (delete-char -1))
457 (forward-line 1)
458 )
459 ;; Delete `.' at end of buffer (end of text mark).
460 (goto-char (point-max))
461 (forward-line -1) ;(beginning-of-line)
462 (if (looking-at "^\\.$")
463 (delete-region (point) (progn (forward-line 1) (point))))
464 ;; Replace `..' at beginning of line with `.'.
465 (goto-char (point-min))
466 ;; (replace-regexp "^\\.\\." ".")
467 (while (search-forward "\n.." nil t)
468 (delete-char -1))
469 ))
470
471(defun nntp-encode-text ()
472 "Encode text in current buffer for NNTP transmission.
4731. Insert `.' at beginning of line.
4742. Insert `.' at end of buffer (end of text mark)."
475 (save-excursion
476 ;; Insert newline at end of buffer.
477 (goto-char (point-max))
478 (if (not (bolp))
479 (insert "\n"))
480 ;; Replace `.' at beginning of line with `..'.
481 (goto-char (point-min))
482 ;; (replace-regexp "^\\." "..")
483 (while (search-forward "\n." nil t)
484 (insert "."))
485 ;; Insert `.' at end of buffer (end of text mark).
486 (goto-char (point-max))
487 (insert ".\n")
488 ))
489
490\f
491;;;
492;;; Synchronous Communication with NNTP Server.
493;;;
494
495(defun nntp-send-command (response cmd &rest args)
496 "Wait for server RESPONSE after sending CMD and optional ARGS to server."
497 (save-excursion
498 ;; Clear communication buffer.
499 (set-buffer nntp-server-buffer)
500 (erase-buffer)
501 (apply 'nntp-send-strings-to-server cmd args)
502 (if response
503 (nntp-wait-for-response response)
504 t)
505 ))
506
507(defun nntp-wait-for-response (regexp)
508 "Wait for server response which matches REGEXP."
509 (save-excursion
510 (let ((status t)
511 (wait t))
512 (set-buffer nntp-server-buffer)
513 ;; Wait for status response (RFC977).
514 ;; 1xx - Informative message.
515 ;; 2xx - Command ok.
516 ;; 3xx - Command ok so far, send the rest of it.
517 ;; 4xx - Command was correct, but couldn't be performed for some
518 ;; reason.
519 ;; 5xx - Command unimplemented, or incorrect, or a serious
520 ;; program error occurred.
521 (nntp-accept-response)
522 (while wait
523 (goto-char (point-min))
524 (cond ((looking-at "[23]")
525 (setq wait nil))
526 ((looking-at "[45]")
527 (setq status nil)
528 (setq wait nil))
529 (t (nntp-accept-response))
530 ))
531 ;; Save status message.
532 (end-of-line)
533 (setq nntp-status-message-string
534 (buffer-substring (point-min) (point)))
535 (if status
536 (progn
537 (setq wait t)
538 (while wait
539 (goto-char (point-max))
540 (forward-line -1) ;(beginning-of-line)
541 ;;(message (buffer-substring
542 ;; (point)
543 ;; (save-excursion (end-of-line) (point))))
544 (if (looking-at regexp)
545 (setq wait nil)
546 (message "NNTP: Reading...")
547 (nntp-accept-response)
548 (message "")
549 ))
550 ;; Successfully received server response.
551 t
552 ))
553 )))
554
555\f
556;;;
557;;; Low-Level Interface to NNTP Server.
558;;;
559
560(defun nntp-send-strings-to-server (&rest strings)
561 "Send list of STRINGS to news server as command and its arguments."
562 (let ((cmd (car strings))
563 (strings (cdr strings)))
564 ;; Command and each argument must be separeted by one or more spaces.
565 (while strings
566 (setq cmd (concat cmd " " (car strings)))
567 (setq strings (cdr strings)))
568 ;; Command line must be terminated by a CR-LF.
569 (process-send-string nntp-server-process (concat cmd "\n"))
570 ))
571
572(defun nntp-send-region-to-server (begin end)
573 "Send current buffer region (from BEGIN to END) to news server."
574 (save-excursion
575 ;; We have to work in the buffer associated with NNTP server
576 ;; process because of NEmacs hack.
577 (copy-to-buffer nntp-server-buffer begin end)
578 (set-buffer nntp-server-buffer)
579 (setq begin (point-min))
580 (setq end (point-max))
581 ;; `process-send-region' does not work if text to be sent is very
582 ;; large. I don't know maximum size of text sent correctly.
583 (let ((last nil)
584 (size 100)) ;Size of text sent at once.
585 (save-restriction
586 (narrow-to-region begin end)
587 (goto-char begin)
588 (while (not (eobp))
589 ;;(setq last (min end (+ (point) size)))
590 ;; NEmacs gets confused if character at `last' is Kanji.
591 (setq last (save-excursion
592 (goto-char (min end (+ (point) size)))
593 (or (eobp) (forward-char 1)) ;Adjust point
594 (point)))
595 (process-send-region nntp-server-process (point) last)
596 ;; I don't know whether the next codes solve the known
597 ;; problem of communication error of GNU Emacs.
598 (accept-process-output)
599 ;;(sit-for 0)
600 (goto-char last)
601 )))
602 ;; We cannot erase buffer, because reply may be received.
603 (delete-region begin end)
604 ))
605
606(defun nntp-open-server-internal (host &optional service)
607 "Open connection to news server on HOST by SERVICE (default is nntp)."
608 (save-excursion
609 ;; Use TCP/IP stream emulation package if needed.
610 (or (fboundp 'open-network-stream)
611 (require 'tcp))
612 ;; Initialize communication buffer.
613 (setq nntp-server-buffer (get-buffer-create " *nntpd*"))
614 (set-buffer nntp-server-buffer)
615 (buffer-flush-undo (current-buffer))
616 (erase-buffer)
617 (kill-all-local-variables)
618 (setq case-fold-search t) ;Should ignore case.
619 (setq nntp-server-process
620 (open-network-stream "nntpd" (current-buffer)
621 host (or service "nntp")))
622 (setq nntp-server-name host)
623 ;; It is possible to change kanji-fileio-code in this hook.
624 (run-hooks 'nntp-server-hook)
625 ;; Return the server process.
626 nntp-server-process
627 ))
628
629(defun nntp-close-server-internal ()
630 "Close connection to news server."
631 (if nntp-server-process
632 (delete-process nntp-server-process))
633 (if nntp-server-buffer
634 (kill-buffer nntp-server-buffer))
635 (setq nntp-server-buffer nil)
636 (setq nntp-server-process nil))
637
638(defun nntp-accept-response ()
639 "Read response of server.
640It is well-known that the communication speed will be much improved by
641defining this function as macro."
642 ;; To deal with server process exiting before
643 ;; accept-process-output is called.
644 ;; Suggested by Jason Venner <jason@violet.berkeley.edu>.
645 ;; This is a copy of `nntp-default-sentinel'.
646 (or (memq (process-status nntp-server-process) '(open run))
647 (error "NNTP: Connection closed."))
648 (if nntp-buggy-select
649 (progn
650 ;; We cannot use `accept-process-output'.
651 ;; Fujitsu UTS requires messages during sleep-for. I don't know why.
652 (message "NNTP: Reading...")
653 (sleep-for 1)
654 (message ""))
655 (condition-case errorcode
656 (accept-process-output nntp-server-process)
657 (error
658 (cond ((string-equal "select error: Invalid argument" (nth 1 errorcode))
659 ;; Ignore select error.
660 nil
661 )
662 (t
663 (signal (car errorcode) (cdr errorcode))))
664 ))
665 ))
49116ac0
JB
666
667(provide 'nntp)
668