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