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