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
38 (eval-and-compile (require 'cl))
42 (defvoo nnmh-directory "~/Mail/"
43 "*Mail spool directory.")
45 (defvoo nnmh-get-new-mail t
46 "*If non-nil, nnmh will check the incoming mail file and split the mail.")
48 (defvoo nnmh-prepare-save-mail-hook nil
49 "*Hook run narrowed to an article before saving.")
51 (defvoo nnmh-be-safe nil
52 "*If non-nil, nnmh will check all articles to make sure whether they are new or not.")
56 (defconst nnmh-version "nnmh 1.0"
59 (defvoo nnmh-current-directory nil
60 "Current news group directory.")
62 (defvoo nnmh-status-string "")
63 (defvoo nnmh-group-alist 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)))
81 (nnmh-possibly-change-directory newsgroup server)
82 ;; We don't support fetching by Message-ID.
83 (if (stringp (car articles))
86 (when (and (file-exists-p
87 (setq file (concat (file-name-as-directory
88 nnmh-current-directory)
90 (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 (message "nnmh: Receiving headers... %d%%"
108 (/ (* count 100) number))))
110 (and large (message "nnmh: Receiving headers...done"))
112 (nnheader-fold-continuation-lines)
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 (nntp-server-buffer (or buffer nntp-server-buffer)))
141 (not (file-directory-p file))
142 (save-excursion (nnmail-find-file file))
143 (string-to-int (file-name-nondirectory file)))))
145 (deffoo nnmh-request-group (group &optional server dont-check)
146 (let ((pathname (nnmail-group-pathname group nnmh-directory))
149 ((not (file-directory-p pathname))
151 'nnmh "Can't select group (no such directory): %s" group))
153 (setq nnmh-current-directory pathname)
154 (and nnmh-get-new-mail
156 (nnmh-update-gnus-unreads group))
159 (nnheader-report 'nnmh "Selected group %s" group)
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 (string-match "/\\.\\.?$" 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))
217 (file-truename (file-name-as-directory
218 (expand-file-name nnmh-toplev))) dir)
219 (nnheader-replace-chars-in-string
220 (substring dir (match-end 0)) ?/ ?.))
221 (apply (function max) files)
222 (apply (function min) files)))))))
225 (deffoo nnmh-request-newgroups (date &optional server)
226 (nnmh-request-list server))
228 (deffoo nnmh-request-expire-articles (articles newsgroup &optional server force)
229 (nnmh-possibly-change-directory newsgroup server)
230 (let* ((active-articles
234 (string-to-int name)))
235 (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
237 article rest mod-time)
238 (nnmail-activate 'nnmh)
240 (while (and articles is-old)
241 (setq article (concat nnmh-current-directory
242 (int-to-string (car articles))))
243 (if (setq mod-time (nth 5 (file-attributes article)))
244 (if (and (nnmh-deletable-article-p newsgroup (car articles))
246 (nnmail-expired-article-p newsgroup mod-time force)))
248 (nnheader-message 5 "Deleting article %s in %s..."
251 (funcall nnmail-delete-file-function article)
253 (setq rest (cons (car articles) rest)))))
254 (setq rest (cons (car articles) rest))))
255 (setq articles (cdr articles)))
257 (nconc rest articles)))
259 (deffoo nnmh-close-group (group &optional server)
262 (deffoo nnmh-request-move-article
263 (article group server accept-form &optional last)
264 (let ((buf (get-buffer-create " *nnmh move*"))
267 (nnmh-deletable-article-p group article)
268 (nnmh-request-article article group server)
271 (insert-buffer-substring nntp-server-buffer)
272 (setq result (eval accept-form))
273 (kill-buffer (current-buffer))
276 (funcall nnmail-delete-file-function
277 (concat nnmh-current-directory (int-to-string article)))
281 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
282 (nnmh-possibly-change-directory group server)
285 (nnmail-activate 'nnmh)
286 ;; We trick the choosing function into believing that only one
287 ;; group is available.
288 (let ((nnmail-split-methods (list (list group ""))))
289 (car (nnmh-save-mail noinsert))))
291 (nnmail-activate 'nnmh)
292 (car (nnmh-save-mail noinsert)))))
294 (deffoo nnmh-request-replace-article (article group buffer)
295 (nnmh-possibly-change-directory group)
298 (nnmh-possibly-create-directory group)
301 (write-region (point-min) (point-max)
302 (concat nnmh-current-directory (int-to-string article))
303 nil (if (nnheader-be-verbose 5) nil 'nomesg))
307 (deffoo nnmh-request-create-group (group &optional server)
308 (nnmail-activate 'nnmh)
309 (or (assoc group nnmh-group-alist)
311 (setq nnmh-group-alist (cons (list group (setq active (cons 1 0)))
313 (nnmh-possibly-create-directory group)
314 (nnmh-possibly-change-directory group server)
315 (let ((articles (mapcar
317 (string-to-int file))
319 nnmh-current-directory nil "^[0-9]+$"))))
322 (setcar active (apply 'min articles))
323 (setcdr active (apply 'max articles)))))))
326 (deffoo nnmh-request-delete-group (group &optional force server)
327 (nnmh-possibly-change-directory group server)
328 ;; Delete all articles in GROUP.
330 () ; Don't delete the articles.
331 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
333 (and (file-writable-p (car articles))
335 (nnheader-message 5 "Deleting article %s in %s..."
336 (car articles) group)
337 (funcall nnmail-delete-file-function (car articles))))
338 (setq articles (cdr articles))))
339 ;; Try to delete the directory itself.
341 (delete-directory nnmh-current-directory)
343 ;; Remove the group from all structures.
344 (setq nnmh-group-alist
345 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
346 nnmh-current-directory nil)
349 (deffoo nnmh-request-rename-group (group new-name &optional server)
350 (nnmh-possibly-change-directory group server)
352 (and (file-writable-p nnmh-current-directory)
356 (directory-file-name nnmh-current-directory)
358 (nnmail-group-pathname new-name nnmh-directory)))
361 ;; That went ok, so we change the internal structures.
362 (let ((entry (assoc group nnmh-group-alist)))
363 (and entry (setcar entry new-name))
364 (setq nnmh-current-directory nil)
368 ;;; Internal functions.
370 (defun nnmh-possibly-change-directory (newsgroup &optional server)
372 (not (nnmh-server-opened server)))
373 (nnmh-open-server server))
375 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory)))
376 (if (file-directory-p pathname)
377 (setq nnmh-current-directory pathname)
378 (error "No such newsgroup: %s" newsgroup)))))
380 (defun nnmh-possibly-create-directory (group)
382 (setq dir (nnmail-group-pathname group nnmh-directory))
383 (while (not (file-directory-p dir))
384 (setq dirs (cons dir dirs))
385 (setq dir (file-name-directory (directory-file-name dir))))
387 (if (make-directory (directory-file-name (car dirs)))
388 (error "Could not create directory %s" (car dirs)))
389 (nnheader-message 5 "Creating mail directory %s" (car dirs))
390 (setq dirs (cdr dirs)))))
392 (defun nnmh-save-mail (&optional noinsert)
393 "Called narrowed to an article."
394 (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number))))
396 (nnmail-insert-lines)
397 (nnmail-insert-xref group-art))
398 (run-hooks 'nnmh-prepare-save-mail-hook)
399 (goto-char (point-min))
400 (while (looking-at "From ")
401 (replace-match "X-From-Line: ")
403 ;; We save the article in all the newsgroups it belongs in.
407 (nnmh-possibly-create-directory (caar ga))
408 (let ((file (concat (nnmail-group-pathname
409 (caar ga) nnmh-directory)
410 (int-to-string (cdar ga)))))
412 ;; It was already saved, so we just make a hard link.
413 (funcall nnmail-crosspost-link-function first file t)
415 (write-region (point-min) (point-max) file nil nil)
420 (defun nnmh-active-number (group)
421 "Compute the next article number in GROUP."
422 (let ((active (cadr (assoc group nnmh-group-alist))))
423 ;; The group wasn't known to nnmh, so we just create an active
427 (setq active (cons 1 0))
428 (setq nnmh-group-alist (cons (list group active) nnmh-group-alist))))
429 (setcdr active (1+ (cdr active)))
430 (while (file-exists-p
431 (concat (nnmail-group-pathname group nnmh-directory)
432 (int-to-string (cdr active))))
433 (setcdr active (1+ (cdr active))))
436 (defun nnmh-update-gnus-unreads (group)
437 ;; Go through the .nnmh-articles file and compare with the actual
438 ;; articles in this folder. The articles that are "new" will be
439 ;; marked as unread by Gnus.
440 (let* ((dir nnmh-current-directory)
441 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
442 (directory-files nnmh-current-directory
443 nil "^[0-9]+$" t)) '<))
444 (nnmh-file (concat dir ".nnmh-articles"))
446 ;; Load the .nnmh-articles file.
447 (if (file-exists-p nnmh-file)
449 (let (nnmh-newsgroup-articles)
450 (condition-case nil (load nnmh-file nil t t) (error nil))
451 nnmh-newsgroup-articles)))
452 ;; Add all new articles to the `new' list.
455 (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
456 (setq art (cdr art))))
457 ;; Remove all deleted articles.
458 (let ((art articles))
460 (if (not (memq (caar art) files))
461 (setq articles (delq (car art) articles)))
462 (setq art (cdr art))))
463 ;; Check whether the highest-numbered articles really are the ones
464 ;; that Gnus thinks they are by looking at the time-stamps.
465 (let ((art articles))
468 (nth 5 (file-attributes
469 (concat dir (int-to-string (caar art)))))
471 (setq articles (delq (car art) articles))
472 (setq new (cons (caar art) new))
473 (setq art (cdr art))))
474 ;; Go through all the new articles and add them, and their
475 ;; time-stamps to the list.
481 (nth 5 (file-attributes
482 (concat dir (int-to-string (car n))))))
485 ;; Make Gnus mark all new articles as unread.
486 (or (zerop (length new))
487 (gnus-make-articles-unread
488 (gnus-group-prefixed-name group (list 'nnmh ""))
489 (setq new (sort new '<))))
490 ;; Sort the article list with highest numbers first.
491 (setq articles (sort articles (lambda (art1 art2)
492 (> (car art1) (car art2)))))
493 ;; Finally write this list back to the .nnmh-articles file.
495 (set-buffer (get-buffer-create "*nnmh out*"))
496 (insert ";; Gnus article active file for " group "\n\n")
497 (insert "(setq nnmh-newsgroup-articles '")
498 (insert (prin1-to-string articles) ")\n")
499 (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
500 (kill-buffer (current-buffer)))))
502 (defun nnmh-deletable-article-p (group article)
503 "Say whether ARTICLE in GROUP can be deleted."
504 (let ((path (concat nnmh-current-directory (int-to-string article))))
505 (and (file-writable-p path)
506 (or (not nnmail-keep-last-article)
507 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
512 ;;; nnmh.el ends here