1 ;;; nnmh.el --- mhspool access for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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
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)
66 ;;; Interface functions.
68 (nnoo-define-basics nnmh)
70 (deffoo nnmh-retrieve-headers (articles &optional newsgroup server fetch-old)
72 (set-buffer nntp-server-buffer)
75 (number (length articles))
76 (large (and (numberp nnmail-large-newsgroup)
77 (> number nnmail-large-newsgroup)))
80 (nnmh-possibly-change-directory newsgroup server)
81 ;; We don't support fetching by Message-ID.
82 (if (stringp (car articles))
85 (when (and (file-exists-p
86 (setq file (concat (file-name-as-directory
87 nnmh-current-directory)
89 (setq article (pop articles))))))
90 (not (file-directory-p file)))
91 (insert (format "221 %d Article retrieved.\n" article))
93 (nnheader-insert-head file)
95 (if (search-forward "\n\n" nil t)
97 (goto-char (point-max))
100 (delete-region (point) (point-max)))
101 (setq count (1+ count))
105 (message "nnmh: Receiving headers... %d%%"
106 (/ (* count 100) number))))
108 (and large (message "nnmh: Receiving headers...done"))
110 (nnheader-fold-continuation-lines)
113 (deffoo nnmh-open-server (server &optional defs)
114 (nnoo-change-server 'nnmh server defs)
115 (when (not (file-exists-p nnmh-directory))
117 (make-directory nnmh-directory t)
120 ((not (file-exists-p nnmh-directory))
122 (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
123 ((not (file-directory-p (file-truename nnmh-directory)))
125 (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
127 (nnheader-report 'nnmh "Opened server %s using directory %s"
128 server nnmh-directory)
131 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
132 (nnmh-possibly-change-directory newsgroup server)
133 (let ((file (if (stringp id)
135 (concat nnmh-current-directory (int-to-string id))))
136 (nntp-server-buffer (or buffer nntp-server-buffer)))
139 (not (file-directory-p file))
140 (save-excursion (nnmail-find-file file))
141 (string-to-int (file-name-nondirectory file)))))
143 (deffoo nnmh-request-group (group &optional server dont-check)
144 (let ((pathname (nnmail-group-pathname group nnmh-directory))
147 ((not (file-directory-p pathname))
149 'nnmh "Can't select group (no such directory): %s" group))
151 (setq nnmh-current-directory pathname)
152 (and nnmh-get-new-mail
154 (nnmh-update-gnus-unreads group))
157 (nnheader-report 'nnmh "Selected group %s" group)
160 ;; Re-scan the directory if it's on a foreign system.
161 (nnheader-re-read-dir pathname)
164 (mapcar (lambda (name) (string-to-int name))
165 (directory-files pathname nil "^[0-9]+$" t))
169 (nnheader-report 'nnmh "Selected group %s" group)
171 "211 %d %d %d %s\n" (length dir) (car dir)
172 (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
175 (nnheader-report 'nnmh "Empty group %s" group)
176 (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
178 (deffoo nnmh-request-scan (&optional group server)
179 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
181 (deffoo nnmh-request-list (&optional server dir)
184 (or dir (file-truename (file-name-as-directory nnmh-directory)))))
185 (nnmh-request-list-1 nnmh-toplev))
186 (setq nnmh-group-alist (nnmail-get-active))
190 (defun nnmh-request-list-1 (dir)
191 (setq dir (expand-file-name dir))
192 ;; Recurse down all directories.
193 (let ((dirs (and (file-readable-p dir)
194 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
195 (directory-files dir t nil t)))
197 ;; Recurse down directories.
198 (while (setq dir (pop dirs))
199 (when (and (not (member (file-name-nondirectory dir) '("." "..")))
200 (file-directory-p dir)
201 (file-readable-p dir))
202 (nnmh-request-list-1 dir))))
203 ;; For each directory, generate an active file line.
204 (unless (string= (expand-file-name nnmh-toplev) dir)
206 (lambda (name) (string-to-int name))
207 (directory-files dir nil "^[0-9]+$" t))))
210 (set-buffer nntp-server-buffer)
211 (goto-char (point-max))
218 (file-truename (file-name-as-directory
219 (expand-file-name nnmh-toplev)))) dir)
220 (nnheader-replace-chars-in-string
221 (substring dir (match-end 0)) ?/ ?.))
223 (apply 'min files)))))))
226 (deffoo nnmh-request-newgroups (date &optional server)
227 (nnmh-request-list server))
229 (deffoo nnmh-request-expire-articles (articles newsgroup &optional server force)
230 (nnmh-possibly-change-directory newsgroup server)
231 (let* ((active-articles
235 (string-to-int name)))
236 (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
238 article rest mod-time)
239 (nnmail-activate 'nnmh)
241 (while (and articles is-old)
242 (setq article (concat nnmh-current-directory
243 (int-to-string (car articles))))
244 (if (setq mod-time (nth 5 (file-attributes article)))
245 (if (and (nnmh-deletable-article-p newsgroup (car articles))
247 (nnmail-expired-article-p newsgroup mod-time force)))
249 (nnheader-message 5 "Deleting article %s in %s..."
252 (funcall nnmail-delete-file-function article)
254 (nnheader-message 1 "Couldn't delete article %s in %s"
256 (setq rest (cons (car articles) rest)))))
257 (setq rest (cons (car articles) rest))))
258 (setq articles (cdr articles)))
260 (nconc rest articles)))
262 (deffoo nnmh-close-group (group &optional server)
265 (deffoo nnmh-request-move-article
266 (article group server accept-form &optional last)
267 (let ((buf (get-buffer-create " *nnmh move*"))
270 (nnmh-deletable-article-p group article)
271 (nnmh-request-article article group server)
274 (insert-buffer-substring nntp-server-buffer)
275 (setq result (eval accept-form))
276 (kill-buffer (current-buffer))
279 (nnmh-possibly-change-directory group server)
281 (funcall nnmail-delete-file-function
282 (concat nnmh-current-directory (int-to-string article)))
286 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
287 (nnmh-possibly-change-directory group server)
288 (nnmail-check-syntax)
291 (nnmail-activate 'nnmh)
293 (list (cons group (nnmh-active-number group)))
296 (nnmail-activate 'nnmh)
297 (car (nnmh-save-mail (nnmail-article-group 'nnmh-active-number)
300 (deffoo nnmh-request-replace-article (article group buffer)
301 (nnmh-possibly-change-directory group)
304 (nnmh-possibly-create-directory group)
308 (point-min) (point-max)
309 (concat nnmh-current-directory (int-to-string article))
310 nil (if (nnheader-be-verbose 5) nil 'nomesg))
314 (deffoo nnmh-request-create-group (group &optional server args)
315 (nnmail-activate 'nnmh)
316 (unless (assoc group nnmh-group-alist)
318 (push (list group (setq active (cons 1 0)))
320 (nnmh-possibly-create-directory group)
321 (nnmh-possibly-change-directory group server)
322 (let ((articles (mapcar
324 (string-to-int file))
326 nnmh-current-directory nil "^[0-9]+$"))))
328 (setcar active (apply 'min articles))
329 (setcdr active (apply 'max articles))))))
332 (deffoo nnmh-request-delete-group (group &optional force server)
333 (nnmh-possibly-change-directory group server)
334 ;; Delete all articles in GROUP.
336 () ; Don't delete the articles.
337 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
339 (and (file-writable-p (car articles))
341 (nnheader-message 5 "Deleting article %s in %s..."
342 (car articles) group)
343 (funcall nnmail-delete-file-function (car articles))))
344 (setq articles (cdr articles))))
345 ;; Try to delete the directory itself.
347 (delete-directory nnmh-current-directory)
349 ;; Remove the group from all structures.
350 (setq nnmh-group-alist
351 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
352 nnmh-current-directory nil)
355 (deffoo nnmh-request-rename-group (group new-name &optional server)
356 (nnmh-possibly-change-directory group server)
357 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
358 (old-dir (nnmail-group-pathname group nnmh-directory)))
359 (when (condition-case ()
361 (make-directory new-dir t)
364 ;; We move the articles file by file instead of renaming
365 ;; the directory -- there may be subgroups in this group.
366 ;; One might be more clever, I guess.
367 (let ((files (nnheader-article-to-file-alist old-dir)))
370 (concat old-dir (cdar files))
371 (concat new-dir (cdar files)))
373 (when (<= (length (directory-files old-dir)) 2)
375 (delete-directory old-dir)
377 ;; That went ok, so we change the internal structures.
378 (let ((entry (assoc group nnmh-group-alist)))
379 (and entry (setcar entry new-name))
380 (setq nnmh-current-directory nil)
384 ;;; Internal functions.
386 (defun nnmh-possibly-change-directory (newsgroup &optional server)
388 (not (nnmh-server-opened server)))
389 (nnmh-open-server server))
391 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory)))
392 (if (file-directory-p pathname)
393 (setq nnmh-current-directory pathname)
394 (error "No such newsgroup: %s" newsgroup)))))
396 (defun nnmh-possibly-create-directory (group)
398 (setq dir (nnmail-group-pathname group nnmh-directory))
399 (while (not (file-directory-p dir))
400 (setq dirs (cons dir dirs))
401 (setq dir (file-name-directory (directory-file-name dir))))
403 (if (make-directory (directory-file-name (car dirs)))
404 (error "Could not create directory %s" (car dirs)))
405 (nnheader-message 5 "Creating mail directory %s" (car dirs))
406 (setq dirs (cdr dirs)))))
408 (defun nnmh-save-mail (group-art &optional noinsert)
409 "Called narrowed to an article."
411 (nnmail-insert-lines)
412 (nnmail-insert-xref group-art))
413 (run-hooks 'nnmail-prepare-save-mail-hook)
414 (run-hooks 'nnmh-prepare-save-mail-hook)
415 (goto-char (point-min))
416 (while (looking-at "From ")
417 (replace-match "X-From-Line: ")
419 ;; We save the article in all the newsgroups it belongs in.
423 (nnmh-possibly-create-directory (caar ga))
424 (let ((file (concat (nnmail-group-pathname
425 (caar ga) nnmh-directory)
426 (int-to-string (cdar ga)))))
428 ;; It was already saved, so we just make a hard link.
429 (funcall nnmail-crosspost-link-function first file t)
431 (nnmail-write-region (point-min) (point-max) file nil nil)
436 (defun nnmh-active-number (group)
437 "Compute the next article number in GROUP."
438 (let ((active (cadr (assoc group nnmh-group-alist))))
440 ;; The group wasn't known to nnmh, so we just create an active
442 (setq active (cons 1 0))
443 (push (list group active) nnmh-group-alist)
444 ;; Find the highest number in the group.
450 (nnmail-group-pathname group nnmh-directory)
454 (setcdr active (car files)))))
455 (setcdr active (1+ (cdr active)))
456 (while (file-exists-p
457 (concat (nnmail-group-pathname group nnmh-directory)
458 (int-to-string (cdr active))))
459 (setcdr active (1+ (cdr active))))
462 (defun nnmh-update-gnus-unreads (group)
463 ;; Go through the .nnmh-articles file and compare with the actual
464 ;; articles in this folder. The articles that are "new" will be
465 ;; marked as unread by Gnus.
466 (let* ((dir nnmh-current-directory)
467 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
468 (directory-files nnmh-current-directory
469 nil "^[0-9]+$" t)) '<))
470 (nnmh-file (concat dir ".nnmh-articles"))
472 ;; Load the .nnmh-articles file.
473 (when (file-exists-p nnmh-file)
475 (let (nnmh-newsgroup-articles)
476 (condition-case nil (load nnmh-file nil t t) (error nil))
477 nnmh-newsgroup-articles)))
478 ;; Add all new articles to the `new' list.
481 (unless (assq (car art) articles)
482 (setq new (cons (car art) new)))
483 (setq art (cdr art))))
484 ;; Remove all deleted articles.
485 (let ((art articles))
487 (unless (memq (caar art) files)
488 (setq articles (delq (car art) articles)))
489 (setq art (cdr art))))
490 ;; Check whether the articles really are the ones that Gnus thinks
491 ;; they are by looking at the time-stamps.
492 (let ((arts articles)
494 (while (setq art (pop arts))
496 (nth 5 (file-attributes
497 (concat dir (int-to-string (car art)))))
499 (setq articles (delq art articles))
500 (push (car art) new))))
501 ;; Go through all the new articles and add them, and their
502 ;; time-stamps, to the list.
508 (nth 5 (file-attributes
509 (concat dir (int-to-string art))))))
511 ;; Make Gnus mark all new articles as unread.
513 (gnus-make-articles-unread
514 (gnus-group-prefixed-name group (list 'nnmh ""))
515 (setq new (sort new '<))))
516 ;; Sort the article list with highest numbers first.
517 (setq articles (sort articles (lambda (art1 art2)
518 (> (car art1) (car art2)))))
519 ;; Finally write this list back to the .nnmh-articles file.
520 (nnheader-temp-write nnmh-file
521 (insert ";; Gnus article active file for " group "\n\n")
522 (insert "(setq nnmh-newsgroup-articles '")
523 (gnus-prin1 articles)
526 (defun nnmh-deletable-article-p (group article)
527 "Say whether ARTICLE in GROUP can be deleted."
528 (let ((path (concat nnmh-current-directory (int-to-string article))))
529 (and (file-writable-p path)
530 (or (not nnmail-keep-last-article)
531 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
536 ;;; nnmh.el ends here