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))
45 (defvoo nnml-directory message-directory
46 "Spool directory for the nnml mail backend.
48 This variable is a virtual server slot. See the Gnus manual for details.")
50 (defvoo nnml-active-file
51 (expand-file-name "active" nnml-directory)
54 This variable is a virtual server slot. See the Gnus manual for details.")
56 (defvoo nnml-newsgroups-file
57 (expand-file-name "newsgroups" nnml-directory)
58 "Mail newsgroups description file.
60 This variable is a virtual server slot. See the Gnus manual for details.")
62 (defvoo nnml-get-new-mail t
63 "If non-nil, nnml will check the incoming mail file and split the mail.
65 This variable is a virtual server slot. See the Gnus manual for details.")
67 (defvoo nnml-nov-is-evil nil
68 "If non-nil, Gnus will never generate and use nov databases for mail spools.
69 Using nov databases will speed up header fetching considerably.
70 This variable shouldn't be flipped much. If you have, for some reason,
71 set this to t, and want to set it to nil again, you should always run
72 the `nnml-generate-nov-databases' command. The function will go
73 through all nnml directories and generate nov databases for them
74 all. This may very well take some time.
76 This variable is a virtual server slot. See the Gnus manual for details.")
78 (defvoo nnml-marks-is-evil nil
79 "If non-nil, Gnus will never generate and use marks file for mail spools.
80 Using marks files makes it possible to backup and restore mail groups
81 separately from `.newsrc.eld'. If you have, for some reason, set this
82 to t, and want to set it to nil again, you should always remove the
83 corresponding marks file (usually named `.marks' in the nnml group
84 directory, but see `nnml-marks-file-name') for the group. Then the
85 marks file will be regenerated properly by Gnus.
87 This variable is a virtual server slot. See the Gnus manual for details.")
89 (defvoo nnml-filenames-are-evil t
90 "If non-nil, Gnus will not assume that the articles file name
91 is the same as the article number listed in the nov database. This
92 variable should be set if any of the files are compressed.
94 This variable is a virtual server slot. See the Gnus manual for details.")
96 (defvoo nnml-prepare-save-mail-hook nil
97 "Hook run narrowed to an article before saving.
99 This variable is a virtual server slot. See the Gnus manual for details.")
101 (defvoo nnml-inhibit-expiry nil
102 "If non-nil, inhibit expiry.
104 This variable is a virtual server slot. See the Gnus manual for details.")
109 (defconst nnml-version "nnml 1.0"
112 (defvoo nnml-nov-file-name ".overview")
113 (defvoo nnml-marks-file-name ".marks")
115 (defvoo nnml-current-directory nil)
116 (defvoo nnml-current-group nil)
117 (defvoo nnml-status-string "")
118 (defvoo nnml-nov-buffer-alist nil)
119 (defvoo nnml-group-alist nil)
120 (defvoo nnml-active-timestamp nil)
121 (defvoo nnml-article-file-alist nil)
123 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
125 (defvar nnml-nov-buffer-file-name nil)
127 (defvoo nnml-file-coding-system nnmail-file-coding-system)
129 (defvoo nnml-marks nil)
131 (defvar nnml-marks-modtime (gnus-make-hashtable))
134 ;;; Interface functions.
136 (nnoo-define-basics nnml)
138 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
139 (when (nnml-possibly-change-directory group server)
141 (set-buffer nntp-server-buffer)
144 (number (length sequence))
146 (file-name-coding-system nnmail-pathname-coding-system)
147 (pathname-coding-system nnmail-pathname-coding-system)
149 (if (stringp (car sequence))
151 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
154 (setq article (car sequence))
155 (setq file (nnml-article-to-file article))
158 (not (file-directory-p file)))
159 (insert (format "221 %d Article retrieved.\n" article))
161 (nnheader-insert-head file)
163 (if (search-forward "\n\n" nil t)
165 (goto-char (point-max))
168 (delete-region (point) (point-max)))
169 (setq sequence (cdr sequence))
170 (setq count (1+ count))
171 (and (numberp nnmail-large-newsgroup)
172 (> number nnmail-large-newsgroup)
174 (nnheader-message 6 "nnml: Receiving headers... %d%%"
175 (/ (* count 100) number))))
177 (and (numberp nnmail-large-newsgroup)
178 (> number nnmail-large-newsgroup)
179 (nnheader-message 6 "nnml: Receiving headers...done"))
181 (nnheader-fold-continuation-lines)
184 (deffoo nnml-open-server (server &optional defs)
185 (nnoo-change-server 'nnml server defs)
186 (when (not (file-exists-p nnml-directory))
187 (ignore-errors (make-directory nnml-directory t)))
189 ((not (file-exists-p nnml-directory))
191 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
192 ((not (file-directory-p (file-truename nnml-directory)))
194 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
196 (nnheader-report 'nnml "Opened server %s using directory %s"
197 server nnml-directory)
200 (deffoo nnml-request-regenerate (server)
201 (nnml-possibly-change-directory nil server)
202 (nnml-generate-nov-databases server)
205 (deffoo nnml-request-article (id &optional group server buffer)
206 (nnml-possibly-change-directory group server)
207 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
208 (file-name-coding-system nnmail-pathname-coding-system)
209 (pathname-coding-system nnmail-pathname-coding-system)
210 path gpath group-num)
212 (when (and (setq group-num (nnml-find-group-number id))
214 (assq (cdr group-num)
215 (nnheader-article-to-file-alist
217 (nnmail-group-pathname
220 (setq path (concat gpath (int-to-string (cdr group-num)))))
221 (setq path (nnml-article-to-file id)))
224 (nnheader-report 'nnml "No such article: %s" id))
225 ((not (file-exists-p path))
226 (nnheader-report 'nnml "No such file: %s" path))
227 ((file-directory-p path)
228 (nnheader-report 'nnml "File is a directory: %s" path))
229 ((not (save-excursion (let ((nnmail-file-coding-system
230 nnml-file-coding-system))
231 (nnmail-find-file path))))
232 (nnheader-report 'nnml "Couldn't read file: %s" path))
234 (nnheader-report 'nnml "Article %s retrieved" id)
235 ;; We return the article number.
236 (cons (if group-num (car group-num) group)
237 (string-to-int (file-name-nondirectory path)))))))
239 (deffoo nnml-request-group (group &optional server dont-check)
240 (let ((file-name-coding-system nnmail-pathname-coding-system)
241 (pathname-coding-system nnmail-pathname-coding-system))
243 ((not (nnml-possibly-change-directory group server))
244 (nnheader-report 'nnml "Invalid group (no such directory)"))
245 ((not (file-exists-p nnml-current-directory))
246 (nnheader-report 'nnml "Directory %s does not exist"
247 nnml-current-directory))
248 ((not (file-directory-p nnml-current-directory))
249 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
251 (nnheader-report 'nnml "Group %s selected" group)
254 (nnheader-re-read-dir nnml-current-directory)
255 (nnmail-activate 'nnml)
256 (let ((active (nth 1 (assoc group nnml-group-alist))))
258 (nnheader-report 'nnml "No such group: %s" group)
259 (nnheader-report 'nnml "Selected group %s" group)
260 (nnheader-insert "211 %d %d %d %s\n"
261 (max (1+ (- (cdr active) (car active))) 0)
262 (car active) (cdr active) group)))))))
264 (deffoo nnml-request-scan (&optional group server)
265 (setq nnml-article-file-alist nil)
266 (nnml-possibly-change-directory group server)
267 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
269 (deffoo nnml-close-group (group &optional server)
270 (setq nnml-article-file-alist nil)
273 (deffoo nnml-request-create-group (group &optional server args)
274 (nnml-possibly-change-directory nil server)
275 (nnmail-activate 'nnml)
277 ((assoc group nnml-group-alist)
279 ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
280 (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
281 (nnheader-report 'nnml "%s is a file"
282 (nnmail-group-pathname group nnml-directory)))
285 (push (list group (setq active (cons 1 0)))
287 (nnml-possibly-create-directory group)
288 (nnml-possibly-change-directory group server)
289 (let ((articles (nnml-directory-articles nnml-current-directory)))
291 (setcar active (apply 'min articles))
292 (setcdr active (apply 'max articles))))
293 (nnmail-save-active nnml-group-alist nnml-active-file)
296 (deffoo nnml-request-list (&optional server)
298 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
299 (file-name-coding-system nnmail-pathname-coding-system)
300 (pathname-coding-system nnmail-pathname-coding-system))
301 (nnmail-find-file nnml-active-file))
302 (setq nnml-group-alist (nnmail-get-active))
305 (deffoo nnml-request-newgroups (date &optional server)
306 (nnml-request-list server))
308 (deffoo nnml-request-list-newsgroups (&optional server)
310 (nnmail-find-file nnml-newsgroups-file)))
312 (deffoo nnml-request-expire-articles (articles group &optional server force)
313 (nnml-possibly-change-directory group server)
314 (let ((active-articles
315 (nnml-directory-articles nnml-current-directory))
317 article rest mod-time number)
318 (nnmail-activate 'nnml)
320 (setq active-articles (sort active-articles '<))
321 ;; Articles not listed in active-articles are already gone,
322 ;; so don't try to expire them.
323 (setq articles (gnus-sorted-intersection articles active-articles))
325 (while (and articles is-old)
326 (if (and (setq article (nnml-article-to-file (setq number (pop articles))))
327 (setq mod-time (nth 5 (file-attributes article)))
328 (nnml-deletable-article-p group number)
329 (setq is-old (nnmail-expired-article-p group mod-time force
330 nnml-inhibit-expiry)))
332 ;; Allow a special target group.
333 (unless (eq nnmail-expiry-target 'delete)
335 (nnml-request-article number group server (current-buffer))
336 (let (nnml-current-directory
338 nnml-article-file-alist)
339 (nnmail-expiry-target-group nnmail-expiry-target group))))
340 (nnheader-message 5 "Deleting article %s in %s"
343 (funcall nnmail-delete-file-function article)
346 (setq active-articles (delq number active-articles))
347 (nnml-nov-delete-article group number))
349 (let ((active (nth 1 (assoc group nnml-group-alist))))
351 (setcar active (or (and active-articles
352 (apply 'min active-articles))
354 (nnmail-save-active nnml-group-alist nnml-active-file))
356 (nconc rest articles)))
358 (deffoo nnml-request-move-article
359 (article group server accept-form &optional last)
360 (let ((buf (get-buffer-create " *nnml move*"))
362 (nnml-possibly-change-directory group server)
363 (nnml-update-file-alist)
365 (nnml-deletable-article-p group article)
366 (nnml-request-article article group server)
367 (let (nnml-current-directory
369 nnml-article-file-alist)
372 (insert-buffer-substring nntp-server-buffer)
373 (setq result (eval accept-form))
374 (kill-buffer (current-buffer))
377 (nnml-possibly-change-directory group server)
379 (funcall nnmail-delete-file-function
380 (nnml-article-to-file article))
382 (nnml-nov-delete-article group article)
385 (nnmail-save-active nnml-group-alist nnml-active-file))))
388 (deffoo nnml-request-accept-article (group &optional server last)
389 (nnml-possibly-change-directory group server)
390 (nnmail-check-syntax)
392 (when nnmail-cache-accepted-message-ids
393 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
396 (nnmail-activate 'nnml)
397 (setq result (car (nnml-save-mail
398 (list (cons group (nnml-active-number group))))))
400 (nnmail-save-active nnml-group-alist nnml-active-file)
401 (and last (nnml-save-nov))))
403 (nnmail-activate 'nnml)
404 (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
405 (yes-or-no-p "Moved to `junk' group; delete article? "))
407 (setq result (car (nnml-save-mail result))))
409 (nnmail-save-active nnml-group-alist nnml-active-file)
410 (when nnmail-cache-accepted-message-ids
411 (nnmail-cache-close))
415 (deffoo nnml-request-post (&optional server)
416 (nnmail-do-request-post 'nnml-request-accept-article server))
418 (deffoo nnml-request-replace-article (article group buffer)
419 (nnml-possibly-change-directory group)
422 (nnml-possibly-create-directory group)
423 (let ((chars (nnmail-insert-lines))
424 (art (concat (int-to-string article) "\t"))
428 (point-min) (point-max)
429 (or (nnml-article-to-file article)
430 (expand-file-name (int-to-string article)
431 nnml-current-directory))
432 nil (if (nnheader-be-verbose 5) nil 'nomesg))
434 (setq headers (nnml-parse-head chars article))
435 ;; Replace the NOV line in the NOV file.
437 (set-buffer (nnml-open-nov group))
438 (goto-char (point-min))
439 (if (or (looking-at art)
440 (search-forward (concat "\n" art) nil t))
441 ;; Delete the old NOV line.
442 (delete-region (progn (beginning-of-line) (point))
443 (progn (forward-line 1) (point)))
444 ;; The line isn't here, so we have to find out where
445 ;; we should insert it. (This situation should never
446 ;; occur, but one likes to make sure...)
447 (while (and (looking-at "[0-9]+\t")
450 (match-beginning 0) (match-end 0)))
452 (zerop (forward-line 1)))))
454 (nnheader-insert-nov headers)
458 (deffoo nnml-request-delete-group (group &optional force server)
459 (nnml-possibly-change-directory group server)
461 ;; Delete all articles in GROUP.
464 nnml-current-directory t
465 (concat nnheader-numerical-short-files
466 "\\|" (regexp-quote nnml-nov-file-name) "$"
467 "\\|" (regexp-quote nnml-marks-file-name) "$")))
470 (setq article (pop articles))
471 (when (file-writable-p article)
472 (nnheader-message 5 "Deleting article %s in %s..." article group)
473 (funcall nnmail-delete-file-function article))))
474 ;; Try to delete the directory itself.
475 (ignore-errors (delete-directory nnml-current-directory)))
476 ;; Remove the group from all structures.
477 (setq nnml-group-alist
478 (delq (assoc group nnml-group-alist) nnml-group-alist)
479 nnml-current-group nil
480 nnml-current-directory nil)
481 ;; Save the active file.
482 (nnmail-save-active nnml-group-alist nnml-active-file)
485 (deffoo nnml-request-rename-group (group new-name &optional server)
486 (nnml-possibly-change-directory group server)
487 (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
488 (old-dir (nnmail-group-pathname group nnml-directory)))
490 (make-directory new-dir t)
492 ;; We move the articles file by file instead of renaming
493 ;; the directory -- there may be subgroups in this group.
494 ;; One might be more clever, I guess.
495 (let ((files (nnheader-article-to-file-alist old-dir)))
498 (concat old-dir (cdar files))
499 (concat new-dir (cdar files)))
501 ;; Move .overview file.
502 (let ((overview (concat old-dir nnml-nov-file-name)))
503 (when (file-exists-p overview)
504 (rename-file overview (concat new-dir nnml-nov-file-name))))
506 (let ((marks (concat old-dir nnml-marks-file-name)))
507 (when (file-exists-p marks)
508 (rename-file marks (concat new-dir nnml-marks-file-name))))
509 (when (<= (length (directory-files old-dir)) 2)
510 (ignore-errors (delete-directory old-dir)))
511 ;; That went ok, so we change the internal structures.
512 (let ((entry (assoc group nnml-group-alist)))
514 (setcar entry new-name))
515 (setq nnml-current-directory nil
516 nnml-current-group nil)
517 ;; Save the new group alist.
518 (nnmail-save-active nnml-group-alist nnml-active-file)
521 (deffoo nnml-set-status (article name value &optional group server)
522 (nnml-possibly-change-directory group server)
523 (let ((file (nnml-article-to-file article)))
525 ((not (file-exists-p file))
526 (nnheader-report 'nnml "File %s does not exist" file))
529 (nnheader-insert-file-contents file)
530 (nnmail-replace-status name value))
534 ;;; Internal functions.
536 (defun nnml-article-to-file (article)
537 (nnml-update-file-alist)
539 (if (setq file (cdr (assq article nnml-article-file-alist)))
540 (expand-file-name file nnml-current-directory)
541 (if (not nnheader-directory-files-is-safe)
542 ;; Just to make sure nothing went wrong when reading over NFS --
545 (setq file (expand-file-name (number-to-string article)
546 nnml-current-directory)))
547 (nnml-update-file-alist t)
550 (defun nnml-deletable-article-p (group article)
551 "Say whether ARTICLE in GROUP can be deleted."
553 (when (setq path (nnml-article-to-file article))
554 (when (file-writable-p path)
555 (or (not nnmail-keep-last-article)
556 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
559 ;; Find an article number in the current group given the Message-ID.
560 (defun nnml-find-group-number (id)
562 (set-buffer (get-buffer-create " *nnml id*"))
563 (let ((alist nnml-group-alist)
565 ;; We want to look through all .overview files, but we want to
566 ;; start with the one in the current directory. It seems most
567 ;; likely that the article we are looking for is in that group.
568 (if (setq number (nnml-find-id nnml-current-group id))
569 (cons nnml-current-group number)
570 ;; It wasn't there, so we look through the other groups as well.
571 (while (and (not number)
573 (or (string= (caar alist) nnml-current-group)
574 (setq number (nnml-find-id (caar alist) id)))
576 (setq alist (cdr alist))))
578 (cons (caar alist) number))))))
580 (defun nnml-find-id (group id)
582 (let ((nov (expand-file-name nnml-nov-file-name
583 (nnmail-group-pathname group nnml-directory)))
585 (when (file-exists-p nov)
586 (nnheader-insert-file-contents nov)
587 (while (and (not found)
588 (search-forward id nil t)) ; We find the ID.
589 ;; And the id is in the fourth field.
590 (if (not (and (search-backward "\t" nil t 4)
591 (not (search-backward"\t" (gnus-point-at-bol) t))))
595 ;; We return the article number.
597 (ignore-errors (read (current-buffer))))))
600 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
601 (if (or gnus-nov-is-evil nnml-nov-is-evil)
603 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
604 (when (file-exists-p nov)
606 (set-buffer nntp-server-buffer)
608 (nnheader-insert-file-contents nov)
610 (not (numberp fetch-old)))
611 t ; Don't remove anything.
612 (nnheader-nov-delete-outside-range
613 (if fetch-old (max 1 (- (car articles) fetch-old))
615 (car (last articles)))
618 (defun nnml-possibly-change-directory (group &optional server)
620 (not (nnml-server-opened server)))
621 (nnml-open-server server))
624 (let ((pathname (nnmail-group-pathname group nnml-directory))
625 (file-name-coding-system nnmail-pathname-coding-system)
626 (pathname-coding-system nnmail-pathname-coding-system))
627 (when (not (equal pathname nnml-current-directory))
628 (setq nnml-current-directory pathname
629 nnml-current-group group
630 nnml-article-file-alist nil))
631 (file-exists-p nnml-current-directory))))
633 (defun nnml-possibly-create-directory (group)
634 (let ((dir (nnmail-group-pathname group nnml-directory)))
635 (unless (file-exists-p dir)
636 (make-directory (directory-file-name dir) t)
637 (nnheader-message 5 "Creating mail directory %s" dir))))
639 (defun nnml-save-mail (group-art)
640 "Called narrowed to an article."
642 (setq chars (nnmail-insert-lines))
643 (nnmail-insert-xref group-art)
644 (run-hooks 'nnmail-prepare-save-mail-hook)
645 (run-hooks 'nnml-prepare-save-mail-hook)
646 (goto-char (point-min))
647 (while (looking-at "From ")
648 (replace-match "X-From-Line: ")
650 ;; We save the article in all the groups it belongs in.
654 (nnml-possibly-create-directory (caar ga))
655 (let ((file (concat (nnmail-group-pathname
656 (caar ga) nnml-directory)
657 (int-to-string (cdar ga)))))
659 ;; It was already saved, so we just make a hard link.
660 (funcall nnmail-crosspost-link-function first file t)
662 (nnmail-write-region (point-min) (point-max) file nil
663 (if (nnheader-be-verbose 5) nil 'nomesg))
666 ;; Generate a nov line for this article. We generate the nov
667 ;; line after saving, because nov generation destroys the
669 (setq headers (nnml-parse-head chars))
670 ;; Output the nov line to all nov databases that should have it.
671 (let ((ga group-art))
673 (nnml-add-nov (caar ga) (cdar ga) headers)
677 (defun nnml-active-number (group)
678 "Compute the next article number in GROUP."
679 (let ((active (cadr (assoc group nnml-group-alist))))
680 ;; The group wasn't known to nnml, so we just create an active
683 ;; Perhaps the active file was corrupt? See whether
684 ;; there are any articles in this group.
685 (nnml-possibly-create-directory group)
686 (nnml-possibly-change-directory group)
687 (unless nnml-article-file-alist
688 (setq nnml-article-file-alist
690 (nnml-current-group-article-to-file-alist)
691 'car-less-than-car)))
693 (if nnml-article-file-alist
694 (cons (caar nnml-article-file-alist)
695 (caar (last nnml-article-file-alist)))
697 (push (list group active) nnml-group-alist))
698 (setcdr active (1+ (cdr active)))
699 (while (file-exists-p
700 (expand-file-name (int-to-string (cdr active))
701 (nnmail-group-pathname group nnml-directory)))
702 (setcdr active (1+ (cdr active))))
705 (defun nnml-add-nov (group article headers)
706 "Add a nov line for the GROUP base."
708 (set-buffer (nnml-open-nov group))
709 (goto-char (point-max))
710 (mail-header-set-number headers article)
711 (nnheader-insert-nov headers)))
713 (defsubst nnml-header-value ()
714 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
716 (defun nnml-parse-head (chars &optional number)
717 "Parse the head of the current buffer."
720 (unless (zerop (buffer-size))
722 (goto-char (point-min))
723 (if (search-forward "\n\n" nil t) (1- (point)) (point-max))))
724 ;; Fold continuation lines.
725 (goto-char (point-min))
726 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
727 (replace-match " " t t))
728 ;; Remove any tabs; they are too confusing.
729 (subst-char-in-region (point-min) (point-max) ?\t ? )
730 (let ((headers (nnheader-parse-head t)))
731 (mail-header-set-chars headers chars)
732 (mail-header-set-number headers number)
735 (defun nnml-get-nov-buffer (group)
736 (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
739 (set (make-local-variable 'nnml-nov-buffer-file-name)
742 (nnmail-group-pathname group nnml-directory)))
744 (when (file-exists-p nnml-nov-buffer-file-name)
745 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
748 (defun nnml-open-nov (group)
749 (or (cdr (assoc group nnml-nov-buffer-alist))
750 (let ((buffer (nnml-get-nov-buffer group)))
751 (push (cons group buffer) nnml-nov-buffer-alist)
754 (defun nnml-save-nov ()
756 (while nnml-nov-buffer-alist
757 (when (buffer-name (cdar nnml-nov-buffer-alist))
758 (set-buffer (cdar nnml-nov-buffer-alist))
759 (when (buffer-modified-p)
760 (nnmail-write-region 1 (point-max) nnml-nov-buffer-file-name
762 (set-buffer-modified-p nil)
763 (kill-buffer (current-buffer)))
764 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
767 (defun nnml-generate-nov-databases (&optional server)
768 "Generate NOV databases in all nnml directories."
769 (interactive (list (or (nnoo-current-server 'nnml) "")))
770 ;; Read the active file to make sure we don't re-use articles
771 ;; numbers in empty groups.
772 (nnmail-activate 'nnml)
773 (unless (nnml-server-opened server)
774 (nnml-open-server server))
775 (setq nnml-directory (expand-file-name nnml-directory))
776 ;; Recurse down the directories.
777 (nnml-generate-nov-databases-1 nnml-directory nil t)
778 ;; Save the active file.
779 (nnmail-save-active nnml-group-alist nnml-active-file))
781 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
782 "Regenerate the NOV database in DIR."
783 (interactive "DRegenerate NOV in: ")
784 (setq dir (file-name-as-directory dir))
785 ;; Only scan this sub-tree if we haven't been here yet.
786 (unless (member (file-truename dir) seen)
787 (push (file-truename dir) seen)
788 ;; We descend recursively
789 (let ((dirs (directory-files dir t nil t))
791 (while (setq dir (pop dirs))
792 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
793 (file-directory-p dir))
794 (nnml-generate-nov-databases-1 dir seen))))
795 ;; Do this directory.
796 (let ((files (sort (nnheader-article-to-file-alist dir)
797 'car-less-than-car)))
799 (let* ((group (nnheader-file-to-group
800 (directory-file-name dir) nnml-directory))
801 (info (cadr (assoc group nnml-group-alist))))
803 (setcar info (1+ (cdr info)))))
804 (funcall nnml-generate-active-function dir)
805 ;; Generate the nov file.
806 (nnml-generate-nov-file dir files)
808 (nnmail-save-active nnml-group-alist nnml-active-file))))))
810 (eval-when-compile (defvar files))
811 (defun nnml-generate-active-info (dir)
812 ;; Update the active info for this group.
813 (let* ((group (nnheader-file-to-group
814 (directory-file-name dir) nnml-directory))
815 (entry (assoc group nnml-group-alist))
816 (last (or (caadr entry) 0)))
817 (setq nnml-group-alist (delq entry nnml-group-alist))
819 (cons (or (caar files) (1+ last))
822 (while (cdr f) (setq f (cdr f)))
827 (defun nnml-generate-nov-file (dir files)
828 (let* ((dir (file-name-as-directory dir))
829 (nov (concat dir nnml-nov-file-name))
830 (nov-buffer (get-buffer-create " *nov*"))
833 ;; Init the nov buffer.
834 (set-buffer nov-buffer)
835 (buffer-disable-undo)
837 (set-buffer nntp-server-buffer)
838 ;; Delete the old NOV file.
839 (when (file-exists-p nov)
840 (funcall nnmail-delete-file-function nov))
842 (unless (file-directory-p (setq file (concat dir (cdar files))))
844 (nnheader-insert-file-contents file)
846 (goto-char (point-min))
848 (search-forward "\n\n" nil t)
849 (setq chars (- (point-max) (point)))
850 (max 1 (1- (point)))))
851 (unless (zerop (buffer-size))
852 (goto-char (point-min))
853 (setq headers (nnml-parse-head chars (caar files)))
855 (set-buffer nov-buffer)
856 (goto-char (point-max))
857 (nnheader-insert-nov headers)))
859 (setq files (cdr files)))
861 (set-buffer nov-buffer)
862 (nnmail-write-region 1 (point-max) nov nil 'nomesg)
863 (kill-buffer (current-buffer))))))
865 (defun nnml-nov-delete-article (group article)
867 (set-buffer (nnml-open-nov group))
868 (when (nnheader-find-nov-line article)
869 (delete-region (point) (progn (forward-line 1) (point)))
871 (let ((active (cadr (assoc group nnml-group-alist)))
875 (setf (car active) (1+ (cdr active)))
876 (when (and (setq num (ignore-errors (read (current-buffer))))
878 (setf (car active) num)))))))
881 (defun nnml-update-file-alist (&optional force)
882 (when (or (not nnml-article-file-alist)
884 (setq nnml-article-file-alist
885 (nnml-current-group-article-to-file-alist))))
887 (defun nnml-directory-articles (dir)
888 "Return a list of all article files in a directory.
889 Use the nov database for that directory if available."
890 (if (or gnus-nov-is-evil nnml-nov-is-evil
892 (expand-file-name nnml-nov-file-name dir))))
893 (nnheader-directory-articles dir)
894 ;; build list from .overview if available
895 ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
896 ;; defvoo'd, and we might get called when it hasn't been swapped in.
900 (buffer (nnml-get-nov-buffer nnml-current-group)))
902 (goto-char (point-min))
904 (setq art (read (current-buffer)))
909 (defun nnml-current-group-article-to-file-alist ()
910 "Return an alist of article/file pairs in the current group.
911 Use the nov database for the current group if available."
912 (if (or gnus-nov-is-evil
914 nnml-filenames-are-evil
916 (expand-file-name nnml-nov-file-name
917 nnml-current-directory))))
918 (nnheader-article-to-file-alist nnml-current-directory)
919 ;; build list from .overview if available
923 (buffer (nnml-get-nov-buffer nnml-current-group)))
925 (goto-char (point-min))
927 (setq art (read (current-buffer)))
928 ;; assume file name is unadorned (ie. not compressed etc)
929 (push (cons art (int-to-string art)) alist)
933 (deffoo nnml-request-set-mark (group actions &optional server)
934 (nnml-possibly-change-directory group server)
935 (unless nnml-marks-is-evil
936 (nnml-open-marks group server)
937 (dolist (action actions)
938 (let ((range (nth 0 action))
939 (what (nth 1 action))
940 (marks (nth 2 action)))
941 (assert (or (eq what 'add) (eq what 'del)) t
942 "Unknown request-set-mark action: %s" what)
944 (setq nnml-marks (gnus-update-alist-soft
946 (funcall (if (eq what 'add) 'gnus-range-add
947 'gnus-remove-from-range)
948 (cdr (assoc mark nnml-marks)) range)
950 (nnml-save-marks group server))
953 (deffoo nnml-request-update-info (group info &optional server)
954 (nnml-possibly-change-directory group server)
955 (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group))
956 (nnheader-message 8 "Updating marks for %s..." group)
957 (nnml-open-marks group server)
958 ;; Update info using `nnml-marks'.
959 (mapcar (lambda (pred)
962 (gnus-update-alist-soft
964 (cdr (assq (cdr pred) nnml-marks))
965 (gnus-info-marks info))
967 gnus-article-mark-lists)
968 (let ((seen (cdr (assq 'read nnml-marks))))
969 (gnus-info-set-read info
970 (if (and (integerp (car seen))
972 (list (cons (car seen) (car seen)))
974 (nnheader-message 8 "Updating marks for %s...done" group))
977 (defun nnml-marks-changed-p (group)
978 (let ((file (expand-file-name nnml-marks-file-name
979 (nnmail-group-pathname group nnml-directory))))
980 (if (null (gnus-gethash file nnml-marks-modtime))
981 t ;; never looked at marks file, assume it has changed
982 (not (eq (gnus-gethash file nnml-marks-modtime)
983 (nth 5 (file-attributes file)))))))
985 (defun nnml-save-marks (group server)
986 (let ((file-name-coding-system nnmail-pathname-coding-system)
987 (file (expand-file-name nnml-marks-file-name
988 (nnmail-group-pathname group nnml-directory))))
991 (nnml-possibly-create-directory group)
994 (princ nnml-marks (current-buffer))
997 (nth 5 (file-attributes file))
999 (error (or (gnus-yes-or-no-p
1000 (format "Could not write to %s (%s). Continue? " file err))
1001 (error "Cannot write to %s (%s)" err))))))
1003 (defun nnml-open-marks (group server)
1004 (let ((file (expand-file-name
1005 nnml-marks-file-name
1006 (nnmail-group-pathname group nnml-directory))))
1007 (if (file-exists-p file)
1008 (setq nnml-marks (condition-case err
1011 (nth 5 (file-attributes file))
1013 (nnheader-insert-file-contents file)
1014 (read (current-buffer)))
1015 (error (or (gnus-yes-or-no-p
1016 (format "Error reading nnml marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1017 (error "Cannot read nnml marks file %s (%s)" file err)))))
1018 ;; User didn't have a .marks file. Probably first time
1019 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1020 (let ((info (gnus-get-info
1021 (gnus-group-prefixed-name
1023 (gnus-server-to-method (format "nnml:%s" server))))))
1024 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1025 (setq nnml-marks (gnus-info-marks info))
1026 (push (cons 'read (gnus-info-read info)) nnml-marks)
1027 (nnml-save-marks group server)))))
1031 ;;; nnml.el ends here