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))
46 (autoload 'gnus-article-unpropagatable-p "gnus-sum"))
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 (defvoo nnml-compressed-files-size-threshold 1000
92 "Default size threshold for compressed message files.
93 Message files with bodies larger than that many characters will
94 be automatically compressed if `nnml-use-compressed-files' is
99 (defconst nnml-version "nnml 1.0"
102 (defvoo nnml-nov-file-name ".overview")
103 (defvoo nnml-marks-file-name ".marks")
105 (defvoo nnml-current-directory nil)
106 (defvoo nnml-current-group nil)
107 (defvoo nnml-status-string "")
108 (defvoo nnml-nov-buffer-alist nil)
109 (defvoo nnml-group-alist nil)
110 (defvoo nnml-active-timestamp nil)
111 (defvoo nnml-article-file-alist nil)
113 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
115 (defvar nnml-nov-buffer-file-name nil)
117 (defvoo nnml-file-coding-system nnmail-file-coding-system)
119 (defvoo nnml-marks nil)
121 (defvar nnml-marks-modtime (gnus-make-hashtable))
124 ;;; Interface functions.
126 (nnoo-define-basics nnml)
128 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
129 (when (nnml-possibly-change-directory group server)
131 (set-buffer nntp-server-buffer)
134 (number (length sequence))
136 (file-name-coding-system nnmail-pathname-coding-system)
138 (if (stringp (car sequence))
140 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
143 (setq article (car sequence))
144 (setq file (nnml-article-to-file article))
147 (not (file-directory-p file)))
148 (insert (format "221 %d Article retrieved.\n" article))
150 (nnheader-insert-head file)
152 (if (re-search-forward "\n\r?\n" nil t)
154 (goto-char (point-max))
157 (delete-region (point) (point-max)))
158 (setq sequence (cdr sequence))
159 (setq count (1+ count))
160 (and (numberp nnmail-large-newsgroup)
161 (> number nnmail-large-newsgroup)
163 (nnheader-message 6 "nnml: Receiving headers... %d%%"
164 (/ (* count 100) number))))
166 (and (numberp nnmail-large-newsgroup)
167 (> number nnmail-large-newsgroup)
168 (nnheader-message 6 "nnml: Receiving headers...done"))
170 (nnheader-fold-continuation-lines)
173 (deffoo nnml-open-server (server &optional defs)
174 (nnoo-change-server 'nnml server defs)
175 (when (not (file-exists-p nnml-directory))
176 (ignore-errors (make-directory nnml-directory t)))
178 ((not (file-exists-p nnml-directory))
180 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
181 ((not (file-directory-p (file-truename nnml-directory)))
183 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
185 (nnheader-report 'nnml "Opened server %s using directory %s"
186 server nnml-directory)
189 (deffoo nnml-request-regenerate (server)
190 (nnml-possibly-change-directory nil server)
191 (nnml-generate-nov-databases server)
194 (deffoo nnml-request-article (id &optional group server buffer)
195 (nnml-possibly-change-directory group server)
196 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
197 (file-name-coding-system nnmail-pathname-coding-system)
198 path gpath group-num)
200 (when (and (setq group-num (nnml-find-group-number id))
202 (assq (cdr group-num)
203 (nnheader-article-to-file-alist
205 (nnmail-group-pathname
208 (setq path (concat gpath (int-to-string (cdr group-num)))))
209 (setq path (nnml-article-to-file id)))
212 (nnheader-report 'nnml "No such article: %s" id))
213 ((not (file-exists-p path))
214 (nnheader-report 'nnml "No such file: %s" path))
215 ((file-directory-p path)
216 (nnheader-report 'nnml "File is a directory: %s" path))
217 ((not (save-excursion (let ((nnmail-file-coding-system
218 nnml-file-coding-system))
219 (nnmail-find-file path))))
220 (nnheader-report 'nnml "Couldn't read file: %s" path))
222 (nnheader-report 'nnml "Article %s retrieved" id)
223 ;; We return the article number.
224 (cons (if group-num (car group-num) group)
225 (string-to-number (file-name-nondirectory path)))))))
227 (deffoo nnml-request-group (group &optional server dont-check)
228 (let ((file-name-coding-system nnmail-pathname-coding-system))
230 ((not (nnml-possibly-change-directory group server))
231 (nnheader-report 'nnml "Invalid group (no such directory)"))
232 ((not (file-exists-p nnml-current-directory))
233 (nnheader-report 'nnml "Directory %s does not exist"
234 nnml-current-directory))
235 ((not (file-directory-p nnml-current-directory))
236 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
238 (nnheader-report 'nnml "Group %s selected" group)
241 (nnheader-re-read-dir nnml-current-directory)
242 (nnmail-activate 'nnml)
243 (let ((active (nth 1 (assoc group nnml-group-alist))))
245 (nnheader-report 'nnml "No such group: %s" group)
246 (nnheader-report 'nnml "Selected group %s" group)
247 (nnheader-insert "211 %d %d %d %s\n"
248 (max (1+ (- (cdr active) (car active))) 0)
249 (car active) (cdr active) group)))))))
251 (deffoo nnml-request-scan (&optional group server)
252 (setq nnml-article-file-alist nil)
253 (nnml-possibly-change-directory group server)
254 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
256 (deffoo nnml-close-group (group &optional server)
257 (setq nnml-article-file-alist nil)
260 (deffoo nnml-request-create-group (group &optional server args)
261 (nnml-possibly-change-directory nil server)
262 (nnmail-activate 'nnml)
264 ((assoc group nnml-group-alist)
266 ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
267 (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
268 (nnheader-report 'nnml "%s is a file"
269 (nnmail-group-pathname group nnml-directory)))
272 (push (list group (setq active (cons 1 0)))
274 (nnml-possibly-create-directory group)
275 (nnml-possibly-change-directory group server)
276 (let ((articles (nnml-directory-articles nnml-current-directory)))
278 (setcar active (apply 'min articles))
279 (setcdr active (apply 'max articles))))
280 (nnmail-save-active nnml-group-alist nnml-active-file)
283 (deffoo nnml-request-list (&optional server)
285 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
286 (file-name-coding-system nnmail-pathname-coding-system))
287 (nnmail-find-file nnml-active-file))
288 (setq nnml-group-alist (nnmail-get-active))
291 (deffoo nnml-request-newgroups (date &optional server)
292 (nnml-request-list server))
294 (deffoo nnml-request-list-newsgroups (&optional server)
296 (nnmail-find-file nnml-newsgroups-file)))
298 (deffoo nnml-request-expire-articles (articles group &optional server force)
299 (nnml-possibly-change-directory group server)
300 (let ((active-articles
301 (nnml-directory-articles nnml-current-directory))
303 article rest mod-time number)
304 (nnmail-activate 'nnml)
306 (setq active-articles (sort active-articles '<))
307 ;; Articles not listed in active-articles are already gone,
308 ;; so don't try to expire them.
309 (setq articles (gnus-sorted-intersection articles active-articles))
311 (while (and articles is-old)
312 (if (and (setq article (nnml-article-to-file
313 (setq number (pop articles))))
314 (setq mod-time (nth 5 (file-attributes article)))
315 (nnml-deletable-article-p group number)
316 (setq is-old (nnmail-expired-article-p group mod-time force
317 nnml-inhibit-expiry)))
319 ;; Allow a special target group.
320 (unless (eq nnmail-expiry-target 'delete)
322 (nnml-request-article number group server (current-buffer))
323 (let (nnml-current-directory
325 nnml-article-file-alist)
326 (nnmail-expiry-target-group nnmail-expiry-target group)))
327 ;; Maybe directory is changed during nnmail-expiry-target-group.
328 (nnml-possibly-change-directory group server))
329 (nnheader-message 5 "Deleting article %s in %s"
332 (funcall nnmail-delete-file-function article)
335 (setq active-articles (delq number active-articles))
336 (nnml-nov-delete-article group number))
338 (let ((active (nth 1 (assoc group nnml-group-alist))))
340 (setcar active (or (and active-articles
341 (apply 'min active-articles))
343 (nnmail-save-active nnml-group-alist nnml-active-file))
345 (nconc rest articles)))
347 (deffoo nnml-request-move-article
348 (article group server accept-form &optional last move-is-internal)
349 (let ((buf (get-buffer-create " *nnml move*"))
351 (nnml-possibly-change-directory group server)
352 (nnml-update-file-alist)
354 (nnml-deletable-article-p group article)
355 (nnml-request-article article group server)
356 (let (nnml-current-directory
358 nnml-article-file-alist)
361 (insert-buffer-substring nntp-server-buffer)
362 (setq result (eval accept-form))
363 (kill-buffer (current-buffer))
366 (nnml-possibly-change-directory group server)
368 (funcall nnmail-delete-file-function
369 (nnml-article-to-file article))
371 (nnml-nov-delete-article group article)
374 (nnmail-save-active nnml-group-alist nnml-active-file))))
377 (deffoo nnml-request-accept-article (group &optional server last)
378 (nnml-possibly-change-directory group server)
379 (nnmail-check-syntax)
381 (when nnmail-cache-accepted-message-ids
382 (nnmail-cache-insert (nnmail-fetch-field "message-id")
384 (nnmail-fetch-field "subject")
385 (nnmail-fetch-field "from")))
388 (nnmail-activate 'nnml)
389 (setq result (car (nnml-save-mail
390 (list (cons group (nnml-active-number group))))))
392 (nnmail-save-active nnml-group-alist nnml-active-file)
393 (and last (nnml-save-nov))))
395 (nnmail-activate 'nnml)
396 (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
397 (yes-or-no-p "Moved to `junk' group; delete article? "))
399 (setq result (car (nnml-save-mail result))))
401 (nnmail-save-active nnml-group-alist nnml-active-file)
402 (when nnmail-cache-accepted-message-ids
403 (nnmail-cache-close))
407 (deffoo nnml-request-post (&optional server)
408 (nnmail-do-request-post 'nnml-request-accept-article server))
410 (deffoo nnml-request-replace-article (article group buffer)
411 (nnml-possibly-change-directory group)
414 (nnml-possibly-create-directory group)
415 (let ((chars (nnmail-insert-lines))
416 (art (concat (int-to-string article) "\t"))
420 (point-min) (point-max)
421 (or (nnml-article-to-file article)
422 (expand-file-name (int-to-string article)
423 nnml-current-directory))
424 nil (if (nnheader-be-verbose 5) nil 'nomesg))
426 (setq headers (nnml-parse-head chars article))
427 ;; Replace the NOV line in the NOV file.
429 (set-buffer (nnml-open-nov group))
430 (goto-char (point-min))
431 (if (or (looking-at art)
432 (search-forward (concat "\n" art) nil t))
433 ;; Delete the old NOV line.
435 ;; The line isn't here, so we have to find out where
436 ;; we should insert it. (This situation should never
437 ;; occur, but one likes to make sure...)
438 (while (and (looking-at "[0-9]+\t")
441 (match-beginning 0) (match-end 0)))
443 (zerop (forward-line 1)))))
445 (nnheader-insert-nov headers)
449 (deffoo nnml-request-delete-group (group &optional force server)
450 (nnml-possibly-change-directory group server)
452 ;; Delete all articles in GROUP.
455 nnml-current-directory t
456 (concat nnheader-numerical-short-files
457 "\\|" (regexp-quote nnml-nov-file-name) "$"
458 "\\|" (regexp-quote nnml-marks-file-name) "$"))))
459 (dolist (article articles)
460 (when (file-writable-p article)
461 (nnheader-message 5 "Deleting article %s in %s..." article group)
462 (funcall nnmail-delete-file-function article))))
463 ;; Try to delete the directory itself.
464 (ignore-errors (delete-directory nnml-current-directory)))
465 ;; Remove the group from all structures.
466 (setq nnml-group-alist
467 (delq (assoc group nnml-group-alist) nnml-group-alist)
468 nnml-current-group nil
469 nnml-current-directory nil)
470 ;; Save the active file.
471 (nnmail-save-active nnml-group-alist nnml-active-file)
474 (deffoo nnml-request-rename-group (group new-name &optional server)
475 (nnml-possibly-change-directory group server)
476 (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
477 (old-dir (nnmail-group-pathname group nnml-directory)))
479 (make-directory new-dir t)
481 ;; We move the articles file by file instead of renaming
482 ;; the directory -- there may be subgroups in this group.
483 ;; One might be more clever, I guess.
484 (dolist (file (nnheader-article-to-file-alist old-dir))
486 (concat old-dir (cdr file))
487 (concat new-dir (cdr file))))
488 ;; Move .overview file.
489 (let ((overview (concat old-dir nnml-nov-file-name)))
490 (when (file-exists-p overview)
491 (rename-file overview (concat new-dir nnml-nov-file-name))))
493 (let ((marks (concat old-dir nnml-marks-file-name)))
494 (when (file-exists-p marks)
495 (rename-file marks (concat new-dir nnml-marks-file-name))))
496 (when (<= (length (directory-files old-dir)) 2)
497 (ignore-errors (delete-directory old-dir)))
498 ;; That went ok, so we change the internal structures.
499 (let ((entry (assoc group nnml-group-alist)))
501 (setcar entry new-name))
502 (setq nnml-current-directory nil
503 nnml-current-group nil)
504 ;; Save the new group alist.
505 (nnmail-save-active nnml-group-alist nnml-active-file)
508 (deffoo nnml-set-status (article name value &optional group server)
509 (nnml-possibly-change-directory group server)
510 (let ((file (nnml-article-to-file article)))
512 ((not (file-exists-p file))
513 (nnheader-report 'nnml "File %s does not exist" file))
516 (nnheader-insert-file-contents file)
517 (nnmail-replace-status name value))
521 ;;; Internal functions.
523 (defun nnml-article-to-file (article)
524 (nnml-update-file-alist)
527 (if nnml-use-compressed-files
528 (cdr (assq article nnml-article-file-alist))
529 (number-to-string article)))
530 (expand-file-name file nnml-current-directory)
531 (when (not nnheader-directory-files-is-safe)
532 ;; Just to make sure nothing went wrong when reading over NFS --
535 (setq file (expand-file-name (number-to-string article)
536 nnml-current-directory)))
537 (nnml-update-file-alist t)
540 (defun nnml-deletable-article-p (group article)
541 "Say whether ARTICLE in GROUP can be deleted."
543 (when (setq path (nnml-article-to-file article))
544 (when (file-writable-p path)
545 (or (not nnmail-keep-last-article)
546 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
549 ;; Find an article number in the current group given the Message-ID.
550 (defun nnml-find-group-number (id)
552 (set-buffer (get-buffer-create " *nnml id*"))
553 (let ((alist nnml-group-alist)
555 ;; We want to look through all .overview files, but we want to
556 ;; start with the one in the current directory. It seems most
557 ;; likely that the article we are looking for is in that group.
558 (if (setq number (nnml-find-id nnml-current-group id))
559 (cons nnml-current-group number)
560 ;; It wasn't there, so we look through the other groups as well.
561 (while (and (not number)
563 (or (string= (caar alist) nnml-current-group)
564 (setq number (nnml-find-id (caar alist) id)))
566 (setq alist (cdr alist))))
568 (cons (caar alist) number))))))
570 (defun nnml-find-id (group id)
572 (let ((nov (expand-file-name nnml-nov-file-name
573 (nnmail-group-pathname group nnml-directory)))
575 (when (file-exists-p nov)
576 (nnheader-insert-file-contents nov)
577 (while (and (not found)
578 (search-forward id nil t)) ; We find the ID.
579 ;; And the id is in the fourth field.
580 (if (not (and (search-backward "\t" nil t 4)
581 (not (search-backward "\t" (point-at-bol) t))))
585 ;; We return the article number.
587 (ignore-errors (read (current-buffer))))))
590 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
591 (if (or gnus-nov-is-evil nnml-nov-is-evil)
593 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
594 (when (file-exists-p nov)
596 (set-buffer nntp-server-buffer)
598 (nnheader-insert-file-contents nov)
600 (not (numberp fetch-old)))
601 t ; Don't remove anything.
602 (nnheader-nov-delete-outside-range
603 (if fetch-old (max 1 (- (car articles) fetch-old))
605 (car (last articles)))
608 (defun nnml-possibly-change-directory (group &optional server)
610 (not (nnml-server-opened server)))
611 (nnml-open-server server))
614 (let ((pathname (nnmail-group-pathname group nnml-directory))
615 (file-name-coding-system nnmail-pathname-coding-system))
616 (when (not (equal pathname nnml-current-directory))
617 (setq nnml-current-directory pathname
618 nnml-current-group group
619 nnml-article-file-alist nil))
620 (file-exists-p nnml-current-directory))))
622 (defun nnml-possibly-create-directory (group)
623 (let ((dir (nnmail-group-pathname group nnml-directory)))
624 (unless (file-exists-p dir)
625 (make-directory (directory-file-name dir) t)
626 (nnheader-message 5 "Creating mail directory %s" dir))))
628 (defun nnml-save-mail (group-art)
629 "Called narrowed to an article."
630 (let (chars headers extension)
631 (setq chars (nnmail-insert-lines))
633 (and nnml-use-compressed-files
634 (> chars nnml-compressed-files-size-threshold)
636 (nnmail-insert-xref group-art)
637 (run-hooks 'nnmail-prepare-save-mail-hook)
638 (run-hooks 'nnml-prepare-save-mail-hook)
639 (goto-char (point-min))
640 (while (looking-at "From ")
641 (replace-match "X-From-Line: ")
643 ;; We save the article in all the groups it belongs in.
647 (nnml-possibly-create-directory (caar ga))
648 (let ((file (concat (nnmail-group-pathname
649 (caar ga) nnml-directory)
650 (int-to-string (cdar ga))
653 ;; It was already saved, so we just make a hard link.
654 (funcall nnmail-crosspost-link-function first file t)
656 (nnmail-write-region (point-min) (point-max) file nil
657 (if (nnheader-be-verbose 5) nil 'nomesg))
660 ;; Generate a nov line for this article. We generate the nov
661 ;; line after saving, because nov generation destroys the
663 (setq headers (nnml-parse-head chars))
664 ;; Output the nov line to all nov databases that should have it.
665 (let ((ga group-art))
667 (nnml-add-nov (caar ga) (cdar ga) headers)
671 (defun nnml-active-number (group)
672 "Compute the next article number in GROUP."
673 (let ((active (cadr (assoc group nnml-group-alist))))
674 ;; The group wasn't known to nnml, so we just create an active
677 ;; Perhaps the active file was corrupt? See whether
678 ;; there are any articles in this group.
679 (nnml-possibly-create-directory group)
680 (nnml-possibly-change-directory group)
681 (unless nnml-article-file-alist
682 (setq nnml-article-file-alist
684 (nnml-current-group-article-to-file-alist)
685 'car-less-than-car)))
687 (if nnml-article-file-alist
688 (cons (caar nnml-article-file-alist)
689 (caar (last nnml-article-file-alist)))
691 (push (list group active) nnml-group-alist))
692 (setcdr active (1+ (cdr active)))
693 (while (file-exists-p
694 (expand-file-name (int-to-string (cdr active))
695 (nnmail-group-pathname group nnml-directory)))
696 (setcdr active (1+ (cdr active))))
699 (defun nnml-add-nov (group article headers)
700 "Add a nov line for the GROUP base."
702 (set-buffer (nnml-open-nov group))
703 (goto-char (point-max))
704 (mail-header-set-number headers article)
705 (nnheader-insert-nov headers)))
707 (defsubst nnml-header-value ()
708 (buffer-substring (match-end 0) (point-at-eol)))
710 (defun nnml-parse-head (chars &optional number)
711 "Parse the head of the current buffer."
714 (unless (zerop (buffer-size))
716 (goto-char (point-min))
717 (if (re-search-forward "\n\r?\n" nil t)
720 (let ((headers (nnheader-parse-naked-head)))
721 (mail-header-set-chars headers chars)
722 (mail-header-set-number headers number)
725 (defun nnml-get-nov-buffer (group)
726 (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
729 (set (make-local-variable 'nnml-nov-buffer-file-name)
732 (nnmail-group-pathname group nnml-directory)))
734 (when (file-exists-p nnml-nov-buffer-file-name)
735 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
738 (defun nnml-open-nov (group)
739 (or (cdr (assoc group nnml-nov-buffer-alist))
740 (let ((buffer (nnml-get-nov-buffer group)))
741 (push (cons group buffer) nnml-nov-buffer-alist)
744 (defun nnml-save-nov ()
746 (while nnml-nov-buffer-alist
747 (when (buffer-name (cdar nnml-nov-buffer-alist))
748 (set-buffer (cdar nnml-nov-buffer-alist))
749 (when (buffer-modified-p)
750 (nnmail-write-region (point-min) (point-max)
751 nnml-nov-buffer-file-name nil 'nomesg))
752 (set-buffer-modified-p nil)
753 (kill-buffer (current-buffer)))
754 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
757 (defun nnml-generate-nov-databases (&optional server)
758 "Generate NOV databases in all nnml directories."
759 (interactive (list (or (nnoo-current-server 'nnml) "")))
760 ;; Read the active file to make sure we don't re-use articles
761 ;; numbers in empty groups.
762 (nnmail-activate 'nnml)
763 (unless (nnml-server-opened server)
764 (nnml-open-server server))
765 (setq nnml-directory (expand-file-name nnml-directory))
766 ;; Recurse down the directories.
767 (nnml-generate-nov-databases-1 nnml-directory nil t)
768 ;; Save the active file.
769 (nnmail-save-active nnml-group-alist nnml-active-file))
771 (defun nnml-generate-nov-databases-1 (dir &optional seen no-active)
772 "Regenerate the NOV database in DIR."
773 (interactive "DRegenerate NOV in: ")
774 (setq dir (file-name-as-directory dir))
775 ;; Only scan this sub-tree if we haven't been here yet.
776 (unless (member (file-truename dir) seen)
777 (push (file-truename dir) seen)
778 ;; We descend recursively
779 (dolist (dir (directory-files dir t nil t))
780 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
781 (file-directory-p dir))
782 (nnml-generate-nov-databases-1 dir seen)))
783 ;; Do this directory.
784 (let ((files (sort (nnheader-article-to-file-alist dir)
785 'car-less-than-car)))
787 (let* ((group (nnheader-file-to-group
788 (directory-file-name dir) nnml-directory))
789 (info (cadr (assoc group nnml-group-alist))))
791 (setcar info (1+ (cdr info)))))
792 (funcall nnml-generate-active-function dir)
793 ;; Generate the nov file.
794 (nnml-generate-nov-file dir files)
796 (nnmail-save-active nnml-group-alist nnml-active-file))))))
798 (eval-when-compile (defvar files))
799 (defun nnml-generate-active-info (dir)
800 ;; Update the active info for this group.
801 (let* ((group (nnheader-file-to-group
802 (directory-file-name dir) nnml-directory))
803 (entry (assoc group nnml-group-alist))
804 (last (or (caadr entry) 0)))
805 (setq nnml-group-alist (delq entry nnml-group-alist))
807 (cons (or (caar files) (1+ last))
809 (or (caar (last files))
813 (defun nnml-generate-nov-file (dir files)
814 (let* ((dir (file-name-as-directory dir))
815 (nov (concat dir nnml-nov-file-name))
816 (nov-buffer (get-buffer-create " *nov*"))
819 ;; Init the nov buffer.
820 (set-buffer nov-buffer)
821 (buffer-disable-undo)
823 (set-buffer nntp-server-buffer)
824 ;; Delete the old NOV file.
825 (when (file-exists-p nov)
826 (funcall nnmail-delete-file-function nov))
828 (unless (file-directory-p (setq file (concat dir (cdar files))))
830 (nnheader-insert-file-contents file)
832 (goto-char (point-min))
834 (re-search-forward "\n\r?\n" nil t)
835 (setq chars (- (point-max) (point)))
836 (max (point-min) (1- (point)))))
837 (unless (zerop (buffer-size))
838 (goto-char (point-min))
839 (setq headers (nnml-parse-head chars (caar files)))
841 (set-buffer nov-buffer)
842 (goto-char (point-max))
843 (nnheader-insert-nov headers)))
845 (setq files (cdr files)))
847 (set-buffer nov-buffer)
848 (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
849 (kill-buffer (current-buffer))))))
851 (defun nnml-nov-delete-article (group article)
853 (set-buffer (nnml-open-nov group))
854 (when (nnheader-find-nov-line article)
855 (delete-region (point) (progn (forward-line 1) (point)))
857 (let ((active (cadr (assoc group nnml-group-alist)))
861 (setf (car active) (1+ (cdr active)))
862 (when (and (setq num (ignore-errors (read (current-buffer))))
864 (setf (car active) num)))))))
867 (defun nnml-update-file-alist (&optional force)
868 (when nnml-use-compressed-files
869 (when (or (not nnml-article-file-alist)
871 (setq nnml-article-file-alist
872 (nnml-current-group-article-to-file-alist)))))
874 (defun nnml-directory-articles (dir)
875 "Return a list of all article files in a directory.
876 Use the nov database for that directory if available."
877 (if (or gnus-nov-is-evil nnml-nov-is-evil
879 (expand-file-name nnml-nov-file-name dir))))
880 (nnheader-directory-articles dir)
881 ;; build list from .overview if available
882 ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
883 ;; defvoo'd, and we might get called when it hasn't been swapped in.
887 (buffer (nnml-get-nov-buffer nnml-current-group)))
889 (goto-char (point-min))
891 (setq art (read (current-buffer)))
896 (defun nnml-current-group-article-to-file-alist ()
897 "Return an alist of article/file pairs in the current group.
898 Use the nov database for the current group if available."
899 (if (or nnml-use-compressed-files
903 (expand-file-name nnml-nov-file-name
904 nnml-current-directory))))
905 (nnheader-article-to-file-alist nnml-current-directory)
906 ;; build list from .overview if available
909 (buffer (nnml-get-nov-buffer nnml-current-group))
912 (goto-char (point-min))
914 (setq art (read (current-buffer)))
915 ;; assume file name is unadorned (ie. not compressed etc)
916 (push (cons art (int-to-string art)) alist)
920 (deffoo nnml-request-set-mark (group actions &optional server)
921 (nnml-possibly-change-directory group server)
922 (unless nnml-marks-is-evil
923 (nnml-open-marks group server)
924 (dolist (action actions)
925 (let ((range (nth 0 action))
926 (what (nth 1 action))
927 (marks (nth 2 action)))
928 (assert (or (eq what 'add) (eq what 'del)) nil
929 "Unknown request-set-mark action: %s" what)
931 (setq nnml-marks (gnus-update-alist-soft
933 (funcall (if (eq what 'add) 'gnus-range-add
934 'gnus-remove-from-range)
935 (cdr (assoc mark nnml-marks)) range)
937 (nnml-save-marks group server))
940 (deffoo nnml-request-update-info (group info &optional server)
941 (nnml-possibly-change-directory group server)
942 (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group))
943 (nnheader-message 8 "Updating marks for %s..." group)
944 (nnml-open-marks group server)
945 ;; Update info using `nnml-marks'.
947 (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
950 (gnus-update-alist-soft
952 (cdr (assq (cdr pred) nnml-marks))
953 (gnus-info-marks info))
955 gnus-article-mark-lists)
956 (let ((seen (cdr (assq 'read nnml-marks))))
957 (gnus-info-set-read info
958 (if (and (integerp (car seen))
960 (list (cons (car seen) (car seen)))
962 (nnheader-message 8 "Updating marks for %s...done" group))
965 (defun nnml-marks-changed-p (group)
966 (let ((file (expand-file-name nnml-marks-file-name
967 (nnmail-group-pathname group nnml-directory))))
968 (if (null (gnus-gethash file nnml-marks-modtime))
969 t ;; never looked at marks file, assume it has changed
970 (not (equal (gnus-gethash file nnml-marks-modtime)
971 (nth 5 (file-attributes file)))))))
973 (defun nnml-save-marks (group server)
974 (let ((file-name-coding-system nnmail-pathname-coding-system)
975 (file (expand-file-name nnml-marks-file-name
976 (nnmail-group-pathname group nnml-directory))))
979 (nnml-possibly-create-directory group)
982 (gnus-prin1 nnml-marks)
985 (nth 5 (file-attributes file))
987 (error (or (gnus-yes-or-no-p
988 (format "Could not write to %s (%s). Continue? " file err))
989 (error "Cannot write to %s (%s)" file err))))))
991 (defun nnml-open-marks (group server)
992 (let ((file (expand-file-name
994 (nnmail-group-pathname group nnml-directory))))
995 (if (file-exists-p file)
998 (gnus-sethash file (nth 5 (file-attributes file))
1000 (nnheader-insert-file-contents file)
1001 (setq nnml-marks (read (current-buffer)))
1002 (dolist (el gnus-article-unpropagated-mark-lists)
1003 (setq nnml-marks (gnus-remassoc el nnml-marks))))
1004 (error (or (gnus-yes-or-no-p
1005 (format "Error reading nnml marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
1006 (error "Cannot read nnml marks file %s (%s)" file err))))
1007 ;; User didn't have a .marks file. Probably first time
1008 ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
1009 (let ((info (gnus-get-info
1010 (gnus-group-prefixed-name
1012 (gnus-server-to-method (format "nnml:%s" server))))))
1013 (nnheader-message 7 "Bootstrapping marks for %s..." group)
1014 (setq nnml-marks (gnus-info-marks info))
1015 (push (cons 'read (gnus-info-read info)) nnml-marks)
1016 (dolist (el gnus-article-unpropagated-mark-lists)
1017 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1018 (nnml-save-marks group server)
1019 (nnheader-message 7 "Bootstrapping marks for %s...done" group)))))
1023 ;;; Group and server compaction. -- dvl
1026 ;; #### FIXME: this function handles self Xref: entry correctly, but I don't
1027 ;; #### know how to handle external cross-references. I actually don't know if
1028 ;; #### this is handled correctly elsewhere. For instance, what happens if you
1029 ;; #### move all articles to a new group (that's what people do for manual
1030 ;; #### compaction) ?
1032 ;; #### NOTE: the function below handles the article backlog. This is
1033 ;; #### conceptually the wrong place to do it because the backend is at a
1034 ;; #### lower level. However, this is the only place where we have the needed
1035 ;; #### information to do the job. Ideally, this function should not handle
1036 ;; #### the backlog by itself, but return a list of moved groups / articles to
1037 ;; #### the caller. This will become important to avoid code duplication when
1038 ;; #### other backends get a compaction feature. Also, note that invalidating
1039 ;; #### the "original article buffer" is already done at an upper level.
1041 (defun nnml-request-compact-group (group &optional server save)
1042 (nnml-possibly-change-directory group server)
1043 (unless nnml-article-file-alist
1044 (setq nnml-article-file-alist
1045 (sort (nnml-current-group-article-to-file-alist)
1046 'car-less-than-car)))
1047 (if (not nnml-article-file-alist)
1048 ;; The group is empty: do nothing but return t
1050 ;; The group is not empty:
1051 (let* ((group-full-name
1052 (gnus-group-prefixed-name
1054 (gnus-server-to-method (format "nnml:%s" server))))
1055 (info (gnus-get-info group-full-name))
1058 (let ((articles nnml-article-file-alist)
1060 (while (setq article (pop articles))
1061 (let ((old-number (car article)))
1062 (when (> old-number new-number)
1063 ;; There is a gap here:
1064 (let ((old-number-string (int-to-string old-number))
1065 (new-number-string (int-to-string new-number)))
1067 ;; #### NOTE: `nnml-article-to-file' calls
1068 ;; #### `nnml-update-file-alist' (which in turn calls
1069 ;; #### `nnml-current-group-article-to-file-alist', which
1070 ;; #### might use the NOV database). This might turn out to be
1071 ;; #### inefficient. In that case, we will do the work
1073 ;; 1/ Move the article to a new file:
1074 (let* ((oldfile (nnml-article-to-file old-number))
1076 (gnus-replace-in-string
1077 oldfile (concat "\\("
1079 "\\)\\(\\(\\.gz\\)?\\)$")
1080 (concat new-number-string "\\2"))))
1081 (with-current-buffer nntp-server-buffer
1082 (nnmail-find-file oldfile)
1083 ;; Update the Xref header in the article itself:
1084 (when (and (re-search-forward "^Xref: [^ ]+ " nil t)
1088 (concat group ":" old-number-string))
1092 (concat group ":" new-number-string)))
1093 ;; Save to the new file:
1094 (nnmail-write-region (point-min) (point-max) newfile))
1095 (funcall nnmail-delete-file-function oldfile))
1096 ;; 2/ Update all marks for this article:
1097 ;; #### NOTE: it is possible that the new article number
1098 ;; #### already belongs to a range, whereas the corresponding
1099 ;; #### article doesn't exist (for example, if you delete an
1100 ;; #### article). For that reason, it is important to update
1101 ;; #### the ranges (meaning remove inexistant articles) before
1102 ;; #### doing anything on them.
1103 ;; 2 a/ read articles:
1104 (let ((read (gnus-info-read info)))
1105 (setq read (gnus-remove-from-range read (list new-number)))
1106 (when (gnus-member-of-range old-number read)
1107 (setq read (gnus-remove-from-range read (list old-number)))
1108 (setq read (gnus-add-to-range read (list new-number))))
1109 (gnus-info-set-read info read))
1110 ;; 2 b/ marked articles:
1111 (let ((oldmarks (gnus-info-marks info))
1113 (while (setq mark (pop oldmarks))
1114 (setcdr mark (gnus-remove-from-range (cdr mark)
1116 (when (gnus-member-of-range old-number (cdr mark))
1117 (setcdr mark (gnus-remove-from-range (cdr mark)
1119 (setcdr mark (gnus-add-to-range (cdr mark)
1120 (list new-number))))
1121 (push mark newmarks))
1122 (gnus-info-set-marks info newmarks))
1123 ;; 3/ Update the NOV entry for this article:
1124 (unless nnml-nov-is-evil
1126 (set-buffer (nnml-open-nov group))
1127 (when (nnheader-find-nov-line old-number)
1128 ;; Replace the article number:
1129 (looking-at old-number-string)
1130 (replace-match new-number-string nil t)
1131 ;; Update the Xref header:
1132 (when (re-search-forward
1133 (concat "\\(Xref:[^\t\n]* \\)\\<"
1135 (concat group ":" old-number-string))
1139 (concat "\\1" group ":" new-number-string))))))
1140 ;; 4/ Possibly remove the article from the backlog:
1141 (when gnus-keep-backlog
1142 ;; #### NOTE: instead of removing the article, we could
1143 ;; #### modify the backlog to reflect the numbering change,
1144 ;; #### but I don't think it's worth it.
1145 (gnus-backlog-remove-article group-full-name old-number)
1146 (gnus-backlog-remove-article group-full-name new-number))))
1147 (setq new-number (1+ new-number)))))
1149 ;; No compaction had to be done:
1151 ;; Some articles have actually been renamed:
1152 ;; 1/ Rebuild active information:
1153 (let ((entry (assoc group nnml-group-alist))
1154 (active (cons 1 (1- new-number))))
1155 (setq nnml-group-alist (delq entry nnml-group-alist))
1156 (push (list group active) nnml-group-alist)
1157 ;; Update the active hashtable to let the *Group* buffer display
1158 ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1159 ;; gnus-newwrc-alist are out of date, since all we did is to modify
1160 ;; the info of the group internally.
1161 (gnus-set-active group-full-name active))
1163 ;; #### NOTE: normally, we should save the overview (NOV) file
1164 ;; #### here, just like we save the marks file. However, there is no
1165 ;; #### such function as nnml-save-nov for a single group. Only for
1166 ;; #### all groups. Gnus inconsistency is getting worse every day...
1167 ;; 2/ Rebuild marks file:
1168 (unless nnml-marks-is-evil
1169 ;; #### NOTE: this constant use of global variables everywhere is
1170 ;; #### truly disgusting. Gnus really needs a *major* cleanup.
1171 (setq nnml-marks (gnus-info-marks info))
1172 (push (cons 'read (gnus-info-read info)) nnml-marks)
1173 (dolist (el gnus-article-unpropagated-mark-lists)
1174 (setq nnml-marks (gnus-remassoc el nnml-marks)))
1175 (nnml-save-marks group server))
1176 ;; 3/ Save everything if this was not part of a bigger operation:
1178 ;; Nothing to save (yet):
1180 ;; Something to save:
1181 ;; a/ Save the NOV databases:
1182 ;; #### NOTE: this should be done directory per directory in 1bis
1183 ;; #### above. See comment there.
1185 ;; b/ Save the active file:
1186 (nnmail-save-active nnml-group-alist nnml-active-file)
1189 (defun nnml-request-compact (&optional server)
1190 "Request compaction of all SERVER nnml groups."
1191 (interactive (list (or (nnoo-current-server 'nnml) "")))
1192 (nnmail-activate 'nnml)
1193 (unless (nnml-server-opened server)
1194 (nnml-open-server server))
1195 (setq nnml-directory (expand-file-name nnml-directory))
1196 (let* ((groups (gnus-groups-from-server
1197 (gnus-server-to-method (format "nnml:%s" server))))
1198 (first (pop groups))
1201 (while (setq group (pop groups))
1202 (nnml-request-compact-group (gnus-group-real-name group) server))
1203 (nnml-request-compact-group (gnus-group-real-name first) server t))))
1208 ;;; nnml.el ends here