1 ;;; mhspool.el --- MH folder access using NNTP for GNU Emacs
3 ;; Copyright (C) 1988, 1989 Fujitsu Laboratories LTD.
4 ;; Copyright (C) 1988, 1989, 1990 Masanobu UMEDA
6 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: mail, news
10 ;; $Header: mhspool.el,v 1.5 90/03/23 13:25:23 umerin Locked $
12 ;; This file is part of GNU Emacs.
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.
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.
31 ;; This package enables you to read mail or articles in MH folders, or
32 ;; articles saved by GNUS. In any case, the file names of mail or
33 ;; articles must consist of only numeric letters.
35 ;; Before using this package, you have to create a server specific
36 ;; startup file according to the directory which you want to read. For
37 ;; example, if you want to read mail under the directory named
38 ;; `~/Mail', the file must be a file named `.newsrc-:Mail'. (There is
39 ;; no way to specify hierarchical directory now.) In this case, the
40 ;; name of the NNTP server passed to GNUS must be `:Mail'.
46 (defvar mhspool-list-directory-switches
'("-R")
47 "*Switches for `nntp-request-list' to pass to `ls' for gettting file lists.
48 One entry should appear on one line. You may need to add `-1' option.")
52 (defconst mhspool-version
"MHSPOOL 1.5"
53 "Version numbers of this version of MHSPOOL.")
55 (defvar mhspool-spool-directory
"~/Mail"
56 "Private mail directory.")
58 (defvar mhspool-current-directory nil
59 "Current news group directory.")
62 ;;; Replacement of Extended Command for retrieving many headers.
65 (defun mhspool-retrieve-headers (sequence)
66 "Return list of article headers specified by SEQUENCE of article id.
68 `([NUMBER SUBJECT FROM XREF LINES DATE MESSAGE-ID REFERENCES] ...)'.
69 Reader macros for the vector are defined as `nntp-header-FIELD'.
70 Writer macros for the vector are defined as `nntp-set-header-FIELD'.
71 News group must be selected before calling me."
73 (set-buffer nntp-server-buffer
)
76 (number (length sequence
))
78 (headers nil
) ;Result list.
88 ;;(nntp-send-strings-to-server "HEAD" (car sequence))
89 (setq article
(car sequence
))
91 (concat mhspool-current-directory
(prin1-to-string article
)))
92 (if (and (file-exists-p file
)
93 (not (file-directory-p file
)))
96 (insert-file-contents file
)
97 ;; Make message body invisible.
98 (goto-char (point-min))
99 (search-forward "\n\n" nil
'move
)
100 (narrow-to-region (point-min) (point))
101 ;; Fold continuation lines.
102 (goto-char (point-min))
103 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t
)
104 (replace-match " " t t
))
105 ;; Make it possible to search for `\nFIELD'.
106 (goto-char (point-min))
109 (goto-char (point-min))
110 (if (search-forward "\nFrom: " nil t
)
111 (setq from
(buffer-substring
113 (save-excursion (end-of-line) (point))))
114 (setq from
"(Unknown User)"))
116 (goto-char (point-min))
117 (if (search-forward "\nSubject: " nil t
)
118 (setq subject
(buffer-substring
120 (save-excursion (end-of-line) (point))))
121 (setq subject
"(None)"))
122 ;; Extract Message-ID:
123 (goto-char (point-min))
124 (if (search-forward "\nMessage-ID: " nil t
)
125 (setq message-id
(buffer-substring
127 (save-excursion (end-of-line) (point))))
128 (setq message-id nil
))
130 (goto-char (point-min))
131 (if (search-forward "\nDate: " nil t
)
132 (setq date
(buffer-substring
134 (save-excursion (end-of-line) (point))))
137 (goto-char (point-min))
138 (if (search-forward "\nLines: " nil t
)
139 (setq lines
(string-to-int
142 (save-excursion (end-of-line) (point)))))
145 (goto-char (point-min))
146 (if (search-forward "\nXref: " nil t
)
147 (setq xref
(buffer-substring
149 (save-excursion (end-of-line) (point))))
151 ;; Extract References:
152 ;; If no References: field, use In-Reply-To: field instead.
153 ;; Suggested by tanaka@flab.fujitsu.co.jp (Hiroshi TANAKA).
154 (goto-char (point-min))
155 (if (or (search-forward "\nReferences: " nil t
)
156 (search-forward "\nIn-Reply-To: " nil t
))
157 (setq references
(buffer-substring
159 (save-excursion (end-of-line) (point))))
160 (setq references nil
))
162 (cons (vector article subject from
164 message-id references
) headers
))
166 (setq sequence
(cdr sequence
))
167 (setq count
(1+ count
))
168 (and (numberp nntp-large-newsgroup
)
169 (> number nntp-large-newsgroup
)
171 (message "MHSPOOL: %d%% of headers received."
172 (/ (* count
100) number
)))
174 (and (numberp nntp-large-newsgroup
)
175 (> number nntp-large-newsgroup
)
176 (message "MHSPOOL: 100%% of headers received."))
182 ;;; Replacement of NNTP Raw Interface.
185 (defun mhspool-open-server (host &optional service
)
186 "Open news server on HOST.
187 If HOST is nil, use value of environment variable `NNTPSERVER'.
188 If optional argument SERVICE is non-nil, open by the service name."
189 (let ((host (or host
(getenv "NNTPSERVER")))
191 ;; Get directory name from HOST name.
192 (if (string-match ":\\(.+\\)$" host
)
194 (setq mhspool-spool-directory
195 (file-name-as-directory
197 (substring host
(match-beginning 1) (match-end 1))
198 (expand-file-name "~/" nil
))))
199 (setq host
(system-name)))
200 (setq mhspool-spool-directory nil
))
201 (setq nntp-status-message-string
"")
202 (cond ((and (stringp host
)
203 (stringp mhspool-spool-directory
)
204 (file-directory-p mhspool-spool-directory
)
205 (string-equal host
(system-name)))
206 (setq status
(mhspool-open-server-internal host service
)))
207 ((string-equal host
(system-name))
208 (setq nntp-status-message-string
209 (format "No such directory: %s. Goodbye."
210 mhspool-spool-directory
)))
212 (setq nntp-status-message-string
"NNTP server is not specified."))
214 (setq nntp-status-message-string
215 (format "MHSPOOL: cannot talk to %s." host
)))
220 (defun mhspool-close-server ()
222 (mhspool-close-server-internal))
224 (fset 'mhspool-request-quit
(symbol-function 'mhspool-close-server
))
226 (defun mhspool-server-opened ()
227 "Return server process status, T or NIL.
228 If the stream is opened, return T, otherwise return NIL."
229 (and nntp-server-buffer
230 (get-buffer nntp-server-buffer
)))
232 (defun mhspool-status-message ()
233 "Return server status response as string."
234 nntp-status-message-string
237 (defun mhspool-request-article (id)
238 "Select article by message ID (or number)."
239 (let ((file (concat mhspool-current-directory
(prin1-to-string id
))))
240 (if (and (stringp file
)
242 (not (file-directory-p file
)))
244 (mhspool-find-file file
)))
247 (defun mhspool-request-body (id)
248 "Select article body by message ID (or number)."
249 (if (mhspool-request-article id
)
251 (set-buffer nntp-server-buffer
)
252 (goto-char (point-min))
253 (if (search-forward "\n\n" nil t
)
254 (delete-region (point-min) (point)))
259 (defun mhspool-request-head (id)
260 "Select article head by message ID (or number)."
261 (if (mhspool-request-article id
)
263 (set-buffer nntp-server-buffer
)
264 (goto-char (point-min))
265 (if (search-forward "\n\n" nil t
)
266 (delete-region (1- (point)) (point-max)))
271 (defun mhspool-request-stat (id)
272 "Select article by message ID (or number)."
273 (error "MHSPOOL: STAT is not implemented."))
275 (defun mhspool-request-group (group)
277 (cond ((file-directory-p
278 (mhspool-article-pathname group
))
280 (setq mhspool-current-directory
281 (mhspool-article-pathname group
)))
283 (mhspool-article-pathname
284 (mhspool-replace-chars-in-string group ?. ?
/)))
286 (setq mhspool-current-directory
287 (mhspool-article-pathname
288 (mhspool-replace-chars-in-string group ?. ?
/))))
291 (defun mhspool-request-list ()
292 "List valid newsgoups."
294 (let* ((newsgroup nil
)
296 (directory (file-name-as-directory
297 (expand-file-name mhspool-spool-directory nil
)))
298 (folder-regexp (concat "^" (regexp-quote directory
) "\\(.+\\):$"))
299 (buffer (get-buffer-create " *GNUS file listing*")))
300 (set-buffer nntp-server-buffer
)
306 (append mhspool-list-directory-switches
(list directory
)))
307 (goto-char (point-min))
308 (while (re-search-forward folder-regexp nil t
)
310 (mhspool-replace-chars-in-string
311 (buffer-substring (match-beginning 1) (match-end 1)) ?
/ ?.
))
313 (forward-line 1) ;(beginning-of-line)
314 ;; Thank nobu@flab.fujitsu.junet for his bug fixes.
315 (while (and (not (eobp))
316 (not (looking-at "^$")))
317 (if (looking-at "^[0-9]+$")
321 (match-beginning 0) (match-end 0)))
325 (princ (format "%s %d %d n\n" newsgroup
326 (apply (function max
) articles
)
327 (apply (function min
) articles
))
331 (set-buffer nntp-server-buffer
)
335 (defun mhspool-request-last ()
336 "Set current article pointer to the previous article in the current newsgroup."
337 (error "MHSPOOL: LAST is not implemented."))
339 (defun mhspool-request-next ()
340 "Advance current article pointer."
341 (error "MHSPOOL: NEXT is not implemented."))
343 (defun mhspool-request-post ()
344 "Post a new news in current buffer."
345 (setq nntp-status-message-string
"MHSPOOL: what do you mean post?")
351 ;;; Replacement of Low-Level Interface to NNTP Server.
354 (defun mhspool-open-server-internal (host &optional service
)
355 "Open connection to news server on HOST by SERVICE (default is nntp)."
357 (if (not (string-equal host
(system-name)))
358 (error "MHSPOOL: cannot talk to %s." host
))
359 ;; Initialize communication buffer.
360 (setq nntp-server-buffer
(get-buffer-create " *nntpd*"))
361 (set-buffer nntp-server-buffer
)
362 (buffer-flush-undo (current-buffer))
364 (kill-all-local-variables)
365 (setq case-fold-search t
) ;Should ignore case.
366 (setq nntp-server-process nil
)
367 (setq nntp-server-name host
)
368 ;; It is possible to change kanji-fileio-code in this hook.
369 (run-hooks 'nntp-server-hook
)
373 (defun mhspool-close-server-internal ()
374 "Close connection to news server."
375 (if nntp-server-buffer
376 (kill-buffer nntp-server-buffer
))
377 (setq nntp-server-buffer nil
)
378 (setq nntp-server-process nil
))
380 (defun mhspool-find-file (file)
381 "Insert FILE in server buffer safely."
382 (set-buffer nntp-server-buffer
)
386 (insert-file-contents file
)
387 (goto-char (point-min))
388 ;; If there is no body, `^L' appears at end of file. Special
389 ;; hack for MH folder.
390 (and (search-forward "\n\n" nil t
)
391 (string-equal (buffer-substring (point) (point-max)) "\^L")
398 (defun mhspool-article-pathname (group)
399 "Make pathname for GROUP."
400 (concat (file-name-as-directory mhspool-spool-directory
) group
"/"))
402 (defun mhspool-replace-chars-in-string (string from to
)
403 "Replace characters in STRING from FROM to TO."
404 (let ((string (substring string
0)) ;Copy string.
405 (len (length string
))
407 ;; Replace all occurence of FROM with TO.
409 (if (= (aref string idx
) from
)
410 (aset string idx to
))
417 ;;; mhspool.el ends here