1 ;;; nnmh.el --- mhspool access for Gnus
2 ;; Copyright (C) 1995,96,97,98 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 nnheader-pathname-coding-system)
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"))
115 (deffoo nnmh-open-server (server &optional defs)
116 (nnoo-change-server 'nnmh server defs)
117 (when (not (file-exists-p nnmh-directory))
119 (make-directory nnmh-directory t)
122 ((not (file-exists-p nnmh-directory))
124 (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
125 ((not (file-directory-p (file-truename nnmh-directory)))
127 (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
129 (nnheader-report 'nnmh "Opened server %s using directory %s"
130 server nnmh-directory)
133 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
134 (nnmh-possibly-change-directory newsgroup server)
135 (let ((file (if (stringp id)
137 (concat nnmh-current-directory (int-to-string id))))
138 (pathname-coding-system nnheader-pathname-coding-system)
139 (nntp-server-buffer (or buffer nntp-server-buffer)))
142 (not (file-directory-p file))
143 (save-excursion (nnmail-find-file file))
144 (string-to-int (file-name-nondirectory file)))))
146 (deffoo nnmh-request-group (group &optional server dont-check)
147 (nnheader-init-server-buffer)
148 (nnmh-possibly-change-directory group server)
149 (let ((pathname (nnmail-group-pathname group nnmh-directory))
150 (pathname-coding-system nnheader-pathname-coding-system)
153 ((not (file-directory-p pathname))
155 'nnmh "Can't select group (no such directory): %s" group))
157 (setq nnmh-current-directory pathname)
158 (and nnmh-get-new-mail
160 (nnmh-update-gnus-unreads group))
163 (nnheader-report 'nnmh "Selected group %s" group)
166 ;; Re-scan the directory if it's on a foreign system.
167 (nnheader-re-read-dir pathname)
170 (mapcar (lambda (name) (string-to-int name))
171 (directory-files pathname nil "^[0-9]+$" t))
175 (nnheader-report 'nnmh "Selected group %s" group)
177 "211 %d %d %d %s\n" (length dir) (car dir)
178 (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
181 (nnheader-report 'nnmh "Empty group %s" group)
182 (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
184 (deffoo nnmh-request-scan (&optional group server)
185 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
187 (deffoo nnmh-request-list (&optional server dir)
189 (nnmh-possibly-change-directory nil server)
190 (let ((pathname-coding-system nnheader-pathname-coding-system)
192 (file-truename (or dir (file-name-as-directory nnmh-directory)))))
193 (nnmh-request-list-1 nnmh-toplev))
194 (setq nnmh-group-alist (nnmail-get-active))
198 (defun nnmh-request-list-1 (dir)
199 (setq dir (expand-file-name dir))
200 ;; Recurse down all directories.
201 (let ((dirs (and (file-readable-p dir)
202 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
203 (nnheader-directory-files dir t nil t)))
205 ;; Recurse down directories.
206 (while (setq rdir (pop dirs))
207 (when (and (file-directory-p rdir)
208 (file-readable-p rdir)
209 (not (equal (file-truename rdir)
210 (file-truename dir))))
211 (nnmh-request-list-1 rdir))))
212 ;; For each directory, generate an active file line.
213 (unless (string= (expand-file-name nnmh-toplev) dir)
215 (lambda (name) (string-to-int name))
216 (directory-files dir nil "^[0-9]+$" t))))
219 (set-buffer nntp-server-buffer)
220 (goto-char (point-max))
227 (file-truename (file-name-as-directory
228 (expand-file-name nnmh-toplev))))
230 (nnheader-replace-chars-in-string
231 (gnus-decode-coding-string (substring dir (match-end 0))
232 nnheader-pathname-coding-system)
235 (apply 'min files)))))))
238 (deffoo nnmh-request-newgroups (date &optional server)
239 (nnmh-request-list server))
241 (deffoo nnmh-request-expire-articles (articles newsgroup
242 &optional server force)
243 (nnmh-possibly-change-directory newsgroup server)
245 article rest mod-time)
246 (nnheader-init-server-buffer)
248 (while (and articles is-old)
249 (setq article (concat nnmh-current-directory
250 (int-to-string (car articles))))
251 (when (setq mod-time (nth 5 (file-attributes article)))
252 (if (and (nnmh-deletable-article-p newsgroup (car articles))
254 (nnmail-expired-article-p newsgroup mod-time force)))
256 (nnheader-message 5 "Deleting article %s in %s..."
259 (funcall nnmail-delete-file-function article)
261 (nnheader-message 1 "Couldn't delete article %s in %s"
263 (push (car articles) rest))))
264 (push (car articles) rest)))
265 (setq articles (cdr articles)))
266 (nnheader-message 5 "")
267 (nconc rest articles)))
269 (deffoo nnmh-close-group (group &optional server)
272 (deffoo nnmh-request-move-article
273 (article group server accept-form &optional last)
274 (let ((buf (get-buffer-create " *nnmh move*"))
277 (nnmh-deletable-article-p group article)
278 (nnmh-request-article article group server)
282 (insert-buffer-substring nntp-server-buffer)
283 (setq result (eval accept-form))
284 (kill-buffer (current-buffer))
287 (nnmh-possibly-change-directory group server)
289 (funcall nnmail-delete-file-function
290 (concat nnmh-current-directory (int-to-string article)))
294 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
295 (nnmh-possibly-change-directory group server)
296 (if (and (not (equal group "queue"))
297 (not (equal group "draft")))
298 (nnmail-check-syntax))
299 (when nnmail-cache-accepted-message-ids
300 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
301 (nnheader-init-server-buffer)
305 (nnmh-active-number group)
307 (list (cons group (nnmh-active-number group)))
309 (let ((res (nnmail-article-group 'nnmh-active-number)))
311 (yes-or-no-p "Moved to `junk' group; delete article? "))
313 (car (nnmh-save-mail res noinsert)))))
314 (when (and last nnmail-cache-accepted-message-ids)
315 (nnmail-cache-close))))
317 (deffoo nnmh-request-replace-article (article group buffer)
318 (nnmh-possibly-change-directory group)
321 (nnmh-possibly-create-directory group)
324 (point-min) (point-max)
325 (concat nnmh-current-directory (int-to-string article))
326 nil (if (nnheader-be-verbose 5) nil 'nomesg))
329 (deffoo nnmh-request-create-group (group &optional server args)
330 (nnheader-init-server-buffer)
331 (unless (assoc group nnmh-group-alist)
333 (push (list group (setq active (cons 1 0)))
335 (nnmh-possibly-create-directory group)
336 (nnmh-possibly-change-directory group server)
337 (let ((articles (mapcar
339 (string-to-int file))
341 nnmh-current-directory nil "^[0-9]+$"))))
343 (setcar active (apply 'min articles))
344 (setcdr active (apply 'max articles))))))
347 (deffoo nnmh-request-delete-group (group &optional force server)
348 (nnmh-possibly-change-directory group server)
349 ;; Delete all articles in GROUP.
351 () ; Don't delete the articles.
352 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
354 (when (file-writable-p (car articles))
355 (nnheader-message 5 "Deleting article %s in %s..."
356 (car articles) group)
357 (funcall nnmail-delete-file-function (car articles)))
358 (setq articles (cdr articles))))
359 ;; Try to delete the directory itself.
361 (delete-directory nnmh-current-directory)))
362 ;; Remove the group from all structures.
363 (setq nnmh-group-alist
364 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
365 nnmh-current-directory nil)
368 (deffoo nnmh-request-rename-group (group new-name &optional server)
369 (nnmh-possibly-change-directory group server)
370 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
371 (old-dir (nnmail-group-pathname group nnmh-directory)))
373 (make-directory new-dir t)
375 ;; We move the articles file by file instead of renaming
376 ;; the directory -- there may be subgroups in this group.
377 ;; One might be more clever, I guess.
378 (let ((files (nnheader-article-to-file-alist old-dir)))
381 (concat old-dir (cdar files))
382 (concat new-dir (cdar files)))
384 (when (<= (length (directory-files old-dir)) 2)
386 (delete-directory old-dir)))
387 ;; That went ok, so we change the internal structures.
388 (let ((entry (assoc group nnmh-group-alist)))
390 (setcar entry new-name))
391 (setq nnmh-current-directory nil)
394 (nnoo-define-skeleton nnmh)
397 ;;; Internal functions.
399 (defun nnmh-possibly-change-directory (newsgroup &optional server)
401 (not (nnmh-server-opened server)))
402 (nnmh-open-server server))
404 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory))
405 (pathname-coding-system nnheader-pathname-coding-system))
406 (if (file-directory-p pathname)
407 (setq nnmh-current-directory pathname)
408 (error "No such newsgroup: %s" newsgroup)))))
410 (defun nnmh-possibly-create-directory (group)
412 (setq dir (nnmail-group-pathname group nnmh-directory))
413 (while (not (file-directory-p dir))
415 (setq dir (file-name-directory (directory-file-name dir))))
417 (when (make-directory (directory-file-name (car dirs)))
418 (error "Could not create directory %s" (car dirs)))
419 (nnheader-message 5 "Creating mail directory %s" (car dirs))
420 (setq dirs (cdr dirs)))))
422 (defun nnmh-save-mail (group-art &optional noinsert)
423 "Called narrowed to an article."
425 (nnmail-insert-lines)
426 (nnmail-insert-xref group-art))
427 (run-hooks 'nnmail-prepare-save-mail-hook)
428 (run-hooks 'nnmh-prepare-save-mail-hook)
429 (goto-char (point-min))
430 (while (looking-at "From ")
431 (replace-match "X-From-Line: ")
433 ;; We save the article in all the newsgroups it belongs in.
437 (nnmh-possibly-create-directory (caar ga))
438 (let ((file (concat (nnmail-group-pathname
439 (caar ga) nnmh-directory)
440 (int-to-string (cdar ga)))))
442 ;; It was already saved, so we just make a hard link.
443 (funcall nnmail-crosspost-link-function first file t)
445 (nnmail-write-region (point-min) (point-max) file nil nil)
450 (defun nnmh-active-number (group)
451 "Compute the next article number in GROUP."
452 (let ((active (cadr (assoc group nnmh-group-alist)))
453 (dir (nnmail-group-pathname group nnmh-directory))
454 (pathname-coding-system nnheader-pathname-coding-system))
456 ;; The group wasn't known to nnmh, so we just create an active
458 (setq active (cons 1 0))
459 (push (list group active) nnmh-group-alist)
460 (unless (file-exists-p dir)
461 (gnus-make-directory dir))
462 ;; Find the highest number in the group.
467 (directory-files dir nil "^[0-9]+$"))
470 (setcdr active (car files)))))
471 (setcdr active (1+ (cdr active)))
472 (while (file-exists-p
473 (concat (nnmail-group-pathname group nnmh-directory)
474 (int-to-string (cdr active))))
475 (setcdr active (1+ (cdr active))))
478 (defun nnmh-update-gnus-unreads (group)
479 ;; Go through the .nnmh-articles file and compare with the actual
480 ;; articles in this folder. The articles that are "new" will be
481 ;; marked as unread by Gnus.
482 (let* ((dir nnmh-current-directory)
483 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
484 (directory-files nnmh-current-directory
487 (nnmh-file (concat dir ".nnmh-articles"))
489 ;; Load the .nnmh-articles file.
490 (when (file-exists-p nnmh-file)
492 (let (nnmh-newsgroup-articles)
493 (ignore-errors (load nnmh-file nil t t))
494 nnmh-newsgroup-articles)))
495 ;; Add all new articles to the `new' list.
498 (unless (assq (car art) articles)
499 (push (car art) new))
500 (setq art (cdr art))))
501 ;; Remove all deleted articles.
502 (let ((art articles))
504 (unless (memq (caar art) files)
505 (setq articles (delq (car art) articles)))
506 (setq art (cdr art))))
507 ;; Check whether the articles really are the ones that Gnus thinks
508 ;; they are by looking at the time-stamps.
509 (let ((arts articles)
511 (while (setq art (pop arts))
513 (nth 5 (file-attributes
514 (concat dir (int-to-string (car art)))))
516 (setq articles (delq art articles))
517 (push (car art) new))))
518 ;; Go through all the new articles and add them, and their
519 ;; time-stamps, to the list.
525 (nth 5 (file-attributes
526 (concat dir (int-to-string art))))))
528 ;; Make Gnus mark all new articles as unread.
530 (gnus-make-articles-unread
531 (gnus-group-prefixed-name group (list 'nnmh ""))
532 (setq new (sort new '<))))
533 ;; Sort the article list with highest numbers first.
534 (setq articles (sort articles (lambda (art1 art2)
535 (> (car art1) (car art2)))))
536 ;; Finally write this list back to the .nnmh-articles file.
537 (nnheader-temp-write nnmh-file
538 (insert ";; Gnus article active file for " group "\n\n")
539 (insert "(setq nnmh-newsgroup-articles '")
540 (gnus-prin1 articles)
543 (defun nnmh-deletable-article-p (group article)
544 "Say whether ARTICLE in GROUP can be deleted."
545 (let ((path (concat nnmh-current-directory (int-to-string article))))
547 (and (file-writable-p path)
549 ;; We can never delete the last article in the group.
550 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
553 nnmh-allow-delete-final))))
557 ;;; nnmh.el ends here