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
))
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
)))
80 (nnmh-possibly-change-directory newsgroup server
)
81 ;; We don't support fetching by Message-ID.
82 (if (stringp (car articles
))
85 (when (and (file-exists-p
86 (setq file
(concat (file-name-as-directory
87 nnmh-current-directory
)
89 (setq article
(pop articles
))))))
90 (not (file-directory-p file
)))
91 (insert (format "221 %d Article retrieved.\n" article
))
93 (nnheader-insert-head file
)
95 (if (search-forward "\n\n" nil t
)
97 (goto-char (point-max))
100 (delete-region (point) (point-max)))
101 (setq count
(1+ count
))
105 (message "nnmh: Receiving headers... %d%%"
106 (/ (* count
100) number
))))
108 (and large
(message "nnmh: Receiving headers...done"))
110 (nnheader-fold-continuation-lines)
113 (deffoo nnmh-open-server
(server &optional defs
)
114 (nnoo-change-server 'nnmh server defs
)
115 (when (not (file-exists-p nnmh-directory
))
117 (make-directory nnmh-directory t
)
120 ((not (file-exists-p nnmh-directory
))
122 (nnheader-report 'nnmh
"Couldn't create directory: %s" nnmh-directory
))
123 ((not (file-directory-p (file-truename nnmh-directory
)))
125 (nnheader-report 'nnmh
"Not a directory: %s" nnmh-directory
))
127 (nnheader-report 'nnmh
"Opened server %s using directory %s"
128 server nnmh-directory
)
131 (deffoo nnmh-request-article
(id &optional newsgroup server buffer
)
132 (nnmh-possibly-change-directory newsgroup server
)
133 (let ((file (if (stringp id
)
135 (concat nnmh-current-directory
(int-to-string id
))))
136 (nntp-server-buffer (or buffer nntp-server-buffer
)))
139 (not (file-directory-p file
))
140 (save-excursion (nnmail-find-file file
))
141 (string-to-int (file-name-nondirectory file
)))))
143 (deffoo nnmh-request-group
(group &optional server dont-check
)
144 (let ((pathname (nnmail-group-pathname group nnmh-directory
))
147 ((not (file-directory-p pathname
))
149 'nnmh
"Can't select group (no such directory): %s" group
))
151 (setq nnmh-current-directory pathname
)
152 (and nnmh-get-new-mail
154 (nnmh-update-gnus-unreads group
))
157 (nnheader-report 'nnmh
"Selected group %s" group
)
162 (mapcar (lambda (name) (string-to-int name
))
163 (directory-files pathname nil
"^[0-9]+$" t
))
167 (nnheader-report 'nnmh
"Selected group %s" group
)
169 "211 %d %d %d %s\n" (length dir
) (car dir
)
170 (progn (while (cdr dir
) (setq dir
(cdr dir
))) (car dir
))
173 (nnheader-report 'nnmh
"Empty group %s" group
)
174 (nnheader-insert (format "211 0 1 0 %s\n" group
))))))))))
176 (deffoo nnmh-request-scan
(&optional group server
)
177 (nnmail-get-new-mail 'nnmh nil nnmh-directory group
))
179 (deffoo nnmh-request-list
(&optional server dir
)
182 (or dir
(file-truename (file-name-as-directory nnmh-directory
)))))
183 (nnmh-request-list-1 nnmh-toplev
))
184 (setq nnmh-group-alist
(nnmail-get-active))
188 (defun nnmh-request-list-1 (dir)
189 (setq dir
(expand-file-name dir
))
190 ;; Recurse down all directories.
191 (let ((dirs (and (file-readable-p dir
)
192 (> (nth 1 (file-attributes (file-chase-links dir
))) 2)
193 (directory-files dir t nil t
)))
195 ;; Recurse down directories.
196 (while (setq dir
(pop dirs
))
197 (when (and (not (member (file-name-nondirectory dir
) '("." "..")))
198 (file-directory-p dir
)
199 (file-readable-p dir
))
200 (nnmh-request-list-1 dir
))))
201 ;; For each directory, generate an active file line.
202 (unless (string= (expand-file-name nnmh-toplev
) dir
)
204 (lambda (name) (string-to-int name
))
205 (directory-files dir nil
"^[0-9]+$" t
))))
208 (set-buffer nntp-server-buffer
)
209 (goto-char (point-max))
216 (file-truename (file-name-as-directory
217 (expand-file-name nnmh-toplev
)))) dir
)
218 (nnheader-replace-chars-in-string
219 (substring dir
(match-end 0)) ?
/ ?.
))
221 (apply 'min files
)))))))
224 (deffoo nnmh-request-newgroups
(date &optional server
)
225 (nnmh-request-list server
))
227 (deffoo nnmh-request-expire-articles
(articles newsgroup
&optional server force
)
228 (nnmh-possibly-change-directory newsgroup server
)
229 (let* ((active-articles
233 (string-to-int name
)))
234 (directory-files nnmh-current-directory nil
"^[0-9]+$" t
)))
236 article rest mod-time
)
237 (nnmail-activate 'nnmh
)
239 (while (and articles is-old
)
240 (setq article
(concat nnmh-current-directory
241 (int-to-string (car articles
))))
242 (if (setq mod-time
(nth 5 (file-attributes article
)))
243 (if (and (nnmh-deletable-article-p newsgroup
(car articles
))
245 (nnmail-expired-article-p newsgroup mod-time force
)))
247 (nnheader-message 5 "Deleting article %s in %s..."
250 (funcall nnmail-delete-file-function article
)
252 (nnheader-message 1 "Couldn't delete article %s in %s"
254 (setq rest
(cons (car articles
) rest
)))))
255 (setq rest
(cons (car articles
) rest
))))
256 (setq articles
(cdr articles
)))
258 (nconc rest articles
)))
260 (deffoo nnmh-close-group
(group &optional server
)
263 (deffoo nnmh-request-move-article
264 (article group server accept-form
&optional last
)
265 (let ((buf (get-buffer-create " *nnmh move*"))
268 (nnmh-deletable-article-p group article
)
269 (nnmh-request-article article group server
)
272 (insert-buffer-substring nntp-server-buffer
)
273 (setq result
(eval accept-form
))
274 (kill-buffer (current-buffer))
277 (nnmh-possibly-change-directory group server
)
279 (funcall nnmail-delete-file-function
280 (concat nnmh-current-directory
(int-to-string article
)))
284 (deffoo nnmh-request-accept-article
(group &optional server last noinsert
)
285 (nnmh-possibly-change-directory group server
)
286 (nnmail-check-syntax)
289 (nnmail-activate 'nnmh
)
290 ;; We trick the choosing function into believing that only one
291 ;; group is available.
292 (let ((nnmail-split-methods (list (list group
""))))
293 (car (nnmh-save-mail noinsert
))))
295 (nnmail-activate 'nnmh
)
296 (car (nnmh-save-mail noinsert
)))))
298 (deffoo nnmh-request-replace-article
(article group buffer
)
299 (nnmh-possibly-change-directory group
)
302 (nnmh-possibly-create-directory group
)
306 (point-min) (point-max)
307 (concat nnmh-current-directory
(int-to-string article
))
308 nil
(if (nnheader-be-verbose 5) nil
'nomesg
))
312 (deffoo nnmh-request-create-group
(group &optional server
)
313 (nnmail-activate 'nnmh
)
314 (or (assoc group nnmh-group-alist
)
316 (setq nnmh-group-alist
(cons (list group
(setq active
(cons 1 0)))
318 (nnmh-possibly-create-directory group
)
319 (nnmh-possibly-change-directory group server
)
320 (let ((articles (mapcar
322 (string-to-int file
))
324 nnmh-current-directory nil
"^[0-9]+$"))))
327 (setcar active
(apply 'min articles
))
328 (setcdr active
(apply 'max articles
)))))))
331 (deffoo nnmh-request-delete-group
(group &optional force server
)
332 (nnmh-possibly-change-directory group server
)
333 ;; Delete all articles in GROUP.
335 () ; Don't delete the articles.
336 (let ((articles (directory-files nnmh-current-directory t
"^[0-9]+$")))
338 (and (file-writable-p (car articles
))
340 (nnheader-message 5 "Deleting article %s in %s..."
341 (car articles
) group
)
342 (funcall nnmail-delete-file-function
(car articles
))))
343 (setq articles
(cdr articles
))))
344 ;; Try to delete the directory itself.
346 (delete-directory nnmh-current-directory
)
348 ;; Remove the group from all structures.
349 (setq nnmh-group-alist
350 (delq (assoc group nnmh-group-alist
) nnmh-group-alist
)
351 nnmh-current-directory nil
)
354 (deffoo nnmh-request-rename-group
(group new-name
&optional server
)
355 (nnmh-possibly-change-directory group server
)
357 (and (file-writable-p nnmh-current-directory
)
361 (directory-file-name nnmh-current-directory
)
363 (nnmail-group-pathname new-name nnmh-directory
)))
366 ;; That went ok, so we change the internal structures.
367 (let ((entry (assoc group nnmh-group-alist
)))
368 (and entry
(setcar entry new-name
))
369 (setq nnmh-current-directory nil
)
373 ;;; Internal functions.
375 (defun nnmh-possibly-change-directory (newsgroup &optional server
)
377 (not (nnmh-server-opened server
)))
378 (nnmh-open-server server
))
380 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory
)))
381 (if (file-directory-p pathname
)
382 (setq nnmh-current-directory pathname
)
383 (error "No such newsgroup: %s" newsgroup
)))))
385 (defun nnmh-possibly-create-directory (group)
387 (setq dir
(nnmail-group-pathname group nnmh-directory
))
388 (while (not (file-directory-p dir
))
389 (setq dirs
(cons dir dirs
))
390 (setq dir
(file-name-directory (directory-file-name dir
))))
392 (if (make-directory (directory-file-name (car dirs
)))
393 (error "Could not create directory %s" (car dirs
)))
394 (nnheader-message 5 "Creating mail directory %s" (car dirs
))
395 (setq dirs
(cdr dirs
)))))
397 (defun nnmh-save-mail (&optional noinsert
)
398 "Called narrowed to an article."
399 (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number
))))
401 (nnmail-insert-lines)
402 (nnmail-insert-xref group-art
))
403 (run-hooks 'nnmail-prepare-save-mail-hook
)
404 (run-hooks 'nnmh-prepare-save-mail-hook
)
405 (goto-char (point-min))
406 (while (looking-at "From ")
407 (replace-match "X-From-Line: ")
409 ;; We save the article in all the newsgroups it belongs in.
413 (nnmh-possibly-create-directory (caar ga
))
414 (let ((file (concat (nnmail-group-pathname
415 (caar ga
) nnmh-directory
)
416 (int-to-string (cdar ga
)))))
418 ;; It was already saved, so we just make a hard link.
419 (funcall nnmail-crosspost-link-function first file t
)
421 (write-region (point-min) (point-max) file nil nil
)
426 (defun nnmh-active-number (group)
427 "Compute the next article number in GROUP."
428 (let ((active (cadr (assoc group nnmh-group-alist
))))
429 ;; The group wasn't known to nnmh, so we just create an active
433 (setq active
(cons 1 0))
434 (setq nnmh-group-alist
(cons (list group active
) nnmh-group-alist
))))
435 (setcdr active
(1+ (cdr active
)))
436 (while (file-exists-p
437 (concat (nnmail-group-pathname group nnmh-directory
)
438 (int-to-string (cdr active
))))
439 (setcdr active
(1+ (cdr active
))))
442 (defun nnmh-update-gnus-unreads (group)
443 ;; Go through the .nnmh-articles file and compare with the actual
444 ;; articles in this folder. The articles that are "new" will be
445 ;; marked as unread by Gnus.
446 (let* ((dir nnmh-current-directory
)
447 (files (sort (mapcar (function (lambda (name) (string-to-int name
)))
448 (directory-files nnmh-current-directory
449 nil
"^[0-9]+$" t
)) '<))
450 (nnmh-file (concat dir
".nnmh-articles"))
452 ;; Load the .nnmh-articles file.
453 (if (file-exists-p nnmh-file
)
455 (let (nnmh-newsgroup-articles)
456 (condition-case nil
(load nnmh-file nil t t
) (error nil
))
457 nnmh-newsgroup-articles
)))
458 ;; Add all new articles to the `new' list.
461 (if (not (assq (car art
) articles
)) (setq new
(cons (car art
) new
)))
462 (setq art
(cdr art
))))
463 ;; Remove all deleted articles.
464 (let ((art articles
))
466 (if (not (memq (caar art
) files
))
467 (setq articles
(delq (car art
) articles
)))
468 (setq art
(cdr art
))))
469 ;; Check whether the highest-numbered articles really are the ones
470 ;; that Gnus thinks they are by looking at the time-stamps.
471 (let ((art articles
))
474 (nth 5 (file-attributes
475 (concat dir
(int-to-string (caar art
)))))
477 (setq articles
(delq (car art
) articles
))
478 (setq new
(cons (caar art
) new
))
479 (setq art
(cdr art
))))
480 ;; Go through all the new articles and add them, and their
481 ;; time-stamps to the list.
487 (nth 5 (file-attributes
488 (concat dir
(int-to-string (car n
))))))
491 ;; Make Gnus mark all new articles as unread.
492 (or (zerop (length new
))
493 (gnus-make-articles-unread
494 (gnus-group-prefixed-name group
(list 'nnmh
""))
495 (setq new
(sort new
'<))))
496 ;; Sort the article list with highest numbers first.
497 (setq articles
(sort articles
(lambda (art1 art2
)
498 (> (car art1
) (car art2
)))))
499 ;; Finally write this list back to the .nnmh-articles file.
501 (set-buffer (get-buffer-create "*nnmh out*"))
502 (insert ";; Gnus article active file for " group
"\n\n")
503 (insert "(setq nnmh-newsgroup-articles '")
504 (insert (prin1-to-string articles
) ")\n")
505 (write-region (point-min) (point-max) nnmh-file nil
'nomesg
)
506 (kill-buffer (current-buffer)))))
508 (defun nnmh-deletable-article-p (group article
)
509 "Say whether ARTICLE in GROUP can be deleted."
510 (let ((path (concat nnmh-current-directory
(int-to-string article
))))
511 (and (file-writable-p path
)
512 (or (not nnmail-keep-last-article
)
513 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist
)))
518 ;;; nnmh.el ends here