87
|
1 ;;; Spool access using NNTP for GNU Emacs
|
|
2 ;; Copyright (C) 1988, 1989 Fujitsu Laboratories LTD.
|
|
3 ;; Copyright (C) 1988, 1989, 1990 Masanobu UMEDA
|
|
4 ;; $Header: nnspool.el,v 1.10 90/03/23 13:25:25 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 (provide 'nnspool)
|
|
24 (require 'nntp)
|
|
25
|
|
26 (defvar nnspool-inews-program news-inews-program
|
|
27 "*Program to post news.")
|
|
28
|
|
29 (defvar nnspool-inews-switches '("-h")
|
|
30 "*Switches for nnspool-request-post to pass to `inews' for posting news.")
|
|
31
|
|
32 (defvar nnspool-spool-directory news-path
|
|
33 "*Local news spool directory.")
|
|
34
|
|
35 (defvar nnspool-active-file "/usr/lib/news/active"
|
|
36 "*Local news active file.")
|
|
37
|
|
38 (defvar nnspool-history-file "/usr/lib/news/history"
|
|
39 "*Local news history file.")
|
|
40
|
|
41
|
|
42
|
|
43 (defconst nnspool-version "NNSPOOL 1.10"
|
|
44 "Version numbers of this version of NNSPOOL.")
|
|
45
|
|
46 (defvar nnspool-current-directory nil
|
|
47 "Current news group directory.")
|
|
48
|
|
49 ;;;
|
|
50 ;;; Replacement of Extended Command for retrieving many headers.
|
|
51 ;;;
|
|
52
|
|
53 (defun nnspool-retrieve-headers (sequence)
|
|
54 "Return list of article headers specified by SEQUENCE of article id.
|
|
55 The format of list is
|
|
56 `([NUMBER SUBJECT FROM XREF LINES DATE MESSAGE-ID REFERENCES] ...)'.
|
|
57 Reader macros for the vector are defined as `nntp-header-FIELD'.
|
|
58 Writer macros for the vector are defined as `nntp-set-header-FIELD'.
|
|
59 News group must be selected before calling me."
|
|
60 (save-excursion
|
|
61 (set-buffer nntp-server-buffer)
|
|
62 ;;(erase-buffer)
|
|
63 (let ((file nil)
|
|
64 (number (length sequence))
|
|
65 (count 0)
|
|
66 (headers nil) ;Result list.
|
|
67 (article 0)
|
|
68 (subject nil)
|
|
69 (message-id nil)
|
|
70 (from nil)
|
|
71 (xref nil)
|
|
72 (lines 0)
|
|
73 (date nil)
|
|
74 (references nil))
|
|
75 (while sequence
|
|
76 ;;(nntp-send-strings-to-server "HEAD" (car sequence))
|
|
77 (setq article (car sequence))
|
|
78 (setq file
|
|
79 (concat nnspool-current-directory (prin1-to-string article)))
|
|
80 (if (and (file-exists-p file)
|
|
81 (not (file-directory-p file)))
|
|
82 (progn
|
|
83 (erase-buffer)
|
|
84 (insert-file-contents file)
|
|
85 ;; Make message body invisible.
|
|
86 (goto-char (point-min))
|
|
87 (search-forward "\n\n" nil 'move)
|
|
88 (narrow-to-region (point-min) (point))
|
|
89 ;; Fold continuation lines.
|
|
90 (goto-char (point-min))
|
|
91 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
|
|
92 (replace-match " " t t))
|
|
93 ;; Make it possible to search for `\nFIELD'.
|
|
94 (goto-char (point-min))
|
|
95 (insert "\n")
|
|
96 ;; Extract From:
|
|
97 (goto-char (point-min))
|
|
98 (if (search-forward "\nFrom: " nil t)
|
|
99 (setq from (buffer-substring
|
|
100 (point)
|
|
101 (save-excursion (end-of-line) (point))))
|
|
102 (setq from "(Unknown User)"))
|
|
103 ;; Extract Subject:
|
|
104 (goto-char (point-min))
|
|
105 (if (search-forward "\nSubject: " nil t)
|
|
106 (setq subject (buffer-substring
|
|
107 (point)
|
|
108 (save-excursion (end-of-line) (point))))
|
|
109 (setq subject "(None)"))
|
|
110 ;; Extract Message-ID:
|
|
111 (goto-char (point-min))
|
|
112 (if (search-forward "\nMessage-ID: " nil t)
|
|
113 (setq message-id (buffer-substring
|
|
114 (point)
|
|
115 (save-excursion (end-of-line) (point))))
|
|
116 (setq message-id nil))
|
|
117 ;; Extract Date:
|
|
118 (goto-char (point-min))
|
|
119 (if (search-forward "\nDate: " nil t)
|
|
120 (setq date (buffer-substring
|
|
121 (point)
|
|
122 (save-excursion (end-of-line) (point))))
|
|
123 (setq date nil))
|
|
124 ;; Extract Lines:
|
|
125 (goto-char (point-min))
|
|
126 (if (search-forward "\nLines: " nil t)
|
|
127 (setq lines (string-to-int
|
|
128 (buffer-substring
|
|
129 (point)
|
|
130 (save-excursion (end-of-line) (point)))))
|
|
131 (setq lines 0))
|
|
132 ;; Extract Xref:
|
|
133 (goto-char (point-min))
|
|
134 (if (search-forward "\nXref: " nil t)
|
|
135 (setq xref (buffer-substring
|
|
136 (point)
|
|
137 (save-excursion (end-of-line) (point))))
|
|
138 (setq xref nil))
|
|
139 ;; Extract References:
|
|
140 (goto-char (point-min))
|
|
141 (if (search-forward "\nReferences: " nil t)
|
|
142 (setq references (buffer-substring
|
|
143 (point)
|
|
144 (save-excursion (end-of-line) (point))))
|
|
145 (setq references nil))
|
|
146 (setq headers
|
|
147 (cons (vector article subject from
|
|
148 xref lines date
|
|
149 message-id references) headers))
|
|
150 ))
|
|
151 (setq sequence (cdr sequence))
|
|
152 (setq count (1+ count))
|
|
153 (and (numberp nntp-large-newsgroup)
|
|
154 (> number nntp-large-newsgroup)
|
|
155 (zerop (% count 20))
|
|
156 (message "NNSPOOL: %d%% of headers received."
|
|
157 (/ (* count 100) number)))
|
|
158 )
|
|
159 (and (numberp nntp-large-newsgroup)
|
|
160 (> number nntp-large-newsgroup)
|
|
161 (message "NNSPOOL: 100%% of headers received."))
|
|
162 (nreverse headers)
|
|
163 )))
|
|
164
|
|
165
|
|
166 ;;;
|
|
167 ;;; Replacement of NNTP Raw Interface.
|
|
168 ;;;
|
|
169
|
|
170 (defun nnspool-open-server (host &optional service)
|
|
171 "Open news server on HOST.
|
|
172 If HOST is nil, use value of environment variable `NNTPSERVER'.
|
|
173 If optional argument SERVICE is non-nil, open by the service name."
|
|
174 (let ((host (or host (getenv "NNTPSERVER")))
|
|
175 (status nil))
|
|
176 (setq nntp-status-message-string "")
|
|
177 (cond ((and (file-directory-p nnspool-spool-directory)
|
|
178 (file-exists-p nnspool-active-file)
|
|
179 (string-equal host (system-name)))
|
|
180 (setq status (nnspool-open-server-internal host service)))
|
|
181 ((string-equal host (system-name))
|
|
182 (setq nntp-status-message-string
|
|
183 (format "%s has no news spool. Goodbye." host)))
|
|
184 ((null host)
|
|
185 (setq nntp-status-message-string "NNTP server is not specified."))
|
|
186 (t
|
|
187 (setq nntp-status-message-string
|
|
188 (format "NNSPOOL: cannot talk to %s." host)))
|
|
189 )
|
|
190 status
|
|
191 ))
|
|
192
|
|
193 (defun nnspool-close-server ()
|
|
194 "Close news server."
|
|
195 (nnspool-close-server-internal))
|
|
196
|
|
197 (fset 'nnspool-request-quit (symbol-function 'nnspool-close-server))
|
|
198
|
|
199 (defun nnspool-server-opened ()
|
|
200 "Return server process status, T or NIL.
|
|
201 If the stream is opened, return T, otherwise return NIL."
|
|
202 (and nntp-server-buffer
|
|
203 (get-buffer nntp-server-buffer)))
|
|
204
|
|
205 (defun nnspool-status-message ()
|
|
206 "Return server status response as string."
|
|
207 nntp-status-message-string
|
|
208 )
|
|
209
|
|
210 (defun nnspool-request-article (id)
|
|
211 "Select article by message ID (or number)."
|
|
212 (let ((file (if (stringp id)
|
|
213 (nnspool-find-article-by-message-id id)
|
|
214 (concat nnspool-current-directory (prin1-to-string id)))))
|
|
215 (if (and (stringp file)
|
|
216 (file-exists-p file)
|
|
217 (not (file-directory-p file)))
|
|
218 (save-excursion
|
|
219 (nnspool-find-file file)))
|
|
220 ))
|
|
221
|
|
222 (defun nnspool-request-body (id)
|
|
223 "Select article body by message ID (or number)."
|
|
224 (if (nnspool-request-article id)
|
|
225 (save-excursion
|
|
226 (set-buffer nntp-server-buffer)
|
|
227 (goto-char (point-min))
|
|
228 (if (search-forward "\n\n" nil t)
|
|
229 (delete-region (point-min) (point)))
|
|
230 t
|
|
231 )
|
|
232 ))
|
|
233
|
|
234 (defun nnspool-request-head (id)
|
|
235 "Select article head by message ID (or number)."
|
|
236 (if (nnspool-request-article id)
|
|
237 (save-excursion
|
|
238 (set-buffer nntp-server-buffer)
|
|
239 (goto-char (point-min))
|
|
240 (if (search-forward "\n\n" nil t)
|
|
241 (delete-region (1- (point)) (point-max)))
|
|
242 t
|
|
243 )
|
|
244 ))
|
|
245
|
|
246 (defun nnspool-request-stat (id)
|
|
247 "Select article by message ID (or number)."
|
|
248 (error "NNSPOOL: STAT is not implemented."))
|
|
249
|
|
250 (defun nnspool-request-group (group)
|
|
251 "Select news GROUP."
|
|
252 (let ((pathname (nnspool-article-pathname
|
|
253 (nnspool-replace-chars-in-string group ?. ?/))))
|
|
254 (if (file-directory-p pathname)
|
|
255 (setq nnspool-current-directory pathname))
|
|
256 ))
|
|
257
|
|
258 (defun nnspool-request-list ()
|
|
259 "List valid newsgoups."
|
|
260 (save-excursion
|
|
261 (nnspool-find-file nnspool-active-file)))
|
|
262
|
|
263 (defun nnspool-request-last ()
|
|
264 "Set current article pointer to the previous article
|
|
265 in the current news group."
|
|
266 (error "NNSPOOL: LAST is not implemented."))
|
|
267
|
|
268 (defun nnspool-request-next ()
|
|
269 "Advance current article pointer."
|
|
270 (error "NNSPOOL: NEXT is not implemented."))
|
|
271
|
|
272 (defun nnspool-request-post ()
|
|
273 "Post a new news in current buffer."
|
|
274 (save-excursion
|
|
275 ;; We have to work in the server buffer because of NEmacs hack.
|
|
276 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
|
|
277 (set-buffer nntp-server-buffer)
|
|
278 (apply 'call-process-region
|
|
279 (point-min) (point-max)
|
|
280 nnspool-inews-program 'delete t nil nnspool-inews-switches)
|
|
281 (prog1
|
|
282 (or (zerop (buffer-size))
|
|
283 ;; If inews returns strings, it must be error message
|
|
284 ;; unless SPOOLNEWS is defined.
|
|
285 ;; This condition is very weak, but there is no good rule
|
|
286 ;; identifying errors when SPOOLNEWS is defined.
|
|
287 ;; Suggested by ohm@kaba.junet.
|
|
288 (string-match "spooled" (buffer-string)))
|
|
289 ;; Make status message by unfolding lines.
|
|
290 (subst-char-in-region (point-min) (point-max) ?\n ?\\ 'noundo)
|
|
291 (setq nntp-status-message-string (buffer-string))
|
|
292 (erase-buffer))
|
|
293 ))
|
|
294
|
|
295
|
|
296 ;;;
|
|
297 ;;; Replacement of Low-Level Interface to NNTP Server.
|
|
298 ;;;
|
|
299
|
|
300 (defun nnspool-open-server-internal (host &optional service)
|
|
301 "Open connection to news server on HOST by SERVICE (default is nntp)."
|
|
302 (save-excursion
|
|
303 (if (not (string-equal host (system-name)))
|
|
304 (error "NNSPOOL: cannot talk to %s." host))
|
|
305 ;; Initialize communication buffer.
|
|
306 (setq nntp-server-buffer (get-buffer-create " *nntpd*"))
|
|
307 (set-buffer nntp-server-buffer)
|
|
308 (buffer-flush-undo (current-buffer))
|
|
309 (erase-buffer)
|
|
310 (kill-all-local-variables)
|
|
311 (setq case-fold-search t) ;Should ignore case.
|
|
312 (setq nntp-server-process nil)
|
|
313 (setq nntp-server-name host)
|
|
314 ;; It is possible to change kanji-fileio-code in this hook.
|
|
315 (run-hooks 'nntp-server-hook)
|
|
316 t
|
|
317 ))
|
|
318
|
|
319 (defun nnspool-close-server-internal ()
|
|
320 "Close connection to news server."
|
|
321 (if (get-file-buffer nnspool-history-file)
|
|
322 (kill-buffer (get-file-buffer nnspool-history-file)))
|
|
323 (if nntp-server-buffer
|
|
324 (kill-buffer nntp-server-buffer))
|
|
325 (setq nntp-server-buffer nil)
|
|
326 (setq nntp-server-process nil))
|
|
327
|
|
328 (defun nnspool-find-article-by-message-id (id)
|
|
329 "Return full pathname of an artilce identified by message-ID."
|
|
330 (save-excursion
|
|
331 (let ((buffer (get-file-buffer nnspool-history-file)))
|
|
332 (if buffer
|
|
333 (set-buffer buffer)
|
|
334 ;; Finding history file may take lots of time.
|
|
335 (message "Reading history file...")
|
|
336 (set-buffer (find-file-noselect nnspool-history-file))
|
|
337 (message "Reading history file... done")))
|
|
338 ;; Search from end of the file. I think this is much faster than
|
|
339 ;; do from the beginning of the file.
|
|
340 (goto-char (point-max))
|
|
341 (if (re-search-backward
|
|
342 (concat "^" (regexp-quote id)
|
|
343 "[ \t].*[ \t]\\([^ \t/]+\\)/\\([0-9]+\\)[ \t]*$") nil t)
|
|
344 (let ((group (buffer-substring (match-beginning 1) (match-end 1)))
|
|
345 (number (buffer-substring (match-beginning 2) (match-end 2))))
|
|
346 (concat (nnspool-article-pathname
|
|
347 (nnspool-replace-chars-in-string group ?. ?/))
|
|
348 number))
|
|
349 )))
|
|
350
|
|
351 (defun nnspool-find-file (file)
|
|
352 "Insert FILE in server buffer safely."
|
|
353 (set-buffer nntp-server-buffer)
|
|
354 (erase-buffer)
|
|
355 (condition-case ()
|
|
356 (progn (insert-file-contents file) t)
|
|
357 (file-error nil)
|
|
358 ))
|
|
359
|
|
360 (defun nnspool-article-pathname (group)
|
|
361 "Make pathname for GROUP."
|
|
362 (concat (file-name-as-directory nnspool-spool-directory) group "/"))
|
|
363
|
|
364 (defun nnspool-replace-chars-in-string (string from to)
|
|
365 "Replace characters in STRING from FROM to TO."
|
|
366 (let ((string (substring string 0)) ;Copy string.
|
|
367 (len (length string))
|
|
368 (idx 0))
|
|
369 ;; Replace all occurence of FROM with TO.
|
|
370 (while (< idx len)
|
|
371 (if (= (aref string idx) from)
|
|
372 (aset string idx to))
|
|
373 (setq idx (1+ idx)))
|
|
374 string
|
|
375 ))
|