1 ;;; nnml.el --- mail spool access for Gnus
3 ;; Copyright (C) 1995-2015 Free Software Foundation, Inc.
5 ;; Authors: Didier Verna <didier@xemacs.org> (adding compaction)
6 ;; Simon Josefsson <simon@josefsson.org>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
9 ;; Keywords: news, mail
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
29 ;; For an overview of what the interface functions do, please see the
38 (eval-when-compile (require 'cl))
40 ;; FIXME first is unused in this file.
41 (autoload 'gnus-article-unpropagatable-p "gnus-sum")
42 (autoload 'gnus-backlog-remove-article "gnus-bcklg")
46 (defvoo nnml-directory message-directory
47 "Spool directory for the nnml mail backend.")
49 (defvoo nnml-active-file
50 (expand-file-name "active" nnml-directory)
53 (defvoo nnml-newsgroups-file
54 (expand-file-name "newsgroups" nnml-directory)
55 "Mail newsgroups description file.")
57 (defvoo nnml-get-new-mail t
58 "If non-nil, nnml will check the incoming mail file and split the mail.")
60 (defvoo nnml-nov-is-evil nil
61 "If non-nil, Gnus will never generate and use nov databases for mail spools.
62 Using nov databases will speed up header fetching considerably.
63 This variable shouldn't be flipped much. If you have, for some reason,
64 set this to t, and want to set it to nil again, you should always run
65 the `nnml-generate-nov-databases' command. The function will go
66 through all nnml directories and generate nov databases for them
67 all. This may very well take some time.")
69 (defvoo nnml-prepare-save-mail-hook nil
70 "Hook run narrowed to an article before saving.")
72 (defvoo nnml-inhibit-expiry nil
73 "If non-nil, inhibit expiry.")
75 (defvoo nnml-use-compressed-files nil
76 "If non-nil, allow using compressed message files.
78 If it is a string, use it as the file extension which specifies
79 the compression program. You can set it to \".bz2\" if your Emacs
80 supports auto-compression using the bzip2 program. A value of t
81 is equivalent to \".gz\".")
83 (defvoo nnml-compressed-files-size-threshold 1000
84 "Default size threshold for compressed message files.
85 Message files with bodies larger than that many characters will
86 be automatically compressed if `nnml-use-compressed-files' is
91 (defconst nnml-version "nnml 1.0"
94 (defvoo nnml-nov-file-name ".overview")
96 (defvoo nnml-current-directory nil)
97 (defvoo nnml-current-group nil)
98 (defvoo nnml-status-string "")
99 (defvoo nnml-nov-buffer-alist nil)
100 (defvoo nnml-group-alist nil)
101 (defvoo nnml-active-timestamp nil)
102 (defvoo nnml-article-file-alist nil)
104 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
106 (defvar nnml-nov-buffer-file-name nil)
108 (defvoo nnml-file-coding-system nnmail-file-coding-system)
111 ;;; Interface functions.
113 (nnoo-define-basics nnml)
116 (defsubst nnml-group-name-charset (group server-or-method)
117 (gnus-group-name-charset
118 (if (stringp server-or-method)
119 (gnus-server-to-method
120 (if (string-match "\\+" server-or-method)
121 (concat (substring server-or-method 0 (match-beginning 0))
122 ":" (substring server-or-method (match-end 0)))
123 (concat "nnml:" server-or-method)))
124 (or server-or-method gnus-command-method '(nnml "")))
127 (defun nnml-decoded-group-name (group &optional server-or-method)
128 "Return a decoded group name of GROUP on SERVER-OR-METHOD."
129 (if nnmail-group-names-not-encoded-p
131 (mm-decode-coding-string
133 (nnml-group-name-charset group server-or-method))))
135 (defun nnml-encoded-group-name (group &optional server-or-method)
136 "Return an encoded group name of GROUP on SERVER-OR-METHOD."
137 (mm-encode-coding-string
139 (nnml-group-name-charset group server-or-method)))
141 (defun nnml-group-pathname (group &optional file server)
142 "Return an absolute file name of FILE for GROUP on SERVER."
143 (nnmail-group-pathname (inline (nnml-decoded-group-name group server))
144 nnml-directory file))
146 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
147 (when (nnml-possibly-change-directory group server)
148 (with-current-buffer nntp-server-buffer
151 (number (length sequence))
153 (file-name-coding-system nnmail-pathname-coding-system)
155 (if (stringp (car sequence))
157 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
160 (setq article (car sequence))
161 (setq file (nnml-article-to-file article))
164 (not (file-directory-p file)))
165 (insert (format "221 %d Article retrieved.\n" article))
167 (nnheader-insert-head file)
169 (if (re-search-forward "\n\r?\n" nil t)
171 (goto-char (point-max))
174 (delete-region (point) (point-max)))
175 (setq sequence (cdr sequence))
176 (setq count (1+ count))
177 (and (numberp nnmail-large-newsgroup)
178 (> number nnmail-large-newsgroup)
180 (nnheader-message 6 "nnml: Receiving headers... %d%%"
181 (floor (* count 100.0) number))))
183 (and (numberp nnmail-large-newsgroup)
184 (> number nnmail-large-newsgroup)
185 (nnheader-message 6 "nnml: Receiving headers...done"))
187 (nnheader-fold-continuation-lines)
190 (deffoo nnml-open-server (server &optional defs)
191 (nnoo-change-server 'nnml server defs)
192 (when (not (file-exists-p nnml-directory))
193 (ignore-errors (make-directory nnml-directory t)))
195 ((not (file-exists-p nnml-directory))
197 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
198 ((not (file-directory-p (file-truename nnml-directory)))
200 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
202 (nnheader-report 'nnml "Opened server %s using directory %s"
203 server nnml-directory)
206 (deffoo nnml-request-regenerate (server)
207 (nnml-possibly-change-directory nil server)
208 (nnml-generate-nov-databases server)
211 (deffoo nnml-request-article (id &optional group server buffer)
212 (nnml-possibly-change-directory group server)
213 (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
214 (file-name-coding-system nnmail-pathname-coding-system)
215 path gpath group-num)
217 (when (and (setq group-num (nnml-find-group-number id server))
219 (assq (cdr group-num)
220 (nnheader-article-to-file-alist
221 (setq gpath (nnml-group-pathname (car group-num)
223 (nnml-update-file-alist)
224 (setq path (concat gpath (if nnml-use-compressed-files
225 (cdr (assq (cdr group-num)
226 nnml-article-file-alist))
227 (number-to-string (cdr group-num))))))
228 (setq path (nnml-article-to-file id)))
231 (nnheader-report 'nnml "No such article: %s" id))
232 ((not (file-exists-p path))
233 (nnheader-report 'nnml "No such file: %s" path))
234 ((file-directory-p path)
235 (nnheader-report 'nnml "File is a directory: %s" path))
236 ((not (save-excursion (let ((nnmail-file-coding-system
237 nnml-file-coding-system))
238 (nnmail-find-file path))))
239 (nnheader-report 'nnml "Couldn't read file: %s" path))
241 (nnheader-report 'nnml "Article %s retrieved" id)
242 ;; We return the article number.
243 (cons (if group-num (car group-num) group)
244 (string-to-number (file-name-nondirectory path)))))))
246 (deffoo nnml-request-group (group &optional server dont-check info)
247 (let ((file-name-coding-system nnmail-pathname-coding-system)
248 (decoded (nnml-decoded-group-name group server)))
250 ((not (nnml-possibly-change-directory group server))
251 (nnheader-report 'nnml "Invalid group (no such directory)"))
252 ((not (file-exists-p nnml-current-directory))
253 (nnheader-report 'nnml "Directory %s does not exist"
254 nnml-current-directory))
255 ((not (file-directory-p nnml-current-directory))
256 (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
258 (nnheader-report 'nnml "Group %s selected" decoded)
261 (nnheader-re-read-dir nnml-current-directory)
262 (nnmail-activate 'nnml)
263 (let ((active (nth 1 (assoc group nnml-group-alist))))
265 (nnheader-report 'nnml "No such group: %s" decoded)
266 (nnheader-report 'nnml "Selected group %s" decoded)
267 (nnheader-insert "211 %d %d %d %s\n"
268 (max (1+ (- (cdr active) (car active))) 0)
269 (car active) (cdr active) group)))))))
271 (deffoo nnml-retrieve-groups (groups &optional server)
272 (when nnml-get-new-mail
273 (if (nnmail-get-new-mail-per-group)
274 (dolist (group groups)
275 (nnml-request-scan group server))
276 (nnml-request-scan nil server)))
277 (with-current-buffer nntp-server-buffer
279 (dolist (group groups)
280 (let* ((entry (assoc group nnml-group-alist))
281 (active (nth 1 entry)))
283 (insert (format "211 %d %d %d %s\n"
284 (max (1+ (- (cdr active) (car active))) 0)
285 (car active) (cdr active) group))))))
288 (deffoo nnml-request-scan (&optional group server)
289 (setq nnml-article-file-alist nil)
290 (nnml-possibly-change-directory group server)
293 (nnmail-get-new-mail 'nnml 'nnml-save-incremental-nov nnml-directory group))
294 ((nnmail-get-new-mail-per-group)
296 (dolist (entry nnml-group-alist)
297 (nnml-request-scan (car entry) server)))
299 (nnmail-get-new-mail 'nnml 'nnml-save-incremental-nov nnml-directory nil))))
301 (deffoo nnml-close-group (group &optional server)
302 (setq nnml-article-file-alist nil)
305 (deffoo nnml-request-create-group (group &optional server args)
306 (nnml-possibly-change-directory nil server)
307 (nnmail-activate 'nnml)
309 ((let ((file (directory-file-name (nnml-group-pathname group nil server)))
310 (file-name-coding-system nnmail-pathname-coding-system))
311 (and (file-exists-p file)
312 (not (file-directory-p file))))
313 (nnheader-report 'nnml "%s is a file"
314 (directory-file-name (nnml-group-pathname group
316 ((assoc group nnml-group-alist)
320 (push (list group (setq active (cons 1 0)))
322 (nnml-possibly-create-directory group server)
323 (nnml-possibly-change-directory group server)
324 (let* ((file-name-coding-system nnmail-pathname-coding-system)
325 (articles (nnml-directory-articles nnml-current-directory)))
327 (setcar active (apply 'min articles))
328 (setcdr active (apply 'max articles))))
329 (nnmail-save-active nnml-group-alist nnml-active-file)
332 (deffoo nnml-request-list (&optional server)
334 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
335 (file-name-coding-system nnmail-pathname-coding-system))
336 (nnmail-find-file nnml-active-file))
337 (setq nnml-group-alist (nnmail-get-active))
340 (deffoo nnml-request-newgroups (date &optional server)
341 (nnml-request-list server))
343 (deffoo nnml-request-list-newsgroups (&optional server)
345 (nnmail-find-file nnml-newsgroups-file)))
347 (deffoo nnml-request-expire-articles (articles group &optional server force)
348 (nnml-possibly-change-directory group server)
349 (let* ((file-name-coding-system nnmail-pathname-coding-system)
351 (nnml-directory-articles nnml-current-directory))
353 (decoded (nnml-decoded-group-name group server))
354 article rest mod-time number target)
355 (nnmail-activate 'nnml)
357 (setq active-articles (sort active-articles '<))
358 ;; Articles not listed in active-articles are already gone,
359 ;; so don't try to expire them.
360 (setq articles (gnus-sorted-intersection articles active-articles))
362 (while (and articles is-old)
363 (if (and (setq article (nnml-article-to-file
364 (setq number (pop articles))))
365 (setq mod-time (nth 5 (file-attributes article)))
366 (nnml-deletable-article-p group number)
367 (setq is-old (nnmail-expired-article-p group mod-time force
368 nnml-inhibit-expiry)))
370 ;; Allow a special target group.
371 (setq target nnmail-expiry-target)
372 (unless (eq target 'delete)
374 (nnml-request-article number group server (current-buffer))
375 (let (nnml-current-directory
377 nnml-article-file-alist)
378 (when (functionp target)
379 (setq target (funcall target group)))
380 (when (and target (not (eq target 'delete)))
381 (if (or (gnus-request-group target)
382 (gnus-request-create-group target))
383 (nnmail-expiry-target-group target group)
384 (setq target nil)))))
385 ;; Maybe directory is changed during nnmail-expiry-target-group.
386 (nnml-possibly-change-directory group server))
389 (nnheader-message 5 "Deleting article %s in %s"
392 (funcall nnmail-delete-file-function article)
395 (setq active-articles (delq number active-articles))
396 (nnml-nov-delete-article group number))
399 (let ((active (nth 1 (assoc group nnml-group-alist))))
401 (setcar active (or (and active-articles
402 (apply 'min active-articles))
404 (nnmail-save-active nnml-group-alist nnml-active-file))
406 (nconc rest articles)))
408 (deffoo nnml-request-move-article
409 (article group server accept-form &optional last move-is-internal)
410 (let ((buf (get-buffer-create " *nnml move*"))
411 (file-name-coding-system nnmail-pathname-coding-system)
413 (nnml-possibly-change-directory group server)
414 (nnml-update-file-alist)
416 (nnml-deletable-article-p group article)
417 (nnml-request-article article group server)
418 (let (nnml-current-directory
420 nnml-article-file-alist)
421 (with-current-buffer buf
422 (insert-buffer-substring nntp-server-buffer)
423 (setq result (eval accept-form))
424 (kill-buffer (current-buffer))
427 (nnml-possibly-change-directory group server)
429 (funcall nnmail-delete-file-function
430 (nnml-article-to-file article))
432 (nnml-nov-delete-article group article)
435 (nnmail-save-active nnml-group-alist nnml-active-file))))
438 (deffoo nnml-request-accept-article (group &optional server last)
439 (nnml-possibly-change-directory group server)
440 (nnmail-check-syntax)
442 (when nnmail-cache-accepted-message-ids
443 (nnmail-cache-insert (nnmail-fetch-field "message-id")
445 (nnmail-fetch-field "subject")
446 (nnmail-fetch-field "from")))
449 (nnmail-activate 'nnml)
450 (setq result (car (nnml-save-mail
451 (list (cons group (nnml-active-number group
455 (nnmail-save-active nnml-group-alist nnml-active-file)
456 (and last (nnml-save-nov))))
458 (nnmail-activate 'nnml)
459 (if (and (not (setq result (nnmail-article-group
461 (nnml-active-number group ,server)))))
462 (yes-or-no-p "Moved to `junk' group; delete article? "))
464 (setq result (car (nnml-save-mail result server t))))
466 (nnmail-save-active nnml-group-alist nnml-active-file)
467 (when nnmail-cache-accepted-message-ids
468 (nnmail-cache-close))
472 (deffoo nnml-request-post (&optional server)
473 (nnmail-do-request-post 'nnml-request-accept-article server))
475 (deffoo nnml-request-replace-article (article group buffer)
476 (nnml-possibly-change-directory group)
477 (with-current-buffer buffer
478 (nnml-possibly-create-directory group)
479 (let ((chars (nnmail-insert-lines))
480 (art (concat (int-to-string article) "\t"))
484 (point-min) (point-max)
485 (or (nnml-article-to-file article)
486 (expand-file-name (int-to-string article)
487 nnml-current-directory))
488 nil (if (nnheader-be-verbose 5) nil 'nomesg))
490 (setq headers (nnml-parse-head chars article))
491 ;; Replace the NOV line in the NOV file.
492 (with-current-buffer (nnml-open-nov group)
493 (goto-char (point-min))
494 (if (or (looking-at art)
495 (search-forward (concat "\n" art) nil t))
496 ;; Delete the old NOV line.
498 ;; The line isn't here, so we have to find out where
499 ;; we should insert it. (This situation should never
500 ;; occur, but one likes to make sure...)
501 (while (and (looking-at "[0-9]+\t")
504 (match-beginning 0) (match-end 0)))
506 (zerop (forward-line 1)))))
508 (nnheader-insert-nov headers)
512 (deffoo nnml-request-delete-group (group &optional force server)
513 (nnml-possibly-change-directory group server)
514 (let ((file (directory-file-name nnml-current-directory))
515 (file-name-coding-system nnmail-pathname-coding-system))
516 (if (file-exists-p file)
517 (if (file-directory-p file)
520 ;; Delete all articles in GROUP.
523 nnml-current-directory t
525 nnheader-numerical-short-files
526 "\\|" (regexp-quote nnml-nov-file-name) "$")))
527 (decoded (nnml-decoded-group-name group server)))
528 (dolist (article articles)
529 (when (file-writable-p article)
530 (nnheader-message 5 "Deleting article %s in %s..."
531 (file-name-nondirectory article)
533 (funcall nnmail-delete-file-function article))))
534 ;; Try to delete the directory itself.
535 (ignore-errors (delete-directory nnml-current-directory))))
536 (nnheader-report 'nnml "%s is not a directory" file))
537 (nnheader-report 'nnml "No such directory: %s/" file))
538 ;; Remove the group from all structures.
539 (setq nnml-group-alist
540 (delq (assoc group nnml-group-alist) nnml-group-alist)
541 nnml-current-group nil
542 nnml-current-directory nil)
543 ;; Save the active file.
544 (nnmail-save-active nnml-group-alist nnml-active-file))
547 (deffoo nnml-request-rename-group (group new-name &optional server)
548 (nnml-possibly-change-directory group server)
549 (let ((new-dir (nnml-group-pathname new-name nil server))
550 (old-dir (nnml-group-pathname group nil server))
551 (file-name-coding-system nnmail-pathname-coding-system))
553 (make-directory new-dir t)
555 ;; We move the articles file by file instead of renaming
556 ;; the directory -- there may be subgroups in this group.
557 ;; One might be more clever, I guess.
558 (dolist (file (nnheader-article-to-file-alist old-dir))
560 (concat old-dir (cdr file))
561 (concat new-dir (cdr file))))
562 ;; Move .overview file.
563 (let ((overview (concat old-dir nnml-nov-file-name)))
564 (when (file-exists-p overview)
565 (rename-file overview (concat new-dir nnml-nov-file-name))))
566 (when (<= (length (directory-files old-dir)) 2)
567 (ignore-errors (delete-directory old-dir)))
568 ;; That went ok, so we change the internal structures.
569 (let ((entry (assoc group nnml-group-alist)))
571 (setcar entry new-name))
572 (setq nnml-current-directory nil
573 nnml-current-group nil)
574 ;; Save the new group alist.
575 (nnmail-save-active nnml-group-alist nnml-active-file)
578 (deffoo nnml-set-status (article name value &optional group server)
579 (nnml-possibly-change-directory group server)
580 (let ((file (nnml-article-to-file article)))
582 ((not (file-exists-p file))
583 (nnheader-report 'nnml "File %s does not exist" file))
586 (nnheader-insert-file-contents file)
587 (nnmail-replace-status name value))
591 ;;; Internal functions.
593 (defun nnml-article-to-file (article)
594 (nnml-update-file-alist)
597 (if nnml-use-compressed-files
598 (cdr (assq article nnml-article-file-alist))
599 (number-to-string article)))
600 (expand-file-name file nnml-current-directory)
601 (when (not nnheader-directory-files-is-safe)
602 ;; Just to make sure nothing went wrong when reading over NFS --
605 (setq file (expand-file-name (number-to-string article)
606 nnml-current-directory)))
607 (nnml-update-file-alist t)
610 (defun nnml-deletable-article-p (group article)
611 "Say whether ARTICLE in GROUP can be deleted."
612 (let ((file-name-coding-system nnmail-pathname-coding-system)
614 (when (setq path (nnml-article-to-file article))
615 (when (file-writable-p path)
616 (or (not nnmail-keep-last-article)
617 (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
620 ;; Find an article number in the current group given the Message-ID.
621 (defun nnml-find-group-number (id server)
622 (with-current-buffer (get-buffer-create " *nnml id*")
623 (let ((alist nnml-group-alist)
625 ;; We want to look through all .overview files, but we want to
626 ;; start with the one in the current directory. It seems most
627 ;; likely that the article we are looking for is in that group.
628 (if (setq number (nnml-find-id nnml-current-group id server))
629 (cons nnml-current-group number)
630 ;; It wasn't there, so we look through the other groups as well.
631 (while (and (not number)
633 (or (string= (caar alist) nnml-current-group)
634 (setq number (nnml-find-id (caar alist) id server)))
636 (setq alist (cdr alist))))
638 (cons (caar alist) number))))))
640 (defun nnml-find-id (group id server)
642 (let ((nov (nnml-group-pathname group nnml-nov-file-name server))
644 (when (file-exists-p nov)
645 (nnheader-insert-file-contents nov)
646 (while (and (not found)
647 (search-forward id nil t)) ; We find the ID.
648 ;; And the id is in the fourth field.
649 (if (not (and (search-backward "\t" nil t 4)
650 (not (search-backward "\t" (point-at-bol) t))))
654 ;; We return the article number.
656 (ignore-errors (read (current-buffer))))))
659 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
660 (if (or gnus-nov-is-evil nnml-nov-is-evil)
662 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
663 (when (file-exists-p nov)
664 (with-current-buffer nntp-server-buffer
666 (nnheader-insert-file-contents nov)
668 (not (numberp fetch-old)))
669 t ; Don't remove anything.
670 (nnheader-nov-delete-outside-range
671 (if fetch-old (max 1 (- (car articles) fetch-old))
673 (car (last articles)))
676 (defun nnml-possibly-change-directory (group &optional server)
678 (not (nnml-server-opened server)))
679 (nnml-open-server server))
682 (let ((pathname (nnml-group-pathname group nil server))
683 (file-name-coding-system nnmail-pathname-coding-system))
684 (when (not (equal pathname nnml-current-directory))
685 (setq nnml-current-directory pathname
686 nnml-current-group group
687 nnml-article-file-alist nil))
688 (file-exists-p nnml-current-directory))))
690 (defun nnml-possibly-create-directory (group &optional server)
691 (let ((dir (nnml-group-pathname group nil server))
692 (file-name-coding-system nnmail-pathname-coding-system))
693 (unless (file-exists-p dir)
694 (make-directory (directory-file-name dir) t)
695 (nnheader-message 5 "Creating mail directory %s" dir))))
697 (defun nnml-save-mail (group-art &optional server full-nov)
698 "Save a mail into the groups GROUP-ART in the nnml server SERVER.
699 GROUP-ART is a list that each element is a cons of a group name and an
700 article number. This function is called narrowed to an article."
701 (let* ((chars (nnmail-insert-lines))
702 (extension (and nnml-use-compressed-files
703 (> chars nnml-compressed-files-size-threshold)
704 (if (stringp nnml-use-compressed-files)
705 nnml-use-compressed-files
707 decoded dec file first headers)
708 (when nnmail-group-names-not-encoded-p
709 (dolist (ga (prog1 group-art (setq group-art nil)))
710 (setq group-art (nconc group-art
711 (list (cons (nnml-encoded-group-name (car ga)
714 decoded (nconc decoded (list (car ga)))))
716 (nnmail-insert-xref group-art)
717 (run-hooks 'nnmail-prepare-save-mail-hook)
718 (run-hooks 'nnml-prepare-save-mail-hook)
719 (goto-char (point-min))
720 (while (looking-at "From ")
721 (replace-match "X-From-Line: ")
723 ;; We save the article in all the groups it belongs in.
724 (dolist (ga group-art)
725 (if nnmail-group-names-not-encoded-p
727 (nnml-possibly-create-directory (car decoded) server)
728 (setq file (nnmail-group-pathname
729 (pop decoded) nnml-directory
730 (concat (number-to-string (cdr ga)) extension))))
731 (nnml-possibly-create-directory (car ga) server)
732 (setq file (nnml-group-pathname
733 (car ga) (concat (number-to-string (cdr ga)) extension)
736 ;; It was already saved, so we just make a hard link.
737 (let ((file-name-coding-system nnmail-pathname-coding-system))
738 (funcall nnmail-crosspost-link-function first file t))
740 (nnmail-write-region (point-min) (point-max) file nil
741 (if (nnheader-be-verbose 5) nil 'nomesg))
743 ;; Generate a nov line for this article. We generate the nov
744 ;; line after saving, because nov generation destroys the
746 (setq headers (nnml-parse-head chars))
747 ;; Output the nov line to all nov databases that should have it.
748 (let ((func (if full-nov
750 'nnml-add-incremental-nov)))
751 (if nnmail-group-names-not-encoded-p
752 (dolist (ga group-art)
753 (funcall func (pop dec) (cdr ga) headers))
754 (dolist (ga group-art)
755 (funcall func (car ga) (cdr ga) headers)))))
758 (defun nnml-active-number (group &optional server)
759 "Compute the next article number in GROUP on SERVER."
760 (let* ((encoded (if nnmail-group-names-not-encoded-p
761 (nnml-encoded-group-name group server)))
762 (active (cadr (assoc (or encoded group) nnml-group-alist))))
763 ;; The group wasn't known to nnml, so we just create an active
766 ;; Perhaps the active file was corrupt? See whether
767 ;; there are any articles in this group.
768 (nnml-possibly-create-directory group server)
769 (nnml-possibly-change-directory group server)
770 (unless nnml-article-file-alist
771 (setq nnml-article-file-alist
773 (nnml-current-group-article-to-file-alist)
774 'car-less-than-car)))
776 (if nnml-article-file-alist
777 (cons (caar nnml-article-file-alist)
778 (caar (last nnml-article-file-alist)))
780 (push (list (or encoded group) active) nnml-group-alist))
781 (setcdr active (1+ (cdr active)))
782 (while (file-exists-p
783 (nnml-group-pathname group (int-to-string (cdr active)) server))
784 (setcdr active (1+ (cdr active))))
787 (defvar nnml-incremental-nov-buffer-alist nil)
789 (defun nnml-save-incremental-nov ()
791 (while nnml-incremental-nov-buffer-alist
792 (when (buffer-name (cdar nnml-incremental-nov-buffer-alist))
793 (set-buffer (cdar nnml-incremental-nov-buffer-alist))
794 (when (buffer-modified-p)
795 (nnmail-write-region (point-min) (point-max)
796 nnml-nov-buffer-file-name t 'nomesg))
797 (set-buffer-modified-p nil)
798 (kill-buffer (current-buffer)))
799 (setq nnml-incremental-nov-buffer-alist
800 (cdr nnml-incremental-nov-buffer-alist)))))
802 (defun nnml-open-incremental-nov (group)
803 (or (cdr (assoc group nnml-incremental-nov-buffer-alist))
804 (let ((buffer (nnml-get-nov-buffer group t)))
805 (push (cons group buffer) nnml-incremental-nov-buffer-alist)
808 (defun nnml-add-incremental-nov (group article headers)
809 "Add a nov line for the GROUP nov headers, incrementally."
810 (with-current-buffer (nnml-open-incremental-nov group)
811 (goto-char (point-max))
812 (mail-header-set-number headers article)
813 (nnheader-insert-nov headers)))
815 (defun nnml-add-nov (group article headers)
816 "Add a nov line for the GROUP base."
817 (with-current-buffer (nnml-open-nov group)
818 (goto-char (point-max))
819 (mail-header-set-number headers article)
820 (nnheader-insert-nov headers)))
822 (defsubst nnml-header-value ()
823 (buffer-substring (match-end 0) (point-at-eol)))
825 (defun nnml-parse-head (chars &optional number)
826 "Parse the head of the current buffer."
829 (unless (zerop (buffer-size))
831 (goto-char (point-min))
832 (if (re-search-forward "\n\r?\n" nil t)
835 (let ((headers (nnheader-parse-naked-head)))
836 (mail-header-set-chars headers chars)
837 (mail-header-set-number headers number)
840 (defun nnml-get-nov-buffer (group &optional incrementalp)
841 (let* ((decoded (nnml-decoded-group-name group))
842 (buffer (get-buffer-create (format " *nnml %soverview %s*"
847 (file-name-coding-system nnmail-pathname-coding-system))
848 (with-current-buffer buffer
849 (set (make-local-variable 'nnml-nov-buffer-file-name)
850 (nnmail-group-pathname decoded nnml-directory nnml-nov-file-name))
852 (when (and (not incrementalp)
853 (file-exists-p nnml-nov-buffer-file-name))
854 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
857 (defun nnml-open-nov (group)
858 (or (let ((buffer (cdr (assoc group nnml-nov-buffer-alist))))
859 (and (buffer-name buffer)
861 (let ((buffer (nnml-get-nov-buffer group)))
862 (push (cons group buffer) nnml-nov-buffer-alist)
865 (defun nnml-save-nov ()
867 (while nnml-nov-buffer-alist
868 (when (buffer-name (cdar nnml-nov-buffer-alist))
869 (set-buffer (cdar nnml-nov-buffer-alist))
870 (when (buffer-modified-p)
871 (nnmail-write-region (point-min) (point-max)
872 nnml-nov-buffer-file-name nil 'nomesg))
873 (set-buffer-modified-p nil)
874 (kill-buffer (current-buffer)))
875 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
878 (defun nnml-generate-nov-databases (&optional server)
879 "Generate NOV databases in all nnml directories."
880 (interactive (list (or (nnoo-current-server 'nnml) "")))
881 ;; Read the active file to make sure we don't re-use articles
882 ;; numbers in empty groups.
883 (nnmail-activate 'nnml)
884 (unless (nnml-server-opened server)
885 (nnml-open-server server))
886 (setq nnml-directory (expand-file-name nnml-directory))
887 ;; Recurse down the directories.
888 (nnml-generate-nov-databases-directory nnml-directory nil t)
889 ;; Save the active file.
890 (nnmail-save-active nnml-group-alist nnml-active-file))
893 (defun nnml-generate-nov-databases-directory (dir &optional seen no-active)
894 "Regenerate the NOV database in DIR.
896 Unless no-active is non-nil, update the active file too."
897 (interactive (list (let ((file-name-coding-system
898 nnmail-pathname-coding-system))
899 (read-directory-name "Regenerate NOV in: "
900 nnml-directory nil t))))
901 (setq dir (file-name-as-directory dir))
902 (let ((file-name-coding-system nnmail-pathname-coding-system))
903 ;; Only scan this sub-tree if we haven't been here yet.
904 (unless (member (file-truename dir) seen)
905 (push (file-truename dir) seen)
906 ;; We descend recursively
907 (dolist (dir (directory-files dir t nil t))
908 (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
909 (file-directory-p dir))
910 (nnml-generate-nov-databases-directory dir seen)))
911 ;; Do this directory.
912 (let ((nnml-files (sort (nnheader-article-to-file-alist dir)
913 'car-less-than-car)))
915 (let* ((group (nnheader-file-to-group
916 (directory-file-name dir) nnml-directory))
917 (info (cadr (assoc group nnml-group-alist))))
919 (setcar info (1+ (cdr info)))))
920 (funcall nnml-generate-active-function dir)
921 ;; Generate the nov file.
922 (nnml-generate-nov-file dir nnml-files)
924 (nnmail-save-active nnml-group-alist nnml-active-file)))))))
926 (defun nnml-generate-active-info (dir)
927 ;; Update the active info for this group.
928 (let ((group (directory-file-name dir))
930 (setq group (nnheader-file-to-group (nnml-encoded-group-name group)
932 entry (assoc group nnml-group-alist)
933 last (or (caadr entry) 0)
934 nnml-group-alist (delq entry nnml-group-alist))
936 (cons (or (caar nnml-files) (1+ last))
938 (or (caar (last nnml-files))
942 (defun nnml-generate-nov-file (dir files)
943 (let* ((dir (file-name-as-directory dir))
944 (nov (concat dir nnml-nov-file-name))
945 (nov-buffer (get-buffer-create " *nov*"))
947 (with-current-buffer nov-buffer
948 ;; Init the nov buffer.
949 (buffer-disable-undo)
951 (set-buffer nntp-server-buffer)
952 ;; Delete the old NOV file.
953 (when (file-exists-p nov)
954 (funcall nnmail-delete-file-function nov))
956 (let ((path (concat dir (cdr file))))
957 (unless (file-directory-p path)
959 (nnheader-insert-file-contents path)
961 (goto-char (point-min))
963 (re-search-forward "\n\r?\n" nil t)
964 (setq chars (- (point-max) (point)))
965 (max (point-min) (1- (point)))))
966 (unless (zerop (buffer-size))
967 (goto-char (point-min))
968 (setq headers (nnml-parse-head chars (car file)))
969 (with-current-buffer nov-buffer
970 (goto-char (point-max))
971 (nnheader-insert-nov headers)))
973 (with-current-buffer nov-buffer
974 (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
975 (kill-buffer (current-buffer))))))
977 (defun nnml-nov-delete-article (group article)
978 (with-current-buffer (nnml-open-nov group)
979 (when (nnheader-find-nov-line article)
980 (delete-region (point) (progn (forward-line 1) (point)))
982 (let ((active (cadr (assoc group nnml-group-alist)))
986 (setf (car active) (1+ (cdr active)))
987 (when (and (setq num (ignore-errors (read (current-buffer))))
989 (setf (car active) num)))))))
992 (defun nnml-update-file-alist (&optional force)
993 (when nnml-use-compressed-files
994 (when (or (not nnml-article-file-alist)
996 (setq nnml-article-file-alist
997 (nnml-current-group-article-to-file-alist)))))
999 (defun nnml-directory-articles (dir)
1000 "Return a list of all article files in a directory.
1001 Use the nov database for that directory if available."
1002 (if (or gnus-nov-is-evil nnml-nov-is-evil
1004 (expand-file-name nnml-nov-file-name dir))))
1005 (nnheader-directory-articles dir)
1006 ;; build list from .overview if available
1007 ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
1008 ;; defvoo'd, and we might get called when it hasn't been swapped in.
1009 (with-current-buffer (nnml-get-nov-buffer nnml-current-group)
1012 (goto-char (point-min))
1014 (setq art (read (current-buffer)))
1019 (defun nnml-current-group-article-to-file-alist ()
1020 "Return an alist of article/file pairs in the current group.
1021 Use the nov database for the current group if available."
1022 (if (or nnml-use-compressed-files
1026 (expand-file-name nnml-nov-file-name
1027 nnml-current-directory))))
1028 (nnheader-article-to-file-alist nnml-current-directory)
1029 ;; build list from .overview if available
1030 (with-current-buffer (nnml-get-nov-buffer nnml-current-group)
1033 (goto-char (point-min))
1035 (setq art (read (current-buffer)))
1036 ;; assume file name is unadorned (ie. not compressed etc)
1037 (push (cons art (int-to-string art)) alist)
1042 ;;; Group and server compaction. -- dvl
1045 ;; #### FIXME: this function handles self Xref: entry correctly, but I don't
1046 ;; #### know how to handle external cross-references. I actually don't know if
1047 ;; #### this is handled correctly elsewhere. For instance, what happens if you
1048 ;; #### move all articles to a new group (that's what people do for manual
1049 ;; #### compaction) ?
1051 ;; #### NOTE: the function below handles the article backlog. This is
1052 ;; #### conceptually the wrong place to do it because the backend is at a
1053 ;; #### lower level. However, this is the only place where we have the needed
1054 ;; #### information to do the job. Ideally, this function should not handle
1055 ;; #### the backlog by itself, but return a list of moved groups / articles to
1056 ;; #### the caller. This will become important to avoid code duplication when
1057 ;; #### other backends get a compaction feature. Also, note that invalidating
1058 ;; #### the "original article buffer" is already done at an upper level.
1060 ;; Shouldn't `nnml-request-compact-group' be interactive? --rsteib
1062 (defun nnml-request-compact-group (group &optional server save)
1063 (nnml-possibly-change-directory group server)
1064 (unless nnml-article-file-alist
1065 (setq nnml-article-file-alist
1066 (sort (nnml-current-group-article-to-file-alist)
1067 'car-less-than-car)))
1068 (if (not nnml-article-file-alist)
1069 ;; The group is empty: do nothing but return t
1071 ;; The group is not empty:
1072 (let* ((group-full-name
1073 (gnus-group-prefixed-name
1075 (gnus-server-to-method (format "nnml:%s" server))))
1076 (info (gnus-get-info group-full-name))
1079 (let ((articles nnml-article-file-alist)
1081 (while (setq article (pop articles))
1082 (let ((old-number (car article)))
1083 (when (> old-number new-number)
1084 ;; There is a gap here:
1085 (let ((old-number-string (int-to-string old-number))
1086 (new-number-string (int-to-string new-number)))
1088 ;; #### NOTE: `nnml-article-to-file' calls
1089 ;; #### `nnml-update-file-alist' (which in turn calls
1090 ;; #### `nnml-current-group-article-to-file-alist', which
1091 ;; #### might use the NOV database). This might turn out to be
1092 ;; #### inefficient. In that case, we will do the work
1094 ;; 1/ Move the article to a new file:
1095 (let* ((oldfile (nnml-article-to-file old-number))
1097 (gnus-replace-in-string
1099 ;; nnml-use-compressed-files might be any string, but
1100 ;; probably it's sufficient to take into account only
1101 ;; "\\.[a-z0-9]+". Note that we can't only use the
1102 ;; value of nnml-use-compressed-files because old
1103 ;; articles might have been saved with a different
1106 "\\(" old-number-string "\\)\\(\\(\\.[a-z0-9]+\\)?\\)$")
1107 (concat new-number-string "\\2"))))
1108 (with-current-buffer nntp-server-buffer
1109 (nnmail-find-file oldfile)
1110 ;; Update the Xref header in the article itself:
1111 (when (and (re-search-forward "^Xref: [^ ]+ " nil t)
1115 (concat group ":" old-number-string))
1119 (concat group ":" new-number-string)))
1120 ;; Save to the new file:
1121 (nnmail-write-region (point-min) (point-max) newfile))
1123 (funcall nnmail-delete-file-function oldfile)
1125 (message "Couldn't delete %s" oldfile))))
1126 ;; 2/ Update all marks for this article:
1127 ;; #### NOTE: it is possible that the new article number
1128 ;; #### already belongs to a range, whereas the corresponding
1129 ;; #### article doesn't exist (for example, if you delete an
1130 ;; #### article). For that reason, it is important to update
1131 ;; #### the ranges (meaning remove nonexistent articles) before
1132 ;; #### doing anything on them.
1133 ;; 2 a/ read articles:
1134 (let ((read (gnus-info-read info)))
1135 (setq read (gnus-remove-from-range read (list new-number)))
1136 (when (gnus-member-of-range old-number read)
1137 (setq read (gnus-remove-from-range read (list old-number)))
1138 (setq read (gnus-add-to-range read (list new-number))))
1139 (gnus-info-set-read info read))
1140 ;; 2 b/ marked articles:
1141 (let ((oldmarks (gnus-info-marks info))
1143 (while (setq mark (pop oldmarks))
1144 (setcdr mark (gnus-remove-from-range (cdr mark)
1146 (when (gnus-member-of-range old-number (cdr mark))
1147 (setcdr mark (gnus-remove-from-range (cdr mark)
1149 (setcdr mark (gnus-add-to-range (cdr mark)
1150 (list new-number))))
1151 (push mark newmarks))
1152 (gnus-info-set-marks info newmarks))
1153 ;; 3/ Update the NOV entry for this article:
1154 (unless nnml-nov-is-evil
1155 (with-current-buffer (nnml-open-nov group)
1156 (when (nnheader-find-nov-line old-number)
1157 ;; Replace the article number:
1158 (looking-at old-number-string)
1159 (replace-match new-number-string nil t)
1160 ;; Update the Xref header:
1161 (when (re-search-forward
1162 (concat "\\(Xref:[^\t\n]* \\)\\<"
1164 (concat group ":" old-number-string))
1168 (concat "\\1" group ":" new-number-string))))))
1169 ;; 4/ Possibly remove the article from the backlog:
1170 (when gnus-keep-backlog
1171 ;; #### NOTE: instead of removing the article, we could
1172 ;; #### modify the backlog to reflect the numbering change,
1173 ;; #### but I don't think it's worth it.
1174 (gnus-backlog-remove-article group-full-name old-number)
1175 (gnus-backlog-remove-article group-full-name new-number))))
1176 (setq new-number (1+ new-number)))))
1178 ;; No compaction had to be done:
1180 ;; Some articles have actually been renamed:
1181 ;; 1/ Rebuild active information:
1182 (let ((entry (assoc group nnml-group-alist))
1183 (active (cons 1 (1- new-number))))
1184 (setq nnml-group-alist (delq entry nnml-group-alist))
1185 (push (list group active) nnml-group-alist)
1186 ;; Update the active hashtable to let the *Group* buffer display
1187 ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1188 ;; gnus-newwrc-alist are out of date, since all we did is to modify
1189 ;; the info of the group internally.
1190 (gnus-set-active group-full-name active))
1192 ;; #### NOTE: normally, we should save the overview (NOV) file
1193 ;; #### here. However, there is no such function as
1194 ;; #### nnml-save-nov for a single group. Only for all
1195 ;; #### groups. Gnus inconsistency is getting worse every
1196 ;; #### day... ;; 3/ Save everything if this was not part of
1197 ;; #### a bigger operation:
1199 ;; Nothing to save (yet):
1201 ;; Something to save:
1202 ;; a/ Save the NOV databases:
1203 ;; #### NOTE: this should be done directory per directory in 1bis
1204 ;; #### above. See comment there.
1206 ;; b/ Save the active file:
1207 (nnmail-save-active nnml-group-alist nnml-active-file)
1210 (defun nnml-request-compact (&optional server)
1211 "Request compaction of all SERVER nnml groups."
1212 (interactive (list (or (nnoo-current-server 'nnml) "")))
1213 (nnmail-activate 'nnml)
1214 (unless (nnml-server-opened server)
1215 (nnml-open-server server))
1216 (setq nnml-directory (expand-file-name nnml-directory))
1217 (let* ((groups (gnus-groups-from-server
1218 (gnus-server-to-method (format "nnml:%s" server))))
1219 (first (pop groups))
1222 (while (setq group (pop groups))
1223 (nnml-request-compact-group (gnus-group-real-name group) server))
1224 (nnml-request-compact-group (gnus-group-real-name first) server t))))
1229 ;;; nnml.el ends here