(ls-lisp-insert-directory): Make -B work
[emacs.git] / lisp / gnus / nnml.el
blob4976f25795e83d84b9faa6f51f051ca275e985ff
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/>.
26 ;;; Commentary:
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
30 ;; Gnus sources.
32 ;;; Code:
34 (require 'gnus)
35 (require 'nnheader)
36 (require 'nnmail)
37 (require 'nnoo)
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")
44 (nnoo-declare nnml)
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)
51 "Mail active file.")
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
87 non-nil.")
91 (defconst nnml-version "nnml 1.0"
92 "nnml version.")
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)
115 (eval-when-compile
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 "")))
125 group)))
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
130 group
131 (decode-coding-string
132 group
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
138 group
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
149 (erase-buffer)
150 (let* ((file nil)
151 (number (length sequence))
152 (count 0)
153 (file-name-coding-system nnmail-pathname-coding-system)
154 beg article)
155 (if (stringp (car sequence))
156 'headers
157 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
158 'nov
159 (while sequence
160 (setq article (car sequence))
161 (setq file (nnml-article-to-file article))
162 (when (and file
163 (file-exists-p file)
164 (not (file-directory-p file)))
165 (insert (format "221 %d Article retrieved.\n" article))
166 (setq beg (point))
167 (nnheader-insert-head file)
168 (goto-char beg)
169 (if (re-search-forward "\n\r?\n" nil t)
170 (forward-char -1)
171 (goto-char (point-max))
172 (insert "\n\n"))
173 (insert ".\n")
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)
179 (zerop (% count 20))
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)
188 'headers))))))
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)))
194 (cond
195 ((not (file-exists-p nnml-directory))
196 (nnml-close-server)
197 (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
198 ((not (file-directory-p (file-truename nnml-directory)))
199 (nnml-close-server)
200 (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
202 (nnheader-report 'nnml "Opened server %s using directory %s"
203 server nnml-directory)
204 t)))
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)
216 (if (stringp id)
217 (when (and (setq group-num (nnml-find-group-number id server))
218 (cdr
219 (assq (cdr group-num)
220 (nnheader-article-to-file-alist
221 (setq gpath (nnml-group-pathname (car group-num)
222 nil server))))))
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)))
229 (cond
230 ((not path)
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)))
249 (cond
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))
257 (dont-check
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))))
264 (if (not active)
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 (cond
275 (group
276 (nnmail-get-new-mail 'nnml 'nnml-save-incremental-nov nnml-directory group))
277 ((nnmail-get-new-mail-per-group)
278 (nnml-request-list)
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)
291 (cond
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
298 nil server))))
299 ((assoc group nnml-group-alist)
302 (let (active)
303 (push (list group (setq active (cons 1 0)))
304 nnml-group-alist)
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)))
309 (when articles
310 (setcar active (apply 'min articles))
311 (setcdr active (apply 'max articles))))
312 (nnmail-save-active nnml-group-alist nnml-active-file)
313 t))))
315 (deffoo nnml-request-list (&optional server)
316 (save-excursion
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)
327 (save-excursion
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)
333 (active-articles
334 (nnml-directory-articles nnml-current-directory))
335 (is-old t)
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)))
352 (progn
353 ;; Allow a special target group.
354 (setq target nnmail-expiry-target)
355 (unless (eq target 'delete)
356 (with-temp-buffer
357 (nnml-request-article number group server (current-buffer))
358 (let (nnml-current-directory
359 nnml-current-group
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))
370 (if target
371 (progn
372 (nnheader-message 5 "Deleting article %s in %s"
373 number decoded)
374 (condition-case ()
375 (funcall nnmail-delete-file-function article)
376 (file-error
377 (push number rest)))
378 (setq active-articles (delq number active-articles))
379 (nnml-nov-delete-article group number))
380 (push number rest)))
381 (push number rest)))
382 (let ((active (nth 1 (assoc group nnml-group-alist))))
383 (when active
384 (setcar active (or (and active-articles
385 (apply 'min active-articles))
386 (1+ (cdr active)))))
387 (nnmail-save-active nnml-group-alist nnml-active-file))
388 (nnml-save-nov)
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)
395 result)
396 (nnml-possibly-change-directory group server)
397 (nnml-update-file-alist)
398 (and
399 (nnml-deletable-article-p group article)
400 (nnml-request-article article group server)
401 (let (nnml-current-directory
402 nnml-current-group
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))
408 result))
409 (progn
410 (nnml-possibly-change-directory group server)
411 (condition-case ()
412 (funcall nnmail-delete-file-function
413 (nnml-article-to-file article))
414 (file-error nil))
415 (nnml-nov-delete-article group article)
416 (when last
417 (nnml-save-nov)
418 (nnmail-save-active nnml-group-alist nnml-active-file))))
419 result))
421 (deffoo nnml-request-accept-article (group &optional server last)
422 (nnml-possibly-change-directory group server)
423 (nnmail-check-syntax)
424 (let (result)
425 (when nnmail-cache-accepted-message-ids
426 (nnmail-cache-insert (nnmail-fetch-field "message-id")
427 group
428 (nnmail-fetch-field "subject")
429 (nnmail-fetch-field "from")))
430 (if (stringp group)
431 (and
432 (nnmail-activate 'nnml)
433 (setq result (car (nnml-save-mail
434 (list (cons group (nnml-active-number group
435 server)))
436 server t)))
437 (progn
438 (nnmail-save-active nnml-group-alist nnml-active-file)
439 (and last (nnml-save-nov))))
440 (and
441 (nnmail-activate 'nnml)
442 (if (and (not (setq result (nnmail-article-group
443 `(lambda (group)
444 (nnml-active-number group ,server)))))
445 (yes-or-no-p "Moved to `junk' group; delete article? "))
446 (setq result 'junk)
447 (setq result (car (nnml-save-mail result server t))))
448 (when last
449 (nnmail-save-active nnml-group-alist nnml-active-file)
450 (when nnmail-cache-accepted-message-ids
451 (nnmail-cache-close))
452 (nnml-save-nov))))
453 result))
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"))
464 headers)
465 (when (ignore-errors
466 (nnmail-write-region
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.
480 (gnus-delete-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")
485 (< (string-to-number
486 (buffer-substring
487 (match-beginning 0) (match-end 0)))
488 article)
489 (zerop (forward-line 1)))))
490 (beginning-of-line)
491 (nnheader-insert-nov headers)
492 (nnml-save-nov)
493 t)))))
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)
501 (progn
502 (when force
503 ;; Delete all articles in GROUP.
504 (let ((articles
505 (directory-files
506 nnml-current-directory t
507 (concat
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)
515 decoded)
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))
535 (when (ignore-errors
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))
542 (rename-file
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)))
553 (when entry
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)
559 t))))
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)))
564 (cond
565 ((not (file-exists-p file))
566 (nnheader-report 'nnml "File %s does not exist" file))
568 (with-temp-file file
569 (nnheader-insert-file-contents file)
570 (nnmail-replace-status name value))
571 t))))
574 ;;; Internal functions.
576 (defun nnml-article-to-file (article)
577 (nnml-update-file-alist)
578 (let (file)
579 (if (setq file
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 --
586 ;; check once more.
587 (when (file-exists-p
588 (setq file (expand-file-name (number-to-string article)
589 nnml-current-directory)))
590 (nnml-update-file-alist t)
591 file)))))
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)
596 path)
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)))
601 article)))))))
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)
607 number)
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)
615 alist)
616 (or (string= (caar alist) nnml-current-group)
617 (setq number (nnml-find-id (caar alist) id server)))
618 (or number
619 (setq alist (cdr alist))))
620 (and number
621 (cons (caar alist) number))))))
623 (defun nnml-find-id (group id server)
624 (erase-buffer)
625 (let ((nov (nnml-group-pathname group nnml-nov-file-name server))
626 number found)
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))))
634 (forward-line 1)
635 (beginning-of-line)
636 (setq found t)
637 ;; We return the article number.
638 (setq number
639 (ignore-errors (read (current-buffer))))))
640 number)))
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
648 (erase-buffer)
649 (nnheader-insert-file-contents nov)
650 (if (and fetch-old
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))
655 (car articles))
656 (car (last articles)))
657 t))))))
659 (defun nnml-possibly-change-directory (group &optional server)
660 (when (and server
661 (not (nnml-server-opened server)))
662 (nnml-open-server server))
663 (if (not group)
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
689 ".gz")))
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)
695 server)
696 (cdr ga))))
697 decoded (nconc decoded (list (car ga)))))
698 (setq dec decoded))
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: ")
705 (forward-line 1))
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
709 (progn
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)
717 server)))
718 (if first
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))
722 ;; Save the article.
723 (nnmail-write-region (point-min) (point-max) file nil
724 (if (nnheader-be-verbose 5) nil 'nomesg))
725 (setq first file)))
726 ;; Generate a nov line for this article. We generate the nov
727 ;; line after saving, because nov generation destroys the
728 ;; header.
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
732 'nnml-add-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)))))
739 group-art)
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
747 ;; entry for it.
748 (unless 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
755 (sort
756 (nnml-current-group-article-to-file-alist)
757 'car-less-than-car)))
758 (setq active
759 (if nnml-article-file-alist
760 (cons (caar nnml-article-file-alist)
761 (caar (last nnml-article-file-alist)))
762 (cons 1 0)))
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))))
768 (cdr active)))
770 (defvar nnml-incremental-nov-buffer-alist nil)
772 (defun nnml-save-incremental-nov ()
773 (save-excursion
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)
789 buffer)))
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."
810 (save-excursion
811 (save-restriction
812 (unless (zerop (buffer-size))
813 (narrow-to-region
814 (goto-char (point-min))
815 (if (re-search-forward "\n\r?\n" nil t)
816 (1- (point))
817 (point-max))))
818 (let ((headers (nnheader-parse-naked-head)))
819 (mail-header-set-chars headers chars)
820 (mail-header-set-number headers number)
821 headers))))
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*"
826 (if incrementalp
827 "incremental "
829 decoded)))
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))
834 (erase-buffer)
835 (when (and (not incrementalp)
836 (file-exists-p nnml-nov-buffer-file-name))
837 (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
838 buffer))
840 (defun nnml-open-nov (group)
841 (or (let ((buffer (cdr (assoc group nnml-nov-buffer-alist))))
842 (and (buffer-name buffer)
843 buffer))
844 (let ((buffer (nnml-get-nov-buffer group)))
845 (push (cons group buffer) nnml-nov-buffer-alist)
846 buffer)))
848 (defun nnml-save-nov ()
849 (save-excursion
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)))))
860 ;;;###autoload
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))
875 (defvar nnml-files)
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)))
897 (if (not nnml-files)
898 (let* ((group (nnheader-file-to-group
899 (directory-file-name dir) nnml-directory))
900 (info (cadr (assoc group nnml-group-alist))))
901 (when info
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)
906 (unless no-active
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))
912 entry last)
913 (setq group (nnheader-file-to-group (nnml-encoded-group-name group)
914 nnml-directory)
915 entry (assoc group nnml-group-alist)
916 last (or (caadr entry) 0)
917 nnml-group-alist (delq entry nnml-group-alist))
918 (push (list group
919 (cons (or (caar nnml-files) (1+ last))
920 (max last
921 (or (caar (last nnml-files))
922 0))))
923 nnml-group-alist)))
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*"))
929 chars file headers)
930 (with-current-buffer nov-buffer
931 ;; Init the nov buffer.
932 (buffer-disable-undo)
933 (erase-buffer)
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))
938 (dolist (file files)
939 (let ((path (concat dir (cdr file))))
940 (unless (file-directory-p path)
941 (erase-buffer)
942 (nnheader-insert-file-contents path)
943 (narrow-to-region
944 (goto-char (point-min))
945 (progn
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)))
955 (widen))))
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)))
964 (when (bobp)
965 (let ((active (cadr (assoc group nnml-group-alist)))
966 num)
967 (when active
968 (if (eobp)
969 (setf (car active) (1+ (cdr active)))
970 (when (and (setq num (ignore-errors (read (current-buffer))))
971 (numberp num))
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)
978 force)
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
986 (not (file-exists-p
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)
993 (let ((list nil)
994 art)
995 (goto-char (point-min))
996 (while (not (eobp))
997 (setq art (read (current-buffer)))
998 (push art list)
999 (forward-line 1))
1000 list))))
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
1006 gnus-nov-is-evil
1007 nnml-nov-is-evil
1008 (not (file-exists-p
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)
1014 (let ((alist nil)
1015 art)
1016 (goto-char (point-min))
1017 (while (not (eobp))
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)
1021 (forward-line 1))
1022 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
1057 group
1058 (gnus-server-to-method (format "nnml:%s" server))))
1059 (info (gnus-get-info group-full-name))
1060 (new-number 1)
1061 compacted)
1062 (let ((articles nnml-article-file-alist)
1063 article)
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)))
1070 (setq compacted t)
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
1076 ;; #### manually.
1077 ;; 1/ Move the article to a new file:
1078 (let* ((oldfile (nnml-article-to-file old-number))
1079 (newfile
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
1086 ;; value.
1087 (concat
1088 "\\(" old-number-string "\\)\\(\\(\\.[a-z0-9]+\\)?\\)$")
1089 (concat new-number-string "\\2")
1090 oldfile)))
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)
1095 (re-search-forward
1096 (concat "\\<"
1097 (regexp-quote
1098 (concat group ":" old-number-string))
1099 "\\>")
1100 (point-at-eol) t))
1101 (replace-match
1102 (concat group ":" new-number-string)))
1103 ;; Save to the new file:
1104 (nnmail-write-region (point-min) (point-max) newfile))
1105 (condition-case ()
1106 (funcall nnmail-delete-file-function oldfile)
1107 (file-error
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))
1125 mark newmarks)
1126 (while (setq mark (pop oldmarks))
1127 (setcdr mark (gnus-remove-from-range (cdr mark)
1128 (list new-number)))
1129 (when (gnus-member-of-range old-number (cdr mark))
1130 (setcdr mark (gnus-remove-from-range (cdr mark)
1131 (list old-number)))
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]* \\)\\<"
1146 (regexp-quote
1147 (concat group ":" old-number-string))
1148 "\\>")
1149 (point-at-eol) t)
1150 (replace-match
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)))))
1160 (if (not compacted)
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))
1174 ;; 1 bis/
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:
1181 (if (not save)
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.
1188 (nnml-save-nov)
1189 ;; b/ Save the active file:
1190 (nnmail-save-active nnml-group-alist nnml-active-file)
1191 t)))))
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))
1203 group)
1204 (when first
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))))
1210 (provide 'nnml)
1212 ;;; nnml.el ends here