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 $
6 ;; This file is part of GNU Emacs.
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.
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.
26 (defvar nnspool-inews-program news-inews-program
27 "*Program to post news.")
29 (defvar nnspool-inews-switches
'("-h")
30 "*Switches for nnspool-request-post to pass to `inews' for posting news.")
32 (defvar nnspool-spool-directory news-path
33 "*Local news spool directory.")
35 (defvar nnspool-active-file
"/usr/lib/news/active"
36 "*Local news active file.")
38 (defvar nnspool-history-file
"/usr/lib/news/history"
39 "*Local news history file.")
43 (defconst nnspool-version
"NNSPOOL 1.10"
44 "Version numbers of this version of NNSPOOL.")
46 (defvar nnspool-current-directory nil
47 "Current news group directory.")
50 ;;; Replacement of Extended Command for retrieving many headers.
53 (defun nnspool-retrieve-headers (sequence)
54 "Return list of article headers specified by SEQUENCE of article id.
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."
61 (set-buffer nntp-server-buffer
)
64 (number (length sequence
))
66 (headers nil
) ;Result list.
76 ;;(nntp-send-strings-to-server "HEAD" (car sequence))
77 (setq article
(car sequence
))
79 (concat nnspool-current-directory
(prin1-to-string article
)))
80 (if (and (file-exists-p file
)
81 (not (file-directory-p file
)))
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))
97 (goto-char (point-min))
98 (if (search-forward "\nFrom: " nil t
)
99 (setq from
(buffer-substring
101 (save-excursion (end-of-line) (point))))
102 (setq from
"(Unknown User)"))
104 (goto-char (point-min))
105 (if (search-forward "\nSubject: " nil t
)
106 (setq subject
(buffer-substring
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
115 (save-excursion (end-of-line) (point))))
116 (setq message-id nil
))
118 (goto-char (point-min))
119 (if (search-forward "\nDate: " nil t
)
120 (setq date
(buffer-substring
122 (save-excursion (end-of-line) (point))))
125 (goto-char (point-min))
126 (if (search-forward "\nLines: " nil t
)
127 (setq lines
(string-to-int
130 (save-excursion (end-of-line) (point)))))
133 (goto-char (point-min))
134 (if (search-forward "\nXref: " nil t
)
135 (setq xref
(buffer-substring
137 (save-excursion (end-of-line) (point))))
139 ;; Extract References:
140 (goto-char (point-min))
141 (if (search-forward "\nReferences: " nil t
)
142 (setq references
(buffer-substring
144 (save-excursion (end-of-line) (point))))
145 (setq references nil
))
147 (cons (vector article subject from
149 message-id references
) headers
))
151 (setq sequence
(cdr sequence
))
152 (setq count
(1+ count
))
153 (and (numberp nntp-large-newsgroup
)
154 (> number nntp-large-newsgroup
)
156 (message "NNSPOOL: %d%% of headers received."
157 (/ (* count
100) number
)))
159 (and (numberp nntp-large-newsgroup
)
160 (> number nntp-large-newsgroup
)
161 (message "NNSPOOL: 100%% of headers received."))
167 ;;; Replacement of NNTP Raw Interface.
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")))
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
)))
185 (setq nntp-status-message-string
"NNTP server is not specified."))
187 (setq nntp-status-message-string
188 (format "NNSPOOL: cannot talk to %s." host
)))
193 (defun nnspool-close-server ()
195 (nnspool-close-server-internal))
197 (fset 'nnspool-request-quit
(symbol-function 'nnspool-close-server
))
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
)))
205 (defun nnspool-status-message ()
206 "Return server status response as string."
207 nntp-status-message-string
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
)
217 (not (file-directory-p file
)))
219 (nnspool-find-file file
)))
222 (defun nnspool-request-body (id)
223 "Select article body by message ID (or number)."
224 (if (nnspool-request-article id
)
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)))
234 (defun nnspool-request-head (id)
235 "Select article head by message ID (or number)."
236 (if (nnspool-request-article id
)
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)))
246 (defun nnspool-request-stat (id)
247 "Select article by message ID (or number)."
248 (error "NNSPOOL: STAT is not implemented."))
250 (defun nnspool-request-group (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
))
258 (defun nnspool-request-list ()
259 "List valid newsgoups."
261 (nnspool-find-file nnspool-active-file
)))
263 (defun nnspool-request-last ()
264 "Set current article pointer to the previous article in the current news group."
265 (error "NNSPOOL: LAST is not implemented."))
267 (defun nnspool-request-next ()
268 "Advance current article pointer."
269 (error "NNSPOOL: NEXT is not implemented."))
271 (defun nnspool-request-post ()
272 "Post a new news in current buffer."
274 ;; We have to work in the server buffer because of NEmacs hack.
275 (copy-to-buffer nntp-server-buffer
(point-min) (point-max))
276 (set-buffer nntp-server-buffer
)
277 (apply 'call-process-region
278 (point-min) (point-max)
279 nnspool-inews-program
'delete t nil nnspool-inews-switches
)
281 (or (zerop (buffer-size))
282 ;; If inews returns strings, it must be error message
283 ;; unless SPOOLNEWS is defined.
284 ;; This condition is very weak, but there is no good rule
285 ;; identifying errors when SPOOLNEWS is defined.
286 ;; Suggested by ohm@kaba.junet.
287 (string-match "spooled" (buffer-string)))
288 ;; Make status message by unfolding lines.
289 (subst-char-in-region (point-min) (point-max) ?
\n ?
\\ 'noundo
)
290 (setq nntp-status-message-string
(buffer-string))
296 ;;; Replacement of Low-Level Interface to NNTP Server.
299 (defun nnspool-open-server-internal (host &optional service
)
300 "Open connection to news server on HOST by SERVICE (default is nntp)."
302 (if (not (string-equal host
(system-name)))
303 (error "NNSPOOL: cannot talk to %s." host
))
304 ;; Initialize communication buffer.
305 (setq nntp-server-buffer
(get-buffer-create " *nntpd*"))
306 (set-buffer nntp-server-buffer
)
307 (buffer-flush-undo (current-buffer))
309 (kill-all-local-variables)
310 (setq case-fold-search t
) ;Should ignore case.
311 (setq nntp-server-process nil
)
312 (setq nntp-server-name host
)
313 ;; It is possible to change kanji-fileio-code in this hook.
314 (run-hooks 'nntp-server-hook
)
318 (defun nnspool-close-server-internal ()
319 "Close connection to news server."
320 (if (get-file-buffer nnspool-history-file
)
321 (kill-buffer (get-file-buffer nnspool-history-file
)))
322 (if nntp-server-buffer
323 (kill-buffer nntp-server-buffer
))
324 (setq nntp-server-buffer nil
)
325 (setq nntp-server-process nil
))
327 (defun nnspool-find-article-by-message-id (id)
328 "Return full pathname of an article identified by message-ID."
330 (let ((buffer (get-file-buffer nnspool-history-file
)))
333 ;; Finding history file may take lots of time.
334 (message "Reading history file...")
335 (set-buffer (find-file-noselect nnspool-history-file
))
336 (message "Reading history file... done")))
337 ;; Search from end of the file. I think this is much faster than
338 ;; do from the beginning of the file.
339 (goto-char (point-max))
340 (if (re-search-backward
341 (concat "^" (regexp-quote id
)
342 "[ \t].*[ \t]\\([^ \t/]+\\)/\\([0-9]+\\)[ \t]*$") nil t
)
343 (let ((group (buffer-substring (match-beginning 1) (match-end 1)))
344 (number (buffer-substring (match-beginning 2) (match-end 2))))
345 (concat (nnspool-article-pathname
346 (nnspool-replace-chars-in-string group ?. ?
/))
350 (defun nnspool-find-file (file)
351 "Insert FILE in server buffer safely."
352 (set-buffer nntp-server-buffer
)
355 (progn (insert-file-contents file
) t
)
359 (defun nnspool-article-pathname (group)
360 "Make pathname for GROUP."
361 (concat (file-name-as-directory nnspool-spool-directory
) group
"/"))
363 (defun nnspool-replace-chars-in-string (string from to
)
364 "Replace characters in STRING from FROM to TO."
365 (let ((string (substring string
0)) ;Copy string.
366 (len (length string
))
368 ;; Replace all occurence of FROM with TO.
370 (if (= (aref string idx
) from
)
371 (aset string idx to
))