1 ;;; nnmh.el --- mhspool access for Gnus
2 ;; Copyright (C) 1995,96,97 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
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)
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)))
79 ;; 1997/8/12 by MORIOKA Tomohiko
80 (file-name-coding-system 'binary) ; for Emacs 20
81 (pathname-coding-system 'binary) ; for XEmacs/mule
83 (nnmh-possibly-change-directory newsgroup server)
84 ;; We don't support fetching by Message-ID.
85 (if (stringp (car articles))
88 (when (and (file-exists-p
89 (setq file (concat (file-name-as-directory
90 nnmh-current-directory)
92 (setq article (pop articles))))))
93 (not (file-directory-p file)))
94 (insert (format "221 %d Article retrieved.\n" article))
96 (nnheader-insert-head file)
98 (if (search-forward "\n\n" nil t)
100 (goto-char (point-max))
103 (delete-region (point) (point-max)))
104 (setq count (1+ count))
108 (message "nnmh: Receiving headers... %d%%"
109 (/ (* count 100) number))))
112 (message "nnmh: Receiving headers...done"))
114 (nnheader-fold-continuation-lines)
117 (deffoo nnmh-open-server (server &optional defs)
118 (nnoo-change-server 'nnmh server defs)
119 (when (not (file-exists-p nnmh-directory))
121 (make-directory nnmh-directory t)
124 ((not (file-exists-p nnmh-directory))
126 (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
127 ((not (file-directory-p (file-truename nnmh-directory)))
129 (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
131 (nnheader-report 'nnmh "Opened server %s using directory %s"
132 server nnmh-directory)
135 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
136 (nnmh-possibly-change-directory newsgroup server)
137 (let ((file (if (stringp id)
139 (concat nnmh-current-directory (int-to-string id))))
140 ;; 1997/8/12 by MORIOKA Tomohiko
141 (file-name-coding-system 'binary) ; for Emacs 20
142 (pathname-coding-system 'binary) ; for XEmacs/mule
143 (nntp-server-buffer (or buffer nntp-server-buffer)))
146 (not (file-directory-p file))
147 (save-excursion (nnmail-find-file file))
148 (string-to-int (file-name-nondirectory file)))))
150 (deffoo nnmh-request-group (group &optional server dont-check)
151 (let ((pathname (nnmail-group-pathname group nnmh-directory))
152 ;; 1997/8/12 by MORIOKA Tomohiko
153 (file-name-coding-system 'binary) ; for Emacs 20
154 (pathname-coding-system 'binary) ; for XEmacs/mule.
157 ((not (file-directory-p pathname))
159 'nnmh "Can't select group (no such directory): %s" group))
161 (setq nnmh-current-directory pathname)
162 (and nnmh-get-new-mail
164 (nnmh-update-gnus-unreads group))
167 (nnheader-report 'nnmh "Selected group %s" group)
170 ;; Re-scan the directory if it's on a foreign system.
171 (nnheader-re-read-dir pathname)
174 (mapcar (lambda (name) (string-to-int name))
175 (directory-files pathname nil "^[0-9]+$" t))
179 (nnheader-report 'nnmh "Selected group %s" group)
181 "211 %d %d %d %s\n" (length dir) (car dir)
182 (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
185 (nnheader-report 'nnmh "Empty group %s" group)
186 (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
188 (deffoo nnmh-request-scan (&optional group server)
189 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
191 (deffoo nnmh-request-list (&optional server dir)
193 (let ((file-name-coding-system 'binary)
194 (pathname-coding-system 'binary)
196 (file-truename (or dir (file-name-as-directory nnmh-directory)))))
197 (nnmh-request-list-1 nnmh-toplev))
198 (setq nnmh-group-alist (nnmail-get-active))
202 (defun nnmh-request-list-1 (dir)
203 (setq dir (expand-file-name dir))
204 ;; Recurse down all directories.
205 (let ((dirs (and (file-readable-p dir)
206 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
207 (directory-files dir t nil t)))
209 ;; Recurse down directories.
210 (while (setq dir (pop dirs))
211 (when (and (not (member (file-name-nondirectory dir) '("." "..")))
212 (file-directory-p dir)
213 (file-readable-p dir))
214 (nnmh-request-list-1 dir))))
215 ;; For each directory, generate an active file line.
216 (unless (string= (expand-file-name nnmh-toplev) dir)
218 (lambda (name) (string-to-int name))
219 (directory-files dir nil "^[0-9]+$" t))))
222 (set-buffer nntp-server-buffer)
223 (goto-char (point-max))
230 (file-truename (file-name-as-directory
231 (expand-file-name nnmh-toplev))))
233 (nnheader-replace-chars-in-string
234 (decode-coding-string (substring dir (match-end 0))
235 nnmail-pathname-coding-system)
238 (apply 'min files)))))))
241 (deffoo nnmh-request-newgroups (date &optional server)
242 (nnmh-request-list server))
244 (deffoo nnmh-request-expire-articles (articles newsgroup
245 &optional server force)
246 (nnmh-possibly-change-directory newsgroup server)
247 (let* ((active-articles
251 (string-to-int name)))
252 (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
254 article rest mod-time)
255 (nnmail-activate 'nnmh)
257 (while (and articles is-old)
258 (setq article (concat nnmh-current-directory
259 (int-to-string (car articles))))
260 (when (setq mod-time (nth 5 (file-attributes article)))
261 (if (and (nnmh-deletable-article-p newsgroup (car articles))
263 (nnmail-expired-article-p newsgroup mod-time force)))
265 (nnheader-message 5 "Deleting article %s in %s..."
268 (funcall nnmail-delete-file-function article)
270 (nnheader-message 1 "Couldn't delete article %s in %s"
272 (push (car articles) rest))))
273 (push (car articles) rest)))
274 (setq articles (cdr articles)))
276 (nconc rest articles)))
278 (deffoo nnmh-close-group (group &optional server)
281 (deffoo nnmh-request-move-article
282 (article group server accept-form &optional last)
283 (let ((buf (get-buffer-create " *nnmh move*"))
286 (nnmh-deletable-article-p group article)
287 (nnmh-request-article article group server)
291 (insert-buffer-substring nntp-server-buffer)
292 (setq result (eval accept-form))
293 (kill-buffer (current-buffer))
296 (nnmh-possibly-change-directory group server)
298 (funcall nnmail-delete-file-function
299 (concat nnmh-current-directory (int-to-string article)))
303 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
304 (nnmh-possibly-change-directory group server)
305 (nnmail-check-syntax)
306 (when nnmail-cache-accepted-message-ids
307 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
311 (nnmail-activate 'nnmh)
313 (list (cons group (nnmh-active-number group)))
316 (nnmail-activate 'nnmh)
317 (let ((res (nnmail-article-group 'nnmh-active-number)))
319 (yes-or-no-p "Moved to `junk' group; delete article? "))
321 (car (nnmh-save-mail res noinsert))))))
322 (when (and last nnmail-cache-accepted-message-ids)
323 (nnmail-cache-close))))
325 (deffoo nnmh-request-replace-article (article group buffer)
326 (nnmh-possibly-change-directory group)
329 (nnmh-possibly-create-directory group)
332 (point-min) (point-max)
333 (concat nnmh-current-directory (int-to-string article))
334 nil (if (nnheader-be-verbose 5) nil 'nomesg))
337 (deffoo nnmh-request-create-group (group &optional server args)
338 (nnmail-activate 'nnmh)
339 (unless (assoc group nnmh-group-alist)
341 (push (list group (setq active (cons 1 0)))
343 (nnmh-possibly-create-directory group)
344 (nnmh-possibly-change-directory group server)
345 (let ((articles (mapcar
347 (string-to-int file))
349 nnmh-current-directory nil "^[0-9]+$"))))
351 (setcar active (apply 'min articles))
352 (setcdr active (apply 'max articles))))))
355 (deffoo nnmh-request-delete-group (group &optional force server)
356 (nnmh-possibly-change-directory group server)
357 ;; Delete all articles in GROUP.
359 () ; Don't delete the articles.
360 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
362 (when (file-writable-p (car articles))
363 (nnheader-message 5 "Deleting article %s in %s..."
364 (car articles) group)
365 (funcall nnmail-delete-file-function (car articles)))
366 (setq articles (cdr articles))))
367 ;; Try to delete the directory itself.
369 (delete-directory nnmh-current-directory)))
370 ;; Remove the group from all structures.
371 (setq nnmh-group-alist
372 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
373 nnmh-current-directory nil)
376 (deffoo nnmh-request-rename-group (group new-name &optional server)
377 (nnmh-possibly-change-directory group server)
378 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
379 (old-dir (nnmail-group-pathname group nnmh-directory)))
381 (make-directory new-dir t)
383 ;; We move the articles file by file instead of renaming
384 ;; the directory -- there may be subgroups in this group.
385 ;; One might be more clever, I guess.
386 (let ((files (nnheader-article-to-file-alist old-dir)))
389 (concat old-dir (cdar files))
390 (concat new-dir (cdar files)))
392 (when (<= (length (directory-files old-dir)) 2)
394 (delete-directory old-dir)))
395 ;; That went ok, so we change the internal structures.
396 (let ((entry (assoc group nnmh-group-alist)))
398 (setcar entry new-name))
399 (setq nnmh-current-directory nil)
402 (nnoo-define-skeleton nnmh)
405 ;;; Internal functions.
407 (defun nnmh-possibly-change-directory (newsgroup &optional server)
409 (not (nnmh-server-opened server)))
410 (nnmh-open-server server))
412 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory))
413 ;; 1997/8/12 by MORIOKA Tomohiko
414 (file-name-coding-system 'binary) ; for Emacs 20
415 (pathname-coding-system 'binary)) ; for XEmacs/mule
416 (if (file-directory-p pathname)
417 (setq nnmh-current-directory pathname)
418 (error "No such newsgroup: %s" newsgroup)))))
420 (defun nnmh-possibly-create-directory (group)
422 (setq dir (nnmail-group-pathname group nnmh-directory))
423 (while (not (file-directory-p dir))
425 (setq dir (file-name-directory (directory-file-name dir))))
427 (when (make-directory (directory-file-name (car dirs)))
428 (error "Could not create directory %s" (car dirs)))
429 (nnheader-message 5 "Creating mail directory %s" (car dirs))
430 (setq dirs (cdr dirs)))))
432 (defun nnmh-save-mail (group-art &optional noinsert)
433 "Called narrowed to an article."
435 (nnmail-insert-lines)
436 (nnmail-insert-xref group-art))
437 (run-hooks 'nnmail-prepare-save-mail-hook)
438 (run-hooks 'nnmh-prepare-save-mail-hook)
439 (goto-char (point-min))
440 (while (looking-at "From ")
441 (replace-match "X-From-Line: ")
443 ;; We save the article in all the newsgroups it belongs in.
447 (nnmh-possibly-create-directory (caar ga))
448 (let ((file (concat (nnmail-group-pathname
449 (caar ga) nnmh-directory)
450 (int-to-string (cdar ga)))))
452 ;; It was already saved, so we just make a hard link.
453 (funcall nnmail-crosspost-link-function first file t)
455 (nnmail-write-region (point-min) (point-max) file nil nil)
460 (defun nnmh-active-number (group)
461 "Compute the next article number in GROUP."
462 (let ((active (cadr (assoc group nnmh-group-alist)))
463 (dir (nnmail-group-pathname group nnmh-directory))
464 ;; 1997/8/14 by MORIOKA Tomohiko
465 (file-name-coding-system 'binary) ; for Emacs 20
466 (pathname-coding-system 'binary)) ; for XEmacs/mule
468 ;; The group wasn't known to nnmh, so we just create an active
470 (setq active (cons 1 0))
471 (push (list group active) nnmh-group-alist)
472 (unless (file-exists-p dir)
473 (make-directory dir))
474 ;; Find the highest number in the group.
479 (directory-files dir nil "^[0-9]+$"))
482 (setcdr active (car files)))))
483 (setcdr active (1+ (cdr active)))
484 (while (file-exists-p
485 (concat (nnmail-group-pathname group nnmh-directory)
486 (int-to-string (cdr active))))
487 (setcdr active (1+ (cdr active))))
490 (defun nnmh-update-gnus-unreads (group)
491 ;; Go through the .nnmh-articles file and compare with the actual
492 ;; articles in this folder. The articles that are "new" will be
493 ;; marked as unread by Gnus.
494 (let* ((dir nnmh-current-directory)
495 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
496 (directory-files nnmh-current-directory
499 (nnmh-file (concat dir ".nnmh-articles"))
501 ;; Load the .nnmh-articles file.
502 (when (file-exists-p nnmh-file)
504 (let (nnmh-newsgroup-articles)
505 (ignore-errors (load nnmh-file nil t t))
506 nnmh-newsgroup-articles)))
507 ;; Add all new articles to the `new' list.
510 (unless (assq (car art) articles)
511 (push (car art) new))
512 (setq art (cdr art))))
513 ;; Remove all deleted articles.
514 (let ((art articles))
516 (unless (memq (caar art) files)
517 (setq articles (delq (car art) articles)))
518 (setq art (cdr art))))
519 ;; Check whether the articles really are the ones that Gnus thinks
520 ;; they are by looking at the time-stamps.
521 (let ((arts articles)
523 (while (setq art (pop arts))
525 (nth 5 (file-attributes
526 (concat dir (int-to-string (car art)))))
528 (setq articles (delq art articles))
529 (push (car art) new))))
530 ;; Go through all the new articles and add them, and their
531 ;; time-stamps, to the list.
537 (nth 5 (file-attributes
538 (concat dir (int-to-string art))))))
540 ;; Make Gnus mark all new articles as unread.
542 (gnus-make-articles-unread
543 (gnus-group-prefixed-name group (list 'nnmh ""))
544 (setq new (sort new '<))))
545 ;; Sort the article list with highest numbers first.
546 (setq articles (sort articles (lambda (art1 art2)
547 (> (car art1) (car art2)))))
548 ;; Finally write this list back to the .nnmh-articles file.
549 (nnheader-temp-write nnmh-file
550 (insert ";; Gnus article active file for " group "\n\n")
551 (insert "(setq nnmh-newsgroup-articles '")
552 (gnus-prin1 articles)
555 (defun nnmh-deletable-article-p (group article)
556 "Say whether ARTICLE in GROUP can be deleted."
557 (let ((path (concat nnmh-current-directory (int-to-string article))))
559 (and (file-writable-p path)
560 ;; We can never delete the last article in the group.
561 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
566 ;;; nnmh.el ends here