1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 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)
93 (defvoo nnfolder-active-file-coding-system mm-text-coding-system)
94 (defvoo nnfolder-active-file-coding-system-for-write
95 nnmail-active-file-coding-system)
96 (defvoo nnfolder-file-coding-system mm-text-coding-system)
97 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
98 "Coding system for save nnfolder file.
99 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
103 ;;; Interface functions
105 (nnoo-define-basics nnfolder)
107 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
109 (set-buffer nntp-server-buffer)
111 (let (article start stop)
112 (nnfolder-possibly-change-group group server)
113 (when nnfolder-current-buffer
114 (set-buffer nnfolder-current-buffer)
115 (goto-char (point-min))
116 (if (stringp (car articles))
118 (while (setq article (pop articles))
119 (set-buffer nnfolder-current-buffer)
120 (when (nnfolder-goto-article article)
122 (search-forward "\n\n" nil t)
123 (setq stop (1- (point)))
124 (set-buffer nntp-server-buffer)
125 (insert (format "221 %d Article retrieved.\n" article))
126 (insert-buffer-substring nnfolder-current-buffer start stop)
127 (goto-char (point-max))
130 (set-buffer nntp-server-buffer)
131 (nnheader-fold-continuation-lines)
134 (deffoo nnfolder-open-server (server &optional defs)
135 (nnoo-change-server 'nnfolder server defs)
136 (nnmail-activate 'nnfolder t)
137 (gnus-make-directory nnfolder-directory)
139 ((not (file-exists-p nnfolder-directory))
140 (nnfolder-close-server)
141 (nnheader-report 'nnfolder "Couldn't create directory: %s"
143 ((not (file-directory-p (file-truename nnfolder-directory)))
144 (nnfolder-close-server)
145 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
147 (nnmail-activate 'nnfolder)
148 (nnheader-report 'nnfolder "Opened server %s using directory %s"
149 server nnfolder-directory)
152 (deffoo nnfolder-request-close ()
153 (let ((alist nnfolder-buffer-alist))
155 (nnfolder-close-group (caar alist) nil t)
156 (setq alist (cdr alist))))
157 (nnoo-close-server 'nnfolder)
158 (setq nnfolder-buffer-alist nil
159 nnfolder-group-alist nil))
161 (deffoo nnfolder-request-article (article &optional group server buffer)
162 (nnfolder-possibly-change-group group server)
164 (set-buffer nnfolder-current-buffer)
165 (goto-char (point-min))
166 (when (nnfolder-goto-article article)
170 (unless (and (nnmail-search-unix-mail-delim)
172 (goto-char (point-max)))
174 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
175 (set-buffer nntp-server-buffer)
177 (insert-buffer-substring nnfolder-current-buffer start stop)
178 (goto-char (point-min))
179 (while (looking-at "From ")
181 (insert "X-From-Line: ")
183 (if (numberp article)
184 (cons nnfolder-current-group article)
185 (goto-char (point-min))
186 (search-forward (concat "\n" nnfolder-article-marker))
187 (cons nnfolder-current-group
190 (point) (progn (end-of-line) (point)))))))))))
192 (deffoo nnfolder-request-group (group &optional server dont-check)
193 (nnfolder-possibly-change-group group server t)
195 (if (not (assoc group nnfolder-group-alist))
196 (nnheader-report 'nnfolder "No such group: %s" group)
199 (nnheader-report 'nnfolder "Selected group %s" group)
201 (let* ((active (assoc group nnfolder-group-alist))
203 (range (cadr active)))
206 (nnheader-report 'nnfolder "No such group: %s" group))
207 ((null nnfolder-current-group)
208 (nnheader-report 'nnfolder "Empty group: %s" group))
210 (nnheader-report 'nnfolder "Selected group %s" group)
211 (nnheader-insert "211 %d %d %d %s\n"
212 (1+ (- (cdr range) (car range)))
213 (car range) (cdr range) group))))))))
215 (deffoo nnfolder-request-scan (&optional group server)
216 (nnfolder-possibly-change-group nil server)
217 (when nnfolder-get-new-mail
218 (nnfolder-possibly-change-group group server)
222 (let ((bufs nnfolder-buffer-alist))
225 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
226 (setq nnfolder-buffer-alist
227 (delq (car bufs) nnfolder-buffer-alist))
228 (set-buffer (nth 1 (car bufs)))
229 (nnfolder-save-buffer)
230 (kill-buffer (current-buffer)))
231 (setq bufs (cdr bufs))))))
235 ;; Don't close the buffer if we're not shutting down the server. This way,
236 ;; we can keep the buffer in the group buffer cache, and not have to grovel
237 ;; over the buffer again unless we add new mail to it or modify it in some
240 (deffoo nnfolder-close-group (group &optional server force)
241 ;; Make sure we _had_ the group open.
242 (when (or (assoc group nnfolder-buffer-alist)
243 (equal group nnfolder-current-group))
244 (let ((inf (assoc group nnfolder-buffer-alist)))
246 (when (and nnfolder-current-group
247 nnfolder-current-buffer)
248 (push (list nnfolder-current-group nnfolder-current-buffer)
249 nnfolder-buffer-alist))
250 (setq nnfolder-buffer-alist
251 (delq inf nnfolder-buffer-alist))
252 (setq nnfolder-current-buffer (cadr inf)
253 nnfolder-current-group (car inf))))
254 (when (and nnfolder-current-buffer
255 (buffer-name nnfolder-current-buffer))
257 (set-buffer nnfolder-current-buffer)
258 ;; If the buffer was modified, write the file out now.
259 (nnfolder-save-buffer)
260 ;; If we're shutting the server down, we need to kill the
261 ;; buffer and remove it from the open buffer list. Or, of
262 ;; course, if we're trying to minimize our space impact.
263 (kill-buffer (current-buffer))
264 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
265 nnfolder-buffer-alist)))))
266 (setq nnfolder-current-group nil
267 nnfolder-current-buffer nil)
270 (deffoo nnfolder-request-create-group (group &optional server args)
271 (nnfolder-possibly-change-group nil server)
272 (nnmail-activate 'nnfolder)
274 (unless (assoc group nnfolder-group-alist)
275 (push (list group (cons 1 0)) nnfolder-group-alist)
276 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
277 (nnfolder-read-folder group)))
280 (deffoo nnfolder-request-list (&optional server)
281 (nnfolder-possibly-change-group nil server)
283 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
284 (nnmail-find-file nnfolder-active-file)
285 (setq nnfolder-group-alist (nnmail-get-active)))
288 (deffoo nnfolder-request-newgroups (date &optional server)
289 (nnfolder-possibly-change-group nil server)
290 (nnfolder-request-list server))
292 (deffoo nnfolder-request-list-newsgroups (&optional server)
293 (nnfolder-possibly-change-group nil server)
295 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
296 (nnmail-find-file nnfolder-newsgroups-file))))
298 (deffoo nnfolder-request-expire-articles
299 (articles newsgroup &optional server force)
300 (nnfolder-possibly-change-group newsgroup server)
303 (nnmail-activate 'nnfolder)
306 (set-buffer nnfolder-current-buffer)
307 (while (and articles is-old)
308 (goto-char (point-min))
309 (when (and (nnfolder-goto-article (car articles))
310 (search-forward (concat "\n" nnfolder-article-marker)
314 (nnmail-expired-article-p
317 (point) (progn (end-of-line) (point)))
318 force nnfolder-inhibit-expiry))
320 (nnheader-message 5 "Deleting article %d..."
321 (car articles) newsgroup)
322 (nnfolder-delete-mail))
323 (push (car articles) rest)))
324 (setq articles (cdr articles)))
325 (unless nnfolder-inhibit-expiry
326 (nnheader-message 5 "Deleting articles...done"))
327 (nnfolder-save-buffer)
328 (nnfolder-adjust-min-active newsgroup)
329 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
330 (nconc rest articles))))
332 (deffoo nnfolder-request-move-article (article group server
333 accept-form &optional last)
335 (let ((buf (get-buffer-create " *nnfolder move*"))
338 (nnfolder-request-article article group server)
342 (insert-buffer-substring nntp-server-buffer)
343 (goto-char (point-min))
344 (while (re-search-forward
345 (concat "^" nnfolder-article-marker)
346 (save-excursion (search-forward "\n\n" nil t) (point)) t)
347 (delete-region (progn (beginning-of-line) (point))
348 (progn (forward-line 1) (point))))
349 (setq result (eval accept-form))
353 (nnfolder-possibly-change-group group server)
354 (set-buffer nnfolder-current-buffer)
355 (goto-char (point-min))
356 (when (nnfolder-goto-article article)
357 (nnfolder-delete-mail))
359 (nnfolder-save-buffer)
360 (nnfolder-adjust-min-active group)
361 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
364 (deffoo nnfolder-request-accept-article (group &optional server last)
366 (nnfolder-possibly-change-group group server)
367 (nnmail-check-syntax)
368 (let ((buf (current-buffer))
370 (goto-char (point-min))
371 (when (looking-at "X-From-Line: ")
372 (replace-match "From "))
374 (nnfolder-request-list)
377 (goto-char (point-min))
378 (search-forward "\n\n" nil t)
380 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
381 (delete-region (point) (progn (forward-line 1) (point))))
382 (when nnmail-cache-accepted-message-ids
383 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
384 (setq result (if (stringp group)
385 (list (cons group (nnfolder-active-number group)))
387 (nnmail-article-group 'nnfolder-active-number))))
388 (if (and (null result)
389 (yes-or-no-p "Moved to `junk' group; delete article? "))
392 (car (nnfolder-save-mail result)))))
395 (nnfolder-possibly-change-folder (or (caar art-group) group))
396 (nnfolder-save-buffer)
397 (when nnmail-cache-accepted-message-ids
398 (nnmail-cache-close)))))
399 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
401 (nnheader-report 'nnfolder "Couldn't store article"))
404 (deffoo nnfolder-request-replace-article (article group buffer)
405 (nnfolder-possibly-change-group group)
408 (goto-char (point-min))
410 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
411 (setq xfrom (match-string 1))
413 (goto-char (point-min))
415 (insert "From " xfrom "\n")
416 (unless (looking-at "From ")
417 (insert "From nobody " (current-time-string) "\n"))))
418 (nnfolder-normalize-buffer)
419 (set-buffer nnfolder-current-buffer)
420 (goto-char (point-min))
421 (if (not (nnfolder-goto-article article))
423 (nnfolder-delete-mail)
424 (insert-buffer-substring buffer)
425 (nnfolder-save-buffer)
428 (deffoo nnfolder-request-delete-group (group &optional force server)
429 (nnfolder-close-group group server t)
430 ;; Delete all articles in GROUP.
432 () ; Don't delete the articles.
433 ;; Delete the file that holds the group.
435 (delete-file (nnfolder-group-pathname group))))
436 ;; Remove the group from all structures.
437 (setq nnfolder-group-alist
438 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
439 nnfolder-current-group nil
440 nnfolder-current-buffer nil)
441 ;; Save the active file.
442 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
445 (deffoo nnfolder-request-rename-group (group new-name &optional server)
446 (nnfolder-possibly-change-group group server)
448 (set-buffer nnfolder-current-buffer)
449 (and (file-writable-p buffer-file-name)
453 (nnfolder-group-pathname new-name))
455 ;; That went ok, so we change the internal structures.
456 (let ((entry (assoc group nnfolder-group-alist)))
457 (and entry (setcar entry new-name))
458 (setq nnfolder-current-buffer nil
459 nnfolder-current-group nil)
460 ;; Save the new group alist.
461 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
462 ;; We kill the buffer instead of renaming it and stuff.
463 (kill-buffer (current-buffer))
466 (defun nnfolder-request-regenerate (server)
467 (nnfolder-possibly-change-group nil server)
468 (nnfolder-generate-active-file)
472 ;;; Internal functions.
474 (defun nnfolder-adjust-min-active (group)
475 ;; Find the lowest active article in this group.
476 (let* ((active (cadr (assoc group nnfolder-group-alist)))
477 (marker (concat "\n" nnfolder-article-marker))
479 (activemin (cdr active)))
481 (set-buffer nnfolder-current-buffer)
482 (goto-char (point-min))
483 (while (and (search-forward marker nil t)
484 (re-search-forward number nil t))
485 (let ((newnum (string-to-number (match-string 0))))
486 (if (nnmail-within-headers-p)
487 (setq activemin (min activemin newnum)))))
488 (setcar active activemin))))
490 (defun nnfolder-article-string (article)
491 (if (numberp article)
492 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
493 (concat "\nMessage-ID: " article)))
495 (defun nnfolder-goto-article (article)
496 "Place point at the start of the headers of ARTICLE.
497 ARTICLE can be an article number or a Message-ID.
498 Returns t if successful, nil otherwise."
499 (let ((art-string (nnfolder-article-string article))
501 ;; It is likely that we are at or before the delimiter line.
502 ;; We therefore go to the end of the previous line, and start
503 ;; searching from there.
508 ;; First search forward.
509 (while (and (setq found (search-forward art-string nil t))
510 (not (nnmail-within-headers-p))))
511 ;; If unsuccessful, search backward from where we started,
514 (while (and (setq found (search-backward art-string nil t))
515 (not (nnmail-within-headers-p)))))
517 (nnmail-search-unix-mail-delim-backward))))
519 (defun nnfolder-delete-mail (&optional leave-delim)
520 "Delete the message that point is in.
521 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
522 deleted. Point is left where the deleted region was."
526 ;; In case point is at the beginning of the message already.
528 (nnmail-search-unix-mail-delim-backward)
529 (if leave-delim (progn (forward-line 1) (point))
533 (if (nnmail-search-unix-mail-delim)
536 (run-hooks 'nnfolder-delete-mail-hook)
537 (delete-region (point-min) (point-max))))
539 (defun nnfolder-possibly-change-group (group &optional server dont-check)
542 (not (nnfolder-server-opened server)))
543 (nnfolder-open-server server))
544 (unless (gnus-buffer-live-p nnfolder-current-buffer)
545 (setq nnfolder-current-buffer nil
546 nnfolder-current-group nil))
549 (not (equal group nnfolder-current-group)))
550 (let ((pathname-coding-system nnmail-pathname-coding-system))
551 (nnmail-activate 'nnfolder)
552 (when (and (not (assoc group nnfolder-group-alist))
554 (nnfolder-group-pathname group))))
555 ;; The group doesn't exist, so we create a new entry for it.
556 (push (list group (cons 1 0)) nnfolder-group-alist)
557 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
560 (setq nnfolder-current-group group
561 nnfolder-current-buffer nil)
563 ;; If we have to change groups, see if we don't already have the
564 ;; folder in memory. If we do, verify the modtime and destroy
565 ;; the folder if needed so we can rescan it.
566 (setq nnfolder-current-buffer
567 (nth 1 (assoc group nnfolder-buffer-alist)))
569 ;; If the buffer is not live, make sure it isn't in the alist. If it
570 ;; is live, verify that nobody else has touched the file since last
572 (when (and nnfolder-current-buffer
573 (not (gnus-buffer-live-p nnfolder-current-buffer)))
574 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
575 nnfolder-current-buffer nil))
577 (setq nnfolder-current-group group)
579 (when (or (not nnfolder-current-buffer)
580 (not (verify-visited-file-modtime
581 nnfolder-current-buffer)))
583 (setq file (nnfolder-group-pathname group))
584 ;; See whether we need to create the new file.
585 (unless (file-exists-p file)
586 (gnus-make-directory (file-name-directory file))
587 (let ((nnmail-file-coding-system
588 (or nnfolder-file-coding-system-for-write
589 nnfolder-file-coding-system-for-write)))
590 (nnmail-write-region 1 1 file t 'nomesg)))
591 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
592 (set-buffer nnfolder-current-buffer)
593 (push (list group nnfolder-current-buffer)
594 nnfolder-buffer-alist)))))))))
596 (defun nnfolder-save-mail (group-art-list)
597 "Called narrowed to an article."
598 (let* (save-list group-art)
599 (goto-char (point-min))
600 ;; The From line may have been quoted by movemail.
601 (when (looking-at ">From")
603 ;; This might come from somewhere else.
604 (unless (looking-at "From ")
605 (insert "From nobody " (current-time-string) "\n")
606 (goto-char (point-min)))
607 ;; Quote all "From " lines in the article.
609 (let (case-fold-search)
610 (while (re-search-forward "^From " nil t)
613 (setq save-list group-art-list)
614 (nnmail-insert-lines)
615 (nnmail-insert-xref group-art-list)
616 (run-hooks 'nnmail-prepare-save-mail-hook)
617 (run-hooks 'nnfolder-prepare-save-mail-hook)
619 ;; Insert the mail into each of the destination groups.
620 (while (setq group-art (pop group-art-list))
621 ;; Kill any previous newsgroup markers.
622 (goto-char (point-min))
623 (search-forward "\n\n" nil t)
625 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
626 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
628 ;; Insert the new newsgroup marker.
629 (nnfolder-insert-newsgroup-line group-art)
632 (let ((beg (point-min))
634 (obuf (current-buffer)))
635 (nnfolder-possibly-change-folder (car group-art))
636 (let ((buffer-read-only nil))
637 (nnfolder-normalize-buffer)
638 (insert-buffer-substring obuf beg end)))))
640 ;; Did we save it anywhere?
643 (defun nnfolder-normalize-buffer ()
644 "Make sure there are two newlines at the end of the buffer."
645 (goto-char (point-max))
646 (skip-chars-backward "\n")
647 (delete-region (point) (point-max))
650 (defun nnfolder-insert-newsgroup-line (group-art)
652 (goto-char (point-min))
653 (when (search-forward "\n\n" nil t)
655 (insert (format (concat nnfolder-article-marker "%d %s\n")
656 (cdr group-art) (current-time-string))))))
658 (defun nnfolder-active-number (group)
659 ;; Find the next article number in GROUP.
660 (let ((active (cadr (assoc group nnfolder-group-alist))))
662 (setcdr active (1+ (cdr active)))
663 ;; This group is new, so we create a new entry for it.
664 ;; This might be a bit naughty... creating groups on the drop of
665 ;; a hat, but I don't know...
666 (push (list group (setq active (cons 1 1)))
667 nnfolder-group-alist))
670 (defun nnfolder-possibly-change-folder (group)
671 (let ((inf (assoc group nnfolder-buffer-alist)))
673 (gnus-buffer-live-p (cadr inf)))
674 (set-buffer (cadr inf))
676 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
677 (when nnfolder-group-alist
678 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
679 (push (list group (nnfolder-read-folder group))
680 nnfolder-buffer-alist))))
682 ;; This method has a problem if you've accidentally let the active list get
683 ;; out of sync with the files. This could happen, say, if you've
684 ;; accidentally gotten new mail with something other than Gnus (but why
685 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
686 ;; processing the file, ready to add new X-Gnus article number markers, and
687 ;; we'll run across a message with no ID yet - the active list _may_not_ be
690 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
691 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
692 ;; rest of the message looking for any more, possibly higher IDs. We'll
693 ;; assume the maximum that we find is the highest active. Note that this
694 ;; shouldn't cost us much extra time at all, but will be a lot less
695 ;; vulnerable to glitches between the mbox and the active file.
697 (defun nnfolder-read-folder (group)
698 (let* ((file (nnfolder-group-pathname group))
700 (let ((nnheader-file-coding-system
701 nnfolder-file-coding-system))
702 (nnheader-find-file-noselect file)))))
703 (if (equal (cadr (assoc group nnfolder-scantime-alist))
704 (nth 5 (file-attributes file)))
705 ;; This looks up-to-date, so we don't do any scanning.
706 (if (file-exists-p file)
708 (push (list group buffer) nnfolder-buffer-alist)
709 (set-buffer-modified-p t)
711 ;; Parse the damn thing.
713 (nnmail-activate 'nnfolder)
715 (let ((delim "^From ")
716 (marker (concat "\n" nnfolder-article-marker))
718 (active (or (cadr (assoc group nnfolder-group-alist))
720 (scantime (assoc group nnfolder-scantime-alist))
722 maxid start end newscantime
724 (buffer-disable-undo)
725 (setq maxid (cdr active))
726 (goto-char (point-min))
728 ;; Anytime the active number is 1 or 0, it is suspect. In that
729 ;; case, search the file manually to find the active number. Or,
730 ;; of course, if we're being paranoid. (This would also be the
731 ;; place to build other lists from the header markers, such as
732 ;; expunge lists, etc., if we ever desired to abandon the active
733 ;; file entirely for mboxes.)
734 (when (or nnfolder-ignore-active-file
736 (while (and (search-forward marker nil t)
737 (re-search-forward number nil t))
738 (let ((newnum (string-to-number (match-string 0))))
739 (if (nnmail-within-headers-p)
740 (setq maxid (max maxid newnum)
741 minid (min minid newnum)))))
742 (setcar active (max 1 (min minid maxid)))
743 (setcdr active (max maxid (cdr active)))
744 (goto-char (point-min)))
746 ;; As long as we trust that the user will only insert unmarked mail
747 ;; at the end, go to the end and search backwards for the last
748 ;; marker. Find the start of that message, and begin to search for
749 ;; unmarked messages from there.
750 (when (not (or nnfolder-distrust-mbox
752 (goto-char (point-max))
753 (unless (re-search-backward marker nil t)
754 (goto-char (point-min)))
755 (when (nnmail-search-unix-mail-delim)
756 (goto-char (point-min))))
758 ;; Keep track of the active number on our own, and insert it back
759 ;; into the active list when we're done. Also, prime the pump to
760 ;; cut down on the number of searches we do.
761 (unless (nnmail-search-unix-mail-delim)
762 (goto-char (point-max)))
763 (setq end (point-marker))
764 (while (not (= end (point-max)))
765 (setq start (marker-position end))
767 ;; There may be more than one "From " line, so we skip past
769 (while (looking-at delim)
771 (set-marker end (if (nnmail-search-unix-mail-delim)
775 (when (not (search-forward marker end t))
776 (narrow-to-region start end)
777 (nnmail-insert-lines)
778 (nnfolder-insert-newsgroup-line
779 (cons nil (nnfolder-active-number nnfolder-current-group)))
783 ;; Make absolutely sure that the active list reflects reality!
784 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
785 ;; Set the scantime for this group.
786 (setq newscantime (visited-file-modtime))
788 (setcdr scantime (list newscantime))
789 (push (list nnfolder-current-group newscantime)
790 nnfolder-scantime-alist))
791 (current-buffer))))))
794 (defun nnfolder-generate-active-file ()
795 "Look for mbox folders in the nnfolder directory and make them into groups.
796 This command does not work if you use short group names."
798 (nnmail-activate 'nnfolder)
799 (let ((files (directory-files nnfolder-directory))
801 (while (setq file (pop files))
802 (when (and (not (backup-file-name-p file))
803 (message-mail-file-mbox-p
804 (nnheader-concat nnfolder-directory file)))
805 (let ((oldgroup (assoc file nnfolder-group-alist)))
807 (nnheader-message 5 "Refreshing group %s..." file)
808 (nnheader-message 5 "Adding group %s..." file))
810 (setq nnfolder-group-alist
811 (delq oldgroup (copy-sequence nnfolder-group-alist))))
812 (push (list file (cons 1 0)) nnfolder-group-alist)
813 (nnfolder-possibly-change-folder file)
814 (nnfolder-possibly-change-group file)
815 (nnfolder-close-group file))))
816 (nnheader-message 5 "")))
818 (defun nnfolder-group-pathname (group)
819 "Make pathname for GROUP."
821 (mm-encode-coding-string group nnmail-pathname-coding-system))
822 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
823 ;; If this file exists, we use it directly.
824 (if (or nnmail-use-long-file-names
825 (file-exists-p (concat dir group)))
827 ;; If not, we translate dots into slashes.
828 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
830 (defun nnfolder-save-buffer ()
832 (when (buffer-modified-p)
833 (run-hooks 'nnfolder-save-buffer-hook)
834 (gnus-make-directory (file-name-directory (buffer-file-name)))
835 (let ((coding-system-for-write
836 (or nnfolder-file-coding-system-for-write
837 nnfolder-file-coding-system)))
840 (defun nnfolder-save-active (group-alist active-file)
841 (let ((nnmail-active-file-coding-system
842 (or nnfolder-active-file-coding-system-for-write
843 nnfolder-active-file-coding-system)))
844 (nnmail-save-active group-alist active-file)))
848 ;;; nnfolder.el ends here