1 ;;; nnml.el --- mail spool access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 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")
45 (autoload 'gnus-backlog-remove-article "gnus-bcklg"))
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 If it is a string, use it as the file extension which specifies
91 the compression program. You can set it to \".bz2\" if your Emacs
92 supports auto-compression using the bzip2 program. A value of t
93 is equivalent to \".gz\".")
95 (defvoo nnml-compressed-files-size-threshold 1000
96 "Default size threshold for compressed message files.
97 Message files with bodies larger than that many characters will
98 be automatically compressed if `nnml-use-compressed-files' is
103 (defconst nnml-version "nnml 1.0"
106 (defvoo nnml-nov-file-name ".overview")
107 (defvoo nnml-marks-file-name ".marks")
109 (defvoo nnml-current-directory nil)
110 (defvoo nnml-current-group nil)
111 (defvoo nnml-status-string "")
112 (defvoo nnml-nov-buffer-alist nil)
113 (defvoo nnml-group-alist nil)
114 (defvoo nnml-active-timestamp nil)
115 (defvoo nnml-article-file-alist nil)
117 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
119 (defvar nnml-nov-buffer-file-name nil)
121 (defvoo nnml-file-coding-system nnmail-file-coding-system)
123 (defvoo nnml-marks nil)
125 (defvar nnml-marks-modtime (gnus-make-hashtable))
128 ;;; Interface functions.
130 (nnoo-define-basics nnml)
132 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
133 (when (nnml-possibly-change-directory group server)
135 (set-buffer nntp-server-buffer)
138 (number (length sequence))
140 (file-name-coding-system nnmail-pathname-coding-system)
142 (if (stringp (car sequence))
144 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
147 (setq article (car sequence))
148 (setq file (nnml-article-to-file article))
151 (not (file-directory-p file)))
152 (insert (format "221 %d Article retrieved.\n" article))
154 (nnheader-insert-head file)
156 (if (re-search-forward "\n\r?\n" nil t)
158 (goto-char (point-max))
161 (delete-region (point) (point-max)))
162 (setq sequence (cdr sequence))
163 (setq count (1+ count))
164 (and (numberp nnmail-large-newsgroup)
165 (> number nnmail-large-newsgroup)
167 (nnheader-message 6 "nnml: Receiving headers... %d%%"
168 (/ (* count 100) number))))
170 (and (numberp nnmail-large-newsgroup)
171 (> number nnmail-large-newsgroup)
172 (nnheader-message 6 "nnml: Receiving headers...done"))
174 (nnheader-fold-continuation-lines)
177 (deffoo nnml-open-server (server &optional defs)
178 (nnoo-change-server 'nnml server defs)
179 (when (not (file-exists-p nnml-directory))
180 (ignore-errors (make-directory nnml-directory t)))
182 ((not (file-exists-p nnml-directory))
184 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
185 ((not (file-directory-p (file-truename nnml-directory)))
187 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
189 (nnheader-report 'nnml "Opened server %s using directory %s"
190 server nnml-directory)
193 (deffoo nnml-request-regenerate (server)
194 (nnml-possibly-change-directory nil server)
195 (nnml-generate-nov-databases server)
198 (deffoo nnml-request-article (id &optional group server buffer)
199 (nnml-possibly-change-directory group server)
200 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
201 (file-name-coding-system nnmail-pathname-coding-system)
202 path gpath group-num)
204 (when (and (setq group-num (nnml-find-group-number id))
206 (assq (cdr group-num)
207 (nnheader-article-to-file-alist
209 (nnmail-group-pathname
212 (setq path (concat gpath (int-to-string (cdr group-num)))))
213 (setq path (nnml-article-to-file id)))
216 (nnheader-report 'nnml "No such article: %s" id))
217 ((not (file-exists-p path))
218 (nnheader-report 'nnml "No such file: %s" path))
219 ((file-directory-p path)
220 (nnheader-report 'nnml "File is a directory: %s" path))
221 ((not (save-excursion (let ((nnmail-file-coding-system
222 nnml-file-coding-system))
223 (nnmail-find-file path))))
224 (nnheader-report 'nnml "Couldn't read file: %s" path))
226 (nnheader-report 'nnml "Article %s retrieved" id)
227 ;; We return the article number.
228 (cons (if group-num (car group-num) group)
229 (string-to-number (file-name-nondirectory path)))))))
231 (deffoo nnml-request-group (group &optional server dont-check)
232 (let ((file-name-coding-system nnmail-pathname-coding-system))
234 ((not (nnml-possibly-change-directory group server))
235 (nnheader-report 'nnml "Invalid group (no such directory)"))
236 ((not (file-exists-p nnml-current-directory))
237 (nnheader-report 'nnml "Directory %s does not exist"
238 nnml-current-directory))
239 ((not (file-directory-p nnml-current-directory))
240 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
242 (nnheader-report 'nnml "Group %s selected" group)
245 (nnheader-re-read-dir nnml-current-directory)
246 (nnmail-activate 'nnml)
247 (let ((active (nth 1 (assoc group nnml-group-alist))))
249 (nnheader-report 'nnml "No such group: %s" group)
250 (nnheader-report 'nnml "Selected group %s" group)
251 (nnheader-insert "211 %d %d %d %s\n"
252 (max (1+ (- (cdr active) (car active))) 0)
253 (car active) (cdr active) group)))))))
255 (deffoo nnml-request-scan (&optional group server)
256 (setq nnml-article-file-alist nil)
257 (nnml-possibly-change-directory group server)
258 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
260 (deffoo nnml-close-group (group &optional server)
261 (setq nnml-article-file-alist nil)
264 (deffoo nnml-request-create-group (group &optional server args)
265 (nnml-possibly-change-directory nil server)
266 (nnmail-activate 'nnml)
268 ((let ((file (directory-file-name
269 (nnmail-group-pathname group nnml-directory))))
270 (and (file-exists-p file)
271 (not (file-directory-p file))))
272 (nnheader-report 'nnml "%s is a file"
274 (let ((nnmail-pathname-coding-system nil))
275 (nnmail-group-pathname group nnml-directory)))))
276 ((assoc group nnml-group-alist)
280 (push (list group (setq active (cons 1 0)))
282 (nnml-possibly-create-directory group)
283 (nnml-possibly-change-directory group server)
284 (let ((articles (nnml-directory-articles nnml-current-directory)))
286 (setcar active (apply 'min articles))
287 (setcdr active (apply 'max articles))))
288 (nnmail-save-active nnml-group-alist nnml-active-file)
291 (deffoo nnml-request-list (&optional server)
293 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
294 (file-name-coding-system nnmail-pathname-coding-system))
295 (nnmail-find-file nnml-active-file))
296 (setq nnml-group-alist (nnmail-get-active))
299 (deffoo nnml-request-newgroups (date &optional server)
300 (nnml-request-list server))
302 (deffoo nnml-request-list-newsgroups (&optional server)
304 (nnmail-find-file nnml-newsgroups-file)))
306 (deffoo nnml-request-expire-articles (articles group &optional server force)
307 (nnml-possibly-change-directory group server)
308 (let ((active-articles
309 (nnml-directory-articles nnml-current-directory))
311 article rest mod-time number target)
312 (nnmail-activate 'nnml)
314 (setq active-articles (sort active-articles '<))
315 ;; Articles not listed in active-articles are already gone,
316 ;; so don't try to expire them.
317 (setq articles (gnus-sorted-intersection articles active-articles))
319 (while (and articles is-old)
320 (if (and (setq article (nnml-article-to-file
321 (setq number (pop articles))))
322 (setq mod-time (nth 5 (file-attributes article)))
323 (nnml-deletable-article-p group number)
324 (setq is-old (nnmail-expired-article-p group mod-time force
325 nnml-inhibit-expiry)))
327 ;; Allow a special target group.
328 (setq target nnmail-expiry-target)
329 (unless (eq target 'delete)
331 (nnml-request-article number group server (current-buffer))
332 (let (nnml-current-directory
334 nnml-article-file-alist)
335 (when (functionp target)
336 (setq target (funcall target group)))
338 (or (gnus-request-group target)
339 (gnus-request-create-group target)))
340 (nnmail-expiry-target-group target group)
342 ;; Maybe directory is changed during nnmail-expiry-target-group.
343 (nnml-possibly-change-directory group server))
346 (nnheader-message 5 "Deleting article %s in %s"
349 (funcall nnmail-delete-file-function article)
352 (setq active-articles (delq number active-articles))
353 (nnml-nov-delete-article group number))
356 (let ((active (nth 1 (assoc group nnml-group-alist))))
358 (setcar active (or (and active-articles
359 (apply 'min active-articles))
361 (nnmail-save-active nnml-group-alist nnml-active-file))
363 (nconc rest articles)))
365 (deffoo nnml-request-move-article
366 (article group server accept-form &optional last move-is-internal)
367 (let ((buf (get-buffer-create " *nnml move*"))
369 (nnml-possibly-change-directory group server)
370 (nnml-update-file-alist)
372 (nnml-deletable-article-p group article)
373 (nnml-request-article article group server)
374 (let (nnml-current-directory
376 nnml-article-file-alist)
379 (insert-buffer-substring nntp-server-buffer)
380 (setq result (eval accept-form))
381 (kill-buffer (current-buffer))
384 (nnml-possibly-change-directory group server)
386 (funcall nnmail-delete-file-function
387 (nnml-article-to-file article))
389 (nnml-nov-delete-article group article)
392 (nnmail-save-active nnml-group-alist nnml-active-file))))
395 (deffoo nnml-request-accept-article (group &optional server last)
396 (nnml-possibly-change-directory group server)
397 (nnmail-check-syntax)
399 (when nnmail-cache-accepted-message-ids
400 (nnmail-cache-insert (nnmail-fetch-field "message-id")
402 (nnmail-fetch-field "subject")
403 (nnmail-fetch-field "from")))
406 (nnmail-activate 'nnml)
407 (setq result (car (nnml-save-mail
408 (list (cons group (nnml-active-number group))))))
410 (nnmail-save-active nnml-group-alist nnml-active-file)
411 (and last (nnml-save-nov))))
413 (nnmail-activate 'nnml)
414 (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
415 (yes-or-no-p "Moved to `junk' group; delete article? "))
417 (setq result (car (nnml-save-mail result))))
419 (nnmail-save-active nnml-group-alist nnml-active-file)
420 (when nnmail-cache-accepted-message-ids
421 (nnmail-cache-close))
425 (deffoo nnml-request-post (&optional server)
426 (nnmail-do-request-post 'nnml-request-accept-article server))
428 (deffoo nnml-request-replace-article (article group buffer)
429 (nnml-possibly-change-directory group)
432 (nnml-possibly-create-directory group)
433 (let ((chars (nnmail-insert-lines))
434 (art (concat (int-to-string article) "\t"))
438 (point-min) (point-max)
439 (or (nnml-article-to-file article)
440 (expand-file-name (int-to-string article)
441 nnml-current-directory))
442 nil (if (nnheader-be-verbose 5) nil 'nomesg))
444 (setq headers (nnml-parse-head chars article))
445 ;; Replace the NOV line in the NOV file.
447 (set-buffer (nnml-open-nov group))
448 (goto-char (point-min))
449 (if (or (looking-at art)
450 (search-forward (concat "\n" art) nil t))
451 ;; Delete the old NOV line.
453 ;; The line isn't here, so we have to find out where
454 ;; we should insert it. (This situation should never
455 ;; occur, but one likes to make sure...)
456 (while (and (looking-at "[0-9]+\t")
459 (match-beginning 0) (match-end 0)))
461 (zerop (forward-line 1)))))
463 (nnheader-insert-nov headers)
467 (deffoo nnml-request-delete-group (group &optional force server)
468 (nnml-possibly-change-directory group server)
470 ;; Delete all articles in GROUP.
473 nnml-current-directory t
474 (concat nnheader-numerical-short-files
475 "\\|" (regexp-quote nnml-nov-file-name) "$"
476 "\\|" (regexp-quote nnml-marks-file-name) "$"))))
477 (dolist (article articles)
478 (when (file-writable-p article)
479 (nnheader-message 5 "Deleting article %s in %s..." article group)
480 (funcall nnmail-delete-file-function article))))
481 ;; Try to delete the directory itself.
482 (ignore-errors (delete-directory nnml-current-directory)))
483 ;; Remove the group from all structures.
484 (setq nnml-group-alist
485 (delq (assoc group nnml-group-alist) nnml-group-alist)
486 nnml-current-group nil
487 nnml-current-directory nil)
488 ;; Save the active file.
489 (nnmail-save-active nnml-group-alist nnml-active-file)
492 (deffoo nnml-request-rename-group (group new-name &optional server)
493 (nnml-possibly-change-directory group server)
494 (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
495 (old-dir (nnmail-group-pathname group nnml-directory)))
497 (make-directory new-dir t)
499 ;; We move the articles file by file instead of renaming
500 ;; the directory -- there may be subgroups in this group.
501 ;; One might be more clever, I guess.
502 (dolist (file (nnheader-article-to-file-alist old-dir))
504 (concat old-dir (cdr file))
505 (concat new-dir (cdr file))))
506 ;; Move .overview file.
507 (let ((overview (concat old-dir nnml-nov-file-name)))
508 (when (file-exists-p overview)
509 (rename-file overview (concat new-dir nnml-nov-file-name))))
511 (let ((marks (concat old-dir nnml-marks-file-name)))
512 (when (file-exists-p marks)
513 (rename-file marks (concat new-dir nnml-marks-file-name))))
514 (when (<= (length (directory-files old-dir)) 2)
515 (ignore-errors (delete-directory old-dir)))
516 ;; That went ok, so we change the internal structures.
517 (let ((entry (assoc group nnml-group-alist)))
519 (setcar entry new-name))
520 (setq nnml-current-directory nil
521 nnml-current-group nil)
522 ;; Save the new group alist.
523 (nnmail-save-active nnml-group-alist nnml-active-file)
526 (deffoo nnml-set-status (article name value &optional group server)
527 (nnml-possibly-change-directory group server)
528 (let ((file (nnml-article-to-file article)))
530 ((not (file-exists-p file))
531 (nnheader-report 'nnml "File %s does not exist" file))
534 (nnheader-insert-file-contents file)
535 (nnmail-replace-status name value))
539 ;;; Internal functions.
541 (defun nnml-article-to-file (article)
542 (nnml-update-file-alist)
545 (if nnml-use-compressed-files
546 (cdr (assq article nnml-article-file-alist))
547 (number-to-string article)))
548 (expand-file-name file nnml-current-directory)
549 (when (not nnheader-directory-files-is-safe)
550 ;; Just to make sure nothing went wrong when reading over NFS --
553 (setq file (expand-file-name (number-to-string article)
554 nnml-current-directory)))
555 (nnml-update-file-alist t)
558 (defun nnml-deletable-article-p (group article)
559 "Say whether ARTICLE in GROUP can be deleted."
561 (when (setq path (nnml-article-to-file article))
562 (when (file-writable-p path)
563 (or (not nnmail-keep-last-article)
564 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
567 ;; Find an article number in the current group given the Message-ID.
568 (defun nnml-find-group-number (id)
570 (set-buffer (get-buffer-create " *nnml id*"))
571 (let ((alist nnml-group-alist)
573 ;; We want to look through all .overview files, but we want to
574 ;; start with the one in the current directory. It seems most
575 ;; likely that the article we are looking for is in that group.
576 (if (setq number (nnml-find-id nnml-current-group id))
577 (cons nnml-current-group number)
578 ;; It wasn't there, so we look through the other groups as well.
579 (while (and (not number)
581 (or (string= (caar alist) nnml-current-group)
582 (setq number (nnml-find-id (caar alist) id)))
584 (setq alist (cdr alist))))
586 (cons (caar alist) number))))))
588 (defun nnml-find-id (group id)
590 (let ((nov (expand-file-name nnml-nov-file-name
591 (nnmail-group-pathname group nnml-directory)))
593 (when (file-exists-p nov)
594 (nnheader-insert-file-contents nov)
595 (while (and (not found)
596 (search-forward id nil t)) ; We find the ID.
597 ;; And the id is in the fourth field.
598 (if (not (and (search-backward "\t" nil t 4)
599 (not (search-backward "\t" (point-at-bol) t))))
603 ;; We return the article number.
605 (ignore-errors (read (current-buffer))))))
608 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
609 (if (or gnus-nov-is-evil nnml-nov-is-evil)
611 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
612 (when (file-exists-p nov)
614 (set-buffer nntp-server-buffer)
616 (nnheader-insert-file-contents nov)
618 (not (numberp fetch-old)))
619 t ; Don't remove anything.
620 (nnheader-nov-delete-outside-range
621 (if fetch-old (max 1 (- (car articles) fetch-old))
623 (car (last articles)))
626 (defun nnml-possibly-change-directory (group &optional server)
628 (not (nnml-server-opened server)))
629 (nnml-open-server server))
632 (let ((pathname (nnmail-group-pathname group nnml-directory))
633 (file-name-coding-system nnmail-pathname-coding-system))
634 (when (not (equal pathname nnml-current-directory))
635 (setq nnml-current-directory pathname
636 nnml-current-group group
637 nnml-article-file-alist nil))
638 (file-exists-p nnml-current-directory))))
640 (defun nnml-possibly-create-directory (group)
641 (let ((dir (nnmail-group-pathname group nnml-directory)))
642 (unless (file-exists-p dir)
643 (make-directory (directory-file-name dir) t)
644 (nnheader-message 5 "Creating mail directory %s" dir))))
646 (defun nnml-save-mail (group-art)
647 "Called narrowed to an article."
648 (let (chars headers extension)
649 (setq chars (nnmail-insert-lines))
651 (and nnml-use-compressed-files
652 (> chars nnml-compressed-files-size-threshold)
653 (if (stringp nnml-use-compressed-files)
654 nnml-use-compressed-files
656 (nnmail-insert-xref group-art)
657 (run-hooks 'nnmail-prepare-save-mail-hook)
658 (run-hooks 'nnml-prepare-save-mail-hook)
659 (goto-char (point-min))
660 (while (looking-at "From ")
661 (replace-match "X-From-Line: ")
663 ;; We save the article in all the groups it belongs in.
667 (nnml-possibly-create-directory (caar ga))
668 (let ((file (concat (nnmail-group-pathname
669 (caar ga) nnml-directory)
670 (int-to-string (cdar ga))
673 ;; It was already saved, so we just make a hard link.
674 (funcall nnmail-crosspost-link-function first file t)
676 (nnmail-write-region (point-min) (point-max) file nil
677 (if (nnheader-be-verbose 5) nil 'nomesg))
680 ;; Generate a nov line for this article. We generate the nov
681 ;; line after saving, because nov generation destroys the
683 (setq headers (nnml-parse-head chars))
684 ;; Output the nov line to all nov databases that should have it.
685 (let ((ga group-art))
687 (nnml-add-nov (caar ga) (cdar ga) headers)
691 (defun nnml-active-number (group)
692 "Compute the next article number in GROUP."
693 (let ((active (cadr (assoc group nnml-group-alist))))
694 ;; The group wasn't known to nnml, so we just create an active
697 ;; Perhaps the active file was corrupt? See whether
698 ;; there are any articles in this group.
699 (nnml-possibly-create-directory group)
700 (nnml-possibly-change-directory group)
701 (unless nnml-article-file-alist
702 (setq nnml-article-file-alist
704 (nnml-current-group-article-to-file-alist)
705 'car-less-than-car)))
707 (if nnml-article-file-alist
708 (cons (caar nnml-article-file-alist)
709 (caar (last nnml-article-file-alist)))
711 (push (list group active) nnml-group-alist))
712 (setcdr active (1+ (cdr active)))
713 (while (file-exists-p
714 (expand-file-name (int-to-string (cdr active))
715 (nnmail-group-pathname group nnml-directory)))
716 (setcdr active (1+ (cdr active))))
719 (defun nnml-add-nov (group article headers)
720 "Add a nov line for the GROUP base."
722 (set-buffer (nnml-open-nov group))
723 (goto-char (point-max))
724 (mail-header-set-number headers article)
725 (nnheader-insert-nov headers)))
727 (defsubst nnml-header-value ()
728 (buffer-substring (match-end 0) (point-at-eol)))
730 (defun nnml-parse-head (chars &optional number)
731 "Parse the head of the current buffer."
734 (unless (zerop (buffer-size))
736 (goto-char (point-min))
737 (if (re-search-forward "\n\r?\n" nil t)
740 (let ((headers (nnheader-parse-naked-head)))
741 (mail-header-set-chars headers chars)
742 (mail-header-set-number headers number)
745 (defun nnml-get-nov-buffer (group)
746 (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
749 (set (make-local-variable 'nnml-nov-buffer-file-name)
752 (nnmail-group-pathname group nnml-directory)))
754 (when (file-exists-p nnml-nov-buffer-file-name)
755 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
758 (defun nnml-open-nov (group)
759 (or (cdr (assoc group nnml-nov-buffer-alist))
760 (let ((buffer (nnml-get-nov-buffer group)))
761 (push (cons group buffer) nnml-nov-buffer-alist)
764 (defun nnml-save-nov ()
766 (while nnml-nov-buffer-alist
767 (when (buffer-name (cdar nnml-nov-buffer-alist))
768 (set-buffer (cdar nnml-nov-buffer-alist))
769 (when (buffer-modified-p)
770 (nnmail-write-region (point-min) (point-max)
771 nnml-nov-buffer-file-name nil 'nomesg))
772 (set-buffer-modified-p nil)
773 (kill-buffer (current-buffer)))
774 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
777 (defun nnml-generate-nov-databases (&optional server)
778 "Generate NOV databases in all nnml directories."
779 (interactive (list (or (nnoo-current-server 'nnml) "")))
780 ;; Read the active file to make sure we don't re-use articles
781 ;; numbers in empty groups.
782 (nnmail-activate 'nnml)
783 (unless (nnml-server-opened server)
784 (nnml-open-server server))
785 (setq nnml-directory (expand-file-name nnml-directory))
786 ;; Recurse down the directories.
787 (nnml-generate-nov-databases-directory nnml-directory nil t)
788 ;; Save the active file.
789 (nnmail-save-active nnml-group-alist nnml-active-file))
791 (defun nnml-generate-nov-databases-directory (dir &optional seen no-active)
792 "Regenerate the NOV database in DIR.
794 Unless no-active is non-nil, update the active file too."
795 (interactive "DRegenerate NOV in: ")
796 (setq dir (file-name-as-directory dir))
797 ;; Only scan this sub-tree if we haven't been here yet.
798 (unless (member (file-truename dir) seen)
799 (push (file-truename dir) seen)
800 ;; We descend recursively
801 (dolist (dir (directory-files dir t nil t))
802 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
803 (file-directory-p dir))
804 (nnml-generate-nov-databases-directory dir seen)))
805 ;; Do this directory.
806 (let ((files (sort (nnheader-article-to-file-alist dir)
807 'car-less-than-car)))
809 (let* ((group (nnheader-file-to-group
810 (directory-file-name dir) nnml-directory))
811 (info (cadr (assoc group nnml-group-alist))))
813 (setcar info (1+ (cdr info)))))
814 (funcall nnml-generate-active-function dir)
815 ;; Generate the nov file.
816 (nnml-generate-nov-file dir files)
818 (nnmail-save-active nnml-group-alist nnml-active-file))))))
820 (eval-when-compile (defvar files))
821 (defun nnml-generate-active-info (dir)
822 ;; Update the active info for this group.
823 (let* ((group (nnheader-file-to-group
824 (directory-file-name dir) nnml-directory))
825 (entry (assoc group nnml-group-alist))
826 (last (or (caadr entry) 0)))
827 (setq nnml-group-alist (delq entry nnml-group-alist))
829 (cons (or (caar files) (1+ last))
831 (or (caar (last files))
835 (defun nnml-generate-nov-file (dir files)
836 (let* ((dir (file-name-as-directory dir))
837 (nov (concat dir nnml-nov-file-name))
838 (nov-buffer (get-buffer-create " *nov*"))
841 ;; Init the nov buffer.
842 (set-buffer nov-buffer)
843 (buffer-disable-undo)
845 (set-buffer nntp-server-buffer)
846 ;; Delete the old NOV file.
847 (when (file-exists-p nov)
848 (funcall nnmail-delete-file-function nov))
850 (unless (file-directory-p (setq file (concat dir (cdar files))))
852 (nnheader-insert-file-contents file)
854 (goto-char (point-min))
856 (re-search-forward "\n\r?\n" nil t)
857 (setq chars (- (point-max) (point)))
858 (max (point-min) (1- (point)))))
859 (unless (zerop (buffer-size))
860 (goto-char (point-min))
861 (setq headers (nnml-parse-head chars (caar files)))
863 (set-buffer nov-buffer)
864 (goto-char (point-max))
865 (nnheader-insert-nov headers)))
867 (setq files (cdr files)))
869 (set-buffer nov-buffer)
870 (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
871 (kill-buffer (current-buffer))))))
873 (defun nnml-nov-delete-article (group article)
875 (set-buffer (nnml-open-nov group))
876 (when (nnheader-find-nov-line article)
877 (delete-region (point) (progn (forward-line 1) (point)))
879 (let ((active (cadr (assoc group nnml-group-alist)))
883 (setf (car active) (1+ (cdr active)))
884 (when (and (setq num (ignore-errors (read (current-buffer))))
886 (setf (car active) num)))))))
889 (defun nnml-update-file-alist (&optional force)
890 (when nnml-use-compressed-files
891 (when (or (not nnml-article-file-alist)
893 (setq nnml-article-file-alist
894 (nnml-current-group-article-to-file-alist)))))
896 (defun nnml-directory-articles (dir)
897 "Return a list of all article files in a directory.
898 Use the nov database for that directory if available."
899 (if (or gnus-nov-is-evil nnml-nov-is-evil
901 (expand-file-name nnml-nov-file-name dir))))
902 (nnheader-directory-articles dir)
903 ;; build list from .overview if available
904 ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
905 ;; defvoo'd, and we might get called when it hasn't been swapped in.
909 (buffer (nnml-get-nov-buffer nnml-current-group)))
911 (goto-char (point-min))
913 (setq art (read (current-buffer)))
918 (defun nnml-current-group-article-to-file-alist ()
919 "Return an alist of article/file pairs in the current group.
920 Use the nov database for the current group if available."
921 (if (or nnml-use-compressed-files
925 (expand-file-name nnml-nov-file-name
926 nnml-current-directory))))
927 (nnheader-article-to-file-alist nnml-current-directory)
928 ;; build list from .overview if available
931 (buffer (nnml-get-nov-buffer nnml-current-group))
934 (goto-char (point-min))
936 (setq art (read (current-buffer)))
937 ;; assume file name is unadorned (ie. not compressed etc)
938 (push (cons art (int-to-string art)) alist)
942 (deffoo nnml-request-set-mark (group actions &optional server)
943 (nnml-possibly-change-directory group server)
944 (unless nnml-marks-is-evil
945 (nnml-open-marks group server)
946 (dolist (action actions)
947 (let ((range (nth 0 action))
948 (what (nth 1 action))
949 (marks (nth 2 action)))
950 (assert (or (eq what 'add) (eq what 'del)) nil
951 "Unknown request-set-mark action: %s" what)
953 (setq nnml-marks (gnus-update-alist-soft
955 (funcall (if (eq what 'add) 'gnus-range-add
956 'gnus-remove-from-range)
957 (cdr (assoc mark nnml-marks)) range)
959 (nnml-save-marks group server))
962 (deffoo nnml-request-update-info (group info &optional server)
963 (nnml-possibly-change-directory group server)
964 (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group))
965 (nnheader-message 8 "Updating marks for %s..." group)
966 (nnml-open-marks group server)
967 ;; Update info using `nnml-marks'.
969 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
972 (gnus-update-alist-soft
974 (cdr (assq (cdr pred) nnml-marks))
975 (gnus-info-marks info))
977 gnus-article-mark-lists)
978 (let ((seen (cdr (assq 'read nnml-marks))))
979 (gnus-info-set-read info
980 (if (and (integerp (car seen))
982 (list (cons (car seen) (car seen)))
984 (nnheader-message 8 "Updating marks for %s...done" group))
987 (defun nnml-marks-changed-p (group)
988 (let ((file (expand-file-name nnml-marks-file-name
989 (nnmail-group-pathname group nnml-directory))))
990 (if (null (gnus-gethash file nnml-marks-modtime))
991 t ;; never looked at marks file, assume it has changed
992 (not (equal (gnus-gethash file nnml-marks-modtime)
993 (nth 5 (file-attributes file)))))))
995 (defun nnml-save-marks (group server)
996 (let ((file-name-coding-system nnmail-pathname-coding-system)
997 (file (expand-file-name nnml-marks-file-name
998 (nnmail-group-pathname group nnml-directory))))
1001 (nnml-possibly-create-directory group)
1002 (with-temp-file file
1004 (gnus-prin1 nnml-marks)
1007 (nth 5 (file-attributes file))
1008 nnml-marks-modtime))
1009 (error (or (gnus-yes-or-no-p
1010 (format "Could not write to %s (%s). Continue? " file err))
1011 (error "Cannot write to %s (%s)" file err))))))
1013 (defun nnml-open-marks (group server)
1014 (let ((file (expand-file-name
1015 nnml-marks-file-name
1016 (nnmail-group-pathname group nnml-directory))))
1017 (if (file-exists-p file)
1020 (gnus-sethash file (nth 5 (file-attributes file))
1022 (nnheader-insert-file-contents file)
1023 (setq nnml-marks (read (current-buffer)))
1024 (dolist (el gnus-article-unpropagated-mark-lists)
1025 (setq nnml-marks (gnus-remassoc el nnml-marks))))
1026 (error (or (gnus-yes-or-no-p
1027 (format "Error reading nnml marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1028 (error "Cannot read nnml marks file %s (%s)" file err))))
1029 ;; User didn't have a .marks file. Probably first time
1030 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1031 (let ((info (gnus-get-info
1032 (gnus-group-prefixed-name
1034 (gnus-server-to-method (format "nnml:%s" server))))))
1035 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1036 (setq nnml-marks (gnus-info-marks info))
1037 (push (cons 'read (gnus-info-read info)) nnml-marks)
1038 (dolist (el gnus-article-unpropagated-mark-lists)
1039 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1040 (nnml-save-marks group server)
1041 (nnheader-message 7 "Bootstrapping marks for %s...done" group)))))
1045 ;;; Group and server compaction. -- dvl
1048 ;; #### FIXME: this function handles self Xref: entry correctly, but I don't
1049 ;; #### know how to handle external cross-references. I actually don't know if
1050 ;; #### this is handled correctly elsewhere. For instance, what happens if you
1051 ;; #### move all articles to a new group (that's what people do for manual
1052 ;; #### compaction) ?
1054 ;; #### NOTE: the function below handles the article backlog. This is
1055 ;; #### conceptually the wrong place to do it because the backend is at a
1056 ;; #### lower level. However, this is the only place where we have the needed
1057 ;; #### information to do the job. Ideally, this function should not handle
1058 ;; #### the backlog by itself, but return a list of moved groups / articles to
1059 ;; #### the caller. This will become important to avoid code duplication when
1060 ;; #### other backends get a compaction feature. Also, note that invalidating
1061 ;; #### the "original article buffer" is already done at an upper level.
1063 ;; Shouldn't `nnml-request-compact-group' be interactive? --rsteib
1065 (defun nnml-request-compact-group (group &optional server save)
1066 (nnml-possibly-change-directory group server)
1067 (unless nnml-article-file-alist
1068 (setq nnml-article-file-alist
1069 (sort (nnml-current-group-article-to-file-alist)
1070 'car-less-than-car)))
1071 (if (not nnml-article-file-alist)
1072 ;; The group is empty: do nothing but return t
1074 ;; The group is not empty:
1075 (let* ((group-full-name
1076 (gnus-group-prefixed-name
1078 (gnus-server-to-method (format "nnml:%s" server))))
1079 (info (gnus-get-info group-full-name))
1082 (let ((articles nnml-article-file-alist)
1084 (while (setq article (pop articles))
1085 (let ((old-number (car article)))
1086 (when (> old-number new-number)
1087 ;; There is a gap here:
1088 (let ((old-number-string (int-to-string old-number))
1089 (new-number-string (int-to-string new-number)))
1091 ;; #### NOTE: `nnml-article-to-file' calls
1092 ;; #### `nnml-update-file-alist' (which in turn calls
1093 ;; #### `nnml-current-group-article-to-file-alist', which
1094 ;; #### might use the NOV database). This might turn out to be
1095 ;; #### inefficient. In that case, we will do the work
1097 ;; 1/ Move the article to a new file:
1098 (let* ((oldfile (nnml-article-to-file old-number))
1100 (gnus-replace-in-string
1102 ;; nnml-use-compressed-files might be any string, but
1103 ;; probably it's sufficient to take into account only
1104 ;; "\\.[a-z0-9]+". Note that we can't only use the
1105 ;; value of nnml-use-compressed-files because old
1106 ;; articles might have been saved with a different
1109 "\\(" old-number-string "\\)\\(\\(\\.[a-z0-9]+\\)?\\)$")
1110 (concat new-number-string "\\2"))))
1111 (with-current-buffer nntp-server-buffer
1112 (nnmail-find-file oldfile)
1113 ;; Update the Xref header in the article itself:
1114 (when (and (re-search-forward "^Xref: [^ ]+ " nil t)
1118 (concat group ":" old-number-string))
1122 (concat group ":" new-number-string)))
1123 ;; Save to the new file:
1124 (nnmail-write-region (point-min) (point-max) newfile))
1125 (funcall nnmail-delete-file-function oldfile))
1126 ;; 2/ Update all marks for this article:
1127 ;; #### NOTE: it is possible that the new article number
1128 ;; #### already belongs to a range, whereas the corresponding
1129 ;; #### article doesn't exist (for example, if you delete an
1130 ;; #### article). For that reason, it is important to update
1131 ;; #### the ranges (meaning remove inexistant articles) before
1132 ;; #### doing anything on them.
1133 ;; 2 a/ read articles:
1134 (let ((read (gnus-info-read info)))
1135 (setq read (gnus-remove-from-range read (list new-number)))
1136 (when (gnus-member-of-range old-number read)
1137 (setq read (gnus-remove-from-range read (list old-number)))
1138 (setq read (gnus-add-to-range read (list new-number))))
1139 (gnus-info-set-read info read))
1140 ;; 2 b/ marked articles:
1141 (let ((oldmarks (gnus-info-marks info))
1143 (while (setq mark (pop oldmarks))
1144 (setcdr mark (gnus-remove-from-range (cdr mark)
1146 (when (gnus-member-of-range old-number (cdr mark))
1147 (setcdr mark (gnus-remove-from-range (cdr mark)
1149 (setcdr mark (gnus-add-to-range (cdr mark)
1150 (list new-number))))
1151 (push mark newmarks))
1152 (gnus-info-set-marks info newmarks))
1153 ;; 3/ Update the NOV entry for this article:
1154 (unless nnml-nov-is-evil
1156 (set-buffer (nnml-open-nov group))
1157 (when (nnheader-find-nov-line old-number)
1158 ;; Replace the article number:
1159 (looking-at old-number-string)
1160 (replace-match new-number-string nil t)
1161 ;; Update the Xref header:
1162 (when (re-search-forward
1163 (concat "\\(Xref:[^\t\n]* \\)\\<"
1165 (concat group ":" old-number-string))
1169 (concat "\\1" group ":" new-number-string))))))
1170 ;; 4/ Possibly remove the article from the backlog:
1171 (when gnus-keep-backlog
1172 ;; #### NOTE: instead of removing the article, we could
1173 ;; #### modify the backlog to reflect the numbering change,
1174 ;; #### but I don't think it's worth it.
1175 (gnus-backlog-remove-article group-full-name old-number)
1176 (gnus-backlog-remove-article group-full-name new-number))))
1177 (setq new-number (1+ new-number)))))
1179 ;; No compaction had to be done:
1181 ;; Some articles have actually been renamed:
1182 ;; 1/ Rebuild active information:
1183 (let ((entry (assoc group nnml-group-alist))
1184 (active (cons 1 (1- new-number))))
1185 (setq nnml-group-alist (delq entry nnml-group-alist))
1186 (push (list group active) nnml-group-alist)
1187 ;; Update the active hashtable to let the *Group* buffer display
1188 ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1189 ;; gnus-newwrc-alist are out of date, since all we did is to modify
1190 ;; the info of the group internally.
1191 (gnus-set-active group-full-name active))
1193 ;; #### NOTE: normally, we should save the overview (NOV) file
1194 ;; #### here, just like we save the marks file. However, there is no
1195 ;; #### such function as nnml-save-nov for a single group. Only for
1196 ;; #### all groups. Gnus inconsistency is getting worse every day...
1197 ;; 2/ Rebuild marks file:
1198 (unless nnml-marks-is-evil
1199 ;; #### NOTE: this constant use of global variables everywhere is
1200 ;; #### truly disgusting. Gnus really needs a *major* cleanup.
1201 (setq nnml-marks (gnus-info-marks info))
1202 (push (cons 'read (gnus-info-read info)) nnml-marks)
1203 (dolist (el gnus-article-unpropagated-mark-lists)
1204 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1205 (nnml-save-marks group server))
1206 ;; 3/ Save everything if this was not part of a bigger operation:
1208 ;; Nothing to save (yet):
1210 ;; Something to save:
1211 ;; a/ Save the NOV databases:
1212 ;; #### NOTE: this should be done directory per directory in 1bis
1213 ;; #### above. See comment there.
1215 ;; b/ Save the active file:
1216 (nnmail-save-active nnml-group-alist nnml-active-file)
1219 (defun nnml-request-compact (&optional server)
1220 "Request compaction of all SERVER nnml groups."
1221 (interactive (list (or (nnoo-current-server 'nnml) "")))
1222 (nnmail-activate 'nnml)
1223 (unless (nnml-server-opened server)
1224 (nnml-open-server server))
1225 (setq nnml-directory (expand-file-name nnml-directory))
1226 (let* ((groups (gnus-groups-from-server
1227 (gnus-server-to-method (format "nnml:%s" server))))
1228 (first (pop groups))
1231 (while (setq group (pop groups))
1232 (nnml-request-compact-group (gnus-group-real-name group) server))
1233 (nnml-request-compact-group (gnus-group-real-name first) server t))))
1238 ;;; arch-tag: 52c97dc3-9735-45de-b439-9e4d23b52004
1239 ;;; nnml.el ends here