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