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)
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)
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)
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 last noinsert)
300 (nnmail-activate 'nnmh)
301 ;; We trick the choosing function into believing that only one
302 ;; group is available.
303 (let ((nnmail-split-methods (list (list group ""))))
304 (car (nnmh-save-mail noinsert))))
306 (nnmail-activate 'nnmh)
307 (car (nnmh-save-mail noinsert)))))
309 (defun nnmh-request-replace-article (article group buffer)
310 (nnmh-possibly-change-directory group)
313 (nnmh-possibly-create-directory group)
316 (write-region (point-min) (point-max)
317 (concat nnmh-current-directory (int-to-string article))
318 nil (if (nnheader-be-verbose 5) nil 'nomesg))
322 (defun nnmh-request-create-group (group &optional server)
323 (nnmail-activate 'nnmh)
324 (or (assoc group nnmh-group-alist)
326 (setq nnmh-group-alist (cons (list group (setq active (cons 1 0)))
328 (nnmh-possibly-create-directory group)
329 (nnmh-possibly-change-directory group)
330 (let ((articles (mapcar
332 (string-to-int file))
334 nnmh-current-directory nil "^[0-9]+$"))))
337 (setcar active (apply 'min articles))
338 (setcdr active (apply 'max articles)))))))
341 (defun nnmh-request-delete-group (group &optional force server)
342 (nnmh-possibly-change-directory group)
343 ;; Delete all articles in GROUP.
345 () ; Don't delete the articles.
346 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
348 (and (file-writable-p (car articles))
350 (nnheader-message 5 "Deleting article %s in %s..."
351 (car articles) group)
352 (funcall nnmail-delete-file-function (car articles))))
353 (setq articles (cdr articles))))
354 ;; Try to delete the directory itself.
356 (delete-directory nnmh-current-directory)
358 ;; Remove the group from all structures.
359 (setq nnmh-group-alist
360 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
361 nnmh-current-directory nil)
364 (defun nnmh-request-rename-group (group new-name &optional server)
365 (nnmh-possibly-change-directory group)
367 (and (file-writable-p nnmh-current-directory)
371 (directory-file-name nnmh-current-directory)
373 (nnmail-group-pathname new-name nnmh-directory)))
376 ;; That went ok, so we change the internal structures.
377 (let ((entry (assoc group nnmh-group-alist)))
378 (and entry (setcar entry new-name))
379 (setq nnmh-current-directory nil)
383 ;;; Internal functions.
385 (defun nnmh-possibly-change-directory (newsgroup)
387 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory)))
388 (if (file-directory-p pathname)
389 (setq nnmh-current-directory pathname)
390 (error "No such newsgroup: %s" newsgroup)))))
392 (defun nnmh-possibly-create-directory (group)
394 (setq dir (nnmail-group-pathname group nnmh-directory))
395 (while (not (file-directory-p dir))
396 (setq dirs (cons dir dirs))
397 (setq dir (file-name-directory (directory-file-name dir))))
399 (if (make-directory (directory-file-name (car dirs)))
400 (error "Could not create directory %s" (car dirs)))
401 (nnheader-message 5 "Creating mail directory %s" (car dirs))
402 (setq dirs (cdr dirs)))))
404 (defun nnmh-save-mail (&optional noinsert)
405 "Called narrowed to an article."
406 (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number))))
408 (nnmail-insert-lines)
409 (nnmail-insert-xref group-art))
410 (run-hooks 'nnmh-prepare-save-mail-hook)
411 (goto-char (point-min))
412 (while (looking-at "From ")
413 (replace-match "X-From-Line: ")
415 ;; We save the article in all the newsgroups it belongs in.
419 (nnmh-possibly-create-directory (caar ga))
420 (let ((file (concat (nnmail-group-pathname
421 (caar ga) nnmh-directory)
422 (int-to-string (cdar ga)))))
424 ;; It was already saved, so we just make a hard link.
425 (funcall nnmail-crosspost-link-function first file t)
427 (write-region (point-min) (point-max) file nil nil)
432 (defun nnmh-active-number (group)
433 "Compute the next article number in GROUP."
434 (let ((active (cadr (assoc group nnmh-group-alist))))
435 ;; The group wasn't known to nnmh, so we just create an active
439 (setq active (cons 1 0))
440 (setq nnmh-group-alist (cons (list group active) nnmh-group-alist))))
441 (setcdr active (1+ (cdr active)))
442 (while (file-exists-p
443 (concat (nnmail-group-pathname group nnmh-directory)
444 (int-to-string (cdr active))))
445 (setcdr active (1+ (cdr active))))
448 (defun nnmh-update-gnus-unreads (group)
449 ;; Go through the .nnmh-articles file and compare with the actual
450 ;; articles in this folder. The articles that are "new" will be
451 ;; marked as unread by Gnus.
452 (let* ((dir nnmh-current-directory)
453 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
454 (directory-files nnmh-current-directory
455 nil "^[0-9]+$" t)) '<))
456 (nnmh-file (concat dir ".nnmh-articles"))
458 ;; Load the .nnmh-articles file.
459 (if (file-exists-p nnmh-file)
461 (let (nnmh-newsgroup-articles)
462 (condition-case nil (load nnmh-file nil t t) (error nil))
463 nnmh-newsgroup-articles)))
464 ;; Add all new articles to the `new' list.
467 (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
468 (setq art (cdr art))))
469 ;; Remove all deleted articles.
470 (let ((art articles))
472 (if (not (memq (caar art) files))
473 (setq articles (delq (car art) articles)))
474 (setq art (cdr art))))
475 ;; Check whether the highest-numbered articles really are the ones
476 ;; that Gnus thinks they are by looking at the time-stamps.
477 (let ((art articles))
480 (nth 5 (file-attributes
481 (concat dir (int-to-string (caar art)))))
483 (setq articles (delq (car art) articles))
484 (setq new (cons (caar art) new))
485 (setq art (cdr art))))
486 ;; Go through all the new articles and add them, and their
487 ;; time-stamps to the list.
493 (nth 5 (file-attributes
494 (concat dir (int-to-string (car n))))))
497 ;; Make Gnus mark all new articles as unread.
498 (or (zerop (length new))
499 (gnus-make-articles-unread
500 (gnus-group-prefixed-name group (list 'nnmh ""))
501 (setq new (sort new '<))))
502 ;; Sort the article list with highest numbers first.
503 (setq articles (sort articles (lambda (art1 art2)
504 (> (car art1) (car art2)))))
505 ;; Finally write this list back to the .nnmh-articles file.
507 (set-buffer (get-buffer-create "*nnmh out*"))
508 (insert ";; Gnus article active file for " group "\n\n")
509 (insert "(setq nnmh-newsgroup-articles '")
510 (insert (prin1-to-string articles) ")\n")
511 (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
512 (kill-buffer (current-buffer)))))
514 (defun nnmh-deletable-article-p (group article)
515 "Say whether ARTICLE in GROUP can be deleted."
516 (let ((path (concat nnmh-current-directory (int-to-string article))))
517 (and (file-writable-p path)
518 (or (not nnmail-keep-last-article)
519 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
524 ;;; nnmh.el ends here