*** empty log message ***
[emacs.git] / lisp / mhspool.el
blobfba1b5ae839f3a5a4e58881c432e839e0fb8685e
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>
7 ;; Maintainer: FSF
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.
29 ;; Commentary:
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'.
42 ;; Code:
44 (require 'nntp)
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.")
61 ;;;
62 ;;; Replacement of Extended Command for retrieving many headers.
63 ;;;
65 (defun mhspool-retrieve-headers (sequence)
66 "Return list of article headers specified by SEQUENCE of article id.
67 The format of list is
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."
72 (save-excursion
73 (set-buffer nntp-server-buffer)
74 ;;(erase-buffer)
75 (let ((file nil)
76 (number (length sequence))
77 (count 0)
78 (headers nil) ;Result list.
79 (article 0)
80 (subject nil)
81 (message-id nil)
82 (from nil)
83 (xref nil)
84 (lines 0)
85 (date nil)
86 (references nil))
87 (while sequence
88 ;;(nntp-send-strings-to-server "HEAD" (car sequence))
89 (setq article (car sequence))
90 (setq file
91 (concat mhspool-current-directory (prin1-to-string article)))
92 (if (and (file-exists-p file)
93 (not (file-directory-p file)))
94 (progn
95 (erase-buffer)
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))
107 (insert "\n")
108 ;; Extract From:
109 (goto-char (point-min))
110 (if (search-forward "\nFrom: " nil t)
111 (setq from (buffer-substring
112 (point)
113 (save-excursion (end-of-line) (point))))
114 (setq from "(Unknown User)"))
115 ;; Extract Subject:
116 (goto-char (point-min))
117 (if (search-forward "\nSubject: " nil t)
118 (setq subject (buffer-substring
119 (point)
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
126 (point)
127 (save-excursion (end-of-line) (point))))
128 (setq message-id nil))
129 ;; Extract Date:
130 (goto-char (point-min))
131 (if (search-forward "\nDate: " nil t)
132 (setq date (buffer-substring
133 (point)
134 (save-excursion (end-of-line) (point))))
135 (setq date nil))
136 ;; Extract Lines:
137 (goto-char (point-min))
138 (if (search-forward "\nLines: " nil t)
139 (setq lines (string-to-int
140 (buffer-substring
141 (point)
142 (save-excursion (end-of-line) (point)))))
143 (setq lines 0))
144 ;; Extract Xref:
145 (goto-char (point-min))
146 (if (search-forward "\nXref: " nil t)
147 (setq xref (buffer-substring
148 (point)
149 (save-excursion (end-of-line) (point))))
150 (setq xref nil))
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
158 (point)
159 (save-excursion (end-of-line) (point))))
160 (setq references nil))
161 (setq headers
162 (cons (vector article subject from
163 xref lines date
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)
170 (zerop (% count 20))
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."))
177 (nreverse headers)
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")))
190 (status nil))
191 ;; Get directory name from HOST name.
192 (if (string-match ":\\(.+\\)$" host)
193 (progn
194 (setq mhspool-spool-directory
195 (file-name-as-directory
196 (expand-file-name
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)))
211 ((null host)
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)))
217 status
220 (defun mhspool-close-server ()
221 "Close news 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)
241 (file-exists-p file)
242 (not (file-directory-p file)))
243 (save-excursion
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)
250 (save-excursion
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)
262 (save-excursion
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)
276 "Select news GROUP."
277 (cond ((file-directory-p
278 (mhspool-article-pathname group))
279 ;; Mail/NEWS.GROUP/N
280 (setq mhspool-current-directory
281 (mhspool-article-pathname group)))
282 ((file-directory-p
283 (mhspool-article-pathname
284 (mhspool-replace-chars-in-string group ?. ?/)))
285 ;; Mail/NEWS/GROUP/N
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."
293 (save-excursion
294 (let* ((newsgroup nil)
295 (articles 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)
301 (erase-buffer)
302 (set-buffer buffer)
303 (erase-buffer)
304 (apply 'call-process
305 "ls" nil t nil
306 (append mhspool-list-directory-switches (list directory)))
307 (goto-char (point-min))
308 (while (re-search-forward folder-regexp nil t)
309 (setq newsgroup
310 (mhspool-replace-chars-in-string
311 (buffer-substring (match-beginning 1) (match-end 1)) ?/ ?.))
312 (setq articles nil)
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]+$")
318 (setq articles
319 (cons (string-to-int
320 (buffer-substring
321 (match-beginning 0) (match-end 0)))
322 articles)))
323 (forward-line 1))
324 (if articles
325 (princ (format "%s %d %d n\n" newsgroup
326 (apply (function max) articles)
327 (apply (function min) articles))
328 nntp-server-buffer))
330 (kill-buffer buffer)
331 (set-buffer nntp-server-buffer)
332 (buffer-size)
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.
352 ;;;
354 (defun mhspool-open-server-internal (host &optional service)
355 "Open connection to news server on HOST by SERVICE (default is nntp)."
356 (save-excursion
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))
363 (erase-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)
383 (erase-buffer)
384 (condition-case ()
385 (progn
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")
392 (delete-char 1))
395 (file-error nil)
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))
406 (idx 0))
407 ;; Replace all occurence of FROM with TO.
408 (while (< idx len)
409 (if (= (aref string idx) from)
410 (aset string idx to))
411 (setq idx (1+ idx)))
412 string
415 (provide 'mhspool)
417 ;;; mhspool.el ends here