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
39 (eval-when-compile (require 'cl))
43 (defvoo nnml-directory message-directory
44 "Spool directory for the nnml mail backend.
46 This variable is a virtual server slot. See the Gnus manual for details.")
48 (defvoo nnml-active-file
49 (expand-file-name "active" nnml-directory)
52 This variable is a virtual server slot. See the Gnus manual for details.")
54 (defvoo nnml-newsgroups-file
55 (expand-file-name "newsgroups" nnml-directory)
56 "Mail newsgroups description file.
58 This variable is a virtual server slot. See the Gnus manual for details.")
60 (defvoo nnml-get-new-mail t
61 "If non-nil, nnml will check the incoming mail file and split the mail.
63 This variable is a virtual server slot. See the Gnus manual for details.")
65 (defvoo nnml-nov-is-evil nil
66 "If non-nil, Gnus will never generate and use nov databases for mail spools.
67 Using nov databases will speed up header fetching considerably.
68 This variable shouldn't be flipped much. If you have, for some reason,
69 set this to t, and want to set it to nil again, you should always run
70 the `nnml-generate-nov-databases' command. The function will go
71 through all nnml directories and generate nov databases for them
72 all. This may very well take some time.
74 This variable is a virtual server slot. See the Gnus manual for details.")
76 (defvoo nnml-marks-is-evil nil
77 "If non-nil, Gnus will never generate and use marks file for mail spools.
78 Using marks files makes it possible to backup and restore mail groups
79 separately from `.newsrc.eld'. If you have, for some reason, set this
80 to t, and want to set it to nil again, you should always remove the
81 corresponding marks file (usually named `.marks' in the nnml group
82 directory, but see `nnml-marks-file-name') for the group. Then the
83 marks file will be regenerated properly by Gnus.
85 This variable is a virtual server slot. See the Gnus manual for details.")
87 (defvoo nnml-prepare-save-mail-hook nil
88 "Hook run narrowed to an article before saving.
90 This variable is a virtual server slot. See the Gnus manual for details.")
92 (defvoo nnml-inhibit-expiry nil
93 "If non-nil, inhibit expiry.
95 This variable is a virtual server slot. See the Gnus manual for details.")
100 (defconst nnml-version "nnml 1.0"
103 (defvoo nnml-nov-file-name ".overview")
104 (defvoo nnml-marks-file-name ".marks")
106 (defvoo nnml-current-directory nil)
107 (defvoo nnml-current-group nil)
108 (defvoo nnml-status-string "")
109 (defvoo nnml-nov-buffer-alist nil)
110 (defvoo nnml-group-alist nil)
111 (defvoo nnml-active-timestamp nil)
112 (defvoo nnml-article-file-alist nil)
114 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
116 (defvar nnml-nov-buffer-file-name nil)
117 (defvar nnml-check-directory-twice t
118 "If t, to make sure nothing went wrong when reading over NFS --
121 (defvoo nnml-file-coding-system nnmail-file-coding-system)
123 (defvoo nnml-marks nil)
127 ;;; Interface functions.
129 (nnoo-define-basics nnml)
131 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
132 (when (nnml-possibly-change-directory group server)
134 (set-buffer nntp-server-buffer)
137 (number (length sequence))
139 (file-name-coding-system nnmail-pathname-coding-system)
141 (nnml-check-directory-twice
142 (and nnml-check-directory-twice
143 ;; To speed up, disable it in some case.
144 (or (not (numberp nnmail-large-newsgroup))
145 (<= number nnmail-large-newsgroup)))))
146 (if (stringp (car sequence))
148 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
151 (setq article (car sequence))
152 (setq file (nnml-article-to-file article))
155 (not (file-directory-p file)))
156 (insert (format "221 %d Article retrieved.\n" article))
158 (nnheader-insert-head file)
160 (if (search-forward "\n\n" nil t)
162 (goto-char (point-max))
165 (delete-region (point) (point-max)))
166 (setq sequence (cdr sequence))
167 (setq count (1+ count))
168 (and (numberp nnmail-large-newsgroup)
169 (> number nnmail-large-newsgroup)
171 (nnheader-message 6 "nnml: Receiving headers... %d%%"
172 (/ (* count 100) number))))
174 (and (numberp nnmail-large-newsgroup)
175 (> number nnmail-large-newsgroup)
176 (nnheader-message 6 "nnml: Receiving headers...done"))
178 (nnheader-fold-continuation-lines)
181 (deffoo nnml-open-server (server &optional defs)
182 (nnoo-change-server 'nnml server defs)
183 (when (not (file-exists-p nnml-directory))
184 (ignore-errors (make-directory nnml-directory t)))
186 ((not (file-exists-p nnml-directory))
188 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
189 ((not (file-directory-p (file-truename nnml-directory)))
191 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
193 (nnheader-report 'nnml "Opened server %s using directory %s"
194 server nnml-directory)
197 (deffoo nnml-request-regenerate (server)
198 (nnml-possibly-change-directory nil server)
199 (nnml-generate-nov-databases server)
202 (deffoo nnml-request-article (id &optional group server buffer)
203 (nnml-possibly-change-directory group server)
204 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
205 (file-name-coding-system nnmail-pathname-coding-system)
206 path gpath group-num)
208 (when (and (setq group-num (nnml-find-group-number id))
210 (assq (cdr group-num)
211 (nnheader-article-to-file-alist
213 (nnmail-group-pathname
216 (setq path (concat gpath (int-to-string (cdr group-num)))))
217 (setq path (nnml-article-to-file id)))
220 (nnheader-report 'nnml "No such article: %s" id))
221 ((not (file-exists-p path))
222 (nnheader-report 'nnml "No such file: %s" path))
223 ((file-directory-p path)
224 (nnheader-report 'nnml "File is a directory: %s" path))
225 ((not (save-excursion (let ((nnmail-file-coding-system
226 nnml-file-coding-system))
227 (nnmail-find-file path))))
228 (nnheader-report 'nnml "Couldn't read file: %s" path))
230 (nnheader-report 'nnml "Article %s retrieved" id)
231 ;; We return the article number.
232 (cons (if group-num (car group-num) group)
233 (string-to-int (file-name-nondirectory path)))))))
235 (deffoo nnml-request-group (group &optional server dont-check)
236 (let ((file-name-coding-system nnmail-pathname-coding-system))
238 ((not (nnml-possibly-change-directory group server))
239 (nnheader-report 'nnml "Invalid group (no such directory)"))
240 ((not (file-exists-p nnml-current-directory))
241 (nnheader-report 'nnml "Directory %s does not exist"
242 nnml-current-directory))
243 ((not (file-directory-p nnml-current-directory))
244 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
246 (nnheader-report 'nnml "Group %s selected" group)
249 (nnheader-re-read-dir nnml-current-directory)
250 (nnmail-activate 'nnml)
251 (let ((active (nth 1 (assoc group nnml-group-alist))))
253 (nnheader-report 'nnml "No such group: %s" group)
254 (nnheader-report 'nnml "Selected group %s" group)
255 (nnheader-insert "211 %d %d %d %s\n"
256 (max (1+ (- (cdr active) (car active))) 0)
257 (car active) (cdr active) group)))))))
259 (deffoo nnml-request-scan (&optional group server)
260 (setq nnml-article-file-alist nil)
261 (nnml-possibly-change-directory group server)
262 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
264 (deffoo nnml-close-group (group &optional server)
265 (setq nnml-article-file-alist nil)
268 (deffoo nnml-request-create-group (group &optional server args)
269 (nnml-possibly-change-directory nil server)
270 (nnmail-activate 'nnml)
272 ((assoc group nnml-group-alist)
274 ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
275 (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
276 (nnheader-report 'nnml "%s is a file"
277 (nnmail-group-pathname group nnml-directory)))
280 (push (list group (setq active (cons 1 0)))
282 (nnml-possibly-create-directory group)
283 (nnml-possibly-change-directory group server)
284 (let ((articles (nnheader-directory-articles nnml-current-directory)))
286 (setcar active (apply 'min articles))
287 (setcdr active (apply 'max articles))))
288 (nnmail-save-active nnml-group-alist nnml-active-file)
291 (deffoo nnml-request-list (&optional server)
293 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
294 (file-name-coding-system nnmail-pathname-coding-system))
295 (nnmail-find-file nnml-active-file))
296 (setq nnml-group-alist (nnmail-get-active))
299 (deffoo nnml-request-newgroups (date &optional server)
300 (nnml-request-list server))
302 (deffoo nnml-request-list-newsgroups (&optional server)
304 (nnmail-find-file nnml-newsgroups-file)))
306 (deffoo nnml-request-expire-articles (articles group &optional server force)
307 (nnml-possibly-change-directory group server)
308 (let ((active-articles
309 (nnheader-directory-articles nnml-current-directory))
311 article rest mod-time number)
312 (nnmail-activate 'nnml)
314 (setq active-articles (sort active-articles '<))
315 ;; Articles not listed in active-articles are already gone,
316 ;; so don't try to expire them.
317 (setq articles (gnus-sorted-intersection articles active-articles))
319 (while (and articles is-old)
320 (when (setq article (nnml-article-to-file (setq number (pop articles))))
321 (when (setq mod-time (nth 5 (file-attributes article)))
322 (if (and (nnml-deletable-article-p group number)
324 (nnmail-expired-article-p group mod-time force
325 nnml-inhibit-expiry)))
327 ;; Allow a special target group.
328 (unless (eq nnmail-expiry-target 'delete)
330 (nnml-request-article number group server
332 (let ((nnml-current-directory nil))
333 (nnmail-expiry-target-group
334 nnmail-expiry-target group))))
335 (nnheader-message 5 "Deleting article %s in %s"
338 (funcall nnmail-delete-file-function article)
341 (setq active-articles (delq number active-articles))
342 (nnml-nov-delete-article group number))
343 (push number rest)))))
344 (let ((active (nth 1 (assoc group nnml-group-alist))))
346 (setcar active (or (and active-articles
347 (apply 'min active-articles))
349 (nnmail-save-active nnml-group-alist nnml-active-file))
351 (nconc rest articles)))
353 (deffoo nnml-request-move-article
354 (article group server accept-form &optional last)
355 (let ((buf (get-buffer-create " *nnml move*"))
357 (nnml-possibly-change-directory group server)
358 (nnml-update-file-alist)
360 (nnml-deletable-article-p group article)
361 (nnml-request-article article group server)
362 (let (nnml-current-directory
364 nnml-article-file-alist)
367 (insert-buffer-substring nntp-server-buffer)
368 (setq result (eval accept-form))
369 (kill-buffer (current-buffer))
372 (nnml-possibly-change-directory group server)
374 (funcall nnmail-delete-file-function
375 (nnml-article-to-file article))
377 (nnml-nov-delete-article group article)
380 (nnmail-save-active nnml-group-alist nnml-active-file))))
383 (deffoo nnml-request-accept-article (group &optional server last)
384 (nnml-possibly-change-directory group server)
385 (nnmail-check-syntax)
387 (when nnmail-cache-accepted-message-ids
388 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
391 (nnmail-activate 'nnml)
392 (setq result (car (nnml-save-mail
393 (list (cons group (nnml-active-number group))))))
395 (nnmail-save-active nnml-group-alist nnml-active-file)
396 (and last (nnml-save-nov))))
398 (nnmail-activate 'nnml)
399 (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
400 (yes-or-no-p "Moved to `junk' group; delete article? "))
402 (setq result (car (nnml-save-mail result))))
404 (nnmail-save-active nnml-group-alist nnml-active-file)
405 (when nnmail-cache-accepted-message-ids
406 (nnmail-cache-close))
410 (deffoo nnml-request-post (&optional server)
411 (nnmail-do-request-post 'nnml-request-accept-article server))
413 (deffoo nnml-request-replace-article (article group buffer)
414 (nnml-possibly-change-directory group)
417 (nnml-possibly-create-directory group)
418 (let ((chars (nnmail-insert-lines))
419 (art (concat (int-to-string article) "\t"))
423 (point-min) (point-max)
424 (or (nnml-article-to-file article)
425 (expand-file-name (int-to-string article)
426 nnml-current-directory))
427 nil (if (nnheader-be-verbose 5) nil 'nomesg))
429 (setq headers (nnml-parse-head chars article))
430 ;; Replace the NOV line in the NOV file.
432 (set-buffer (nnml-open-nov group))
433 (goto-char (point-min))
434 (if (or (looking-at art)
435 (search-forward (concat "\n" art) nil t))
436 ;; Delete the old NOV line.
437 (delete-region (progn (beginning-of-line) (point))
438 (progn (forward-line 1) (point)))
439 ;; The line isn't here, so we have to find out where
440 ;; we should insert it. (This situation should never
441 ;; occur, but one likes to make sure...)
442 (while (and (looking-at "[0-9]+\t")
445 (match-beginning 0) (match-end 0)))
447 (zerop (forward-line 1)))))
449 (nnheader-insert-nov headers)
453 (deffoo nnml-request-delete-group (group &optional force server)
454 (nnml-possibly-change-directory group server)
456 ;; Delete all articles in GROUP.
459 nnml-current-directory t
460 (concat nnheader-numerical-short-files
461 "\\|" (regexp-quote nnml-nov-file-name) "$"
462 "\\|" (regexp-quote nnml-marks-file-name) "$")))
465 (setq article (pop articles))
466 (when (file-writable-p article)
467 (nnheader-message 5 "Deleting article %s in %s..." article group)
468 (funcall nnmail-delete-file-function article))))
469 ;; Try to delete the directory itself.
470 (ignore-errors (delete-directory nnml-current-directory)))
471 ;; Remove the group from all structures.
472 (setq nnml-group-alist
473 (delq (assoc group nnml-group-alist) nnml-group-alist)
474 nnml-current-group nil
475 nnml-current-directory nil)
476 ;; Save the active file.
477 (nnmail-save-active nnml-group-alist nnml-active-file)
480 (deffoo nnml-request-rename-group (group new-name &optional server)
481 (nnml-possibly-change-directory group server)
482 (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
483 (old-dir (nnmail-group-pathname group nnml-directory)))
485 (make-directory new-dir t)
487 ;; We move the articles file by file instead of renaming
488 ;; the directory -- there may be subgroups in this group.
489 ;; One might be more clever, I guess.
490 (let ((files (nnheader-article-to-file-alist old-dir)))
493 (concat old-dir (cdar files))
494 (concat new-dir (cdar files)))
496 ;; Move .overview file.
497 (let ((overview (concat old-dir nnml-nov-file-name)))
498 (when (file-exists-p overview)
499 (rename-file overview (concat new-dir nnml-nov-file-name))))
501 (let ((marks (concat old-dir nnml-marks-file-name)))
502 (when (file-exists-p marks)
503 (rename-file marks (concat new-dir nnml-marks-file-name))))
504 (when (<= (length (directory-files old-dir)) 2)
505 (ignore-errors (delete-directory old-dir)))
506 ;; That went ok, so we change the internal structures.
507 (let ((entry (assoc group nnml-group-alist)))
509 (setcar entry new-name))
510 (setq nnml-current-directory nil
511 nnml-current-group nil)
512 ;; Save the new group alist.
513 (nnmail-save-active nnml-group-alist nnml-active-file)
516 (deffoo nnml-set-status (article name value &optional group server)
517 (nnml-possibly-change-directory group server)
518 (let ((file (nnml-article-to-file article)))
520 ((not (file-exists-p file))
521 (nnheader-report 'nnml "File %s does not exist" file))
524 (nnheader-insert-file-contents file)
525 (nnmail-replace-status name value))
529 ;;; Internal functions.
531 (defun nnml-article-to-file (article)
532 (nnml-update-file-alist)
534 (if (setq file (cdr (assq article nnml-article-file-alist)))
535 (expand-file-name file nnml-current-directory)
536 (if nnml-check-directory-twice
537 ;; Just to make sure nothing went wrong when reading over NFS --
540 (setq file (expand-file-name (number-to-string article)
541 nnml-current-directory)))
542 (nnml-update-file-alist t)
545 (defun nnml-deletable-article-p (group article)
546 "Say whether ARTICLE in GROUP can be deleted."
548 (when (setq path (nnml-article-to-file article))
549 (when (file-writable-p path)
550 (or (not nnmail-keep-last-article)
551 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
554 ;; Find an article number in the current group given the Message-ID.
555 (defun nnml-find-group-number (id)
557 (set-buffer (get-buffer-create " *nnml id*"))
558 (let ((alist nnml-group-alist)
560 ;; We want to look through all .overview files, but we want to
561 ;; start with the one in the current directory. It seems most
562 ;; likely that the article we are looking for is in that group.
563 (if (setq number (nnml-find-id nnml-current-group id))
564 (cons nnml-current-group number)
565 ;; It wasn't there, so we look through the other groups as well.
566 (while (and (not number)
568 (or (string= (caar alist) nnml-current-group)
569 (setq number (nnml-find-id (caar alist) id)))
571 (setq alist (cdr alist))))
573 (cons (caar alist) number))))))
575 (defun nnml-find-id (group id)
577 (let ((nov (expand-file-name nnml-nov-file-name
578 (nnmail-group-pathname group nnml-directory)))
580 (when (file-exists-p nov)
581 (nnheader-insert-file-contents nov)
582 (while (and (not found)
583 (search-forward id nil t)) ; We find the ID.
584 ;; And the id is in the fourth field.
585 (if (not (and (search-backward "\t" nil t 4)
586 (not (search-backward"\t" (gnus-point-at-bol) t))))
590 ;; We return the article number.
592 (ignore-errors (read (current-buffer))))))
595 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
596 (if (or gnus-nov-is-evil nnml-nov-is-evil)
598 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
599 (when (file-exists-p nov)
601 (set-buffer nntp-server-buffer)
603 (nnheader-insert-file-contents nov)
605 (not (numberp fetch-old)))
606 t ; Don't remove anything.
607 (nnheader-nov-delete-outside-range
608 (if fetch-old (max 1 (- (car articles) fetch-old))
610 (car (last articles)))
613 (defun nnml-possibly-change-directory (group &optional server)
615 (not (nnml-server-opened server)))
616 (nnml-open-server server))
619 (let ((pathname (nnmail-group-pathname group nnml-directory))
620 (file-name-coding-system nnmail-pathname-coding-system))
621 (when (not (equal pathname nnml-current-directory))
622 (setq nnml-current-directory pathname
623 nnml-current-group group
624 nnml-article-file-alist nil))
625 (file-exists-p nnml-current-directory))))
627 (defun nnml-possibly-create-directory (group)
628 (let ((dir (nnmail-group-pathname group nnml-directory)))
629 (unless (file-exists-p dir)
630 (make-directory (directory-file-name dir) t)
631 (nnheader-message 5 "Creating mail directory %s" dir))))
633 (defun nnml-save-mail (group-art)
634 "Called narrowed to an article."
636 (setq chars (nnmail-insert-lines))
637 (nnmail-insert-xref group-art)
638 (run-hooks 'nnmail-prepare-save-mail-hook)
639 (run-hooks 'nnml-prepare-save-mail-hook)
640 (goto-char (point-min))
641 (while (looking-at "From ")
642 (replace-match "X-From-Line: ")
644 ;; We save the article in all the groups it belongs in.
648 (nnml-possibly-create-directory (caar ga))
649 (let ((file (concat (nnmail-group-pathname
650 (caar ga) nnml-directory)
651 (int-to-string (cdar ga)))))
653 ;; It was already saved, so we just make a hard link.
654 (funcall nnmail-crosspost-link-function first file t)
656 (nnmail-write-region (point-min) (point-max) file nil
657 (if (nnheader-be-verbose 5) nil 'nomesg))
660 ;; Generate a nov line for this article. We generate the nov
661 ;; line after saving, because nov generation destroys the
663 (setq headers (nnml-parse-head chars))
664 ;; Output the nov line to all nov databases that should have it.
665 (let ((ga group-art))
667 (nnml-add-nov (caar ga) (cdar ga) headers)
671 (defun nnml-active-number (group)
672 "Compute the next article number in GROUP."
673 (let ((active (cadr (assoc group nnml-group-alist))))
674 ;; The group wasn't known to nnml, so we just create an active
677 ;; Perhaps the active file was corrupt? See whether
678 ;; there are any articles in this group.
679 (nnml-possibly-create-directory group)
680 (nnml-possibly-change-directory group)
681 (unless nnml-article-file-alist
682 (setq nnml-article-file-alist
684 (nnheader-article-to-file-alist nnml-current-directory)
685 'car-less-than-car)))
687 (if nnml-article-file-alist
688 (cons (caar nnml-article-file-alist)
689 (caar (last nnml-article-file-alist)))
691 (push (list group active) nnml-group-alist))
692 (setcdr active (1+ (cdr active)))
693 (while (file-exists-p
694 (expand-file-name (int-to-string (cdr active))
695 (nnmail-group-pathname group nnml-directory)))
696 (setcdr active (1+ (cdr active))))
699 (defun nnml-add-nov (group article headers)
700 "Add a nov line for the GROUP base."
702 (set-buffer (nnml-open-nov group))
703 (goto-char (point-max))
704 (mail-header-set-number headers article)
705 (nnheader-insert-nov headers)))
707 (defsubst nnml-header-value ()
708 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
710 (defun nnml-parse-head (chars &optional number)
711 "Parse the head of the current buffer."
714 (unless (zerop (buffer-size))
716 (goto-char (point-min))
717 (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
718 ;; Fold continuation lines.
719 (goto-char (point-min))
720 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
721 (replace-match " " t t))
722 ;; Remove any tabs; they are too confusing.
723 (subst-char-in-region (point-min) (point-max) ?\t ? )
724 (let ((headers (nnheader-parse-head t)))
725 (mail-header-set-chars headers chars)
726 (mail-header-set-number headers number)
729 (defun nnml-open-nov (group)
730 (or (cdr (assoc group nnml-nov-buffer-alist))
731 (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
734 (set (make-local-variable 'nnml-nov-buffer-file-name)
737 (nnmail-group-pathname group nnml-directory)))
739 (when (file-exists-p nnml-nov-buffer-file-name)
740 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
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 1 (point-max) nnml-nov-buffer-file-name
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 (search-forward "\n\n" nil t)
839 (setq chars (- (point-max) (point)))
840 (max 1 (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 1 (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 (or (not nnml-article-file-alist)
874 (setq nnml-article-file-alist
875 (nnheader-article-to-file-alist nnml-current-directory))))
877 (deffoo nnml-request-set-mark (group actions &optional server)
878 (nnml-possibly-change-directory group server)
879 (unless nnml-marks-is-evil
880 (nnml-open-marks group server)
881 (dolist (action actions)
882 (let ((range (nth 0 action))
883 (what (nth 1 action))
884 (marks (nth 2 action)))
885 (assert (or (eq what 'add) (eq what 'del)) t
886 "Unknown request-set-mark action: %s" what)
888 (setq nnml-marks (gnus-update-alist-soft
890 (funcall (if (eq what 'add) 'gnus-range-add
891 'gnus-remove-from-range)
892 (cdr (assoc mark nnml-marks)) range)
894 (nnml-save-marks group server))
897 (deffoo nnml-request-update-info (group info &optional server)
898 (nnml-possibly-change-directory group server)
899 (unless nnml-marks-is-evil
900 (nnml-open-marks group server)
901 ;; Update info using `nnml-marks'.
902 (mapcar (lambda (pred)
905 (gnus-update-alist-soft
907 (cdr (assq (cdr pred) nnml-marks))
908 (gnus-info-marks info))
910 gnus-article-mark-lists)
911 (let ((seen (cdr (assq 'read nnml-marks))))
912 (gnus-info-set-read info
913 (if (and (integerp (car seen))
915 (list (cons (car seen) (car seen)))
919 (defun nnml-save-marks (group server)
920 (let ((file-name-coding-system nnmail-pathname-coding-system)
921 (file (expand-file-name nnml-marks-file-name
922 (nnmail-group-pathname group nnml-directory))))
925 (nnml-possibly-create-directory group)
928 (princ nnml-marks (current-buffer))
930 (error (or (gnus-yes-or-no-p
931 (format "Could not write to %s (%s). Continue? " file err))
932 (error "Cannot write to %s (%s)" err))))))
934 (defun nnml-open-marks (group server)
935 (let ((file (expand-file-name
937 (nnmail-group-pathname group nnml-directory))))
938 (if (file-exists-p file)
939 (setq nnml-marks (condition-case err
941 (nnheader-insert-file-contents file)
942 (read (current-buffer)))
943 (error (or (gnus-yes-or-no-p
944 (format "Error reading nnml marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
945 (error "Cannot read nnml marks file %s (%s)" file err)))))
946 ;; User didn't have a .marks file. Probably first time
947 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
948 (let ((info (gnus-get-info
949 (gnus-group-prefixed-name
951 (gnus-server-to-method (format "nnml:%s" server))))))
952 (nnheader-message 6 "Bootstrapping nnml marks...")
953 (setq nnml-marks (gnus-info-marks info))
954 (push (cons 'read (gnus-info-read info)) nnml-marks)
955 (nnml-save-marks group server)))))
959 ;;; nnml.el ends here