1 ;;; nnspool.el --- spool access using NNTP for GNU Emacs
3 ;; Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; This file is part of GNU Emacs.
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)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
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.
28 (defvar nnspool-inews-program news-inews-program
29 "*Program to post news.")
31 (defvar nnspool-inews-switches
'("-h")
32 "*Switches for nnspool-request-post to pass to `inews' for posting news.")
34 (defvar nnspool-spool-directory news-path
35 "*Local news spool directory.")
37 (defvar nnspool-active-file
"/usr/lib/news/active"
38 "*Local news active file.")
40 (defvar nnspool-history-file
"/usr/lib/news/history"
41 "*Local news history file.")
45 (defconst nnspool-version
"NNSPOOL 1.10"
46 "Version numbers of this version of NNSPOOL.")
48 (defvar nnspool-current-directory nil
49 "Current news group directory.")
52 ;;; Replacement of Extended Command for retrieving many headers.
55 (defun nnspool-retrieve-headers (sequence)
56 "Return list of article headers specified by SEQUENCE of article id.
58 `([NUMBER SUBJECT FROM XREF LINES DATE MESSAGE-ID REFERENCES] ...)'.
59 Reader macros for the vector are defined as `nntp-header-FIELD'.
60 Writer macros for the vector are defined as `nntp-set-header-FIELD'.
61 News group must be selected before calling me."
63 (set-buffer nntp-server-buffer
)
66 (number (length sequence
))
68 (headers nil
) ;Result list.
78 ;;(nntp-send-strings-to-server "HEAD" (car sequence))
79 (setq article
(car sequence
))
81 (concat nnspool-current-directory
(prin1-to-string article
)))
82 (if (and (file-exists-p file
)
83 (not (file-directory-p file
)))
86 (insert-file-contents file
)
87 ;; Make message body invisible.
88 (goto-char (point-min))
89 (search-forward "\n\n" nil
'move
)
90 (narrow-to-region (point-min) (point))
91 ;; Fold continuation lines.
92 (goto-char (point-min))
93 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t
)
94 (replace-match " " t t
))
95 ;; Make it possible to search for `\nFIELD'.
96 (goto-char (point-min))
99 (goto-char (point-min))
100 (if (search-forward "\nFrom: " nil t
)
101 (setq from
(buffer-substring
103 (save-excursion (end-of-line) (point))))
104 (setq from
"(Unknown User)"))
106 (goto-char (point-min))
107 (if (search-forward "\nSubject: " nil t
)
108 (setq subject
(buffer-substring
110 (save-excursion (end-of-line) (point))))
111 (setq subject
"(None)"))
112 ;; Extract Message-ID:
113 (goto-char (point-min))
114 (if (search-forward "\nMessage-ID: " nil t
)
115 (setq message-id
(buffer-substring
117 (save-excursion (end-of-line) (point))))
118 (setq message-id nil
))
120 (goto-char (point-min))
121 (if (search-forward "\nDate: " nil t
)
122 (setq date
(buffer-substring
124 (save-excursion (end-of-line) (point))))
127 (goto-char (point-min))
128 (if (search-forward "\nLines: " nil t
)
129 (setq lines
(string-to-int
132 (save-excursion (end-of-line) (point)))))
135 (goto-char (point-min))
136 (if (search-forward "\nXref: " nil t
)
137 (setq xref
(buffer-substring
139 (save-excursion (end-of-line) (point))))
141 ;; Extract References:
142 (goto-char (point-min))
143 (if (search-forward "\nReferences: " nil t
)
144 (setq references
(buffer-substring
146 (save-excursion (end-of-line) (point))))
147 (setq references nil
))
149 (cons (vector article subject from
151 message-id references
) headers
))
153 (setq sequence
(cdr sequence
))
154 (setq count
(1+ count
))
155 (and (numberp nntp-large-newsgroup
)
156 (> number nntp-large-newsgroup
)
158 (message "NNSPOOL: %d%% of headers received."
159 (/ (* count
100) number
)))
161 (and (numberp nntp-large-newsgroup
)
162 (> number nntp-large-newsgroup
)
163 (message "NNSPOOL: 100%% of headers received."))
169 ;;; Replacement of NNTP Raw Interface.
172 (defun nnspool-open-server (host &optional service
)
173 "Open news server on HOST.
174 If HOST is nil, use value of environment variable `NNTPSERVER'.
175 If optional argument SERVICE is non-nil, open by the service name."
176 (let ((host (or host
(getenv "NNTPSERVER")))
178 (setq nntp-status-message-string
"")
179 (cond ((and (file-directory-p nnspool-spool-directory
)
180 (file-exists-p nnspool-active-file
)
181 (string-equal host
(system-name)))
182 (setq status
(nnspool-open-server-internal host service
)))
183 ((string-equal host
(system-name))
184 (setq nntp-status-message-string
185 (format "%s has no news spool. Goodbye." host
)))
187 (setq nntp-status-message-string
"NNTP server is not specified."))
189 (setq nntp-status-message-string
190 (format "NNSPOOL: cannot talk to %s." host
)))
195 (defun nnspool-close-server ()
197 (nnspool-close-server-internal))
199 (fset 'nnspool-request-quit
(symbol-function 'nnspool-close-server
))
201 (defun nnspool-server-opened ()
202 "Return server process status, T or NIL.
203 If the stream is opened, return T, otherwise return NIL."
204 (and nntp-server-buffer
205 (get-buffer nntp-server-buffer
)))
207 (defun nnspool-status-message ()
208 "Return server status response as string."
209 nntp-status-message-string
212 (defun nnspool-request-article (id)
213 "Select article by message ID (or number)."
214 (let ((file (if (stringp id
)
215 (nnspool-find-article-by-message-id id
)
216 (concat nnspool-current-directory
(prin1-to-string id
)))))
217 (if (and (stringp file
)
219 (not (file-directory-p file
)))
221 (nnspool-find-file file
)))
224 (defun nnspool-request-body (id)
225 "Select article body by message ID (or number)."
226 (if (nnspool-request-article id
)
228 (set-buffer nntp-server-buffer
)
229 (goto-char (point-min))
230 (if (search-forward "\n\n" nil t
)
231 (delete-region (point-min) (point)))
236 (defun nnspool-request-head (id)
237 "Select article head by message ID (or number)."
238 (if (nnspool-request-article id
)
240 (set-buffer nntp-server-buffer
)
241 (goto-char (point-min))
242 (if (search-forward "\n\n" nil t
)
243 (delete-region (1- (point)) (point-max)))
248 (defun nnspool-request-stat (id)
249 "Select article by message ID (or number)."
250 (error "NNSPOOL: STAT is not implemented."))
252 (defun nnspool-request-group (group)
254 (let ((pathname (nnspool-article-pathname
255 (nnspool-replace-chars-in-string group ?. ?
/))))
256 (if (file-directory-p pathname
)
257 (setq nnspool-current-directory pathname
))
260 (defun nnspool-request-list ()
261 "List valid newsgoups."
263 (nnspool-find-file nnspool-active-file
)))
265 (defun nnspool-request-last ()
266 "Set current article pointer to the previous article in the current news group."
267 (error "NNSPOOL: LAST is not implemented."))
269 (defun nnspool-request-next ()
270 "Advance current article pointer."
271 (error "NNSPOOL: NEXT is not implemented."))
273 (defun nnspool-request-post ()
274 "Post a new news in current buffer."
276 ;; We have to work in the server buffer because of NEmacs hack.
277 (copy-to-buffer nntp-server-buffer
(point-min) (point-max))
278 (set-buffer nntp-server-buffer
)
279 (apply 'call-process-region
280 (point-min) (point-max)
281 nnspool-inews-program
'delete t nil nnspool-inews-switches
)
283 (or (zerop (buffer-size))
284 ;; If inews returns strings, it must be error message
285 ;; unless SPOOLNEWS is defined.
286 ;; This condition is very weak, but there is no good rule
287 ;; identifying errors when SPOOLNEWS is defined.
288 ;; Suggested by ohm@kaba.junet.
289 (string-match "spooled" (buffer-string)))
290 ;; Make status message by unfolding lines.
291 (subst-char-in-region (point-min) (point-max) ?
\n ?
\\ 'noundo
)
292 (setq nntp-status-message-string
(buffer-string))
298 ;;; Replacement of Low-Level Interface to NNTP Server.
301 (defun nnspool-open-server-internal (host &optional service
)
302 "Open connection to news server on HOST by SERVICE (default is nntp)."
304 (if (not (string-equal host
(system-name)))
305 (error "NNSPOOL: cannot talk to %s." host
))
306 ;; Initialize communication buffer.
307 (setq nntp-server-buffer
(get-buffer-create " *nntpd*"))
308 (set-buffer nntp-server-buffer
)
309 (buffer-flush-undo (current-buffer))
311 (kill-all-local-variables)
312 (setq case-fold-search t
) ;Should ignore case.
313 (setq nntp-server-process nil
)
314 (setq nntp-server-name host
)
315 ;; It is possible to change kanji-fileio-code in this hook.
316 (run-hooks 'nntp-server-hook
)
320 (defun nnspool-close-server-internal ()
321 "Close connection to news server."
322 (if (get-file-buffer nnspool-history-file
)
323 (kill-buffer (get-file-buffer nnspool-history-file
)))
324 (if nntp-server-buffer
325 (kill-buffer nntp-server-buffer
))
326 (setq nntp-server-buffer nil
)
327 (setq nntp-server-process nil
))
329 (defun nnspool-find-article-by-message-id (id)
330 "Return full pathname of an article identified by message-ID."
332 (let ((buffer (get-file-buffer nnspool-history-file
)))
335 ;; Finding history file may take lots of time.
336 (message "Reading history file...")
337 (set-buffer (find-file-noselect nnspool-history-file
))
338 (message "Reading history file... done")))
339 ;; Search from end of the file. I think this is much faster than
340 ;; do from the beginning of the file.
341 (goto-char (point-max))
342 (if (re-search-backward
343 (concat "^" (regexp-quote id
)
344 "[ \t].*[ \t]\\([^ \t/]+\\)/\\([0-9]+\\)[ \t]*$") nil t
)
345 (let ((group (buffer-substring (match-beginning 1) (match-end 1)))
346 (number (buffer-substring (match-beginning 2) (match-end 2))))
347 (concat (nnspool-article-pathname
348 (nnspool-replace-chars-in-string group ?. ?
/))
352 (defun nnspool-find-file (file)
353 "Insert FILE in server buffer safely."
354 (set-buffer nntp-server-buffer
)
357 (progn (insert-file-contents file
) t
)
361 (defun nnspool-article-pathname (group)
362 "Make pathname for GROUP."
363 (concat (file-name-as-directory nnspool-spool-directory
) group
"/"))
365 (defun nnspool-replace-chars-in-string (string from to
)
366 "Replace characters in STRING from FROM to TO."
367 (let ((string (substring string
0)) ;Copy string.
368 (len (length string
))
370 ;; Replace all occurence of FROM with TO.
372 (if (= (aref string idx
) from
)
373 (aset string idx to
))
380 ;;; nnspool.el ends here