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
37 (eval-and-compile (require 'cl))
39 (defvar nnmh-directory "~/Mail/"
40 "*Mail spool directory.")
42 (defvar nnmh-get-new-mail t
43 "*If non-nil, nnmh will check the incoming mail file and split the mail.")
45 (defvar nnmh-prepare-save-mail-hook nil
46 "*Hook run narrowed to an article before saving.")
48 (defvar nnmh-be-safe nil
49 "*If non-nil, nnmh will check all articles to make sure whether they are new or not.")
53 (defconst nnmh-version "nnmh 1.0"
56 (defvar nnmh-current-directory nil
57 "Current news group directory.")
59 (defvar nnmh-status-string "")
60 (defvar nnmh-group-alist nil)
64 (defvar nnmh-current-server nil)
65 (defvar nnmh-server-alist nil)
66 (defvar nnmh-server-variables
67 `((nnmh-directory ,nnmh-directory)
68 (nnmh-get-new-mail ,nnmh-get-new-mail)
69 (nnmh-current-directory nil)
70 (nnmh-status-string "")
75 ;;; Interface functions.
77 (defun nnmh-retrieve-headers (articles &optional newsgroup server fetch-old)
79 (set-buffer nntp-server-buffer)
82 (number (length articles))
83 (large (and (numberp nnmail-large-newsgroup)
84 (> number nnmail-large-newsgroup)))
87 (nnmh-possibly-change-directory newsgroup)
88 ;; We don't support fetching by Message-ID.
89 (if (stringp (car articles))
92 (when (and (file-exists-p
93 (setq file (concat (file-name-as-directory
94 nnmh-current-directory)
96 (setq article (pop articles))))))
98 (not (file-directory-p file)))
99 (insert (format "221 %d Article retrieved.\n" article))
101 (nnheader-insert-head file)
103 (if (search-forward "\n\n" nil t)
105 (goto-char (point-max))
108 (delete-region (point) (point-max)))
109 (setq count (1+ count))
113 (message "nnmh: Receiving headers... %d%%"
114 (/ (* count 100) number))))
116 (and large (message "nnmh: Receiving headers...done"))
118 (nnheader-fold-continuation-lines)
121 (defun nnmh-open-server (server &optional defs)
122 (nnheader-change-server 'nnmh server defs)
123 (when (not (file-exists-p nnmh-directory))
125 (make-directory nnmh-directory t)
128 ((not (file-exists-p nnmh-directory))
130 (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
131 ((not (file-directory-p (file-truename nnmh-directory)))
133 (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
135 (nnheader-report 'nnmh "Opened server %s using directory %s"
136 server nnmh-directory)
139 (defun nnmh-close-server (&optional server)
140 (setq nnmh-current-server nil
141 nnmh-group-alist nil)
144 (defun nnmh-server-opened (&optional server)
145 (and (equal server nnmh-current-server)
147 (buffer-name nntp-server-buffer)))
149 (defun nnmh-status-message (&optional server)
152 (defun nnmh-request-article (id &optional newsgroup server buffer)
153 (nnmh-possibly-change-directory newsgroup)
154 (let ((file (if (stringp id)
156 (concat nnmh-current-directory (int-to-string id))))
157 (nntp-server-buffer (or buffer nntp-server-buffer)))
160 (not (file-directory-p file))
161 (save-excursion (nnmail-find-file file))
162 (string-to-int (file-name-nondirectory file)))))
164 (defun nnmh-request-group (group &optional server dont-check)
165 (let ((pathname (nnmail-group-pathname group nnmh-directory))
168 ((not (file-directory-p pathname))
170 'nnmh "Can't select group (no such directory): %s" group))
172 (setq nnmh-current-directory pathname)
173 (and nnmh-get-new-mail
175 (nnmh-update-gnus-unreads group))
178 (nnheader-report 'nnmh "Selected group %s" group)
183 (mapcar (lambda (name) (string-to-int name))
184 (directory-files pathname nil "^[0-9]+$" t))
188 (nnheader-report 'nnmh "Selected group %s" group)
190 "211 %d %d %d %s\n" (length dir) (car dir)
191 (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
194 (nnheader-report 'nnmh "Empty group %s" group)
195 (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
197 (defun nnmh-request-scan (&optional group server)
198 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
200 (defun nnmh-request-list (&optional server dir)
203 (setq dir (file-truename (file-name-as-directory nnmh-directory))))
204 (setq dir (expand-file-name dir))
205 ;; Recurse down all directories.
206 (let ((dirs (and (file-readable-p dir)
207 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
208 (directory-files dir t nil t)))
210 ;; Recurse down directories.
211 (while (setq dir (pop dirs))
212 (when (and (not (string-match "/\\.\\.?$" dir))
213 (file-directory-p dir)
214 (file-readable-p dir))
215 (nnmh-request-list nil dir))))
216 ;; For each directory, generate an active file line.
217 (unless (string= (expand-file-name nnmh-directory) dir)
219 (lambda (name) (string-to-int name))
220 (directory-files dir nil "^[0-9]+$" t))))
223 (set-buffer nntp-server-buffer)
224 (goto-char (point-max))
230 (file-truename (file-name-as-directory
231 (expand-file-name nnmh-directory))) dir)
232 (nnheader-replace-chars-in-string
233 (substring dir (match-end 0)) ?/ ?.))
234 (apply (function max) files)
235 (apply (function min) files)))))))
236 (setq nnmh-group-alist (nnmail-get-active))
239 (defun nnmh-request-newgroups (date &optional server)
240 (nnmh-request-list server))
242 (defun nnmh-request-expire-articles (articles newsgroup &optional server force)
243 (nnmh-possibly-change-directory newsgroup)
244 (let* ((active-articles
248 (string-to-int name)))
249 (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
250 (max-article (and active-articles (apply 'max active-articles)))
252 article rest mod-time)
253 (nnmail-activate 'nnmh)
255 (while (and articles is-old)
256 (setq article (concat nnmh-current-directory
257 (int-to-string (car articles))))
258 (if (setq mod-time (nth 5 (file-attributes article)))
259 (if (and (nnmh-deletable-article-p newsgroup (car articles))
261 (nnmail-expired-article-p newsgroup mod-time force)))
263 (nnheader-message 5 "Deleting article %s in %s..."
266 (funcall nnmail-delete-file-function article)
268 (setq rest (cons (car articles) rest)))))
269 (setq rest (cons (car articles) rest))))
270 (setq articles (cdr articles)))
272 (nconc rest articles)))
274 (defun nnmh-close-group (group &optional server)
277 (defun nnmh-request-move-article
278 (article group server accept-form &optional last)
279 (let ((buf (get-buffer-create " *nnmh move*"))
282 (nnmh-deletable-article-p group article)
283 (nnmh-request-article article group server)
286 (insert-buffer-substring nntp-server-buffer)
287 (setq result (eval accept-form))
288 (kill-buffer (current-buffer))
291 (funcall nnmail-delete-file-function
292 (concat nnmh-current-directory (int-to-string article)))
296 (defun nnmh-request-accept-article (group &optional last noinsert)
299 (nnmail-activate 'nnmh)
300 ;; We trick the choosing function into believing that only one
301 ;; group is available.
302 (let ((nnmail-split-methods (list (list group ""))))
303 (car (nnmh-save-mail noinsert))))
305 (nnmail-activate 'nnmh)
306 (car (nnmh-save-mail noinsert)))))
308 (defun nnmh-request-replace-article (article group buffer)
309 (nnmh-possibly-change-directory group)
312 (nnmh-possibly-create-directory group)
315 (write-region (point-min) (point-max)
316 (concat nnmh-current-directory (int-to-string article))
317 nil (if (nnheader-be-verbose 5) nil 'nomesg))
321 (defun nnmh-request-create-group (group &optional server)
322 (nnmail-activate 'nnmh)
323 (or (assoc group nnmh-group-alist)
325 (setq nnmh-group-alist (cons (list group (setq active (cons 1 0)))
327 (nnmh-possibly-create-directory group)
328 (nnmh-possibly-change-directory group)
329 (let ((articles (mapcar
331 (string-to-int file))
333 nnmh-current-directory nil "^[0-9]+$"))))
336 (setcar active (apply 'min articles))
337 (setcdr active (apply 'max articles)))))))
340 (defun nnmh-request-delete-group (group &optional force server)
341 (nnmh-possibly-change-directory group)
342 ;; Delete all articles in GROUP.
344 () ; Don't delete the articles.
345 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
347 (and (file-writable-p (car articles))
349 (nnheader-message 5 "Deleting article %s in %s..."
350 (car articles) group)
351 (funcall nnmail-delete-file-function (car articles))))
352 (setq articles (cdr articles))))
353 ;; Try to delete the directory itself.
355 (delete-directory nnmh-current-directory)
357 ;; Remove the group from all structures.
358 (setq nnmh-group-alist
359 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
360 nnmh-current-directory nil)
363 (defun nnmh-request-rename-group (group new-name &optional server)
364 (nnmh-possibly-change-directory group)
366 (and (file-writable-p nnmh-current-directory)
370 (directory-file-name nnmh-current-directory)
372 (nnmail-group-pathname new-name nnmh-directory)))
375 ;; That went ok, so we change the internal structures.
376 (let ((entry (assoc group nnmh-group-alist)))
377 (and entry (setcar entry new-name))
378 (setq nnmh-current-directory nil)
382 ;;; Internal functions.
384 (defun nnmh-possibly-change-directory (newsgroup)
386 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory)))
387 (if (file-directory-p pathname)
388 (setq nnmh-current-directory pathname)
389 (error "No such newsgroup: %s" newsgroup)))))
391 (defun nnmh-possibly-create-directory (group)
393 (setq dir (nnmail-group-pathname group nnmh-directory))
394 (while (not (file-directory-p dir))
395 (setq dirs (cons dir dirs))
396 (setq dir (file-name-directory (directory-file-name dir))))
398 (if (make-directory (directory-file-name (car dirs)))
399 (error "Could not create directory %s" (car dirs)))
400 (nnheader-message 5 "Creating mail directory %s" (car dirs))
401 (setq dirs (cdr dirs)))))
403 (defun nnmh-save-mail (&optional noinsert)
404 "Called narrowed to an article."
405 (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number))))
407 (nnmail-insert-lines)
408 (nnmail-insert-xref group-art))
409 (run-hooks 'nnmh-prepare-save-mail-hook)
410 (goto-char (point-min))
411 (while (looking-at "From ")
412 (replace-match "X-From-Line: ")
414 ;; We save the article in all the newsgroups it belongs in.
418 (nnmh-possibly-create-directory (car (car ga)))
419 (let ((file (concat (nnmail-group-pathname
420 (car (car ga)) nnmh-directory)
421 (int-to-string (cdr (car ga))))))
423 ;; It was already saved, so we just make a hard link.
424 (funcall nnmail-crosspost-link-function first file t)
426 (write-region (point-min) (point-max) file nil nil)
431 (defun nnmh-active-number (group)
432 "Compute the next article number in GROUP."
433 (let ((active (car (cdr (assoc group nnmh-group-alist)))))
434 ;; The group wasn't known to nnmh, so we just create an active
438 (setq active (cons 1 0))
439 (setq nnmh-group-alist (cons (list group active) nnmh-group-alist))))
440 (setcdr active (1+ (cdr active)))
441 (while (file-exists-p
442 (concat (nnmail-group-pathname group nnmh-directory)
443 (int-to-string (cdr active))))
444 (setcdr active (1+ (cdr active))))
447 (defun nnmh-update-gnus-unreads (group)
448 ;; Go through the .nnmh-articles file and compare with the actual
449 ;; articles in this folder. The articles that are "new" will be
450 ;; marked as unread by Gnus.
451 (let* ((dir nnmh-current-directory)
452 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
453 (directory-files nnmh-current-directory
454 nil "^[0-9]+$" t)) '<))
455 (nnmh-file (concat dir ".nnmh-articles"))
457 ;; Load the .nnmh-articles file.
458 (if (file-exists-p nnmh-file)
460 (let (nnmh-newsgroup-articles)
461 (condition-case nil (load nnmh-file nil t t) (error nil))
462 nnmh-newsgroup-articles)))
463 ;; Add all new articles to the `new' list.
466 (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
467 (setq art (cdr art))))
468 ;; Remove all deleted articles.
469 (let ((art articles))
471 (if (not (memq (car (car art)) files))
472 (setq articles (delq (car art) articles)))
473 (setq art (cdr art))))
474 ;; Check whether the highest-numbered articles really are the ones
475 ;; that Gnus thinks they are by looking at the time-stamps.
476 (let ((art articles))
479 (nth 5 (file-attributes
480 (concat dir (int-to-string (car (car art))))))
482 (setq articles (delq (car art) articles))
483 (setq new (cons (car (car art)) new))
484 (setq art (cdr art))))
485 ;; Go through all the new articles and add them, and their
486 ;; time-stamps to the list.
492 (nth 5 (file-attributes
493 (concat dir (int-to-string (car n))))))
496 ;; Make Gnus mark all new articles as unread.
497 (or (zerop (length new))
498 (gnus-make-articles-unread
499 (gnus-group-prefixed-name group (list 'nnmh ""))
500 (setq new (sort new '<))))
501 ;; Sort the article list with highest numbers first.
502 (setq articles (sort articles (lambda (art1 art2)
503 (> (car art1) (car art2)))))
504 ;; Finally write this list back to the .nnmh-articles file.
506 (set-buffer (get-buffer-create "*nnmh out*"))
507 (insert ";; Gnus article active file for " group "\n\n")
508 (insert "(setq nnmh-newsgroup-articles '")
509 (insert (prin1-to-string articles) ")\n")
510 (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
511 (kill-buffer (current-buffer)))))
513 (defun nnmh-deletable-article-p (group article)
514 "Say whether ARTICLE in GROUP can be deleted."
515 (let ((path (concat nnmh-current-directory (int-to-string article))))
516 (and (file-writable-p path)
517 (or (not nnmail-keep-last-article)
518 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
523 ;;; nnmh.el ends here