1 ;;; nnmh.el --- mhspool access for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Keywords: news, mail
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 the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
29 ;; For an overview of what the interface functions do, please see the
34 (eval-when-compile (require 'cl))
35 (eval-when-compile (require 'gnus-clfns))
44 (defvoo nnmh-directory message-directory
45 "*Mail spool directory.")
47 (defvoo nnmh-get-new-mail t
48 "*If non-nil, nnmh will check the incoming mail file and split the mail.")
50 (defvoo nnmh-prepare-save-mail-hook nil
51 "*Hook run narrowed to an article before saving.")
53 (defvoo nnmh-be-safe nil
54 "*If non-nil, nnmh will check all articles to make sure whether they are new or not.
55 Go through the .nnmh-articles file and compare with the actual
56 articles in this folder. The articles that are \"new\" will be marked
61 (defconst nnmh-version "nnmh 1.0"
64 (defvoo nnmh-current-directory nil
65 "Current news group directory.")
67 (defvoo nnmh-status-string "")
68 (defvoo nnmh-group-alist nil)
69 ;; Don't even think about setting this variable. It does not exist.
70 ;; Forget about it. Uh-huh. Nope. Nobody here. It's only bound
71 ;; dynamically by certain functions in nndraft.
72 (defvar nnmh-allow-delete-final nil)
76 ;;; Interface functions.
78 (nnoo-define-basics nnmh)
80 (deffoo nnmh-retrieve-headers (articles &optional newsgroup server fetch-old)
82 (set-buffer nntp-server-buffer)
85 (number (length articles))
86 (large (and (numberp nnmail-large-newsgroup)
87 (> number nnmail-large-newsgroup)))
89 (file-name-coding-system nnmail-pathname-coding-system)
90 (pathname-coding-system nnmail-pathname-coding-system)
92 (nnmh-possibly-change-directory newsgroup server)
93 ;; We don't support fetching by Message-ID.
94 (if (stringp (car articles))
97 (when (and (file-exists-p
98 (setq file (concat (file-name-as-directory
99 nnmh-current-directory)
101 (setq article (pop articles))))))
102 (not (file-directory-p file)))
103 (insert (format "221 %d Article retrieved.\n" article))
105 (nnheader-insert-head file)
107 (if (search-forward "\n\n" nil t)
109 (goto-char (point-max))
112 (delete-region (point) (point-max)))
113 (setq count (1+ count))
117 (nnheader-message 5 "nnmh: Receiving headers... %d%%"
118 (/ (* count 100) number))))
121 (nnheader-message 5 "nnmh: Receiving headers...done"))
123 ;; (nnheader-fold-continuation-lines)
126 (deffoo nnmh-retrieve-parsed-headers (articles
128 &optional newsgroup server fetch-old
131 (set-buffer nntp-server-buffer)
133 (number (length articles))
134 (large (and (numberp nnmail-large-newsgroup)
135 (> number nnmail-large-newsgroup)))
137 (file-name-coding-system 'binary)
138 (pathname-coding-system 'binary)
142 headers header id end ref lines chars ctype in-reply-to
143 (cur (current-buffer)))
144 (nnmh-possibly-change-directory newsgroup server)
145 ;; We don't support fetching by Message-ID.
146 (nnheader-retrieve-headers-from-directory
147 articles nnmh-current-directory dependencies
148 fetch-old force-new large "nnmh")
151 (deffoo nnmh-open-server (server &optional defs)
152 (nnoo-change-server 'nnmh server defs)
153 (when (not (file-exists-p nnmh-directory))
155 (make-directory nnmh-directory t)
158 ((not (file-exists-p nnmh-directory))
160 (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
161 ((not (file-directory-p (file-truename nnmh-directory)))
163 (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
165 (nnheader-report 'nnmh "Opened server %s using directory %s"
166 server nnmh-directory)
169 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
170 (nnmh-possibly-change-directory newsgroup server)
171 (let ((file (if (stringp id)
173 (concat nnmh-current-directory (int-to-string id))))
174 (file-name-coding-system nnmail-pathname-coding-system)
175 (pathname-coding-system nnmail-pathname-coding-system)
176 (nntp-server-buffer (or buffer nntp-server-buffer)))
179 (not (file-directory-p file))
180 (save-excursion (nnmail-find-file file))
181 (string-to-int (file-name-nondirectory file)))))
183 (deffoo nnmh-request-group (group &optional server dont-check)
184 (nnheader-init-server-buffer)
185 (nnmh-possibly-change-directory group server)
186 (let ((pathname (nnmail-group-pathname group nnmh-directory))
187 (file-name-coding-system nnmail-pathname-coding-system)
188 (pathname-coding-system nnmail-pathname-coding-system)
191 ((not (file-directory-p pathname))
193 'nnmh "Can't select group (no such directory): %s" group))
195 (setq nnmh-current-directory pathname)
196 (and nnmh-get-new-mail
198 (nnmh-update-gnus-unreads group))
201 (nnheader-report 'nnmh "Selected group %s" group)
204 ;; Re-scan the directory if it's on a foreign system.
205 (nnheader-re-read-dir pathname)
208 (mapcar (lambda (name) (string-to-int name))
209 (directory-files pathname nil "^[0-9]+$" t))
213 (setq nnmh-group-alist
214 (delq (assoc group nnmh-group-alist) nnmh-group-alist))
215 (push (list group (cons (car dir) (car (last dir))))
217 (nnheader-report 'nnmh "Selected group %s" group)
219 "211 %d %d %d %s\n" (length dir) (car dir)
220 (car (last dir)) group))
222 (nnheader-report 'nnmh "Empty group %s" group)
223 (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
225 (deffoo nnmh-request-scan (&optional group server)
226 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
228 (deffoo nnmh-request-list (&optional server dir)
230 (nnmh-possibly-change-directory nil server)
231 (let ((file-name-coding-system nnmail-pathname-coding-system)
232 (pathname-coding-system nnmail-pathname-coding-system)
234 (file-truename (or dir (file-name-as-directory nnmh-directory)))))
235 (nnmh-request-list-1 nnmh-toplev))
236 (setq nnmh-group-alist (nnmail-get-active))
240 (defun nnmh-request-list-1 (dir)
241 (setq dir (expand-file-name dir))
242 ;; Recurse down all directories.
243 (let ((dirs (and (file-readable-p dir)
244 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
245 (nnheader-directory-files dir t nil t)))
247 ;; Recurse down directories.
248 (while (setq rdir (pop dirs))
249 (when (and (file-directory-p rdir)
250 (file-readable-p rdir)
251 (not (equal (file-truename rdir)
252 (file-truename dir))))
253 (nnmh-request-list-1 rdir))))
254 ;; For each directory, generate an active file line.
255 (unless (string= (expand-file-name nnmh-toplev) dir)
257 (lambda (name) (string-to-int name))
258 (directory-files dir nil "^[0-9]+$" t))))
261 (set-buffer nntp-server-buffer)
262 (goto-char (point-max))
269 (file-truename (file-name-as-directory
270 (expand-file-name nnmh-toplev))))
272 (nnheader-replace-chars-in-string
273 (decode-coding-string (substring dir (match-end 0))
274 nnmail-pathname-coding-system)
277 (apply 'min files)))))))
280 (deffoo nnmh-request-newgroups (date &optional server)
281 (nnmh-request-list server))
283 (deffoo nnmh-request-expire-articles (articles newsgroup
284 &optional server force)
285 (nnmh-possibly-change-directory newsgroup server)
287 article rest mod-time)
288 (nnheader-init-server-buffer)
290 (while (and articles is-old)
291 (setq article (concat nnmh-current-directory
292 (int-to-string (car articles))))
293 (when (setq mod-time (nth 5 (file-attributes article)))
294 (if (and (nnmh-deletable-article-p newsgroup (car articles))
296 (nnmail-expired-article-p newsgroup mod-time force)))
298 (nnheader-message 5 "Deleting article %s in %s..."
301 (funcall nnmail-delete-file-function article)
303 (nnheader-message 1 "Couldn't delete article %s in %s"
305 (push (car articles) rest))))
306 (push (car articles) rest)))
307 (setq articles (cdr articles)))
308 (nnheader-message 5 "")
309 (nconc rest articles)))
311 (deffoo nnmh-close-group (group &optional server)
314 (deffoo nnmh-request-move-article
315 (article group server accept-form &optional last)
316 (let ((buf (get-buffer-create " *nnmh move*"))
319 (nnmh-deletable-article-p group article)
320 (nnmh-request-article article group server)
324 (insert-buffer-substring nntp-server-buffer)
325 (setq result (eval accept-form))
326 (kill-buffer (current-buffer))
329 (nnmh-possibly-change-directory group server)
331 (funcall nnmail-delete-file-function
332 (concat nnmh-current-directory (int-to-string article)))
336 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
337 (nnmh-possibly-change-directory group server)
338 (if (and (not (equal group "queue"))
339 (not (equal group "draft")))
340 (nnmail-check-syntax))
341 (when nnmail-cache-accepted-message-ids
342 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
343 (nnheader-init-server-buffer)
347 (nnmh-active-number group)
349 (list (cons group (nnmh-active-number group)))
351 (let ((res (nnmail-article-group 'nnmh-active-number)))
353 (yes-or-no-p "Moved to `junk' group; delete article? "))
355 (car (nnmh-save-mail res noinsert)))))
356 (when (and last nnmail-cache-accepted-message-ids)
357 (nnmail-cache-close))))
359 (deffoo nnmh-request-replace-article (article group buffer)
360 (nnmh-possibly-change-directory group)
363 (nnmh-possibly-create-directory group)
366 (point-min) (point-max)
367 (concat nnmh-current-directory (int-to-string article))
368 nil (if (nnheader-be-verbose 5) nil 'nomesg))
371 (deffoo nnmh-request-create-group (group &optional server args)
372 (nnheader-init-server-buffer)
373 (unless (assoc group nnmh-group-alist)
375 (push (list group (setq active (cons 1 0)))
377 (nnmh-possibly-create-directory group)
378 (nnmh-possibly-change-directory group server)
379 (let ((articles (mapcar
381 (string-to-int file))
383 nnmh-current-directory nil "^[0-9]+$"))))
385 (setcar active (apply 'min articles))
386 (setcdr active (apply 'max articles))))))
389 (deffoo nnmh-request-delete-group (group &optional force server)
390 (nnmh-possibly-change-directory group server)
391 ;; Delete all articles in GROUP.
393 () ; Don't delete the articles.
394 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
396 (when (file-writable-p (car articles))
397 (nnheader-message 5 "Deleting article %s in %s..."
398 (car articles) group)
399 (funcall nnmail-delete-file-function (car articles)))
400 (setq articles (cdr articles))))
401 ;; Try to delete the directory itself.
403 (delete-directory nnmh-current-directory)))
404 ;; Remove the group from all structures.
405 (setq nnmh-group-alist
406 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
407 nnmh-current-directory nil)
410 (deffoo nnmh-request-rename-group (group new-name &optional server)
411 (nnmh-possibly-change-directory group server)
412 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
413 (old-dir (nnmail-group-pathname group nnmh-directory)))
415 (make-directory new-dir t)
417 ;; We move the articles file by file instead of renaming
418 ;; the directory -- there may be subgroups in this group.
419 ;; One might be more clever, I guess.
420 (let ((files (nnheader-article-to-file-alist old-dir)))
423 (concat old-dir (cdar files))
424 (concat new-dir (cdar files)))
426 (when (<= (length (directory-files old-dir)) 2)
428 (delete-directory old-dir)))
429 ;; That went ok, so we change the internal structures.
430 (let ((entry (assoc group nnmh-group-alist)))
432 (setcar entry new-name))
433 (setq nnmh-current-directory nil)
436 (nnoo-define-skeleton nnmh)
439 ;;; Internal functions.
441 (defun nnmh-possibly-change-directory (newsgroup &optional server)
443 (not (nnmh-server-opened server)))
444 (nnmh-open-server server))
446 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory))
447 (file-name-coding-system nnmail-pathname-coding-system)
448 (pathname-coding-system nnmail-pathname-coding-system))
449 (if (file-directory-p pathname)
450 (setq nnmh-current-directory pathname)
451 (error "No such newsgroup: %s" newsgroup)))))
453 (defun nnmh-possibly-create-directory (group)
455 (setq dir (nnmail-group-pathname group nnmh-directory))
456 (while (not (file-directory-p dir))
458 (setq dir (file-name-directory (directory-file-name dir))))
460 (when (make-directory (directory-file-name (car dirs)))
461 (error "Could not create directory %s" (car dirs)))
462 (nnheader-message 5 "Creating mail directory %s" (car dirs))
463 (setq dirs (cdr dirs)))))
465 (defun nnmh-save-mail (group-art &optional noinsert)
466 "Called narrowed to an article."
468 (nnmail-insert-lines)
469 (nnmail-insert-xref group-art))
470 (run-hooks 'nnmail-prepare-save-mail-hook)
471 (run-hooks 'nnmh-prepare-save-mail-hook)
472 (goto-char (point-min))
473 (while (looking-at "From ")
474 (replace-match "X-From-Line: ")
476 ;; We save the article in all the newsgroups it belongs in.
480 (nnmh-possibly-create-directory (caar ga))
481 (let ((file (concat (nnmail-group-pathname
482 (caar ga) nnmh-directory)
483 (int-to-string (cdar ga)))))
485 ;; It was already saved, so we just make a hard link.
486 (funcall nnmail-crosspost-link-function first file t)
488 (nnmail-write-region (point-min) (point-max) file nil nil)
493 (defun nnmh-active-number (group)
494 "Compute the next article number in GROUP."
495 (let ((active (cadr (assoc group nnmh-group-alist)))
496 (dir (nnmail-group-pathname group nnmh-directory))
497 (file-name-coding-system nnmail-pathname-coding-system)
498 (pathname-coding-system nnmail-pathname-coding-system)
501 ;; The group wasn't known to nnmh, so we just create an active
503 (setq active (cons 1 0))
504 (push (list group active) nnmh-group-alist)
505 (unless (file-exists-p dir)
506 (gnus-make-directory dir))
507 ;; Find the highest number in the group.
512 (directory-files dir nil "^[0-9]+$"))
515 (setcdr active (car files)))))
516 (setcdr active (1+ (cdr active)))
518 ;; See whether the file exists...
520 (setq file (concat (nnmail-group-pathname group nnmh-directory)
521 (int-to-string (cdr active)))))
522 ;; ... or there is a buffer that will make that file exist
524 (get-file-buffer file))
525 ;; Skip past that file.
526 (setcdr active (1+ (cdr active))))
529 (defun nnmh-update-gnus-unreads (group)
530 ;; Go through the .nnmh-articles file and compare with the actual
531 ;; articles in this folder. The articles that are "new" will be
532 ;; marked as unread by Gnus.
533 (let* ((dir nnmh-current-directory)
534 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
535 (directory-files nnmh-current-directory
538 (nnmh-file (concat dir ".nnmh-articles"))
540 ;; Load the .nnmh-articles file.
541 (when (file-exists-p nnmh-file)
543 (let (nnmh-newsgroup-articles)
544 (ignore-errors (load nnmh-file nil t t))
545 nnmh-newsgroup-articles)))
546 ;; Add all new articles to the `new' list.
549 (unless (assq (car art) articles)
550 (push (car art) new))
551 (setq art (cdr art))))
552 ;; Remove all deleted articles.
553 (let ((art articles))
555 (unless (memq (caar art) files)
556 (setq articles (delq (car art) articles)))
557 (setq art (cdr art))))
558 ;; Check whether the articles really are the ones that Gnus thinks
559 ;; they are by looking at the time-stamps.
560 (let ((arts articles)
562 (while (setq art (pop arts))
564 (nth 5 (file-attributes
565 (concat dir (int-to-string (car art)))))
567 (setq articles (delq art articles))
568 (push (car art) new))))
569 ;; Go through all the new articles and add them, and their
570 ;; time-stamps, to the list.
576 (nth 5 (file-attributes
577 (concat dir (int-to-string art))))))
579 ;; Make Gnus mark all new articles as unread.
581 (gnus-make-articles-unread
582 (gnus-group-prefixed-name group (list 'nnmh ""))
583 (setq new (sort new '<))))
584 ;; Sort the article list with highest numbers first.
585 (setq articles (sort articles (lambda (art1 art2)
586 (> (car art1) (car art2)))))
587 ;; Finally write this list back to the .nnmh-articles file.
588 (with-temp-file nnmh-file
589 (insert ";; Gnus article active file for " group "\n\n")
590 (insert "(setq nnmh-newsgroup-articles '")
591 (gnus-prin1 articles)
594 (defun nnmh-deletable-article-p (group article)
595 "Say whether ARTICLE in GROUP can be deleted."
596 (let ((path (concat nnmh-current-directory (int-to-string article))))
598 (and (file-writable-p path)
600 ;; We can never delete the last article in the group.
601 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
604 nnmh-allow-delete-final))))
608 ;;; nnmh.el ends here