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))))
275 (encode-coding-string
276 (nnheader-replace-chars-in-string
277 (substring dir (match-end 0))
279 nnmail-pathname-coding-system)))
281 (apply 'min files)))))))
284 (deffoo nnmh-request-newgroups (date &optional server)
285 (nnmh-request-list server))
287 (deffoo nnmh-request-expire-articles (articles newsgroup
288 &optional server force)
289 (nnmh-possibly-change-directory newsgroup server)
291 article rest mod-time)
292 (nnheader-init-server-buffer)
294 (while (and articles is-old)
295 (setq article (concat nnmh-current-directory
296 (int-to-string (car articles))))
297 (when (setq mod-time (nth 5 (file-attributes article)))
298 (if (and (nnmh-deletable-article-p newsgroup (car articles))
300 (nnmail-expired-article-p newsgroup mod-time force)))
302 ;; Allow a special target group. -- jcn
303 (unless (eq nnmail-expiry-target 'delete)
305 (nnmh-request-article (car articles)
306 newsgroup server (current-buffer))
307 (nnmail-expiry-target-group
308 nnmail-expiry-target newsgroup)))
309 (nnheader-message 5 "Deleting article %s in %s..."
312 (funcall nnmail-delete-file-function article)
314 (nnheader-message 1 "Couldn't delete article %s in %s"
316 (push (car articles) rest))))
317 (push (car articles) rest)))
318 (setq articles (cdr articles)))
319 (nnheader-message 5 "")
320 (nconc rest articles)))
322 (deffoo nnmh-close-group (group &optional server)
325 (deffoo nnmh-request-move-article
326 (article group server accept-form &optional last)
327 (let ((buf (get-buffer-create " *nnmh move*"))
330 (nnmh-deletable-article-p group article)
331 (nnmh-request-article article group server)
335 (insert-buffer-substring nntp-server-buffer)
336 (setq result (eval accept-form))
337 (kill-buffer (current-buffer))
340 (nnmh-possibly-change-directory group server)
342 (funcall nnmail-delete-file-function
343 (concat nnmh-current-directory (int-to-string article)))
347 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
348 (nnmh-possibly-change-directory group server)
349 (if (and (not (equal group "queue"))
350 (not (equal group "draft")))
351 (nnmail-check-syntax))
352 (when nnmail-cache-accepted-message-ids
353 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
354 (nnheader-init-server-buffer)
358 (nnmh-active-number group)
360 (list (cons group (nnmh-active-number group)))
362 (let ((res (nnmail-article-group 'nnmh-active-number)))
364 (yes-or-no-p "Moved to `junk' group; delete article? "))
366 (car (nnmh-save-mail res noinsert)))))
367 (when (and last nnmail-cache-accepted-message-ids)
368 (nnmail-cache-close))))
370 (deffoo nnmh-request-replace-article (article group buffer)
371 (nnmh-possibly-change-directory group)
374 (nnmh-possibly-create-directory group)
377 (point-min) (point-max)
378 (concat nnmh-current-directory (int-to-string article))
379 nil (if (nnheader-be-verbose 5) nil 'nomesg))
382 (deffoo nnmh-request-create-group (group &optional server args)
383 (nnheader-init-server-buffer)
384 (unless (assoc group nnmh-group-alist)
386 (push (list group (setq active (cons 1 0)))
388 (nnmh-possibly-create-directory group)
389 (nnmh-possibly-change-directory group server)
390 (let ((articles (mapcar
392 (string-to-int file))
394 nnmh-current-directory nil "^[0-9]+$"))))
396 (setcar active (apply 'min articles))
397 (setcdr active (apply 'max articles))))))
400 (deffoo nnmh-request-delete-group (group &optional force server)
401 (nnmh-possibly-change-directory group server)
402 ;; Delete all articles in GROUP.
404 () ; Don't delete the articles.
405 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
407 (when (file-writable-p (car articles))
408 (nnheader-message 5 "Deleting article %s in %s..."
409 (car articles) group)
410 (funcall nnmail-delete-file-function (car articles)))
411 (setq articles (cdr articles))))
412 ;; Try to delete the directory itself.
414 (delete-directory nnmh-current-directory)))
415 ;; Remove the group from all structures.
416 (setq nnmh-group-alist
417 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
418 nnmh-current-directory nil)
421 (deffoo nnmh-request-rename-group (group new-name &optional server)
422 (nnmh-possibly-change-directory group server)
423 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
424 (old-dir (nnmail-group-pathname group nnmh-directory)))
426 (make-directory new-dir t)
428 ;; We move the articles file by file instead of renaming
429 ;; the directory -- there may be subgroups in this group.
430 ;; One might be more clever, I guess.
431 (let ((files (nnheader-article-to-file-alist old-dir)))
434 (concat old-dir (cdar files))
435 (concat new-dir (cdar files)))
437 (when (<= (length (directory-files old-dir)) 2)
439 (delete-directory old-dir)))
440 ;; That went ok, so we change the internal structures.
441 (let ((entry (assoc group nnmh-group-alist)))
443 (setcar entry new-name))
444 (setq nnmh-current-directory nil)
447 (nnoo-define-skeleton nnmh)
450 ;;; Internal functions.
452 (defun nnmh-possibly-change-directory (newsgroup &optional server)
454 (not (nnmh-server-opened server)))
455 (nnmh-open-server server))
457 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory))
458 (file-name-coding-system nnmail-pathname-coding-system)
459 (pathname-coding-system nnmail-pathname-coding-system))
460 (if (file-directory-p pathname)
461 (setq nnmh-current-directory pathname)
462 (error "No such newsgroup: %s" newsgroup)))))
464 (defun nnmh-possibly-create-directory (group)
466 (setq dir (nnmail-group-pathname group nnmh-directory))
467 (while (not (file-directory-p dir))
469 (setq dir (file-name-directory (directory-file-name dir))))
471 (when (make-directory (directory-file-name (car dirs)))
472 (error "Could not create directory %s" (car dirs)))
473 (nnheader-message 5 "Creating mail directory %s" (car dirs))
474 (setq dirs (cdr dirs)))))
476 (defun nnmh-save-mail (group-art &optional noinsert)
477 "Called narrowed to an article."
479 (nnmail-insert-lines)
480 (nnmail-insert-xref group-art))
481 (run-hooks 'nnmail-prepare-save-mail-hook)
482 (run-hooks 'nnmh-prepare-save-mail-hook)
483 (goto-char (point-min))
484 (while (looking-at "From ")
485 (replace-match "X-From-Line: ")
487 ;; We save the article in all the newsgroups it belongs in.
491 (nnmh-possibly-create-directory (caar ga))
492 (let ((file (concat (nnmail-group-pathname
493 (caar ga) nnmh-directory)
494 (int-to-string (cdar ga)))))
496 ;; It was already saved, so we just make a hard link.
497 (funcall nnmail-crosspost-link-function first file t)
499 (nnmail-write-region (point-min) (point-max) file nil nil)
504 (defun nnmh-active-number (group)
505 "Compute the next article number in GROUP."
506 (let ((active (cadr (assoc group nnmh-group-alist)))
507 (dir (nnmail-group-pathname group nnmh-directory))
508 (file-name-coding-system nnmail-pathname-coding-system)
509 (pathname-coding-system nnmail-pathname-coding-system)
512 ;; The group wasn't known to nnmh, so we just create an active
514 (setq active (cons 1 0))
515 (push (list group active) nnmh-group-alist)
516 (unless (file-exists-p dir)
517 (gnus-make-directory dir))
518 ;; Find the highest number in the group.
523 (directory-files dir nil "^[0-9]+$"))
526 (setcdr active (car files)))))
527 (setcdr active (1+ (cdr active)))
529 ;; See whether the file exists...
531 (setq file (concat (nnmail-group-pathname group nnmh-directory)
532 (int-to-string (cdr active)))))
533 ;; ... or there is a buffer that will make that file exist
535 (get-file-buffer file))
536 ;; Skip past that file.
537 (setcdr active (1+ (cdr active))))
540 (defun nnmh-update-gnus-unreads (group)
541 ;; Go through the .nnmh-articles file and compare with the actual
542 ;; articles in this folder. The articles that are "new" will be
543 ;; marked as unread by Gnus.
544 (let* ((dir nnmh-current-directory)
545 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
546 (directory-files nnmh-current-directory
549 (nnmh-file (concat dir ".nnmh-articles"))
551 ;; Load the .nnmh-articles file.
552 (when (file-exists-p nnmh-file)
554 (let (nnmh-newsgroup-articles)
555 (ignore-errors (load nnmh-file nil t t))
556 nnmh-newsgroup-articles)))
557 ;; Add all new articles to the `new' list.
560 (unless (assq (car art) articles)
561 (push (car art) new))
562 (setq art (cdr art))))
563 ;; Remove all deleted articles.
564 (let ((art articles))
566 (unless (memq (caar art) files)
567 (setq articles (delq (car art) articles)))
568 (setq art (cdr art))))
569 ;; Check whether the articles really are the ones that Gnus thinks
570 ;; they are by looking at the time-stamps.
571 (let ((arts articles)
573 (while (setq art (pop arts))
575 (nth 5 (file-attributes
576 (concat dir (int-to-string (car art)))))
578 (setq articles (delq art articles))
579 (push (car art) new))))
580 ;; Go through all the new articles and add them, and their
581 ;; time-stamps, to the list.
587 (nth 5 (file-attributes
588 (concat dir (int-to-string art))))))
590 ;; Make Gnus mark all new articles as unread.
592 (gnus-make-articles-unread
593 (gnus-group-prefixed-name group (list 'nnmh ""))
594 (setq new (sort new '<))))
595 ;; Sort the article list with highest numbers first.
596 (setq articles (sort articles (lambda (art1 art2)
597 (> (car art1) (car art2)))))
598 ;; Finally write this list back to the .nnmh-articles file.
599 (with-temp-file nnmh-file
600 (insert ";; Gnus article active file for " group "\n\n")
601 (insert "(setq nnmh-newsgroup-articles '")
602 (gnus-prin1 articles)
605 (defun nnmh-deletable-article-p (group article)
606 "Say whether ARTICLE in GROUP can be deleted."
607 (let ((path (concat nnmh-current-directory (int-to-string article))))
609 (and (file-writable-p path)
611 ;; We can never delete the last article in the group.
612 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
615 nnmh-allow-delete-final))))
619 ;;; nnmh.el ends here