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