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")
46 (autoload 'gnus-backlog-remove-article "gnus-bcklg"))
50 (defvoo nnml-directory message-directory
51 "Spool directory for the nnml mail backend.")
53 (defvoo nnml-active-file
54 (expand-file-name "active" nnml-directory)
57 (defvoo nnml-newsgroups-file
58 (expand-file-name "newsgroups" nnml-directory)
59 "Mail newsgroups description file.")
61 (defvoo nnml-get-new-mail t
62 "If non-nil, nnml will check the incoming mail file and split the mail.")
64 (defvoo nnml-nov-is-evil nil
65 "If non-nil, Gnus will never generate and use nov databases for mail spools.
66 Using nov databases will speed up header fetching considerably.
67 This variable shouldn't be flipped much. If you have, for some reason,
68 set this to t, and want to set it to nil again, you should always run
69 the `nnml-generate-nov-databases' command. The function will go
70 through all nnml directories and generate nov databases for them
71 all. This may very well take some time.")
73 (defvoo nnml-marks-is-evil nil
74 "If non-nil, Gnus will never generate and use marks file for mail spools.
75 Using marks files makes it possible to backup and restore mail groups
76 separately from `.newsrc.eld'. If you have, for some reason, set this
77 to t, and want to set it to nil again, you should always remove the
78 corresponding marks file (usually named `.marks' in the nnml group
79 directory, but see `nnml-marks-file-name') for the group. Then the
80 marks file will be regenerated properly by Gnus.")
82 (defvoo nnml-prepare-save-mail-hook nil
83 "Hook run narrowed to an article before saving.")
85 (defvoo nnml-inhibit-expiry nil
86 "If non-nil, inhibit expiry.")
88 (defvoo nnml-use-compressed-files nil
89 "If non-nil, allow using compressed message files.
91 If it is a string, use it as the file extension which specifies
92 the comression program. You can set it to \".bz2\" if your Emacs
93 supports auto-compression using the bzip2 program. A value of t
94 is equivalent to \".gz\".")
96 (defvoo nnml-compressed-files-size-threshold 1000
97 "Default size threshold for compressed message files.
98 Message files with bodies larger than that many characters will
99 be automatically compressed if `nnml-use-compressed-files' is
104 (defconst nnml-version "nnml 1.0"
107 (defvoo nnml-nov-file-name ".overview")
108 (defvoo nnml-marks-file-name ".marks")
110 (defvoo nnml-current-directory nil)
111 (defvoo nnml-current-group nil)
112 (defvoo nnml-status-string "")
113 (defvoo nnml-nov-buffer-alist nil)
114 (defvoo nnml-group-alist nil)
115 (defvoo nnml-active-timestamp nil)
116 (defvoo nnml-article-file-alist nil)
118 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
120 (defvar nnml-nov-buffer-file-name nil)
122 (defvoo nnml-file-coding-system nnmail-file-coding-system)
124 (defvoo nnml-marks nil)
126 (defvar nnml-marks-modtime (gnus-make-hashtable))
129 ;;; Interface functions.
131 (nnoo-define-basics nnml)
133 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
134 (when (nnml-possibly-change-directory group server)
136 (set-buffer nntp-server-buffer)
139 (number (length sequence))
141 (file-name-coding-system nnmail-pathname-coding-system)
143 (if (stringp (car sequence))
145 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
148 (setq article (car sequence))
149 (setq file (nnml-article-to-file article))
152 (not (file-directory-p file)))
153 (insert (format "221 %d Article retrieved.\n" article))
155 (nnheader-insert-head file)
157 (if (re-search-forward "\n\r?\n" nil t)
159 (goto-char (point-max))
162 (delete-region (point) (point-max)))
163 (setq sequence (cdr sequence))
164 (setq count (1+ count))
165 (and (numberp nnmail-large-newsgroup)
166 (> number nnmail-large-newsgroup)
168 (nnheader-message 6 "nnml: Receiving headers... %d%%"
169 (/ (* count 100) number))))
171 (and (numberp nnmail-large-newsgroup)
172 (> number nnmail-large-newsgroup)
173 (nnheader-message 6 "nnml: Receiving headers...done"))
175 (nnheader-fold-continuation-lines)
178 (deffoo nnml-open-server (server &optional defs)
179 (nnoo-change-server 'nnml server defs)
180 (when (not (file-exists-p nnml-directory))
181 (ignore-errors (make-directory nnml-directory t)))
183 ((not (file-exists-p nnml-directory))
185 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
186 ((not (file-directory-p (file-truename nnml-directory)))
188 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
190 (nnheader-report 'nnml "Opened server %s using directory %s"
191 server nnml-directory)
194 (deffoo nnml-request-regenerate (server)
195 (nnml-possibly-change-directory nil server)
196 (nnml-generate-nov-databases server)
199 (deffoo nnml-request-article (id &optional group server buffer)
200 (nnml-possibly-change-directory group server)
201 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
202 (file-name-coding-system nnmail-pathname-coding-system)
203 path gpath group-num)
205 (when (and (setq group-num (nnml-find-group-number id))
207 (assq (cdr group-num)
208 (nnheader-article-to-file-alist
210 (nnmail-group-pathname
213 (setq path (concat gpath (int-to-string (cdr group-num)))))
214 (setq path (nnml-article-to-file id)))
217 (nnheader-report 'nnml "No such article: %s" id))
218 ((not (file-exists-p path))
219 (nnheader-report 'nnml "No such file: %s" path))
220 ((file-directory-p path)
221 (nnheader-report 'nnml "File is a directory: %s" path))
222 ((not (save-excursion (let ((nnmail-file-coding-system
223 nnml-file-coding-system))
224 (nnmail-find-file path))))
225 (nnheader-report 'nnml "Couldn't read file: %s" path))
227 (nnheader-report 'nnml "Article %s retrieved" id)
228 ;; We return the article number.
229 (cons (if group-num (car group-num) group)
230 (string-to-number (file-name-nondirectory path)))))))
232 (deffoo nnml-request-group (group &optional server dont-check)
233 (let ((file-name-coding-system nnmail-pathname-coding-system))
235 ((not (nnml-possibly-change-directory group server))
236 (nnheader-report 'nnml "Invalid group (no such directory)"))
237 ((not (file-exists-p nnml-current-directory))
238 (nnheader-report 'nnml "Directory %s does not exist"
239 nnml-current-directory))
240 ((not (file-directory-p nnml-current-directory))
241 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
243 (nnheader-report 'nnml "Group %s selected" group)
246 (nnheader-re-read-dir nnml-current-directory)
247 (nnmail-activate 'nnml)
248 (let ((active (nth 1 (assoc group nnml-group-alist))))
250 (nnheader-report 'nnml "No such group: %s" group)
251 (nnheader-report 'nnml "Selected group %s" group)
252 (nnheader-insert "211 %d %d %d %s\n"
253 (max (1+ (- (cdr active) (car active))) 0)
254 (car active) (cdr active) group)))))))
256 (deffoo nnml-request-scan (&optional group server)
257 (setq nnml-article-file-alist nil)
258 (nnml-possibly-change-directory group server)
259 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
261 (deffoo nnml-close-group (group &optional server)
262 (setq nnml-article-file-alist nil)
265 (deffoo nnml-request-create-group (group &optional server args)
266 (nnml-possibly-change-directory nil server)
267 (nnmail-activate 'nnml)
269 ((assoc group nnml-group-alist)
271 ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
272 (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
273 (nnheader-report 'nnml "%s is a file"
274 (nnmail-group-pathname group nnml-directory)))
277 (push (list group (setq active (cons 1 0)))
279 (nnml-possibly-create-directory group)
280 (nnml-possibly-change-directory group server)
281 (let ((articles (nnml-directory-articles nnml-current-directory)))
283 (setcar active (apply 'min articles))
284 (setcdr active (apply 'max articles))))
285 (nnmail-save-active nnml-group-alist nnml-active-file)
288 (deffoo nnml-request-list (&optional server)
290 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
291 (file-name-coding-system nnmail-pathname-coding-system))
292 (nnmail-find-file nnml-active-file))
293 (setq nnml-group-alist (nnmail-get-active))
296 (deffoo nnml-request-newgroups (date &optional server)
297 (nnml-request-list server))
299 (deffoo nnml-request-list-newsgroups (&optional server)
301 (nnmail-find-file nnml-newsgroups-file)))
303 (deffoo nnml-request-expire-articles (articles group &optional server force)
304 (nnml-possibly-change-directory group server)
305 (let ((active-articles
306 (nnml-directory-articles nnml-current-directory))
308 article rest mod-time number)
309 (nnmail-activate 'nnml)
311 (setq active-articles (sort active-articles '<))
312 ;; Articles not listed in active-articles are already gone,
313 ;; so don't try to expire them.
314 (setq articles (gnus-sorted-intersection articles active-articles))
316 (while (and articles is-old)
317 (if (and (setq article (nnml-article-to-file
318 (setq number (pop articles))))
319 (setq mod-time (nth 5 (file-attributes article)))
320 (nnml-deletable-article-p group number)
321 (setq is-old (nnmail-expired-article-p group mod-time force
322 nnml-inhibit-expiry)))
324 ;; Allow a special target group.
325 (unless (eq nnmail-expiry-target 'delete)
327 (nnml-request-article number group server (current-buffer))
328 (let (nnml-current-directory
330 nnml-article-file-alist)
331 (nnmail-expiry-target-group nnmail-expiry-target group)))
332 ;; Maybe directory is changed during nnmail-expiry-target-group.
333 (nnml-possibly-change-directory group server))
334 (nnheader-message 5 "Deleting article %s in %s"
337 (funcall nnmail-delete-file-function article)
340 (setq active-articles (delq number active-articles))
341 (nnml-nov-delete-article group number))
343 (let ((active (nth 1 (assoc group nnml-group-alist))))
345 (setcar active (or (and active-articles
346 (apply 'min active-articles))
348 (nnmail-save-active nnml-group-alist nnml-active-file))
350 (nconc rest articles)))
352 (deffoo nnml-request-move-article
353 (article group server accept-form &optional last move-is-internal)
354 (let ((buf (get-buffer-create " *nnml move*"))
356 (nnml-possibly-change-directory group server)
357 (nnml-update-file-alist)
359 (nnml-deletable-article-p group article)
360 (nnml-request-article article group server)
361 (let (nnml-current-directory
363 nnml-article-file-alist)
366 (insert-buffer-substring nntp-server-buffer)
367 (setq result (eval accept-form))
368 (kill-buffer (current-buffer))
371 (nnml-possibly-change-directory group server)
373 (funcall nnmail-delete-file-function
374 (nnml-article-to-file article))
376 (nnml-nov-delete-article group article)
379 (nnmail-save-active nnml-group-alist nnml-active-file))))
382 (deffoo nnml-request-accept-article (group &optional server last)
383 (nnml-possibly-change-directory group server)
384 (nnmail-check-syntax)
386 (when nnmail-cache-accepted-message-ids
387 (nnmail-cache-insert (nnmail-fetch-field "message-id")
389 (nnmail-fetch-field "subject")
390 (nnmail-fetch-field "from")))
393 (nnmail-activate 'nnml)
394 (setq result (car (nnml-save-mail
395 (list (cons group (nnml-active-number group))))))
397 (nnmail-save-active nnml-group-alist nnml-active-file)
398 (and last (nnml-save-nov))))
400 (nnmail-activate 'nnml)
401 (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
402 (yes-or-no-p "Moved to `junk' group; delete article? "))
404 (setq result (car (nnml-save-mail result))))
406 (nnmail-save-active nnml-group-alist nnml-active-file)
407 (when nnmail-cache-accepted-message-ids
408 (nnmail-cache-close))
412 (deffoo nnml-request-post (&optional server)
413 (nnmail-do-request-post 'nnml-request-accept-article server))
415 (deffoo nnml-request-replace-article (article group buffer)
416 (nnml-possibly-change-directory group)
419 (nnml-possibly-create-directory group)
420 (let ((chars (nnmail-insert-lines))
421 (art (concat (int-to-string article) "\t"))
425 (point-min) (point-max)
426 (or (nnml-article-to-file article)
427 (expand-file-name (int-to-string article)
428 nnml-current-directory))
429 nil (if (nnheader-be-verbose 5) nil 'nomesg))
431 (setq headers (nnml-parse-head chars article))
432 ;; Replace the NOV line in the NOV file.
434 (set-buffer (nnml-open-nov group))
435 (goto-char (point-min))
436 (if (or (looking-at art)
437 (search-forward (concat "\n" art) nil t))
438 ;; Delete the old NOV line.
440 ;; The line isn't here, so we have to find out where
441 ;; we should insert it. (This situation should never
442 ;; occur, but one likes to make sure...)
443 (while (and (looking-at "[0-9]+\t")
446 (match-beginning 0) (match-end 0)))
448 (zerop (forward-line 1)))))
450 (nnheader-insert-nov headers)
454 (deffoo nnml-request-delete-group (group &optional force server)
455 (nnml-possibly-change-directory group server)
457 ;; Delete all articles in GROUP.
460 nnml-current-directory t
461 (concat nnheader-numerical-short-files
462 "\\|" (regexp-quote nnml-nov-file-name) "$"
463 "\\|" (regexp-quote nnml-marks-file-name) "$"))))
464 (dolist (article articles)
465 (when (file-writable-p article)
466 (nnheader-message 5 "Deleting article %s in %s..." article group)
467 (funcall nnmail-delete-file-function article))))
468 ;; Try to delete the directory itself.
469 (ignore-errors (delete-directory nnml-current-directory)))
470 ;; Remove the group from all structures.
471 (setq nnml-group-alist
472 (delq (assoc group nnml-group-alist) nnml-group-alist)
473 nnml-current-group nil
474 nnml-current-directory nil)
475 ;; Save the active file.
476 (nnmail-save-active nnml-group-alist nnml-active-file)
479 (deffoo nnml-request-rename-group (group new-name &optional server)
480 (nnml-possibly-change-directory group server)
481 (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
482 (old-dir (nnmail-group-pathname group nnml-directory)))
484 (make-directory new-dir t)
486 ;; We move the articles file by file instead of renaming
487 ;; the directory -- there may be subgroups in this group.
488 ;; One might be more clever, I guess.
489 (dolist (file (nnheader-article-to-file-alist old-dir))
491 (concat old-dir (cdr file))
492 (concat new-dir (cdr file))))
493 ;; Move .overview file.
494 (let ((overview (concat old-dir nnml-nov-file-name)))
495 (when (file-exists-p overview)
496 (rename-file overview (concat new-dir nnml-nov-file-name))))
498 (let ((marks (concat old-dir nnml-marks-file-name)))
499 (when (file-exists-p marks)
500 (rename-file marks (concat new-dir nnml-marks-file-name))))
501 (when (<= (length (directory-files old-dir)) 2)
502 (ignore-errors (delete-directory old-dir)))
503 ;; That went ok, so we change the internal structures.
504 (let ((entry (assoc group nnml-group-alist)))
506 (setcar entry new-name))
507 (setq nnml-current-directory nil
508 nnml-current-group nil)
509 ;; Save the new group alist.
510 (nnmail-save-active nnml-group-alist nnml-active-file)
513 (deffoo nnml-set-status (article name value &optional group server)
514 (nnml-possibly-change-directory group server)
515 (let ((file (nnml-article-to-file article)))
517 ((not (file-exists-p file))
518 (nnheader-report 'nnml "File %s does not exist" file))
521 (nnheader-insert-file-contents file)
522 (nnmail-replace-status name value))
526 ;;; Internal functions.
528 (defun nnml-article-to-file (article)
529 (nnml-update-file-alist)
532 (if nnml-use-compressed-files
533 (cdr (assq article nnml-article-file-alist))
534 (number-to-string article)))
535 (expand-file-name file nnml-current-directory)
536 (when (not nnheader-directory-files-is-safe)
537 ;; Just to make sure nothing went wrong when reading over NFS --
540 (setq file (expand-file-name (number-to-string article)
541 nnml-current-directory)))
542 (nnml-update-file-alist t)
545 (defun nnml-deletable-article-p (group article)
546 "Say whether ARTICLE in GROUP can be deleted."
548 (when (setq path (nnml-article-to-file article))
549 (when (file-writable-p path)
550 (or (not nnmail-keep-last-article)
551 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
554 ;; Find an article number in the current group given the Message-ID.
555 (defun nnml-find-group-number (id)
557 (set-buffer (get-buffer-create " *nnml id*"))
558 (let ((alist nnml-group-alist)
560 ;; We want to look through all .overview files, but we want to
561 ;; start with the one in the current directory. It seems most
562 ;; likely that the article we are looking for is in that group.
563 (if (setq number (nnml-find-id nnml-current-group id))
564 (cons nnml-current-group number)
565 ;; It wasn't there, so we look through the other groups as well.
566 (while (and (not number)
568 (or (string= (caar alist) nnml-current-group)
569 (setq number (nnml-find-id (caar alist) id)))
571 (setq alist (cdr alist))))
573 (cons (caar alist) number))))))
575 (defun nnml-find-id (group id)
577 (let ((nov (expand-file-name nnml-nov-file-name
578 (nnmail-group-pathname group nnml-directory)))
580 (when (file-exists-p nov)
581 (nnheader-insert-file-contents nov)
582 (while (and (not found)
583 (search-forward id nil t)) ; We find the ID.
584 ;; And the id is in the fourth field.
585 (if (not (and (search-backward "\t" nil t 4)
586 (not (search-backward "\t" (point-at-bol) t))))
590 ;; We return the article number.
592 (ignore-errors (read (current-buffer))))))
595 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
596 (if (or gnus-nov-is-evil nnml-nov-is-evil)
598 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
599 (when (file-exists-p nov)
601 (set-buffer nntp-server-buffer)
603 (nnheader-insert-file-contents nov)
605 (not (numberp fetch-old)))
606 t ; Don't remove anything.
607 (nnheader-nov-delete-outside-range
608 (if fetch-old (max 1 (- (car articles) fetch-old))
610 (car (last articles)))
613 (defun nnml-possibly-change-directory (group &optional server)
615 (not (nnml-server-opened server)))
616 (nnml-open-server server))
619 (let ((pathname (nnmail-group-pathname group nnml-directory))
620 (file-name-coding-system nnmail-pathname-coding-system))
621 (when (not (equal pathname nnml-current-directory))
622 (setq nnml-current-directory pathname
623 nnml-current-group group
624 nnml-article-file-alist nil))
625 (file-exists-p nnml-current-directory))))
627 (defun nnml-possibly-create-directory (group)
628 (let ((dir (nnmail-group-pathname group nnml-directory)))
629 (unless (file-exists-p dir)
630 (make-directory (directory-file-name dir) t)
631 (nnheader-message 5 "Creating mail directory %s" dir))))
633 (defun nnml-save-mail (group-art)
634 "Called narrowed to an article."
635 (let (chars headers extension)
636 (setq chars (nnmail-insert-lines))
638 (and nnml-use-compressed-files
639 (> chars nnml-compressed-files-size-threshold)
640 (if (stringp nnml-use-compressed-files)
641 nnml-use-compressed-files
643 (nnmail-insert-xref group-art)
644 (run-hooks 'nnmail-prepare-save-mail-hook)
645 (run-hooks 'nnml-prepare-save-mail-hook)
646 (goto-char (point-min))
647 (while (looking-at "From ")
648 (replace-match "X-From-Line: ")
650 ;; We save the article in all the groups it belongs in.
654 (nnml-possibly-create-directory (caar ga))
655 (let ((file (concat (nnmail-group-pathname
656 (caar ga) nnml-directory)
657 (int-to-string (cdar ga))
660 ;; It was already saved, so we just make a hard link.
661 (funcall nnmail-crosspost-link-function first file t)
663 (nnmail-write-region (point-min) (point-max) file nil
664 (if (nnheader-be-verbose 5) nil 'nomesg))
667 ;; Generate a nov line for this article. We generate the nov
668 ;; line after saving, because nov generation destroys the
670 (setq headers (nnml-parse-head chars))
671 ;; Output the nov line to all nov databases that should have it.
672 (let ((ga group-art))
674 (nnml-add-nov (caar ga) (cdar ga) headers)
678 (defun nnml-active-number (group)
679 "Compute the next article number in GROUP."
680 (let ((active (cadr (assoc group nnml-group-alist))))
681 ;; The group wasn't known to nnml, so we just create an active
684 ;; Perhaps the active file was corrupt? See whether
685 ;; there are any articles in this group.
686 (nnml-possibly-create-directory group)
687 (nnml-possibly-change-directory group)
688 (unless nnml-article-file-alist
689 (setq nnml-article-file-alist
691 (nnml-current-group-article-to-file-alist)
692 'car-less-than-car)))
694 (if nnml-article-file-alist
695 (cons (caar nnml-article-file-alist)
696 (caar (last nnml-article-file-alist)))
698 (push (list group active) nnml-group-alist))
699 (setcdr active (1+ (cdr active)))
700 (while (file-exists-p
701 (expand-file-name (int-to-string (cdr active))
702 (nnmail-group-pathname group nnml-directory)))
703 (setcdr active (1+ (cdr active))))
706 (defun nnml-add-nov (group article headers)
707 "Add a nov line for the GROUP base."
709 (set-buffer (nnml-open-nov group))
710 (goto-char (point-max))
711 (mail-header-set-number headers article)
712 (nnheader-insert-nov headers)))
714 (defsubst nnml-header-value ()
715 (buffer-substring (match-end 0) (point-at-eol)))
717 (defun nnml-parse-head (chars &optional number)
718 "Parse the head of the current buffer."
721 (unless (zerop (buffer-size))
723 (goto-char (point-min))
724 (if (re-search-forward "\n\r?\n" nil t)
727 (let ((headers (nnheader-parse-naked-head)))
728 (mail-header-set-chars headers chars)
729 (mail-header-set-number headers number)
732 (defun nnml-get-nov-buffer (group)
733 (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
736 (set (make-local-variable 'nnml-nov-buffer-file-name)
739 (nnmail-group-pathname group nnml-directory)))
741 (when (file-exists-p nnml-nov-buffer-file-name)
742 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
745 (defun nnml-open-nov (group)
746 (or (cdr (assoc group nnml-nov-buffer-alist))
747 (let ((buffer (nnml-get-nov-buffer group)))
748 (push (cons group buffer) nnml-nov-buffer-alist)
751 (defun nnml-save-nov ()
753 (while nnml-nov-buffer-alist
754 (when (buffer-name (cdar nnml-nov-buffer-alist))
755 (set-buffer (cdar nnml-nov-buffer-alist))
756 (when (buffer-modified-p)
757 (nnmail-write-region (point-min) (point-max)
758 nnml-nov-buffer-file-name nil 'nomesg))
759 (set-buffer-modified-p nil)
760 (kill-buffer (current-buffer)))
761 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
764 (defun nnml-generate-nov-databases (&optional server)
765 "Generate NOV databases in all nnml directories."
766 (interactive (list (or (nnoo-current-server 'nnml) "")))
767 ;; Read the active file to make sure we don't re-use articles
768 ;; numbers in empty groups.
769 (nnmail-activate 'nnml)
770 (unless (nnml-server-opened server)
771 (nnml-open-server server))
772 (setq nnml-directory (expand-file-name nnml-directory))
773 ;; Recurse down the directories.
774 (nnml-generate-nov-databases-1 nnml-directory nil t)
775 ;; Save the active file.
776 (nnmail-save-active nnml-group-alist nnml-active-file))
778 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
779 "Regenerate the NOV database in DIR."
780 (interactive "DRegenerate NOV in: ")
781 (setq dir (file-name-as-directory dir))
782 ;; Only scan this sub-tree if we haven't been here yet.
783 (unless (member (file-truename dir) seen)
784 (push (file-truename dir) seen)
785 ;; We descend recursively
786 (dolist (dir (directory-files dir t nil t))
787 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
788 (file-directory-p dir))
789 (nnml-generate-nov-databases-1 dir seen)))
790 ;; Do this directory.
791 (let ((files (sort (nnheader-article-to-file-alist dir)
792 'car-less-than-car)))
794 (let* ((group (nnheader-file-to-group
795 (directory-file-name dir) nnml-directory))
796 (info (cadr (assoc group nnml-group-alist))))
798 (setcar info (1+ (cdr info)))))
799 (funcall nnml-generate-active-function dir)
800 ;; Generate the nov file.
801 (nnml-generate-nov-file dir files)
803 (nnmail-save-active nnml-group-alist nnml-active-file))))))
805 (eval-when-compile (defvar files))
806 (defun nnml-generate-active-info (dir)
807 ;; Update the active info for this group.
808 (let* ((group (nnheader-file-to-group
809 (directory-file-name dir) nnml-directory))
810 (entry (assoc group nnml-group-alist))
811 (last (or (caadr entry) 0)))
812 (setq nnml-group-alist (delq entry nnml-group-alist))
814 (cons (or (caar files) (1+ last))
816 (or (caar (last files))
820 (defun nnml-generate-nov-file (dir files)
821 (let* ((dir (file-name-as-directory dir))
822 (nov (concat dir nnml-nov-file-name))
823 (nov-buffer (get-buffer-create " *nov*"))
826 ;; Init the nov buffer.
827 (set-buffer nov-buffer)
828 (buffer-disable-undo)
830 (set-buffer nntp-server-buffer)
831 ;; Delete the old NOV file.
832 (when (file-exists-p nov)
833 (funcall nnmail-delete-file-function nov))
835 (unless (file-directory-p (setq file (concat dir (cdar files))))
837 (nnheader-insert-file-contents file)
839 (goto-char (point-min))
841 (re-search-forward "\n\r?\n" nil t)
842 (setq chars (- (point-max) (point)))
843 (max (point-min) (1- (point)))))
844 (unless (zerop (buffer-size))
845 (goto-char (point-min))
846 (setq headers (nnml-parse-head chars (caar files)))
848 (set-buffer nov-buffer)
849 (goto-char (point-max))
850 (nnheader-insert-nov headers)))
852 (setq files (cdr files)))
854 (set-buffer nov-buffer)
855 (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
856 (kill-buffer (current-buffer))))))
858 (defun nnml-nov-delete-article (group article)
860 (set-buffer (nnml-open-nov group))
861 (when (nnheader-find-nov-line article)
862 (delete-region (point) (progn (forward-line 1) (point)))
864 (let ((active (cadr (assoc group nnml-group-alist)))
868 (setf (car active) (1+ (cdr active)))
869 (when (and (setq num (ignore-errors (read (current-buffer))))
871 (setf (car active) num)))))))
874 (defun nnml-update-file-alist (&optional force)
875 (when nnml-use-compressed-files
876 (when (or (not nnml-article-file-alist)
878 (setq nnml-article-file-alist
879 (nnml-current-group-article-to-file-alist)))))
881 (defun nnml-directory-articles (dir)
882 "Return a list of all article files in a directory.
883 Use the nov database for that directory if available."
884 (if (or gnus-nov-is-evil nnml-nov-is-evil
886 (expand-file-name nnml-nov-file-name dir))))
887 (nnheader-directory-articles dir)
888 ;; build list from .overview if available
889 ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
890 ;; defvoo'd, and we might get called when it hasn't been swapped in.
894 (buffer (nnml-get-nov-buffer nnml-current-group)))
896 (goto-char (point-min))
898 (setq art (read (current-buffer)))
903 (defun nnml-current-group-article-to-file-alist ()
904 "Return an alist of article/file pairs in the current group.
905 Use the nov database for the current group if available."
906 (if (or nnml-use-compressed-files
910 (expand-file-name nnml-nov-file-name
911 nnml-current-directory))))
912 (nnheader-article-to-file-alist nnml-current-directory)
913 ;; build list from .overview if available
916 (buffer (nnml-get-nov-buffer nnml-current-group))
919 (goto-char (point-min))
921 (setq art (read (current-buffer)))
922 ;; assume file name is unadorned (ie. not compressed etc)
923 (push (cons art (int-to-string art)) alist)
927 (deffoo nnml-request-set-mark (group actions &optional server)
928 (nnml-possibly-change-directory group server)
929 (unless nnml-marks-is-evil
930 (nnml-open-marks group server)
931 (dolist (action actions)
932 (let ((range (nth 0 action))
933 (what (nth 1 action))
934 (marks (nth 2 action)))
935 (assert (or (eq what 'add) (eq what 'del)) nil
936 "Unknown request-set-mark action: %s" what)
938 (setq nnml-marks (gnus-update-alist-soft
940 (funcall (if (eq what 'add) 'gnus-range-add
941 'gnus-remove-from-range)
942 (cdr (assoc mark nnml-marks)) range)
944 (nnml-save-marks group server))
947 (deffoo nnml-request-update-info (group info &optional server)
948 (nnml-possibly-change-directory group server)
949 (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group))
950 (nnheader-message 8 "Updating marks for %s..." group)
951 (nnml-open-marks group server)
952 ;; Update info using `nnml-marks'.
954 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
957 (gnus-update-alist-soft
959 (cdr (assq (cdr pred) nnml-marks))
960 (gnus-info-marks info))
962 gnus-article-mark-lists)
963 (let ((seen (cdr (assq 'read nnml-marks))))
964 (gnus-info-set-read info
965 (if (and (integerp (car seen))
967 (list (cons (car seen) (car seen)))
969 (nnheader-message 8 "Updating marks for %s...done" group))
972 (defun nnml-marks-changed-p (group)
973 (let ((file (expand-file-name nnml-marks-file-name
974 (nnmail-group-pathname group nnml-directory))))
975 (if (null (gnus-gethash file nnml-marks-modtime))
976 t ;; never looked at marks file, assume it has changed
977 (not (equal (gnus-gethash file nnml-marks-modtime)
978 (nth 5 (file-attributes file)))))))
980 (defun nnml-save-marks (group server)
981 (let ((file-name-coding-system nnmail-pathname-coding-system)
982 (file (expand-file-name nnml-marks-file-name
983 (nnmail-group-pathname group nnml-directory))))
986 (nnml-possibly-create-directory group)
989 (gnus-prin1 nnml-marks)
992 (nth 5 (file-attributes file))
994 (error (or (gnus-yes-or-no-p
995 (format "Could not write to %s (%s). Continue? " file err))
996 (error "Cannot write to %s (%s)" file err))))))
998 (defun nnml-open-marks (group server)
999 (let ((file (expand-file-name
1000 nnml-marks-file-name
1001 (nnmail-group-pathname group nnml-directory))))
1002 (if (file-exists-p file)
1005 (gnus-sethash file (nth 5 (file-attributes file))
1007 (nnheader-insert-file-contents file)
1008 (setq nnml-marks (read (current-buffer)))
1009 (dolist (el gnus-article-unpropagated-mark-lists)
1010 (setq nnml-marks (gnus-remassoc el nnml-marks))))
1011 (error (or (gnus-yes-or-no-p
1012 (format "Error reading nnml marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1013 (error "Cannot read nnml marks file %s (%s)" file err))))
1014 ;; User didn't have a .marks file. Probably first time
1015 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1016 (let ((info (gnus-get-info
1017 (gnus-group-prefixed-name
1019 (gnus-server-to-method (format "nnml:%s" server))))))
1020 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1021 (setq nnml-marks (gnus-info-marks info))
1022 (push (cons 'read (gnus-info-read info)) nnml-marks)
1023 (dolist (el gnus-article-unpropagated-mark-lists)
1024 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1025 (nnml-save-marks group server)
1026 (nnheader-message 7 "Bootstrapping marks for %s...done" group)))))
1030 ;;; Group and server compaction. -- dvl
1033 ;; #### FIXME: this function handles self Xref: entry correctly, but I don't
1034 ;; #### know how to handle external cross-references. I actually don't know if
1035 ;; #### this is handled correctly elsewhere. For instance, what happens if you
1036 ;; #### move all articles to a new group (that's what people do for manual
1037 ;; #### compaction) ?
1039 ;; #### NOTE: the function below handles the article backlog. This is
1040 ;; #### conceptually the wrong place to do it because the backend is at a
1041 ;; #### lower level. However, this is the only place where we have the needed
1042 ;; #### information to do the job. Ideally, this function should not handle
1043 ;; #### the backlog by itself, but return a list of moved groups / articles to
1044 ;; #### the caller. This will become important to avoid code duplication when
1045 ;; #### other backends get a compaction feature. Also, note that invalidating
1046 ;; #### the "original article buffer" is already done at an upper level.
1048 (defun nnml-request-compact-group (group &optional server save)
1049 (nnml-possibly-change-directory group server)
1050 (unless nnml-article-file-alist
1051 (setq nnml-article-file-alist
1052 (sort (nnml-current-group-article-to-file-alist)
1053 'car-less-than-car)))
1054 (if (not nnml-article-file-alist)
1055 ;; The group is empty: do nothing but return t
1057 ;; The group is not empty:
1058 (let* ((group-full-name
1059 (gnus-group-prefixed-name
1061 (gnus-server-to-method (format "nnml:%s" server))))
1062 (info (gnus-get-info group-full-name))
1065 (let ((articles nnml-article-file-alist)
1067 (while (setq article (pop articles))
1068 (let ((old-number (car article)))
1069 (when (> old-number new-number)
1070 ;; There is a gap here:
1071 (let ((old-number-string (int-to-string old-number))
1072 (new-number-string (int-to-string new-number)))
1074 ;; #### NOTE: `nnml-article-to-file' calls
1075 ;; #### `nnml-update-file-alist' (which in turn calls
1076 ;; #### `nnml-current-group-article-to-file-alist', which
1077 ;; #### might use the NOV database). This might turn out to be
1078 ;; #### inefficient. In that case, we will do the work
1080 ;; 1/ Move the article to a new file:
1081 (let* ((oldfile (nnml-article-to-file old-number))
1083 (gnus-replace-in-string
1084 oldfile (concat "\\("
1086 "\\)\\(\\(\\.gz\\)?\\)$")
1087 (concat new-number-string "\\2"))))
1088 (with-current-buffer nntp-server-buffer
1089 (nnmail-find-file oldfile)
1090 ;; Update the Xref header in the article itself:
1091 (when (and (re-search-forward "^Xref: [^ ]+ " nil t)
1095 (concat group ":" old-number-string))
1099 (concat group ":" new-number-string)))
1100 ;; Save to the new file:
1101 (nnmail-write-region (point-min) (point-max) newfile))
1102 (funcall nnmail-delete-file-function oldfile))
1103 ;; 2/ Update all marks for this article:
1104 ;; #### NOTE: it is possible that the new article number
1105 ;; #### already belongs to a range, whereas the corresponding
1106 ;; #### article doesn't exist (for example, if you delete an
1107 ;; #### article). For that reason, it is important to update
1108 ;; #### the ranges (meaning remove inexistant articles) before
1109 ;; #### doing anything on them.
1110 ;; 2 a/ read articles:
1111 (let ((read (gnus-info-read info)))
1112 (setq read (gnus-remove-from-range read (list new-number)))
1113 (when (gnus-member-of-range old-number read)
1114 (setq read (gnus-remove-from-range read (list old-number)))
1115 (setq read (gnus-add-to-range read (list new-number))))
1116 (gnus-info-set-read info read))
1117 ;; 2 b/ marked articles:
1118 (let ((oldmarks (gnus-info-marks info))
1120 (while (setq mark (pop oldmarks))
1121 (setcdr mark (gnus-remove-from-range (cdr mark)
1123 (when (gnus-member-of-range old-number (cdr mark))
1124 (setcdr mark (gnus-remove-from-range (cdr mark)
1126 (setcdr mark (gnus-add-to-range (cdr mark)
1127 (list new-number))))
1128 (push mark newmarks))
1129 (gnus-info-set-marks info newmarks))
1130 ;; 3/ Update the NOV entry for this article:
1131 (unless nnml-nov-is-evil
1133 (set-buffer (nnml-open-nov group))
1134 (when (nnheader-find-nov-line old-number)
1135 ;; Replace the article number:
1136 (looking-at old-number-string)
1137 (replace-match new-number-string nil t)
1138 ;; Update the Xref header:
1139 (when (re-search-forward
1140 (concat "\\(Xref:[^\t\n]* \\)\\<"
1142 (concat group ":" old-number-string))
1146 (concat "\\1" group ":" new-number-string))))))
1147 ;; 4/ Possibly remove the article from the backlog:
1148 (when gnus-keep-backlog
1149 ;; #### NOTE: instead of removing the article, we could
1150 ;; #### modify the backlog to reflect the numbering change,
1151 ;; #### but I don't think it's worth it.
1152 (gnus-backlog-remove-article group-full-name old-number)
1153 (gnus-backlog-remove-article group-full-name new-number))))
1154 (setq new-number (1+ new-number)))))
1156 ;; No compaction had to be done:
1158 ;; Some articles have actually been renamed:
1159 ;; 1/ Rebuild active information:
1160 (let ((entry (assoc group nnml-group-alist))
1161 (active (cons 1 (1- new-number))))
1162 (setq nnml-group-alist (delq entry nnml-group-alist))
1163 (push (list group active) nnml-group-alist)
1164 ;; Update the active hashtable to let the *Group* buffer display
1165 ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1166 ;; gnus-newwrc-alist are out of date, since all we did is to modify
1167 ;; the info of the group internally.
1168 (gnus-set-active group-full-name active))
1170 ;; #### NOTE: normally, we should save the overview (NOV) file
1171 ;; #### here, just like we save the marks file. However, there is no
1172 ;; #### such function as nnml-save-nov for a single group. Only for
1173 ;; #### all groups. Gnus inconsistency is getting worse every day...
1174 ;; 2/ Rebuild marks file:
1175 (unless nnml-marks-is-evil
1176 ;; #### NOTE: this constant use of global variables everywhere is
1177 ;; #### truly disgusting. Gnus really needs a *major* cleanup.
1178 (setq nnml-marks (gnus-info-marks info))
1179 (push (cons 'read (gnus-info-read info)) nnml-marks)
1180 (dolist (el gnus-article-unpropagated-mark-lists)
1181 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1182 (nnml-save-marks group server))
1183 ;; 3/ Save everything if this was not part of a bigger operation:
1185 ;; Nothing to save (yet):
1187 ;; Something to save:
1188 ;; a/ Save the NOV databases:
1189 ;; #### NOTE: this should be done directory per directory in 1bis
1190 ;; #### above. See comment there.
1192 ;; b/ Save the active file:
1193 (nnmail-save-active nnml-group-alist nnml-active-file)
1196 (defun nnml-request-compact (&optional server)
1197 "Request compaction of all SERVER nnml groups."
1198 (interactive (list (or (nnoo-current-server 'nnml) "")))
1199 (nnmail-activate 'nnml)
1200 (unless (nnml-server-opened server)
1201 (nnml-open-server server))
1202 (setq nnml-directory (expand-file-name nnml-directory))
1203 (let* ((groups (gnus-groups-from-server
1204 (gnus-server-to-method (format "nnml:%s" server))))
1205 (first (pop groups))
1208 (while (setq group (pop groups))
1209 (nnml-request-compact-group (gnus-group-real-name group) server))
1210 (nnml-request-compact-group (gnus-group-real-name first) server t))))
1215 ;;; nnml.el ends here