*** empty log message ***
[gnus] / lisp / nnfolder.el
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Scott Byer <byer@mv.us.adobe.com>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; Keywords: news, mail
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;; For an overview of what the interface functions do, please see the
28 ;; Gnus sources.  
29
30 ;; Various enhancements by byer@mv.us.adobe.com (Scott Byer).
31
32 ;;; Code:
33
34 (require 'nnheader)
35 (require 'rmail)
36 (require 'nnmail)
37
38 (defvar nnfolder-directory (expand-file-name "~/Mail/")
39   "The name of the mail box file in the users home directory.")
40
41 (defvar nnfolder-active-file 
42   (concat (file-name-as-directory nnfolder-directory) "active")
43   "The name of the active file.")
44
45 ;; I renamed this variable to somehting more in keeping with the general GNU
46 ;; style. -SLB
47
48 (defvar nnfolder-ignore-active-file nil
49   "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.  
50 Note that the active file is still saved, but it's values are not
51 used.  This costs some extra time when scanning an mbox when opening
52 it.")
53
54 ;; Note that this variable may not be completely implemented yet. -SLB
55
56 (defvar nnfolder-always-close nil
57   "If non-nil, nnfolder attempts to only ever have one mbox open at a time.  
58 This is a straight space/performance trade off, as the mboxes will have to 
59 be scanned every time they are read in.  If nil (default), nnfolder will
60 attempt to keep the buffers around (saving the nnfolder's buffer upon group 
61 close, but not killing it), speeding some things up tremendously, especially
62 such things as moving mail.  All buffers always get killed upon server close.")
63
64 (defvar nnfolder-newsgroups-file 
65   (concat (file-name-as-directory  nnfolder-directory) "newsgroups")
66   "Mail newsgroups description file.")
67
68 (defvar nnfolder-get-new-mail t
69   "If non-nil, nnfolder will check the incoming mail file and split the mail.")
70
71 (defvar nnfolder-prepare-save-mail-hook nil
72   "Hook run narrowed to an article before saving.")
73
74 \f
75
76 (defconst nnfolder-version "nnfolder 1.0"
77   "nnfolder version.")
78
79 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
80   "String used to demarcate what the article number for a message is.")
81
82 (defvar nnfolder-current-group nil)
83 (defvar nnfolder-current-buffer nil)
84 (defvar nnfolder-status-string "")
85 (defvar nnfolder-group-alist nil)
86 (defvar nnfolder-buffer-alist nil)
87 (defvar nnfolder-active-timestamp nil)
88
89 \f
90
91 (defvar nnfolder-current-server nil)
92 (defvar nnfolder-server-alist nil)
93 (defvar nnfolder-server-variables 
94   (list 
95    (list 'nnfolder-directory nnfolder-directory)
96    (list 'nnfolder-active-file nnfolder-active-file)
97    (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
98    (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
99    '(nnfolder-current-group nil)
100    '(nnfolder-current-buffer nil)
101    '(nnfolder-status-string "")
102    '(nnfolder-group-alist nil)
103    '(nnfolder-buffer-alist nil)
104    '(nnfolder-active-timestamp nil)))
105
106 \f
107
108 ;;; Interface functions
109
110 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server)
111   (save-excursion
112     (set-buffer nntp-server-buffer)
113     (erase-buffer)
114     (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
115           article art-string start stop)
116       (nnfolder-possibly-change-group newsgroup)
117       (set-buffer nnfolder-current-buffer)
118       (goto-char (point-min))
119       (if (stringp (car sequence))
120           'headers
121         (while sequence
122           (setq article (car sequence))
123           (setq art-string (nnfolder-article-string article))
124           (set-buffer nnfolder-current-buffer)
125           (if (or (search-forward art-string nil t)
126                   ;; Don't search the whole file twice!  Also, articles
127                   ;; probably have some locality by number, so searching
128                   ;; backwards will be faster.  Especially if we're at the
129                   ;; beginning of the buffer :-). -SLB
130                   (search-backward art-string nil t))
131               (progn
132                 (setq start (or (re-search-backward delim-string nil t)
133                                 (point)))
134                 (search-forward "\n\n" nil t)
135                 (setq stop (1- (point)))
136                 (set-buffer nntp-server-buffer)
137                 (insert (format "221 %d Article retrieved.\n" article))
138                 (insert-buffer-substring nnfolder-current-buffer start stop)
139                 (goto-char (point-max))
140                 (insert ".\n")))
141           (setq sequence (cdr sequence)))
142
143         ;; Fold continuation lines.
144         (set-buffer nntp-server-buffer)
145         (goto-char (point-min))
146         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
147           (replace-match " " t t))
148         'headers))))
149
150 (defun nnfolder-open-server (server &optional defs)
151   (nnheader-init-server-buffer)
152   (if (equal server nnfolder-current-server)
153       t
154     (if nnfolder-current-server
155         (setq nnfolder-server-alist 
156               (cons (list nnfolder-current-server
157                           (nnheader-save-variables nnfolder-server-variables))
158                     nnfolder-server-alist)))
159     (let ((state (assoc server nnfolder-server-alist)))
160       (if state 
161           (progn
162             (nnheader-restore-variables (nth 1 state))
163             (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
164         (nnheader-set-init-variables nnfolder-server-variables defs)))
165     (setq nnfolder-current-server server)))
166
167 (defun nnfolder-close-server (&optional server)
168   t)
169
170 (defun nnfolder-server-opened (&optional server)
171   (and (equal server nnfolder-current-server)
172        nntp-server-buffer
173        (buffer-name nntp-server-buffer)))
174
175 (defun nnfolder-request-close ()
176   (let ((alist nnfolder-buffer-alist))
177     (while alist
178       (nnfolder-close-group (car (car alist)) nil t)
179       (setq alist (cdr alist))))
180   (setq nnfolder-buffer-alist nil
181         nnfolder-group-alist nil))
182
183 (defun nnfolder-status-message (&optional server)
184   nnfolder-status-string)
185
186 (defun nnfolder-request-article (article &optional newsgroup server buffer)
187   (nnfolder-possibly-change-group newsgroup)
188   (save-excursion
189     (set-buffer nnfolder-current-buffer)
190     (goto-char (point-min))
191     (if (search-forward (nnfolder-article-string article) nil t)
192         (let (start stop)
193           (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
194           (setq start (point))
195           (forward-line 1)
196           (or (and (re-search-forward 
197                     (concat "^" rmail-unix-mail-delimiter) nil t)
198                    (forward-line -1))
199               (goto-char (point-max)))
200           (setq stop (point))
201           (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
202             (set-buffer nntp-server-buffer)
203             (erase-buffer)
204             (insert-buffer-substring nnfolder-current-buffer start stop)
205             (goto-char (point-min))
206             (while (looking-at "From ")
207               (delete-char 5)
208               (insert "X-From-Line: ")
209               (forward-line 1))
210             (if (numberp article) 
211                 (cons nnfolder-current-group article)
212               (goto-char (point-min))
213               (search-forward (concat "\n" nnfolder-article-marker))
214               (cons nnfolder-current-group
215                     (string-to-int 
216                      (buffer-substring 
217                       (point) (progn (end-of-line) (point)))))))))))
218
219 (defun nnfolder-request-group (group &optional server dont-check)
220   (save-excursion
221     (nnmail-activate 'nnfolder)
222     (nnfolder-possibly-change-group group)
223     (and (assoc group nnfolder-group-alist)
224          (progn
225            (if dont-check
226                t
227              (let* ((active (assoc group nnfolder-group-alist))
228                     (group (car active))
229                     (range (car (cdr active)))
230                     (minactive (car range))
231                     (maxactive (cdr range)))
232                ;; I've been getting stray 211 lines in my nnfolder active
233                ;; file.  So, let's make sure that doesn't happen. -SLB
234                (set-buffer nntp-server-buffer)
235                (erase-buffer)
236                (if (not active)
237                    ()
238                  (insert (format "211 %d %d %d %s\n" 
239                                  (1+ (- maxactive minactive))
240                                  minactive maxactive group))
241                  t)))))))
242
243 (defun nnfolder-request-scan (&optional group server)
244   (nnmail-get-new-mail
245    'nnfolder 
246    (lambda ()
247      (let ((bufs nnfolder-buffer-alist))
248        (save-excursion
249          (while bufs
250            (if (not (buffer-name (nth 1 (car bufs))))
251                (setq nnfolder-buffer-alist 
252                      (delq (car bufs) nnfolder-buffer-alist))
253              (set-buffer (nth 1 (car bufs)))
254              (and (buffer-modified-p) (save-buffer)))
255            (setq bufs (cdr bufs))))))
256    nnfolder-directory
257    group))
258
259 ;; Don't close the buffer if we're not shutting down the server.  This way,
260 ;; we can keep the buffer in the group buffer cache, and not have to grovel
261 ;; over the buffer again unless we add new mail to it or modify it in some
262 ;; way.
263
264 (defun nnfolder-close-group (group &optional server force)
265   ;; Make sure we _had_ the group open.
266   (if (or (assoc group nnfolder-buffer-alist)
267           (equal group nnfolder-current-group))
268       (progn
269         (nnfolder-possibly-change-group group)
270         (save-excursion
271           (set-buffer nnfolder-current-buffer)
272           ;; If the buffer was modified, write the file out now.
273           (and (buffer-modified-p) (save-buffer))
274           (if (or force
275                   nnfolder-always-close)
276               ;; If we're shutting the server down, we need to kill the
277               ;; buffer and remove it from the open buffer list.  Or, of
278               ;; course, if we're trying to minimize our space impact.
279               (progn
280                 (kill-buffer (current-buffer))
281                 (setq nnfolder-buffer-alist (delq (assoc group 
282                                                          nnfolder-buffer-alist)
283                                                   nnfolder-buffer-alist)))))))
284   (setq nnfolder-current-group nil
285         nnfolder-current-buffer nil)
286   t)
287
288 (defun nnfolder-request-create-group (group &optional server) 
289   (nnmail-activate 'nnfolder)
290   (or (assoc group nnfolder-group-alist)
291       (let (active)
292         (setq nnfolder-group-alist 
293               (cons (list group (setq active (cons 1 0)))
294                     nnfolder-group-alist))
295         (nnfolder-possibly-change-group group)
296         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
297   t)
298
299 (defun nnfolder-request-list (&optional server)
300   (save-excursion
301     (nnmail-find-file nnfolder-active-file)
302     (setq nnfolder-group-alist (nnmail-get-active))))
303
304 (defun nnfolder-request-newgroups (date &optional server)
305   (nnfolder-request-list server))
306
307 (defun nnfolder-request-list-newsgroups (&optional server)
308   (save-excursion
309     (nnmail-find-file nnfolder-newsgroups-file)))
310
311 (defun nnfolder-request-post (&optional server)
312   (mail-send-and-exit nil))
313
314 (defalias 'nnfolder-request-post-buffer 'nnmail-request-post-buffer)
315
316 (defun nnfolder-request-expire-articles 
317   (articles newsgroup &optional server force)
318   (nnfolder-possibly-change-group newsgroup)
319   (let* ((days (or (and nnmail-expiry-wait-function
320                         (funcall nnmail-expiry-wait-function newsgroup))
321                    nnmail-expiry-wait))
322          (is-old t)
323          rest)
324     (nnmail-activate 'nnfolder)
325
326     (save-excursion 
327       (set-buffer nnfolder-current-buffer)
328       (while (and articles is-old)
329         (goto-char (point-min))
330         (if (search-forward (nnfolder-article-string (car articles)) nil t)
331             (if (or force
332                     (setq is-old
333                           (> (nnmail-days-between 
334                               (current-time-string)
335                               (buffer-substring 
336                                (point) (progn (end-of-line) (point))))
337                              days)))
338                 (progn
339                   (and gnus-verbose-backends
340                        (message "Deleting article %d..." 
341                                 (car articles) newsgroup))
342                   (nnfolder-delete-mail))
343               (setq rest (cons (car articles) rest))))
344         (setq articles (cdr articles)))
345       (and (buffer-modified-p) (save-buffer))
346       ;; Find the lowest active article in this group.
347       (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
348              (marker (concat "\n" nnfolder-article-marker))
349              (number "[0-9]+")
350              (activemin (cdr active)))
351         (goto-char (point-min))
352         (while (and (search-forward marker nil t)
353                     (re-search-forward number nil t))
354           (setq activemin (min activemin
355                                (string-to-number (buffer-substring
356                                                   (match-beginning 0)
357                                                   (match-end 0))))))
358         (setcar active activemin))
359       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
360       (nconc rest articles))))
361
362 (defun nnfolder-request-move-article
363   (article group server accept-form &optional last)
364   (nnfolder-possibly-change-group group)
365   (let ((buf (get-buffer-create " *nnfolder move*"))
366         result)
367     (and 
368      (nnfolder-request-article article group server)
369      (save-excursion
370        (set-buffer buf)
371        (buffer-disable-undo (current-buffer))
372        (erase-buffer)
373        (insert-buffer-substring nntp-server-buffer)
374        (goto-char (point-min))
375        (while (re-search-forward 
376                (concat "^" nnfolder-article-marker)
377                (save-excursion (search-forward "\n\n" nil t) (point)) t)
378          (delete-region (progn (beginning-of-line) (point))
379                         (progn (forward-line 1) (point))))
380        (setq result (eval accept-form))
381        (kill-buffer buf)
382        result)
383      (save-excursion
384        (nnfolder-possibly-change-group group)
385        (set-buffer nnfolder-current-buffer)
386        (goto-char (point-min))
387        (if (search-forward (nnfolder-article-string article) nil t)
388            (nnfolder-delete-mail))
389        (and last 
390             (buffer-modified-p)
391             (save-buffer))))
392     result))
393
394 (defun nnfolder-request-accept-article (group &optional last)
395   (and (stringp group) (nnfolder-possibly-change-group group))
396   (let ((buf (current-buffer))
397         result)
398     (goto-char (point-min))
399     (cond ((looking-at "X-From-Line: ")
400            (replace-match "From "))
401           ((not (looking-at "From "))
402            (insert "From nobody " (current-time-string) "\n")))
403     (and 
404      (nnfolder-request-list)
405      (save-excursion
406        (set-buffer buf)
407        (goto-char (point-min))
408        (search-forward "\n\n" nil t)
409        (forward-line -1)
410        (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
411          (delete-region (point) (progn (forward-line 1) (point))))
412        (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
413      (save-excursion
414        (set-buffer nnfolder-current-buffer)
415        (and last (buffer-modified-p) (save-buffer))))
416     (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
417     result))
418
419 (defun nnfolder-request-replace-article (article group buffer)
420   (nnfolder-possibly-change-group group)
421   (save-excursion
422     (set-buffer nnfolder-current-buffer)
423     (goto-char (point-min))
424     (if (not (search-forward (nnfolder-article-string article) nil t))
425         nil
426       (nnfolder-delete-mail t t)
427       (insert-buffer-substring buffer)
428       (and (buffer-modified-p) (save-buffer))
429       t)))
430
431 \f
432 ;;; Internal functions.
433
434 (defun nnfolder-article-string (article)
435   (if (numberp article)
436       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
437     (concat "\nMessage-ID: " article)))
438
439 (defun nnfolder-delete-mail (&optional force leave-delim)
440   ;; Beginning of the article.
441   (save-excursion
442     (save-restriction
443       (narrow-to-region
444        (save-excursion
445          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
446          (if leave-delim (progn (forward-line 1) (point))
447            (match-beginning 0)))
448        (progn
449          (forward-line 1)
450          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
451                                      nil t)
452                   (if (and (not (bobp)) leave-delim)
453                       (progn (forward-line -2) (point))
454                     (match-beginning 0)))
455              (point-max))))
456       (delete-region (point-min) (point-max)))))
457
458 (defun nnfolder-possibly-change-group (group)
459   (or (file-exists-p nnfolder-directory)
460       (make-directory (directory-file-name nnfolder-directory)))
461   (nnfolder-possibly-activate-groups nil)
462   (or (assoc group nnfolder-group-alist)
463       (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
464                                   group)))
465       (progn
466         (setq nnfolder-group-alist 
467               (cons (list group (cons 1 0)) nnfolder-group-alist))
468         (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
469   (let (inf file)
470     (if (and (equal group nnfolder-current-group)
471              nnfolder-current-buffer
472              (buffer-name nnfolder-current-buffer))
473         ()
474       (setq nnfolder-current-group group)
475
476       ;; If we have to change groups, see if we don't already have the mbox
477       ;; in memory.  If we do, verify the modtime and destroy the mbox if
478       ;; needed so we can rescan it.
479       (if (setq inf (assoc group nnfolder-buffer-alist))
480           (setq nnfolder-current-buffer (nth 1 inf)))
481
482       ;; If the buffer is not live, make sure it isn't in the alist.  If it
483       ;; is live, verify that nobody else has touched the file since last
484       ;; time.
485       (if (or (not (and nnfolder-current-buffer
486                         (buffer-name nnfolder-current-buffer)))
487               (not (and (bufferp nnfolder-current-buffer)
488                         (verify-visited-file-modtime 
489                          nnfolder-current-buffer))))
490           (progn
491             (if (and nnfolder-current-buffer
492                      (buffer-name nnfolder-current-buffer)
493                      (bufferp nnfolder-current-buffer))
494                 (kill-buffer nnfolder-current-buffer))
495             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
496             (setq inf nil)))
497       
498       (if inf
499           ()
500         (save-excursion
501           (setq file (concat (file-name-as-directory nnfolder-directory)
502                              group))
503           (if (file-directory-p (file-truename file))
504               ()
505             (if (not (file-exists-p file))
506                 (write-region 1 1 file t 'nomesg))
507             (setq nnfolder-current-buffer 
508                   (set-buffer (nnfolder-read-folder file)))
509             (setq nnfolder-buffer-alist (cons (list group (current-buffer))
510                                               nnfolder-buffer-alist)))))))
511   (setq nnfolder-current-group group))
512
513 (defun nnfolder-save-mail (&optional group)
514   "Called narrowed to an article."
515   (let* ((nnmail-split-methods 
516           (if group (list (list group "")) nnmail-split-methods))
517          (group-art-list
518           (nreverse (nnmail-article-group 'nnfolder-active-number)))
519          save-list group-art)
520     (setq save-list group-art-list)
521     (nnmail-insert-lines)
522     (nnmail-insert-xref group-art-list)
523     (run-hooks 'nnfolder-prepare-save-mail-hook)
524
525     ;; Insert the mail into each of the destination groups.
526     (while group-art-list
527       (setq group-art (car group-art-list)
528             group-art-list (cdr group-art-list))
529
530       ;; Kill the previous newsgroup markers.
531       (goto-char (point-min))
532       (search-forward "\n\n" nil t)
533       (forward-line -1)
534       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
535         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
536
537       ;; Insert the new newsgroup marker.
538       (nnfolder-possibly-change-group (car group-art))
539       (nnfolder-insert-newsgroup-line group-art)
540       (let ((beg (point-min))
541             (end (point-max))
542             (obuf (current-buffer)))
543         (set-buffer nnfolder-current-buffer)
544         (goto-char (point-max))
545         (insert-buffer-substring obuf beg end)
546         (set-buffer obuf)))
547
548     ;; Did we save it anywhere?
549     save-list))
550
551 (defun nnfolder-insert-newsgroup-line (group-art)
552   (save-excursion
553     (goto-char (point-min))
554     (if (search-forward "\n\n" nil t)
555         (progn
556           (forward-char -1)
557           (insert (format (concat nnfolder-article-marker "%d   %s\n")
558                           (cdr group-art) (current-time-string)))))))
559
560 (defun nnfolder-possibly-activate-groups (&optional group)
561   (save-excursion
562     ;; If we're looking for the activation of a specific group, find out
563     ;; its real name and switch to it.
564     (if group (nnfolder-possibly-change-group group))
565     ;; If the group alist isn't active, activate it now.
566     (nnmail-activate 'nnfolder)))
567
568 (defun nnfolder-active-number (group)
569   (save-excursion 
570     ;; Find the next article number in GROUP.
571     (prog1
572         (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
573           (if active
574               (setcdr active (1+ (cdr active)))
575             ;; This group is new, so we create a new entry for it.
576             ;; This might be a bit naughty... creating groups on the drop of
577             ;; a hat, but I don't know...
578             (setq nnfolder-group-alist 
579                   (cons (list group (setq active (cons 1 1)))
580                         nnfolder-group-alist)))
581           (cdr active))
582       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
583       (nnfolder-possibly-activate-groups group)
584       )))
585
586
587 ;; This method has a problem if you've accidentally let the active list get
588 ;; out of sync with the files.  This could happen, say, if you've
589 ;; accidentally gotten new mail with something other than Gnus (but why
590 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
591 ;; processing the file, ready to add new X-Gnus article number markers, and
592 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
593 ;; ready for us yet.
594
595 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
596 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
597 ;; rest of the message looking for any more, possibly higher IDs.  We'll
598 ;; assume the maximum that we find is the highest active.  Note that this
599 ;; shouldn't cost us much extra time at all, but will be a lot less
600 ;; vulnerable to glitches between the mbox and the active file.
601
602 (defun nnfolder-read-folder (file)
603   (save-excursion
604     (nnfolder-possibly-activate-groups nil)
605     ;; We should be paranoid here and make sure the group is in the alist,
606     ;; and add it if it isn't.
607     ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
608     (set-buffer (setq nnfolder-current-buffer 
609                       (nnheader-find-file-noselect file nil 'raw)))
610     (buffer-disable-undo (current-buffer))
611     (let ((delim (concat "^" rmail-unix-mail-delimiter))
612           (marker (concat "\n" nnfolder-article-marker))
613           (number "[0-9]+")
614           (active (car (cdr (assoc nnfolder-current-group 
615                                    nnfolder-group-alist))))
616           activenumber activemin start end)
617       (goto-char (point-min))
618       ;;
619       ;; Anytime the active number is 1 or 0, it is supect.  In that case,
620       ;; search the file manually to find the active number.  Or, of course,
621       ;; if we're being paranoid.  (This would also be the place to build
622       ;; other lists from the header markers, such as expunge lists, etc., if
623       ;; we ever desired to abandon the active file entirely for mboxes.)
624       (setq activenumber (cdr active))
625       (if (or nnfolder-ignore-active-file
626               (< activenumber 2))
627           (progn
628             (setq activemin (max (1- (lsh 1 23)) 
629                                  (1- (lsh 1 24)) 
630                                  (1- (lsh 1 25))))
631             (while (and (search-forward marker nil t)
632                         (re-search-forward number nil t))
633               (let ((newnum (string-to-number (buffer-substring
634                                                (match-beginning 0)
635                                                (match-end 0)))))
636                 (setq activenumber (max activenumber newnum))
637                 (setq activemin (min activemin newnum))))
638             (setcar active (min activemin activenumber))
639             (setcdr active (max activenumber (cdr active)))
640             (goto-char (point-min))))
641
642       ;; Keep track of the active number on our own, and insert it back into
643       ;; the active list when we're done. Also, prime the pump to cut down on
644       ;; the number of searches we do.
645       (setq end (point-marker))
646       (set-marker end (or (and (re-search-forward delim nil t)
647                                (match-beginning 0))
648                           (point-max)))
649       (while (not (= end (point-max)))
650         (setq start (marker-position end))
651         (goto-char end)
652         ;; There may be more than one "From " line, so we skip past
653         ;; them.  
654         (while (looking-at delim) 
655           (forward-line 1))
656         (set-marker end (or (and (re-search-forward delim nil t)
657                                  (match-beginning 0))
658                             (point-max)))
659         (goto-char start)
660         (if (not (search-forward marker end t))
661             (progn
662               (narrow-to-region start end)
663               (nnmail-insert-lines)
664               (nnfolder-insert-newsgroup-line
665                (cons nil (nnfolder-active-number nnfolder-current-group)))
666               (widen))))
667
668       ;; Make absolutely sure that the active list reflects reality!
669       (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
670       (current-buffer))))
671
672 (provide 'nnfolder)
673
674 ;;; nnfolder.el ends here