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.
36 (eval-when-compile (require 'cl))
40 (nnoo-declare nnfolder)
42 (defvoo nnfolder-directory (expand-file-name message-directory)
43 "The name of the nnfolder directory.")
45 (defvoo nnfolder-nov-directory nil
46 "The name of the nnfolder NOV directory.
47 If nil, `nnfolder-directory' is used.")
49 (defvoo nnfolder-active-file
50 (nnheader-concat nnfolder-directory "active")
51 "The name of the active file.")
53 ;; I renamed this variable to something more in keeping with the general GNU
56 (defvoo nnfolder-ignore-active-file nil
57 "If non-nil, the active file is ignored.
58 This causes nnfolder to do some extra work in order to determine the
59 true active ranges of an mbox file. Note that the active file is
60 still saved, but its values are not used. This costs some extra time
61 when scanning an mbox when opening it.")
63 (defvoo nnfolder-distrust-mbox nil
64 "If non-nil, the folder will be distrusted.
65 This means that nnfolder will not trust the user with respect to
66 inserting unaccounted for mail in the middle of an mbox file. This
67 can greatly slow down scans, which now must scan the entire file for
68 unmarked messages. When nil, scans occur forward from the last marked
69 message, a huge time saver for large mailboxes.")
71 (defvoo nnfolder-newsgroups-file
72 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
73 "Mail newsgroups description file.")
75 (defvoo nnfolder-get-new-mail t
76 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
78 (defvoo nnfolder-prepare-save-mail-hook nil
79 "Hook run narrowed to an article before saving.")
81 (defvoo nnfolder-save-buffer-hook nil
82 "Hook run before saving the nnfolder mbox buffer.")
84 (defvoo nnfolder-inhibit-expiry nil
85 "If non-nil, inhibit expiry.")
89 (defconst nnfolder-version "nnfolder 2.0"
92 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
93 "String used to demarcate what the article number for a message is.")
95 (defvoo nnfolder-current-group nil)
96 (defvoo nnfolder-current-buffer nil)
97 (defvoo nnfolder-status-string "")
98 (defvoo nnfolder-group-alist nil)
99 (defvoo nnfolder-buffer-alist nil)
100 (defvoo nnfolder-scantime-alist nil)
101 (defvoo nnfolder-active-timestamp nil)
102 (defvoo nnfolder-active-file-coding-system mm-text-coding-system)
103 (defvoo nnfolder-active-file-coding-system-for-write
104 nnmail-active-file-coding-system)
105 (defvoo nnfolder-file-coding-system mm-text-coding-system)
106 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
107 "Coding system for save nnfolder file.
108 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
110 (defvoo nnfolder-nov-is-evil nil
111 "If non-nil, Gnus will never generate and use nov databases for mail groups.
112 Using nov databases will speed up header fetching considerably.
113 This variable shouldn't be flipped much. If you have, for some reason,
114 set this to t, and want to set it to nil again, you should always run
115 the `nnfolder-generate-active-file' command. The function will go
116 through all nnfolder directories and generate nov databases for them
117 all. This may very well take some time.")
119 (defvoo nnfolder-nov-file-suffix ".nov")
121 (defvoo nnfolder-nov-buffer-alist nil)
123 (defvar nnfolder-nov-buffer-file-name nil)
127 ;;; Interface functions
129 (nnoo-define-basics nnfolder)
131 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
133 (set-buffer nntp-server-buffer)
135 (let (article start stop)
136 (nnfolder-possibly-change-group group server)
137 (when nnfolder-current-buffer
138 (set-buffer nnfolder-current-buffer)
139 (goto-char (point-min))
140 (if (stringp (car articles))
142 (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
144 (while (setq article (pop articles))
145 (set-buffer nnfolder-current-buffer)
146 (when (nnfolder-goto-article article)
148 (setq stop (if (search-forward "\n\n" nil t)
151 (set-buffer nntp-server-buffer)
152 (insert (format "221 %d Article retrieved.\n" article))
153 (insert-buffer-substring nnfolder-current-buffer start stop)
154 (goto-char (point-max))
156 (set-buffer nntp-server-buffer)
157 (nnheader-fold-continuation-lines)
160 (deffoo nnfolder-open-server (server &optional defs)
161 (nnoo-change-server 'nnfolder server defs)
162 (nnmail-activate 'nnfolder t)
163 (gnus-make-directory nnfolder-directory)
164 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
165 (and nnfolder-nov-directory
166 (gnus-make-directory nnfolder-nov-directory)))
168 ((not (file-exists-p nnfolder-directory))
169 (nnfolder-close-server)
170 (nnheader-report 'nnfolder "Couldn't create directory: %s"
172 ((not (file-directory-p (file-truename nnfolder-directory)))
173 (nnfolder-close-server)
174 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
176 (nnmail-activate 'nnfolder)
177 (nnheader-report 'nnfolder "Opened server %s using directory %s"
178 server nnfolder-directory)
181 (deffoo nnfolder-request-close ()
182 (let ((alist nnfolder-buffer-alist))
184 (nnfolder-close-group (caar alist) nil t)
185 (setq alist (cdr alist))))
186 (nnoo-close-server 'nnfolder)
187 (setq nnfolder-buffer-alist nil
188 nnfolder-group-alist nil))
190 (deffoo nnfolder-request-article (article &optional group server buffer)
191 (nnfolder-possibly-change-group group server)
193 (set-buffer nnfolder-current-buffer)
194 (goto-char (point-min))
195 (when (nnfolder-goto-article article)
199 (unless (and (nnmail-search-unix-mail-delim)
201 (goto-char (point-max)))
203 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
204 (set-buffer nntp-server-buffer)
206 (insert-buffer-substring nnfolder-current-buffer start stop)
207 (goto-char (point-min))
208 (while (looking-at "From ")
210 (insert "X-From-Line: ")
212 (if (numberp article)
213 (cons nnfolder-current-group article)
214 (goto-char (point-min))
215 (cons nnfolder-current-group
216 (if (search-forward (concat "\n" nnfolder-article-marker)
220 (point) (progn (end-of-line) (point))))
223 (deffoo nnfolder-request-group (group &optional server dont-check)
224 (nnfolder-possibly-change-group group server t)
226 (if (not (assoc group nnfolder-group-alist))
227 (nnheader-report 'nnfolder "No such group: %s" group)
230 (nnheader-report 'nnfolder "Selected group %s" group)
232 (let* ((active (assoc group nnfolder-group-alist))
234 (range (cadr active)))
237 (nnheader-report 'nnfolder "No such group: %s" group))
238 ((null nnfolder-current-group)
239 (nnheader-report 'nnfolder "Empty group: %s" group))
241 (nnheader-report 'nnfolder "Selected group %s" group)
242 (nnheader-insert "211 %d %d %d %s\n"
243 (1+ (- (cdr range) (car range)))
244 (car range) (cdr range) group))))))))
246 (deffoo nnfolder-request-scan (&optional group server)
247 (nnfolder-possibly-change-group nil server)
248 (when nnfolder-get-new-mail
249 (nnfolder-possibly-change-group group server)
253 (let ((bufs nnfolder-buffer-alist))
256 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
257 (setq nnfolder-buffer-alist
258 (delq (car bufs) nnfolder-buffer-alist))
259 (set-buffer (nth 1 (car bufs)))
260 (nnfolder-save-buffer)
261 (kill-buffer (current-buffer)))
262 (setq bufs (cdr bufs))))))
266 ;; Don't close the buffer if we're not shutting down the server. This way,
267 ;; we can keep the buffer in the group buffer cache, and not have to grovel
268 ;; over the buffer again unless we add new mail to it or modify it in some
271 (deffoo nnfolder-close-group (group &optional server force)
272 ;; Make sure we _had_ the group open.
273 (when (or (assoc group nnfolder-buffer-alist)
274 (equal group nnfolder-current-group))
275 (let ((inf (assoc group nnfolder-buffer-alist)))
277 (when (and nnfolder-current-group
278 nnfolder-current-buffer)
279 (push (list nnfolder-current-group nnfolder-current-buffer)
280 nnfolder-buffer-alist))
281 (setq nnfolder-buffer-alist
282 (delq inf nnfolder-buffer-alist))
283 (setq nnfolder-current-buffer (cadr inf)
284 nnfolder-current-group (car inf))))
285 (when (and nnfolder-current-buffer
286 (buffer-name nnfolder-current-buffer))
288 (set-buffer nnfolder-current-buffer)
289 ;; If the buffer was modified, write the file out now.
290 (nnfolder-save-buffer)
291 ;; If we're shutting the server down, we need to kill the
292 ;; buffer and remove it from the open buffer list. Or, of
293 ;; course, if we're trying to minimize our space impact.
294 (kill-buffer (current-buffer))
295 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
296 nnfolder-buffer-alist)))))
297 (setq nnfolder-current-group nil
298 nnfolder-current-buffer nil)
301 (deffoo nnfolder-request-create-group (group &optional server args)
302 (nnfolder-possibly-change-group nil server)
303 (nnmail-activate 'nnfolder)
305 (unless (assoc group nnfolder-group-alist)
306 (push (list group (cons 1 0)) nnfolder-group-alist)
307 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
308 (nnfolder-read-folder group)))
311 (deffoo nnfolder-request-list (&optional server)
312 (nnfolder-possibly-change-group nil server)
314 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
315 (nnmail-find-file nnfolder-active-file)
316 (setq nnfolder-group-alist (nnmail-get-active)))
319 (deffoo nnfolder-request-newgroups (date &optional server)
320 (nnfolder-possibly-change-group nil server)
321 (nnfolder-request-list server))
323 (deffoo nnfolder-request-list-newsgroups (&optional server)
324 (nnfolder-possibly-change-group nil server)
326 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
327 (nnmail-find-file nnfolder-newsgroups-file))))
329 ;; Return a list consisting of all article numbers existing in the
332 (defun nnfolder-existing-articles ()
334 (when nnfolder-current-buffer
335 (set-buffer nnfolder-current-buffer)
336 (goto-char (point-min))
337 (let ((marker (concat "\n" nnfolder-article-marker))
341 (while (and (search-forward marker nil t)
342 (re-search-forward number nil t))
343 (let ((newnum (string-to-number (match-string 0))))
344 (if (nnmail-within-headers-p)
345 (push newnum numbers))))
348 (deffoo nnfolder-request-expire-articles
349 (articles newsgroup &optional server force)
350 (nnfolder-possibly-change-group newsgroup server)
352 ;; The articles we have deleted so far.
353 (deleted-articles nil)
354 ;; The articles that really exist and will
355 ;; be expired if they are old enough.
357 (gnus-intersection articles (nnfolder-existing-articles))))
358 (nnmail-activate 'nnfolder)
361 (set-buffer nnfolder-current-buffer)
362 ;; Since messages are sorted in arrival order and expired in the
363 ;; same order, we can stop as soon as we find a message that is
365 (while (and maybe-expirable is-old)
366 (goto-char (point-min))
367 (when (and (nnfolder-goto-article (car maybe-expirable))
368 (search-forward (concat "\n" nnfolder-article-marker)
372 (nnmail-expired-article-p
375 (point) (progn (end-of-line) (point)))
376 force nnfolder-inhibit-expiry))
377 (unless (eq nnmail-expiry-target 'delete)
379 (nnfolder-request-article (car maybe-expirable)
380 newsgroup server (current-buffer))
381 (let ((nnml-current-directory nil))
382 (nnmail-expiry-target-group
383 nnmail-expiry-target newsgroup))))
384 (nnheader-message 5 "Deleting article %d in %s..."
385 (car maybe-expirable) newsgroup)
386 (nnfolder-delete-mail)
387 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
388 (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
389 ;; Must remember which articles were actually deleted
390 (push (car maybe-expirable) deleted-articles)))
391 (setq maybe-expirable (cdr maybe-expirable)))
392 (unless nnfolder-inhibit-expiry
393 (nnheader-message 5 "Deleting articles...done"))
394 (nnfolder-save-buffer)
395 (nnfolder-adjust-min-active newsgroup)
396 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
397 (gnus-sorted-complement articles (nreverse deleted-articles)))))
399 (deffoo nnfolder-request-move-article (article group server
400 accept-form &optional last)
402 (let ((buf (get-buffer-create " *nnfolder move*"))
405 (nnfolder-request-article article group server)
409 (insert-buffer-substring nntp-server-buffer)
410 (goto-char (point-min))
411 (while (re-search-forward
412 (concat "^" nnfolder-article-marker)
413 (save-excursion (and (search-forward "\n\n" nil t) (point)))
415 (delete-region (progn (beginning-of-line) (point))
416 (progn (forward-line 1) (point))))
417 (setq result (eval accept-form))
421 (nnfolder-possibly-change-group group server)
422 (set-buffer nnfolder-current-buffer)
423 (goto-char (point-min))
424 (when (nnfolder-goto-article article)
425 (nnfolder-delete-mail))
426 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
427 (nnfolder-nov-delete-article group article))
429 (nnfolder-save-buffer)
430 (nnfolder-adjust-min-active group)
431 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
434 (deffoo nnfolder-request-accept-article (group &optional server last)
436 (nnfolder-possibly-change-group group server)
437 (nnmail-check-syntax)
438 (let ((buf (current-buffer))
440 (goto-char (point-min))
441 (when (looking-at "X-From-Line: ")
442 (replace-match "From "))
444 (nnfolder-request-list)
447 (goto-char (point-min))
448 (if (search-forward "\n\n" nil t)
450 (goto-char (point-max)))
451 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
452 (delete-region (point) (progn (forward-line 1) (point))))
453 (when nnmail-cache-accepted-message-ids
454 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
455 (setq result (if (stringp group)
456 (list (cons group (nnfolder-active-number group)))
458 (nnmail-article-group 'nnfolder-active-number))))
459 (if (and (null result)
460 (yes-or-no-p "Moved to `junk' group; delete article? "))
463 (car (nnfolder-save-mail result)))))
466 (nnfolder-possibly-change-folder (or (caar art-group) group))
467 (nnfolder-save-buffer)
468 (when nnmail-cache-accepted-message-ids
469 (nnmail-cache-close)))))
470 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
472 (nnheader-report 'nnfolder "Couldn't store article"))
475 (deffoo nnfolder-request-replace-article (article group buffer)
476 (nnfolder-possibly-change-group group)
479 (goto-char (point-min))
481 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
482 (setq xfrom (match-string 1))
484 (goto-char (point-min))
486 (insert "From " xfrom "\n")
487 (unless (looking-at "From ")
488 (insert "From nobody " (current-time-string) "\n"))))
489 (nnfolder-normalize-buffer)
490 (set-buffer nnfolder-current-buffer)
491 (goto-char (point-min))
492 (if (not (nnfolder-goto-article article))
494 (nnfolder-delete-mail)
495 (insert-buffer-substring buffer)
496 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
499 (let ((headers (nnfolder-parse-head article
500 (point-min) (point-max))))
501 (with-current-buffer (nnfolder-open-nov group)
502 (if (nnheader-find-nov-line article)
503 (delete-region (point) (progn (forward-line 1) (point))))
504 (nnheader-insert-nov headers)))))
505 (nnfolder-save-buffer)
508 (deffoo nnfolder-request-delete-group (group &optional force server)
509 (nnfolder-close-group group server t)
510 ;; Delete all articles in GROUP.
512 () ; Don't delete the articles.
513 ;; Delete the file that holds the group.
515 (delete-file (nnfolder-group-pathname group))
516 (delete-file (nnfolder-group-nov-pathname group))))
517 ;; Remove the group from all structures.
518 (setq nnfolder-group-alist
519 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
520 nnfolder-current-group nil
521 nnfolder-current-buffer nil)
522 ;; Save the active file.
523 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
526 (deffoo nnfolder-request-rename-group (group new-name &optional server)
527 (nnfolder-possibly-change-group group server)
529 (set-buffer nnfolder-current-buffer)
530 (and (file-writable-p buffer-file-name)
532 (let ((new-file (nnfolder-group-pathname new-name)))
533 (gnus-make-directory (file-name-directory new-file))
534 (rename-file buffer-file-name new-file)
535 (setq new-file (nnfolder-group-nov-pathname new-name))
536 (rename-file (nnfolder-group-nov-pathname group)
539 ;; That went ok, so we change the internal structures.
540 (let ((entry (assoc group nnfolder-group-alist)))
541 (and entry (setcar entry new-name))
542 (setq nnfolder-current-buffer nil
543 nnfolder-current-group nil)
544 ;; Save the new group alist.
545 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
546 ;; We kill the buffer instead of renaming it and stuff.
547 (kill-buffer (current-buffer))
550 (defun nnfolder-request-regenerate (server)
551 (nnfolder-possibly-change-group nil server)
552 (nnfolder-generate-active-file)
556 ;;; Internal functions.
558 (defun nnfolder-adjust-min-active (group)
559 ;; Find the lowest active article in this group.
560 (let* ((active (cadr (assoc group nnfolder-group-alist)))
561 (marker (concat "\n" nnfolder-article-marker))
563 (activemin (cdr active)))
565 (set-buffer nnfolder-current-buffer)
566 (goto-char (point-min))
567 (while (and (search-forward marker nil t)
568 (re-search-forward number nil t))
569 (let ((newnum (string-to-number (match-string 0))))
570 (if (nnmail-within-headers-p)
571 (setq activemin (min activemin newnum)))))
572 (setcar active activemin))))
574 (defun nnfolder-article-string (article)
575 (if (numberp article)
576 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
577 (concat "\nMessage-ID: " article)))
579 (defun nnfolder-goto-article (article)
580 "Place point at the start of the headers of ARTICLE.
581 ARTICLE can be an article number or a Message-ID.
582 Returns t if successful, nil otherwise."
583 (let ((art-string (nnfolder-article-string article))
585 ;; It is likely that we are at or before the delimiter line.
586 ;; We therefore go to the end of the previous line, and start
587 ;; searching from there.
592 ;; First search forward.
593 (while (and (setq found (search-forward art-string nil t))
594 (not (nnmail-within-headers-p))))
595 ;; If unsuccessful, search backward from where we started,
598 (while (and (setq found (search-backward art-string nil t))
599 (not (nnmail-within-headers-p)))))
601 (nnmail-search-unix-mail-delim-backward))))
603 (defun nnfolder-delete-mail (&optional leave-delim)
604 "Delete the message that point is in.
605 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
606 deleted. Point is left where the deleted region was."
610 ;; In case point is at the beginning of the message already.
612 (nnmail-search-unix-mail-delim-backward)
613 (if leave-delim (progn (forward-line 1) (point))
617 (if (nnmail-search-unix-mail-delim)
620 (run-hooks 'nnfolder-delete-mail-hook)
621 (delete-region (point-min) (point-max))))
623 (defun nnfolder-possibly-change-group (group &optional server dont-check)
626 (not (nnfolder-server-opened server)))
627 (nnfolder-open-server server))
628 (unless (gnus-buffer-live-p nnfolder-current-buffer)
629 (setq nnfolder-current-buffer nil
630 nnfolder-current-group nil))
633 (not (equal group nnfolder-current-group)))
634 (let ((file-name-coding-system nnmail-pathname-coding-system))
635 (nnmail-activate 'nnfolder)
636 (when (and (not (assoc group nnfolder-group-alist))
638 (nnfolder-group-pathname group))))
639 ;; The group doesn't exist, so we create a new entry for it.
640 (push (list group (cons 1 0)) nnfolder-group-alist)
641 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
644 (setq nnfolder-current-group group
645 nnfolder-current-buffer nil)
647 ;; If we have to change groups, see if we don't already have the
648 ;; folder in memory. If we do, verify the modtime and destroy
649 ;; the folder if needed so we can rescan it.
650 (setq nnfolder-current-buffer
651 (nth 1 (assoc group nnfolder-buffer-alist)))
653 ;; If the buffer is not live, make sure it isn't in the alist. If it
654 ;; is live, verify that nobody else has touched the file since last
656 (when (and nnfolder-current-buffer
657 (not (gnus-buffer-live-p nnfolder-current-buffer)))
658 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
659 nnfolder-current-buffer nil))
661 (setq nnfolder-current-group group)
663 (when (or (not nnfolder-current-buffer)
664 (not (verify-visited-file-modtime
665 nnfolder-current-buffer)))
667 (setq file (nnfolder-group-pathname group))
668 ;; See whether we need to create the new file.
669 (unless (file-exists-p file)
670 (gnus-make-directory (file-name-directory file))
671 (let ((nnmail-file-coding-system
672 (or nnfolder-file-coding-system-for-write
673 nnfolder-file-coding-system-for-write)))
674 (nnmail-write-region 1 1 file t 'nomesg)))
675 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
676 (set-buffer nnfolder-current-buffer)
677 (push (list group nnfolder-current-buffer)
678 nnfolder-buffer-alist)))))))))
680 (defun nnfolder-save-mail (group-art-list)
681 "Called narrowed to an article."
682 (let* (save-list group-art)
683 (goto-char (point-min))
684 ;; The From line may have been quoted by movemail.
685 (when (looking-at ">From")
687 ;; This might come from somewhere else.
688 (unless (looking-at "From ")
689 (insert "From nobody " (current-time-string) "\n")
690 (goto-char (point-min)))
691 ;; Quote all "From " lines in the article.
693 (let (case-fold-search)
694 (while (re-search-forward "^From " nil t)
697 (setq save-list group-art-list)
698 (nnmail-insert-lines)
699 (nnmail-insert-xref group-art-list)
700 (run-hooks 'nnmail-prepare-save-mail-hook)
701 (run-hooks 'nnfolder-prepare-save-mail-hook)
703 ;; Insert the mail into each of the destination groups.
704 (while (setq group-art (pop group-art-list))
705 ;; Kill any previous newsgroup markers.
706 (goto-char (point-min))
707 (if (search-forward "\n\n" nil t)
709 (goto-char (point-max)))
710 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
711 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
713 ;; Insert the new newsgroup marker.
714 (nnfolder-insert-newsgroup-line group-art)
717 (let ((beg (point-min))
719 (obuf (current-buffer)))
720 (nnfolder-possibly-change-folder (car group-art))
721 (let ((buffer-read-only nil))
722 (nnfolder-normalize-buffer)
723 (insert-buffer-substring obuf beg end))
724 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
726 (nnfolder-add-nov (car group-art) (cdr group-art)
727 (nnfolder-parse-head nil beg end))))))
729 ;; Did we save it anywhere?
732 (defun nnfolder-normalize-buffer ()
733 "Make sure there are two newlines at the end of the buffer."
734 (goto-char (point-max))
735 (skip-chars-backward "\n")
736 (delete-region (point) (point-max))
739 (defun nnfolder-insert-newsgroup-line (group-art)
741 (goto-char (point-min))
742 (unless (search-forward "\n\n" nil t)
743 (goto-char (point-max))
746 (insert (format (concat nnfolder-article-marker "%d %s\n")
747 (cdr group-art) (current-time-string)))))
749 (defun nnfolder-active-number (group)
750 ;; Find the next article number in GROUP.
751 (let ((active (cadr (assoc group nnfolder-group-alist))))
753 (setcdr active (1+ (cdr active)))
754 ;; This group is new, so we create a new entry for it.
755 ;; This might be a bit naughty... creating groups on the drop of
756 ;; a hat, but I don't know...
757 (push (list group (setq active (cons 1 1)))
758 nnfolder-group-alist))
761 (defun nnfolder-possibly-change-folder (group)
762 (let ((inf (assoc group nnfolder-buffer-alist)))
764 (gnus-buffer-live-p (cadr inf)))
765 (set-buffer (cadr inf))
767 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
768 (when nnfolder-group-alist
769 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
770 (push (list group (nnfolder-read-folder group))
771 nnfolder-buffer-alist))))
773 ;; This method has a problem if you've accidentally let the active list get
774 ;; out of sync with the files. This could happen, say, if you've
775 ;; accidentally gotten new mail with something other than Gnus (but why
776 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
777 ;; processing the file, ready to add new X-Gnus article number markers, and
778 ;; we'll run across a message with no ID yet - the active list _may_not_ be
781 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
782 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
783 ;; rest of the message looking for any more, possibly higher IDs. We'll
784 ;; assume the maximum that we find is the highest active. Note that this
785 ;; shouldn't cost us much extra time at all, but will be a lot less
786 ;; vulnerable to glitches between the mbox and the active file.
788 (defun nnfolder-read-folder (group)
789 (let* ((file (nnfolder-group-pathname group))
790 (nov (nnfolder-group-nov-pathname group))
792 (let ((nnheader-file-coding-system
793 nnfolder-file-coding-system))
794 (nnheader-find-file-noselect file)))))
795 (if (equal (cadr (assoc group nnfolder-scantime-alist))
796 (nth 5 (file-attributes file)))
797 ;; This looks up-to-date, so we don't do any scanning.
798 (if (file-exists-p file)
800 (push (list group buffer) nnfolder-buffer-alist)
801 (set-buffer-modified-p t)
802 (nnfolder-save-buffer))
803 ;; Parse the damn thing.
805 (goto-char (point-min))
806 ;; Remove any blank lines at the start.
807 (while (eq (following-char) ?\n)
809 (nnmail-activate 'nnfolder)
811 (let ((delim "^From ")
812 (marker (concat "\n" nnfolder-article-marker))
814 (active (or (cadr (assoc group nnfolder-group-alist))
816 (scantime (assoc group nnfolder-scantime-alist))
818 maxid start end newscantime
819 novbuf articles newnum
821 (buffer-disable-undo)
822 (setq maxid (cdr active))
824 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
825 (and (file-exists-p nov)
826 (file-newer-than-file-p nov file)))
827 (unless (file-exists-p nov)
828 (gnus-make-directory (file-name-directory nov)))
830 (setq novbuf (nnfolder-open-nov group))
831 (goto-char (point-min))
833 (push (read novbuf) articles)
835 (setq articles (nreverse articles))))
836 (goto-char (point-min))
838 ;; Anytime the active number is 1 or 0, it is suspect. In that
839 ;; case, search the file manually to find the active number. Or,
840 ;; of course, if we're being paranoid. (This would also be the
841 ;; place to build other lists from the header markers, such as
842 ;; expunge lists, etc., if we ever desired to abandon the active
843 ;; file entirely for mboxes.)
844 (when (or nnfolder-ignore-active-file
847 (while (and (search-forward marker nil t)
849 (setq newnum (string-to-number (match-string 0)))
850 (when (nnmail-within-headers-p)
851 (setq maxid (max maxid newnum)
852 minid (min minid newnum))
854 (if (memq newnum articles)
855 (setq articles (delq newnum articles))
856 (let ((headers (nnfolder-parse-head newnum)))
857 (with-current-buffer novbuf
858 (nnheader-find-nov-line newnum)
859 (nnheader-insert-nov headers)))))))
860 (when (and novbuf articles)
861 (with-current-buffer novbuf
862 (dolist (article articles)
863 (when (nnheader-find-nov-line article)
864 (delete-region (point)
865 (progn (forward-line 1) (point)))))))
866 (setcar active (max 1 (min minid maxid)))
867 (setcdr active (max maxid (cdr active)))
868 (goto-char (point-min)))
870 ;; As long as we trust that the user will only insert unmarked mail
871 ;; at the end, go to the end and search backwards for the last
872 ;; marker. Find the start of that message, and begin to search for
873 ;; unmarked messages from there.
874 (when (not (or nnfolder-distrust-mbox
876 (goto-char (point-max))
877 (unless (re-search-backward marker nil t)
878 (goto-char (point-min)))
879 ;;(when (nnmail-search-unix-mail-delim)
880 ;; (goto-char (point-min)))
883 ;; Keep track of the active number on our own, and insert it back
884 ;; into the active list when we're done. Also, prime the pump to
885 ;; cut down on the number of searches we do.
886 (unless (nnmail-search-unix-mail-delim)
887 (goto-char (point-max)))
888 (setq end (point-marker))
889 (while (not (= end (point-max)))
890 (setq start (marker-position end))
892 ;; There may be more than one "From " line, so we skip past
894 (while (looking-at delim)
896 (set-marker end (if (nnmail-search-unix-mail-delim)
900 (when (not (search-forward marker end t))
901 (narrow-to-region start end)
902 (nnmail-insert-lines)
903 (nnfolder-insert-newsgroup-line
906 (nnfolder-active-number nnfolder-current-group))))
908 (let ((headers (nnfolder-parse-head newnum (point-min)
910 (with-current-buffer novbuf
911 (goto-char (point-max))
912 (nnheader-insert-nov headers))))
916 ;; Make absolutely sure that the active list reflects reality!
917 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
919 ;; Set the scantime for this group.
920 (setq newscantime (visited-file-modtime))
922 (setcdr scantime (list newscantime))
923 (push (list nnfolder-current-group newscantime)
924 nnfolder-scantime-alist))
928 (current-buffer))))))
931 (defun nnfolder-generate-active-file ()
932 "Look for mbox folders in the nnfolder directory and make them into groups.
933 This command does not work if you use short group names."
935 (nnmail-activate 'nnfolder)
936 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
937 (dolist (file (directory-files (or nnfolder-nov-directory
941 (regexp-quote nnfolder-nov-file-suffix)
943 (when (not (message-mail-file-mbox-p file))
945 (delete-file file)))))
946 (let ((files (directory-files nnfolder-directory))
948 (while (setq file (pop files))
949 (when (and (not (backup-file-name-p file))
950 (message-mail-file-mbox-p
951 (nnheader-concat nnfolder-directory file)))
952 (let ((oldgroup (assoc file nnfolder-group-alist)))
954 (nnheader-message 5 "Refreshing group %s..." file)
955 (nnheader-message 5 "Adding group %s..." file))
957 (setq nnfolder-group-alist
958 (delq oldgroup (copy-sequence nnfolder-group-alist))))
959 (push (list file (cons 1 0)) nnfolder-group-alist)
960 (nnfolder-possibly-change-folder file)
961 (nnfolder-possibly-change-group file)
962 (nnfolder-close-group file))))
963 (nnheader-message 5 "")))
965 (defun nnfolder-group-pathname (group)
966 "Make pathname for GROUP."
968 (mm-encode-coding-string group nnmail-pathname-coding-system))
969 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
970 ;; If this file exists, we use it directly.
971 (if (or nnmail-use-long-file-names
972 (file-exists-p (concat dir group)))
974 ;; If not, we translate dots into slashes.
975 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
977 (defun nnfolder-group-nov-pathname (group)
978 "Make pathname for GROUP NOV."
979 (let ((nnfolder-directory
980 (or nnfolder-nov-directory nnfolder-directory)))
981 (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
983 (defun nnfolder-save-buffer ()
985 (when (buffer-modified-p)
986 (run-hooks 'nnfolder-save-buffer-hook)
987 (gnus-make-directory (file-name-directory (buffer-file-name)))
988 (let ((coding-system-for-write
989 (or nnfolder-file-coding-system-for-write
990 nnfolder-file-coding-system)))
992 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
993 (nnfolder-save-nov)))
995 (defun nnfolder-save-active (group-alist active-file)
996 (let ((nnmail-active-file-coding-system
997 (or nnfolder-active-file-coding-system-for-write
998 nnfolder-active-file-coding-system)))
999 (nnmail-save-active group-alist active-file)))
1001 (defun nnfolder-open-nov (group)
1002 (or (cdr (assoc group nnfolder-nov-buffer-alist))
1003 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1006 (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1007 (nnfolder-group-nov-pathname group))
1009 (when (file-exists-p nnfolder-nov-buffer-file-name)
1010 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1011 (push (cons group buffer) nnfolder-nov-buffer-alist)
1014 (defun nnfolder-save-nov ()
1016 (while nnfolder-nov-buffer-alist
1017 (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1018 (set-buffer (cdar nnfolder-nov-buffer-alist))
1019 (when (buffer-modified-p)
1020 (gnus-make-directory (file-name-directory
1021 nnfolder-nov-buffer-file-name))
1022 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1024 (set-buffer-modified-p nil)
1025 (kill-buffer (current-buffer)))
1026 (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1028 (defun nnfolder-nov-delete-article (group article)
1030 (set-buffer (nnfolder-open-nov group))
1031 (when (nnheader-find-nov-line article)
1032 (delete-region (point) (progn (forward-line 1) (point))))
1035 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1036 (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1038 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1039 (when (file-exists-p nov)
1041 (set-buffer nntp-server-buffer)
1043 (nnheader-insert-file-contents nov)
1045 (not (numberp fetch-old)))
1046 t ; Don't remove anything.
1047 (nnheader-nov-delete-outside-range
1048 (if fetch-old (max 1 (- (car articles) fetch-old))
1050 (car (last articles)))
1053 (defun nnfolder-parse-head (&optional number b e)
1054 "Parse the head of the current buffer."
1055 (let ((buf (current-buffer))
1059 (setq b (if (nnmail-search-unix-mail-delim-backward)
1060 (point) (point-min)))
1062 (setq e (if (nnmail-search-unix-mail-delim)
1063 (point) (point-max))))
1064 (setq chars (- e b))
1065 (unless (zerop chars)
1067 (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1069 (insert-buffer-substring buf b e)
1070 ;; Fold continuation lines.
1071 (goto-char (point-min))
1072 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1073 (replace-match " " t t))
1074 ;; Remove any tabs; they are too confusing.
1075 (subst-char-in-region (point-min) (point-max) ?\t ? )
1076 (let ((headers (nnheader-parse-head t)))
1077 (mail-header-set-chars headers chars)
1078 (mail-header-set-number headers number)
1081 (defun nnfolder-add-nov (group article headers)
1082 "Add a nov line for the GROUP base."
1084 (set-buffer (nnfolder-open-nov group))
1085 (goto-char (point-max))
1086 (mail-header-set-number headers article)
1087 (nnheader-insert-nov headers)))
1091 ;;; nnfolder.el ends here