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 (setq stop (if (search-forward "\n\n" nil t)
125 (set-buffer nntp-server-buffer)
126 (insert (format "221 %d Article retrieved.\n" article))
127 (insert-buffer-substring nnfolder-current-buffer start stop)
128 (goto-char (point-max))
131 (set-buffer nntp-server-buffer)
132 (nnheader-fold-continuation-lines)
135 (deffoo nnfolder-open-server (server &optional defs)
136 (nnoo-change-server 'nnfolder server defs)
137 (nnmail-activate 'nnfolder t)
138 (gnus-make-directory nnfolder-directory)
140 ((not (file-exists-p nnfolder-directory))
141 (nnfolder-close-server)
142 (nnheader-report 'nnfolder "Couldn't create directory: %s"
144 ((not (file-directory-p (file-truename nnfolder-directory)))
145 (nnfolder-close-server)
146 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
148 (nnmail-activate 'nnfolder)
149 (nnheader-report 'nnfolder "Opened server %s using directory %s"
150 server nnfolder-directory)
153 (deffoo nnfolder-request-close ()
154 (let ((alist nnfolder-buffer-alist))
156 (nnfolder-close-group (caar alist) nil t)
157 (setq alist (cdr alist))))
158 (nnoo-close-server 'nnfolder)
159 (setq nnfolder-buffer-alist nil
160 nnfolder-group-alist nil))
162 (deffoo nnfolder-request-article (article &optional group server buffer)
163 (nnfolder-possibly-change-group group server)
165 (set-buffer nnfolder-current-buffer)
166 (goto-char (point-min))
167 (when (nnfolder-goto-article article)
171 (unless (and (nnmail-search-unix-mail-delim)
173 (goto-char (point-max)))
175 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
176 (set-buffer nntp-server-buffer)
178 (insert-buffer-substring nnfolder-current-buffer start stop)
179 (goto-char (point-min))
180 (while (looking-at "From ")
182 (insert "X-From-Line: ")
184 (if (numberp article)
185 (cons nnfolder-current-group article)
186 (goto-char (point-min))
187 (search-forward (concat "\n" nnfolder-article-marker))
188 (cons nnfolder-current-group
191 (point) (progn (end-of-line) (point)))))))))))
193 (deffoo nnfolder-request-group (group &optional server dont-check)
194 (nnfolder-possibly-change-group group server t)
196 (if (not (assoc group nnfolder-group-alist))
197 (nnheader-report 'nnfolder "No such group: %s" group)
200 (nnheader-report 'nnfolder "Selected group %s" group)
202 (let* ((active (assoc group nnfolder-group-alist))
204 (range (cadr active)))
207 (nnheader-report 'nnfolder "No such group: %s" group))
208 ((null nnfolder-current-group)
209 (nnheader-report 'nnfolder "Empty group: %s" group))
211 (nnheader-report 'nnfolder "Selected group %s" group)
212 (nnheader-insert "211 %d %d %d %s\n"
213 (1+ (- (cdr range) (car range)))
214 (car range) (cdr range) group))))))))
216 (deffoo nnfolder-request-scan (&optional group server)
217 (nnfolder-possibly-change-group nil server)
218 (when nnfolder-get-new-mail
219 (nnfolder-possibly-change-group group server)
223 (let ((bufs nnfolder-buffer-alist))
226 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
227 (setq nnfolder-buffer-alist
228 (delq (car bufs) nnfolder-buffer-alist))
229 (set-buffer (nth 1 (car bufs)))
230 (nnfolder-save-buffer)
231 (kill-buffer (current-buffer)))
232 (setq bufs (cdr bufs))))))
236 ;; Don't close the buffer if we're not shutting down the server. This way,
237 ;; we can keep the buffer in the group buffer cache, and not have to grovel
238 ;; over the buffer again unless we add new mail to it or modify it in some
241 (deffoo nnfolder-close-group (group &optional server force)
242 ;; Make sure we _had_ the group open.
243 (when (or (assoc group nnfolder-buffer-alist)
244 (equal group nnfolder-current-group))
245 (let ((inf (assoc group nnfolder-buffer-alist)))
247 (when (and nnfolder-current-group
248 nnfolder-current-buffer)
249 (push (list nnfolder-current-group nnfolder-current-buffer)
250 nnfolder-buffer-alist))
251 (setq nnfolder-buffer-alist
252 (delq inf nnfolder-buffer-alist))
253 (setq nnfolder-current-buffer (cadr inf)
254 nnfolder-current-group (car inf))))
255 (when (and nnfolder-current-buffer
256 (buffer-name nnfolder-current-buffer))
258 (set-buffer nnfolder-current-buffer)
259 ;; If the buffer was modified, write the file out now.
260 (nnfolder-save-buffer)
261 ;; If we're shutting the server down, we need to kill the
262 ;; buffer and remove it from the open buffer list. Or, of
263 ;; course, if we're trying to minimize our space impact.
264 (kill-buffer (current-buffer))
265 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
266 nnfolder-buffer-alist)))))
267 (setq nnfolder-current-group nil
268 nnfolder-current-buffer nil)
271 (deffoo nnfolder-request-create-group (group &optional server args)
272 (nnfolder-possibly-change-group nil server)
273 (nnmail-activate 'nnfolder)
275 (unless (assoc group nnfolder-group-alist)
276 (push (list group (cons 1 0)) nnfolder-group-alist)
277 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
278 (nnfolder-read-folder group)))
281 (deffoo nnfolder-request-list (&optional server)
282 (nnfolder-possibly-change-group nil server)
284 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
285 (nnmail-find-file nnfolder-active-file)
286 (setq nnfolder-group-alist (nnmail-get-active)))
289 (deffoo nnfolder-request-newgroups (date &optional server)
290 (nnfolder-possibly-change-group nil server)
291 (nnfolder-request-list server))
293 (deffoo nnfolder-request-list-newsgroups (&optional server)
294 (nnfolder-possibly-change-group nil server)
296 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
297 (nnmail-find-file nnfolder-newsgroups-file))))
299 ;; Return a list consisting of all article numbers existing in the
302 (defun nnfolder-existing-articles ()
304 (when nnfolder-current-buffer
305 (set-buffer nnfolder-current-buffer)
306 (goto-char (point-min))
307 (let ((marker (concat "\n" nnfolder-article-marker))
311 (while (and (search-forward marker nil t)
312 (re-search-forward number nil t))
313 (let ((newnum (string-to-number (match-string 0))))
314 (if (nnmail-within-headers-p)
315 (push newnum numbers))))
318 (deffoo nnfolder-request-expire-articles
319 (articles newsgroup &optional server force)
320 (nnfolder-possibly-change-group newsgroup server)
322 ;; The articles we have deleted so far.
323 (deleted-articles nil)
324 ;; The articles that really exist and will be expired if they are old enough.
325 (maybe-expirable (gnus-intersection articles (nnfolder-existing-articles))))
326 (nnmail-activate 'nnfolder)
329 (set-buffer nnfolder-current-buffer)
330 ;; Since messages are sorted in arrival order and expired in the
331 ;; same order, we can stop as soon as we find a message that is
333 (while (and maybe-expirable is-old)
334 (goto-char (point-min))
335 (when (and (nnfolder-goto-article (car maybe-expirable))
336 (search-forward (concat "\n" nnfolder-article-marker)
340 (nnmail-expired-article-p
343 (point) (progn (end-of-line) (point)))
344 force nnfolder-inhibit-expiry))
345 (nnheader-message 5 "Deleting article %d..."
346 (car maybe-expirable) newsgroup)
347 (nnfolder-delete-mail)
348 ;; Must remember which articles were actually deleted
349 (push (car maybe-expirable) deleted-articles)))
350 (setq maybe-expirable (cdr maybe-expirable)))
351 (unless nnfolder-inhibit-expiry
352 (nnheader-message 5 "Deleting articles...done"))
353 (nnfolder-save-buffer)
354 (nnfolder-adjust-min-active newsgroup)
355 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
356 (gnus-sorted-complement articles (nreverse deleted-articles)))))
358 (deffoo nnfolder-request-move-article (article group server
359 accept-form &optional last)
361 (let ((buf (get-buffer-create " *nnfolder move*"))
364 (nnfolder-request-article article group server)
368 (insert-buffer-substring nntp-server-buffer)
369 (goto-char (point-min))
370 (while (re-search-forward
371 (concat "^" nnfolder-article-marker)
372 (save-excursion (and (search-forward "\n\n" nil t) (point)))
374 (delete-region (progn (beginning-of-line) (point))
375 (progn (forward-line 1) (point))))
376 (setq result (eval accept-form))
380 (nnfolder-possibly-change-group group server)
381 (set-buffer nnfolder-current-buffer)
382 (goto-char (point-min))
383 (when (nnfolder-goto-article article)
384 (nnfolder-delete-mail))
386 (nnfolder-save-buffer)
387 (nnfolder-adjust-min-active group)
388 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
391 (deffoo nnfolder-request-accept-article (group &optional server last)
393 (nnfolder-possibly-change-group group server)
394 (nnmail-check-syntax)
395 (let ((buf (current-buffer))
397 (goto-char (point-min))
398 (when (looking-at "X-From-Line: ")
399 (replace-match "From "))
401 (nnfolder-request-list)
404 (goto-char (point-min))
405 (if (search-forward "\n\n" nil t)
407 (goto-char (point-max)))
408 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
409 (delete-region (point) (progn (forward-line 1) (point))))
410 (when nnmail-cache-accepted-message-ids
411 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
412 (setq result (if (stringp group)
413 (list (cons group (nnfolder-active-number group)))
415 (nnmail-article-group 'nnfolder-active-number))))
416 (if (and (null result)
417 (yes-or-no-p "Moved to `junk' group; delete article? "))
420 (car (nnfolder-save-mail result)))))
423 (nnfolder-possibly-change-folder (or (caar art-group) group))
424 (nnfolder-save-buffer)
425 (when nnmail-cache-accepted-message-ids
426 (nnmail-cache-close)))))
427 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
429 (nnheader-report 'nnfolder "Couldn't store article"))
432 (deffoo nnfolder-request-replace-article (article group buffer)
433 (nnfolder-possibly-change-group group)
436 (goto-char (point-min))
438 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
439 (setq xfrom (match-string 1))
441 (goto-char (point-min))
443 (insert "From " xfrom "\n")
444 (unless (looking-at "From ")
445 (insert "From nobody " (current-time-string) "\n"))))
446 (nnfolder-normalize-buffer)
447 (set-buffer nnfolder-current-buffer)
448 (goto-char (point-min))
449 (if (not (nnfolder-goto-article article))
451 (nnfolder-delete-mail)
452 (insert-buffer-substring buffer)
453 (nnfolder-save-buffer)
456 (deffoo nnfolder-request-delete-group (group &optional force server)
457 (nnfolder-close-group group server t)
458 ;; Delete all articles in GROUP.
460 () ; Don't delete the articles.
461 ;; Delete the file that holds the group.
463 (delete-file (nnfolder-group-pathname group))))
464 ;; Remove the group from all structures.
465 (setq nnfolder-group-alist
466 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
467 nnfolder-current-group nil
468 nnfolder-current-buffer nil)
469 ;; Save the active file.
470 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
473 (deffoo nnfolder-request-rename-group (group new-name &optional server)
474 (nnfolder-possibly-change-group group server)
476 (set-buffer nnfolder-current-buffer)
477 (and (file-writable-p buffer-file-name)
481 (let ((new-file (nnfolder-group-pathname new-name)))
482 (gnus-make-directory (file-name-directory new-file))
485 ;; That went ok, so we change the internal structures.
486 (let ((entry (assoc group nnfolder-group-alist)))
487 (and entry (setcar entry new-name))
488 (setq nnfolder-current-buffer nil
489 nnfolder-current-group nil)
490 ;; Save the new group alist.
491 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
492 ;; We kill the buffer instead of renaming it and stuff.
493 (kill-buffer (current-buffer))
496 (defun nnfolder-request-regenerate (server)
497 (nnfolder-possibly-change-group nil server)
498 (nnfolder-generate-active-file)
502 ;;; Internal functions.
504 (defun nnfolder-adjust-min-active (group)
505 ;; Find the lowest active article in this group.
506 (let* ((active (cadr (assoc group nnfolder-group-alist)))
507 (marker (concat "\n" nnfolder-article-marker))
509 (activemin (cdr active)))
511 (set-buffer nnfolder-current-buffer)
512 (goto-char (point-min))
513 (while (and (search-forward marker nil t)
514 (re-search-forward number nil t))
515 (let ((newnum (string-to-number (match-string 0))))
516 (if (nnmail-within-headers-p)
517 (setq activemin (min activemin newnum)))))
518 (setcar active activemin))))
520 (defun nnfolder-article-string (article)
521 (if (numberp article)
522 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
523 (concat "\nMessage-ID: " article)))
525 (defun nnfolder-goto-article (article)
526 "Place point at the start of the headers of ARTICLE.
527 ARTICLE can be an article number or a Message-ID.
528 Returns t if successful, nil otherwise."
529 (let ((art-string (nnfolder-article-string article))
531 ;; It is likely that we are at or before the delimiter line.
532 ;; We therefore go to the end of the previous line, and start
533 ;; searching from there.
538 ;; First search forward.
539 (while (and (setq found (search-forward art-string nil t))
540 (not (nnmail-within-headers-p))))
541 ;; If unsuccessful, search backward from where we started,
544 (while (and (setq found (search-backward art-string nil t))
545 (not (nnmail-within-headers-p)))))
547 (nnmail-search-unix-mail-delim-backward))))
549 (defun nnfolder-delete-mail (&optional leave-delim)
550 "Delete the message that point is in.
551 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
552 deleted. Point is left where the deleted region was."
556 ;; In case point is at the beginning of the message already.
558 (nnmail-search-unix-mail-delim-backward)
559 (if leave-delim (progn (forward-line 1) (point))
563 (if (nnmail-search-unix-mail-delim)
566 (run-hooks 'nnfolder-delete-mail-hook)
567 (delete-region (point-min) (point-max))))
569 (defun nnfolder-possibly-change-group (group &optional server dont-check)
572 (not (nnfolder-server-opened server)))
573 (nnfolder-open-server server))
574 (unless (gnus-buffer-live-p nnfolder-current-buffer)
575 (setq nnfolder-current-buffer nil
576 nnfolder-current-group nil))
579 (not (equal group nnfolder-current-group)))
580 (let ((pathname-coding-system nnmail-pathname-coding-system))
581 (nnmail-activate 'nnfolder)
582 (when (and (not (assoc group nnfolder-group-alist))
584 (nnfolder-group-pathname group))))
585 ;; The group doesn't exist, so we create a new entry for it.
586 (push (list group (cons 1 0)) nnfolder-group-alist)
587 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
590 (setq nnfolder-current-group group
591 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-buffer-alist (delq inf nnfolder-buffer-alist)
605 nnfolder-current-buffer nil))
607 (setq nnfolder-current-group group)
609 (when (or (not nnfolder-current-buffer)
610 (not (verify-visited-file-modtime
611 nnfolder-current-buffer)))
613 (setq file (nnfolder-group-pathname group))
614 ;; See whether we need to create the new file.
615 (unless (file-exists-p file)
616 (gnus-make-directory (file-name-directory file))
617 (let ((nnmail-file-coding-system
618 (or nnfolder-file-coding-system-for-write
619 nnfolder-file-coding-system-for-write)))
620 (nnmail-write-region 1 1 file t 'nomesg)))
621 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
622 (set-buffer nnfolder-current-buffer)
623 (push (list group nnfolder-current-buffer)
624 nnfolder-buffer-alist)))))))))
626 (defun nnfolder-save-mail (group-art-list)
627 "Called narrowed to an article."
628 (let* (save-list group-art)
629 (goto-char (point-min))
630 ;; The From line may have been quoted by movemail.
631 (when (looking-at ">From")
633 ;; This might come from somewhere else.
634 (unless (looking-at "From ")
635 (insert "From nobody " (current-time-string) "\n")
636 (goto-char (point-min)))
637 ;; Quote all "From " lines in the article.
639 (let (case-fold-search)
640 (while (re-search-forward "^From " nil t)
643 (setq save-list group-art-list)
644 (nnmail-insert-lines)
645 (nnmail-insert-xref group-art-list)
646 (run-hooks 'nnmail-prepare-save-mail-hook)
647 (run-hooks 'nnfolder-prepare-save-mail-hook)
649 ;; Insert the mail into each of the destination groups.
650 (while (setq group-art (pop group-art-list))
651 ;; Kill any previous newsgroup markers.
652 (goto-char (point-min))
653 (if (search-forward "\n\n" nil t)
655 (goto-char (point-max)))
656 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
657 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
659 ;; Insert the new newsgroup marker.
660 (nnfolder-insert-newsgroup-line group-art)
663 (let ((beg (point-min))
665 (obuf (current-buffer)))
666 (nnfolder-possibly-change-folder (car group-art))
667 (let ((buffer-read-only nil))
668 (nnfolder-normalize-buffer)
669 (insert-buffer-substring obuf beg end)))))
671 ;; Did we save it anywhere?
674 (defun nnfolder-normalize-buffer ()
675 "Make sure there are two newlines at the end of the buffer."
676 (goto-char (point-max))
677 (skip-chars-backward "\n")
678 (delete-region (point) (point-max))
681 (defun nnfolder-insert-newsgroup-line (group-art)
683 (goto-char (point-min))
684 (unless (search-forward "\n\n" nil t)
685 (goto-char (point-max))
688 (insert (format (concat nnfolder-article-marker "%d %s\n")
689 (cdr group-art) (current-time-string)))))
691 (defun nnfolder-active-number (group)
692 ;; Find the next article number in GROUP.
693 (let ((active (cadr (assoc group nnfolder-group-alist))))
695 (setcdr active (1+ (cdr active)))
696 ;; This group is new, so we create a new entry for it.
697 ;; This might be a bit naughty... creating groups on the drop of
698 ;; a hat, but I don't know...
699 (push (list group (setq active (cons 1 1)))
700 nnfolder-group-alist))
703 (defun nnfolder-possibly-change-folder (group)
704 (let ((inf (assoc group nnfolder-buffer-alist)))
706 (gnus-buffer-live-p (cadr inf)))
707 (set-buffer (cadr inf))
709 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
710 (when nnfolder-group-alist
711 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
712 (push (list group (nnfolder-read-folder group))
713 nnfolder-buffer-alist))))
715 ;; This method has a problem if you've accidentally let the active list get
716 ;; out of sync with the files. This could happen, say, if you've
717 ;; accidentally gotten new mail with something other than Gnus (but why
718 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
719 ;; processing the file, ready to add new X-Gnus article number markers, and
720 ;; we'll run across a message with no ID yet - the active list _may_not_ be
723 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
724 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
725 ;; rest of the message looking for any more, possibly higher IDs. We'll
726 ;; assume the maximum that we find is the highest active. Note that this
727 ;; shouldn't cost us much extra time at all, but will be a lot less
728 ;; vulnerable to glitches between the mbox and the active file.
730 (defun nnfolder-read-folder (group)
731 (let* ((file (nnfolder-group-pathname group))
733 (let ((nnheader-file-coding-system
734 nnfolder-file-coding-system))
735 (nnheader-find-file-noselect file)))))
736 (if (equal (cadr (assoc group nnfolder-scantime-alist))
737 (nth 5 (file-attributes file)))
738 ;; This looks up-to-date, so we don't do any scanning.
739 (if (file-exists-p file)
741 (push (list group buffer) nnfolder-buffer-alist)
742 (set-buffer-modified-p t)
744 ;; Parse the damn thing.
746 (goto-char (point-min))
747 ;; Remove any blank lines at the start.
748 (while (eq (following-char) ?\n)
750 (nnmail-activate 'nnfolder)
752 (let ((delim "^From ")
753 (marker (concat "\n" nnfolder-article-marker))
755 (active (or (cadr (assoc group nnfolder-group-alist))
757 (scantime (assoc group nnfolder-scantime-alist))
759 maxid start end newscantime
761 (buffer-disable-undo)
762 (setq maxid (cdr active))
763 (goto-char (point-min))
765 ;; Anytime the active number is 1 or 0, it is suspect. In that
766 ;; case, search the file manually to find the active number. Or,
767 ;; of course, if we're being paranoid. (This would also be the
768 ;; place to build other lists from the header markers, such as
769 ;; expunge lists, etc., if we ever desired to abandon the active
770 ;; file entirely for mboxes.)
771 (when (or nnfolder-ignore-active-file
773 (while (and (search-forward marker nil t)
774 (re-search-forward number nil t))
775 (let ((newnum (string-to-number (match-string 0))))
776 (if (nnmail-within-headers-p)
777 (setq maxid (max maxid newnum)
778 minid (min minid newnum)))))
779 (setcar active (max 1 (min minid maxid)))
780 (setcdr active (max maxid (cdr active)))
781 (goto-char (point-min)))
783 ;; As long as we trust that the user will only insert unmarked mail
784 ;; at the end, go to the end and search backwards for the last
785 ;; marker. Find the start of that message, and begin to search for
786 ;; unmarked messages from there.
787 (when (not (or nnfolder-distrust-mbox
789 (goto-char (point-max))
790 (unless (re-search-backward marker nil t)
791 (goto-char (point-min)))
792 (when (nnmail-search-unix-mail-delim)
793 (goto-char (point-min))))
795 ;; Keep track of the active number on our own, and insert it back
796 ;; into the active list when we're done. Also, prime the pump to
797 ;; cut down on the number of searches we do.
798 (unless (nnmail-search-unix-mail-delim)
799 (goto-char (point-max)))
800 (setq end (point-marker))
801 (while (not (= end (point-max)))
802 (setq start (marker-position end))
804 ;; There may be more than one "From " line, so we skip past
806 (while (looking-at delim)
808 (set-marker end (if (nnmail-search-unix-mail-delim)
812 (when (not (search-forward marker end t))
813 (narrow-to-region start end)
814 (nnmail-insert-lines)
815 (nnfolder-insert-newsgroup-line
816 (cons nil (nnfolder-active-number nnfolder-current-group)))
820 ;; Make absolutely sure that the active list reflects reality!
821 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
822 ;; Set the scantime for this group.
823 (setq newscantime (visited-file-modtime))
825 (setcdr scantime (list newscantime))
826 (push (list nnfolder-current-group newscantime)
827 nnfolder-scantime-alist))
828 (current-buffer))))))
831 (defun nnfolder-generate-active-file ()
832 "Look for mbox folders in the nnfolder directory and make them into groups.
833 This command does not work if you use short group names."
835 (nnmail-activate 'nnfolder)
836 (let ((files (directory-files nnfolder-directory))
838 (while (setq file (pop files))
839 (when (and (not (backup-file-name-p file))
840 (message-mail-file-mbox-p
841 (nnheader-concat nnfolder-directory file)))
842 (let ((oldgroup (assoc file nnfolder-group-alist)))
844 (nnheader-message 5 "Refreshing group %s..." file)
845 (nnheader-message 5 "Adding group %s..." file))
847 (setq nnfolder-group-alist
848 (delq oldgroup (copy-sequence nnfolder-group-alist))))
849 (push (list file (cons 1 0)) nnfolder-group-alist)
850 (nnfolder-possibly-change-folder file)
851 (nnfolder-possibly-change-group file)
852 (nnfolder-close-group file))))
853 (nnheader-message 5 "")))
855 (defun nnfolder-group-pathname (group)
856 "Make pathname for GROUP."
858 (mm-encode-coding-string group nnmail-pathname-coding-system))
859 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
860 ;; If this file exists, we use it directly.
861 (if (or nnmail-use-long-file-names
862 (file-exists-p (concat dir group)))
864 ;; If not, we translate dots into slashes.
865 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
867 (defun nnfolder-save-buffer ()
869 (when (buffer-modified-p)
870 (run-hooks 'nnfolder-save-buffer-hook)
871 (gnus-make-directory (file-name-directory (buffer-file-name)))
872 (let ((coding-system-for-write
873 (or nnfolder-file-coding-system-for-write
874 nnfolder-file-coding-system)))
877 (defun nnfolder-save-active (group-alist active-file)
878 (let ((nnmail-active-file-coding-system
879 (or nnfolder-active-file-coding-system-for-write
880 nnfolder-active-file-coding-system)))
881 (nnmail-save-active group-alist active-file)))
885 ;;; nnfolder.el ends here