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
41 (eval-when-compile (require 'cl))
44 (autoload 'gnus-article-unpropagatable-p "gnus-sum"))
48 (defvoo nnml-directory message-directory
49 "Spool directory for the nnml mail backend.")
51 (defvoo nnml-active-file
52 (expand-file-name "active" nnml-directory)
55 (defvoo nnml-newsgroups-file
56 (expand-file-name "newsgroups" nnml-directory)
57 "Mail newsgroups description file.")
59 (defvoo nnml-get-new-mail t
60 "If non-nil, nnml will check the incoming mail file and split the mail.")
62 (defvoo nnml-nov-is-evil nil
63 "If non-nil, Gnus will never generate and use nov databases for mail spools.
64 Using nov databases will speed up header fetching considerably.
65 This variable shouldn't be flipped much. If you have, for some reason,
66 set this to t, and want to set it to nil again, you should always run
67 the `nnml-generate-nov-databases' command. The function will go
68 through all nnml directories and generate nov databases for them
69 all. This may very well take some time.")
71 (defvoo nnml-marks-is-evil nil
72 "If non-nil, Gnus will never generate and use marks file for mail spools.
73 Using marks files makes it possible to backup and restore mail groups
74 separately from `.newsrc.eld'. If you have, for some reason, set this
75 to t, and want to set it to nil again, you should always remove the
76 corresponding marks file (usually named `.marks' in the nnml group
77 directory, but see `nnml-marks-file-name') for the group. Then the
78 marks file will be regenerated properly by Gnus.")
80 (defvoo nnml-prepare-save-mail-hook nil
81 "Hook run narrowed to an article before saving.")
83 (defvoo nnml-inhibit-expiry nil
84 "If non-nil, inhibit expiry.")
86 (defvoo nnml-use-compressed-files nil
87 "If non-nil, allow using compressed message files.")
89 (defvoo nnml-compressed-files-size-threshold 1000
90 "Default size threshold for compressed message files.
91 Message files with bodies larger than that many characters will
92 be automatically compressed if `nnml-use-compressed-files' is
97 (defconst nnml-version "nnml 1.0"
100 (defvoo nnml-nov-file-name ".overview")
101 (defvoo nnml-marks-file-name ".marks")
103 (defvoo nnml-current-directory nil)
104 (defvoo nnml-current-group nil)
105 (defvoo nnml-status-string "")
106 (defvoo nnml-nov-buffer-alist nil)
107 (defvoo nnml-group-alist nil)
108 (defvoo nnml-active-timestamp nil)
109 (defvoo nnml-article-file-alist nil)
111 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
113 (defvar nnml-nov-buffer-file-name nil)
115 (defvoo nnml-file-coding-system nnmail-file-coding-system)
117 (defvoo nnml-marks nil)
119 (defvar nnml-marks-modtime (gnus-make-hashtable))
122 ;;; Interface functions.
124 (nnoo-define-basics nnml)
126 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
127 (when (nnml-possibly-change-directory group server)
129 (set-buffer nntp-server-buffer)
132 (number (length sequence))
134 (file-name-coding-system nnmail-pathname-coding-system)
136 (if (stringp (car sequence))
138 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
141 (setq article (car sequence))
142 (setq file (nnml-article-to-file article))
145 (not (file-directory-p file)))
146 (insert (format "221 %d Article retrieved.\n" article))
148 (nnheader-insert-head file)
150 (if (re-search-forward "\n\r?\n" nil t)
152 (goto-char (point-max))
155 (delete-region (point) (point-max)))
156 (setq sequence (cdr sequence))
157 (setq count (1+ count))
158 (and (numberp nnmail-large-newsgroup)
159 (> number nnmail-large-newsgroup)
161 (nnheader-message 6 "nnml: Receiving headers... %d%%"
162 (/ (* count 100) number))))
164 (and (numberp nnmail-large-newsgroup)
165 (> number nnmail-large-newsgroup)
166 (nnheader-message 6 "nnml: Receiving headers...done"))
168 (nnheader-fold-continuation-lines)
171 (deffoo nnml-open-server (server &optional defs)
172 (nnoo-change-server 'nnml server defs)
173 (when (not (file-exists-p nnml-directory))
174 (ignore-errors (make-directory nnml-directory t)))
176 ((not (file-exists-p nnml-directory))
178 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
179 ((not (file-directory-p (file-truename nnml-directory)))
181 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
183 (nnheader-report 'nnml "Opened server %s using directory %s"
184 server nnml-directory)
187 (deffoo nnml-request-regenerate (server)
188 (nnml-possibly-change-directory nil server)
189 (nnml-generate-nov-databases server)
192 (deffoo nnml-request-article (id &optional group server buffer)
193 (nnml-possibly-change-directory group server)
194 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
195 (file-name-coding-system nnmail-pathname-coding-system)
196 path gpath group-num)
198 (when (and (setq group-num (nnml-find-group-number id))
200 (assq (cdr group-num)
201 (nnheader-article-to-file-alist
203 (nnmail-group-pathname
206 (setq path (concat gpath (int-to-string (cdr group-num)))))
207 (setq path (nnml-article-to-file id)))
210 (nnheader-report 'nnml "No such article: %s" id))
211 ((not (file-exists-p path))
212 (nnheader-report 'nnml "No such file: %s" path))
213 ((file-directory-p path)
214 (nnheader-report 'nnml "File is a directory: %s" path))
215 ((not (save-excursion (let ((nnmail-file-coding-system
216 nnml-file-coding-system))
217 (nnmail-find-file path))))
218 (nnheader-report 'nnml "Couldn't read file: %s" path))
220 (nnheader-report 'nnml "Article %s retrieved" id)
221 ;; We return the article number.
222 (cons (if group-num (car group-num) group)
223 (string-to-number (file-name-nondirectory path)))))))
225 (deffoo nnml-request-group (group &optional server dont-check)
226 (let ((file-name-coding-system nnmail-pathname-coding-system))
228 ((not (nnml-possibly-change-directory group server))
229 (nnheader-report 'nnml "Invalid group (no such directory)"))
230 ((not (file-exists-p nnml-current-directory))
231 (nnheader-report 'nnml "Directory %s does not exist"
232 nnml-current-directory))
233 ((not (file-directory-p nnml-current-directory))
234 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
236 (nnheader-report 'nnml "Group %s selected" group)
239 (nnheader-re-read-dir nnml-current-directory)
240 (nnmail-activate 'nnml)
241 (let ((active (nth 1 (assoc group nnml-group-alist))))
243 (nnheader-report 'nnml "No such group: %s" group)
244 (nnheader-report 'nnml "Selected group %s" group)
245 (nnheader-insert "211 %d %d %d %s\n"
246 (max (1+ (- (cdr active) (car active))) 0)
247 (car active) (cdr active) group)))))))
249 (deffoo nnml-request-scan (&optional group server)
250 (setq nnml-article-file-alist nil)
251 (nnml-possibly-change-directory group server)
252 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
254 (deffoo nnml-close-group (group &optional server)
255 (setq nnml-article-file-alist nil)
258 (deffoo nnml-request-create-group (group &optional server args)
259 (nnml-possibly-change-directory nil server)
260 (nnmail-activate 'nnml)
262 ((assoc group nnml-group-alist)
264 ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
265 (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
266 (nnheader-report 'nnml "%s is a file"
267 (nnmail-group-pathname group nnml-directory)))
270 (push (list group (setq active (cons 1 0)))
272 (nnml-possibly-create-directory group)
273 (nnml-possibly-change-directory group server)
274 (let ((articles (nnml-directory-articles nnml-current-directory)))
276 (setcar active (apply 'min articles))
277 (setcdr active (apply 'max articles))))
278 (nnmail-save-active nnml-group-alist nnml-active-file)
281 (deffoo nnml-request-list (&optional server)
283 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
284 (file-name-coding-system nnmail-pathname-coding-system))
285 (nnmail-find-file nnml-active-file))
286 (setq nnml-group-alist (nnmail-get-active))
289 (deffoo nnml-request-newgroups (date &optional server)
290 (nnml-request-list server))
292 (deffoo nnml-request-list-newsgroups (&optional server)
294 (nnmail-find-file nnml-newsgroups-file)))
296 (deffoo nnml-request-expire-articles (articles group &optional server force)
297 (nnml-possibly-change-directory group server)
298 (let ((active-articles
299 (nnml-directory-articles nnml-current-directory))
301 article rest mod-time number)
302 (nnmail-activate 'nnml)
304 (setq active-articles (sort active-articles '<))
305 ;; Articles not listed in active-articles are already gone,
306 ;; so don't try to expire them.
307 (setq articles (gnus-sorted-intersection articles active-articles))
309 (while (and articles is-old)
310 (if (and (setq article (nnml-article-to-file
311 (setq number (pop articles))))
312 (setq mod-time (nth 5 (file-attributes article)))
313 (nnml-deletable-article-p group number)
314 (setq is-old (nnmail-expired-article-p group mod-time force
315 nnml-inhibit-expiry)))
317 ;; Allow a special target group.
318 (unless (eq nnmail-expiry-target 'delete)
320 (nnml-request-article number group server (current-buffer))
321 (let (nnml-current-directory
323 nnml-article-file-alist)
324 (nnmail-expiry-target-group nnmail-expiry-target group)))
325 ;; Maybe directory is changed during nnmail-expiry-target-group.
326 (nnml-possibly-change-directory group server))
327 (nnheader-message 5 "Deleting article %s in %s"
330 (funcall nnmail-delete-file-function article)
333 (setq active-articles (delq number active-articles))
334 (nnml-nov-delete-article group number))
336 (let ((active (nth 1 (assoc group nnml-group-alist))))
338 (setcar active (or (and active-articles
339 (apply 'min active-articles))
341 (nnmail-save-active nnml-group-alist nnml-active-file))
343 (nconc rest articles)))
345 (deffoo nnml-request-move-article
346 (article group server accept-form &optional last move-is-internal)
347 (let ((buf (get-buffer-create " *nnml move*"))
349 (nnml-possibly-change-directory group server)
350 (nnml-update-file-alist)
352 (nnml-deletable-article-p group article)
353 (nnml-request-article article group server)
354 (let (nnml-current-directory
356 nnml-article-file-alist)
359 (insert-buffer-substring nntp-server-buffer)
360 (setq result (eval accept-form))
361 (kill-buffer (current-buffer))
364 (nnml-possibly-change-directory group server)
366 (funcall nnmail-delete-file-function
367 (nnml-article-to-file article))
369 (nnml-nov-delete-article group article)
372 (nnmail-save-active nnml-group-alist nnml-active-file))))
375 (deffoo nnml-request-accept-article (group &optional server last)
376 (nnml-possibly-change-directory group server)
377 (nnmail-check-syntax)
379 (when nnmail-cache-accepted-message-ids
380 (nnmail-cache-insert (nnmail-fetch-field "message-id")
382 (nnmail-fetch-field "subject")
383 (nnmail-fetch-field "from")))
386 (nnmail-activate 'nnml)
387 (setq result (car (nnml-save-mail
388 (list (cons group (nnml-active-number group))))))
390 (nnmail-save-active nnml-group-alist nnml-active-file)
391 (and last (nnml-save-nov))))
393 (nnmail-activate 'nnml)
394 (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
395 (yes-or-no-p "Moved to `junk' group; delete article? "))
397 (setq result (car (nnml-save-mail result))))
399 (nnmail-save-active nnml-group-alist nnml-active-file)
400 (when nnmail-cache-accepted-message-ids
401 (nnmail-cache-close))
405 (deffoo nnml-request-post (&optional server)
406 (nnmail-do-request-post 'nnml-request-accept-article server))
408 (deffoo nnml-request-replace-article (article group buffer)
409 (nnml-possibly-change-directory group)
412 (nnml-possibly-create-directory group)
413 (let ((chars (nnmail-insert-lines))
414 (art (concat (int-to-string article) "\t"))
418 (point-min) (point-max)
419 (or (nnml-article-to-file article)
420 (expand-file-name (int-to-string article)
421 nnml-current-directory))
422 nil (if (nnheader-be-verbose 5) nil 'nomesg))
424 (setq headers (nnml-parse-head chars article))
425 ;; Replace the NOV line in the NOV file.
427 (set-buffer (nnml-open-nov group))
428 (goto-char (point-min))
429 (if (or (looking-at art)
430 (search-forward (concat "\n" art) nil t))
431 ;; Delete the old NOV line.
433 ;; The line isn't here, so we have to find out where
434 ;; we should insert it. (This situation should never
435 ;; occur, but one likes to make sure...)
436 (while (and (looking-at "[0-9]+\t")
439 (match-beginning 0) (match-end 0)))
441 (zerop (forward-line 1)))))
443 (nnheader-insert-nov headers)
447 (deffoo nnml-request-delete-group (group &optional force server)
448 (nnml-possibly-change-directory group server)
450 ;; Delete all articles in GROUP.
453 nnml-current-directory t
454 (concat nnheader-numerical-short-files
455 "\\|" (regexp-quote nnml-nov-file-name) "$"
456 "\\|" (regexp-quote nnml-marks-file-name) "$"))))
457 (dolist (article articles)
458 (when (file-writable-p article)
459 (nnheader-message 5 "Deleting article %s in %s..." article group)
460 (funcall nnmail-delete-file-function article))))
461 ;; Try to delete the directory itself.
462 (ignore-errors (delete-directory nnml-current-directory)))
463 ;; Remove the group from all structures.
464 (setq nnml-group-alist
465 (delq (assoc group nnml-group-alist) nnml-group-alist)
466 nnml-current-group nil
467 nnml-current-directory nil)
468 ;; Save the active file.
469 (nnmail-save-active nnml-group-alist nnml-active-file)
472 (deffoo nnml-request-rename-group (group new-name &optional server)
473 (nnml-possibly-change-directory group server)
474 (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
475 (old-dir (nnmail-group-pathname group nnml-directory)))
477 (make-directory new-dir t)
479 ;; We move the articles file by file instead of renaming
480 ;; the directory -- there may be subgroups in this group.
481 ;; One might be more clever, I guess.
482 (dolist (file (nnheader-article-to-file-alist old-dir))
484 (concat old-dir (cdr file))
485 (concat new-dir (cdr file))))
486 ;; Move .overview file.
487 (let ((overview (concat old-dir nnml-nov-file-name)))
488 (when (file-exists-p overview)
489 (rename-file overview (concat new-dir nnml-nov-file-name))))
491 (let ((marks (concat old-dir nnml-marks-file-name)))
492 (when (file-exists-p marks)
493 (rename-file marks (concat new-dir nnml-marks-file-name))))
494 (when (<= (length (directory-files old-dir)) 2)
495 (ignore-errors (delete-directory old-dir)))
496 ;; That went ok, so we change the internal structures.
497 (let ((entry (assoc group nnml-group-alist)))
499 (setcar entry new-name))
500 (setq nnml-current-directory nil
501 nnml-current-group nil)
502 ;; Save the new group alist.
503 (nnmail-save-active nnml-group-alist nnml-active-file)
506 (deffoo nnml-set-status (article name value &optional group server)
507 (nnml-possibly-change-directory group server)
508 (let ((file (nnml-article-to-file article)))
510 ((not (file-exists-p file))
511 (nnheader-report 'nnml "File %s does not exist" file))
514 (nnheader-insert-file-contents file)
515 (nnmail-replace-status name value))
519 ;;; Internal functions.
521 (defun nnml-article-to-file (article)
522 (nnml-update-file-alist)
525 (if nnml-use-compressed-files
526 (cdr (assq article nnml-article-file-alist))
527 (number-to-string article)))
528 (expand-file-name file nnml-current-directory)
529 (when (not nnheader-directory-files-is-safe)
530 ;; Just to make sure nothing went wrong when reading over NFS --
533 (setq file (expand-file-name (number-to-string article)
534 nnml-current-directory)))
535 (nnml-update-file-alist t)
538 (defun nnml-deletable-article-p (group article)
539 "Say whether ARTICLE in GROUP can be deleted."
541 (when (setq path (nnml-article-to-file article))
542 (when (file-writable-p path)
543 (or (not nnmail-keep-last-article)
544 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
547 ;; Find an article number in the current group given the Message-ID.
548 (defun nnml-find-group-number (id)
550 (set-buffer (get-buffer-create " *nnml id*"))
551 (let ((alist nnml-group-alist)
553 ;; We want to look through all .overview files, but we want to
554 ;; start with the one in the current directory. It seems most
555 ;; likely that the article we are looking for is in that group.
556 (if (setq number (nnml-find-id nnml-current-group id))
557 (cons nnml-current-group number)
558 ;; It wasn't there, so we look through the other groups as well.
559 (while (and (not number)
561 (or (string= (caar alist) nnml-current-group)
562 (setq number (nnml-find-id (caar alist) id)))
564 (setq alist (cdr alist))))
566 (cons (caar alist) number))))))
568 (defun nnml-find-id (group id)
570 (let ((nov (expand-file-name nnml-nov-file-name
571 (nnmail-group-pathname group nnml-directory)))
573 (when (file-exists-p nov)
574 (nnheader-insert-file-contents nov)
575 (while (and (not found)
576 (search-forward id nil t)) ; We find the ID.
577 ;; And the id is in the fourth field.
578 (if (not (and (search-backward "\t" nil t 4)
579 (not (search-backward "\t" (point-at-bol) t))))
583 ;; We return the article number.
585 (ignore-errors (read (current-buffer))))))
588 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
589 (if (or gnus-nov-is-evil nnml-nov-is-evil)
591 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
592 (when (file-exists-p nov)
594 (set-buffer nntp-server-buffer)
596 (nnheader-insert-file-contents nov)
598 (not (numberp fetch-old)))
599 t ; Don't remove anything.
600 (nnheader-nov-delete-outside-range
601 (if fetch-old (max 1 (- (car articles) fetch-old))
603 (car (last articles)))
606 (defun nnml-possibly-change-directory (group &optional server)
608 (not (nnml-server-opened server)))
609 (nnml-open-server server))
612 (let ((pathname (nnmail-group-pathname group nnml-directory))
613 (file-name-coding-system nnmail-pathname-coding-system))
614 (when (not (equal pathname nnml-current-directory))
615 (setq nnml-current-directory pathname
616 nnml-current-group group
617 nnml-article-file-alist nil))
618 (file-exists-p nnml-current-directory))))
620 (defun nnml-possibly-create-directory (group)
621 (let ((dir (nnmail-group-pathname group nnml-directory)))
622 (unless (file-exists-p dir)
623 (make-directory (directory-file-name dir) t)
624 (nnheader-message 5 "Creating mail directory %s" dir))))
626 (defun nnml-save-mail (group-art)
627 "Called narrowed to an article."
628 (let (chars headers extension)
629 (setq chars (nnmail-insert-lines))
631 (and nnml-use-compressed-files
632 (> chars nnml-compressed-files-size-threshold)
634 (nnmail-insert-xref group-art)
635 (run-hooks 'nnmail-prepare-save-mail-hook)
636 (run-hooks 'nnml-prepare-save-mail-hook)
637 (goto-char (point-min))
638 (while (looking-at "From ")
639 (replace-match "X-From-Line: ")
641 ;; We save the article in all the groups it belongs in.
645 (nnml-possibly-create-directory (caar ga))
646 (let ((file (concat (nnmail-group-pathname
647 (caar ga) nnml-directory)
648 (int-to-string (cdar ga))
651 ;; It was already saved, so we just make a hard link.
652 (funcall nnmail-crosspost-link-function first file t)
654 (nnmail-write-region (point-min) (point-max) file nil
655 (if (nnheader-be-verbose 5) nil 'nomesg))
658 ;; Generate a nov line for this article. We generate the nov
659 ;; line after saving, because nov generation destroys the
661 (setq headers (nnml-parse-head chars))
662 ;; Output the nov line to all nov databases that should have it.
663 (let ((ga group-art))
665 (nnml-add-nov (caar ga) (cdar ga) headers)
669 (defun nnml-active-number (group)
670 "Compute the next article number in GROUP."
671 (let ((active (cadr (assoc group nnml-group-alist))))
672 ;; The group wasn't known to nnml, so we just create an active
675 ;; Perhaps the active file was corrupt? See whether
676 ;; there are any articles in this group.
677 (nnml-possibly-create-directory group)
678 (nnml-possibly-change-directory group)
679 (unless nnml-article-file-alist
680 (setq nnml-article-file-alist
682 (nnml-current-group-article-to-file-alist)
683 'car-less-than-car)))
685 (if nnml-article-file-alist
686 (cons (caar nnml-article-file-alist)
687 (caar (last nnml-article-file-alist)))
689 (push (list group active) nnml-group-alist))
690 (setcdr active (1+ (cdr active)))
691 (while (file-exists-p
692 (expand-file-name (int-to-string (cdr active))
693 (nnmail-group-pathname group nnml-directory)))
694 (setcdr active (1+ (cdr active))))
697 (defun nnml-add-nov (group article headers)
698 "Add a nov line for the GROUP base."
700 (set-buffer (nnml-open-nov group))
701 (goto-char (point-max))
702 (mail-header-set-number headers article)
703 (nnheader-insert-nov headers)))
705 (defsubst nnml-header-value ()
706 (buffer-substring (match-end 0) (point-at-eol)))
708 (defun nnml-parse-head (chars &optional number)
709 "Parse the head of the current buffer."
712 (unless (zerop (buffer-size))
714 (goto-char (point-min))
715 (if (re-search-forward "\n\r?\n" nil t)
718 (let ((headers (nnheader-parse-naked-head)))
719 (mail-header-set-chars headers chars)
720 (mail-header-set-number headers number)
723 (defun nnml-get-nov-buffer (group)
724 (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
727 (set (make-local-variable 'nnml-nov-buffer-file-name)
730 (nnmail-group-pathname group nnml-directory)))
732 (when (file-exists-p nnml-nov-buffer-file-name)
733 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
736 (defun nnml-open-nov (group)
737 (or (cdr (assoc group nnml-nov-buffer-alist))
738 (let ((buffer (nnml-get-nov-buffer group)))
739 (push (cons group buffer) nnml-nov-buffer-alist)
742 (defun nnml-save-nov ()
744 (while nnml-nov-buffer-alist
745 (when (buffer-name (cdar nnml-nov-buffer-alist))
746 (set-buffer (cdar nnml-nov-buffer-alist))
747 (when (buffer-modified-p)
748 (nnmail-write-region (point-min) (point-max)
749 nnml-nov-buffer-file-name nil 'nomesg))
750 (set-buffer-modified-p nil)
751 (kill-buffer (current-buffer)))
752 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
755 (defun nnml-generate-nov-databases (&optional server)
756 "Generate NOV databases in all nnml directories."
757 (interactive (list (or (nnoo-current-server 'nnml) "")))
758 ;; Read the active file to make sure we don't re-use articles
759 ;; numbers in empty groups.
760 (nnmail-activate 'nnml)
761 (unless (nnml-server-opened server)
762 (nnml-open-server server))
763 (setq nnml-directory (expand-file-name nnml-directory))
764 ;; Recurse down the directories.
765 (nnml-generate-nov-databases-1 nnml-directory nil t)
766 ;; Save the active file.
767 (nnmail-save-active nnml-group-alist nnml-active-file))
769 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
770 "Regenerate the NOV database in DIR."
771 (interactive "DRegenerate NOV in: ")
772 (setq dir (file-name-as-directory dir))
773 ;; Only scan this sub-tree if we haven't been here yet.
774 (unless (member (file-truename dir) seen)
775 (push (file-truename dir) seen)
776 ;; We descend recursively
777 (dolist (dir (directory-files dir t nil t))
778 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
779 (file-directory-p dir))
780 (nnml-generate-nov-databases-1 dir seen)))
781 ;; Do this directory.
782 (let ((files (sort (nnheader-article-to-file-alist dir)
783 'car-less-than-car)))
785 (let* ((group (nnheader-file-to-group
786 (directory-file-name dir) nnml-directory))
787 (info (cadr (assoc group nnml-group-alist))))
789 (setcar info (1+ (cdr info)))))
790 (funcall nnml-generate-active-function dir)
791 ;; Generate the nov file.
792 (nnml-generate-nov-file dir files)
794 (nnmail-save-active nnml-group-alist nnml-active-file))))))
796 (eval-when-compile (defvar files))
797 (defun nnml-generate-active-info (dir)
798 ;; Update the active info for this group.
799 (let* ((group (nnheader-file-to-group
800 (directory-file-name dir) nnml-directory))
801 (entry (assoc group nnml-group-alist))
802 (last (or (caadr entry) 0)))
803 (setq nnml-group-alist (delq entry nnml-group-alist))
805 (cons (or (caar files) (1+ last))
807 (or (caar (last files))
811 (defun nnml-generate-nov-file (dir files)
812 (let* ((dir (file-name-as-directory dir))
813 (nov (concat dir nnml-nov-file-name))
814 (nov-buffer (get-buffer-create " *nov*"))
817 ;; Init the nov buffer.
818 (set-buffer nov-buffer)
819 (buffer-disable-undo)
821 (set-buffer nntp-server-buffer)
822 ;; Delete the old NOV file.
823 (when (file-exists-p nov)
824 (funcall nnmail-delete-file-function nov))
826 (unless (file-directory-p (setq file (concat dir (cdar files))))
828 (nnheader-insert-file-contents file)
830 (goto-char (point-min))
832 (re-search-forward "\n\r?\n" nil t)
833 (setq chars (- (point-max) (point)))
834 (max (point-min) (1- (point)))))
835 (unless (zerop (buffer-size))
836 (goto-char (point-min))
837 (setq headers (nnml-parse-head chars (caar files)))
839 (set-buffer nov-buffer)
840 (goto-char (point-max))
841 (nnheader-insert-nov headers)))
843 (setq files (cdr files)))
845 (set-buffer nov-buffer)
846 (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
847 (kill-buffer (current-buffer))))))
849 (defun nnml-nov-delete-article (group article)
851 (set-buffer (nnml-open-nov group))
852 (when (nnheader-find-nov-line article)
853 (delete-region (point) (progn (forward-line 1) (point)))
855 (let ((active (cadr (assoc group nnml-group-alist)))
859 (setf (car active) (1+ (cdr active)))
860 (when (and (setq num (ignore-errors (read (current-buffer))))
862 (setf (car active) num)))))))
865 (defun nnml-update-file-alist (&optional force)
866 (when nnml-use-compressed-files
867 (when (or (not nnml-article-file-alist)
869 (setq nnml-article-file-alist
870 (nnml-current-group-article-to-file-alist)))))
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-current-group-article-to-file-alist ()
895 "Return an alist of article/file pairs in the current group.
896 Use the nov database for the current group if available."
897 (if (or nnml-use-compressed-files
901 (expand-file-name nnml-nov-file-name
902 nnml-current-directory))))
903 (nnheader-article-to-file-alist nnml-current-directory)
904 ;; build list from .overview if available
907 (buffer (nnml-get-nov-buffer nnml-current-group))
910 (goto-char (point-min))
912 (setq art (read (current-buffer)))
913 ;; assume file name is unadorned (ie. not compressed etc)
914 (push (cons art (int-to-string art)) alist)
918 (deffoo nnml-request-set-mark (group actions &optional server)
919 (nnml-possibly-change-directory group server)
920 (unless nnml-marks-is-evil
921 (nnml-open-marks group server)
922 (dolist (action actions)
923 (let ((range (nth 0 action))
924 (what (nth 1 action))
925 (marks (nth 2 action)))
926 (assert (or (eq what 'add) (eq what 'del)) nil
927 "Unknown request-set-mark action: %s" what)
929 (setq nnml-marks (gnus-update-alist-soft
931 (funcall (if (eq what 'add) 'gnus-range-add
932 'gnus-remove-from-range)
933 (cdr (assoc mark nnml-marks)) range)
935 (nnml-save-marks group server))
938 (deffoo nnml-request-update-info (group info &optional server)
939 (nnml-possibly-change-directory group server)
940 (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group))
941 (nnheader-message 8 "Updating marks for %s..." group)
942 (nnml-open-marks group server)
943 ;; Update info using `nnml-marks'.
945 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
948 (gnus-update-alist-soft
950 (cdr (assq (cdr pred) nnml-marks))
951 (gnus-info-marks info))
953 gnus-article-mark-lists)
954 (let ((seen (cdr (assq 'read nnml-marks))))
955 (gnus-info-set-read info
956 (if (and (integerp (car seen))
958 (list (cons (car seen) (car seen)))
960 (nnheader-message 8 "Updating marks for %s...done" group))
963 (defun nnml-marks-changed-p (group)
964 (let ((file (expand-file-name nnml-marks-file-name
965 (nnmail-group-pathname group nnml-directory))))
966 (if (null (gnus-gethash file nnml-marks-modtime))
967 t ;; never looked at marks file, assume it has changed
968 (not (equal (gnus-gethash file nnml-marks-modtime)
969 (nth 5 (file-attributes file)))))))
971 (defun nnml-save-marks (group server)
972 (let ((file-name-coding-system nnmail-pathname-coding-system)
973 (file (expand-file-name nnml-marks-file-name
974 (nnmail-group-pathname group nnml-directory))))
977 (nnml-possibly-create-directory group)
980 (gnus-prin1 nnml-marks)
983 (nth 5 (file-attributes file))
985 (error (or (gnus-yes-or-no-p
986 (format "Could not write to %s (%s). Continue? " file err))
987 (error "Cannot write to %s (%s)" file err))))))
989 (defun nnml-open-marks (group server)
990 (let ((file (expand-file-name
992 (nnmail-group-pathname group nnml-directory))))
993 (if (file-exists-p file)
996 (gnus-sethash file (nth 5 (file-attributes file))
998 (nnheader-insert-file-contents file)
999 (setq nnml-marks (read (current-buffer)))
1000 (dolist (el gnus-article-unpropagated-mark-lists)
1001 (setq nnml-marks (gnus-remassoc el nnml-marks))))
1002 (error (or (gnus-yes-or-no-p
1003 (format "Error reading nnml marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1004 (error "Cannot read nnml marks file %s (%s)" file err))))
1005 ;; User didn't have a .marks file. Probably first time
1006 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1007 (let ((info (gnus-get-info
1008 (gnus-group-prefixed-name
1010 (gnus-server-to-method (format "nnml:%s" server))))))
1011 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1012 (setq nnml-marks (gnus-info-marks info))
1013 (push (cons 'read (gnus-info-read info)) nnml-marks)
1014 (dolist (el gnus-article-unpropagated-mark-lists)
1015 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1016 (nnml-save-marks group server)
1017 (nnheader-message 7 "Bootstrapping marks for %s...done" group)))))
1021 ;;; Group and server compaction
1024 (defun nnml-request-compact-group (group &optional server save)
1025 (nnml-possibly-change-directory group server)
1026 (unless nnml-article-file-alist
1027 (setq nnml-article-file-alist
1028 (sort (nnml-current-group-article-to-file-alist)
1029 'car-less-than-car)))
1030 (if (not nnml-article-file-alist)
1031 ;; The group is empty: do nothing but return t
1033 ;; The group is not empty:
1034 (let* ((group-full-name
1035 (gnus-group-prefixed-name
1037 (gnus-server-to-method (format "nnml:%s" server))))
1038 (info (gnus-get-info group-full-name))
1041 (let ((articles nnml-article-file-alist)
1043 (while (setq article (pop articles))
1044 (let ((old-number (car article)))
1045 (when (> old-number new-number)
1046 ;; There is a gap here:
1048 ;; #### NOTE: `nnml-article-to-file' calls
1049 ;; #### `nnml-update-file-alist' (which in turn calls
1050 ;; #### `nnml-current-group-article-to-file-alist', which might
1051 ;; #### use the NOV database). This might turn out to be
1052 ;; #### inefficient. In that case, we will do the work manually.
1053 ;; 1/ Move the article to a new file:
1054 (let* ((oldfile (nnml-article-to-file old-number))
1056 (gnus-replace-in-string
1057 oldfile (concat "\\("
1058 (int-to-string old-number)
1059 "\\)\\(\\(\\.gz\\)?\\)$")
1060 (concat (int-to-string new-number) "\\2"))))
1061 (with-current-buffer nntp-server-buffer
1062 (nnmail-find-file oldfile)
1063 (nnmail-write-region (point-min) (point-max) newfile))
1064 (funcall nnmail-delete-file-function oldfile))
1065 ;; 2/ Update all marks for this article:
1066 ;; #### NOTE: it is possible that the new article number already
1067 ;; #### belongs to a range, whereas the corresponding article
1068 ;; #### doesn't exist (for example, if you delete an article).
1069 ;; #### For that reason, it is important to update the ranges
1070 ;; #### (meaning remove inexistant articles) before doing
1071 ;; anything on them.
1072 ;; 2 a/ read articles:
1073 (let ((read (gnus-info-read info)))
1074 (setq read (gnus-remove-from-range read (list new-number)))
1075 (when (gnus-member-of-range old-number read)
1076 (setq read (gnus-remove-from-range read (list old-number)))
1077 (setq read (gnus-add-to-range read (list new-number))))
1078 (gnus-info-set-read info read))
1079 ;; 2 b/ marked articles:
1080 (let ((oldmarks (gnus-info-marks info))
1082 (while (setq mark (pop oldmarks))
1083 (setcdr mark (gnus-remove-from-range (cdr mark)
1085 (when (gnus-member-of-range old-number (cdr mark))
1086 (setcdr mark (gnus-remove-from-range (cdr mark)
1088 (setcdr mark (gnus-add-to-range (cdr mark)
1089 (list new-number))))
1090 (push mark newmarks))
1091 (gnus-info-set-marks info newmarks))
1092 ;; 3/ Update the NOV entry for this article:
1093 (unless nnml-nov-is-evil
1095 (set-buffer (nnml-open-nov group))
1096 (when (nnheader-find-nov-line old-number)
1097 (looking-at (int-to-string old-number))
1098 (replace-match (int-to-string new-number) nil t)))))
1099 (setq new-number (1+ new-number)))))
1101 ;; No compaction had to be done:
1103 ;; Some articles have actually been renamed:
1104 ;; 1/ Rebuild active information:
1105 (let ((entry (assoc group nnml-group-alist))
1106 (active (cons 1 (1- new-number))))
1107 (setq nnml-group-alist (delq entry nnml-group-alist))
1108 (push (list group active) nnml-group-alist)
1109 ;; Update the active hashtable to let the *Group* buffer display
1110 ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1111 ;; gnus-newwrc-alist are out of date, since all we did is to modify
1112 ;; the info of the group internally.
1113 (gnus-set-active group-full-name active))
1115 ;; #### NOTE: normally, we should save the overview (NOV) file
1116 ;; #### here, just like we save the marks file. However, there is no
1117 ;; #### such function as nnml-save-nov for a single group. Only for
1118 ;; #### all groups. Gnus inconsistency is getting worse every day...
1119 ;; 2/ Rebuild marks file:
1120 (unless nnml-marks-is-evil
1121 ;; #### NOTE: this constant use of global variables everywhere is
1122 ;; #### truly disgusting. Gnus really needs a *major* cleanup.
1123 (setq nnml-marks (gnus-info-marks info))
1124 (push (cons 'read (gnus-info-read info)) nnml-marks)
1125 (dolist (el gnus-article-unpropagated-mark-lists)
1126 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1127 (nnml-save-marks group server))
1128 ;; 3/ Save everything if this was not part of a bigger operation:
1130 ;; Nothing to save (yet):
1132 ;; Something to save:
1133 ;; a/ Save the NOV databases:
1134 ;; #### NOTE: this should be done directory per directory in 1bis
1135 ;; #### above. See comment there.
1137 ;; b/ Save the active file:
1138 (nnmail-save-active nnml-group-alist nnml-active-file)
1141 (defun nnml-request-compact (&optional server)
1142 "Request compaction of all SERVER nnml groups."
1143 (interactive (list (or (nnoo-current-server 'nnml) "")))
1144 (nnmail-activate 'nnml)
1145 (unless (nnml-server-opened server)
1146 (nnml-open-server server))
1147 (setq nnml-directory (expand-file-name nnml-directory))
1148 (let* ((groups (gnus-groups-from-server
1149 (gnus-server-to-method (format "nnml:%s" server))))
1150 (first (pop groups))
1153 (while (setq group (pop groups))
1154 (nnml-request-compact-group (gnus-group-real-name group) server))
1155 (nnml-request-compact-group (gnus-group-real-name first) server t))))
1160 ;;; arch-tag: 52c97dc3-9735-45de-b439-9e4d23b52004
1161 ;;; nnml.el ends here