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 (/ (* count
100) 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-request-scan
(&optional group server
)
272 (setq nnml-article-file-alist nil
)
273 (nnml-possibly-change-directory group server
)
274 (nnmail-get-new-mail 'nnml
'nnml-save-incremental-nov nnml-directory group
))
276 (deffoo nnml-close-group
(group &optional server
)
277 (setq nnml-article-file-alist nil
)
280 (deffoo nnml-request-create-group
(group &optional server args
)
281 (nnml-possibly-change-directory nil server
)
282 (nnmail-activate 'nnml
)
284 ((let ((file (directory-file-name (nnml-group-pathname group nil server
)))
285 (file-name-coding-system nnmail-pathname-coding-system
))
286 (and (file-exists-p file
)
287 (not (file-directory-p file
))))
288 (nnheader-report 'nnml
"%s is a file"
289 (directory-file-name (nnml-group-pathname group
291 ((assoc group nnml-group-alist
)
295 (push (list group
(setq active
(cons 1 0)))
297 (nnml-possibly-create-directory group server
)
298 (nnml-possibly-change-directory group server
)
299 (let* ((file-name-coding-system nnmail-pathname-coding-system
)
300 (articles (nnml-directory-articles nnml-current-directory
)))
302 (setcar active
(apply 'min articles
))
303 (setcdr active
(apply 'max articles
))))
304 (nnmail-save-active nnml-group-alist nnml-active-file
)
307 (deffoo nnml-request-list
(&optional server
)
309 (let ((nnmail-file-coding-system nnmail-active-file-coding-system
)
310 (file-name-coding-system nnmail-pathname-coding-system
))
311 (nnmail-find-file nnml-active-file
))
312 (setq nnml-group-alist
(nnmail-get-active))
315 (deffoo nnml-request-newgroups
(date &optional server
)
316 (nnml-request-list server
))
318 (deffoo nnml-request-list-newsgroups
(&optional server
)
320 (nnmail-find-file nnml-newsgroups-file
)))
322 (deffoo nnml-request-expire-articles
(articles group
&optional server force
)
323 (nnml-possibly-change-directory group server
)
324 (let* ((file-name-coding-system nnmail-pathname-coding-system
)
326 (nnml-directory-articles nnml-current-directory
))
328 (decoded (nnml-decoded-group-name group server
))
329 article rest mod-time number target
)
330 (nnmail-activate 'nnml
)
332 (setq active-articles
(sort active-articles
'<))
333 ;; Articles not listed in active-articles are already gone,
334 ;; so don't try to expire them.
335 (setq articles
(gnus-sorted-intersection articles active-articles
))
337 (while (and articles is-old
)
338 (if (and (setq article
(nnml-article-to-file
339 (setq number
(pop articles
))))
340 (setq mod-time
(nth 5 (file-attributes article
)))
341 (nnml-deletable-article-p group number
)
342 (setq is-old
(nnmail-expired-article-p group mod-time force
343 nnml-inhibit-expiry
)))
345 ;; Allow a special target group.
346 (setq target nnmail-expiry-target
)
347 (unless (eq target
'delete
)
349 (nnml-request-article number group server
(current-buffer))
350 (let (nnml-current-directory
352 nnml-article-file-alist
)
353 (when (functionp target
)
354 (setq target
(funcall target group
)))
355 (when (and target
(not (eq target
'delete
)))
356 (if (or (gnus-request-group target
)
357 (gnus-request-create-group target
))
358 (nnmail-expiry-target-group target group
)
359 (setq target nil
)))))
360 ;; Maybe directory is changed during nnmail-expiry-target-group.
361 (nnml-possibly-change-directory group server
))
364 (nnheader-message 5 "Deleting article %s in %s"
367 (funcall nnmail-delete-file-function article
)
370 (setq active-articles
(delq number active-articles
))
371 (nnml-nov-delete-article group number
))
374 (let ((active (nth 1 (assoc group nnml-group-alist
))))
376 (setcar active
(or (and active-articles
377 (apply 'min active-articles
))
379 (nnmail-save-active nnml-group-alist nnml-active-file
))
381 (nconc rest articles
)))
383 (deffoo nnml-request-move-article
384 (article group server accept-form
&optional last move-is-internal
)
385 (let ((buf (get-buffer-create " *nnml move*"))
386 (file-name-coding-system nnmail-pathname-coding-system
)
388 (nnml-possibly-change-directory group server
)
389 (nnml-update-file-alist)
391 (nnml-deletable-article-p group article
)
392 (nnml-request-article article group server
)
393 (let (nnml-current-directory
395 nnml-article-file-alist
)
396 (with-current-buffer buf
397 (insert-buffer-substring nntp-server-buffer
)
398 (setq result
(eval accept-form
))
399 (kill-buffer (current-buffer))
402 (nnml-possibly-change-directory group server
)
404 (funcall nnmail-delete-file-function
405 (nnml-article-to-file article
))
407 (nnml-nov-delete-article group article
)
410 (nnmail-save-active nnml-group-alist nnml-active-file
))))
413 (deffoo nnml-request-accept-article
(group &optional server last
)
414 (nnml-possibly-change-directory group server
)
415 (nnmail-check-syntax)
417 (when nnmail-cache-accepted-message-ids
418 (nnmail-cache-insert (nnmail-fetch-field "message-id")
420 (nnmail-fetch-field "subject")
421 (nnmail-fetch-field "from")))
424 (nnmail-activate 'nnml
)
425 (setq result
(car (nnml-save-mail
426 (list (cons group
(nnml-active-number group
430 (nnmail-save-active nnml-group-alist nnml-active-file
)
431 (and last
(nnml-save-nov))))
433 (nnmail-activate 'nnml
)
434 (if (and (not (setq result
(nnmail-article-group
436 (nnml-active-number group
,server
)))))
437 (yes-or-no-p "Moved to `junk' group; delete article? "))
439 (setq result
(car (nnml-save-mail result server t
))))
441 (nnmail-save-active nnml-group-alist nnml-active-file
)
442 (when nnmail-cache-accepted-message-ids
443 (nnmail-cache-close))
447 (deffoo nnml-request-post
(&optional server
)
448 (nnmail-do-request-post 'nnml-request-accept-article server
))
450 (deffoo nnml-request-replace-article
(article group buffer
)
451 (nnml-possibly-change-directory group
)
452 (with-current-buffer buffer
453 (nnml-possibly-create-directory group
)
454 (let ((chars (nnmail-insert-lines))
455 (art (concat (int-to-string article
) "\t"))
459 (point-min) (point-max)
460 (or (nnml-article-to-file article
)
461 (expand-file-name (int-to-string article
)
462 nnml-current-directory
))
463 nil
(if (nnheader-be-verbose 5) nil
'nomesg
))
465 (setq headers
(nnml-parse-head chars article
))
466 ;; Replace the NOV line in the NOV file.
467 (with-current-buffer (nnml-open-nov group
)
468 (goto-char (point-min))
469 (if (or (looking-at art
)
470 (search-forward (concat "\n" art
) nil t
))
471 ;; Delete the old NOV line.
473 ;; The line isn't here, so we have to find out where
474 ;; we should insert it. (This situation should never
475 ;; occur, but one likes to make sure...)
476 (while (and (looking-at "[0-9]+\t")
479 (match-beginning 0) (match-end 0)))
481 (zerop (forward-line 1)))))
483 (nnheader-insert-nov headers
)
487 (deffoo nnml-request-delete-group
(group &optional force server
)
488 (nnml-possibly-change-directory group server
)
489 (let ((file (directory-file-name nnml-current-directory
))
490 (file-name-coding-system nnmail-pathname-coding-system
))
491 (if (file-exists-p file
)
492 (if (file-directory-p file
)
495 ;; Delete all articles in GROUP.
498 nnml-current-directory t
500 nnheader-numerical-short-files
501 "\\|" (regexp-quote nnml-nov-file-name
) "$")))
502 (decoded (nnml-decoded-group-name group server
)))
503 (dolist (article articles
)
504 (when (file-writable-p article
)
505 (nnheader-message 5 "Deleting article %s in %s..."
506 (file-name-nondirectory article
)
508 (funcall nnmail-delete-file-function article
))))
509 ;; Try to delete the directory itself.
510 (ignore-errors (delete-directory nnml-current-directory
))))
511 (nnheader-report 'nnml
"%s is not a directory" file
))
512 (nnheader-report 'nnml
"No such directory: %s/" file
))
513 ;; Remove the group from all structures.
514 (setq nnml-group-alist
515 (delq (assoc group nnml-group-alist
) nnml-group-alist
)
516 nnml-current-group nil
517 nnml-current-directory nil
)
518 ;; Save the active file.
519 (nnmail-save-active nnml-group-alist nnml-active-file
))
522 (deffoo nnml-request-rename-group
(group new-name
&optional server
)
523 (nnml-possibly-change-directory group server
)
524 (let ((new-dir (nnml-group-pathname new-name nil server
))
525 (old-dir (nnml-group-pathname group nil server
))
526 (file-name-coding-system nnmail-pathname-coding-system
))
528 (make-directory new-dir t
)
530 ;; We move the articles file by file instead of renaming
531 ;; the directory -- there may be subgroups in this group.
532 ;; One might be more clever, I guess.
533 (dolist (file (nnheader-article-to-file-alist old-dir
))
535 (concat old-dir
(cdr file
))
536 (concat new-dir
(cdr file
))))
537 ;; Move .overview file.
538 (let ((overview (concat old-dir nnml-nov-file-name
)))
539 (when (file-exists-p overview
)
540 (rename-file overview
(concat new-dir nnml-nov-file-name
))))
541 (when (<= (length (directory-files old-dir
)) 2)
542 (ignore-errors (delete-directory old-dir
)))
543 ;; That went ok, so we change the internal structures.
544 (let ((entry (assoc group nnml-group-alist
)))
546 (setcar entry new-name
))
547 (setq nnml-current-directory nil
548 nnml-current-group nil
)
549 ;; Save the new group alist.
550 (nnmail-save-active nnml-group-alist nnml-active-file
)
553 (deffoo nnml-set-status
(article name value
&optional group server
)
554 (nnml-possibly-change-directory group server
)
555 (let ((file (nnml-article-to-file article
)))
557 ((not (file-exists-p file
))
558 (nnheader-report 'nnml
"File %s does not exist" file
))
561 (nnheader-insert-file-contents file
)
562 (nnmail-replace-status name value
))
566 ;;; Internal functions.
568 (defun nnml-article-to-file (article)
569 (nnml-update-file-alist)
572 (if nnml-use-compressed-files
573 (cdr (assq article nnml-article-file-alist
))
574 (number-to-string article
)))
575 (expand-file-name file nnml-current-directory
)
576 (when (not nnheader-directory-files-is-safe
)
577 ;; Just to make sure nothing went wrong when reading over NFS --
580 (setq file
(expand-file-name (number-to-string article
)
581 nnml-current-directory
)))
582 (nnml-update-file-alist t
)
585 (defun nnml-deletable-article-p (group article
)
586 "Say whether ARTICLE in GROUP can be deleted."
587 (let ((file-name-coding-system nnmail-pathname-coding-system
)
589 (when (setq path
(nnml-article-to-file article
))
590 (when (file-writable-p path
)
591 (or (not nnmail-keep-last-article
)
592 (not (eq (cdr (nth 1 (assoc group nnml-group-alist
)))
595 ;; Find an article number in the current group given the Message-ID.
596 (defun nnml-find-group-number (id server
)
597 (with-current-buffer (get-buffer-create " *nnml id*")
598 (let ((alist nnml-group-alist
)
600 ;; We want to look through all .overview files, but we want to
601 ;; start with the one in the current directory. It seems most
602 ;; likely that the article we are looking for is in that group.
603 (if (setq number
(nnml-find-id nnml-current-group id server
))
604 (cons nnml-current-group number
)
605 ;; It wasn't there, so we look through the other groups as well.
606 (while (and (not number
)
608 (or (string= (caar alist
) nnml-current-group
)
609 (setq number
(nnml-find-id (caar alist
) id server
)))
611 (setq alist
(cdr alist
))))
613 (cons (caar alist
) number
))))))
615 (defun nnml-find-id (group id server
)
617 (let ((nov (nnml-group-pathname group nnml-nov-file-name server
))
619 (when (file-exists-p nov
)
620 (nnheader-insert-file-contents nov
)
621 (while (and (not found
)
622 (search-forward id nil t
)) ; We find the ID.
623 ;; And the id is in the fourth field.
624 (if (not (and (search-backward "\t" nil t
4)
625 (not (search-backward "\t" (point-at-bol) t
))))
629 ;; We return the article number.
631 (ignore-errors (read (current-buffer))))))
634 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old
)
635 (if (or gnus-nov-is-evil nnml-nov-is-evil
)
637 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory
)))
638 (when (file-exists-p nov
)
639 (with-current-buffer nntp-server-buffer
641 (nnheader-insert-file-contents nov
)
643 (not (numberp fetch-old
)))
644 t
; Don't remove anything.
645 (nnheader-nov-delete-outside-range
646 (if fetch-old
(max 1 (- (car articles
) fetch-old
))
648 (car (last articles
)))
651 (defun nnml-possibly-change-directory (group &optional server
)
653 (not (nnml-server-opened server
)))
654 (nnml-open-server server
))
657 (let ((pathname (nnml-group-pathname group nil server
))
658 (file-name-coding-system nnmail-pathname-coding-system
))
659 (when (not (equal pathname nnml-current-directory
))
660 (setq nnml-current-directory pathname
661 nnml-current-group group
662 nnml-article-file-alist nil
))
663 (file-exists-p nnml-current-directory
))))
665 (defun nnml-possibly-create-directory (group &optional server
)
666 (let ((dir (nnml-group-pathname group nil server
))
667 (file-name-coding-system nnmail-pathname-coding-system
))
668 (unless (file-exists-p dir
)
669 (make-directory (directory-file-name dir
) t
)
670 (nnheader-message 5 "Creating mail directory %s" dir
))))
672 (defun nnml-save-mail (group-art &optional server full-nov
)
673 "Save a mail into the groups GROUP-ART in the nnml server SERVER.
674 GROUP-ART is a list that each element is a cons of a group name and an
675 article number. This function is called narrowed to an article."
676 (let* ((chars (nnmail-insert-lines))
677 (extension (and nnml-use-compressed-files
678 (> chars nnml-compressed-files-size-threshold
)
679 (if (stringp nnml-use-compressed-files
)
680 nnml-use-compressed-files
682 decoded dec file first headers
)
683 (when nnmail-group-names-not-encoded-p
684 (dolist (ga (prog1 group-art
(setq group-art nil
)))
685 (setq group-art
(nconc group-art
686 (list (cons (nnml-encoded-group-name (car ga
)
689 decoded
(nconc decoded
(list (car ga
)))))
691 (nnmail-insert-xref group-art
)
692 (run-hooks 'nnmail-prepare-save-mail-hook
)
693 (run-hooks 'nnml-prepare-save-mail-hook
)
694 (goto-char (point-min))
695 (while (looking-at "From ")
696 (replace-match "X-From-Line: ")
698 ;; We save the article in all the groups it belongs in.
699 (dolist (ga group-art
)
700 (if nnmail-group-names-not-encoded-p
702 (nnml-possibly-create-directory (car decoded
) server
)
703 (setq file
(nnmail-group-pathname
704 (pop decoded
) nnml-directory
705 (concat (number-to-string (cdr ga
)) extension
))))
706 (nnml-possibly-create-directory (car ga
) server
)
707 (setq file
(nnml-group-pathname
708 (car ga
) (concat (number-to-string (cdr ga
)) extension
)
711 ;; It was already saved, so we just make a hard link.
712 (let ((file-name-coding-system nnmail-pathname-coding-system
))
713 (funcall nnmail-crosspost-link-function first file t
))
715 (nnmail-write-region (point-min) (point-max) file nil
716 (if (nnheader-be-verbose 5) nil
'nomesg
))
718 ;; Generate a nov line for this article. We generate the nov
719 ;; line after saving, because nov generation destroys the
721 (setq headers
(nnml-parse-head chars
))
722 ;; Output the nov line to all nov databases that should have it.
723 (let ((func (if full-nov
725 'nnml-add-incremental-nov
)))
726 (if nnmail-group-names-not-encoded-p
727 (dolist (ga group-art
)
728 (funcall func
(pop dec
) (cdr ga
) headers
))
729 (dolist (ga group-art
)
730 (funcall func
(car ga
) (cdr ga
) headers
)))))
733 (defun nnml-active-number (group &optional server
)
734 "Compute the next article number in GROUP on SERVER."
735 (let* ((encoded (if nnmail-group-names-not-encoded-p
736 (nnml-encoded-group-name group server
)))
737 (active (cadr (assoc (or encoded group
) nnml-group-alist
))))
738 ;; The group wasn't known to nnml, so we just create an active
741 ;; Perhaps the active file was corrupt? See whether
742 ;; there are any articles in this group.
743 (nnml-possibly-create-directory group server
)
744 (nnml-possibly-change-directory group server
)
745 (unless nnml-article-file-alist
746 (setq nnml-article-file-alist
748 (nnml-current-group-article-to-file-alist)
749 'car-less-than-car
)))
751 (if nnml-article-file-alist
752 (cons (caar nnml-article-file-alist
)
753 (caar (last nnml-article-file-alist
)))
755 (push (list (or encoded group
) active
) nnml-group-alist
))
756 (setcdr active
(1+ (cdr active
)))
757 (while (file-exists-p
758 (nnml-group-pathname group
(int-to-string (cdr active
)) server
))
759 (setcdr active
(1+ (cdr active
))))
762 (defvar nnml-incremental-nov-buffer-alist nil
)
764 (defun nnml-save-incremental-nov ()
766 (while nnml-incremental-nov-buffer-alist
767 (when (buffer-name (cdar nnml-incremental-nov-buffer-alist
))
768 (set-buffer (cdar nnml-incremental-nov-buffer-alist
))
769 (when (buffer-modified-p)
770 (nnmail-write-region (point-min) (point-max)
771 nnml-nov-buffer-file-name t
'nomesg
))
772 (set-buffer-modified-p nil
)
773 (kill-buffer (current-buffer)))
774 (setq nnml-incremental-nov-buffer-alist
775 (cdr nnml-incremental-nov-buffer-alist
)))))
777 (defun nnml-open-incremental-nov (group)
778 (or (cdr (assoc group nnml-incremental-nov-buffer-alist
))
779 (let ((buffer (nnml-get-nov-buffer group t
)))
780 (push (cons group buffer
) nnml-incremental-nov-buffer-alist
)
783 (defun nnml-add-incremental-nov (group article headers
)
784 "Add a nov line for the GROUP nov headers, incrementally."
785 (with-current-buffer (nnml-open-incremental-nov group
)
786 (goto-char (point-max))
787 (mail-header-set-number headers article
)
788 (nnheader-insert-nov headers
)))
790 (defun nnml-add-nov (group article headers
)
791 "Add a nov line for the GROUP base."
792 (with-current-buffer (nnml-open-nov group
)
793 (goto-char (point-max))
794 (mail-header-set-number headers article
)
795 (nnheader-insert-nov headers
)))
797 (defsubst nnml-header-value
()
798 (buffer-substring (match-end 0) (point-at-eol)))
800 (defun nnml-parse-head (chars &optional number
)
801 "Parse the head of the current buffer."
804 (unless (zerop (buffer-size))
806 (goto-char (point-min))
807 (if (re-search-forward "\n\r?\n" nil t
)
810 (let ((headers (nnheader-parse-naked-head)))
811 (mail-header-set-chars headers chars
)
812 (mail-header-set-number headers number
)
815 (defun nnml-get-nov-buffer (group &optional incrementalp
)
816 (let* ((decoded (nnml-decoded-group-name group
))
817 (buffer (get-buffer-create (format " *nnml %soverview %s*"
822 (file-name-coding-system nnmail-pathname-coding-system
))
823 (with-current-buffer buffer
824 (set (make-local-variable 'nnml-nov-buffer-file-name
)
825 (nnmail-group-pathname decoded nnml-directory nnml-nov-file-name
))
827 (when (and (not incrementalp
)
828 (file-exists-p nnml-nov-buffer-file-name
))
829 (nnheader-insert-file-contents nnml-nov-buffer-file-name
)))
832 (defun nnml-open-nov (group)
833 (or (let ((buffer (cdr (assoc group nnml-nov-buffer-alist
))))
834 (and (buffer-name buffer
)
836 (let ((buffer (nnml-get-nov-buffer group
)))
837 (push (cons group buffer
) nnml-nov-buffer-alist
)
840 (defun nnml-save-nov ()
842 (while nnml-nov-buffer-alist
843 (when (buffer-name (cdar nnml-nov-buffer-alist
))
844 (set-buffer (cdar nnml-nov-buffer-alist
))
845 (when (buffer-modified-p)
846 (nnmail-write-region (point-min) (point-max)
847 nnml-nov-buffer-file-name nil
'nomesg
))
848 (set-buffer-modified-p nil
)
849 (kill-buffer (current-buffer)))
850 (setq nnml-nov-buffer-alist
(cdr nnml-nov-buffer-alist
)))))
853 (defun nnml-generate-nov-databases (&optional server
)
854 "Generate NOV databases in all nnml directories."
855 (interactive (list (or (nnoo-current-server 'nnml
) "")))
856 ;; Read the active file to make sure we don't re-use articles
857 ;; numbers in empty groups.
858 (nnmail-activate 'nnml
)
859 (unless (nnml-server-opened server
)
860 (nnml-open-server server
))
861 (setq nnml-directory
(expand-file-name nnml-directory
))
862 ;; Recurse down the directories.
863 (nnml-generate-nov-databases-directory nnml-directory nil t
)
864 ;; Save the active file.
865 (nnmail-save-active nnml-group-alist nnml-active-file
))
868 (defun nnml-generate-nov-databases-directory (dir &optional seen no-active
)
869 "Regenerate the NOV database in DIR.
871 Unless no-active is non-nil, update the active file too."
872 (interactive (list (let ((file-name-coding-system
873 nnmail-pathname-coding-system
))
874 (read-directory-name "Regenerate NOV in: "
875 nnml-directory nil t
))))
876 (setq dir
(file-name-as-directory dir
))
877 (let ((file-name-coding-system nnmail-pathname-coding-system
))
878 ;; Only scan this sub-tree if we haven't been here yet.
879 (unless (member (file-truename dir
) seen
)
880 (push (file-truename dir
) seen
)
881 ;; We descend recursively
882 (dolist (dir (directory-files dir t nil t
))
883 (when (and (not (string-match "^\\." (file-name-nondirectory dir
)))
884 (file-directory-p dir
))
885 (nnml-generate-nov-databases-directory dir seen
)))
886 ;; Do this directory.
887 (let ((nnml-files (sort (nnheader-article-to-file-alist dir
)
888 'car-less-than-car
)))
890 (let* ((group (nnheader-file-to-group
891 (directory-file-name dir
) nnml-directory
))
892 (info (cadr (assoc group nnml-group-alist
))))
894 (setcar info
(1+ (cdr info
)))))
895 (funcall nnml-generate-active-function dir
)
896 ;; Generate the nov file.
897 (nnml-generate-nov-file dir nnml-files
)
899 (nnmail-save-active nnml-group-alist nnml-active-file
)))))))
901 (defun nnml-generate-active-info (dir)
902 ;; Update the active info for this group.
903 (let ((group (directory-file-name dir
))
905 (setq group
(nnheader-file-to-group (nnml-encoded-group-name group
)
907 entry
(assoc group nnml-group-alist
)
908 last
(or (caadr entry
) 0)
909 nnml-group-alist
(delq entry nnml-group-alist
))
911 (cons (or (caar nnml-files
) (1+ last
))
913 (or (caar (last nnml-files
))
917 (defun nnml-generate-nov-file (dir files
)
918 (let* ((dir (file-name-as-directory dir
))
919 (nov (concat dir nnml-nov-file-name
))
920 (nov-buffer (get-buffer-create " *nov*"))
922 (with-current-buffer nov-buffer
923 ;; Init the nov buffer.
924 (buffer-disable-undo)
926 (set-buffer nntp-server-buffer
)
927 ;; Delete the old NOV file.
928 (when (file-exists-p nov
)
929 (funcall nnmail-delete-file-function nov
))
931 (let ((path (concat dir
(cdr file
))))
932 (unless (file-directory-p path
)
934 (nnheader-insert-file-contents path
)
936 (goto-char (point-min))
938 (re-search-forward "\n\r?\n" nil t
)
939 (setq chars
(- (point-max) (point)))
940 (max (point-min) (1- (point)))))
941 (unless (zerop (buffer-size))
942 (goto-char (point-min))
943 (setq headers
(nnml-parse-head chars
(car file
)))
944 (with-current-buffer nov-buffer
945 (goto-char (point-max))
946 (nnheader-insert-nov headers
)))
948 (with-current-buffer nov-buffer
949 (nnmail-write-region (point-min) (point-max) nov nil
'nomesg
)
950 (kill-buffer (current-buffer))))))
952 (defun nnml-nov-delete-article (group article
)
953 (with-current-buffer (nnml-open-nov group
)
954 (when (nnheader-find-nov-line article
)
955 (delete-region (point) (progn (forward-line 1) (point)))
957 (let ((active (cadr (assoc group nnml-group-alist
)))
961 (setf (car active
) (1+ (cdr active
)))
962 (when (and (setq num
(ignore-errors (read (current-buffer))))
964 (setf (car active
) num
)))))))
967 (defun nnml-update-file-alist (&optional force
)
968 (when nnml-use-compressed-files
969 (when (or (not nnml-article-file-alist
)
971 (setq nnml-article-file-alist
972 (nnml-current-group-article-to-file-alist)))))
974 (defun nnml-directory-articles (dir)
975 "Return a list of all article files in a directory.
976 Use the nov database for that directory if available."
977 (if (or gnus-nov-is-evil nnml-nov-is-evil
979 (expand-file-name nnml-nov-file-name dir
))))
980 (nnheader-directory-articles dir
)
981 ;; build list from .overview if available
982 ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
983 ;; defvoo'd, and we might get called when it hasn't been swapped in.
984 (with-current-buffer (nnml-get-nov-buffer nnml-current-group
)
987 (goto-char (point-min))
989 (setq art
(read (current-buffer)))
994 (defun nnml-current-group-article-to-file-alist ()
995 "Return an alist of article/file pairs in the current group.
996 Use the nov database for the current group if available."
997 (if (or nnml-use-compressed-files
1001 (expand-file-name nnml-nov-file-name
1002 nnml-current-directory
))))
1003 (nnheader-article-to-file-alist nnml-current-directory
)
1004 ;; build list from .overview if available
1005 (with-current-buffer (nnml-get-nov-buffer nnml-current-group
)
1008 (goto-char (point-min))
1010 (setq art
(read (current-buffer)))
1011 ;; assume file name is unadorned (ie. not compressed etc)
1012 (push (cons art
(int-to-string art
)) alist
)
1017 ;;; Group and server compaction. -- dvl
1020 ;; #### FIXME: this function handles self Xref: entry correctly, but I don't
1021 ;; #### know how to handle external cross-references. I actually don't know if
1022 ;; #### this is handled correctly elsewhere. For instance, what happens if you
1023 ;; #### move all articles to a new group (that's what people do for manual
1024 ;; #### compaction) ?
1026 ;; #### NOTE: the function below handles the article backlog. This is
1027 ;; #### conceptually the wrong place to do it because the backend is at a
1028 ;; #### lower level. However, this is the only place where we have the needed
1029 ;; #### information to do the job. Ideally, this function should not handle
1030 ;; #### the backlog by itself, but return a list of moved groups / articles to
1031 ;; #### the caller. This will become important to avoid code duplication when
1032 ;; #### other backends get a compaction feature. Also, note that invalidating
1033 ;; #### the "original article buffer" is already done at an upper level.
1035 ;; Shouldn't `nnml-request-compact-group' be interactive? --rsteib
1037 (defun nnml-request-compact-group (group &optional server save
)
1038 (nnml-possibly-change-directory group server
)
1039 (unless nnml-article-file-alist
1040 (setq nnml-article-file-alist
1041 (sort (nnml-current-group-article-to-file-alist)
1042 'car-less-than-car
)))
1043 (if (not nnml-article-file-alist
)
1044 ;; The group is empty: do nothing but return t
1046 ;; The group is not empty:
1047 (let* ((group-full-name
1048 (gnus-group-prefixed-name
1050 (gnus-server-to-method (format "nnml:%s" server
))))
1051 (info (gnus-get-info group-full-name
))
1054 (let ((articles nnml-article-file-alist
)
1056 (while (setq article
(pop articles
))
1057 (let ((old-number (car article
)))
1058 (when (> old-number new-number
)
1059 ;; There is a gap here:
1060 (let ((old-number-string (int-to-string old-number
))
1061 (new-number-string (int-to-string new-number
)))
1063 ;; #### NOTE: `nnml-article-to-file' calls
1064 ;; #### `nnml-update-file-alist' (which in turn calls
1065 ;; #### `nnml-current-group-article-to-file-alist', which
1066 ;; #### might use the NOV database). This might turn out to be
1067 ;; #### inefficient. In that case, we will do the work
1069 ;; 1/ Move the article to a new file:
1070 (let* ((oldfile (nnml-article-to-file old-number
))
1072 (gnus-replace-in-string
1074 ;; nnml-use-compressed-files might be any string, but
1075 ;; probably it's sufficient to take into account only
1076 ;; "\\.[a-z0-9]+". Note that we can't only use the
1077 ;; value of nnml-use-compressed-files because old
1078 ;; articles might have been saved with a different
1081 "\\(" old-number-string
"\\)\\(\\(\\.[a-z0-9]+\\)?\\)$")
1082 (concat new-number-string
"\\2"))))
1083 (with-current-buffer nntp-server-buffer
1084 (nnmail-find-file oldfile
)
1085 ;; Update the Xref header in the article itself:
1086 (when (and (re-search-forward "^Xref: [^ ]+ " nil t
)
1090 (concat group
":" old-number-string
))
1094 (concat group
":" new-number-string
)))
1095 ;; Save to the new file:
1096 (nnmail-write-region (point-min) (point-max) newfile
))
1098 (funcall nnmail-delete-file-function oldfile
)
1100 (message "Couldn't delete %s" oldfile
))))
1101 ;; 2/ Update all marks for this article:
1102 ;; #### NOTE: it is possible that the new article number
1103 ;; #### already belongs to a range, whereas the corresponding
1104 ;; #### article doesn't exist (for example, if you delete an
1105 ;; #### article). For that reason, it is important to update
1106 ;; #### the ranges (meaning remove nonexistent articles) before
1107 ;; #### doing anything on them.
1108 ;; 2 a/ read articles:
1109 (let ((read (gnus-info-read info
)))
1110 (setq read
(gnus-remove-from-range read
(list new-number
)))
1111 (when (gnus-member-of-range old-number read
)
1112 (setq read
(gnus-remove-from-range read
(list old-number
)))
1113 (setq read
(gnus-add-to-range read
(list new-number
))))
1114 (gnus-info-set-read info read
))
1115 ;; 2 b/ marked articles:
1116 (let ((oldmarks (gnus-info-marks info
))
1118 (while (setq mark
(pop oldmarks
))
1119 (setcdr mark
(gnus-remove-from-range (cdr mark
)
1121 (when (gnus-member-of-range old-number
(cdr mark
))
1122 (setcdr mark
(gnus-remove-from-range (cdr mark
)
1124 (setcdr mark
(gnus-add-to-range (cdr mark
)
1125 (list new-number
))))
1126 (push mark newmarks
))
1127 (gnus-info-set-marks info newmarks
))
1128 ;; 3/ Update the NOV entry for this article:
1129 (unless nnml-nov-is-evil
1130 (with-current-buffer (nnml-open-nov group
)
1131 (when (nnheader-find-nov-line old-number
)
1132 ;; Replace the article number:
1133 (looking-at old-number-string
)
1134 (replace-match new-number-string nil t
)
1135 ;; Update the Xref header:
1136 (when (re-search-forward
1137 (concat "\\(Xref:[^\t\n]* \\)\\<"
1139 (concat group
":" old-number-string
))
1143 (concat "\\1" group
":" new-number-string
))))))
1144 ;; 4/ Possibly remove the article from the backlog:
1145 (when gnus-keep-backlog
1146 ;; #### NOTE: instead of removing the article, we could
1147 ;; #### modify the backlog to reflect the numbering change,
1148 ;; #### but I don't think it's worth it.
1149 (gnus-backlog-remove-article group-full-name old-number
)
1150 (gnus-backlog-remove-article group-full-name new-number
))))
1151 (setq new-number
(1+ new-number
)))))
1153 ;; No compaction had to be done:
1155 ;; Some articles have actually been renamed:
1156 ;; 1/ Rebuild active information:
1157 (let ((entry (assoc group nnml-group-alist
))
1158 (active (cons 1 (1- new-number
))))
1159 (setq nnml-group-alist
(delq entry nnml-group-alist
))
1160 (push (list group active
) nnml-group-alist
)
1161 ;; Update the active hashtable to let the *Group* buffer display
1162 ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1163 ;; gnus-newwrc-alist are out of date, since all we did is to modify
1164 ;; the info of the group internally.
1165 (gnus-set-active group-full-name active
))
1167 ;; #### NOTE: normally, we should save the overview (NOV) file
1168 ;; #### here. However, there is no such function as
1169 ;; #### nnml-save-nov for a single group. Only for all
1170 ;; #### groups. Gnus inconsistency is getting worse every
1171 ;; #### day... ;; 3/ Save everything if this was not part of
1172 ;; #### a bigger operation:
1174 ;; Nothing to save (yet):
1176 ;; Something to save:
1177 ;; a/ Save the NOV databases:
1178 ;; #### NOTE: this should be done directory per directory in 1bis
1179 ;; #### above. See comment there.
1181 ;; b/ Save the active file:
1182 (nnmail-save-active nnml-group-alist nnml-active-file
)
1185 (defun nnml-request-compact (&optional server
)
1186 "Request compaction of all SERVER nnml groups."
1187 (interactive (list (or (nnoo-current-server 'nnml
) "")))
1188 (nnmail-activate 'nnml
)
1189 (unless (nnml-server-opened server
)
1190 (nnml-open-server server
))
1191 (setq nnml-directory
(expand-file-name nnml-directory
))
1192 (let* ((groups (gnus-groups-from-server
1193 (gnus-server-to-method (format "nnml:%s" server
))))
1194 (first (pop groups
))
1197 (while (setq group
(pop groups
))
1198 (nnml-request-compact-group (gnus-group-real-name group
) server
))
1199 (nnml-request-compact-group (gnus-group-real-name first
) server t
))))
1204 ;;; nnml.el ends here