1 ;;; nnml.el --- mail spool access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
6 ;; Authors: Didier Verna <didier@xemacs.org> (adding compaction)
7 ;; Simon Josefsson <simon@josefsson.org> (adding MARKS)
8 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; Keywords: news, mail
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
31 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
32 ;; For an overview of what the interface functions do, please see the
37 (eval-when-compile (require 'cl))
45 (autoload 'gnus-article-unpropagatable-p "gnus-sum"))
49 (defvoo nnml-directory message-directory
50 "Spool directory for the nnml mail backend.")
52 (defvoo nnml-active-file
53 (expand-file-name "active" nnml-directory)
56 (defvoo nnml-newsgroups-file
57 (expand-file-name "newsgroups" nnml-directory)
58 "Mail newsgroups description file.")
60 (defvoo nnml-get-new-mail t
61 "If non-nil, nnml will check the incoming mail file and split the mail.")
63 (defvoo nnml-nov-is-evil nil
64 "If non-nil, Gnus will never generate and use nov databases for mail spools.
65 Using nov databases will speed up header fetching considerably.
66 This variable shouldn't be flipped much. If you have, for some reason,
67 set this to t, and want to set it to nil again, you should always run
68 the `nnml-generate-nov-databases' command. The function will go
69 through all nnml directories and generate nov databases for them
70 all. This may very well take some time.")
72 (defvoo nnml-marks-is-evil nil
73 "If non-nil, Gnus will never generate and use marks file for mail spools.
74 Using marks files makes it possible to backup and restore mail groups
75 separately from `.newsrc.eld'. If you have, for some reason, set this
76 to t, and want to set it to nil again, you should always remove the
77 corresponding marks file (usually named `.marks' in the nnml group
78 directory, but see `nnml-marks-file-name') for the group. Then the
79 marks file will be regenerated properly by Gnus.")
81 (defvoo nnml-prepare-save-mail-hook nil
82 "Hook run narrowed to an article before saving.")
84 (defvoo nnml-inhibit-expiry nil
85 "If non-nil, inhibit expiry.")
87 (defvoo nnml-use-compressed-files nil
88 "If non-nil, allow using compressed message files.")
90 (defvoo nnml-compressed-files-size-threshold 1000
91 "Default size threshold for compressed message files.
92 Message files with bodies larger than that many characters will
93 be automatically compressed if `nnml-use-compressed-files' is
98 (defconst nnml-version "nnml 1.0"
101 (defvoo nnml-nov-file-name ".overview")
102 (defvoo nnml-marks-file-name ".marks")
104 (defvoo nnml-current-directory nil)
105 (defvoo nnml-current-group nil)
106 (defvoo nnml-status-string "")
107 (defvoo nnml-nov-buffer-alist nil)
108 (defvoo nnml-group-alist nil)
109 (defvoo nnml-active-timestamp nil)
110 (defvoo nnml-article-file-alist nil)
112 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
114 (defvar nnml-nov-buffer-file-name nil)
116 (defvoo nnml-file-coding-system nnmail-file-coding-system)
118 (defvoo nnml-marks nil)
120 (defvar nnml-marks-modtime (gnus-make-hashtable))
123 ;;; Interface functions.
125 (nnoo-define-basics nnml)
127 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
128 (when (nnml-possibly-change-directory group server)
130 (set-buffer nntp-server-buffer)
133 (number (length sequence))
135 (file-name-coding-system nnmail-pathname-coding-system)
137 (if (stringp (car sequence))
139 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
142 (setq article (car sequence))
143 (setq file (nnml-article-to-file article))
146 (not (file-directory-p file)))
147 (insert (format "221 %d Article retrieved.\n" article))
149 (nnheader-insert-head file)
151 (if (re-search-forward "\n\r?\n" nil t)
153 (goto-char (point-max))
156 (delete-region (point) (point-max)))
157 (setq sequence (cdr sequence))
158 (setq count (1+ count))
159 (and (numberp nnmail-large-newsgroup)
160 (> number nnmail-large-newsgroup)
162 (nnheader-message 6 "nnml: Receiving headers... %d%%"
163 (/ (* count 100) number))))
165 (and (numberp nnmail-large-newsgroup)
166 (> number nnmail-large-newsgroup)
167 (nnheader-message 6 "nnml: Receiving headers...done"))
169 (nnheader-fold-continuation-lines)
172 (deffoo nnml-open-server (server &optional defs)
173 (nnoo-change-server 'nnml server defs)
174 (when (not (file-exists-p nnml-directory))
175 (ignore-errors (make-directory nnml-directory t)))
177 ((not (file-exists-p nnml-directory))
179 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
180 ((not (file-directory-p (file-truename nnml-directory)))
182 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
184 (nnheader-report 'nnml "Opened server %s using directory %s"
185 server nnml-directory)
188 (deffoo nnml-request-regenerate (server)
189 (nnml-possibly-change-directory nil server)
190 (nnml-generate-nov-databases server)
193 (deffoo nnml-request-article (id &optional group server buffer)
194 (nnml-possibly-change-directory group server)
195 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
196 (file-name-coding-system nnmail-pathname-coding-system)
197 path gpath group-num)
199 (when (and (setq group-num (nnml-find-group-number id))
201 (assq (cdr group-num)
202 (nnheader-article-to-file-alist
204 (nnmail-group-pathname
207 (setq path (concat gpath (int-to-string (cdr group-num)))))
208 (setq path (nnml-article-to-file id)))
211 (nnheader-report 'nnml "No such article: %s" id))
212 ((not (file-exists-p path))
213 (nnheader-report 'nnml "No such file: %s" path))
214 ((file-directory-p path)
215 (nnheader-report 'nnml "File is a directory: %s" path))
216 ((not (save-excursion (let ((nnmail-file-coding-system
217 nnml-file-coding-system))
218 (nnmail-find-file path))))
219 (nnheader-report 'nnml "Couldn't read file: %s" path))
221 (nnheader-report 'nnml "Article %s retrieved" id)
222 ;; We return the article number.
223 (cons (if group-num (car group-num) group)
224 (string-to-number (file-name-nondirectory path)))))))
226 (deffoo nnml-request-group (group &optional server dont-check)
227 (let ((file-name-coding-system nnmail-pathname-coding-system))
229 ((not (nnml-possibly-change-directory group server))
230 (nnheader-report 'nnml "Invalid group (no such directory)"))
231 ((not (file-exists-p nnml-current-directory))
232 (nnheader-report 'nnml "Directory %s does not exist"
233 nnml-current-directory))
234 ((not (file-directory-p nnml-current-directory))
235 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
237 (nnheader-report 'nnml "Group %s selected" group)
240 (nnheader-re-read-dir nnml-current-directory)
241 (nnmail-activate 'nnml)
242 (let ((active (nth 1 (assoc group nnml-group-alist))))
244 (nnheader-report 'nnml "No such group: %s" group)
245 (nnheader-report 'nnml "Selected group %s" group)
246 (nnheader-insert "211 %d %d %d %s\n"
247 (max (1+ (- (cdr active) (car active))) 0)
248 (car active) (cdr active) group)))))))
250 (deffoo nnml-request-scan (&optional group server)
251 (setq nnml-article-file-alist nil)
252 (nnml-possibly-change-directory group server)
253 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
255 (deffoo nnml-close-group (group &optional server)
256 (setq nnml-article-file-alist nil)
259 (deffoo nnml-request-create-group (group &optional server args)
260 (nnml-possibly-change-directory nil server)
261 (nnmail-activate 'nnml)
263 ((assoc group nnml-group-alist)
265 ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
266 (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
267 (nnheader-report 'nnml "%s is a file"
268 (nnmail-group-pathname group nnml-directory)))
271 (push (list group (setq active (cons 1 0)))
273 (nnml-possibly-create-directory group)
274 (nnml-possibly-change-directory group server)
275 (let ((articles (nnml-directory-articles nnml-current-directory)))
277 (setcar active (apply 'min articles))
278 (setcdr active (apply 'max articles))))
279 (nnmail-save-active nnml-group-alist nnml-active-file)
282 (deffoo nnml-request-list (&optional server)
284 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
285 (file-name-coding-system nnmail-pathname-coding-system))
286 (nnmail-find-file nnml-active-file))
287 (setq nnml-group-alist (nnmail-get-active))
290 (deffoo nnml-request-newgroups (date &optional server)
291 (nnml-request-list server))
293 (deffoo nnml-request-list-newsgroups (&optional server)
295 (nnmail-find-file nnml-newsgroups-file)))
297 (deffoo nnml-request-expire-articles (articles group &optional server force)
298 (nnml-possibly-change-directory group server)
299 (let ((active-articles
300 (nnml-directory-articles nnml-current-directory))
302 article rest mod-time number)
303 (nnmail-activate 'nnml)
305 (setq active-articles (sort active-articles '<))
306 ;; Articles not listed in active-articles are already gone,
307 ;; so don't try to expire them.
308 (setq articles (gnus-sorted-intersection articles active-articles))
310 (while (and articles is-old)
311 (if (and (setq article (nnml-article-to-file
312 (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 ;; Maybe directory is changed during nnmail-expiry-target-group.
327 (nnml-possibly-change-directory group server))
328 (nnheader-message 5 "Deleting article %s in %s"
331 (funcall nnmail-delete-file-function article)
334 (setq active-articles (delq number active-articles))
335 (nnml-nov-delete-article group number))
337 (let ((active (nth 1 (assoc group nnml-group-alist))))
339 (setcar active (or (and active-articles
340 (apply 'min active-articles))
342 (nnmail-save-active nnml-group-alist nnml-active-file))
344 (nconc rest articles)))
346 (deffoo nnml-request-move-article
347 (article group server accept-form &optional last move-is-internal)
348 (let ((buf (get-buffer-create " *nnml move*"))
350 (nnml-possibly-change-directory group server)
351 (nnml-update-file-alist)
353 (nnml-deletable-article-p group article)
354 (nnml-request-article article group server)
355 (let (nnml-current-directory
357 nnml-article-file-alist)
360 (insert-buffer-substring nntp-server-buffer)
361 (setq result (eval accept-form))
362 (kill-buffer (current-buffer))
365 (nnml-possibly-change-directory group server)
367 (funcall nnmail-delete-file-function
368 (nnml-article-to-file article))
370 (nnml-nov-delete-article group article)
373 (nnmail-save-active nnml-group-alist nnml-active-file))))
376 (deffoo nnml-request-accept-article (group &optional server last)
377 (nnml-possibly-change-directory group server)
378 (nnmail-check-syntax)
380 (when nnmail-cache-accepted-message-ids
381 (nnmail-cache-insert (nnmail-fetch-field "message-id")
383 (nnmail-fetch-field "subject")
384 (nnmail-fetch-field "from")))
387 (nnmail-activate 'nnml)
388 (setq result (car (nnml-save-mail
389 (list (cons group (nnml-active-number group))))))
391 (nnmail-save-active nnml-group-alist nnml-active-file)
392 (and last (nnml-save-nov))))
394 (nnmail-activate 'nnml)
395 (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
396 (yes-or-no-p "Moved to `junk' group; delete article? "))
398 (setq result (car (nnml-save-mail result))))
400 (nnmail-save-active nnml-group-alist nnml-active-file)
401 (when nnmail-cache-accepted-message-ids
402 (nnmail-cache-close))
406 (deffoo nnml-request-post (&optional server)
407 (nnmail-do-request-post 'nnml-request-accept-article server))
409 (deffoo nnml-request-replace-article (article group buffer)
410 (nnml-possibly-change-directory group)
413 (nnml-possibly-create-directory group)
414 (let ((chars (nnmail-insert-lines))
415 (art (concat (int-to-string article) "\t"))
419 (point-min) (point-max)
420 (or (nnml-article-to-file article)
421 (expand-file-name (int-to-string article)
422 nnml-current-directory))
423 nil (if (nnheader-be-verbose 5) nil 'nomesg))
425 (setq headers (nnml-parse-head chars article))
426 ;; Replace the NOV line in the NOV file.
428 (set-buffer (nnml-open-nov group))
429 (goto-char (point-min))
430 (if (or (looking-at art)
431 (search-forward (concat "\n" art) nil t))
432 ;; Delete the old NOV line.
434 ;; The line isn't here, so we have to find out where
435 ;; we should insert it. (This situation should never
436 ;; occur, but one likes to make sure...)
437 (while (and (looking-at "[0-9]+\t")
440 (match-beginning 0) (match-end 0)))
442 (zerop (forward-line 1)))))
444 (nnheader-insert-nov headers)
448 (deffoo nnml-request-delete-group (group &optional force server)
449 (nnml-possibly-change-directory group server)
451 ;; Delete all articles in GROUP.
454 nnml-current-directory t
455 (concat nnheader-numerical-short-files
456 "\\|" (regexp-quote nnml-nov-file-name) "$"
457 "\\|" (regexp-quote nnml-marks-file-name) "$"))))
458 (dolist (article articles)
459 (when (file-writable-p article)
460 (nnheader-message 5 "Deleting article %s in %s..." article group)
461 (funcall nnmail-delete-file-function article))))
462 ;; Try to delete the directory itself.
463 (ignore-errors (delete-directory nnml-current-directory)))
464 ;; Remove the group from all structures.
465 (setq nnml-group-alist
466 (delq (assoc group nnml-group-alist) nnml-group-alist)
467 nnml-current-group nil
468 nnml-current-directory nil)
469 ;; Save the active file.
470 (nnmail-save-active nnml-group-alist nnml-active-file)
473 (deffoo nnml-request-rename-group (group new-name &optional server)
474 (nnml-possibly-change-directory group server)
475 (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
476 (old-dir (nnmail-group-pathname group nnml-directory)))
478 (make-directory new-dir t)
480 ;; We move the articles file by file instead of renaming
481 ;; the directory -- there may be subgroups in this group.
482 ;; One might be more clever, I guess.
483 (dolist (file (nnheader-article-to-file-alist old-dir))
485 (concat old-dir (cdr file))
486 (concat new-dir (cdr file))))
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)
526 (if nnml-use-compressed-files
527 (cdr (assq article nnml-article-file-alist))
528 (number-to-string article)))
529 (expand-file-name file nnml-current-directory)
530 (when (not nnheader-directory-files-is-safe)
531 ;; Just to make sure nothing went wrong when reading over NFS --
534 (setq file (expand-file-name (number-to-string article)
535 nnml-current-directory)))
536 (nnml-update-file-alist t)
539 (defun nnml-deletable-article-p (group article)
540 "Say whether ARTICLE in GROUP can be deleted."
542 (when (setq path (nnml-article-to-file article))
543 (when (file-writable-p path)
544 (or (not nnmail-keep-last-article)
545 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
548 ;; Find an article number in the current group given the Message-ID.
549 (defun nnml-find-group-number (id)
551 (set-buffer (get-buffer-create " *nnml id*"))
552 (let ((alist nnml-group-alist)
554 ;; We want to look through all .overview files, but we want to
555 ;; start with the one in the current directory. It seems most
556 ;; likely that the article we are looking for is in that group.
557 (if (setq number (nnml-find-id nnml-current-group id))
558 (cons nnml-current-group number)
559 ;; It wasn't there, so we look through the other groups as well.
560 (while (and (not number)
562 (or (string= (caar alist) nnml-current-group)
563 (setq number (nnml-find-id (caar alist) id)))
565 (setq alist (cdr alist))))
567 (cons (caar alist) number))))))
569 (defun nnml-find-id (group id)
571 (let ((nov (expand-file-name nnml-nov-file-name
572 (nnmail-group-pathname group nnml-directory)))
574 (when (file-exists-p nov)
575 (nnheader-insert-file-contents nov)
576 (while (and (not found)
577 (search-forward id nil t)) ; We find the ID.
578 ;; And the id is in the fourth field.
579 (if (not (and (search-backward "\t" nil t 4)
580 (not (search-backward "\t" (point-at-bol) t))))
584 ;; We return the article number.
586 (ignore-errors (read (current-buffer))))))
589 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
590 (if (or gnus-nov-is-evil nnml-nov-is-evil)
592 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
593 (when (file-exists-p nov)
595 (set-buffer nntp-server-buffer)
597 (nnheader-insert-file-contents nov)
599 (not (numberp fetch-old)))
600 t ; Don't remove anything.
601 (nnheader-nov-delete-outside-range
602 (if fetch-old (max 1 (- (car articles) fetch-old))
604 (car (last articles)))
607 (defun nnml-possibly-change-directory (group &optional server)
609 (not (nnml-server-opened server)))
610 (nnml-open-server server))
613 (let ((pathname (nnmail-group-pathname group nnml-directory))
614 (file-name-coding-system nnmail-pathname-coding-system))
615 (when (not (equal pathname nnml-current-directory))
616 (setq nnml-current-directory pathname
617 nnml-current-group group
618 nnml-article-file-alist nil))
619 (file-exists-p nnml-current-directory))))
621 (defun nnml-possibly-create-directory (group)
622 (let ((dir (nnmail-group-pathname group nnml-directory)))
623 (unless (file-exists-p dir)
624 (make-directory (directory-file-name dir) t)
625 (nnheader-message 5 "Creating mail directory %s" dir))))
627 (defun nnml-save-mail (group-art)
628 "Called narrowed to an article."
629 (let (chars headers extension)
630 (setq chars (nnmail-insert-lines))
632 (and nnml-use-compressed-files
633 (> chars nnml-compressed-files-size-threshold)
635 (nnmail-insert-xref group-art)
636 (run-hooks 'nnmail-prepare-save-mail-hook)
637 (run-hooks 'nnml-prepare-save-mail-hook)
638 (goto-char (point-min))
639 (while (looking-at "From ")
640 (replace-match "X-From-Line: ")
642 ;; We save the article in all the groups it belongs in.
646 (nnml-possibly-create-directory (caar ga))
647 (let ((file (concat (nnmail-group-pathname
648 (caar ga) nnml-directory)
649 (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 (nnml-current-group-article-to-file-alist)
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) (point-at-eol)))
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 (re-search-forward "\n\r?\n" nil t)
719 (let ((headers (nnheader-parse-naked-head)))
720 (mail-header-set-chars headers chars)
721 (mail-header-set-number headers number)
724 (defun nnml-get-nov-buffer (group)
725 (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
728 (set (make-local-variable 'nnml-nov-buffer-file-name)
731 (nnmail-group-pathname group nnml-directory)))
733 (when (file-exists-p nnml-nov-buffer-file-name)
734 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
737 (defun nnml-open-nov (group)
738 (or (cdr (assoc group nnml-nov-buffer-alist))
739 (let ((buffer (nnml-get-nov-buffer group)))
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 (point-min) (point-max)
750 nnml-nov-buffer-file-name nil 'nomesg))
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 (dolist (dir (directory-files dir t nil t))
779 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
780 (file-directory-p dir))
781 (nnml-generate-nov-databases-1 dir seen)))
782 ;; Do this directory.
783 (let ((files (sort (nnheader-article-to-file-alist dir)
784 'car-less-than-car)))
786 (let* ((group (nnheader-file-to-group
787 (directory-file-name dir) nnml-directory))
788 (info (cadr (assoc group nnml-group-alist))))
790 (setcar info (1+ (cdr info)))))
791 (funcall nnml-generate-active-function dir)
792 ;; Generate the nov file.
793 (nnml-generate-nov-file dir files)
795 (nnmail-save-active nnml-group-alist nnml-active-file))))))
797 (eval-when-compile (defvar files))
798 (defun nnml-generate-active-info (dir)
799 ;; Update the active info for this group.
800 (let* ((group (nnheader-file-to-group
801 (directory-file-name dir) nnml-directory))
802 (entry (assoc group nnml-group-alist))
803 (last (or (caadr entry) 0)))
804 (setq nnml-group-alist (delq entry nnml-group-alist))
806 (cons (or (caar files) (1+ last))
808 (or (caar (last files))
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 (re-search-forward "\n\r?\n" nil t)
834 (setq chars (- (point-max) (point)))
835 (max (point-min) (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 (point-min) (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 nnml-use-compressed-files
868 (when (or (not nnml-article-file-alist)
870 (setq nnml-article-file-alist
871 (nnml-current-group-article-to-file-alist)))))
873 (defun nnml-directory-articles (dir)
874 "Return a list of all article files in a directory.
875 Use the nov database for that directory if available."
876 (if (or gnus-nov-is-evil nnml-nov-is-evil
878 (expand-file-name nnml-nov-file-name dir))))
879 (nnheader-directory-articles dir)
880 ;; build list from .overview if available
881 ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
882 ;; defvoo'd, and we might get called when it hasn't been swapped in.
886 (buffer (nnml-get-nov-buffer nnml-current-group)))
888 (goto-char (point-min))
890 (setq art (read (current-buffer)))
895 (defun nnml-current-group-article-to-file-alist ()
896 "Return an alist of article/file pairs in the current group.
897 Use the nov database for the current group if available."
898 (if (or nnml-use-compressed-files
902 (expand-file-name nnml-nov-file-name
903 nnml-current-directory))))
904 (nnheader-article-to-file-alist nnml-current-directory)
905 ;; build list from .overview if available
908 (buffer (nnml-get-nov-buffer nnml-current-group))
911 (goto-char (point-min))
913 (setq art (read (current-buffer)))
914 ;; assume file name is unadorned (ie. not compressed etc)
915 (push (cons art (int-to-string art)) alist)
919 (deffoo nnml-request-set-mark (group actions &optional server)
920 (nnml-possibly-change-directory group server)
921 (unless nnml-marks-is-evil
922 (nnml-open-marks group server)
923 (dolist (action actions)
924 (let ((range (nth 0 action))
925 (what (nth 1 action))
926 (marks (nth 2 action)))
927 (assert (or (eq what 'add) (eq what 'del)) nil
928 "Unknown request-set-mark action: %s" what)
930 (setq nnml-marks (gnus-update-alist-soft
932 (funcall (if (eq what 'add) 'gnus-range-add
933 'gnus-remove-from-range)
934 (cdr (assoc mark nnml-marks)) range)
936 (nnml-save-marks group server))
939 (deffoo nnml-request-update-info (group info &optional server)
940 (nnml-possibly-change-directory group server)
941 (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group))
942 (nnheader-message 8 "Updating marks for %s..." group)
943 (nnml-open-marks group server)
944 ;; Update info using `nnml-marks'.
946 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
949 (gnus-update-alist-soft
951 (cdr (assq (cdr pred) nnml-marks))
952 (gnus-info-marks info))
954 gnus-article-mark-lists)
955 (let ((seen (cdr (assq 'read nnml-marks))))
956 (gnus-info-set-read info
957 (if (and (integerp (car seen))
959 (list (cons (car seen) (car seen)))
961 (nnheader-message 8 "Updating marks for %s...done" group))
964 (defun nnml-marks-changed-p (group)
965 (let ((file (expand-file-name nnml-marks-file-name
966 (nnmail-group-pathname group nnml-directory))))
967 (if (null (gnus-gethash file nnml-marks-modtime))
968 t ;; never looked at marks file, assume it has changed
969 (not (equal (gnus-gethash file nnml-marks-modtime)
970 (nth 5 (file-attributes file)))))))
972 (defun nnml-save-marks (group server)
973 (let ((file-name-coding-system nnmail-pathname-coding-system)
974 (file (expand-file-name nnml-marks-file-name
975 (nnmail-group-pathname group nnml-directory))))
978 (nnml-possibly-create-directory group)
981 (gnus-prin1 nnml-marks)
984 (nth 5 (file-attributes file))
986 (error (or (gnus-yes-or-no-p
987 (format "Could not write to %s (%s). Continue? " file err))
988 (error "Cannot write to %s (%s)" file err))))))
990 (defun nnml-open-marks (group server)
991 (let ((file (expand-file-name
993 (nnmail-group-pathname group nnml-directory))))
994 (if (file-exists-p file)
997 (gnus-sethash file (nth 5 (file-attributes file))
999 (nnheader-insert-file-contents file)
1000 (setq nnml-marks (read (current-buffer)))
1001 (dolist (el gnus-article-unpropagated-mark-lists)
1002 (setq nnml-marks (gnus-remassoc el nnml-marks))))
1003 (error (or (gnus-yes-or-no-p
1004 (format "Error reading nnml marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1005 (error "Cannot read nnml marks file %s (%s)" file err))))
1006 ;; User didn't have a .marks file. Probably first time
1007 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1008 (let ((info (gnus-get-info
1009 (gnus-group-prefixed-name
1011 (gnus-server-to-method (format "nnml:%s" server))))))
1012 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1013 (setq nnml-marks (gnus-info-marks info))
1014 (push (cons 'read (gnus-info-read info)) nnml-marks)
1015 (dolist (el gnus-article-unpropagated-mark-lists)
1016 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1017 (nnml-save-marks group server)
1018 (nnheader-message 7 "Bootstrapping marks for %s...done" group)))))
1022 ;;; Group and server compaction
1025 (defun nnml-request-compact-group (group &optional server save)
1026 (nnml-possibly-change-directory group server)
1027 (unless nnml-article-file-alist
1028 (setq nnml-article-file-alist
1029 (sort (nnml-current-group-article-to-file-alist)
1030 'car-less-than-car)))
1031 (if (not nnml-article-file-alist)
1032 ;; The group is empty: do nothing but return t
1034 ;; The group is not empty:
1035 (let* ((group-full-name
1036 (gnus-group-prefixed-name
1038 (gnus-server-to-method (format "nnml:%s" server))))
1039 (info (gnus-get-info group-full-name))
1042 (let ((articles nnml-article-file-alist)
1044 (while (setq article (pop articles))
1045 (let ((old-number (car article)))
1046 (when (> old-number new-number)
1047 ;; There is a gap here:
1049 ;; #### NOTE: `nnml-article-to-file' calls
1050 ;; #### `nnml-update-file-alist' (which in turn calls
1051 ;; #### `nnml-current-group-article-to-file-alist', which might
1052 ;; #### use the NOV database). This might turn out to be
1053 ;; #### inefficient. In that case, we will do the work manually.
1054 ;; 1/ Move the article to a new file:
1055 (let* ((oldfile (nnml-article-to-file old-number))
1057 (gnus-replace-in-string
1058 oldfile (concat "\\("
1059 (int-to-string old-number)
1060 "\\)\\(\\(\\.gz\\)?\\)$")
1061 (concat (int-to-string new-number) "\\2"))))
1062 (with-current-buffer nntp-server-buffer
1063 (nnmail-find-file oldfile)
1064 (nnmail-write-region (point-min) (point-max) newfile))
1065 (funcall nnmail-delete-file-function oldfile))
1066 ;; 2/ Update all marks for this article:
1067 ;; #### NOTE: it is possible that the new article number already
1068 ;; #### belongs to a range, whereas the corresponding article
1069 ;; #### doesn't exist (for example, if you delete an article).
1070 ;; #### For that reason, it is important to update the ranges
1071 ;; #### (meaning remove inexistant articles) before doing
1072 ;; anything on them.
1073 ;; 2 a/ read articles:
1074 (let ((read (gnus-info-read info)))
1075 (setq read (gnus-remove-from-range read (list new-number)))
1076 (when (gnus-member-of-range old-number read)
1077 (setq read (gnus-remove-from-range read (list old-number)))
1078 (setq read (gnus-add-to-range read (list new-number))))
1079 (gnus-info-set-read info read))
1080 ;; 2 b/ marked articles:
1081 (let ((oldmarks (gnus-info-marks info))
1083 (while (setq mark (pop oldmarks))
1084 (setcdr mark (gnus-remove-from-range (cdr mark)
1086 (when (gnus-member-of-range old-number (cdr mark))
1087 (setcdr mark (gnus-remove-from-range (cdr mark)
1089 (setcdr mark (gnus-add-to-range (cdr mark)
1090 (list new-number))))
1091 (push mark newmarks))
1092 (gnus-info-set-marks info newmarks))
1093 ;; 3/ Update the NOV entry for this article:
1094 (unless nnml-nov-is-evil
1096 (set-buffer (nnml-open-nov group))
1097 (when (nnheader-find-nov-line old-number)
1098 (looking-at (int-to-string old-number))
1099 (replace-match (int-to-string new-number) nil t)))))
1100 (setq new-number (1+ new-number)))))
1102 ;; No compaction had to be done:
1104 ;; Some articles have actually been renamed:
1105 ;; 1/ Rebuild active information:
1106 (let ((entry (assoc group nnml-group-alist))
1107 (active (cons 1 (1- new-number))))
1108 (setq nnml-group-alist (delq entry nnml-group-alist))
1109 (push (list group active) nnml-group-alist)
1110 ;; Update the active hashtable to let the *Group* buffer display
1111 ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1112 ;; gnus-newwrc-alist are out of date, since all we did is to modify
1113 ;; the info of the group internally.
1114 (gnus-set-active group-full-name active))
1116 ;; #### NOTE: normally, we should save the overview (NOV) file
1117 ;; #### here, just like we save the marks file. However, there is no
1118 ;; #### such function as nnml-save-nov for a single group. Only for
1119 ;; #### all groups. Gnus inconsistency is getting worse every day...
1120 ;; 2/ Rebuild marks file:
1121 (unless nnml-marks-is-evil
1122 ;; #### NOTE: this constant use of global variables everywhere is
1123 ;; #### truly disgusting. Gnus really needs a *major* cleanup.
1124 (setq nnml-marks (gnus-info-marks info))
1125 (push (cons 'read (gnus-info-read info)) nnml-marks)
1126 (dolist (el gnus-article-unpropagated-mark-lists)
1127 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1128 (nnml-save-marks group server))
1129 ;; 3/ Save everything if this was not part of a bigger operation:
1131 ;; Nothing to save (yet):
1133 ;; Something to save:
1134 ;; a/ Save the NOV databases:
1135 ;; #### NOTE: this should be done directory per directory in 1bis
1136 ;; #### above. See comment there.
1138 ;; b/ Save the active file:
1139 (nnmail-save-active nnml-group-alist nnml-active-file)
1142 (defun nnml-request-compact (&optional server)
1143 "Request compaction of all SERVER nnml groups."
1144 (interactive (list (or (nnoo-current-server 'nnml) "")))
1145 (nnmail-activate 'nnml)
1146 (unless (nnml-server-opened server)
1147 (nnml-open-server server))
1148 (setq nnml-directory (expand-file-name nnml-directory))
1149 (let* ((groups (gnus-groups-from-server
1150 (gnus-server-to-method (format "nnml:%s" server))))
1151 (first (pop groups))
1154 (while (setq group (pop groups))
1155 (nnml-request-compact-group (gnus-group-real-name group) server))
1156 (nnml-request-compact-group (gnus-group-real-name first) server t))))
1161 ;;; nnml.el ends here