1 ;;; nnmh.el --- mhspool access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: news, mail
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
30 ;; For an overview of what the interface functions do, please see the
39 (eval-when-compile (require 'cl))
43 (defvoo nnmh-directory message-directory
44 "*Mail spool directory.
46 This variable is a virtual server slot. See the Gnus manual for details.")
48 (defvoo nnmh-get-new-mail t
49 "*If non-nil, nnmh will check the incoming mail file and split the mail.
51 This variable is a virtual server slot. See the Gnus manual for details.")
53 (defvoo nnmh-prepare-save-mail-hook nil
54 "*Hook run narrowed to an article before saving.
56 This variable is a virtual server slot. See the Gnus manual for details.")
58 (defvoo nnmh-be-safe nil
59 "*If non-nil, nnmh will check all articles to make sure whether they are new or not.
60 Go through the .nnmh-articles file and compare with the actual
61 articles in this folder. The articles that are \"new\" will be marked
64 This variable is a virtual server slot. See the Gnus manual for details.")
68 (defconst nnmh-version "nnmh 1.0"
71 (defvoo nnmh-current-directory nil
72 "Current news group directory.")
74 (defvoo nnmh-status-string "")
75 (defvoo nnmh-group-alist nil)
76 ;; Don't even think about setting this variable. It does not exist.
77 ;; Forget about it. Uh-huh. Nope. Nobody here. It's only bound
78 ;; dynamically by certain functions in nndraft.
79 (defvar nnmh-allow-delete-final nil)
83 ;;; Interface functions.
85 (nnoo-define-basics nnmh)
87 (deffoo nnmh-retrieve-headers (articles &optional newsgroup server fetch-old)
89 (set-buffer nntp-server-buffer)
92 (number (length articles))
93 (large (and (numberp nnmail-large-newsgroup)
94 (> number nnmail-large-newsgroup)))
96 (file-name-coding-system nnmail-pathname-coding-system)
98 (nnmh-possibly-change-directory newsgroup server)
99 ;; We don't support fetching by Message-ID.
100 (if (stringp (car articles))
103 (when (and (file-exists-p
104 (setq file (concat (file-name-as-directory
105 nnmh-current-directory)
107 (setq article (pop articles))))))
108 (not (file-directory-p file)))
109 (insert (format "221 %d Article retrieved.\n" article))
111 (nnheader-insert-head file)
113 (if (search-forward "\n\n" nil t)
115 (goto-char (point-max))
118 (delete-region (point) (point-max)))
119 (setq count (1+ count))
123 (nnheader-message 5 "nnmh: Receiving headers... %d%%"
124 (/ (* count 100) number))))
127 (nnheader-message 5 "nnmh: Receiving headers...done"))
129 (nnheader-fold-continuation-lines)
132 (deffoo nnmh-open-server (server &optional defs)
133 (nnoo-change-server 'nnmh server defs)
134 (when (not (file-exists-p nnmh-directory))
136 (make-directory nnmh-directory t)
139 ((not (file-exists-p nnmh-directory))
141 (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
142 ((not (file-directory-p (file-truename nnmh-directory)))
144 (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
146 (nnheader-report 'nnmh "Opened server %s using directory %s"
147 server nnmh-directory)
150 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
151 (nnmh-possibly-change-directory newsgroup server)
152 (let ((file (if (stringp id)
154 (concat nnmh-current-directory (int-to-string id))))
155 (file-name-coding-system nnmail-pathname-coding-system)
156 (nntp-server-buffer (or buffer nntp-server-buffer)))
159 (not (file-directory-p file))
160 (save-excursion (nnmail-find-file file))
161 (string-to-int (file-name-nondirectory file)))))
163 (deffoo nnmh-request-group (group &optional server dont-check)
164 (nnheader-init-server-buffer)
165 (nnmh-possibly-change-directory group server)
166 (let ((pathname (nnmail-group-pathname group nnmh-directory))
167 (file-name-coding-system nnmail-pathname-coding-system)
170 ((not (file-directory-p pathname))
172 'nnmh "Can't select group (no such directory): %s" group))
174 (setq nnmh-current-directory pathname)
175 (and nnmh-get-new-mail
177 (nnmh-update-gnus-unreads group))
180 (nnheader-report 'nnmh "Selected group %s" group)
183 ;; Re-scan the directory if it's on a foreign system.
184 (nnheader-re-read-dir pathname)
187 (mapcar (lambda (name) (string-to-int name))
188 (directory-files pathname nil "^[0-9]+$" t))
192 (setq nnmh-group-alist
193 (delq (assoc group nnmh-group-alist) nnmh-group-alist))
194 (push (list group (cons (car dir) (car (last dir))))
196 (nnheader-report 'nnmh "Selected group %s" group)
198 "211 %d %d %d %s\n" (length dir) (car dir)
199 (car (last dir)) group))
201 (nnheader-report 'nnmh "Empty group %s" group)
202 (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
204 (deffoo nnmh-request-scan (&optional group server)
205 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
207 (deffoo nnmh-request-list (&optional server dir)
209 (nnmh-possibly-change-directory nil server)
210 (let ((file-name-coding-system nnmail-pathname-coding-system)
212 (file-truename (or dir (file-name-as-directory nnmh-directory)))))
213 (nnmh-request-list-1 nnmh-toplev))
214 (setq nnmh-group-alist (nnmail-get-active))
218 (defun nnmh-request-list-1 (dir)
219 (setq dir (expand-file-name dir))
220 ;; Recurse down all directories.
221 (let ((dirs (and (file-readable-p dir)
222 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
223 (nnheader-directory-files dir t nil t)))
225 ;; Recurse down directories.
226 (while (setq rdir (pop dirs))
227 (when (and (file-directory-p rdir)
228 (file-readable-p rdir)
229 (not (equal (file-truename rdir)
230 (file-truename dir))))
231 (nnmh-request-list-1 rdir))))
232 ;; For each directory, generate an active file line.
233 (unless (string= (expand-file-name nnmh-toplev) dir)
235 (lambda (name) (string-to-int name))
236 (directory-files dir nil "^[0-9]+$" t))))
239 (set-buffer nntp-server-buffer)
240 (goto-char (point-max))
247 (file-truename (file-name-as-directory
248 (expand-file-name nnmh-toplev))))
250 (mm-string-as-multibyte
251 (mm-encode-coding-string
252 (nnheader-replace-chars-in-string
253 (substring dir (match-end 0))
255 nnmail-pathname-coding-system)))
257 (apply 'min files)))))))
260 (deffoo nnmh-request-newgroups (date &optional server)
261 (nnmh-request-list server))
263 (deffoo nnmh-request-expire-articles (articles newsgroup
264 &optional server force)
265 (nnmh-possibly-change-directory newsgroup server)
267 article rest mod-time)
268 (nnheader-init-server-buffer)
270 (while (and articles is-old)
271 (setq article (concat nnmh-current-directory
272 (int-to-string (car articles))))
273 (when (setq mod-time (nth 5 (file-attributes article)))
274 (if (and (nnmh-deletable-article-p newsgroup (car articles))
276 (nnmail-expired-article-p newsgroup mod-time force)))
278 ;; Allow a special target group. -- jcn
279 (unless (eq nnmail-expiry-target 'delete)
281 (nnmh-request-article (car articles)
282 newsgroup server (current-buffer))
283 (nnmail-expiry-target-group
284 nnmail-expiry-target newsgroup)))
285 (nnheader-message 5 "Deleting article %s in %s..."
288 (funcall nnmail-delete-file-function article)
290 (nnheader-message 1 "Couldn't delete article %s in %s"
292 (push (car articles) rest))))
293 (push (car articles) rest)))
294 (setq articles (cdr articles)))
295 (nnheader-message 5 "")
296 (nconc rest articles)))
298 (deffoo nnmh-close-group (group &optional server)
301 (deffoo nnmh-request-move-article
302 (article group server accept-form &optional last)
303 (let ((buf (get-buffer-create " *nnmh move*"))
306 (nnmh-deletable-article-p group article)
307 (nnmh-request-article article group server)
311 (insert-buffer-substring nntp-server-buffer)
312 (setq result (eval accept-form))
313 (kill-buffer (current-buffer))
316 (nnmh-possibly-change-directory group server)
318 (funcall nnmail-delete-file-function
319 (concat nnmh-current-directory (int-to-string article)))
323 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
324 (nnmh-possibly-change-directory group server)
325 (nnmail-check-syntax)
326 (when nnmail-cache-accepted-message-ids
327 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
328 (nnheader-init-server-buffer)
332 (nnmh-active-number group)
334 (list (cons group (nnmh-active-number group)))
336 (let ((res (nnmail-article-group 'nnmh-active-number)))
338 (yes-or-no-p "Moved to `junk' group; delete article? "))
340 (car (nnmh-save-mail res noinsert)))))
341 (when (and last nnmail-cache-accepted-message-ids)
342 (nnmail-cache-close))))
344 (deffoo nnmh-request-replace-article (article group buffer)
345 (nnmh-possibly-change-directory group)
348 (nnmh-possibly-create-directory group)
351 (point-min) (point-max)
352 (concat nnmh-current-directory (int-to-string article))
353 nil (if (nnheader-be-verbose 5) nil 'nomesg))
356 (deffoo nnmh-request-create-group (group &optional server args)
357 (nnheader-init-server-buffer)
358 (unless (assoc group nnmh-group-alist)
360 (push (list group (setq active (cons 1 0)))
362 (nnmh-possibly-create-directory group)
363 (nnmh-possibly-change-directory group server)
364 (let ((articles (mapcar
366 (string-to-int file))
368 nnmh-current-directory nil "^[0-9]+$"))))
370 (setcar active (apply 'min articles))
371 (setcdr active (apply 'max articles))))))
374 (deffoo nnmh-request-delete-group (group &optional force server)
375 (nnmh-possibly-change-directory group server)
376 ;; Delete all articles in GROUP.
378 () ; Don't delete the articles.
379 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
381 (when (file-writable-p (car articles))
382 (nnheader-message 5 "Deleting article %s in %s..."
383 (car articles) group)
384 (funcall nnmail-delete-file-function (car articles)))
385 (setq articles (cdr articles))))
386 ;; Try to delete the directory itself.
388 (delete-directory nnmh-current-directory)))
389 ;; Remove the group from all structures.
390 (setq nnmh-group-alist
391 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
392 nnmh-current-directory nil)
395 (deffoo nnmh-request-rename-group (group new-name &optional server)
396 (nnmh-possibly-change-directory group server)
397 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
398 (old-dir (nnmail-group-pathname group nnmh-directory)))
400 (make-directory new-dir t)
402 ;; We move the articles file by file instead of renaming
403 ;; the directory -- there may be subgroups in this group.
404 ;; One might be more clever, I guess.
405 (let ((files (nnheader-article-to-file-alist old-dir)))
408 (concat old-dir (cdar files))
409 (concat new-dir (cdar files)))
411 (when (<= (length (directory-files old-dir)) 2)
413 (delete-directory old-dir)))
414 ;; That went ok, so we change the internal structures.
415 (let ((entry (assoc group nnmh-group-alist)))
417 (setcar entry new-name))
418 (setq nnmh-current-directory nil)
421 (nnoo-define-skeleton nnmh)
424 ;;; Internal functions.
426 (defun nnmh-possibly-change-directory (newsgroup &optional server)
428 (not (nnmh-server-opened server)))
429 (nnmh-open-server server))
431 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory))
432 (file-name-coding-system nnmail-pathname-coding-system))
433 (if (file-directory-p pathname)
434 (setq nnmh-current-directory pathname)
435 (error "No such newsgroup: %s" newsgroup)))))
437 (defun nnmh-possibly-create-directory (group)
439 (setq dir (nnmail-group-pathname group nnmh-directory))
440 (while (not (file-directory-p dir))
442 (setq dir (file-name-directory (directory-file-name dir))))
444 (when (make-directory (directory-file-name (car dirs)))
445 (error "Could not create directory %s" (car dirs)))
446 (nnheader-message 5 "Creating mail directory %s" (car dirs))
447 (setq dirs (cdr dirs)))))
449 (defun nnmh-save-mail (group-art &optional noinsert)
450 "Called narrowed to an article."
452 (nnmail-insert-lines)
453 (nnmail-insert-xref group-art))
454 (run-hooks 'nnmail-prepare-save-mail-hook)
455 (run-hooks 'nnmh-prepare-save-mail-hook)
456 (goto-char (point-min))
457 (while (looking-at "From ")
458 (replace-match "X-From-Line: ")
460 ;; We save the article in all the newsgroups it belongs in.
464 (nnmh-possibly-create-directory (caar ga))
465 (let ((file (concat (nnmail-group-pathname
466 (caar ga) nnmh-directory)
467 (int-to-string (cdar ga)))))
469 ;; It was already saved, so we just make a hard link.
470 (funcall nnmail-crosspost-link-function first file t)
472 (nnmail-write-region (point-min) (point-max) file nil nil)
477 (defun nnmh-active-number (group)
478 "Compute the next article number in GROUP."
479 (let ((active (cadr (assoc group nnmh-group-alist)))
480 (dir (nnmail-group-pathname group nnmh-directory))
481 (file-name-coding-system nnmail-pathname-coding-system)
484 ;; The group wasn't known to nnmh, so we just create an active
486 (setq active (cons 1 0))
487 (push (list group active) nnmh-group-alist)
488 (unless (file-exists-p dir)
489 (gnus-make-directory dir))
490 ;; Find the highest number in the group.
495 (directory-files dir nil "^[0-9]+$"))
498 (setcdr active (car files)))))
499 (setcdr active (1+ (cdr active)))
501 ;; See whether the file exists...
503 (setq file (concat (nnmail-group-pathname group nnmh-directory)
504 (int-to-string (cdr active)))))
505 ;; ... or there is a buffer that will make that file exist
507 (get-file-buffer file))
508 ;; Skip past that file.
509 (setcdr active (1+ (cdr active))))
512 (defun nnmh-update-gnus-unreads (group)
513 ;; Go through the .nnmh-articles file and compare with the actual
514 ;; articles in this folder. The articles that are "new" will be
515 ;; marked as unread by Gnus.
516 (let* ((dir nnmh-current-directory)
517 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
518 (directory-files nnmh-current-directory
521 (nnmh-file (concat dir ".nnmh-articles"))
523 ;; Load the .nnmh-articles file.
524 (when (file-exists-p nnmh-file)
526 (let (nnmh-newsgroup-articles)
527 (ignore-errors (load nnmh-file nil t t))
528 nnmh-newsgroup-articles)))
529 ;; Add all new articles to the `new' list.
532 (unless (assq (car art) articles)
533 (push (car art) new))
534 (setq art (cdr art))))
535 ;; Remove all deleted articles.
536 (let ((art articles))
538 (unless (memq (caar art) files)
539 (setq articles (delq (car art) articles)))
540 (setq art (cdr art))))
541 ;; Check whether the articles really are the ones that Gnus thinks
542 ;; they are by looking at the time-stamps.
543 (let ((arts articles)
545 (while (setq art (pop arts))
547 (nth 5 (file-attributes
548 (concat dir (int-to-string (car art)))))
550 (setq articles (delq art articles))
551 (push (car art) new))))
552 ;; Go through all the new articles and add them, and their
553 ;; time-stamps, to the list.
559 (nth 5 (file-attributes
560 (concat dir (int-to-string art))))))
562 ;; Make Gnus mark all new articles as unread.
564 (gnus-make-articles-unread
565 (gnus-group-prefixed-name group (list 'nnmh ""))
566 (setq new (sort new '<))))
567 ;; Sort the article list with highest numbers first.
568 (setq articles (sort articles (lambda (art1 art2)
569 (> (car art1) (car art2)))))
570 ;; Finally write this list back to the .nnmh-articles file.
571 (with-temp-file nnmh-file
572 (insert ";; Gnus article active file for " group "\n\n")
573 (insert "(setq nnmh-newsgroup-articles '")
574 (gnus-prin1 articles)
577 (defun nnmh-deletable-article-p (group article)
578 "Say whether ARTICLE in GROUP can be deleted."
579 (let ((path (concat nnmh-current-directory (int-to-string article))))
581 (and (file-writable-p path)
583 ;; We can never delete the last article in the group.
584 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
587 nnmh-allow-delete-final))))
591 ;;; nnmh.el ends here