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)
118 (defvoo nnml-file-coding-system nnmail-file-coding-system)
120 (defvoo nnml-marks nil)
124 ;;; Interface functions.
126 (nnoo-define-basics nnml)
128 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
129 (when (nnml-possibly-change-directory group server)
131 (set-buffer nntp-server-buffer)
134 (number (length sequence))
136 (file-name-coding-system nnmail-pathname-coding-system)
138 (if (stringp (car sequence))
140 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
143 (setq article (car sequence))
144 (setq file (nnml-article-to-file article))
147 (not (file-directory-p file)))
148 (insert (format "221 %d Article retrieved.\n" article))
150 (nnheader-insert-head file)
152 (if (search-forward "\n\n" nil t)
154 (goto-char (point-max))
157 (delete-region (point) (point-max)))
158 (setq sequence (cdr sequence))
159 (setq count (1+ count))
160 (and (numberp nnmail-large-newsgroup)
161 (> number nnmail-large-newsgroup)
163 (nnheader-message 6 "nnml: Receiving headers... %d%%"
164 (/ (* count 100) number))))
166 (and (numberp nnmail-large-newsgroup)
167 (> number nnmail-large-newsgroup)
168 (nnheader-message 6 "nnml: Receiving headers...done"))
170 (nnheader-fold-continuation-lines)
173 (deffoo nnml-open-server (server &optional defs)
174 (nnoo-change-server 'nnml server defs)
175 (when (not (file-exists-p nnml-directory))
176 (ignore-errors (make-directory nnml-directory t)))
178 ((not (file-exists-p nnml-directory))
180 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
181 ((not (file-directory-p (file-truename nnml-directory)))
183 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
185 (nnheader-report 'nnml "Opened server %s using directory %s"
186 server nnml-directory)
189 (deffoo nnml-request-regenerate (server)
190 (nnml-possibly-change-directory nil server)
191 (nnml-generate-nov-databases server)
194 (deffoo nnml-request-article (id &optional group server buffer)
195 (nnml-possibly-change-directory group server)
196 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
197 (file-name-coding-system nnmail-pathname-coding-system)
198 path gpath group-num)
200 (when (and (setq group-num (nnml-find-group-number id))
202 (assq (cdr group-num)
203 (nnml-article-to-file-alist
205 (nnmail-group-pathname
208 (setq path (concat gpath (int-to-string (cdr group-num)))))
209 (setq path (nnml-article-to-file id)))
212 (nnheader-report 'nnml "No such article: %s" id))
213 ((not (file-exists-p path))
214 (nnheader-report 'nnml "No such file: %s" path))
215 ((file-directory-p path)
216 (nnheader-report 'nnml "File is a directory: %s" path))
217 ((not (save-excursion (let ((nnmail-file-coding-system
218 nnml-file-coding-system))
219 (nnmail-find-file path))))
220 (nnheader-report 'nnml "Couldn't read file: %s" path))
222 (nnheader-report 'nnml "Article %s retrieved" id)
223 ;; We return the article number.
224 (cons (if group-num (car group-num) group)
225 (string-to-int (file-name-nondirectory path)))))))
227 (deffoo nnml-request-group (group &optional server dont-check)
228 (let ((file-name-coding-system nnmail-pathname-coding-system))
230 ((not (nnml-possibly-change-directory group server))
231 (nnheader-report 'nnml "Invalid group (no such directory)"))
232 ((not (file-exists-p nnml-current-directory))
233 (nnheader-report 'nnml "Directory %s does not exist"
234 nnml-current-directory))
235 ((not (file-directory-p nnml-current-directory))
236 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
238 (nnheader-report 'nnml "Group %s selected" group)
241 (nnheader-re-read-dir nnml-current-directory)
242 (nnmail-activate 'nnml)
243 (let ((active (nth 1 (assoc group nnml-group-alist))))
245 (nnheader-report 'nnml "No such group: %s" group)
246 (nnheader-report 'nnml "Selected group %s" group)
247 (nnheader-insert "211 %d %d %d %s\n"
248 (max (1+ (- (cdr active) (car active))) 0)
249 (car active) (cdr active) group)))))))
251 (deffoo nnml-request-scan (&optional group server)
252 (setq nnml-article-file-alist nil)
253 (nnml-possibly-change-directory group server)
254 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
256 (deffoo nnml-close-group (group &optional server)
257 (setq nnml-article-file-alist nil)
260 (deffoo nnml-request-create-group (group &optional server args)
261 (nnml-possibly-change-directory nil server)
262 (nnmail-activate 'nnml)
264 ((assoc group nnml-group-alist)
266 ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
267 (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
268 (nnheader-report 'nnml "%s is a file"
269 (nnmail-group-pathname group nnml-directory)))
272 (push (list group (setq active (cons 1 0)))
274 (nnml-possibly-create-directory group)
275 (nnml-possibly-change-directory group server)
276 (let ((articles (nnml-directory-articles nnml-current-directory)))
278 (setcar active (apply 'min articles))
279 (setcdr active (apply 'max articles))))
280 (nnmail-save-active nnml-group-alist nnml-active-file)
283 (deffoo nnml-request-list (&optional server)
285 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
286 (file-name-coding-system nnmail-pathname-coding-system))
287 (nnmail-find-file nnml-active-file))
288 (setq nnml-group-alist (nnmail-get-active))
291 (deffoo nnml-request-newgroups (date &optional server)
292 (nnml-request-list server))
294 (deffoo nnml-request-list-newsgroups (&optional server)
296 (nnmail-find-file nnml-newsgroups-file)))
298 (deffoo nnml-request-expire-articles (articles group &optional server force)
299 (nnml-possibly-change-directory group server)
300 (let ((active-articles
301 (nnml-directory-articles nnml-current-directory))
303 article rest mod-time number)
304 (nnmail-activate 'nnml)
306 (setq active-articles (sort active-articles '<))
307 ;; Articles not listed in active-articles are already gone,
308 ;; so don't try to expire them.
309 (setq articles (gnus-sorted-intersection articles active-articles))
311 (while (and articles is-old)
312 (if (and (setq article (nnml-article-to-file (setq number (pop articles))))
313 (setq mod-time (nth 5 (file-attributes article)))
314 (nnml-deletable-article-p group number)
315 (setq is-old (nnmail-expired-article-p group mod-time force
316 nnml-inhibit-expiry)))
318 ;; Allow a special target group.
319 (unless (eq nnmail-expiry-target 'delete)
321 (nnml-request-article number group server (current-buffer))
322 (let (nnml-current-directory
324 nnml-article-file-alist)
325 (nnmail-expiry-target-group nnmail-expiry-target group))))
326 (nnheader-message 5 "Deleting article %s in %s"
329 (funcall nnmail-delete-file-function article)
332 (setq active-articles (delq number active-articles))
333 (nnml-nov-delete-article group number))
335 (let ((active (nth 1 (assoc group nnml-group-alist))))
337 (setcar active (or (and active-articles
338 (apply 'min active-articles))
340 (nnmail-save-active nnml-group-alist nnml-active-file))
342 (nconc rest articles)))
344 (deffoo nnml-request-move-article
345 (article group server accept-form &optional last)
346 (let ((buf (get-buffer-create " *nnml move*"))
348 (nnml-possibly-change-directory group server)
349 (nnml-update-file-alist)
351 (nnml-deletable-article-p group article)
352 (nnml-request-article article group server)
353 (let (nnml-current-directory
355 nnml-article-file-alist)
358 (insert-buffer-substring nntp-server-buffer)
359 (setq result (eval accept-form))
360 (kill-buffer (current-buffer))
363 (nnml-possibly-change-directory group server)
365 (funcall nnmail-delete-file-function
366 (nnml-article-to-file article))
368 (nnml-nov-delete-article group article)
371 (nnmail-save-active nnml-group-alist nnml-active-file))))
374 (deffoo nnml-request-accept-article (group &optional server last)
375 (nnml-possibly-change-directory group server)
376 (nnmail-check-syntax)
378 (when nnmail-cache-accepted-message-ids
379 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
382 (nnmail-activate 'nnml)
383 (setq result (car (nnml-save-mail
384 (list (cons group (nnml-active-number group))))))
386 (nnmail-save-active nnml-group-alist nnml-active-file)
387 (and last (nnml-save-nov))))
389 (nnmail-activate 'nnml)
390 (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
391 (yes-or-no-p "Moved to `junk' group; delete article? "))
393 (setq result (car (nnml-save-mail result))))
395 (nnmail-save-active nnml-group-alist nnml-active-file)
396 (when nnmail-cache-accepted-message-ids
397 (nnmail-cache-close))
401 (deffoo nnml-request-post (&optional server)
402 (nnmail-do-request-post 'nnml-request-accept-article server))
404 (deffoo nnml-request-replace-article (article group buffer)
405 (nnml-possibly-change-directory group)
408 (nnml-possibly-create-directory group)
409 (let ((chars (nnmail-insert-lines))
410 (art (concat (int-to-string article) "\t"))
414 (point-min) (point-max)
415 (or (nnml-article-to-file article)
416 (expand-file-name (int-to-string article)
417 nnml-current-directory))
418 nil (if (nnheader-be-verbose 5) nil 'nomesg))
420 (setq headers (nnml-parse-head chars article))
421 ;; Replace the NOV line in the NOV file.
423 (set-buffer (nnml-open-nov group))
424 (goto-char (point-min))
425 (if (or (looking-at art)
426 (search-forward (concat "\n" art) nil t))
427 ;; Delete the old NOV line.
428 (delete-region (progn (beginning-of-line) (point))
429 (progn (forward-line 1) (point)))
430 ;; The line isn't here, so we have to find out where
431 ;; we should insert it. (This situation should never
432 ;; occur, but one likes to make sure...)
433 (while (and (looking-at "[0-9]+\t")
436 (match-beginning 0) (match-end 0)))
438 (zerop (forward-line 1)))))
440 (nnheader-insert-nov headers)
444 (deffoo nnml-request-delete-group (group &optional force server)
445 (nnml-possibly-change-directory group server)
447 ;; Delete all articles in GROUP.
450 nnml-current-directory t
451 (concat nnheader-numerical-short-files
452 "\\|" (regexp-quote nnml-nov-file-name) "$"
453 "\\|" (regexp-quote nnml-marks-file-name) "$")))
456 (setq article (pop articles))
457 (when (file-writable-p article)
458 (nnheader-message 5 "Deleting article %s in %s..." article group)
459 (funcall nnmail-delete-file-function article))))
460 ;; Try to delete the directory itself.
461 (ignore-errors (delete-directory nnml-current-directory)))
462 ;; Remove the group from all structures.
463 (setq nnml-group-alist
464 (delq (assoc group nnml-group-alist) nnml-group-alist)
465 nnml-current-group nil
466 nnml-current-directory nil)
467 ;; Save the active file.
468 (nnmail-save-active nnml-group-alist nnml-active-file)
471 (deffoo nnml-request-rename-group (group new-name &optional server)
472 (nnml-possibly-change-directory group server)
473 (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
474 (old-dir (nnmail-group-pathname group nnml-directory)))
476 (make-directory new-dir t)
478 ;; We move the articles file by file instead of renaming
479 ;; the directory -- there may be subgroups in this group.
480 ;; One might be more clever, I guess.
481 (let ((files (nnml-article-to-file-alist old-dir)))
484 (concat old-dir (cdar files))
485 (concat new-dir (cdar files)))
487 ;; Move .overview file.
488 (let ((overview (concat old-dir nnml-nov-file-name)))
489 (when (file-exists-p overview)
490 (rename-file overview (concat new-dir nnml-nov-file-name))))
492 (let ((marks (concat old-dir nnml-marks-file-name)))
493 (when (file-exists-p marks)
494 (rename-file marks (concat new-dir nnml-marks-file-name))))
495 (when (<= (length (directory-files old-dir)) 2)
496 (ignore-errors (delete-directory old-dir)))
497 ;; That went ok, so we change the internal structures.
498 (let ((entry (assoc group nnml-group-alist)))
500 (setcar entry new-name))
501 (setq nnml-current-directory nil
502 nnml-current-group nil)
503 ;; Save the new group alist.
504 (nnmail-save-active nnml-group-alist nnml-active-file)
507 (deffoo nnml-set-status (article name value &optional group server)
508 (nnml-possibly-change-directory group server)
509 (let ((file (nnml-article-to-file article)))
511 ((not (file-exists-p file))
512 (nnheader-report 'nnml "File %s does not exist" file))
515 (nnheader-insert-file-contents file)
516 (nnmail-replace-status name value))
520 ;;; Internal functions.
522 (defun nnml-article-to-file (article)
523 (nnml-update-file-alist)
525 (if (setq file (cdr (assq article nnml-article-file-alist)))
526 (expand-file-name file nnml-current-directory)
527 (if (not nnheader-directory-files-is-safe)
528 ;; Just to make sure nothing went wrong when reading over NFS --
531 (setq file (expand-file-name (number-to-string article)
532 nnml-current-directory)))
533 (nnml-update-file-alist t)
536 (defun nnml-deletable-article-p (group article)
537 "Say whether ARTICLE in GROUP can be deleted."
539 (when (setq path (nnml-article-to-file article))
540 (when (file-writable-p path)
541 (or (not nnmail-keep-last-article)
542 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
545 ;; Find an article number in the current group given the Message-ID.
546 (defun nnml-find-group-number (id)
548 (set-buffer (get-buffer-create " *nnml id*"))
549 (let ((alist nnml-group-alist)
551 ;; We want to look through all .overview files, but we want to
552 ;; start with the one in the current directory. It seems most
553 ;; likely that the article we are looking for is in that group.
554 (if (setq number (nnml-find-id nnml-current-group id))
555 (cons nnml-current-group number)
556 ;; It wasn't there, so we look through the other groups as well.
557 (while (and (not number)
559 (or (string= (caar alist) nnml-current-group)
560 (setq number (nnml-find-id (caar alist) id)))
562 (setq alist (cdr alist))))
564 (cons (caar alist) number))))))
566 (defun nnml-find-id (group id)
568 (let ((nov (expand-file-name nnml-nov-file-name
569 (nnmail-group-pathname group nnml-directory)))
571 (when (file-exists-p nov)
572 (nnheader-insert-file-contents nov)
573 (while (and (not found)
574 (search-forward id nil t)) ; We find the ID.
575 ;; And the id is in the fourth field.
576 (if (not (and (search-backward "\t" nil t 4)
577 (not (search-backward"\t" (gnus-point-at-bol) t))))
581 ;; We return the article number.
583 (ignore-errors (read (current-buffer))))))
586 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
587 (if (or gnus-nov-is-evil nnml-nov-is-evil)
589 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
590 (when (file-exists-p nov)
592 (set-buffer nntp-server-buffer)
594 (nnheader-insert-file-contents nov)
596 (not (numberp fetch-old)))
597 t ; Don't remove anything.
598 (nnheader-nov-delete-outside-range
599 (if fetch-old (max 1 (- (car articles) fetch-old))
601 (car (last articles)))
604 (defun nnml-possibly-change-directory (group &optional server)
606 (not (nnml-server-opened server)))
607 (nnml-open-server server))
610 (let ((pathname (nnmail-group-pathname group nnml-directory))
611 (file-name-coding-system nnmail-pathname-coding-system))
612 (when (not (equal pathname nnml-current-directory))
613 (setq nnml-current-directory pathname
614 nnml-current-group group
615 nnml-article-file-alist nil))
616 (file-exists-p nnml-current-directory))))
618 (defun nnml-possibly-create-directory (group)
619 (let ((dir (nnmail-group-pathname group nnml-directory)))
620 (unless (file-exists-p dir)
621 (make-directory (directory-file-name dir) t)
622 (nnheader-message 5 "Creating mail directory %s" dir))))
624 (defun nnml-save-mail (group-art)
625 "Called narrowed to an article."
627 (setq chars (nnmail-insert-lines))
628 (nnmail-insert-xref group-art)
629 (run-hooks 'nnmail-prepare-save-mail-hook)
630 (run-hooks 'nnml-prepare-save-mail-hook)
631 (goto-char (point-min))
632 (while (looking-at "From ")
633 (replace-match "X-From-Line: ")
635 ;; We save the article in all the groups it belongs in.
639 (nnml-possibly-create-directory (caar ga))
640 (let ((file (concat (nnmail-group-pathname
641 (caar ga) nnml-directory)
642 (int-to-string (cdar ga)))))
644 ;; It was already saved, so we just make a hard link.
645 (funcall nnmail-crosspost-link-function first file t)
647 (nnmail-write-region (point-min) (point-max) file nil
648 (if (nnheader-be-verbose 5) nil 'nomesg))
651 ;; Generate a nov line for this article. We generate the nov
652 ;; line after saving, because nov generation destroys the
654 (setq headers (nnml-parse-head chars))
655 ;; Output the nov line to all nov databases that should have it.
656 (let ((ga group-art))
658 (nnml-add-nov (caar ga) (cdar ga) headers)
662 (defun nnml-active-number (group)
663 "Compute the next article number in GROUP."
664 (let ((active (cadr (assoc group nnml-group-alist))))
665 ;; The group wasn't known to nnml, so we just create an active
668 ;; Perhaps the active file was corrupt? See whether
669 ;; there are any articles in this group.
670 (nnml-possibly-create-directory group)
671 (nnml-possibly-change-directory group)
672 (unless nnml-article-file-alist
673 (setq nnml-article-file-alist
675 (nnml-article-to-file-alist nnml-current-directory)
676 'car-less-than-car)))
678 (if nnml-article-file-alist
679 (cons (caar nnml-article-file-alist)
680 (caar (last nnml-article-file-alist)))
682 (push (list group active) nnml-group-alist))
683 (setcdr active (1+ (cdr active)))
684 (while (file-exists-p
685 (expand-file-name (int-to-string (cdr active))
686 (nnmail-group-pathname group nnml-directory)))
687 (setcdr active (1+ (cdr active))))
690 (defun nnml-add-nov (group article headers)
691 "Add a nov line for the GROUP base."
693 (set-buffer (nnml-open-nov group))
694 (goto-char (point-max))
695 (mail-header-set-number headers article)
696 (nnheader-insert-nov headers)))
698 (defsubst nnml-header-value ()
699 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
701 (defun nnml-parse-head (chars &optional number)
702 "Parse the head of the current buffer."
705 (unless (zerop (buffer-size))
707 (goto-char (point-min))
708 (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
709 ;; Fold continuation lines.
710 (goto-char (point-min))
711 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
712 (replace-match " " t t))
713 ;; Remove any tabs; they are too confusing.
714 (subst-char-in-region (point-min) (point-max) ?\t ? )
715 (let ((headers (nnheader-parse-head t)))
716 (mail-header-set-chars headers chars)
717 (mail-header-set-number headers number)
720 (defun nnml-get-nov-buffer (group)
721 (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
724 (set (make-local-variable 'nnml-nov-buffer-file-name)
727 (nnmail-group-pathname group nnml-directory)))
729 (when (file-exists-p nnml-nov-buffer-file-name)
730 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
733 (defun nnml-open-nov (group)
734 (or (cdr (assoc group nnml-nov-buffer-alist))
735 (let ((buffer (nnml-get-nov-buffer group)))
736 (push (cons group buffer) nnml-nov-buffer-alist)
739 (defun nnml-save-nov ()
741 (while nnml-nov-buffer-alist
742 (when (buffer-name (cdar nnml-nov-buffer-alist))
743 (set-buffer (cdar nnml-nov-buffer-alist))
744 (when (buffer-modified-p)
745 (nnmail-write-region 1 (point-max) nnml-nov-buffer-file-name
747 (set-buffer-modified-p nil)
748 (kill-buffer (current-buffer)))
749 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
752 (defun nnml-generate-nov-databases (&optional server)
753 "Generate NOV databases in all nnml directories."
754 (interactive (list (or (nnoo-current-server 'nnml) "")))
755 ;; Read the active file to make sure we don't re-use articles
756 ;; numbers in empty groups.
757 (nnmail-activate 'nnml)
758 (unless (nnml-server-opened server)
759 (nnml-open-server server))
760 (setq nnml-directory (expand-file-name nnml-directory))
761 ;; Recurse down the directories.
762 (nnml-generate-nov-databases-1 nnml-directory nil t)
763 ;; Save the active file.
764 (nnmail-save-active nnml-group-alist nnml-active-file))
766 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
767 "Regenerate the NOV database in DIR."
768 (interactive "DRegenerate NOV in: ")
769 (setq dir (file-name-as-directory dir))
770 ;; Only scan this sub-tree if we haven't been here yet.
771 (unless (member (file-truename dir) seen)
772 (push (file-truename dir) seen)
773 ;; We descend recursively
774 (let ((dirs (directory-files dir t nil t))
776 (while (setq dir (pop dirs))
777 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
778 (file-directory-p dir))
779 (nnml-generate-nov-databases-1 dir seen))))
780 ;; Do this directory.
781 (let ((files (sort (nnheader-article-to-file-alist dir)
782 'car-less-than-car)))
784 (let* ((group (nnheader-file-to-group
785 (directory-file-name dir) nnml-directory))
786 (info (cadr (assoc group nnml-group-alist))))
788 (setcar info (1+ (cdr info)))))
789 (funcall nnml-generate-active-function dir)
790 ;; Generate the nov file.
791 (nnml-generate-nov-file dir files)
793 (nnmail-save-active nnml-group-alist nnml-active-file))))))
795 (eval-when-compile (defvar files))
796 (defun nnml-generate-active-info (dir)
797 ;; Update the active info for this group.
798 (let* ((group (nnheader-file-to-group
799 (directory-file-name dir) nnml-directory))
800 (entry (assoc group nnml-group-alist))
801 (last (or (caadr entry) 0)))
802 (setq nnml-group-alist (delq entry nnml-group-alist))
804 (cons (or (caar files) (1+ last))
807 (while (cdr f) (setq f (cdr f)))
812 (defun nnml-generate-nov-file (dir files)
813 (let* ((dir (file-name-as-directory dir))
814 (nov (concat dir nnml-nov-file-name))
815 (nov-buffer (get-buffer-create " *nov*"))
818 ;; Init the nov buffer.
819 (set-buffer nov-buffer)
820 (buffer-disable-undo)
822 (set-buffer nntp-server-buffer)
823 ;; Delete the old NOV file.
824 (when (file-exists-p nov)
825 (funcall nnmail-delete-file-function nov))
827 (unless (file-directory-p (setq file (concat dir (cdar files))))
829 (nnheader-insert-file-contents file)
831 (goto-char (point-min))
833 (search-forward "\n\n" nil t)
834 (setq chars (- (point-max) (point)))
835 (max 1 (1- (point)))))
836 (unless (zerop (buffer-size))
837 (goto-char (point-min))
838 (setq headers (nnml-parse-head chars (caar files)))
840 (set-buffer nov-buffer)
841 (goto-char (point-max))
842 (nnheader-insert-nov headers)))
844 (setq files (cdr files)))
846 (set-buffer nov-buffer)
847 (nnmail-write-region 1 (point-max) nov nil 'nomesg)
848 (kill-buffer (current-buffer))))))
850 (defun nnml-nov-delete-article (group article)
852 (set-buffer (nnml-open-nov group))
853 (when (nnheader-find-nov-line article)
854 (delete-region (point) (progn (forward-line 1) (point)))
856 (let ((active (cadr (assoc group nnml-group-alist)))
860 (setf (car active) (1+ (cdr active)))
861 (when (and (setq num (ignore-errors (read (current-buffer))))
863 (setf (car active) num)))))))
866 (defun nnml-update-file-alist (&optional force)
867 (when (or (not nnml-article-file-alist)
869 (setq nnml-article-file-alist
870 (nnml-article-to-file-alist nnml-current-directory))))
872 (defun nnml-directory-articles (dir)
873 "Return a list of all article files in a directory.
874 Use the nov database for that directory if available."
875 (if (or gnus-nov-is-evil nnml-nov-is-evil
877 (expand-file-name nnml-nov-file-name dir))))
878 (nnheader-directory-articles dir)
879 ;; build list from .overview if available
880 ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
881 ;; defvoo'd, and we might get called when it hasn't been swapped in.
885 (buffer (nnml-get-nov-buffer nnml-current-group)))
887 (goto-char (point-min))
889 (setq art (read (current-buffer)))
894 (defun nnml-article-to-file-alist (dir)
895 "Return an alist of article/file pairs in DIR.
896 Use the nov database for that directory if available."
897 (if (or gnus-nov-is-evil nnml-nov-is-evil
899 (expand-file-name nnml-nov-file-name
900 nnml-current-directory))))
901 (nnheader-article-to-file-alist nnml-current-directory)
902 ;; build list from .overview if available
906 (buffer (nnml-get-nov-buffer nnml-current-group)))
908 (goto-char (point-min))
910 (setq art (read (current-buffer)))
911 ;; assume file name is unadorned (ie. not compressed etc)
912 (push (cons art (int-to-string art)) alist)
916 (deffoo nnml-request-set-mark (group actions &optional server)
917 (nnml-possibly-change-directory group server)
918 (unless nnml-marks-is-evil
919 (nnml-open-marks group server)
920 (dolist (action actions)
921 (let ((range (nth 0 action))
922 (what (nth 1 action))
923 (marks (nth 2 action)))
924 (assert (or (eq what 'add) (eq what 'del)) t
925 "Unknown request-set-mark action: %s" what)
927 (setq nnml-marks (gnus-update-alist-soft
929 (funcall (if (eq what 'add) 'gnus-range-add
930 'gnus-remove-from-range)
931 (cdr (assoc mark nnml-marks)) range)
933 (nnml-save-marks group server))
936 (deffoo nnml-request-update-info (group info &optional server)
937 (nnml-possibly-change-directory group server)
938 (unless nnml-marks-is-evil
939 (nnheader-message 8 "Updating marks for %s..." group)
940 (nnml-open-marks group server)
941 ;; Update info using `nnml-marks'.
942 (mapcar (lambda (pred)
945 (gnus-update-alist-soft
947 (cdr (assq (cdr pred) nnml-marks))
948 (gnus-info-marks info))
950 gnus-article-mark-lists)
951 (let ((seen (cdr (assq 'read nnml-marks))))
952 (gnus-info-set-read info
953 (if (and (integerp (car seen))
955 (list (cons (car seen) (car seen)))
957 (nnheader-message 8 "Updating marks for %s...done" group))
960 (defun nnml-save-marks (group server)
961 (let ((file-name-coding-system nnmail-pathname-coding-system)
962 (file (expand-file-name nnml-marks-file-name
963 (nnmail-group-pathname group nnml-directory))))
966 (nnml-possibly-create-directory group)
969 (princ nnml-marks (current-buffer))
971 (error (or (gnus-yes-or-no-p
972 (format "Could not write to %s (%s). Continue? " file err))
973 (error "Cannot write to %s (%s)" err))))))
975 (defun nnml-open-marks (group server)
976 (let ((file (expand-file-name
978 (nnmail-group-pathname group nnml-directory))))
979 (if (file-exists-p file)
980 (setq nnml-marks (condition-case err
982 (nnheader-insert-file-contents file)
983 (read (current-buffer)))
984 (error (or (gnus-yes-or-no-p
985 (format "Error reading nnml marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
986 (error "Cannot read nnml marks file %s (%s)" file err)))))
987 ;; User didn't have a .marks file. Probably first time
988 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
989 (let ((info (gnus-get-info
990 (gnus-group-prefixed-name
992 (gnus-server-to-method (format "nnml:%s" server))))))
993 (nnheader-message 7 "Bootstrapping marks for %s..." group)
994 (setq nnml-marks (gnus-info-marks info))
995 (push (cons 'read (gnus-info-read info)) nnml-marks)
996 (nnml-save-marks group server)))))
1000 ;;; nnml.el ends here