1 ;;; mhspool.el --- MH folder 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>
7 ;; Keywords: mail, news
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; This package enables you to read mail or articles in MH folders, or
28 ;; articles saved by GNUS. In any case, the file names of mail or
29 ;; articles must consist of only numeric letters.
31 ;; Before using this package, you have to create a server specific
32 ;; startup file according to the directory which you want to read. For
33 ;; example, if you want to read mail under the directory named
34 ;; `~/Mail', the file must be a file named `.newsrc-:Mail'. (There is
35 ;; no way to specify hierarchical directory now.) In this case, the
36 ;; name of the NNTP server passed to GNUS must be `:Mail'.
42 (defvar mhspool-list-directory-switches
'("-R")
43 "*Switches for `nntp-request-list' to pass to `ls' for gettting file lists.
44 One entry should appear on one line. You may need to add `-1' option.")
48 (defconst mhspool-version
"MHSPOOL 1.5"
49 "Version numbers of this version of MHSPOOL.")
51 (defvar mhspool-spool-directory
"~/Mail"
52 "Private mail directory.")
54 (defvar mhspool-current-directory nil
55 "Current news group directory.")
58 ;;; Replacement of Extended Command for retrieving many headers.
61 (defun mhspool-retrieve-headers (sequence)
62 "Return list of article headers specified by SEQUENCE of article id.
64 `([NUMBER SUBJECT FROM XREF LINES DATE MESSAGE-ID REFERENCES] ...)'.
65 Reader macros for the vector are defined as `nntp-header-FIELD'.
66 Writer macros for the vector are defined as `nntp-set-header-FIELD'.
67 News group must be selected before calling me."
69 (set-buffer nntp-server-buffer
)
72 (number (length sequence
))
74 (headers nil
) ;Result list.
84 ;;(nntp-send-strings-to-server "HEAD" (car sequence))
85 (setq article
(car sequence
))
87 (concat mhspool-current-directory
(prin1-to-string article
)))
88 (if (and (file-exists-p file
)
89 (not (file-directory-p file
)))
92 (insert-file-contents file
)
93 ;; Make message body invisible.
94 (goto-char (point-min))
95 (search-forward "\n\n" nil
'move
)
96 (narrow-to-region (point-min) (point))
97 ;; Fold continuation lines.
98 (goto-char (point-min))
99 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t
)
100 (replace-match " " t t
))
101 ;; Make it possible to search for `\nFIELD'.
102 (goto-char (point-min))
105 (goto-char (point-min))
106 (if (search-forward "\nFrom: " nil t
)
107 (setq from
(buffer-substring
109 (save-excursion (end-of-line) (point))))
110 (setq from
"(Unknown User)"))
112 (goto-char (point-min))
113 (if (search-forward "\nSubject: " nil t
)
114 (setq subject
(buffer-substring
116 (save-excursion (end-of-line) (point))))
117 (setq subject
"(None)"))
118 ;; Extract Message-ID:
119 (goto-char (point-min))
120 (if (search-forward "\nMessage-ID: " nil t
)
121 (setq message-id
(buffer-substring
123 (save-excursion (end-of-line) (point))))
124 (setq message-id nil
))
126 (goto-char (point-min))
127 (if (search-forward "\nDate: " nil t
)
128 (setq date
(buffer-substring
130 (save-excursion (end-of-line) (point))))
133 (goto-char (point-min))
134 (if (search-forward "\nLines: " nil t
)
135 (setq lines
(string-to-int
138 (save-excursion (end-of-line) (point)))))
141 (goto-char (point-min))
142 (if (search-forward "\nXref: " nil t
)
143 (setq xref
(buffer-substring
145 (save-excursion (end-of-line) (point))))
147 ;; Extract References:
148 ;; If no References: field, use In-Reply-To: field instead.
149 ;; Suggested by tanaka@flab.fujitsu.co.jp (Hiroshi TANAKA).
150 (goto-char (point-min))
151 (if (or (search-forward "\nReferences: " nil t
)
152 (search-forward "\nIn-Reply-To: " nil t
))
153 (setq references
(buffer-substring
155 (save-excursion (end-of-line) (point))))
156 (setq references nil
))
158 (cons (vector article subject from
160 message-id references
) headers
))
162 (setq sequence
(cdr sequence
))
163 (setq count
(1+ count
))
164 (and (numberp nntp-large-newsgroup
)
165 (> number nntp-large-newsgroup
)
167 (message "MHSPOOL: %d%% of headers received."
168 (/ (* count
100) number
)))
170 (and (numberp nntp-large-newsgroup
)
171 (> number nntp-large-newsgroup
)
172 (message "MHSPOOL: 100%% of headers received."))
178 ;;; Replacement of NNTP Raw Interface.
181 (defun mhspool-open-server (host &optional service
)
182 "Open news server on HOST.
183 If HOST is nil, use value of environment variable `NNTPSERVER'.
184 If optional argument SERVICE is non-nil, open by the service name."
185 (let ((host (or host
(getenv "NNTPSERVER")))
187 ;; Get directory name from HOST name.
188 (if (string-match ":\\(.+\\)$" host
)
190 (setq mhspool-spool-directory
191 (file-name-as-directory
193 (substring host
(match-beginning 1) (match-end 1))
194 (expand-file-name "~/" nil
))))
195 (setq host
(system-name)))
196 (setq mhspool-spool-directory nil
))
197 (setq nntp-status-message-string
"")
198 (cond ((and (stringp host
)
199 (stringp mhspool-spool-directory
)
200 (file-directory-p mhspool-spool-directory
)
201 (string-equal host
(system-name)))
202 (setq status
(mhspool-open-server-internal host service
)))
203 ((string-equal host
(system-name))
204 (setq nntp-status-message-string
205 (format "No such directory: %s. Goodbye."
206 mhspool-spool-directory
)))
208 (setq nntp-status-message-string
"NNTP server is not specified."))
210 (setq nntp-status-message-string
211 (format "MHSPOOL: cannot talk to %s." host
)))
216 (defun mhspool-close-server ()
218 (mhspool-close-server-internal))
220 (fset 'mhspool-request-quit
(symbol-function 'mhspool-close-server
))
222 (defun mhspool-server-opened ()
223 "Return server process status, T or NIL.
224 If the stream is opened, return T, otherwise return NIL."
225 (and nntp-server-buffer
226 (get-buffer nntp-server-buffer
)))
228 (defun mhspool-status-message ()
229 "Return server status response as string."
230 nntp-status-message-string
233 (defun mhspool-request-article (id)
234 "Select article by message ID (or number)."
235 (let ((file (concat mhspool-current-directory
(prin1-to-string id
))))
236 (if (and (stringp file
)
238 (not (file-directory-p file
)))
240 (mhspool-find-file file
)))
243 (defun mhspool-request-body (id)
244 "Select article body by message ID (or number)."
245 (if (mhspool-request-article id
)
247 (set-buffer nntp-server-buffer
)
248 (goto-char (point-min))
249 (if (search-forward "\n\n" nil t
)
250 (delete-region (point-min) (point)))
255 (defun mhspool-request-head (id)
256 "Select article head by message ID (or number)."
257 (if (mhspool-request-article id
)
259 (set-buffer nntp-server-buffer
)
260 (goto-char (point-min))
261 (if (search-forward "\n\n" nil t
)
262 (delete-region (1- (point)) (point-max)))
267 (defun mhspool-request-stat (id)
268 "Select article by message ID (or number)."
269 (error "MHSPOOL: STAT is not implemented."))
271 (defun mhspool-request-group (group)
273 (cond ((file-directory-p
274 (mhspool-article-pathname group
))
276 (setq mhspool-current-directory
277 (mhspool-article-pathname group
)))
279 (mhspool-article-pathname
280 (mhspool-replace-chars-in-string group ?. ?
/)))
282 (setq mhspool-current-directory
283 (mhspool-article-pathname
284 (mhspool-replace-chars-in-string group ?. ?
/))))
287 (defun mhspool-request-list ()
288 "List valid newsgoups."
290 (let* ((newsgroup nil
)
292 (directory (file-name-as-directory
293 (expand-file-name mhspool-spool-directory nil
)))
294 (folder-regexp (concat "^" (regexp-quote directory
) "\\(.+\\):$"))
295 (buffer (get-buffer-create " *GNUS file listing*")))
296 (set-buffer nntp-server-buffer
)
302 (append mhspool-list-directory-switches
(list directory
)))
303 (goto-char (point-min))
304 (while (re-search-forward folder-regexp nil t
)
306 (mhspool-replace-chars-in-string
307 (buffer-substring (match-beginning 1) (match-end 1)) ?
/ ?.
))
309 (forward-line 1) ;(beginning-of-line)
310 ;; Thank nobu@flab.fujitsu.junet for his bug fixes.
311 (while (and (not (eobp))
312 (not (looking-at "^$")))
313 (if (looking-at "^[0-9]+$")
317 (match-beginning 0) (match-end 0)))
321 (princ (format "%s %d %d n\n" newsgroup
322 (apply (function max
) articles
)
323 (apply (function min
) articles
))
327 (set-buffer nntp-server-buffer
)
331 (defun mhspool-request-last ()
332 "Set current article pointer to the previous article in the current newsgroup."
333 (error "MHSPOOL: LAST is not implemented."))
335 (defun mhspool-request-next ()
336 "Advance current article pointer."
337 (error "MHSPOOL: NEXT is not implemented."))
339 (defun mhspool-request-post ()
340 "Post a new news in current buffer."
341 (setq nntp-status-message-string
"MHSPOOL: what do you mean post?")
347 ;;; Replacement of Low-Level Interface to NNTP Server.
350 (defun mhspool-open-server-internal (host &optional service
)
351 "Open connection to news server on HOST by SERVICE (default is nntp)."
353 (if (not (string-equal host
(system-name)))
354 (error "MHSPOOL: cannot talk to %s." host
))
355 ;; Initialize communication buffer.
356 (setq nntp-server-buffer
(get-buffer-create " *nntpd*"))
357 (set-buffer nntp-server-buffer
)
358 (buffer-flush-undo (current-buffer))
360 (kill-all-local-variables)
361 (setq case-fold-search t
) ;Should ignore case.
362 (setq nntp-server-process nil
)
363 (setq nntp-server-name host
)
364 ;; It is possible to change kanji-fileio-code in this hook.
365 (run-hooks 'nntp-server-hook
)
369 (defun mhspool-close-server-internal ()
370 "Close connection to news server."
371 (if nntp-server-buffer
372 (kill-buffer nntp-server-buffer
))
373 (setq nntp-server-buffer nil
)
374 (setq nntp-server-process nil
))
376 (defun mhspool-find-file (file)
377 "Insert FILE in server buffer safely."
378 (set-buffer nntp-server-buffer
)
382 (insert-file-contents file
)
383 (goto-char (point-min))
384 ;; If there is no body, `^L' appears at end of file. Special
385 ;; hack for MH folder.
386 (and (search-forward "\n\n" nil t
)
387 (string-equal (buffer-substring (point) (point-max)) "\^L")
394 (defun mhspool-article-pathname (group)
395 "Make pathname for GROUP."
396 (concat (file-name-as-directory mhspool-spool-directory
) group
"/"))
398 (defun mhspool-replace-chars-in-string (string from to
)
399 "Replace characters in STRING from FROM to TO."
400 (let ((string (substring string
0)) ;Copy string.
401 (len (length string
))
403 ;; Replace all occurence of FROM with TO.
405 (if (= (aref string idx
) from
)
406 (aset string idx to
))
413 ;;; mhspool.el ends here