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 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
28 ;; For an overview of what the interface functions do, please see the
37 (eval-when-compile (require 'cl))
41 (defvoo nnmh-directory message-directory
42 "*Mail spool directory.")
44 (defvoo nnmh-get-new-mail t
45 "*If non-nil, nnmh will check the incoming mail file and split the mail.")
47 (defvoo nnmh-prepare-save-mail-hook nil
48 "*Hook run narrowed to an article before saving.")
50 (defvoo nnmh-be-safe nil
51 "*If non-nil, nnmh will check all articles to make sure whether they are new or not.")
55 (defconst nnmh-version "nnmh 1.0"
58 (defvoo nnmh-current-directory nil
59 "Current news group directory.")
61 (defvoo nnmh-status-string "")
62 (defvoo nnmh-group-alist nil)
63 (defvoo nnmh-allow-delete-final nil)
67 ;;; Interface functions.
69 (nnoo-define-basics nnmh)
71 (deffoo nnmh-retrieve-headers (articles &optional newsgroup server fetch-old)
73 (set-buffer nntp-server-buffer)
76 (number (length articles))
77 (large (and (numberp nnmail-large-newsgroup)
78 (> number nnmail-large-newsgroup)))
80 (pathname-coding-system 'binary)
82 (nnmh-possibly-change-directory newsgroup server)
83 ;; We don't support fetching by Message-ID.
84 (if (stringp (car articles))
87 (when (and (file-exists-p
88 (setq file (concat (file-name-as-directory
89 nnmh-current-directory)
91 (setq article (pop articles))))))
92 (not (file-directory-p file)))
93 (insert (format "221 %d Article retrieved.\n" article))
95 (nnheader-insert-head file)
97 (if (search-forward "\n\n" nil t)
99 (goto-char (point-max))
102 (delete-region (point) (point-max)))
103 (setq count (1+ count))
107 (nnheader-message 5 "nnmh: Receiving headers... %d%%"
108 (/ (* count 100) number))))
111 (nnheader-message 5 "nnmh: Receiving headers...done"))
113 ;; (nnheader-fold-continuation-lines)
116 (deffoo nnmh-retrieve-parsed-headers (articles
118 &optional newsgroup server fetch-old
121 (set-buffer nntp-server-buffer)
123 (number (length articles))
124 (large (and (numberp nnmail-large-newsgroup)
125 (> number nnmail-large-newsgroup)))
127 (pathname-coding-system 'binary)
131 headers header id end ref lines chars ctype in-reply-to
132 (cur (current-buffer)))
133 (nnmh-possibly-change-directory newsgroup server)
134 ;; We don't support fetching by Message-ID.
135 (if (stringp (car articles))
138 (when (and (file-exists-p
139 (setq file (concat (file-name-as-directory
140 nnmh-current-directory)
142 (setq article (pop articles))))))
143 (not (file-directory-p file)))
144 ;;(insert (format "221 %d Article retrieved.\n" article))
147 (nnheader-insert-head file)
149 (std11-narrow-to-header)
152 (make-full-mail-header
156 (or (std11-fetch-field "Subject")
159 (or (std11-fetch-field "From")
162 (or (std11-fetch-field "Date")
166 (goto-char (point-min))
167 (setq id (if (re-search-forward
168 "^Message-ID: *\\(<[^\n\t> ]+>\\)" nil t)
169 ;; We do it this way to make sure the Message-ID
170 ;; is (somewhat) syntactically valid.
171 (buffer-substring (match-beginning 1)
173 ;; If there was no message-id, we just fake one
174 ;; to make subsequent routines simpler.
175 (nnheader-generate-fake-message-id))))
178 (goto-char (point-min))
179 (if (search-forward "\nReferences: " nil t)
183 (buffer-substring (match-end 0) (std11-field-end))
188 (search-backward ">" end t)
191 (search-backward "<" end t)
193 ;; Get the references from the in-reply-to header if there
194 ;; were no references and the in-reply-to header looks
196 (if (and (search-forward "\nIn-Reply-To: " nil t)
198 (buffer-substring (match-end 0)
200 (string-match "<[^>]+>" in-reply-to))
202 (setq ref (substring in-reply-to (match-beginning 0)
204 (while (string-match "<[^>]+>"
205 in-reply-to (match-end 0))
207 (substring in-reply-to (match-beginning 0)
209 (when (> (length ref2) (length ref))
215 (goto-char (point-min))
216 (if (search-forward "\nChars: " nil t)
217 (if (numberp (setq chars (ignore-errors (read cur))))
222 (goto-char (point-min))
223 (if (search-forward "\nLines: " nil t)
224 (if (numberp (setq lines (ignore-errors (read cur))))
228 (std11-fetch-field "Xref")
230 (goto-char (point-min))
231 (if (setq ctype (std11-fetch-field "Content-Type"))
232 (mime-entity-set-content-type-internal
233 header (mime-parse-Content-Type ctype)))
236 (gnus-dependencies-add-header
237 header dependencies force-new))
238 (push header headers))
240 (setq count (1+ count))
244 (nnheader-message 5 "nnmh: Receiving headers... %d%%"
245 (/ (* count 100) number))))
248 (nnheader-message 5 "nnmh: Receiving headers...done"))
250 ;; (nnheader-fold-continuation-lines)
251 (cons 'header (nreverse headers))
254 (deffoo nnmh-open-server (server &optional defs)
255 (nnoo-change-server 'nnmh server defs)
256 (when (not (file-exists-p nnmh-directory))
258 (make-directory nnmh-directory t)
261 ((not (file-exists-p nnmh-directory))
263 (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
264 ((not (file-directory-p (file-truename nnmh-directory)))
266 (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
268 (nnheader-report 'nnmh "Opened server %s using directory %s"
269 server nnmh-directory)
272 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
273 (nnmh-possibly-change-directory newsgroup server)
274 (let ((file (if (stringp id)
276 (concat nnmh-current-directory (int-to-string id))))
277 (pathname-coding-system 'binary)
278 (nntp-server-buffer (or buffer nntp-server-buffer)))
281 (not (file-directory-p file))
282 (save-excursion (nnmail-find-file file))
283 (string-to-int (file-name-nondirectory file)))))
285 (deffoo nnmh-request-group (group &optional server dont-check)
286 (nnheader-init-server-buffer)
287 (nnmh-possibly-change-directory group server)
288 (let ((pathname (nnmail-group-pathname group nnmh-directory))
289 (pathname-coding-system 'binary)
292 ((not (file-directory-p pathname))
294 'nnmh "Can't select group (no such directory): %s" group))
296 (setq nnmh-current-directory pathname)
297 (and nnmh-get-new-mail
299 (nnmh-update-gnus-unreads group))
302 (nnheader-report 'nnmh "Selected group %s" group)
305 ;; Re-scan the directory if it's on a foreign system.
306 (nnheader-re-read-dir pathname)
309 (mapcar (lambda (name) (string-to-int name))
310 (directory-files pathname nil "^[0-9]+$" t))
314 (nnheader-report 'nnmh "Selected group %s" group)
316 "211 %d %d %d %s\n" (length dir) (car dir)
317 (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
320 (nnheader-report 'nnmh "Empty group %s" group)
321 (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
323 (deffoo nnmh-request-scan (&optional group server)
324 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
326 (deffoo nnmh-request-list (&optional server dir)
328 (nnmh-possibly-change-directory nil server)
329 (let ((pathname-coding-system 'binary)
331 (file-truename (or dir (file-name-as-directory nnmh-directory)))))
332 (nnmh-request-list-1 nnmh-toplev))
333 (setq nnmh-group-alist (nnmail-get-active))
337 (defun nnmh-request-list-1 (dir)
338 (setq dir (expand-file-name dir))
339 ;; Recurse down all directories.
340 (let ((dirs (and (file-readable-p dir)
341 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
342 (nnheader-directory-files dir t nil t)))
344 ;; Recurse down directories.
345 (while (setq rdir (pop dirs))
346 (when (and (file-directory-p rdir)
347 (file-readable-p rdir)
348 (not (equal (file-truename rdir)
349 (file-truename dir))))
350 (nnmh-request-list-1 rdir))))
351 ;; For each directory, generate an active file line.
352 (unless (string= (expand-file-name nnmh-toplev) dir)
354 (lambda (name) (string-to-int name))
355 (directory-files dir nil "^[0-9]+$" t))))
358 (set-buffer nntp-server-buffer)
359 (goto-char (point-max))
366 (file-truename (file-name-as-directory
367 (expand-file-name nnmh-toplev))))
369 (nnheader-replace-chars-in-string
370 (decode-coding-string (substring dir (match-end 0))
371 nnmail-pathname-coding-system)
374 (apply 'min files)))))))
377 (deffoo nnmh-request-newgroups (date &optional server)
378 (nnmh-request-list server))
380 (deffoo nnmh-request-expire-articles (articles newsgroup
381 &optional server force)
382 (nnmh-possibly-change-directory newsgroup server)
384 article rest mod-time)
385 (nnheader-init-server-buffer)
387 (while (and articles is-old)
388 (setq article (concat nnmh-current-directory
389 (int-to-string (car articles))))
390 (when (setq mod-time (nth 5 (file-attributes article)))
391 (if (and (nnmh-deletable-article-p newsgroup (car articles))
393 (nnmail-expired-article-p newsgroup mod-time force)))
395 (nnheader-message 5 "Deleting article %s in %s..."
398 (funcall nnmail-delete-file-function article)
400 (nnheader-message 1 "Couldn't delete article %s in %s"
402 (push (car articles) rest))))
403 (push (car articles) rest)))
404 (setq articles (cdr articles)))
405 (nnheader-message 5 "")
406 (nconc rest articles)))
408 (deffoo nnmh-close-group (group &optional server)
411 (deffoo nnmh-request-move-article
412 (article group server accept-form &optional last)
413 (let ((buf (get-buffer-create " *nnmh move*"))
416 (nnmh-deletable-article-p group article)
417 (nnmh-request-article article group server)
421 (insert-buffer-substring nntp-server-buffer)
422 (setq result (eval accept-form))
423 (kill-buffer (current-buffer))
426 (nnmh-possibly-change-directory group server)
428 (funcall nnmail-delete-file-function
429 (concat nnmh-current-directory (int-to-string article)))
433 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
434 (nnmh-possibly-change-directory group server)
435 (if (and (not (equal group "queue"))
436 (not (equal group "draft")))
437 (nnmail-check-syntax))
438 (when nnmail-cache-accepted-message-ids
439 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
440 (nnheader-init-server-buffer)
444 (nnmh-active-number group)
446 (list (cons group (nnmh-active-number group)))
448 (let ((res (nnmail-article-group 'nnmh-active-number)))
450 (yes-or-no-p "Moved to `junk' group; delete article? "))
452 (car (nnmh-save-mail res noinsert)))))
453 (when (and last nnmail-cache-accepted-message-ids)
454 (nnmail-cache-close))))
456 (deffoo nnmh-request-replace-article (article group buffer)
457 (nnmh-possibly-change-directory group)
460 (nnmh-possibly-create-directory group)
463 (point-min) (point-max)
464 (concat nnmh-current-directory (int-to-string article))
465 nil (if (nnheader-be-verbose 5) nil 'nomesg))
468 (deffoo nnmh-request-create-group (group &optional server args)
469 (nnheader-init-server-buffer)
470 (unless (assoc group nnmh-group-alist)
472 (push (list group (setq active (cons 1 0)))
474 (nnmh-possibly-create-directory group)
475 (nnmh-possibly-change-directory group server)
476 (let ((articles (mapcar
478 (string-to-int file))
480 nnmh-current-directory nil "^[0-9]+$"))))
482 (setcar active (apply 'min articles))
483 (setcdr active (apply 'max articles))))))
486 (deffoo nnmh-request-delete-group (group &optional force server)
487 (nnmh-possibly-change-directory group server)
488 ;; Delete all articles in GROUP.
490 () ; Don't delete the articles.
491 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
493 (when (file-writable-p (car articles))
494 (nnheader-message 5 "Deleting article %s in %s..."
495 (car articles) group)
496 (funcall nnmail-delete-file-function (car articles)))
497 (setq articles (cdr articles))))
498 ;; Try to delete the directory itself.
500 (delete-directory nnmh-current-directory)))
501 ;; Remove the group from all structures.
502 (setq nnmh-group-alist
503 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
504 nnmh-current-directory nil)
507 (deffoo nnmh-request-rename-group (group new-name &optional server)
508 (nnmh-possibly-change-directory group server)
509 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
510 (old-dir (nnmail-group-pathname group nnmh-directory)))
512 (make-directory new-dir t)
514 ;; We move the articles file by file instead of renaming
515 ;; the directory -- there may be subgroups in this group.
516 ;; One might be more clever, I guess.
517 (let ((files (nnheader-article-to-file-alist old-dir)))
520 (concat old-dir (cdar files))
521 (concat new-dir (cdar files)))
523 (when (<= (length (directory-files old-dir)) 2)
525 (delete-directory old-dir)))
526 ;; That went ok, so we change the internal structures.
527 (let ((entry (assoc group nnmh-group-alist)))
529 (setcar entry new-name))
530 (setq nnmh-current-directory nil)
533 (nnoo-define-skeleton nnmh)
536 ;;; Internal functions.
538 (defun nnmh-possibly-change-directory (newsgroup &optional server)
540 (not (nnmh-server-opened server)))
541 (nnmh-open-server server))
543 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory))
544 (pathname-coding-system 'binary))
545 (if (file-directory-p pathname)
546 (setq nnmh-current-directory pathname)
547 (error "No such newsgroup: %s" newsgroup)))))
549 (defun nnmh-possibly-create-directory (group)
551 (setq dir (nnmail-group-pathname group nnmh-directory))
552 (while (not (file-directory-p dir))
554 (setq dir (file-name-directory (directory-file-name dir))))
556 (when (make-directory (directory-file-name (car dirs)))
557 (error "Could not create directory %s" (car dirs)))
558 (nnheader-message 5 "Creating mail directory %s" (car dirs))
559 (setq dirs (cdr dirs)))))
561 (defun nnmh-save-mail (group-art &optional noinsert)
562 "Called narrowed to an article."
564 (nnmail-insert-lines)
565 (nnmail-insert-xref group-art))
566 (run-hooks 'nnmail-prepare-save-mail-hook)
567 (run-hooks 'nnmh-prepare-save-mail-hook)
568 (goto-char (point-min))
569 (while (looking-at "From ")
570 (replace-match "X-From-Line: ")
572 ;; We save the article in all the newsgroups it belongs in.
576 (nnmh-possibly-create-directory (caar ga))
577 (let ((file (concat (nnmail-group-pathname
578 (caar ga) nnmh-directory)
579 (int-to-string (cdar ga)))))
581 ;; It was already saved, so we just make a hard link.
582 (funcall nnmail-crosspost-link-function first file t)
584 (nnmail-write-region (point-min) (point-max) file nil nil)
589 (defun nnmh-active-number (group)
590 "Compute the next article number in GROUP."
591 (let ((active (cadr (assoc group nnmh-group-alist)))
592 (dir (nnmail-group-pathname group nnmh-directory))
593 (pathname-coding-system 'binary))
595 ;; The group wasn't known to nnmh, so we just create an active
597 (setq active (cons 1 0))
598 (push (list group active) nnmh-group-alist)
599 (unless (file-exists-p dir)
600 (gnus-make-directory dir))
601 ;; Find the highest number in the group.
606 (directory-files dir nil "^[0-9]+$"))
609 (setcdr active (car files)))))
610 (setcdr active (1+ (cdr active)))
611 (while (file-exists-p
612 (concat (nnmail-group-pathname group nnmh-directory)
613 (int-to-string (cdr active))))
614 (setcdr active (1+ (cdr active))))
617 (defun nnmh-update-gnus-unreads (group)
618 ;; Go through the .nnmh-articles file and compare with the actual
619 ;; articles in this folder. The articles that are "new" will be
620 ;; marked as unread by Gnus.
621 (let* ((dir nnmh-current-directory)
622 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
623 (directory-files nnmh-current-directory
626 (nnmh-file (concat dir ".nnmh-articles"))
628 ;; Load the .nnmh-articles file.
629 (when (file-exists-p nnmh-file)
631 (let (nnmh-newsgroup-articles)
632 (ignore-errors (load nnmh-file nil t t))
633 nnmh-newsgroup-articles)))
634 ;; Add all new articles to the `new' list.
637 (unless (assq (car art) articles)
638 (push (car art) new))
639 (setq art (cdr art))))
640 ;; Remove all deleted articles.
641 (let ((art articles))
643 (unless (memq (caar art) files)
644 (setq articles (delq (car art) articles)))
645 (setq art (cdr art))))
646 ;; Check whether the articles really are the ones that Gnus thinks
647 ;; they are by looking at the time-stamps.
648 (let ((arts articles)
650 (while (setq art (pop arts))
652 (nth 5 (file-attributes
653 (concat dir (int-to-string (car art)))))
655 (setq articles (delq art articles))
656 (push (car art) new))))
657 ;; Go through all the new articles and add them, and their
658 ;; time-stamps, to the list.
664 (nth 5 (file-attributes
665 (concat dir (int-to-string art))))))
667 ;; Make Gnus mark all new articles as unread.
669 (gnus-make-articles-unread
670 (gnus-group-prefixed-name group (list 'nnmh ""))
671 (setq new (sort new '<))))
672 ;; Sort the article list with highest numbers first.
673 (setq articles (sort articles (lambda (art1 art2)
674 (> (car art1) (car art2)))))
675 ;; Finally write this list back to the .nnmh-articles file.
676 (with-temp-file nnmh-file
677 (insert ";; Gnus article active file for " group "\n\n")
678 (insert "(setq nnmh-newsgroup-articles '")
679 (gnus-prin1 articles)
682 (defun nnmh-deletable-article-p (group article)
683 "Say whether ARTICLE in GROUP can be deleted."
684 (let ((path (concat nnmh-current-directory (int-to-string article))))
686 (and (file-writable-p path)
688 ;; We can never delete the last article in the group.
689 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
692 nnmh-allow-delete-final))))
696 ;;; nnmh.el ends here