1 ;;; nnml.el --- mail spool access for Gnus
3 ;; Copyright (C) 1995-2016 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 (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 (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-request-scan
(&optional group server
)
272 (setq nnml-article-file-alist nil
)
273 (nnml-possibly-change-directory group server
)
276 (nnmail-get-new-mail 'nnml
'nnml-save-incremental-nov nnml-directory group
))
277 ((nnmail-get-new-mail-per-group)
279 (dolist (entry nnml-group-alist
)
280 (nnml-request-scan (car entry
) server
)))
282 (nnmail-get-new-mail 'nnml
'nnml-save-incremental-nov nnml-directory nil
))))
284 (deffoo nnml-close-group
(group &optional server
)
285 (setq nnml-article-file-alist nil
)
288 (deffoo nnml-request-create-group
(group &optional server args
)
289 (nnml-possibly-change-directory nil server
)
290 (nnmail-activate 'nnml
)
292 ((let ((file (directory-file-name (nnml-group-pathname group nil server
)))
293 (file-name-coding-system nnmail-pathname-coding-system
))
294 (and (file-exists-p file
)
295 (not (file-directory-p file
))))
296 (nnheader-report 'nnml
"%s is a file"
297 (directory-file-name (nnml-group-pathname group
299 ((assoc group nnml-group-alist
)
303 (push (list group
(setq active
(cons 1 0)))
305 (nnml-possibly-create-directory group server
)
306 (nnml-possibly-change-directory group server
)
307 (let* ((file-name-coding-system nnmail-pathname-coding-system
)
308 (articles (nnml-directory-articles nnml-current-directory
)))
310 (setcar active
(apply 'min articles
))
311 (setcdr active
(apply 'max articles
))))
312 (nnmail-save-active nnml-group-alist nnml-active-file
)
315 (deffoo nnml-request-list
(&optional server
)
317 (let ((nnmail-file-coding-system nnmail-active-file-coding-system
)
318 (file-name-coding-system nnmail-pathname-coding-system
))
319 (nnmail-find-file nnml-active-file
))
320 (setq nnml-group-alist
(nnmail-get-active))
323 (deffoo nnml-request-newgroups
(date &optional server
)
324 (nnml-request-list server
))
326 (deffoo nnml-request-list-newsgroups
(&optional server
)
328 (nnmail-find-file nnml-newsgroups-file
)))
330 (deffoo nnml-request-expire-articles
(articles group
&optional server force
)
331 (nnml-possibly-change-directory group server
)
332 (let* ((file-name-coding-system nnmail-pathname-coding-system
)
334 (nnml-directory-articles nnml-current-directory
))
336 (decoded (nnml-decoded-group-name group server
))
337 article rest mod-time number target
)
338 (nnmail-activate 'nnml
)
340 (setq active-articles
(sort active-articles
'<))
341 ;; Articles not listed in active-articles are already gone,
342 ;; so don't try to expire them.
343 (setq articles
(gnus-sorted-intersection articles active-articles
))
345 (while (and articles is-old
)
346 (if (and (setq article
(nnml-article-to-file
347 (setq number
(pop articles
))))
348 (setq mod-time
(nth 5 (file-attributes article
)))
349 (nnml-deletable-article-p group number
)
350 (setq is-old
(nnmail-expired-article-p group mod-time force
351 nnml-inhibit-expiry
)))
353 ;; Allow a special target group.
354 (setq target nnmail-expiry-target
)
355 (unless (eq target
'delete
)
357 (nnml-request-article number group server
(current-buffer))
358 (let (nnml-current-directory
360 nnml-article-file-alist
)
361 (when (functionp target
)
362 (setq target
(funcall target group
)))
363 (when (and target
(not (eq target
'delete
)))
364 (if (or (gnus-request-group target
)
365 (gnus-request-create-group target
))
366 (nnmail-expiry-target-group target group
)
367 (setq target nil
)))))
368 ;; Maybe directory is changed during nnmail-expiry-target-group.
369 (nnml-possibly-change-directory group server
))
372 (nnheader-message 5 "Deleting article %s in %s"
375 (funcall nnmail-delete-file-function article
)
378 (setq active-articles
(delq number active-articles
))
379 (nnml-nov-delete-article group number
))
382 (let ((active (nth 1 (assoc group nnml-group-alist
))))
384 (setcar active
(or (and active-articles
385 (apply 'min active-articles
))
387 (nnmail-save-active nnml-group-alist nnml-active-file
))
389 (nconc rest articles
)))
391 (deffoo nnml-request-move-article
392 (article group server accept-form
&optional last move-is-internal
)
393 (let ((buf (get-buffer-create " *nnml move*"))
394 (file-name-coding-system nnmail-pathname-coding-system
)
396 (nnml-possibly-change-directory group server
)
397 (nnml-update-file-alist)
399 (nnml-deletable-article-p group article
)
400 (nnml-request-article article group server
)
401 (let (nnml-current-directory
403 nnml-article-file-alist
)
404 (with-current-buffer buf
405 (insert-buffer-substring nntp-server-buffer
)
406 (setq result
(eval accept-form
))
407 (kill-buffer (current-buffer))
410 (nnml-possibly-change-directory group server
)
412 (funcall nnmail-delete-file-function
413 (nnml-article-to-file article
))
415 (nnml-nov-delete-article group article
)
418 (nnmail-save-active nnml-group-alist nnml-active-file
))))
421 (deffoo nnml-request-accept-article
(group &optional server last
)
422 (nnml-possibly-change-directory group server
)
423 (nnmail-check-syntax)
425 (when nnmail-cache-accepted-message-ids
426 (nnmail-cache-insert (nnmail-fetch-field "message-id")
428 (nnmail-fetch-field "subject")
429 (nnmail-fetch-field "from")))
432 (nnmail-activate 'nnml
)
433 (setq result
(car (nnml-save-mail
434 (list (cons group
(nnml-active-number group
438 (nnmail-save-active nnml-group-alist nnml-active-file
)
439 (and last
(nnml-save-nov))))
441 (nnmail-activate 'nnml
)
442 (if (and (not (setq result
(nnmail-article-group
444 (nnml-active-number group
,server
)))))
445 (yes-or-no-p "Moved to `junk' group; delete article? "))
447 (setq result
(car (nnml-save-mail result server t
))))
449 (nnmail-save-active nnml-group-alist nnml-active-file
)
450 (when nnmail-cache-accepted-message-ids
451 (nnmail-cache-close))
455 (deffoo nnml-request-post
(&optional server
)
456 (nnmail-do-request-post 'nnml-request-accept-article server
))
458 (deffoo nnml-request-replace-article
(article group buffer
)
459 (nnml-possibly-change-directory group
)
460 (with-current-buffer buffer
461 (nnml-possibly-create-directory group
)
462 (let ((chars (nnmail-insert-lines))
463 (art (concat (int-to-string article
) "\t"))
467 (point-min) (point-max)
468 (or (nnml-article-to-file article
)
469 (expand-file-name (int-to-string article
)
470 nnml-current-directory
))
471 nil
(if (nnheader-be-verbose 5) nil
'nomesg
))
473 (setq headers
(nnml-parse-head chars article
))
474 ;; Replace the NOV line in the NOV file.
475 (with-current-buffer (nnml-open-nov group
)
476 (goto-char (point-min))
477 (if (or (looking-at art
)
478 (search-forward (concat "\n" art
) nil t
))
479 ;; Delete the old NOV line.
481 ;; The line isn't here, so we have to find out where
482 ;; we should insert it. (This situation should never
483 ;; occur, but one likes to make sure...)
484 (while (and (looking-at "[0-9]+\t")
487 (match-beginning 0) (match-end 0)))
489 (zerop (forward-line 1)))))
491 (nnheader-insert-nov headers
)
495 (deffoo nnml-request-delete-group
(group &optional force server
)
496 (nnml-possibly-change-directory group server
)
497 (let ((file (directory-file-name nnml-current-directory
))
498 (file-name-coding-system nnmail-pathname-coding-system
))
499 (if (file-exists-p file
)
500 (if (file-directory-p file
)
503 ;; Delete all articles in GROUP.
506 nnml-current-directory t
508 nnheader-numerical-short-files
509 "\\|" (regexp-quote nnml-nov-file-name
) "$")))
510 (decoded (nnml-decoded-group-name group server
)))
511 (dolist (article articles
)
512 (when (file-writable-p article
)
513 (nnheader-message 5 "Deleting article %s in %s..."
514 (file-name-nondirectory article
)
516 (funcall nnmail-delete-file-function article
))))
517 ;; Try to delete the directory itself.
518 (ignore-errors (delete-directory nnml-current-directory
))))
519 (nnheader-report 'nnml
"%s is not a directory" file
))
520 (nnheader-report 'nnml
"No such directory: %s/" file
))
521 ;; Remove the group from all structures.
522 (setq nnml-group-alist
523 (delq (assoc group nnml-group-alist
) nnml-group-alist
)
524 nnml-current-group nil
525 nnml-current-directory nil
)
526 ;; Save the active file.
527 (nnmail-save-active nnml-group-alist nnml-active-file
))
530 (deffoo nnml-request-rename-group
(group new-name
&optional server
)
531 (nnml-possibly-change-directory group server
)
532 (let ((new-dir (nnml-group-pathname new-name nil server
))
533 (old-dir (nnml-group-pathname group nil server
))
534 (file-name-coding-system nnmail-pathname-coding-system
))
536 (make-directory new-dir t
)
538 ;; We move the articles file by file instead of renaming
539 ;; the directory -- there may be subgroups in this group.
540 ;; One might be more clever, I guess.
541 (dolist (file (nnheader-article-to-file-alist old-dir
))
543 (concat old-dir
(cdr file
))
544 (concat new-dir
(cdr file
))))
545 ;; Move .overview file.
546 (let ((overview (concat old-dir nnml-nov-file-name
)))
547 (when (file-exists-p overview
)
548 (rename-file overview
(concat new-dir nnml-nov-file-name
))))
549 (when (<= (length (directory-files old-dir
)) 2)
550 (ignore-errors (delete-directory old-dir
)))
551 ;; That went ok, so we change the internal structures.
552 (let ((entry (assoc group nnml-group-alist
)))
554 (setcar entry new-name
))
555 (setq nnml-current-directory nil
556 nnml-current-group nil
)
557 ;; Save the new group alist.
558 (nnmail-save-active nnml-group-alist nnml-active-file
)
561 (deffoo nnml-set-status
(article name value
&optional group server
)
562 (nnml-possibly-change-directory group server
)
563 (let ((file (nnml-article-to-file article
)))
565 ((not (file-exists-p file
))
566 (nnheader-report 'nnml
"File %s does not exist" file
))
569 (nnheader-insert-file-contents file
)
570 (nnmail-replace-status name value
))
574 ;;; Internal functions.
576 (defun nnml-article-to-file (article)
577 (nnml-update-file-alist)
580 (if nnml-use-compressed-files
581 (cdr (assq article nnml-article-file-alist
))
582 (number-to-string article
)))
583 (expand-file-name file nnml-current-directory
)
584 (when (not nnheader-directory-files-is-safe
)
585 ;; Just to make sure nothing went wrong when reading over NFS --
588 (setq file
(expand-file-name (number-to-string article
)
589 nnml-current-directory
)))
590 (nnml-update-file-alist t
)
593 (defun nnml-deletable-article-p (group article
)
594 "Say whether ARTICLE in GROUP can be deleted."
595 (let ((file-name-coding-system nnmail-pathname-coding-system
)
597 (when (setq path
(nnml-article-to-file article
))
598 (when (file-writable-p path
)
599 (or (not nnmail-keep-last-article
)
600 (not (eq (cdr (nth 1 (assoc group nnml-group-alist
)))
603 ;; Find an article number in the current group given the Message-ID.
604 (defun nnml-find-group-number (id server
)
605 (with-current-buffer (get-buffer-create " *nnml id*")
606 (let ((alist nnml-group-alist
)
608 ;; We want to look through all .overview files, but we want to
609 ;; start with the one in the current directory. It seems most
610 ;; likely that the article we are looking for is in that group.
611 (if (setq number
(nnml-find-id nnml-current-group id server
))
612 (cons nnml-current-group number
)
613 ;; It wasn't there, so we look through the other groups as well.
614 (while (and (not number
)
616 (or (string= (caar alist
) nnml-current-group
)
617 (setq number
(nnml-find-id (caar alist
) id server
)))
619 (setq alist
(cdr alist
))))
621 (cons (caar alist
) number
))))))
623 (defun nnml-find-id (group id server
)
625 (let ((nov (nnml-group-pathname group nnml-nov-file-name server
))
627 (when (file-exists-p nov
)
628 (nnheader-insert-file-contents nov
)
629 (while (and (not found
)
630 (search-forward id nil t
)) ; We find the ID.
631 ;; And the id is in the fourth field.
632 (if (not (and (search-backward "\t" nil t
4)
633 (not (search-backward "\t" (point-at-bol) t
))))
637 ;; We return the article number.
639 (ignore-errors (read (current-buffer))))))
642 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old
)
643 (if (or gnus-nov-is-evil nnml-nov-is-evil
)
645 (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory
)))
646 (when (file-exists-p nov
)
647 (with-current-buffer nntp-server-buffer
649 (nnheader-insert-file-contents nov
)
651 (not (numberp fetch-old
)))
652 t
; Don't remove anything.
653 (nnheader-nov-delete-outside-range
654 (if fetch-old
(max 1 (- (car articles
) fetch-old
))
656 (car (last articles
)))
659 (defun nnml-possibly-change-directory (group &optional server
)
661 (not (nnml-server-opened server
)))
662 (nnml-open-server server
))
665 (let ((pathname (nnml-group-pathname group nil server
))
666 (file-name-coding-system nnmail-pathname-coding-system
))
667 (when (not (equal pathname nnml-current-directory
))
668 (setq nnml-current-directory pathname
669 nnml-current-group group
670 nnml-article-file-alist nil
))
671 (file-exists-p nnml-current-directory
))))
673 (defun nnml-possibly-create-directory (group &optional server
)
674 (let ((dir (nnml-group-pathname group nil server
))
675 (file-name-coding-system nnmail-pathname-coding-system
))
676 (unless (file-exists-p dir
)
677 (make-directory (directory-file-name dir
) t
)
678 (nnheader-message 5 "Creating mail directory %s" dir
))))
680 (defun nnml-save-mail (group-art &optional server full-nov
)
681 "Save a mail into the groups GROUP-ART in the nnml server SERVER.
682 GROUP-ART is a list that each element is a cons of a group name and an
683 article number. This function is called narrowed to an article."
684 (let* ((chars (nnmail-insert-lines))
685 (extension (and nnml-use-compressed-files
686 (> chars nnml-compressed-files-size-threshold
)
687 (if (stringp nnml-use-compressed-files
)
688 nnml-use-compressed-files
690 decoded dec file first headers
)
691 (when nnmail-group-names-not-encoded-p
692 (dolist (ga (prog1 group-art
(setq group-art nil
)))
693 (setq group-art
(nconc group-art
694 (list (cons (nnml-encoded-group-name (car ga
)
697 decoded
(nconc decoded
(list (car ga
)))))
699 (nnmail-insert-xref group-art
)
700 (run-hooks 'nnmail-prepare-save-mail-hook
)
701 (run-hooks 'nnml-prepare-save-mail-hook
)
702 (goto-char (point-min))
703 (while (looking-at "From ")
704 (replace-match "X-From-Line: ")
706 ;; We save the article in all the groups it belongs in.
707 (dolist (ga group-art
)
708 (if nnmail-group-names-not-encoded-p
710 (nnml-possibly-create-directory (car decoded
) server
)
711 (setq file
(nnmail-group-pathname
712 (pop decoded
) nnml-directory
713 (concat (number-to-string (cdr ga
)) extension
))))
714 (nnml-possibly-create-directory (car ga
) server
)
715 (setq file
(nnml-group-pathname
716 (car ga
) (concat (number-to-string (cdr ga
)) extension
)
719 ;; It was already saved, so we just make a hard link.
720 (let ((file-name-coding-system nnmail-pathname-coding-system
))
721 (funcall nnmail-crosspost-link-function first file t
))
723 (nnmail-write-region (point-min) (point-max) file nil
724 (if (nnheader-be-verbose 5) nil
'nomesg
))
726 ;; Generate a nov line for this article. We generate the nov
727 ;; line after saving, because nov generation destroys the
729 (setq headers
(nnml-parse-head chars
))
730 ;; Output the nov line to all nov databases that should have it.
731 (let ((func (if full-nov
733 'nnml-add-incremental-nov
)))
734 (if nnmail-group-names-not-encoded-p
735 (dolist (ga group-art
)
736 (funcall func
(pop dec
) (cdr ga
) headers
))
737 (dolist (ga group-art
)
738 (funcall func
(car ga
) (cdr ga
) headers
)))))
741 (defun nnml-active-number (group &optional server
)
742 "Compute the next article number in GROUP on SERVER."
743 (let* ((encoded (if nnmail-group-names-not-encoded-p
744 (nnml-encoded-group-name group server
)))
745 (active (cadr (assoc (or encoded group
) nnml-group-alist
))))
746 ;; The group wasn't known to nnml, so we just create an active
749 ;; Perhaps the active file was corrupt? See whether
750 ;; there are any articles in this group.
751 (nnml-possibly-create-directory group server
)
752 (nnml-possibly-change-directory group server
)
753 (unless nnml-article-file-alist
754 (setq nnml-article-file-alist
756 (nnml-current-group-article-to-file-alist)
757 'car-less-than-car
)))
759 (if nnml-article-file-alist
760 (cons (caar nnml-article-file-alist
)
761 (caar (last nnml-article-file-alist
)))
763 (push (list (or encoded group
) active
) nnml-group-alist
))
764 (setcdr active
(1+ (cdr active
)))
765 (while (file-exists-p
766 (nnml-group-pathname group
(int-to-string (cdr active
)) server
))
767 (setcdr active
(1+ (cdr active
))))
770 (defvar nnml-incremental-nov-buffer-alist nil
)
772 (defun nnml-save-incremental-nov ()
774 (while nnml-incremental-nov-buffer-alist
775 (when (buffer-name (cdar nnml-incremental-nov-buffer-alist
))
776 (set-buffer (cdar nnml-incremental-nov-buffer-alist
))
777 (when (buffer-modified-p)
778 (nnmail-write-region (point-min) (point-max)
779 nnml-nov-buffer-file-name t
'nomesg
))
780 (set-buffer-modified-p nil
)
781 (kill-buffer (current-buffer)))
782 (setq nnml-incremental-nov-buffer-alist
783 (cdr nnml-incremental-nov-buffer-alist
)))))
785 (defun nnml-open-incremental-nov (group)
786 (or (cdr (assoc group nnml-incremental-nov-buffer-alist
))
787 (let ((buffer (nnml-get-nov-buffer group t
)))
788 (push (cons group buffer
) nnml-incremental-nov-buffer-alist
)
791 (defun nnml-add-incremental-nov (group article headers
)
792 "Add a nov line for the GROUP nov headers, incrementally."
793 (with-current-buffer (nnml-open-incremental-nov group
)
794 (goto-char (point-max))
795 (mail-header-set-number headers article
)
796 (nnheader-insert-nov headers
)))
798 (defun nnml-add-nov (group article headers
)
799 "Add a nov line for the GROUP base."
800 (with-current-buffer (nnml-open-nov group
)
801 (goto-char (point-max))
802 (mail-header-set-number headers article
)
803 (nnheader-insert-nov headers
)))
805 (defsubst nnml-header-value
()
806 (buffer-substring (match-end 0) (point-at-eol)))
808 (defun nnml-parse-head (chars &optional number
)
809 "Parse the head of the current buffer."
812 (unless (zerop (buffer-size))
814 (goto-char (point-min))
815 (if (re-search-forward "\n\r?\n" nil t
)
818 (let ((headers (nnheader-parse-naked-head)))
819 (mail-header-set-chars headers chars
)
820 (mail-header-set-number headers number
)
823 (defun nnml-get-nov-buffer (group &optional incrementalp
)
824 (let* ((decoded (nnml-decoded-group-name group
))
825 (buffer (get-buffer-create (format " *nnml %soverview %s*"
830 (file-name-coding-system nnmail-pathname-coding-system
))
831 (with-current-buffer buffer
832 (set (make-local-variable 'nnml-nov-buffer-file-name
)
833 (nnmail-group-pathname decoded nnml-directory nnml-nov-file-name
))
835 (when (and (not incrementalp
)
836 (file-exists-p nnml-nov-buffer-file-name
))
837 (nnheader-insert-file-contents nnml-nov-buffer-file-name
)))
840 (defun nnml-open-nov (group)
841 (or (let ((buffer (cdr (assoc group nnml-nov-buffer-alist
))))
842 (and (buffer-name buffer
)
844 (let ((buffer (nnml-get-nov-buffer group
)))
845 (push (cons group buffer
) nnml-nov-buffer-alist
)
848 (defun nnml-save-nov ()
850 (while nnml-nov-buffer-alist
851 (when (buffer-name (cdar nnml-nov-buffer-alist
))
852 (set-buffer (cdar nnml-nov-buffer-alist
))
853 (when (buffer-modified-p)
854 (nnmail-write-region (point-min) (point-max)
855 nnml-nov-buffer-file-name nil
'nomesg
))
856 (set-buffer-modified-p nil
)
857 (kill-buffer (current-buffer)))
858 (setq nnml-nov-buffer-alist
(cdr nnml-nov-buffer-alist
)))))
861 (defun nnml-generate-nov-databases (&optional server
)
862 "Generate NOV databases in all nnml directories."
863 (interactive (list (or (nnoo-current-server 'nnml
) "")))
864 ;; Read the active file to make sure we don't re-use articles
865 ;; numbers in empty groups.
866 (nnmail-activate 'nnml
)
867 (unless (nnml-server-opened server
)
868 (nnml-open-server server
))
869 (setq nnml-directory
(expand-file-name nnml-directory
))
870 ;; Recurse down the directories.
871 (nnml-generate-nov-databases-directory nnml-directory nil t
)
872 ;; Save the active file.
873 (nnmail-save-active nnml-group-alist nnml-active-file
))
876 (defun nnml-generate-nov-databases-directory (dir &optional seen no-active
)
877 "Regenerate the NOV database in DIR.
879 Unless no-active is non-nil, update the active file too."
880 (interactive (list (let ((file-name-coding-system
881 nnmail-pathname-coding-system
))
882 (read-directory-name "Regenerate NOV in: "
883 nnml-directory nil t
))))
884 (setq dir
(file-name-as-directory dir
))
885 (let ((file-name-coding-system nnmail-pathname-coding-system
))
886 ;; Only scan this sub-tree if we haven't been here yet.
887 (unless (member (file-truename dir
) seen
)
888 (push (file-truename dir
) seen
)
889 ;; We descend recursively
890 (dolist (dir (directory-files dir t nil t
))
891 (when (and (not (string-match "^\\." (file-name-nondirectory dir
)))
892 (file-directory-p dir
))
893 (nnml-generate-nov-databases-directory dir seen
)))
894 ;; Do this directory.
895 (let ((nnml-files (sort (nnheader-article-to-file-alist dir
)
896 'car-less-than-car
)))
898 (let* ((group (nnheader-file-to-group
899 (directory-file-name dir
) nnml-directory
))
900 (info (cadr (assoc group nnml-group-alist
))))
902 (setcar info
(1+ (cdr info
)))))
903 (funcall nnml-generate-active-function dir
)
904 ;; Generate the nov file.
905 (nnml-generate-nov-file dir nnml-files
)
907 (nnmail-save-active nnml-group-alist nnml-active-file
)))))))
909 (defun nnml-generate-active-info (dir)
910 ;; Update the active info for this group.
911 (let ((group (directory-file-name dir
))
913 (setq group
(nnheader-file-to-group (nnml-encoded-group-name group
)
915 entry
(assoc group nnml-group-alist
)
916 last
(or (caadr entry
) 0)
917 nnml-group-alist
(delq entry nnml-group-alist
))
919 (cons (or (caar nnml-files
) (1+ last
))
921 (or (caar (last nnml-files
))
925 (defun nnml-generate-nov-file (dir files
)
926 (let* ((dir (file-name-as-directory dir
))
927 (nov (concat dir nnml-nov-file-name
))
928 (nov-buffer (get-buffer-create " *nov*"))
930 (with-current-buffer nov-buffer
931 ;; Init the nov buffer.
932 (buffer-disable-undo)
934 (set-buffer nntp-server-buffer
)
935 ;; Delete the old NOV file.
936 (when (file-exists-p nov
)
937 (funcall nnmail-delete-file-function nov
))
939 (let ((path (concat dir
(cdr file
))))
940 (unless (file-directory-p path
)
942 (nnheader-insert-file-contents path
)
944 (goto-char (point-min))
946 (re-search-forward "\n\r?\n" nil t
)
947 (setq chars
(- (point-max) (point)))
948 (max (point-min) (1- (point)))))
949 (unless (zerop (buffer-size))
950 (goto-char (point-min))
951 (setq headers
(nnml-parse-head chars
(car file
)))
952 (with-current-buffer nov-buffer
953 (goto-char (point-max))
954 (nnheader-insert-nov headers
)))
956 (with-current-buffer nov-buffer
957 (nnmail-write-region (point-min) (point-max) nov nil
'nomesg
)
958 (kill-buffer (current-buffer))))))
960 (defun nnml-nov-delete-article (group article
)
961 (with-current-buffer (nnml-open-nov group
)
962 (when (nnheader-find-nov-line article
)
963 (delete-region (point) (progn (forward-line 1) (point)))
965 (let ((active (cadr (assoc group nnml-group-alist
)))
969 (setf (car active
) (1+ (cdr active
)))
970 (when (and (setq num
(ignore-errors (read (current-buffer))))
972 (setf (car active
) num
)))))))
975 (defun nnml-update-file-alist (&optional force
)
976 (when nnml-use-compressed-files
977 (when (or (not nnml-article-file-alist
)
979 (setq nnml-article-file-alist
980 (nnml-current-group-article-to-file-alist)))))
982 (defun nnml-directory-articles (dir)
983 "Return a list of all article files in a directory.
984 Use the nov database for that directory if available."
985 (if (or gnus-nov-is-evil nnml-nov-is-evil
987 (expand-file-name nnml-nov-file-name dir
))))
988 (nnheader-directory-articles dir
)
989 ;; build list from .overview if available
990 ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
991 ;; defvoo'd, and we might get called when it hasn't been swapped in.
992 (with-current-buffer (nnml-get-nov-buffer nnml-current-group
)
995 (goto-char (point-min))
997 (setq art
(read (current-buffer)))
1002 (defun nnml-current-group-article-to-file-alist ()
1003 "Return an alist of article/file pairs in the current group.
1004 Use the nov database for the current group if available."
1005 (if (or nnml-use-compressed-files
1009 (expand-file-name nnml-nov-file-name
1010 nnml-current-directory
))))
1011 (nnheader-article-to-file-alist nnml-current-directory
)
1012 ;; build list from .overview if available
1013 (with-current-buffer (nnml-get-nov-buffer nnml-current-group
)
1016 (goto-char (point-min))
1018 (setq art
(read (current-buffer)))
1019 ;; assume file name is unadorned (ie. not compressed etc)
1020 (push (cons art
(int-to-string art
)) alist
)
1025 ;;; Group and server compaction. -- dvl
1028 ;; #### FIXME: this function handles self Xref: entry correctly, but I don't
1029 ;; #### know how to handle external cross-references. I actually don't know if
1030 ;; #### this is handled correctly elsewhere. For instance, what happens if you
1031 ;; #### move all articles to a new group (that's what people do for manual
1032 ;; #### compaction) ?
1034 ;; #### NOTE: the function below handles the article backlog. This is
1035 ;; #### conceptually the wrong place to do it because the backend is at a
1036 ;; #### lower level. However, this is the only place where we have the needed
1037 ;; #### information to do the job. Ideally, this function should not handle
1038 ;; #### the backlog by itself, but return a list of moved groups / articles to
1039 ;; #### the caller. This will become important to avoid code duplication when
1040 ;; #### other backends get a compaction feature. Also, note that invalidating
1041 ;; #### the "original article buffer" is already done at an upper level.
1043 ;; Shouldn't `nnml-request-compact-group' be interactive? --rsteib
1045 (defun nnml-request-compact-group (group &optional server save
)
1046 (nnml-possibly-change-directory group server
)
1047 (unless nnml-article-file-alist
1048 (setq nnml-article-file-alist
1049 (sort (nnml-current-group-article-to-file-alist)
1050 'car-less-than-car
)))
1051 (if (not nnml-article-file-alist
)
1052 ;; The group is empty: do nothing but return t
1054 ;; The group is not empty:
1055 (let* ((group-full-name
1056 (gnus-group-prefixed-name
1058 (gnus-server-to-method (format "nnml:%s" server
))))
1059 (info (gnus-get-info group-full-name
))
1062 (let ((articles nnml-article-file-alist
)
1064 (while (setq article
(pop articles
))
1065 (let ((old-number (car article
)))
1066 (when (> old-number new-number
)
1067 ;; There is a gap here:
1068 (let ((old-number-string (int-to-string old-number
))
1069 (new-number-string (int-to-string new-number
)))
1071 ;; #### NOTE: `nnml-article-to-file' calls
1072 ;; #### `nnml-update-file-alist' (which in turn calls
1073 ;; #### `nnml-current-group-article-to-file-alist', which
1074 ;; #### might use the NOV database). This might turn out to be
1075 ;; #### inefficient. In that case, we will do the work
1077 ;; 1/ Move the article to a new file:
1078 (let* ((oldfile (nnml-article-to-file old-number
))
1080 (replace-regexp-in-string
1081 ;; nnml-use-compressed-files might be any string, but
1082 ;; probably it's sufficient to take into account only
1083 ;; "\\.[a-z0-9]+". Note that we can't only use the
1084 ;; value of nnml-use-compressed-files because old
1085 ;; articles might have been saved with a different
1088 "\\(" old-number-string
"\\)\\(\\(\\.[a-z0-9]+\\)?\\)$")
1089 (concat new-number-string
"\\2")
1091 (with-current-buffer nntp-server-buffer
1092 (nnmail-find-file oldfile
)
1093 ;; Update the Xref header in the article itself:
1094 (when (and (re-search-forward "^Xref: [^ ]+ " nil t
)
1098 (concat group
":" old-number-string
))
1102 (concat group
":" new-number-string
)))
1103 ;; Save to the new file:
1104 (nnmail-write-region (point-min) (point-max) newfile
))
1106 (funcall nnmail-delete-file-function oldfile
)
1108 (message "Couldn't delete %s" oldfile
))))
1109 ;; 2/ Update all marks for this article:
1110 ;; #### NOTE: it is possible that the new article number
1111 ;; #### already belongs to a range, whereas the corresponding
1112 ;; #### article doesn't exist (for example, if you delete an
1113 ;; #### article). For that reason, it is important to update
1114 ;; #### the ranges (meaning remove nonexistent articles) before
1115 ;; #### doing anything on them.
1116 ;; 2 a/ read articles:
1117 (let ((read (gnus-info-read info
)))
1118 (setq read
(gnus-remove-from-range read
(list new-number
)))
1119 (when (gnus-member-of-range old-number read
)
1120 (setq read
(gnus-remove-from-range read
(list old-number
)))
1121 (setq read
(gnus-add-to-range read
(list new-number
))))
1122 (gnus-info-set-read info read
))
1123 ;; 2 b/ marked articles:
1124 (let ((oldmarks (gnus-info-marks info
))
1126 (while (setq mark
(pop oldmarks
))
1127 (setcdr mark
(gnus-remove-from-range (cdr mark
)
1129 (when (gnus-member-of-range old-number
(cdr mark
))
1130 (setcdr mark
(gnus-remove-from-range (cdr mark
)
1132 (setcdr mark
(gnus-add-to-range (cdr mark
)
1133 (list new-number
))))
1134 (push mark newmarks
))
1135 (gnus-info-set-marks info newmarks
))
1136 ;; 3/ Update the NOV entry for this article:
1137 (unless nnml-nov-is-evil
1138 (with-current-buffer (nnml-open-nov group
)
1139 (when (nnheader-find-nov-line old-number
)
1140 ;; Replace the article number:
1141 (looking-at old-number-string
)
1142 (replace-match new-number-string nil t
)
1143 ;; Update the Xref header:
1144 (when (re-search-forward
1145 (concat "\\(Xref:[^\t\n]* \\)\\<"
1147 (concat group
":" old-number-string
))
1151 (concat "\\1" group
":" new-number-string
))))))
1152 ;; 4/ Possibly remove the article from the backlog:
1153 (when gnus-keep-backlog
1154 ;; #### NOTE: instead of removing the article, we could
1155 ;; #### modify the backlog to reflect the numbering change,
1156 ;; #### but I don't think it's worth it.
1157 (gnus-backlog-remove-article group-full-name old-number
)
1158 (gnus-backlog-remove-article group-full-name new-number
))))
1159 (setq new-number
(1+ new-number
)))))
1161 ;; No compaction had to be done:
1163 ;; Some articles have actually been renamed:
1164 ;; 1/ Rebuild active information:
1165 (let ((entry (assoc group nnml-group-alist
))
1166 (active (cons 1 (1- new-number
))))
1167 (setq nnml-group-alist
(delq entry nnml-group-alist
))
1168 (push (list group active
) nnml-group-alist
)
1169 ;; Update the active hashtable to let the *Group* buffer display
1170 ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1171 ;; gnus-newwrc-alist are out of date, since all we did is to modify
1172 ;; the info of the group internally.
1173 (gnus-set-active group-full-name active
))
1175 ;; #### NOTE: normally, we should save the overview (NOV) file
1176 ;; #### here. However, there is no such function as
1177 ;; #### nnml-save-nov for a single group. Only for all
1178 ;; #### groups. Gnus inconsistency is getting worse every
1179 ;; #### day... ;; 3/ Save everything if this was not part of
1180 ;; #### a bigger operation:
1182 ;; Nothing to save (yet):
1184 ;; Something to save:
1185 ;; a/ Save the NOV databases:
1186 ;; #### NOTE: this should be done directory per directory in 1bis
1187 ;; #### above. See comment there.
1189 ;; b/ Save the active file:
1190 (nnmail-save-active nnml-group-alist nnml-active-file
)
1193 (defun nnml-request-compact (&optional server
)
1194 "Request compaction of all SERVER nnml groups."
1195 (interactive (list (or (nnoo-current-server 'nnml
) "")))
1196 (nnmail-activate 'nnml
)
1197 (unless (nnml-server-opened server
)
1198 (nnml-open-server server
))
1199 (setq nnml-directory
(expand-file-name nnml-directory
))
1200 (let* ((groups (gnus-groups-from-server
1201 (gnus-server-to-method (format "nnml:%s" server
))))
1202 (first (pop groups
))
1205 (while (setq group
(pop groups
))
1206 (nnml-request-compact-group (gnus-group-real-name group
) server
))
1207 (nnml-request-compact-group (gnus-group-real-name first
) server t
))))
1212 ;;; nnml.el ends here