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 (defvoo nnmh-get-new-mail t
50 "*If non-nil, nnmh will check the incoming mail file and split the mail.")
52 (defvoo nnmh-prepare-save-mail-hook nil
53 "*Hook run narrowed to an article before saving.")
55 (defvoo nnmh-be-safe nil
56 "*If non-nil, nnmh will check all articles to make sure whether they are new or not.
57 Go through the .nnmh-articles file and compare with the actual
58 articles in this folder. The articles that are \"new\" will be marked
63 (defconst nnmh-version "nnmh 1.0"
66 (defvoo nnmh-current-directory nil
67 "Current news group directory.")
69 (defvoo nnmh-status-string "")
70 (defvoo nnmh-group-alist nil)
71 ;; Don't even think about setting this variable. It does not exist.
72 ;; Forget about it. Uh-huh. Nope. Nobody here. It's only bound
73 ;; dynamically by certain functions in nndraft.
74 (defvar nnmh-allow-delete-final nil)
78 ;;; Interface functions.
80 (nnoo-define-basics nnmh)
82 (deffoo nnmh-retrieve-headers (articles &optional newsgroup server fetch-old)
84 (set-buffer nntp-server-buffer)
87 (number (length articles))
88 (large (and (numberp nnmail-large-newsgroup)
89 (> number nnmail-large-newsgroup)))
91 (file-name-coding-system nnmail-pathname-coding-system)
92 (pathname-coding-system nnmail-pathname-coding-system)
94 (nnmh-possibly-change-directory newsgroup server)
95 ;; We don't support fetching by Message-ID.
96 (if (stringp (car articles))
99 (when (and (file-exists-p
100 (setq file (concat (file-name-as-directory
101 nnmh-current-directory)
103 (setq article (pop articles))))))
104 (not (file-directory-p file)))
105 (insert (format "221 %d Article retrieved.\n" article))
107 (nnheader-insert-head file)
109 (if (search-forward "\n\n" nil t)
111 (goto-char (point-max))
114 (delete-region (point) (point-max)))
115 (setq count (1+ count))
119 (nnheader-message 5 "nnmh: Receiving headers... %d%%"
120 (/ (* count 100) number))))
123 (nnheader-message 5 "nnmh: Receiving headers...done"))
125 ;; (nnheader-fold-continuation-lines)
128 (deffoo nnmh-retrieve-parsed-headers (articles
130 &optional newsgroup server fetch-old
133 (set-buffer nntp-server-buffer)
135 (number (length articles))
136 (large (and (numberp nnmail-large-newsgroup)
137 (> number nnmail-large-newsgroup)))
139 (file-name-coding-system 'binary)
140 (pathname-coding-system 'binary)
144 headers header id end ref lines chars ctype in-reply-to
145 (cur (current-buffer)))
146 (nnmh-possibly-change-directory newsgroup server)
147 ;; We don't support fetching by Message-ID.
148 (nnheader-retrieve-headers-from-directory
149 articles nnmh-current-directory dependencies
150 fetch-old force-new large "nnmh")
153 (deffoo nnmh-open-server (server &optional defs)
154 (nnoo-change-server 'nnmh server defs)
155 (when (not (file-exists-p nnmh-directory))
157 (make-directory nnmh-directory t)
160 ((not (file-exists-p nnmh-directory))
162 (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
163 ((not (file-directory-p (file-truename nnmh-directory)))
165 (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
167 (nnheader-report 'nnmh "Opened server %s using directory %s"
168 server nnmh-directory)
171 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
172 (nnmh-possibly-change-directory newsgroup server)
173 (let ((file (if (stringp id)
175 (concat nnmh-current-directory (int-to-string id))))
176 (file-name-coding-system nnmail-pathname-coding-system)
177 (pathname-coding-system nnmail-pathname-coding-system)
178 (nntp-server-buffer (or buffer nntp-server-buffer)))
181 (not (file-directory-p file))
182 (save-excursion (nnmail-find-file file))
183 (string-to-int (file-name-nondirectory file)))))
185 (deffoo nnmh-request-group (group &optional server dont-check)
186 (nnheader-init-server-buffer)
187 (nnmh-possibly-change-directory group server)
188 (let ((pathname (nnmail-group-pathname group nnmh-directory))
189 (file-name-coding-system nnmail-pathname-coding-system)
190 (pathname-coding-system nnmail-pathname-coding-system)
193 ((not (file-directory-p pathname))
195 'nnmh "Can't select group (no such directory): %s" group))
197 (setq nnmh-current-directory pathname)
198 (and nnmh-get-new-mail
200 (nnmh-update-gnus-unreads group))
203 (nnheader-report 'nnmh "Selected group %s" group)
206 ;; Re-scan the directory if it's on a foreign system.
207 (nnheader-re-read-dir pathname)
210 (mapcar (lambda (name) (string-to-int name))
211 (directory-files pathname nil "^[0-9]+$" t))
215 (setq nnmh-group-alist
216 (delq (assoc group nnmh-group-alist) nnmh-group-alist))
217 (push (list group (cons (car dir) (car (last dir))))
219 (nnheader-report 'nnmh "Selected group %s" group)
221 "211 %d %d %d %s\n" (length dir) (car dir)
222 (car (last dir)) group))
224 (nnheader-report 'nnmh "Empty group %s" group)
225 (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
227 (deffoo nnmh-request-scan (&optional group server)
228 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
230 (deffoo nnmh-request-list (&optional server dir)
232 (nnmh-possibly-change-directory nil server)
233 (let ((file-name-coding-system nnmail-pathname-coding-system)
234 (pathname-coding-system nnmail-pathname-coding-system)
236 (file-truename (or dir (file-name-as-directory nnmh-directory)))))
237 (nnmh-request-list-1 nnmh-toplev))
238 (setq nnmh-group-alist (nnmail-get-active))
242 (defun nnmh-request-list-1 (dir)
243 (setq dir (expand-file-name dir))
244 ;; Recurse down all directories.
245 (let ((dirs (and (file-readable-p dir)
246 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
247 (nnheader-directory-files dir t nil t)))
249 ;; Recurse down directories.
250 (while (setq rdir (pop dirs))
251 (when (and (file-directory-p rdir)
252 (file-readable-p rdir)
253 (not (equal (file-truename rdir)
254 (file-truename dir))))
255 (nnmh-request-list-1 rdir))))
256 ;; For each directory, generate an active file line.
257 (unless (string= (expand-file-name nnmh-toplev) dir)
259 (lambda (name) (string-to-int name))
260 (directory-files dir nil "^[0-9]+$" t))))
263 (set-buffer nntp-server-buffer)
264 (goto-char (point-max))
271 (file-truename (file-name-as-directory
272 (expand-file-name nnmh-toplev))))
274 (nnheader-replace-chars-in-string
275 (decode-coding-string (substring dir (match-end 0))
276 nnmail-pathname-coding-system)
279 (apply 'min files)))))))
282 (deffoo nnmh-request-newgroups (date &optional server)
283 (nnmh-request-list server))
285 (deffoo nnmh-request-expire-articles (articles newsgroup
286 &optional server force)
287 (nnmh-possibly-change-directory newsgroup server)
289 article rest mod-time)
290 (nnheader-init-server-buffer)
292 (while (and articles is-old)
293 (setq article (concat nnmh-current-directory
294 (int-to-string (car articles))))
295 (when (setq mod-time (nth 5 (file-attributes article)))
296 (if (and (nnmh-deletable-article-p newsgroup (car articles))
298 (nnmail-expired-article-p newsgroup mod-time force)))
300 (nnheader-message 5 "Deleting article %s in %s..."
303 (funcall nnmail-delete-file-function article)
305 (nnheader-message 1 "Couldn't delete article %s in %s"
307 (push (car articles) rest))))
308 (push (car articles) rest)))
309 (setq articles (cdr articles)))
310 (nnheader-message 5 "")
311 (nconc rest articles)))
313 (deffoo nnmh-close-group (group &optional server)
316 (deffoo nnmh-request-move-article
317 (article group server accept-form &optional last)
318 (let ((buf (get-buffer-create " *nnmh move*"))
321 (nnmh-deletable-article-p group article)
322 (nnmh-request-article article group server)
326 (insert-buffer-substring nntp-server-buffer)
327 (setq result (eval accept-form))
328 (kill-buffer (current-buffer))
331 (nnmh-possibly-change-directory group server)
333 (funcall nnmail-delete-file-function
334 (concat nnmh-current-directory (int-to-string article)))
338 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
339 (nnmh-possibly-change-directory group server)
340 (if (and (not (equal group "queue"))
341 (not (equal group "draft")))
342 (nnmail-check-syntax))
343 (when nnmail-cache-accepted-message-ids
344 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
345 (nnheader-init-server-buffer)
349 (nnmh-active-number group)
351 (list (cons group (nnmh-active-number group)))
353 (let ((res (nnmail-article-group 'nnmh-active-number)))
355 (yes-or-no-p "Moved to `junk' group; delete article? "))
357 (car (nnmh-save-mail res noinsert)))))
358 (when (and last nnmail-cache-accepted-message-ids)
359 (nnmail-cache-close))))
361 (deffoo nnmh-request-replace-article (article group buffer)
362 (nnmh-possibly-change-directory group)
365 (nnmh-possibly-create-directory group)
368 (point-min) (point-max)
369 (concat nnmh-current-directory (int-to-string article))
370 nil (if (nnheader-be-verbose 5) nil 'nomesg))
373 (deffoo nnmh-request-create-group (group &optional server args)
374 (nnheader-init-server-buffer)
375 (unless (assoc group nnmh-group-alist)
377 (push (list group (setq active (cons 1 0)))
379 (nnmh-possibly-create-directory group)
380 (nnmh-possibly-change-directory group server)
381 (let ((articles (mapcar
383 (string-to-int file))
385 nnmh-current-directory nil "^[0-9]+$"))))
387 (setcar active (apply 'min articles))
388 (setcdr active (apply 'max articles))))))
391 (deffoo nnmh-request-delete-group (group &optional force server)
392 (nnmh-possibly-change-directory group server)
393 ;; Delete all articles in GROUP.
395 () ; Don't delete the articles.
396 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
398 (when (file-writable-p (car articles))
399 (nnheader-message 5 "Deleting article %s in %s..."
400 (car articles) group)
401 (funcall nnmail-delete-file-function (car articles)))
402 (setq articles (cdr articles))))
403 ;; Try to delete the directory itself.
405 (delete-directory nnmh-current-directory)))
406 ;; Remove the group from all structures.
407 (setq nnmh-group-alist
408 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
409 nnmh-current-directory nil)
412 (deffoo nnmh-request-rename-group (group new-name &optional server)
413 (nnmh-possibly-change-directory group server)
414 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
415 (old-dir (nnmail-group-pathname group nnmh-directory)))
417 (make-directory new-dir t)
419 ;; We move the articles file by file instead of renaming
420 ;; the directory -- there may be subgroups in this group.
421 ;; One might be more clever, I guess.
422 (let ((files (nnheader-article-to-file-alist old-dir)))
425 (concat old-dir (cdar files))
426 (concat new-dir (cdar files)))
428 (when (<= (length (directory-files old-dir)) 2)
430 (delete-directory old-dir)))
431 ;; That went ok, so we change the internal structures.
432 (let ((entry (assoc group nnmh-group-alist)))
434 (setcar entry new-name))
435 (setq nnmh-current-directory nil)
438 (nnoo-define-skeleton nnmh)
441 ;;; Internal functions.
443 (defun nnmh-possibly-change-directory (newsgroup &optional server)
445 (not (nnmh-server-opened server)))
446 (nnmh-open-server server))
448 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory))
449 (file-name-coding-system nnmail-pathname-coding-system)
450 (pathname-coding-system nnmail-pathname-coding-system))
451 (if (file-directory-p pathname)
452 (setq nnmh-current-directory pathname)
453 (error "No such newsgroup: %s" newsgroup)))))
455 (defun nnmh-possibly-create-directory (group)
457 (setq dir (nnmail-group-pathname group nnmh-directory))
458 (while (not (file-directory-p dir))
460 (setq dir (file-name-directory (directory-file-name dir))))
462 (when (make-directory (directory-file-name (car dirs)))
463 (error "Could not create directory %s" (car dirs)))
464 (nnheader-message 5 "Creating mail directory %s" (car dirs))
465 (setq dirs (cdr dirs)))))
467 (defun nnmh-save-mail (group-art &optional noinsert)
468 "Called narrowed to an article."
470 (nnmail-insert-lines)
471 (nnmail-insert-xref group-art))
472 (run-hooks 'nnmail-prepare-save-mail-hook)
473 (run-hooks 'nnmh-prepare-save-mail-hook)
474 (goto-char (point-min))
475 (while (looking-at "From ")
476 (replace-match "X-From-Line: ")
478 ;; We save the article in all the newsgroups it belongs in.
482 (nnmh-possibly-create-directory (caar ga))
483 (let ((file (concat (nnmail-group-pathname
484 (caar ga) nnmh-directory)
485 (int-to-string (cdar ga)))))
487 ;; It was already saved, so we just make a hard link.
488 (funcall nnmail-crosspost-link-function first file t)
490 (nnmail-write-region (point-min) (point-max) file nil nil)
495 (defun nnmh-active-number (group)
496 "Compute the next article number in GROUP."
497 (let ((active (cadr (assoc group nnmh-group-alist)))
498 (dir (nnmail-group-pathname group nnmh-directory))
499 (file-name-coding-system nnmail-pathname-coding-system)
500 (pathname-coding-system nnmail-pathname-coding-system)
503 ;; The group wasn't known to nnmh, so we just create an active
505 (setq active (cons 1 0))
506 (push (list group active) nnmh-group-alist)
507 (unless (file-exists-p dir)
508 (gnus-make-directory dir))
509 ;; Find the highest number in the group.
514 (directory-files dir nil "^[0-9]+$"))
517 (setcdr active (car files)))))
518 (setcdr active (1+ (cdr active)))
520 ;; See whether the file exists...
522 (setq file (concat (nnmail-group-pathname group nnmh-directory)
523 (int-to-string (cdr active)))))
524 ;; ... or there is a buffer that will make that file exist
526 (get-file-buffer file))
527 ;; Skip past that file.
528 (setcdr active (1+ (cdr active))))
531 (defun nnmh-update-gnus-unreads (group)
532 ;; Go through the .nnmh-articles file and compare with the actual
533 ;; articles in this folder. The articles that are "new" will be
534 ;; marked as unread by Gnus.
535 (let* ((dir nnmh-current-directory)
536 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
537 (directory-files nnmh-current-directory
540 (nnmh-file (concat dir ".nnmh-articles"))
542 ;; Load the .nnmh-articles file.
543 (when (file-exists-p nnmh-file)
545 (let (nnmh-newsgroup-articles)
546 (ignore-errors (load nnmh-file nil t t))
547 nnmh-newsgroup-articles)))
548 ;; Add all new articles to the `new' list.
551 (unless (assq (car art) articles)
552 (push (car art) new))
553 (setq art (cdr art))))
554 ;; Remove all deleted articles.
555 (let ((art articles))
557 (unless (memq (caar art) files)
558 (setq articles (delq (car art) articles)))
559 (setq art (cdr art))))
560 ;; Check whether the articles really are the ones that Gnus thinks
561 ;; they are by looking at the time-stamps.
562 (let ((arts articles)
564 (while (setq art (pop arts))
566 (nth 5 (file-attributes
567 (concat dir (int-to-string (car art)))))
569 (setq articles (delq art articles))
570 (push (car art) new))))
571 ;; Go through all the new articles and add them, and their
572 ;; time-stamps, to the list.
578 (nth 5 (file-attributes
579 (concat dir (int-to-string art))))))
581 ;; Make Gnus mark all new articles as unread.
583 (gnus-make-articles-unread
584 (gnus-group-prefixed-name group (list 'nnmh ""))
585 (setq new (sort new '<))))
586 ;; Sort the article list with highest numbers first.
587 (setq articles (sort articles (lambda (art1 art2)
588 (> (car art1) (car art2)))))
589 ;; Finally write this list back to the .nnmh-articles file.
590 (with-temp-file nnmh-file
591 (insert ";; Gnus article active file for " group "\n\n")
592 (insert "(setq nnmh-newsgroup-articles '")
593 (gnus-prin1 articles)
596 (defun nnmh-deletable-article-p (group article)
597 "Say whether ARTICLE in GROUP can be deleted."
598 (let ((path (concat nnmh-current-directory (int-to-string article))))
600 (and (file-writable-p path)
602 ;; We can never delete the last article in the group.
603 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
606 nnmh-allow-delete-final))))
610 ;;; nnmh.el ends here