1 ;;; nnmh.el --- mhspool access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: news, mail
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
30 ;; For an overview of what the interface functions do, please see the
39 (eval-when-compile (require 'cl
))
43 (defvoo nnmh-directory message-directory
44 "Mail spool directory.")
46 (defvoo nnmh-get-new-mail t
47 "If non-nil, nnmh will check the incoming mail file and split the mail.")
49 (defvoo nnmh-prepare-save-mail-hook nil
50 "Hook run narrowed to an article before saving.")
52 (defvoo nnmh-be-safe nil
53 "If non-nil, nnmh will check all articles to make sure whether they are new or not.
54 Go through the .nnmh-articles file and compare with the actual
55 articles in this folder. The articles that are \"new\" will be marked
60 (defconst nnmh-version
"nnmh 1.0"
63 (defvoo nnmh-current-directory nil
64 "Current news group directory.")
66 (defvoo nnmh-status-string
"")
67 (defvoo nnmh-group-alist nil
)
68 ;; Don't even think about setting this variable. It does not exist.
69 ;; Forget about it. Uh-huh. Nope. Nobody here. It's only bound
70 ;; dynamically by certain functions in nndraft.
71 (defvar nnmh-allow-delete-final nil
)
75 ;;; Interface functions.
77 (nnoo-define-basics nnmh
)
79 (deffoo nnmh-retrieve-headers
(articles &optional newsgroup server fetch-old
)
80 (with-current-buffer nntp-server-buffer
83 (number (length articles
))
84 (large (and (numberp nnmail-large-newsgroup
)
85 (> number nnmail-large-newsgroup
)))
87 (file-name-coding-system nnmail-pathname-coding-system
)
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
))))))
99 (not (file-directory-p file
)))
100 (insert (format "221 %d Article retrieved.\n" article
))
102 (nnheader-insert-head file
)
104 (if (search-forward "\n\n" nil t
)
106 (goto-char (point-max))
109 (delete-region (point) (point-max)))
110 (setq count
(1+ count
))
114 (nnheader-message 5 "nnmh: Receiving headers... %d%%"
115 (/ (* count
100) number
))))
118 (nnheader-message 5 "nnmh: Receiving headers...done"))
120 (nnheader-fold-continuation-lines)
123 (deffoo nnmh-open-server
(server &optional defs
)
124 (nnoo-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 (deffoo nnmh-request-article
(id &optional newsgroup server buffer
)
142 (nnmh-possibly-change-directory newsgroup server
)
143 (let ((file (if (stringp id
)
145 (concat nnmh-current-directory
(int-to-string id
))))
146 (file-name-coding-system nnmail-pathname-coding-system
)
147 (nntp-server-buffer (or buffer nntp-server-buffer
)))
150 (not (file-directory-p file
))
151 (save-excursion (nnmail-find-file file
))
152 (string-to-number (file-name-nondirectory file
)))))
154 (deffoo nnmh-request-group
(group &optional server dont-check
)
155 (nnheader-init-server-buffer)
156 (nnmh-possibly-change-directory group server
)
157 (let ((pathname (nnmail-group-pathname group nnmh-directory
))
158 (file-name-coding-system nnmail-pathname-coding-system
)
161 ((not (file-directory-p pathname
))
163 'nnmh
"Can't select group (no such directory): %s" group
))
165 (setq nnmh-current-directory pathname
)
166 (and nnmh-get-new-mail
168 (nnmh-update-gnus-unreads group
))
171 (nnheader-report 'nnmh
"Selected group %s" group
)
174 ;; Re-scan the directory if it's on a foreign system.
175 (nnheader-re-read-dir pathname
)
178 (mapcar 'string-to-number
179 (directory-files pathname nil
"^[0-9]+$" t
))
183 (setq nnmh-group-alist
184 (delq (assoc group nnmh-group-alist
) nnmh-group-alist
))
185 (push (list group
(cons (car dir
) (car (last dir
))))
187 (nnheader-report 'nnmh
"Selected group %s" group
)
189 "211 %d %d %d %s\n" (length dir
) (car dir
)
190 (car (last dir
)) group
))
192 (nnheader-report 'nnmh
"Empty group %s" group
)
193 (nnheader-insert (format "211 0 1 0 %s\n" group
))))))))))
195 (deffoo nnmh-request-scan
(&optional group server
)
196 (nnmail-get-new-mail 'nnmh nil nnmh-directory group
))
198 (deffoo nnmh-request-list
(&optional server dir
)
200 (nnmh-possibly-change-directory nil server
)
201 (let ((file-name-coding-system nnmail-pathname-coding-system
)
203 (file-truename (or dir
(file-name-as-directory nnmh-directory
)))))
204 (nnmh-request-list-1 nnmh-toplev
))
205 (setq nnmh-group-alist
(nnmail-get-active))
209 (defun nnmh-request-list-1 (dir)
210 (setq dir
(expand-file-name dir
))
211 ;; Recurse down all directories.
212 (let ((dirs (and (file-readable-p dir
)
213 (nnheader-directory-files dir t nil t
)))
215 ;; Recurse down directories.
216 (while (setq rdir
(pop dirs
))
217 (when (and (file-directory-p rdir
)
218 (file-readable-p rdir
)
219 (not (equal (file-truename rdir
)
220 (file-truename dir
))))
221 (nnmh-request-list-1 rdir
))))
222 ;; For each directory, generate an active file line.
223 (unless (string= (expand-file-name nnmh-toplev
) dir
)
224 (let ((files (mapcar 'string-to-number
225 (directory-files dir nil
"^[0-9]+$" t
))))
227 (with-current-buffer nntp-server-buffer
228 (goto-char (point-max))
235 (file-truename (file-name-as-directory
236 (expand-file-name nnmh-toplev
))))
238 (mm-string-to-multibyte ;Why? Isn't it multibyte already?
239 (mm-encode-coding-string
240 (nnheader-replace-chars-in-string
241 (substring dir
(match-end 0))
243 nnmail-pathname-coding-system
)))
245 (apply 'min files
)))))))
248 (deffoo nnmh-request-newgroups
(date &optional server
)
249 (nnmh-request-list server
))
251 (deffoo nnmh-request-expire-articles
(articles newsgroup
252 &optional server force
)
253 (nnmh-possibly-change-directory newsgroup server
)
255 article rest mod-time
)
256 (nnheader-init-server-buffer)
258 (while (and articles is-old
)
259 (setq article
(concat nnmh-current-directory
260 (int-to-string (car articles
))))
261 (when (setq mod-time
(nth 5 (file-attributes article
)))
262 (if (and (nnmh-deletable-article-p newsgroup
(car articles
))
264 (nnmail-expired-article-p newsgroup mod-time force
)))
266 ;; Allow a special target group. -- jcn
267 (unless (eq nnmail-expiry-target
'delete
)
269 (nnmh-request-article (car articles
)
270 newsgroup server
(current-buffer))
271 (nnmail-expiry-target-group
272 nnmail-expiry-target newsgroup
)))
273 (nnheader-message 5 "Deleting article %s in %s..."
276 (funcall nnmail-delete-file-function article
)
278 (nnheader-message 1 "Couldn't delete article %s in %s"
280 (push (car articles
) rest
))))
281 (push (car articles
) rest
)))
282 (setq articles
(cdr articles
)))
283 (nnheader-message 5 "")
284 (nconc rest articles
)))
286 (deffoo nnmh-close-group
(group &optional server
)
289 (deffoo nnmh-request-move-article
(article group server accept-form
290 &optional last move-is-internal
)
291 (let ((buf (get-buffer-create " *nnmh move*"))
294 (nnmh-deletable-article-p group article
)
295 (nnmh-request-article article group server
)
296 (with-current-buffer buf
298 (insert-buffer-substring nntp-server-buffer
)
299 (setq result
(eval accept-form
))
300 (kill-buffer (current-buffer))
303 (nnmh-possibly-change-directory group server
)
305 (funcall nnmail-delete-file-function
306 (concat nnmh-current-directory
(int-to-string article
)))
310 (deffoo nnmh-request-accept-article
(group &optional server last noinsert
)
311 (nnmh-possibly-change-directory group server
)
312 (nnmail-check-syntax)
313 (when nnmail-cache-accepted-message-ids
314 (nnmail-cache-insert (nnmail-fetch-field "message-id")
316 (nnmail-fetch-field "subject")
317 (nnmail-fetch-field "from")))
318 (nnheader-init-server-buffer)
322 (nnmh-active-number group
)
324 (list (cons group
(nnmh-active-number group
)))
326 (let ((res (nnmail-article-group 'nnmh-active-number
)))
328 (yes-or-no-p "Moved to `junk' group; delete article? "))
330 (car (nnmh-save-mail res noinsert
)))))
331 (when (and last nnmail-cache-accepted-message-ids
)
332 (nnmail-cache-close))))
334 (deffoo nnmh-request-replace-article
(article group buffer
)
335 (nnmh-possibly-change-directory group
)
336 (with-current-buffer buffer
337 (nnmh-possibly-create-directory group
)
340 (point-min) (point-max)
341 (concat nnmh-current-directory
(int-to-string article
))
342 nil
(if (nnheader-be-verbose 5) nil
'nomesg
))
345 (deffoo nnmh-request-create-group
(group &optional server args
)
346 (nnheader-init-server-buffer)
347 (unless (assoc group nnmh-group-alist
)
349 (push (list group
(setq active
(cons 1 0)))
351 (nnmh-possibly-create-directory group
)
352 (nnmh-possibly-change-directory group server
)
353 (let ((articles (mapcar 'string-to-number
355 nnmh-current-directory nil
"^[0-9]+$"))))
357 (setcar active
(apply 'min articles
))
358 (setcdr active
(apply 'max articles
))))))
361 (deffoo nnmh-request-delete-group
(group &optional force server
)
362 (nnmh-possibly-change-directory group server
)
363 ;; Delete all articles in GROUP.
365 () ; Don't delete the articles.
366 (let ((articles (directory-files nnmh-current-directory t
"^[0-9]+$")))
368 (when (file-writable-p (car articles
))
369 (nnheader-message 5 "Deleting article %s in %s..."
370 (car articles
) group
)
371 (funcall nnmail-delete-file-function
(car articles
)))
372 (setq articles
(cdr articles
))))
373 ;; Try to delete the directory itself.
375 (delete-directory nnmh-current-directory
)))
376 ;; Remove the group from all structures.
377 (setq nnmh-group-alist
378 (delq (assoc group nnmh-group-alist
) nnmh-group-alist
)
379 nnmh-current-directory nil
)
382 (deffoo nnmh-request-rename-group
(group new-name
&optional server
)
383 (nnmh-possibly-change-directory group server
)
384 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory
))
385 (old-dir (nnmail-group-pathname group nnmh-directory
)))
387 (make-directory new-dir t
)
389 ;; We move the articles file by file instead of renaming
390 ;; the directory -- there may be subgroups in this group.
391 ;; One might be more clever, I guess.
392 (let ((files (nnheader-article-to-file-alist old-dir
)))
395 (concat old-dir
(cdar files
))
396 (concat new-dir
(cdar files
)))
398 (when (<= (length (directory-files old-dir
)) 2)
400 (delete-directory old-dir
)))
401 ;; That went ok, so we change the internal structures.
402 (let ((entry (assoc group nnmh-group-alist
)))
404 (setcar entry new-name
))
405 (setq nnmh-current-directory nil
)
408 (nnoo-define-skeleton nnmh
)
411 ;;; Internal functions.
413 (defun nnmh-possibly-change-directory (newsgroup &optional server
)
415 (not (nnmh-server-opened server
)))
416 (nnmh-open-server server
))
418 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory
))
419 (file-name-coding-system nnmail-pathname-coding-system
))
420 (if (file-directory-p pathname
)
421 (setq nnmh-current-directory pathname
)
422 (nnheader-report 'nnmh
"Not a directory: %s" nnmh-directory
)))))
424 (defun nnmh-possibly-create-directory (group)
426 (setq dir
(nnmail-group-pathname group nnmh-directory
))
427 (while (not (file-directory-p dir
))
429 (setq dir
(file-name-directory (directory-file-name dir
))))
431 (when (make-directory (directory-file-name (car dirs
)))
432 (error "Could not create directory %s" (car dirs
)))
433 (nnheader-message 5 "Creating mail directory %s" (car dirs
))
434 (setq dirs
(cdr dirs
)))))
436 (defun nnmh-save-mail (group-art &optional noinsert
)
437 "Called narrowed to an article."
439 (nnmail-insert-lines)
440 (nnmail-insert-xref group-art
))
441 (run-hooks 'nnmail-prepare-save-mail-hook
)
442 (run-hooks 'nnmh-prepare-save-mail-hook
)
443 (goto-char (point-min))
444 (while (looking-at "From ")
445 (replace-match "X-From-Line: ")
447 ;; We save the article in all the newsgroups it belongs in.
451 (nnmh-possibly-create-directory (caar ga
))
452 (let ((file (concat (nnmail-group-pathname
453 (caar ga
) nnmh-directory
)
454 (int-to-string (cdar ga
)))))
456 ;; It was already saved, so we just make a hard link.
457 (funcall nnmail-crosspost-link-function first file t
)
459 (nnmail-write-region (point-min) (point-max) file nil nil
)
464 (defun nnmh-active-number (group)
465 "Compute the next article number in GROUP."
466 (let ((active (cadr (assoc group nnmh-group-alist
)))
467 (dir (nnmail-group-pathname group nnmh-directory
))
468 (file-name-coding-system nnmail-pathname-coding-system
)
471 ;; The group wasn't known to nnmh, so we just create an active
473 (setq active
(cons 1 0))
474 (push (list group active
) nnmh-group-alist
)
475 (unless (file-exists-p dir
)
476 (gnus-make-directory dir
))
477 ;; Find the highest number in the group.
479 (mapcar 'string-to-number
480 (directory-files dir nil
"^[0-9]+$"))
483 (setcdr active
(car files
)))))
484 (setcdr active
(1+ (cdr active
)))
486 ;; See whether the file exists...
488 (setq file
(concat (nnmail-group-pathname group nnmh-directory
)
489 (int-to-string (cdr active
)))))
490 ;; ... or there is a buffer that will make that file exist
492 (get-file-buffer file
))
493 ;; Skip past that file.
494 (setcdr active
(1+ (cdr active
))))
497 (defun nnmh-update-gnus-unreads (group)
498 ;; Go through the .nnmh-articles file and compare with the actual
499 ;; articles in this folder. The articles that are "new" will be
500 ;; marked as unread by Gnus.
501 (let* ((dir nnmh-current-directory
)
502 (files (sort (mapcar 'string-to-number
503 (directory-files nnmh-current-directory
506 (nnmh-file (concat dir
".nnmh-articles"))
508 ;; Load the .nnmh-articles file.
509 (when (file-exists-p nnmh-file
)
511 (let (nnmh-newsgroup-articles)
512 (ignore-errors (load nnmh-file nil t t
))
513 nnmh-newsgroup-articles
)))
514 ;; Add all new articles to the `new' list.
517 (unless (assq (car art
) articles
)
518 (push (car art
) new
))
519 (setq art
(cdr art
))))
520 ;; Remove all deleted articles.
521 (let ((art articles
))
523 (unless (memq (caar art
) files
)
524 (setq articles
(delq (car art
) articles
)))
525 (setq art
(cdr art
))))
526 ;; Check whether the articles really are the ones that Gnus thinks
527 ;; they are by looking at the time-stamps.
528 (let ((arts articles
)
530 (while (setq art
(pop arts
))
532 (nth 5 (file-attributes
533 (concat dir
(int-to-string (car art
)))))
535 (setq articles
(delq art articles
))
536 (push (car art
) new
))))
537 ;; Go through all the new articles and add them, and their
538 ;; time-stamps, to the list.
544 (nth 5 (file-attributes
545 (concat dir
(int-to-string art
))))))
547 ;; Make Gnus mark all new articles as unread.
549 (gnus-make-articles-unread
550 (gnus-group-prefixed-name group
(list 'nnmh
""))
551 (setq new
(sort new
'<))))
552 ;; Sort the article list with highest numbers first.
553 (setq articles
(sort articles
(lambda (art1 art2
)
554 (> (car art1
) (car art2
)))))
555 ;; Finally write this list back to the .nnmh-articles file.
556 (with-temp-file nnmh-file
557 (insert ";; Gnus article active file for " group
"\n\n")
558 (insert "(setq nnmh-newsgroup-articles '")
559 (gnus-prin1 articles
)
562 (defun nnmh-deletable-article-p (group article
)
563 "Say whether ARTICLE in GROUP can be deleted."
564 (let ((path (concat nnmh-current-directory
(int-to-string article
))))
566 (and (file-writable-p path
)
568 ;; We can never delete the last article in the group.
569 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist
)))
572 nnmh-allow-delete-final
))))
576 ;; arch-tag: 36c12a98-3bad-44b3-9953-628078ef0e04
577 ;;; nnmh.el ends here