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