1 ;;; nnmh.el --- mhspool access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;; Keywords: news, mail
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
31 ;; For an overview of what the interface functions do, please see the
36 (eval-when-compile (require 'cl))
37 (eval-when-compile (require 'gnus-clfns))
46 (defvoo nnmh-directory message-directory
47 "*Mail spool directory.
49 This variable is a virtual server slot. See the Gnus manual for details.")
51 (defvoo nnmh-get-new-mail t
52 "*If non-nil, nnmh will check the incoming mail file and split the mail.
54 This variable is a virtual server slot. See the Gnus manual for details.")
56 (defvoo nnmh-prepare-save-mail-hook nil
57 "*Hook run narrowed to an article before saving.
59 This variable is a virtual server slot. See the Gnus manual for details.")
61 (defvoo nnmh-be-safe nil
62 "*If non-nil, nnmh will check all articles to make sure whether they are new or not.
63 Go through the .nnmh-articles file and compare with the actual
64 articles in this folder. The articles that are \"new\" will be marked
67 This variable is a virtual server slot. See the Gnus manual for details.")
71 (defconst nnmh-version "nnmh 1.0"
74 (defvoo nnmh-current-directory nil
75 "Current news group directory.")
77 (defvoo nnmh-status-string "")
78 (defvoo nnmh-group-alist nil)
79 ;; Don't even think about setting this variable. It does not exist.
80 ;; Forget about it. Uh-huh. Nope. Nobody here. It's only bound
81 ;; dynamically by certain functions in nndraft.
82 (defvar nnmh-allow-delete-final nil)
86 ;;; Interface functions.
88 (nnoo-define-basics nnmh)
90 (deffoo nnmh-retrieve-headers (articles &optional newsgroup server fetch-old)
92 (set-buffer nntp-server-buffer)
95 (number (length articles))
96 (large (and (numberp nnmail-large-newsgroup)
97 (> number nnmail-large-newsgroup)))
99 (file-name-coding-system nnmail-pathname-coding-system)
100 (pathname-coding-system nnmail-pathname-coding-system)
102 (nnmh-possibly-change-directory newsgroup server)
103 ;; We don't support fetching by Message-ID.
104 (if (stringp (car articles))
107 (when (and (file-exists-p
108 (setq file (concat (file-name-as-directory
109 nnmh-current-directory)
111 (setq article (pop articles))))))
112 (not (file-directory-p file)))
113 (insert (format "221 %d Article retrieved.\n" article))
115 (nnheader-insert-head file)
117 (if (search-forward "\n\n" nil t)
119 (goto-char (point-max))
122 (delete-region (point) (point-max)))
123 (setq count (1+ count))
127 (nnheader-message 5 "nnmh: Receiving headers... %d%%"
128 (/ (* count 100) number))))
131 (nnheader-message 5 "nnmh: Receiving headers...done"))
133 ;; (nnheader-fold-continuation-lines)
136 (deffoo nnmh-open-server (server &optional defs)
137 (nnoo-change-server 'nnmh server defs)
138 (when (not (file-exists-p nnmh-directory))
140 (make-directory nnmh-directory t)
143 ((not (file-exists-p nnmh-directory))
145 (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
146 ((not (file-directory-p (file-truename nnmh-directory)))
148 (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
150 (nnheader-report 'nnmh "Opened server %s using directory %s"
151 server nnmh-directory)
154 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
155 (nnmh-possibly-change-directory newsgroup server)
156 (let ((file (if (stringp id)
158 (concat nnmh-current-directory (int-to-string id))))
159 (file-name-coding-system nnmail-pathname-coding-system)
160 (pathname-coding-system nnmail-pathname-coding-system)
161 (nntp-server-buffer (or buffer nntp-server-buffer)))
164 (not (file-directory-p file))
165 (save-excursion (nnmail-find-file file))
166 (string-to-int (file-name-nondirectory file)))))
168 (deffoo nnmh-request-group (group &optional server dont-check)
169 (nnheader-init-server-buffer)
170 (nnmh-possibly-change-directory group server)
171 (let ((pathname (nnmail-group-pathname group nnmh-directory))
172 (file-name-coding-system nnmail-pathname-coding-system)
173 (pathname-coding-system nnmail-pathname-coding-system)
176 ((not (file-directory-p pathname))
178 'nnmh "Can't select group (no such directory): %s" group))
180 (setq nnmh-current-directory pathname)
181 (and nnmh-get-new-mail
183 (nnmh-update-gnus-unreads group))
186 (nnheader-report 'nnmh "Selected group %s" group)
189 ;; Re-scan the directory if it's on a foreign system.
190 (nnheader-re-read-dir pathname)
193 (mapcar (lambda (name) (string-to-int name))
194 (directory-files pathname nil "^[0-9]+$" t))
198 (setq nnmh-group-alist
199 (delq (assoc group nnmh-group-alist) nnmh-group-alist))
200 (push (list group (cons (car dir) (car (last dir))))
202 (nnheader-report 'nnmh "Selected group %s" group)
204 "211 %d %d %d %s\n" (length dir) (car dir)
205 (car (last dir)) group))
207 (nnheader-report 'nnmh "Empty group %s" group)
208 (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
210 (deffoo nnmh-request-scan (&optional group server)
211 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
213 (deffoo nnmh-request-list (&optional server dir)
215 (nnmh-possibly-change-directory nil server)
216 (let ((file-name-coding-system nnmail-pathname-coding-system)
217 (pathname-coding-system nnmail-pathname-coding-system)
219 (file-truename (or dir (file-name-as-directory nnmh-directory)))))
220 (nnmh-request-list-1 nnmh-toplev))
221 (setq nnmh-group-alist (nnmail-get-active))
225 (defun nnmh-request-list-1 (dir)
226 (setq dir (expand-file-name dir))
227 ;; Recurse down all directories.
228 (let ((dirs (and (file-readable-p dir)
229 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
230 (nnheader-directory-files dir t nil t)))
232 ;; Recurse down directories.
233 (while (setq rdir (pop dirs))
234 (when (and (file-directory-p rdir)
235 (file-readable-p rdir)
236 (not (equal (file-truename rdir)
237 (file-truename dir))))
238 (nnmh-request-list-1 rdir))))
239 ;; For each directory, generate an active file line.
240 (unless (string= (expand-file-name nnmh-toplev) dir)
242 (lambda (name) (string-to-int name))
243 (directory-files dir nil "^[0-9]+$" t))))
246 (set-buffer nntp-server-buffer)
247 (goto-char (point-max))
254 (file-truename (file-name-as-directory
255 (expand-file-name nnmh-toplev))))
258 (encode-coding-string
259 (nnheader-replace-chars-in-string
260 (substring dir (match-end 0))
262 nnmail-pathname-coding-system)))
264 (apply 'min files)))))))
267 (deffoo nnmh-request-newgroups (date &optional server)
268 (nnmh-request-list server))
270 (deffoo nnmh-request-expire-articles (articles newsgroup
271 &optional server force)
272 (nnmh-possibly-change-directory newsgroup server)
274 article rest mod-time)
275 (nnheader-init-server-buffer)
277 (while (and articles is-old)
278 (setq article (concat nnmh-current-directory
279 (int-to-string (car articles))))
280 (when (setq mod-time (nth 5 (file-attributes article)))
281 (if (and (nnmh-deletable-article-p newsgroup (car articles))
283 (nnmail-expired-article-p newsgroup mod-time force)))
285 ;; Allow a special target group. -- jcn
286 (unless (eq nnmail-expiry-target 'delete)
288 (nnmh-request-article (car articles)
289 newsgroup server (current-buffer))
290 (nnmail-expiry-target-group
291 nnmail-expiry-target newsgroup)))
292 (nnheader-message 5 "Deleting article %s in %s..."
295 (funcall nnmail-delete-file-function article)
297 (nnheader-message 1 "Couldn't delete article %s in %s"
299 (push (car articles) rest))))
300 (push (car articles) rest)))
301 (setq articles (cdr articles)))
302 (nnheader-message 5 "")
303 (nconc rest articles)))
305 (deffoo nnmh-close-group (group &optional server)
308 (deffoo nnmh-request-move-article
309 (article group server accept-form &optional last)
310 (let ((buf (get-buffer-create " *nnmh move*"))
313 (nnmh-deletable-article-p group article)
314 (nnmh-request-article article group server)
318 (insert-buffer-substring nntp-server-buffer)
319 (setq result (eval accept-form))
320 (kill-buffer (current-buffer))
323 (nnmh-possibly-change-directory group server)
325 (funcall nnmail-delete-file-function
326 (concat nnmh-current-directory (int-to-string article)))
330 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
331 (nnmh-possibly-change-directory group server)
332 (if (and (not (equal group "queue"))
333 (not (equal group "draft")))
334 (nnmail-check-syntax))
335 (when nnmail-cache-accepted-message-ids
336 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
337 (nnheader-init-server-buffer)
341 (nnmh-active-number group)
343 (list (cons group (nnmh-active-number group)))
345 (let ((res (nnmail-article-group 'nnmh-active-number)))
347 (yes-or-no-p "Moved to `junk' group; delete article? "))
349 (car (nnmh-save-mail res noinsert)))))
350 (when (and last nnmail-cache-accepted-message-ids)
351 (nnmail-cache-close))))
353 (deffoo nnmh-request-replace-article (article group buffer)
354 (nnmh-possibly-change-directory group)
357 (nnmh-possibly-create-directory group)
360 (point-min) (point-max)
361 (concat nnmh-current-directory (int-to-string article))
362 nil (if (nnheader-be-verbose 5) nil 'nomesg))
365 (deffoo nnmh-request-create-group (group &optional server args)
366 (nnheader-init-server-buffer)
367 (unless (assoc group nnmh-group-alist)
369 (push (list group (setq active (cons 1 0)))
371 (nnmh-possibly-create-directory group)
372 (nnmh-possibly-change-directory group server)
373 (let ((articles (mapcar
375 (string-to-int file))
377 nnmh-current-directory nil "^[0-9]+$"))))
379 (setcar active (apply 'min articles))
380 (setcdr active (apply 'max articles))))))
383 (deffoo nnmh-request-delete-group (group &optional force server)
384 (nnmh-possibly-change-directory group server)
385 ;; Delete all articles in GROUP.
387 () ; Don't delete the articles.
388 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
390 (when (file-writable-p (car articles))
391 (nnheader-message 5 "Deleting article %s in %s..."
392 (car articles) group)
393 (funcall nnmail-delete-file-function (car articles)))
394 (setq articles (cdr articles))))
395 ;; Try to delete the directory itself.
397 (delete-directory nnmh-current-directory)))
398 ;; Remove the group from all structures.
399 (setq nnmh-group-alist
400 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
401 nnmh-current-directory nil)
404 (deffoo nnmh-request-rename-group (group new-name &optional server)
405 (nnmh-possibly-change-directory group server)
406 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
407 (old-dir (nnmail-group-pathname group nnmh-directory)))
409 (make-directory new-dir t)
411 ;; We move the articles file by file instead of renaming
412 ;; the directory -- there may be subgroups in this group.
413 ;; One might be more clever, I guess.
414 (let ((files (nnheader-article-to-file-alist old-dir)))
417 (concat old-dir (cdar files))
418 (concat new-dir (cdar files)))
420 (when (<= (length (directory-files old-dir)) 2)
422 (delete-directory old-dir)))
423 ;; That went ok, so we change the internal structures.
424 (let ((entry (assoc group nnmh-group-alist)))
426 (setcar entry new-name))
427 (setq nnmh-current-directory nil)
430 (nnoo-define-skeleton nnmh)
433 ;;; Internal functions.
435 (defun nnmh-possibly-change-directory (newsgroup &optional server)
437 (not (nnmh-server-opened server)))
438 (nnmh-open-server server))
440 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory))
441 (file-name-coding-system nnmail-pathname-coding-system)
442 (pathname-coding-system nnmail-pathname-coding-system))
443 (if (file-directory-p pathname)
444 (setq nnmh-current-directory pathname)
445 (error "No such newsgroup: %s" newsgroup)))))
447 (defun nnmh-possibly-create-directory (group)
449 (setq dir (nnmail-group-pathname group nnmh-directory))
450 (while (not (file-directory-p dir))
452 (setq dir (file-name-directory (directory-file-name dir))))
454 (when (make-directory (directory-file-name (car dirs)))
455 (error "Could not create directory %s" (car dirs)))
456 (nnheader-message 5 "Creating mail directory %s" (car dirs))
457 (setq dirs (cdr dirs)))))
459 (defun nnmh-save-mail (group-art &optional noinsert)
460 "Called narrowed to an article."
462 (nnmail-insert-lines)
463 (nnmail-insert-xref group-art))
464 (run-hooks 'nnmail-prepare-save-mail-hook)
465 (run-hooks 'nnmh-prepare-save-mail-hook)
466 (goto-char (point-min))
467 (while (looking-at "From ")
468 (replace-match "X-From-Line: ")
470 ;; We save the article in all the newsgroups it belongs in.
474 (nnmh-possibly-create-directory (caar ga))
475 (let ((file (concat (nnmail-group-pathname
476 (caar ga) nnmh-directory)
477 (int-to-string (cdar ga)))))
479 ;; It was already saved, so we just make a hard link.
480 (funcall nnmail-crosspost-link-function first file t)
482 (nnmail-write-region (point-min) (point-max) file nil nil)
487 (defun nnmh-active-number (group)
488 "Compute the next article number in GROUP."
489 (let ((active (cadr (assoc group nnmh-group-alist)))
490 (dir (nnmail-group-pathname group nnmh-directory))
491 (file-name-coding-system nnmail-pathname-coding-system)
492 (pathname-coding-system nnmail-pathname-coding-system)
495 ;; The group wasn't known to nnmh, so we just create an active
497 (setq active (cons 1 0))
498 (push (list group active) nnmh-group-alist)
499 (unless (file-exists-p dir)
500 (gnus-make-directory dir))
501 ;; Find the highest number in the group.
506 (directory-files dir nil "^[0-9]+$"))
509 (setcdr active (car files)))))
510 (setcdr active (1+ (cdr active)))
512 ;; See whether the file exists...
514 (setq file (concat (nnmail-group-pathname group nnmh-directory)
515 (int-to-string (cdr active)))))
516 ;; ... or there is a buffer that will make that file exist
518 (get-file-buffer file))
519 ;; Skip past that file.
520 (setcdr active (1+ (cdr active))))
523 (defun nnmh-update-gnus-unreads (group)
524 ;; Go through the .nnmh-articles file and compare with the actual
525 ;; articles in this folder. The articles that are "new" will be
526 ;; marked as unread by Gnus.
527 (let* ((dir nnmh-current-directory)
528 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
529 (directory-files nnmh-current-directory
532 (nnmh-file (concat dir ".nnmh-articles"))
534 ;; Load the .nnmh-articles file.
535 (when (file-exists-p nnmh-file)
537 (let (nnmh-newsgroup-articles)
538 (ignore-errors (load nnmh-file nil t t))
539 nnmh-newsgroup-articles)))
540 ;; Add all new articles to the `new' list.
543 (unless (assq (car art) articles)
544 (push (car art) new))
545 (setq art (cdr art))))
546 ;; Remove all deleted articles.
547 (let ((art articles))
549 (unless (memq (caar art) files)
550 (setq articles (delq (car art) articles)))
551 (setq art (cdr art))))
552 ;; Check whether the articles really are the ones that Gnus thinks
553 ;; they are by looking at the time-stamps.
554 (let ((arts articles)
556 (while (setq art (pop arts))
558 (nth 5 (file-attributes
559 (concat dir (int-to-string (car art)))))
561 (setq articles (delq art articles))
562 (push (car art) new))))
563 ;; Go through all the new articles and add them, and their
564 ;; time-stamps, to the list.
570 (nth 5 (file-attributes
571 (concat dir (int-to-string art))))))
573 ;; Make Gnus mark all new articles as unread.
575 (gnus-make-articles-unread
576 (gnus-group-prefixed-name group (list 'nnmh ""))
577 (setq new (sort new '<))))
578 ;; Sort the article list with highest numbers first.
579 (setq articles (sort articles (lambda (art1 art2)
580 (> (car art1) (car art2)))))
581 ;; Finally write this list back to the .nnmh-articles file.
582 (with-temp-file nnmh-file
583 (insert ";; Gnus article active file for " group "\n\n")
584 (insert "(setq nnmh-newsgroup-articles '")
585 (gnus-prin1 articles)
588 (defun nnmh-deletable-article-p (group article)
589 "Say whether ARTICLE in GROUP can be deleted."
590 (let ((path (concat nnmh-current-directory (int-to-string article))))
592 (and (file-writable-p path)
594 ;; We can never delete the last article in the group.
595 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
598 nnmh-allow-delete-final))))
602 ;;; nnmh.el ends here