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))))
109 (message "nnmh: Receiving headers...done"))
111 (nnheader-fold-continuation-lines)
114 (deffoo nnmh-open-server (server &optional defs)
115 (nnoo-change-server 'nnmh server defs)
116 (when (not (file-exists-p nnmh-directory))
118 (make-directory nnmh-directory t)
121 ((not (file-exists-p nnmh-directory))
123 (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
124 ((not (file-directory-p (file-truename nnmh-directory)))
126 (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
128 (nnheader-report 'nnmh "Opened server %s using directory %s"
129 server nnmh-directory)
132 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
133 (nnmh-possibly-change-directory newsgroup server)
134 (let ((file (if (stringp id)
136 (concat nnmh-current-directory (int-to-string id))))
137 (nntp-server-buffer (or buffer nntp-server-buffer)))
140 (not (file-directory-p file))
141 (save-excursion (nnmail-find-file file))
142 (string-to-int (file-name-nondirectory file)))))
144 (deffoo nnmh-request-group (group &optional server dont-check)
145 (let ((pathname (nnmail-group-pathname group nnmh-directory))
148 ((not (file-directory-p pathname))
150 'nnmh "Can't select group (no such directory): %s" group))
152 (setq nnmh-current-directory pathname)
153 (and nnmh-get-new-mail
155 (nnmh-update-gnus-unreads group))
158 (nnheader-report 'nnmh "Selected group %s" group)
161 ;; Re-scan the directory if it's on a foreign system.
162 (nnheader-re-read-dir pathname)
165 (mapcar (lambda (name) (string-to-int name))
166 (directory-files pathname nil "^[0-9]+$" t))
170 (nnheader-report 'nnmh "Selected group %s" group)
172 "211 %d %d %d %s\n" (length dir) (car dir)
173 (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
176 (nnheader-report 'nnmh "Empty group %s" group)
177 (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
179 (deffoo nnmh-request-scan (&optional group server)
180 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
182 (deffoo nnmh-request-list (&optional server dir)
185 (or dir (file-truename (file-name-as-directory nnmh-directory)))))
186 (nnmh-request-list-1 nnmh-toplev))
187 (setq nnmh-group-alist (nnmail-get-active))
191 (defun nnmh-request-list-1 (dir)
192 (setq dir (expand-file-name dir))
193 ;; Recurse down all directories.
194 (let ((dirs (and (file-readable-p dir)
195 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
196 (directory-files dir t nil t)))
198 ;; Recurse down directories.
199 (while (setq dir (pop dirs))
200 (when (and (not (member (file-name-nondirectory dir) '("." "..")))
201 (file-directory-p dir)
202 (file-readable-p dir))
203 (nnmh-request-list-1 dir))))
204 ;; For each directory, generate an active file line.
205 (unless (string= (expand-file-name nnmh-toplev) dir)
207 (lambda (name) (string-to-int name))
208 (directory-files dir nil "^[0-9]+$" t))))
211 (set-buffer nntp-server-buffer)
212 (goto-char (point-max))
219 (file-truename (file-name-as-directory
220 (expand-file-name nnmh-toplev))))
222 (nnheader-replace-chars-in-string
223 (substring dir (match-end 0)) ?/ ?.))
225 (apply 'min files)))))))
228 (deffoo nnmh-request-newgroups (date &optional server)
229 (nnmh-request-list server))
231 (deffoo nnmh-request-expire-articles (articles newsgroup &optional server force)
232 (nnmh-possibly-change-directory newsgroup server)
233 (let* ((active-articles
237 (string-to-int name)))
238 (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
240 article rest mod-time)
241 (nnmail-activate 'nnmh)
243 (while (and articles is-old)
244 (setq article (concat nnmh-current-directory
245 (int-to-string (car articles))))
246 (when (setq mod-time (nth 5 (file-attributes article)))
247 (if (and (nnmh-deletable-article-p newsgroup (car articles))
249 (nnmail-expired-article-p newsgroup mod-time force)))
251 (nnheader-message 5 "Deleting article %s in %s..."
254 (funcall nnmail-delete-file-function article)
256 (nnheader-message 1 "Couldn't delete article %s in %s"
258 (push (car articles) rest))))
259 (push (car articles) rest)))
260 (setq articles (cdr articles)))
262 (nconc rest articles)))
264 (deffoo nnmh-close-group (group &optional server)
267 (deffoo nnmh-request-move-article
268 (article group server accept-form &optional last)
269 (let ((buf (get-buffer-create " *nnmh move*"))
272 (nnmh-deletable-article-p group article)
273 (nnmh-request-article article group server)
276 (insert-buffer-substring nntp-server-buffer)
277 (setq result (eval accept-form))
278 (kill-buffer (current-buffer))
281 (nnmh-possibly-change-directory group server)
283 (funcall nnmail-delete-file-function
284 (concat nnmh-current-directory (int-to-string article)))
288 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
289 (nnmh-possibly-change-directory group server)
290 (nnmail-check-syntax)
293 (nnmail-activate 'nnmh)
295 (list (cons group (nnmh-active-number group)))
298 (nnmail-activate 'nnmh)
299 (car (nnmh-save-mail (nnmail-article-group 'nnmh-active-number)
302 (deffoo nnmh-request-replace-article (article group buffer)
303 (nnmh-possibly-change-directory group)
306 (nnmh-possibly-create-directory group)
310 (point-min) (point-max)
311 (concat nnmh-current-directory (int-to-string article))
312 nil (if (nnheader-be-verbose 5) nil 'nomesg))
316 (deffoo nnmh-request-create-group (group &optional server args)
317 (nnmail-activate 'nnmh)
318 (unless (assoc group nnmh-group-alist)
320 (push (list group (setq active (cons 1 0)))
322 (nnmh-possibly-create-directory group)
323 (nnmh-possibly-change-directory group server)
324 (let ((articles (mapcar
326 (string-to-int file))
328 nnmh-current-directory nil "^[0-9]+$"))))
330 (setcar active (apply 'min articles))
331 (setcdr active (apply 'max articles))))))
334 (deffoo nnmh-request-delete-group (group &optional force server)
335 (nnmh-possibly-change-directory group server)
336 ;; Delete all articles in GROUP.
338 () ; Don't delete the articles.
339 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
341 (when (file-writable-p (car articles))
342 (nnheader-message 5 "Deleting article %s in %s..."
343 (car articles) group)
344 (funcall nnmail-delete-file-function (car articles)))
345 (setq articles (cdr articles))))
346 ;; Try to delete the directory itself.
348 (delete-directory nnmh-current-directory)
350 ;; Remove the group from all structures.
351 (setq nnmh-group-alist
352 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
353 nnmh-current-directory nil)
356 (deffoo nnmh-request-rename-group (group new-name &optional server)
357 (nnmh-possibly-change-directory group server)
358 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
359 (old-dir (nnmail-group-pathname group nnmh-directory)))
360 (when (condition-case ()
362 (make-directory new-dir t)
365 ;; We move the articles file by file instead of renaming
366 ;; the directory -- there may be subgroups in this group.
367 ;; One might be more clever, I guess.
368 (let ((files (nnheader-article-to-file-alist old-dir)))
371 (concat old-dir (cdar files))
372 (concat new-dir (cdar files)))
374 (when (<= (length (directory-files old-dir)) 2)
376 (delete-directory old-dir)
378 ;; That went ok, so we change the internal structures.
379 (let ((entry (assoc group nnmh-group-alist)))
381 (setcar entry new-name))
382 (setq nnmh-current-directory nil)
386 ;;; Internal functions.
388 (defun nnmh-possibly-change-directory (newsgroup &optional server)
390 (not (nnmh-server-opened server)))
391 (nnmh-open-server server))
393 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory)))
394 (if (file-directory-p pathname)
395 (setq nnmh-current-directory pathname)
396 (error "No such newsgroup: %s" newsgroup)))))
398 (defun nnmh-possibly-create-directory (group)
400 (setq dir (nnmail-group-pathname group nnmh-directory))
401 (while (not (file-directory-p dir))
403 (setq dir (file-name-directory (directory-file-name dir))))
405 (when (make-directory (directory-file-name (car dirs)))
406 (error "Could not create directory %s" (car dirs)))
407 (nnheader-message 5 "Creating mail directory %s" (car dirs))
408 (setq dirs (cdr dirs)))))
410 (defun nnmh-save-mail (group-art &optional noinsert)
411 "Called narrowed to an article."
413 (nnmail-insert-lines)
414 (nnmail-insert-xref group-art))
415 (run-hooks 'nnmail-prepare-save-mail-hook)
416 (run-hooks 'nnmh-prepare-save-mail-hook)
417 (goto-char (point-min))
418 (while (looking-at "From ")
419 (replace-match "X-From-Line: ")
421 ;; We save the article in all the newsgroups it belongs in.
425 (nnmh-possibly-create-directory (caar ga))
426 (let ((file (concat (nnmail-group-pathname
427 (caar ga) nnmh-directory)
428 (int-to-string (cdar ga)))))
430 ;; It was already saved, so we just make a hard link.
431 (funcall nnmail-crosspost-link-function first file t)
433 (nnmail-write-region (point-min) (point-max) file nil nil)
438 (defun nnmh-active-number (group)
439 "Compute the next article number in GROUP."
440 (let ((active (cadr (assoc group nnmh-group-alist))))
442 ;; The group wasn't known to nnmh, so we just create an active
444 (setq active (cons 1 0))
445 (push (list group active) nnmh-group-alist)
446 ;; Find the highest number in the group.
452 (nnmail-group-pathname group nnmh-directory)
456 (setcdr active (car files)))))
457 (setcdr active (1+ (cdr active)))
458 (while (file-exists-p
459 (concat (nnmail-group-pathname group nnmh-directory)
460 (int-to-string (cdr active))))
461 (setcdr active (1+ (cdr active))))
464 (defun nnmh-update-gnus-unreads (group)
465 ;; Go through the .nnmh-articles file and compare with the actual
466 ;; articles in this folder. The articles that are "new" will be
467 ;; marked as unread by Gnus.
468 (let* ((dir nnmh-current-directory)
469 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
470 (directory-files nnmh-current-directory
473 (nnmh-file (concat dir ".nnmh-articles"))
475 ;; Load the .nnmh-articles file.
476 (when (file-exists-p nnmh-file)
478 (let (nnmh-newsgroup-articles)
479 (condition-case nil (load nnmh-file nil t t) (error nil))
480 nnmh-newsgroup-articles)))
481 ;; Add all new articles to the `new' list.
484 (unless (assq (car art) articles)
485 (push (car art) new))
486 (setq art (cdr art))))
487 ;; Remove all deleted articles.
488 (let ((art articles))
490 (unless (memq (caar art) files)
491 (setq articles (delq (car art) articles)))
492 (setq art (cdr art))))
493 ;; Check whether the articles really are the ones that Gnus thinks
494 ;; they are by looking at the time-stamps.
495 (let ((arts articles)
497 (while (setq art (pop arts))
499 (nth 5 (file-attributes
500 (concat dir (int-to-string (car art)))))
502 (setq articles (delq art articles))
503 (push (car art) new))))
504 ;; Go through all the new articles and add them, and their
505 ;; time-stamps, to the list.
511 (nth 5 (file-attributes
512 (concat dir (int-to-string art))))))
514 ;; Make Gnus mark all new articles as unread.
516 (gnus-make-articles-unread
517 (gnus-group-prefixed-name group (list 'nnmh ""))
518 (setq new (sort new '<))))
519 ;; Sort the article list with highest numbers first.
520 (setq articles (sort articles (lambda (art1 art2)
521 (> (car art1) (car art2)))))
522 ;; Finally write this list back to the .nnmh-articles file.
523 (nnheader-temp-write nnmh-file
524 (insert ";; Gnus article active file for " group "\n\n")
525 (insert "(setq nnmh-newsgroup-articles '")
526 (gnus-prin1 articles)
529 (defun nnmh-deletable-article-p (group article)
530 "Say whether ARTICLE in GROUP can be deleted."
531 (let ((path (concat nnmh-current-directory (int-to-string article))))
532 (when (file-writable-p path)
533 (or (not nnmail-keep-last-article)
534 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
539 ;;; nnmh.el ends here