1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
6 ;; Scott Byer <byer@mv.us.adobe.com>
7 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
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 2, or (at your option)
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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
32 (eval-when-compile (require 'cl))
33 (eval-when-compile (require 'gnus-clfns))
43 (autoload 'gnus-intersection "gnus-range"))
45 (nnoo-declare nnfolder)
47 (defvoo nnfolder-directory (expand-file-name message-directory)
48 "The name of the nnfolder directory.")
50 (defvoo nnfolder-nov-directory nil
51 "The name of the nnfolder NOV directory.
52 If nil, `nnfolder-directory' is used.")
54 (defvoo nnfolder-active-file
55 (nnheader-concat nnfolder-directory "active")
56 "The name of the active file.")
58 ;; I renamed this variable to something more in keeping with the general GNU
61 (defvoo nnfolder-ignore-active-file nil
62 "If non-nil, the active file is ignored.
63 This causes nnfolder to do some extra work in order to determine the
64 true active ranges of an mbox file. Note that the active file is
65 still saved, but its values are not used. This costs some extra time
66 when scanning an mbox when opening it.")
68 (defvoo nnfolder-distrust-mbox nil
69 "If non-nil, the folder will be distrusted.
70 This means that nnfolder will not trust the user with respect to
71 inserting unaccounted for mail in the middle of an mbox file. This
72 can greatly slow down scans, which now must scan the entire file for
73 unmarked messages. When nil, scans occur forward from the last marked
74 message, a huge time saver for large mailboxes.")
76 (defvoo nnfolder-newsgroups-file
77 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
78 "Mail newsgroups description file.")
80 (defvoo nnfolder-get-new-mail t
81 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
83 (defvoo nnfolder-prepare-save-mail-hook nil
84 "Hook run narrowed to an article before saving.")
86 (defvoo nnfolder-save-buffer-hook nil
87 "Hook run before saving the nnfolder mbox buffer.")
89 (defvoo nnfolder-inhibit-expiry nil
90 "If non-nil, inhibit expiry.")
94 (defconst nnfolder-version "nnfolder 2.0"
97 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
98 "String used to demarcate what the article number for a message is.")
100 (defvoo nnfolder-current-group nil)
101 (defvoo nnfolder-current-buffer nil)
102 (defvoo nnfolder-status-string "")
103 (defvoo nnfolder-group-alist nil)
104 (defvoo nnfolder-buffer-alist nil)
105 (defvoo nnfolder-scantime-alist nil)
106 (defvoo nnfolder-active-timestamp nil)
107 (defvoo nnfolder-active-file-coding-system nnheader-text-coding-system)
108 (defvoo nnfolder-active-file-coding-system-for-write
109 nnmail-active-file-coding-system)
110 (defvoo nnfolder-file-coding-system nnheader-text-coding-system)
111 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
112 "Coding system for save nnfolder file.
113 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
115 (defvoo nnfolder-nov-is-evil nil
116 "If non-nil, Gnus will never generate and use nov databases for mail groups.
117 Using nov databases will speed up header fetching considerably.
118 This variable shouldn't be flipped much. If you have, for some reason,
119 set this to t, and want to set it to nil again, you should always run
120 the `nnfolder-generate-active-file' command. The function will go
121 through all nnfolder directories and generate nov databases for them
122 all. This may very well take some time.")
124 (defvoo nnfolder-nov-file-suffix ".nov")
126 (defvoo nnfolder-nov-buffer-alist nil)
128 (defvar nnfolder-nov-buffer-file-name nil)
132 ;;; Interface functions
134 (nnoo-define-basics nnfolder)
136 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
138 (set-buffer nntp-server-buffer)
140 (let (article start stop)
141 (nnfolder-possibly-change-group group server)
142 (when nnfolder-current-buffer
143 (set-buffer nnfolder-current-buffer)
144 (goto-char (point-min))
145 (if (stringp (car articles))
147 (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
149 (setq articles (gnus-sorted-intersection
150 ;; Is ARTICLES sorted?
152 (nnfolder-existing-articles)))
153 (while (setq article (pop articles))
154 (set-buffer nnfolder-current-buffer)
155 (when (nnfolder-goto-article article)
157 (setq stop (if (search-forward "\n\n" nil t)
160 (set-buffer nntp-server-buffer)
161 (insert (format "221 %d Article retrieved.\n" article))
162 (insert-buffer-substring nnfolder-current-buffer start stop)
163 (goto-char (point-max))
165 (set-buffer nntp-server-buffer)
166 (nnheader-fold-continuation-lines)
169 (deffoo nnfolder-open-server (server &optional defs)
170 (nnoo-change-server 'nnfolder server defs)
171 (nnmail-activate 'nnfolder t)
172 (gnus-make-directory nnfolder-directory)
173 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
174 (and nnfolder-nov-directory
175 (gnus-make-directory nnfolder-nov-directory)))
177 ((not (file-exists-p nnfolder-directory))
178 (nnfolder-close-server)
179 (nnheader-report 'nnfolder "Couldn't create directory: %s"
181 ((not (file-directory-p (file-truename nnfolder-directory)))
182 (nnfolder-close-server)
183 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
185 (nnmail-activate 'nnfolder)
186 (nnheader-report 'nnfolder "Opened server %s using directory %s"
187 server nnfolder-directory)
190 (deffoo nnfolder-request-close ()
191 (let ((alist nnfolder-buffer-alist))
193 (nnfolder-close-group (caar alist) nil t)
194 (setq alist (cdr alist))))
195 (nnoo-close-server 'nnfolder)
196 (setq nnfolder-buffer-alist nil
197 nnfolder-group-alist nil))
199 (deffoo nnfolder-request-article (article &optional group server buffer)
200 (nnfolder-possibly-change-group group server)
202 (set-buffer nnfolder-current-buffer)
203 (goto-char (point-min))
204 (when (nnfolder-goto-article article)
208 (unless (and (nnmail-search-unix-mail-delim)
210 (goto-char (point-max)))
212 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
213 (set-buffer nntp-server-buffer)
215 (insert-buffer-substring nnfolder-current-buffer start stop)
216 (goto-char (point-min))
217 (while (looking-at "From ")
219 (insert "X-From-Line: ")
221 (if (numberp article)
222 (cons nnfolder-current-group article)
223 (goto-char (point-min))
224 (cons nnfolder-current-group
225 (if (search-forward (concat "\n" nnfolder-article-marker)
229 (point) (progn (end-of-line) (point))))
232 (deffoo nnfolder-request-group (group &optional server dont-check)
233 (nnfolder-possibly-change-group group server t)
235 (if (not (assoc group nnfolder-group-alist))
236 (nnheader-report 'nnfolder "No such group: %s" group)
239 (nnheader-report 'nnfolder "Selected group %s" group)
241 (let* ((active (assoc group nnfolder-group-alist))
243 (range (cadr active)))
246 (nnheader-report 'nnfolder "No such group: %s" group))
247 ((null nnfolder-current-group)
248 (nnheader-report 'nnfolder "Empty group: %s" group))
250 (nnheader-report 'nnfolder "Selected group %s" group)
251 (nnheader-insert "211 %d %d %d %s\n"
252 (1+ (- (cdr range) (car range)))
253 (car range) (cdr range) group))))))))
255 (deffoo nnfolder-request-scan (&optional group server)
256 (nnfolder-possibly-change-group nil server)
257 (when nnfolder-get-new-mail
258 (nnfolder-possibly-change-group group server)
262 (let ((bufs nnfolder-buffer-alist))
265 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
266 (setq nnfolder-buffer-alist
267 (delq (car bufs) nnfolder-buffer-alist))
268 (set-buffer (nth 1 (car bufs)))
269 (nnfolder-save-buffer)
270 (kill-buffer (current-buffer)))
271 (setq bufs (cdr bufs))))))
275 ;; Don't close the buffer if we're not shutting down the server. This way,
276 ;; we can keep the buffer in the group buffer cache, and not have to grovel
277 ;; over the buffer again unless we add new mail to it or modify it in some
280 (deffoo nnfolder-close-group (group &optional server force)
281 ;; Make sure we _had_ the group open.
282 (when (or (assoc group nnfolder-buffer-alist)
283 (equal group nnfolder-current-group))
284 (let ((inf (assoc group nnfolder-buffer-alist)))
286 (when (and nnfolder-current-group
287 nnfolder-current-buffer)
288 (push (list nnfolder-current-group nnfolder-current-buffer)
289 nnfolder-buffer-alist))
290 (setq nnfolder-buffer-alist
291 (delq inf nnfolder-buffer-alist))
292 (setq nnfolder-current-buffer (cadr inf)
293 nnfolder-current-group (car inf))))
294 (when (and nnfolder-current-buffer
295 (buffer-name nnfolder-current-buffer))
297 (set-buffer nnfolder-current-buffer)
298 ;; If the buffer was modified, write the file out now.
299 (nnfolder-save-buffer)
300 ;; If we're shutting the server down, we need to kill the
301 ;; buffer and remove it from the open buffer list. Or, of
302 ;; course, if we're trying to minimize our space impact.
303 (kill-buffer (current-buffer))
304 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
305 nnfolder-buffer-alist)))))
306 (setq nnfolder-current-group nil
307 nnfolder-current-buffer nil)
310 (deffoo nnfolder-request-create-group (group &optional server args)
311 (nnfolder-possibly-change-group nil server)
312 (nnmail-activate 'nnfolder)
314 (unless (assoc group nnfolder-group-alist)
315 (push (list group (cons 1 0)) nnfolder-group-alist)
316 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
317 (nnfolder-read-folder group)))
320 (deffoo nnfolder-request-list (&optional server)
321 (nnfolder-possibly-change-group nil server)
323 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
324 (nnmail-find-file nnfolder-active-file)
325 (setq nnfolder-group-alist (nnmail-get-active)))
328 (deffoo nnfolder-request-newgroups (date &optional server)
329 (nnfolder-possibly-change-group nil server)
330 (nnfolder-request-list server))
332 (deffoo nnfolder-request-list-newsgroups (&optional server)
333 (nnfolder-possibly-change-group nil server)
335 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
336 (nnmail-find-file nnfolder-newsgroups-file))))
338 ;; Return a list consisting of all article numbers existing in the
341 (defun nnfolder-existing-articles ()
343 (when nnfolder-current-buffer
344 (set-buffer nnfolder-current-buffer)
345 (goto-char (point-min))
346 (let ((marker (concat "\n" nnfolder-article-marker))
349 (while (and (search-forward marker nil t)
350 (re-search-forward number nil t))
351 (let ((newnum (string-to-number (match-string 0))))
352 (if (nnmail-within-headers-p)
353 (push newnum numbers))))
354 ;; The article numbers are increasing, so this result is sorted.
355 (nreverse numbers)))))
357 (deffoo nnfolder-request-expire-articles
358 (articles newsgroup &optional server force)
359 (nnfolder-possibly-change-group newsgroup server)
361 ;; The articles we have deleted so far.
362 (deleted-articles nil)
363 ;; The articles that really exist and will
364 ;; be expired if they are old enough.
366 (gnus-sorted-intersection articles (nnfolder-existing-articles))))
367 (nnmail-activate 'nnfolder)
370 (set-buffer nnfolder-current-buffer)
371 ;; Since messages are sorted in arrival order and expired in the
372 ;; same order, we can stop as soon as we find a message that is
374 (while (and maybe-expirable is-old)
375 (goto-char (point-min))
376 (when (and (nnfolder-goto-article (car maybe-expirable))
377 (search-forward (concat "\n" nnfolder-article-marker)
381 (nnmail-expired-article-p
384 (point) (progn (end-of-line) (point)))
385 force nnfolder-inhibit-expiry))
386 (unless (eq nnmail-expiry-target 'delete)
388 (nnfolder-request-article (car maybe-expirable)
389 newsgroup server (current-buffer))
390 (let ((nnml-current-directory nil))
391 (nnmail-expiry-target-group
392 nnmail-expiry-target newsgroup))))
393 (nnheader-message 5 "Deleting article %d in %s..."
394 (car maybe-expirable) newsgroup)
395 (nnfolder-delete-mail)
396 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
397 (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
398 ;; Must remember which articles were actually deleted
399 (push (car maybe-expirable) deleted-articles)))
400 (setq maybe-expirable (cdr maybe-expirable)))
401 (unless nnfolder-inhibit-expiry
402 (nnheader-message 5 "Deleting articles...done"))
403 (nnfolder-save-buffer)
404 (nnfolder-adjust-min-active newsgroup)
405 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
406 (gnus-sorted-complement articles (nreverse deleted-articles)))))
408 (deffoo nnfolder-request-move-article (article group server
409 accept-form &optional last)
411 (let ((buf (get-buffer-create " *nnfolder move*"))
414 (nnfolder-request-article article group server)
418 (insert-buffer-substring nntp-server-buffer)
419 (goto-char (point-min))
420 (while (re-search-forward
421 (concat "^" nnfolder-article-marker)
422 (save-excursion (and (search-forward "\n\n" nil t) (point)))
424 (delete-region (progn (beginning-of-line) (point))
425 (progn (forward-line 1) (point))))
426 (setq result (eval accept-form))
430 (nnfolder-possibly-change-group group server)
431 (set-buffer nnfolder-current-buffer)
432 (goto-char (point-min))
433 (when (nnfolder-goto-article article)
434 (nnfolder-delete-mail))
435 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
436 (nnfolder-nov-delete-article group article))
438 (nnfolder-save-buffer)
439 (nnfolder-adjust-min-active group)
440 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
443 (deffoo nnfolder-request-accept-article (group &optional server last)
445 (nnfolder-possibly-change-group group server)
446 (nnmail-check-syntax)
447 (let ((buf (current-buffer))
449 (goto-char (point-min))
450 (when (looking-at "X-From-Line: ")
451 (replace-match "From "))
453 (nnfolder-request-list)
456 (goto-char (point-min))
457 (if (search-forward "\n\n" nil t)
459 (goto-char (point-max)))
460 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
461 (delete-region (point) (progn (forward-line 1) (point))))
462 (when nnmail-cache-accepted-message-ids
463 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
464 (setq result (if (stringp group)
465 (list (cons group (nnfolder-active-number group)))
467 (nnmail-article-group 'nnfolder-active-number))))
468 (if (and (null result)
469 (yes-or-no-p "Moved to `junk' group; delete article? "))
472 (car (nnfolder-save-mail result)))))
475 (nnfolder-possibly-change-folder (or (caar art-group) group))
476 (nnfolder-save-buffer)
477 (when nnmail-cache-accepted-message-ids
478 (nnmail-cache-close)))))
479 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
481 (nnheader-report 'nnfolder "Couldn't store article"))
484 (deffoo nnfolder-request-replace-article (article group buffer)
485 (nnfolder-possibly-change-group group)
488 (goto-char (point-min))
490 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
491 (setq xfrom (match-string 1))
493 (goto-char (point-min))
495 (insert "From " xfrom "\n")
496 (unless (looking-at "From ")
497 (insert "From nobody " (current-time-string) "\n"))))
498 (nnfolder-normalize-buffer)
499 (set-buffer nnfolder-current-buffer)
500 (goto-char (point-min))
501 (if (not (nnfolder-goto-article article))
503 (nnfolder-delete-mail)
504 (insert-buffer-substring buffer)
505 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
508 (let ((headers (nnfolder-parse-head article
509 (point-min) (point-max))))
510 (with-current-buffer (nnfolder-open-nov group)
511 (if (nnheader-find-nov-line article)
512 (delete-region (point) (progn (forward-line 1) (point))))
513 (nnheader-insert-nov headers)))))
514 (nnfolder-save-buffer)
517 (deffoo nnfolder-request-delete-group (group &optional force server)
518 (nnfolder-close-group group server t)
519 ;; Delete all articles in GROUP.
521 () ; Don't delete the articles.
522 ;; Delete the file that holds the group.
524 (delete-file (nnfolder-group-pathname group))
525 (delete-file (nnfolder-group-nov-pathname group))))
526 ;; Remove the group from all structures.
527 (setq nnfolder-group-alist
528 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
529 nnfolder-current-group nil
530 nnfolder-current-buffer nil)
531 ;; Save the active file.
532 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
535 (deffoo nnfolder-request-rename-group (group new-name &optional server)
536 (nnfolder-possibly-change-group group server)
538 (set-buffer nnfolder-current-buffer)
539 (and (file-writable-p buffer-file-name)
541 (let ((new-file (nnfolder-group-pathname new-name)))
542 (gnus-make-directory (file-name-directory new-file))
543 (rename-file buffer-file-name new-file)
544 (setq new-file (nnfolder-group-nov-pathname new-name))
545 (rename-file (nnfolder-group-nov-pathname group)
548 ;; That went ok, so we change the internal structures.
549 (let ((entry (assoc group nnfolder-group-alist)))
550 (and entry (setcar entry new-name))
551 (setq nnfolder-current-buffer nil
552 nnfolder-current-group nil)
553 ;; Save the new group alist.
554 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
555 ;; We kill the buffer instead of renaming it and stuff.
556 (kill-buffer (current-buffer))
559 (deffoo nnfolder-request-regenerate (server)
560 (nnfolder-possibly-change-group nil server)
561 (nnfolder-generate-active-file)
565 ;;; Internal functions.
567 (defun nnfolder-adjust-min-active (group)
568 ;; Find the lowest active article in this group.
569 (let* ((active (cadr (assoc group nnfolder-group-alist)))
570 (marker (concat "\n" nnfolder-article-marker))
572 (activemin (cdr active)))
574 (set-buffer nnfolder-current-buffer)
575 (goto-char (point-min))
576 (while (and (search-forward marker nil t)
577 (re-search-forward number nil t))
578 (let ((newnum (string-to-number (match-string 0))))
579 (if (nnmail-within-headers-p)
580 (setq activemin (min activemin newnum)))))
581 (setcar active activemin))))
583 (defun nnfolder-article-string (article)
584 (if (numberp article)
585 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
586 (concat "\nMessage-ID: " article)))
588 (defun nnfolder-goto-article (article)
589 "Place point at the start of the headers of ARTICLE.
590 ARTICLE can be an article number or a Message-ID.
591 Returns t if successful, nil otherwise."
592 (let ((art-string (nnfolder-article-string article))
594 ;; It is likely that we are at or before the delimiter line.
595 ;; We therefore go to the end of the previous line, and start
596 ;; searching from there.
601 ;; First search forward.
602 (while (and (setq found (search-forward art-string nil t))
603 (not (nnmail-within-headers-p))))
604 ;; If unsuccessful, search backward from where we started,
607 (while (and (setq found (search-backward art-string nil t))
608 (not (nnmail-within-headers-p)))))
610 (nnmail-search-unix-mail-delim-backward))))
612 (defun nnfolder-delete-mail (&optional leave-delim)
613 "Delete the message that point is in.
614 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
615 deleted. Point is left where the deleted region was."
619 ;; In case point is at the beginning of the message already.
621 (nnmail-search-unix-mail-delim-backward)
622 (if leave-delim (progn (forward-line 1) (point))
626 (if (nnmail-search-unix-mail-delim)
629 (run-hooks 'nnfolder-delete-mail-hook)
630 (delete-region (point-min) (point-max))))
632 (defun nnfolder-possibly-change-group (group &optional server dont-check)
635 (not (nnfolder-server-opened server)))
636 (nnfolder-open-server server))
637 (unless (gnus-buffer-live-p nnfolder-current-buffer)
638 (setq nnfolder-current-buffer nil
639 nnfolder-current-group nil))
641 (let ((file-name-coding-system nnmail-pathname-coding-system)
642 (pathname-coding-system nnmail-pathname-coding-system))
644 (not (equal group nnfolder-current-group))
646 (nnmail-activate 'nnfolder)
647 (and (assoc group nnfolder-group-alist)
648 (file-exists-p (nnfolder-group-pathname group)))))
650 (setq nnfolder-current-group group
651 nnfolder-current-buffer nil)
653 ;; If we have to change groups, see if we don't already have the
654 ;; folder in memory. If we do, verify the modtime and destroy
655 ;; the folder if needed so we can rescan it.
656 (setq nnfolder-current-buffer
657 (nth 1 (assoc group nnfolder-buffer-alist)))
659 ;; If the buffer is not live, make sure it isn't in the alist. If it
660 ;; is live, verify that nobody else has touched the file since last
662 (when (and nnfolder-current-buffer
663 (not (gnus-buffer-live-p nnfolder-current-buffer)))
664 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
665 nnfolder-current-buffer nil))
667 (setq nnfolder-current-group group)
669 (when (or (not nnfolder-current-buffer)
670 (not (verify-visited-file-modtime
671 nnfolder-current-buffer)))
673 (setq file (nnfolder-group-pathname group))
674 ;; See whether we need to create the new file.
675 (unless (file-exists-p file)
676 (gnus-make-directory (file-name-directory file))
677 (let ((nnmail-file-coding-system
678 (or nnfolder-file-coding-system-for-write
679 nnfolder-file-coding-system-for-write)))
680 (nnmail-write-region 1 1 file t 'nomesg)))
681 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
682 (set-buffer nnfolder-current-buffer)
683 (push (list group nnfolder-current-buffer)
684 nnfolder-buffer-alist)))))))))
686 (defun nnfolder-save-mail (group-art-list)
687 "Called narrowed to an article."
688 (let* (save-list group-art)
689 (goto-char (point-min))
690 ;; The From line may have been quoted by movemail.
691 (when (looking-at ">From")
693 ;; This might come from somewhere else.
694 (unless (looking-at "From ")
695 (insert "From nobody " (current-time-string) "\n")
696 (goto-char (point-min)))
697 ;; Quote all "From " lines in the article.
699 (let (case-fold-search)
700 (while (re-search-forward "^From " nil t)
703 (setq save-list group-art-list)
704 (nnmail-insert-lines)
705 (nnmail-insert-xref group-art-list)
706 (run-hooks 'nnmail-prepare-save-mail-hook)
707 (run-hooks 'nnfolder-prepare-save-mail-hook)
709 ;; Insert the mail into each of the destination groups.
710 (while (setq group-art (pop group-art-list))
711 ;; Kill any previous newsgroup markers.
712 (goto-char (point-min))
713 (if (search-forward "\n\n" nil t)
715 (goto-char (point-max)))
716 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
717 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
719 ;; Insert the new newsgroup marker.
720 (nnfolder-insert-newsgroup-line group-art)
723 (let ((beg (point-min))
725 (obuf (current-buffer)))
726 (nnfolder-possibly-change-folder (car group-art))
727 (let ((buffer-read-only nil))
728 (nnfolder-normalize-buffer)
729 (insert-buffer-substring obuf beg end))
730 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
732 (nnfolder-add-nov (car group-art) (cdr group-art)
733 (nnfolder-parse-head nil beg end))))))
735 ;; Did we save it anywhere?
738 (defun nnfolder-normalize-buffer ()
739 "Make sure there are two newlines at the end of the buffer."
740 (goto-char (point-max))
741 (skip-chars-backward "\n")
742 (delete-region (point) (point-max))
745 (defun nnfolder-insert-newsgroup-line (group-art)
747 (goto-char (point-min))
748 (unless (search-forward "\n\n" nil t)
749 (goto-char (point-max))
752 (insert (format (concat nnfolder-article-marker "%d %s\n")
753 (cdr group-art) (current-time-string)))))
755 (defun nnfolder-active-number (group)
756 ;; Find the next article number in GROUP.
757 (let ((active (cadr (assoc group nnfolder-group-alist))))
759 (setcdr active (1+ (cdr active)))
760 ;; This group is new, so we create a new entry for it.
761 ;; This might be a bit naughty... creating groups on the drop of
762 ;; a hat, but I don't know...
763 (push (list group (setq active (cons 1 1)))
764 nnfolder-group-alist))
767 (defun nnfolder-possibly-change-folder (group)
768 (let ((inf (assoc group nnfolder-buffer-alist)))
770 (gnus-buffer-live-p (cadr inf)))
771 (set-buffer (cadr inf))
773 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
774 (when nnfolder-group-alist
775 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
776 (push (list group (nnfolder-read-folder group))
777 nnfolder-buffer-alist))))
779 ;; This method has a problem if you've accidentally let the active list get
780 ;; out of sync with the files. This could happen, say, if you've
781 ;; accidentally gotten new mail with something other than Gnus (but why
782 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
783 ;; processing the file, ready to add new X-Gnus article number markers, and
784 ;; we'll run across a message with no ID yet - the active list _may_not_ be
787 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
788 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
789 ;; rest of the message looking for any more, possibly higher IDs. We'll
790 ;; assume the maximum that we find is the highest active. Note that this
791 ;; shouldn't cost us much extra time at all, but will be a lot less
792 ;; vulnerable to glitches between the mbox and the active file.
794 (defun nnfolder-read-folder (group)
795 (let* ((file (nnfolder-group-pathname group))
796 (nov (nnfolder-group-nov-pathname group))
798 (let ((nnheader-file-coding-system
799 nnfolder-file-coding-system))
800 (nnheader-find-file-noselect file)))))
801 (if (equal (cadr (assoc group nnfolder-scantime-alist))
802 (nth 5 (file-attributes file)))
803 ;; This looks up-to-date, so we don't do any scanning.
804 (if (file-exists-p file)
806 (push (list group buffer) nnfolder-buffer-alist)
807 (set-buffer-modified-p t)
808 (nnfolder-save-buffer))
809 ;; Parse the damn thing.
811 (goto-char (point-min))
812 ;; Remove any blank lines at the start.
813 (while (eq (following-char) ?\n)
815 (nnmail-activate 'nnfolder)
817 (let ((delim "^From ")
818 (marker (concat "\n" nnfolder-article-marker))
820 (active (or (cadr (assoc group nnfolder-group-alist))
822 (scantime (assoc group nnfolder-scantime-alist))
824 maxid start end newscantime
825 novbuf articles newnum
827 (buffer-disable-undo)
828 (setq maxid (cdr active))
830 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
831 (and (file-exists-p nov)
832 (file-newer-than-file-p nov file)))
833 (unless (file-exists-p nov)
834 (gnus-make-directory (file-name-directory nov)))
836 (setq novbuf (nnfolder-open-nov group))
837 (goto-char (point-min))
839 (push (read novbuf) articles)
841 (setq articles (nreverse articles))))
842 (goto-char (point-min))
844 ;; Anytime the active number is 1 or 0, it is suspect. In that
845 ;; case, search the file manually to find the active number. Or,
846 ;; of course, if we're being paranoid. (This would also be the
847 ;; place to build other lists from the header markers, such as
848 ;; expunge lists, etc., if we ever desired to abandon the active
849 ;; file entirely for mboxes.)
850 (when (or nnfolder-ignore-active-file
853 (while (and (search-forward marker nil t)
855 (setq newnum (string-to-number (match-string 0)))
856 (when (nnmail-within-headers-p)
857 (setq maxid (max maxid newnum)
858 minid (min minid newnum))
860 (if (memq newnum articles)
861 (setq articles (delq newnum articles))
862 (let ((headers (nnfolder-parse-head newnum)))
863 (with-current-buffer novbuf
864 (nnheader-find-nov-line newnum)
865 (nnheader-insert-nov headers)))))))
866 (when (and novbuf articles)
867 (with-current-buffer novbuf
868 (dolist (article articles)
869 (when (nnheader-find-nov-line article)
870 (delete-region (point)
871 (progn (forward-line 1) (point)))))))
872 (setcar active (max 1 (min minid maxid)))
873 (setcdr active (max maxid (cdr active)))
874 (goto-char (point-min)))
876 ;; As long as we trust that the user will only insert unmarked mail
877 ;; at the end, go to the end and search backwards for the last
878 ;; marker. Find the start of that message, and begin to search for
879 ;; unmarked messages from there.
880 (when (not (or nnfolder-distrust-mbox
882 (goto-char (point-max))
883 (unless (re-search-backward marker nil t)
884 (goto-char (point-min)))
885 ;;(when (nnmail-search-unix-mail-delim)
886 ;; (goto-char (point-min)))
889 ;; Keep track of the active number on our own, and insert it back
890 ;; into the active list when we're done. Also, prime the pump to
891 ;; cut down on the number of searches we do.
892 (unless (nnmail-search-unix-mail-delim)
893 (goto-char (point-max)))
894 (setq end (point-marker))
895 (while (not (= end (point-max)))
896 (setq start (marker-position end))
898 ;; There may be more than one "From " line, so we skip past
900 (while (looking-at delim)
902 (set-marker end (if (nnmail-search-unix-mail-delim)
906 (when (not (search-forward marker end t))
907 (narrow-to-region start end)
908 (nnmail-insert-lines)
909 (nnfolder-insert-newsgroup-line
912 (nnfolder-active-number nnfolder-current-group))))
914 (let ((headers (nnfolder-parse-head newnum (point-min)
916 (with-current-buffer novbuf
917 (goto-char (point-max))
918 (nnheader-insert-nov headers))))
922 ;; Make absolutely sure that the active list reflects reality!
923 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
925 ;; Set the scantime for this group.
926 (setq newscantime (visited-file-modtime))
928 (setcdr scantime (list newscantime))
929 (push (list nnfolder-current-group newscantime)
930 nnfolder-scantime-alist))
934 (current-buffer))))))
937 (defun nnfolder-generate-active-file ()
938 "Look for mbox folders in the nnfolder directory and make them into groups.
939 This command does not work if you use short group names."
941 (nnmail-activate 'nnfolder)
942 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
943 (dolist (file (directory-files (or nnfolder-nov-directory
947 (regexp-quote nnfolder-nov-file-suffix)
949 (when (not (message-mail-file-mbox-p file))
951 (delete-file file)))))
952 (let ((files (directory-files nnfolder-directory))
954 (while (setq file (pop files))
955 (when (and (not (backup-file-name-p file))
956 (message-mail-file-mbox-p
957 (nnheader-concat nnfolder-directory file)))
958 (let ((oldgroup (assoc file nnfolder-group-alist)))
960 (nnheader-message 5 "Refreshing group %s..." file)
961 (nnheader-message 5 "Adding group %s..." file))
963 (setq nnfolder-group-alist
964 (delq oldgroup (copy-sequence nnfolder-group-alist))))
965 (push (list file (cons 1 0)) nnfolder-group-alist)
966 (nnfolder-possibly-change-folder file)
967 (nnfolder-possibly-change-group file)
968 (nnfolder-close-group file))))
969 (nnheader-message 5 "")))
971 (defun nnfolder-group-pathname (group)
972 "Make pathname for GROUP."
974 (encode-coding-string group nnmail-pathname-coding-system))
975 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
976 ;; If this file exists, we use it directly.
977 (if (or nnmail-use-long-file-names
978 (file-exists-p (concat dir group)))
980 ;; If not, we translate dots into slashes.
981 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
983 (defun nnfolder-group-nov-pathname (group)
984 "Make pathname for GROUP NOV."
985 (let ((nnfolder-directory
986 (or nnfolder-nov-directory nnfolder-directory)))
987 (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
989 (defun nnfolder-save-buffer ()
991 (when (buffer-modified-p)
992 (run-hooks 'nnfolder-save-buffer-hook)
993 (gnus-make-directory (file-name-directory (buffer-file-name)))
994 (let* ((coding-system-for-write
995 (or nnfolder-file-coding-system-for-write
996 nnfolder-file-coding-system))
997 (output-coding-system coding-system-for-write))
999 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1000 (nnfolder-save-nov)))
1002 (defun nnfolder-save-active (group-alist active-file)
1003 (let ((nnmail-active-file-coding-system
1004 (or nnfolder-active-file-coding-system-for-write
1005 nnfolder-active-file-coding-system)))
1006 (nnmail-save-active group-alist active-file)))
1008 (defun nnfolder-open-nov (group)
1009 (or (cdr (assoc group nnfolder-nov-buffer-alist))
1010 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1013 (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1014 (nnfolder-group-nov-pathname group))
1016 (when (file-exists-p nnfolder-nov-buffer-file-name)
1017 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1018 (push (cons group buffer) nnfolder-nov-buffer-alist)
1021 (defun nnfolder-save-nov ()
1023 (while nnfolder-nov-buffer-alist
1024 (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1025 (set-buffer (cdar nnfolder-nov-buffer-alist))
1026 (when (buffer-modified-p)
1027 (gnus-make-directory (file-name-directory
1028 nnfolder-nov-buffer-file-name))
1029 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1031 (set-buffer-modified-p nil)
1032 (kill-buffer (current-buffer)))
1033 (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1035 (defun nnfolder-nov-delete-article (group article)
1037 (set-buffer (nnfolder-open-nov group))
1038 (when (nnheader-find-nov-line article)
1039 (delete-region (point) (progn (forward-line 1) (point))))
1042 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1043 (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1045 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1046 (when (file-exists-p nov)
1048 (set-buffer nntp-server-buffer)
1050 (nnheader-insert-file-contents nov)
1052 (not (numberp fetch-old)))
1053 t ; Don't remove anything.
1054 (nnheader-nov-delete-outside-range
1055 (if fetch-old (max 1 (- (car articles) fetch-old))
1057 (car (last articles)))
1060 (defun nnfolder-parse-head (&optional number b e)
1061 "Parse the head of the current buffer."
1062 (let ((buf (current-buffer))
1066 (setq b (if (nnmail-search-unix-mail-delim-backward)
1067 (point) (point-min)))
1069 (setq e (if (nnmail-search-unix-mail-delim)
1070 (point) (point-max))))
1071 (setq chars (- e b))
1072 (unless (zerop chars)
1074 (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1076 (insert-buffer-substring buf b e)
1077 ;; Fold continuation lines.
1078 (goto-char (point-min))
1079 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1080 (replace-match " " t t))
1081 ;; Remove any tabs; they are too confusing.
1082 (subst-char-in-region (point-min) (point-max) ?\t ? )
1083 (let ((headers (nnheader-parse-head t)))
1084 (mail-header-set-chars headers chars)
1085 (mail-header-set-number headers number)
1088 (defun nnfolder-add-nov (group article headers)
1089 "Add a nov line for the GROUP base."
1091 (set-buffer (nnfolder-open-nov group))
1092 (goto-char (point-max))
1093 (mail-header-set-number headers article)
1094 (nnheader-insert-nov headers)))
1098 ;;; nnfolder.el ends here