1 ;;; nnml.el --- mail spool access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;; Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org> (adding MARKS)
6 ;; 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 2, 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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, 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
35 (eval-when-compile (require 'cl))
36 (eval-when-compile (require 'gnus-clfns))
44 (autoload 'gnus-article-unpropagatable-p "gnus-sum"))
48 (defvoo nnml-directory message-directory
49 "Spool directory for the nnml mail backend.")
51 (defvoo nnml-active-file
52 (expand-file-name "active" nnml-directory)
55 (defvoo nnml-newsgroups-file
56 (expand-file-name "newsgroups" nnml-directory)
57 "Mail newsgroups description file.")
59 (defvoo nnml-get-new-mail t
60 "If non-nil, nnml will check the incoming mail file and split the mail.")
62 (defvoo nnml-nov-is-evil nil
63 "If non-nil, Gnus will never generate and use nov databases for mail spools.
64 Using nov databases will speed up header fetching considerably.
65 This variable shouldn't be flipped much. If you have, for some reason,
66 set this to t, and want to set it to nil again, you should always run
67 the `nnml-generate-nov-databases' command. The function will go
68 through all nnml directories and generate nov databases for them
69 all. This may very well take some time.")
71 (defvoo nnml-marks-is-evil nil
72 "If non-nil, Gnus will never generate and use marks file for mail spools.
73 Using marks files makes it possible to backup and restore mail groups
74 separately from `.newsrc.eld'. If you have, for some reason, set this
75 to t, and want to set it to nil again, you should always remove the
76 corresponding marks file (usually named `.marks' in the nnml group
77 directory, but see `nnml-marks-file-name') for the group. Then the
78 marks file will be regenerated properly by Gnus.")
80 (defvoo nnml-prepare-save-mail-hook nil
81 "Hook run narrowed to an article before saving.")
83 (defvoo nnml-inhibit-expiry nil
84 "If non-nil, inhibit expiry.")
86 (defvoo nnml-use-compressed-files nil
87 "If non-nil, allow using compressed message files.")
91 (defconst nnml-version "nnml 1.0"
94 (defvoo nnml-nov-file-name ".overview")
95 (defvoo nnml-marks-file-name ".marks")
97 (defvoo nnml-current-directory nil)
98 (defvoo nnml-current-group nil)
99 (defvoo nnml-status-string "")
100 (defvoo nnml-nov-buffer-alist nil)
101 (defvoo nnml-group-alist nil)
102 (defvoo nnml-active-timestamp nil)
103 (defvoo nnml-article-file-alist nil)
105 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
107 (defvar nnml-nov-buffer-file-name nil)
109 (defvoo nnml-file-coding-system nnmail-file-coding-system)
111 (defvoo nnml-marks nil)
113 (defvar nnml-marks-modtime (gnus-make-hashtable))
116 ;;; Interface functions.
118 (nnoo-define-basics nnml)
120 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
121 (when (nnml-possibly-change-directory group server)
123 (set-buffer nntp-server-buffer)
126 (number (length sequence))
128 (file-name-coding-system nnmail-pathname-coding-system)
129 (pathname-coding-system nnmail-pathname-coding-system)
131 (if (stringp (car sequence))
133 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
136 (setq article (car sequence))
137 (setq file (nnml-article-to-file article))
140 (not (file-directory-p file)))
141 (insert (format "221 %d Article retrieved.\n" article))
143 (nnheader-insert-head file)
145 (if (re-search-forward "\n\r?\n" nil t)
147 (goto-char (point-max))
150 (delete-region (point) (point-max)))
151 (setq sequence (cdr sequence))
152 (setq count (1+ count))
153 (and (numberp nnmail-large-newsgroup)
154 (> number nnmail-large-newsgroup)
156 (nnheader-message 6 "nnml: Receiving headers... %d%%"
157 (/ (* count 100) number))))
159 (and (numberp nnmail-large-newsgroup)
160 (> number nnmail-large-newsgroup)
161 (nnheader-message 6 "nnml: Receiving headers...done"))
163 (nnheader-fold-continuation-lines)
166 (deffoo nnml-open-server (server &optional defs)
167 (nnoo-change-server 'nnml server defs)
168 (when (not (file-exists-p nnml-directory))
169 (ignore-errors (make-directory nnml-directory t)))
171 ((not (file-exists-p nnml-directory))
173 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
174 ((not (file-directory-p (file-truename nnml-directory)))
176 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
178 (nnheader-report 'nnml "Opened server %s using directory %s"
179 server nnml-directory)
182 (deffoo nnml-request-regenerate (server)
183 (nnml-possibly-change-directory nil server)
184 (nnml-generate-nov-databases server)
187 (deffoo nnml-request-article (id &optional group server buffer)
188 (nnml-possibly-change-directory group server)
189 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
190 (file-name-coding-system nnmail-pathname-coding-system)
191 (pathname-coding-system nnmail-pathname-coding-system)
192 path gpath group-num)
194 (when (and (setq group-num (nnml-find-group-number id))
196 (assq (cdr group-num)
197 (nnheader-article-to-file-alist
199 (nnmail-group-pathname
202 (setq path (concat gpath (int-to-string (cdr group-num)))))
203 (setq path (nnml-article-to-file id)))
206 (nnheader-report 'nnml "No such article: %s" id))
207 ((not (file-exists-p path))
208 (nnheader-report 'nnml "No such file: %s" path))
209 ((file-directory-p path)
210 (nnheader-report 'nnml "File is a directory: %s" path))
211 ((not (save-excursion (let ((nnmail-file-coding-system
212 nnml-file-coding-system))
213 (nnmail-find-file path))))
214 (nnheader-report 'nnml "Couldn't read file: %s" path))
216 (nnheader-report 'nnml "Article %s retrieved" id)
217 ;; We return the article number.
218 (cons (if group-num (car group-num) group)
219 (string-to-int (file-name-nondirectory path)))))))
221 (deffoo nnml-request-group (group &optional server dont-check)
222 (let ((file-name-coding-system nnmail-pathname-coding-system)
223 (pathname-coding-system nnmail-pathname-coding-system))
225 ((not (nnml-possibly-change-directory group server))
226 (nnheader-report 'nnml "Invalid group (no such directory)"))
227 ((not (file-exists-p nnml-current-directory))
228 (nnheader-report 'nnml "Directory %s does not exist"
229 nnml-current-directory))
230 ((not (file-directory-p nnml-current-directory))
231 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
233 (nnheader-report 'nnml "Group %s selected" group)
236 (nnheader-re-read-dir nnml-current-directory)
237 (nnmail-activate 'nnml)
238 (let ((active (nth 1 (assoc group nnml-group-alist))))
240 (nnheader-report 'nnml "No such group: %s" group)
241 (nnheader-report 'nnml "Selected group %s" group)
242 (nnheader-insert "211 %d %d %d %s\n"
243 (max (1+ (- (cdr active) (car active))) 0)
244 (car active) (cdr active) group)))))))
246 (deffoo nnml-request-scan (&optional group server)
247 (setq nnml-article-file-alist nil)
248 (nnml-possibly-change-directory group server)
249 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
251 (deffoo nnml-close-group (group &optional server)
252 (setq nnml-article-file-alist nil)
255 (deffoo nnml-request-create-group (group &optional server args)
256 (nnml-possibly-change-directory nil server)
257 (nnmail-activate 'nnml)
259 ((assoc group nnml-group-alist)
261 ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
262 (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
263 (nnheader-report 'nnml "%s is a file"
264 (nnmail-group-pathname group nnml-directory)))
267 (push (list group (setq active (cons 1 0)))
269 (nnml-possibly-create-directory group)
270 (nnml-possibly-change-directory group server)
271 (let ((articles (nnml-directory-articles nnml-current-directory)))
273 (setcar active (apply 'min articles))
274 (setcdr active (apply 'max articles))))
275 (nnmail-save-active nnml-group-alist nnml-active-file)
278 (deffoo nnml-request-list (&optional server)
280 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
281 (file-name-coding-system nnmail-pathname-coding-system)
282 (pathname-coding-system nnmail-pathname-coding-system))
283 (nnmail-find-file nnml-active-file))
284 (setq nnml-group-alist (nnmail-get-active))
287 (deffoo nnml-request-newgroups (date &optional server)
288 (nnml-request-list server))
290 (deffoo nnml-request-list-newsgroups (&optional server)
292 (nnmail-find-file nnml-newsgroups-file)))
294 (deffoo nnml-request-expire-articles (articles group &optional server force)
295 (nnml-possibly-change-directory group server)
296 (let ((active-articles
297 (nnml-directory-articles nnml-current-directory))
299 article rest mod-time number)
300 (nnmail-activate 'nnml)
302 (setq active-articles (sort active-articles '<))
303 ;; Articles not listed in active-articles are already gone,
304 ;; so don't try to expire them.
305 (setq articles (gnus-sorted-intersection articles active-articles))
307 (while (and articles is-old)
308 (if (and (setq article (nnml-article-to-file
309 (setq number (pop articles))))
310 (setq mod-time (nth 5 (file-attributes article)))
311 (nnml-deletable-article-p group number)
312 (setq is-old (nnmail-expired-article-p group mod-time force
313 nnml-inhibit-expiry)))
315 ;; Allow a special target group.
316 (unless (eq nnmail-expiry-target 'delete)
318 (nnml-request-article number group server (current-buffer))
319 (let (nnml-current-directory
321 nnml-article-file-alist)
322 (nnmail-expiry-target-group nnmail-expiry-target group)))
323 ;; Maybe directory is changed during nnmail-expiry-target-group.
324 (nnml-possibly-change-directory group server))
325 (nnheader-message 5 "Deleting article %s in %s"
328 (funcall nnmail-delete-file-function article)
331 (setq active-articles (delq number active-articles))
332 (nnml-nov-delete-article group number))
334 (let ((active (nth 1 (assoc group nnml-group-alist))))
336 (setcar active (or (and active-articles
337 (apply 'min active-articles))
339 (nnmail-save-active nnml-group-alist nnml-active-file))
341 (nconc rest articles)))
343 (deffoo nnml-request-move-article
344 (article group server accept-form &optional last)
345 (let ((buf (get-buffer-create " *nnml move*"))
347 (nnml-possibly-change-directory group server)
348 (nnml-update-file-alist)
350 (nnml-deletable-article-p group article)
351 (nnml-request-article article group server)
352 (let (nnml-current-directory
354 nnml-article-file-alist)
357 (insert-buffer-substring nntp-server-buffer)
358 (setq result (eval accept-form))
359 (kill-buffer (current-buffer))
362 (nnml-possibly-change-directory group server)
364 (funcall nnmail-delete-file-function
365 (nnml-article-to-file article))
367 (nnml-nov-delete-article group article)
370 (nnmail-save-active nnml-group-alist nnml-active-file))))
373 (deffoo nnml-request-accept-article (group &optional server last)
374 (nnml-possibly-change-directory group server)
375 (nnmail-check-syntax)
377 (when nnmail-cache-accepted-message-ids
378 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
381 (nnmail-activate 'nnml)
382 (setq result (car (nnml-save-mail
383 (list (cons group (nnml-active-number group))))))
385 (nnmail-save-active nnml-group-alist nnml-active-file)
386 (and last (nnml-save-nov))))
388 (nnmail-activate 'nnml)
389 (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
390 (yes-or-no-p "Moved to `junk' group; delete article? "))
392 (setq result (car (nnml-save-mail result))))
394 (nnmail-save-active nnml-group-alist nnml-active-file)
395 (when nnmail-cache-accepted-message-ids
396 (nnmail-cache-close))
400 (deffoo nnml-request-post (&optional server)
401 (nnmail-do-request-post 'nnml-request-accept-article server))
403 (deffoo nnml-request-replace-article (article group buffer)
404 (nnml-possibly-change-directory group)
407 (nnml-possibly-create-directory group)
408 (let ((chars (nnmail-insert-lines))
409 (art (concat (int-to-string article) "\t"))
413 (point-min) (point-max)
414 (or (nnml-article-to-file article)
415 (expand-file-name (int-to-string article)
416 nnml-current-directory))
417 nil (if (nnheader-be-verbose 5) nil 'nomesg))
419 (setq headers (nnml-parse-head chars article))
420 ;; Replace the NOV line in the NOV file.
422 (set-buffer (nnml-open-nov group))
423 (goto-char (point-min))
424 (if (or (looking-at art)
425 (search-forward (concat "\n" art) nil t))
426 ;; Delete the old NOV line.
427 (delete-region (progn (beginning-of-line) (point))
428 (progn (forward-line 1) (point)))
429 ;; The line isn't here, so we have to find out where
430 ;; we should insert it. (This situation should never
431 ;; occur, but one likes to make sure...)
432 (while (and (looking-at "[0-9]+\t")
435 (match-beginning 0) (match-end 0)))
437 (zerop (forward-line 1)))))
439 (nnheader-insert-nov headers)
443 (deffoo nnml-request-delete-group (group &optional force server)
444 (nnml-possibly-change-directory group server)
446 ;; Delete all articles in GROUP.
449 nnml-current-directory t
450 (concat nnheader-numerical-short-files
451 "\\|" (regexp-quote nnml-nov-file-name) "$"
452 "\\|" (regexp-quote nnml-marks-file-name) "$")))
455 (setq article (pop articles))
456 (when (file-writable-p article)
457 (nnheader-message 5 "Deleting article %s in %s..." article group)
458 (funcall nnmail-delete-file-function article))))
459 ;; Try to delete the directory itself.
460 (ignore-errors (delete-directory nnml-current-directory)))
461 ;; Remove the group from all structures.
462 (setq nnml-group-alist
463 (delq (assoc group nnml-group-alist) nnml-group-alist)
464 nnml-current-group nil
465 nnml-current-directory nil)
466 ;; Save the active file.
467 (nnmail-save-active nnml-group-alist nnml-active-file)
470 (deffoo nnml-request-rename-group (group new-name &optional server)
471 (nnml-possibly-change-directory group server)
472 (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
473 (old-dir (nnmail-group-pathname group nnml-directory)))
475 (make-directory new-dir t)
477 ;; We move the articles file by file instead of renaming
478 ;; the directory -- there may be subgroups in this group.
479 ;; One might be more clever, I guess.
480 (let ((files (nnheader-article-to-file-alist old-dir)))
483 (concat old-dir (cdar files))
484 (concat new-dir (cdar files)))
486 ;; Move .overview file.
487 (let ((overview (concat old-dir nnml-nov-file-name)))
488 (when (file-exists-p overview)
489 (rename-file overview (concat new-dir nnml-nov-file-name))))
491 (let ((marks (concat old-dir nnml-marks-file-name)))
492 (when (file-exists-p marks)
493 (rename-file marks (concat new-dir nnml-marks-file-name))))
494 (when (<= (length (directory-files old-dir)) 2)
495 (ignore-errors (delete-directory old-dir)))
496 ;; That went ok, so we change the internal structures.
497 (let ((entry (assoc group nnml-group-alist)))
499 (setcar entry new-name))
500 (setq nnml-current-directory nil
501 nnml-current-group nil)
502 ;; Save the new group alist.
503 (nnmail-save-active nnml-group-alist nnml-active-file)
506 (deffoo nnml-set-status (article name value &optional group server)
507 (nnml-possibly-change-directory group server)
508 (let ((file (nnml-article-to-file article)))
510 ((not (file-exists-p file))
511 (nnheader-report 'nnml "File %s does not exist" file))
514 (nnheader-insert-file-contents file)
515 (nnmail-replace-status name value))
519 ;;; Internal functions.
521 (defun nnml-article-to-file (article)
522 (nnml-update-file-alist)
525 (if nnml-use-compressed-files
526 (cdr (assq article nnml-article-file-alist))
527 (number-to-string article)))
528 (expand-file-name file nnml-current-directory)
529 (when (not nnheader-directory-files-is-safe)
530 ;; Just to make sure nothing went wrong when reading over NFS --
533 (setq file (expand-file-name (number-to-string article)
534 nnml-current-directory)))
535 (nnml-update-file-alist t)
538 (defun nnml-deletable-article-p (group article)
539 "Say whether ARTICLE in GROUP can be deleted."
541 (when (setq path (nnml-article-to-file article))
542 (when (file-writable-p path)
543 (or (not nnmail-keep-last-article)
544 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
547 ;; Find an article number in the current group given the Message-ID.
548 (defun nnml-find-group-number (id)
550 (set-buffer (get-buffer-create " *nnml id*"))
551 (let ((alist nnml-group-alist)
553 ;; We want to look through all .overview files, but we want to
554 ;; start with the one in the current directory. It seems most
555 ;; likely that the article we are looking for is in that group.
556 (if (setq number (nnml-find-id nnml-current-group id))
557 (cons nnml-current-group number)
558 ;; It wasn't there, so we look through the other groups as well.
559 (while (and (not number)
561 (or (string= (caar alist) nnml-current-group)
562 (setq number (nnml-find-id (caar alist) id)))
564 (setq alist (cdr alist))))
566 (cons (caar alist) number))))))
568 (defun nnml-find-id (group id)
570 (let ((nov (expand-file-name nnml-nov-file-name
571 (nnmail-group-pathname group nnml-directory)))
573 (when (file-exists-p nov)
574 (nnheader-insert-file-contents nov)
575 (while (and (not found)
576 (search-forward id nil t)) ; We find the ID.
577 ;; And the id is in the fourth field.
578 (if (not (and (search-backward "\t" nil t 4)
579 (not (search-backward"\t" (gnus-point-at-bol) t))))
583 ;; We return the article number.
585 (ignore-errors (read (current-buffer))))))
588 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
589 (if (or gnus-nov-is-evil nnml-nov-is-evil)
591 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
592 (when (file-exists-p nov)
594 (set-buffer nntp-server-buffer)
596 (nnheader-insert-file-contents nov)
598 (not (numberp fetch-old)))
599 t ; Don't remove anything.
600 (nnheader-nov-delete-outside-range
601 (if fetch-old (max 1 (- (car articles) fetch-old))
603 (car (last articles)))
606 (defun nnml-possibly-change-directory (group &optional server)
608 (not (nnml-server-opened server)))
609 (nnml-open-server server))
612 (let ((pathname (nnmail-group-pathname group nnml-directory))
613 (file-name-coding-system nnmail-pathname-coding-system)
614 (pathname-coding-system nnmail-pathname-coding-system))
615 (when (not (equal pathname nnml-current-directory))
616 (setq nnml-current-directory pathname
617 nnml-current-group group
618 nnml-article-file-alist nil))
619 (file-exists-p nnml-current-directory))))
621 (defun nnml-possibly-create-directory (group)
622 (let ((dir (nnmail-group-pathname group nnml-directory)))
623 (unless (file-exists-p dir)
624 (make-directory (directory-file-name dir) t)
625 (nnheader-message 5 "Creating mail directory %s" dir))))
627 (defun nnml-save-mail (group-art)
628 "Called narrowed to an article."
630 (setq chars (nnmail-insert-lines))
631 (nnmail-insert-xref group-art)
632 (run-hooks 'nnmail-prepare-save-mail-hook)
633 (run-hooks 'nnml-prepare-save-mail-hook)
634 (goto-char (point-min))
635 (while (looking-at "From ")
636 (replace-match "X-From-Line: ")
638 ;; We save the article in all the groups it belongs in.
642 (nnml-possibly-create-directory (caar ga))
643 (let ((file (concat (nnmail-group-pathname
644 (caar ga) nnml-directory)
645 (int-to-string (cdar ga)))))
647 ;; It was already saved, so we just make a hard link.
648 (funcall nnmail-crosspost-link-function first file t)
650 (nnmail-write-region (point-min) (point-max) file nil
651 (if (nnheader-be-verbose 5) nil 'nomesg))
654 ;; Generate a nov line for this article. We generate the nov
655 ;; line after saving, because nov generation destroys the
657 (setq headers (nnml-parse-head chars))
658 ;; Output the nov line to all nov databases that should have it.
659 (let ((ga group-art))
661 (nnml-add-nov (caar ga) (cdar ga) headers)
665 (defun nnml-active-number (group)
666 "Compute the next article number in GROUP."
667 (let ((active (cadr (assoc group nnml-group-alist))))
668 ;; The group wasn't known to nnml, so we just create an active
671 ;; Perhaps the active file was corrupt? See whether
672 ;; there are any articles in this group.
673 (nnml-possibly-create-directory group)
674 (nnml-possibly-change-directory group)
675 (unless nnml-article-file-alist
676 (setq nnml-article-file-alist
678 (nnml-current-group-article-to-file-alist)
679 'car-less-than-car)))
681 (if nnml-article-file-alist
682 (cons (caar nnml-article-file-alist)
683 (caar (last nnml-article-file-alist)))
685 (push (list group active) nnml-group-alist))
686 (setcdr active (1+ (cdr active)))
687 (while (file-exists-p
688 (expand-file-name (int-to-string (cdr active))
689 (nnmail-group-pathname group nnml-directory)))
690 (setcdr active (1+ (cdr active))))
693 (defun nnml-add-nov (group article headers)
694 "Add a nov line for the GROUP base."
696 (set-buffer (nnml-open-nov group))
697 (goto-char (point-max))
698 (mail-header-set-number headers article)
699 (nnheader-insert-nov headers)))
701 (defsubst nnml-header-value ()
702 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
704 (defun nnml-parse-head (chars &optional number)
705 "Parse the head of the current buffer."
708 (unless (zerop (buffer-size))
710 (goto-char (point-min))
711 (if (re-search-forward "\n\r?\n" nil t) (1- (point)) (point-max))))
712 ;; Fold continuation lines.
713 (goto-char (point-min))
714 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
715 (replace-match " " t t))
716 ;; Remove any tabs; they are too confusing.
717 (subst-char-in-region (point-min) (point-max) ?\t ? )
718 ;; Remove any ^M's; they are too confusing.
719 (subst-char-in-region (point-min) (point-max) ?\r ? )
720 (let ((headers (nnheader-parse-head t)))
721 (mail-header-set-chars headers chars)
722 (mail-header-set-number headers number)
725 (defun nnml-get-nov-buffer (group)
726 (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
729 (set (make-local-variable 'nnml-nov-buffer-file-name)
732 (nnmail-group-pathname group nnml-directory)))
734 (when (file-exists-p nnml-nov-buffer-file-name)
735 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
738 (defun nnml-open-nov (group)
739 (or (cdr (assoc group nnml-nov-buffer-alist))
740 (let ((buffer (nnml-get-nov-buffer group)))
741 (push (cons group buffer) nnml-nov-buffer-alist)
744 (defun nnml-save-nov ()
746 (while nnml-nov-buffer-alist
747 (when (buffer-name (cdar nnml-nov-buffer-alist))
748 (set-buffer (cdar nnml-nov-buffer-alist))
749 (when (buffer-modified-p)
750 (nnmail-write-region (point-min) (point-max)
751 nnml-nov-buffer-file-name nil 'nomesg))
752 (set-buffer-modified-p nil)
753 (kill-buffer (current-buffer)))
754 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
757 (defun nnml-generate-nov-databases (&optional server)
758 "Generate NOV databases in all nnml directories."
759 (interactive (list (or (nnoo-current-server 'nnml) "")))
760 ;; Read the active file to make sure we don't re-use articles
761 ;; numbers in empty groups.
762 (nnmail-activate 'nnml)
763 (unless (nnml-server-opened server)
764 (nnml-open-server server))
765 (setq nnml-directory (expand-file-name nnml-directory))
766 ;; Recurse down the directories.
767 (nnml-generate-nov-databases-1 nnml-directory nil t)
768 ;; Save the active file.
769 (nnmail-save-active nnml-group-alist nnml-active-file))
771 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
772 "Regenerate the NOV database in DIR."
773 (interactive "DRegenerate NOV in: ")
774 (setq dir (file-name-as-directory dir))
775 ;; Only scan this sub-tree if we haven't been here yet.
776 (unless (member (file-truename dir) seen)
777 (push (file-truename dir) seen)
778 ;; We descend recursively
779 (let ((dirs (directory-files dir t nil t))
781 (while (setq dir (pop dirs))
782 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
783 (file-directory-p dir))
784 (nnml-generate-nov-databases-1 dir seen))))
785 ;; Do this directory.
786 (let ((files (sort (nnheader-article-to-file-alist dir)
787 'car-less-than-car)))
789 (let* ((group (nnheader-file-to-group
790 (directory-file-name dir) nnml-directory))
791 (info (cadr (assoc group nnml-group-alist))))
793 (setcar info (1+ (cdr info)))))
794 (funcall nnml-generate-active-function dir)
795 ;; Generate the nov file.
796 (nnml-generate-nov-file dir files)
798 (nnmail-save-active nnml-group-alist nnml-active-file))))))
800 (eval-when-compile (defvar files))
801 (defun nnml-generate-active-info (dir)
802 ;; Update the active info for this group.
803 (let* ((group (nnheader-file-to-group
804 (directory-file-name dir) nnml-directory))
805 (entry (assoc group nnml-group-alist))
806 (last (or (caadr entry) 0)))
807 (setq nnml-group-alist (delq entry nnml-group-alist))
809 (cons (or (caar files) (1+ last))
812 (while (cdr f) (setq f (cdr f)))
817 (defun nnml-generate-nov-file (dir files)
818 (let* ((dir (file-name-as-directory dir))
819 (nov (concat dir nnml-nov-file-name))
820 (nov-buffer (get-buffer-create " *nov*"))
823 ;; Init the nov buffer.
824 (set-buffer nov-buffer)
825 (buffer-disable-undo)
827 (set-buffer nntp-server-buffer)
828 ;; Delete the old NOV file.
829 (when (file-exists-p nov)
830 (funcall nnmail-delete-file-function nov))
832 (unless (file-directory-p (setq file (concat dir (cdar files))))
834 (nnheader-insert-file-contents file)
836 (goto-char (point-min))
838 (re-search-forward "\n\r?\n" nil t)
839 (setq chars (- (point-max) (point)))
840 (max (point-min) (1- (point)))))
841 (unless (zerop (buffer-size))
842 (goto-char (point-min))
843 (setq headers (nnml-parse-head chars (caar files)))
845 (set-buffer nov-buffer)
846 (goto-char (point-max))
847 (nnheader-insert-nov headers)))
849 (setq files (cdr files)))
851 (set-buffer nov-buffer)
852 (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
853 (kill-buffer (current-buffer))))))
855 (defun nnml-nov-delete-article (group article)
857 (set-buffer (nnml-open-nov group))
858 (when (nnheader-find-nov-line article)
859 (delete-region (point) (progn (forward-line 1) (point)))
861 (let ((active (cadr (assoc group nnml-group-alist)))
865 (setf (car active) (1+ (cdr active)))
866 (when (and (setq num (ignore-errors (read (current-buffer))))
868 (setf (car active) num)))))))
871 (defun nnml-update-file-alist (&optional force)
872 (when nnml-use-compressed-files
873 (when (or (not nnml-article-file-alist)
875 (setq nnml-article-file-alist
876 (nnml-current-group-article-to-file-alist)))))
878 (defun nnml-directory-articles (dir)
879 "Return a list of all article files in a directory.
880 Use the nov database for that directory if available."
881 (if (or gnus-nov-is-evil nnml-nov-is-evil
883 (expand-file-name nnml-nov-file-name dir))))
884 (nnheader-directory-articles dir)
885 ;; build list from .overview if available
886 ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
887 ;; defvoo'd, and we might get called when it hasn't been swapped in.
891 (buffer (nnml-get-nov-buffer nnml-current-group)))
893 (goto-char (point-min))
895 (setq art (read (current-buffer)))
900 (defun nnml-current-group-article-to-file-alist ()
901 "Return an alist of article/file pairs in the current group.
902 Use the nov database for the current group if available."
903 (if (or gnus-nov-is-evil
906 (expand-file-name nnml-nov-file-name
907 nnml-current-directory))))
908 (nnheader-article-to-file-alist nnml-current-directory)
909 ;; build list from .overview if available
912 (buffer (nnml-get-nov-buffer nnml-current-group))
915 (goto-char (point-min))
917 (setq art (read (current-buffer)))
918 ;; assume file name is unadorned (ie. not compressed etc)
919 (push (cons art (int-to-string art)) alist)
923 (deffoo nnml-request-set-mark (group actions &optional server)
924 (nnml-possibly-change-directory group server)
925 (unless nnml-marks-is-evil
926 (nnml-open-marks group server)
927 (dolist (action actions)
928 (let ((range (nth 0 action))
929 (what (nth 1 action))
930 (marks (nth 2 action)))
931 (assert (or (eq what 'add) (eq what 'del)) t
932 "Unknown request-set-mark action: %s" what)
934 (setq nnml-marks (gnus-update-alist-soft
936 (funcall (if (eq what 'add) 'gnus-range-add
937 'gnus-remove-from-range)
938 (cdr (assoc mark nnml-marks)) range)
940 (nnml-save-marks group server))
943 (deffoo nnml-request-update-info (group info &optional server)
944 (nnml-possibly-change-directory group server)
945 (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group))
946 (nnheader-message 8 "Updating marks for %s..." group)
947 (nnml-open-marks group server)
948 ;; Update info using `nnml-marks'.
949 (mapcar (lambda (pred)
950 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
953 (gnus-update-alist-soft
955 (cdr (assq (cdr pred) nnml-marks))
956 (gnus-info-marks info))
958 gnus-article-mark-lists)
959 (let ((seen (cdr (assq 'read nnml-marks))))
960 (gnus-info-set-read info
961 (if (and (integerp (car seen))
963 (list (cons (car seen) (car seen)))
965 (nnheader-message 8 "Updating marks for %s...done" group))
968 (defun nnml-marks-changed-p (group)
969 (let ((file (expand-file-name nnml-marks-file-name
970 (nnmail-group-pathname group nnml-directory))))
971 (if (null (gnus-gethash file nnml-marks-modtime))
972 t ;; never looked at marks file, assume it has changed
973 (not (equal (gnus-gethash file nnml-marks-modtime)
974 (nth 5 (file-attributes file)))))))
976 (defun nnml-save-marks (group server)
977 (let ((file-name-coding-system nnmail-pathname-coding-system)
978 (file (expand-file-name nnml-marks-file-name
979 (nnmail-group-pathname group nnml-directory))))
982 (nnml-possibly-create-directory group)
985 (gnus-prin1 nnml-marks)
988 (nth 5 (file-attributes file))
990 (error (or (gnus-yes-or-no-p
991 (format "Could not write to %s (%s). Continue? " file err))
992 (error "Cannot write to %s (%s)" err))))))
994 (defun nnml-open-marks (group server)
995 (let ((file (expand-file-name
997 (nnmail-group-pathname group nnml-directory))))
998 (if (file-exists-p file)
1001 (gnus-sethash file (nth 5 (file-attributes file))
1003 (nnheader-insert-file-contents file)
1004 (setq nnml-marks (read (current-buffer)))
1005 (dolist (el gnus-article-unpropagated-mark-lists)
1006 (setq nnml-marks (gnus-remassoc el nnml-marks))))
1007 (error (or (gnus-yes-or-no-p
1008 (format "Error reading nnml marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1009 (error "Cannot read nnml marks file %s (%s)" file err))))
1010 ;; User didn't have a .marks file. Probably first time
1011 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1012 (let ((info (gnus-get-info
1013 (gnus-group-prefixed-name
1015 (gnus-server-to-method (format "nnml:%s" server))))))
1016 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1017 (setq nnml-marks (gnus-info-marks info))
1018 (push (cons 'read (gnus-info-read info)) nnml-marks)
1019 (dolist (el gnus-article-unpropagated-mark-lists)
1020 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1021 (nnml-save-marks group server)
1022 (nnheader-message 7 "Bootstrapping marks for %s...done" group)))))
1026 ;;; nnml.el ends here