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 (nnmail-expiry-target
256 (or (gnus-group-find-parameter newsgroup
'expiry-target t
)
257 nnmail-expiry-target
))
258 article rest mod-time
)
259 (nnheader-init-server-buffer)
261 (while (and articles is-old
)
262 (setq article
(concat nnmh-current-directory
263 (int-to-string (car articles
))))
264 (when (setq mod-time
(nth 5 (file-attributes article
)))
265 (if (and (nnmh-deletable-article-p newsgroup
(car articles
))
267 (nnmail-expired-article-p newsgroup mod-time force
)))
269 ;; Allow a special target group. -- jcn
270 (unless (eq nnmail-expiry-target
'delete
)
272 (nnmh-request-article (car articles
)
273 newsgroup server
(current-buffer))
274 (nnmail-expiry-target-group
275 nnmail-expiry-target newsgroup
)))
276 (nnheader-message 5 "Deleting article %s in %s..."
279 (funcall nnmail-delete-file-function article
)
281 (nnheader-message 1 "Couldn't delete article %s in %s"
283 (push (car articles
) rest
))))
284 (push (car articles
) rest
)))
285 (setq articles
(cdr articles
)))
286 (nnheader-message 5 "")
287 (nconc rest articles
)))
289 (deffoo nnmh-close-group
(group &optional server
)
292 (deffoo nnmh-request-move-article
(article group server accept-form
293 &optional last move-is-internal
)
294 (let ((buf (get-buffer-create " *nnmh move*"))
297 (nnmh-deletable-article-p group article
)
298 (nnmh-request-article article group server
)
299 (with-current-buffer buf
301 (insert-buffer-substring nntp-server-buffer
)
302 (setq result
(eval accept-form
))
303 (kill-buffer (current-buffer))
306 (nnmh-possibly-change-directory group server
)
308 (funcall nnmail-delete-file-function
309 (concat nnmh-current-directory
(int-to-string article
)))
313 (deffoo nnmh-request-accept-article
(group &optional server last noinsert
)
314 (nnmh-possibly-change-directory group server
)
315 (nnmail-check-syntax)
316 (when nnmail-cache-accepted-message-ids
317 (nnmail-cache-insert (nnmail-fetch-field "message-id")
319 (nnmail-fetch-field "subject")
320 (nnmail-fetch-field "from")))
321 (nnheader-init-server-buffer)
325 (nnmh-active-number group
)
327 (list (cons group
(nnmh-active-number group
)))
329 (let ((res (nnmail-article-group 'nnmh-active-number
)))
331 (yes-or-no-p "Moved to `junk' group; delete article? "))
333 (car (nnmh-save-mail res noinsert
)))))
334 (when (and last nnmail-cache-accepted-message-ids
)
335 (nnmail-cache-close))))
337 (deffoo nnmh-request-replace-article
(article group buffer
)
338 (nnmh-possibly-change-directory group
)
339 (with-current-buffer buffer
340 (nnmh-possibly-create-directory group
)
343 (point-min) (point-max)
344 (concat nnmh-current-directory
(int-to-string article
))
345 nil
(if (nnheader-be-verbose 5) nil
'nomesg
))
348 (deffoo nnmh-request-create-group
(group &optional server args
)
349 (nnheader-init-server-buffer)
350 (unless (assoc group nnmh-group-alist
)
352 (push (list group
(setq active
(cons 1 0)))
354 (nnmh-possibly-create-directory group
)
355 (nnmh-possibly-change-directory group server
)
356 (let ((articles (mapcar 'string-to-number
358 nnmh-current-directory nil
"^[0-9]+$"))))
360 (setcar active
(apply 'min articles
))
361 (setcdr active
(apply 'max articles
))))))
364 (deffoo nnmh-request-delete-group
(group &optional force server
)
365 (nnmh-possibly-change-directory group server
)
366 ;; Delete all articles in GROUP.
368 () ; Don't delete the articles.
369 (let ((articles (directory-files nnmh-current-directory t
"^[0-9]+$")))
371 (when (file-writable-p (car articles
))
372 (nnheader-message 5 "Deleting article %s in %s..."
373 (car articles
) group
)
374 (funcall nnmail-delete-file-function
(car articles
)))
375 (setq articles
(cdr articles
))))
376 ;; Try to delete the directory itself.
378 (delete-directory nnmh-current-directory
)))
379 ;; Remove the group from all structures.
380 (setq nnmh-group-alist
381 (delq (assoc group nnmh-group-alist
) nnmh-group-alist
)
382 nnmh-current-directory nil
)
385 (deffoo nnmh-request-rename-group
(group new-name
&optional server
)
386 (nnmh-possibly-change-directory group server
)
387 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory
))
388 (old-dir (nnmail-group-pathname group nnmh-directory
)))
390 (make-directory new-dir t
)
392 ;; We move the articles file by file instead of renaming
393 ;; the directory -- there may be subgroups in this group.
394 ;; One might be more clever, I guess.
395 (let ((files (nnheader-article-to-file-alist old-dir
)))
398 (concat old-dir
(cdar files
))
399 (concat new-dir
(cdar files
)))
401 (when (<= (length (directory-files old-dir
)) 2)
403 (delete-directory old-dir
)))
404 ;; That went ok, so we change the internal structures.
405 (let ((entry (assoc group nnmh-group-alist
)))
407 (setcar entry new-name
))
408 (setq nnmh-current-directory nil
)
411 (nnoo-define-skeleton nnmh
)
414 ;;; Internal functions.
416 (defun nnmh-possibly-change-directory (newsgroup &optional server
)
418 (not (nnmh-server-opened server
)))
419 (nnmh-open-server server
))
421 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory
))
422 (file-name-coding-system nnmail-pathname-coding-system
))
423 (if (file-directory-p pathname
)
424 (setq nnmh-current-directory pathname
)
425 (nnheader-report 'nnmh
"Not a directory: %s" nnmh-directory
)))))
427 (defun nnmh-possibly-create-directory (group)
429 (setq dir
(nnmail-group-pathname group nnmh-directory
))
430 (while (not (file-directory-p dir
))
432 (setq dir
(file-name-directory (directory-file-name dir
))))
434 (when (make-directory (directory-file-name (car dirs
)))
435 (error "Could not create directory %s" (car dirs
)))
436 (nnheader-message 5 "Creating mail directory %s" (car dirs
))
437 (setq dirs
(cdr dirs
)))))
439 (defun nnmh-save-mail (group-art &optional noinsert
)
440 "Called narrowed to an article."
442 (nnmail-insert-lines)
443 (nnmail-insert-xref group-art
))
444 (run-hooks 'nnmail-prepare-save-mail-hook
)
445 (run-hooks 'nnmh-prepare-save-mail-hook
)
446 (goto-char (point-min))
447 (while (looking-at "From ")
448 (replace-match "X-From-Line: ")
450 ;; We save the article in all the newsgroups it belongs in.
454 (nnmh-possibly-create-directory (caar ga
))
455 (let ((file (concat (nnmail-group-pathname
456 (caar ga
) nnmh-directory
)
457 (int-to-string (cdar ga
)))))
459 ;; It was already saved, so we just make a hard link.
460 (funcall nnmail-crosspost-link-function first file t
)
462 (nnmail-write-region (point-min) (point-max) file nil nil
)
467 (defun nnmh-active-number (group)
468 "Compute the next article number in GROUP."
469 (let ((active (cadr (assoc group nnmh-group-alist
)))
470 (dir (nnmail-group-pathname group nnmh-directory
))
471 (file-name-coding-system nnmail-pathname-coding-system
)
474 ;; The group wasn't known to nnmh, so we just create an active
476 (setq active
(cons 1 0))
477 (push (list group active
) nnmh-group-alist
)
478 (unless (file-exists-p dir
)
479 (gnus-make-directory dir
))
480 ;; Find the highest number in the group.
482 (mapcar 'string-to-number
483 (directory-files dir nil
"^[0-9]+$"))
486 (setcdr active
(car files
)))))
487 (setcdr active
(1+ (cdr active
)))
489 ;; See whether the file exists...
491 (setq file
(concat (nnmail-group-pathname group nnmh-directory
)
492 (int-to-string (cdr active
)))))
493 ;; ... or there is a buffer that will make that file exist
495 (get-file-buffer file
))
496 ;; Skip past that file.
497 (setcdr active
(1+ (cdr active
))))
500 (defun nnmh-update-gnus-unreads (group)
501 ;; Go through the .nnmh-articles file and compare with the actual
502 ;; articles in this folder. The articles that are "new" will be
503 ;; marked as unread by Gnus.
504 (let* ((dir nnmh-current-directory
)
505 (files (sort (mapcar 'string-to-number
506 (directory-files nnmh-current-directory
509 (nnmh-file (concat dir
".nnmh-articles"))
511 ;; Load the .nnmh-articles file.
512 (when (file-exists-p nnmh-file
)
514 (let (nnmh-newsgroup-articles)
515 (ignore-errors (load nnmh-file nil t t
))
516 nnmh-newsgroup-articles
)))
517 ;; Add all new articles to the `new' list.
520 (unless (assq (car art
) articles
)
521 (push (car art
) new
))
522 (setq art
(cdr art
))))
523 ;; Remove all deleted articles.
524 (let ((art articles
))
526 (unless (memq (caar art
) files
)
527 (setq articles
(delq (car art
) articles
)))
528 (setq art
(cdr art
))))
529 ;; Check whether the articles really are the ones that Gnus thinks
530 ;; they are by looking at the time-stamps.
531 (let ((arts articles
)
533 (while (setq art
(pop arts
))
535 (nth 5 (file-attributes
536 (concat dir
(int-to-string (car art
)))))
538 (setq articles
(delq art articles
))
539 (push (car art
) new
))))
540 ;; Go through all the new articles and add them, and their
541 ;; time-stamps, to the list.
547 (nth 5 (file-attributes
548 (concat dir
(int-to-string art
))))))
550 ;; Make Gnus mark all new articles as unread.
552 (gnus-make-articles-unread
553 (gnus-group-prefixed-name group
(list 'nnmh
""))
554 (setq new
(sort new
'<))))
555 ;; Sort the article list with highest numbers first.
556 (setq articles
(sort articles
(lambda (art1 art2
)
557 (> (car art1
) (car art2
)))))
558 ;; Finally write this list back to the .nnmh-articles file.
559 (with-temp-file nnmh-file
560 (insert ";; Gnus article active file for " group
"\n\n")
561 (insert "(setq nnmh-newsgroup-articles '")
562 (gnus-prin1 articles
)
565 (defun nnmh-deletable-article-p (group article
)
566 "Say whether ARTICLE in GROUP can be deleted."
567 (let ((path (concat nnmh-current-directory
(int-to-string article
))))
569 (and (file-writable-p path
)
571 ;; We can never delete the last article in the group.
572 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist
)))
575 nnmh-allow-delete-final
))))
579 ;; arch-tag: 36c12a98-3bad-44b3-9953-628078ef0e04
580 ;;; nnmh.el ends here