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)
70 (nnmh-prepare-save-mail-hook nil)
71 (nnmh-current-directory nil)
72 (nnmh-status-string "")
77 ;;; Interface functions.
79 (defun nnmh-retrieve-headers (articles &optional newsgroup server fetch-old)
81 (set-buffer nntp-server-buffer)
84 (number (length articles))
85 (large (and (numberp nnmail-large-newsgroup)
86 (> number nnmail-large-newsgroup)))
89 (nnmh-possibly-change-directory newsgroup server)
90 ;; We don't support fetching by Message-ID.
91 (if (stringp (car articles))
94 (when (and (file-exists-p
95 (setq file (concat (file-name-as-directory
96 nnmh-current-directory)
98 (setq article (pop articles))))))
100 (not (file-directory-p file)))
101 (insert (format "221 %d Article retrieved.\n" article))
103 (nnheader-insert-head file)
105 (if (search-forward "\n\n" nil t)
107 (goto-char (point-max))
110 (delete-region (point) (point-max)))
111 (setq count (1+ count))
115 (message "nnmh: Receiving headers... %d%%"
116 (/ (* count 100) number))))
118 (and large (message "nnmh: Receiving headers...done"))
120 (nnheader-fold-continuation-lines)
123 (defun nnmh-open-server (server &optional defs)
124 (nnheader-change-server 'nnmh server defs)
125 (when (not (file-exists-p nnmh-directory))
127 (make-directory nnmh-directory t)
130 ((not (file-exists-p nnmh-directory))
132 (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
133 ((not (file-directory-p (file-truename nnmh-directory)))
135 (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
137 (nnheader-report 'nnmh "Opened server %s using directory %s"
138 server nnmh-directory)
141 (defun nnmh-close-server (&optional server)
142 (setq nnmh-current-server nil
143 nnmh-group-alist nil)
146 (defun nnmh-server-opened (&optional server)
147 (and (equal server nnmh-current-server)
149 (buffer-name nntp-server-buffer)))
151 (defun nnmh-status-message (&optional server)
154 (defun nnmh-request-article (id &optional newsgroup server buffer)
155 (nnmh-possibly-change-directory newsgroup server)
156 (let ((file (if (stringp id)
158 (concat nnmh-current-directory (int-to-string id))))
159 (nntp-server-buffer (or buffer nntp-server-buffer)))
162 (not (file-directory-p file))
163 (save-excursion (nnmail-find-file file))
164 (string-to-int (file-name-nondirectory file)))))
166 (defun nnmh-request-group (group &optional server dont-check)
167 (let ((pathname (nnmail-group-pathname group nnmh-directory))
170 ((not (file-directory-p pathname))
172 'nnmh "Can't select group (no such directory): %s" group))
174 (setq nnmh-current-directory pathname)
175 (and nnmh-get-new-mail
177 (nnmh-update-gnus-unreads group))
180 (nnheader-report 'nnmh "Selected group %s" group)
185 (mapcar (lambda (name) (string-to-int name))
186 (directory-files pathname nil "^[0-9]+$" t))
190 (nnheader-report 'nnmh "Selected group %s" group)
192 "211 %d %d %d %s\n" (length dir) (car dir)
193 (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
196 (nnheader-report 'nnmh "Empty group %s" group)
197 (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
199 (defun nnmh-request-scan (&optional group server)
200 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
202 (defun nnmh-request-list (&optional server dir)
205 (setq dir (file-truename (file-name-as-directory nnmh-directory))))
206 (setq dir (expand-file-name dir))
207 ;; Recurse down all directories.
208 (let ((dirs (and (file-readable-p dir)
209 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
210 (directory-files dir t nil t)))
212 ;; Recurse down directories.
213 (while (setq dir (pop dirs))
214 (when (and (not (string-match "/\\.\\.?$" dir))
215 (file-directory-p dir)
216 (file-readable-p dir))
217 (nnmh-request-list nil dir))))
218 ;; For each directory, generate an active file line.
219 (unless (string= (expand-file-name nnmh-directory) dir)
221 (lambda (name) (string-to-int name))
222 (directory-files dir nil "^[0-9]+$" t))))
225 (set-buffer nntp-server-buffer)
226 (goto-char (point-max))
232 (file-truename (file-name-as-directory
233 (expand-file-name nnmh-directory))) dir)
234 (nnheader-replace-chars-in-string
235 (substring dir (match-end 0)) ?/ ?.))
236 (apply (function max) files)
237 (apply (function min) files)))))))
238 (setq nnmh-group-alist (nnmail-get-active))
241 (defun nnmh-request-newgroups (date &optional server)
242 (nnmh-request-list server))
244 (defun nnmh-request-expire-articles (articles newsgroup &optional server force)
245 (nnmh-possibly-change-directory newsgroup server)
246 (let* ((active-articles
250 (string-to-int name)))
251 (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
253 article rest mod-time)
254 (nnmail-activate 'nnmh)
256 (while (and articles is-old)
257 (setq article (concat nnmh-current-directory
258 (int-to-string (car articles))))
259 (if (setq mod-time (nth 5 (file-attributes article)))
260 (if (and (nnmh-deletable-article-p newsgroup (car articles))
262 (nnmail-expired-article-p newsgroup mod-time force)))
264 (nnheader-message 5 "Deleting article %s in %s..."
267 (funcall nnmail-delete-file-function article)
269 (setq rest (cons (car articles) rest)))))
270 (setq rest (cons (car articles) rest))))
271 (setq articles (cdr articles)))
273 (nconc rest articles)))
275 (defun nnmh-close-group (group &optional server)
278 (defun nnmh-request-move-article
279 (article group server accept-form &optional last)
280 (let ((buf (get-buffer-create " *nnmh move*"))
283 (nnmh-deletable-article-p group article)
284 (nnmh-request-article article group server)
287 (insert-buffer-substring nntp-server-buffer)
288 (setq result (eval accept-form))
289 (kill-buffer (current-buffer))
292 (funcall nnmail-delete-file-function
293 (concat nnmh-current-directory (int-to-string article)))
297 (defun nnmh-request-accept-article (group &optional server last noinsert)
298 (nnmh-possibly-change-directory group server)
301 (nnmail-activate 'nnmh)
302 ;; We trick the choosing function into believing that only one
303 ;; group is available.
304 (let ((nnmail-split-methods (list (list group ""))))
305 (car (nnmh-save-mail noinsert))))
307 (nnmail-activate 'nnmh)
308 (car (nnmh-save-mail noinsert)))))
310 (defun nnmh-request-replace-article (article group buffer)
311 (nnmh-possibly-change-directory group)
314 (nnmh-possibly-create-directory group)
317 (write-region (point-min) (point-max)
318 (concat nnmh-current-directory (int-to-string article))
319 nil (if (nnheader-be-verbose 5) nil 'nomesg))
323 (defun nnmh-request-create-group (group &optional server)
324 (nnmail-activate 'nnmh)
325 (or (assoc group nnmh-group-alist)
327 (setq nnmh-group-alist (cons (list group (setq active (cons 1 0)))
329 (nnmh-possibly-create-directory group)
330 (nnmh-possibly-change-directory group server)
331 (let ((articles (mapcar
333 (string-to-int file))
335 nnmh-current-directory nil "^[0-9]+$"))))
338 (setcar active (apply 'min articles))
339 (setcdr active (apply 'max articles)))))))
342 (defun nnmh-request-delete-group (group &optional force server)
343 (nnmh-possibly-change-directory group server)
344 ;; Delete all articles in GROUP.
346 () ; Don't delete the articles.
347 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
349 (and (file-writable-p (car articles))
351 (nnheader-message 5 "Deleting article %s in %s..."
352 (car articles) group)
353 (funcall nnmail-delete-file-function (car articles))))
354 (setq articles (cdr articles))))
355 ;; Try to delete the directory itself.
357 (delete-directory nnmh-current-directory)
359 ;; Remove the group from all structures.
360 (setq nnmh-group-alist
361 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
362 nnmh-current-directory nil)
365 (defun nnmh-request-rename-group (group new-name &optional server)
366 (nnmh-possibly-change-directory group server)
368 (and (file-writable-p nnmh-current-directory)
372 (directory-file-name nnmh-current-directory)
374 (nnmail-group-pathname new-name nnmh-directory)))
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 (&optional noinsert)
409 "Called narrowed to an article."
410 (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number))))
412 (nnmail-insert-lines)
413 (nnmail-insert-xref group-art))
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 (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))))
439 ;; The group wasn't known to nnmh, so we just create an active
443 (setq active (cons 1 0))
444 (setq nnmh-group-alist (cons (list group active) nnmh-group-alist))))
445 (setcdr active (1+ (cdr active)))
446 (while (file-exists-p
447 (concat (nnmail-group-pathname group nnmh-directory)
448 (int-to-string (cdr active))))
449 (setcdr active (1+ (cdr active))))
452 (defun nnmh-update-gnus-unreads (group)
453 ;; Go through the .nnmh-articles file and compare with the actual
454 ;; articles in this folder. The articles that are "new" will be
455 ;; marked as unread by Gnus.
456 (let* ((dir nnmh-current-directory)
457 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
458 (directory-files nnmh-current-directory
459 nil "^[0-9]+$" t)) '<))
460 (nnmh-file (concat dir ".nnmh-articles"))
462 ;; Load the .nnmh-articles file.
463 (if (file-exists-p nnmh-file)
465 (let (nnmh-newsgroup-articles)
466 (condition-case nil (load nnmh-file nil t t) (error nil))
467 nnmh-newsgroup-articles)))
468 ;; Add all new articles to the `new' list.
471 (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
472 (setq art (cdr art))))
473 ;; Remove all deleted articles.
474 (let ((art articles))
476 (if (not (memq (caar art) files))
477 (setq articles (delq (car art) articles)))
478 (setq art (cdr art))))
479 ;; Check whether the highest-numbered articles really are the ones
480 ;; that Gnus thinks they are by looking at the time-stamps.
481 (let ((art articles))
484 (nth 5 (file-attributes
485 (concat dir (int-to-string (caar art)))))
487 (setq articles (delq (car art) articles))
488 (setq new (cons (caar art) new))
489 (setq art (cdr art))))
490 ;; Go through all the new articles and add them, and their
491 ;; time-stamps to the list.
497 (nth 5 (file-attributes
498 (concat dir (int-to-string (car n))))))
501 ;; Make Gnus mark all new articles as unread.
502 (or (zerop (length new))
503 (gnus-make-articles-unread
504 (gnus-group-prefixed-name group (list 'nnmh ""))
505 (setq new (sort new '<))))
506 ;; Sort the article list with highest numbers first.
507 (setq articles (sort articles (lambda (art1 art2)
508 (> (car art1) (car art2)))))
509 ;; Finally write this list back to the .nnmh-articles file.
511 (set-buffer (get-buffer-create "*nnmh out*"))
512 (insert ";; Gnus article active file for " group "\n\n")
513 (insert "(setq nnmh-newsgroup-articles '")
514 (insert (prin1-to-string articles) ")\n")
515 (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
516 (kill-buffer (current-buffer)))))
518 (defun nnmh-deletable-article-p (group article)
519 "Say whether ARTICLE in GROUP can be deleted."
520 (let ((path (concat nnmh-current-directory (int-to-string article))))
521 (and (file-writable-p path)
522 (or (not nnmail-keep-last-article)
523 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
528 ;;; nnmh.el ends here