1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Scott Byer <byer@mv.us.adobe.com>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
35 (eval-when-compile (require 'cl))
38 (nnoo-declare nnfolder)
40 (defvoo nnfolder-directory (expand-file-name message-directory)
41 "The name of the nnfolder directory.")
43 (defvoo nnfolder-active-file
44 (nnheader-concat nnfolder-directory "active")
45 "The name of the active file.")
47 ;; I renamed this variable to something more in keeping with the general GNU
50 (defvoo nnfolder-ignore-active-file nil
51 "If non-nil, the active file is ignores.
52 This causes nnfolder to do some extra work in order to determine the
53 true active ranges of an mbox file. Note that the active file is
54 still saved, but it's values are not used. This costs some extra time
55 when scanning an mbox when opening it.")
57 (defvoo nnfolder-distrust-mbox nil
58 "If non-nil, the folder will be distrusted.
59 This means that nnfolder will not trust the user with respect to
60 inserting unaccounted for mail in the middle of an mbox file. This
61 can greatly slow down scans, which now must scan the entire file for
62 unmarked messages. When nil, scans occur forward from the last marked
63 message, a huge time saver for large mailboxes.")
65 (defvoo nnfolder-newsgroups-file
66 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
67 "Mail newsgroups description file.")
69 (defvoo nnfolder-get-new-mail t
70 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
72 (defvoo nnfolder-prepare-save-mail-hook nil
73 "Hook run narrowed to an article before saving.")
75 (defvoo nnfolder-save-buffer-hook nil
76 "Hook run before saving the nnfolder mbox buffer.")
78 (defvoo nnfolder-inhibit-expiry nil
79 "If non-nil, inhibit expiry.")
83 (defconst nnfolder-version "nnfolder 1.0"
86 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
87 "String used to demarcate what the article number for a message is.")
89 (defvoo nnfolder-current-group nil)
90 (defvoo nnfolder-current-buffer nil)
91 (defvoo nnfolder-status-string "")
92 (defvoo nnfolder-group-alist nil)
93 (defvoo nnfolder-buffer-alist nil)
94 (defvoo nnfolder-scantime-alist nil)
95 (defvoo nnfolder-active-timestamp nil)
96 (defvoo nnfolder-active-file-coding-system
97 (if (memq system-type '(windows-nt ms-dos ms-windows))
98 'raw-text-dos 'raw-text))
99 (defvoo nnfolder-active-file-coding-system-for-write
100 nnmail-active-file-coding-system)
101 (defvoo nnfolder-file-coding-system nnfolder-active-file-coding-system)
102 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
103 "Coding system for save nnfolder file.
104 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
108 ;;; Interface functions
110 (nnoo-define-basics nnfolder)
112 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
114 (set-buffer nntp-server-buffer)
116 (let (article start stop)
117 (nnfolder-possibly-change-group group server)
118 (when nnfolder-current-buffer
119 (set-buffer nnfolder-current-buffer)
120 (goto-char (point-min))
121 (if (stringp (car articles))
123 (while (setq article (pop articles))
124 (set-buffer nnfolder-current-buffer)
125 (when (nnfolder-goto-article article)
127 (setq stop (if (search-forward "\n\n" nil t)
130 (set-buffer nntp-server-buffer)
131 (insert (format "221 %d Article retrieved.\n" article))
132 (insert-buffer-substring nnfolder-current-buffer start stop)
133 (goto-char (point-max))
136 (set-buffer nntp-server-buffer)
137 (nnheader-fold-continuation-lines)
140 (deffoo nnfolder-open-server (server &optional defs)
141 (nnoo-change-server 'nnfolder server defs)
142 (nnmail-activate 'nnfolder t)
143 (gnus-make-directory nnfolder-directory)
145 ((not (file-exists-p nnfolder-directory))
146 (nnfolder-close-server)
147 (nnheader-report 'nnfolder "Couldn't create directory: %s"
149 ((not (file-directory-p (file-truename nnfolder-directory)))
150 (nnfolder-close-server)
151 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
153 (nnmail-activate 'nnfolder)
154 (nnheader-report 'nnfolder "Opened server %s using directory %s"
155 server nnfolder-directory)
158 (deffoo nnfolder-request-close ()
159 (let ((alist nnfolder-buffer-alist))
161 (nnfolder-close-group (caar alist) nil t)
162 (setq alist (cdr alist))))
163 (nnoo-close-server 'nnfolder)
164 (setq nnfolder-buffer-alist nil
165 nnfolder-group-alist nil))
167 (deffoo nnfolder-request-article (article &optional group server buffer)
168 (nnfolder-possibly-change-group group server)
170 (set-buffer nnfolder-current-buffer)
171 (goto-char (point-min))
172 (when (nnfolder-goto-article article)
176 (unless (and (nnmail-search-unix-mail-delim)
178 (goto-char (point-max)))
180 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
181 (set-buffer nntp-server-buffer)
183 (insert-buffer-substring nnfolder-current-buffer start stop)
184 (goto-char (point-min))
185 (while (looking-at "From ")
187 (insert "X-From-Line: ")
189 (if (numberp article)
190 (cons nnfolder-current-group article)
191 (goto-char (point-min))
192 (cons nnfolder-current-group
193 (if (search-forward (concat "\n" nnfolder-article-marker)
197 (point) (progn (end-of-line) (point))))
200 (deffoo nnfolder-request-group (group &optional server dont-check)
201 (nnfolder-possibly-change-group group server t)
203 (if (not (assoc group nnfolder-group-alist))
204 (nnheader-report 'nnfolder "No such group: %s" group)
207 (nnheader-report 'nnfolder "Selected group %s" group)
209 (let* ((active (assoc group nnfolder-group-alist))
211 (range (cadr active)))
214 (nnheader-report 'nnfolder "No such group: %s" group))
215 ((null nnfolder-current-group)
216 (nnheader-report 'nnfolder "Empty group: %s" group))
218 (nnheader-report 'nnfolder "Selected group %s" group)
219 (nnheader-insert "211 %d %d %d %s\n"
220 (1+ (- (cdr range) (car range)))
221 (car range) (cdr range) group))))))))
223 (deffoo nnfolder-request-scan (&optional group server)
224 (nnfolder-possibly-change-group nil server)
225 (when nnfolder-get-new-mail
226 (nnfolder-possibly-change-group group server)
230 (let ((bufs nnfolder-buffer-alist))
233 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
234 (setq nnfolder-buffer-alist
235 (delq (car bufs) nnfolder-buffer-alist))
236 (set-buffer (nth 1 (car bufs)))
237 (nnfolder-save-buffer)
238 (kill-buffer (current-buffer)))
239 (setq bufs (cdr bufs))))))
243 ;; Don't close the buffer if we're not shutting down the server. This way,
244 ;; we can keep the buffer in the group buffer cache, and not have to grovel
245 ;; over the buffer again unless we add new mail to it or modify it in some
248 (deffoo nnfolder-close-group (group &optional server force)
249 ;; Make sure we _had_ the group open.
250 (when (or (assoc group nnfolder-buffer-alist)
251 (equal group nnfolder-current-group))
252 (let ((inf (assoc group nnfolder-buffer-alist)))
254 (when (and nnfolder-current-group
255 nnfolder-current-buffer)
256 (push (list nnfolder-current-group nnfolder-current-buffer)
257 nnfolder-buffer-alist))
258 (setq nnfolder-buffer-alist
259 (delq inf nnfolder-buffer-alist))
260 (setq nnfolder-current-buffer (cadr inf)
261 nnfolder-current-group (car inf))))
262 (when (and nnfolder-current-buffer
263 (buffer-name nnfolder-current-buffer))
265 (set-buffer nnfolder-current-buffer)
266 ;; If the buffer was modified, write the file out now.
267 (nnfolder-save-buffer)
268 ;; If we're shutting the server down, we need to kill the
269 ;; buffer and remove it from the open buffer list. Or, of
270 ;; course, if we're trying to minimize our space impact.
271 (kill-buffer (current-buffer))
272 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
273 nnfolder-buffer-alist)))))
274 (setq nnfolder-current-group nil
275 nnfolder-current-buffer nil)
278 (deffoo nnfolder-request-create-group (group &optional server args)
279 (nnfolder-possibly-change-group nil server)
280 (nnmail-activate 'nnfolder)
282 (unless (assoc group nnfolder-group-alist)
283 (push (list group (cons 1 0)) nnfolder-group-alist)
284 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
285 (nnfolder-read-folder group)))
288 (deffoo nnfolder-request-list (&optional server)
289 (nnfolder-possibly-change-group nil server)
291 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
292 (nnmail-find-file nnfolder-active-file)
293 (setq nnfolder-group-alist (nnmail-get-active)))
296 (deffoo nnfolder-request-newgroups (date &optional server)
297 (nnfolder-possibly-change-group nil server)
298 (nnfolder-request-list server))
300 (deffoo nnfolder-request-list-newsgroups (&optional server)
301 (nnfolder-possibly-change-group nil server)
303 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
304 (nnmail-find-file nnfolder-newsgroups-file))))
306 ;; Return a list consisting of all article numbers existing in the
309 (defun nnfolder-existing-articles ()
311 (when nnfolder-current-buffer
312 (set-buffer nnfolder-current-buffer)
313 (goto-char (point-min))
314 (let ((marker (concat "\n" nnfolder-article-marker))
318 (while (and (search-forward marker nil t)
319 (re-search-forward number nil t))
320 (let ((newnum (string-to-number (match-string 0))))
321 (if (nnmail-within-headers-p)
322 (push newnum numbers))))
325 (deffoo nnfolder-request-expire-articles
326 (articles newsgroup &optional server force)
327 (nnfolder-possibly-change-group newsgroup server)
329 ;; The articles we have deleted so far.
330 (deleted-articles nil)
331 ;; The articles that really exist and will
332 ;; be expired if they are old enough.
334 (gnus-intersection articles (nnfolder-existing-articles))))
335 (nnmail-activate 'nnfolder)
338 (set-buffer nnfolder-current-buffer)
339 ;; Since messages are sorted in arrival order and expired in the
340 ;; same order, we can stop as soon as we find a message that is
342 (while (and maybe-expirable is-old)
343 (goto-char (point-min))
344 (when (and (nnfolder-goto-article (car maybe-expirable))
345 (search-forward (concat "\n" nnfolder-article-marker)
349 (nnmail-expired-article-p
352 (point) (progn (end-of-line) (point)))
353 force nnfolder-inhibit-expiry))
354 (nnheader-message 5 "Deleting article %d..."
355 (car maybe-expirable) newsgroup)
356 (nnfolder-delete-mail)
357 ;; Must remember which articles were actually deleted
358 (push (car maybe-expirable) deleted-articles)))
359 (setq maybe-expirable (cdr maybe-expirable)))
360 (unless nnfolder-inhibit-expiry
361 (nnheader-message 5 "Deleting articles...done"))
362 (nnfolder-save-buffer)
363 (nnfolder-adjust-min-active newsgroup)
364 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
365 (gnus-sorted-complement articles (nreverse deleted-articles)))))
367 (deffoo nnfolder-request-move-article (article group server
368 accept-form &optional last)
370 (let ((buf (get-buffer-create " *nnfolder move*"))
373 (nnfolder-request-article article group server)
377 (insert-buffer-substring nntp-server-buffer)
378 (goto-char (point-min))
379 (while (re-search-forward
380 (concat "^" nnfolder-article-marker)
381 (save-excursion (and (search-forward "\n\n" nil t) (point)))
383 (delete-region (progn (beginning-of-line) (point))
384 (progn (forward-line 1) (point))))
385 (setq result (eval accept-form))
389 (nnfolder-possibly-change-group group server)
390 (set-buffer nnfolder-current-buffer)
391 (goto-char (point-min))
392 (when (nnfolder-goto-article article)
393 (nnfolder-delete-mail))
395 (nnfolder-save-buffer)
396 (nnfolder-adjust-min-active group)
397 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
400 (deffoo nnfolder-request-accept-article (group &optional server last)
402 (nnfolder-possibly-change-group group server)
403 (nnmail-check-syntax)
404 (let ((buf (current-buffer))
406 (goto-char (point-min))
407 (when (looking-at "X-From-Line: ")
408 (replace-match "From "))
410 (nnfolder-request-list)
413 (goto-char (point-min))
414 (if (search-forward "\n\n" nil t)
416 (goto-char (point-max)))
417 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
418 (delete-region (point) (progn (forward-line 1) (point))))
419 (when nnmail-cache-accepted-message-ids
420 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
421 (setq result (if (stringp group)
422 (list (cons group (nnfolder-active-number group)))
424 (nnmail-article-group 'nnfolder-active-number))))
425 (if (and (null result)
426 (yes-or-no-p "Moved to `junk' group; delete article? "))
429 (car (nnfolder-save-mail result)))))
432 (nnfolder-possibly-change-folder (or (caar art-group) group))
433 (nnfolder-save-buffer)
434 (when nnmail-cache-accepted-message-ids
435 (nnmail-cache-close)))))
436 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
438 (nnheader-report 'nnfolder "Couldn't store article"))
441 (deffoo nnfolder-request-replace-article (article group buffer)
442 (nnfolder-possibly-change-group group)
445 (goto-char (point-min))
447 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
448 (setq xfrom (match-string 1))
450 (goto-char (point-min))
452 (insert "From " xfrom "\n")
453 (unless (looking-at "From ")
454 (insert "From nobody " (current-time-string) "\n"))))
455 (nnfolder-normalize-buffer)
456 (set-buffer nnfolder-current-buffer)
457 (goto-char (point-min))
458 (if (not (nnfolder-goto-article article))
460 (nnfolder-delete-mail)
461 (insert-buffer-substring buffer)
462 (nnfolder-save-buffer)
465 (deffoo nnfolder-request-delete-group (group &optional force server)
466 (nnfolder-close-group group server t)
467 ;; Delete all articles in GROUP.
469 () ; Don't delete the articles.
470 ;; Delete the file that holds the group.
472 (delete-file (nnfolder-group-pathname group))))
473 ;; Remove the group from all structures.
474 (setq nnfolder-group-alist
475 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
476 nnfolder-current-group nil
477 nnfolder-current-buffer nil)
478 ;; Save the active file.
479 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
482 (deffoo nnfolder-request-rename-group (group new-name &optional server)
483 (nnfolder-possibly-change-group group server)
485 (set-buffer nnfolder-current-buffer)
486 (and (file-writable-p buffer-file-name)
490 (let ((new-file (nnfolder-group-pathname new-name)))
491 (gnus-make-directory (file-name-directory new-file))
494 ;; That went ok, so we change the internal structures.
495 (let ((entry (assoc group nnfolder-group-alist)))
496 (and entry (setcar entry new-name))
497 (setq nnfolder-current-buffer nil
498 nnfolder-current-group nil)
499 ;; Save the new group alist.
500 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
501 ;; We kill the buffer instead of renaming it and stuff.
502 (kill-buffer (current-buffer))
505 (defun nnfolder-request-regenerate (server)
506 (nnfolder-possibly-change-group nil server)
507 (nnfolder-generate-active-file)
511 ;;; Internal functions.
513 (defun nnfolder-adjust-min-active (group)
514 ;; Find the lowest active article in this group.
515 (let* ((active (cadr (assoc group nnfolder-group-alist)))
516 (marker (concat "\n" nnfolder-article-marker))
518 (activemin (cdr active)))
520 (set-buffer nnfolder-current-buffer)
521 (goto-char (point-min))
522 (while (and (search-forward marker nil t)
523 (re-search-forward number nil t))
524 (let ((newnum (string-to-number (match-string 0))))
525 (if (nnmail-within-headers-p)
526 (setq activemin (min activemin newnum)))))
527 (setcar active activemin))))
529 (defun nnfolder-article-string (article)
530 (if (numberp article)
531 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
532 (concat "\nMessage-ID: " article)))
534 (defun nnfolder-goto-article (article)
535 "Place point at the start of the headers of ARTICLE.
536 ARTICLE can be an article number or a Message-ID.
537 Returns t if successful, nil otherwise."
538 (let ((art-string (nnfolder-article-string article))
540 ;; It is likely that we are at or before the delimiter line.
541 ;; We therefore go to the end of the previous line, and start
542 ;; searching from there.
547 ;; First search forward.
548 (while (and (setq found (search-forward art-string nil t))
549 (not (nnmail-within-headers-p))))
550 ;; If unsuccessful, search backward from where we started,
553 (while (and (setq found (search-backward art-string nil t))
554 (not (nnmail-within-headers-p)))))
556 (nnmail-search-unix-mail-delim-backward))))
558 (defun nnfolder-delete-mail (&optional leave-delim)
559 "Delete the message that point is in.
560 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
561 deleted. Point is left where the deleted region was."
565 ;; In case point is at the beginning of the message already.
567 (nnmail-search-unix-mail-delim-backward)
568 (if leave-delim (progn (forward-line 1) (point))
572 (if (nnmail-search-unix-mail-delim)
575 (run-hooks 'nnfolder-delete-mail-hook)
576 (delete-region (point-min) (point-max))))
578 (defun nnfolder-possibly-change-group (group &optional server dont-check)
581 (not (nnfolder-server-opened server)))
582 (nnfolder-open-server server))
583 (unless (gnus-buffer-live-p nnfolder-current-buffer)
584 (setq nnfolder-current-buffer nil
585 nnfolder-current-group nil))
588 (not (equal group nnfolder-current-group)))
589 (nnmail-activate 'nnfolder)
591 (setq nnfolder-current-group group
592 nnfolder-current-buffer nil)
593 ;; If we have to change groups, see if we don't already have the
594 ;; folder in memory. If we do, verify the modtime and destroy
595 ;; the folder if needed so we can rescan it.
596 (setq nnfolder-current-buffer
597 (nth 1 (assoc group nnfolder-buffer-alist)))
599 ;; If the buffer is not live, make sure it isn't in the alist. If it
600 ;; is live, verify that nobody else has touched the file since last
602 (when (and nnfolder-current-buffer
603 (not (gnus-buffer-live-p nnfolder-current-buffer)))
604 (setq nnfolder-current-buffer nil))
606 (setq nnfolder-current-group group)
608 (when (or (not nnfolder-current-buffer)
609 (not (verify-visited-file-modtime
610 nnfolder-current-buffer)))
612 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
613 (set-buffer nnfolder-current-buffer)
614 (push (list group nnfolder-current-buffer)
615 nnfolder-buffer-alist)))))))
617 (defun nnfolder-save-mail (group-art-list)
618 "Called narrowed to an article."
619 (let* (save-list group-art)
620 (goto-char (point-min))
621 ;; The From line may have been quoted by movemail.
622 (when (looking-at ">From")
624 ;; This might come from somewhere else.
625 (unless (looking-at "From ")
626 (insert "From nobody " (current-time-string) "\n")
627 (goto-char (point-min)))
628 ;; Quote all "From " lines in the article.
630 (let (case-fold-search)
631 (while (re-search-forward "^From " nil t)
634 (setq save-list group-art-list)
635 (nnmail-insert-lines)
636 (nnmail-insert-xref group-art-list)
637 (run-hooks 'nnmail-prepare-save-mail-hook)
638 (run-hooks 'nnfolder-prepare-save-mail-hook)
640 ;; Insert the mail into each of the destination groups.
641 (while (setq group-art (pop group-art-list))
642 ;; Kill any previous newsgroup markers.
643 (goto-char (point-min))
644 (if (search-forward "\n\n" nil t)
646 (goto-char (point-max)))
647 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
648 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
650 ;; Insert the new newsgroup marker.
651 (nnfolder-insert-newsgroup-line group-art)
654 (let ((beg (point-min))
656 (obuf (current-buffer)))
657 (nnfolder-possibly-change-folder (car group-art))
658 (let ((buffer-read-only nil))
659 (nnfolder-normalize-buffer)
660 (insert-buffer-substring obuf beg end)))))
662 ;; Did we save it anywhere?
665 (defun nnfolder-normalize-buffer ()
666 "Make sure there are two newlines at the end of the buffer."
667 (goto-char (point-max))
668 (skip-chars-backward "\n")
669 (delete-region (point) (point-max))
672 (defun nnfolder-insert-newsgroup-line (group-art)
674 (goto-char (point-min))
675 (unless (search-forward "\n\n" nil t)
676 (goto-char (point-max))
679 (insert (format (concat nnfolder-article-marker "%d %s\n")
680 (cdr group-art) (current-time-string)))))
682 (defun nnfolder-active-number (group)
683 ;; Find the next article number in GROUP.
684 (let ((active (cadr (assoc group nnfolder-group-alist))))
686 (setcdr active (1+ (cdr active)))
687 ;; This group is new, so we create a new entry for it.
688 ;; This might be a bit naughty... creating groups on the drop of
689 ;; a hat, but I don't know...
690 (push (list group (setq active (cons 1 1)))
691 nnfolder-group-alist))
694 (defun nnfolder-possibly-change-folder (group)
695 (let ((inf (assoc group nnfolder-buffer-alist)))
697 (gnus-buffer-live-p (cadr inf)))
698 (set-buffer (cadr inf))
700 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
701 (when nnfolder-group-alist
702 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
703 (push (list group (nnfolder-read-folder group))
704 nnfolder-buffer-alist))))
706 ;; This method has a problem if you've accidentally let the active list get
707 ;; out of sync with the files. This could happen, say, if you've
708 ;; accidentally gotten new mail with something other than Gnus (but why
709 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
710 ;; processing the file, ready to add new X-Gnus article number markers, and
711 ;; we'll run across a message with no ID yet - the active list _may_not_ be
714 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
715 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
716 ;; rest of the message looking for any more, possibly higher IDs. We'll
717 ;; assume the maximum that we find is the highest active. Note that this
718 ;; shouldn't cost us much extra time at all, but will be a lot less
719 ;; vulnerable to glitches between the mbox and the active file.
721 (defun nnfolder-read-folder (group)
722 (let* ((file (nnfolder-group-pathname group))
724 (let ((nnheader-file-coding-system
725 nnfolder-file-coding-system))
726 (nnheader-find-file-noselect file)))))
727 (if (equal (cadr (assoc group nnfolder-scantime-alist))
728 (nth 5 (file-attributes file)))
729 ;; This looks up-to-date, so we don't do any scanning.
730 (if (file-exists-p file)
732 (push (list group buffer) nnfolder-buffer-alist)
733 (set-buffer-modified-p t)
734 (nnfolder-save-buffer))
735 ;; Parse the damn thing.
737 (goto-char (point-min))
738 ;; Remove any blank lines at the start.
739 (while (eq (following-char) ?\n)
741 (nnmail-activate 'nnfolder)
743 (let ((delim "^From ")
744 (marker (concat "\n" nnfolder-article-marker))
746 (active (or (cadr (assoc group nnfolder-group-alist))
748 (scantime (assoc group nnfolder-scantime-alist))
750 maxid start end newscantime
752 (buffer-disable-undo)
753 (setq maxid (cdr active))
754 (goto-char (point-min))
756 ;; Anytime the active number is 1 or 0, it is suspect. In that
757 ;; case, search the file manually to find the active number. Or,
758 ;; of course, if we're being paranoid. (This would also be the
759 ;; place to build other lists from the header markers, such as
760 ;; expunge lists, etc., if we ever desired to abandon the active
761 ;; file entirely for mboxes.)
762 (when (or nnfolder-ignore-active-file
764 (while (and (search-forward marker nil t)
765 (re-search-forward number nil t))
766 (let ((newnum (string-to-number (match-string 0))))
767 (if (nnmail-within-headers-p)
768 (setq maxid (max maxid newnum)
769 minid (min minid newnum)))))
770 (setcar active (max 1 (min minid maxid)))
771 (setcdr active (max maxid (cdr active)))
772 (goto-char (point-min)))
774 ;; As long as we trust that the user will only insert unmarked mail
775 ;; at the end, go to the end and search backwards for the last
776 ;; marker. Find the start of that message, and begin to search for
777 ;; unmarked messages from there.
778 (when (not (or nnfolder-distrust-mbox
780 (goto-char (point-max))
781 (unless (re-search-backward marker nil t)
782 (goto-char (point-min)))
783 (when (nnmail-search-unix-mail-delim)
784 (goto-char (point-min))))
786 ;; Keep track of the active number on our own, and insert it back
787 ;; into the active list when we're done. Also, prime the pump to
788 ;; cut down on the number of searches we do.
789 (unless (nnmail-search-unix-mail-delim)
790 (goto-char (point-max)))
791 (setq end (point-marker))
792 (while (not (= end (point-max)))
793 (setq start (marker-position end))
795 ;; There may be more than one "From " line, so we skip past
797 (while (looking-at delim)
799 (set-marker end (if (nnmail-search-unix-mail-delim)
803 (when (not (search-forward marker end t))
804 (narrow-to-region start end)
805 (nnmail-insert-lines)
806 (nnfolder-insert-newsgroup-line
807 (cons nil (nnfolder-active-number nnfolder-current-group)))
811 ;; Make absolutely sure that the active list reflects reality!
812 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
813 ;; Set the scantime for this group.
814 (setq newscantime (visited-file-modtime))
816 (setcdr scantime (list newscantime))
817 (push (list nnfolder-current-group newscantime)
818 nnfolder-scantime-alist))
819 (current-buffer))))))
822 (defun nnfolder-generate-active-file ()
823 "Look for mbox folders in the nnfolder directory and make them into groups.
824 This command does not work if you use short group names."
826 (nnmail-activate 'nnfolder)
827 (let ((files (directory-files nnfolder-directory))
829 (while (setq file (pop files))
830 (when (and (not (backup-file-name-p file))
831 (message-mail-file-mbox-p
832 (nnheader-concat nnfolder-directory file)))
833 (let ((oldgroup (assoc file nnfolder-group-alist)))
835 (nnheader-message 5 "Refreshing group %s..." file)
836 (nnheader-message 5 "Adding group %s..." file))
838 (setq nnfolder-group-alist
839 (delq oldgroup (copy-sequence nnfolder-group-alist))))
840 (push (list file (cons 1 0)) nnfolder-group-alist)
841 (nnfolder-possibly-change-folder file)
842 (nnfolder-possibly-change-group file)
843 (nnfolder-close-group file))))
844 (nnheader-message 5 "")))
846 (defun nnfolder-group-pathname (group)
847 "Make pathname for GROUP."
849 (encode-coding-string group nnmail-pathname-coding-system))
850 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
851 ;; If this file exists, we use it directly.
852 (if (or nnmail-use-long-file-names
853 (file-exists-p (concat dir group)))
855 ;; If not, we translate dots into slashes.
856 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
858 (defun nnfolder-save-buffer ()
860 (when (buffer-modified-p)
861 (run-hooks 'nnfolder-save-buffer-hook)
862 (gnus-make-directory (file-name-directory (buffer-file-name)))
863 (let* ((coding-system-for-write
864 (or nnfolder-file-coding-system-for-write
865 nnfolder-file-coding-system))
866 (output-coding-system coding-system-for-write))
869 (defun nnfolder-save-active (group-alist active-file)
870 (let ((nnmail-active-file-coding-system
871 (or nnfolder-active-file-coding-system-for-write
872 nnfolder-active-file-coding-system)))
873 (nnmail-save-active group-alist active-file)))
877 ;;; nnfolder.el ends here