1 ;;; nnml.el --- mail spool access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
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
38 (eval-when-compile (require 'cl))
42 (defvoo nnml-directory message-directory
43 "Spool directory for the nnml mail backend.
45 This variable is a virtual server slot. See the Gnus manual for details.")
47 (defvoo nnml-active-file
48 (expand-file-name "active" nnml-directory)
51 This variable is a virtual server slot. See the Gnus manual for details.")
53 (defvoo nnml-newsgroups-file
54 (expand-file-name "newsgroups" nnml-directory)
55 "Mail newsgroups description file.
57 This variable is a virtual server slot. See the Gnus manual for details.")
59 (defvoo nnml-get-new-mail t
60 "If non-nil, nnml will check the incoming mail file and split the mail.
62 This variable is a virtual server slot. See the Gnus manual for details.")
64 (defvoo nnml-nov-is-evil nil
65 "If non-nil, Gnus will never generate and use nov databases for mail spools.
66 Using nov databases will speed up header fetching considerably.
67 This variable shouldn't be flipped much. If you have, for some reason,
68 set this to t, and want to set it to nil again, you should always run
69 the `nnml-generate-nov-databases' command. The function will go
70 through all nnml directories and generate nov databases for them
71 all. This may very well take some time.
73 This variable is a virtual server slot. See the Gnus manual for details.")
75 (defvoo nnml-marks-is-evil nil
76 "If non-nil, Gnus will never generate and use marks file for mail spools.
77 Using marks files makes it possible to backup and restore mail groups
78 separately from `.newsrc.eld'. If you have, for some reason, set this
79 to t, and want to set it to nil again, you should always remove the
80 corresponding marks file (usually named `.marks' in the nnml group
81 directory, but see `nnml-marks-file-name') for the group. Then the
82 marks file will be regenerated properly by Gnus.
84 This variable is a virtual server slot. See the Gnus manual for details.")
86 (defvoo nnml-prepare-save-mail-hook nil
87 "Hook run narrowed to an article before saving.
89 This variable is a virtual server slot. See the Gnus manual for details.")
91 (defvoo nnml-inhibit-expiry nil
92 "If non-nil, inhibit expiry.
94 This variable is a virtual server slot. See the Gnus manual for details.")
99 (defconst nnml-version "nnml 1.0"
102 (defvoo nnml-nov-file-name ".overview")
103 (defvoo nnml-marks-file-name ".marks")
105 (defvoo nnml-current-directory nil)
106 (defvoo nnml-current-group nil)
107 (defvoo nnml-status-string "")
108 (defvoo nnml-nov-buffer-alist nil)
109 (defvoo nnml-group-alist nil)
110 (defvoo nnml-active-timestamp nil)
111 (defvoo nnml-article-file-alist nil)
113 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
115 (defvar nnml-nov-buffer-file-name nil)
116 (defvar nnml-check-directory-twice t
117 "If t, to make sure nothing went wrong when reading over NFS --
120 (defvoo nnml-file-coding-system nnmail-file-coding-system)
122 (defvoo nnml-marks nil)
126 ;;; Interface functions.
128 (nnoo-define-basics nnml)
130 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
131 (when (nnml-possibly-change-directory group server)
133 (set-buffer nntp-server-buffer)
136 (number (length sequence))
138 (file-name-coding-system nnmail-pathname-coding-system)
140 (nnml-check-directory-twice
141 (and nnml-check-directory-twice
142 ;; To speed up, disable it in some case.
143 (or (not (numberp nnmail-large-newsgroup))
144 (<= number nnmail-large-newsgroup)))))
145 (if (stringp (car sequence))
147 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
150 (setq article (car sequence))
151 (setq file (nnml-article-to-file article))
154 (not (file-directory-p file)))
155 (insert (format "221 %d Article retrieved.\n" article))
157 (nnheader-insert-head file)
159 (if (search-forward "\n\n" nil t)
161 (goto-char (point-max))
164 (delete-region (point) (point-max)))
165 (setq sequence (cdr sequence))
166 (setq count (1+ count))
167 (and (numberp nnmail-large-newsgroup)
168 (> number nnmail-large-newsgroup)
170 (nnheader-message 6 "nnml: Receiving headers... %d%%"
171 (/ (* count 100) number))))
173 (and (numberp nnmail-large-newsgroup)
174 (> number nnmail-large-newsgroup)
175 (nnheader-message 6 "nnml: Receiving headers...done"))
177 (nnheader-fold-continuation-lines)
180 (deffoo nnml-open-server (server &optional defs)
181 (nnoo-change-server 'nnml server defs)
182 (when (not (file-exists-p nnml-directory))
183 (ignore-errors (make-directory nnml-directory t)))
185 ((not (file-exists-p nnml-directory))
187 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
188 ((not (file-directory-p (file-truename nnml-directory)))
190 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
192 (nnheader-report 'nnml "Opened server %s using directory %s"
193 server nnml-directory)
196 (deffoo nnml-request-regenerate (server)
197 (nnml-possibly-change-directory nil server)
198 (nnml-generate-nov-databases server)
201 (deffoo nnml-request-article (id &optional group server buffer)
202 (nnml-possibly-change-directory group server)
203 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
204 (file-name-coding-system nnmail-pathname-coding-system)
205 path gpath group-num)
207 (when (and (setq group-num (nnml-find-group-number id))
209 (assq (cdr group-num)
210 (nnheader-article-to-file-alist
212 (nnmail-group-pathname
215 (setq path (concat gpath (int-to-string (cdr group-num)))))
216 (setq path (nnml-article-to-file id)))
219 (nnheader-report 'nnml "No such article: %s" id))
220 ((not (file-exists-p path))
221 (nnheader-report 'nnml "No such file: %s" path))
222 ((file-directory-p path)
223 (nnheader-report 'nnml "File is a directory: %s" path))
224 ((not (save-excursion (let ((nnmail-file-coding-system
225 nnml-file-coding-system))
226 (nnmail-find-file path))))
227 (nnheader-report 'nnml "Couldn't read file: %s" path))
229 (nnheader-report 'nnml "Article %s retrieved" id)
230 ;; We return the article number.
231 (cons (if group-num (car group-num) group)
232 (string-to-int (file-name-nondirectory path)))))))
234 (deffoo nnml-request-group (group &optional server dont-check)
235 (let ((file-name-coding-system nnmail-pathname-coding-system))
237 ((not (nnml-possibly-change-directory group server))
238 (nnheader-report 'nnml "Invalid group (no such directory)"))
239 ((not (file-exists-p nnml-current-directory))
240 (nnheader-report 'nnml "Directory %s does not exist"
241 nnml-current-directory))
242 ((not (file-directory-p nnml-current-directory))
243 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
245 (nnheader-report 'nnml "Group %s selected" group)
248 (nnheader-re-read-dir nnml-current-directory)
249 (nnmail-activate 'nnml)
250 (let ((active (nth 1 (assoc group nnml-group-alist))))
252 (nnheader-report 'nnml "No such group: %s" group)
253 (nnheader-report 'nnml "Selected group %s" group)
254 (nnheader-insert "211 %d %d %d %s\n"
255 (max (1+ (- (cdr active) (car active))) 0)
256 (car active) (cdr active) group)))))))
258 (deffoo nnml-request-scan (&optional group server)
259 (setq nnml-article-file-alist nil)
260 (nnml-possibly-change-directory group server)
261 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
263 (deffoo nnml-close-group (group &optional server)
264 (setq nnml-article-file-alist nil)
267 (deffoo nnml-request-create-group (group &optional server args)
268 (nnml-possibly-change-directory nil server)
269 (nnmail-activate 'nnml)
271 ((assoc group nnml-group-alist)
273 ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
274 (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
275 (nnheader-report 'nnml "%s is a file"
276 (nnmail-group-pathname group nnml-directory)))
279 (push (list group (setq active (cons 1 0)))
281 (nnml-possibly-create-directory group)
282 (nnml-possibly-change-directory group server)
283 (let ((articles (nnheader-directory-articles nnml-current-directory)))
285 (setcar active (apply 'min articles))
286 (setcdr active (apply 'max articles))))
287 (nnmail-save-active nnml-group-alist nnml-active-file)
290 (deffoo nnml-request-list (&optional server)
292 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
293 (file-name-coding-system nnmail-pathname-coding-system))
294 (nnmail-find-file nnml-active-file))
295 (setq nnml-group-alist (nnmail-get-active))
298 (deffoo nnml-request-newgroups (date &optional server)
299 (nnml-request-list server))
301 (deffoo nnml-request-list-newsgroups (&optional server)
303 (nnmail-find-file nnml-newsgroups-file)))
305 (deffoo nnml-request-expire-articles (articles group &optional server force)
306 (nnml-possibly-change-directory group server)
307 (let ((active-articles
308 (nnheader-directory-articles nnml-current-directory))
310 article rest mod-time number)
311 (nnmail-activate 'nnml)
313 (setq active-articles (sort active-articles '<))
314 ;; Articles not listed in active-articles are already gone,
315 ;; so don't try to expire them.
316 (setq articles (gnus-sorted-intersection articles active-articles))
318 (while (and articles is-old)
319 (when (setq article (nnml-article-to-file (setq number (pop articles))))
320 (when (setq mod-time (nth 5 (file-attributes article)))
321 (if (and (nnml-deletable-article-p group number)
323 (nnmail-expired-article-p group mod-time force
324 nnml-inhibit-expiry)))
326 ;; Allow a special target group.
327 (unless (eq nnmail-expiry-target 'delete)
329 (nnml-request-article number group server
331 (let ((nnml-current-directory nil))
332 (nnmail-expiry-target-group
333 nnmail-expiry-target group))))
334 (nnheader-message 5 "Deleting article %s in %s"
337 (funcall nnmail-delete-file-function article)
340 (setq active-articles (delq number active-articles))
341 (nnml-nov-delete-article group number))
342 (push number rest)))))
343 (let ((active (nth 1 (assoc group nnml-group-alist))))
345 (setcar active (or (and active-articles
346 (apply 'min active-articles))
348 (nnmail-save-active nnml-group-alist nnml-active-file))
350 (nconc rest articles)))
352 (deffoo nnml-request-move-article
353 (article group server accept-form &optional last)
354 (let ((buf (get-buffer-create " *nnml move*"))
356 (nnml-possibly-change-directory group server)
357 (nnml-update-file-alist)
359 (nnml-deletable-article-p group article)
360 (nnml-request-article article group server)
361 (let (nnml-current-directory
363 nnml-article-file-alist)
366 (insert-buffer-substring nntp-server-buffer)
367 (setq result (eval accept-form))
368 (kill-buffer (current-buffer))
371 (nnml-possibly-change-directory group server)
373 (funcall nnmail-delete-file-function
374 (nnml-article-to-file article))
376 (nnml-nov-delete-article group article)
379 (nnmail-save-active nnml-group-alist nnml-active-file))))
382 (deffoo nnml-request-accept-article (group &optional server last)
383 (nnml-possibly-change-directory group server)
384 (nnmail-check-syntax)
386 (when nnmail-cache-accepted-message-ids
387 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
390 (nnmail-activate 'nnml)
391 (setq result (car (nnml-save-mail
392 (list (cons group (nnml-active-number group))))))
394 (nnmail-save-active nnml-group-alist nnml-active-file)
395 (and last (nnml-save-nov))))
397 (nnmail-activate 'nnml)
398 (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
399 (yes-or-no-p "Moved to `junk' group; delete article? "))
401 (setq result (car (nnml-save-mail result))))
403 (nnmail-save-active nnml-group-alist nnml-active-file)
404 (when nnmail-cache-accepted-message-ids
405 (nnmail-cache-close))
409 (deffoo nnml-request-post (&optional server)
410 (nnmail-do-request-post 'nnml-request-accept-article server))
412 (deffoo nnml-request-replace-article (article group buffer)
413 (nnml-possibly-change-directory group)
416 (nnml-possibly-create-directory group)
417 (let ((chars (nnmail-insert-lines))
418 (art (concat (int-to-string article) "\t"))
422 (point-min) (point-max)
423 (or (nnml-article-to-file article)
424 (expand-file-name (int-to-string article)
425 nnml-current-directory))
426 nil (if (nnheader-be-verbose 5) nil 'nomesg))
428 (setq headers (nnml-parse-head chars article))
429 ;; Replace the NOV line in the NOV file.
431 (set-buffer (nnml-open-nov group))
432 (goto-char (point-min))
433 (if (or (looking-at art)
434 (search-forward (concat "\n" art) nil t))
435 ;; Delete the old NOV line.
436 (delete-region (progn (beginning-of-line) (point))
437 (progn (forward-line 1) (point)))
438 ;; The line isn't here, so we have to find out where
439 ;; we should insert it. (This situation should never
440 ;; occur, but one likes to make sure...)
441 (while (and (looking-at "[0-9]+\t")
444 (match-beginning 0) (match-end 0)))
446 (zerop (forward-line 1)))))
448 (nnheader-insert-nov headers)
452 (deffoo nnml-request-delete-group (group &optional force server)
453 (nnml-possibly-change-directory group server)
455 ;; Delete all articles in GROUP.
458 nnml-current-directory t
459 (concat nnheader-numerical-short-files
460 "\\|" (regexp-quote nnml-nov-file-name) "$"
461 "\\|" (regexp-quote nnml-marks-file-name) "$")))
464 (setq article (pop articles))
465 (when (file-writable-p article)
466 (nnheader-message 5 "Deleting article %s in %s..." article group)
467 (funcall nnmail-delete-file-function article))))
468 ;; Try to delete the directory itself.
469 (ignore-errors (delete-directory nnml-current-directory)))
470 ;; Remove the group from all structures.
471 (setq nnml-group-alist
472 (delq (assoc group nnml-group-alist) nnml-group-alist)
473 nnml-current-group nil
474 nnml-current-directory nil)
475 ;; Save the active file.
476 (nnmail-save-active nnml-group-alist nnml-active-file)
479 (deffoo nnml-request-rename-group (group new-name &optional server)
480 (nnml-possibly-change-directory group server)
481 (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
482 (old-dir (nnmail-group-pathname group nnml-directory)))
484 (make-directory new-dir t)
486 ;; We move the articles file by file instead of renaming
487 ;; the directory -- there may be subgroups in this group.
488 ;; One might be more clever, I guess.
489 (let ((files (nnheader-article-to-file-alist old-dir)))
492 (concat old-dir (cdar files))
493 (concat new-dir (cdar files)))
495 ;; Move .overview file.
496 (let ((overview (concat old-dir nnml-nov-file-name)))
497 (when (file-exists-p overview)
498 (rename-file overview (concat new-dir nnml-nov-file-name))))
500 (let ((marks (concat old-dir nnml-marks-file-name)))
501 (when (file-exists-p marks)
502 (rename-file marks (concat new-dir nnml-marks-file-name))))
503 (when (<= (length (directory-files old-dir)) 2)
504 (ignore-errors (delete-directory old-dir)))
505 ;; That went ok, so we change the internal structures.
506 (let ((entry (assoc group nnml-group-alist)))
508 (setcar entry new-name))
509 (setq nnml-current-directory nil
510 nnml-current-group nil)
511 ;; Save the new group alist.
512 (nnmail-save-active nnml-group-alist nnml-active-file)
515 (deffoo nnml-set-status (article name value &optional group server)
516 (nnml-possibly-change-directory group server)
517 (let ((file (nnml-article-to-file article)))
519 ((not (file-exists-p file))
520 (nnheader-report 'nnml "File %s does not exist" file))
523 (nnheader-insert-file-contents file)
524 (nnmail-replace-status name value))
528 ;;; Internal functions.
530 (defun nnml-article-to-file (article)
531 (nnml-update-file-alist)
533 (if (setq file (cdr (assq article nnml-article-file-alist)))
534 (expand-file-name file nnml-current-directory)
535 (if nnml-check-directory-twice
536 ;; Just to make sure nothing went wrong when reading over NFS --
539 (setq file (expand-file-name (number-to-string article)
540 nnml-current-directory)))
541 (nnml-update-file-alist t)
544 (defun nnml-deletable-article-p (group article)
545 "Say whether ARTICLE in GROUP can be deleted."
547 (when (setq path (nnml-article-to-file article))
548 (when (file-writable-p path)
549 (or (not nnmail-keep-last-article)
550 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
553 ;; Find an article number in the current group given the Message-ID.
554 (defun nnml-find-group-number (id)
556 (set-buffer (get-buffer-create " *nnml id*"))
557 (let ((alist nnml-group-alist)
559 ;; We want to look through all .overview files, but we want to
560 ;; start with the one in the current directory. It seems most
561 ;; likely that the article we are looking for is in that group.
562 (if (setq number (nnml-find-id nnml-current-group id))
563 (cons nnml-current-group number)
564 ;; It wasn't there, so we look through the other groups as well.
565 (while (and (not number)
567 (or (string= (caar alist) nnml-current-group)
568 (setq number (nnml-find-id (caar alist) id)))
570 (setq alist (cdr alist))))
572 (cons (caar alist) number))))))
574 (defun nnml-find-id (group id)
576 (let ((nov (expand-file-name nnml-nov-file-name
577 (nnmail-group-pathname group nnml-directory)))
579 (when (file-exists-p nov)
580 (nnheader-insert-file-contents nov)
581 (while (and (not found)
582 (search-forward id nil t)) ; We find the ID.
583 ;; And the id is in the fourth field.
584 (if (not (and (search-backward "\t" nil t 4)
585 (not (search-backward"\t" (gnus-point-at-bol) t))))
589 ;; We return the article number.
591 (ignore-errors (read (current-buffer))))))
594 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
595 (if (or gnus-nov-is-evil nnml-nov-is-evil)
597 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
598 (when (file-exists-p nov)
600 (set-buffer nntp-server-buffer)
602 (nnheader-insert-file-contents nov)
604 (not (numberp fetch-old)))
605 t ; Don't remove anything.
606 (nnheader-nov-delete-outside-range
607 (if fetch-old (max 1 (- (car articles) fetch-old))
609 (car (last articles)))
612 (defun nnml-possibly-change-directory (group &optional server)
614 (not (nnml-server-opened server)))
615 (nnml-open-server server))
618 (let ((pathname (nnmail-group-pathname group nnml-directory))
619 (file-name-coding-system nnmail-pathname-coding-system))
620 (when (not (equal pathname nnml-current-directory))
621 (setq nnml-current-directory pathname
622 nnml-current-group group
623 nnml-article-file-alist nil))
624 (file-exists-p nnml-current-directory))))
626 (defun nnml-possibly-create-directory (group)
627 (let ((dir (nnmail-group-pathname group nnml-directory)))
628 (unless (file-exists-p dir)
629 (make-directory (directory-file-name dir) t)
630 (nnheader-message 5 "Creating mail directory %s" dir))))
632 (defun nnml-save-mail (group-art)
633 "Called narrowed to an article."
635 (setq chars (nnmail-insert-lines))
636 (nnmail-insert-xref group-art)
637 (run-hooks 'nnmail-prepare-save-mail-hook)
638 (run-hooks 'nnml-prepare-save-mail-hook)
639 (goto-char (point-min))
640 (while (looking-at "From ")
641 (replace-match "X-From-Line: ")
643 ;; We save the article in all the groups it belongs in.
647 (nnml-possibly-create-directory (caar ga))
648 (let ((file (concat (nnmail-group-pathname
649 (caar ga) nnml-directory)
650 (int-to-string (cdar ga)))))
652 ;; It was already saved, so we just make a hard link.
653 (funcall nnmail-crosspost-link-function first file t)
655 (nnmail-write-region (point-min) (point-max) file nil
656 (if (nnheader-be-verbose 5) nil 'nomesg))
659 ;; Generate a nov line for this article. We generate the nov
660 ;; line after saving, because nov generation destroys the
662 (setq headers (nnml-parse-head chars))
663 ;; Output the nov line to all nov databases that should have it.
664 (let ((ga group-art))
666 (nnml-add-nov (caar ga) (cdar ga) headers)
670 (defun nnml-active-number (group)
671 "Compute the next article number in GROUP."
672 (let ((active (cadr (assoc group nnml-group-alist))))
673 ;; The group wasn't known to nnml, so we just create an active
676 ;; Perhaps the active file was corrupt? See whether
677 ;; there are any articles in this group.
678 (nnml-possibly-create-directory group)
679 (nnml-possibly-change-directory group)
680 (unless nnml-article-file-alist
681 (setq nnml-article-file-alist
683 (nnheader-article-to-file-alist nnml-current-directory)
684 'car-less-than-car)))
686 (if nnml-article-file-alist
687 (cons (caar nnml-article-file-alist)
688 (caar (last nnml-article-file-alist)))
690 (push (list group active) nnml-group-alist))
691 (setcdr active (1+ (cdr active)))
692 (while (file-exists-p
693 (expand-file-name (int-to-string (cdr active))
694 (nnmail-group-pathname group nnml-directory)))
695 (setcdr active (1+ (cdr active))))
698 (defun nnml-add-nov (group article headers)
699 "Add a nov line for the GROUP base."
701 (set-buffer (nnml-open-nov group))
702 (goto-char (point-max))
703 (mail-header-set-number headers article)
704 (nnheader-insert-nov headers)))
706 (defsubst nnml-header-value ()
707 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
709 (defun nnml-parse-head (chars &optional number)
710 "Parse the head of the current buffer."
713 (unless (zerop (buffer-size))
715 (goto-char (point-min))
716 (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
717 ;; Fold continuation lines.
718 (goto-char (point-min))
719 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
720 (replace-match " " t t))
721 ;; Remove any tabs; they are too confusing.
722 (subst-char-in-region (point-min) (point-max) ?\t ? )
723 (let ((headers (nnheader-parse-head t)))
724 (mail-header-set-chars headers chars)
725 (mail-header-set-number headers number)
728 (defun nnml-open-nov (group)
729 (or (cdr (assoc group nnml-nov-buffer-alist))
730 (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
733 (set (make-local-variable 'nnml-nov-buffer-file-name)
736 (nnmail-group-pathname group nnml-directory)))
738 (when (file-exists-p nnml-nov-buffer-file-name)
739 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
740 (push (cons group buffer) nnml-nov-buffer-alist)
743 (defun nnml-save-nov ()
745 (while nnml-nov-buffer-alist
746 (when (buffer-name (cdar nnml-nov-buffer-alist))
747 (set-buffer (cdar nnml-nov-buffer-alist))
748 (when (buffer-modified-p)
749 (nnmail-write-region 1 (point-max) nnml-nov-buffer-file-name
751 (set-buffer-modified-p nil)
752 (kill-buffer (current-buffer)))
753 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
756 (defun nnml-generate-nov-databases (&optional server)
757 "Generate NOV databases in all nnml directories."
758 (interactive (list (or (nnoo-current-server 'nnml) "")))
759 ;; Read the active file to make sure we don't re-use articles
760 ;; numbers in empty groups.
761 (nnmail-activate 'nnml)
762 (unless (nnml-server-opened server)
763 (nnml-open-server server))
764 (setq nnml-directory (expand-file-name nnml-directory))
765 ;; Recurse down the directories.
766 (nnml-generate-nov-databases-1 nnml-directory nil t)
767 ;; Save the active file.
768 (nnmail-save-active nnml-group-alist nnml-active-file))
770 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
771 "Regenerate the NOV database in DIR."
772 (interactive "DRegenerate NOV in: ")
773 (setq dir (file-name-as-directory dir))
774 ;; Only scan this sub-tree if we haven't been here yet.
775 (unless (member (file-truename dir) seen)
776 (push (file-truename dir) seen)
777 ;; We descend recursively
778 (let ((dirs (directory-files dir t nil t))
780 (while (setq dir (pop dirs))
781 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
782 (file-directory-p dir))
783 (nnml-generate-nov-databases-1 dir seen))))
784 ;; Do this directory.
785 (let ((files (sort (nnheader-article-to-file-alist dir)
786 'car-less-than-car)))
788 (let* ((group (nnheader-file-to-group
789 (directory-file-name dir) nnml-directory))
790 (info (cadr (assoc group nnml-group-alist))))
792 (setcar info (1+ (cdr info)))))
793 (funcall nnml-generate-active-function dir)
794 ;; Generate the nov file.
795 (nnml-generate-nov-file dir files)
797 (nnmail-save-active nnml-group-alist nnml-active-file))))))
799 (eval-when-compile (defvar files))
800 (defun nnml-generate-active-info (dir)
801 ;; Update the active info for this group.
802 (let* ((group (nnheader-file-to-group
803 (directory-file-name dir) nnml-directory))
804 (entry (assoc group nnml-group-alist))
805 (last (or (caadr entry) 0)))
806 (setq nnml-group-alist (delq entry nnml-group-alist))
808 (cons (or (caar files) (1+ last))
811 (while (cdr f) (setq f (cdr f)))
816 (defun nnml-generate-nov-file (dir files)
817 (let* ((dir (file-name-as-directory dir))
818 (nov (concat dir nnml-nov-file-name))
819 (nov-buffer (get-buffer-create " *nov*"))
822 ;; Init the nov buffer.
823 (set-buffer nov-buffer)
824 (buffer-disable-undo)
826 (set-buffer nntp-server-buffer)
827 ;; Delete the old NOV file.
828 (when (file-exists-p nov)
829 (funcall nnmail-delete-file-function nov))
831 (unless (file-directory-p (setq file (concat dir (cdar files))))
833 (nnheader-insert-file-contents file)
835 (goto-char (point-min))
837 (search-forward "\n\n" nil t)
838 (setq chars (- (point-max) (point)))
839 (max 1 (1- (point)))))
840 (unless (zerop (buffer-size))
841 (goto-char (point-min))
842 (setq headers (nnml-parse-head chars (caar files)))
844 (set-buffer nov-buffer)
845 (goto-char (point-max))
846 (nnheader-insert-nov headers)))
848 (setq files (cdr files)))
850 (set-buffer nov-buffer)
851 (nnmail-write-region 1 (point-max) nov nil 'nomesg)
852 (kill-buffer (current-buffer))))))
854 (defun nnml-nov-delete-article (group article)
856 (set-buffer (nnml-open-nov group))
857 (when (nnheader-find-nov-line article)
858 (delete-region (point) (progn (forward-line 1) (point)))
860 (let ((active (cadr (assoc group nnml-group-alist)))
864 (setf (car active) (1+ (cdr active)))
865 (when (and (setq num (ignore-errors (read (current-buffer))))
867 (setf (car active) num)))))))
870 (defun nnml-update-file-alist (&optional force)
871 (when (or (not nnml-article-file-alist)
873 (setq nnml-article-file-alist
874 (nnheader-article-to-file-alist nnml-current-directory))))
876 (deffoo nnml-request-set-mark (group actions &optional server)
877 (nnml-possibly-change-directory group server)
878 (unless nnml-marks-is-evil
879 (nnml-open-marks group server)
880 (dolist (action actions)
881 (let ((range (nth 0 action))
882 (what (nth 1 action))
883 (marks (nth 2 action)))
884 (assert (or (eq what 'add) (eq what 'del)) t
885 "Unknown request-set-mark action: %s" what)
887 (setq nnml-marks (gnus-update-alist-soft
889 (funcall (if (eq what 'add) 'gnus-range-add
890 'gnus-remove-from-range)
891 (cdr (assoc mark nnml-marks)) range)
893 (nnml-save-marks group server))
896 (deffoo nnml-request-update-info (group info &optional server)
897 (nnml-possibly-change-directory group server)
898 (unless nnml-marks-is-evil
899 (nnml-open-marks group server)
900 ;; Update info using `nnml-marks'.
901 (mapcar (lambda (pred)
904 (gnus-update-alist-soft
906 (cdr (assq (cdr pred) nnml-marks))
907 (gnus-info-marks info))
909 gnus-article-mark-lists)
910 (let ((seen (cdr (assq 'read nnml-marks))))
911 (gnus-info-set-read info
912 (if (and (integerp (car seen))
914 (list (cons (car seen) (car seen)))
918 (defun nnml-save-marks (group server)
919 (let ((file-name-coding-system nnmail-pathname-coding-system)
920 (file (expand-file-name nnml-marks-file-name
921 (nnmail-group-pathname group nnml-directory))))
924 (nnml-possibly-create-directory group)
927 (princ nnml-marks (current-buffer))
929 (error (or (gnus-yes-or-no-p
930 (format "Could not write to %s (%s). Continue? " file err))
931 (error "Cannot write to %s (%s)" err))))))
933 (defun nnml-open-marks (group server)
934 (let ((file (expand-file-name
936 (nnmail-group-pathname group nnml-directory))))
937 (if (file-exists-p file)
938 (setq nnml-marks (condition-case err
940 (nnheader-insert-file-contents file)
941 (read (current-buffer)))
942 (error (or (gnus-yes-or-no-p
943 (format "Error reading nnml marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
944 (error "Cannot read nnml marks file %s (%s)" file err)))))
945 ;; User didn't have a .marks file. Probably first time
946 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
947 (let ((info (gnus-get-info
948 (gnus-group-prefixed-name
950 (gnus-server-to-method (format "nnml:%s" server))))))
951 (nnheader-message 6 "Boostrapping nnml marks...")
952 (setq nnml-marks (gnus-info-marks info))
953 (push (cons 'read (gnus-info-read info)) nnml-marks)
954 (nnml-save-marks group server)))))
958 ;;; nnml.el ends here