1 ;;; nnml.el --- mail spool access for Gnus
3 ;; Copyright (C) 1995-2012 Free Software
6 ;; Authors: Didier Verna <didier@xemacs.org> (adding compaction)
7 ;; Simon Josefsson <simon@josefsson.org>
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 3 of the License, or
17 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
29 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
30 ;; For an overview of what the interface functions do, please see the
39 (eval-when-compile (require 'cl))
41 ;; FIXME first is unused in this file.
42 (autoload 'gnus-article-unpropagatable-p "gnus-sum")
43 (autoload 'gnus-backlog-remove-article "gnus-bcklg")
47 (defvoo nnml-directory message-directory
48 "Spool directory for the nnml mail backend.")
50 (defvoo nnml-active-file
51 (expand-file-name "active" nnml-directory)
54 (defvoo nnml-newsgroups-file
55 (expand-file-name "newsgroups" nnml-directory)
56 "Mail newsgroups description file.")
58 (defvoo nnml-get-new-mail t
59 "If non-nil, nnml will check the incoming mail file and split the mail.")
61 (defvoo nnml-nov-is-evil nil
62 "If non-nil, Gnus will never generate and use nov databases for mail spools.
63 Using nov databases will speed up header fetching considerably.
64 This variable shouldn't be flipped much. If you have, for some reason,
65 set this to t, and want to set it to nil again, you should always run
66 the `nnml-generate-nov-databases' command. The function will go
67 through all nnml directories and generate nov databases for them
68 all. This may very well take some time.")
70 (defvoo nnml-prepare-save-mail-hook nil
71 "Hook run narrowed to an article before saving.")
73 (defvoo nnml-inhibit-expiry nil
74 "If non-nil, inhibit expiry.")
76 (defvoo nnml-use-compressed-files nil
77 "If non-nil, allow using compressed message files.
79 If it is a string, use it as the file extension which specifies
80 the compression program. You can set it to \".bz2\" if your Emacs
81 supports auto-compression using the bzip2 program. A value of t
82 is equivalent to \".gz\".")
84 (defvoo nnml-compressed-files-size-threshold 1000
85 "Default size threshold for compressed message files.
86 Message files with bodies larger than that many characters will
87 be automatically compressed if `nnml-use-compressed-files' is
92 (defconst nnml-version "nnml 1.0"
95 (defvoo nnml-nov-file-name ".overview")
97 (defvoo nnml-current-directory nil)
98 (defvoo nnml-current-group nil)
99 (defvoo nnml-status-string "")
100 (defvoo nnml-nov-buffer-alist nil)
101 (defvoo nnml-group-alist nil)
102 (defvoo nnml-active-timestamp nil)
103 (defvoo nnml-article-file-alist nil)
105 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
107 (defvar nnml-nov-buffer-file-name nil)
109 (defvoo nnml-file-coding-system nnmail-file-coding-system)
112 ;;; Interface functions.
114 (nnoo-define-basics nnml)
117 (defsubst nnml-group-name-charset (group server-or-method)
118 (gnus-group-name-charset
119 (if (stringp server-or-method)
120 (gnus-server-to-method
121 (if (string-match "\\+" server-or-method)
122 (concat (substring server-or-method 0 (match-beginning 0))
123 ":" (substring server-or-method (match-end 0)))
124 (concat "nnml:" server-or-method)))
125 (or server-or-method gnus-command-method '(nnml "")))
128 (defun nnml-decoded-group-name (group &optional server-or-method)
129 "Return a decoded group name of GROUP on SERVER-OR-METHOD."
130 (if nnmail-group-names-not-encoded-p
132 (mm-decode-coding-string
134 (nnml-group-name-charset group server-or-method))))
136 (defun nnml-encoded-group-name (group &optional server-or-method)
137 "Return an encoded group name of GROUP on SERVER-OR-METHOD."
138 (mm-encode-coding-string
140 (nnml-group-name-charset group server-or-method)))
142 (defun nnml-group-pathname (group &optional file server)
143 "Return an absolute file name of FILE for GROUP on SERVER."
144 (nnmail-group-pathname (inline (nnml-decoded-group-name group server))
145 nnml-directory file))
147 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
148 (when (nnml-possibly-change-directory group server)
149 (with-current-buffer nntp-server-buffer
152 (number (length sequence))
154 (file-name-coding-system nnmail-pathname-coding-system)
156 (if (stringp (car sequence))
158 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
161 (setq article (car sequence))
162 (setq file (nnml-article-to-file article))
165 (not (file-directory-p file)))
166 (insert (format "221 %d Article retrieved.\n" article))
168 (nnheader-insert-head file)
170 (if (re-search-forward "\n\r?\n" nil t)
172 (goto-char (point-max))
175 (delete-region (point) (point-max)))
176 (setq sequence (cdr sequence))
177 (setq count (1+ count))
178 (and (numberp nnmail-large-newsgroup)
179 (> number nnmail-large-newsgroup)
181 (nnheader-message 6 "nnml: Receiving headers... %d%%"
182 (/ (* count 100) number))))
184 (and (numberp nnmail-large-newsgroup)
185 (> number nnmail-large-newsgroup)
186 (nnheader-message 6 "nnml: Receiving headers...done"))
188 (nnheader-fold-continuation-lines)
191 (deffoo nnml-open-server (server &optional defs)
192 (nnoo-change-server 'nnml server defs)
193 (when (not (file-exists-p nnml-directory))
194 (ignore-errors (make-directory nnml-directory t)))
196 ((not (file-exists-p nnml-directory))
198 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
199 ((not (file-directory-p (file-truename nnml-directory)))
201 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
203 (nnheader-report 'nnml "Opened server %s using directory %s"
204 server nnml-directory)
207 (deffoo nnml-request-regenerate (server)
208 (nnml-possibly-change-directory nil server)
209 (nnml-generate-nov-databases server)
212 (deffoo nnml-request-article (id &optional group server buffer)
213 (nnml-possibly-change-directory group server)
214 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
215 (file-name-coding-system nnmail-pathname-coding-system)
216 path gpath group-num)
218 (when (and (setq group-num (nnml-find-group-number id server))
220 (assq (cdr group-num)
221 (nnheader-article-to-file-alist
222 (setq gpath (nnml-group-pathname (car group-num)
224 (nnml-update-file-alist)
225 (setq path (concat gpath (if nnml-use-compressed-files
226 (cdr (assq (cdr group-num)
227 nnml-article-file-alist))
228 (number-to-string (cdr group-num))))))
229 (setq path (nnml-article-to-file id)))
232 (nnheader-report 'nnml "No such article: %s" id))
233 ((not (file-exists-p path))
234 (nnheader-report 'nnml "No such file: %s" path))
235 ((file-directory-p path)
236 (nnheader-report 'nnml "File is a directory: %s" path))
237 ((not (save-excursion (let ((nnmail-file-coding-system
238 nnml-file-coding-system))
239 (nnmail-find-file path))))
240 (nnheader-report 'nnml "Couldn't read file: %s" path))
242 (nnheader-report 'nnml "Article %s retrieved" id)
243 ;; We return the article number.
244 (cons (if group-num (car group-num) group)
245 (string-to-number (file-name-nondirectory path)))))))
247 (deffoo nnml-request-group (group &optional server dont-check info)
248 (let ((file-name-coding-system nnmail-pathname-coding-system)
249 (decoded (nnml-decoded-group-name group server)))
251 ((not (nnml-possibly-change-directory group server))
252 (nnheader-report 'nnml "Invalid group (no such directory)"))
253 ((not (file-exists-p nnml-current-directory))
254 (nnheader-report 'nnml "Directory %s does not exist"
255 nnml-current-directory))
256 ((not (file-directory-p nnml-current-directory))
257 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
259 (nnheader-report 'nnml "Group %s selected" decoded)
262 (nnheader-re-read-dir nnml-current-directory)
263 (nnmail-activate 'nnml)
264 (let ((active (nth 1 (assoc group nnml-group-alist))))
266 (nnheader-report 'nnml "No such group: %s" decoded)
267 (nnheader-report 'nnml "Selected group %s" decoded)
268 (nnheader-insert "211 %d %d %d %s\n"
269 (max (1+ (- (cdr active) (car active))) 0)
270 (car active) (cdr active) group)))))))
272 (deffoo nnml-request-scan (&optional group server)
273 (setq nnml-article-file-alist nil)
274 (nnml-possibly-change-directory group server)
275 (nnmail-get-new-mail 'nnml 'nnml-save-incremental-nov nnml-directory group))
277 (deffoo nnml-close-group (group &optional server)
278 (setq nnml-article-file-alist nil)
281 (deffoo nnml-request-create-group (group &optional server args)
282 (nnml-possibly-change-directory nil server)
283 (nnmail-activate 'nnml)
285 ((let ((file (directory-file-name (nnml-group-pathname group nil server)))
286 (file-name-coding-system nnmail-pathname-coding-system))
287 (and (file-exists-p file)
288 (not (file-directory-p file))))
289 (nnheader-report 'nnml "%s is a file"
290 (directory-file-name (nnml-group-pathname group
292 ((assoc group nnml-group-alist)
296 (push (list group (setq active (cons 1 0)))
298 (nnml-possibly-create-directory group server)
299 (nnml-possibly-change-directory group server)
300 (let* ((file-name-coding-system nnmail-pathname-coding-system)
301 (articles (nnml-directory-articles nnml-current-directory)))
303 (setcar active (apply 'min articles))
304 (setcdr active (apply 'max articles))))
305 (nnmail-save-active nnml-group-alist nnml-active-file)
308 (deffoo nnml-request-list (&optional server)
310 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
311 (file-name-coding-system nnmail-pathname-coding-system))
312 (nnmail-find-file nnml-active-file))
313 (setq nnml-group-alist (nnmail-get-active))
316 (deffoo nnml-request-newgroups (date &optional server)
317 (nnml-request-list server))
319 (deffoo nnml-request-list-newsgroups (&optional server)
321 (nnmail-find-file nnml-newsgroups-file)))
323 (deffoo nnml-request-expire-articles (articles group &optional server force)
324 (nnml-possibly-change-directory group server)
325 (let* ((file-name-coding-system nnmail-pathname-coding-system)
327 (nnml-directory-articles nnml-current-directory))
329 (decoded (nnml-decoded-group-name group server))
330 article rest mod-time number target)
331 (nnmail-activate 'nnml)
333 (setq active-articles (sort active-articles '<))
334 ;; Articles not listed in active-articles are already gone,
335 ;; so don't try to expire them.
336 (setq articles (gnus-sorted-intersection articles active-articles))
338 (while (and articles is-old)
339 (if (and (setq article (nnml-article-to-file
340 (setq number (pop articles))))
341 (setq mod-time (nth 5 (file-attributes article)))
342 (nnml-deletable-article-p group number)
343 (setq is-old (nnmail-expired-article-p group mod-time force
344 nnml-inhibit-expiry)))
346 ;; Allow a special target group.
347 (setq target nnmail-expiry-target)
348 (unless (eq target 'delete)
350 (nnml-request-article number group server (current-buffer))
351 (let (nnml-current-directory
353 nnml-article-file-alist)
354 (when (functionp target)
355 (setq target (funcall target group)))
356 (when (and target (not (eq target 'delete)))
357 (if (or (gnus-request-group target)
358 (gnus-request-create-group target))
359 (nnmail-expiry-target-group target group)
360 (setq target nil)))))
361 ;; Maybe directory is changed during nnmail-expiry-target-group.
362 (nnml-possibly-change-directory group server))
365 (nnheader-message 5 "Deleting article %s in %s"
368 (funcall nnmail-delete-file-function article)
371 (setq active-articles (delq number active-articles))
372 (nnml-nov-delete-article group number))
375 (let ((active (nth 1 (assoc group nnml-group-alist))))
377 (setcar active (or (and active-articles
378 (apply 'min active-articles))
380 (nnmail-save-active nnml-group-alist nnml-active-file))
382 (nconc rest articles)))
384 (deffoo nnml-request-move-article
385 (article group server accept-form &optional last move-is-internal)
386 (let ((buf (get-buffer-create " *nnml move*"))
387 (file-name-coding-system nnmail-pathname-coding-system)
389 (nnml-possibly-change-directory group server)
390 (nnml-update-file-alist)
392 (nnml-deletable-article-p group article)
393 (nnml-request-article article group server)
394 (let (nnml-current-directory
396 nnml-article-file-alist)
397 (with-current-buffer buf
398 (insert-buffer-substring nntp-server-buffer)
399 (setq result (eval accept-form))
400 (kill-buffer (current-buffer))
403 (nnml-possibly-change-directory group server)
405 (funcall nnmail-delete-file-function
406 (nnml-article-to-file article))
408 (nnml-nov-delete-article group article)
411 (nnmail-save-active nnml-group-alist nnml-active-file))))
414 (deffoo nnml-request-accept-article (group &optional server last)
415 (nnml-possibly-change-directory group server)
416 (nnmail-check-syntax)
418 (when nnmail-cache-accepted-message-ids
419 (nnmail-cache-insert (nnmail-fetch-field "message-id")
421 (nnmail-fetch-field "subject")
422 (nnmail-fetch-field "from")))
425 (nnmail-activate 'nnml)
426 (setq result (car (nnml-save-mail
427 (list (cons group (nnml-active-number group
431 (nnmail-save-active nnml-group-alist nnml-active-file)
432 (and last (nnml-save-nov))))
434 (nnmail-activate 'nnml)
435 (if (and (not (setq result (nnmail-article-group
437 (nnml-active-number group ,server)))))
438 (yes-or-no-p "Moved to `junk' group; delete article? "))
440 (setq result (car (nnml-save-mail result server t))))
442 (nnmail-save-active nnml-group-alist nnml-active-file)
443 (when nnmail-cache-accepted-message-ids
444 (nnmail-cache-close))
448 (deffoo nnml-request-post (&optional server)
449 (nnmail-do-request-post 'nnml-request-accept-article server))
451 (deffoo nnml-request-replace-article (article group buffer)
452 (nnml-possibly-change-directory group)
453 (with-current-buffer buffer
454 (nnml-possibly-create-directory group)
455 (let ((chars (nnmail-insert-lines))
456 (art (concat (int-to-string article) "\t"))
460 (point-min) (point-max)
461 (or (nnml-article-to-file article)
462 (expand-file-name (int-to-string article)
463 nnml-current-directory))
464 nil (if (nnheader-be-verbose 5) nil 'nomesg))
466 (setq headers (nnml-parse-head chars article))
467 ;; Replace the NOV line in the NOV file.
468 (with-current-buffer (nnml-open-nov group)
469 (goto-char (point-min))
470 (if (or (looking-at art)
471 (search-forward (concat "\n" art) nil t))
472 ;; Delete the old NOV line.
474 ;; The line isn't here, so we have to find out where
475 ;; we should insert it. (This situation should never
476 ;; occur, but one likes to make sure...)
477 (while (and (looking-at "[0-9]+\t")
480 (match-beginning 0) (match-end 0)))
482 (zerop (forward-line 1)))))
484 (nnheader-insert-nov headers)
488 (deffoo nnml-request-delete-group (group &optional force server)
489 (nnml-possibly-change-directory group server)
490 (let ((file (directory-file-name nnml-current-directory))
491 (file-name-coding-system nnmail-pathname-coding-system))
492 (if (file-exists-p file)
493 (if (file-directory-p file)
496 ;; Delete all articles in GROUP.
499 nnml-current-directory t
501 nnheader-numerical-short-files
502 "\\|" (regexp-quote nnml-nov-file-name) "$")))
503 (decoded (nnml-decoded-group-name group server)))
504 (dolist (article articles)
505 (when (file-writable-p article)
506 (nnheader-message 5 "Deleting article %s in %s..."
507 (file-name-nondirectory article)
509 (funcall nnmail-delete-file-function article))))
510 ;; Try to delete the directory itself.
511 (ignore-errors (delete-directory nnml-current-directory))))
512 (nnheader-report 'nnml "%s is not a directory" file))
513 (nnheader-report 'nnml "No such directory: %s/" file))
514 ;; Remove the group from all structures.
515 (setq nnml-group-alist
516 (delq (assoc group nnml-group-alist) nnml-group-alist)
517 nnml-current-group nil
518 nnml-current-directory nil)
519 ;; Save the active file.
520 (nnmail-save-active nnml-group-alist nnml-active-file))
523 (deffoo nnml-request-rename-group (group new-name &optional server)
524 (nnml-possibly-change-directory group server)
525 (let ((new-dir (nnml-group-pathname new-name nil server))
526 (old-dir (nnml-group-pathname group nil server))
527 (file-name-coding-system nnmail-pathname-coding-system))
529 (make-directory new-dir t)
531 ;; We move the articles file by file instead of renaming
532 ;; the directory -- there may be subgroups in this group.
533 ;; One might be more clever, I guess.
534 (dolist (file (nnheader-article-to-file-alist old-dir))
536 (concat old-dir (cdr file))
537 (concat new-dir (cdr file))))
538 ;; Move .overview file.
539 (let ((overview (concat old-dir nnml-nov-file-name)))
540 (when (file-exists-p overview)
541 (rename-file overview (concat new-dir nnml-nov-file-name))))
542 (when (<= (length (directory-files old-dir)) 2)
543 (ignore-errors (delete-directory old-dir)))
544 ;; That went ok, so we change the internal structures.
545 (let ((entry (assoc group nnml-group-alist)))
547 (setcar entry new-name))
548 (setq nnml-current-directory nil
549 nnml-current-group nil)
550 ;; Save the new group alist.
551 (nnmail-save-active nnml-group-alist nnml-active-file)
554 (deffoo nnml-set-status (article name value &optional group server)
555 (nnml-possibly-change-directory group server)
556 (let ((file (nnml-article-to-file article)))
558 ((not (file-exists-p file))
559 (nnheader-report 'nnml "File %s does not exist" file))
562 (nnheader-insert-file-contents file)
563 (nnmail-replace-status name value))
567 ;;; Internal functions.
569 (defun nnml-article-to-file (article)
570 (nnml-update-file-alist)
573 (if nnml-use-compressed-files
574 (cdr (assq article nnml-article-file-alist))
575 (number-to-string article)))
576 (expand-file-name file nnml-current-directory)
577 (when (not nnheader-directory-files-is-safe)
578 ;; Just to make sure nothing went wrong when reading over NFS --
581 (setq file (expand-file-name (number-to-string article)
582 nnml-current-directory)))
583 (nnml-update-file-alist t)
586 (defun nnml-deletable-article-p (group article)
587 "Say whether ARTICLE in GROUP can be deleted."
588 (let ((file-name-coding-system nnmail-pathname-coding-system)
590 (when (setq path (nnml-article-to-file article))
591 (when (file-writable-p path)
592 (or (not nnmail-keep-last-article)
593 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
596 ;; Find an article number in the current group given the Message-ID.
597 (defun nnml-find-group-number (id server)
598 (with-current-buffer (get-buffer-create " *nnml id*")
599 (let ((alist nnml-group-alist)
601 ;; We want to look through all .overview files, but we want to
602 ;; start with the one in the current directory. It seems most
603 ;; likely that the article we are looking for is in that group.
604 (if (setq number (nnml-find-id nnml-current-group id server))
605 (cons nnml-current-group number)
606 ;; It wasn't there, so we look through the other groups as well.
607 (while (and (not number)
609 (or (string= (caar alist) nnml-current-group)
610 (setq number (nnml-find-id (caar alist) id server)))
612 (setq alist (cdr alist))))
614 (cons (caar alist) number))))))
616 (defun nnml-find-id (group id server)
618 (let ((nov (nnml-group-pathname group nnml-nov-file-name server))
620 (when (file-exists-p nov)
621 (nnheader-insert-file-contents nov)
622 (while (and (not found)
623 (search-forward id nil t)) ; We find the ID.
624 ;; And the id is in the fourth field.
625 (if (not (and (search-backward "\t" nil t 4)
626 (not (search-backward "\t" (point-at-bol) t))))
630 ;; We return the article number.
632 (ignore-errors (read (current-buffer))))))
635 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
636 (if (or gnus-nov-is-evil nnml-nov-is-evil)
638 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
639 (when (file-exists-p nov)
640 (with-current-buffer nntp-server-buffer
642 (nnheader-insert-file-contents nov)
644 (not (numberp fetch-old)))
645 t ; Don't remove anything.
646 (nnheader-nov-delete-outside-range
647 (if fetch-old (max 1 (- (car articles) fetch-old))
649 (car (last articles)))
652 (defun nnml-possibly-change-directory (group &optional server)
654 (not (nnml-server-opened server)))
655 (nnml-open-server server))
658 (let ((pathname (nnml-group-pathname group nil server))
659 (file-name-coding-system nnmail-pathname-coding-system))
660 (when (not (equal pathname nnml-current-directory))
661 (setq nnml-current-directory pathname
662 nnml-current-group group
663 nnml-article-file-alist nil))
664 (file-exists-p nnml-current-directory))))
666 (defun nnml-possibly-create-directory (group &optional server)
667 (let ((dir (nnml-group-pathname group nil server))
668 (file-name-coding-system nnmail-pathname-coding-system))
669 (unless (file-exists-p dir)
670 (make-directory (directory-file-name dir) t)
671 (nnheader-message 5 "Creating mail directory %s" dir))))
673 (defun nnml-save-mail (group-art &optional server full-nov)
674 "Save a mail into the groups GROUP-ART in the nnml server SERVER.
675 GROUP-ART is a list that each element is a cons of a group name and an
676 article number. This function is called narrowed to an article."
677 (let* ((chars (nnmail-insert-lines))
678 (extension (and nnml-use-compressed-files
679 (> chars nnml-compressed-files-size-threshold)
680 (if (stringp nnml-use-compressed-files)
681 nnml-use-compressed-files
683 decoded dec file first headers)
684 (when nnmail-group-names-not-encoded-p
685 (dolist (ga (prog1 group-art (setq group-art nil)))
686 (setq group-art (nconc group-art
687 (list (cons (nnml-encoded-group-name (car ga)
690 decoded (nconc decoded (list (car ga)))))
692 (nnmail-insert-xref group-art)
693 (run-hooks 'nnmail-prepare-save-mail-hook)
694 (run-hooks 'nnml-prepare-save-mail-hook)
695 (goto-char (point-min))
696 (while (looking-at "From ")
697 (replace-match "X-From-Line: ")
699 ;; We save the article in all the groups it belongs in.
700 (dolist (ga group-art)
701 (if nnmail-group-names-not-encoded-p
703 (nnml-possibly-create-directory (car decoded) server)
704 (setq file (nnmail-group-pathname
705 (pop decoded) nnml-directory
706 (concat (number-to-string (cdr ga)) extension))))
707 (nnml-possibly-create-directory (car ga) server)
708 (setq file (nnml-group-pathname
709 (car ga) (concat (number-to-string (cdr ga)) extension)
712 ;; It was already saved, so we just make a hard link.
713 (let ((file-name-coding-system nnmail-pathname-coding-system))
714 (funcall nnmail-crosspost-link-function first file t))
716 (nnmail-write-region (point-min) (point-max) file nil
717 (if (nnheader-be-verbose 5) nil 'nomesg))
719 ;; Generate a nov line for this article. We generate the nov
720 ;; line after saving, because nov generation destroys the
722 (setq headers (nnml-parse-head chars))
723 ;; Output the nov line to all nov databases that should have it.
724 (let ((func (if full-nov
726 'nnml-add-incremental-nov)))
727 (if nnmail-group-names-not-encoded-p
728 (dolist (ga group-art)
729 (funcall func (pop dec) (cdr ga) headers))
730 (dolist (ga group-art)
731 (funcall func (car ga) (cdr ga) headers)))))
734 (defun nnml-active-number (group &optional server)
735 "Compute the next article number in GROUP on SERVER."
736 (let* ((encoded (if nnmail-group-names-not-encoded-p
737 (nnml-encoded-group-name group server)))
738 (active (cadr (assoc (or encoded group) nnml-group-alist))))
739 ;; The group wasn't known to nnml, so we just create an active
742 ;; Perhaps the active file was corrupt? See whether
743 ;; there are any articles in this group.
744 (nnml-possibly-create-directory group server)
745 (nnml-possibly-change-directory group server)
746 (unless nnml-article-file-alist
747 (setq nnml-article-file-alist
749 (nnml-current-group-article-to-file-alist)
750 'car-less-than-car)))
752 (if nnml-article-file-alist
753 (cons (caar nnml-article-file-alist)
754 (caar (last nnml-article-file-alist)))
756 (push (list (or encoded group) active) nnml-group-alist))
757 (setcdr active (1+ (cdr active)))
758 (while (file-exists-p
759 (nnml-group-pathname group (int-to-string (cdr active)) server))
760 (setcdr active (1+ (cdr active))))
763 (defvar nnml-incremental-nov-buffer-alist nil)
765 (defun nnml-save-incremental-nov ()
767 (while nnml-incremental-nov-buffer-alist
768 (when (buffer-name (cdar nnml-incremental-nov-buffer-alist))
769 (set-buffer (cdar nnml-incremental-nov-buffer-alist))
770 (when (buffer-modified-p)
771 (nnmail-write-region (point-min) (point-max)
772 nnml-nov-buffer-file-name t 'nomesg))
773 (set-buffer-modified-p nil)
774 (kill-buffer (current-buffer)))
775 (setq nnml-incremental-nov-buffer-alist
776 (cdr nnml-incremental-nov-buffer-alist)))))
778 (defun nnml-open-incremental-nov (group)
779 (or (cdr (assoc group nnml-incremental-nov-buffer-alist))
780 (let ((buffer (nnml-get-nov-buffer group t)))
781 (push (cons group buffer) nnml-incremental-nov-buffer-alist)
784 (defun nnml-add-incremental-nov (group article headers)
785 "Add a nov line for the GROUP nov headers, incrementally."
786 (with-current-buffer (nnml-open-incremental-nov group)
787 (goto-char (point-max))
788 (mail-header-set-number headers article)
789 (nnheader-insert-nov headers)))
791 (defun nnml-add-nov (group article headers)
792 "Add a nov line for the GROUP base."
793 (with-current-buffer (nnml-open-nov group)
794 (goto-char (point-max))
795 (mail-header-set-number headers article)
796 (nnheader-insert-nov headers)))
798 (defsubst nnml-header-value ()
799 (buffer-substring (match-end 0) (point-at-eol)))
801 (defun nnml-parse-head (chars &optional number)
802 "Parse the head of the current buffer."
805 (unless (zerop (buffer-size))
807 (goto-char (point-min))
808 (if (re-search-forward "\n\r?\n" nil t)
811 (let ((headers (nnheader-parse-naked-head)))
812 (mail-header-set-chars headers chars)
813 (mail-header-set-number headers number)
816 (defun nnml-get-nov-buffer (group &optional incrementalp)
817 (let* ((decoded (nnml-decoded-group-name group))
818 (buffer (get-buffer-create (format " *nnml %soverview %s*"
823 (file-name-coding-system nnmail-pathname-coding-system))
824 (with-current-buffer buffer
825 (set (make-local-variable 'nnml-nov-buffer-file-name)
826 (nnmail-group-pathname decoded nnml-directory nnml-nov-file-name))
828 (when (and (not incrementalp)
829 (file-exists-p nnml-nov-buffer-file-name))
830 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
833 (defun nnml-open-nov (group)
834 (or (let ((buffer (cdr (assoc group nnml-nov-buffer-alist))))
835 (and (buffer-name buffer)
837 (let ((buffer (nnml-get-nov-buffer group)))
838 (push (cons group buffer) nnml-nov-buffer-alist)
841 (defun nnml-save-nov ()
843 (while nnml-nov-buffer-alist
844 (when (buffer-name (cdar nnml-nov-buffer-alist))
845 (set-buffer (cdar nnml-nov-buffer-alist))
846 (when (buffer-modified-p)
847 (nnmail-write-region (point-min) (point-max)
848 nnml-nov-buffer-file-name nil 'nomesg))
849 (set-buffer-modified-p nil)
850 (kill-buffer (current-buffer)))
851 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
854 (defun nnml-generate-nov-databases (&optional server)
855 "Generate NOV databases in all nnml directories."
856 (interactive (list (or (nnoo-current-server 'nnml) "")))
857 ;; Read the active file to make sure we don't re-use articles
858 ;; numbers in empty groups.
859 (nnmail-activate 'nnml)
860 (unless (nnml-server-opened server)
861 (nnml-open-server server))
862 (setq nnml-directory (expand-file-name nnml-directory))
863 ;; Recurse down the directories.
864 (nnml-generate-nov-databases-directory nnml-directory nil t)
865 ;; Save the active file.
866 (nnmail-save-active nnml-group-alist nnml-active-file))
869 (defun nnml-generate-nov-databases-directory (dir &optional seen no-active)
870 "Regenerate the NOV database in DIR.
872 Unless no-active is non-nil, update the active file too."
873 (interactive (list (let ((file-name-coding-system
874 nnmail-pathname-coding-system))
875 (read-directory-name "Regenerate NOV in: "
876 nnml-directory nil t))))
877 (setq dir (file-name-as-directory dir))
878 (let ((file-name-coding-system nnmail-pathname-coding-system))
879 ;; Only scan this sub-tree if we haven't been here yet.
880 (unless (member (file-truename dir) seen)
881 (push (file-truename dir) seen)
882 ;; We descend recursively
883 (dolist (dir (directory-files dir t nil t))
884 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
885 (file-directory-p dir))
886 (nnml-generate-nov-databases-directory dir seen)))
887 ;; Do this directory.
888 (let ((nnml-files (sort (nnheader-article-to-file-alist dir)
889 'car-less-than-car)))
891 (let* ((group (nnheader-file-to-group
892 (directory-file-name dir) nnml-directory))
893 (info (cadr (assoc group nnml-group-alist))))
895 (setcar info (1+ (cdr info)))))
896 (funcall nnml-generate-active-function dir)
897 ;; Generate the nov file.
898 (nnml-generate-nov-file dir nnml-files)
900 (nnmail-save-active nnml-group-alist nnml-active-file)))))))
902 (defun nnml-generate-active-info (dir)
903 ;; Update the active info for this group.
904 (let ((group (directory-file-name dir))
906 (setq group (nnheader-file-to-group (nnml-encoded-group-name group)
908 entry (assoc group nnml-group-alist)
909 last (or (caadr entry) 0)
910 nnml-group-alist (delq entry nnml-group-alist))
912 (cons (or (caar nnml-files) (1+ last))
914 (or (caar (last nnml-files))
918 (defun nnml-generate-nov-file (dir files)
919 (let* ((dir (file-name-as-directory dir))
920 (nov (concat dir nnml-nov-file-name))
921 (nov-buffer (get-buffer-create " *nov*"))
923 (with-current-buffer nov-buffer
924 ;; Init the nov buffer.
925 (buffer-disable-undo)
927 (set-buffer nntp-server-buffer)
928 ;; Delete the old NOV file.
929 (when (file-exists-p nov)
930 (funcall nnmail-delete-file-function nov))
932 (let ((path (concat dir (cdr file))))
933 (unless (file-directory-p path)
935 (nnheader-insert-file-contents path)
937 (goto-char (point-min))
939 (re-search-forward "\n\r?\n" nil t)
940 (setq chars (- (point-max) (point)))
941 (max (point-min) (1- (point)))))
942 (unless (zerop (buffer-size))
943 (goto-char (point-min))
944 (setq headers (nnml-parse-head chars (car file)))
945 (with-current-buffer nov-buffer
946 (goto-char (point-max))
947 (nnheader-insert-nov headers)))
949 (with-current-buffer nov-buffer
950 (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
951 (kill-buffer (current-buffer))))))
953 (defun nnml-nov-delete-article (group article)
954 (with-current-buffer (nnml-open-nov group)
955 (when (nnheader-find-nov-line article)
956 (delete-region (point) (progn (forward-line 1) (point)))
958 (let ((active (cadr (assoc group nnml-group-alist)))
962 (setf (car active) (1+ (cdr active)))
963 (when (and (setq num (ignore-errors (read (current-buffer))))
965 (setf (car active) num)))))))
968 (defun nnml-update-file-alist (&optional force)
969 (when nnml-use-compressed-files
970 (when (or (not nnml-article-file-alist)
972 (setq nnml-article-file-alist
973 (nnml-current-group-article-to-file-alist)))))
975 (defun nnml-directory-articles (dir)
976 "Return a list of all article files in a directory.
977 Use the nov database for that directory if available."
978 (if (or gnus-nov-is-evil nnml-nov-is-evil
980 (expand-file-name nnml-nov-file-name dir))))
981 (nnheader-directory-articles dir)
982 ;; build list from .overview if available
983 ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
984 ;; defvoo'd, and we might get called when it hasn't been swapped in.
985 (with-current-buffer (nnml-get-nov-buffer nnml-current-group)
988 (goto-char (point-min))
990 (setq art (read (current-buffer)))
995 (defun nnml-current-group-article-to-file-alist ()
996 "Return an alist of article/file pairs in the current group.
997 Use the nov database for the current group if available."
998 (if (or nnml-use-compressed-files
1002 (expand-file-name nnml-nov-file-name
1003 nnml-current-directory))))
1004 (nnheader-article-to-file-alist nnml-current-directory)
1005 ;; build list from .overview if available
1006 (with-current-buffer (nnml-get-nov-buffer nnml-current-group)
1009 (goto-char (point-min))
1011 (setq art (read (current-buffer)))
1012 ;; assume file name is unadorned (ie. not compressed etc)
1013 (push (cons art (int-to-string art)) alist)
1018 ;;; Group and server compaction. -- dvl
1021 ;; #### FIXME: this function handles self Xref: entry correctly, but I don't
1022 ;; #### know how to handle external cross-references. I actually don't know if
1023 ;; #### this is handled correctly elsewhere. For instance, what happens if you
1024 ;; #### move all articles to a new group (that's what people do for manual
1025 ;; #### compaction) ?
1027 ;; #### NOTE: the function below handles the article backlog. This is
1028 ;; #### conceptually the wrong place to do it because the backend is at a
1029 ;; #### lower level. However, this is the only place where we have the needed
1030 ;; #### information to do the job. Ideally, this function should not handle
1031 ;; #### the backlog by itself, but return a list of moved groups / articles to
1032 ;; #### the caller. This will become important to avoid code duplication when
1033 ;; #### other backends get a compaction feature. Also, note that invalidating
1034 ;; #### the "original article buffer" is already done at an upper level.
1036 ;; Shouldn't `nnml-request-compact-group' be interactive? --rsteib
1038 (defun nnml-request-compact-group (group &optional server save)
1039 (nnml-possibly-change-directory group server)
1040 (unless nnml-article-file-alist
1041 (setq nnml-article-file-alist
1042 (sort (nnml-current-group-article-to-file-alist)
1043 'car-less-than-car)))
1044 (if (not nnml-article-file-alist)
1045 ;; The group is empty: do nothing but return t
1047 ;; The group is not empty:
1048 (let* ((group-full-name
1049 (gnus-group-prefixed-name
1051 (gnus-server-to-method (format "nnml:%s" server))))
1052 (info (gnus-get-info group-full-name))
1055 (let ((articles nnml-article-file-alist)
1057 (while (setq article (pop articles))
1058 (let ((old-number (car article)))
1059 (when (> old-number new-number)
1060 ;; There is a gap here:
1061 (let ((old-number-string (int-to-string old-number))
1062 (new-number-string (int-to-string new-number)))
1064 ;; #### NOTE: `nnml-article-to-file' calls
1065 ;; #### `nnml-update-file-alist' (which in turn calls
1066 ;; #### `nnml-current-group-article-to-file-alist', which
1067 ;; #### might use the NOV database). This might turn out to be
1068 ;; #### inefficient. In that case, we will do the work
1070 ;; 1/ Move the article to a new file:
1071 (let* ((oldfile (nnml-article-to-file old-number))
1073 (gnus-replace-in-string
1075 ;; nnml-use-compressed-files might be any string, but
1076 ;; probably it's sufficient to take into account only
1077 ;; "\\.[a-z0-9]+". Note that we can't only use the
1078 ;; value of nnml-use-compressed-files because old
1079 ;; articles might have been saved with a different
1082 "\\(" old-number-string "\\)\\(\\(\\.[a-z0-9]+\\)?\\)$")
1083 (concat new-number-string "\\2"))))
1084 (with-current-buffer nntp-server-buffer
1085 (nnmail-find-file oldfile)
1086 ;; Update the Xref header in the article itself:
1087 (when (and (re-search-forward "^Xref: [^ ]+ " nil t)
1091 (concat group ":" old-number-string))
1095 (concat group ":" new-number-string)))
1096 ;; Save to the new file:
1097 (nnmail-write-region (point-min) (point-max) newfile))
1098 (funcall nnmail-delete-file-function oldfile))
1099 ;; 2/ Update all marks for this article:
1100 ;; #### NOTE: it is possible that the new article number
1101 ;; #### already belongs to a range, whereas the corresponding
1102 ;; #### article doesn't exist (for example, if you delete an
1103 ;; #### article). For that reason, it is important to update
1104 ;; #### the ranges (meaning remove nonexistent articles) before
1105 ;; #### doing anything on them.
1106 ;; 2 a/ read articles:
1107 (let ((read (gnus-info-read info)))
1108 (setq read (gnus-remove-from-range read (list new-number)))
1109 (when (gnus-member-of-range old-number read)
1110 (setq read (gnus-remove-from-range read (list old-number)))
1111 (setq read (gnus-add-to-range read (list new-number))))
1112 (gnus-info-set-read info read))
1113 ;; 2 b/ marked articles:
1114 (let ((oldmarks (gnus-info-marks info))
1116 (while (setq mark (pop oldmarks))
1117 (setcdr mark (gnus-remove-from-range (cdr mark)
1119 (when (gnus-member-of-range old-number (cdr mark))
1120 (setcdr mark (gnus-remove-from-range (cdr mark)
1122 (setcdr mark (gnus-add-to-range (cdr mark)
1123 (list new-number))))
1124 (push mark newmarks))
1125 (gnus-info-set-marks info newmarks))
1126 ;; 3/ Update the NOV entry for this article:
1127 (unless nnml-nov-is-evil
1128 (with-current-buffer (nnml-open-nov group)
1129 (when (nnheader-find-nov-line old-number)
1130 ;; Replace the article number:
1131 (looking-at old-number-string)
1132 (replace-match new-number-string nil t)
1133 ;; Update the Xref header:
1134 (when (re-search-forward
1135 (concat "\\(Xref:[^\t\n]* \\)\\<"
1137 (concat group ":" old-number-string))
1141 (concat "\\1" group ":" new-number-string))))))
1142 ;; 4/ Possibly remove the article from the backlog:
1143 (when gnus-keep-backlog
1144 ;; #### NOTE: instead of removing the article, we could
1145 ;; #### modify the backlog to reflect the numbering change,
1146 ;; #### but I don't think it's worth it.
1147 (gnus-backlog-remove-article group-full-name old-number)
1148 (gnus-backlog-remove-article group-full-name new-number))))
1149 (setq new-number (1+ new-number)))))
1151 ;; No compaction had to be done:
1153 ;; Some articles have actually been renamed:
1154 ;; 1/ Rebuild active information:
1155 (let ((entry (assoc group nnml-group-alist))
1156 (active (cons 1 (1- new-number))))
1157 (setq nnml-group-alist (delq entry nnml-group-alist))
1158 (push (list group active) nnml-group-alist)
1159 ;; Update the active hashtable to let the *Group* buffer display
1160 ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1161 ;; gnus-newwrc-alist are out of date, since all we did is to modify
1162 ;; the info of the group internally.
1163 (gnus-set-active group-full-name active))
1165 ;; #### NOTE: normally, we should save the overview (NOV) file
1166 ;; #### here. However, there is no such function as
1167 ;; #### nnml-save-nov for a single group. Only for all
1168 ;; #### groups. Gnus inconsistency is getting worse every
1169 ;; #### day... ;; 3/ Save everything if this was not part of
1170 ;; #### a bigger operation:
1172 ;; Nothing to save (yet):
1174 ;; Something to save:
1175 ;; a/ Save the NOV databases:
1176 ;; #### NOTE: this should be done directory per directory in 1bis
1177 ;; #### above. See comment there.
1179 ;; b/ Save the active file:
1180 (nnmail-save-active nnml-group-alist nnml-active-file)
1183 (defun nnml-request-compact (&optional server)
1184 "Request compaction of all SERVER nnml groups."
1185 (interactive (list (or (nnoo-current-server 'nnml) "")))
1186 (nnmail-activate 'nnml)
1187 (unless (nnml-server-opened server)
1188 (nnml-open-server server))
1189 (setq nnml-directory (expand-file-name nnml-directory))
1190 (let* ((groups (gnus-groups-from-server
1191 (gnus-server-to-method (format "nnml:%s" server))))
1192 (first (pop groups))