1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
4 ;; Author: Scott Byer <byer@mv.us.adobe.com>
5 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
34 (eval-when-compile (require 'cl))
37 (nnoo-declare nnfolder)
39 (defvoo nnfolder-directory (expand-file-name message-directory)
40 "The name of the nnfolder directory.")
42 (defvoo nnfolder-active-file
43 (nnheader-concat nnfolder-directory "active")
44 "The name of the active file.")
46 ;; I renamed this variable to something more in keeping with the general GNU
49 (defvoo nnfolder-ignore-active-file nil
50 "If non-nil, causes nnfolder to do some extra work in order to determine
51 the true active ranges of an mbox file. Note that the active file is still
52 saved, but it's values are not used. This costs some extra time when
53 scanning an mbox when opening it.")
55 (defvoo nnfolder-distrust-mbox nil
56 "If non-nil, causes nnfolder to not trust the user with respect to
57 inserting unaccounted for mail in the middle of an mbox file. This can greatly
58 slow down scans, which now must scan the entire file for unmarked messages.
59 When nil, scans occur forward from the last marked message, a huge
60 time saver for large mailboxes.")
62 (defvoo nnfolder-newsgroups-file
63 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
64 "Mail newsgroups description file.")
66 (defvoo nnfolder-get-new-mail t
67 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
69 (defvoo nnfolder-prepare-save-mail-hook nil
70 "Hook run narrowed to an article before saving.")
72 (defvoo nnfolder-save-buffer-hook nil
73 "Hook run before saving the nnfolder mbox buffer.")
75 (defvoo nnfolder-inhibit-expiry nil
76 "If non-nil, inhibit expiry.")
80 (defconst nnfolder-version "nnfolder 1.0"
83 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
84 "String used to demarcate what the article number for a message is.")
86 (defvoo nnfolder-current-group nil)
87 (defvoo nnfolder-current-buffer nil)
88 (defvoo nnfolder-status-string "")
89 (defvoo nnfolder-group-alist nil)
90 (defvoo nnfolder-buffer-alist nil)
91 (defvoo nnfolder-scantime-alist nil)
92 (defvoo nnfolder-active-timestamp nil)
96 ;;; Interface functions
98 (nnoo-define-basics nnfolder)
100 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
102 (set-buffer nntp-server-buffer)
104 (let (article start stop)
105 (nnfolder-possibly-change-group group server)
106 (when nnfolder-current-buffer
107 (set-buffer nnfolder-current-buffer)
108 (goto-char (point-min))
109 (if (stringp (car articles))
111 (while (setq article (pop articles))
112 (set-buffer nnfolder-current-buffer)
113 (when (nnfolder-goto-article article)
115 (search-forward "\n\n" nil t)
116 (setq stop (1- (point)))
117 (set-buffer nntp-server-buffer)
118 (insert (format "221 %d Article retrieved.\n" article))
119 (insert-buffer-substring nnfolder-current-buffer start stop)
120 (goto-char (point-max))
123 (set-buffer nntp-server-buffer)
124 (nnheader-fold-continuation-lines)
127 (deffoo nnfolder-open-server (server &optional defs)
128 (nnoo-change-server 'nnfolder server defs)
129 (nnmail-activate 'nnfolder t)
130 (gnus-make-directory nnfolder-directory)
132 ((not (file-exists-p nnfolder-directory))
133 (nnfolder-close-server)
134 (nnheader-report 'nnfolder "Couldn't create directory: %s"
136 ((not (file-directory-p (file-truename nnfolder-directory)))
137 (nnfolder-close-server)
138 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
140 (nnmail-activate 'nnfolder)
141 (nnheader-report 'nnfolder "Opened server %s using directory %s"
142 server nnfolder-directory)
145 (deffoo nnfolder-request-close ()
146 (let ((alist nnfolder-buffer-alist))
148 (nnfolder-close-group (caar alist) nil t)
149 (setq alist (cdr alist))))
150 (nnoo-close-server 'nnfolder)
151 (setq nnfolder-buffer-alist nil
152 nnfolder-group-alist nil))
154 (deffoo nnfolder-request-article (article &optional group server buffer)
155 (nnfolder-possibly-change-group group server)
157 (set-buffer nnfolder-current-buffer)
158 (goto-char (point-min))
159 (when (nnfolder-goto-article article)
163 (unless (and (nnmail-search-unix-mail-delim)
165 (goto-char (point-max)))
167 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
168 (set-buffer nntp-server-buffer)
170 (insert-buffer-substring nnfolder-current-buffer start stop)
171 (goto-char (point-min))
172 (while (looking-at "From ")
174 (insert "X-From-Line: ")
176 (if (numberp article)
177 (cons nnfolder-current-group article)
178 (goto-char (point-min))
179 (search-forward (concat "\n" nnfolder-article-marker))
180 (cons nnfolder-current-group
183 (point) (progn (end-of-line) (point)))))))))))
185 (deffoo nnfolder-request-group (group &optional server dont-check)
186 (nnfolder-possibly-change-group group server t)
188 (if (not (assoc group nnfolder-group-alist))
189 (nnheader-report 'nnfolder "No such group: %s" group)
192 (nnheader-report 'nnfolder "Selected group %s" group)
194 (let* ((active (assoc group nnfolder-group-alist))
196 (range (cadr active)))
199 (nnheader-report 'nnfolder "No such group: %s" group))
200 ((null nnfolder-current-group)
201 (nnheader-report 'nnfolder "Empty group: %s" group))
203 (nnheader-report 'nnfolder "Selected group %s" group)
204 (nnheader-insert "211 %d %d %d %s\n"
205 (1+ (- (cdr range) (car range)))
206 (car range) (cdr range) group))))))))
208 (deffoo nnfolder-request-scan (&optional group server)
209 (nnfolder-possibly-change-group nil server)
210 (when nnfolder-get-new-mail
211 (nnfolder-possibly-change-group group server)
215 (let ((bufs nnfolder-buffer-alist))
218 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
219 (setq nnfolder-buffer-alist
220 (delq (car bufs) nnfolder-buffer-alist))
221 (set-buffer (nth 1 (car bufs)))
222 (nnfolder-save-buffer)
223 (kill-buffer (current-buffer)))
224 (setq bufs (cdr bufs))))))
228 ;; Don't close the buffer if we're not shutting down the server. This way,
229 ;; we can keep the buffer in the group buffer cache, and not have to grovel
230 ;; over the buffer again unless we add new mail to it or modify it in some
233 (deffoo nnfolder-close-group (group &optional server force)
234 ;; Make sure we _had_ the group open.
235 (when (or (assoc group nnfolder-buffer-alist)
236 (equal group nnfolder-current-group))
237 (let ((inf (assoc group nnfolder-buffer-alist)))
239 (when (and nnfolder-current-group
240 nnfolder-current-buffer)
241 (push (list nnfolder-current-group nnfolder-current-buffer)
242 nnfolder-buffer-alist))
243 (setq nnfolder-buffer-alist
244 (delq inf nnfolder-buffer-alist))
245 (setq nnfolder-current-buffer (cadr inf)
246 nnfolder-current-group (car inf))))
247 (when (and nnfolder-current-buffer
248 (buffer-name nnfolder-current-buffer))
250 (set-buffer nnfolder-current-buffer)
251 ;; If the buffer was modified, write the file out now.
252 (nnfolder-save-buffer)
253 ;; If we're shutting the server down, we need to kill the
254 ;; buffer and remove it from the open buffer list. Or, of
255 ;; course, if we're trying to minimize our space impact.
256 (kill-buffer (current-buffer))
257 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
258 nnfolder-buffer-alist)))))
259 (setq nnfolder-current-group nil
260 nnfolder-current-buffer nil)
263 (deffoo nnfolder-request-create-group (group &optional server args)
264 (nnfolder-possibly-change-group nil server)
265 (nnmail-activate 'nnfolder)
267 (unless (assoc group nnfolder-group-alist)
268 (push (list group (cons 1 0)) nnfolder-group-alist)
269 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
270 (nnfolder-read-folder group)))
273 (deffoo nnfolder-request-list (&optional server)
274 (nnfolder-possibly-change-group nil server)
276 (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
277 (pathname-coding-system 'binary))
278 (nnmail-find-file nnfolder-active-file)
279 (setq nnfolder-group-alist (nnmail-get-active)))
282 (deffoo nnfolder-request-newgroups (date &optional server)
283 (nnfolder-possibly-change-group nil server)
284 (nnfolder-request-list server))
286 (deffoo nnfolder-request-list-newsgroups (&optional server)
287 (nnfolder-possibly-change-group nil server)
289 (nnmail-find-file nnfolder-newsgroups-file)))
291 (deffoo nnfolder-request-expire-articles
292 (articles newsgroup &optional server force)
293 (nnfolder-possibly-change-group newsgroup server)
296 (nnmail-activate 'nnfolder)
299 (set-buffer nnfolder-current-buffer)
300 (while (and articles is-old)
301 (goto-char (point-min))
302 (when (nnfolder-goto-article (car articles))
304 (nnmail-expired-article-p
307 (point) (progn (end-of-line) (point)))
308 force nnfolder-inhibit-expiry))
310 (nnheader-message 5 "Deleting article %d..."
311 (car articles) newsgroup)
312 (nnfolder-delete-mail))
313 (push (car articles) rest)))
314 (setq articles (cdr articles)))
315 (unless nnfolder-inhibit-expiry
316 (nnheader-message 5 "Deleting articles...done"))
317 (nnfolder-save-buffer)
318 (nnfolder-adjust-min-active newsgroup)
319 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
320 (nconc rest articles))))
322 (deffoo nnfolder-request-move-article (article group server
323 accept-form &optional last)
325 (let ((buf (get-buffer-create " *nnfolder move*"))
328 (nnfolder-request-article article group server)
332 (insert-buffer-substring nntp-server-buffer)
333 (goto-char (point-min))
334 (while (re-search-forward
335 (concat "^" nnfolder-article-marker)
336 (save-excursion (search-forward "\n\n" nil t) (point)) t)
337 (delete-region (progn (beginning-of-line) (point))
338 (progn (forward-line 1) (point))))
339 (setq result (eval accept-form))
343 (nnfolder-possibly-change-group group server)
344 (set-buffer nnfolder-current-buffer)
345 (goto-char (point-min))
346 (when (nnfolder-goto-article article)
347 (nnfolder-delete-mail))
349 (nnfolder-save-buffer)
350 (nnfolder-adjust-min-active group)
351 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))))
354 (deffoo nnfolder-request-accept-article (group &optional server last)
356 (nnfolder-possibly-change-group group server)
357 (nnmail-check-syntax)
358 (let ((buf (current-buffer))
360 (goto-char (point-min))
361 (when (looking-at "X-From-Line: ")
362 (replace-match "From "))
364 (nnfolder-request-list)
367 (goto-char (point-min))
368 (search-forward "\n\n" nil t)
370 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
371 (delete-region (point) (progn (forward-line 1) (point))))
372 (when nnmail-cache-accepted-message-ids
373 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
374 (setq result (if (stringp group)
375 (list (cons group (nnfolder-active-number group)))
377 (nnmail-article-group 'nnfolder-active-number))))
378 (if (and (null result)
379 (yes-or-no-p "Moved to `junk' group; delete article? "))
382 (car (nnfolder-save-mail result)))))
385 (nnfolder-possibly-change-folder (or (caar art-group) group))
386 (nnfolder-save-buffer)
387 (when nnmail-cache-accepted-message-ids
388 (nnmail-cache-close)))))
389 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
391 (nnheader-report 'nnfolder "Couldn't store article"))
394 (deffoo nnfolder-request-replace-article (article group buffer)
395 (nnfolder-possibly-change-group group)
398 (goto-char (point-min))
400 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
401 (setq xfrom (match-string 1))
403 (goto-char (point-min))
405 (insert "From " xfrom "\n")
406 (unless (looking-at message-unix-mail-delimiter)
407 (insert "From nobody " (current-time-string) "\n"))))
408 (nnfolder-normalize-buffer)
409 (set-buffer nnfolder-current-buffer)
410 (goto-char (point-min))
411 (if (not (nnfolder-goto-article article))
413 (nnfolder-delete-mail)
414 (insert-buffer-substring buffer)
415 (nnfolder-save-buffer)
418 (deffoo nnfolder-request-delete-group (group &optional force server)
419 (nnfolder-close-group group server t)
420 ;; Delete all articles in GROUP.
422 () ; Don't delete the articles.
423 ;; Delete the file that holds the group.
425 (delete-file (nnfolder-group-pathname group))))
426 ;; Remove the group from all structures.
427 (setq nnfolder-group-alist
428 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
429 nnfolder-current-group nil
430 nnfolder-current-buffer nil)
431 ;; Save the active file.
432 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
435 (deffoo nnfolder-request-rename-group (group new-name &optional server)
436 (nnfolder-possibly-change-group group server)
438 (set-buffer nnfolder-current-buffer)
439 (and (file-writable-p buffer-file-name)
443 (nnfolder-group-pathname new-name))
445 ;; That went ok, so we change the internal structures.
446 (let ((entry (assoc group nnfolder-group-alist)))
447 (and entry (setcar entry new-name))
448 (setq nnfolder-current-buffer nil
449 nnfolder-current-group nil)
450 ;; Save the new group alist.
451 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
452 ;; We kill the buffer instead of renaming it and stuff.
453 (kill-buffer (current-buffer))
456 (defun nnfolder-request-regenerate (server)
457 (nnfolder-possibly-change-group nil server)
458 (nnfolder-generate-active-file)
462 ;;; Internal functions.
464 (defun nnfolder-adjust-min-active (group)
465 ;; Find the lowest active article in this group.
466 (let* ((active (cadr (assoc group nnfolder-group-alist)))
467 (marker (concat "\n" nnfolder-article-marker))
469 (activemin (cdr active)))
471 (set-buffer nnfolder-current-buffer)
472 (goto-char (point-min))
473 (while (and (search-forward marker nil t)
474 (re-search-forward number nil t))
475 (let ((newnum (string-to-number (match-string 0))))
476 (if (nnmail-within-headers-p)
477 (setq activemin (min activemin newnum)))))
478 (setcar active activemin))))
480 (defun nnfolder-article-string (article)
481 (if (numberp article)
482 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
483 (concat "\nMessage-ID: " article)))
485 (defun nnfolder-goto-article (article)
486 "Place point at the start of the headers of ARTICLE.
487 ARTICLE can be an article number or a Message-ID.
488 Returns t if successful, nil otherwise."
489 (let ((art-string (nnfolder-article-string article))
491 ;; It is likely that we are at or before the delimiter line.
492 ;; We therefore go to the end of the previous line, and start
493 ;; searching from there.
498 ;; First search forward.
499 (while (and (setq found (search-forward art-string nil t))
500 (not (nnmail-within-headers-p))))
501 ;; If unsuccessful, search backward from where we started,
504 (while (and (setq found (search-backward art-string nil t))
505 (not (nnmail-within-headers-p)))))
507 (nnmail-search-unix-mail-delim-backward))))
509 (defun nnfolder-delete-mail (&optional leave-delim)
510 "Delete the message that point is in.
511 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
512 deleted. Point is left where the deleted region was."
516 (forward-line 1) ; in case point is at beginning of message already
517 (nnmail-search-unix-mail-delim-backward)
518 (if leave-delim (progn (forward-line 1) (point))
522 (if (nnmail-search-unix-mail-delim)
525 (run-hooks 'nnfolder-delete-mail-hook)
526 (delete-region (point-min) (point-max))))
528 (defun nnfolder-possibly-change-group (group &optional server dont-check)
531 (not (nnfolder-server-opened server)))
532 (nnfolder-open-server server))
533 (unless (gnus-buffer-live-p nnfolder-current-buffer)
534 (setq nnfolder-current-buffer nil
535 nnfolder-current-group nil))
538 (not (equal group nnfolder-current-group)))
539 (let ((pathname-coding-system 'binary))
540 (nnmail-activate 'nnfolder)
541 (when (and (not (assoc group nnfolder-group-alist))
543 (nnfolder-group-pathname group))))
544 ;; The group doesn't exist, so we create a new entry for it.
545 (push (list group (cons 1 0)) nnfolder-group-alist)
546 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
549 (setq nnfolder-current-group group
550 nnfolder-current-buffer nil)
552 ;; If we have to change groups, see if we don't already have the
553 ;; folder in memory. If we do, verify the modtime and destroy
554 ;; the folder if needed so we can rescan it.
555 (setq nnfolder-current-buffer
556 (nth 1 (assoc group nnfolder-buffer-alist)))
558 ;; If the buffer is not live, make sure it isn't in the alist. If it
559 ;; is live, verify that nobody else has touched the file since last
561 (when (and nnfolder-current-buffer
562 (not (gnus-buffer-live-p nnfolder-current-buffer)))
563 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
564 nnfolder-current-buffer nil))
566 (setq nnfolder-current-group group)
568 (when (or (not nnfolder-current-buffer)
569 (not (verify-visited-file-modtime
570 nnfolder-current-buffer)))
572 (setq file (nnfolder-group-pathname group))
573 ;; See whether we need to create the new file.
574 (unless (file-exists-p file)
575 (gnus-make-directory (file-name-directory file))
576 (nnmail-write-region 1 1 file t 'nomesg))
577 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
578 (set-buffer nnfolder-current-buffer)
579 (push (list group nnfolder-current-buffer)
580 nnfolder-buffer-alist)))))))))
582 (defun nnfolder-save-mail (group-art-list)
583 "Called narrowed to an article."
584 (let* (save-list group-art)
585 (goto-char (point-min))
586 ;; The From line may have been quoted by movemail.
587 (when (looking-at (concat ">" message-unix-mail-delimiter))
589 ;; This might come from somewhere else.
590 (unless (looking-at message-unix-mail-delimiter)
591 (insert "From nobody " (current-time-string) "\n")
592 (goto-char (point-min)))
593 ;; Quote all "From " lines in the article.
595 (let (case-fold-search)
596 (while (re-search-forward "^From " nil t)
599 (setq save-list group-art-list)
600 (nnmail-insert-lines)
601 (nnmail-insert-xref group-art-list)
602 (run-hooks 'nnmail-prepare-save-mail-hook)
603 (run-hooks 'nnfolder-prepare-save-mail-hook)
605 ;; Insert the mail into each of the destination groups.
606 (while (setq group-art (pop group-art-list))
607 ;; Kill any previous newsgroup markers.
608 (goto-char (point-min))
609 (search-forward "\n\n" nil t)
611 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
612 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
614 ;; Insert the new newsgroup marker.
615 (nnfolder-insert-newsgroup-line group-art)
618 (let ((beg (point-min))
620 (obuf (current-buffer)))
621 (nnfolder-possibly-change-folder (car group-art))
622 (let ((buffer-read-only nil))
623 (nnfolder-normalize-buffer)
624 (insert-buffer-substring obuf beg end)))))
626 ;; Did we save it anywhere?
629 (defun nnfolder-normalize-buffer ()
630 "Make sure there are two newlines at the end of the buffer."
631 (goto-char (point-max))
632 (skip-chars-backward "\n")
633 (delete-region (point) (point-max))
636 (defun nnfolder-insert-newsgroup-line (group-art)
638 (goto-char (point-min))
639 (when (search-forward "\n\n" nil t)
641 (insert (format (concat nnfolder-article-marker "%d %s\n")
642 (cdr group-art) (current-time-string))))))
644 (defun nnfolder-active-number (group)
645 ;; Find the next article number in GROUP.
646 (let ((active (cadr (assoc group nnfolder-group-alist))))
648 (setcdr active (1+ (cdr active)))
649 ;; This group is new, so we create a new entry for it.
650 ;; This might be a bit naughty... creating groups on the drop of
651 ;; a hat, but I don't know...
652 (push (list group (setq active (cons 1 1)))
653 nnfolder-group-alist))
656 (defun nnfolder-possibly-change-folder (group)
657 (let ((inf (assoc group nnfolder-buffer-alist)))
659 (gnus-buffer-live-p (cadr inf)))
660 (set-buffer (cadr inf))
662 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
663 (when nnfolder-group-alist
664 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
665 (push (list group (nnfolder-read-folder group))
666 nnfolder-buffer-alist))))
668 ;; This method has a problem if you've accidentally let the active list get
669 ;; out of sync with the files. This could happen, say, if you've
670 ;; accidentally gotten new mail with something other than Gnus (but why
671 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
672 ;; processing the file, ready to add new X-Gnus article number markers, and
673 ;; we'll run across a message with no ID yet - the active list _may_not_ be
676 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
677 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
678 ;; rest of the message looking for any more, possibly higher IDs. We'll
679 ;; assume the maximum that we find is the highest active. Note that this
680 ;; shouldn't cost us much extra time at all, but will be a lot less
681 ;; vulnerable to glitches between the mbox and the active file.
683 (defun nnfolder-read-folder (group)
684 (let* ((file (nnfolder-group-pathname group))
685 (buffer (set-buffer (nnheader-find-file-noselect file))))
686 (if (equal (cadr (assoc group nnfolder-scantime-alist))
687 (nth 5 (file-attributes file)))
688 ;; This looks up-to-date, so we don't do any scanning.
689 (if (file-exists-p file)
691 (push (list group buffer) nnfolder-buffer-alist)
692 (set-buffer-modified-p t)
694 ;; Parse the damn thing.
696 (nnmail-activate 'nnfolder)
698 (let ((delim (concat "^" message-unix-mail-delimiter))
699 (marker (concat "\n" nnfolder-article-marker))
701 (active (or (cadr (assoc group nnfolder-group-alist))
703 (scantime (assoc group nnfolder-scantime-alist))
705 maxid start end newscantime
707 (buffer-disable-undo)
708 (setq maxid (cdr active))
709 (goto-char (point-min))
711 ;; Anytime the active number is 1 or 0, it is suspect. In that
712 ;; case, search the file manually to find the active number. Or,
713 ;; of course, if we're being paranoid. (This would also be the
714 ;; place to build other lists from the header markers, such as
715 ;; expunge lists, etc., if we ever desired to abandon the active
716 ;; file entirely for mboxes.)
717 (when (or nnfolder-ignore-active-file
719 (while (and (search-forward marker nil t)
720 (re-search-forward number nil t))
721 (let ((newnum (string-to-number (match-string 0))))
722 (if (nnmail-within-headers-p)
723 (setq maxid (max maxid newnum)
724 minid (min minid newnum)))))
725 (setcar active (max 1 (min minid maxid)))
726 (setcdr active (max maxid (cdr active)))
727 (goto-char (point-min)))
729 ;; As long as we trust that the user will only insert unmarked mail
730 ;; at the end, go to the end and search backwards for the last
731 ;; marker. Find the start of that message, and begin to search for
732 ;; unmarked messages from there.
733 (when (not (or nnfolder-distrust-mbox
735 (goto-char (point-max))
736 (unless (re-search-backward marker nil t)
737 (goto-char (point-min)))
738 (when (nnmail-search-unix-mail-delim)
739 (goto-char (point-min))))
741 ;; Keep track of the active number on our own, and insert it back
742 ;; into the active list when we're done. Also, prime the pump to
743 ;; cut down on the number of searches we do.
744 (unless (nnmail-search-unix-mail-delim)
745 (goto-char (point-max)))
746 (setq end (point-marker))
747 (while (not (= end (point-max)))
748 (setq start (marker-position end))
750 ;; There may be more than one "From " line, so we skip past
752 (while (looking-at delim)
754 (set-marker end (if (nnmail-search-unix-mail-delim)
758 (when (not (search-forward marker end t))
759 (narrow-to-region start end)
760 (nnmail-insert-lines)
761 (nnfolder-insert-newsgroup-line
762 (cons nil (nnfolder-active-number nnfolder-current-group)))
766 ;; Make absolutely sure that the active list reflects reality!
767 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
768 ;; Set the scantime for this group.
769 (setq newscantime (visited-file-modtime))
771 (setcdr scantime (list newscantime))
772 (push (list nnfolder-current-group newscantime)
773 nnfolder-scantime-alist))
774 (current-buffer))))))
777 (defun nnfolder-generate-active-file ()
778 "Look for mbox folders in the nnfolder directory and make them into groups."
780 (nnmail-activate 'nnfolder)
781 (let ((files (directory-files nnfolder-directory))
783 (while (setq file (pop files))
784 (when (and (not (backup-file-name-p file))
785 (message-mail-file-mbox-p
786 (nnheader-concat nnfolder-directory file)))
787 (let ((oldgroup (assoc file nnfolder-group-alist)))
789 (nnheader-message 5 "Refreshing group %s..." file)
790 (nnheader-message 5 "Adding group %s..." file))
792 (setq nnfolder-group-alist
793 (delq oldgroup (copy-sequence nnfolder-group-alist))))
794 (push (list file (cons 1 0)) nnfolder-group-alist)
795 (nnfolder-possibly-change-folder file)
796 (nnfolder-possibly-change-group file)
797 (nnfolder-close-group file))))
798 (nnheader-message 5 "")))
800 (defun nnfolder-group-pathname (group)
801 "Make pathname for GROUP."
803 (mm-encode-coding-string group nnmail-pathname-coding-system))
804 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
805 ;; If this file exists, we use it directly.
806 (if (or nnmail-use-long-file-names
807 (file-exists-p (concat dir group)))
809 ;; If not, we translate dots into slashes.
810 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
812 (defun nnfolder-save-buffer ()
814 (when (buffer-modified-p)
815 (run-hooks 'nnfolder-save-buffer-hook)
816 (gnus-make-directory (file-name-directory (buffer-file-name)))
821 ;;; nnfolder.el ends here